Move randomness to allocated buffer
[openssl.git] / crypto / rand / drbg_lib.c
index 935d5c8fd7433e7feab93484d690b68f098293c8..6aced40fd4cc66b3347c5f41dc53c55f918c9d6b 100644 (file)
@@ -64,15 +64,13 @@ int RAND_DRBG_set(RAND_DRBG *drbg, int nid, unsigned int flags)
 RAND_DRBG *RAND_DRBG_new(int type, unsigned int flags, RAND_DRBG *parent)
 {
     RAND_DRBG *drbg = OPENSSL_zalloc(sizeof(*drbg));
-    unsigned char *ucp = OPENSSL_zalloc(RANDOMNESS_NEEDED);
 
-    if (drbg == NULL || ucp == NULL) {
+    if (drbg == NULL) {
         RANDerr(RAND_F_RAND_DRBG_NEW, ERR_R_MALLOC_FAILURE);
         goto err;
     }
     drbg->size = RANDOMNESS_NEEDED;
-    drbg->randomness = ucp;
-
+    drbg->fork_count = rand_fork_count;
     drbg->parent = parent;
     if (RAND_DRBG_set(drbg, type, flags) < 0)
         goto err;
@@ -96,7 +94,6 @@ RAND_DRBG *RAND_DRBG_new(int type, unsigned int flags, RAND_DRBG *parent)
     return drbg;
 
 err:
-    OPENSSL_free(ucp);
     OPENSSL_free(drbg);
     return NULL;
 }
@@ -116,8 +113,6 @@ void RAND_DRBG_free(RAND_DRBG *drbg)
         return;
 
     ctr_uninstantiate(drbg);
-    OPENSSL_cleanse(drbg->randomness, drbg->size);
-    OPENSSL_free(drbg->randomness);
     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_DRBG, drbg, &drbg->ex_data);
     OPENSSL_clear_free(drbg, sizeof(*drbg));
 }
@@ -266,6 +261,11 @@ int RAND_DRBG_generate(RAND_DRBG *drbg, unsigned char *out, size_t outlen,
         return 0;
     }
 
+    if (drbg->fork_count != rand_fork_count) {
+        drbg->fork_count = rand_fork_count;
+        drbg->state = DRBG_RESEED;
+    }
+
     if (drbg->reseed_counter >= drbg->reseed_interval)
         drbg->state = DRBG_RESEED;
 
@@ -398,6 +398,8 @@ static int drbg_status(void)
     int ret;
 
     CRYPTO_THREAD_write_lock(rand_drbg.lock);
+    if (rand_drbg.state == DRBG_UNINITIALISED)
+        RAND_DRBG_instantiate(&rand_drbg, NULL, 0);
     ret = rand_drbg.state == DRBG_READY ? 1 : 0;
     CRYPTO_THREAD_unlock(rand_drbg.lock);
     return ret;