/src/openssl/providers/implementations/rands/drbg_hash.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2011-2025 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 <assert.h> |
11 | | #include <stdlib.h> |
12 | | #include <string.h> |
13 | | #include <openssl/sha.h> |
14 | | #include <openssl/crypto.h> |
15 | | #include <openssl/err.h> |
16 | | #include <openssl/rand.h> |
17 | | #include <openssl/core_dispatch.h> |
18 | | #include <openssl/proverr.h> |
19 | | #include "internal/thread_once.h" |
20 | | #include "prov/providercommon.h" |
21 | | #include "prov/provider_ctx.h" |
22 | | #include "prov/provider_util.h" |
23 | | #include "prov/implementations.h" |
24 | | #include "drbg_local.h" |
25 | | #include "crypto/evp.h" |
26 | | #include "crypto/evp/evp_local.h" |
27 | | #include "internal/provider.h" |
28 | | |
29 | | static OSSL_FUNC_rand_newctx_fn drbg_hash_new_wrapper; |
30 | | static OSSL_FUNC_rand_freectx_fn drbg_hash_free; |
31 | | static OSSL_FUNC_rand_instantiate_fn drbg_hash_instantiate_wrapper; |
32 | | static OSSL_FUNC_rand_uninstantiate_fn drbg_hash_uninstantiate_wrapper; |
33 | | static OSSL_FUNC_rand_generate_fn drbg_hash_generate_wrapper; |
34 | | static OSSL_FUNC_rand_reseed_fn drbg_hash_reseed_wrapper; |
35 | | static OSSL_FUNC_rand_settable_ctx_params_fn drbg_hash_settable_ctx_params; |
36 | | static OSSL_FUNC_rand_set_ctx_params_fn drbg_hash_set_ctx_params; |
37 | | static OSSL_FUNC_rand_gettable_ctx_params_fn drbg_hash_gettable_ctx_params; |
38 | | static OSSL_FUNC_rand_get_ctx_params_fn drbg_hash_get_ctx_params; |
39 | | static OSSL_FUNC_rand_verify_zeroization_fn drbg_hash_verify_zeroization; |
40 | | |
41 | | static int drbg_hash_set_ctx_params_locked(void *vctx, const OSSL_PARAM params[]); |
42 | | |
43 | | /* 888 bits from SP800-90Ar1 10.1 table 2 */ |
44 | 0 | #define HASH_PRNG_MAX_SEEDLEN (888/8) |
45 | | |
46 | | /* 440 bits from SP800-90Ar1 10.1 table 2 */ |
47 | 0 | #define HASH_PRNG_SMALL_SEEDLEN (440/8) |
48 | | |
49 | | /* Determine what seedlen to use based on the block length */ |
50 | 0 | #define MAX_BLOCKLEN_USING_SMALL_SEEDLEN (256/8) |
51 | 0 | #define INBYTE_IGNORE ((unsigned char)0xFF) |
52 | | |
53 | | typedef struct rand_drbg_hash_st { |
54 | | PROV_DIGEST digest; |
55 | | EVP_MD_CTX *ctx; |
56 | | size_t blocklen; |
57 | | unsigned char V[HASH_PRNG_MAX_SEEDLEN]; |
58 | | unsigned char C[HASH_PRNG_MAX_SEEDLEN]; |
59 | | /* Temporary value storage: should always exceed max digest length */ |
60 | | unsigned char vtmp[HASH_PRNG_MAX_SEEDLEN]; |
61 | | } PROV_DRBG_HASH; |
62 | | |
63 | | /* |
64 | | * SP800-90Ar1 10.3.1 Derivation function using a Hash Function (Hash_df). |
65 | | * The input string used is composed of: |
66 | | * inbyte - An optional leading byte (ignore if equal to INBYTE_IGNORE) |
67 | | * in - input string 1 (A Non NULL value). |
68 | | * in2 - optional input string (Can be NULL). |
69 | | * in3 - optional input string (Can be NULL). |
70 | | * These are concatenated as part of the DigestUpdate process. |
71 | | */ |
72 | | static int hash_df(PROV_DRBG *drbg, unsigned char *out, |
73 | | const unsigned char inbyte, |
74 | | const unsigned char *in, size_t inlen, |
75 | | const unsigned char *in2, size_t in2len, |
76 | | const unsigned char *in3, size_t in3len) |
77 | 0 | { |
78 | 0 | PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data; |
79 | 0 | EVP_MD_CTX *ctx = hash->ctx; |
80 | 0 | unsigned char *vtmp = hash->vtmp; |
81 | | /* tmp = counter || num_bits_returned || [inbyte] */ |
82 | 0 | unsigned char tmp[1 + 4 + 1]; |
83 | 0 | int tmp_sz = 0; |
84 | 0 | size_t outlen = drbg->seedlen; |
85 | 0 | size_t num_bits_returned = outlen * 8; |
86 | | /* |
87 | | * No need to check outlen size here, as the standard only ever needs |
88 | | * seedlen bytes which is always less than the maximum permitted. |
89 | | */ |
90 | | |
91 | | /* (Step 3) counter = 1 (tmp[0] is the 8 bit counter) */ |
92 | 0 | tmp[tmp_sz++] = 1; |
93 | | /* tmp[1..4] is the fixed 32 bit no_of_bits_to_return */ |
94 | 0 | tmp[tmp_sz++] = (unsigned char)((num_bits_returned >> 24) & 0xff); |
95 | 0 | tmp[tmp_sz++] = (unsigned char)((num_bits_returned >> 16) & 0xff); |
96 | 0 | tmp[tmp_sz++] = (unsigned char)((num_bits_returned >> 8) & 0xff); |
97 | 0 | tmp[tmp_sz++] = (unsigned char)(num_bits_returned & 0xff); |
98 | | /* Tack the additional input byte onto the end of tmp if it exists */ |
99 | 0 | if (inbyte != INBYTE_IGNORE) |
100 | 0 | tmp[tmp_sz++] = inbyte; |
101 | | |
102 | | /* (Step 4) */ |
103 | 0 | for (;;) { |
104 | | /* |
105 | | * (Step 4.1) out = out || Hash(tmp || in || [in2] || [in3]) |
106 | | * (where tmp = counter || num_bits_returned || [inbyte]) |
107 | | */ |
108 | 0 | if (!(EVP_DigestInit_ex(ctx, ossl_prov_digest_md(&hash->digest), NULL) |
109 | 0 | && EVP_DigestUpdate(ctx, tmp, tmp_sz) |
110 | 0 | && EVP_DigestUpdate(ctx, in, inlen) |
111 | 0 | && (in2 == NULL || EVP_DigestUpdate(ctx, in2, in2len)) |
112 | 0 | && (in3 == NULL || EVP_DigestUpdate(ctx, in3, in3len)))) |
113 | 0 | return 0; |
114 | | |
115 | 0 | if (outlen < hash->blocklen) { |
116 | 0 | if (!EVP_DigestFinal(ctx, vtmp, NULL)) |
117 | 0 | return 0; |
118 | 0 | memcpy(out, vtmp, outlen); |
119 | 0 | OPENSSL_cleanse(vtmp, hash->blocklen); |
120 | 0 | break; |
121 | 0 | } else if (!EVP_DigestFinal(ctx, out, NULL)) { |
122 | 0 | return 0; |
123 | 0 | } |
124 | | |
125 | 0 | outlen -= hash->blocklen; |
126 | 0 | if (outlen == 0) |
127 | 0 | break; |
128 | | /* (Step 4.2) counter++ */ |
129 | 0 | tmp[0]++; |
130 | 0 | out += hash->blocklen; |
131 | 0 | } |
132 | 0 | return 1; |
133 | 0 | } |
134 | | |
135 | | /* Helper function that just passes 2 input parameters to hash_df() */ |
136 | | static int hash_df1(PROV_DRBG *drbg, unsigned char *out, |
137 | | const unsigned char in_byte, |
138 | | const unsigned char *in1, size_t in1len) |
139 | 0 | { |
140 | 0 | return hash_df(drbg, out, in_byte, in1, in1len, NULL, 0, NULL, 0); |
141 | 0 | } |
142 | | |
143 | | /* |
144 | | * Add 2 byte buffers together. The first elements in each buffer are the top |
145 | | * most bytes. The result is stored in the dst buffer. |
146 | | * The final carry is ignored i.e: dst = (dst + in) mod (2^seedlen_bits). |
147 | | * where dst size is drbg->seedlen, and inlen <= drbg->seedlen. |
148 | | */ |
149 | | static int add_bytes(PROV_DRBG *drbg, unsigned char *dst, |
150 | | unsigned char *in, size_t inlen) |
151 | 0 | { |
152 | 0 | size_t i; |
153 | 0 | int result; |
154 | 0 | const unsigned char *add; |
155 | 0 | unsigned char carry = 0, *d; |
156 | |
|
157 | 0 | assert(drbg->seedlen >= 1 && inlen >= 1 && inlen <= drbg->seedlen); |
158 | | |
159 | 0 | d = &dst[drbg->seedlen - 1]; |
160 | 0 | add = &in[inlen - 1]; |
161 | |
|
162 | 0 | for (i = inlen; i > 0; i--, d--, add--) { |
163 | 0 | result = *d + *add + carry; |
164 | 0 | carry = (unsigned char)(result >> 8); |
165 | 0 | *d = (unsigned char)(result & 0xff); |
166 | 0 | } |
167 | |
|
168 | 0 | if (carry != 0) { |
169 | | /* Add the carry to the top of the dst if inlen is not the same size */ |
170 | 0 | for (i = drbg->seedlen - inlen; i > 0; --i, d--) { |
171 | 0 | *d += 1; /* Carry can only be 1 */ |
172 | 0 | if (*d != 0) /* exit if carry doesn't propagate to the next byte */ |
173 | 0 | break; |
174 | 0 | } |
175 | 0 | } |
176 | 0 | return 1; |
177 | 0 | } |
178 | | |
179 | | /* V = (V + Hash(inbyte || V || [additional_input]) mod (2^seedlen) */ |
180 | | static int add_hash_to_v(PROV_DRBG *drbg, unsigned char inbyte, |
181 | | const unsigned char *adin, size_t adinlen) |
182 | 0 | { |
183 | 0 | PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data; |
184 | 0 | EVP_MD_CTX *ctx = hash->ctx; |
185 | |
|
186 | 0 | return EVP_DigestInit_ex(ctx, ossl_prov_digest_md(&hash->digest), NULL) |
187 | 0 | && EVP_DigestUpdate(ctx, &inbyte, 1) |
188 | 0 | && EVP_DigestUpdate(ctx, hash->V, drbg->seedlen) |
189 | 0 | && (adin == NULL || EVP_DigestUpdate(ctx, adin, adinlen)) |
190 | 0 | && EVP_DigestFinal(ctx, hash->vtmp, NULL) |
191 | 0 | && add_bytes(drbg, hash->V, hash->vtmp, hash->blocklen); |
192 | 0 | } |
193 | | |
194 | | /* |
195 | | * The Hashgen() as listed in SP800-90Ar1 10.1.1.4 Hash_DRBG_Generate_Process. |
196 | | * |
197 | | * drbg contains the current value of V. |
198 | | * outlen is the requested number of bytes. |
199 | | * out is a buffer to return the generated bits. |
200 | | * |
201 | | * The algorithm to generate the bits is: |
202 | | * data = V |
203 | | * w = NULL |
204 | | * for (i = 1 to m) { |
205 | | * W = W || Hash(data) |
206 | | * data = (data + 1) mod (2^seedlen) |
207 | | * } |
208 | | * out = Leftmost(W, outlen) |
209 | | * |
210 | | * Returns zero if an error occurs otherwise it returns 1. |
211 | | */ |
212 | | static int hash_gen(PROV_DRBG *drbg, unsigned char *out, size_t outlen) |
213 | 0 | { |
214 | 0 | PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data; |
215 | 0 | unsigned char one = 1; |
216 | |
|
217 | 0 | if (outlen == 0) |
218 | 0 | return 1; |
219 | 0 | memcpy(hash->vtmp, hash->V, drbg->seedlen); |
220 | 0 | for (;;) { |
221 | 0 | if (!EVP_DigestInit_ex(hash->ctx, ossl_prov_digest_md(&hash->digest), |
222 | 0 | NULL) |
223 | 0 | || !EVP_DigestUpdate(hash->ctx, hash->vtmp, drbg->seedlen)) |
224 | 0 | return 0; |
225 | | |
226 | 0 | if (outlen < hash->blocklen) { |
227 | 0 | if (!EVP_DigestFinal(hash->ctx, hash->vtmp, NULL)) |
228 | 0 | return 0; |
229 | 0 | memcpy(out, hash->vtmp, outlen); |
230 | 0 | return 1; |
231 | 0 | } else { |
232 | 0 | if (!EVP_DigestFinal(hash->ctx, out, NULL)) |
233 | 0 | return 0; |
234 | 0 | outlen -= hash->blocklen; |
235 | 0 | if (outlen == 0) |
236 | 0 | break; |
237 | 0 | out += hash->blocklen; |
238 | 0 | } |
239 | 0 | add_bytes(drbg, hash->vtmp, &one, 1); |
240 | 0 | } |
241 | 0 | return 1; |
242 | 0 | } |
243 | | |
244 | | /* |
245 | | * SP800-90Ar1 10.1.1.2 Hash_DRBG_Instantiate_Process: |
246 | | * |
247 | | * ent is entropy input obtained from a randomness source of length ent_len. |
248 | | * nonce is a string of bytes of length nonce_len. |
249 | | * pstr is a personalization string received from an application. May be NULL. |
250 | | * |
251 | | * Returns zero if an error occurs otherwise it returns 1. |
252 | | */ |
253 | | static int drbg_hash_instantiate(PROV_DRBG *drbg, |
254 | | const unsigned char *ent, size_t ent_len, |
255 | | const unsigned char *nonce, size_t nonce_len, |
256 | | const unsigned char *pstr, size_t pstr_len) |
257 | 0 | { |
258 | 0 | PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data; |
259 | |
|
260 | 0 | EVP_MD_CTX_free(hash->ctx); |
261 | 0 | hash->ctx = EVP_MD_CTX_new(); |
262 | | |
263 | | /* (Step 1-3) V = Hash_df(entropy||nonce||pers, seedlen) */ |
264 | 0 | return hash->ctx != NULL |
265 | 0 | && hash_df(drbg, hash->V, INBYTE_IGNORE, |
266 | 0 | ent, ent_len, nonce, nonce_len, pstr, pstr_len) |
267 | | /* (Step 4) C = Hash_df(0x00||V, seedlen) */ |
268 | 0 | && hash_df1(drbg, hash->C, 0x00, hash->V, drbg->seedlen); |
269 | 0 | } |
270 | | |
271 | | static int drbg_hash_instantiate_wrapper(void *vdrbg, unsigned int strength, |
272 | | int prediction_resistance, |
273 | | const unsigned char *pstr, |
274 | | size_t pstr_len, |
275 | | const OSSL_PARAM params[]) |
276 | 0 | { |
277 | 0 | PROV_DRBG *drbg = (PROV_DRBG *)vdrbg; |
278 | 0 | int ret = 0; |
279 | |
|
280 | 0 | if (drbg->lock != NULL && !CRYPTO_THREAD_write_lock(drbg->lock)) |
281 | 0 | return 0; |
282 | | |
283 | 0 | if (!ossl_prov_is_running() |
284 | 0 | || !drbg_hash_set_ctx_params_locked(drbg, params)) |
285 | 0 | goto err; |
286 | 0 | ret = ossl_prov_drbg_instantiate(drbg, strength, prediction_resistance, |
287 | 0 | pstr, pstr_len); |
288 | 0 | err: |
289 | 0 | if (drbg->lock != NULL) |
290 | 0 | CRYPTO_THREAD_unlock(drbg->lock); |
291 | 0 | return ret; |
292 | 0 | } |
293 | | |
294 | | /* |
295 | | * SP800-90Ar1 10.1.1.3 Hash_DRBG_Reseed_Process: |
296 | | * |
297 | | * ent is entropy input bytes obtained from a randomness source. |
298 | | * addin is additional input received from an application. May be NULL. |
299 | | * |
300 | | * Returns zero if an error occurs otherwise it returns 1. |
301 | | */ |
302 | | static int drbg_hash_reseed(PROV_DRBG *drbg, |
303 | | const unsigned char *ent, size_t ent_len, |
304 | | const unsigned char *adin, size_t adin_len) |
305 | 0 | { |
306 | 0 | PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data; |
307 | | |
308 | | /* (Step 1-2) V = Hash_df(0x01 || V || entropy_input || additional_input) */ |
309 | | /* V about to be updated so use C as output instead */ |
310 | 0 | if (!hash_df(drbg, hash->C, 0x01, hash->V, drbg->seedlen, ent, ent_len, |
311 | 0 | adin, adin_len)) |
312 | 0 | return 0; |
313 | 0 | memcpy(hash->V, hash->C, drbg->seedlen); |
314 | | /* (Step 4) C = Hash_df(0x00||V, seedlen) */ |
315 | 0 | return hash_df1(drbg, hash->C, 0x00, hash->V, drbg->seedlen); |
316 | 0 | } |
317 | | |
318 | | static int drbg_hash_reseed_wrapper(void *vdrbg, int prediction_resistance, |
319 | | const unsigned char *ent, size_t ent_len, |
320 | | const unsigned char *adin, size_t adin_len) |
321 | 0 | { |
322 | 0 | PROV_DRBG *drbg = (PROV_DRBG *)vdrbg; |
323 | |
|
324 | 0 | return ossl_prov_drbg_reseed(drbg, prediction_resistance, ent, ent_len, |
325 | 0 | adin, adin_len); |
326 | 0 | } |
327 | | |
328 | | /* |
329 | | * SP800-90Ar1 10.1.1.4 Hash_DRBG_Generate_Process: |
330 | | * |
331 | | * Generates pseudo random bytes using the drbg. |
332 | | * out is a buffer to fill with outlen bytes of pseudo random data. |
333 | | * addin is additional input received from an application. May be NULL. |
334 | | * |
335 | | * Returns zero if an error occurs otherwise it returns 1. |
336 | | */ |
337 | | static int drbg_hash_generate(PROV_DRBG *drbg, |
338 | | unsigned char *out, size_t outlen, |
339 | | const unsigned char *adin, size_t adin_len) |
340 | 0 | { |
341 | 0 | PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data; |
342 | 0 | unsigned char counter[4]; |
343 | 0 | int reseed_counter = drbg->generate_counter; |
344 | |
|
345 | 0 | counter[0] = (unsigned char)((reseed_counter >> 24) & 0xff); |
346 | 0 | counter[1] = (unsigned char)((reseed_counter >> 16) & 0xff); |
347 | 0 | counter[2] = (unsigned char)((reseed_counter >> 8) & 0xff); |
348 | 0 | counter[3] = (unsigned char)(reseed_counter & 0xff); |
349 | |
|
350 | 0 | return hash->ctx != NULL |
351 | 0 | && (adin == NULL |
352 | | /* (Step 2) if adin != NULL then V = V + Hash(0x02||V||adin) */ |
353 | 0 | || adin_len == 0 |
354 | 0 | || add_hash_to_v(drbg, 0x02, adin, adin_len)) |
355 | | /* (Step 3) Hashgen(outlen, V) */ |
356 | 0 | && hash_gen(drbg, out, outlen) |
357 | | /* (Step 4/5) H = V = (V + Hash(0x03||V) mod (2^seedlen_bits) */ |
358 | 0 | && add_hash_to_v(drbg, 0x03, NULL, 0) |
359 | | /* (Step 5) V = (V + H + C + reseed_counter) mod (2^seedlen_bits) */ |
360 | | /* V = (V + C) mod (2^seedlen_bits) */ |
361 | 0 | && add_bytes(drbg, hash->V, hash->C, drbg->seedlen) |
362 | | /* V = (V + reseed_counter) mod (2^seedlen_bits) */ |
363 | 0 | && add_bytes(drbg, hash->V, counter, 4); |
364 | 0 | } |
365 | | |
366 | | static int drbg_hash_generate_wrapper |
367 | | (void *vdrbg, unsigned char *out, size_t outlen, unsigned int strength, |
368 | | int prediction_resistance, const unsigned char *adin, size_t adin_len) |
369 | 0 | { |
370 | 0 | PROV_DRBG *drbg = (PROV_DRBG *)vdrbg; |
371 | |
|
372 | 0 | return ossl_prov_drbg_generate(drbg, out, outlen, strength, |
373 | 0 | prediction_resistance, adin, adin_len); |
374 | 0 | } |
375 | | |
376 | | static int drbg_hash_uninstantiate(PROV_DRBG *drbg) |
377 | 0 | { |
378 | 0 | PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data; |
379 | |
|
380 | 0 | OPENSSL_cleanse(hash->V, sizeof(hash->V)); |
381 | 0 | OPENSSL_cleanse(hash->C, sizeof(hash->C)); |
382 | 0 | OPENSSL_cleanse(hash->vtmp, sizeof(hash->vtmp)); |
383 | 0 | return ossl_prov_drbg_uninstantiate(drbg); |
384 | 0 | } |
385 | | |
386 | | static int drbg_hash_uninstantiate_wrapper(void *vdrbg) |
387 | 0 | { |
388 | 0 | PROV_DRBG *drbg = (PROV_DRBG *)vdrbg; |
389 | 0 | int ret; |
390 | |
|
391 | 0 | if (drbg->lock != NULL && !CRYPTO_THREAD_write_lock(drbg->lock)) |
392 | 0 | return 0; |
393 | | |
394 | 0 | ret = drbg_hash_uninstantiate(drbg); |
395 | |
|
396 | 0 | if (drbg->lock != NULL) |
397 | 0 | CRYPTO_THREAD_unlock(drbg->lock); |
398 | |
|
399 | 0 | return ret; |
400 | 0 | } |
401 | | |
402 | | static int drbg_hash_verify_zeroization(void *vdrbg) |
403 | 0 | { |
404 | 0 | PROV_DRBG *drbg = (PROV_DRBG *)vdrbg; |
405 | 0 | PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data; |
406 | 0 | int ret = 0; |
407 | |
|
408 | 0 | if (drbg->lock != NULL && !CRYPTO_THREAD_read_lock(drbg->lock)) |
409 | 0 | return 0; |
410 | | |
411 | 0 | PROV_DRBG_VERIFY_ZEROIZATION(hash->V); |
412 | 0 | PROV_DRBG_VERIFY_ZEROIZATION(hash->C); |
413 | 0 | PROV_DRBG_VERIFY_ZEROIZATION(hash->vtmp); |
414 | |
|
415 | 0 | ret = 1; |
416 | 0 | err: |
417 | 0 | if (drbg->lock != NULL) |
418 | 0 | CRYPTO_THREAD_unlock(drbg->lock); |
419 | 0 | return ret; |
420 | 0 | } |
421 | | |
422 | | static int drbg_hash_new(PROV_DRBG *ctx) |
423 | 0 | { |
424 | 0 | PROV_DRBG_HASH *hash; |
425 | |
|
426 | 0 | hash = OPENSSL_secure_zalloc(sizeof(*hash)); |
427 | 0 | if (hash == NULL) |
428 | 0 | return 0; |
429 | | |
430 | 0 | OSSL_FIPS_IND_INIT(ctx) |
431 | | |
432 | 0 | ctx->data = hash; |
433 | 0 | ctx->seedlen = HASH_PRNG_MAX_SEEDLEN; |
434 | 0 | ctx->max_entropylen = DRBG_MAX_LENGTH; |
435 | 0 | ctx->max_noncelen = DRBG_MAX_LENGTH; |
436 | 0 | ctx->max_perslen = DRBG_MAX_LENGTH; |
437 | 0 | ctx->max_adinlen = DRBG_MAX_LENGTH; |
438 | | |
439 | | /* Maximum number of bits per request = 2^19 = 2^16 bytes */ |
440 | 0 | ctx->max_request = 1 << 16; |
441 | 0 | return 1; |
442 | 0 | } |
443 | | |
444 | | static void *drbg_hash_new_wrapper(void *provctx, void *parent, |
445 | | const OSSL_DISPATCH *parent_dispatch) |
446 | 0 | { |
447 | 0 | return ossl_rand_drbg_new(provctx, parent, parent_dispatch, |
448 | 0 | &drbg_hash_new, &drbg_hash_free, |
449 | 0 | &drbg_hash_instantiate, &drbg_hash_uninstantiate, |
450 | 0 | &drbg_hash_reseed, &drbg_hash_generate); |
451 | 0 | } |
452 | | |
453 | | static void drbg_hash_free(void *vdrbg) |
454 | 0 | { |
455 | 0 | PROV_DRBG *drbg = (PROV_DRBG *)vdrbg; |
456 | 0 | PROV_DRBG_HASH *hash; |
457 | |
|
458 | 0 | if (drbg != NULL && (hash = (PROV_DRBG_HASH *)drbg->data) != NULL) { |
459 | 0 | EVP_MD_CTX_free(hash->ctx); |
460 | 0 | ossl_prov_digest_reset(&hash->digest); |
461 | 0 | OPENSSL_secure_clear_free(hash, sizeof(*hash)); |
462 | 0 | } |
463 | 0 | ossl_rand_drbg_free(drbg); |
464 | 0 | } |
465 | | |
466 | | static int drbg_hash_get_ctx_params(void *vdrbg, OSSL_PARAM params[]) |
467 | 0 | { |
468 | 0 | PROV_DRBG *drbg = (PROV_DRBG *)vdrbg; |
469 | 0 | PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)drbg->data; |
470 | 0 | const EVP_MD *md; |
471 | 0 | OSSL_PARAM *p; |
472 | 0 | int ret = 0, complete = 0; |
473 | |
|
474 | 0 | if (!ossl_drbg_get_ctx_params_no_lock(drbg, params, &complete)) |
475 | 0 | return 0; |
476 | | |
477 | 0 | if (complete) |
478 | 0 | return 1; |
479 | | |
480 | 0 | if (drbg->lock != NULL && !CRYPTO_THREAD_read_lock(drbg->lock)) |
481 | 0 | return 0; |
482 | | |
483 | 0 | p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_DIGEST); |
484 | 0 | if (p != NULL) { |
485 | 0 | md = ossl_prov_digest_md(&hash->digest); |
486 | 0 | if (md == NULL || !OSSL_PARAM_set_utf8_string(p, EVP_MD_get0_name(md))) |
487 | 0 | goto err; |
488 | 0 | } |
489 | | |
490 | 0 | ret = ossl_drbg_get_ctx_params(drbg, params); |
491 | 0 | err: |
492 | 0 | if (drbg->lock != NULL) |
493 | 0 | CRYPTO_THREAD_unlock(drbg->lock); |
494 | |
|
495 | 0 | return ret; |
496 | 0 | } |
497 | | |
498 | | static const OSSL_PARAM *drbg_hash_gettable_ctx_params(ossl_unused void *vctx, |
499 | | ossl_unused void *p_ctx) |
500 | 0 | { |
501 | 0 | static const OSSL_PARAM known_gettable_ctx_params[] = { |
502 | 0 | OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_DIGEST, NULL, 0), |
503 | 0 | OSSL_PARAM_DRBG_GETTABLE_CTX_COMMON, |
504 | 0 | OSSL_FIPS_IND_GETTABLE_CTX_PARAM() |
505 | 0 | OSSL_PARAM_END |
506 | 0 | }; |
507 | 0 | return known_gettable_ctx_params; |
508 | 0 | } |
509 | | |
510 | | static int drbg_fetch_digest_from_prov(const OSSL_PARAM params[], |
511 | | OSSL_LIB_CTX *libctx, |
512 | | EVP_MD **digest) |
513 | 0 | { |
514 | 0 | OSSL_PROVIDER *prov = NULL; |
515 | 0 | const OSSL_PARAM *p; |
516 | 0 | EVP_MD *md = NULL; |
517 | 0 | int ret = 0; |
518 | |
|
519 | 0 | if (digest == NULL) |
520 | 0 | return 0; |
521 | | |
522 | 0 | if ((p = OSSL_PARAM_locate_const(params, |
523 | 0 | OSSL_PROV_PARAM_CORE_PROV_NAME)) == NULL) |
524 | 0 | return 0; |
525 | 0 | if (p->data_type != OSSL_PARAM_UTF8_STRING) |
526 | 0 | return 0; |
527 | 0 | if ((prov = ossl_provider_find(libctx, (const char *)p->data, 1)) == NULL) |
528 | 0 | return 0; |
529 | | |
530 | 0 | p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST); |
531 | 0 | if (p == NULL) { |
532 | 0 | ret = 1; |
533 | 0 | goto done; |
534 | 0 | } |
535 | | |
536 | 0 | if (p->data_type != OSSL_PARAM_UTF8_STRING) |
537 | 0 | goto done; |
538 | | |
539 | 0 | md = evp_digest_fetch_from_prov(prov, (const char *)p->data, NULL); |
540 | 0 | if (md) { |
541 | 0 | EVP_MD_free(*digest); |
542 | 0 | *digest = md; |
543 | 0 | ret = 1; |
544 | 0 | } |
545 | |
|
546 | 0 | done: |
547 | 0 | ossl_provider_free(prov); |
548 | 0 | return ret; |
549 | 0 | } |
550 | | |
551 | | static int drbg_hash_set_ctx_params_locked(void *vctx, const OSSL_PARAM params[]) |
552 | 0 | { |
553 | 0 | PROV_DRBG *ctx = (PROV_DRBG *)vctx; |
554 | 0 | PROV_DRBG_HASH *hash = (PROV_DRBG_HASH *)ctx->data; |
555 | 0 | OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx); |
556 | 0 | EVP_MD *prov_md = NULL; |
557 | 0 | const EVP_MD *md; |
558 | 0 | int md_size; |
559 | |
|
560 | 0 | if (!OSSL_FIPS_IND_SET_CTX_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, params, |
561 | 0 | OSSL_DRBG_PARAM_FIPS_DIGEST_CHECK)) |
562 | 0 | return 0; |
563 | | |
564 | | /* try to fetch digest from provider */ |
565 | 0 | (void)ERR_set_mark(); |
566 | 0 | if (!drbg_fetch_digest_from_prov(params, libctx, &prov_md)) { |
567 | 0 | (void)ERR_pop_to_mark(); |
568 | | /* fall back to full implementation search */ |
569 | 0 | if (!ossl_prov_digest_load_from_params(&hash->digest, params, libctx)) |
570 | 0 | return 0; |
571 | 0 | } else { |
572 | 0 | (void)ERR_clear_last_mark(); |
573 | 0 | if (prov_md) |
574 | 0 | ossl_prov_digest_set_md(&hash->digest, prov_md); |
575 | 0 | } |
576 | | |
577 | 0 | md = ossl_prov_digest_md(&hash->digest); |
578 | 0 | if (md != NULL) { |
579 | 0 | if (!ossl_drbg_verify_digest(ctx, libctx, md)) |
580 | 0 | return 0; /* Error already raised for us */ |
581 | | |
582 | | /* These are taken from SP 800-90 10.1 Table 2 */ |
583 | 0 | md_size = EVP_MD_get_size(md); |
584 | 0 | if (md_size <= 0) |
585 | 0 | return 0; |
586 | 0 | hash->blocklen = md_size; |
587 | | /* See SP800-57 Part1 Rev4 5.6.1 Table 3 */ |
588 | 0 | ctx->strength = 64 * (hash->blocklen >> 3); |
589 | 0 | if (ctx->strength > 256) |
590 | 0 | ctx->strength = 256; |
591 | 0 | if (hash->blocklen > MAX_BLOCKLEN_USING_SMALL_SEEDLEN) |
592 | 0 | ctx->seedlen = HASH_PRNG_MAX_SEEDLEN; |
593 | 0 | else |
594 | 0 | ctx->seedlen = HASH_PRNG_SMALL_SEEDLEN; |
595 | |
|
596 | 0 | ctx->min_entropylen = ctx->strength / 8; |
597 | 0 | ctx->min_noncelen = ctx->min_entropylen / 2; |
598 | 0 | } |
599 | | |
600 | 0 | return ossl_drbg_set_ctx_params(ctx, params); |
601 | 0 | } |
602 | | |
603 | | static int drbg_hash_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
604 | 0 | { |
605 | 0 | PROV_DRBG *drbg = (PROV_DRBG *)vctx; |
606 | 0 | int ret; |
607 | |
|
608 | 0 | if (drbg->lock != NULL && !CRYPTO_THREAD_write_lock(drbg->lock)) |
609 | 0 | return 0; |
610 | | |
611 | 0 | ret = drbg_hash_set_ctx_params_locked(vctx, params); |
612 | |
|
613 | 0 | if (drbg->lock != NULL) |
614 | 0 | CRYPTO_THREAD_unlock(drbg->lock); |
615 | |
|
616 | 0 | return ret; |
617 | 0 | } |
618 | | |
619 | | static const OSSL_PARAM *drbg_hash_settable_ctx_params(ossl_unused void *vctx, |
620 | | ossl_unused void *p_ctx) |
621 | 0 | { |
622 | 0 | static const OSSL_PARAM known_settable_ctx_params[] = { |
623 | 0 | OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_PROPERTIES, NULL, 0), |
624 | 0 | OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_DIGEST, NULL, 0), |
625 | 0 | OSSL_PARAM_DRBG_SETTABLE_CTX_COMMON, |
626 | 0 | OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_DRBG_PARAM_FIPS_DIGEST_CHECK) |
627 | 0 | OSSL_PARAM_END |
628 | 0 | }; |
629 | 0 | return known_settable_ctx_params; |
630 | 0 | } |
631 | | |
632 | | const OSSL_DISPATCH ossl_drbg_hash_functions[] = { |
633 | | { OSSL_FUNC_RAND_NEWCTX, (void(*)(void))drbg_hash_new_wrapper }, |
634 | | { OSSL_FUNC_RAND_FREECTX, (void(*)(void))drbg_hash_free }, |
635 | | { OSSL_FUNC_RAND_INSTANTIATE, |
636 | | (void(*)(void))drbg_hash_instantiate_wrapper }, |
637 | | { OSSL_FUNC_RAND_UNINSTANTIATE, |
638 | | (void(*)(void))drbg_hash_uninstantiate_wrapper }, |
639 | | { OSSL_FUNC_RAND_GENERATE, (void(*)(void))drbg_hash_generate_wrapper }, |
640 | | { OSSL_FUNC_RAND_RESEED, (void(*)(void))drbg_hash_reseed_wrapper }, |
641 | | { OSSL_FUNC_RAND_ENABLE_LOCKING, (void(*)(void))ossl_drbg_enable_locking }, |
642 | | { OSSL_FUNC_RAND_LOCK, (void(*)(void))ossl_drbg_lock }, |
643 | | { OSSL_FUNC_RAND_UNLOCK, (void(*)(void))ossl_drbg_unlock }, |
644 | | { OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS, |
645 | | (void(*)(void))drbg_hash_settable_ctx_params }, |
646 | | { OSSL_FUNC_RAND_SET_CTX_PARAMS, (void(*)(void))drbg_hash_set_ctx_params }, |
647 | | { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS, |
648 | | (void(*)(void))drbg_hash_gettable_ctx_params }, |
649 | | { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))drbg_hash_get_ctx_params }, |
650 | | { OSSL_FUNC_RAND_VERIFY_ZEROIZATION, |
651 | | (void(*)(void))drbg_hash_verify_zeroization }, |
652 | | { OSSL_FUNC_RAND_GET_SEED, (void(*)(void))ossl_drbg_get_seed }, |
653 | | { OSSL_FUNC_RAND_CLEAR_SEED, (void(*)(void))ossl_drbg_clear_seed }, |
654 | | OSSL_DISPATCH_END |
655 | | }; |