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