summaryrefslogtreecommitdiff
path: root/crypto/rand/rand_lib.c
diff options
context:
space:
mode:
authorBernd Edlinger <bernd.edlinger@hotmail.de>2019-07-20 11:22:46 +0200
committerBernd Edlinger <bernd.edlinger@hotmail.de>2019-07-22 13:37:13 +0200
commit1372560f64c9a7cfad1979fa8c41bee335a04373 (patch)
tree0e36898357cc623e3d0904921d091ba2c4b38ff8 /crypto/rand/rand_lib.c
parenta8f1aabd4b44db668bca638c111598b2e0688cc4 (diff)
downloadopenssl-new-1372560f64c9a7cfad1979fa8c41bee335a04373.tar.gz
Allocate DRBG additional data pool from non-secure memory
The additional data allocates 12K per DRBG instance in the secure memory, which is not necessary. Also nonces are not considered secret. [extended tests] Reviewed-by: Matthias St. Pierre <Matthias.St.Pierre@ncp-e.com> Reviewed-by: Paul Dale <paul.dale@oracle.com> (Merged from https://github.com/openssl/openssl/pull/9423)
Diffstat (limited to 'crypto/rand/rand_lib.c')
-rw-r--r--crypto/rand/rand_lib.c31
1 files changed, 23 insertions, 8 deletions
diff --git a/crypto/rand/rand_lib.c b/crypto/rand/rand_lib.c
index 7768ade8b7..9c99cc9003 100644
--- a/crypto/rand/rand_lib.c
+++ b/crypto/rand/rand_lib.c
@@ -149,7 +149,7 @@ size_t rand_drbg_get_entropy(RAND_DRBG *drbg,
pool = drbg->seed_pool;
pool->entropy_requested = entropy;
} else {
- pool = rand_pool_new(entropy, min_len, max_len);
+ pool = rand_pool_new(entropy, drbg->secure, min_len, max_len);
if (pool == NULL)
return 0;
}
@@ -203,8 +203,12 @@ size_t rand_drbg_get_entropy(RAND_DRBG *drbg,
void rand_drbg_cleanup_entropy(RAND_DRBG *drbg,
unsigned char *out, size_t outlen)
{
- if (drbg->seed_pool == NULL)
- OPENSSL_secure_clear_free(out, outlen);
+ if (drbg->seed_pool == NULL) {
+ if (drbg->secure)
+ OPENSSL_secure_clear_free(out, outlen);
+ else
+ OPENSSL_clear_free(out, outlen);
+ }
}
/*
@@ -331,7 +335,7 @@ int RAND_poll(void)
RAND_POOL *pool = NULL;
/* fill random pool and seed the current legacy RNG */
- pool = rand_pool_new(RAND_DRBG_STRENGTH,
+ pool = rand_pool_new(RAND_DRBG_STRENGTH, 1,
(RAND_DRBG_STRENGTH + 7) / 8,
RAND_POOL_MAX_LENGTH);
if (pool == NULL)
@@ -360,7 +364,8 @@ int RAND_poll(void)
* Allocate memory and initialize a new random pool
*/
-RAND_POOL *rand_pool_new(int entropy_requested, size_t min_len, size_t max_len)
+RAND_POOL *rand_pool_new(int entropy_requested, int secure,
+ size_t min_len, size_t max_len)
{
RAND_POOL *pool = OPENSSL_zalloc(sizeof(*pool));
@@ -373,13 +378,18 @@ RAND_POOL *rand_pool_new(int entropy_requested, size_t min_len, size_t max_len)
pool->max_len = (max_len > RAND_POOL_MAX_LENGTH) ?
RAND_POOL_MAX_LENGTH : max_len;
- pool->buffer = OPENSSL_secure_zalloc(pool->max_len);
+ if (secure)
+ pool->buffer = OPENSSL_secure_zalloc(pool->max_len);
+ else
+ pool->buffer = OPENSSL_zalloc(pool->max_len);
+
if (pool->buffer == NULL) {
RANDerr(RAND_F_RAND_POOL_NEW, ERR_R_MALLOC_FAILURE);
goto err;
}
pool->entropy_requested = entropy_requested;
+ pool->secure = secure;
return pool;
@@ -434,8 +444,13 @@ void rand_pool_free(RAND_POOL *pool)
* to rand_pool_attach() as `const unsigned char*`.
* (see corresponding comment in rand_pool_attach()).
*/
- if (!pool->attached)
- OPENSSL_secure_clear_free(pool->buffer, pool->max_len);
+ if (!pool->attached) {
+ if (pool->secure)
+ OPENSSL_secure_clear_free(pool->buffer, pool->max_len);
+ else
+ OPENSSL_clear_free(pool->buffer, pool->max_len);
+ }
+
OPENSSL_free(pool);
}