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