diff --git a/wolfcrypt/src/pwdbased.c b/wolfcrypt/src/pwdbased.c index b2400a2a2..756b4e2c1 100644 --- a/wolfcrypt/src/pwdbased.c +++ b/wolfcrypt/src/pwdbased.c @@ -568,8 +568,6 @@ int wc_PKCS12_PBKDF_ex(byte* output, const byte* passwd, int passLen, /* (2^32 - 1) */ #define SCRYPT_WORD32_MAX 4294967295U -/* (2^32 - 1) * 32, used in a couple of scrypt max calculations. */ -#define SCRYPT_MAX 137438953440UL /* One round of Salsa20/8. * Code taken from RFC 7914: scrypt PBKDF. @@ -760,7 +758,13 @@ int wc_scrypt(byte* output, const byte* passwd, int passLen, if (cost < 1 || cost >= 128 * blockSize / 8 || parallel < 1 || dkLen < 1) return BAD_FUNC_ARG; - if ((word32)parallel > (SCRYPT_MAX / (128 * blockSize))) + /* The following comparison used to be: + * ((word32)parallel > (SCRYPT_MAX / (128 * blockSize))) + * where SCRYPT_MAX is (2^32 - 1) * 32. For some compilers, the RHS of + * the comparison is greater than parallel's type. It wouldn't promote + * both sides to word64. What follows is just arithmetic simplification. + */ + if ((word32)parallel > (SCRYPT_WORD32_MAX / (4 * blockSize))) return BAD_FUNC_ARG; bSz = 128 * blockSize;