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