Skip to content

Commit 0568743

Browse files
authored
Merge pull request #10880 from dgarske/stm32_bare2
Fix STM32 DHUK AES-CBC in-place decrypt chaining IV and correct stale CTR comment
2 parents 54694f9 + e9aebd2 commit 0568743

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

wolfcrypt/src/aes.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7437,9 +7437,9 @@ int wc_AesCbcEncrypt(Aes* aes, byte* out, const byte* in, word32 sz)
74377437
xorbufout(out, in, ks, WC_AES_BLOCK_SIZE);
74387438
}
74397439
else {
7440-
/* The XTRANSFORM_AESCTRBLOCK macro discards this return; zero
7441-
* the block so a failed HW ECB does not leave stale/prior
7442-
* plaintext in the output. */
7440+
/* The CTR loop breaks on this non-zero return; zero the block
7441+
* so a failed HW ECB does not leave stale/prior plaintext in
7442+
* the output. */
74437443
ForceZero(out, WC_AES_BLOCK_SIZE);
74447444
}
74457445
ForceZero(ks, sizeof(ks));

wolfcrypt/src/port/st/stm32.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3729,6 +3729,21 @@ static int Stm32Dhuk_Cipher(struct wc_CryptoInfo* info)
37293729
if (ret != 0) {
37303730
return ret;
37313731
}
3732+
/* CBC requires a non-zero, block-multiple length. Validate before the
3733+
* pre-decrypt last-block copy below so an invalid sz cannot read past
3734+
* the input buffer (mirrors the bare backend wc_Stm32_Aes_Cbc). */
3735+
if (info->cipher.aescbc.sz == 0 ||
3736+
(info->cipher.aescbc.sz % WC_AES_BLOCK_SIZE) != 0) {
3737+
return BAD_FUNC_ARG;
3738+
}
3739+
if (!info->cipher.enc) {
3740+
/* In-place decrypt overwrites the last ciphertext block, so
3741+
* capture it for the next chaining IV before the decrypt (mirrors
3742+
* the non-DHUK bare backend wc_Stm32_Aes_Cbc). */
3743+
XMEMCPY(info->cipher.aescbc.aes->tmp,
3744+
info->cipher.aescbc.in + info->cipher.aescbc.sz
3745+
- WC_AES_BLOCK_SIZE, WC_AES_BLOCK_SIZE);
3746+
}
37323747
ret = Stm32Dhuk_Aes(NULL, WC_DHUK_MODE_CBC, info->cipher.enc,
37333748
info->cipher.aescbc.in, info->cipher.aescbc.sz,
37343749
info->cipher.aescbc.out,
@@ -3743,8 +3758,7 @@ static int Stm32Dhuk_Cipher(struct wc_CryptoInfo* info)
37433758
}
37443759
else {
37453760
XMEMCPY(info->cipher.aescbc.aes->reg,
3746-
info->cipher.aescbc.in + info->cipher.aescbc.sz
3747-
- WC_AES_BLOCK_SIZE, WC_AES_BLOCK_SIZE);
3761+
info->cipher.aescbc.aes->tmp, WC_AES_BLOCK_SIZE);
37483762
}
37493763
}
37503764
return ret;

0 commit comments

Comments
 (0)