/src/openssl/providers/implementations/rands/drbg_hmac.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 <stdlib.h>  | 
11  |  | #include <string.h>  | 
12  |  | #include <openssl/crypto.h>  | 
13  |  | #include <openssl/err.h>  | 
14  |  | #include <openssl/rand.h>  | 
15  |  | #include <openssl/proverr.h>  | 
16  |  | #include "internal/cryptlib.h"  | 
17  |  | #include "internal/thread_once.h"  | 
18  |  | #include "prov/providercommon.h"  | 
19  |  | #include "prov/implementations.h"  | 
20  |  | #include "prov/provider_ctx.h"  | 
21  |  | #include "prov/hmac_drbg.h"  | 
22  |  | #include "prov/drbg.h"  | 
23  |  | #include "crypto/evp.h"  | 
24  |  | #include "crypto/evp/evp_local.h"  | 
25  |  | #include "internal/provider.h"  | 
26  |  |  | 
27  |  | #define drbg_hmac_get_ctx_params_st  drbg_get_ctx_params_st  | 
28  |  | #define drbg_hmac_set_ctx_params_st  drbg_set_ctx_params_st  | 
29  |  |  | 
30  |  | #include "providers/implementations/rands/drbg_hmac.inc"  | 
31  |  |  | 
32  |  | static OSSL_FUNC_rand_newctx_fn drbg_hmac_new_wrapper;  | 
33  |  | static OSSL_FUNC_rand_freectx_fn drbg_hmac_free;  | 
34  |  | static OSSL_FUNC_rand_instantiate_fn drbg_hmac_instantiate_wrapper;  | 
35  |  | static OSSL_FUNC_rand_uninstantiate_fn drbg_hmac_uninstantiate_wrapper;  | 
36  |  | static OSSL_FUNC_rand_generate_fn drbg_hmac_generate_wrapper;  | 
37  |  | static OSSL_FUNC_rand_reseed_fn drbg_hmac_reseed_wrapper;  | 
38  |  | static OSSL_FUNC_rand_settable_ctx_params_fn drbg_hmac_settable_ctx_params;  | 
39  |  | static OSSL_FUNC_rand_set_ctx_params_fn drbg_hmac_set_ctx_params;  | 
40  |  | static OSSL_FUNC_rand_gettable_ctx_params_fn drbg_hmac_gettable_ctx_params;  | 
41  |  | static OSSL_FUNC_rand_get_ctx_params_fn drbg_hmac_get_ctx_params;  | 
42  |  | static OSSL_FUNC_rand_verify_zeroization_fn drbg_hmac_verify_zeroization;  | 
43  |  |  | 
44  |  | static int drbg_hmac_set_ctx_params_locked(PROV_DRBG *drbg,  | 
45  |  |                                            const struct drbg_set_ctx_params_st *p);  | 
46  |  |  | 
47  |  | /*  | 
48  |  |  * Called twice by SP800-90Ar1 10.1.2.2 HMAC_DRBG_Update_Process.  | 
49  |  |  *  | 
50  |  |  * hmac is an object that holds the input/output Key and Value (K and V).  | 
51  |  |  * inbyte is 0x00 on the first call and 0x01 on the second call.  | 
52  |  |  * in1, in2, in3 are optional inputs that can be NULL.  | 
53  |  |  * in1len, in2len, in3len are the lengths of the input buffers.  | 
54  |  |  *  | 
55  |  |  * The returned K,V is:  | 
56  |  |  *   hmac->K = HMAC(hmac->K, hmac->V || inbyte || [in1] || [in2] || [in3])  | 
57  |  |  *   hmac->V = HMAC(hmac->K, hmac->V)  | 
58  |  |  *  | 
59  |  |  * Returns zero if an error occurs otherwise it returns 1.  | 
60  |  |  */  | 
61  |  | static int do_hmac(PROV_DRBG_HMAC *hmac, unsigned char inbyte,  | 
62  |  |                    const unsigned char *in1, size_t in1len,  | 
63  |  |                    const unsigned char *in2, size_t in2len,  | 
64  |  |                    const unsigned char *in3, size_t in3len)  | 
65  | 0  | { | 
66  | 0  |     EVP_MAC_CTX *ctx = hmac->ctx;  | 
67  |  | 
  | 
68  | 0  |     if (!EVP_MAC_init(ctx, hmac->K, hmac->blocklen, NULL)  | 
69  |  |             /* K = HMAC(K, V || inbyte || [in1] || [in2] || [in3]) */  | 
70  | 0  |             || !EVP_MAC_update(ctx, hmac->V, hmac->blocklen)  | 
71  | 0  |             || !EVP_MAC_update(ctx, &inbyte, 1)  | 
72  | 0  |             || !(in1 == NULL || in1len == 0 || EVP_MAC_update(ctx, in1, in1len))  | 
73  | 0  |             || !(in2 == NULL || in2len == 0 || EVP_MAC_update(ctx, in2, in2len))  | 
74  | 0  |             || !(in3 == NULL || in3len == 0 || EVP_MAC_update(ctx, in3, in3len))  | 
75  | 0  |             || !EVP_MAC_final(ctx, hmac->K, NULL, sizeof(hmac->K)))  | 
76  | 0  |         return 0;  | 
77  |  |  | 
78  |  |    /* V = HMAC(K, V) */  | 
79  | 0  |     return EVP_MAC_init(ctx, hmac->K, hmac->blocklen, NULL)  | 
80  | 0  |            && EVP_MAC_update(ctx, hmac->V, hmac->blocklen)  | 
81  | 0  |            && EVP_MAC_final(ctx, hmac->V, NULL, sizeof(hmac->V));  | 
82  | 0  | }  | 
83  |  |  | 
84  |  | /*  | 
85  |  |  * SP800-90Ar1 10.1.2.2 HMAC_DRBG_Update_Process  | 
86  |  |  *  | 
87  |  |  *  | 
88  |  |  * Updates the drbg objects Key(K) and Value(V) using the following algorithm:  | 
89  |  |  *   K,V = do_hmac(hmac, 0, in1, in2, in3)  | 
90  |  |  *   if (any input is not NULL)  | 
91  |  |  *     K,V = do_hmac(hmac, 1, in1, in2, in3)  | 
92  |  |  *  | 
93  |  |  * where in1, in2, in3 are optional input buffers that can be NULL.  | 
94  |  |  *       in1len, in2len, in3len are the lengths of the input buffers.  | 
95  |  |  *  | 
96  |  |  * Returns zero if an error occurs otherwise it returns 1.  | 
97  |  |  */  | 
98  |  | static int drbg_hmac_update(PROV_DRBG_HMAC *hmac,  | 
99  |  |                             const unsigned char *in1, size_t in1len,  | 
100  |  |                             const unsigned char *in2, size_t in2len,  | 
101  |  |                             const unsigned char *in3, size_t in3len)  | 
102  | 0  | { | 
103  |  |     /* (Steps 1-2) K = HMAC(K, V||0x00||provided_data). V = HMAC(K,V) */  | 
104  | 0  |     if (!do_hmac(hmac, 0x00, in1, in1len, in2, in2len, in3, in3len))  | 
105  | 0  |         return 0;  | 
106  |  |     /* (Step 3) If provided_data == NULL then return (K,V) */  | 
107  | 0  |     if (in1len == 0 && in2len == 0 && in3len == 0)  | 
108  | 0  |         return 1;  | 
109  |  |     /* (Steps 4-5) K = HMAC(K, V||0x01||provided_data). V = HMAC(K,V) */  | 
110  | 0  |     return do_hmac(hmac, 0x01, in1, in1len, in2, in2len, in3, in3len);  | 
111  | 0  | }  | 
112  |  |  | 
113  |  | /*  | 
114  |  |  * SP800-90Ar1 10.1.2.3 HMAC_DRBG_Instantiate_Process:  | 
115  |  |  *  | 
116  |  |  * This sets the drbg Key (K) to all zeros, and Value (V) to all 1's.  | 
117  |  |  * and then calls (K,V) = drbg_hmac_update() with input parameters:  | 
118  |  |  *   ent = entropy data (Can be NULL) of length ent_len.  | 
119  |  |  *   nonce = nonce data (Can be NULL) of length nonce_len.  | 
120  |  |  *   pstr = personalization data (Can be NULL) of length pstr_len.  | 
121  |  |  *  | 
122  |  |  * Returns zero if an error occurs otherwise it returns 1.  | 
123  |  |  */  | 
124  |  | int ossl_drbg_hmac_init(PROV_DRBG_HMAC *hmac,  | 
125  |  |                         const unsigned char *ent, size_t ent_len,  | 
126  |  |                         const unsigned char *nonce, size_t nonce_len,  | 
127  |  |                         const unsigned char *pstr, size_t pstr_len)  | 
128  | 0  | { | 
129  | 0  |     if (hmac->ctx == NULL) { | 
130  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MAC);  | 
131  | 0  |         return 0;  | 
132  | 0  |     }  | 
133  |  |  | 
134  |  |     /* (Step 2) Key = 0x00 00...00 */  | 
135  | 0  |     memset(hmac->K, 0x00, hmac->blocklen);  | 
136  |  |     /* (Step 3) V = 0x01 01...01 */  | 
137  | 0  |     memset(hmac->V, 0x01, hmac->blocklen);  | 
138  |  |     /* (Step 4) (K,V) = HMAC_DRBG_Update(entropy||nonce||pers string, K, V) */  | 
139  | 0  |     return drbg_hmac_update(hmac, ent, ent_len, nonce, nonce_len, pstr,  | 
140  | 0  |                             pstr_len);  | 
141  | 0  | }  | 
142  |  | static int drbg_hmac_instantiate(PROV_DRBG *drbg,  | 
143  |  |                                  const unsigned char *ent, size_t ent_len,  | 
144  |  |                                  const unsigned char *nonce, size_t nonce_len,  | 
145  |  |                                  const unsigned char *pstr, size_t pstr_len)  | 
146  | 0  | { | 
147  | 0  |     return ossl_drbg_hmac_init((PROV_DRBG_HMAC *)drbg->data, ent, ent_len,  | 
148  | 0  |                                nonce, nonce_len, pstr, pstr_len);  | 
149  | 0  | }  | 
150  |  |  | 
151  |  | static int drbg_hmac_instantiate_wrapper(void *vdrbg, unsigned int strength,  | 
152  |  |                                          int prediction_resistance,  | 
153  |  |                                          const unsigned char *pstr,  | 
154  |  |                                          size_t pstr_len,  | 
155  |  |                                          const OSSL_PARAM params[])  | 
156  | 0  | { | 
157  | 0  |     PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;  | 
158  | 0  |     struct drbg_set_ctx_params_st p;  | 
159  | 0  |     int ret = 0;  | 
160  |  | 
  | 
161  | 0  |     if (drbg == NULL || !drbg_hmac_set_ctx_params_decoder(params, &p))  | 
162  | 0  |         return 0;  | 
163  |  |  | 
164  | 0  |     if (drbg->lock != NULL && !CRYPTO_THREAD_write_lock(drbg->lock))  | 
165  | 0  |         return 0;  | 
166  |  |  | 
167  | 0  |     if (!ossl_prov_is_running()  | 
168  | 0  |             || !drbg_hmac_set_ctx_params_locked(drbg, &p))  | 
169  | 0  |         goto err;  | 
170  | 0  |     ret = ossl_prov_drbg_instantiate(drbg, strength, prediction_resistance,  | 
171  | 0  |                                      pstr, pstr_len);  | 
172  | 0  |  err:  | 
173  | 0  |     if (drbg->lock != NULL)  | 
174  | 0  |         CRYPTO_THREAD_unlock(drbg->lock);  | 
175  | 0  |     return ret;  | 
176  | 0  | }  | 
177  |  |  | 
178  |  | /*  | 
179  |  |  * SP800-90Ar1 10.1.2.4 HMAC_DRBG_Reseed_Process:  | 
180  |  |  *  | 
181  |  |  * Reseeds the drbg's Key (K) and Value (V) by calling  | 
182  |  |  * (K,V) = drbg_hmac_update() with the following input parameters:  | 
183  |  |  *   ent = entropy input data (Can be NULL) of length ent_len.  | 
184  |  |  *   adin = additional input data (Can be NULL) of length adin_len.  | 
185  |  |  *  | 
186  |  |  * Returns zero if an error occurs otherwise it returns 1.  | 
187  |  |  */  | 
188  |  | static int drbg_hmac_reseed(PROV_DRBG *drbg,  | 
189  |  |                             const unsigned char *ent, size_t ent_len,  | 
190  |  |                             const unsigned char *adin, size_t adin_len)  | 
191  | 0  | { | 
192  | 0  |     PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)drbg->data;  | 
193  |  |  | 
194  |  |     /* (Step 2) (K,V) = HMAC_DRBG_Update(entropy||additional_input, K, V) */  | 
195  | 0  |     return drbg_hmac_update(hmac, ent, ent_len, adin, adin_len, NULL, 0);  | 
196  | 0  | }  | 
197  |  |  | 
198  |  | static int drbg_hmac_reseed_wrapper(void *vdrbg, int prediction_resistance,  | 
199  |  |                                     const unsigned char *ent, size_t ent_len,  | 
200  |  |                                     const unsigned char *adin, size_t adin_len)  | 
201  | 0  | { | 
202  | 0  |     PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;  | 
203  |  | 
  | 
204  | 0  |     return ossl_prov_drbg_reseed(drbg, prediction_resistance, ent, ent_len,  | 
205  | 0  |                                  adin, adin_len);  | 
206  | 0  | }  | 
207  |  |  | 
208  |  | /*  | 
209  |  |  * SP800-90Ar1 10.1.2.5 HMAC_DRBG_Generate_Process:  | 
210  |  |  *  | 
211  |  |  * Generates pseudo random bytes and updates the internal K,V for the drbg.  | 
212  |  |  * out is a buffer to fill with outlen bytes of pseudo random data.  | 
213  |  |  * adin is an additional_input string of size adin_len that may be NULL.  | 
214  |  |  *  | 
215  |  |  * Returns zero if an error occurs otherwise it returns 1.  | 
216  |  |  */  | 
217  |  | int ossl_drbg_hmac_generate(PROV_DRBG_HMAC *hmac,  | 
218  |  |                             unsigned char *out, size_t outlen,  | 
219  |  |                             const unsigned char *adin, size_t adin_len)  | 
220  | 0  | { | 
221  | 0  |     EVP_MAC_CTX *ctx = hmac->ctx;  | 
222  | 0  |     const unsigned char *temp = hmac->V;  | 
223  |  |  | 
224  |  |     /* (Step 2) if adin != NULL then (K,V) = HMAC_DRBG_Update(adin, K, V) */  | 
225  | 0  |     if (adin != NULL  | 
226  | 0  |             && adin_len > 0  | 
227  | 0  |             && !drbg_hmac_update(hmac, adin, adin_len, NULL, 0, NULL, 0))  | 
228  | 0  |         return 0;  | 
229  |  |  | 
230  |  |     /*  | 
231  |  |      * (Steps 3-5) temp = NULL  | 
232  |  |      *             while (len(temp) < outlen) { | 
233  |  |      *                 V = HMAC(K, V)  | 
234  |  |      *                 temp = temp || V  | 
235  |  |      *             }  | 
236  |  |      */  | 
237  | 0  |     for (;;) { | 
238  | 0  |         if (!EVP_MAC_init(ctx, hmac->K, hmac->blocklen, NULL)  | 
239  | 0  |             || !EVP_MAC_update(ctx, temp, hmac->blocklen))  | 
240  | 0  |             return 0;  | 
241  |  |  | 
242  | 0  |         if (outlen > hmac->blocklen) { | 
243  | 0  |             if (!EVP_MAC_final(ctx, out, NULL, outlen))  | 
244  | 0  |                 return 0;  | 
245  | 0  |             temp = out;  | 
246  | 0  |         } else { | 
247  | 0  |             if (!EVP_MAC_final(ctx, hmac->V, NULL, sizeof(hmac->V)))  | 
248  | 0  |                 return 0;  | 
249  | 0  |             memcpy(out, hmac->V, outlen);  | 
250  | 0  |             break;  | 
251  | 0  |         }  | 
252  | 0  |         out += hmac->blocklen;  | 
253  | 0  |         outlen -= hmac->blocklen;  | 
254  | 0  |     }  | 
255  |  |     /* (Step 6) (K,V) = HMAC_DRBG_Update(adin, K, V) */  | 
256  | 0  |     if (!drbg_hmac_update(hmac, adin, adin_len, NULL, 0, NULL, 0))  | 
257  | 0  |         return 0;  | 
258  |  |  | 
259  | 0  |     return 1;  | 
260  | 0  | }  | 
261  |  |  | 
262  |  | static int drbg_hmac_generate(PROV_DRBG *drbg,  | 
263  |  |                               unsigned char *out, size_t outlen,  | 
264  |  |                               const unsigned char *adin, size_t adin_len)  | 
265  | 0  | { | 
266  | 0  |     return ossl_drbg_hmac_generate((PROV_DRBG_HMAC *)drbg->data, out, outlen,  | 
267  | 0  |                                     adin, adin_len);  | 
268  | 0  | }  | 
269  |  |  | 
270  |  | static int drbg_hmac_generate_wrapper(void *vdrbg,  | 
271  |  |      unsigned char *out, size_t outlen, unsigned int strength,  | 
272  |  |      int prediction_resistance, const unsigned char *adin, size_t adin_len)  | 
273  | 0  | { | 
274  | 0  |     PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;  | 
275  |  | 
  | 
276  | 0  |     return ossl_prov_drbg_generate(drbg, out, outlen, strength,  | 
277  | 0  |                                    prediction_resistance, adin, adin_len);  | 
278  | 0  | }  | 
279  |  |  | 
280  |  | static int drbg_hmac_uninstantiate(PROV_DRBG *drbg)  | 
281  | 0  | { | 
282  | 0  |     PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)drbg->data;  | 
283  |  | 
  | 
284  | 0  |     OPENSSL_cleanse(hmac->K, sizeof(hmac->K));  | 
285  | 0  |     OPENSSL_cleanse(hmac->V, sizeof(hmac->V));  | 
286  | 0  |     return ossl_prov_drbg_uninstantiate(drbg);  | 
287  | 0  | }  | 
288  |  |  | 
289  |  | static int drbg_hmac_uninstantiate_wrapper(void *vdrbg)  | 
290  | 0  | { | 
291  | 0  |     PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;  | 
292  | 0  |     int ret;  | 
293  |  | 
  | 
294  | 0  |     if (drbg->lock != NULL && !CRYPTO_THREAD_write_lock(drbg->lock))  | 
295  | 0  |         return 0;  | 
296  |  |  | 
297  | 0  |     ret = drbg_hmac_uninstantiate(drbg);  | 
298  |  | 
  | 
299  | 0  |     if (drbg->lock != NULL)  | 
300  | 0  |         CRYPTO_THREAD_unlock(drbg->lock);  | 
301  |  | 
  | 
302  | 0  |     return ret;  | 
303  | 0  | }  | 
304  |  |  | 
305  |  | static int drbg_hmac_verify_zeroization(void *vdrbg)  | 
306  | 0  | { | 
307  | 0  |     PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;  | 
308  | 0  |     PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)drbg->data;  | 
309  | 0  |     int ret = 0;  | 
310  |  | 
  | 
311  | 0  |     if (drbg->lock != NULL && !CRYPTO_THREAD_read_lock(drbg->lock))  | 
312  | 0  |         return 0;  | 
313  |  |  | 
314  | 0  |     PROV_DRBG_VERIFY_ZEROIZATION(hmac->K);  | 
315  | 0  |     PROV_DRBG_VERIFY_ZEROIZATION(hmac->V);  | 
316  |  | 
  | 
317  | 0  |     ret = 1;  | 
318  | 0  |  err:  | 
319  | 0  |     if (drbg->lock != NULL)  | 
320  | 0  |         CRYPTO_THREAD_unlock(drbg->lock);  | 
321  | 0  |     return ret;  | 
322  | 0  | }  | 
323  |  |  | 
324  |  | static int drbg_hmac_new(PROV_DRBG *drbg)  | 
325  | 0  | { | 
326  | 0  |     PROV_DRBG_HMAC *hmac;  | 
327  |  | 
  | 
328  | 0  |     hmac = OPENSSL_secure_zalloc(sizeof(*hmac));  | 
329  | 0  |     if (hmac == NULL)  | 
330  | 0  |         return 0;  | 
331  |  |  | 
332  | 0  |     OSSL_FIPS_IND_INIT(drbg)  | 
333  |  |  | 
334  | 0  |     drbg->data = hmac;  | 
335  |  |     /* See SP800-57 Part1 Rev4 5.6.1 Table 3 */  | 
336  | 0  |     drbg->max_entropylen = DRBG_MAX_LENGTH;  | 
337  | 0  |     drbg->max_noncelen = DRBG_MAX_LENGTH;  | 
338  | 0  |     drbg->max_perslen = DRBG_MAX_LENGTH;  | 
339  | 0  |     drbg->max_adinlen = DRBG_MAX_LENGTH;  | 
340  |  |  | 
341  |  |     /* Maximum number of bits per request = 2^19  = 2^16 bytes */  | 
342  | 0  |     drbg->max_request = 1 << 16;  | 
343  | 0  |     return 1;  | 
344  | 0  | }  | 
345  |  |  | 
346  |  | static void *drbg_hmac_new_wrapper(void *provctx, void *parent,  | 
347  |  |                                    const OSSL_DISPATCH *parent_dispatch)  | 
348  | 0  | { | 
349  | 0  |     return ossl_rand_drbg_new(provctx, parent, parent_dispatch,  | 
350  | 0  |                               &drbg_hmac_new, &drbg_hmac_free,  | 
351  | 0  |                               &drbg_hmac_instantiate, &drbg_hmac_uninstantiate,  | 
352  | 0  |                               &drbg_hmac_reseed, &drbg_hmac_generate);  | 
353  | 0  | }  | 
354  |  |  | 
355  |  | static void drbg_hmac_free(void *vdrbg)  | 
356  | 0  | { | 
357  | 0  |     PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;  | 
358  | 0  |     PROV_DRBG_HMAC *hmac;  | 
359  |  | 
  | 
360  | 0  |     if (drbg != NULL && (hmac = (PROV_DRBG_HMAC *)drbg->data) != NULL) { | 
361  | 0  |         EVP_MAC_CTX_free(hmac->ctx);  | 
362  | 0  |         ossl_prov_digest_reset(&hmac->digest);  | 
363  | 0  |         OPENSSL_secure_clear_free(hmac, sizeof(*hmac));  | 
364  | 0  |     }  | 
365  | 0  |     ossl_rand_drbg_free(drbg);  | 
366  | 0  | }  | 
367  |  |  | 
368  |  | static int drbg_hmac_get_ctx_params(void *vdrbg, OSSL_PARAM params[])  | 
369  | 0  | { | 
370  | 0  |     PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;  | 
371  | 0  |     PROV_DRBG_HMAC *hmac;  | 
372  | 0  |     const char *name;  | 
373  | 0  |     const EVP_MD *md;  | 
374  | 0  |     struct drbg_get_ctx_params_st p;  | 
375  | 0  |     int ret = 0, complete = 0;  | 
376  |  | 
  | 
377  | 0  |     if (drbg == NULL || !drbg_hmac_get_ctx_params_decoder(params, &p))  | 
378  | 0  |         return 0;  | 
379  |  |  | 
380  | 0  |     if (!ossl_drbg_get_ctx_params_no_lock(drbg, &p, params, &complete))  | 
381  | 0  |         return 0;  | 
382  |  |  | 
383  | 0  |     if (complete)  | 
384  | 0  |         return 1;  | 
385  |  |  | 
386  | 0  |     hmac = (PROV_DRBG_HMAC *)drbg->data;  | 
387  |  | 
  | 
388  | 0  |     if (drbg->lock != NULL && !CRYPTO_THREAD_read_lock(drbg->lock))  | 
389  | 0  |         return 0;  | 
390  |  |  | 
391  | 0  |     if (p.mac != NULL) { | 
392  | 0  |         if (hmac->ctx == NULL)  | 
393  | 0  |             goto err;  | 
394  | 0  |         name = EVP_MAC_get0_name(EVP_MAC_CTX_get0_mac(hmac->ctx));  | 
395  | 0  |         if (!OSSL_PARAM_set_utf8_string(p.mac, name))  | 
396  | 0  |             goto err;  | 
397  | 0  |     }  | 
398  |  |  | 
399  | 0  |     if (p.digest != NULL) { | 
400  | 0  |         md = ossl_prov_digest_md(&hmac->digest);  | 
401  | 0  |         if (md == NULL  | 
402  | 0  |                 || !OSSL_PARAM_set_utf8_string(p.digest, EVP_MD_get0_name(md)))  | 
403  | 0  |             goto err;  | 
404  | 0  |     }  | 
405  |  |  | 
406  | 0  |     ret = ossl_drbg_get_ctx_params(drbg, &p);  | 
407  | 0  |  err:  | 
408  | 0  |     if (drbg->lock != NULL)  | 
409  | 0  |         CRYPTO_THREAD_unlock(drbg->lock);  | 
410  |  | 
  | 
411  | 0  |     return ret;  | 
412  | 0  | }  | 
413  |  |  | 
414  |  | static const OSSL_PARAM *drbg_hmac_gettable_ctx_params(ossl_unused void *vctx,  | 
415  |  |                                                        ossl_unused void *p_ctx)  | 
416  | 0  | { | 
417  | 0  |     return drbg_hmac_get_ctx_params_list;  | 
418  | 0  | }  | 
419  |  |  | 
420  |  | static int drbg_fetch_algs_from_prov(const struct drbg_set_ctx_params_st *p,  | 
421  |  |                                      OSSL_LIB_CTX *libctx,  | 
422  |  |                                      EVP_MAC_CTX **macctx,  | 
423  |  |                                      EVP_MD **digest)  | 
424  | 0  | { | 
425  | 0  |     OSSL_PROVIDER *prov = NULL;  | 
426  | 0  |     EVP_MD *md = NULL;  | 
427  | 0  |     EVP_MAC *mac = NULL;  | 
428  | 0  |     int ret = 0;  | 
429  |  | 
  | 
430  | 0  |     if (macctx == NULL || digest == NULL)  | 
431  | 0  |         return 0;  | 
432  |  |  | 
433  | 0  |     if (p->prov == NULL || p->prov->data_type != OSSL_PARAM_UTF8_STRING)  | 
434  | 0  |         return 0;  | 
435  | 0  |     if ((prov = ossl_provider_find(libctx, (const char *)p->prov->data, 1)) == NULL)  | 
436  | 0  |         return 0;  | 
437  |  |  | 
438  | 0  |     if (p->digest != NULL) { | 
439  | 0  |         if (p->digest->data_type != OSSL_PARAM_UTF8_STRING)  | 
440  | 0  |             goto done;  | 
441  |  |  | 
442  | 0  |         md = evp_digest_fetch_from_prov(prov, (const char *)p->digest->data, NULL);  | 
443  | 0  |         if (md) { | 
444  | 0  |             EVP_MD_free(*digest);  | 
445  | 0  |             *digest = md;  | 
446  | 0  |         } else { | 
447  | 0  |             goto done;  | 
448  | 0  |         }  | 
449  | 0  |     }  | 
450  |  |  | 
451  | 0  |     if (p->mac == NULL) { | 
452  | 0  |         ret = 1;  | 
453  | 0  |         goto done;  | 
454  | 0  |     }  | 
455  |  |  | 
456  | 0  |     if (p->mac->data_type != OSSL_PARAM_UTF8_STRING)  | 
457  | 0  |         goto done;  | 
458  |  |  | 
459  | 0  |     EVP_MAC_CTX_free(*macctx);  | 
460  | 0  |     *macctx = NULL;  | 
461  |  | 
  | 
462  | 0  |     mac = evp_mac_fetch_from_prov(prov, (const char *)p->mac->data, NULL);  | 
463  | 0  |     if (mac) { | 
464  | 0  |         *macctx = EVP_MAC_CTX_new(mac);  | 
465  |  |         /* The context holds on to the MAC */  | 
466  | 0  |         EVP_MAC_free(mac);  | 
467  | 0  |         ret = 1;  | 
468  | 0  |     }  | 
469  |  | 
  | 
470  | 0  | done:  | 
471  | 0  |     ossl_provider_free(prov);  | 
472  | 0  |     return ret;  | 
473  | 0  | }  | 
474  |  |  | 
475  |  | static int drbg_hmac_set_ctx_params_locked  | 
476  |  |         (PROV_DRBG *ctx, const struct drbg_set_ctx_params_st *p)  | 
477  | 0  | { | 
478  | 0  |     PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)ctx->data;  | 
479  | 0  |     OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);  | 
480  | 0  |     EVP_MD *prov_md = NULL;  | 
481  | 0  |     const EVP_MD *md;  | 
482  | 0  |     int md_size;  | 
483  |  | 
  | 
484  | 0  |     if (!OSSL_FIPS_IND_SET_CTX_FROM_PARAM(ctx, OSSL_FIPS_IND_SETTABLE0, p->ind_d))  | 
485  | 0  |         return 0;  | 
486  |  |  | 
487  |  |     /* try to fetch mac and digest from provider */  | 
488  | 0  |     (void)ERR_set_mark();  | 
489  | 0  |     if (!drbg_fetch_algs_from_prov(p, libctx, &hmac->ctx, &prov_md)) { | 
490  | 0  |         (void)ERR_pop_to_mark();  | 
491  |  |         /* fall back to full implementation search */  | 
492  | 0  |         if (!ossl_prov_digest_load(&hmac->digest, p->digest, p->propq,  | 
493  | 0  |                                    p->engine, libctx))  | 
494  | 0  |             return 0;  | 
495  |  |  | 
496  | 0  |         if (!ossl_prov_macctx_load(&hmac->ctx, p->mac, NULL, p->digest,  | 
497  | 0  |                                    p->propq, p->engine,  | 
498  | 0  |                                    NULL, NULL, NULL, libctx))  | 
499  | 0  |             return 0;  | 
500  | 0  |     } else { | 
501  | 0  |         (void)ERR_clear_last_mark();  | 
502  | 0  |         if (prov_md)  | 
503  | 0  |             ossl_prov_digest_set_md(&hmac->digest, prov_md);  | 
504  | 0  |     }  | 
505  |  |  | 
506  | 0  |     md = ossl_prov_digest_md(&hmac->digest);  | 
507  | 0  |     if (md != NULL && !ossl_drbg_verify_digest(ctx, libctx, md))  | 
508  | 0  |         return 0;   /* Error already raised for us */  | 
509  |  |  | 
510  | 0  |     if (md != NULL && hmac->ctx != NULL) { | 
511  |  |         /* These are taken from SP 800-90 10.1 Table 2 */  | 
512  | 0  |         md_size = EVP_MD_get_size(md);  | 
513  | 0  |         if (md_size <= 0)  | 
514  | 0  |             return 0;  | 
515  | 0  |         hmac->blocklen = (size_t)md_size;  | 
516  |  |         /* See SP800-57 Part1 Rev4 5.6.1 Table 3 */  | 
517  | 0  |         ctx->strength = 64 * (int)(hmac->blocklen >> 3);  | 
518  | 0  |         if (ctx->strength > 256)  | 
519  | 0  |             ctx->strength = 256;  | 
520  | 0  |         ctx->seedlen = hmac->blocklen;  | 
521  | 0  |         ctx->min_entropylen = ctx->strength / 8;  | 
522  | 0  |         ctx->min_noncelen = ctx->min_entropylen / 2;  | 
523  | 0  |     }  | 
524  |  |  | 
525  | 0  |     return ossl_drbg_set_ctx_params(ctx, p);  | 
526  | 0  | }  | 
527  |  |  | 
528  |  | static int drbg_hmac_set_ctx_params(void *vctx, const OSSL_PARAM params[])  | 
529  | 0  | { | 
530  | 0  |     PROV_DRBG *drbg = (PROV_DRBG *)vctx;  | 
531  | 0  |     struct drbg_set_ctx_params_st p;  | 
532  | 0  |     int ret;  | 
533  |  | 
  | 
534  | 0  |     if (drbg == NULL || !drbg_hmac_set_ctx_params_decoder(params, &p))  | 
535  | 0  |         return 0;  | 
536  |  |  | 
537  | 0  |     if (drbg->lock != NULL && !CRYPTO_THREAD_write_lock(drbg->lock))  | 
538  | 0  |         return 0;  | 
539  |  |  | 
540  | 0  |     ret = drbg_hmac_set_ctx_params_locked(drbg, &p);  | 
541  |  | 
  | 
542  | 0  |     if (drbg->lock != NULL)  | 
543  | 0  |         CRYPTO_THREAD_unlock(drbg->lock);  | 
544  |  | 
  | 
545  | 0  |     return ret;  | 
546  | 0  | }  | 
547  |  |  | 
548  |  | static const OSSL_PARAM *drbg_hmac_settable_ctx_params(ossl_unused void *vctx,  | 
549  |  |                                                        ossl_unused void *p_ctx)  | 
550  | 0  | { | 
551  | 0  |     return drbg_hmac_set_ctx_params_list;  | 
552  | 0  | }  | 
553  |  |  | 
554  |  | const OSSL_DISPATCH ossl_drbg_ossl_hmac_functions[] = { | 
555  |  |     { OSSL_FUNC_RAND_NEWCTX, (void(*)(void))drbg_hmac_new_wrapper }, | 
556  |  |     { OSSL_FUNC_RAND_FREECTX, (void(*)(void))drbg_hmac_free }, | 
557  |  |     { OSSL_FUNC_RAND_INSTANTIATE, | 
558  |  |       (void(*)(void))drbg_hmac_instantiate_wrapper },  | 
559  |  |     { OSSL_FUNC_RAND_UNINSTANTIATE, | 
560  |  |       (void(*)(void))drbg_hmac_uninstantiate_wrapper },  | 
561  |  |     { OSSL_FUNC_RAND_GENERATE, (void(*)(void))drbg_hmac_generate_wrapper }, | 
562  |  |     { OSSL_FUNC_RAND_RESEED, (void(*)(void))drbg_hmac_reseed_wrapper }, | 
563  |  |     { OSSL_FUNC_RAND_ENABLE_LOCKING, (void(*)(void))ossl_drbg_enable_locking }, | 
564  |  |     { OSSL_FUNC_RAND_LOCK, (void(*)(void))ossl_drbg_lock }, | 
565  |  |     { OSSL_FUNC_RAND_UNLOCK, (void(*)(void))ossl_drbg_unlock }, | 
566  |  |     { OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS, | 
567  |  |       (void(*)(void))drbg_hmac_settable_ctx_params },  | 
568  |  |     { OSSL_FUNC_RAND_SET_CTX_PARAMS, (void(*)(void))drbg_hmac_set_ctx_params }, | 
569  |  |     { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS, | 
570  |  |       (void(*)(void))drbg_hmac_gettable_ctx_params },  | 
571  |  |     { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))drbg_hmac_get_ctx_params }, | 
572  |  |     { OSSL_FUNC_RAND_VERIFY_ZEROIZATION, | 
573  |  |       (void(*)(void))drbg_hmac_verify_zeroization },  | 
574  |  |     { OSSL_FUNC_RAND_GET_SEED, (void(*)(void))ossl_drbg_get_seed }, | 
575  |  |     { OSSL_FUNC_RAND_CLEAR_SEED, (void(*)(void))ossl_drbg_clear_seed }, | 
576  |  |     OSSL_DISPATCH_END  | 
577  |  | };  |