Skip to content

Commit 4110f37

Browse files
committed
[crypto] Wipe DMEM also in error cases
When an error occurs, HARDENED_TRY() immediately returns. As we also want to wipe the OTBN DMEM when an error happens, this commit adds HARDENED_TRY_WIPE_DMEM() which wipes DMEM on an error before returning. Signed-off-by: Pascal Nasahl <[email protected]> (cherry picked from commit a5952c3)
1 parent ccebaec commit 4110f37

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

sw/device/lib/crypto/impl/ecc/p256.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ status_t p256_sideload_keygen_start(void) {
176176
status_t p256_keygen_finalize(p256_masked_scalar_t *private_key,
177177
p256_point_t *public_key) {
178178
// Spin here waiting for OTBN to complete.
179-
HARDENED_TRY(otbn_busy_wait_for_done());
179+
HARDENED_TRY_WIPE_DMEM(otbn_busy_wait_for_done());
180180
HARDENED_CHECK_EQ(otbn_instruction_count_get(), kModeKeygenInsCnt);
181181

182182
// Read the masked private key from OTBN dmem.
@@ -200,7 +200,7 @@ status_t p256_keygen_finalize(p256_masked_scalar_t *private_key,
200200

201201
status_t p256_sideload_keygen_finalize(p256_point_t *public_key) {
202202
// Spin here waiting for OTBN to complete.
203-
HARDENED_TRY(otbn_busy_wait_for_done());
203+
HARDENED_TRY_WIPE_DMEM(otbn_busy_wait_for_done());
204204
HARDENED_CHECK_EQ(otbn_instruction_count_get(), kModeKeygenSideloadInsCnt);
205205

206206
// Read the public key from OTBN dmem.
@@ -277,7 +277,7 @@ status_t p256_ecdsa_sideload_sign_start(
277277
status_t p256_ecdsa_sign_finalize(p256_ecdsa_signature_t *result) {
278278
uint32_t ins_cnt;
279279
// Spin here waiting for OTBN to complete.
280-
HARDENED_TRY(otbn_busy_wait_for_done());
280+
HARDENED_TRY_WIPE_DMEM(otbn_busy_wait_for_done());
281281
ins_cnt = otbn_instruction_count_get();
282282
if (launder32(ins_cnt) == kModeEcdsaSignSideloadInsCnt) {
283283
HARDENED_CHECK_EQ(ins_cnt, kModeEcdsaSignSideloadInsCnt);
@@ -379,7 +379,7 @@ status_t p256_ecdh_start(p256_masked_scalar_t *private_key,
379379

380380
status_t p256_ecdh_finalize(p256_ecdh_shared_key_t *shared_key) {
381381
// Spin here waiting for OTBN to complete.
382-
HARDENED_TRY(otbn_busy_wait_for_done());
382+
HARDENED_TRY_WIPE_DMEM(otbn_busy_wait_for_done());
383383

384384
// Read the code indicating if the public key is valid.
385385
uint32_t ok;

sw/device/lib/crypto/impl/ecc/p384.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ status_t p384_keygen_start(void) {
217217
status_t p384_keygen_finalize(p384_masked_scalar_t *private_key,
218218
p384_point_t *public_key) {
219219
// Spin here waiting for OTBN to complete.
220-
HARDENED_TRY(otbn_busy_wait_for_done());
220+
HARDENED_TRY_WIPE_DMEM(otbn_busy_wait_for_done());
221221
HARDENED_CHECK_EQ(otbn_instruction_count_get(), kModeKeygenInsCnt);
222222

223223
// Read the masked private key from OTBN dmem.
@@ -253,7 +253,7 @@ status_t p384_sideload_keygen_start(void) {
253253

254254
status_t p384_sideload_keygen_finalize(p384_point_t *public_key) {
255255
// Spin here waiting for OTBN to complete.
256-
HARDENED_TRY(otbn_busy_wait_for_done());
256+
HARDENED_TRY_WIPE_DMEM(otbn_busy_wait_for_done());
257257
HARDENED_CHECK_EQ(otbn_instruction_count_get(), kModeKeygenSideloadInsCnt);
258258

259259
// Read the public key from OTBN dmem.
@@ -306,7 +306,7 @@ status_t p384_ecdsa_sideload_sign_start(
306306
status_t p384_ecdsa_sign_finalize(p384_ecdsa_signature_t *result) {
307307
uint32_t ins_cnt;
308308
// Spin here waiting for OTBN to complete.
309-
HARDENED_TRY(otbn_busy_wait_for_done());
309+
HARDENED_TRY_WIPE_DMEM(otbn_busy_wait_for_done());
310310
ins_cnt = otbn_instruction_count_get();
311311
if (launder32(ins_cnt) == kModeEcdsaSignSideloadInsCnt) {
312312
HARDENED_CHECK_EQ(ins_cnt, kModeEcdsaSignSideloadInsCnt);
@@ -402,7 +402,7 @@ status_t p384_ecdh_start(p384_masked_scalar_t *private_key,
402402

403403
status_t p384_ecdh_finalize(p384_ecdh_shared_key_t *shared_key) {
404404
// Spin here waiting for OTBN to complete.
405-
HARDENED_TRY(otbn_busy_wait_for_done());
405+
HARDENED_TRY_WIPE_DMEM(otbn_busy_wait_for_done());
406406

407407
// Read the status code out of DMEM (false if basic checks on the validity of
408408
// the signature and public key failed).

0 commit comments

Comments
 (0)