From 30b63bda6f0bad5d709de044a055eb3fcebbe356 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mois=C3=A9s=20Guimar=C3=A3es?= Date: Wed, 23 Apr 2014 16:06:14 -0300 Subject: [PATCH] random: InitRng and RNG_GenerateBlock to reduce stack usage: (up to 512 bytes - pointer sizes) moved to the heap. with ARC4 --- InitRng: entropy variable moved to the heap; (256 bytes) --- RNG_GenerateBlock: entropy variable moved to the heap; (256 bytes) without ARC4 --- InitRng: key variable moved to the heap; (32 bytes) --- InitRng: junk variable moved to the heap; (256 bytes) --- ctaocrypt/src/random.c | 72 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 63 insertions(+), 9 deletions(-) diff --git a/ctaocrypt/src/random.c b/ctaocrypt/src/random.c index ab39089c8..9663262c5 100644 --- a/ctaocrypt/src/random.c +++ b/ctaocrypt/src/random.c @@ -307,13 +307,28 @@ static int Hash_DBRG_Uninstantiate(RNG* rng) /* Get seed and key cipher */ int InitRng(RNG* rng) { +#ifdef CYASSL_SMALL_STACK + byte* entropy; +#else byte entropy[ENTROPY_SZ]; +#endif int ret = DBRG_ERROR; - if (GenerateSeed(&rng->seed, entropy, sizeof(entropy)) == 0) - ret = Hash_DBRG_Instantiate(rng, entropy, sizeof(entropy)); +#ifdef CYASSL_SMALL_STACK + entropy = (byte*)XMALLOC(ENTROPY_SZ, NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (entropy == NULL) + return MEMORY_E; +#endif + + if (GenerateSeed(&rng->seed, entropy, ENTROPY_SZ) == 0) + ret = Hash_DBRG_Instantiate(rng, entropy, ENTROPY_SZ); + + XMEMSET(entropy, 0, ENTROPY_SZ); + +#ifdef CYASSL_SMALL_STACK + XFREE(entropy, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif - XMEMSET(entropy, 0, sizeof(entropy)); return ret; } @@ -327,17 +342,33 @@ int RNG_GenerateBlock(RNG* rng, byte* output, word32 sz) ret = Hash_DBRG_Generate(rng, output, sz); if (ret == DBRG_NEED_RESEED) { +#ifdef CYASSL_SMALL_STACK + byte* entropy; +#else byte entropy[ENTROPY_SZ]; - ret = GenerateSeed(&rng->seed, entropy, sizeof(entropy)); +#endif + +#ifdef CYASSL_SMALL_STACK + entropy = (byte*)XMALLOC(ENTROPY_SZ, NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (entropy == NULL) + return MEMORY_E; +#endif + + ret = GenerateSeed(&rng->seed, entropy, ENTROPY_SZ); if (ret == 0) { - ret = Hash_DBRG_Reseed(rng, entropy, sizeof(entropy)); + ret = Hash_DBRG_Reseed(rng, entropy, ENTROPY_SZ); if (ret == 0) ret = Hash_DBRG_Generate(rng, output, sz); } else ret = DBRG_ERROR; - XMEMSET(entropy, 0, sizeof(entropy)); + + XMEMSET(entropy, 0, ENTROPY_SZ); + +#ifdef CYASSL_SMALL_STACK + XFREE(entropy, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif } return ret; @@ -360,22 +391,45 @@ void FreeRng(RNG* rng) /* Get seed and key cipher */ int InitRng(RNG* rng) { + int ret; +#ifdef CYASSL_SMALL_STACK + byte* key; + byte* junk; +#else byte key[32]; byte junk[256]; - int ret; +#endif #ifdef HAVE_CAVIUM if (rng->magic == CYASSL_RNG_CAVIUM_MAGIC) return 0; #endif - ret = GenerateSeed(&rng->seed, key, sizeof(key)); + +#ifdef CYASSL_SMALL_STACK + key = (byte*)XMALLOC(32, NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (key == NULL) + return MEMORY_E; + + junk = (byte*)XMALLOC(256, NULL, DYNAMIC_TYPE_TMP_BUFFER); + if (junk == NULL) { + XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER); + return MEMORY_E; + } +#endif + + ret = GenerateSeed(&rng->seed, key, 32); if (ret == 0) { Arc4SetKey(&rng->cipher, key, sizeof(key)); - return RNG_GenerateBlock(rng, junk, sizeof(junk)); /*rid initial state*/ + ret = RNG_GenerateBlock(rng, junk, 256); /*rid initial state*/ } +#ifdef CYASSL_SMALL_STACK + XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER); + XFREE(junk, NULL, DYNAMIC_TYPE_TMP_BUFFER); +#endif + return ret; }