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 <openssl/engine.h>
15 #include <openssl/evp.h>
16 #include <openssl/x509v3.h>
17 #include <openssl/rand.h>
18 #include <openssl/core.h>
19 #include <openssl/core_names.h>
20 #include <openssl/crypto.h>
21 #include "crypto/asn1.h"
22 #include "crypto/evp.h"
23 #include "internal/cryptlib.h"
24 #include "internal/numbers.h"
25 #include "internal/provider.h"
26 #include "evp_local.h"
31 CRYPTO_REF_COUNT refcnt
;
32 CRYPTO_RWLOCK
*refcnt_lock
;
34 const OSSL_DISPATCH
*dispatch
;
35 OSSL_FUNC_rand_newctx_fn
*newctx
;
36 OSSL_FUNC_rand_freectx_fn
*freectx
;
37 OSSL_FUNC_rand_instantiate_fn
*instantiate
;
38 OSSL_FUNC_rand_uninstantiate_fn
*uninstantiate
;
39 OSSL_FUNC_rand_generate_fn
*generate
;
40 OSSL_FUNC_rand_reseed_fn
*reseed
;
41 OSSL_FUNC_rand_nonce_fn
*nonce
;
42 OSSL_FUNC_rand_enable_locking_fn
*enable_locking
;
43 OSSL_FUNC_rand_lock_fn
*lock
;
44 OSSL_FUNC_rand_unlock_fn
*unlock
;
45 OSSL_FUNC_rand_gettable_params_fn
*gettable_params
;
46 OSSL_FUNC_rand_gettable_ctx_params_fn
*gettable_ctx_params
;
47 OSSL_FUNC_rand_settable_ctx_params_fn
*settable_ctx_params
;
48 OSSL_FUNC_rand_get_params_fn
*get_params
;
49 OSSL_FUNC_rand_get_ctx_params_fn
*get_ctx_params
;
50 OSSL_FUNC_rand_set_ctx_params_fn
*set_ctx_params
;
51 OSSL_FUNC_rand_verify_zeroization_fn
*verify_zeroization
;
54 static int evp_rand_up_ref(void *vrand
)
56 EVP_RAND
*rand
= (EVP_RAND
*)vrand
;
60 return CRYPTO_UP_REF(&rand
->refcnt
, &ref
, rand
->refcnt_lock
);
64 static void evp_rand_free(void *vrand
){
65 EVP_RAND
*rand
= (EVP_RAND
*)vrand
;
69 CRYPTO_DOWN_REF(&rand
->refcnt
, &ref
, rand
->refcnt_lock
);
71 ossl_provider_free(rand
->prov
);
72 CRYPTO_THREAD_lock_free(rand
->refcnt_lock
);
78 static void *evp_rand_new(void)
80 EVP_RAND
*rand
= OPENSSL_zalloc(sizeof(*rand
));
83 || (rand
->refcnt_lock
= CRYPTO_THREAD_lock_new()) == NULL
) {
91 /* Enable locking of the underlying DRBG/RAND if available */
92 int EVP_RAND_enable_locking(EVP_RAND_CTX
*rand
)
94 if (rand
->meth
->enable_locking
!= NULL
)
95 return rand
->meth
->enable_locking(rand
->data
);
96 EVPerr(0, EVP_R_LOCKING_NOT_SUPPORTED
);
100 /* Lock the underlying DRBG/RAND if available */
101 static int evp_rand_lock(EVP_RAND_CTX
*rand
)
103 if (rand
->meth
->lock
!= NULL
)
104 return rand
->meth
->lock(rand
->data
);
108 /* Unlock the underlying DRBG/RAND if available */
109 static void evp_rand_unlock(EVP_RAND_CTX
*rand
)
111 if (rand
->meth
->unlock
!= NULL
)
112 rand
->meth
->unlock(rand
->data
);
115 static void *evp_rand_from_dispatch(int name_id
,
116 const OSSL_DISPATCH
*fns
,
119 EVP_RAND
*rand
= NULL
;
120 int fnrandcnt
= 0, fnctxcnt
= 0, fnlockcnt
= 0;
122 int fnzeroizecnt
= 0;
125 if ((rand
= evp_rand_new()) == NULL
) {
126 EVPerr(0, ERR_R_MALLOC_FAILURE
);
129 rand
->name_id
= name_id
;
130 rand
->dispatch
= fns
;
131 for (; fns
->function_id
!= 0; fns
++) {
132 switch (fns
->function_id
) {
133 case OSSL_FUNC_RAND_NEWCTX
:
134 if (rand
->newctx
!= NULL
)
136 rand
->newctx
= OSSL_FUNC_rand_newctx(fns
);
139 case OSSL_FUNC_RAND_FREECTX
:
140 if (rand
->freectx
!= NULL
)
142 rand
->freectx
= OSSL_FUNC_rand_freectx(fns
);
145 case OSSL_FUNC_RAND_INSTANTIATE
:
146 if (rand
->instantiate
!= NULL
)
148 rand
->instantiate
= OSSL_FUNC_rand_instantiate(fns
);
151 case OSSL_FUNC_RAND_UNINSTANTIATE
:
152 if (rand
->uninstantiate
!= NULL
)
154 rand
->uninstantiate
= OSSL_FUNC_rand_uninstantiate(fns
);
157 case OSSL_FUNC_RAND_GENERATE
:
158 if (rand
->generate
!= NULL
)
160 rand
->generate
= OSSL_FUNC_rand_generate(fns
);
163 case OSSL_FUNC_RAND_RESEED
:
164 if (rand
->reseed
!= NULL
)
166 rand
->reseed
= OSSL_FUNC_rand_reseed(fns
);
168 case OSSL_FUNC_RAND_NONCE
:
169 if (rand
->nonce
!= NULL
)
171 rand
->nonce
= OSSL_FUNC_rand_nonce(fns
);
173 case OSSL_FUNC_RAND_ENABLE_LOCKING
:
174 if (rand
->enable_locking
!= NULL
)
176 rand
->enable_locking
= OSSL_FUNC_rand_enable_locking(fns
);
179 case OSSL_FUNC_RAND_LOCK
:
180 if (rand
->lock
!= NULL
)
182 rand
->lock
= OSSL_FUNC_rand_lock(fns
);
185 case OSSL_FUNC_RAND_UNLOCK
:
186 if (rand
->unlock
!= NULL
)
188 rand
->unlock
= OSSL_FUNC_rand_unlock(fns
);
191 case OSSL_FUNC_RAND_GETTABLE_PARAMS
:
192 if (rand
->gettable_params
!= NULL
)
194 rand
->gettable_params
=
195 OSSL_FUNC_rand_gettable_params(fns
);
197 case OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS
:
198 if (rand
->gettable_ctx_params
!= NULL
)
200 rand
->gettable_ctx_params
=
201 OSSL_FUNC_rand_gettable_ctx_params(fns
);
203 case OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS
:
204 if (rand
->settable_ctx_params
!= NULL
)
206 rand
->settable_ctx_params
=
207 OSSL_FUNC_rand_settable_ctx_params(fns
);
209 case OSSL_FUNC_RAND_GET_PARAMS
:
210 if (rand
->get_params
!= NULL
)
212 rand
->get_params
= OSSL_FUNC_rand_get_params(fns
);
214 case OSSL_FUNC_RAND_GET_CTX_PARAMS
:
215 if (rand
->get_ctx_params
!= NULL
)
217 rand
->get_ctx_params
= OSSL_FUNC_rand_get_ctx_params(fns
);
220 case OSSL_FUNC_RAND_SET_CTX_PARAMS
:
221 if (rand
->set_ctx_params
!= NULL
)
223 rand
->set_ctx_params
= OSSL_FUNC_rand_set_ctx_params(fns
);
225 case OSSL_FUNC_RAND_VERIFY_ZEROIZATION
:
226 if (rand
->verify_zeroization
!= NULL
)
228 rand
->verify_zeroization
= OSSL_FUNC_rand_verify_zeroization(fns
);
236 * In order to be a consistent set of functions we must have at least
237 * a complete set of "rand" functions and a complete set of context
238 * management functions. In FIPS mode, we also require the zeroization
239 * verification function.
241 * In addition, if locking can be enabled, we need a complete set of
246 || (fnlockcnt
!= 0 && fnlockcnt
!= 3)
252 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_PROVIDER_FUNCTIONS
);
256 if (prov
!= NULL
&& !ossl_provider_up_ref(prov
)) {
258 ERR_raise(ERR_LIB_EVP
, ERR_R_INTERNAL_ERROR
);
266 EVP_RAND
*EVP_RAND_fetch(OPENSSL_CTX
*libctx
, const char *algorithm
,
267 const char *properties
)
269 return evp_generic_fetch(libctx
, OSSL_OP_RAND
, algorithm
, properties
,
270 evp_rand_from_dispatch
, evp_rand_up_ref
,
274 int EVP_RAND_up_ref(EVP_RAND
*rand
)
276 return evp_rand_up_ref(rand
);
279 void EVP_RAND_free(EVP_RAND
*rand
)
284 int EVP_RAND_number(const EVP_RAND
*rand
)
286 return rand
->name_id
;
289 const char *EVP_RAND_name(const EVP_RAND
*rand
)
291 return evp_first_name(rand
->prov
, rand
->name_id
);
294 int EVP_RAND_is_a(const EVP_RAND
*rand
, const char *name
)
296 return evp_is_a(rand
->prov
, rand
->name_id
, NULL
, name
);
299 const OSSL_PROVIDER
*EVP_RAND_provider(const EVP_RAND
*rand
)
304 int EVP_RAND_get_params(EVP_RAND
*rand
, OSSL_PARAM params
[])
306 if (rand
->get_params
!= NULL
)
307 return rand
->get_params(params
);
311 EVP_RAND_CTX
*EVP_RAND_CTX_new(EVP_RAND
*rand
, EVP_RAND_CTX
*parent
)
314 void *parent_ctx
= NULL
;
315 const OSSL_DISPATCH
*parent_dispatch
= NULL
;
318 EVPerr(0, EVP_R_INVALID_NULL_ALGORITHM
);
322 ctx
= OPENSSL_zalloc(sizeof(*ctx
));
324 EVPerr(0, ERR_R_MALLOC_FAILURE
);
327 if (parent
!= NULL
) {
328 if (!EVP_RAND_enable_locking(parent
)) {
329 EVPerr(0, EVP_R_UNABLE_TO_ENABLE_PARENT_LOCKING
);
333 parent_ctx
= parent
->data
;
334 parent_dispatch
= parent
->meth
->dispatch
;
336 if ((ctx
->data
= rand
->newctx(ossl_provider_ctx(rand
->prov
), parent_ctx
,
337 parent_dispatch
)) == NULL
338 || !EVP_RAND_up_ref(rand
)) {
339 EVPerr(0, ERR_R_MALLOC_FAILURE
);
340 rand
->freectx(ctx
->data
);
348 void EVP_RAND_CTX_free(EVP_RAND_CTX
*ctx
)
351 ctx
->meth
->freectx(ctx
->data
);
353 EVP_RAND_free(ctx
->meth
);
358 EVP_RAND
*EVP_RAND_CTX_rand(EVP_RAND_CTX
*ctx
)
363 static int evp_rand_get_ctx_params_locked(EVP_RAND_CTX
*ctx
,
366 return ctx
->meth
->get_ctx_params(ctx
->data
, params
);
369 int EVP_RAND_get_ctx_params(EVP_RAND_CTX
*ctx
, OSSL_PARAM params
[])
373 if (!evp_rand_lock(ctx
))
375 res
= evp_rand_get_ctx_params_locked(ctx
, params
);
376 evp_rand_unlock(ctx
);
380 static int evp_rand_set_ctx_params_locked(EVP_RAND_CTX
*ctx
,
381 const OSSL_PARAM params
[])
383 if (ctx
->meth
->set_ctx_params
!= NULL
)
384 return ctx
->meth
->set_ctx_params(ctx
->data
, params
);
388 int EVP_RAND_set_ctx_params(EVP_RAND_CTX
*ctx
, const OSSL_PARAM params
[])
392 if (!evp_rand_lock(ctx
))
394 res
= evp_rand_set_ctx_params_locked(ctx
, params
);
395 evp_rand_unlock(ctx
);
399 const OSSL_PARAM
*EVP_RAND_gettable_params(const EVP_RAND
*rand
)
401 if (rand
->gettable_params
== NULL
)
403 return rand
->gettable_params(ossl_provider_ctx(EVP_RAND_provider(rand
)));
406 const OSSL_PARAM
*EVP_RAND_gettable_ctx_params(const EVP_RAND
*rand
)
408 if (rand
->gettable_params
== NULL
)
410 return rand
->gettable_ctx_params(
411 ossl_provider_ctx(EVP_RAND_provider(rand
)));
414 const OSSL_PARAM
*EVP_RAND_settable_ctx_params(const EVP_RAND
*rand
)
416 if (rand
->gettable_params
== NULL
)
418 return rand
->settable_ctx_params(
419 ossl_provider_ctx(EVP_RAND_provider(rand
)));
422 void EVP_RAND_do_all_provided(OPENSSL_CTX
*libctx
,
423 void (*fn
)(EVP_RAND
*rand
, void *arg
),
426 evp_generic_do_all(libctx
, OSSL_OP_RAND
,
427 (void (*)(void *, void *))fn
, arg
,
428 evp_rand_from_dispatch
, evp_rand_free
);
431 void EVP_RAND_names_do_all(const EVP_RAND
*rand
,
432 void (*fn
)(const char *name
, void *data
),
435 if (rand
->prov
!= NULL
)
436 evp_names_do_all(rand
->prov
, rand
->name_id
, fn
, data
);
439 static int evp_rand_instantiate_locked
440 (EVP_RAND_CTX
*ctx
, unsigned int strength
, int prediction_resistance
,
441 const unsigned char *pstr
, size_t pstr_len
)
443 return ctx
->meth
->instantiate(ctx
->data
, strength
, prediction_resistance
,
447 int EVP_RAND_instantiate(EVP_RAND_CTX
*ctx
, unsigned int strength
,
448 int prediction_resistance
,
449 const unsigned char *pstr
, size_t pstr_len
)
453 if (!evp_rand_lock(ctx
))
455 res
= evp_rand_instantiate_locked(ctx
, strength
, prediction_resistance
,
457 evp_rand_unlock(ctx
);
461 static int evp_rand_uninstantiate_locked(EVP_RAND_CTX
*ctx
)
463 return ctx
->meth
->uninstantiate(ctx
->data
);
466 int EVP_RAND_uninstantiate(EVP_RAND_CTX
*ctx
)
470 if (!evp_rand_lock(ctx
))
472 res
= evp_rand_uninstantiate_locked(ctx
);
473 evp_rand_unlock(ctx
);
477 static int evp_rand_generate_locked(EVP_RAND_CTX
*ctx
, unsigned char *out
,
478 size_t outlen
, unsigned int strength
,
479 int prediction_resistance
,
480 const unsigned char *addin
,
483 size_t chunk
, max_request
= 0;
484 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
486 params
[0] = OSSL_PARAM_construct_size_t(OSSL_DRBG_PARAM_MAX_REQUEST
,
488 if (!evp_rand_get_ctx_params_locked(ctx
, params
)
489 || max_request
== 0) {
490 EVPerr(0, EVP_R_UNABLE_TO_GET_MAXIMUM_REQUEST_SIZE
);
493 for (; outlen
> 0; outlen
-= chunk
, out
+= chunk
) {
494 chunk
= outlen
> max_request
? max_request
: outlen
;
495 if (!ctx
->meth
->generate(ctx
->data
, out
, chunk
, strength
,
496 prediction_resistance
, addin
, addin_len
)) {
497 EVPerr(0, EVP_R_GENERATE_ERROR
);
501 * Prediction resistance is only relevant the first time around,
502 * subsequently, the DRBG has already been properly reseeded.
504 prediction_resistance
= 0;
509 int EVP_RAND_generate(EVP_RAND_CTX
*ctx
, unsigned char *out
, size_t outlen
,
510 unsigned int strength
, int prediction_resistance
,
511 const unsigned char *addin
, size_t addin_len
)
515 if (!evp_rand_lock(ctx
))
517 res
= evp_rand_generate_locked(ctx
, out
, outlen
, strength
,
518 prediction_resistance
, addin
, addin_len
);
519 evp_rand_unlock(ctx
);
523 static int evp_rand_reseed_locked(EVP_RAND_CTX
*ctx
, int prediction_resistance
,
524 const unsigned char *ent
, size_t ent_len
,
525 const unsigned char *addin
, size_t addin_len
)
527 if (ctx
->meth
->reseed
!= NULL
)
528 return ctx
->meth
->reseed(ctx
->data
, prediction_resistance
,
529 ent
, ent_len
, addin
, addin_len
);
533 int EVP_RAND_reseed(EVP_RAND_CTX
*ctx
, int prediction_resistance
,
534 const unsigned char *ent
, size_t ent_len
,
535 const unsigned char *addin
, size_t addin_len
)
539 if (!evp_rand_lock(ctx
))
541 res
= evp_rand_reseed_locked(ctx
, prediction_resistance
,
542 ent
, ent_len
, addin
, addin_len
);
543 evp_rand_unlock(ctx
);
547 static unsigned int evp_rand_strength_locked(EVP_RAND_CTX
*ctx
)
549 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
550 unsigned int strength
= 0;
552 params
[0] = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH
, &strength
);
553 if (!evp_rand_get_ctx_params_locked(ctx
, params
))
558 unsigned int EVP_RAND_strength(EVP_RAND_CTX
*ctx
)
562 if (!evp_rand_lock(ctx
))
564 res
= evp_rand_strength_locked(ctx
);
565 evp_rand_unlock(ctx
);
569 static int evp_rand_nonce_locked(EVP_RAND_CTX
*ctx
, unsigned char *out
,
572 unsigned int str
= evp_rand_strength_locked(ctx
);
574 if (ctx
->meth
->nonce
== NULL
)
576 if (ctx
->meth
->nonce(ctx
->data
, out
, str
, outlen
, outlen
))
578 return evp_rand_generate_locked(ctx
, out
, outlen
, str
, 0, NULL
, 0);
581 int EVP_RAND_nonce(EVP_RAND_CTX
*ctx
, unsigned char *out
, size_t outlen
)
585 if (!evp_rand_lock(ctx
))
587 res
= evp_rand_nonce_locked(ctx
, out
, outlen
);
588 evp_rand_unlock(ctx
);
592 int EVP_RAND_state(EVP_RAND_CTX
*ctx
)
594 OSSL_PARAM params
[2] = { OSSL_PARAM_END
, OSSL_PARAM_END
};
597 params
[0] = OSSL_PARAM_construct_int(OSSL_RAND_PARAM_STATE
, &state
);
598 if (!EVP_RAND_get_ctx_params(ctx
, params
))
599 state
= EVP_RAND_STATE_ERROR
;
603 static int evp_rand_verify_zeroization_locked(EVP_RAND_CTX
*ctx
)
605 if (ctx
->meth
->verify_zeroization
!= NULL
)
606 return ctx
->meth
->verify_zeroization(ctx
->data
);
610 int EVP_RAND_verify_zeroization(EVP_RAND_CTX
*ctx
)
614 if (!evp_rand_lock(ctx
))
616 res
= evp_rand_verify_zeroization_locked(ctx
);
617 evp_rand_unlock(ctx
);