@@ -206,9 +206,10 @@ static inline int fp_truncate(FILE *f, size_t len)
206206#define ENC_AES128 2
207207#define ENC_AES256 3
208208
209- #define ENC_BLOCK_SIZE 16
210- #define ENC_MAX_KEY_SZ 32
211- #define ENC_MAX_IV_SZ 16
209+ /* Use algorithm-specific constants from wolfboot.h */
210+ #define ENC_MAX_BLOCK_SZ ENCRYPT_BLOCK_SIZE_CHACHA /* 64 - largest block size */
211+ #define ENC_MAX_KEY_SZ ENCRYPT_KEY_SIZE_AES256 /* 32 */
212+ #define ENC_MAX_IV_SZ ENCRYPT_NONCE_SIZE_AES /* 16 */
212213
213214static void header_append_u32 (uint8_t * header , uint32_t * idx , uint32_t tmp32 )
214215{
@@ -1761,21 +1762,24 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
17611762
17621763 if ((CMD .encrypt != ENC_OFF ) && CMD .encrypt_key_file ) {
17631764 uint8_t key [ENC_MAX_KEY_SZ ], iv [ENC_MAX_IV_SZ ];
1764- uint8_t enc_buf [ENC_BLOCK_SIZE ];
1765- int ivSz , keySz ;
1765+ uint8_t enc_buf [ENC_MAX_BLOCK_SZ ];
1766+ int ivSz , keySz , encBlockSz ;
17661767 uint32_t fsize = 0 ;
17671768 switch (CMD .encrypt ) {
17681769 case ENC_CHACHA :
1769- ivSz = CHACHA_IV_BYTES ;
1770- keySz = CHACHA_MAX_KEY_SZ ;
1770+ ivSz = ENCRYPT_NONCE_SIZE_CHACHA ;
1771+ keySz = ENCRYPT_KEY_SIZE_CHACHA ;
1772+ encBlockSz = ENCRYPT_BLOCK_SIZE_CHACHA ;
17711773 break ;
17721774 case ENC_AES128 :
1773- ivSz = 16 ;
1774- keySz = 16 ;
1775+ ivSz = ENCRYPT_NONCE_SIZE_AES ;
1776+ keySz = ENCRYPT_KEY_SIZE_AES128 ;
1777+ encBlockSz = ENCRYPT_BLOCK_SIZE_AES ;
17751778 break ;
17761779 case ENC_AES256 :
1777- ivSz = 16 ;
1778- keySz = 32 ;
1780+ ivSz = ENCRYPT_NONCE_SIZE_AES ;
1781+ keySz = ENCRYPT_KEY_SIZE_AES256 ;
1782+ encBlockSz = ENCRYPT_BLOCK_SIZE_AES ;
17791783 break ;
17801784 default :
17811785 printf ("No valid encryption mode selected\n" );
@@ -1819,9 +1823,9 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
18191823#endif
18201824 wc_Chacha_SetKey (& cha , key , sizeof (key ));
18211825 wc_Chacha_SetIV (& cha , iv , 0 );
1822- for (pos = 0 ; pos < fsize ; pos += ENC_BLOCK_SIZE ) {
1826+ for (pos = 0 ; pos < fsize ; pos += encBlockSz ) {
18231827 int fread_retval ;
1824- fread_retval = (int )fread (buf , 1 , ENC_BLOCK_SIZE , f );
1828+ fread_retval = (int )fread (buf , 1 , encBlockSz , f );
18251829 if ((fread_retval == 0 ) && feof (f )) {
18261830 break ;
18271831 }
@@ -1832,14 +1836,14 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
18321836 Aes aes_e ;
18331837 wc_AesInit (& aes_e , NULL , 0 );
18341838 wc_AesSetKeyDirect (& aes_e , key , keySz , iv , AES_ENCRYPTION );
1835- for (pos = 0 ; pos < fsize ; pos += ENC_BLOCK_SIZE ) {
1839+ for (pos = 0 ; pos < fsize ; pos += encBlockSz ) {
18361840 int fread_retval ;
1837- fread_retval = (int )fread (buf , 1 , ENC_BLOCK_SIZE , f );
1841+ fread_retval = (int )fread (buf , 1 , encBlockSz , f );
18381842 if ((fread_retval == 0 ) && feof (f )) {
18391843 break ;
18401844 }
18411845 /* Pad with FF if input is too short */
1842- while ((fread_retval % ENC_BLOCK_SIZE ) != 0 ) {
1846+ while ((fread_retval % encBlockSz ) != 0 ) {
18431847 buf [fread_retval ++ ] = 0xFF ;
18441848 }
18451849 wc_AesCtrEncrypt (& aes_e , enc_buf , buf , fread_retval );
0 commit comments