]>
Commit | Line | Data |
---|---|---|
5183ebdc | 1 | /* |
da1c088f | 2 | * Copyright 2018-2023 The OpenSSL Project Authors. All Rights Reserved. |
5183ebdc RL |
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 | ||
dbde4726 P |
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 | ||
3fddbb26 MC |
16 | #include <string.h> |
17 | ||
23c48d94 | 18 | #include <openssl/core_dispatch.h> |
5183ebdc RL |
19 | #include <openssl/core_names.h> |
20 | #include <openssl/params.h> | |
5183ebdc RL |
21 | #include <openssl/evp.h> |
22 | #include <openssl/hmac.h> | |
23 | ||
25624c90 TM |
24 | #include "internal/ssl3_cbc.h" |
25 | ||
af3e7e1b | 26 | #include "prov/implementations.h" |
ddd21319 RL |
27 | #include "prov/provider_ctx.h" |
28 | #include "prov/provider_util.h" | |
5b104a81 | 29 | #include "prov/providercommon.h" |
5183ebdc RL |
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 | */ | |
363b1e5d DMSP |
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; | |
5183ebdc RL |
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 */ | |
103d8b0b | 53 | PROV_DIGEST digest; |
3fddbb26 MC |
54 | unsigned char *key; |
55 | size_t keylen; | |
2e2084da | 56 | /* Length of full TLS record including the MAC and any padding */ |
3fddbb26 MC |
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; | |
5183ebdc RL |
62 | }; |
63 | ||
5183ebdc RL |
64 | static void *hmac_new(void *provctx) |
65 | { | |
66 | struct hmac_data_st *macctx; | |
67 | ||
5b104a81 P |
68 | if (!ossl_prov_is_running()) |
69 | return NULL; | |
70 | ||
5183ebdc RL |
71 | if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL |
72 | || (macctx->ctx = HMAC_CTX_new()) == NULL) { | |
73 | OPENSSL_free(macctx); | |
74 | return NULL; | |
75 | } | |
5183ebdc RL |
76 | macctx->provctx = provctx; |
77 | ||
78 | return macctx; | |
79 | } | |
80 | ||
81 | static void hmac_free(void *vmacctx) | |
82 | { | |
83 | struct hmac_data_st *macctx = vmacctx; | |
84 | ||
85 | if (macctx != NULL) { | |
86 | HMAC_CTX_free(macctx->ctx); | |
103d8b0b | 87 | ossl_prov_digest_reset(&macctx->digest); |
3fddbb26 | 88 | OPENSSL_secure_clear_free(macctx->key, macctx->keylen); |
5183ebdc RL |
89 | OPENSSL_free(macctx); |
90 | } | |
91 | } | |
92 | ||
93 | static void *hmac_dup(void *vsrc) | |
94 | { | |
95 | struct hmac_data_st *src = vsrc; | |
5b104a81 | 96 | struct hmac_data_st *dst; |
3fddbb26 | 97 | HMAC_CTX *ctx; |
5183ebdc | 98 | |
5b104a81 P |
99 | if (!ossl_prov_is_running()) |
100 | return NULL; | |
101 | dst = hmac_new(src->provctx); | |
5183ebdc RL |
102 | if (dst == NULL) |
103 | return NULL; | |
104 | ||
3fddbb26 MC |
105 | ctx = dst->ctx; |
106 | *dst = *src; | |
107 | dst->ctx = ctx; | |
108 | dst->key = NULL; | |
cec1699f | 109 | memset(&dst->digest, 0, sizeof(dst->digest)); |
3fddbb26 | 110 | |
103d8b0b P |
111 | if (!HMAC_CTX_copy(dst->ctx, src->ctx) |
112 | || !ossl_prov_digest_copy(&dst->digest, &src->digest)) { | |
5183ebdc RL |
113 | hmac_free(dst); |
114 | return NULL; | |
115 | } | |
3fddbb26 MC |
116 | if (src->key != NULL) { |
117 | /* There is no "secure" OPENSSL_memdup */ | |
00108705 | 118 | dst->key = OPENSSL_secure_malloc(src->keylen > 0 ? src->keylen : 1); |
3fddbb26 MC |
119 | if (dst->key == NULL) { |
120 | hmac_free(dst); | |
121 | return 0; | |
122 | } | |
123 | memcpy(dst->key, src->key, src->keylen); | |
124 | } | |
5183ebdc RL |
125 | return dst; |
126 | } | |
127 | ||
eb1b66f0 | 128 | static size_t hmac_size(struct hmac_data_st *macctx) |
5183ebdc | 129 | { |
5183ebdc RL |
130 | return HMAC_size(macctx->ctx); |
131 | } | |
132 | ||
eb1b66f0 P |
133 | static int hmac_block_size(struct hmac_data_st *macctx) |
134 | { | |
135 | const EVP_MD *md = ossl_prov_digest_md(&macctx->digest); | |
136 | ||
137 | if (md == NULL) | |
138 | return 0; | |
139 | return EVP_MD_block_size(md); | |
140 | } | |
141 | ||
c23f96f3 P |
142 | static int hmac_setkey(struct hmac_data_st *macctx, |
143 | const unsigned char *key, size_t keylen) | |
5183ebdc | 144 | { |
5b104a81 | 145 | const EVP_MD *digest; |
5183ebdc | 146 | |
c9ddc5af | 147 | if (macctx->key != NULL) |
c23f96f3 P |
148 | OPENSSL_secure_clear_free(macctx->key, macctx->keylen); |
149 | /* Keep a copy of the key in case we need it for TLS HMAC */ | |
150 | macctx->key = OPENSSL_secure_malloc(keylen > 0 ? keylen : 1); | |
151 | if (macctx->key == NULL) | |
5b104a81 | 152 | return 0; |
c23f96f3 P |
153 | memcpy(macctx->key, key, keylen); |
154 | macctx->keylen = keylen; | |
5b104a81 P |
155 | |
156 | digest = ossl_prov_digest_md(&macctx->digest); | |
5183ebdc | 157 | /* HMAC_Init_ex doesn't tolerate all zero params, so we must be careful */ |
c23f96f3 P |
158 | if (key != NULL || (macctx->tls_data_size == 0 && digest != NULL)) |
159 | return HMAC_Init_ex(macctx->ctx, key, keylen, digest, | |
160 | ossl_prov_digest_engine(&macctx->digest)); | |
161 | return 1; | |
162 | } | |
163 | ||
164 | static int hmac_init(void *vmacctx, const unsigned char *key, | |
165 | size_t keylen, const OSSL_PARAM params[]) | |
166 | { | |
167 | struct hmac_data_st *macctx = vmacctx; | |
3fddbb26 | 168 | |
c23f96f3 P |
169 | if (!ossl_prov_is_running() || !hmac_set_ctx_params(macctx, params)) |
170 | return 0; | |
171 | ||
c9ddc5af TM |
172 | if (key != NULL) |
173 | return hmac_setkey(macctx, key, keylen); | |
174 | ||
175 | /* Just reinit the HMAC context */ | |
176 | return HMAC_Init_ex(macctx->ctx, NULL, 0, NULL, NULL); | |
5183ebdc RL |
177 | } |
178 | ||
179 | static int hmac_update(void *vmacctx, const unsigned char *data, | |
180 | size_t datalen) | |
181 | { | |
182 | struct hmac_data_st *macctx = vmacctx; | |
183 | ||
3fddbb26 MC |
184 | if (macctx->tls_data_size > 0) { |
185 | /* We're doing a TLS HMAC */ | |
186 | if (!macctx->tls_header_set) { | |
187 | /* We expect the first update call to contain the TLS header */ | |
188 | if (datalen != sizeof(macctx->tls_header)) | |
189 | return 0; | |
190 | memcpy(macctx->tls_header, data, datalen); | |
191 | macctx->tls_header_set = 1; | |
192 | return 1; | |
193 | } | |
2e2084da MC |
194 | /* macctx->tls_data_size is datalen plus the padding length */ |
195 | if (macctx->tls_data_size < datalen) | |
3fddbb26 MC |
196 | return 0; |
197 | ||
198 | return ssl3_cbc_digest_record(ossl_prov_digest_md(&macctx->digest), | |
199 | macctx->tls_mac_out, | |
200 | &macctx->tls_mac_out_size, | |
201 | macctx->tls_header, | |
202 | data, | |
3fddbb26 | 203 | datalen, |
2e2084da | 204 | macctx->tls_data_size, |
3fddbb26 MC |
205 | macctx->key, |
206 | macctx->keylen, | |
207 | 0); | |
208 | } | |
209 | ||
5183ebdc RL |
210 | return HMAC_Update(macctx->ctx, data, datalen); |
211 | } | |
212 | ||
213 | static int hmac_final(void *vmacctx, unsigned char *out, size_t *outl, | |
214 | size_t outsize) | |
215 | { | |
216 | unsigned int hlen; | |
217 | struct hmac_data_st *macctx = vmacctx; | |
218 | ||
5b104a81 P |
219 | if (!ossl_prov_is_running()) |
220 | return 0; | |
3fddbb26 MC |
221 | if (macctx->tls_data_size > 0) { |
222 | if (macctx->tls_mac_out_size == 0) | |
223 | return 0; | |
224 | if (outl != NULL) | |
225 | *outl = macctx->tls_mac_out_size; | |
226 | memcpy(out, macctx->tls_mac_out, macctx->tls_mac_out_size); | |
227 | return 1; | |
228 | } | |
5183ebdc RL |
229 | if (!HMAC_Final(macctx->ctx, out, &hlen)) |
230 | return 0; | |
5f6a0b2f | 231 | *outl = hlen; |
5183ebdc RL |
232 | return 1; |
233 | } | |
234 | ||
235 | static const OSSL_PARAM known_gettable_ctx_params[] = { | |
703170d4 | 236 | OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL), |
eb1b66f0 | 237 | OSSL_PARAM_size_t(OSSL_MAC_PARAM_BLOCK_SIZE, NULL), |
5183ebdc RL |
238 | OSSL_PARAM_END |
239 | }; | |
eee323c3 P |
240 | static const OSSL_PARAM *hmac_gettable_ctx_params(ossl_unused void *ctx, |
241 | ossl_unused void *provctx) | |
5183ebdc RL |
242 | { |
243 | return known_gettable_ctx_params; | |
244 | } | |
245 | ||
92d9d0ae | 246 | static int hmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[]) |
5183ebdc | 247 | { |
eb1b66f0 | 248 | struct hmac_data_st *macctx = vmacctx; |
5183ebdc RL |
249 | OSSL_PARAM *p; |
250 | ||
eb1b66f0 P |
251 | if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL |
252 | && !OSSL_PARAM_set_size_t(p, hmac_size(macctx))) | |
253 | return 0; | |
254 | ||
255 | if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_BLOCK_SIZE)) != NULL | |
256 | && !OSSL_PARAM_set_int(p, hmac_block_size(macctx))) | |
257 | return 0; | |
5183ebdc RL |
258 | |
259 | return 1; | |
260 | } | |
261 | ||
262 | static const OSSL_PARAM known_settable_ctx_params[] = { | |
5183ebdc | 263 | OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST, NULL, 0), |
5183ebdc RL |
264 | OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0), |
265 | OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0), | |
36978c19 SL |
266 | OSSL_PARAM_int(OSSL_MAC_PARAM_DIGEST_NOINIT, NULL), |
267 | OSSL_PARAM_int(OSSL_MAC_PARAM_DIGEST_ONESHOT, NULL), | |
3fddbb26 | 268 | OSSL_PARAM_size_t(OSSL_MAC_PARAM_TLS_DATA_SIZE, NULL), |
5183ebdc RL |
269 | OSSL_PARAM_END |
270 | }; | |
eee323c3 P |
271 | static const OSSL_PARAM *hmac_settable_ctx_params(ossl_unused void *ctx, |
272 | ossl_unused void *provctx) | |
5183ebdc RL |
273 | { |
274 | return known_settable_ctx_params; | |
275 | } | |
276 | ||
36978c19 SL |
277 | static int set_flag(const OSSL_PARAM params[], const char *key, int mask, |
278 | int *flags) | |
279 | { | |
280 | const OSSL_PARAM *p = OSSL_PARAM_locate_const(params, key); | |
281 | int flag = 0; | |
282 | ||
283 | if (p != NULL) { | |
284 | if (!OSSL_PARAM_get_int(p, &flag)) | |
285 | return 0; | |
286 | if (flag == 0) | |
287 | *flags &= ~mask; | |
288 | else | |
289 | *flags |= mask; | |
290 | } | |
291 | return 1; | |
292 | } | |
293 | ||
5183ebdc RL |
294 | /* |
295 | * ALL parameters should be set before init(). | |
296 | */ | |
92d9d0ae | 297 | static int hmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[]) |
5183ebdc RL |
298 | { |
299 | struct hmac_data_st *macctx = vmacctx; | |
a829b735 | 300 | OSSL_LIB_CTX *ctx = PROV_LIBCTX_OF(macctx->provctx); |
5183ebdc | 301 | const OSSL_PARAM *p; |
36978c19 | 302 | int flags = 0; |
5183ebdc | 303 | |
5a6b62bb P |
304 | if (params == NULL) |
305 | return 1; | |
306 | ||
103d8b0b P |
307 | if (!ossl_prov_digest_load_from_params(&macctx->digest, params, ctx)) |
308 | return 0; | |
5183ebdc | 309 | |
36978c19 SL |
310 | if (!set_flag(params, OSSL_MAC_PARAM_DIGEST_NOINIT, EVP_MD_CTX_FLAG_NO_INIT, |
311 | &flags)) | |
312 | return 0; | |
313 | if (!set_flag(params, OSSL_MAC_PARAM_DIGEST_ONESHOT, EVP_MD_CTX_FLAG_ONESHOT, | |
314 | &flags)) | |
315 | return 0; | |
316 | if (flags) | |
5183ebdc | 317 | HMAC_CTX_set_flags(macctx->ctx, flags); |
36978c19 | 318 | |
5183ebdc RL |
319 | if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) { |
320 | if (p->data_type != OSSL_PARAM_OCTET_STRING) | |
321 | return 0; | |
c9ddc5af | 322 | if (!hmac_setkey(macctx, p->data, p->data_size)) |
5183ebdc | 323 | return 0; |
3fddbb26 | 324 | } |
c9ddc5af | 325 | |
3fddbb26 MC |
326 | if ((p = OSSL_PARAM_locate_const(params, |
327 | OSSL_MAC_PARAM_TLS_DATA_SIZE)) != NULL) { | |
328 | if (!OSSL_PARAM_get_size_t(p, &macctx->tls_data_size)) | |
329 | return 0; | |
5183ebdc RL |
330 | } |
331 | return 1; | |
332 | } | |
333 | ||
1be63951 | 334 | const OSSL_DISPATCH ossl_hmac_functions[] = { |
5183ebdc RL |
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 }, | |
92d9d0ae | 343 | { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))hmac_get_ctx_params }, |
5183ebdc RL |
344 | { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS, |
345 | (void (*)(void))hmac_settable_ctx_params }, | |
92d9d0ae | 346 | { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))hmac_set_ctx_params }, |
1e6bd31e | 347 | OSSL_DISPATCH_END |
5183ebdc | 348 | }; |