/src/openssl32/providers/implementations/macs/hmac_prov.c
Line | Count | Source (jump to first uncovered line) |
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 | 639k | { |
66 | 639k | struct hmac_data_st *macctx; |
67 | | |
68 | 639k | if (!ossl_prov_is_running()) |
69 | 0 | return NULL; |
70 | | |
71 | 639k | if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL |
72 | 639k | || (macctx->ctx = HMAC_CTX_new()) == NULL) { |
73 | 0 | OPENSSL_free(macctx); |
74 | 0 | return NULL; |
75 | 0 | } |
76 | 639k | macctx->provctx = provctx; |
77 | | |
78 | 639k | return macctx; |
79 | 639k | } |
80 | | |
81 | | static void hmac_free(void *vmacctx) |
82 | 639k | { |
83 | 639k | struct hmac_data_st *macctx = vmacctx; |
84 | | |
85 | 639k | if (macctx != NULL) { |
86 | 639k | HMAC_CTX_free(macctx->ctx); |
87 | 639k | ossl_prov_digest_reset(&macctx->digest); |
88 | 639k | OPENSSL_secure_clear_free(macctx->key, macctx->keylen); |
89 | 639k | OPENSSL_free(macctx); |
90 | 639k | } |
91 | 639k | } |
92 | | |
93 | | static void *hmac_dup(void *vsrc) |
94 | 523k | { |
95 | 523k | struct hmac_data_st *src = vsrc; |
96 | 523k | struct hmac_data_st *dst; |
97 | 523k | HMAC_CTX *ctx; |
98 | | |
99 | 523k | if (!ossl_prov_is_running()) |
100 | 0 | return NULL; |
101 | 523k | dst = hmac_new(src->provctx); |
102 | 523k | if (dst == NULL) |
103 | 0 | return NULL; |
104 | | |
105 | 523k | ctx = dst->ctx; |
106 | 523k | *dst = *src; |
107 | 523k | dst->ctx = ctx; |
108 | 523k | dst->key = NULL; |
109 | 523k | memset(&dst->digest, 0, sizeof(dst->digest)); |
110 | | |
111 | 523k | if (!HMAC_CTX_copy(dst->ctx, src->ctx) |
112 | 523k | || !ossl_prov_digest_copy(&dst->digest, &src->digest)) { |
113 | 0 | hmac_free(dst); |
114 | 0 | return NULL; |
115 | 0 | } |
116 | 523k | if (src->key != NULL) { |
117 | | /* There is no "secure" OPENSSL_memdup */ |
118 | 523k | dst->key = OPENSSL_secure_malloc(src->keylen > 0 ? src->keylen : 1); |
119 | 523k | if (dst->key == NULL) { |
120 | 0 | hmac_free(dst); |
121 | 0 | return 0; |
122 | 0 | } |
123 | 523k | memcpy(dst->key, src->key, src->keylen); |
124 | 523k | } |
125 | 523k | return dst; |
126 | 523k | } |
127 | | |
128 | | static size_t hmac_size(struct hmac_data_st *macctx) |
129 | 460k | { |
130 | 460k | return HMAC_size(macctx->ctx); |
131 | 460k | } |
132 | | |
133 | | static int hmac_block_size(struct hmac_data_st *macctx) |
134 | 0 | { |
135 | 0 | const EVP_MD *md = ossl_prov_digest_md(&macctx->digest); |
136 | |
|
137 | 0 | if (md == NULL) |
138 | 0 | return 0; |
139 | 0 | return EVP_MD_block_size(md); |
140 | 0 | } |
141 | | |
142 | | static int hmac_setkey(struct hmac_data_st *macctx, |
143 | | const unsigned char *key, size_t keylen) |
144 | 118k | { |
145 | 118k | const EVP_MD *digest; |
146 | | |
147 | 118k | if (macctx->key != NULL) |
148 | 4.16k | OPENSSL_secure_clear_free(macctx->key, macctx->keylen); |
149 | | /* Keep a copy of the key in case we need it for TLS HMAC */ |
150 | 118k | macctx->key = OPENSSL_secure_malloc(keylen > 0 ? keylen : 1); |
151 | 118k | if (macctx->key == NULL) |
152 | 0 | return 0; |
153 | 118k | memcpy(macctx->key, key, keylen); |
154 | 118k | macctx->keylen = keylen; |
155 | | |
156 | 118k | digest = ossl_prov_digest_md(&macctx->digest); |
157 | | /* HMAC_Init_ex doesn't tolerate all zero params, so we must be careful */ |
158 | 118k | if (key != NULL || (macctx->tls_data_size == 0 && digest != NULL)) |
159 | 118k | return HMAC_Init_ex(macctx->ctx, key, keylen, digest, |
160 | 118k | ossl_prov_digest_engine(&macctx->digest)); |
161 | 0 | return 1; |
162 | 118k | } |
163 | | |
164 | | static int hmac_init(void *vmacctx, const unsigned char *key, |
165 | | size_t keylen, const OSSL_PARAM params[]) |
166 | 118k | { |
167 | 118k | struct hmac_data_st *macctx = vmacctx; |
168 | | |
169 | 118k | if (!ossl_prov_is_running() || !hmac_set_ctx_params(macctx, params)) |
170 | 57 | return 0; |
171 | | |
172 | 118k | if (key != NULL) |
173 | 118k | return hmac_setkey(macctx, key, keylen); |
174 | | |
175 | | /* Just reinit the HMAC context */ |
176 | 0 | return HMAC_Init_ex(macctx->ctx, NULL, 0, NULL, NULL); |
177 | 118k | } |
178 | | |
179 | | static int hmac_update(void *vmacctx, const unsigned char *data, |
180 | | size_t datalen) |
181 | 636k | { |
182 | 636k | struct hmac_data_st *macctx = vmacctx; |
183 | | |
184 | 636k | if (macctx->tls_data_size > 0) { |
185 | | /* We're doing a TLS HMAC */ |
186 | 75.0k | if (!macctx->tls_header_set) { |
187 | | /* We expect the first update call to contain the TLS header */ |
188 | 37.5k | if (datalen != sizeof(macctx->tls_header)) |
189 | 10 | return 0; |
190 | 37.5k | memcpy(macctx->tls_header, data, datalen); |
191 | 37.5k | macctx->tls_header_set = 1; |
192 | 37.5k | return 1; |
193 | 37.5k | } |
194 | | /* macctx->tls_data_size is datalen plus the padding length */ |
195 | 37.5k | if (macctx->tls_data_size < datalen) |
196 | 0 | return 0; |
197 | | |
198 | 37.5k | return ssl3_cbc_digest_record(ossl_prov_digest_md(&macctx->digest), |
199 | 37.5k | macctx->tls_mac_out, |
200 | 37.5k | &macctx->tls_mac_out_size, |
201 | 37.5k | macctx->tls_header, |
202 | 37.5k | data, |
203 | 37.5k | datalen, |
204 | 37.5k | macctx->tls_data_size, |
205 | 37.5k | macctx->key, |
206 | 37.5k | macctx->keylen, |
207 | 37.5k | 0); |
208 | 37.5k | } |
209 | | |
210 | 561k | return HMAC_Update(macctx->ctx, data, datalen); |
211 | 636k | } |
212 | | |
213 | | static int hmac_final(void *vmacctx, unsigned char *out, size_t *outl, |
214 | | size_t outsize) |
215 | 424k | { |
216 | 424k | unsigned int hlen; |
217 | 424k | struct hmac_data_st *macctx = vmacctx; |
218 | | |
219 | 424k | if (!ossl_prov_is_running()) |
220 | 0 | return 0; |
221 | 424k | if (macctx->tls_data_size > 0) { |
222 | 37.5k | if (macctx->tls_mac_out_size == 0) |
223 | 0 | return 0; |
224 | 37.5k | if (outl != NULL) |
225 | 37.5k | *outl = macctx->tls_mac_out_size; |
226 | 37.5k | memcpy(out, macctx->tls_mac_out, macctx->tls_mac_out_size); |
227 | 37.5k | return 1; |
228 | 37.5k | } |
229 | 386k | if (!HMAC_Final(macctx->ctx, out, &hlen)) |
230 | 0 | return 0; |
231 | 386k | *outl = hlen; |
232 | 386k | return 1; |
233 | 386k | } |
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 | 0 | { |
243 | 0 | return known_gettable_ctx_params; |
244 | 0 | } |
245 | | |
246 | | static int hmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[]) |
247 | 460k | { |
248 | 460k | struct hmac_data_st *macctx = vmacctx; |
249 | 460k | OSSL_PARAM *p; |
250 | | |
251 | 460k | if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL |
252 | 460k | && !OSSL_PARAM_set_size_t(p, hmac_size(macctx))) |
253 | 0 | return 0; |
254 | | |
255 | 460k | if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_BLOCK_SIZE)) != NULL |
256 | 460k | && !OSSL_PARAM_set_int(p, hmac_block_size(macctx))) |
257 | 0 | return 0; |
258 | | |
259 | 460k | return 1; |
260 | 460k | } |
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 | 70.9k | { |
274 | 70.9k | return known_settable_ctx_params; |
275 | 70.9k | } |
276 | | |
277 | | static int set_flag(const OSSL_PARAM params[], const char *key, int mask, |
278 | | int *flags) |
279 | 156k | { |
280 | 156k | const OSSL_PARAM *p = OSSL_PARAM_locate_const(params, key); |
281 | 156k | int flag = 0; |
282 | | |
283 | 156k | if (p != NULL) { |
284 | 0 | if (!OSSL_PARAM_get_int(p, &flag)) |
285 | 0 | return 0; |
286 | 0 | if (flag == 0) |
287 | 0 | *flags &= ~mask; |
288 | 0 | else |
289 | 0 | *flags |= mask; |
290 | 0 | } |
291 | 156k | return 1; |
292 | 156k | } |
293 | | |
294 | | /* |
295 | | * ALL parameters should be set before init(). |
296 | | */ |
297 | | static int hmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[]) |
298 | 163k | { |
299 | 163k | struct hmac_data_st *macctx = vmacctx; |
300 | 163k | OSSL_LIB_CTX *ctx = PROV_LIBCTX_OF(macctx->provctx); |
301 | 163k | const OSSL_PARAM *p; |
302 | 163k | int flags = 0; |
303 | | |
304 | 163k | if (params == NULL) |
305 | 85.1k | return 1; |
306 | | |
307 | 78.1k | if (!ossl_prov_digest_load_from_params(&macctx->digest, params, ctx)) |
308 | 3 | return 0; |
309 | | |
310 | 78.1k | if (!set_flag(params, OSSL_MAC_PARAM_DIGEST_NOINIT, EVP_MD_CTX_FLAG_NO_INIT, |
311 | 78.1k | &flags)) |
312 | 0 | return 0; |
313 | 78.1k | if (!set_flag(params, OSSL_MAC_PARAM_DIGEST_ONESHOT, EVP_MD_CTX_FLAG_ONESHOT, |
314 | 78.1k | &flags)) |
315 | 0 | return 0; |
316 | 78.1k | if (flags) |
317 | 0 | HMAC_CTX_set_flags(macctx->ctx, flags); |
318 | | |
319 | 78.1k | if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) { |
320 | 0 | if (p->data_type != OSSL_PARAM_OCTET_STRING) |
321 | 0 | return 0; |
322 | 0 | if (!hmac_setkey(macctx, p->data, p->data_size)) |
323 | 0 | return 0; |
324 | 0 | } |
325 | | |
326 | 78.1k | if ((p = OSSL_PARAM_locate_const(params, |
327 | 78.1k | OSSL_MAC_PARAM_TLS_DATA_SIZE)) != NULL) { |
328 | 441 | if (!OSSL_PARAM_get_size_t(p, &macctx->tls_data_size)) |
329 | 0 | return 0; |
330 | 441 | } |
331 | 78.1k | return 1; |
332 | 78.1k | } |
333 | | |
334 | | const OSSL_DISPATCH ossl_hmac_functions[] = { |
335 | | { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))hmac_new }, |
336 | | { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))hmac_dup }, |
337 | | { OSSL_FUNC_MAC_FREECTX, (void (*)(void))hmac_free }, |
338 | | { OSSL_FUNC_MAC_INIT, (void (*)(void))hmac_init }, |
339 | | { OSSL_FUNC_MAC_UPDATE, (void (*)(void))hmac_update }, |
340 | | { OSSL_FUNC_MAC_FINAL, (void (*)(void))hmac_final }, |
341 | | { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS, |
342 | | (void (*)(void))hmac_gettable_ctx_params }, |
343 | | { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))hmac_get_ctx_params }, |
344 | | { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS, |
345 | | (void (*)(void))hmac_settable_ctx_params }, |
346 | | { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))hmac_set_ctx_params }, |
347 | | OSSL_DISPATCH_END |
348 | | }; |