From 86abe793d75923afefe7e7d55acb3fe5a98e6bba Mon Sep 17 00:00:00 2001 From: Ruby Martin Date: Tue, 16 Sep 2025 11:03:21 -0600 Subject: [PATCH 1/2] address undefined shift behavior and overflow --- wolfcrypt/src/pwdbased.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/wolfcrypt/src/pwdbased.c b/wolfcrypt/src/pwdbased.c index 8c7c64cae..9a255874c 100644 --- a/wolfcrypt/src/pwdbased.c +++ b/wolfcrypt/src/pwdbased.c @@ -816,9 +816,16 @@ int wc_scrypt(byte* output, const byte* passwd, int passLen, ret = MEMORY_E; goto end; } + + /* Check that (1 << cost) * bSz won't overflow or exceed allowed max */ + if (((size_t)1 << cost) * (size_t)bSz > SCRYPT_WORD32_MAX) { + ret = BAD_FUNC_ARG; + goto end; + } + /* Temporary for scryptROMix. */ - v = (byte*)XMALLOC((size_t)((1U << cost) * bSz), NULL, - DYNAMIC_TYPE_TMP_BUFFER); + v = (byte*)XMALLOC(((size_t)1 << cost) * (size_t)bSz, NULL, + DYNAMIC_TYPE_TMP_BUFFER); if (v == NULL) { ret = MEMORY_E; goto end; @@ -841,7 +848,8 @@ int wc_scrypt(byte* output, const byte* passwd, int passLen, /* Step 2. */ for (i = 0; i < parallel; i++) - scryptROMix(blocks + i * (int)bSz, v, y, (int)blockSize, 1U << cost); + scryptROMix(blocks + i * (int)bSz, v, y, (int)blockSize, + (word32)((size_t)1 << cost)); /* Step 3. */ ret = wc_PBKDF2(output, passwd, passLen, blocks, (int)blocksSz, 1, dkLen, From a8fca08b7e810c4f2a0165ca3977267c6967fa0a Mon Sep 17 00:00:00 2001 From: Ruby Martin Date: Tue, 16 Sep 2025 11:04:43 -0600 Subject: [PATCH 2/2] add edge case unit test where cost=22, block=8 --- wolfcrypt/test/test.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 5664f7b97..6b70be4b7 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -27553,6 +27553,11 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t scrypt_test(void) return WC_TEST_RET_ENC_EC(ret); if (XMEMCMP(derived, verify4, sizeof(verify4)) != 0) return WC_TEST_RET_ENC_NC; + + ret = wc_scrypt(derived,(byte*)"pleaseletmein", 13, + (byte*)"SodiumChloride", 14, 22, 8, 1, sizeof(derived)); + if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG)) + return WC_TEST_RET_ENC_EC(ret); #endif #else #ifdef SCRYPT_TEST_ALL