2 * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include <openssl/evp.h>
14 #include "internal/cryptlib.h"
15 #include <openssl/engine.h>
16 #include <openssl/evp.h>
17 #include <openssl/x509v3.h>
18 #include <openssl/rand.h>
19 #include <openssl/core.h>
20 #include <openssl/core_names.h>
21 #include <openssl/crypto.h>
22 #include "crypto/asn1.h"
23 #include "crypto/evp.h"
24 #include "internal/numbers.h"
25 #include "internal/provider.h"
26 #include "evp_local.h"
28 static int evp_rand_up_ref(void *vrand)
30 EVP_RAND *rand = (EVP_RAND *)vrand;
34 return CRYPTO_UP_REF(&rand->refcnt, &ref, rand->lock);
38 static void evp_rand_free(void *vrand){
39 EVP_RAND *rand = (EVP_RAND *)vrand;
43 CRYPTO_DOWN_REF(&rand->refcnt, &ref, rand->lock);
45 ossl_provider_free(rand->prov);
46 CRYPTO_THREAD_lock_free(rand->lock);
52 static void *evp_rand_new(void)
54 EVP_RAND *rand = NULL;
56 if ((rand = OPENSSL_zalloc(sizeof(*rand))) == NULL
57 || (rand->lock = CRYPTO_THREAD_lock_new()) == NULL) {
67 static void *evp_rand_from_dispatch(int name_id,
68 const OSSL_DISPATCH *fns,
71 EVP_RAND *rand = NULL;
72 int fnrandcnt = 0, fnctxcnt = 0;
74 if ((rand = evp_rand_new()) == NULL) {
75 EVPerr(0, ERR_R_MALLOC_FAILURE);
78 rand->name_id = name_id;
80 for (; fns->function_id != 0; fns++) {
81 switch (fns->function_id) {
82 case OSSL_FUNC_RAND_NEWCTX:
83 if (rand->newctx != NULL)
85 rand->newctx = OSSL_get_OP_rand_newctx(fns);
88 case OSSL_FUNC_RAND_FREECTX:
89 if (rand->freectx != NULL)
91 rand->freectx = OSSL_get_OP_rand_freectx(fns);
94 case OSSL_FUNC_RAND_INSTANTIATE:
95 if (rand->instantiate != NULL)
97 rand->instantiate = OSSL_get_OP_rand_instantiate(fns);
100 case OSSL_FUNC_RAND_UNINSTANTIATE:
101 if (rand->uninstantiate != NULL)
103 rand->uninstantiate = OSSL_get_OP_rand_uninstantiate(fns);
106 case OSSL_FUNC_RAND_GENERATE:
107 if (rand->generate != NULL)
109 rand->generate = OSSL_get_OP_rand_generate(fns);
112 case OSSL_FUNC_RAND_RESEED:
113 if (rand->reseed != NULL)
115 rand->reseed = OSSL_get_OP_rand_reseed(fns);
117 case OSSL_FUNC_RAND_NONCE:
118 if (rand->nonce != NULL)
120 rand->nonce = OSSL_get_OP_rand_nonce(fns);
122 case OSSL_FUNC_RAND_SET_CALLBACKS:
123 if (rand->set_callbacks != NULL)
125 rand->set_callbacks = OSSL_get_OP_rand_set_callbacks(fns);
127 case OSSL_FUNC_RAND_ENABLE_LOCKING:
128 if (rand->enable_prov_locking != NULL)
130 rand->enable_prov_locking = OSSL_get_OP_rand_enable_locking(fns);
132 case OSSL_FUNC_RAND_LOCK:
133 if (rand->prov_lock != NULL)
135 rand->prov_lock = OSSL_get_OP_rand_lock(fns);
137 case OSSL_FUNC_RAND_UNLOCK:
138 if (rand->prov_unlock != NULL)
140 rand->prov_unlock = OSSL_get_OP_rand_unlock(fns);
142 case OSSL_FUNC_RAND_GETTABLE_PARAMS:
143 if (rand->gettable_params != NULL)
145 rand->gettable_params =
146 OSSL_get_OP_rand_gettable_params(fns);
148 case OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS:
149 if (rand->gettable_ctx_params != NULL)
151 rand->gettable_ctx_params =
152 OSSL_get_OP_rand_gettable_ctx_params(fns);
154 case OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS:
155 if (rand->settable_ctx_params != NULL)
157 rand->settable_ctx_params =
158 OSSL_get_OP_rand_settable_ctx_params(fns);
160 case OSSL_FUNC_RAND_GET_PARAMS:
161 if (rand->get_params != NULL)
163 rand->get_params = OSSL_get_OP_rand_get_params(fns);
165 case OSSL_FUNC_RAND_GET_CTX_PARAMS:
166 if (rand->get_ctx_params != NULL)
168 rand->get_ctx_params = OSSL_get_OP_rand_get_ctx_params(fns);
170 case OSSL_FUNC_RAND_SET_CTX_PARAMS:
171 if (rand->set_ctx_params != NULL)
173 rand->set_ctx_params = OSSL_get_OP_rand_set_ctx_params(fns);
177 if (fnrandcnt != 3 || fnctxcnt != 2) {
179 * In order to be a consistent set of functions we must have at least
180 * a complete set of "rand" functions and a complete set of context
181 * management functions.
184 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
189 ossl_provider_up_ref(prov);
194 EVP_RAND *EVP_RAND_fetch(OPENSSL_CTX *libctx, const char *algorithm,
195 const char *properties)
197 return evp_generic_fetch(libctx, OSSL_OP_RAND, algorithm, properties,
198 evp_rand_from_dispatch, evp_rand_up_ref,
202 int EVP_RAND_up_ref(EVP_RAND *rand)
204 return evp_rand_up_ref(rand);
207 void EVP_RAND_free(EVP_RAND *rand)
212 int EVP_RAND_number(const EVP_RAND *rand)
214 return rand->name_id;
217 const char *EVP_RAND_name(const EVP_RAND *rand)
219 return evp_first_name(rand->prov, rand->name_id);
222 int EVP_RAND_is_a(const EVP_RAND *rand, const char *name)
224 return evp_is_a(rand->prov, rand->name_id, NULL, name);
227 const OSSL_PROVIDER *EVP_RAND_provider(const EVP_RAND *rand)
232 int EVP_RAND_get_params(EVP_RAND *rand, OSSL_PARAM params[])
234 if (rand->get_params != NULL)
235 return rand->get_params(params);
239 EVP_RAND_CTX *EVP_RAND_CTX_new(EVP_RAND *rand, int secure, unsigned int df,
240 EVP_RAND_CTX *parent)
243 void *parent_ctx = NULL;
244 const OSSL_DISPATCH *parent_dispatch = NULL;
249 ctx = OPENSSL_zalloc(sizeof(EVP_RAND_CTX));
252 if (parent != NULL) {
253 parent_ctx = parent->data;
254 parent_dispatch = parent->meth->dispatch;
256 if ((ctx->data = rand->newctx(ossl_provider_ctx(rand->prov), secure, df,
257 parent_ctx, parent_dispatch)) == NULL
258 || !EVP_RAND_up_ref(rand)) {
259 EVPerr(0, ERR_R_MALLOC_FAILURE);
260 rand->freectx(ctx->data);
268 void EVP_RAND_CTX_free(EVP_RAND_CTX *ctx)
271 ctx->meth->freectx(ctx->data);
273 EVP_RAND_CTX_free(ctx->parent);
274 EVP_RAND_free(ctx->meth);
279 EVP_RAND *EVP_RAND_CTX_rand(EVP_RAND_CTX *ctx)
284 int EVP_RAND_CTX_get_params(EVP_RAND_CTX *ctx, OSSL_PARAM params[])
286 if (ctx->meth->get_ctx_params != NULL)
287 return ctx->meth->get_ctx_params(ctx->data, params);
291 int EVP_RAND_CTX_set_params(EVP_RAND_CTX *ctx, const OSSL_PARAM params[])
293 if (ctx->meth->set_ctx_params != NULL)
294 return ctx->meth->set_ctx_params(ctx->data, params);
298 const OSSL_PARAM *EVP_RAND_gettable_params(const EVP_RAND *rand)
300 if (rand->gettable_params == NULL)
302 return rand->gettable_params();
305 const OSSL_PARAM *EVP_RAND_gettable_ctx_params(const EVP_RAND *rand)
307 if (rand->gettable_ctx_params == NULL)
309 return rand->gettable_ctx_params();
312 const OSSL_PARAM *EVP_RAND_settable_ctx_params(const EVP_RAND *rand)
314 if (rand->settable_ctx_params == NULL)
316 return rand->settable_ctx_params();
319 void EVP_RAND_do_all_provided(OPENSSL_CTX *libctx,
320 void (*fn)(EVP_RAND *rand, void *arg),
323 evp_generic_do_all(libctx, OSSL_OP_RAND,
324 (void (*)(void *, void *))fn, arg,
325 evp_rand_from_dispatch, evp_rand_free);
328 void EVP_RAND_names_do_all(const EVP_RAND *rand,
329 void (*fn)(const char *name, void *data),
332 if (rand->prov != NULL)
333 evp_names_do_all(rand->prov, rand->name_id, fn, data);
336 int EVP_RAND_CTX_instantiate(EVP_RAND_CTX *ctx, int strength,
337 int prediction_resistance,
338 const unsigned char *pstr, size_t pstr_len)
340 return ctx->meth->instantiate(ctx->data, strength, prediction_resistance,
344 int EVP_RAND_CTX_uninstantiate(EVP_RAND_CTX *ctx)
346 return ctx->meth->uninstantiate(ctx->data);
349 int EVP_RAND_CTX_generate(EVP_RAND_CTX *ctx, unsigned char *out, size_t outlen,
350 int strength, int prediction_resistance,
351 const unsigned char *addin, size_t addin_len)
353 return ctx->meth->generate(ctx->data, out, outlen, strength,
354 prediction_resistance, addin, addin_len);
357 int EVP_RAND_CTX_reseed(EVP_RAND_CTX *ctx, int prediction_resistance,
358 const unsigned char *addin, size_t addin_len)
360 if (ctx->meth->reseed == NULL)
362 return ctx->meth->reseed(ctx->data, prediction_resistance,
366 int EVP_RAND_CTX_nonce(EVP_RAND_CTX *ctx, unsigned char *out, size_t outlen)
368 if (ctx->meth->nonce != NULL)
369 return ctx->meth->nonce(ctx->data, out, outlen);
370 return ctx->meth->generate(ctx->data, out, outlen, 0, 0, NULL, 0);
373 int EVP_RAND_CTX_set_callbacks(const EVP_RAND_CTX *ctx,
374 OSSL_CALLBACK *get_entropy,
375 OSSL_CALLBACK *cleanup_entropy,
376 OSSL_CALLBACK *get_nonce,
377 OSSL_CALLBACK *cleanup_nonce)
379 if (ctx->meth->set_callbacks == NULL)
381 return ctx->meth->set_callbacks(ctx->data, get_entropy, cleanup_entropy,
382 get_nonce, cleanup_nonce);