Update Chacha ENCRYPT_BLOCK_SIZE to match IV ctr

pull/167/head
Daniele Lacamera 2022-02-07 16:35:25 +01:00
parent b7026a5b1c
commit 61275ec9dd
5 changed files with 8 additions and 9 deletions

View File

@ -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

View File

@ -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) */

View File

@ -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

View File

@ -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);
}

View File

@ -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)