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