Skip to content

Commit 674f1ff

Browse files
Fix issue in ECC test from size mismatch. Code review feedback.
1 parent 2398fcb commit 674f1ff

3 files changed

Lines changed: 38 additions & 24 deletions

File tree

wolfcrypt/src/ecc.c

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,14 @@ ECC Curve Sizes:
327327
#define MAX_ECC_BITS_USE MAX_ECC_BITS_NEEDED
328328
#endif
329329

330+
#if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
331+
#define ECC_DECL_MP_OVER_MAX(bits) \
332+
(MP_BITS_CNT(bits) > MP_BITS_CNT(MAX_ECC_BITS_USE))
333+
#else
334+
#define ECC_DECL_MP_OVER_MAX(bits) \
335+
((bits) > MAX_ECC_BITS_USE)
336+
#endif
337+
330338
#if !defined(WOLFSSL_CUSTOM_CURVES) && (ECC_MIN_KEY_SZ > 160) && \
331339
(!defined(HAVE_ECC_KOBLITZ) || (ECC_MIN_KEY_SZ > 224))
332340

@@ -2012,7 +2020,7 @@ static int _ecc_projective_add_point(ecc_point* P, ecc_point* Q, ecc_point* R,
20122020
mp_int *x, *y, *z;
20132021
int err;
20142022

2015-
if (mp_bitsused(modulus) > MAX_ECC_BITS_USE) {
2023+
if (ECC_DECL_MP_OVER_MAX(mp_bitsused(modulus))) {
20162024
return WC_KEY_SIZE_E;
20172025
}
20182026

@@ -2416,7 +2424,7 @@ static int _ecc_projective_dbl_point(ecc_point *P, ecc_point *R, mp_int* a,
24162424
mp_int *x, *y, *z;
24172425
int err;
24182426

2419-
if (mp_bitsused(modulus) > MAX_ECC_BITS_USE) {
2427+
if (ECC_DECL_MP_OVER_MAX(mp_bitsused(modulus))) {
24202428
return WC_KEY_SIZE_E;
24212429
}
24222430

@@ -2770,7 +2778,7 @@ int ecc_map_ex(ecc_point* P, mp_int* modulus, mp_digit mp, int ct)
27702778
#endif
27712779
mp_int *x, *y, *z;
27722780

2773-
if (mp_bitsused(modulus) > MAX_ECC_BITS_USE) {
2781+
if (ECC_DECL_MP_OVER_MAX(mp_bitsused(modulus))) {
27742782
return WC_KEY_SIZE_E;
27752783
}
27762784

@@ -3595,7 +3603,7 @@ static int ecc_point_to_mont(ecc_point* p, ecc_point* r, mp_int* modulus,
35953603

35963604
DECL_MP_INT_SIZE_DYN(mu, mp_bitsused(modulus), MAX_ECC_BITS_USE);
35973605

3598-
if (mp_bitsused(modulus) > MAX_ECC_BITS_USE) {
3606+
if (ECC_DECL_MP_OVER_MAX(mp_bitsused(modulus))) {
35993607
return WC_KEY_SIZE_E;
36003608
}
36013609

@@ -3903,8 +3911,8 @@ static int ecc_check_order_minus_1(const mp_int* k, ecc_point* tG, ecc_point* R,
39033911
int err;
39043912
DECL_MP_INT_SIZE_DYN(t, mp_bitsused(order), MAX_ECC_BITS_USE);
39053913

3906-
if (mp_bitsused(order) > MAX_ECC_BITS_USE ||
3907-
mp_bitsused(modulus) > MAX_ECC_BITS_USE) {
3914+
if (ECC_DECL_MP_OVER_MAX(mp_bitsused(order)) ||
3915+
ECC_DECL_MP_OVER_MAX(mp_bitsused(modulus))) {
39083916
return WC_KEY_SIZE_E;
39093917
}
39103918

@@ -6858,7 +6866,7 @@ int wc_ecc_sign_hash(const byte* in, word32 inlen, byte* out, word32 *outlen,
68586866
word32 keySz;
68596867
#endif
68606868

6861-
if (ECC_KEY_MAX_BITS(key) > MAX_ECC_BITS_USE) {
6869+
if (ECC_DECL_MP_OVER_MAX(ECC_KEY_MAX_BITS(key))) {
68626870
return WC_KEY_SIZE_E;
68636871
}
68646872

@@ -7063,7 +7071,7 @@ static int ecc_sign_hash_sw(ecc_key* key, ecc_key* pubkey, WC_RNG* rng,
70637071

70647072
DECL_MP_INT_SIZE_DYN(b, ECC_KEY_MAX_BITS_NONULLCHECK(key), MAX_ECC_BITS_USE);
70657073

7066-
if (ECC_KEY_MAX_BITS_NONULLCHECK(key) > MAX_ECC_BITS_USE) {
7074+
if (ECC_DECL_MP_OVER_MAX(ECC_KEY_MAX_BITS_NONULLCHECK(key))) {
70677075
return WC_KEY_SIZE_E;
70687076
}
70697077

@@ -7394,7 +7402,7 @@ int wc_ecc_sign_hash_ex(const byte* in, word32 inlen, WC_RNG* rng,
73947402
#else
73957403
DECLARE_CURVE_SPECS(1);
73967404
#endif
7397-
if (ECC_KEY_MAX_BITS(key) > MAX_ECC_BITS_USE) {
7405+
if (ECC_DECL_MP_OVER_MAX(ECC_KEY_MAX_BITS(key))) {
73987406
return WC_KEY_SIZE_E;
73997407
}
74007408
#endif /* !WOLFSSL_SP_MATH */
@@ -8317,7 +8325,7 @@ static int ecc_mont_norm_points(ecc_point* A, ecc_point* Am, ecc_point* B,
83178325

83188326
DECL_MP_INT_SIZE_DYN(mu, mp_bitsused(modulus), MAX_ECC_BITS_USE);
83198327

8320-
if (mp_bitsused(modulus) > MAX_ECC_BITS_USE) {
8328+
if (ECC_DECL_MP_OVER_MAX(mp_bitsused(modulus))) {
83218329
return WC_KEY_SIZE_E;
83228330
}
83238331

@@ -8707,7 +8715,7 @@ int wc_ecc_verify_hash(const byte* sig, word32 siglen, const byte* hash,
87078715
word32 keySz;
87088716
#endif
87098717

8710-
if (ECC_KEY_MAX_BITS(key) > MAX_ECC_BITS_USE) {
8718+
if (ECC_DECL_MP_OVER_MAX(ECC_KEY_MAX_BITS(key))) {
87118719
return WC_KEY_SIZE_E;
87128720
}
87138721

@@ -9095,7 +9103,7 @@ static int ecc_verify_hash(mp_int *r, mp_int *s, const byte* hash,
90959103
mp_int* u1 = NULL; /* Will be e. */
90969104
mp_int* u2 = NULL; /* Will be w. */
90979105

9098-
if (ECC_KEY_MAX_BITS_NONULLCHECK(key) > MAX_ECC_BITS_USE) {
9106+
if (ECC_DECL_MP_OVER_MAX(ECC_KEY_MAX_BITS_NONULLCHECK(key))) {
90999107
return WC_KEY_SIZE_E;
91009108
}
91019109

wolfcrypt/src/rsa.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ RSA keys can be used to encrypt, decrypt, sign and verify data.
4444
#include <wolfssl/wolfcrypt/rsa.h>
4545
#include <wolfssl/wolfcrypt/logging.h>
4646

47+
#if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
48+
#define RSA_DECL_MP_OVER_MAX(bits) \
49+
(MP_BITS_CNT(bits) > MP_BITS_CNT(RSA_MAX_SIZE))
50+
#else
51+
#define RSA_DECL_MP_OVER_MAX(bits) \
52+
((bits) > RSA_MAX_SIZE)
53+
#endif
54+
4755
#ifdef WOLFSSL_AFALG_XILINX_RSA
4856
#include <wolfssl/wolfcrypt/port/af_alg/wc_afalg.h>
4957
#endif
@@ -839,7 +847,7 @@ int wc_CheckRsaKey(RsaKey* key)
839847
}
840848
#endif
841849

842-
if (mp_bitsused(&key->n) > RSA_MAX_SIZE) {
850+
if (RSA_DECL_MP_OVER_MAX(mp_bitsused(&key->n))) {
843851
return WC_KEY_SIZE_E;
844852
}
845853

@@ -2879,7 +2887,7 @@ static int RsaFunctionPrivate(mp_int* tmp, RsaKey* key, WC_RNG* rng)
28792887
DECL_MP_INT_SIZE_DYN(rndi, mp_bitsused(&key->n), RSA_MAX_SIZE);
28802888
#endif /* WC_RSA_BLINDING && !WC_NO_RNG */
28812889

2882-
if (mp_bitsused(&key->n) > RSA_MAX_SIZE) {
2890+
if (RSA_DECL_MP_OVER_MAX(mp_bitsused(&key->n))) {
28832891
return WC_KEY_SIZE_E;
28842892
}
28852893

@@ -3062,7 +3070,7 @@ static int RsaFunctionSync(const byte* in, word32 inLen, byte* out,
30623070
DECL_MP_INT_SIZE_DYN(tmp, mp_bitsused(&key->n), RSA_MAX_SIZE);
30633071
int ret = 0;
30643072

3065-
if (mp_bitsused(&key->n) > RSA_MAX_SIZE) {
3073+
if (RSA_DECL_MP_OVER_MAX(mp_bitsused(&key->n))) {
30663074
return WC_KEY_SIZE_E;
30673075
}
30683076

@@ -3496,7 +3504,7 @@ int RsaFunctionCheckIn(const byte* in, word32 inLen, RsaKey* key,
34963504

34973505
DECL_MP_INT_SIZE_DYN(c, mp_bitsused(&key->n), RSA_MAX_SIZE);
34983506

3499-
if (mp_bitsused(&key->n) > RSA_MAX_SIZE) {
3507+
if (RSA_DECL_MP_OVER_MAX(mp_bitsused(&key->n))) {
35003508
return WC_KEY_SIZE_E;
35013509
}
35023510

@@ -4840,10 +4848,6 @@ int wc_RsaEncryptSize(const RsaKey* key)
48404848
return BAD_FUNC_ARG;
48414849
}
48424850

4843-
if (mp_bitsused(&key->n) > RSA_MAX_SIZE) {
4844-
return WC_KEY_SIZE_E;
4845-
}
4846-
48474851
ret = mp_unsigned_bin_size(&key->n);
48484852

48494853
#if defined(WOLFSSL_MICROCHIP_TA100)

wolfssl/wolfcrypt/sp_int.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -895,11 +895,13 @@ while (0)
895895
#define DECL_MP_INT_SIZE(name, bits) \
896896
sp_int_digit name##d[MP_INT_SIZEOF_DIGITS(MP_BITS_CNT(bits))]; \
897897
sp_int* (name) = (sp_int*)name##d
898-
/* Zero out mp_int of minimal size.
899-
* Use the declared digit array size, not bits, so memset cannot exceed the
900-
* buffer allocated by DECL_MP_INT_SIZE_DYN(..., bits, max). */
898+
/* Bytes to zero for a static mp_int: min(requested bits size, declared buffer). */
899+
#define MP_INT_ZERO_SIZE(name, bits) \
900+
((sizeof(name##d) < MP_INT_SIZEOF(MP_BITS_CNT(bits))) ? \
901+
sizeof(name##d) : MP_INT_SIZEOF(MP_BITS_CNT(bits)))
902+
/* Zero out mp_int without clearing more than the declared digit buffer. */
901903
#define NEW_MP_INT_SIZE(name, bits, heap, type) \
902-
XMEMSET(name, 0, sizeof(name##d))
904+
XMEMSET((name), 0, MP_INT_ZERO_SIZE(name, bits))
903905
/* Dispose of static mp_int. */
904906
#define FREE_MP_INT_SIZE(name, heap, type) WC_DO_NOTHING
905907
/* Type to force compiler to not complain about size. */

0 commit comments

Comments
 (0)