From cab23472be5889c5caa5502b6e5ab8e3151c24bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moise=CC=81s=20Guimara=CC=83es?= Date: Fri, 12 Sep 2014 13:39:10 -0300 Subject: [PATCH] ssl: refactoring CyaSSL_RSA_generate_key_ex to reduce stack usage: --- variable rng moved to the heap (sizeof(RNG) saved) --- src/ssl.c | 49 +++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/src/ssl.c b/src/ssl.c index 8a8b45457..aa09e3245 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -10930,7 +10930,7 @@ static int SetRsaExternal(CYASSL_RSA* rsa) int CyaSSL_RSA_generate_key_ex(CYASSL_RSA* rsa, int bits, CYASSL_BIGNUM* bn, void* cb) { - RNG rng; + int ret = SSL_FATAL_ERROR; CYASSL_MSG("CyaSSL_RSA_generate_key_ex"); @@ -10939,30 +10939,39 @@ int CyaSSL_RSA_generate_key_ex(CYASSL_RSA* rsa, int bits, CYASSL_BIGNUM* bn, (void)cb; (void)bn; - if (InitRng(&rng) < 0) { - CYASSL_MSG("RNG init failed"); - return SSL_FATAL_ERROR; - } - #ifdef CYASSL_KEY_GEN - if (MakeRsaKey((RsaKey*)rsa->internal, bits, 65537, &rng) < 0) { - CYASSL_MSG("MakeRsaKey failed"); - return SSL_FATAL_ERROR; + { + #ifdef CYASSL_SMALL_STACK + RNG* rng = NULL; + #else + RNG rng[1]; + #endif + + #ifdef CYASSL_SMALL_STACK + rng = (RNG*)XMALLOC(sizeof(RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (rng == NULL) + return SSL_FATAL_ERROR; + #endif + + if (InitRng(rng) < 0) + CYASSL_MSG("RNG init failed"); + else if (MakeRsaKey((RsaKey*)rsa->internal, bits, 65537, rng) < 0) + CYASSL_MSG("MakeRsaKey failed"); + else if (SetRsaExternal(rsa) < 0) + CYASSL_MSG("SetRsaExternal failed"); + else { + rsa->inSet = 1; + ret = SSL_SUCCESS; + } + + #ifdef CYASSL_SMALL_STACK + XFREE(rng, NULL, DYNAMIC_TYPE_TMP_BUFFER); + #endif } - - if (SetRsaExternal(rsa) < 0) { - CYASSL_MSG("SetRsaExternal failed"); - return SSL_FATAL_ERROR; - } - - rsa->inSet = 1; - - return SSL_SUCCESS; #else CYASSL_MSG("No Key Gen built in"); - return SSL_FATAL_ERROR; #endif - + return ret; }