2020-05-08 08:25:36 +08:00
|
|
|
/*
|
2021-02-18 22:57:13 +08:00
|
|
|
* Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
|
2020-05-08 08:25:36 +08:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
2020-06-21 07:19:16 +08:00
|
|
|
#include <openssl/core_dispatch.h>
|
2020-05-08 08:25:36 +08:00
|
|
|
#include <openssl/e_os2.h>
|
|
|
|
#include <openssl/params.h>
|
2020-11-02 10:03:31 +08:00
|
|
|
#include <openssl/core_names.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/err.h>
|
|
|
|
#include <openssl/randerr.h>
|
2020-05-08 08:25:36 +08:00
|
|
|
#include "prov/providercommon.h"
|
|
|
|
#include "prov/provider_ctx.h"
|
|
|
|
#include "prov/provider_util.h"
|
|
|
|
#include "prov/implementations.h"
|
|
|
|
|
2020-11-02 10:03:31 +08:00
|
|
|
static OSSL_FUNC_rand_newctx_fn test_rng_new;
|
2020-06-21 07:19:16 +08:00
|
|
|
static OSSL_FUNC_rand_freectx_fn test_rng_free;
|
2020-11-02 10:03:31 +08:00
|
|
|
static OSSL_FUNC_rand_instantiate_fn test_rng_instantiate;
|
|
|
|
static OSSL_FUNC_rand_uninstantiate_fn test_rng_uninstantiate;
|
|
|
|
static OSSL_FUNC_rand_generate_fn test_rng_generate;
|
|
|
|
static OSSL_FUNC_rand_reseed_fn test_rng_reseed;
|
2020-06-21 07:19:16 +08:00
|
|
|
static OSSL_FUNC_rand_nonce_fn test_rng_nonce;
|
|
|
|
static OSSL_FUNC_rand_settable_ctx_params_fn test_rng_settable_ctx_params;
|
|
|
|
static OSSL_FUNC_rand_set_ctx_params_fn test_rng_set_ctx_params;
|
|
|
|
static OSSL_FUNC_rand_gettable_ctx_params_fn test_rng_gettable_ctx_params;
|
|
|
|
static OSSL_FUNC_rand_get_ctx_params_fn test_rng_get_ctx_params;
|
|
|
|
static OSSL_FUNC_rand_verify_zeroization_fn test_rng_verify_zeroization;
|
2020-11-02 10:03:31 +08:00
|
|
|
static OSSL_FUNC_rand_enable_locking_fn test_rng_enable_locking;
|
|
|
|
static OSSL_FUNC_rand_lock_fn test_rng_lock;
|
|
|
|
static OSSL_FUNC_rand_unlock_fn test_rng_unlock;
|
2021-02-12 10:44:43 +08:00
|
|
|
static OSSL_FUNC_rand_get_seed_fn test_rng_get_seed;
|
2020-05-08 08:25:36 +08:00
|
|
|
|
|
|
|
typedef struct {
|
2020-11-02 10:03:31 +08:00
|
|
|
void *provctx;
|
|
|
|
int state;
|
|
|
|
unsigned int strength;
|
|
|
|
size_t max_request;
|
2020-05-08 08:25:36 +08:00
|
|
|
unsigned char *entropy, *nonce;
|
|
|
|
size_t entropy_len, entropy_pos, nonce_len;
|
2020-11-02 10:03:31 +08:00
|
|
|
CRYPTO_RWLOCK *lock;
|
2020-05-08 08:25:36 +08:00
|
|
|
} PROV_TEST_RNG;
|
|
|
|
|
2020-11-02 10:03:31 +08:00
|
|
|
static void *test_rng_new(void *provctx, void *parent,
|
|
|
|
const OSSL_DISPATCH *parent_dispatch)
|
2020-05-08 08:25:36 +08:00
|
|
|
{
|
|
|
|
PROV_TEST_RNG *t;
|
|
|
|
|
2020-11-02 10:03:31 +08:00
|
|
|
if (parent != NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2020-05-08 08:25:36 +08:00
|
|
|
t = OPENSSL_zalloc(sizeof(*t));
|
|
|
|
if (t == NULL)
|
2020-11-02 10:03:31 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
t->max_request = INT_MAX;
|
|
|
|
t->provctx = provctx;
|
|
|
|
t->state = EVP_RAND_STATE_UNINITIALISED;
|
|
|
|
return t;
|
2020-05-08 08:25:36 +08:00
|
|
|
}
|
|
|
|
|
2020-11-02 10:03:31 +08:00
|
|
|
static void test_rng_free(void *vtest)
|
2020-05-08 08:25:36 +08:00
|
|
|
{
|
2020-11-02 10:03:31 +08:00
|
|
|
PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
|
2020-05-08 08:25:36 +08:00
|
|
|
|
2020-11-02 10:03:31 +08:00
|
|
|
if (t == NULL)
|
|
|
|
return;
|
2020-05-08 08:25:36 +08:00
|
|
|
OPENSSL_free(t->entropy);
|
|
|
|
OPENSSL_free(t->nonce);
|
2020-11-02 10:03:31 +08:00
|
|
|
CRYPTO_THREAD_lock_free(t->lock);
|
|
|
|
OPENSSL_free(t);
|
2020-05-08 08:25:36 +08:00
|
|
|
}
|
|
|
|
|
2020-11-02 10:03:31 +08:00
|
|
|
static int test_rng_instantiate(void *vtest, unsigned int strength,
|
|
|
|
int prediction_resistance,
|
2021-02-26 08:55:40 +08:00
|
|
|
const unsigned char *pstr, size_t pstr_len,
|
|
|
|
const OSSL_PARAM params[])
|
2020-05-08 08:25:36 +08:00
|
|
|
{
|
2020-11-02 10:03:31 +08:00
|
|
|
PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
|
2020-05-08 08:25:36 +08:00
|
|
|
|
2021-02-26 08:55:40 +08:00
|
|
|
if (!test_rng_set_ctx_params(t, params) || strength > t->strength)
|
2020-05-08 08:25:36 +08:00
|
|
|
return 0;
|
|
|
|
|
2020-11-02 10:03:31 +08:00
|
|
|
t->state = EVP_RAND_STATE_READY;
|
2020-05-08 08:25:36 +08:00
|
|
|
t->entropy_pos = 0;
|
|
|
|
|
2020-11-02 10:03:31 +08:00
|
|
|
return 1;
|
2020-05-08 08:25:36 +08:00
|
|
|
}
|
|
|
|
|
2020-11-02 10:03:31 +08:00
|
|
|
static int test_rng_uninstantiate(void *vtest)
|
2020-05-08 08:25:36 +08:00
|
|
|
{
|
2020-11-02 10:03:31 +08:00
|
|
|
PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
|
2020-05-08 08:25:36 +08:00
|
|
|
|
|
|
|
t->entropy_pos = 0;
|
2020-11-04 11:05:46 +08:00
|
|
|
t->state = EVP_RAND_STATE_UNINITIALISED;
|
2020-11-02 10:03:31 +08:00
|
|
|
return 1;
|
2020-05-08 08:25:36 +08:00
|
|
|
}
|
|
|
|
|
2020-11-02 10:03:31 +08:00
|
|
|
static int test_rng_generate(void *vtest, unsigned char *out, size_t outlen,
|
|
|
|
unsigned int strength, int prediction_resistance,
|
2020-05-08 08:25:36 +08:00
|
|
|
const unsigned char *adin, size_t adin_len)
|
|
|
|
{
|
2020-11-02 10:03:31 +08:00
|
|
|
PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
|
2020-05-08 08:25:36 +08:00
|
|
|
size_t i;
|
|
|
|
|
2020-11-02 10:03:31 +08:00
|
|
|
if (strength > t->strength)
|
2020-05-08 08:25:36 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (i = 0; i < outlen; i++) {
|
|
|
|
out[i] = t->entropy[t->entropy_pos++];
|
|
|
|
if (t->entropy_pos >= t->entropy_len)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-11-02 10:03:31 +08:00
|
|
|
static int test_rng_reseed(ossl_unused void *vtest,
|
|
|
|
ossl_unused int prediction_resistance,
|
|
|
|
ossl_unused const unsigned char *ent,
|
|
|
|
ossl_unused size_t ent_len,
|
|
|
|
ossl_unused const unsigned char *adin,
|
|
|
|
ossl_unused size_t adin_len)
|
2020-05-08 08:25:36 +08:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-11-02 10:03:31 +08:00
|
|
|
static size_t test_rng_nonce(void *vtest, unsigned char *out,
|
|
|
|
unsigned int strength,
|
|
|
|
ossl_unused size_t min_noncelen,
|
|
|
|
ossl_unused size_t max_noncelen)
|
2020-05-08 08:25:36 +08:00
|
|
|
{
|
2020-11-02 10:03:31 +08:00
|
|
|
PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
|
2020-05-08 08:25:36 +08:00
|
|
|
|
2020-11-02 10:03:31 +08:00
|
|
|
if (t->nonce == NULL || strength > t->strength)
|
2020-05-08 08:25:36 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (out != NULL)
|
|
|
|
memcpy(out, t->nonce, t->nonce_len);
|
|
|
|
return t->nonce_len;
|
|
|
|
}
|
|
|
|
|
2020-11-02 10:03:31 +08:00
|
|
|
static int test_rng_get_ctx_params(void *vtest, OSSL_PARAM params[])
|
2020-05-08 08:25:36 +08:00
|
|
|
{
|
2020-11-02 10:03:31 +08:00
|
|
|
PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
|
|
|
|
OSSL_PARAM *p;
|
2020-05-08 08:25:36 +08:00
|
|
|
|
2020-11-02 10:03:31 +08:00
|
|
|
p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STATE);
|
|
|
|
if (p != NULL && !OSSL_PARAM_set_int(p, t->state))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_STRENGTH);
|
|
|
|
if (p != NULL && !OSSL_PARAM_set_int(p, t->strength))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_MAX_REQUEST);
|
|
|
|
if (p != NULL && !OSSL_PARAM_set_size_t(p, t->max_request))
|
|
|
|
return 0;
|
|
|
|
return 1;
|
2020-05-08 08:25:36 +08:00
|
|
|
}
|
|
|
|
|
2021-02-23 07:51:10 +08:00
|
|
|
static const OSSL_PARAM *test_rng_gettable_ctx_params(ossl_unused void *vtest,
|
|
|
|
ossl_unused void *provctx)
|
2020-05-08 08:25:36 +08:00
|
|
|
{
|
|
|
|
static const OSSL_PARAM known_gettable_ctx_params[] = {
|
2020-11-02 10:03:31 +08:00
|
|
|
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),
|
2020-05-08 08:25:36 +08:00
|
|
|
OSSL_PARAM_END
|
|
|
|
};
|
|
|
|
return known_gettable_ctx_params;
|
|
|
|
}
|
|
|
|
|
2020-11-02 10:03:31 +08:00
|
|
|
static int test_rng_set_ctx_params(void *vtest, const OSSL_PARAM params[])
|
2020-05-08 08:25:36 +08:00
|
|
|
{
|
2020-11-02 10:03:31 +08:00
|
|
|
PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
|
2020-05-08 08:25:36 +08:00
|
|
|
const OSSL_PARAM *p;
|
|
|
|
void *ptr = NULL;
|
|
|
|
size_t size = 0;
|
|
|
|
|
2021-03-10 16:38:04 +08:00
|
|
|
if (params == NULL)
|
|
|
|
return 1;
|
|
|
|
|
2020-05-08 08:25:36 +08:00
|
|
|
p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_STRENGTH);
|
2020-11-02 10:03:31 +08:00
|
|
|
if (p != NULL && !OSSL_PARAM_get_uint(p, &t->strength))
|
2020-05-08 08:25:36 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_TEST_ENTROPY);
|
|
|
|
if (p != NULL) {
|
|
|
|
if (!OSSL_PARAM_get_octet_string(p, &ptr, 0, &size))
|
|
|
|
return 0;
|
|
|
|
OPENSSL_free(t->entropy);
|
|
|
|
t->entropy = ptr;
|
|
|
|
t->entropy_len = size;
|
|
|
|
t->entropy_pos = 0;
|
|
|
|
ptr = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_TEST_NONCE);
|
|
|
|
if (p != NULL) {
|
|
|
|
if (!OSSL_PARAM_get_octet_string(p, &ptr, 0, &size))
|
|
|
|
return 0;
|
|
|
|
OPENSSL_free(t->nonce);
|
|
|
|
t->nonce = ptr;
|
|
|
|
t->nonce_len = size;
|
|
|
|
}
|
|
|
|
|
2020-11-02 10:03:31 +08:00
|
|
|
p = OSSL_PARAM_locate_const(params, OSSL_RAND_PARAM_MAX_REQUEST);
|
|
|
|
if (p != NULL && !OSSL_PARAM_get_size_t(p, &t->max_request))
|
2020-05-08 08:25:36 +08:00
|
|
|
return 0;
|
|
|
|
|
2020-11-02 10:03:31 +08:00
|
|
|
return 1;
|
2020-05-08 08:25:36 +08:00
|
|
|
}
|
|
|
|
|
2021-02-23 07:51:10 +08:00
|
|
|
static const OSSL_PARAM *test_rng_settable_ctx_params(ossl_unused void *vtest,
|
|
|
|
ossl_unused void *provctx)
|
2020-05-08 08:25:36 +08:00
|
|
|
{
|
|
|
|
static const OSSL_PARAM known_settable_ctx_params[] = {
|
|
|
|
OSSL_PARAM_octet_string(OSSL_RAND_PARAM_TEST_ENTROPY, NULL, 0),
|
|
|
|
OSSL_PARAM_octet_string(OSSL_RAND_PARAM_TEST_NONCE, NULL, 0),
|
|
|
|
OSSL_PARAM_uint(OSSL_RAND_PARAM_STRENGTH, NULL),
|
2020-10-30 13:54:03 +08:00
|
|
|
OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL),
|
2020-05-08 08:25:36 +08:00
|
|
|
OSSL_PARAM_END
|
|
|
|
};
|
|
|
|
return known_settable_ctx_params;
|
|
|
|
}
|
|
|
|
|
2020-11-02 10:03:31 +08:00
|
|
|
static int test_rng_verify_zeroization(ossl_unused void *vtest)
|
2020-05-08 08:25:36 +08:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-02-12 10:44:43 +08:00
|
|
|
static size_t test_rng_get_seed(void *vtest, unsigned char **pout,
|
|
|
|
int entropy, size_t min_len, size_t max_len,
|
|
|
|
ossl_unused int prediction_resistance,
|
|
|
|
ossl_unused const unsigned char *adin,
|
|
|
|
ossl_unused size_t adin_len)
|
|
|
|
{
|
|
|
|
PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
|
|
|
|
|
|
|
|
*pout = t->entropy;
|
|
|
|
return t->entropy_len > max_len ? max_len : t->entropy_len;
|
|
|
|
}
|
|
|
|
|
2020-11-02 10:03:31 +08:00
|
|
|
static int test_rng_enable_locking(void *vtest)
|
2020-05-08 08:25:19 +08:00
|
|
|
{
|
2020-11-02 10:03:31 +08:00
|
|
|
PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
|
|
|
|
|
|
|
|
if (t != NULL && t->lock == NULL) {
|
|
|
|
t->lock = CRYPTO_THREAD_lock_new();
|
|
|
|
if (t->lock == NULL) {
|
|
|
|
ERR_raise(ERR_LIB_PROV, RAND_R_FAILED_TO_CREATE_LOCK);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_rng_lock(void *vtest)
|
|
|
|
{
|
|
|
|
PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
|
|
|
|
|
|
|
|
if (t == NULL || t->lock == NULL)
|
|
|
|
return 1;
|
|
|
|
return CRYPTO_THREAD_write_lock(t->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_rng_unlock(void *vtest)
|
|
|
|
{
|
|
|
|
PROV_TEST_RNG *t = (PROV_TEST_RNG *)vtest;
|
|
|
|
|
|
|
|
if (t != NULL && t->lock != NULL)
|
|
|
|
CRYPTO_THREAD_unlock(t->lock);
|
2020-05-08 08:25:19 +08:00
|
|
|
}
|
|
|
|
|
2020-09-28 10:28:29 +08:00
|
|
|
const OSSL_DISPATCH ossl_test_rng_functions[] = {
|
2020-11-02 10:03:31 +08:00
|
|
|
{ OSSL_FUNC_RAND_NEWCTX, (void(*)(void))test_rng_new },
|
2020-05-08 08:25:36 +08:00
|
|
|
{ OSSL_FUNC_RAND_FREECTX, (void(*)(void))test_rng_free },
|
|
|
|
{ OSSL_FUNC_RAND_INSTANTIATE,
|
2020-11-02 10:03:31 +08:00
|
|
|
(void(*)(void))test_rng_instantiate },
|
2020-05-08 08:25:36 +08:00
|
|
|
{ OSSL_FUNC_RAND_UNINSTANTIATE,
|
2020-11-02 10:03:31 +08:00
|
|
|
(void(*)(void))test_rng_uninstantiate },
|
|
|
|
{ OSSL_FUNC_RAND_GENERATE, (void(*)(void))test_rng_generate },
|
|
|
|
{ OSSL_FUNC_RAND_RESEED, (void(*)(void))test_rng_reseed },
|
2020-05-08 08:25:36 +08:00
|
|
|
{ OSSL_FUNC_RAND_NONCE, (void(*)(void))test_rng_nonce },
|
2020-11-02 10:03:31 +08:00
|
|
|
{ OSSL_FUNC_RAND_ENABLE_LOCKING, (void(*)(void))test_rng_enable_locking },
|
|
|
|
{ OSSL_FUNC_RAND_LOCK, (void(*)(void))test_rng_lock },
|
|
|
|
{ OSSL_FUNC_RAND_UNLOCK, (void(*)(void))test_rng_unlock },
|
2020-05-08 08:25:36 +08:00
|
|
|
{ OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS,
|
|
|
|
(void(*)(void))test_rng_settable_ctx_params },
|
|
|
|
{ OSSL_FUNC_RAND_SET_CTX_PARAMS, (void(*)(void))test_rng_set_ctx_params },
|
|
|
|
{ OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS,
|
|
|
|
(void(*)(void))test_rng_gettable_ctx_params },
|
|
|
|
{ OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))test_rng_get_ctx_params },
|
|
|
|
{ OSSL_FUNC_RAND_VERIFY_ZEROIZATION,
|
|
|
|
(void(*)(void))test_rng_verify_zeroization },
|
2021-02-12 10:44:43 +08:00
|
|
|
{ OSSL_FUNC_RAND_GET_SEED, (void(*)(void))test_rng_get_seed },
|
2020-05-08 08:25:36 +08:00
|
|
|
{ 0, NULL }
|
|
|
|
};
|