@@ -1710,6 +1710,7 @@ ChaCha chacha;
17101710
17111711int RAMFUNCTION chacha_init (void )
17121712{
1713+ int ret = 0 ;
17131714#ifdef CUSTOM_ENCRYPT_KEY
17141715 uint8_t stored_nonce [ENCRYPT_NONCE_SIZE ];
17151716 uint8_t key [ENCRYPT_KEY_SIZE ];
@@ -1718,9 +1719,9 @@ int RAMFUNCTION chacha_init(void)
17181719 uint8_t * key ;
17191720#endif
17201721#ifdef CUSTOM_ENCRYPT_KEY
1721- int ret = wolfBoot_get_encrypt_key (key , stored_nonce );
1722+ ret = wolfBoot_get_encrypt_key (key , stored_nonce );
17221723 if (ret != 0 )
1723- return ret ;
1724+ goto exit ;
17241725#else
17251726 #if defined(MMU ) || defined(UNIT_TEST )
17261727 key = ENCRYPT_KEY ;
@@ -1736,14 +1737,21 @@ int RAMFUNCTION chacha_init(void)
17361737
17371738 XMEMSET (& chacha , 0 , sizeof (chacha ));
17381739
1739- if (!encrypt_key_is_valid (key , ENCRYPT_KEY_SIZE ))
1740- return -1 ;
1740+ if (!encrypt_key_is_valid (key , ENCRYPT_KEY_SIZE )) {
1741+ ret = -1 ;
1742+ goto exit ;
1743+ }
17411744
17421745 XMEMCPY (encrypt_iv_nonce , stored_nonce , ENCRYPT_NONCE_SIZE );
17431746
17441747 wc_Chacha_SetKey (& chacha , key , ENCRYPT_KEY_SIZE );
17451748 encrypt_initialized = 1 ;
1746- return 0 ;
1749+ exit :
1750+ #ifdef CUSTOM_ENCRYPT_KEY
1751+ ForceZero (key , sizeof (key ));
1752+ ForceZero (stored_nonce , sizeof (stored_nonce ));
1753+ #endif
1754+ return ret ;
17471755}
17481756
17491757#elif defined(ENCRYPT_WITH_AES128 ) || defined(ENCRYPT_WITH_AES256 )
@@ -1762,6 +1770,7 @@ Aes aes_dec, aes_enc;
17621770int aes_init (void )
17631771{
17641772 int devId = INVALID_DEVID ;
1773+ int ret = 0 ;
17651774#if defined(CUSTOM_ENCRYPT_KEY ) && !defined(WOLFBOOT_RENESAS_TSIP )
17661775 uint8_t stored_nonce [ENCRYPT_NONCE_SIZE ];
17671776 uint8_t key [ENCRYPT_KEY_SIZE ];
@@ -1770,7 +1779,6 @@ int aes_init(void)
17701779 uint8_t * key ;
17711780#endif
17721781#ifdef WOLFBOOT_RENESAS_TSIP
1773- int ret ;
17741782 wrap_enc_key_t * enc_key ;
17751783 devId = RENESAS_DEVID + 1 ;
17761784 enc_key = (wrap_enc_key_t * )RENESAS_TSIP_INSTALLEDENCKEY_ADDR ;
@@ -1797,8 +1805,10 @@ int aes_init(void)
17971805 wc_AesInit (& aes_enc , NULL , devId );
17981806 wc_AesInit (& aes_dec , NULL , devId );
17991807
1800- if (!encrypt_key_is_valid (key , ENCRYPT_KEY_SIZE ))
1801- return -1 ;
1808+ if (!encrypt_key_is_valid (key , ENCRYPT_KEY_SIZE )) {
1809+ ret = -1 ;
1810+ goto exit ;
1811+ }
18021812
18031813#ifdef WOLFBOOT_RENESAS_TSIP
18041814 /* Unwrap key and get key index */
@@ -1810,7 +1820,8 @@ int aes_init(void)
18101820 enc_key -> encrypted_user_key , & aes_enc .ctx .tsip_keyIdx );
18111821#endif
18121822 if (ret != TSIP_SUCCESS ) {
1813- return -1 ;
1823+ ret = -1 ;
1824+ goto exit ;
18141825 }
18151826 /* set encryption key size */
18161827 aes_enc .ctx .keySize = ENCRYPT_KEY_SIZE ;
@@ -1831,7 +1842,12 @@ int aes_init(void)
18311842 XMEMCPY (encrypt_iv_nonce , stored_nonce , ENCRYPT_NONCE_SIZE );
18321843 encrypt_initialized = 1 ;
18331844
1834- return 0 ;
1845+ exit :
1846+ #if defined(CUSTOM_ENCRYPT_KEY ) && !defined(WOLFBOOT_RENESAS_TSIP )
1847+ ForceZero (key , sizeof (key ));
1848+ ForceZero (stored_nonce , sizeof (stored_nonce ));
1849+ #endif
1850+ return ret ;
18351851}
18361852
18371853/**
0 commit comments