Skip to content

Commit 3d9beb2

Browse files
dgarskedanielinux
authored andcommitted
Cleanup the AES CTR IV.
1 parent 2e5bbfe commit 3d9beb2

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

include/encrypt.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ int aes_init(void);
6565
void aes_set_iv(uint8_t *nonce, uint32_t address);
6666
#endif /* ENCRYPT_WITH_CHACHA */
6767

68-
/* Internal read/write functions (not exported in the libwolfboot API) */
68+
/* external flash encryption read/write functions */
6969
int ext_flash_encrypt_write(uintptr_t address, const uint8_t *data, int len);
7070
int ext_flash_decrypt_read(uintptr_t address, uint8_t *data, int len);
7171

src/libwolfboot.c

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,13 +1600,14 @@ int aes_init(void)
16001600
/* register AES crypto callback */
16011601
wc_CryptoCb_RegisterDevice(devId, wc_tsip_AesCipher, NULL);
16021602

1603-
/* AES_ENCRYPTION is used for both directions in CTR */
1604-
/* unwrapped key never leaves TSIP and is referenced by tsip_keyIdx */
1603+
/* AES_ENCRYPTION is used for both directions in CTR
1604+
* IV is set later with "wc_AesSetIV" */
16051605
wc_AesSetKeyDirect(&aes_enc, enc_key->encrypted_user_key,
1606-
ENCRYPT_KEY_SIZE, enc_key->initial_vector, AES_ENCRYPTION);
1606+
ENCRYPT_KEY_SIZE, NULL, AES_ENCRYPTION);
16071607
wc_AesSetKeyDirect(&aes_dec, enc_key->encrypted_user_key,
1608-
ENCRYPT_KEY_SIZE, enc_key->initial_vector, AES_ENCRYPTION);
1608+
ENCRYPT_KEY_SIZE, NULL, AES_ENCRYPTION);
16091609

1610+
/* set IV nonce use in aes_set_iv */
16101611
XMEMCPY(encrypt_iv_nonce, enc_key->initial_vector, ENCRYPT_NONCE_SIZE);
16111612
encrypt_initialized = 1;
16121613
}
@@ -1619,7 +1620,6 @@ int aes_init(void)
16191620
ENCRYPT_TMP_SECRET_OFFSET);
16201621
#endif
16211622
uint8_t ff[ENCRYPT_KEY_SIZE];
1622-
uint8_t iv_buf[ENCRYPT_NONCE_SIZE];
16231623
uint8_t* stored_nonce;
16241624

16251625
#ifdef NVM_FLASH_WRITEONCE
@@ -1641,11 +1641,13 @@ int aes_init(void)
16411641
if (XMEMCMP(key, ff, ENCRYPT_KEY_SIZE) == 0)
16421642
return -1;
16431643

1644+
/* AES_ENCRYPTION is used for both directions in CTR
1645+
* IV is set later with "wc_AesSetIV" */
1646+
wc_AesSetKeyDirect(&aes_enc, key, ENCRYPT_KEY_SIZE, NULL, AES_ENCRYPTION);
1647+
wc_AesSetKeyDirect(&aes_dec, key, ENCRYPT_KEY_SIZE, NULL, AES_ENCRYPTION);
1648+
1649+
/* set IV nonce use in aes_set_iv */
16441650
XMEMCPY(encrypt_iv_nonce, stored_nonce, ENCRYPT_NONCE_SIZE);
1645-
XMEMCPY(iv_buf, stored_nonce, ENCRYPT_NONCE_SIZE);
1646-
/* AES_ENCRYPTION is used for both directions in CTR */
1647-
wc_AesSetKeyDirect(&aes_enc, key, ENCRYPT_KEY_SIZE, iv_buf, AES_ENCRYPTION);
1648-
wc_AesSetKeyDirect(&aes_dec, key, ENCRYPT_KEY_SIZE, iv_buf, AES_ENCRYPTION);
16491651
encrypt_initialized = 1;
16501652
#endif
16511653
return 0;
@@ -1655,10 +1657,10 @@ int aes_init(void)
16551657
* @brief Set the AES initialization vector (IV) for CTR mode.
16561658
*
16571659
* This function sets the AES initialization vector (IV) for the Counter (CTR)
1658-
* mode encryption. It takes a 12-byte nonce and a 32-bit IV counter value to
1660+
* mode encryption. It takes a 16-byte nonce and a 32-bit IV counter value to
16591661
* construct the 16-byte IV used for encryption.
16601662
*
1661-
* @param nonce Pointer to the 12-byte nonce (IV) buffer.
1663+
* @param nonce Pointer to the 16-byte nonce (IV) buffer.
16621664
* @param iv_ctr The IV counter value.
16631665
*
16641666
*/
@@ -1751,7 +1753,8 @@ int RAMFUNCTION ext_flash_encrypt_write(uintptr_t address, const uint8_t *data,
17511753
int sz = len, i, step;
17521754
uint8_t part;
17531755
uint32_t iv_counter = 0;
1754-
#if defined(EXT_ENCRYPTED) && !defined(WOLFBOOT_SMALL_STACK) && !defined(NVM_FLASH_WRITEONCE)
1756+
#if defined(EXT_ENCRYPTED) && !defined(WOLFBOOT_SMALL_STACK) && \
1757+
!defined(NVM_FLASH_WRITEONCE)
17551758
uint8_t ENCRYPT_CACHE[NVM_CACHE_SIZE] XALIGNED_STACK(32);
17561759
#endif
17571760

@@ -1863,9 +1866,8 @@ int RAMFUNCTION ext_flash_decrypt_read(uintptr_t address, uint8_t *data, int len
18631866
crypto_set_iv(encrypt_iv_nonce, iv_counter);
18641867
break;
18651868
case PART_SWAP:
1866-
{
1867-
break;
1868-
}
1869+
break;
1870+
18691871
default:
18701872
return -1;
18711873
}
@@ -1908,7 +1910,7 @@ int RAMFUNCTION ext_flash_decrypt_read(uintptr_t address, uint8_t *data, int len
19081910
unaligned_trailer_size = read_remaining;
19091911
if (unaligned_trailer_size > 0)
19101912
{
1911-
uint8_t dec_block[ENCRYPT_BLOCK_SIZE];
1913+
uint8_t dec_block[ENCRYPT_BLOCK_SIZE] XALIGNED(4);
19121914
if (ext_flash_read(address, block, ENCRYPT_BLOCK_SIZE)
19131915
!= ENCRYPT_BLOCK_SIZE)
19141916
return -1;

0 commit comments

Comments
 (0)