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