]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
Add test case for RAND_set1_random_provider() call
authorPauli <ppzgs1@gmail.com>
Thu, 30 May 2024 02:08:51 +0000 (12:08 +1000)
committerPauli <ppzgs1@gmail.com>
Tue, 4 Feb 2025 20:20:22 +0000 (07:20 +1100)
Reviewed-by: Tim Hudson <tjh@openssl.org>
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/24498)

test/rand_test.c

index d3dca252d5c131e3fdfa3d954536c56da114f8e2..90c8d8b231c7c94460b830af726f7c1041e380df 100644 (file)
@@ -162,6 +162,88 @@ static int fips_health_tests(void)
     return 1;
 }
 
+typedef struct r_test_ctx {
+    const OSSL_CORE_HANDLE *handle;
+} R_TEST_CTX;
+
+static void r_teardown(void *provctx)
+{
+    R_TEST_CTX *ctx = (R_TEST_CTX *)provctx;
+
+    free(ctx);
+}
+
+static int r_random_bytes(ossl_unused void *vprov, ossl_unused int which,
+                          void *buf, size_t n, ossl_unused unsigned int strength)
+{
+    while (n-- > 0)
+        ((unsigned char *)buf)[n] = 0xff & n;
+    return 1;
+}
+
+static const OSSL_DISPATCH r_test_table[] = {
+    { OSSL_FUNC_PROVIDER_RANDOM_BYTES, (void (*)(void))r_random_bytes },
+    { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))r_teardown },
+    OSSL_DISPATCH_END
+};
+
+static int r_init(const OSSL_CORE_HANDLE *handle,
+                  ossl_unused const OSSL_DISPATCH *oin,
+                  const OSSL_DISPATCH **out,
+                  void **provctx)
+{
+    R_TEST_CTX *ctx;
+
+    ctx = malloc(sizeof(*ctx));
+    if (ctx == NULL)
+        return 0;
+    ctx->handle = handle;
+
+    *provctx = (void *)ctx;
+    *out = r_test_table;
+    return 1;
+}
+
+static int test_rand_random_provider(void)
+{
+    OSSL_LIB_CTX *ctx = NULL;
+    OSSL_PROVIDER *prov = NULL;
+    int res = 0;
+    static const unsigned char data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+    unsigned char buf[sizeof(data)], privbuf[sizeof(data)];
+
+    memset(buf, 255, sizeof(buf));
+    memset(privbuf, 255, sizeof(privbuf));
+
+    if (!test_get_libctx(&ctx, NULL, NULL, NULL, NULL)
+            || !TEST_true(OSSL_PROVIDER_add_builtin(ctx, "r_prov", &r_init))
+            || !TEST_ptr(prov = OSSL_PROVIDER_try_load(ctx, "r_prov", 1))
+            || !TEST_true(RAND_set1_random_provider(ctx, prov))
+            || !RAND_bytes_ex(ctx, buf, sizeof(buf), 256)
+            || !TEST_mem_eq(buf, sizeof(buf), data, sizeof(data))
+            || !RAND_priv_bytes_ex(ctx, privbuf, sizeof(privbuf), 256)
+            || !TEST_mem_eq(privbuf, sizeof(privbuf), data, sizeof(data)))
+        goto err;
+
+    /* Test we can revert to not using the provider based randomness */
+    if (!TEST_true(RAND_set1_random_provider(ctx, NULL))
+            || !RAND_bytes_ex(ctx, buf, sizeof(buf), 256)
+            || !TEST_mem_ne(buf, sizeof(buf), data, sizeof(data)))
+        goto err;
+
+    /* And back to the provided randomness */
+    if (!TEST_true(RAND_set1_random_provider(ctx, prov))
+            || !RAND_bytes_ex(ctx, buf, sizeof(buf), 256)
+            || !TEST_mem_eq(buf, sizeof(buf), data, sizeof(data)))
+        goto err;
+
+    res = 1;
+ err:
+    OSSL_PROVIDER_unload(prov);
+    OSSL_LIB_CTX_free(ctx);
+    return res;
+}
+
 int setup_tests(void)
 {
     char *configfile;
@@ -180,5 +262,6 @@ int setup_tests(void)
             && fips_provider_version_ge(NULL, 3, 4, 0))
         ADD_TEST(fips_health_tests);
 
+    ADD_TEST(test_rand_random_provider);
     return 1;
 }