@@ -1270,6 +1270,8 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
12701270 int ret = -1 ;
12711271 uint8_t buf [4096 ];
12721272 uint8_t second_buf [4096 ];
1273+ uint8_t key [ENC_MAX_KEY_SZ ];
1274+ uint8_t iv [ENC_MAX_IV_SZ ];
12731275 uint32_t read_sz , pos ;
12741276 uint8_t digest [48 ]; /* max digest */
12751277 uint32_t digest_sz = 0 ;
@@ -1278,6 +1280,9 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
12781280 uint8_t * cert_chain = NULL ;
12791281 uint32_t cert_chain_sz = 0 ;
12801282
1283+ XMEMSET (key , 0 , sizeof (key ));
1284+ XMEMSET (iv , 0 , sizeof (iv ));
1285+
12811286 /* Check certificate chain file size before allocating header, and adjust
12821287 * header size if needed */
12831288 if (CMD .cert_chain_file != NULL ) {
@@ -1993,7 +1998,6 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
19931998 }
19941999
19952000 if (!CMD .header_only && (CMD .encrypt != ENC_OFF ) && CMD .encrypt_key_file ) {
1996- uint8_t key [ENC_MAX_KEY_SZ ], iv [ENC_MAX_IV_SZ ];
19972001 uint8_t enc_buf [ENC_MAX_BLOCK_SZ ];
19982002 int ivSz , keySz , encBlockSz ;
19992003 uint32_t fsize = 0 ;
@@ -2022,19 +2026,20 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
20222026 if (fek == NULL ) {
20232027 fprintf (stderr , "Open encryption key file %s: %s\n" ,
20242028 CMD .encrypt_key_file , strerror (errno ));
2025- exit ( 1 ) ;
2029+ goto failure ;
20262030 }
20272031 ret = (int )fread (key , 1 , keySz , fek );
20282032 if (ret != keySz ) {
20292033 fprintf (stderr , "Error reading key from %s\n" , CMD .encrypt_key_file );
2030- exit ( 1 ) ;
2034+ goto failure ;
20312035 }
20322036 ret = (int )fread (iv , 1 , ivSz , fek );
20332037 if (ret != ivSz ) {
20342038 fprintf (stderr , "Error reading IV from %s\n" , CMD .encrypt_key_file );
2035- exit ( 1 ) ;
2039+ goto failure ;
20362040 }
20372041 fclose (fek );
2042+ fek = NULL ;
20382043
20392044 fef = fopen (CMD .output_encrypted_image_file , "wb" );
20402045 if (!fef ) {
@@ -2052,7 +2057,8 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
20522057#ifndef HAVE_CHACHA
20532058 fprintf (stderr , "Encryption not supported: chacha support not found"
20542059 "in wolfssl configuration.\n" );
2055- exit (100 );
2060+ ret = 100 ;
2061+ goto failure ;
20562062#endif
20572063 wc_Chacha_SetKey (& cha , key , sizeof (key ));
20582064 wc_Chacha_SetIV (& cha , iv , 0 );
@@ -2084,6 +2090,7 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
20842090 }
20852091 }
20862092 fclose (fef );
2093+ fef = NULL ;
20872094 printf ("Encryption complete.\n" );
20882095 }
20892096 printf ("Output image(s) successfully created.\n" );
@@ -2095,6 +2102,12 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
20952102 fclose (f );
20962103 }
20972104failure :
2105+ wc_ForceZero (key , sizeof (key ));
2106+ wc_ForceZero (iv , sizeof (iv ));
2107+ if (fek )
2108+ fclose (fek );
2109+ if (fef )
2110+ fclose (fef );
20982111 if (cert_chain )
20992112 free (cert_chain );
21002113 if (policy )
0 commit comments