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

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