prov: prefix all OSSL_DISPATCH tables names with ossl_
[openssl.git] / providers / implementations / rands / drbg_hmac.c
1 /*
2  * Copyright 2011-2020 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 "prov/provider_util.h"
16 #include "internal/thread_once.h"
17 #include "prov/providercommon.h"
18 #include "prov/providercommonerr.h"
19 #include "prov/implementations.h"
20 #include "prov/provider_ctx.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 typedef 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;
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  */
57 static int do_hmac(PROV_DRBG_HMAC *hmac, unsigned char inbyte,
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 {
62     EVP_MAC_CTX *ctx = hmac->ctx;
63     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
64
65     *params = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY, hmac->K,
66                                                 hmac->blocklen);
67     if (!EVP_MAC_CTX_set_params(ctx, params)
68             || !EVP_MAC_init(ctx)
69             /* K = HMAC(K, V || inbyte || [in1] || [in2] || [in3]) */
70             || !EVP_MAC_update(ctx, hmac->V, hmac->blocklen)
71             || !EVP_MAC_update(ctx, &inbyte, 1)
72             || !(in1 == NULL || in1len == 0 || EVP_MAC_update(ctx, in1, in1len))
73             || !(in2 == NULL || in2len == 0 || EVP_MAC_update(ctx, in2, in2len))
74             || !(in3 == NULL || in3len == 0 || EVP_MAC_update(ctx, in3, in3len))
75             || !EVP_MAC_final(ctx, hmac->K, NULL, sizeof(hmac->K)))
76         return 0;
77
78    /* V = HMAC(K, V) */
79     *params = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY, hmac->K,
80                                                 hmac->blocklen);
81     return EVP_MAC_CTX_set_params(ctx, params)
82            && EVP_MAC_init(ctx)
83            && EVP_MAC_update(ctx, hmac->V, hmac->blocklen)
84            && EVP_MAC_final(ctx, hmac->V, NULL, sizeof(hmac->V));
85 }
86
87 /*
88  * SP800-90Ar1 10.1.2.2 HMAC_DRBG_Update_Process
89  *
90  *
91  * Updates the drbg objects Key(K) and Value(V) using the following algorithm:
92  *   K,V = do_hmac(hmac, 0, in1, in2, in3)
93  *   if (any input is not NULL)
94  *     K,V = do_hmac(hmac, 1, in1, in2, in3)
95  *
96  * where in1, in2, in3 are optional input buffers that can be NULL.
97  *       in1len, in2len, in3len are the lengths of the input buffers.
98  *
99  * Returns zero if an error occurs otherwise it returns 1.
100  */
101 static int drbg_hmac_update(PROV_DRBG *drbg,
102                             const unsigned char *in1, size_t in1len,
103                             const unsigned char *in2, size_t in2len,
104                             const unsigned char *in3, size_t in3len)
105 {
106     PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)drbg->data;
107
108     /* (Steps 1-2) K = HMAC(K, V||0x00||provided_data). V = HMAC(K,V) */
109     if (!do_hmac(hmac, 0x00, in1, in1len, in2, in2len, in3, in3len))
110         return 0;
111     /* (Step 3) If provided_data == NULL then return (K,V) */
112     if (in1len == 0 && in2len == 0 && in3len == 0)
113         return 1;
114     /* (Steps 4-5) K = HMAC(K, V||0x01||provided_data). V = HMAC(K,V) */
115     return do_hmac(hmac, 0x01, in1, in1len, in2, in2len, in3, in3len);
116 }
117
118 /*
119  * SP800-90Ar1 10.1.2.3 HMAC_DRBG_Instantiate_Process:
120  *
121  * This sets the drbg Key (K) to all zeros, and Value (V) to all 1's.
122  * and then calls (K,V) = drbg_hmac_update() with input parameters:
123  *   ent = entropy data (Can be NULL) of length ent_len.
124  *   nonce = nonce data (Can be NULL) of length nonce_len.
125  *   pstr = personalization data (Can be NULL) of length pstr_len.
126  *
127  * Returns zero if an error occurs otherwise it returns 1.
128  */
129 static int drbg_hmac_instantiate(PROV_DRBG *drbg,
130                                  const unsigned char *ent, size_t ent_len,
131                                  const unsigned char *nonce, size_t nonce_len,
132                                  const unsigned char *pstr, size_t pstr_len)
133 {
134     PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)drbg->data;
135
136     if (hmac->ctx == NULL) {
137         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MAC);
138         return 0;
139     }
140
141     /* (Step 2) Key = 0x00 00...00 */
142     memset(hmac->K, 0x00, hmac->blocklen);
143     /* (Step 3) V = 0x01 01...01 */
144     memset(hmac->V, 0x01, hmac->blocklen);
145     /* (Step 4) (K,V) = HMAC_DRBG_Update(entropy||nonce||pers string, K, V) */
146     return drbg_hmac_update(drbg, ent, ent_len, nonce, nonce_len, pstr,
147                             pstr_len);
148 }
149
150 static int drbg_hmac_instantiate_wrapper(void *vdrbg, unsigned int strength,
151                                          int prediction_resistance,
152                                          const unsigned char *pstr,
153                                          size_t pstr_len)
154 {
155     PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
156
157     return PROV_DRBG_instantiate(drbg, strength, prediction_resistance,
158                                  pstr, pstr_len);
159 }
160
161 /*
162  * SP800-90Ar1 10.1.2.4 HMAC_DRBG_Reseed_Process:
163  *
164  * Reseeds the drbg's Key (K) and Value (V) by calling
165  * (K,V) = drbg_hmac_update() with the following input parameters:
166  *   ent = entropy input data (Can be NULL) of length ent_len.
167  *   adin = additional input data (Can be NULL) of length adin_len.
168  *
169  * Returns zero if an error occurs otherwise it returns 1.
170  */
171 static int drbg_hmac_reseed(PROV_DRBG *drbg,
172                             const unsigned char *ent, size_t ent_len,
173                             const unsigned char *adin, size_t adin_len)
174 {
175     /* (Step 2) (K,V) = HMAC_DRBG_Update(entropy||additional_input, K, V) */
176     return drbg_hmac_update(drbg, ent, ent_len, adin, adin_len, NULL, 0);
177 }
178
179 static int drbg_hmac_reseed_wrapper(void *vdrbg, int prediction_resistance,
180                                     const unsigned char *ent, size_t ent_len,
181                                     const unsigned char *adin, size_t adin_len)
182 {
183     PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
184
185     return PROV_DRBG_reseed(drbg, prediction_resistance, ent, ent_len,
186                             adin, adin_len);
187 }
188
189 /*
190  * SP800-90Ar1 10.1.2.5 HMAC_DRBG_Generate_Process:
191  *
192  * Generates pseudo random bytes and updates the internal K,V for the drbg.
193  * out is a buffer to fill with outlen bytes of pseudo random data.
194  * adin is an additional_input string of size adin_len that may be NULL.
195  *
196  * Returns zero if an error occurs otherwise it returns 1.
197  */
198 static int drbg_hmac_generate(PROV_DRBG *drbg,
199                               unsigned char *out, size_t outlen,
200                               const unsigned char *adin, size_t adin_len)
201 {
202     PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)drbg->data;
203     EVP_MAC_CTX *ctx = hmac->ctx;
204     const unsigned char *temp = hmac->V;
205     OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
206
207     /* (Step 2) if adin != NULL then (K,V) = HMAC_DRBG_Update(adin, K, V) */
208     if (adin != NULL
209             && adin_len > 0
210             && !drbg_hmac_update(drbg, adin, adin_len, NULL, 0, NULL, 0))
211         return 0;
212
213     /*
214      * (Steps 3-5) temp = NULL
215      *             while (len(temp) < outlen) {
216      *                 V = HMAC(K, V)
217      *                 temp = temp || V
218      *             }
219      */
220     for (;;) {
221         *params = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
222                                                     hmac->K, hmac->blocklen);
223         if (!EVP_MAC_CTX_set_params(ctx, params)
224             || !EVP_MAC_init(ctx)
225             || !EVP_MAC_update(ctx, temp, hmac->blocklen))
226             return 0;
227
228         if (outlen > hmac->blocklen) {
229             if (!EVP_MAC_final(ctx, out, NULL, outlen))
230                 return 0;
231             temp = out;
232         } else {
233             if (!EVP_MAC_final(ctx, hmac->V, NULL, sizeof(hmac->V)))
234                 return 0;
235             memcpy(out, hmac->V, outlen);
236             break;
237         }
238         out += hmac->blocklen;
239         outlen -= hmac->blocklen;
240     }
241     /* (Step 6) (K,V) = HMAC_DRBG_Update(adin, K, V) */
242     if (!drbg_hmac_update(drbg, adin, adin_len, NULL, 0, NULL, 0))
243         return 0;
244
245     return 1;
246 }
247
248 static int drbg_hmac_generate_wrapper
249     (void *vdrbg, unsigned char *out, size_t outlen, unsigned int strength,
250      int prediction_resistance, const unsigned char *adin, size_t adin_len)
251 {
252     PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
253
254     return PROV_DRBG_generate(drbg, out, outlen, strength,
255                               prediction_resistance, adin, adin_len);
256 }
257
258 static int drbg_hmac_uninstantiate(PROV_DRBG *drbg)
259 {
260     PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)drbg->data;
261
262     OPENSSL_cleanse(hmac->K, sizeof(hmac->K));
263     OPENSSL_cleanse(hmac->V, sizeof(hmac->V));
264     return PROV_DRBG_uninstantiate(drbg);
265 }
266
267 static int drbg_hmac_uninstantiate_wrapper(void *vdrbg)
268 {
269     return drbg_hmac_uninstantiate((PROV_DRBG *)vdrbg);
270 }
271
272 static int drbg_hmac_verify_zeroization(void *vdrbg)
273 {
274     PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
275     PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)drbg->data;
276
277     PROV_DRBG_VERYIFY_ZEROIZATION(hmac->K);
278     PROV_DRBG_VERYIFY_ZEROIZATION(hmac->V);
279     return 1;
280 }
281
282 static int drbg_hmac_new(PROV_DRBG *drbg)
283 {
284     PROV_DRBG_HMAC *hmac;
285
286     hmac = OPENSSL_secure_zalloc(sizeof(*hmac));
287     if (hmac == NULL) {
288         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
289         return 0;
290     }
291
292     drbg->data = hmac;
293     /* See SP800-57 Part1 Rev4 5.6.1 Table 3 */
294     drbg->max_entropylen = DRBG_MAX_LENGTH;
295     drbg->max_noncelen = DRBG_MAX_LENGTH;
296     drbg->max_perslen = DRBG_MAX_LENGTH;
297     drbg->max_adinlen = DRBG_MAX_LENGTH;
298
299     /* Maximum number of bits per request = 2^19  = 2^16 bytes */
300     drbg->max_request = 1 << 16;
301     return 1;
302 }
303
304 static void *drbg_hmac_new_wrapper(void *provctx, void *parent,
305                                    const OSSL_DISPATCH *parent_dispatch)
306 {
307     return prov_rand_drbg_new(provctx, parent, parent_dispatch, &drbg_hmac_new,
308                               &drbg_hmac_instantiate, &drbg_hmac_uninstantiate,
309                               &drbg_hmac_reseed, &drbg_hmac_generate);
310 }
311
312 static void drbg_hmac_free(void *vdrbg)
313 {
314     PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
315     PROV_DRBG_HMAC *hmac;
316
317     if (drbg != NULL && (hmac = (PROV_DRBG_HMAC *)drbg->data) != NULL) {
318         EVP_MAC_CTX_free(hmac->ctx);
319         ossl_prov_digest_reset(&hmac->digest);
320         OPENSSL_secure_clear_free(hmac, sizeof(*hmac));
321     }
322     prov_rand_drbg_free(drbg);
323 }
324
325 static int drbg_hmac_get_ctx_params(void *vdrbg, OSSL_PARAM params[])
326 {
327     PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
328     PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)drbg->data;
329     const char *name;
330     const EVP_MD *md;
331     OSSL_PARAM *p;
332
333     p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_MAC);
334     if (p != NULL) {
335         if (hmac->ctx == NULL)
336             return 0;
337         name = EVP_MAC_name(EVP_MAC_CTX_mac(hmac->ctx));
338         if (!OSSL_PARAM_set_utf8_string(p, name))
339             return 0;
340     }
341
342     p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_DIGEST);
343     if (p != NULL) {
344         md = ossl_prov_digest_md(&hmac->digest);
345         if (md == NULL || !OSSL_PARAM_set_utf8_string(p, EVP_MD_name(md)))
346             return 0;
347     }
348
349     return drbg_get_ctx_params(drbg, params);
350 }
351
352 static const OSSL_PARAM *drbg_hmac_gettable_ctx_params(ossl_unused void *p_ctx)
353 {
354     static const OSSL_PARAM known_gettable_ctx_params[] = {
355         OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_MAC, NULL, 0),
356         OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_DIGEST, NULL, 0),
357         OSSL_PARAM_DRBG_GETTABLE_CTX_COMMON,
358         OSSL_PARAM_END
359     };
360     return known_gettable_ctx_params;
361 }
362
363 static int drbg_hmac_set_ctx_params(void *vctx, const OSSL_PARAM params[])
364 {
365     PROV_DRBG *ctx = (PROV_DRBG *)vctx;
366     PROV_DRBG_HMAC *hmac = (PROV_DRBG_HMAC *)ctx->data;
367     OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(ctx->provctx);
368     const EVP_MD *md;
369
370     if (!ossl_prov_digest_load_from_params(&hmac->digest, params, libctx))
371         return 0;
372
373     /*
374      * Confirm digest is allowed. We allow all digests that are not XOF
375      * (such as SHAKE).  In FIPS mode, the fetch will fail for non-approved
376      * digests.
377      */
378     md = ossl_prov_digest_md(&hmac->digest);
379     if (md != NULL && (EVP_MD_flags(md) & EVP_MD_FLAG_XOF) != 0) {
380         ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED);
381         return 0;
382     }
383
384     if (!ossl_prov_macctx_load_from_params(&hmac->ctx, params,
385                                            NULL, NULL, NULL, libctx))
386         return 0;
387
388     if (hmac->ctx != NULL) {
389         /* These are taken from SP 800-90 10.1 Table 2 */
390         hmac->blocklen = EVP_MD_size(md);
391         /* See SP800-57 Part1 Rev4 5.6.1 Table 3 */
392         ctx->strength = 64 * (int)(hmac->blocklen >> 3);
393         if (ctx->strength > 256)
394             ctx->strength = 256;
395         ctx->seedlen = hmac->blocklen;
396         ctx->min_entropylen = ctx->strength / 8;
397         ctx->min_noncelen = ctx->min_entropylen / 2;
398     }
399
400     return drbg_set_ctx_params(ctx, params);
401 }
402
403 static const OSSL_PARAM *drbg_hmac_settable_ctx_params(ossl_unused void *p_ctx)
404 {
405     static const OSSL_PARAM known_settable_ctx_params[] = {
406         OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_PROPERTIES, NULL, 0),
407         OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_DIGEST, NULL, 0),
408         OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_MAC, NULL, 0),
409         OSSL_PARAM_DRBG_SETTABLE_CTX_COMMON,
410         OSSL_PARAM_END
411     };
412     return known_settable_ctx_params;
413 }
414
415 const OSSL_DISPATCH ossl_drbg_ossl_hmac_functions[] = {
416     { OSSL_FUNC_RAND_NEWCTX, (void(*)(void))drbg_hmac_new_wrapper },
417     { OSSL_FUNC_RAND_FREECTX, (void(*)(void))drbg_hmac_free },
418     { OSSL_FUNC_RAND_INSTANTIATE,
419       (void(*)(void))drbg_hmac_instantiate_wrapper },
420     { OSSL_FUNC_RAND_UNINSTANTIATE,
421       (void(*)(void))drbg_hmac_uninstantiate_wrapper },
422     { OSSL_FUNC_RAND_GENERATE, (void(*)(void))drbg_hmac_generate_wrapper },
423     { OSSL_FUNC_RAND_RESEED, (void(*)(void))drbg_hmac_reseed_wrapper },
424     { OSSL_FUNC_RAND_ENABLE_LOCKING, (void(*)(void))drbg_enable_locking },
425     { OSSL_FUNC_RAND_LOCK, (void(*)(void))drbg_lock },
426     { OSSL_FUNC_RAND_UNLOCK, (void(*)(void))drbg_unlock },
427     { OSSL_FUNC_RAND_SETTABLE_CTX_PARAMS,
428       (void(*)(void))drbg_hmac_settable_ctx_params },
429     { OSSL_FUNC_RAND_SET_CTX_PARAMS, (void(*)(void))drbg_hmac_set_ctx_params },
430     { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS,
431       (void(*)(void))drbg_hmac_gettable_ctx_params },
432     { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))drbg_hmac_get_ctx_params },
433     { OSSL_FUNC_RAND_VERIFY_ZEROIZATION,
434       (void(*)(void))drbg_hmac_verify_zeroization },
435     { 0, NULL }
436 };