/src/openssl34/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 | | #include <openssl/proverr.h> |
24 | | #include <openssl/err.h> |
25 | | |
26 | | #include "internal/ssl3_cbc.h" |
27 | | |
28 | | #include "prov/implementations.h" |
29 | | #include "prov/provider_ctx.h" |
30 | | #include "prov/provider_util.h" |
31 | | #include "prov/providercommon.h" |
32 | | #include "prov/securitycheck.h" |
33 | | |
34 | | /* |
35 | | * Forward declaration of everything implemented here. This is not strictly |
36 | | * necessary for the compiler, but provides an assurance that the signatures |
37 | | * of the functions in the dispatch table are correct. |
38 | | */ |
39 | | static OSSL_FUNC_mac_newctx_fn hmac_new; |
40 | | static OSSL_FUNC_mac_dupctx_fn hmac_dup; |
41 | | static OSSL_FUNC_mac_freectx_fn hmac_free; |
42 | | static OSSL_FUNC_mac_gettable_ctx_params_fn hmac_gettable_ctx_params; |
43 | | static OSSL_FUNC_mac_get_ctx_params_fn hmac_get_ctx_params; |
44 | | static OSSL_FUNC_mac_settable_ctx_params_fn hmac_settable_ctx_params; |
45 | | static OSSL_FUNC_mac_set_ctx_params_fn hmac_set_ctx_params; |
46 | | static OSSL_FUNC_mac_init_fn hmac_init; |
47 | | static OSSL_FUNC_mac_update_fn hmac_update; |
48 | | static OSSL_FUNC_mac_final_fn hmac_final; |
49 | | |
50 | | /* local HMAC context structure */ |
51 | | |
52 | | /* typedef EVP_MAC_IMPL */ |
53 | | struct hmac_data_st { |
54 | | void *provctx; |
55 | | HMAC_CTX *ctx; /* HMAC context */ |
56 | | PROV_DIGEST digest; |
57 | | unsigned char *key; |
58 | | size_t keylen; |
59 | | /* Length of full TLS record including the MAC and any padding */ |
60 | | size_t tls_data_size; |
61 | | unsigned char tls_header[13]; |
62 | | int tls_header_set; |
63 | | unsigned char tls_mac_out[EVP_MAX_MD_SIZE]; |
64 | | size_t tls_mac_out_size; |
65 | | #ifdef FIPS_MODULE |
66 | | /* |
67 | | * 'internal' is set to 1 if HMAC is used inside another algorithm such as a |
68 | | * KDF. In this case it is the parent algorithm that is responsible for |
69 | | * performing any conditional FIPS indicator related checks for the HMAC. |
70 | | */ |
71 | | int internal; |
72 | | #endif |
73 | | OSSL_FIPS_IND_DECLARE |
74 | | }; |
75 | | |
76 | | static void *hmac_new(void *provctx) |
77 | 1.57M | { |
78 | 1.57M | struct hmac_data_st *macctx; |
79 | | |
80 | 1.57M | if (!ossl_prov_is_running()) |
81 | 0 | return NULL; |
82 | | |
83 | 1.57M | if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL |
84 | 1.57M | || (macctx->ctx = HMAC_CTX_new()) == NULL) { |
85 | 0 | OPENSSL_free(macctx); |
86 | 0 | return NULL; |
87 | 0 | } |
88 | 1.57M | macctx->provctx = provctx; |
89 | 1.57M | OSSL_FIPS_IND_INIT(macctx) |
90 | | |
91 | 1.57M | return macctx; |
92 | 1.57M | } |
93 | | |
94 | | static void hmac_free(void *vmacctx) |
95 | 1.57M | { |
96 | 1.57M | struct hmac_data_st *macctx = vmacctx; |
97 | | |
98 | 1.57M | if (macctx != NULL) { |
99 | 1.57M | HMAC_CTX_free(macctx->ctx); |
100 | 1.57M | ossl_prov_digest_reset(&macctx->digest); |
101 | 1.57M | OPENSSL_clear_free(macctx->key, macctx->keylen); |
102 | 1.57M | OPENSSL_free(macctx); |
103 | 1.57M | } |
104 | 1.57M | } |
105 | | |
106 | | static void *hmac_dup(void *vsrc) |
107 | 1.32M | { |
108 | 1.32M | struct hmac_data_st *src = vsrc; |
109 | 1.32M | struct hmac_data_st *dst; |
110 | 1.32M | HMAC_CTX *ctx; |
111 | | |
112 | 1.32M | if (!ossl_prov_is_running()) |
113 | 0 | return NULL; |
114 | 1.32M | dst = hmac_new(src->provctx); |
115 | 1.32M | if (dst == NULL) |
116 | 0 | return NULL; |
117 | | |
118 | 1.32M | ctx = dst->ctx; |
119 | 1.32M | *dst = *src; |
120 | 1.32M | dst->ctx = ctx; |
121 | 1.32M | dst->key = NULL; |
122 | 1.32M | memset(&dst->digest, 0, sizeof(dst->digest)); |
123 | | |
124 | 1.32M | if (!HMAC_CTX_copy(dst->ctx, src->ctx) |
125 | 1.32M | || !ossl_prov_digest_copy(&dst->digest, &src->digest)) { |
126 | 0 | hmac_free(dst); |
127 | 0 | return NULL; |
128 | 0 | } |
129 | 1.32M | if (src->key != NULL) { |
130 | 1.32M | dst->key = OPENSSL_malloc(src->keylen > 0 ? src->keylen : 1); |
131 | 1.32M | if (dst->key == NULL) { |
132 | 0 | hmac_free(dst); |
133 | 0 | return 0; |
134 | 0 | } |
135 | 1.32M | if (src->keylen > 0) |
136 | 1.32M | memcpy(dst->key, src->key, src->keylen); |
137 | 1.32M | } |
138 | 1.32M | return dst; |
139 | 1.32M | } |
140 | | |
141 | | static size_t hmac_size(struct hmac_data_st *macctx) |
142 | 1.12M | { |
143 | 1.12M | return HMAC_size(macctx->ctx); |
144 | 1.12M | } |
145 | | |
146 | | static int hmac_block_size(struct hmac_data_st *macctx) |
147 | 0 | { |
148 | 0 | const EVP_MD *md = ossl_prov_digest_md(&macctx->digest); |
149 | |
|
150 | 0 | if (md == NULL) |
151 | 0 | return 0; |
152 | 0 | return EVP_MD_block_size(md); |
153 | 0 | } |
154 | | |
155 | | static int hmac_setkey(struct hmac_data_st *macctx, |
156 | | const unsigned char *key, size_t keylen) |
157 | 266k | { |
158 | 266k | const EVP_MD *digest; |
159 | | |
160 | | #ifdef FIPS_MODULE |
161 | | /* |
162 | | * KDF's pass a salt rather than a key, |
163 | | * which is why it skips the key check unless "HMAC" is fetched directly. |
164 | | */ |
165 | | if (!macctx->internal) { |
166 | | OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(macctx->provctx); |
167 | | int approved = ossl_mac_check_key_size(keylen); |
168 | | |
169 | | if (!approved) { |
170 | | if (!OSSL_FIPS_IND_ON_UNAPPROVED(macctx, OSSL_FIPS_IND_SETTABLE0, |
171 | | libctx, "HMAC", "keysize", |
172 | | ossl_fips_config_hmac_key_check)) { |
173 | | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
174 | | return 0; |
175 | | } |
176 | | } |
177 | | } |
178 | | #endif |
179 | | |
180 | 266k | if (macctx->key != NULL) |
181 | 15.1k | OPENSSL_clear_free(macctx->key, macctx->keylen); |
182 | | /* Keep a copy of the key in case we need it for TLS HMAC */ |
183 | 266k | macctx->key = OPENSSL_malloc(keylen > 0 ? keylen : 1); |
184 | 266k | if (macctx->key == NULL) |
185 | 0 | return 0; |
186 | | |
187 | 266k | if (keylen > 0) |
188 | 254k | memcpy(macctx->key, key, keylen); |
189 | 266k | macctx->keylen = keylen; |
190 | | |
191 | 266k | digest = ossl_prov_digest_md(&macctx->digest); |
192 | | /* HMAC_Init_ex doesn't tolerate all zero params, so we must be careful */ |
193 | 266k | if (key != NULL || (macctx->tls_data_size == 0 && digest != NULL)) |
194 | 266k | return HMAC_Init_ex(macctx->ctx, key, keylen, digest, |
195 | 266k | ossl_prov_digest_engine(&macctx->digest)); |
196 | 0 | return 1; |
197 | 266k | } |
198 | | |
199 | | static int hmac_init(void *vmacctx, const unsigned char *key, |
200 | | size_t keylen, const OSSL_PARAM params[]) |
201 | 266k | { |
202 | 266k | struct hmac_data_st *macctx = vmacctx; |
203 | | |
204 | 266k | if (!ossl_prov_is_running() || !hmac_set_ctx_params(macctx, params)) |
205 | 106 | return 0; |
206 | | |
207 | 266k | if (key != NULL) |
208 | 266k | return hmac_setkey(macctx, key, keylen); |
209 | | |
210 | | /* Just reinit the HMAC context */ |
211 | 0 | return HMAC_Init_ex(macctx->ctx, NULL, 0, NULL, NULL); |
212 | 266k | } |
213 | | |
214 | | static int hmac_update(void *vmacctx, const unsigned char *data, |
215 | | size_t datalen) |
216 | 1.57M | { |
217 | 1.57M | struct hmac_data_st *macctx = vmacctx; |
218 | | |
219 | 1.57M | if (macctx->tls_data_size > 0) { |
220 | | /* We're doing a TLS HMAC */ |
221 | 274k | if (!macctx->tls_header_set) { |
222 | | /* We expect the first update call to contain the TLS header */ |
223 | 137k | if (datalen != sizeof(macctx->tls_header)) |
224 | 90 | return 0; |
225 | 137k | memcpy(macctx->tls_header, data, datalen); |
226 | 137k | macctx->tls_header_set = 1; |
227 | 137k | return 1; |
228 | 137k | } |
229 | | /* macctx->tls_data_size is datalen plus the padding length */ |
230 | 137k | if (macctx->tls_data_size < datalen) |
231 | 0 | return 0; |
232 | | |
233 | 137k | return ssl3_cbc_digest_record(ossl_prov_digest_md(&macctx->digest), |
234 | 137k | macctx->tls_mac_out, |
235 | 137k | &macctx->tls_mac_out_size, |
236 | 137k | macctx->tls_header, |
237 | 137k | data, |
238 | 137k | datalen, |
239 | 137k | macctx->tls_data_size, |
240 | 137k | macctx->key, |
241 | 137k | macctx->keylen, |
242 | 137k | 0); |
243 | 137k | } |
244 | | |
245 | 1.29M | return HMAC_Update(macctx->ctx, data, datalen); |
246 | 1.57M | } |
247 | | |
248 | | static int hmac_final(void *vmacctx, unsigned char *out, size_t *outl, |
249 | | size_t outsize) |
250 | 1.03M | { |
251 | 1.03M | unsigned int hlen; |
252 | 1.03M | struct hmac_data_st *macctx = vmacctx; |
253 | | |
254 | 1.03M | if (!ossl_prov_is_running()) |
255 | 0 | return 0; |
256 | 1.03M | if (macctx->tls_data_size > 0) { |
257 | 137k | if (macctx->tls_mac_out_size == 0) |
258 | 0 | return 0; |
259 | 137k | if (outl != NULL) |
260 | 137k | *outl = macctx->tls_mac_out_size; |
261 | 137k | memcpy(out, macctx->tls_mac_out, macctx->tls_mac_out_size); |
262 | 137k | return 1; |
263 | 137k | } |
264 | 901k | if (!HMAC_Final(macctx->ctx, out, &hlen)) |
265 | 0 | return 0; |
266 | 901k | *outl = hlen; |
267 | 901k | return 1; |
268 | 901k | } |
269 | | |
270 | | static const OSSL_PARAM known_gettable_ctx_params[] = { |
271 | | OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL), |
272 | | OSSL_PARAM_size_t(OSSL_MAC_PARAM_BLOCK_SIZE, NULL), |
273 | | OSSL_FIPS_IND_GETTABLE_CTX_PARAM() |
274 | | OSSL_PARAM_END |
275 | | }; |
276 | | static const OSSL_PARAM *hmac_gettable_ctx_params(ossl_unused void *ctx, |
277 | | ossl_unused void *provctx) |
278 | 0 | { |
279 | 0 | return known_gettable_ctx_params; |
280 | 0 | } |
281 | | |
282 | | static int hmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[]) |
283 | 874k | { |
284 | 874k | struct hmac_data_st *macctx = vmacctx; |
285 | 874k | OSSL_PARAM *p; |
286 | | |
287 | 874k | if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL |
288 | 874k | && !OSSL_PARAM_set_size_t(p, hmac_size(macctx))) |
289 | 0 | return 0; |
290 | | |
291 | 874k | if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_BLOCK_SIZE)) != NULL |
292 | 0 | && !OSSL_PARAM_set_int(p, hmac_block_size(macctx))) |
293 | 0 | return 0; |
294 | | |
295 | | #ifdef FIPS_MODULE |
296 | | p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_FIPS_APPROVED_INDICATOR); |
297 | | if (p != NULL) { |
298 | | int approved = 0; |
299 | | |
300 | | if (!macctx->internal) |
301 | | approved = OSSL_FIPS_IND_GET(macctx)->approved; |
302 | | if (!OSSL_PARAM_set_int(p, approved)) |
303 | | return 0; |
304 | | } |
305 | | #endif |
306 | 874k | return 1; |
307 | 874k | } |
308 | | |
309 | | static const OSSL_PARAM known_settable_ctx_params[] = { |
310 | | OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST, NULL, 0), |
311 | | OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0), |
312 | | OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0), |
313 | | OSSL_PARAM_int(OSSL_MAC_PARAM_DIGEST_NOINIT, NULL), |
314 | | OSSL_PARAM_int(OSSL_MAC_PARAM_DIGEST_ONESHOT, NULL), |
315 | | OSSL_PARAM_size_t(OSSL_MAC_PARAM_TLS_DATA_SIZE, NULL), |
316 | | OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_MAC_PARAM_FIPS_KEY_CHECK) |
317 | | OSSL_PARAM_END |
318 | | }; |
319 | | static const OSSL_PARAM *hmac_settable_ctx_params(ossl_unused void *ctx, |
320 | | ossl_unused void *provctx) |
321 | 149k | { |
322 | 149k | return known_settable_ctx_params; |
323 | 149k | } |
324 | | |
325 | | /* |
326 | | * ALL parameters should be set before init(). |
327 | | */ |
328 | | static int hmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[]) |
329 | 153k | { |
330 | 153k | struct hmac_data_st *macctx = vmacctx; |
331 | 153k | OSSL_LIB_CTX *ctx = PROV_LIBCTX_OF(macctx->provctx); |
332 | 153k | const OSSL_PARAM *p; |
333 | | |
334 | 153k | if (params == NULL) |
335 | 67.8k | return 1; |
336 | | |
337 | 85.7k | if (!OSSL_FIPS_IND_SET_CTX_PARAM(macctx, OSSL_FIPS_IND_SETTABLE0, params, |
338 | 85.7k | OSSL_MAC_PARAM_FIPS_KEY_CHECK)) |
339 | 0 | return 0; |
340 | | |
341 | 85.7k | if (!ossl_prov_digest_load_from_params(&macctx->digest, params, ctx)) |
342 | 430 | return 0; |
343 | | |
344 | 85.3k | if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) { |
345 | 235 | if (p->data_type != OSSL_PARAM_OCTET_STRING) |
346 | 0 | return 0; |
347 | | |
348 | 235 | if (!hmac_setkey(macctx, p->data, p->data_size)) |
349 | 1 | return 0; |
350 | 235 | } |
351 | | |
352 | 85.3k | if ((p = OSSL_PARAM_locate_const(params, |
353 | 85.3k | OSSL_MAC_PARAM_TLS_DATA_SIZE)) != NULL) { |
354 | 28.7k | if (!OSSL_PARAM_get_size_t(p, &macctx->tls_data_size)) |
355 | 0 | return 0; |
356 | 28.7k | } |
357 | 85.3k | return 1; |
358 | 85.3k | } |
359 | | |
360 | | const OSSL_DISPATCH ossl_hmac_functions[] = { |
361 | | { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))hmac_new }, |
362 | | { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))hmac_dup }, |
363 | | { OSSL_FUNC_MAC_FREECTX, (void (*)(void))hmac_free }, |
364 | | { OSSL_FUNC_MAC_INIT, (void (*)(void))hmac_init }, |
365 | | { OSSL_FUNC_MAC_UPDATE, (void (*)(void))hmac_update }, |
366 | | { OSSL_FUNC_MAC_FINAL, (void (*)(void))hmac_final }, |
367 | | { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS, |
368 | | (void (*)(void))hmac_gettable_ctx_params }, |
369 | | { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))hmac_get_ctx_params }, |
370 | | { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS, |
371 | | (void (*)(void))hmac_settable_ctx_params }, |
372 | | { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))hmac_set_ctx_params }, |
373 | | OSSL_DISPATCH_END |
374 | | }; |
375 | | |
376 | | #ifdef FIPS_MODULE |
377 | | static OSSL_FUNC_mac_newctx_fn hmac_internal_new; |
378 | | |
379 | | static void *hmac_internal_new(void *provctx) |
380 | | { |
381 | | struct hmac_data_st *macctx = hmac_new(provctx); |
382 | | |
383 | | if (macctx != NULL) |
384 | | macctx->internal = 1; |
385 | | return macctx; |
386 | | } |
387 | | |
388 | | const OSSL_DISPATCH ossl_hmac_internal_functions[] = { |
389 | | { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))hmac_internal_new }, |
390 | | { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))hmac_dup }, |
391 | | { OSSL_FUNC_MAC_FREECTX, (void (*)(void))hmac_free }, |
392 | | { OSSL_FUNC_MAC_INIT, (void (*)(void))hmac_init }, |
393 | | { OSSL_FUNC_MAC_UPDATE, (void (*)(void))hmac_update }, |
394 | | { OSSL_FUNC_MAC_FINAL, (void (*)(void))hmac_final }, |
395 | | { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS, |
396 | | (void (*)(void))hmac_gettable_ctx_params }, |
397 | | { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))hmac_get_ctx_params }, |
398 | | { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS, |
399 | | (void (*)(void))hmac_settable_ctx_params }, |
400 | | { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))hmac_set_ctx_params }, |
401 | | OSSL_DISPATCH_END |
402 | | }; |
403 | | |
404 | | #endif /* FIPS_MODULE */ |