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