From f860f4bf81be9bd8d09607a83ac9e4eaf3a174ac Mon Sep 17 00:00:00 2001 From: Aidan Keefe Date: Wed, 2 Sep 2026 15:22:43 -0600 Subject: [PATCH] Added cheap bounds checking: Check that the bottm 3 bits of any input size are 0. --- wolfcrypt/src/des3.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/wolfcrypt/src/des3.c b/wolfcrypt/src/des3.c index f816a5fe96..39a7b5ee90 100644 --- a/wolfcrypt/src/des3.c +++ b/wolfcrypt/src/des3.c @@ -881,6 +881,11 @@ static WC_INLINE void wc_Stm32_CrypDesBlock(const byte* in, byte* out) } #endif + if (sz & (DES_BLOCK_SIZE - 1)) { + WOLFSSL_MSG("Buffer length was not a multiple of DES block size"); + return BAD_LENGTH_E; + } + while (len > 0) { XMEMCPY(temp_block, in + offset, DES_BLOCK_SIZE); @@ -926,6 +931,11 @@ static WC_INLINE void wc_Stm32_CrypDesBlock(const byte* in, byte* out) } #endif + if (sz & (DES_BLOCK_SIZE - 1)) { + WOLFSSL_MSG("Buffer length was not a multiple of DES block size"); + return BAD_LENGTH_E; + } + while (len > 0) { XMEMCPY(temp_block, in + offset, DES_BLOCK_SIZE); @@ -973,6 +983,11 @@ static WC_INLINE void wc_Stm32_CrypDesBlock(const byte* in, byte* out) } #endif + if (sz & (DES_BLOCK_SIZE - 1)) { + WOLFSSL_MSG("Buffer length was not a multiple of DES block size"); + return BAD_LENGTH_E; + } + while (len > 0) { XMEMCPY(temp_block, in + offset, DES_BLOCK_SIZE); @@ -1023,6 +1038,11 @@ static WC_INLINE void wc_Stm32_CrypDesBlock(const byte* in, byte* out) } #endif + if (sz & (DES_BLOCK_SIZE - 1)) { + WOLFSSL_MSG("Buffer length was not a multiple of DES block size"); + return BAD_LENGTH_E; + } + while (len > 0) { XMEMCPY(temp_block, in + offset, DES_BLOCK_SIZE); @@ -1073,6 +1093,11 @@ static WC_INLINE void wc_Stm32_CrypDesBlock(const byte* in, byte* out) } #endif + if (sz & (DES_BLOCK_SIZE - 1)) { + WOLFSSL_MSG("Buffer length was not a multiple of DES block size"); + return BAD_LENGTH_E; + } + while (len > 0) { XMEMCPY(temp_block, in + offset, DES_BLOCK_SIZE); @@ -1110,6 +1135,11 @@ static WC_INLINE void wc_Stm32_CrypDesBlock(const byte* in, byte* out) } #endif + if (sz & (DES_BLOCK_SIZE - 1)) { + WOLFSSL_MSG("Buffer length was not a multiple of DES block size"); + return BAD_LENGTH_E; + } + while (len > 0) { XMEMCPY(temp_block, in + offset, DES_BLOCK_SIZE); @@ -1149,6 +1179,11 @@ static WC_INLINE void wc_Stm32_CrypDesBlock(const byte* in, byte* out) return MISSING_KEY; } + if (sz & (DES_BLOCK_SIZE - 1)) { + WOLFSSL_MSG("Buffer length was not a multiple of DES block size"); + return BAD_LENGTH_E; + } + #ifdef FREESCALE_MMCAU_CLASSIC if ((wc_ptr_t)out % WOLFSSL_MMCAU_ALIGNMENT) { WOLFSSL_MSG("Bad 3ede cau_des_encrypt alignment"); @@ -1199,6 +1234,11 @@ static WC_INLINE void wc_Stm32_CrypDesBlock(const byte* in, byte* out) return MISSING_KEY; } + if (sz & (DES_BLOCK_SIZE - 1)) { + WOLFSSL_MSG("Buffer length was not a multiple of DES block size"); + return BAD_LENGTH_E; + } + #ifdef FREESCALE_MMCAU_CLASSIC if ((wc_ptr_t)out % WOLFSSL_MMCAU_ALIGNMENT) { WOLFSSL_MSG("Bad 3ede cau_des_decrypt alignment");