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

acdedc
diff -up openssl-1.1.1b/crypto/include/internal/rand_int.h.crng-test openssl-1.1.1b/crypto/include/internal/rand_int.h
acdedc
--- openssl-1.1.1b/crypto/include/internal/rand_int.h.crng-test	2019-05-07 08:56:33.242179136 +0200
acdedc
+++ openssl-1.1.1b/crypto/include/internal/rand_int.h	2019-05-07 09:54:14.920204875 +0200
acdedc
@@ -49,6 +49,14 @@ size_t rand_drbg_get_additional_data(RAN
acdedc
 
acdedc
 void rand_drbg_cleanup_additional_data(RAND_POOL *pool, unsigned char *out);
acdedc
 
acdedc
+/* CRNG test entropy filter callbacks. */
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
+void rand_crngt_cleanup_entropy(RAND_DRBG *drbg,
acdedc
+                                unsigned char *out, size_t outlen);
acdedc
+
acdedc
 /*
acdedc
  * RAND_POOL functions
acdedc
  */
acdedc
diff -up openssl-1.1.1b/crypto/rand/build.info.crng-test openssl-1.1.1b/crypto/rand/build.info
acdedc
--- openssl-1.1.1b/crypto/rand/build.info.crng-test	2019-05-07 09:54:14.921204857 +0200
acdedc
+++ openssl-1.1.1b/crypto/rand/build.info	2019-05-07 09:55:22.730014705 +0200
acdedc
@@ -1,4 +1,4 @@
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
acdedc
diff -up openssl-1.1.1b/crypto/rand/drbg_lib.c.crng-test openssl-1.1.1b/crypto/rand/drbg_lib.c
acdedc
--- openssl-1.1.1b/crypto/rand/drbg_lib.c.crng-test	2019-02-26 15:15:30.000000000 +0100
acdedc
+++ openssl-1.1.1b/crypto/rand/drbg_lib.c	2019-05-07 10:04:51.753157224 +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;
acdedc
diff -up openssl-1.1.1b/crypto/rand/rand_crng_test.c.crng-test openssl-1.1.1b/crypto/rand/rand_crng_test.c
acdedc
--- openssl-1.1.1b/crypto/rand/rand_crng_test.c.crng-test	2019-05-07 09:54:14.925204787 +0200
acdedc
+++ openssl-1.1.1b/crypto/rand/rand_crng_test.c	2019-05-07 09:54:14.932204664 +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>
acdedc
+#include "internal/rand_int.h"
acdedc
+#include "internal/thread_once.h"
acdedc
+#include "rand_lcl.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
+
acdedc
+    if ((crngt_pool = rand_pool_new(0, 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
+
acdedc
+    if ((pool = rand_pool_new(entropy, 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
+}
acdedc
diff -up openssl-1.1.1b/crypto/rand/rand_lcl.h.crng-test openssl-1.1.1b/crypto/rand/rand_lcl.h
acdedc
--- openssl-1.1.1b/crypto/rand/rand_lcl.h.crng-test	2019-05-07 08:56:33.330177674 +0200
acdedc
+++ openssl-1.1.1b/crypto/rand/rand_lcl.h	2019-05-07 09:54:14.933204647 +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)
acdedc
@@ -44,7 +52,8 @@
acdedc
  */
acdedc
 # define DRBG_MAX_LENGTH                         INT32_MAX
acdedc
 
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
acdedc
@@ -290,4 +299,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
acdedc
diff -up openssl-1.1.1b/test/drbgtest.c.crng-test openssl-1.1.1b/test/drbgtest.c
acdedc
--- openssl-1.1.1b/test/drbgtest.c.crng-test	2019-02-26 15:15:30.000000000 +0100
acdedc
+++ openssl-1.1.1b/test/drbgtest.c	2019-05-07 10:06:24.706551561 +0200
acdedc
@@ -143,6 +143,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);
acdedc
@@ -168,7 +193,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
     }
acdedc
@@ -286,7 +312,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
     /*
acdedc
@@ -699,6 +726,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);
acdedc
@@ -919,7 +950,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
acdedc
@@ -968,6 +1000,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);
acdedc
@@ -980,5 +1101,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
 }