2020-04-30 12:46:12 +08:00
|
|
|
/*
|
2021-01-28 20:54:57 +08:00
|
|
|
* Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
|
2020-04-30 12:46:12 +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 <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/rand.h>
|
|
|
|
#include <openssl/core.h>
|
|
|
|
#include <openssl/core_names.h>
|
|
|
|
#include <openssl/crypto.h>
|
2020-05-08 08:25:19 +08:00
|
|
|
#include "internal/cryptlib.h"
|
2020-04-30 12:46:12 +08:00
|
|
|
#include "internal/numbers.h"
|
|
|
|
#include "internal/provider.h"
|
2021-04-16 22:22:03 +08:00
|
|
|
#include "internal/core.h"
|
|
|
|
#include "crypto/evp.h"
|
2020-04-30 12:46:12 +08:00
|
|
|
#include "evp_local.h"
|
|
|
|
|
2020-07-22 10:55:31 +08:00
|
|
|
struct evp_rand_st {
|
|
|
|
OSSL_PROVIDER *prov;
|
|
|
|
int name_id;
|
2021-04-16 22:22:03 +08:00
|
|
|
char *type_name;
|
2021-03-16 21:14:43 +08:00
|
|
|
const char *description;
|
2020-07-22 10:55:31 +08:00
|
|
|
CRYPTO_REF_COUNT refcnt;
|
|
|
|
CRYPTO_RWLOCK *refcnt_lock;
|
|
|
|
|
|
|
|
const OSSL_DISPATCH *dispatch;
|
|
|
|
OSSL_FUNC_rand_newctx_fn *newctx;
|
|
|
|
OSSL_FUNC_rand_freectx_fn *freectx;
|
|
|
|
OSSL_FUNC_rand_instantiate_fn *instantiate;
|
|
|
|
OSSL_FUNC_rand_uninstantiate_fn *uninstantiate;
|
|
|
|
OSSL_FUNC_rand_generate_fn *generate;
|
|
|
|
OSSL_FUNC_rand_reseed_fn *reseed;
|
|
|
|
OSSL_FUNC_rand_nonce_fn *nonce;
|
|
|
|
OSSL_FUNC_rand_enable_locking_fn *enable_locking;
|
|
|
|
OSSL_FUNC_rand_lock_fn *lock;
|
|
|
|
OSSL_FUNC_rand_unlock_fn *unlock;
|
|
|
|
OSSL_FUNC_rand_gettable_params_fn *gettable_params;
|
|
|
|
OSSL_FUNC_rand_gettable_ctx_params_fn *gettable_ctx_params;
|
|
|
|
OSSL_FUNC_rand_settable_ctx_params_fn *settable_ctx_params;
|
|
|
|
OSSL_FUNC_rand_get_params_fn *get_params;
|
|
|
|
OSSL_FUNC_rand_get_ctx_params_fn *get_ctx_params;
|
|
|
|
OSSL_FUNC_rand_set_ctx_params_fn *set_ctx_params;
|
|
|
|
OSSL_FUNC_rand_verify_zeroization_fn *verify_zeroization;
|
|
|
|
} /* EVP_RAND */ ;
|
|
|
|
|
2020-04-30 12:46:12 +08:00
|
|
|
static int evp_rand_up_ref(void *vrand)
|
|
|
|
{
|
|
|
|
EVP_RAND *rand = (EVP_RAND *)vrand;
|
|
|
|
int ref = 0;
|
|
|
|
|
|
|
|
if (rand != NULL)
|
2020-05-08 08:25:19 +08:00
|
|
|
return CRYPTO_UP_REF(&rand->refcnt, &ref, rand->refcnt_lock);
|
2020-04-30 12:46:12 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-02-16 01:31:36 +08:00
|
|
|
static void evp_rand_free(void *vrand)
|
|
|
|
{
|
2020-04-30 12:46:12 +08:00
|
|
|
EVP_RAND *rand = (EVP_RAND *)vrand;
|
|
|
|
int ref = 0;
|
|
|
|
|
2021-02-16 01:31:36 +08:00
|
|
|
if (rand == NULL)
|
|
|
|
return;
|
|
|
|
CRYPTO_DOWN_REF(&rand->refcnt, &ref, rand->refcnt_lock);
|
|
|
|
if (ref > 0)
|
|
|
|
return;
|
2021-04-16 22:22:03 +08:00
|
|
|
OPENSSL_free(rand->type_name);
|
2021-02-16 01:31:36 +08:00
|
|
|
ossl_provider_free(rand->prov);
|
|
|
|
CRYPTO_THREAD_lock_free(rand->refcnt_lock);
|
|
|
|
OPENSSL_free(rand);
|
2020-04-30 12:46:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *evp_rand_new(void)
|
|
|
|
{
|
2020-05-08 08:25:19 +08:00
|
|
|
EVP_RAND *rand = OPENSSL_zalloc(sizeof(*rand));
|
2020-04-30 12:46:12 +08:00
|
|
|
|
2020-05-08 08:25:19 +08:00
|
|
|
if (rand == NULL
|
|
|
|
|| (rand->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL) {
|
|
|
|
OPENSSL_free(rand);
|
2020-04-30 12:46:12 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
rand->refcnt = 1;
|
|
|
|
return rand;
|
|
|
|
}
|
|
|
|
|
2020-05-08 08:22:45 +08:00
|
|
|
/* Enable locking of the underlying DRBG/RAND if available */
|
2020-05-08 08:25:19 +08:00
|
|
|
int EVP_RAND_enable_locking(EVP_RAND_CTX *rand)
|
2020-05-08 08:22:45 +08:00
|
|
|
{
|
2020-05-08 08:25:19 +08:00
|
|
|
if (rand->meth->enable_locking != NULL)
|
2021-05-14 11:08:42 +08:00
|
|
|
return rand->meth->enable_locking(rand->algctx);
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_EVP, EVP_R_LOCKING_NOT_SUPPORTED);
|
2020-05-08 08:25:19 +08:00
|
|
|
return 0;
|
2020-05-08 08:22:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Lock the underlying DRBG/RAND if available */
|
|
|
|
static int evp_rand_lock(EVP_RAND_CTX *rand)
|
|
|
|
{
|
2020-05-08 08:25:19 +08:00
|
|
|
if (rand->meth->lock != NULL)
|
2021-05-14 11:08:42 +08:00
|
|
|
return rand->meth->lock(rand->algctx);
|
2020-05-08 08:22:45 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unlock the underlying DRBG/RAND if available */
|
|
|
|
static void evp_rand_unlock(EVP_RAND_CTX *rand)
|
|
|
|
{
|
2020-05-08 08:25:19 +08:00
|
|
|
if (rand->meth->unlock != NULL)
|
2021-05-14 11:08:42 +08:00
|
|
|
rand->meth->unlock(rand->algctx);
|
2020-05-08 08:22:45 +08:00
|
|
|
}
|
|
|
|
|
2021-03-16 21:14:43 +08:00
|
|
|
static void *evp_rand_from_algorithm(int name_id,
|
|
|
|
const OSSL_ALGORITHM *algodef,
|
|
|
|
OSSL_PROVIDER *prov)
|
2020-04-30 12:46:12 +08:00
|
|
|
{
|
2021-03-16 21:14:43 +08:00
|
|
|
const OSSL_DISPATCH *fns = algodef->implementation;
|
2020-04-30 12:46:12 +08:00
|
|
|
EVP_RAND *rand = NULL;
|
2021-02-22 07:45:37 +08:00
|
|
|
int fnrandcnt = 0, fnctxcnt = 0, fnlockcnt = 0, fnenablelockcnt = 0;
|
2020-05-08 08:22:45 +08:00
|
|
|
#ifdef FIPS_MODULE
|
2020-05-08 08:25:19 +08:00
|
|
|
int fnzeroizecnt = 0;
|
2020-05-08 08:22:45 +08:00
|
|
|
#endif
|
2020-04-30 12:46:12 +08:00
|
|
|
|
|
|
|
if ((rand = evp_rand_new()) == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
|
2020-04-30 12:46:12 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
rand->name_id = name_id;
|
2021-04-16 22:22:03 +08:00
|
|
|
if ((rand->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
|
|
|
|
evp_rand_free(rand);
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-03-16 21:14:43 +08:00
|
|
|
rand->description = algodef->algorithm_description;
|
2020-04-30 12:46:12 +08:00
|
|
|
rand->dispatch = fns;
|
|
|
|
for (; fns->function_id != 0; fns++) {
|
|
|
|
switch (fns->function_id) {
|
|
|
|
case OSSL_FUNC_RAND_NEWCTX:
|
|
|
|
if (rand->newctx != NULL)
|
|
|
|
break;
|
2020-06-21 07:19:16 +08:00
|
|
|
rand->newctx = OSSL_FUNC_rand_newctx(fns);
|
2020-04-30 12:46:12 +08:00
|
|
|
fnctxcnt++;
|
|
|
|
break;
|
|
|
|
case OSSL_FUNC_RAND_FREECTX:
|
|
|
|
if (rand->freectx != NULL)
|
|
|
|
break;
|
2020-06-21 07:19:16 +08:00
|
|
|
rand->freectx = OSSL_FUNC_rand_freectx(fns);
|
2020-04-30 12:46:12 +08:00
|
|
|
fnctxcnt++;
|
|
|
|
break;
|
|
|
|
case OSSL_FUNC_RAND_INSTANTIATE:
|
|
|
|
if (rand->instantiate != NULL)
|
|
|
|
break;
|
2020-06-21 07:19:16 +08:00
|
|
|
rand->instantiate = OSSL_FUNC_rand_instantiate(fns);
|
2020-04-30 12:46:12 +08:00
|
|
|
fnrandcnt++;
|
|
|
|
break;
|
|
|
|
case OSSL_FUNC_RAND_UNINSTANTIATE:
|
|
|
|
if (rand->uninstantiate != NULL)
|
|
|
|
break;
|
2020-06-21 07:19:16 +08:00
|
|
|
rand->uninstantiate = OSSL_FUNC_rand_uninstantiate(fns);
|
2020-04-30 12:46:12 +08:00
|
|
|
fnrandcnt++;
|
|
|
|
break;
|
|
|
|
case OSSL_FUNC_RAND_GENERATE:
|
|
|
|
if (rand->generate != NULL)
|
|
|
|
break;
|
2020-06-21 07:19:16 +08:00
|
|
|
rand->generate = OSSL_FUNC_rand_generate(fns);
|
2020-04-30 12:46:12 +08:00
|
|
|
fnrandcnt++;
|
|
|
|
break;
|
|
|
|
case OSSL_FUNC_RAND_RESEED:
|
|
|
|
if (rand->reseed != NULL)
|
|
|
|
break;
|
2020-06-21 07:19:16 +08:00
|
|
|
rand->reseed = OSSL_FUNC_rand_reseed(fns);
|
2020-04-30 12:46:12 +08:00
|
|
|
break;
|
|
|
|
case OSSL_FUNC_RAND_NONCE:
|
|
|
|
if (rand->nonce != NULL)
|
|
|
|
break;
|
2020-06-21 07:19:16 +08:00
|
|
|
rand->nonce = OSSL_FUNC_rand_nonce(fns);
|
2020-04-30 12:46:12 +08:00
|
|
|
break;
|
|
|
|
case OSSL_FUNC_RAND_ENABLE_LOCKING:
|
2020-05-08 08:25:19 +08:00
|
|
|
if (rand->enable_locking != NULL)
|
2020-04-30 12:46:12 +08:00
|
|
|
break;
|
2020-06-21 07:19:16 +08:00
|
|
|
rand->enable_locking = OSSL_FUNC_rand_enable_locking(fns);
|
2021-02-22 07:45:37 +08:00
|
|
|
fnenablelockcnt++;
|
2020-04-30 12:46:12 +08:00
|
|
|
break;
|
|
|
|
case OSSL_FUNC_RAND_LOCK:
|
2020-05-08 08:25:19 +08:00
|
|
|
if (rand->lock != NULL)
|
2020-04-30 12:46:12 +08:00
|
|
|
break;
|
2020-06-21 07:19:16 +08:00
|
|
|
rand->lock = OSSL_FUNC_rand_lock(fns);
|
2020-05-08 08:25:19 +08:00
|
|
|
fnlockcnt++;
|
2020-04-30 12:46:12 +08:00
|
|
|
break;
|
|
|
|
case OSSL_FUNC_RAND_UNLOCK:
|
2020-05-08 08:25:19 +08:00
|
|
|
if (rand->unlock != NULL)
|
2020-04-30 12:46:12 +08:00
|
|
|
break;
|
2020-06-21 07:19:16 +08:00
|
|
|
rand->unlock = OSSL_FUNC_rand_unlock(fns);
|
2020-05-08 08:25:19 +08:00
|
|
|
fnlockcnt++;
|
2020-04-30 12:46:12 +08:00
|
|
|
break;
|
|
|
|
case OSSL_FUNC_RAND_GETTABLE_PARAMS:
|
|
|
|
if (rand->gettable_params != NULL)
|
|
|
|
break;
|
|
|
|
rand->gettable_params =
|
2020-06-21 07:19:16 +08:00
|
|
|
OSSL_FUNC_rand_gettable_params(fns);
|
2020-04-30 12:46:12 +08:00
|
|
|
break;
|
|
|
|
case OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS:
|
|
|
|
if (rand->gettable_ctx_params != NULL)
|
|
|
|
break;
|
|
|
|
rand->gettable_ctx_params =
|
2020-06-21 07:19:16 +08:00
|
|
|
OSSL_FUNC_rand_gettable_ctx_params(fns);
|
2020-04-30 12:46:12 +08:00
|
|
|
break;
|
|
|
|
case OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS:
|
|
|
|
if (rand->settable_ctx_params != NULL)
|
|
|
|
break;
|
|
|
|
rand->settable_ctx_params =
|
2020-06-21 07:19:16 +08:00
|
|
|
OSSL_FUNC_rand_settable_ctx_params(fns);
|
2020-04-30 12:46:12 +08:00
|
|
|
break;
|
|
|
|
case OSSL_FUNC_RAND_GET_PARAMS:
|
|
|
|
if (rand->get_params != NULL)
|
|
|
|
break;
|
2020-06-21 07:19:16 +08:00
|
|
|
rand->get_params = OSSL_FUNC_rand_get_params(fns);
|
2020-04-30 12:46:12 +08:00
|
|
|
break;
|
|
|
|
case OSSL_FUNC_RAND_GET_CTX_PARAMS:
|
|
|
|
if (rand->get_ctx_params != NULL)
|
|
|
|
break;
|
2020-06-21 07:19:16 +08:00
|
|
|
rand->get_ctx_params = OSSL_FUNC_rand_get_ctx_params(fns);
|
2020-06-30 07:36:47 +08:00
|
|
|
fnctxcnt++;
|
2020-04-30 12:46:12 +08:00
|
|
|
break;
|
|
|
|
case OSSL_FUNC_RAND_SET_CTX_PARAMS:
|
|
|
|
if (rand->set_ctx_params != NULL)
|
|
|
|
break;
|
2020-06-21 07:19:16 +08:00
|
|
|
rand->set_ctx_params = OSSL_FUNC_rand_set_ctx_params(fns);
|
2020-04-30 12:46:12 +08:00
|
|
|
break;
|
2020-05-08 08:22:45 +08:00
|
|
|
case OSSL_FUNC_RAND_VERIFY_ZEROIZATION:
|
|
|
|
if (rand->verify_zeroization != NULL)
|
|
|
|
break;
|
2020-06-21 07:19:16 +08:00
|
|
|
rand->verify_zeroization = OSSL_FUNC_rand_verify_zeroization(fns);
|
2020-05-08 08:22:45 +08:00
|
|
|
#ifdef FIPS_MODULE
|
2020-05-08 08:25:19 +08:00
|
|
|
fnzeroizecnt++;
|
2020-05-08 08:22:45 +08:00
|
|
|
#endif
|
|
|
|
break;
|
2020-04-30 12:46:12 +08:00
|
|
|
}
|
|
|
|
}
|
2020-05-08 08:25:19 +08:00
|
|
|
/*
|
|
|
|
* In order to be a consistent set of functions we must have at least
|
|
|
|
* a complete set of "rand" functions and a complete set of context
|
|
|
|
* management functions. In FIPS mode, we also require the zeroization
|
|
|
|
* verification function.
|
|
|
|
*
|
|
|
|
* In addition, if locking can be enabled, we need a complete set of
|
|
|
|
* locking functions.
|
|
|
|
*/
|
2020-05-08 08:22:45 +08:00
|
|
|
if (fnrandcnt != 3
|
2020-06-30 07:36:47 +08:00
|
|
|
|| fnctxcnt != 3
|
2021-02-22 07:45:37 +08:00
|
|
|
|| (fnenablelockcnt != 0 && fnenablelockcnt != 1)
|
|
|
|
|| (fnlockcnt != 0 && fnlockcnt != 2)
|
2020-05-08 08:22:45 +08:00
|
|
|
#ifdef FIPS_MODULE
|
2020-05-08 08:25:19 +08:00
|
|
|
|| fnzeroizecnt != 1
|
2020-05-08 08:22:45 +08:00
|
|
|
#endif
|
|
|
|
) {
|
2020-04-30 12:46:12 +08:00
|
|
|
evp_rand_free(rand);
|
|
|
|
ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-05-08 08:25:19 +08:00
|
|
|
|
|
|
|
if (prov != NULL && !ossl_provider_up_ref(prov)) {
|
|
|
|
evp_rand_free(rand);
|
|
|
|
ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-04-30 12:46:12 +08:00
|
|
|
rand->prov = prov;
|
|
|
|
|
|
|
|
return rand;
|
|
|
|
}
|
|
|
|
|
2020-10-15 17:55:50 +08:00
|
|
|
EVP_RAND *EVP_RAND_fetch(OSSL_LIB_CTX *libctx, const char *algorithm,
|
2020-05-08 08:25:19 +08:00
|
|
|
const char *properties)
|
2020-04-30 12:46:12 +08:00
|
|
|
{
|
|
|
|
return evp_generic_fetch(libctx, OSSL_OP_RAND, algorithm, properties,
|
2021-03-16 21:14:43 +08:00
|
|
|
evp_rand_from_algorithm, evp_rand_up_ref,
|
2020-04-30 12:46:12 +08:00
|
|
|
evp_rand_free);
|
|
|
|
}
|
|
|
|
|
|
|
|
int EVP_RAND_up_ref(EVP_RAND *rand)
|
|
|
|
{
|
|
|
|
return evp_rand_up_ref(rand);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EVP_RAND_free(EVP_RAND *rand)
|
|
|
|
{
|
|
|
|
evp_rand_free(rand);
|
|
|
|
}
|
|
|
|
|
2021-06-01 19:18:04 +08:00
|
|
|
int evp_rand_get_number(const EVP_RAND *rand)
|
2020-04-30 12:46:12 +08:00
|
|
|
{
|
|
|
|
return rand->name_id;
|
|
|
|
}
|
|
|
|
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
const char *EVP_RAND_get0_name(const EVP_RAND *rand)
|
2020-04-30 12:46:12 +08:00
|
|
|
{
|
2021-04-16 22:22:03 +08:00
|
|
|
return rand->type_name;
|
2020-04-30 12:46:12 +08:00
|
|
|
}
|
|
|
|
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
const char *EVP_RAND_get0_description(const EVP_RAND *rand)
|
EVP: Add EVP_<TYPE>_description()
The following operation types are covered:
EVP_MD, EVP_CIPHER, EVP_MAC, EVP_RAND, EVP_KEYMGMT, EVP_SIGNATURE,
EVP_ASYM_CIPHER, EVP_KEM, EVP_KEYEXCH, EVP_KDF. Also EVP_PKEY.
For EVP_MD and EVP_CIPHER, OBJ_nid2ln() is used as a fallback for
legacy implementations.
For EVP_PKEY, the info field of the EVP_PKEY_ASN1_METHOD is used as a
fallback for legacy implementations.
Fixes #14514
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/14656)
2021-03-16 21:23:54 +08:00
|
|
|
{
|
|
|
|
return rand->description;
|
|
|
|
}
|
|
|
|
|
2020-04-30 12:46:12 +08:00
|
|
|
int EVP_RAND_is_a(const EVP_RAND *rand, const char *name)
|
|
|
|
{
|
|
|
|
return evp_is_a(rand->prov, rand->name_id, NULL, name);
|
|
|
|
}
|
|
|
|
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
const OSSL_PROVIDER *EVP_RAND_get0_provider(const EVP_RAND *rand)
|
2020-04-30 12:46:12 +08:00
|
|
|
{
|
|
|
|
return rand->prov;
|
|
|
|
}
|
|
|
|
|
|
|
|
int EVP_RAND_get_params(EVP_RAND *rand, OSSL_PARAM params[])
|
|
|
|
{
|
|
|
|
if (rand->get_params != NULL)
|
|
|
|
return rand->get_params(params);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-09-16 09:10:01 +08:00
|
|
|
static int evp_rand_ctx_up_ref(EVP_RAND_CTX *ctx)
|
|
|
|
{
|
|
|
|
int ref = 0;
|
|
|
|
|
|
|
|
return CRYPTO_UP_REF(&ctx->refcnt, &ref, ctx->refcnt_lock);
|
|
|
|
}
|
|
|
|
|
2020-05-08 08:25:19 +08:00
|
|
|
EVP_RAND_CTX *EVP_RAND_CTX_new(EVP_RAND *rand, EVP_RAND_CTX *parent)
|
2020-04-30 12:46:12 +08:00
|
|
|
{
|
|
|
|
EVP_RAND_CTX *ctx;
|
|
|
|
void *parent_ctx = NULL;
|
|
|
|
const OSSL_DISPATCH *parent_dispatch = NULL;
|
|
|
|
|
2020-05-08 08:25:19 +08:00
|
|
|
if (rand == NULL) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_NULL_ALGORITHM);
|
2020-04-30 12:46:12 +08:00
|
|
|
return NULL;
|
2020-05-08 08:25:19 +08:00
|
|
|
}
|
2020-04-30 12:46:12 +08:00
|
|
|
|
2020-05-08 08:25:19 +08:00
|
|
|
ctx = OPENSSL_zalloc(sizeof(*ctx));
|
2020-09-16 09:10:01 +08:00
|
|
|
if (ctx == NULL || (ctx->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL) {
|
|
|
|
OPENSSL_free(ctx);
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
|
2020-04-30 12:46:12 +08:00
|
|
|
return NULL;
|
2020-05-08 08:25:19 +08:00
|
|
|
}
|
2020-04-30 12:46:12 +08:00
|
|
|
if (parent != NULL) {
|
2020-09-16 09:10:01 +08:00
|
|
|
if (!evp_rand_ctx_up_ref(parent)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
|
2020-09-16 09:10:01 +08:00
|
|
|
CRYPTO_THREAD_lock_free(ctx->refcnt_lock);
|
2020-05-08 08:25:19 +08:00
|
|
|
OPENSSL_free(ctx);
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-05-14 11:08:42 +08:00
|
|
|
parent_ctx = parent->algctx;
|
2020-04-30 12:46:12 +08:00
|
|
|
parent_dispatch = parent->meth->dispatch;
|
|
|
|
}
|
2021-05-14 11:08:42 +08:00
|
|
|
if ((ctx->algctx = rand->newctx(ossl_provider_ctx(rand->prov), parent_ctx,
|
|
|
|
parent_dispatch)) == NULL
|
2020-04-30 12:46:12 +08:00
|
|
|
|| !EVP_RAND_up_ref(rand)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
|
2021-05-14 11:08:42 +08:00
|
|
|
rand->freectx(ctx->algctx);
|
2020-09-16 09:10:01 +08:00
|
|
|
CRYPTO_THREAD_lock_free(ctx->refcnt_lock);
|
2020-04-30 12:46:12 +08:00
|
|
|
OPENSSL_free(ctx);
|
2020-09-16 09:10:01 +08:00
|
|
|
EVP_RAND_CTX_free(parent);
|
2020-04-30 12:46:12 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ctx->meth = rand;
|
2020-09-16 09:10:01 +08:00
|
|
|
ctx->parent = parent;
|
|
|
|
ctx->refcnt = 1;
|
2020-04-30 12:46:12 +08:00
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EVP_RAND_CTX_free(EVP_RAND_CTX *ctx)
|
|
|
|
{
|
2021-02-16 01:31:36 +08:00
|
|
|
int ref = 0;
|
|
|
|
EVP_RAND_CTX *parent;
|
|
|
|
|
|
|
|
if (ctx == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CRYPTO_DOWN_REF(&ctx->refcnt, &ref, ctx->refcnt_lock);
|
|
|
|
if (ref > 0)
|
|
|
|
return;
|
|
|
|
parent = ctx->parent;
|
2021-05-14 11:08:42 +08:00
|
|
|
ctx->meth->freectx(ctx->algctx);
|
|
|
|
ctx->algctx = NULL;
|
2021-02-16 01:31:36 +08:00
|
|
|
EVP_RAND_free(ctx->meth);
|
|
|
|
CRYPTO_THREAD_lock_free(ctx->refcnt_lock);
|
|
|
|
OPENSSL_free(ctx);
|
|
|
|
EVP_RAND_CTX_free(parent);
|
2020-04-30 12:46:12 +08:00
|
|
|
}
|
|
|
|
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
EVP_RAND *EVP_RAND_CTX_get0_rand(EVP_RAND_CTX *ctx)
|
2020-04-30 12:46:12 +08:00
|
|
|
{
|
|
|
|
return ctx->meth;
|
|
|
|
}
|
|
|
|
|
2020-07-01 08:57:03 +08:00
|
|
|
static int evp_rand_get_ctx_params_locked(EVP_RAND_CTX *ctx,
|
|
|
|
OSSL_PARAM params[])
|
|
|
|
{
|
2021-05-14 11:08:42 +08:00
|
|
|
return ctx->meth->get_ctx_params(ctx->algctx, params);
|
2020-07-01 08:57:03 +08:00
|
|
|
}
|
|
|
|
|
2021-04-16 07:13:43 +08:00
|
|
|
int EVP_RAND_CTX_get_params(EVP_RAND_CTX *ctx, OSSL_PARAM params[])
|
2020-04-30 12:46:12 +08:00
|
|
|
{
|
2020-06-30 07:36:47 +08:00
|
|
|
int res;
|
2020-05-08 08:22:45 +08:00
|
|
|
|
2020-06-30 07:36:47 +08:00
|
|
|
if (!evp_rand_lock(ctx))
|
|
|
|
return 0;
|
2020-07-01 08:57:03 +08:00
|
|
|
res = evp_rand_get_ctx_params_locked(ctx, params);
|
2020-06-30 07:36:47 +08:00
|
|
|
evp_rand_unlock(ctx);
|
2020-05-08 08:22:45 +08:00
|
|
|
return res;
|
2020-04-30 12:46:12 +08:00
|
|
|
}
|
|
|
|
|
2020-07-01 08:57:03 +08:00
|
|
|
static int evp_rand_set_ctx_params_locked(EVP_RAND_CTX *ctx,
|
|
|
|
const OSSL_PARAM params[])
|
|
|
|
{
|
|
|
|
if (ctx->meth->set_ctx_params != NULL)
|
2021-05-14 11:08:42 +08:00
|
|
|
return ctx->meth->set_ctx_params(ctx->algctx, params);
|
2020-07-01 08:57:03 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-04-16 07:13:43 +08:00
|
|
|
int EVP_RAND_CTX_set_params(EVP_RAND_CTX *ctx, const OSSL_PARAM params[])
|
2020-04-30 12:46:12 +08:00
|
|
|
{
|
2020-07-01 08:57:03 +08:00
|
|
|
int res;
|
2020-05-08 08:22:45 +08:00
|
|
|
|
2020-07-01 08:57:03 +08:00
|
|
|
if (!evp_rand_lock(ctx))
|
|
|
|
return 0;
|
|
|
|
res = evp_rand_set_ctx_params_locked(ctx, params);
|
|
|
|
evp_rand_unlock(ctx);
|
2020-05-08 08:22:45 +08:00
|
|
|
return res;
|
2020-04-30 12:46:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const OSSL_PARAM *EVP_RAND_gettable_params(const EVP_RAND *rand)
|
|
|
|
{
|
2020-08-05 11:23:16 +08:00
|
|
|
if (rand->gettable_params == NULL)
|
|
|
|
return NULL;
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
return rand->gettable_params(ossl_provider_ctx(EVP_RAND_get0_provider(rand)));
|
2020-04-30 12:46:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const OSSL_PARAM *EVP_RAND_gettable_ctx_params(const EVP_RAND *rand)
|
|
|
|
{
|
2021-02-23 07:52:15 +08:00
|
|
|
void *provctx;
|
|
|
|
|
2020-09-22 06:29:58 +08:00
|
|
|
if (rand->gettable_ctx_params == NULL)
|
2020-08-05 11:23:16 +08:00
|
|
|
return NULL;
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
provctx = ossl_provider_ctx(EVP_RAND_get0_provider(rand));
|
2021-02-23 07:52:15 +08:00
|
|
|
return rand->gettable_ctx_params(NULL, provctx);
|
2020-04-30 12:46:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const OSSL_PARAM *EVP_RAND_settable_ctx_params(const EVP_RAND *rand)
|
|
|
|
{
|
2021-02-23 07:52:15 +08:00
|
|
|
void *provctx;
|
|
|
|
|
2020-09-22 06:29:58 +08:00
|
|
|
if (rand->settable_ctx_params == NULL)
|
2020-08-05 11:23:16 +08:00
|
|
|
return NULL;
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
provctx = ossl_provider_ctx(EVP_RAND_get0_provider(rand));
|
2021-02-23 07:52:15 +08:00
|
|
|
return rand->settable_ctx_params(NULL, provctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
const OSSL_PARAM *EVP_RAND_CTX_gettable_params(EVP_RAND_CTX *ctx)
|
|
|
|
{
|
|
|
|
void *provctx;
|
|
|
|
|
|
|
|
if (ctx->meth->gettable_ctx_params == NULL)
|
|
|
|
return NULL;
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
provctx = ossl_provider_ctx(EVP_RAND_get0_provider(ctx->meth));
|
2021-05-14 11:08:42 +08:00
|
|
|
return ctx->meth->gettable_ctx_params(ctx->algctx, provctx);
|
2021-02-23 07:52:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const OSSL_PARAM *EVP_RAND_CTX_settable_params(EVP_RAND_CTX *ctx)
|
|
|
|
{
|
|
|
|
void *provctx;
|
|
|
|
|
|
|
|
if (ctx->meth->settable_ctx_params == NULL)
|
|
|
|
return NULL;
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
provctx = ossl_provider_ctx(EVP_RAND_get0_provider(ctx->meth));
|
2021-05-14 11:08:42 +08:00
|
|
|
return ctx->meth->settable_ctx_params(ctx->algctx, provctx);
|
2020-04-30 12:46:12 +08:00
|
|
|
}
|
|
|
|
|
2020-10-15 17:55:50 +08:00
|
|
|
void EVP_RAND_do_all_provided(OSSL_LIB_CTX *libctx,
|
2020-04-30 12:46:12 +08:00
|
|
|
void (*fn)(EVP_RAND *rand, void *arg),
|
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
evp_generic_do_all(libctx, OSSL_OP_RAND,
|
|
|
|
(void (*)(void *, void *))fn, arg,
|
2021-06-09 13:52:09 +08:00
|
|
|
evp_rand_from_algorithm, evp_rand_up_ref,
|
|
|
|
evp_rand_free);
|
2020-04-30 12:46:12 +08:00
|
|
|
}
|
|
|
|
|
2021-02-20 01:03:43 +08:00
|
|
|
int EVP_RAND_names_do_all(const EVP_RAND *rand,
|
|
|
|
void (*fn)(const char *name, void *data),
|
|
|
|
void *data)
|
2020-04-30 12:46:12 +08:00
|
|
|
{
|
|
|
|
if (rand->prov != NULL)
|
2021-02-20 01:03:43 +08:00
|
|
|
return evp_names_do_all(rand->prov, rand->name_id, fn, data);
|
|
|
|
|
|
|
|
return 1;
|
2020-04-30 12:46:12 +08:00
|
|
|
}
|
|
|
|
|
2020-07-01 08:57:03 +08:00
|
|
|
static int evp_rand_instantiate_locked
|
|
|
|
(EVP_RAND_CTX *ctx, unsigned int strength, int prediction_resistance,
|
2021-02-26 08:52:13 +08:00
|
|
|
const unsigned char *pstr, size_t pstr_len, const OSSL_PARAM params[])
|
2020-07-01 08:57:03 +08:00
|
|
|
{
|
2021-05-14 11:08:42 +08:00
|
|
|
return ctx->meth->instantiate(ctx->algctx, strength, prediction_resistance,
|
2021-02-26 08:52:13 +08:00
|
|
|
pstr, pstr_len, params);
|
2020-07-01 08:57:03 +08:00
|
|
|
}
|
|
|
|
|
2020-05-08 08:25:19 +08:00
|
|
|
int EVP_RAND_instantiate(EVP_RAND_CTX *ctx, unsigned int strength,
|
|
|
|
int prediction_resistance,
|
2021-02-26 08:52:13 +08:00
|
|
|
const unsigned char *pstr, size_t pstr_len,
|
|
|
|
const OSSL_PARAM params[])
|
2020-04-30 12:46:12 +08:00
|
|
|
{
|
2020-05-08 08:22:45 +08:00
|
|
|
int res;
|
|
|
|
|
|
|
|
if (!evp_rand_lock(ctx))
|
|
|
|
return 0;
|
2020-07-01 08:57:03 +08:00
|
|
|
res = evp_rand_instantiate_locked(ctx, strength, prediction_resistance,
|
2021-02-26 08:52:13 +08:00
|
|
|
pstr, pstr_len, params);
|
2020-05-08 08:22:45 +08:00
|
|
|
evp_rand_unlock(ctx);
|
|
|
|
return res;
|
2020-04-30 12:46:12 +08:00
|
|
|
}
|
|
|
|
|
2020-07-01 08:57:03 +08:00
|
|
|
static int evp_rand_uninstantiate_locked(EVP_RAND_CTX *ctx)
|
|
|
|
{
|
2021-05-14 11:08:42 +08:00
|
|
|
return ctx->meth->uninstantiate(ctx->algctx);
|
2020-07-01 08:57:03 +08:00
|
|
|
}
|
|
|
|
|
2020-05-08 08:25:19 +08:00
|
|
|
int EVP_RAND_uninstantiate(EVP_RAND_CTX *ctx)
|
2020-04-30 12:46:12 +08:00
|
|
|
{
|
2020-05-08 08:22:45 +08:00
|
|
|
int res;
|
|
|
|
|
|
|
|
if (!evp_rand_lock(ctx))
|
|
|
|
return 0;
|
2020-07-01 08:57:03 +08:00
|
|
|
res = evp_rand_uninstantiate_locked(ctx);
|
2020-05-08 08:22:45 +08:00
|
|
|
evp_rand_unlock(ctx);
|
|
|
|
return res;
|
2020-04-30 12:46:12 +08:00
|
|
|
}
|
|
|
|
|
2020-07-01 08:57:03 +08:00
|
|
|
static int evp_rand_generate_locked(EVP_RAND_CTX *ctx, unsigned char *out,
|
|
|
|
size_t outlen, unsigned int strength,
|
|
|
|
int prediction_resistance,
|
|
|
|
const unsigned char *addin,
|
|
|
|
size_t addin_len)
|
2020-04-30 12:46:12 +08:00
|
|
|
{
|
2020-07-02 08:45:23 +08:00
|
|
|
size_t chunk, max_request = 0;
|
|
|
|
OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
|
2020-05-08 08:22:45 +08:00
|
|
|
|
2020-10-30 13:53:22 +08:00
|
|
|
params[0] = OSSL_PARAM_construct_size_t(OSSL_RAND_PARAM_MAX_REQUEST,
|
2020-07-02 08:45:23 +08:00
|
|
|
&max_request);
|
|
|
|
if (!evp_rand_get_ctx_params_locked(ctx, params)
|
|
|
|
|| max_request == 0) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_EVP, EVP_R_UNABLE_TO_GET_MAXIMUM_REQUEST_SIZE);
|
2020-07-02 08:45:23 +08:00
|
|
|
return 0;
|
2020-05-08 08:22:45 +08:00
|
|
|
}
|
|
|
|
for (; outlen > 0; outlen -= chunk, out += chunk) {
|
2020-07-02 08:45:23 +08:00
|
|
|
chunk = outlen > max_request ? max_request : outlen;
|
2021-05-14 11:08:42 +08:00
|
|
|
if (!ctx->meth->generate(ctx->algctx, out, chunk, strength,
|
2020-05-08 08:25:19 +08:00
|
|
|
prediction_resistance, addin, addin_len)) {
|
2020-11-04 19:23:19 +08:00
|
|
|
ERR_raise(ERR_LIB_EVP, EVP_R_GENERATE_ERROR);
|
2020-07-01 08:57:03 +08:00
|
|
|
return 0;
|
2020-05-08 08:25:19 +08:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Prediction resistance is only relevant the first time around,
|
|
|
|
* subsequently, the DRBG has already been properly reseeded.
|
|
|
|
*/
|
|
|
|
prediction_resistance = 0;
|
2020-05-08 08:22:45 +08:00
|
|
|
}
|
2020-07-01 08:57:03 +08:00
|
|
|
return 1;
|
2020-04-30 12:46:12 +08:00
|
|
|
}
|
|
|
|
|
2020-07-01 08:57:03 +08:00
|
|
|
int EVP_RAND_generate(EVP_RAND_CTX *ctx, unsigned char *out, size_t outlen,
|
|
|
|
unsigned int strength, int prediction_resistance,
|
|
|
|
const unsigned char *addin, size_t addin_len)
|
2020-04-30 12:46:12 +08:00
|
|
|
{
|
2020-07-01 08:57:03 +08:00
|
|
|
int res;
|
2020-05-08 08:22:45 +08:00
|
|
|
|
|
|
|
if (!evp_rand_lock(ctx))
|
|
|
|
return 0;
|
2020-07-01 08:57:03 +08:00
|
|
|
res = evp_rand_generate_locked(ctx, out, outlen, strength,
|
|
|
|
prediction_resistance, addin, addin_len);
|
2020-05-08 08:22:45 +08:00
|
|
|
evp_rand_unlock(ctx);
|
|
|
|
return res;
|
2020-04-30 12:46:12 +08:00
|
|
|
}
|
|
|
|
|
2020-07-01 08:57:03 +08:00
|
|
|
static int evp_rand_reseed_locked(EVP_RAND_CTX *ctx, int prediction_resistance,
|
|
|
|
const unsigned char *ent, size_t ent_len,
|
|
|
|
const unsigned char *addin, size_t addin_len)
|
|
|
|
{
|
|
|
|
if (ctx->meth->reseed != NULL)
|
2021-05-14 11:08:42 +08:00
|
|
|
return ctx->meth->reseed(ctx->algctx, prediction_resistance,
|
2020-07-01 08:57:03 +08:00
|
|
|
ent, ent_len, addin, addin_len);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int EVP_RAND_reseed(EVP_RAND_CTX *ctx, int prediction_resistance,
|
|
|
|
const unsigned char *ent, size_t ent_len,
|
|
|
|
const unsigned char *addin, size_t addin_len)
|
2020-04-30 12:46:12 +08:00
|
|
|
{
|
2020-07-01 08:57:03 +08:00
|
|
|
int res;
|
2020-05-08 08:22:45 +08:00
|
|
|
|
|
|
|
if (!evp_rand_lock(ctx))
|
|
|
|
return 0;
|
2020-07-01 08:57:03 +08:00
|
|
|
res = evp_rand_reseed_locked(ctx, prediction_resistance,
|
|
|
|
ent, ent_len, addin, addin_len);
|
2020-05-08 08:22:45 +08:00
|
|
|
evp_rand_unlock(ctx);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2020-07-01 08:57:03 +08:00
|
|
|
static unsigned int evp_rand_strength_locked(EVP_RAND_CTX *ctx)
|
2020-05-08 08:22:45 +08:00
|
|
|
{
|
2020-07-01 08:57:03 +08:00
|
|
|
OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
|
2020-07-02 08:45:23 +08:00
|
|
|
unsigned int strength = 0;
|
2020-05-08 08:22:45 +08:00
|
|
|
|
2020-07-02 08:45:23 +08:00
|
|
|
params[0] = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, &strength);
|
|
|
|
if (!evp_rand_get_ctx_params_locked(ctx, params))
|
|
|
|
return 0;
|
|
|
|
return strength;
|
2020-04-30 12:46:12 +08:00
|
|
|
}
|
|
|
|
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
unsigned int EVP_RAND_get_strength(EVP_RAND_CTX *ctx)
|
2020-07-01 08:57:03 +08:00
|
|
|
{
|
|
|
|
unsigned int res;
|
|
|
|
|
|
|
|
if (!evp_rand_lock(ctx))
|
|
|
|
return 0;
|
|
|
|
res = evp_rand_strength_locked(ctx);
|
|
|
|
evp_rand_unlock(ctx);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int evp_rand_nonce_locked(EVP_RAND_CTX *ctx, unsigned char *out,
|
|
|
|
size_t outlen)
|
|
|
|
{
|
|
|
|
unsigned int str = evp_rand_strength_locked(ctx);
|
|
|
|
|
|
|
|
if (ctx->meth->nonce == NULL)
|
|
|
|
return 0;
|
2021-05-14 11:08:42 +08:00
|
|
|
if (ctx->meth->nonce(ctx->algctx, out, str, outlen, outlen))
|
2020-07-01 08:57:03 +08:00
|
|
|
return 1;
|
|
|
|
return evp_rand_generate_locked(ctx, out, outlen, str, 0, NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int EVP_RAND_nonce(EVP_RAND_CTX *ctx, unsigned char *out, size_t outlen)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
if (!evp_rand_lock(ctx))
|
|
|
|
return 0;
|
|
|
|
res = evp_rand_nonce_locked(ctx, out, outlen);
|
|
|
|
evp_rand_unlock(ctx);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
Rename all getters to use get/get0 in name
For functions that exist in 1.1.1 provide a simple aliases via #define.
Fixes #15236
Functions with OSSL_DECODER_, OSSL_ENCODER_, OSSL_STORE_LOADER_,
EVP_KEYEXCH_, EVP_KEM_, EVP_ASYM_CIPHER_, EVP_SIGNATURE_,
EVP_KEYMGMT_, EVP_RAND_, EVP_MAC_, EVP_KDF_, EVP_PKEY_,
EVP_MD_, and EVP_CIPHER_ prefixes are renamed.
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15405)
2021-05-21 22:58:08 +08:00
|
|
|
int EVP_RAND_get_state(EVP_RAND_CTX *ctx)
|
2020-04-30 12:46:12 +08:00
|
|
|
{
|
2020-05-08 08:22:45 +08:00
|
|
|
OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
|
2020-07-01 08:57:03 +08:00
|
|
|
int state;
|
2020-05-08 08:22:45 +08:00
|
|
|
|
2020-07-01 08:57:03 +08:00
|
|
|
params[0] = OSSL_PARAM_construct_int(OSSL_RAND_PARAM_STATE, &state);
|
2021-04-16 07:13:43 +08:00
|
|
|
if (!EVP_RAND_CTX_get_params(ctx, params))
|
2020-07-01 08:57:03 +08:00
|
|
|
state = EVP_RAND_STATE_ERROR;
|
|
|
|
return state;
|
2020-04-30 12:46:12 +08:00
|
|
|
}
|
|
|
|
|
2020-07-01 08:57:03 +08:00
|
|
|
static int evp_rand_verify_zeroization_locked(EVP_RAND_CTX *ctx)
|
|
|
|
{
|
|
|
|
if (ctx->meth->verify_zeroization != NULL)
|
2021-05-14 11:08:42 +08:00
|
|
|
return ctx->meth->verify_zeroization(ctx->algctx);
|
2020-07-01 08:57:03 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-08 08:25:19 +08:00
|
|
|
int EVP_RAND_verify_zeroization(EVP_RAND_CTX *ctx)
|
2020-05-08 08:22:45 +08:00
|
|
|
{
|
2020-07-01 08:57:03 +08:00
|
|
|
int res;
|
2020-05-08 08:22:45 +08:00
|
|
|
|
2020-07-01 08:57:03 +08:00
|
|
|
if (!evp_rand_lock(ctx))
|
|
|
|
return 0;
|
|
|
|
res = evp_rand_verify_zeroization_locked(ctx);
|
|
|
|
evp_rand_unlock(ctx);
|
2020-05-08 08:22:45 +08:00
|
|
|
return res;
|
|
|
|
}
|