Skip to content

Commit b2ef89b

Browse files
committed
wolfcrypt/src/rsa.c and wolfssl/wolfcrypt/rsa.h: make RsaKey.rng and wc_RsaSetRNG() available unconditionally, rather than only if WC_RSA_BLINDING, for use by wc_CheckRsaKey().
1 parent 59f8435 commit b2ef89b

2 files changed

Lines changed: 28 additions & 16 deletions

File tree

wolfcrypt/src/rsa.c

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,10 @@ static int _ifc_pairwise_consistency_test(RsaKey* key, WC_RNG* rng)
703703

704704
int wc_CheckRsaKey(RsaKey* key)
705705
{
706-
WC_DECLARE_VAR(rng, WC_RNG, 1, 0);
706+
WC_RNG *rng = NULL;
707+
#if !defined(WOLFSSL_SMALL_STACK) || defined(WOLFSSL_NO_MALLOC)
708+
WC_RNG rng_buf;
709+
#endif
707710
int ret = 0;
708711
DECL_MP_INT_SIZE_DYN(tmp, (key)? mp_bitsused(&key->n) : 0, RSA_MAX_SIZE);
709712

@@ -718,17 +721,27 @@ int wc_CheckRsaKey(RsaKey* key)
718721
}
719722
#endif
720723

721-
WC_ALLOC_VAR_EX(rng, WC_RNG, 1, NULL, DYNAMIC_TYPE_RNG,
722-
return MEMORY_E);
723724
NEW_MP_INT_SIZE(tmp, mp_bitsused(&key->n), NULL, DYNAMIC_TYPE_RSA);
724725
#ifdef MP_INT_SIZE_CHECK_NULL
725726
if (tmp == NULL) {
726-
XFREE(rng, NULL, DYNAMIC_TYPE_RNG);
727727
return MEMORY_E;
728728
}
729729
#endif
730730

731-
ret = wc_InitRng(rng);
731+
if (key->rng)
732+
rng = key->rng;
733+
else {
734+
#if !defined(WOLFSSL_SMALL_STACK) || defined(WOLFSSL_NO_MALLOC)
735+
rng = &rng_buf;
736+
#else
737+
rng = (WC_RNG *)XMALLOC(sizeof(*rng), NULL, DYNAMIC_TYPE_RNG);
738+
if (rng == NULL) {
739+
FREE_MP_INT_SIZE(tmp, NULL, DYNAMIC_TYPE_RSA);
740+
return MEMORY_E;
741+
}
742+
#endif
743+
ret = wc_InitRng(rng);
744+
}
732745

733746
SAVE_VECTOR_REGISTERS(ret = _svr_ret;);
734747

@@ -846,11 +859,14 @@ int wc_CheckRsaKey(RsaKey* key)
846859

847860
RESTORE_VECTOR_REGISTERS();
848861

849-
wc_FreeRng(rng);
850-
FREE_MP_INT_SIZE(tmp, NULL, DYNAMIC_TYPE_RSA);
862+
if ((rng != NULL) && (rng != key->rng)) {
863+
wc_FreeRng(rng);
851864
#ifdef WOLFSSL_SMALL_STACK
852-
XFREE(rng, NULL, DYNAMIC_TYPE_RNG);
853-
#elif defined(WOLFSSL_CHECK_MEM_ZERO)
865+
XFREE(rng, NULL, DYNAMIC_TYPE_RNG);
866+
#endif
867+
}
868+
FREE_MP_INT_SIZE(tmp, NULL, DYNAMIC_TYPE_RSA);
869+
#ifdef WOLFSSL_CHECK_MEM_ZERO
854870
mp_memzero_check(tmp);
855871
#endif
856872

@@ -5239,7 +5255,6 @@ int wc_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng)
52395255
#endif /* WOLFSSL_KEY_GEN */
52405256

52415257

5242-
#ifdef WC_RSA_BLINDING
52435258
int wc_RsaSetRNG(RsaKey* key, WC_RNG* rng)
52445259
{
52455260
if (key == NULL || rng == NULL)
@@ -5249,7 +5264,6 @@ int wc_RsaSetRNG(RsaKey* key, WC_RNG* rng)
52495264

52505265
return 0;
52515266
}
5252-
#endif /* WC_RSA_BLINDING */
52535267

52545268
#ifdef WC_RSA_NONBLOCK
52555269
int wc_RsaSetNonBlock(RsaKey* key, RsaNb* nb)

wolfssl/wolfcrypt/rsa.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,9 @@ struct RsaKey {
214214
int type; /* public or private */
215215
int state;
216216
word32 dataLen;
217-
#ifdef WC_RSA_BLINDING
218-
WC_RNG* rng; /* for PrivateDecrypt blinding */
219-
#endif
217+
WC_RNG* rng; /* for PrivateDecrypt blinding and
218+
* _ifc_pairwise_consistency_test()
219+
*/
220220
#ifdef WOLFSSL_SE050
221221
word32 keyId;
222222
byte keyIdSet;
@@ -403,9 +403,7 @@ WOLFSSL_API int wc_RsaPublicKeyDecodeRaw(const byte* n, word32 nSz,
403403
WOLFSSL_API int wc_RsaKeyToDer(RsaKey* key, byte* output, word32 inLen);
404404
#endif
405405

406-
#ifdef WC_RSA_BLINDING
407406
WOLFSSL_API int wc_RsaSetRNG(RsaKey* key, WC_RNG* rng);
408-
#endif
409407
#ifdef WC_RSA_NONBLOCK
410408
WOLFSSL_API int wc_RsaSetNonBlock(RsaKey* key, RsaNb* nb);
411409
#ifdef WC_RSA_NONBLOCK_TIME

0 commit comments

Comments
 (0)