Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/atecc608_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,5 @@ extern "C" int atecc_handler_sign(int slot, const uint8_t * msg, uint8_t * signa
extern "C" int atecc_handler_verify(int slot, const uint8_t * msg, const uint8_t * signature, const uint8_t * pub_key);
extern "C" int atecc_handler_genkey(int slot, uint8_t * pub_key);
extern "C" int atecc_handler_lock_slot(int slot);
extern "C" int atecc_handler_write_data(int slot, uint8_t* data, size_t data_len);
extern "C" int atecc_handler_read_data(int slot, uint8_t* data, size_t data_len);
86 changes: 86 additions & 0 deletions src/atecc608_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,92 @@ int atecc_handler_inject_priv_key(int slot, uint8_t* priv_key){
return status;
}

/** save data in slot
* \param[in] slot slot number to which data is to be written
* \param[in] data data byte array to write
* \param[in] data_len length of the data byte array
* \return ATCA_SUCCESS on success, otherwise an error code.
*/
int atecc_handler_write_data(int slot, uint8_t* data, size_t data_len) {
ATCA_STATUS status = ATCA_GEN_FAIL;
uint8_t config_data[128];

// Wake up the device
status = atcab_wakeup();
if (status != ATCA_SUCCESS) {
return status;
}

// Read the configuration zone
status = atecc_handler_read_configuration(config_data);
if (status != ATCA_SUCCESS) {
return status;
}

/* Check if writing is allowed for the given slot */
std::bitset<8> slotConfig_H = config_data[21 + (slot * 2)];
if (!slotConfig_H[6]) { // Example condition; adapt as necessary
return ATCA_EXECUTION_ERROR;
}

/* Config Zone should be locked for this process */
status = check_lock_zone(LOCK_ZONE_CONFIG);
if (status == ATCA_NOT_LOCKED) {
return status;
}

// Write data to the specified slot
status = atcab_write_bytes_zone(ATCA_ZONE_DATA, slot, 0, data, data_len);
if (status != ATCA_SUCCESS) {
return status;
}

return ATCA_SUCCESS;
}

/* read data from slot
* \param[in] slot slot number from which data is to be read
* \param[out] data buffer to store the read data
* \param[in] data_len length of the data byte array
* \return ATCA_SUCCESS on success, otherwise an error code.
*/
int atecc_handler_read_data(int slot, uint8_t* data, size_t data_len) {
ATCA_STATUS status = ATCA_GEN_FAIL;
uint8_t config_data[128];

// Wake up the device
status = atcab_wakeup();
if (status != ATCA_SUCCESS) {
return status;
}

// Read the configuration zone
status = atecc_handler_read_configuration(config_data);
if (status != ATCA_SUCCESS) {
return status;
}

/* Check if reading is allowed for the given slot */
std::bitset<8> slotConfig_H = config_data[21 + (slot * 2)];
if (!slotConfig_H[6]) { // Example condition; adapt as necessary
return ATCA_EXECUTION_ERROR;
}

/* Config Zone should be locked for this process */
status = check_lock_zone(LOCK_ZONE_CONFIG);
if (status == ATCA_NOT_LOCKED) {
return status;
}

// Read data from the specified slot
status = atcab_read_bytes_zone(ATCA_ZONE_DATA, slot, 0, data, data_len);
if (status != ATCA_SUCCESS) {
return status;
}

return ATCA_SUCCESS;
}

/** \brief Initialize atecc object and bus
* \param[in] slot slot number of key to be written
* \param[in] pub_key public key will be written here
Expand Down