Skip to content

Commit fd79ba7

Browse files
committed
Add Capability to store different types of keys or seed in atec
Signed-off-by: cybnon <stefan.weber93@googlemail.com>
1 parent 40749d8 commit fd79ba7

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

include/atecc608_handler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,5 @@ extern "C" int atecc_handler_sign(int slot, const uint8_t * msg, uint8_t * signa
8686
extern "C" int atecc_handler_verify(int slot, const uint8_t * msg, const uint8_t * signature, const uint8_t * pub_key);
8787
extern "C" int atecc_handler_genkey(int slot, uint8_t * pub_key);
8888
extern "C" int atecc_handler_lock_slot(int slot);
89+
extern "C" int atecc_handler_write_data(int slot, uint8_t* data, size_t data_len);
90+
extern "C" int atecc_handler_read_data(int slot, uint8_t* data, size_t data_len);

src/atecc608_handler.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,92 @@ int atecc_handler_inject_priv_key(int slot, uint8_t* priv_key){
194194
return status;
195195
}
196196

197+
/** save data in slot
198+
* \param[in] slot slot number to which data is to be written
199+
* \param[in] data data byte array to write
200+
* \param[in] data_len length of the data byte array
201+
* \return ATCA_SUCCESS on success, otherwise an error code.
202+
*/
203+
int atecc_handler_write_data(int slot, uint8_t* data, size_t data_len) {
204+
ATCA_STATUS status = ATCA_GEN_FAIL;
205+
uint8_t config_data[128];
206+
207+
// Wake up the device
208+
status = atcab_wakeup();
209+
if (status != ATCA_SUCCESS) {
210+
return status;
211+
}
212+
213+
// Read the configuration zone
214+
status = atecc_handler_read_configuration(config_data);
215+
if (status != ATCA_SUCCESS) {
216+
return status;
217+
}
218+
219+
/* Check if writing is allowed for the given slot */
220+
std::bitset<8> slotConfig_H = config_data[21 + (slot * 2)];
221+
if (!slotConfig_H[6]) { // Example condition; adapt as necessary
222+
return ATCA_EXECUTION_ERROR;
223+
}
224+
225+
/* Config Zone should be locked for this process */
226+
status = check_lock_zone(LOCK_ZONE_CONFIG);
227+
if (status == ATCA_NOT_LOCKED) {
228+
return status;
229+
}
230+
231+
// Write data to the specified slot
232+
status = atcab_write_bytes_zone(ATCA_ZONE_DATA, slot, 0, data, data_len);
233+
if (status != ATCA_SUCCESS) {
234+
return status;
235+
}
236+
237+
return ATCA_SUCCESS;
238+
}
239+
240+
/* read data from slot
241+
* \param[in] slot slot number from which data is to be read
242+
* \param[out] data buffer to store the read data
243+
* \param[in] data_len length of the data byte array
244+
* \return ATCA_SUCCESS on success, otherwise an error code.
245+
*/
246+
int atecc_handler_read_data(int slot, uint8_t* data, size_t data_len) {
247+
ATCA_STATUS status = ATCA_GEN_FAIL;
248+
uint8_t config_data[128];
249+
250+
// Wake up the device
251+
status = atcab_wakeup();
252+
if (status != ATCA_SUCCESS) {
253+
return status;
254+
}
255+
256+
// Read the configuration zone
257+
status = atecc_handler_read_configuration(config_data);
258+
if (status != ATCA_SUCCESS) {
259+
return status;
260+
}
261+
262+
/* Check if reading is allowed for the given slot */
263+
std::bitset<8> slotConfig_H = config_data[21 + (slot * 2)];
264+
if (!slotConfig_H[6]) { // Example condition; adapt as necessary
265+
return ATCA_EXECUTION_ERROR;
266+
}
267+
268+
/* Config Zone should be locked for this process */
269+
status = check_lock_zone(LOCK_ZONE_CONFIG);
270+
if (status == ATCA_NOT_LOCKED) {
271+
return status;
272+
}
273+
274+
// Read data from the specified slot
275+
status = atcab_read_bytes_zone(ATCA_ZONE_DATA, slot, 0, data, data_len);
276+
if (status != ATCA_SUCCESS) {
277+
return status;
278+
}
279+
280+
return ATCA_SUCCESS;
281+
}
282+
197283
/** \brief Initialize atecc object and bus
198284
* \param[in] slot slot number of key to be written
199285
* \param[in] pub_key public key will be written here

0 commit comments

Comments
 (0)