Skip to content

Commit fc8584d

Browse files
committed
wolfcrypt/src/pwdbased.c:
* fix typography of wc_PBKDF_max_iterations_set() and wc_PBKDF_max_iterations_get() (peer review). * refactor overflow prevention in wc_PKCS12_PBKDF_ex() to use WC_SAFE_SUM_UNSIGNED(). wolfcrypt/test/test.c: in pwdbased_test(), omit "INT_MAX MAC iterations" test if WOLFSSL_NO_MALLOC (uses wc_PKCS12_new_ex()).
1 parent c7b5e9a commit fc8584d

2 files changed

Lines changed: 12 additions & 19 deletions

File tree

wolfcrypt/src/pwdbased.c

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656

5757
static int current_wc_pbkdf_max_iterations = WC_PBKDF_DEFAULT_MAX_ITERATIONS;
5858

59-
int wc_PBKDF_max_iterations_set(int iters) {
59+
int wc_PBKDF_max_iterations_set(int iters)
60+
{
6061
if (iters <= 0)
6162
return BAD_FUNC_ARG;
6263
else {
@@ -66,7 +67,8 @@ int wc_PBKDF_max_iterations_set(int iters) {
6667
}
6768
}
6869

69-
int wc_PBKDF_max_iterations_get(void) {
70+
int wc_PBKDF_max_iterations_get(void)
71+
{
7072
return current_wc_pbkdf_max_iterations;
7173
}
7274

@@ -475,22 +477,17 @@ int wc_PKCS12_PBKDF_ex(byte* output, const byte* passwd, int passLen,
475477
* must be 1 or greater here and is always 'true' */
476478
pLen = v * (((word32)passLen + v - 1) / v);
477479

478-
/* Guard against overflow in iLen = sLen + pLen and totalLen = dLen + iLen.
479-
* Individual sLen/pLen values fit in word32 (max 0x80000000 for INT_MAX
480-
* inputs), but their sum can overflow. */
481-
if (sLen > 0xFFFFFFFFU - pLen) {
480+
if (! WC_SAFE_SUM_UNSIGNED(word32, sLen, pLen, iLen)) {
482481
WC_FREE_VAR_EX(Ai, heap, DYNAMIC_TYPE_TMP_BUFFER);
483482
WC_FREE_VAR_EX(B, heap, DYNAMIC_TYPE_TMP_BUFFER);
484483
return BAD_FUNC_ARG;
485484
}
486-
iLen = sLen + pLen;
487485

488-
if (iLen > 0xFFFFFFFFU - dLen) {
486+
if (! WC_SAFE_SUM_UNSIGNED(word32, dLen, sLen, totalLen)) {
489487
WC_FREE_VAR_EX(Ai, heap, DYNAMIC_TYPE_TMP_BUFFER);
490488
WC_FREE_VAR_EX(B, heap, DYNAMIC_TYPE_TMP_BUFFER);
491489
return BAD_FUNC_ARG;
492490
}
493-
totalLen = dLen + sLen + pLen;
494491

495492
if (totalLen > sizeof(staticBuffer)) {
496493
buffer = (byte*)XMALLOC(totalLen, heap, DYNAMIC_TYPE_KEY);
@@ -694,19 +691,14 @@ int wc_PKCS12_PBKDF_ex(byte* output, const byte* passwd, int passLen,
694691
/* RFC 7292 B.2 step 3: P = password repeated to ceil(passLen/v)*v bytes */
695692
pLen = v * (((word32)passLen + v - 1) / v);
696693

697-
/* Guard against overflow in iLen = sLen + pLen and totalLen = v + iLen.
698-
* Individual sLen/pLen values fit in word32 (max 0x80000000 for INT_MAX
699-
* inputs), but their sum can overflow. */
700-
if (sLen > 0xFFFFFFFFU - pLen) {
694+
/* RFC 7292 B.2 step 4: I = S || P */
695+
if (! WC_SAFE_SUM_UNSIGNED(word32, sLen, pLen, iLen)) {
701696
return BAD_FUNC_ARG;
702697
}
703-
/* RFC 7292 B.2 step 4: I = S || P */
704-
iLen = sLen + pLen;
705698

706-
if (iLen > 0xFFFFFFFFU - v) {
699+
if (! WC_SAFE_SUM_UNSIGNED(word32, v, iLen, totalLen)) {
707700
return BAD_FUNC_ARG;
708701
}
709-
totalLen = v + iLen;
710702

711703
nwc = v / (word32)sizeof(PKCS12_WORD);
712704
nBlocks = iLen / v;

wolfcrypt/test/test.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31964,7 +31964,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pwdbased_test(void)
3196431964
return ret;
3196531965
#endif
3196631966
#if defined(HAVE_PKCS12) && !defined(NO_ASN) && !defined(NO_PWDBASED) && \
31967-
!defined(NO_HMAC) && !defined(NO_CERTS)
31967+
!defined(NO_HMAC) && !defined(NO_CERTS) && !defined(WOLFSSL_NO_MALLOC)
3196831968
/* Test that a crafted PKCS#12 with INT_MAX MAC iterations is rejected
3196931969
* immediately rather than hanging in DoPKCS12Hash(). */
3197031970
{
@@ -32009,7 +32009,8 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pwdbased_test(void)
3200932009
}
3201032010
ret = 0;
3201132011
}
32012-
#endif /* HAVE_PKCS12 && !NO_ASN && !NO_PWDBASED && !NO_HMAC && !NO_CERTS */
32012+
#endif /* HAVE_PKCS12 && !NO_ASN && !NO_PWDBASED && !NO_HMAC && !NO_CERTS && */
32013+
/* !WOLFSSL_NO_MALLOC */
3201332014
#ifdef HAVE_SCRYPT
3201432015
ret = scrypt_test();
3201532016
if (ret != 0)

0 commit comments

Comments
 (0)