3535#include <wolfssl/wolfcrypt/random.h>
3636#include <wolfssl/wolfcrypt/sha256.h>
3737#include <wolfssl/wolfcrypt/integer.h>
38- #include <wolfssl/wolfcrypt/memory.h>
3938
4039#if defined(WOLFBOOT_HASH_SHA384 )
4140#include <wolfssl/wolfcrypt/sha512.h>
6867#define WOLFBOOT_DICE_ERR_HW -3
6968#define WOLFBOOT_DICE_ERR_CRYPTO -4
7069
70+ static NOINLINEFUNCTION void wolfboot_dice_zeroize (void * ptr , size_t len )
71+ {
72+ volatile uint8_t * p = (volatile uint8_t * )ptr ;
73+ while (len -- > 0U ) {
74+ * p ++ = 0U ;
75+ }
76+ }
77+
7178#define COSE_LABEL_ALG 1
7279#define COSE_ALG_ES256 (-7)
7380
@@ -621,7 +628,7 @@ static int wolfboot_dice_derive_attestation_key(ecc_key *key,
621628 goto cleanup ;
622629 }
623630 /* CDI is no longer needed once the seed has been derived. */
624- wc_ForceZero (cdi , sizeof (cdi ));
631+ wolfboot_dice_zeroize (cdi , sizeof (cdi ));
625632
626633 if (wolfboot_dice_hkdf (seed , sizeof (seed ),
627634 (const uint8_t * )"WOLFBOOT-IAK" , 12 ,
@@ -630,7 +637,7 @@ static int wolfboot_dice_derive_attestation_key(ecc_key *key,
630637 goto cleanup ;
631638 }
632639 /* Seed is no longer needed once the private key material is derived. */
633- wc_ForceZero (seed , sizeof (seed ));
640+ wolfboot_dice_zeroize (seed , sizeof (seed ));
634641
635642 if (wolfboot_dice_fixup_priv (priv , sizeof (priv )) != 0 ) {
636643 goto cleanup ;
@@ -644,9 +651,9 @@ static int wolfboot_dice_derive_attestation_key(ecc_key *key,
644651 ret = 0 ;
645652
646653cleanup :
647- wc_ForceZero (priv , sizeof (priv ));
648- wc_ForceZero (seed , sizeof (seed ));
649- wc_ForceZero (cdi , sizeof (cdi ));
654+ wolfboot_dice_zeroize (priv , sizeof (priv ));
655+ wolfboot_dice_zeroize (seed , sizeof (seed ));
656+ wolfboot_dice_zeroize (cdi , sizeof (cdi ));
650657 return ret ;
651658}
652659
@@ -660,24 +667,32 @@ static int wolfboot_attest_get_private_key(ecc_key *key,
660667 {
661668 uint8_t priv [WOLFBOOT_DICE_KEY_LEN ];
662669 size_t priv_len = sizeof (priv );
670+ int ret = -1 ;
663671
664672 if (hal_attestation_get_iak_private_key (priv , & priv_len ) != 0 ) {
665- return -1 ;
673+ goto cleanup ;
666674 }
667675 if (priv_len != WOLFBOOT_DICE_KEY_LEN ) {
668- return -1 ;
676+ goto cleanup ;
669677 }
670678 if (wc_ecc_import_private_key_ex (priv , (word32 )priv_len , NULL , 0 ,
671679 key , ECC_SECP256R1 ) != 0 ) {
672- return -1 ;
680+ goto cleanup ;
673681 }
674- return 0 ;
682+ ret = 0 ;
683+
684+ cleanup :
685+ wolfboot_dice_zeroize (priv , sizeof (priv ));
686+ return ret ;
675687 }
676688#else
677- if (hal_uds_derive_key (uds , uds_len ) != 0 ) {
678- return -1 ;
689+ int ret = -1 ;
690+
691+ if (hal_uds_derive_key (uds , uds_len ) == 0 ) {
692+ ret = wolfboot_dice_derive_attestation_key (key , uds , uds_len , claims );
679693 }
680- return wolfboot_dice_derive_attestation_key (key , uds , uds_len , claims );
694+ wolfboot_dice_zeroize (uds , sizeof (uds ));
695+ return ret ;
681696#endif
682697}
683698
@@ -801,7 +816,10 @@ static int wolfboot_dice_sign_tbs(const uint8_t *tbs,
801816{
802817 ecc_key key ;
803818 WC_RNG rng ;
804- int ret ;
819+ int ret = WOLFBOOT_DICE_ERR_CRYPTO ;
820+ int wc_ret ;
821+ int key_inited = 0 ;
822+ int rng_inited = 0 ;
805823 uint8_t hash [SHA256_DIGEST_SIZE ];
806824 uint8_t der_sig [128 ];
807825 word32 der_sig_len = sizeof (der_sig );
@@ -815,16 +833,18 @@ static int wolfboot_dice_sign_tbs(const uint8_t *tbs,
815833 }
816834
817835 wc_ecc_init (& key );
836+ key_inited = 1 ;
818837 if (wolfboot_attest_get_private_key (& key , claims ) != 0 ) {
819- wc_ecc_free ( & key ) ;
820- return WOLFBOOT_DICE_ERR_HW ;
838+ ret = WOLFBOOT_DICE_ERR_HW ;
839+ goto cleanup ;
821840 }
822841
823842 (void )wc_ecc_set_deterministic (& key , 1 );
824843 if (wc_InitRng (& rng ) != 0 ) {
825- wc_ecc_free ( & key ) ;
826- return WOLFBOOT_DICE_ERR_HW ;
844+ ret = WOLFBOOT_DICE_ERR_HW ;
845+ goto cleanup ;
827846 }
847+ rng_inited = 1 ;
828848
829849 {
830850 wc_Sha256 sha ;
@@ -833,26 +853,35 @@ static int wolfboot_dice_sign_tbs(const uint8_t *tbs,
833853 wc_Sha256Final (& sha , hash );
834854 }
835855
836- ret = wc_ecc_sign_hash (hash , sizeof (hash ), der_sig , & der_sig_len , & rng , & key );
837- wc_FreeRng (& rng );
838- if (ret != 0 ) {
839- wc_ecc_free (& key );
840- return WOLFBOOT_DICE_ERR_CRYPTO ;
856+ wc_ret = wc_ecc_sign_hash (hash , sizeof (hash ), der_sig , & der_sig_len , & rng , & key );
857+ if (wc_ret != 0 ) {
858+ ret = WOLFBOOT_DICE_ERR_CRYPTO ;
859+ goto cleanup ;
841860 }
842861
843- ret = wc_ecc_sig_to_rs (der_sig , der_sig_len , r , & r_len , s , & s_len );
844- if (ret != 0 || r_len > sizeof (r ) || s_len > sizeof (s )) {
845- wc_ecc_free ( & key ) ;
846- return WOLFBOOT_DICE_ERR_CRYPTO ;
862+ wc_ret = wc_ecc_sig_to_rs (der_sig , der_sig_len , r , & r_len , s , & s_len );
863+ if (wc_ret != 0 || r_len > sizeof (r ) || s_len > sizeof (s )) {
864+ ret = WOLFBOOT_DICE_ERR_CRYPTO ;
865+ goto cleanup ;
847866 }
848867
849868 XMEMSET (sig , 0 , WOLFBOOT_DICE_SIG_LEN );
850869 XMEMCPY (sig + (sizeof (r ) - r_len ), r , r_len );
851870 XMEMCPY (sig + sizeof (r ) + (sizeof (s ) - s_len ), s , s_len );
852871 * sig_len = WOLFBOOT_DICE_SIG_LEN ;
872+ ret = WOLFBOOT_DICE_SUCCESS ;
853873
854- wc_ecc_free (& key );
855- return WOLFBOOT_DICE_SUCCESS ;
874+ cleanup :
875+ if (rng_inited ) {
876+ wc_FreeRng (& rng );
877+ }
878+ if (key_inited ) {
879+ wc_ecc_free (& key );
880+ wolfboot_dice_zeroize (& key , sizeof (key ));
881+ }
882+ wolfboot_dice_zeroize (hash , sizeof (hash ));
883+ wolfboot_dice_zeroize (der_sig , sizeof (der_sig ));
884+ return ret ;
856885}
857886
858887static int wolfboot_dice_build_token (uint8_t * token_buf ,
0 commit comments