From 6b518fef381a36d655f2394da10e580099b5fc3a Mon Sep 17 00:00:00 2001 From: Pauli Date: Thu, 30 May 2024 12:08:51 +1000 Subject: [PATCH] Add test case for RAND_set1_random_provider() call Reviewed-by: Tim Hudson Reviewed-by: Shane Lontis (Merged from https://github.com/openssl/openssl/pull/24498) --- test/rand_test.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/test/rand_test.c b/test/rand_test.c index d3dca252d5c..90c8d8b231c 100644 --- a/test/rand_test.c +++ b/test/rand_test.c @@ -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; } -- 2.47.2