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