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

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