@@ -1269,6 +1269,8 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
12691269 int ret = -1 ;
12701270 uint8_t buf [4096 ];
12711271 uint8_t second_buf [4096 ];
1272+ uint8_t key [ENC_MAX_KEY_SZ ];
1273+ uint8_t iv [ENC_MAX_IV_SZ ];
12721274 uint32_t read_sz , pos ;
12731275 uint8_t digest [48 ]; /* max digest */
12741276 uint32_t digest_sz = 0 ;
@@ -1277,6 +1279,9 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
12771279 uint8_t * cert_chain = NULL ;
12781280 uint32_t cert_chain_sz = 0 ;
12791281
1282+ XMEMSET (key , 0 , sizeof (key ));
1283+ XMEMSET (iv , 0 , sizeof (iv ));
1284+
12801285 /* Check certificate chain file size before allocating header, and adjust
12811286 * header size if needed */
12821287 if (CMD .cert_chain_file != NULL ) {
@@ -1992,7 +1997,6 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
19921997 }
19931998
19941999 if (!CMD .header_only && (CMD .encrypt != ENC_OFF ) && CMD .encrypt_key_file ) {
1995- uint8_t key [ENC_MAX_KEY_SZ ], iv [ENC_MAX_IV_SZ ];
19962000 uint8_t enc_buf [ENC_MAX_BLOCK_SZ ];
19972001 int ivSz , keySz , encBlockSz ;
19982002 uint32_t fsize = 0 ;
@@ -2021,19 +2025,20 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
20212025 if (fek == NULL ) {
20222026 fprintf (stderr , "Open encryption key file %s: %s\n" ,
20232027 CMD .encrypt_key_file , strerror (errno ));
2024- exit ( 1 ) ;
2028+ goto failure ;
20252029 }
20262030 ret = (int )fread (key , 1 , keySz , fek );
20272031 if (ret != keySz ) {
20282032 fprintf (stderr , "Error reading key from %s\n" , CMD .encrypt_key_file );
2029- exit ( 1 ) ;
2033+ goto failure ;
20302034 }
20312035 ret = (int )fread (iv , 1 , ivSz , fek );
20322036 if (ret != ivSz ) {
20332037 fprintf (stderr , "Error reading IV from %s\n" , CMD .encrypt_key_file );
2034- exit ( 1 ) ;
2038+ goto failure ;
20352039 }
20362040 fclose (fek );
2041+ fek = NULL ;
20372042
20382043 fef = fopen (CMD .output_encrypted_image_file , "wb" );
20392044 if (!fef ) {
@@ -2051,7 +2056,8 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
20512056#ifndef HAVE_CHACHA
20522057 fprintf (stderr , "Encryption not supported: chacha support not found"
20532058 "in wolfssl configuration.\n" );
2054- exit (100 );
2059+ ret = 100 ;
2060+ goto failure ;
20552061#endif
20562062 wc_Chacha_SetKey (& cha , key , sizeof (key ));
20572063 wc_Chacha_SetIV (& cha , iv , 0 );
@@ -2083,6 +2089,7 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
20832089 }
20842090 }
20852091 fclose (fef );
2092+ fef = NULL ;
20862093 printf ("Encryption complete.\n" );
20872094 }
20882095 printf ("Output image(s) successfully created.\n" );
@@ -2094,6 +2101,12 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
20942101 fclose (f );
20952102 }
20962103failure :
2104+ wc_ForceZero (key , sizeof (key ));
2105+ wc_ForceZero (iv , sizeof (iv ));
2106+ if (fek )
2107+ fclose (fek );
2108+ if (fef )
2109+ fclose (fef );
20972110 if (cert_chain )
20982111 free (cert_chain );
20992112 if (policy )
0 commit comments