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