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