Blame SOURCES/openssl-1.1.1-fips-drbg-selftest.patch

7d1228
diff -up openssl-1.1.1g/crypto/fips/fips_post.c.drbg-selftest openssl-1.1.1g/crypto/fips/fips_post.c
7d1228
--- openssl-1.1.1g/crypto/fips/fips_post.c.drbg-selftest	2020-04-23 13:33:12.500624151 +0200
7d1228
+++ openssl-1.1.1g/crypto/fips/fips_post.c	2020-04-23 13:33:12.618621925 +0200
7d1228
@@ -67,12 +67,18 @@
7d1228
 
7d1228
 # include <openssl/fips.h>
7d1228
 # include "crypto/fips.h"
7d1228
+# include "crypto/rand.h"
7d1228
 # include "fips_locl.h"
7d1228
 
7d1228
 /* Run all selftests */
7d1228
 int FIPS_selftest(void)
7d1228
 {
7d1228
     int rv = 1;
7d1228
+    if (!rand_drbg_selftest()) {
7d1228
+        FIPSerr(FIPS_F_FIPS_SELFTEST, FIPS_R_TEST_FAILURE);
7d1228
+        ERR_add_error_data(2, "Type=", "rand_drbg_selftest");
7d1228
+        rv = 0;
7d1228
+    }
7d1228
     if (!FIPS_selftest_drbg())
7d1228
         rv = 0;
7d1228
     if (!FIPS_selftest_sha1())
7d1228
diff -up openssl-1.1.1g/crypto/rand/build.info.drbg-selftest openssl-1.1.1g/crypto/rand/build.info
7d1228
--- openssl-1.1.1g/crypto/rand/build.info.drbg-selftest	2020-04-23 13:33:12.619621907 +0200
7d1228
+++ openssl-1.1.1g/crypto/rand/build.info	2020-04-23 13:34:10.857523497 +0200
7d1228
@@ -1,6 +1,6 @@
7d1228
 LIBS=../../libcrypto
7d1228
 SOURCE[../../libcrypto]=\
7d1228
         randfile.c rand_lib.c rand_err.c rand_crng_test.c rand_egd.c \
7d1228
-        rand_win.c rand_unix.c rand_vms.c drbg_lib.c drbg_ctr.c
7d1228
+        rand_win.c rand_unix.c rand_vms.c drbg_lib.c drbg_ctr.c drbg_selftest.c
7d1228
 
7d1228
 INCLUDE[drbg_ctr.o]=../modes
7d1228
diff -up openssl-1.1.1g/crypto/rand/drbg_selftest.c.drbg-selftest openssl-1.1.1g/crypto/rand/drbg_selftest.c
7d1228
--- openssl-1.1.1g/crypto/rand/drbg_selftest.c.drbg-selftest	2020-04-23 13:33:12.619621907 +0200
7d1228
+++ openssl-1.1.1g/crypto/rand/drbg_selftest.c	2020-04-23 13:33:12.619621907 +0200
7d1228
@@ -0,0 +1,537 @@
7d1228
+/*
7d1228
+ * Copyright 2017-2019 The OpenSSL Project Authors. All Rights Reserved.
7d1228
+ *
7d1228
+ * Licensed under the OpenSSL license (the "License").  You may not use
7d1228
+ * this file except in compliance with the License.  You can obtain a copy
7d1228
+ * in the file LICENSE in the source distribution or at
7d1228
+ * https://www.openssl.org/source/license.html
7d1228
+ */
7d1228
+
7d1228
+#include <string.h>
7d1228
+#include <stddef.h>
7d1228
+#include "internal/nelem.h"
7d1228
+#include <openssl/crypto.h>
7d1228
+#include <openssl/err.h>
7d1228
+#include <openssl/rand_drbg.h>
7d1228
+#include <openssl/obj_mac.h>
7d1228
+#include "internal/thread_once.h"
7d1228
+#include "crypto/rand.h"
7d1228
+
7d1228
+typedef struct test_ctx_st {
7d1228
+    const unsigned char *entropy;
7d1228
+    size_t entropylen;
7d1228
+    int entropycnt;
7d1228
+    const unsigned char *nonce;
7d1228
+    size_t noncelen;
7d1228
+    int noncecnt;
7d1228
+} TEST_CTX;
7d1228
+
7d1228
+static int app_data_index = -1;
7d1228
+static CRYPTO_ONCE get_index_once = CRYPTO_ONCE_STATIC_INIT;
7d1228
+DEFINE_RUN_ONCE_STATIC(drbg_app_data_index_init)
7d1228
+{
7d1228
+    app_data_index = RAND_DRBG_get_ex_new_index(0L, NULL, NULL, NULL, NULL);
7d1228
+
7d1228
+    return 1;
7d1228
+}
7d1228
+
7d1228
+enum drbg_kat_type {
7d1228
+    NO_RESEED,
7d1228
+    PR_FALSE,
7d1228
+    PR_TRUE
7d1228
+};
7d1228
+
7d1228
+enum drbg_df {
7d1228
+    USE_DF,
7d1228
+    NO_DF,
7d1228
+    NA
7d1228
+};
7d1228
+
7d1228
+struct drbg_kat_no_reseed {
7d1228
+    size_t count;
7d1228
+    const unsigned char *entropyin;
7d1228
+    const unsigned char *nonce;
7d1228
+    const unsigned char *persstr;
7d1228
+    const unsigned char *addin1;
7d1228
+    const unsigned char *addin2;
7d1228
+    const unsigned char *retbytes;
7d1228
+};
7d1228
+
7d1228
+struct drbg_kat_pr_false {
7d1228
+    size_t count;
7d1228
+    const unsigned char *entropyin;
7d1228
+    const unsigned char *nonce;
7d1228
+    const unsigned char *persstr;
7d1228
+    const unsigned char *entropyinreseed;
7d1228
+    const unsigned char *addinreseed;
7d1228
+    const unsigned char *addin1;
7d1228
+    const unsigned char *addin2;
7d1228
+    const unsigned char *retbytes;
7d1228
+};
7d1228
+
7d1228
+struct drbg_kat_pr_true {
7d1228
+    size_t count;
7d1228
+    const unsigned char *entropyin;
7d1228
+    const unsigned char *nonce;
7d1228
+    const unsigned char *persstr;
7d1228
+    const unsigned char *entropyinpr1;
7d1228
+    const unsigned char *addin1;
7d1228
+    const unsigned char *entropyinpr2;
7d1228
+    const unsigned char *addin2;
7d1228
+    const unsigned char *retbytes;
7d1228
+};
7d1228
+
7d1228
+struct drbg_kat {
7d1228
+    enum drbg_kat_type type;
7d1228
+    enum drbg_df df;
7d1228
+    int nid;
7d1228
+
7d1228
+    size_t entropyinlen;
7d1228
+    size_t noncelen;
7d1228
+    size_t persstrlen;
7d1228
+    size_t addinlen;
7d1228
+    size_t retbyteslen;
7d1228
+
7d1228
+    const void *t;
7d1228
+};
7d1228
+
7d1228
+/*
7d1228
+ * Excerpt from test/drbg_cavs_data.c
7d1228
+ * DRBG test vectors from:
7d1228
+ * https://csrc.nist.gov/projects/cryptographic-algorithm-validation-program/
7d1228
+ */
7d1228
+
7d1228
+static const unsigned char kat1308_entropyin[] = {
7d1228
+    0x7c, 0x5d, 0x90, 0x70, 0x3b, 0x8a, 0xc7, 0x0f, 0x23, 0x73, 0x24, 0x9c,
7d1228
+    0xa7, 0x15, 0x41, 0x71, 0x7a, 0x31, 0xea, 0x32, 0xfc, 0x28, 0x0d, 0xd7,
7d1228
+    0x5b, 0x09, 0x01, 0x98, 0x1b, 0xe2, 0xa5, 0x53, 0xd9, 0x05, 0x32, 0x97,
7d1228
+    0xec, 0xbe, 0x86, 0xfd, 0x1c, 0x1c, 0x71, 0x4c, 0x52, 0x29, 0x9e, 0x52,
7d1228
+};
7d1228
+static const unsigned char kat1308_nonce[] = {0};
7d1228
+static const unsigned char kat1308_persstr[] = {
7d1228
+    0xdc, 0x07, 0x2f, 0x68, 0xfa, 0x77, 0x03, 0x23, 0x42, 0xb0, 0xf5, 0xa2,
7d1228
+    0xd9, 0xad, 0xa1, 0xd0, 0xad, 0xa2, 0x14, 0xb4, 0xd0, 0x8e, 0xfb, 0x39,
7d1228
+    0xdd, 0xc2, 0xac, 0xfb, 0x98, 0xdf, 0x7f, 0xce, 0x4c, 0x75, 0x56, 0x45,
7d1228
+    0xcd, 0x86, 0x93, 0x74, 0x90, 0x6e, 0xf6, 0x9e, 0x85, 0x7e, 0xfb, 0xc3,
7d1228
+};
7d1228
+static const unsigned char kat1308_addin0[] = {
7d1228
+    0x52, 0x25, 0xc4, 0x2f, 0x03, 0xce, 0x29, 0x71, 0xc5, 0x0b, 0xc3, 0x4e,
7d1228
+    0xad, 0x8d, 0x6f, 0x17, 0x82, 0xe1, 0xf3, 0xfd, 0xfd, 0x9b, 0x94, 0x9a,
7d1228
+    0x1d, 0xac, 0xd0, 0xd4, 0x3f, 0x2b, 0xe3, 0xab, 0x7c, 0x3d, 0x3e, 0x5a,
7d1228
+    0x68, 0xbb, 0xa4, 0x74, 0x68, 0x1a, 0xc6, 0x27, 0xff, 0xe0, 0xc0, 0x6c,
7d1228
+};
7d1228
+static const unsigned char kat1308_addin1[] = {
7d1228
+    0xdc, 0x91, 0xd7, 0xb7, 0xb9, 0x94, 0x79, 0x0f, 0x06, 0xc4, 0x70, 0x19,
7d1228
+    0x33, 0x25, 0x7c, 0x96, 0x01, 0xa0, 0x62, 0xb0, 0x50, 0xe6, 0xc0, 0x3a,
7d1228
+    0x56, 0x8f, 0xc5, 0x50, 0x48, 0xc6, 0xf4, 0x49, 0xe5, 0x70, 0x16, 0x2e,
7d1228
+    0xae, 0xf2, 0x99, 0xb4, 0x2d, 0x70, 0x18, 0x16, 0xcd, 0xe0, 0x24, 0xe4,
7d1228
+};
7d1228
+static const unsigned char kat1308_retbits[] = {
7d1228
+    0xde, 0xf8, 0x91, 0x1b, 0xf1, 0xe1, 0xa9, 0x97, 0xd8, 0x61, 0x84, 0xe2,
7d1228
+    0xdb, 0x83, 0x3e, 0x60, 0x45, 0xcd, 0xc8, 0x66, 0x93, 0x28, 0xc8, 0x92,
7d1228
+    0xbc, 0x25, 0xae, 0xe8, 0xb0, 0xed, 0xed, 0x16, 0x3d, 0xa5, 0xf9, 0x0f,
7d1228
+    0xb3, 0x72, 0x08, 0x84, 0xac, 0x3c, 0x3b, 0xaa, 0x5f, 0xf9, 0x7d, 0x63,
7d1228
+    0x3e, 0xde, 0x59, 0x37, 0x0e, 0x40, 0x12, 0x2b, 0xbc, 0x6c, 0x96, 0x53,
7d1228
+    0x26, 0x32, 0xd0, 0xb8,
7d1228
+};
7d1228
+static const struct drbg_kat_no_reseed kat1308_t = {
7d1228
+    2, kat1308_entropyin, kat1308_nonce, kat1308_persstr,
7d1228
+    kat1308_addin0, kat1308_addin1, kat1308_retbits
7d1228
+};
7d1228
+static const struct drbg_kat kat1308 = {
7d1228
+    NO_RESEED, NO_DF, NID_aes_256_ctr, 48, 0, 48, 48, 64, &kat1308_t
7d1228
+};
7d1228
+
7d1228
+static const unsigned char kat1465_entropyin[] = {
7d1228
+    0xc9, 0x96, 0x3a, 0x15, 0x51, 0x76, 0x4f, 0xe0, 0x45, 0x82, 0x8a, 0x64,
7d1228
+    0x87, 0xbe, 0xaa, 0xc0,
7d1228
+};
7d1228
+static const unsigned char kat1465_nonce[] = {
7d1228
+    0x08, 0xcd, 0x69, 0x39, 0xf8, 0x58, 0x9a, 0x85,
7d1228
+};
7d1228
+static const unsigned char kat1465_persstr[] = {0};
7d1228
+static const unsigned char kat1465_entropyinreseed[] = {
7d1228
+    0x16, 0xcc, 0x35, 0x15, 0xb1, 0x17, 0xf5, 0x33, 0x80, 0x9a, 0x80, 0xc5,
7d1228
+    0x1f, 0x4b, 0x7b, 0x51,
7d1228
+};
7d1228
+static const unsigned char kat1465_addinreseed[] = {
7d1228
+    0xf5, 0x3d, 0xf1, 0x2e, 0xdb, 0x28, 0x1c, 0x00, 0x7b, 0xcb, 0xb6, 0x12,
7d1228
+    0x61, 0x9f, 0x26, 0x5f,
7d1228
+};
7d1228
+static const unsigned char kat1465_addin0[] = {
7d1228
+    0xe2, 0x67, 0x06, 0x62, 0x09, 0xa7, 0xcf, 0xd6, 0x84, 0x8c, 0x20, 0xf6,
7d1228
+    0x10, 0x5a, 0x73, 0x9c,
7d1228
+};
7d1228
+static const unsigned char kat1465_addin1[] = {
7d1228
+    0x26, 0xfa, 0x50, 0xe1, 0xb3, 0xcb, 0x65, 0xed, 0xbc, 0x6d, 0xda, 0x18,
7d1228
+    0x47, 0x99, 0x1f, 0xeb,
7d1228
+};
7d1228
+static const unsigned char kat1465_retbits[] = {
7d1228
+    0xf9, 0x47, 0xc6, 0xb0, 0x58, 0xa8, 0x66, 0x8a, 0xf5, 0x2b, 0x2a, 0x6d,
7d1228
+    0x4e, 0x24, 0x6f, 0x65, 0xbf, 0x51, 0x22, 0xbf, 0xe8, 0x8d, 0x6c, 0xeb,
7d1228
+    0xf9, 0x68, 0x7f, 0xed, 0x3b, 0xdd, 0x6b, 0xd5, 0x28, 0x47, 0x56, 0x52,
7d1228
+    0xda, 0x50, 0xf0, 0x90, 0x73, 0x95, 0x06, 0x58, 0xaf, 0x08, 0x98, 0x6e,
7d1228
+    0x24, 0x18, 0xfd, 0x2f, 0x48, 0x72, 0x57, 0xd6, 0x59, 0xab, 0xe9, 0x41,
7d1228
+    0x58, 0xdb, 0x27, 0xba,
7d1228
+};
7d1228
+static const struct drbg_kat_pr_false kat1465_t = {
7d1228
+    9, kat1465_entropyin, kat1465_nonce, kat1465_persstr,
7d1228
+    kat1465_entropyinreseed, kat1465_addinreseed, kat1465_addin0,
7d1228
+    kat1465_addin1, kat1465_retbits
7d1228
+};
7d1228
+static const struct drbg_kat kat1465 = {
7d1228
+    PR_FALSE, USE_DF, NID_aes_128_ctr, 16, 8, 0, 16, 64, &kat1465_t
7d1228
+};
7d1228
+
7d1228
+static const unsigned char kat3146_entropyin[] = {
7d1228
+    0xd7, 0x08, 0x42, 0x82, 0xc2, 0xd2, 0xd1, 0xde, 0x01, 0xb4, 0x36, 0xb3,
7d1228
+    0x7f, 0xbd, 0xd3, 0xdd, 0xb3, 0xc4, 0x31, 0x4f, 0x8f, 0xa7, 0x10, 0xf4,
7d1228
+};
7d1228
+static const unsigned char kat3146_nonce[] = {
7d1228
+    0x7b, 0x9e, 0xcd, 0x49, 0x4f, 0x46, 0xa0, 0x08, 0x32, 0xff, 0x2e, 0xc3,
7d1228
+    0x50, 0x86, 0xca, 0xca,
7d1228
+};
7d1228
+static const unsigned char kat3146_persstr[] = {0};
7d1228
+static const unsigned char kat3146_entropyinpr1[] = {
7d1228
+    0x68, 0xd0, 0x7b, 0xa4, 0xe7, 0x22, 0x19, 0xe6, 0xb6, 0x46, 0x6a, 0xda,
7d1228
+    0x8e, 0x67, 0xea, 0x63, 0x3f, 0xaf, 0x2f, 0x6c, 0x9d, 0x5e, 0x48, 0x15,
7d1228
+};
7d1228
+static const unsigned char kat3146_addinpr1[] = {
7d1228
+    0x70, 0x0f, 0x54, 0xf4, 0x53, 0xde, 0xca, 0x61, 0x5c, 0x49, 0x51, 0xd1,
7d1228
+    0x41, 0xc4, 0xf1, 0x2f, 0x65, 0xfb, 0x7e, 0xbc, 0x9b, 0x14, 0xba, 0x90,
7d1228
+    0x05, 0x33, 0x7e, 0x64, 0xb7, 0x2b, 0xaf, 0x99,
7d1228
+};
7d1228
+static const unsigned char kat3146_entropyinpr2[] = {
7d1228
+    0xeb, 0x77, 0xb0, 0xe9, 0x2d, 0x31, 0xc8, 0x66, 0xc5, 0xc4, 0xa7, 0xf7,
7d1228
+    0x6c, 0xb2, 0x74, 0x36, 0x4b, 0x25, 0x78, 0x04, 0xd8, 0xd7, 0xd2, 0x34,
7d1228
+};
7d1228
+static const unsigned char kat3146_addinpr2[] = {
7d1228
+    0x05, 0xcd, 0x2a, 0x97, 0x5a, 0x5d, 0xfb, 0x98, 0xc1, 0xf1, 0x00, 0x0c,
7d1228
+    0xed, 0xe6, 0x2a, 0xba, 0xf0, 0x89, 0x1f, 0x5a, 0x4f, 0xd7, 0x48, 0xb3,
7d1228
+    0x24, 0xc0, 0x8a, 0x3d, 0x60, 0x59, 0x5d, 0xb6,
7d1228
+};
7d1228
+static const unsigned char kat3146_retbits[] = {
7d1228
+    0x29, 0x94, 0xa4, 0xa8, 0x17, 0x3e, 0x62, 0x2f, 0x94, 0xdd, 0x40, 0x1f,
7d1228
+    0xe3, 0x7e, 0x77, 0xd4, 0x38, 0xbc, 0x0e, 0x49, 0x46, 0xf6, 0x0e, 0x28,
7d1228
+    0x91, 0xc6, 0x9c, 0xc4, 0xa6, 0xa1, 0xf8, 0x9a, 0x64, 0x5e, 0x99, 0x76,
7d1228
+    0xd0, 0x2d, 0xee, 0xde, 0xe1, 0x2c, 0x93, 0x29, 0x4b, 0x12, 0xcf, 0x87,
7d1228
+    0x03, 0x98, 0xb9, 0x74, 0x41, 0xdb, 0x3a, 0x49, 0x9f, 0x92, 0xd0, 0x45,
7d1228
+    0xd4, 0x30, 0x73, 0xbb,
7d1228
+};
7d1228
+static const struct drbg_kat_pr_true kat3146_t = {
7d1228
+    10, kat3146_entropyin, kat3146_nonce, kat3146_persstr,
7d1228
+    kat3146_entropyinpr1, kat3146_addinpr1, kat3146_entropyinpr2,
7d1228
+    kat3146_addinpr2, kat3146_retbits
7d1228
+};
7d1228
+static const struct drbg_kat kat3146 = {
7d1228
+    PR_TRUE, USE_DF, NID_aes_192_ctr, 24, 16, 0, 32, 64, &kat3146_t
7d1228
+};
7d1228
+
7d1228
+static const struct drbg_kat *drbg_test[] = { &kat1308, &kat1465, &kat3146 };
7d1228
+
7d1228
+static const size_t drbg_test_nelem = OSSL_NELEM(drbg_test);
7d1228
+
7d1228
+static size_t kat_entropy(RAND_DRBG *drbg, unsigned char **pout,
7d1228
+                          int entropy, size_t min_len, size_t max_len,
7d1228
+                          int prediction_resistance)
7d1228
+{
7d1228
+    TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
7d1228
+
7d1228
+    t->entropycnt++;
7d1228
+    *pout = (unsigned char *)t->entropy;
7d1228
+    return t->entropylen;
7d1228
+}
7d1228
+
7d1228
+static size_t kat_nonce(RAND_DRBG *drbg, unsigned char **pout,
7d1228
+                        int entropy, size_t min_len, size_t max_len)
7d1228
+{
7d1228
+    TEST_CTX *t = (TEST_CTX *)RAND_DRBG_get_ex_data(drbg, app_data_index);
7d1228
+
7d1228
+    t->noncecnt++;
7d1228
+    *pout = (unsigned char *)t->nonce;
7d1228
+    return t->noncelen;
7d1228
+}
7d1228
+
7d1228
+/*
7d1228
+ * Do a single NO_RESEED KAT:
7d1228
+ *
7d1228
+ * Instantiate
7d1228
+ * Generate Random Bits (pr=false)
7d1228
+ * Generate Random Bits (pr=false)
7d1228
+ * Uninstantiate
7d1228
+ *
7d1228
+ * Return 0 on failure.
7d1228
+ */
7d1228
+static int single_kat_no_reseed(const struct drbg_kat *td)
7d1228
+{
7d1228
+    struct drbg_kat_no_reseed *data = (struct drbg_kat_no_reseed *)td->t;
7d1228
+    RAND_DRBG *drbg = NULL;
7d1228
+    unsigned char *buff = NULL;
7d1228
+    unsigned int flags = 0;
7d1228
+    int failures = 0;
7d1228
+    TEST_CTX t;
7d1228
+
7d1228
+    if (td->df != USE_DF)
7d1228
+        flags |= RAND_DRBG_FLAG_CTR_NO_DF;
7d1228
+
7d1228
+    if ((drbg = RAND_DRBG_new(td->nid, flags, NULL)) == NULL)
7d1228
+        return 0;
7d1228
+
7d1228
+    if (!RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
7d1228
+                                 kat_nonce, NULL)) {
7d1228
+        failures++;
7d1228
+        goto err;
7d1228
+    }
7d1228
+    memset(&t, 0, sizeof(t));
7d1228
+    t.entropy = data->entropyin;
7d1228
+    t.entropylen = td->entropyinlen;
7d1228
+    t.nonce = data->nonce;
7d1228
+    t.noncelen = td->noncelen;
7d1228
+    RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
7d1228
+
7d1228
+    buff = OPENSSL_malloc(td->retbyteslen);
7d1228
+    if (buff == NULL) {
7d1228
+        failures++;
7d1228
+        goto err;
7d1228
+    }
7d1228
+
7d1228
+    if (!RAND_DRBG_instantiate(drbg, data->persstr, td->persstrlen)
7d1228
+        || !RAND_DRBG_generate(drbg, buff, td->retbyteslen, 0,
7d1228
+                               data->addin1, td->addinlen)
7d1228
+        || !RAND_DRBG_generate(drbg, buff, td->retbyteslen, 0,
7d1228
+                               data->addin2, td->addinlen)
7d1228
+        || memcmp(data->retbytes, buff,
7d1228
+                  td->retbyteslen) != 0)
7d1228
+        failures++;
7d1228
+
7d1228
+err:
7d1228
+    OPENSSL_free(buff);
7d1228
+    RAND_DRBG_uninstantiate(drbg);
7d1228
+    RAND_DRBG_free(drbg);
7d1228
+    return failures == 0;
7d1228
+}
7d1228
+
7d1228
+/*-
7d1228
+ * Do a single PR_FALSE KAT:
7d1228
+ *
7d1228
+ * Instantiate
7d1228
+ * Reseed
7d1228
+ * Generate Random Bits (pr=false)
7d1228
+ * Generate Random Bits (pr=false)
7d1228
+ * Uninstantiate
7d1228
+ *
7d1228
+ * Return 0 on failure.
7d1228
+ */
7d1228
+static int single_kat_pr_false(const struct drbg_kat *td)
7d1228
+{
7d1228
+    struct drbg_kat_pr_false *data = (struct drbg_kat_pr_false *)td->t;
7d1228
+    RAND_DRBG *drbg = NULL;
7d1228
+    unsigned char *buff = NULL;
7d1228
+    unsigned int flags = 0;
7d1228
+    int failures = 0;
7d1228
+    TEST_CTX t;
7d1228
+
7d1228
+    if (td->df != USE_DF)
7d1228
+        flags |= RAND_DRBG_FLAG_CTR_NO_DF;
7d1228
+
7d1228
+    if ((drbg = RAND_DRBG_new(td->nid, flags, NULL)) == NULL)
7d1228
+        return 0;
7d1228
+
7d1228
+    if (!RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
7d1228
+                                 kat_nonce, NULL)) {
7d1228
+        failures++;
7d1228
+        goto err;
7d1228
+    }
7d1228
+    memset(&t, 0, sizeof(t));
7d1228
+    t.entropy = data->entropyin;
7d1228
+    t.entropylen = td->entropyinlen;
7d1228
+    t.nonce = data->nonce;
7d1228
+    t.noncelen = td->noncelen;
7d1228
+    RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
7d1228
+
7d1228
+    buff = OPENSSL_malloc(td->retbyteslen);
7d1228
+    if (buff == NULL) {
7d1228
+        failures++;
7d1228
+        goto err;
7d1228
+    }
7d1228
+
7d1228
+    if (!RAND_DRBG_instantiate(drbg, data->persstr, td->persstrlen))
7d1228
+        failures++;
7d1228
+
7d1228
+    t.entropy = data->entropyinreseed;
7d1228
+    t.entropylen = td->entropyinlen;
7d1228
+
7d1228
+    if (!RAND_DRBG_reseed(drbg, data->addinreseed, td->addinlen, 0)
7d1228
+        || !RAND_DRBG_generate(drbg, buff, td->retbyteslen, 0,
7d1228
+                               data->addin1, td->addinlen)
7d1228
+        || !RAND_DRBG_generate(drbg, buff, td->retbyteslen, 0,
7d1228
+                               data->addin2, td->addinlen)
7d1228
+        || memcmp(data->retbytes, buff,
7d1228
+                  td->retbyteslen) != 0)
7d1228
+        failures++;
7d1228
+
7d1228
+err:
7d1228
+    OPENSSL_free(buff);
7d1228
+    RAND_DRBG_uninstantiate(drbg);
7d1228
+    RAND_DRBG_free(drbg);
7d1228
+    return failures == 0;
7d1228
+}
7d1228
+
7d1228
+/*-
7d1228
+ * Do a single PR_TRUE KAT:
7d1228
+ *
7d1228
+ * Instantiate
7d1228
+ * Generate Random Bits (pr=true)
7d1228
+ * Generate Random Bits (pr=true)
7d1228
+ * Uninstantiate
7d1228
+ *
7d1228
+ * Return 0 on failure.
7d1228
+ */
7d1228
+static int single_kat_pr_true(const struct drbg_kat *td)
7d1228
+{
7d1228
+    struct drbg_kat_pr_true *data = (struct drbg_kat_pr_true *)td->t;
7d1228
+    RAND_DRBG *drbg = NULL;
7d1228
+    unsigned char *buff = NULL;
7d1228
+    unsigned int flags = 0;
7d1228
+    int failures = 0;
7d1228
+    TEST_CTX t;
7d1228
+
7d1228
+    if (td->df != USE_DF)
7d1228
+        flags |= RAND_DRBG_FLAG_CTR_NO_DF;
7d1228
+
7d1228
+    if ((drbg = RAND_DRBG_new(td->nid, flags, NULL)) == NULL)
7d1228
+        return 0;
7d1228
+
7d1228
+    if (!RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
7d1228
+                                 kat_nonce, NULL)) {
7d1228
+        failures++;
7d1228
+        goto err;
7d1228
+    }
7d1228
+    memset(&t, 0, sizeof(t));
7d1228
+    t.nonce = data->nonce;
7d1228
+    t.noncelen = td->noncelen;
7d1228
+    t.entropy = data->entropyin;
7d1228
+    t.entropylen = td->entropyinlen;
7d1228
+    RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
7d1228
+
7d1228
+    buff = OPENSSL_malloc(td->retbyteslen);
7d1228
+    if (buff == NULL) {
7d1228
+        failures++;
7d1228
+        goto err;
7d1228
+    }
7d1228
+
7d1228
+    if (!RAND_DRBG_instantiate(drbg, data->persstr, td->persstrlen))
7d1228
+        failures++;
7d1228
+
7d1228
+    t.entropy = data->entropyinpr1;
7d1228
+    t.entropylen = td->entropyinlen;
7d1228
+
7d1228
+    if (!RAND_DRBG_generate(drbg, buff, td->retbyteslen, 1,
7d1228
+                            data->addin1, td->addinlen))
7d1228
+        failures++;
7d1228
+
7d1228
+    t.entropy = data->entropyinpr2;
7d1228
+    t.entropylen = td->entropyinlen;
7d1228
+
7d1228
+    if (!RAND_DRBG_generate(drbg, buff, td->retbyteslen, 1,
7d1228
+                            data->addin2, td->addinlen)
7d1228
+        || memcmp(data->retbytes, buff,
7d1228
+                  td->retbyteslen) != 0)
7d1228
+        failures++;
7d1228
+
7d1228
+err:
7d1228
+    OPENSSL_free(buff);
7d1228
+    RAND_DRBG_uninstantiate(drbg);
7d1228
+    RAND_DRBG_free(drbg);
7d1228
+    return failures == 0;
7d1228
+}
7d1228
+
7d1228
+static int test_kats(int i)
7d1228
+{
7d1228
+    const struct drbg_kat *td = drbg_test[i];
7d1228
+    int rv = 0;
7d1228
+
7d1228
+    switch (td->type) {
7d1228
+    case NO_RESEED:
7d1228
+        if (!single_kat_no_reseed(td))
7d1228
+            goto err;
7d1228
+        break;
7d1228
+    case PR_FALSE:
7d1228
+        if (!single_kat_pr_false(td))
7d1228
+            goto err;
7d1228
+        break;
7d1228
+    case PR_TRUE:
7d1228
+        if (!single_kat_pr_true(td))
7d1228
+            goto err;
7d1228
+        break;
7d1228
+    default:	/* cant happen */
7d1228
+        goto err;
7d1228
+    }
7d1228
+    rv = 1;
7d1228
+err:
7d1228
+    return rv;
7d1228
+}
7d1228
+
7d1228
+/*-
7d1228
+ * Do one expected-error test:
7d1228
+ *
7d1228
+ * Instantiate with no entropy supplied
7d1228
+ *
7d1228
+ * Return 0 on failure.
7d1228
+ */
7d1228
+static int test_drbg_sanity(const struct drbg_kat *td)
7d1228
+{
7d1228
+    struct drbg_kat_pr_false *data = (struct drbg_kat_pr_false *)td->t;
7d1228
+    RAND_DRBG *drbg = NULL;
7d1228
+    unsigned int flags = 0;
7d1228
+    int failures = 0;
7d1228
+    TEST_CTX t;
7d1228
+
7d1228
+    if (td->df != USE_DF)
7d1228
+        flags |= RAND_DRBG_FLAG_CTR_NO_DF;
7d1228
+
7d1228
+    if ((drbg = RAND_DRBG_new(td->nid, flags, NULL)) == NULL)
7d1228
+        return 0;
7d1228
+
7d1228
+    if (!RAND_DRBG_set_callbacks(drbg, kat_entropy, NULL,
7d1228
+                                 kat_nonce, NULL)) {
7d1228
+        failures++;
7d1228
+        goto err;
7d1228
+    }
7d1228
+    memset(&t, 0, sizeof(t));
7d1228
+    t.entropy = data->entropyin;
7d1228
+    t.entropylen = 0;     /* No entropy */
7d1228
+    t.nonce = data->nonce;
7d1228
+    t.noncelen = td->noncelen;
7d1228
+    RAND_DRBG_set_ex_data(drbg, app_data_index, &t);
7d1228
+
7d1228
+    ERR_set_mark();
7d1228
+    /* This must fail. */
7d1228
+    if (RAND_DRBG_instantiate(drbg, data->persstr, td->persstrlen))
7d1228
+        failures++;
7d1228
+    RAND_DRBG_uninstantiate(drbg);
7d1228
+    ERR_pop_to_mark();
7d1228
+
7d1228
+err:
7d1228
+    RAND_DRBG_free(drbg);
7d1228
+    return failures == 0;
7d1228
+}
7d1228
+
7d1228
+
7d1228
+int rand_drbg_selftest(void)
7d1228
+{
7d1228
+    int i;
7d1228
+
7d1228
+    if (!RUN_ONCE(&get_index_once, drbg_app_data_index_init))
7d1228
+        return 0;
7d1228
+
7d1228
+    for (i = 0; i < drbg_test_nelem; i++) {
7d1228
+        if (test_kats(i) <= 0)
7d1228
+            return 0;
7d1228
+    }
7d1228
+
7d1228
+    if (test_drbg_sanity(&kat1465) <= 0)
7d1228
+        return 0;
7d1228
+
7d1228
+    return 1;
7d1228
+}
7d1228
diff -up openssl-1.1.1g/include/crypto/rand.h.drbg-selftest openssl-1.1.1g/include/crypto/rand.h
7d1228
--- openssl-1.1.1g/include/crypto/rand.h.drbg-selftest	2020-04-23 13:33:12.587622510 +0200
7d1228
+++ openssl-1.1.1g/include/crypto/rand.h	2020-04-23 13:33:12.619621907 +0200
7d1228
@@ -140,4 +140,9 @@ void rand_pool_cleanup(void);
7d1228
  */
7d1228
 void rand_pool_keep_random_devices_open(int keep);
7d1228
 
7d1228
+/*
7d1228
+ * Perform the DRBG KAT selftests
7d1228
+ */
7d1228
+int rand_drbg_selftest(void);
7d1228
+
7d1228
 #endif