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