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