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