hkdf: when HMAC key is all zeros, still set a valid key length
[openssl.git] / providers / implementations / macs / hmac_prov.c
1 /*
2  * Copyright 2018-2023 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 /*
11  * HMAC low level APIs are deprecated for public use, but still ok for internal
12  * use.
13  */
14 #include "internal/deprecated.h"
15
16 #include <string.h>
17
18 #include <openssl/core_dispatch.h>
19 #include <openssl/core_names.h>
20 #include <openssl/params.h>
21 #include <openssl/evp.h>
22 #include <openssl/hmac.h>
23
24 #include "internal/ssl3_cbc.h"
25
26 #include "prov/implementations.h"
27 #include "prov/provider_ctx.h"
28 #include "prov/provider_util.h"
29 #include "prov/providercommon.h"
30
31 /*
32  * Forward declaration of everything implemented here.  This is not strictly
33  * necessary for the compiler, but provides an assurance that the signatures
34  * of the functions in the dispatch table are correct.
35  */
36 static OSSL_FUNC_mac_newctx_fn hmac_new;
37 static OSSL_FUNC_mac_dupctx_fn hmac_dup;
38 static OSSL_FUNC_mac_freectx_fn hmac_free;
39 static OSSL_FUNC_mac_gettable_ctx_params_fn hmac_gettable_ctx_params;
40 static OSSL_FUNC_mac_get_ctx_params_fn hmac_get_ctx_params;
41 static OSSL_FUNC_mac_settable_ctx_params_fn hmac_settable_ctx_params;
42 static OSSL_FUNC_mac_set_ctx_params_fn hmac_set_ctx_params;
43 static OSSL_FUNC_mac_init_fn hmac_init;
44 static OSSL_FUNC_mac_update_fn hmac_update;
45 static OSSL_FUNC_mac_final_fn hmac_final;
46
47 /* local HMAC context structure */
48
49 /* typedef EVP_MAC_IMPL */
50 struct hmac_data_st {
51     void *provctx;
52     HMAC_CTX *ctx;               /* HMAC context */
53     PROV_DIGEST digest;
54     unsigned char *key;
55     size_t keylen;
56     /* Length of full TLS record including the MAC and any padding */
57     size_t tls_data_size;
58     unsigned char tls_header[13];
59     int tls_header_set;
60     unsigned char tls_mac_out[EVP_MAX_MD_SIZE];
61     size_t tls_mac_out_size;
62 };
63
64 static void *hmac_new(void *provctx)
65 {
66     struct hmac_data_st *macctx;
67
68     if (!ossl_prov_is_running())
69         return NULL;
70
71     if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL
72         || (macctx->ctx = HMAC_CTX_new()) == NULL) {
73         OPENSSL_free(macctx);
74         return NULL;
75     }
76     macctx->provctx = provctx;
77
78     return macctx;
79 }
80
81 static void hmac_free(void *vmacctx)
82 {
83     struct hmac_data_st *macctx = vmacctx;
84
85     if (macctx != NULL) {
86         HMAC_CTX_free(macctx->ctx);
87         ossl_prov_digest_reset(&macctx->digest);
88         OPENSSL_secure_clear_free(macctx->key, macctx->keylen);
89         OPENSSL_free(macctx);
90     }
91 }
92
93 static void *hmac_dup(void *vsrc)
94 {
95     struct hmac_data_st *src = vsrc;
96     struct hmac_data_st *dst;
97     HMAC_CTX *ctx;
98
99     if (!ossl_prov_is_running())
100         return NULL;
101     dst = hmac_new(src->provctx);
102     if (dst == NULL)
103         return NULL;
104
105     ctx = dst->ctx;
106     *dst = *src;
107     dst->ctx = ctx;
108     dst->key = NULL;
109     memset(&dst->digest, 0, sizeof(dst->digest));
110
111     if (!HMAC_CTX_copy(dst->ctx, src->ctx)
112         || !ossl_prov_digest_copy(&dst->digest, &src->digest)) {
113         hmac_free(dst);
114         return NULL;
115     }
116     if (src->key != NULL) {
117         /* There is no "secure" OPENSSL_memdup */
118         dst->key = OPENSSL_secure_malloc(src->keylen > 0 ? src->keylen : 1);
119         if (dst->key == NULL) {
120             hmac_free(dst);
121             return 0;
122         }
123         memcpy(dst->key, src->key, src->keylen);
124     }
125     return dst;
126 }
127
128 static size_t hmac_size(struct hmac_data_st *macctx)
129 {
130     return HMAC_size(macctx->ctx);
131 }
132
133 static int hmac_block_size(struct hmac_data_st *macctx)
134 {
135     const EVP_MD *md = ossl_prov_digest_md(&macctx->digest);
136
137     if (md == NULL)
138         return 0;
139     return EVP_MD_block_size(md);
140 }
141
142 static int hmac_setkey(struct hmac_data_st *macctx,
143                        const unsigned char *key, size_t keylen)
144 {
145     const EVP_MD *digest;
146
147     if (macctx->key != NULL)
148         OPENSSL_secure_clear_free(macctx->key, macctx->keylen);
149     /* Keep a copy of the key in case we need it for TLS HMAC */
150     macctx->key = OPENSSL_secure_malloc(keylen > 0 ? keylen : 1);
151     if (macctx->key == NULL)
152         return 0;
153     memcpy(macctx->key, key, keylen);
154     macctx->keylen = keylen;
155
156     digest = ossl_prov_digest_md(&macctx->digest);
157     /* HMAC_Init_ex doesn't tolerate all zero params, so we must be careful */
158     if (key != NULL || (macctx->tls_data_size == 0 && digest != NULL))
159         return HMAC_Init_ex(macctx->ctx, key, keylen, digest,
160                             ossl_prov_digest_engine(&macctx->digest));
161     return 1;
162 }
163
164 static int hmac_init(void *vmacctx, const unsigned char *key,
165                      size_t keylen, const OSSL_PARAM params[])
166 {
167     struct hmac_data_st *macctx = vmacctx;
168
169     if (!ossl_prov_is_running() || !hmac_set_ctx_params(macctx, params))
170         return 0;
171
172     if (key != NULL)
173         return hmac_setkey(macctx, key, keylen);
174
175     /* Just reinit the HMAC context */
176     return HMAC_Init_ex(macctx->ctx, NULL, 0, NULL, NULL);
177 }
178
179 static int hmac_update(void *vmacctx, const unsigned char *data,
180                        size_t datalen)
181 {
182     struct hmac_data_st *macctx = vmacctx;
183
184     if (macctx->tls_data_size > 0) {
185         /* We're doing a TLS HMAC */
186         if (!macctx->tls_header_set) {
187             /* We expect the first update call to contain the TLS header */
188             if (datalen != sizeof(macctx->tls_header))
189                 return 0;
190             memcpy(macctx->tls_header, data, datalen);
191             macctx->tls_header_set = 1;
192             return 1;
193         }
194         /* macctx->tls_data_size is datalen plus the padding length */
195         if (macctx->tls_data_size < datalen)
196             return 0;
197
198         return ssl3_cbc_digest_record(ossl_prov_digest_md(&macctx->digest),
199                                       macctx->tls_mac_out,
200                                       &macctx->tls_mac_out_size,
201                                       macctx->tls_header,
202                                       data,
203                                       datalen,
204                                       macctx->tls_data_size,
205                                       macctx->key,
206                                       macctx->keylen,
207                                       0);
208     }
209
210     return HMAC_Update(macctx->ctx, data, datalen);
211 }
212
213 static int hmac_final(void *vmacctx, unsigned char *out, size_t *outl,
214                       size_t outsize)
215 {
216     unsigned int hlen;
217     struct hmac_data_st *macctx = vmacctx;
218
219     if (!ossl_prov_is_running())
220         return 0;
221     if (macctx->tls_data_size > 0) {
222         if (macctx->tls_mac_out_size == 0)
223             return 0;
224         if (outl != NULL)
225             *outl = macctx->tls_mac_out_size;
226         memcpy(out, macctx->tls_mac_out, macctx->tls_mac_out_size);
227         return 1;
228     }
229     if (!HMAC_Final(macctx->ctx, out, &hlen))
230         return 0;
231     *outl = hlen;
232     return 1;
233 }
234
235 static const OSSL_PARAM known_gettable_ctx_params[] = {
236     OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL),
237     OSSL_PARAM_size_t(OSSL_MAC_PARAM_BLOCK_SIZE, NULL),
238     OSSL_PARAM_END
239 };
240 static const OSSL_PARAM *hmac_gettable_ctx_params(ossl_unused void *ctx,
241                                                   ossl_unused void *provctx)
242 {
243     return known_gettable_ctx_params;
244 }
245
246 static int hmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
247 {
248     struct hmac_data_st *macctx = vmacctx;
249     OSSL_PARAM *p;
250
251     if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL
252             && !OSSL_PARAM_set_size_t(p, hmac_size(macctx)))
253         return 0;
254
255     if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_BLOCK_SIZE)) != NULL
256             && !OSSL_PARAM_set_int(p, hmac_block_size(macctx)))
257         return 0;
258
259     return 1;
260 }
261
262 static const OSSL_PARAM known_settable_ctx_params[] = {
263     OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST, NULL, 0),
264     OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0),
265     OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
266     OSSL_PARAM_int(OSSL_MAC_PARAM_DIGEST_NOINIT, NULL),
267     OSSL_PARAM_int(OSSL_MAC_PARAM_DIGEST_ONESHOT, NULL),
268     OSSL_PARAM_size_t(OSSL_MAC_PARAM_TLS_DATA_SIZE, NULL),
269     OSSL_PARAM_END
270 };
271 static const OSSL_PARAM *hmac_settable_ctx_params(ossl_unused void *ctx,
272                                                   ossl_unused void *provctx)
273 {
274     return known_settable_ctx_params;
275 }
276
277 /*
278  * ALL parameters should be set before init().
279  */
280 static int hmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
281 {
282     struct hmac_data_st *macctx = vmacctx;
283     OSSL_LIB_CTX *ctx = PROV_LIBCTX_OF(macctx->provctx);
284     const OSSL_PARAM *p;
285
286     if (params == NULL)
287         return 1;
288
289     if (!ossl_prov_digest_load_from_params(&macctx->digest, params, ctx))
290         return 0;
291
292     if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
293         if (p->data_type != OSSL_PARAM_OCTET_STRING)
294             return 0;
295         if (!hmac_setkey(macctx, p->data, p->data_size))
296             return 0;
297     }
298
299     if ((p = OSSL_PARAM_locate_const(params,
300                                      OSSL_MAC_PARAM_TLS_DATA_SIZE)) != NULL) {
301         if (!OSSL_PARAM_get_size_t(p, &macctx->tls_data_size))
302             return 0;
303     }
304     return 1;
305 }
306
307 const OSSL_DISPATCH ossl_hmac_functions[] = {
308     { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))hmac_new },
309     { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))hmac_dup },
310     { OSSL_FUNC_MAC_FREECTX, (void (*)(void))hmac_free },
311     { OSSL_FUNC_MAC_INIT, (void (*)(void))hmac_init },
312     { OSSL_FUNC_MAC_UPDATE, (void (*)(void))hmac_update },
313     { OSSL_FUNC_MAC_FINAL, (void (*)(void))hmac_final },
314     { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
315       (void (*)(void))hmac_gettable_ctx_params },
316     { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))hmac_get_ctx_params },
317     { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
318       (void (*)(void))hmac_settable_ctx_params },
319     { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))hmac_set_ctx_params },
320     OSSL_DISPATCH_END
321 };