crypto: Erase key from RAM after storing into enclave

When storing a new unique secret key in the secure enclave, it is
temporarily stored in a stack buffer accessible by CPU1.
Since it is a secret key, it should not be kept in memory as it could
be leaked.

This commit calls the explicit_bzero() function from the libc to ensure
that the buffer containing the key is cleared. Unlike with bzero() and
memset(), the compiler won't optimize away calls to explicit_bzero().
This commit is contained in:
Hugo Grostabussiat
2023-02-05 19:22:31 +01:00
committed by Willy-JL
parent 512bba335b
commit be8387afb5
2 changed files with 4 additions and 0 deletions

View File

@@ -276,6 +276,7 @@ void crypto_cli_store_key(Cli* cli, FuriString* args) {
} }
} while(0); } while(0);
explicit_bzero(data, sizeof(data));
furi_string_free(key_type); furi_string_free(key_type);
} }

View File

@@ -80,9 +80,11 @@ static bool furi_hal_crypto_generate_unique_keys(uint8_t start_slot, uint8_t end
key.data = key_data; key.data = key_data;
furi_hal_random_fill_buf(key_data, 32); furi_hal_random_fill_buf(key_data, 32);
if(!furi_hal_crypto_store_add_key(&key, &slot)) { if(!furi_hal_crypto_store_add_key(&key, &slot)) {
explicit_bzero(key_data, sizeof(key_data));
FURI_LOG_E(TAG, "Error writing key to slot %u", slot); FURI_LOG_E(TAG, "Error writing key to slot %u", slot);
return false; return false;
} }
explicit_bzero(key_data, sizeof(key_data));
} }
return true; return true;
} }
@@ -176,6 +178,7 @@ bool furi_hal_crypto_store_add_key(FuriHalCryptoKey* key, uint8_t* slot) {
memcpy(pParam.KeyData, key->data, key_data_size); memcpy(pParam.KeyData, key->data, key_data_size);
SHCI_CmdStatus_t shci_state = SHCI_C2_FUS_StoreUsrKey(&pParam, slot); SHCI_CmdStatus_t shci_state = SHCI_C2_FUS_StoreUsrKey(&pParam, slot);
explicit_bzero(&pParam, sizeof(pParam));
furi_check(furi_mutex_release(furi_hal_crypto_mutex) == FuriStatusOk); furi_check(furi_mutex_release(furi_hal_crypto_mutex) == FuriStatusOk);
return (shci_state == SHCI_Success); return (shci_state == SHCI_Success);
} }