test: add utility function to set the fake random callback on both the public and private instances

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/14341)
This commit is contained in:
Pauli 2021-02-27 12:17:57 +10:00
parent fccdb61aee
commit 740582cfaf
2 changed files with 18 additions and 5 deletions

View File

@ -567,11 +567,16 @@ uint32_t test_random(void);
void test_random_seed(uint32_t sd);
/* Fake non-secure random number generator */
typedef int fake_random_generate_cb(unsigned char *out, size_t outlen,
const char *name, EVP_RAND_CTX *ctx);
OSSL_PROVIDER *fake_rand_start(OSSL_LIB_CTX *libctx);
void fake_rand_finish(OSSL_PROVIDER *p);
void fake_rand_set_callback(EVP_RAND_CTX *ctx,
int (*cb)(unsigned char *out, size_t outlen,
const char *name, EVP_RAND_CTX *ctx));
void fake_rand_set_public_private_callbacks(OSSL_LIB_CTX *libctx,
fake_random_generate_cb *cb);
/* Create a file path from a directory and a filename */
char *test_mk_file_path(const char *dir, const char *file);

View File

@ -17,8 +17,7 @@
#include "../testutil.h"
typedef struct {
int (*cb)(unsigned char *out, size_t outlen,
const char *name, EVP_RAND_CTX *ctx);
fake_random_generate_cb *cb;
int state;
const char *name;
EVP_RAND_CTX *ctx;
@ -216,8 +215,17 @@ void fake_rand_set_callback(EVP_RAND_CTX *rng,
int (*cb)(unsigned char *out, size_t outlen,
const char *name, EVP_RAND_CTX *ctx))
{
FAKE_RAND *f = rng->data;
f->cb = cb;
if (rng != NULL)
((FAKE_RAND *)rng->data)->cb = cb;
}
void fake_rand_set_public_private_callbacks(OSSL_LIB_CTX *libctx,
int (*cb)(unsigned char *out,
size_t outlen,
const char *name,
EVP_RAND_CTX *ctx))
{
fake_rand_set_callback(RAND_get0_private(libctx), cb);
fake_rand_set_callback(RAND_get0_public(libctx), cb);
}