diff --git a/config/examples/stm32wb-uart-flash-encryption.config b/config/examples/stm32wb-uart-flash-encryption.config index bef5700f..4b130939 100644 --- a/config/examples/stm32wb-uart-flash-encryption.config +++ b/config/examples/stm32wb-uart-flash-encryption.config @@ -21,6 +21,7 @@ DUALBANK_SWAP?=0 IMAGE_HEADER_SIZE?=256 PKA?=0 ENCRYPT=1 +ENCRYPT_WITH_CHACHA=1 WOLFTPM?=0 WOLFBOOT_PARTITION_SIZE?=0x20000 WOLFBOOT_SECTOR_SIZE?=0x1000 diff --git a/include/encrypt.h b/include/encrypt.h index 9967f3d7..0ce389cc 100644 --- a/include/encrypt.h +++ b/include/encrypt.h @@ -57,10 +57,10 @@ extern Aes aes_dec, aes_enc; #define crypto_init() aes_init() #define crypto_encrypt(eb,b,sz) wc_AesCtrEncrypt(&aes_enc, eb, b, sz) #define crypto_decrypt(db,b,sz) wc_AesCtrEncrypt(&aes_dec, db, b, sz) -#define crypto_set_iv(n, iv) aes_set_iv(n, iv) +#define crypto_set_iv(n,a) aes_set_iv(n, a) int aes_init(void); -void aes_set_iv(uint8_t *nonce, uint32_t iv_ctr); +void aes_set_iv(uint8_t *nonce, uint32_t address); #endif /* ENCRYPT_WITH_CHACHA */ /* Internal read/write functions (not exported in the libwolfboot API) */ diff --git a/include/wolfboot/wolfboot.h b/include/wolfboot/wolfboot.h index 2133b9ce..5f7f487d 100644 --- a/include/wolfboot/wolfboot.h +++ b/include/wolfboot/wolfboot.h @@ -131,7 +131,7 @@ int wolfBoot_dualboot_candidate(void); /* Hashing function configuration */ #if defined(WOLFBOOT_HASH_SHA256) -# define WOLFBOOT_SHA_BLOCK_SIZE (16) +# define WOLFBOOT_SHA_BLOCK_SIZE (256) # define WOLFBOOT_SHA_HDR HDR_SHA256 # define WOLFBOOT_SHA_DIGEST_SIZE (32) # define image_hash image_sha256 @@ -148,15 +148,17 @@ int wolfBoot_dualboot_candidate(void); #ifdef EXT_ENCRYPTED -#define ENCRYPT_BLOCK_SIZE 16 /* Encryption support */ #if defined(ENCRYPT_WITH_CHACHA) + #define ENCRYPT_BLOCK_SIZE 64 #define ENCRYPT_KEY_SIZE 32 /* Chacha20 - 256bit */ #define ENCRYPT_NONCE_SIZE 12 /* 96 bit*/ #elif defined(ENCRYPT_WITH_AES128) + #define ENCRYPT_BLOCK_SIZE 16 #define ENCRYPT_KEY_SIZE 16 /* AES128 */ #define ENCRYPT_NONCE_SIZE 16 /* AES IV size */ #elif defined(ENCRYPT_WITH_AES256) + #define ENCRYPT_BLOCK_SIZE 16 #define ENCRYPT_KEY_SIZE 32 /* AES256 */ #define ENCRYPT_NONCE_SIZE 16 /* AES IV size */ #else diff --git a/src/libwolfboot.c b/src/libwolfboot.c index f2351ca3..adf7c35b 100644 --- a/src/libwolfboot.c +++ b/src/libwolfboot.c @@ -871,7 +871,6 @@ static inline uint8_t part_address(uintptr_t a) int ext_flash_encrypt_write(uintptr_t address, const uint8_t *data, int len) { - uint32_t iv_counter; uint8_t block[ENCRYPT_BLOCK_SIZE]; uint8_t part; int sz = len; @@ -913,12 +912,10 @@ int ext_flash_encrypt_write(uintptr_t address, const uint8_t *data, int len) address += step; data += step; sz -= step; - iv_counter++; } for (i = 0; i < sz / ENCRYPT_BLOCK_SIZE; i++) { XMEMCPY(block, data + (ENCRYPT_BLOCK_SIZE * i), ENCRYPT_BLOCK_SIZE); crypto_encrypt(ENCRYPT_CACHE + (ENCRYPT_BLOCK_SIZE * i), block, ENCRYPT_BLOCK_SIZE); - iv_counter++; } return ext_flash_write(address, ENCRYPT_CACHE, len); } diff --git a/tools/keytools/sign.py b/tools/keytools/sign.py index f3016b0c..3739e7bb 100755 --- a/tools/keytools/sign.py +++ b/tools/keytools/sign.py @@ -548,13 +548,12 @@ if (encrypt): key = ekeyfile.read(32) iv_nonce = ekeyfile.read(12) cha = ciphers.ChaCha(key, 32) + cha.set_iv(iv_nonce, 0) while True: - cha.set_iv(iv_nonce, off) buf = outfile.read(16) if len(buf) == 0: break enc_outfile.write(cha.encrypt(buf)) - off += 1 elif aes128: key = ekeyfile.read(16) iv = ekeyfile.read(16)