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

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