@@ -175,6 +175,11 @@ static status_t clear(void) {
175175 return LAUNDERED_OTCRYPTO_OK ;
176176}
177177
178+ /**
179+ * Hardware wipe guard.
180+ */
181+ static void hmac_wipe_guard (uint32_t * dummy ) { (void )clear (); }
182+
178183/**
179184 * Write given key to HMAC HWIP.
180185 *
@@ -438,6 +443,8 @@ static status_t ensure_idle(void) {
438443static status_t oneshot (const uint32_t cfg , const hmac_key_t * key ,
439444 const otcrypto_const_byte_buf_t * msg ,
440445 size_t digest_wordlen , uint32_t * digest ) {
446+ uint32_t hw_cleanup_guard __attribute__((cleanup (hmac_wipe_guard ))) = 1 ;
447+
441448 // Check that the block is idle.
442449 HARDENED_TRY (ensure_idle ());
443450
@@ -476,7 +483,7 @@ static status_t oneshot(const uint32_t cfg, const hmac_key_t *key,
476483
477484 HARDENED_CHECK_EQ (kHardenedBoolTrue , OTCRYPTO_CHECK_BUF (msg ));
478485
479- return clear () ;
486+ return OTCRYPTO_OK ;
480487}
481488
482489/**
@@ -825,6 +832,8 @@ hardened_bool_t hmac_key_integrity_checksum_check(const hmac_key_t *key) {
825832}
826833
827834status_t hmac_update (hmac_ctx_t * ctx , const otcrypto_const_byte_buf_t * data ) {
835+ uint32_t hw_cleanup_guard __attribute__((cleanup (hmac_wipe_guard ))) = 1 ;
836+
828837 // If we don't have enough new bytes to fill a block, just update the partial
829838 // block and return.
830839 size_t block_bytelen = ctx -> msg_block_wordlen * sizeof (uint32_t );
@@ -877,11 +886,12 @@ status_t hmac_update(hmac_ctx_t *ctx, const otcrypto_const_byte_buf_t *data) {
877886
878887 HARDENED_CHECK_EQ (kHardenedBoolTrue , OTCRYPTO_CHECK_BUF (data ));
879888
880- // Clean up.
881- return clear ();
889+ return OTCRYPTO_OK ;
882890}
883891
884892status_t hmac_final (hmac_ctx_t * ctx , otcrypto_word32_buf_t * digest ) {
893+ uint32_t hw_cleanup_guard __attribute__((cleanup (hmac_wipe_guard ))) = 1 ;
894+
885895 // Restore context will restore the context and also hit start or continue
886896 // button as necessary.
887897 HARDENED_TRY (context_restore (ctx ));
@@ -908,6 +918,5 @@ status_t hmac_final(hmac_ctx_t *ctx, otcrypto_word32_buf_t *digest) {
908918
909919 HARDENED_CHECK_EQ (kHardenedBoolTrue , OTCRYPTO_CHECK_BUF (digest ));
910920
911- // Clean up.
912- return clear ();
921+ return OTCRYPTO_OK ;
913922}
0 commit comments