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