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