Skip to content

Commit 12d7f0a

Browse files
dgarskedanielinux
authored andcommitted
Improved encryption constants between the bootloader and the signing tool. Allows the sign tool to properly use the correct block size for each encryption algorithm (64 for ChaCha, 16 for AES).
1 parent 4673c82 commit 12d7f0a

File tree

2 files changed

+42
-26
lines changed

2 files changed

+42
-26
lines changed

include/wolfboot/wolfboot.h

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -499,21 +499,33 @@ int wolfBoot_dualboot_candidate_addr(void**);
499499
int wolfBoot_get_partition_state(uint8_t part, uint8_t *st);
500500

501501

502+
/* Encryption algorithm constants - always available for tools */
503+
#define ENCRYPT_BLOCK_SIZE_CHACHA 64
504+
#define ENCRYPT_BLOCK_SIZE_AES 16
505+
506+
#define ENCRYPT_KEY_SIZE_CHACHA 32 /* ChaCha20 - 256bit */
507+
#define ENCRYPT_KEY_SIZE_AES128 16 /* AES128 */
508+
#define ENCRYPT_KEY_SIZE_AES256 32 /* AES256 */
509+
510+
#define ENCRYPT_NONCE_SIZE_CHACHA 12 /* 96 bit */
511+
#define ENCRYPT_NONCE_SIZE_AES 16 /* AES IV size */
512+
513+
502514
#ifdef EXT_ENCRYPTED
503-
/* Encryption support */
515+
/* Encryption support - compile-time algorithm selection */
504516

505517
#if defined(ENCRYPT_WITH_CHACHA)
506-
#define ENCRYPT_BLOCK_SIZE 64
507-
#define ENCRYPT_KEY_SIZE 32 /* Chacha20 - 256bit */
508-
#define ENCRYPT_NONCE_SIZE 12 /* 96 bit*/
518+
#define ENCRYPT_BLOCK_SIZE ENCRYPT_BLOCK_SIZE_CHACHA
519+
#define ENCRYPT_KEY_SIZE ENCRYPT_KEY_SIZE_CHACHA
520+
#define ENCRYPT_NONCE_SIZE ENCRYPT_NONCE_SIZE_CHACHA
509521
#elif defined(ENCRYPT_WITH_AES128)
510-
#define ENCRYPT_BLOCK_SIZE 16
511-
#define ENCRYPT_KEY_SIZE 16 /* AES128 */
512-
#define ENCRYPT_NONCE_SIZE 16 /* AES IV size */
522+
#define ENCRYPT_BLOCK_SIZE ENCRYPT_BLOCK_SIZE_AES
523+
#define ENCRYPT_KEY_SIZE ENCRYPT_KEY_SIZE_AES128
524+
#define ENCRYPT_NONCE_SIZE ENCRYPT_NONCE_SIZE_AES
513525
#elif defined(ENCRYPT_WITH_AES256)
514-
#define ENCRYPT_BLOCK_SIZE 16
515-
#define ENCRYPT_KEY_SIZE 32 /* AES256 */
516-
#define ENCRYPT_NONCE_SIZE 16 /* AES IV size */
526+
#define ENCRYPT_BLOCK_SIZE ENCRYPT_BLOCK_SIZE_AES
527+
#define ENCRYPT_KEY_SIZE ENCRYPT_KEY_SIZE_AES256
528+
#define ENCRYPT_NONCE_SIZE ENCRYPT_NONCE_SIZE_AES
517529
#elif defined(ENCRYPT_PKCS11)
518530
#define ENCRYPT_BLOCK_SIZE ENCRYPT_PKCS11_BLOCK_SIZE
519531
/* In this case, the key ID is stored in flash rather than the key itself */

tools/keytools/sign.c

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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

213214
static 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

Comments
 (0)