Skip to content

Commit 5d9c608

Browse files
Merge pull request #9195 from rlm2002/zd20508
address undefined shift behavior and overflow
2 parents f143dbb + a8fca08 commit 5d9c608

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

wolfcrypt/src/pwdbased.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -816,9 +816,16 @@ int wc_scrypt(byte* output, const byte* passwd, int passLen,
816816
ret = MEMORY_E;
817817
goto end;
818818
}
819+
820+
/* Check that (1 << cost) * bSz won't overflow or exceed allowed max */
821+
if (((size_t)1 << cost) * (size_t)bSz > SCRYPT_WORD32_MAX) {
822+
ret = BAD_FUNC_ARG;
823+
goto end;
824+
}
825+
819826
/* Temporary for scryptROMix. */
820-
v = (byte*)XMALLOC((size_t)((1U << cost) * bSz), NULL,
821-
DYNAMIC_TYPE_TMP_BUFFER);
827+
v = (byte*)XMALLOC(((size_t)1 << cost) * (size_t)bSz, NULL,
828+
DYNAMIC_TYPE_TMP_BUFFER);
822829
if (v == NULL) {
823830
ret = MEMORY_E;
824831
goto end;
@@ -841,7 +848,8 @@ int wc_scrypt(byte* output, const byte* passwd, int passLen,
841848

842849
/* Step 2. */
843850
for (i = 0; i < parallel; i++)
844-
scryptROMix(blocks + i * (int)bSz, v, y, (int)blockSize, 1U << cost);
851+
scryptROMix(blocks + i * (int)bSz, v, y, (int)blockSize,
852+
(word32)((size_t)1 << cost));
845853

846854
/* Step 3. */
847855
ret = wc_PBKDF2(output, passwd, passLen, blocks, (int)blocksSz, 1, dkLen,

wolfcrypt/test/test.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27555,6 +27555,11 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t scrypt_test(void)
2755527555
return WC_TEST_RET_ENC_EC(ret);
2755627556
if (XMEMCMP(derived, verify4, sizeof(verify4)) != 0)
2755727557
return WC_TEST_RET_ENC_NC;
27558+
27559+
ret = wc_scrypt(derived,(byte*)"pleaseletmein", 13,
27560+
(byte*)"SodiumChloride", 14, 22, 8, 1, sizeof(derived));
27561+
if (ret != WC_NO_ERR_TRACE(BAD_FUNC_ARG))
27562+
return WC_TEST_RET_ENC_EC(ret);
2755827563
#endif
2755927564
#else
2756027565
#ifdef SCRYPT_TEST_ALL

0 commit comments

Comments
 (0)