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().

This commit is contained in:
Daniel Pouzzner
2025-12-22 22:55:40 -06:00
parent 59f84355a5
commit b2ef89b2db
2 changed files with 28 additions and 16 deletions

View File

@@ -703,7 +703,10 @@ static int _ifc_pairwise_consistency_test(RsaKey* key, WC_RNG* rng)
int wc_CheckRsaKey(RsaKey* key) int wc_CheckRsaKey(RsaKey* key)
{ {
WC_DECLARE_VAR(rng, WC_RNG, 1, 0); WC_RNG *rng = NULL;
#if !defined(WOLFSSL_SMALL_STACK) || defined(WOLFSSL_NO_MALLOC)
WC_RNG rng_buf;
#endif
int ret = 0; int ret = 0;
DECL_MP_INT_SIZE_DYN(tmp, (key)? mp_bitsused(&key->n) : 0, RSA_MAX_SIZE); DECL_MP_INT_SIZE_DYN(tmp, (key)? mp_bitsused(&key->n) : 0, RSA_MAX_SIZE);
@@ -718,17 +721,27 @@ int wc_CheckRsaKey(RsaKey* key)
} }
#endif #endif
WC_ALLOC_VAR_EX(rng, WC_RNG, 1, NULL, DYNAMIC_TYPE_RNG,
return MEMORY_E);
NEW_MP_INT_SIZE(tmp, mp_bitsused(&key->n), NULL, DYNAMIC_TYPE_RSA); NEW_MP_INT_SIZE(tmp, mp_bitsused(&key->n), NULL, DYNAMIC_TYPE_RSA);
#ifdef MP_INT_SIZE_CHECK_NULL #ifdef MP_INT_SIZE_CHECK_NULL
if (tmp == NULL) { if (tmp == NULL) {
XFREE(rng, NULL, DYNAMIC_TYPE_RNG);
return MEMORY_E; return MEMORY_E;
} }
#endif #endif
ret = wc_InitRng(rng); if (key->rng)
rng = key->rng;
else {
#if !defined(WOLFSSL_SMALL_STACK) || defined(WOLFSSL_NO_MALLOC)
rng = &rng_buf;
#else
rng = (WC_RNG *)XMALLOC(sizeof(*rng), NULL, DYNAMIC_TYPE_RNG);
if (rng == NULL) {
FREE_MP_INT_SIZE(tmp, NULL, DYNAMIC_TYPE_RSA);
return MEMORY_E;
}
#endif
ret = wc_InitRng(rng);
}
SAVE_VECTOR_REGISTERS(ret = _svr_ret;); SAVE_VECTOR_REGISTERS(ret = _svr_ret;);
@@ -846,11 +859,14 @@ int wc_CheckRsaKey(RsaKey* key)
RESTORE_VECTOR_REGISTERS(); RESTORE_VECTOR_REGISTERS();
wc_FreeRng(rng); if ((rng != NULL) && (rng != key->rng)) {
FREE_MP_INT_SIZE(tmp, NULL, DYNAMIC_TYPE_RSA); wc_FreeRng(rng);
#ifdef WOLFSSL_SMALL_STACK #ifdef WOLFSSL_SMALL_STACK
XFREE(rng, NULL, DYNAMIC_TYPE_RNG); XFREE(rng, NULL, DYNAMIC_TYPE_RNG);
#elif defined(WOLFSSL_CHECK_MEM_ZERO) #endif
}
FREE_MP_INT_SIZE(tmp, NULL, DYNAMIC_TYPE_RSA);
#ifdef WOLFSSL_CHECK_MEM_ZERO
mp_memzero_check(tmp); mp_memzero_check(tmp);
#endif #endif
@@ -5239,7 +5255,6 @@ int wc_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng)
#endif /* WOLFSSL_KEY_GEN */ #endif /* WOLFSSL_KEY_GEN */
#ifdef WC_RSA_BLINDING
int wc_RsaSetRNG(RsaKey* key, WC_RNG* rng) int wc_RsaSetRNG(RsaKey* key, WC_RNG* rng)
{ {
if (key == NULL || rng == NULL) if (key == NULL || rng == NULL)
@@ -5249,7 +5264,6 @@ int wc_RsaSetRNG(RsaKey* key, WC_RNG* rng)
return 0; return 0;
} }
#endif /* WC_RSA_BLINDING */
#ifdef WC_RSA_NONBLOCK #ifdef WC_RSA_NONBLOCK
int wc_RsaSetNonBlock(RsaKey* key, RsaNb* nb) int wc_RsaSetNonBlock(RsaKey* key, RsaNb* nb)

View File

@@ -214,9 +214,9 @@ struct RsaKey {
int type; /* public or private */ int type; /* public or private */
int state; int state;
word32 dataLen; word32 dataLen;
#ifdef WC_RSA_BLINDING WC_RNG* rng; /* for PrivateDecrypt blinding and
WC_RNG* rng; /* for PrivateDecrypt blinding */ * _ifc_pairwise_consistency_test()
#endif */
#ifdef WOLFSSL_SE050 #ifdef WOLFSSL_SE050
word32 keyId; word32 keyId;
byte keyIdSet; byte keyIdSet;
@@ -403,9 +403,7 @@ WOLFSSL_API int wc_RsaPublicKeyDecodeRaw(const byte* n, word32 nSz,
WOLFSSL_API int wc_RsaKeyToDer(RsaKey* key, byte* output, word32 inLen); WOLFSSL_API int wc_RsaKeyToDer(RsaKey* key, byte* output, word32 inLen);
#endif #endif
#ifdef WC_RSA_BLINDING
WOLFSSL_API int wc_RsaSetRNG(RsaKey* key, WC_RNG* rng); WOLFSSL_API int wc_RsaSetRNG(RsaKey* key, WC_RNG* rng);
#endif
#ifdef WC_RSA_NONBLOCK #ifdef WC_RSA_NONBLOCK
WOLFSSL_API int wc_RsaSetNonBlock(RsaKey* key, RsaNb* nb); WOLFSSL_API int wc_RsaSetNonBlock(RsaKey* key, RsaNb* nb);
#ifdef WC_RSA_NONBLOCK_TIME #ifdef WC_RSA_NONBLOCK_TIME