Skip to content

Commit d143187

Browse files
committed
securechip_random : multiple tries
secure_chip random function uses atca library for randomness, but this call to library may not be successful and error codes might be returned by atca. In this case, firmware was also calling `Abort()`. This stops the usb connection and terminates the program. Should it happen during a factory reset, then it means the device is left in a half-reset state. That's why it is a good practice to try this atca library call for randomness multiple times, making it possible to avoid rare unexpected errors from atca random implementation. Furthermore, the primary factory reset functions `securechip_update_keys` and `securechip_u2f_counter_set` are also retried to avoid unexpected errors in them, quitting the execution half-reset. Signed-off-by: asi345 <[email protected]>
1 parent 6ca99ff commit d143187

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/reset.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,25 @@ void reset_reset(bool status)
4848
{
4949
keystore_lock();
5050
#if !defined(TESTING)
51-
if (!securechip_update_keys()) {
51+
bool sc_result_update_keys = false;
52+
for (int retries = 0; retries < 5; retries++) {
53+
sc_result_update_keys = securechip_update_keys();
54+
if (sc_result_update_keys) {
55+
break;
56+
}
57+
}
58+
if (!sc_result_update_keys) {
5259
Abort("Could not reset secure chip.");
5360
}
5461
#if APP_U2F == 1
55-
if (!securechip_u2f_counter_set(0)) {
62+
bool sc_result_u2f_counter_set = false;
63+
for (int retries = 0; retries < 5; retries++) {
64+
sc_result_u2f_counter_set = securechip_u2f_counter_set(0);
65+
if (sc_result_u2f_counter_set) {
66+
break;
67+
}
68+
}
69+
if (!sc_result_u2f_counter_set) {
5670
Abort("Could not initialize U2F counter.");
5771
}
5872
#endif

src/securechip/securechip.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,12 @@ bool securechip_monotonic_increments_remaining(uint32_t* remaining_out)
605605

606606
bool securechip_random(uint8_t* rand_out)
607607
{
608-
return atcab_random(rand_out) == ATCA_SUCCESS;
608+
for (int retries = 0; retries < 5; retries++) {
609+
if (atcab_random(rand_out) == ATCA_SUCCESS) {
610+
return true;
611+
}
612+
}
613+
return false;
609614
}
610615

611616
// Length of priv_key must be 32 bytes

0 commit comments

Comments
 (0)