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