Blame SOURCES/openssl-1.1.1-fips-crng-test.patch

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