@@ -215,28 +215,36 @@ int atecc_handler_inject_priv_key(int slot, uint8_t* priv_key){
215215 * \return ATCA_SUCCESS on success, otherwise an error code.
216216 */
217217int atecc_handler_write_data (int slot, uint8_t * data, size_t data_len) {
218- if (data_len % 32 != 0 || !data ) {
219- return ATCA_BAD_PARAM; // Validate data and length
218+ if (!data || data_len == 0 ) {
219+ return ATCA_BAD_PARAM;
220220 }
221221
222- ATCA_STATUS status = atcab_wakeup (); // Wake up the device
222+ ATCA_STATUS status = atcab_wakeup ();
223223 if (status != ATCA_SUCCESS) {
224224 return status;
225225 }
226226
227- // Ensure the config zone is locked and slot allows writing
228- uint8_t config_data[128 ];
229- status = atecc_handler_read_configuration (config_data);
230- if (status != ATCA_SUCCESS) return status;
227+ // Check both config and data zone locks
228+ if (check_lock_zone (LOCK_ZONE_CONFIG) == ATCA_NOT_LOCKED ||
229+ check_lock_zone (LOCK_ZONE_DATA) == ATCA_NOT_LOCKED) {
230+ return ATCA_EXECUTION_ERROR;
231+ }
231232
232- std::bitset<8 > slotConfig_H = config_data[21 + (slot * 2 )];
233- if (!slotConfig_H[6 ] || check_lock_zone (LOCK_ZONE_CONFIG) == ATCA_NOT_LOCKED) {
234- return ATCA_EXECUTION_ERROR; // Ensure write is allowed
233+ // Calculate the number of blocks and any remaining bytes
234+ size_t num_blocks = data_len / 32 ;
235+ size_t remaining_bytes = data_len % 32 ;
236+
237+ // Write full blocks
238+ for (size_t i = 0 ; i < num_blocks; i++) {
239+ status = atcab_write_zone (ATCA_ZONE_DATA, slot, i, 0 , &data[i * 32 ], 32 );
240+ if (status != ATCA_SUCCESS) {
241+ return status;
242+ }
235243 }
236244
237- // Write data in 32-byte chunks
238- for ( size_t offset = 0 ; offset < data_len; offset += 32 ) {
239- status = atcab_write_bytes_zone (ATCA_ZONE_DATA, slot, offset, &data[offset ], 32 );
245+ // Write remaining bytes if any
246+ if (remaining_bytes > 0 ) {
247+ status = atcab_write_zone (ATCA_ZONE_DATA, slot, num_blocks, 0 , &data[num_blocks * 32 ], remaining_bytes );
240248 if (status != ATCA_SUCCESS) {
241249 return status;
242250 }
@@ -253,28 +261,36 @@ int atecc_handler_write_data(int slot, uint8_t* data, size_t data_len) {
253261 * \return ATCA_SUCCESS on success, otherwise an error code.
254262 */
255263int atecc_handler_read_data (int slot, uint8_t * data, size_t data_len) {
256- if (data_len % 32 != 0 || !data ) {
257- return ATCA_BAD_PARAM; // Validate data and length
264+ if (!data || data_len == 0 ) {
265+ return ATCA_BAD_PARAM;
258266 }
259267
260- ATCA_STATUS status = atcab_wakeup (); // Wake up the device
268+ ATCA_STATUS status = atcab_wakeup ();
261269 if (status != ATCA_SUCCESS) {
262270 return status;
263271 }
264272
265- // Ensure the config zone is locked and slot allows reading
266- uint8_t config_data[128 ];
267- status = atecc_handler_read_configuration (config_data);
268- if (status != ATCA_SUCCESS) return status;
273+ // Check both config and data zone locks
274+ if (check_lock_zone (LOCK_ZONE_CONFIG) == ATCA_NOT_LOCKED ||
275+ check_lock_zone (LOCK_ZONE_DATA) == ATCA_NOT_LOCKED) {
276+ return ATCA_EXECUTION_ERROR;
277+ }
269278
270- std::bitset<8 > slotConfig_H = config_data[21 + (slot * 2 )];
271- if (!slotConfig_H[6 ] || check_lock_zone (LOCK_ZONE_CONFIG) == ATCA_NOT_LOCKED) {
272- return ATCA_EXECUTION_ERROR; // Ensure read is allowed
279+ // Calculate the number of blocks and any remaining bytes
280+ size_t num_blocks = data_len / 32 ;
281+ size_t remaining_bytes = data_len % 32 ;
282+
283+ // Read full blocks
284+ for (size_t i = 0 ; i < num_blocks; i++) {
285+ status = atcab_read_zone (ATCA_ZONE_DATA, slot, i, 0 , &data[i * 32 ], 32 );
286+ if (status != ATCA_SUCCESS) {
287+ return status;
288+ }
273289 }
274290
275- // Read data in 32-byte chunks
276- for ( size_t offset = 0 ; offset < data_len; offset += 32 ) {
277- status = atcab_read_bytes_zone (ATCA_ZONE_DATA, slot, offset, &data[offset ], 32 );
291+ // Read remaining bytes if any
292+ if (remaining_bytes > 0 ) {
293+ status = atcab_read_zone (ATCA_ZONE_DATA, slot, num_blocks, 0 , &data[num_blocks * 32 ], remaining_bytes );
278294 if (status != ATCA_SUCCESS) {
279295 return status;
280296 }
0 commit comments