Rename all getters to use get/get0 in name
[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_free);
493 }
494
495 int EVP_RAND_names_do_all(const EVP_RAND *rand,
496                           void (*fn)(const char *name, void *data),
497                           void *data)
498 {
499     if (rand->prov != NULL)
500         return evp_names_do_all(rand->prov, rand->name_id, fn, data);
501
502     return 1;
503 }
504
505 static int evp_rand_instantiate_locked
506     (EVP_RAND_CTX *ctx, unsigned int strength, int prediction_resistance,
507      const unsigned char *pstr, size_t pstr_len, const OSSL_PARAM params[])
508 {
509     return ctx->meth->instantiate(ctx->algctx, strength, prediction_resistance,
510                                   pstr, pstr_len, params);
511 }
512
513 int EVP_RAND_instantiate(EVP_RAND_CTX *ctx, unsigned int strength,
514                          int prediction_resistance,
515                          const unsigned char *pstr, size_t pstr_len,
516                          const OSSL_PARAM params[])
517 {
518     int res;
519
520     if (!evp_rand_lock(ctx))
521         return 0;
522     res = evp_rand_instantiate_locked(ctx, strength, prediction_resistance,
523                                       pstr, pstr_len, params);
524     evp_rand_unlock(ctx);
525     return res;
526 }
527
528 static int evp_rand_uninstantiate_locked(EVP_RAND_CTX *ctx)
529 {
530     return ctx->meth->uninstantiate(ctx->algctx);
531 }
532
533 int EVP_RAND_uninstantiate(EVP_RAND_CTX *ctx)
534 {
535     int res;
536
537     if (!evp_rand_lock(ctx))
538         return 0;
539     res = evp_rand_uninstantiate_locked(ctx);
540     evp_rand_unlock(ctx);
541     return res;
542 }
543
544 static int evp_rand_generate_locked(EVP_RAND_CTX *ctx, unsigned char *out,
545                                     size_t outlen, unsigned int strength,
546                                     int prediction_resistance,
547                                     const unsigned char *addin,
548                                     size_t addin_len)
549 {
550     size_t chunk, max_request = 0;
551     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
552
553     params[0] = OSSL_PARAM_construct_size_t(OSSL_RAND_PARAM_MAX_REQUEST,
554                                             &max_request);
555     if (!evp_rand_get_ctx_params_locked(ctx, params)
556             || max_request == 0) {
557         ERR_raise(ERR_LIB_EVP, EVP_R_UNABLE_TO_GET_MAXIMUM_REQUEST_SIZE);
558         return 0;
559     }
560     for (; outlen > 0; outlen -= chunk, out += chunk) {
561         chunk = outlen > max_request ? max_request : outlen;
562         if (!ctx->meth->generate(ctx->algctx, out, chunk, strength,
563                                  prediction_resistance, addin, addin_len)) {
564             ERR_raise(ERR_LIB_EVP, EVP_R_GENERATE_ERROR);
565             return 0;
566         }
567         /*
568          * Prediction resistance is only relevant the first time around,
569          * subsequently, the DRBG has already been properly reseeded.
570          */
571         prediction_resistance = 0;
572     }
573     return 1;
574 }
575
576 int EVP_RAND_generate(EVP_RAND_CTX *ctx, unsigned char *out, size_t outlen,
577                       unsigned int strength, int prediction_resistance,
578                       const unsigned char *addin, size_t addin_len)
579 {
580     int res;
581
582     if (!evp_rand_lock(ctx))
583         return 0;
584     res = evp_rand_generate_locked(ctx, out, outlen, strength,
585                                    prediction_resistance, addin, addin_len);
586     evp_rand_unlock(ctx);
587     return res;
588 }
589
590 static int evp_rand_reseed_locked(EVP_RAND_CTX *ctx, int prediction_resistance,
591                                   const unsigned char *ent, size_t ent_len,
592                                   const unsigned char *addin, size_t addin_len)
593 {
594     if (ctx->meth->reseed != NULL)
595         return ctx->meth->reseed(ctx->algctx, prediction_resistance,
596                                  ent, ent_len, addin, addin_len);
597     return 1;
598 }
599
600 int EVP_RAND_reseed(EVP_RAND_CTX *ctx, int prediction_resistance,
601                     const unsigned char *ent, size_t ent_len,
602                     const unsigned char *addin, size_t addin_len)
603 {
604     int res;
605
606     if (!evp_rand_lock(ctx))
607         return 0;
608     res = evp_rand_reseed_locked(ctx, prediction_resistance,
609                                  ent, ent_len, addin, addin_len);
610     evp_rand_unlock(ctx);
611     return res;
612 }
613
614 static unsigned int evp_rand_strength_locked(EVP_RAND_CTX *ctx)
615 {
616     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
617     unsigned int strength = 0;
618
619     params[0] = OSSL_PARAM_construct_uint(OSSL_RAND_PARAM_STRENGTH, &strength);
620     if (!evp_rand_get_ctx_params_locked(ctx, params))
621         return 0;
622     return strength;
623 }
624
625 unsigned int EVP_RAND_get_strength(EVP_RAND_CTX *ctx)
626 {
627     unsigned int res;
628
629     if (!evp_rand_lock(ctx))
630         return 0;
631     res = evp_rand_strength_locked(ctx);
632     evp_rand_unlock(ctx);
633     return res;
634 }
635
636 static int evp_rand_nonce_locked(EVP_RAND_CTX *ctx, unsigned char *out,
637                                  size_t outlen)
638 {
639     unsigned int str = evp_rand_strength_locked(ctx);
640
641     if (ctx->meth->nonce == NULL)
642         return 0;
643     if (ctx->meth->nonce(ctx->algctx, out, str, outlen, outlen))
644         return 1;
645     return evp_rand_generate_locked(ctx, out, outlen, str, 0, NULL, 0);
646 }
647
648 int EVP_RAND_nonce(EVP_RAND_CTX *ctx, unsigned char *out, size_t outlen)
649 {
650     int res;
651
652     if (!evp_rand_lock(ctx))
653         return 0;
654     res = evp_rand_nonce_locked(ctx, out, outlen);
655     evp_rand_unlock(ctx);
656     return res;
657 }
658
659 int EVP_RAND_get_state(EVP_RAND_CTX *ctx)
660 {
661     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
662     int state;
663
664     params[0] = OSSL_PARAM_construct_int(OSSL_RAND_PARAM_STATE, &state);
665     if (!EVP_RAND_CTX_get_params(ctx, params))
666         state = EVP_RAND_STATE_ERROR;
667     return state;
668 }
669
670 static int evp_rand_verify_zeroization_locked(EVP_RAND_CTX *ctx)
671 {
672     if (ctx->meth->verify_zeroization != NULL)
673         return ctx->meth->verify_zeroization(ctx->algctx);
674     return 0;
675 }
676
677 int EVP_RAND_verify_zeroization(EVP_RAND_CTX *ctx)
678 {
679     int res;
680
681     if (!evp_rand_lock(ctx))
682         return 0;
683     res = evp_rand_verify_zeroization_locked(ctx);
684     evp_rand_unlock(ctx);
685     return res;
686 }