7d1228
diff -up openssl-1.1.1g/crypto/rand/build.info.crng-test openssl-1.1.1g/crypto/rand/build.info
7d1228
--- openssl-1.1.1g/crypto/rand/build.info.crng-test	2020-04-23 13:30:45.863389837 +0200
7d1228
+++ openssl-1.1.1g/crypto/rand/build.info	2020-04-23 13:31:55.847069892 +0200
7d1228
@@ -1,6 +1,6 @@
acdedc
 LIBS=../../libcrypto
acdedc
 SOURCE[../../libcrypto]=\
acdedc
-        randfile.c rand_lib.c rand_err.c rand_egd.c \
acdedc
+        randfile.c rand_lib.c rand_err.c rand_crng_test.c rand_egd.c \
acdedc
         rand_win.c rand_unix.c rand_vms.c drbg_lib.c drbg_ctr.c
7d1228
 
7d1228
 INCLUDE[drbg_ctr.o]=../modes
7d1228
diff -up openssl-1.1.1g/crypto/rand/drbg_lib.c.crng-test openssl-1.1.1g/crypto/rand/drbg_lib.c
7d1228
--- openssl-1.1.1g/crypto/rand/drbg_lib.c.crng-test	2020-04-23 13:30:45.818390686 +0200
7d1228
+++ openssl-1.1.1g/crypto/rand/drbg_lib.c	2020-04-23 13:30:45.864389819 +0200
acdedc
@@ -67,7 +67,7 @@ static CRYPTO_THREAD_LOCAL private_drbg;
acdedc
 
acdedc
 
acdedc
 /* NIST SP 800-90A DRBG recommends the use of a personalization string. */
acdedc
-static const char ossl_pers_string[] = "OpenSSL NIST SP 800-90A DRBG";
acdedc
+static const char ossl_pers_string[] = DRBG_DEFAULT_PERS_STRING;
acdedc
 
acdedc
 static CRYPTO_ONCE rand_drbg_init = CRYPTO_ONCE_STATIC_INIT;
acdedc
 
acdedc
@@ -201,8 +201,13 @@ static RAND_DRBG *rand_drbg_new(int secu
acdedc
     drbg->parent = parent;
acdedc
 
acdedc
     if (parent == NULL) {
acdedc
+#ifdef OPENSSL_FIPS
acdedc
+        drbg->get_entropy = rand_crngt_get_entropy;
acdedc
+        drbg->cleanup_entropy = rand_crngt_cleanup_entropy;
acdedc
+#else
acdedc
         drbg->get_entropy = rand_drbg_get_entropy;
acdedc
         drbg->cleanup_entropy = rand_drbg_cleanup_entropy;
acdedc
+#endif
acdedc
 #ifndef RAND_DRBG_GET_RANDOM_NONCE
acdedc
         drbg->get_nonce = rand_drbg_get_nonce;
acdedc
         drbg->cleanup_nonce = rand_drbg_cleanup_nonce;
7d1228
diff -up openssl-1.1.1g/crypto/rand/rand_crng_test.c.crng-test openssl-1.1.1g/crypto/rand/rand_crng_test.c
7d1228
--- openssl-1.1.1g/crypto/rand/rand_crng_test.c.crng-test	2020-04-23 13:30:45.864389819 +0200
7d1228
+++ openssl-1.1.1g/crypto/rand/rand_crng_test.c	2020-04-23 13:30:45.864389819 +0200
acdedc
@@ -0,0 +1,118 @@
acdedc
+/*
acdedc
+ * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
acdedc
+ * Copyright (c) 2019, Oracle and/or its affiliates.  All rights reserved.
acdedc
+ *
acdedc
+ * Licensed under the Apache License 2.0 (the "License").  You may not use
acdedc
+ * this file except in compliance with the License.  You can obtain a copy
acdedc
+ * in the file LICENSE in the source distribution or at
acdedc
+ * https://www.openssl.org/source/license.html
acdedc
+ */
acdedc
+
acdedc
+/*
acdedc
+ * Implementation of the FIPS 140-2 section 4.9.2 Conditional Tests.
acdedc
+ */
acdedc
+
acdedc
+#include <string.h>
acdedc
+#include <openssl/evp.h>
7d1228
+#include "crypto/rand.h"
acdedc
+#include "internal/thread_once.h"
7d1228
+#include "rand_local.h"
acdedc
+
acdedc
+static RAND_POOL *crngt_pool;
acdedc
+static unsigned char crngt_prev[EVP_MAX_MD_SIZE];
acdedc
+
acdedc
+int (*crngt_get_entropy)(unsigned char *, unsigned char *, unsigned int *)
acdedc
+    = &rand_crngt_get_entropy_cb;
acdedc
+
acdedc
+int rand_crngt_get_entropy_cb(unsigned char *buf, unsigned char *md,
acdedc
+                              unsigned int *md_size)
acdedc
+{
acdedc
+    int r;
acdedc
+    size_t n;
acdedc
+    unsigned char *p;
acdedc
+
acdedc
+    n = rand_pool_acquire_entropy(crngt_pool);
acdedc
+    if (n >= CRNGT_BUFSIZ) {
acdedc
+        p = rand_pool_detach(crngt_pool);
acdedc
+        r = EVP_Digest(p, CRNGT_BUFSIZ, md, md_size, EVP_sha256(), NULL);
acdedc
+        if (r != 0)
acdedc
+            memcpy(buf, p, CRNGT_BUFSIZ);
acdedc
+        rand_pool_reattach(crngt_pool, p);
acdedc
+        return r;
acdedc
+    }
acdedc
+    return 0;
acdedc
+}
acdedc
+
acdedc
+void rand_crngt_cleanup(void)
acdedc
+{
acdedc
+    rand_pool_free(crngt_pool);
acdedc
+    crngt_pool = NULL;
acdedc
+}
acdedc
+
acdedc
+int rand_crngt_init(void)
acdedc
+{
acdedc
+    unsigned char buf[CRNGT_BUFSIZ];
acdedc
+
7d1228
+    if ((crngt_pool = rand_pool_new(0, 1, CRNGT_BUFSIZ, CRNGT_BUFSIZ)) == NULL)
acdedc
+        return 0;
acdedc
+    if (crngt_get_entropy(buf, crngt_prev, NULL)) {
acdedc
+        OPENSSL_cleanse(buf, sizeof(buf));
acdedc
+        return 1;
acdedc
+    }
acdedc
+    rand_crngt_cleanup();
acdedc
+    return 0;
acdedc
+}
acdedc
+
acdedc
+static CRYPTO_ONCE rand_crngt_init_flag = CRYPTO_ONCE_STATIC_INIT;
acdedc
+DEFINE_RUN_ONCE_STATIC(do_rand_crngt_init)
acdedc
+{
acdedc
+    return OPENSSL_init_crypto(0, NULL)
acdedc
+        && rand_crngt_init()
acdedc
+        && OPENSSL_atexit(&rand_crngt_cleanup);
acdedc
+}
acdedc
+
acdedc
+int rand_crngt_single_init(void)
acdedc
+{
acdedc
+    return RUN_ONCE(&rand_crngt_init_flag, do_rand_crngt_init);
acdedc
+}
acdedc
+
acdedc
+size_t rand_crngt_get_entropy(RAND_DRBG *drbg,
acdedc
+                              unsigned char **pout,
acdedc
+                              int entropy, size_t min_len, size_t max_len,
acdedc
+                              int prediction_resistance)
acdedc
+{
acdedc
+    unsigned char buf[CRNGT_BUFSIZ], md[EVP_MAX_MD_SIZE];
acdedc
+    unsigned int sz;
acdedc
+    RAND_POOL *pool;
acdedc
+    size_t q, r = 0, s, t = 0;
acdedc
+    int attempts = 3;
acdedc
+
acdedc
+    if (!RUN_ONCE(&rand_crngt_init_flag, do_rand_crngt_init))
acdedc
+        return 0;
acdedc
+
7d1228
+    if ((pool = rand_pool_new(entropy, 1, min_len, max_len)) == NULL)
acdedc
+        return 0;
acdedc
+
acdedc
+    while ((q = rand_pool_bytes_needed(pool, 1)) > 0 && attempts-- > 0) {
acdedc
+        s = q > sizeof(buf) ? sizeof(buf) : q;
acdedc
+        if (!crngt_get_entropy(buf, md, &sz)
acdedc
+            || memcmp(crngt_prev, md, sz) == 0
acdedc
+            || !rand_pool_add(pool, buf, s, s * 8))
acdedc
+            goto err;
acdedc
+        memcpy(crngt_prev, md, sz);
acdedc
+        t += s;
acdedc
+        attempts++;
acdedc
+    }
acdedc
+    r = t;
acdedc
+    *pout = rand_pool_detach(pool);
acdedc
+err:
acdedc
+    OPENSSL_cleanse(buf, sizeof(buf));
acdedc
+    rand_pool_free(pool);
acdedc
+    return r;
acdedc
+}
acdedc
+
acdedc
+void rand_crngt_cleanup_entropy(RAND_DRBG *drbg,
acdedc
+                                unsigned char *out, size_t outlen)
acdedc
+{
acdedc
+    OPENSSL_secure_clear_free(out, outlen);
acdedc
+}
7d1228
diff -up openssl-1.1.1g/crypto/rand/rand_local.h.crng-test openssl-1.1.1g/crypto/rand/rand_local.h
7d1228
--- openssl-1.1.1g/crypto/rand/rand_local.h.crng-test	2020-04-23 13:30:45.470397250 +0200
7d1228
+++ openssl-1.1.1g/crypto/rand/rand_local.h	2020-04-23 13:30:45.864389819 +0200
acdedc
@@ -33,7 +33,15 @@
acdedc
 # define MASTER_RESEED_TIME_INTERVAL             (60*60)   /* 1 hour */
acdedc
 # define SLAVE_RESEED_TIME_INTERVAL              (7*60)    /* 7 minutes */
acdedc
 
acdedc
-
acdedc
+/*
acdedc
+ * The number of bytes that constitutes an atomic lump of entropy with respect
acdedc
+ * to the FIPS 140-2 section 4.9.2 Conditional Tests.  The size is somewhat
acdedc
+ * arbitrary, the smaller the value, the less entropy is consumed on first
acdedc
+ * read but the higher the probability of the test failing by accident.
acdedc
+ *
acdedc
+ * The value is in bytes.
acdedc
+ */
acdedc
+#define CRNGT_BUFSIZ    16
acdedc
 
acdedc
 /*
acdedc
  * Maximum input size for the DRBG (entropy, nonce, personalization string)
7d1228
@@ -44,6 +52,8 @@
acdedc
  */
acdedc
 # define DRBG_MAX_LENGTH                         INT32_MAX
acdedc
 
acdedc
+/* The default nonce */
acdedc
+# define DRBG_DEFAULT_PERS_STRING                "OpenSSL NIST SP 800-90A DRBG"
acdedc
 
acdedc
 /*
acdedc
  * Maximum allocation size for RANDOM_POOL buffers
7d1228
@@ -296,4 +306,22 @@ int rand_drbg_enable_locking(RAND_DRBG *
acdedc
 /* initializes the AES-CTR DRBG implementation */
acdedc
 int drbg_ctr_init(RAND_DRBG *drbg);
acdedc
 
acdedc
+/*
acdedc
+ * Entropy call back for the FIPS 140-2 section 4.9.2 Conditional Tests.
acdedc
+ * These need to be exposed for the unit tests.
acdedc
+ */
acdedc
+int rand_crngt_get_entropy_cb(unsigned char *buf, unsigned char *md,
acdedc
+                              unsigned int *md_size);
acdedc
+extern int (*crngt_get_entropy)(unsigned char *buf, unsigned char *md,
acdedc
+                                unsigned int *md_size);
acdedc
+int rand_crngt_init(void);
acdedc
+void rand_crngt_cleanup(void);
acdedc
+
acdedc
+/*
acdedc
+ * Expose the run once initialisation function for the unit tests because.
acdedc
+ * they need to restart from scratch to validate the first block is skipped
acdedc
+ * properly.
acdedc
+ */
acdedc
+int rand_crngt_single_init(void);
acdedc
+
acdedc
 #endif
7d1228
diff -up openssl-1.1.1g/include/crypto/rand.h.crng-test openssl-1.1.1g/include/crypto/rand.h
7d1228
--- openssl-1.1.1g/include/crypto/rand.h.crng-test	2020-04-23 13:30:45.824390573 +0200
7d1228
+++ openssl-1.1.1g/include/crypto/rand.h	2020-04-23 13:30:45.864389819 +0200
7d1228
@@ -49,6 +49,14 @@ size_t rand_drbg_get_additional_data(RAN
7d1228
 
7d1228
 void rand_drbg_cleanup_additional_data(RAND_POOL *pool, unsigned char *out);
7d1228
 
7d1228
+/* CRNG test entropy filter callbacks. */
7d1228
+size_t rand_crngt_get_entropy(RAND_DRBG *drbg,
7d1228
+                              unsigned char **pout,
7d1228
+                              int entropy, size_t min_len, size_t max_len,
7d1228
+                              int prediction_resistance);
7d1228
+void rand_crngt_cleanup_entropy(RAND_DRBG *drbg,
7d1228
+                                unsigned char *out, size_t outlen);
7d1228
+
7d1228
 /*
7d1228
  * RAND_POOL functions
7d1228
  */
7d1228
diff -up openssl-1.1.1g/test/drbgtest.c.crng-test openssl-1.1.1g/test/drbgtest.c
7d1228
--- openssl-1.1.1g/test/drbgtest.c.crng-test	2020-04-21 14:22:39.000000000 +0200
7d1228
+++ openssl-1.1.1g/test/drbgtest.c	2020-04-23 13:30:45.865389800 +0200
7d1228
@@ -150,6 +150,31 @@ static size_t kat_nonce(RAND_DRBG *drbg,
acdedc
     return t->noncelen;
acdedc
 }
acdedc
 
acdedc
+ /*
acdedc
+ * Disable CRNG testing if it is enabled.
acdedc
+ * If the DRBG is ready or in an error state, this means an instantiate cycle
acdedc
+ * for which the default personalisation string is used.
acdedc
+ */
acdedc
+static int disable_crngt(RAND_DRBG *drbg)
acdedc
+{
acdedc
+    static const char pers[] = DRBG_DEFAULT_PERS_STRING;
acdedc
+    const int instantiate = drbg->state != DRBG_UNINITIALISED;
acdedc
+
acdedc
+    if (drbg->get_entropy != rand_crngt_get_entropy)
acdedc
+        return 1;
acdedc
+
acdedc
+     if ((instantiate && !RAND_DRBG_uninstantiate(drbg))
acdedc
+        || !TEST_true(RAND_DRBG_set_callbacks(drbg, &rand_drbg_get_entropy,
acdedc
+                                              &rand_drbg_cleanup_entropy,
acdedc
+                                              &rand_drbg_get_nonce,
acdedc
+                                              &rand_drbg_cleanup_nonce))
acdedc
+        || (instantiate
acdedc
+            && !RAND_DRBG_instantiate(drbg, (const unsigned char *)pers,
acdedc
+                                      sizeof(pers) - 1)))
acdedc
+        return 0;
acdedc
+    return 1;
acdedc
+}
acdedc
+
acdedc
 static int uninstantiate(RAND_DRBG *drbg)
acdedc
 {
acdedc
     int ret = drbg == NULL ? 1 : RAND_DRBG_uninstantiate(drbg);
7d1228
@@ -175,7 +200,8 @@ static int single_kat(DRBG_SELFTEST_DATA
acdedc
     if (!TEST_ptr(drbg = RAND_DRBG_new(td->nid, td->flags, NULL)))
acdedc
         return 0;
acdedc
     if (!TEST_true(RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
acdedc
-                                           kat_nonce, NULL))) {
acdedc
+                                           kat_nonce, NULL))
acdedc
+        || !TEST_true(disable_crngt(drbg))) {
acdedc
         failures++;
acdedc
         goto err;
acdedc
     }
7d1228
@@ -293,7 +319,8 @@ static int error_check(DRBG_SELFTEST_DAT
acdedc
     unsigned int reseed_counter_tmp;
acdedc
     int ret = 0;
acdedc
 
acdedc
-    if (!TEST_ptr(drbg = RAND_DRBG_new(0, 0, NULL)))
acdedc
+    if (!TEST_ptr(drbg = RAND_DRBG_new(0, 0, NULL))
acdedc
+	|| !TEST_true(disable_crngt(drbg)))
acdedc
         goto err;
acdedc
 
acdedc
     /*
7d1228
@@ -740,6 +767,10 @@ static int test_rand_drbg_reseed(void)
acdedc
         || !TEST_ptr_eq(private->parent, master))
acdedc
         return 0;
acdedc
 
acdedc
+    /* Disable CRNG testing for the master DRBG */
acdedc
+    if (!TEST_true(disable_crngt(master)))
acdedc
+        return 0;
acdedc
+
acdedc
     /* uninstantiate the three global DRBGs */
acdedc
     RAND_DRBG_uninstantiate(private);
acdedc
     RAND_DRBG_uninstantiate(public);
7d1228
@@ -964,7 +995,8 @@ static int test_rand_seed(void)
acdedc
     size_t rand_buflen;
acdedc
     size_t required_seed_buflen = 0;
acdedc
 
acdedc
-    if (!TEST_ptr(master = RAND_DRBG_get0_master()))
acdedc
+    if (!TEST_ptr(master = RAND_DRBG_get0_master())
acdedc
+        || !TEST_true(disable_crngt(master)))
acdedc
         return 0;
acdedc
 
acdedc
 #ifdef OPENSSL_RAND_SEED_NONE
7d1228
@@ -1013,6 +1045,95 @@ static int test_rand_add(void)
acdedc
     return 1;
acdedc
 }
acdedc
 
acdedc
+/*
acdedc
+ * A list of the FIPS DRGB types.
acdedc
+ */
acdedc
+static const struct s_drgb_types {
acdedc
+    int nid;
acdedc
+    int flags;
acdedc
+} drgb_types[] = {
acdedc
+    { NID_aes_128_ctr,  0                   },
acdedc
+    { NID_aes_192_ctr,  0                   },
acdedc
+    { NID_aes_256_ctr,  0                   },
acdedc
+};
acdedc
+
acdedc
+/* Six cases for each covers seed sizes up to 32 bytes */
acdedc
+static const size_t crngt_num_cases = 6;
acdedc
+
acdedc
+static size_t crngt_case, crngt_idx;
acdedc
+
acdedc
+static int crngt_entropy_cb(unsigned char *buf, unsigned char *md,
acdedc
+                            unsigned int *md_size)
acdedc
+{
acdedc
+    size_t i, z;
acdedc
+
acdedc
+    if (!TEST_int_lt(crngt_idx, crngt_num_cases))
acdedc
+        return 0;
acdedc
+    /* Generate a block of unique data unless this is the duplication point */
acdedc
+    z = crngt_idx++;
acdedc
+    if (z > 0 && crngt_case == z)
acdedc
+        z--;
acdedc
+    for (i = 0; i < CRNGT_BUFSIZ; i++)
acdedc
+        buf[i] = (unsigned char)(i + 'A' + z);
acdedc
+    return EVP_Digest(buf, CRNGT_BUFSIZ, md, md_size, EVP_sha256(), NULL);
acdedc
+}
acdedc
+
acdedc
+static int test_crngt(int n)
acdedc
+{
acdedc
+    const struct s_drgb_types *dt = drgb_types + n / crngt_num_cases;
acdedc
+    RAND_DRBG *drbg = NULL;
acdedc
+    unsigned char buff[100];
acdedc
+    size_t ent;
acdedc
+    int res = 0;
acdedc
+    int expect;
acdedc
+
acdedc
+    if (!TEST_true(rand_crngt_single_init()))
acdedc
+        return 0;
acdedc
+    rand_crngt_cleanup();
acdedc
+
acdedc
+    if (!TEST_ptr(drbg = RAND_DRBG_new(dt->nid, dt->flags, NULL)))
acdedc
+        return 0;
acdedc
+    ent = (drbg->min_entropylen + CRNGT_BUFSIZ - 1) / CRNGT_BUFSIZ;
acdedc
+    crngt_case = n % crngt_num_cases;
acdedc
+    crngt_idx = 0;
acdedc
+    crngt_get_entropy = &crngt_entropy_cb;
acdedc
+    if (!TEST_true(rand_crngt_init()))
acdedc
+        goto err;
acdedc
+#ifndef OPENSSL_FIPS
acdedc
+    if (!TEST_true(RAND_DRBG_set_callbacks(drbg, &rand_crngt_get_entropy,
acdedc
+                                           &rand_crngt_cleanup_entropy,
acdedc
+                                           &rand_drbg_get_nonce,
acdedc
+                                           &rand_drbg_cleanup_nonce)))
acdedc
+        goto err;
acdedc
+#endif
acdedc
+    expect = crngt_case == 0 || crngt_case > ent;
acdedc
+    if (!TEST_int_eq(RAND_DRBG_instantiate(drbg, NULL, 0), expect))
acdedc
+        goto err;
acdedc
+    if (!expect)
acdedc
+        goto fin;
acdedc
+    if (!TEST_true(RAND_DRBG_generate(drbg, buff, sizeof(buff), 0, NULL, 0)))
acdedc
+        goto err;
acdedc
+
acdedc
+    expect = crngt_case == 0 || crngt_case > 2 * ent;
acdedc
+    if (!TEST_int_eq(RAND_DRBG_reseed(drbg, NULL, 0, 0), expect))
acdedc
+        goto err;
acdedc
+    if (!expect)
acdedc
+        goto fin;
acdedc
+    if (!TEST_true(RAND_DRBG_generate(drbg, buff, sizeof(buff), 0, NULL, 0)))
acdedc
+        goto err;
acdedc
+
acdedc
+fin:
acdedc
+    res = 1;
acdedc
+err:
acdedc
+    if (!res)
acdedc
+        TEST_note("DRBG %zd case %zd block %zd", n / crngt_num_cases,
acdedc
+                  crngt_case, crngt_idx);
acdedc
+    uninstantiate(drbg);
acdedc
+    RAND_DRBG_free(drbg);
acdedc
+    crngt_get_entropy = &rand_crngt_get_entropy_cb;
acdedc
+    return res;
acdedc
+}
acdedc
+
acdedc
 int setup_tests(void)
acdedc
 {
acdedc
     app_data_index = RAND_DRBG_get_ex_new_index(0L, NULL, NULL, NULL, NULL);
7d1228
@@ -1025,5 +1146,6 @@ int setup_tests(void)
acdedc
 #if defined(OPENSSL_THREADS)
acdedc
     ADD_TEST(test_multi_thread);
acdedc
 #endif
acdedc
+    ADD_ALL_TESTS(test_crngt, crngt_num_cases * OSSL_NELEM(drgb_types));
acdedc
     return 1;
acdedc
 }