Skip to content

Commit 80a0f6b

Browse files
committed
RSA PKCS#1.5 verify: bounds check input
As long as NO_RSA_BOUNDS_CHECK is not defined, the input range is checked for verification.
1 parent 2354ea1 commit 80a0f6b

3 files changed

Lines changed: 20 additions & 16 deletions

File tree

wolfcrypt/src/rsa.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3115,8 +3115,7 @@ int cc310_RsaSSL_Verify(const byte* in, word32 inLen, byte* sig,
31153115
#endif /* WOLFSSL_CRYPTOCELL */
31163116

31173117
#ifndef WOLF_CRYPTO_CB_ONLY_RSA
3118-
#if !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(TEST_UNPAD_CONSTANT_TIME) && \
3119-
!defined(NO_RSA_BOUNDS_CHECK)
3118+
#if !defined(NO_RSA_BOUNDS_CHECK)
31203119
/* Check that 1 < in < n-1. (Requirement of 800-56B.) */
31213120
int RsaFunctionCheckIn(const byte* in, word32 inLen, RsaKey* key,
31223121
int checkSmallCt)
@@ -3158,8 +3157,7 @@ int RsaFunctionCheckIn(const byte* in, word32 inLen, RsaKey* key,
31583157

31593158
return ret;
31603159
}
3161-
#endif /* !WOLFSSL_RSA_VERIFY_ONLY && !TEST_UNPAD_CONSTANT_TIME &&
3162-
* !NO_RSA_BOUNDS_CHECK */
3160+
#endif /* !NO_RSA_BOUNDS_CHECK */
31633161
#endif /* WOLF_CRYPTO_CB_ONLY_RSA */
31643162

31653163
static int wc_RsaFunction_ex(const byte* in, word32 inLen, byte* out,
@@ -3228,6 +3226,17 @@ static int wc_RsaFunction_ex(const byte* in, word32 inLen, byte* out,
32283226
}
32293227
#endif /* !WOLFSSL_RSA_VERIFY_ONLY && !TEST_UNPAD_CONSTANT_TIME && \
32303228
* !NO_RSA_BOUNDS_CHECK */
3229+
#if !defined(NO_RSA_BOUNDS_CHECK)
3230+
if (type == RSA_PUBLIC_DECRYPT &&
3231+
key->state == RSA_STATE_DECRYPT_EXPTMOD) {
3232+
3233+
ret = RsaFunctionCheckIn(in, inLen, key, checkSmallCt);
3234+
if (ret != 0) {
3235+
RESTORE_VECTOR_REGISTERS();
3236+
return ret;
3237+
}
3238+
}
3239+
#endif
32313240

32323241
#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_RSA)
32333242
if (key->asyncDev.marker == WOLFSSL_ASYNC_MARKER_RSA &&

wolfcrypt/src/sp_int.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5579,7 +5579,7 @@ int sp_abs(const sp_int* a, sp_int* r)
55795579
#endif /* WOLFSSL_SP_INT_NEGATIVE */
55805580

55815581
#if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_DH) || defined(HAVE_ECC) || \
5582-
(!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY))
5582+
!defined(NO_RSA)
55835583
/* Compare absolute value of two multi-precision numbers.
55845584
*
55855585
* @param [in] a SP integer.
@@ -5662,9 +5662,7 @@ int sp_cmp_mag(const sp_int* a, const sp_int* b)
56625662
#endif
56635663

56645664
#if defined(WOLFSSL_SP_MATH_ALL) || defined(HAVE_ECC) || !defined(NO_DSA) || \
5665-
defined(OPENSSL_EXTRA) || !defined(NO_DH) || \
5666-
(!defined(NO_RSA) && (!defined(WOLFSSL_RSA_VERIFY_ONLY) || \
5667-
defined(WOLFSSL_KEY_GEN)))
5665+
defined(OPENSSL_EXTRA) || !defined(NO_DH) || !defined(NO_RSA)
56685666
/* Compare two multi-precision numbers.
56695667
*
56705668
* Assumes a and b are not NULL.
@@ -5706,9 +5704,8 @@ static int _sp_cmp(const sp_int* a, const sp_int* b)
57065704
}
57075705
#endif
57085706

5709-
#if (!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
5710-
!defined(NO_DSA) || defined(HAVE_ECC) || !defined(NO_DH) || \
5711-
defined(WOLFSSL_SP_MATH_ALL)
5707+
#if !defined(NO_RSA) || !defined(NO_DSA) || defined(HAVE_ECC) || \
5708+
!defined(NO_DH) || defined(WOLFSSL_SP_MATH_ALL)
57125709
/* Compare two multi-precision numbers.
57135710
*
57145711
* Pointers are compared such that NULL is less than not NULL.
@@ -6197,9 +6194,8 @@ int sp_set_int(sp_int* a, unsigned long n)
61976194
}
61986195
#endif /* WOLFSSL_SP_MATH_ALL || !NO_RSA */
61996196

6200-
#if defined(WOLFSSL_SP_MATH_ALL) || \
6201-
(!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
6202-
!defined(NO_DH) || defined(HAVE_ECC)
6197+
#if defined(WOLFSSL_SP_MATH_ALL) || !defined(NO_RSA) || !defined(NO_DH) || \
6198+
defined(HAVE_ECC)
62036199
/* Compare a one digit number with a multi-precision number.
62046200
*
62056201
* When a is NULL, MP_LT is returned.

wolfssl/wolfcrypt/settings.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2942,8 +2942,7 @@ extern void uITRON4_free(void *p) ;
29422942
/* Determine when mp_add_d is required. */
29432943
#if !defined(NO_PWDBASED) || defined(WOLFSSL_KEY_GEN) || !defined(NO_DH) || \
29442944
!defined(NO_DSA) || defined(HAVE_ECC) || \
2945-
(!defined(NO_RSA) && !defined(WOLFSSL_RSA_VERIFY_ONLY)) || \
2946-
defined(OPENSSL_EXTRA)
2945+
!defined(NO_RSA) || defined(OPENSSL_EXTRA)
29472946
#define WOLFSSL_SP_ADD_D
29482947
#endif
29492948

0 commit comments

Comments
 (0)