Skip to content

Commit 86abe79

Browse files
committed
address undefined shift behavior and overflow
1 parent f0b35d1 commit 86abe79

1 file changed

Lines changed: 11 additions & 3 deletions

File tree

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,

0 commit comments

Comments
 (0)