Adapt all public EVP_XXX_do_all_provided() for the changed evp_generic_do_all()
[openssl.git] / crypto / evp / evp_rand.c
1 /*
2  * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <openssl/evp.h>
11
12 #include <stdio.h>
13 #include <stdlib.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 "internal/cryptlib.h"
22 #include "internal/numbers.h"
23 #include "internal/provider.h"
24 #include "internal/core.h"
25 #include "crypto/asn1.h"
26 #include "crypto/evp.h"
27 #include "evp_local.h"
28
29 struct evp_rand_st {
30     OSSL_PROVIDER *prov;
31     int name_id;
32     char *type_name;
33     const char *description;
34     CRYPTO_REF_COUNT refcnt;
35     CRYPTO_RWLOCK *refcnt_lock;
36
37     const OSSL_DISPATCH *dispatch;
38     OSSL_FUNC_rand_newctx_fn *newctx;
39     OSSL_FUNC_rand_freectx_fn *freectx;
40     OSSL_FUNC_rand_instantiate_fn *instantiate;
41     OSSL_FUNC_rand_uninstantiate_fn *uninstantiate;
42     OSSL_FUNC_rand_generate_fn *generate;
43     OSSL_FUNC_rand_reseed_fn *reseed;
44     OSSL_FUNC_rand_nonce_fn *nonce;
45     OSSL_FUNC_rand_enable_locking_fn *enable_locking;
46     OSSL_FUNC_rand_lock_fn *lock;
47     OSSL_FUNC_rand_unlock_fn *unlock;
48     OSSL_FUNC_rand_gettable_params_fn *gettable_params;
49     OSSL_FUNC_rand_gettable_ctx_params_fn *gettable_ctx_params;
50     OSSL_FUNC_rand_settable_ctx_params_fn *settable_ctx_params;
51     OSSL_FUNC_rand_get_params_fn *get_params;
52     OSSL_FUNC_rand_get_ctx_params_fn *get_ctx_params;
53     OSSL_FUNC_rand_set_ctx_params_fn *set_ctx_params;
54     OSSL_FUNC_rand_verify_zeroization_fn *verify_zeroization;
55 } /* EVP_RAND */ ;
56
57 static int evp_rand_up_ref(void *vrand)
58 {
59     EVP_RAND *rand = (EVP_RAND *)vrand;
60     int ref = 0;
61
62     if (rand != NULL)
63         return CRYPTO_UP_REF(&rand->refcnt, &ref, rand->refcnt_lock);
64     return 1;
65 }
66
67 static void evp_rand_free(void *vrand)
68 {
69     EVP_RAND *rand = (EVP_RAND *)vrand;
70     int ref = 0;
71
72     if (rand == NULL)
73         return;
74     CRYPTO_DOWN_REF(&rand->refcnt, &ref, rand->refcnt_lock);
75     if (ref > 0)
76         return;
77     OPENSSL_free(rand->type_name);
78     ossl_provider_free(rand->prov);
79     CRYPTO_THREAD_lock_free(rand->refcnt_lock);
80     OPENSSL_free(rand);
81 }
82
83 static void *evp_rand_new(void)
84 {
85     EVP_RAND *rand = OPENSSL_zalloc(sizeof(*rand));
86
87     if (rand == NULL
88             || (rand->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL) {
89         OPENSSL_free(rand);
90         return NULL;
91     }
92     rand->refcnt = 1;
93     return rand;
94 }
95
96 /* Enable locking of the underlying DRBG/RAND if available */
97 int EVP_RAND_enable_locking(EVP_RAND_CTX *rand)
98 {
99     if (rand->meth->enable_locking != NULL)
100         return rand->meth->enable_locking(rand->algctx);
101     ERR_raise(ERR_LIB_EVP, EVP_R_LOCKING_NOT_SUPPORTED);
102     return 0;
103 }
104
105 /* Lock the underlying DRBG/RAND if available */
106 static int evp_rand_lock(EVP_RAND_CTX *rand)
107 {
108     if (rand->meth->lock != NULL)
109         return rand->meth->lock(rand->algctx);
110     return 1;
111 }
112
113 /* Unlock the underlying DRBG/RAND if available */
114 static void evp_rand_unlock(EVP_RAND_CTX *rand)
115 {
116     if (rand->meth->unlock != NULL)
117         rand->meth->unlock(rand->algctx);
118 }
119
120 static void *evp_rand_from_algorithm(int name_id,
121                                      const OSSL_ALGORITHM *algodef,
122                                      OSSL_PROVIDER *prov)
123 {
124     const OSSL_DISPATCH *fns = algodef->implementation;
125     EVP_RAND *rand = NULL;
126     int fnrandcnt = 0, fnctxcnt = 0, fnlockcnt = 0, fnenablelockcnt = 0;
127 #ifdef FIPS_MODULE
128     int fnzeroizecnt = 0;
129 #endif
130
131     if ((rand = evp_rand_new()) == NULL) {
132         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
133         return NULL;
134     }
135     rand->name_id = name_id;
136     if ((rand->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) {
137         evp_rand_free(rand);
138         return NULL;
139     }
140     rand->description = algodef->algorithm_description;
141     rand->dispatch = fns;
142     for (; fns->function_id != 0; fns++) {
143         switch (fns->function_id) {
144         case OSSL_FUNC_RAND_NEWCTX:
145             if (rand->newctx != NULL)
146                 break;
147             rand->newctx = OSSL_FUNC_rand_newctx(fns);
148             fnctxcnt++;
149             break;
150         case OSSL_FUNC_RAND_FREECTX:
151             if (rand->freectx != NULL)
152                 break;
153             rand->freectx = OSSL_FUNC_rand_freectx(fns);
154             fnctxcnt++;
155             break;
156         case OSSL_FUNC_RAND_INSTANTIATE:
157             if (rand->instantiate != NULL)
158                 break;
159             rand->instantiate = OSSL_FUNC_rand_instantiate(fns);
160             fnrandcnt++;
161             break;
162         case OSSL_FUNC_RAND_UNINSTANTIATE:
163              if (rand->uninstantiate != NULL)
164                 break;
165             rand->uninstantiate = OSSL_FUNC_rand_uninstantiate(fns);
166             fnrandcnt++;
167             break;
168         case OSSL_FUNC_RAND_GENERATE:
169             if (rand->generate != NULL)
170                 break;
171             rand->generate = OSSL_FUNC_rand_generate(fns);
172             fnrandcnt++;
173             break;
174         case OSSL_FUNC_RAND_RESEED:
175             if (rand->reseed != NULL)
176                 break;
177             rand->reseed = OSSL_FUNC_rand_reseed(fns);
178             break;
179         case OSSL_FUNC_RAND_NONCE:
180             if (rand->nonce != NULL)
181                 break;
182             rand->nonce = OSSL_FUNC_rand_nonce(fns);
183             break;
184         case OSSL_FUNC_RAND_ENABLE_LOCKING:
185             if (rand->enable_locking != NULL)
186                 break;
187             rand->enable_locking = OSSL_FUNC_rand_enable_locking(fns);
188             fnenablelockcnt++;
189             break;
190         case OSSL_FUNC_RAND_LOCK:
191             if (rand->lock != NULL)
192                 break;
193             rand->lock = OSSL_FUNC_rand_lock(fns);
194             fnlockcnt++;
195             break;
196         case OSSL_FUNC_RAND_UNLOCK:
197             if (rand->unlock != NULL)
198                 break;
199             rand->unlock = OSSL_FUNC_rand_unlock(fns);
200             fnlockcnt++;
201             break;
202         case OSSL_FUNC_RAND_GETTABLE_PARAMS:
203             if (rand->gettable_params != NULL)
204                 break;
205             rand->gettable_params =
206                 OSSL_FUNC_rand_gettable_params(fns);
207             break;
208         case OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS:
209             if (rand->gettable_ctx_params != NULL)
210                 break;
211             rand->gettable_ctx_params =
212                 OSSL_FUNC_rand_gettable_ctx_params(fns);
213             break;
214         case OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS:
215             if (rand->settable_ctx_params != NULL)
216                 break;
217             rand->settable_ctx_params =
218                 OSSL_FUNC_rand_settable_ctx_params(fns);
219             break;
220         case OSSL_FUNC_RAND_GET_PARAMS:
221             if (rand->get_params != NULL)
222                 break;
223             rand->get_params = OSSL_FUNC_rand_get_params(fns);
224             break;
225         case OSSL_FUNC_RAND_GET_CTX_PARAMS:
226             if (rand->get_ctx_params != NULL)
227                 break;
228             rand->get_ctx_params = OSSL_FUNC_rand_get_ctx_params(fns);
229             fnctxcnt++;
230             break;
231         case OSSL_FUNC_RAND_SET_CTX_PARAMS:
232             if (rand->set_ctx_params != NULL)
233                 break;
234             rand->set_ctx_params = OSSL_FUNC_rand_set_ctx_params(fns);
235             break;
236         case OSSL_FUNC_RAND_VERIFY_ZEROIZATION:
237             if (rand->verify_zeroization != NULL)
238                 break;
239             rand->verify_zeroization = OSSL_FUNC_rand_verify_zeroization(fns);
240 #ifdef FIPS_MODULE
241             fnzeroizecnt++;
242 #endif
243             break;
244         }
245     }
246     /*
247      * In order to be a consistent set of functions we must have at least
248      * a complete set of "rand" functions and a complete set of context
249      * management functions.  In FIPS mode, we also require the zeroization
250      * verification function.
251      *
252      * In addition, if locking can be enabled, we need a complete set of
253      * locking functions.
254      */
255     if (fnrandcnt != 3
256             || fnctxcnt != 3
257             || (fnenablelockcnt != 0 && fnenablelockcnt != 1)
258             || (fnlockcnt != 0 && fnlockcnt != 2)
259 #ifdef FIPS_MODULE
260             || fnzeroizecnt != 1
261 #endif
262        ) {
263         evp_rand_free(rand);
264         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
265         return NULL;
266     }
267
268     if (prov != NULL && !ossl_provider_up_ref(prov)) {
269         evp_rand_free(rand);
270         ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
271         return NULL;
272     }
273     rand->prov = prov;
274
275     return rand;
276 }
277
278 EVP_RAND *EVP_RAND_fetch(OSSL_LIB_CTX *libctx, const char *algorithm,
279                          const char *properties)
280 {
281     return evp_generic_fetch(libctx, OSSL_OP_RAND, algorithm, properties,
282                              evp_rand_from_algorithm, evp_rand_up_ref,
283                              evp_rand_free);
284 }
285
286 int EVP_RAND_up_ref(EVP_RAND *rand)
287 {
288     return evp_rand_up_ref(rand);
289 }
290
291 void EVP_RAND_free(EVP_RAND *rand)
292 {
293     evp_rand_free(rand);
294 }
295
296 int evp_rand_get_number(const EVP_RAND *rand)
297 {
298     return rand->name_id;
299 }
300
301 const char *EVP_RAND_get0_name(const EVP_RAND *rand)
302 {
303     return rand->type_name;
304 }
305
306 const char *EVP_RAND_get0_description(const EVP_RAND *rand)
307 {
308     return rand->description;
309 }
310
311 int EVP_RAND_is_a(const EVP_RAND *rand, const char *name)
312 {
313     return evp_is_a(rand->prov, rand->name_id, NULL, name);
314 }
315
316 const OSSL_PROVIDER *EVP_RAND_get0_provider(const EVP_RAND *rand)
317 {
318     return rand->prov;
319 }
320
321 int EVP_RAND_get_params(EVP_RAND *rand, OSSL_PARAM params[])
322 {
323     if (rand->get_params != NULL)
324         return rand->get_params(params);
325     return 1;
326 }
327
328 static int evp_rand_ctx_up_ref(EVP_RAND_CTX *ctx)
329 {
330     int ref = 0;
331
332     return CRYPTO_UP_REF(&ctx->refcnt, &ref, ctx->refcnt_lock);
333 }
334
335 EVP_RAND_CTX *EVP_RAND_CTX_new(EVP_RAND *rand, EVP_RAND_CTX *parent)
336 {
337     EVP_RAND_CTX *ctx;
338     void *parent_ctx = NULL;
339     const OSSL_DISPATCH *parent_dispatch = NULL;
340
341     if (rand == NULL) {
342         ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_NULL_ALGORITHM);
343         return NULL;
344     }
345
346     ctx = OPENSSL_zalloc(sizeof(*ctx));
347     if (ctx == NULL || (ctx->refcnt_lock = CRYPTO_THREAD_lock_new()) == NULL) {
348         OPENSSL_free(ctx);
349         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
350         return NULL;
351     }
352     if (parent != NULL) {
353         if (!evp_rand_ctx_up_ref(parent)) {
354             ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
355             CRYPTO_THREAD_lock_free(ctx->refcnt_lock);
356             OPENSSL_free(ctx);
357             return NULL;
358         }
359         parent_ctx = parent->algctx;
360         parent_dispatch = parent->meth->dispatch;
361     }
362     if ((ctx->algctx = rand->newctx(ossl_provider_ctx(rand->prov), parent_ctx,
363                                     parent_dispatch)) == NULL
364             || !EVP_RAND_up_ref(rand)) {
365         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
366         rand->freectx(ctx->algctx);
367         CRYPTO_THREAD_lock_free(ctx->refcnt_lock);
368         OPENSSL_free(ctx);
369         EVP_RAND_CTX_free(parent);
370         return NULL;
371     }
372     ctx->meth = rand;
373     ctx->parent = parent;
374     ctx->refcnt = 1;
375     return ctx;
376 }
377
378 void EVP_RAND_CTX_free(EVP_RAND_CTX *ctx)
379 {
380     int ref = 0;
381     EVP_RAND_CTX *parent;
382
383     if (ctx == NULL)
384         return;
385
386     CRYPTO_DOWN_REF(&ctx->refcnt, &ref, ctx->refcnt_lock);
387     if (ref > 0)
388         return;
389     parent = ctx->parent;
390     ctx->meth->freectx(ctx->algctx);
391     ctx->algctx = NULL;
392     EVP_RAND_free(ctx->meth);
393     CRYPTO_THREAD_lock_free(ctx->refcnt_lock);
394     OPENSSL_free(ctx);
395     EVP_RAND_CTX_free(parent);
396 }
397
398 EVP_RAND *EVP_RAND_CTX_get0_rand(EVP_RAND_CTX *ctx)
399 {
400     return ctx->meth;
401 }
402
403 static int evp_rand_get_ctx_params_locked(EVP_RAND_CTX *ctx,
404                                           OSSL_PARAM params[])
405 {
406     return ctx->meth->get_ctx_params(ctx->algctx, params);
407 }
408
409 int EVP_RAND_CTX_get_params(EVP_RAND_CTX *ctx, OSSL_PARAM params[])
410 {
411     int res;
412
413     if (!evp_rand_lock(ctx))
414         return 0;
415     res = evp_rand_get_ctx_params_locked(ctx, params);
416     evp_rand_unlock(ctx);
417     return res;
418 }
419
420 static int evp_rand_set_ctx_params_locked(EVP_RAND_CTX *ctx,
421                                           const OSSL_PARAM params[])
422 {
423     if (ctx->meth->set_ctx_params != NULL)
424         return ctx->meth->set_ctx_params(ctx->algctx, params);
425     return 1;
426 }
427
428 int EVP_RAND_CTX_set_params(EVP_RAND_CTX *ctx, const OSSL_PARAM params[])
429 {
430     int res;
431
432     if (!evp_rand_lock(ctx))
433         return 0;
434     res = evp_rand_set_ctx_params_locked(ctx, params);
435     evp_rand_unlock(ctx);
436     return res;
437 }
438
439 const OSSL_PARAM *EVP_RAND_gettable_params(const EVP_RAND *rand)
440 {
441     if (rand->gettable_params == NULL)
442         return NULL;
443     return rand->gettable_params(ossl_provider_ctx(EVP_RAND_get0_provider(rand)));
444 }
445
446 const OSSL_PARAM *EVP_RAND_gettable_ctx_params(const EVP_RAND *rand)
447 {
448     void *provctx;
449
450     if (rand->gettable_ctx_params == NULL)
451         return NULL;
452     provctx = ossl_provider_ctx(EVP_RAND_get0_provider(rand));
453     return rand->gettable_ctx_params(NULL, provctx);
454 }
455
456 const OSSL_PARAM *EVP_RAND_settable_ctx_params(const EVP_RAND *rand)
457 {
458     void *provctx;
459
460     if (rand->settable_ctx_params == NULL)
461         return NULL;
462     provctx = ossl_provider_ctx(EVP_RAND_get0_provider(rand));
463     return rand->settable_ctx_params(NULL, provctx);
464 }
465
466 const OSSL_PARAM *EVP_RAND_CTX_gettable_params(EVP_RAND_CTX *ctx)
467 {
468     void *provctx;
469
470     if (ctx->meth->gettable_ctx_params == NULL)
471         return NULL;
472     provctx = ossl_provider_ctx(EVP_RAND_get0_provider(ctx->meth));
473     return ctx->meth->gettable_ctx_params(ctx->algctx, provctx);
474 }
475
476 const OSSL_PARAM *EVP_RAND_CTX_settable_params(EVP_RAND_CTX *ctx)
477 {
478     void *provctx;
479
480     if (ctx->meth->settable_ctx_params == NULL)
481         return NULL;
482     provctx = ossl_provider_ctx(EVP_RAND_get0_provider(ctx->meth));
483     return ctx->meth->settable_ctx_params(ctx->algctx, provctx);
484 }
485
486 void EVP_RAND_do_all_provided(OSSL_LIB_CTX *libctx,
487                               void (*fn)(EVP_RAND *rand, void *arg),
488                               void *arg)
489 {
490     evp_generic_do_all(libctx, OSSL_OP_RAND,
491                        (void (*)(void *, void *))fn, arg,
492                        evp_rand_from_algorithm, evp_rand_up_ref,
493                        evp_rand_free);
494 }
495
496 int EVP_RAND_names_do_all(const EVP_RAND *rand,
497                           void (*fn)(const char *name, void *data),
498                           void *data)
499 {
500     if (rand->prov != NULL)
501         return evp_names_do_all(rand->prov, rand->name_id, fn, data);
502
503     return 1;
504 }
505
506 static int evp_rand_instantiate_locked
507     (EVP_RAND_CTX *ctx, unsigned int strength, int prediction_resistance,
508      const unsigned char *pstr, size_t pstr_len, const OSSL_PARAM params[])
509 {
510     return ctx->meth->instantiate(ctx->algctx, strength, prediction_resistance,
511                                   pstr, pstr_len, params);
512 }
513
514 int EVP_RAND_instantiate(EVP_RAND_CTX *ctx, unsigned int strength,
515                          int prediction_resistance,
516                          const unsigned char *pstr, size_t pstr_len,
517                          const OSSL_PARAM params[])
518 {
519     int res;
520
521     if (!evp_rand_lock(ctx))
522         return 0;
523     res = evp_rand_instantiate_locked(ctx, strength, prediction_resistance,
524                                       pstr, pstr_len, params);
525     evp_rand_unlock(ctx);
526     return res;
527 }
528
529 static int evp_rand_uninstantiate_locked(EVP_RAND_CTX *ctx)
530 {
531     return ctx->meth->uninstantiate(ctx->algctx);
532 }
533
534 int EVP_RAND_uninstantiate(EVP_RAND_CTX *ctx)
535 {
536     int res;
537
538     if (!evp_rand_lock(ctx))
539         return 0;
540     res = evp_rand_uninstantiate_locked(ctx);
541     evp_rand_unlock(ctx);
542     return res;
543 }
544
545 static int evp_rand_generate_locked(EVP_RAND_CTX *ctx, unsigned char *out,
546                                     size_t outlen, unsigned int strength,
547                                     int prediction_resistance,
548                                     const unsigned char *addin,
549                                     size_t addin_len)
550 {
551     size_t chunk, max_request = 0;
552     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
553
554     params[0] = OSSL_PARAM_construct_size_t(OSSL_RAND_PARAM_MAX_REQUEST,
555                                             &max_request);
556     if (!evp_rand_get_ctx_params_locked(ctx, params)
557             || max_request == 0) {
558         ERR_raise(ERR_LIB_EVP, EVP_R_UNABLE_TO_GET_MAXIMUM_REQUEST_SIZE);
559         return 0;
560     }
561     for (; outlen > 0; outlen -= chunk, out += chunk) {
562         chunk = outlen > max_request ? max_request : outlen;
563         if (!ctx->meth->generate(ctx->algctx, out, chunk, strength,
564                                  prediction_resistance, addin, addin_len)) {
565             ERR_raise(ERR_LIB_EVP, EVP_R_GENERATE_ERROR);
566             return 0;
567         }
568         /*
569          * Prediction resistance is only relevant the first time around,
570          * subsequently, the DRBG has already been properly reseeded.
571          */
572         prediction_resistance = 0;
573     }
574     return 1;
575 }
576
577 int EVP_RAND_generate(EVP_RAND_CTX *ctx, unsigned char *out, size_t outlen,
578                       unsigned int strength, int prediction_resistance,
579                       const unsigned char *addin, size_t addin_len)
580 {
581     int res;
582
583     if (!evp_rand_lock(ctx))
584         return 0;
585     res = evp_rand_generate_locked(ctx, out, outlen, strength,
586                                    prediction_resistance, addin, addin_len);
587     evp_rand_unlock(ctx);
588     return res;
589 }
590
591 static int evp_rand_reseed_locked(EVP_RAND_CTX *ctx, int prediction_resistance,
592                                   const unsigned char *ent, size_t ent_len,
593                                   const unsigned char *addin, size_t addin_len)
594 {
595     if (ctx->meth->reseed != NULL)
596         return ctx->meth->reseed(ctx->algctx, prediction_resistance,
597                                  ent, ent_len, addin, addin_len);
598     return 1;
599 }
600
601 int EVP_RAND_reseed(EVP_RAND_CTX *ctx, int prediction_resistance,
602                     const unsigned char *ent, size_t ent_len,
603                     const unsigned char *addin, size_t addin_len)
604 {
605     int res;
606
607     if (!evp_rand_lock(ctx))
608         return 0;
609     res = evp_rand_reseed_locked(ctx, prediction_resistance,
610                                  ent, ent_len, addin, addin_len);
611     evp_rand_unlock(ctx);
612     return res;
613 }
614
615 static unsigned int evp_rand_strength_locked(EVP_RAND_CTX *ctx)
616 {
617     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
618     unsigned int strength = 0;
619
620     params[0] = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, &strength);
621     if (!evp_rand_get_ctx_params_locked(ctx, params))
622         return 0;
623     return strength;
624 }
625
626 unsigned int EVP_RAND_get_strength(EVP_RAND_CTX *ctx)
627 {
628     unsigned int res;
629
630     if (!evp_rand_lock(ctx))
631         return 0;
632     res = evp_rand_strength_locked(ctx);
633     evp_rand_unlock(ctx);
634     return res;
635 }
636
637 static int evp_rand_nonce_locked(EVP_RAND_CTX *ctx, unsigned char *out,
638                                  size_t outlen)
639 {
640     unsigned int str = evp_rand_strength_locked(ctx);
641
642     if (ctx->meth->nonce == NULL)
643         return 0;
644     if (ctx->meth->nonce(ctx->algctx, out, str, outlen, outlen))
645         return 1;
646     return evp_rand_generate_locked(ctx, out, outlen, str, 0, NULL, 0);
647 }
648
649 int EVP_RAND_nonce(EVP_RAND_CTX *ctx, unsigned char *out, size_t outlen)
650 {
651     int res;
652
653     if (!evp_rand_lock(ctx))
654         return 0;
655     res = evp_rand_nonce_locked(ctx, out, outlen);
656     evp_rand_unlock(ctx);
657     return res;
658 }
659
660 int EVP_RAND_get_state(EVP_RAND_CTX *ctx)
661 {
662     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
663     int state;
664
665     params[0] = OSSL_PARAM_construct_int(OSSL_RAND_PARAM_STATE, &state);
666     if (!EVP_RAND_CTX_get_params(ctx, params))
667         state = EVP_RAND_STATE_ERROR;
668     return state;
669 }
670
671 static int evp_rand_verify_zeroization_locked(EVP_RAND_CTX *ctx)
672 {
673     if (ctx->meth->verify_zeroization != NULL)
674         return ctx->meth->verify_zeroization(ctx->algctx);
675     return 0;
676 }
677
678 int EVP_RAND_verify_zeroization(EVP_RAND_CTX *ctx)
679 {
680     int res;
681
682     if (!evp_rand_lock(ctx))
683         return 0;
684     res = evp_rand_verify_zeroization_locked(ctx);
685     evp_rand_unlock(ctx);
686     return res;
687 }