diff --git a/wolfcrypt/src/rsa.c b/wolfcrypt/src/rsa.c index 02ed7eeee..0d19ae680 100644 --- a/wolfcrypt/src/rsa.c +++ b/wolfcrypt/src/rsa.c @@ -703,7 +703,10 @@ static int _ifc_pairwise_consistency_test(RsaKey* key, WC_RNG* rng) 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; 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 - 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); #ifdef MP_INT_SIZE_CHECK_NULL if (tmp == NULL) { - XFREE(rng, NULL, DYNAMIC_TYPE_RNG); return MEMORY_E; } #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;); @@ -846,11 +859,14 @@ int wc_CheckRsaKey(RsaKey* key) RESTORE_VECTOR_REGISTERS(); - wc_FreeRng(rng); - FREE_MP_INT_SIZE(tmp, NULL, DYNAMIC_TYPE_RSA); + if ((rng != NULL) && (rng != key->rng)) { + wc_FreeRng(rng); #ifdef WOLFSSL_SMALL_STACK - XFREE(rng, NULL, DYNAMIC_TYPE_RNG); -#elif defined(WOLFSSL_CHECK_MEM_ZERO) + XFREE(rng, NULL, DYNAMIC_TYPE_RNG); +#endif + } + FREE_MP_INT_SIZE(tmp, NULL, DYNAMIC_TYPE_RSA); +#ifdef WOLFSSL_CHECK_MEM_ZERO mp_memzero_check(tmp); #endif @@ -5239,7 +5255,6 @@ int wc_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng) #endif /* WOLFSSL_KEY_GEN */ -#ifdef WC_RSA_BLINDING int wc_RsaSetRNG(RsaKey* key, WC_RNG* rng) { if (key == NULL || rng == NULL) @@ -5249,7 +5264,6 @@ int wc_RsaSetRNG(RsaKey* key, WC_RNG* rng) return 0; } -#endif /* WC_RSA_BLINDING */ #ifdef WC_RSA_NONBLOCK int wc_RsaSetNonBlock(RsaKey* key, RsaNb* nb) diff --git a/wolfssl/wolfcrypt/rsa.h b/wolfssl/wolfcrypt/rsa.h index 94350fcab..a5a21ed65 100644 --- a/wolfssl/wolfcrypt/rsa.h +++ b/wolfssl/wolfcrypt/rsa.h @@ -214,9 +214,9 @@ struct RsaKey { int type; /* public or private */ int state; word32 dataLen; -#ifdef WC_RSA_BLINDING - WC_RNG* rng; /* for PrivateDecrypt blinding */ -#endif + WC_RNG* rng; /* for PrivateDecrypt blinding and + * _ifc_pairwise_consistency_test() + */ #ifdef WOLFSSL_SE050 word32 keyId; 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); #endif -#ifdef WC_RSA_BLINDING WOLFSSL_API int wc_RsaSetRNG(RsaKey* key, WC_RNG* rng); -#endif #ifdef WC_RSA_NONBLOCK WOLFSSL_API int wc_RsaSetNonBlock(RsaKey* key, RsaNb* nb); #ifdef WC_RSA_NONBLOCK_TIME