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