test: add framework for generic fake random number generator
authorPauli <ppzgs1@gmail.com>
Wed, 17 Feb 2021 01:54:01 +0000 (11:54 +1000)
committerPauli <ppzgs1@gmail.com>
Tue, 23 Feb 2021 13:24:41 +0000 (23:24 +1000)
Reviewed-by: Tim Hudson <tjh@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/13652)

test/build.info
test/testutil.h
test/testutil/driver.c
test/testutil/fake_random.c [new file with mode: 0644]

index 3f65d68b8c831726e73fa26fddab83f2abff602d..5bf35dcb10d9a45c3fec0f53979123abc6200fea 100644 (file)
@@ -20,7 +20,7 @@ IF[{- !$disabled{tests} -}]
   LIBS{noinst,has_main}=libtestutil.a
   SOURCE[libtestutil.a]=testutil/basic_output.c testutil/output.c \
           testutil/driver.c testutil/tests.c testutil/cb.c testutil/stanza.c \
-          testutil/format_output.c testutil/load.c \
+          testutil/format_output.c testutil/load.c testutil/fake_random.c \
           testutil/test_cleanup.c testutil/main.c testutil/testutil_init.c \
           testutil/options.c testutil/test_options.c testutil/provider.c \
           testutil/apps_mem.c testutil/random.c $LIBAPPSSRC
index 491082c3f4ca3cb0fe8c9592ef7eb12233ae77dc..93c91a4a41aea24ee635c79bfc9711e1a4d9e16c 100644 (file)
@@ -566,6 +566,11 @@ char *glue_strings(const char *list[], size_t *out_len);
 uint32_t test_random(void);
 void test_random_seed(uint32_t sd);
 
+/* Fake non-secure random number generator */
+OSSL_PROVIDER *fake_rand_start(OSSL_LIB_CTX *libctx);
+void fake_rand_finish(OSSL_PROVIDER *p);
+void fake_rand_set_callback(int (*cb)(unsigned char *out, size_t outlen));
+
 /* Create a file path from a directory and a filename */
 char *test_mk_file_path(const char *dir, const char *file);
 
index 0b4332b4926b1b92dbef3a743dcd25dcf3d65f8e..467c3e8eb300e19c76dbd07b60f9a0fee6d7134a 100644 (file)
@@ -44,6 +44,8 @@ static int single_test = -1;
 static int single_iter = -1;
 static int level = 0;
 static int seed = 0;
+static int rand_order = 0;
+
 /*
  * A parameterised test runs a loop of test cases.
  * |num_test_cases| counts the total number of test cases
@@ -103,8 +105,12 @@ int setup_test_framework(int argc, char *argv[])
     if (TAP_levels != NULL)
         level = 4 * atoi(TAP_levels);
     test_adjust_streams_tap_level(level);
-    if (test_seed != NULL)
+    if (test_seed != NULL) {
+        rand_order = 1;
         set_seed(atoi(test_seed));
+    } else {
+        set_seed(0);
+    }
 
 #if defined(OPENSSL_SYS_VMS) && defined(__DECC)
     argv = copy_argv(&argc, argv);
@@ -294,7 +300,7 @@ int run_tests(const char *test_prog_name)
 
     for (i = 0; i < num_tests; i++)
         permute[i] = i;
-    if (seed != 0)
+    if (rand_order != 0)
         for (i = num_tests - 1; i >= 1; i--) {
             j = test_random() % (1 + i);
             ii = permute[j];
@@ -340,7 +346,7 @@ int run_tests(const char *test_prog_name)
             }
 
             j = -1;
-            if (seed == 0 || all_tests[i].num < 3)
+            if (rand_order == 0 || all_tests[i].num < 3)
                 jstep = 1;
             else
                 do
diff --git a/test/testutil/fake_random.c b/test/testutil/fake_random.c
new file mode 100644 (file)
index 0000000..95a3023
--- /dev/null
@@ -0,0 +1,192 @@
+/*
+ * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
+ *
+ * Licensed under the Apache License 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * https://www.openssl.org/source/license.html
+ * or in the file LICENSE in the source distribution.
+ */
+
+#include <string.h>
+#include <openssl/core_names.h>
+#include <openssl/rand.h>
+#include <openssl/provider.h>
+#include "../testutil.h"
+
+typedef struct {
+    int (*cb)(unsigned char *out, size_t outlen);
+    int state;
+} FAKE_RAND;
+
+static FAKE_RAND fake_rand;
+
+static OSSL_FUNC_rand_newctx_fn fake_rand_newctx;
+static OSSL_FUNC_rand_freectx_fn fake_rand_freectx;
+static OSSL_FUNC_rand_instantiate_fn fake_rand_instantiate;
+static OSSL_FUNC_rand_uninstantiate_fn fake_rand_uninstantiate;
+static OSSL_FUNC_rand_generate_fn fake_rand_generate;
+static OSSL_FUNC_rand_gettable_ctx_params_fn fake_rand_gettable_ctx_params;
+static OSSL_FUNC_rand_get_ctx_params_fn fake_rand_get_ctx_params;
+static OSSL_FUNC_rand_enable_locking_fn fake_rand_enable_locking;
+
+static void *fake_rand_newctx(void *provctx, void *parent,
+                              const OSSL_DISPATCH *parent_dispatch)
+{
+    fake_rand.cb = NULL;
+    fake_rand.state = EVP_RAND_STATE_UNINITIALISED;
+    return &fake_rand;
+}
+
+static void fake_rand_freectx(void *vrng)
+{
+    FAKE_RAND *frng = (FAKE_RAND *)vrng;
+
+    frng->cb = NULL;
+    frng->state = EVP_RAND_STATE_UNINITIALISED;
+}
+
+static int fake_rand_instantiate(void *vrng, ossl_unused unsigned int strength,
+                                 ossl_unused  int prediction_resistance,
+                                 ossl_unused const unsigned char *pstr,
+                                 size_t pstr_len)
+{
+    FAKE_RAND *frng = (FAKE_RAND *)vrng;
+
+    frng->state = EVP_RAND_STATE_READY;
+    return 1;
+}
+
+static int fake_rand_uninstantiate(void *vrng)
+{
+    FAKE_RAND *frng = (FAKE_RAND *)vrng;
+
+    frng->state = EVP_RAND_STATE_UNINITIALISED;
+    return 1;
+}
+
+static int fake_rand_generate(void *vrng, unsigned char *out, size_t outlen,
+                              unsigned int strength, int prediction_resistance,
+                              const unsigned char *adin, size_t adinlen)
+{
+    FAKE_RAND *frng = (FAKE_RAND *)vrng;
+    size_t l;
+    uint32_t r;
+
+    if (frng->cb != NULL)
+        return (*frng->cb)(out, outlen);
+    while (outlen > 0) {
+        r = test_random();
+        l = outlen < sizeof(r) ? outlen : sizeof(r);
+
+        memcpy(out, &r, l);
+        out += l;
+        outlen -= l;
+    }
+    return 1;
+}
+
+static int fake_rand_enable_locking(void *vrng)
+{
+    return 1;
+}
+
+static int fake_rand_get_ctx_params(ossl_unused void *vrng, OSSL_PARAM params[])
+{
+    FAKE_RAND *frng = (FAKE_RAND *)vrng;
+    OSSL_PARAM *p;
+
+    p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STATE);
+    if (p != NULL && !OSSL_PARAM_set_int(p, frng->state))
+        return 0;
+
+    p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STRENGTH);
+    if (p != NULL && !OSSL_PARAM_set_int(p, 256))
+        return 0;
+
+    p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_MAX_REQUEST);
+    if (p != NULL && !OSSL_PARAM_set_size_t(p, INT_MAX))
+        return 0;
+    return 1;
+}
+
+static const OSSL_PARAM *fake_rand_gettable_ctx_params(void *vrng)
+{
+    static const OSSL_PARAM known_gettable_ctx_params[] = {
+        OSSL_PARAM_int(OSSL_RAND_PARAM_STATE, NULL),
+        OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL),
+        OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL),
+        OSSL_PARAM_END
+    };
+    return known_gettable_ctx_params;
+}
+
+static const OSSL_DISPATCH fake_rand_functions[] = {
+    { OSSL_FUNC_RAND_NEWCTX, (void (*)(void))fake_rand_newctx },
+    { OSSL_FUNC_RAND_FREECTX, (void (*)(void))fake_rand_freectx },
+    { OSSL_FUNC_RAND_INSTANTIATE, (void (*)(void))fake_rand_instantiate },
+    { OSSL_FUNC_RAND_UNINSTANTIATE, (void (*)(void))fake_rand_uninstantiate },
+    { OSSL_FUNC_RAND_GENERATE, (void (*)(void))fake_rand_generate },
+    { OSSL_FUNC_RAND_ENABLE_LOCKING, (void (*)(void))fake_rand_enable_locking },
+    { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS,
+      (void(*)(void))fake_rand_gettable_ctx_params },
+    { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))fake_rand_get_ctx_params },
+    { 0, NULL }
+};
+
+static const OSSL_ALGORITHM fake_rand_rand[] = {
+    { "FAKE", "provider=fake", fake_rand_functions },
+    { NULL, NULL, NULL }
+};
+
+static const OSSL_ALGORITHM *fake_rand_query(void *provctx,
+                                             int operation_id,
+                                             int *no_cache)
+{
+    *no_cache = 0;
+    switch (operation_id) {
+    case OSSL_OP_RAND:
+        return fake_rand_rand;
+    }
+    return NULL;
+}
+
+/* Functions we provide to the core */
+static const OSSL_DISPATCH fake_rand_method[] = {
+    { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))OSSL_LIB_CTX_free },
+    { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))fake_rand_query },
+    { 0, NULL }
+};
+
+static int fake_rand_provider_init(const OSSL_CORE_HANDLE *handle,
+                                   const OSSL_DISPATCH *in,
+                                   const OSSL_DISPATCH **out, void **provctx)
+{
+    if (!TEST_ptr(*provctx = OSSL_LIB_CTX_new()))
+        return 0;
+    *out = fake_rand_method;
+    return 1;
+}
+
+OSSL_PROVIDER *fake_rand_start(OSSL_LIB_CTX *libctx)
+{
+    OSSL_PROVIDER *p;
+
+    if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, "fake-rand",
+                                             fake_rand_provider_init))
+            || !TEST_true(RAND_set_DRBG_type(libctx, "fake", NULL, NULL, NULL))
+            || !TEST_ptr(p = OSSL_PROVIDER_try_load(libctx, "fake-rand", 1)))
+        return NULL;
+    return p;
+}
+
+void fake_rand_finish(OSSL_PROVIDER *p)
+{
+    OSSL_PROVIDER_unload(p);
+}
+
+void fake_rand_set_callback(int (*cb)(unsigned char *out, size_t outlen))
+{
+    fake_rand.cb = cb;
+}
+