]>
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> | |
21 | #include <openssl/engine.h> | |
22 | #include <openssl/evp.h> | |
23 | #include <openssl/hmac.h> | |
24 | ||
af3e7e1b | 25 | #include "prov/implementations.h" |
ddd21319 RL |
26 | #include "prov/provider_ctx.h" |
27 | #include "prov/provider_util.h" | |
5b104a81 | 28 | #include "prov/providercommon.h" |
5183ebdc RL |
29 | |
30 | /* | |
31 | * Forward declaration of everything implemented here. This is not strictly | |
32 | * necessary for the compiler, but provides an assurance that the signatures | |
33 | * of the functions in the dispatch table are correct. | |
34 | */ | |
363b1e5d DMSP |
35 | static OSSL_FUNC_mac_newctx_fn hmac_new; |
36 | static OSSL_FUNC_mac_dupctx_fn hmac_dup; | |
37 | static OSSL_FUNC_mac_freectx_fn hmac_free; | |
38 | static OSSL_FUNC_mac_gettable_ctx_params_fn hmac_gettable_ctx_params; | |
39 | static OSSL_FUNC_mac_get_ctx_params_fn hmac_get_ctx_params; | |
40 | static OSSL_FUNC_mac_settable_ctx_params_fn hmac_settable_ctx_params; | |
41 | static OSSL_FUNC_mac_set_ctx_params_fn hmac_set_ctx_params; | |
42 | static OSSL_FUNC_mac_init_fn hmac_init; | |
43 | static OSSL_FUNC_mac_update_fn hmac_update; | |
44 | static OSSL_FUNC_mac_final_fn hmac_final; | |
5183ebdc RL |
45 | |
46 | /* local HMAC context structure */ | |
47 | ||
48 | /* typedef EVP_MAC_IMPL */ | |
49 | struct hmac_data_st { | |
50 | void *provctx; | |
51 | HMAC_CTX *ctx; /* HMAC context */ | |
103d8b0b | 52 | PROV_DIGEST digest; |
3fddbb26 MC |
53 | unsigned char *key; |
54 | size_t keylen; | |
2e2084da | 55 | /* Length of full TLS record including the MAC and any padding */ |
3fddbb26 MC |
56 | size_t tls_data_size; |
57 | unsigned char tls_header[13]; | |
58 | int tls_header_set; | |
59 | unsigned char tls_mac_out[EVP_MAX_MD_SIZE]; | |
60 | size_t tls_mac_out_size; | |
5183ebdc RL |
61 | }; |
62 | ||
3fddbb26 MC |
63 | /* Defined in ssl/s3_cbc.c */ |
64 | int ssl3_cbc_digest_record(const EVP_MD *md, | |
65 | unsigned char *md_out, | |
66 | size_t *md_out_size, | |
67 | const unsigned char header[13], | |
68 | const unsigned char *data, | |
e08f86dd | 69 | size_t data_size, |
3fddbb26 MC |
70 | size_t data_plus_mac_plus_padding_size, |
71 | const unsigned char *mac_secret, | |
72 | size_t mac_secret_length, char is_sslv3); | |
73 | ||
5183ebdc RL |
74 | static void *hmac_new(void *provctx) |
75 | { | |
76 | struct hmac_data_st *macctx; | |
77 | ||
5b104a81 P |
78 | if (!ossl_prov_is_running()) |
79 | return NULL; | |
80 | ||
5183ebdc RL |
81 | if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL |
82 | || (macctx->ctx = HMAC_CTX_new()) == NULL) { | |
83 | OPENSSL_free(macctx); | |
84 | return NULL; | |
85 | } | |
5183ebdc RL |
86 | macctx->provctx = provctx; |
87 | ||
88 | return macctx; | |
89 | } | |
90 | ||
91 | static void hmac_free(void *vmacctx) | |
92 | { | |
93 | struct hmac_data_st *macctx = vmacctx; | |
94 | ||
95 | if (macctx != NULL) { | |
96 | HMAC_CTX_free(macctx->ctx); | |
103d8b0b | 97 | ossl_prov_digest_reset(&macctx->digest); |
3fddbb26 | 98 | OPENSSL_secure_clear_free(macctx->key, macctx->keylen); |
5183ebdc RL |
99 | OPENSSL_free(macctx); |
100 | } | |
101 | } | |
102 | ||
103 | static void *hmac_dup(void *vsrc) | |
104 | { | |
105 | struct hmac_data_st *src = vsrc; | |
5b104a81 | 106 | struct hmac_data_st *dst; |
3fddbb26 | 107 | HMAC_CTX *ctx; |
5183ebdc | 108 | |
5b104a81 P |
109 | if (!ossl_prov_is_running()) |
110 | return NULL; | |
111 | dst = hmac_new(src->provctx); | |
5183ebdc RL |
112 | if (dst == NULL) |
113 | return NULL; | |
114 | ||
3fddbb26 MC |
115 | ctx = dst->ctx; |
116 | *dst = *src; | |
117 | dst->ctx = ctx; | |
118 | dst->key = NULL; | |
119 | ||
103d8b0b P |
120 | if (!HMAC_CTX_copy(dst->ctx, src->ctx) |
121 | || !ossl_prov_digest_copy(&dst->digest, &src->digest)) { | |
5183ebdc RL |
122 | hmac_free(dst); |
123 | return NULL; | |
124 | } | |
3fddbb26 MC |
125 | if (src->key != NULL) { |
126 | /* There is no "secure" OPENSSL_memdup */ | |
00108705 | 127 | dst->key = OPENSSL_secure_malloc(src->keylen > 0 ? src->keylen : 1); |
3fddbb26 MC |
128 | if (dst->key == NULL) { |
129 | hmac_free(dst); | |
130 | return 0; | |
131 | } | |
132 | memcpy(dst->key, src->key, src->keylen); | |
133 | } | |
5183ebdc RL |
134 | return dst; |
135 | } | |
136 | ||
137 | static size_t hmac_size(void *vmacctx) | |
138 | { | |
139 | struct hmac_data_st *macctx = vmacctx; | |
140 | ||
141 | return HMAC_size(macctx->ctx); | |
142 | } | |
143 | ||
144 | static int hmac_init(void *vmacctx) | |
145 | { | |
146 | struct hmac_data_st *macctx = vmacctx; | |
5b104a81 | 147 | const EVP_MD *digest; |
5183ebdc RL |
148 | int rv = 1; |
149 | ||
5b104a81 P |
150 | if (!ossl_prov_is_running()) |
151 | return 0; | |
152 | ||
153 | digest = ossl_prov_digest_md(&macctx->digest); | |
5183ebdc | 154 | /* HMAC_Init_ex doesn't tolerate all zero params, so we must be careful */ |
3fddbb26 | 155 | if (macctx->tls_data_size == 0 && digest != NULL) |
103d8b0b P |
156 | rv = HMAC_Init_ex(macctx->ctx, NULL, 0, digest, |
157 | ossl_prov_digest_engine(&macctx->digest)); | |
3fddbb26 | 158 | |
5183ebdc RL |
159 | return rv; |
160 | } | |
161 | ||
162 | static int hmac_update(void *vmacctx, const unsigned char *data, | |
163 | size_t datalen) | |
164 | { | |
165 | struct hmac_data_st *macctx = vmacctx; | |
166 | ||
3fddbb26 MC |
167 | if (macctx->tls_data_size > 0) { |
168 | /* We're doing a TLS HMAC */ | |
169 | if (!macctx->tls_header_set) { | |
170 | /* We expect the first update call to contain the TLS header */ | |
171 | if (datalen != sizeof(macctx->tls_header)) | |
172 | return 0; | |
173 | memcpy(macctx->tls_header, data, datalen); | |
174 | macctx->tls_header_set = 1; | |
175 | return 1; | |
176 | } | |
2e2084da MC |
177 | /* macctx->tls_data_size is datalen plus the padding length */ |
178 | if (macctx->tls_data_size < datalen) | |
3fddbb26 MC |
179 | return 0; |
180 | ||
181 | return ssl3_cbc_digest_record(ossl_prov_digest_md(&macctx->digest), | |
182 | macctx->tls_mac_out, | |
183 | &macctx->tls_mac_out_size, | |
184 | macctx->tls_header, | |
185 | data, | |
3fddbb26 | 186 | datalen, |
2e2084da | 187 | macctx->tls_data_size, |
3fddbb26 MC |
188 | macctx->key, |
189 | macctx->keylen, | |
190 | 0); | |
191 | } | |
192 | ||
5183ebdc RL |
193 | return HMAC_Update(macctx->ctx, data, datalen); |
194 | } | |
195 | ||
196 | static int hmac_final(void *vmacctx, unsigned char *out, size_t *outl, | |
197 | size_t outsize) | |
198 | { | |
199 | unsigned int hlen; | |
200 | struct hmac_data_st *macctx = vmacctx; | |
201 | ||
5b104a81 P |
202 | if (!ossl_prov_is_running()) |
203 | return 0; | |
3fddbb26 MC |
204 | if (macctx->tls_data_size > 0) { |
205 | if (macctx->tls_mac_out_size == 0) | |
206 | return 0; | |
207 | if (outl != NULL) | |
208 | *outl = macctx->tls_mac_out_size; | |
209 | memcpy(out, macctx->tls_mac_out, macctx->tls_mac_out_size); | |
210 | return 1; | |
211 | } | |
5183ebdc RL |
212 | if (!HMAC_Final(macctx->ctx, out, &hlen)) |
213 | return 0; | |
5f6a0b2f | 214 | *outl = hlen; |
5183ebdc RL |
215 | return 1; |
216 | } | |
217 | ||
218 | static const OSSL_PARAM known_gettable_ctx_params[] = { | |
703170d4 | 219 | OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL), |
5183ebdc RL |
220 | OSSL_PARAM_END |
221 | }; | |
1017ab21 | 222 | static const OSSL_PARAM *hmac_gettable_ctx_params(ossl_unused void *provctx) |
5183ebdc RL |
223 | { |
224 | return known_gettable_ctx_params; | |
225 | } | |
226 | ||
92d9d0ae | 227 | static int hmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[]) |
5183ebdc RL |
228 | { |
229 | OSSL_PARAM *p; | |
230 | ||
703170d4 | 231 | if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL) |
5183ebdc RL |
232 | return OSSL_PARAM_set_size_t(p, hmac_size(vmacctx)); |
233 | ||
234 | return 1; | |
235 | } | |
236 | ||
237 | static const OSSL_PARAM known_settable_ctx_params[] = { | |
5183ebdc | 238 | OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST, NULL, 0), |
5183ebdc RL |
239 | OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0), |
240 | OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0), | |
36978c19 SL |
241 | OSSL_PARAM_int(OSSL_MAC_PARAM_DIGEST_NOINIT, NULL), |
242 | OSSL_PARAM_int(OSSL_MAC_PARAM_DIGEST_ONESHOT, NULL), | |
3fddbb26 | 243 | OSSL_PARAM_size_t(OSSL_MAC_PARAM_TLS_DATA_SIZE, NULL), |
5183ebdc RL |
244 | OSSL_PARAM_END |
245 | }; | |
1017ab21 | 246 | static const OSSL_PARAM *hmac_settable_ctx_params(ossl_unused void *provctx) |
5183ebdc RL |
247 | { |
248 | return known_settable_ctx_params; | |
249 | } | |
250 | ||
36978c19 SL |
251 | static int set_flag(const OSSL_PARAM params[], const char *key, int mask, |
252 | int *flags) | |
253 | { | |
254 | const OSSL_PARAM *p = OSSL_PARAM_locate_const(params, key); | |
255 | int flag = 0; | |
256 | ||
257 | if (p != NULL) { | |
258 | if (!OSSL_PARAM_get_int(p, &flag)) | |
259 | return 0; | |
260 | if (flag == 0) | |
261 | *flags &= ~mask; | |
262 | else | |
263 | *flags |= mask; | |
264 | } | |
265 | return 1; | |
266 | } | |
267 | ||
5183ebdc RL |
268 | /* |
269 | * ALL parameters should be set before init(). | |
270 | */ | |
92d9d0ae | 271 | static int hmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[]) |
5183ebdc RL |
272 | { |
273 | struct hmac_data_st *macctx = vmacctx; | |
a829b735 | 274 | OSSL_LIB_CTX *ctx = PROV_LIBCTX_OF(macctx->provctx); |
5183ebdc | 275 | const OSSL_PARAM *p; |
36978c19 | 276 | int flags = 0; |
5183ebdc | 277 | |
103d8b0b P |
278 | if (!ossl_prov_digest_load_from_params(&macctx->digest, params, ctx)) |
279 | return 0; | |
5183ebdc | 280 | |
36978c19 SL |
281 | if (!set_flag(params, OSSL_MAC_PARAM_DIGEST_NOINIT, EVP_MD_CTX_FLAG_NO_INIT, |
282 | &flags)) | |
283 | return 0; | |
284 | if (!set_flag(params, OSSL_MAC_PARAM_DIGEST_ONESHOT, EVP_MD_CTX_FLAG_ONESHOT, | |
285 | &flags)) | |
286 | return 0; | |
287 | if (flags) | |
5183ebdc | 288 | HMAC_CTX_set_flags(macctx->ctx, flags); |
36978c19 | 289 | |
5183ebdc RL |
290 | if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) { |
291 | if (p->data_type != OSSL_PARAM_OCTET_STRING) | |
292 | return 0; | |
293 | ||
3fddbb26 MC |
294 | if (macctx->keylen > 0) |
295 | OPENSSL_secure_clear_free(macctx->key, macctx->keylen); | |
296 | /* Keep a copy of the key if we need it for TLS HMAC */ | |
00108705 | 297 | macctx->key = OPENSSL_secure_malloc(p->data_size > 0 ? p->data_size : 1); |
3fddbb26 MC |
298 | if (macctx->key == NULL) |
299 | return 0; | |
300 | memcpy(macctx->key, p->data, p->data_size); | |
301 | macctx->keylen = p->data_size; | |
302 | ||
5183ebdc | 303 | if (!HMAC_Init_ex(macctx->ctx, p->data, p->data_size, |
103d8b0b P |
304 | ossl_prov_digest_md(&macctx->digest), |
305 | NULL /* ENGINE */)) | |
5183ebdc RL |
306 | return 0; |
307 | ||
3fddbb26 MC |
308 | } |
309 | if ((p = OSSL_PARAM_locate_const(params, | |
310 | OSSL_MAC_PARAM_TLS_DATA_SIZE)) != NULL) { | |
311 | if (!OSSL_PARAM_get_size_t(p, &macctx->tls_data_size)) | |
312 | return 0; | |
5183ebdc RL |
313 | } |
314 | return 1; | |
315 | } | |
316 | ||
1be63951 | 317 | const OSSL_DISPATCH ossl_hmac_functions[] = { |
5183ebdc RL |
318 | { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))hmac_new }, |
319 | { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))hmac_dup }, | |
320 | { OSSL_FUNC_MAC_FREECTX, (void (*)(void))hmac_free }, | |
321 | { OSSL_FUNC_MAC_INIT, (void (*)(void))hmac_init }, | |
322 | { OSSL_FUNC_MAC_UPDATE, (void (*)(void))hmac_update }, | |
323 | { OSSL_FUNC_MAC_FINAL, (void (*)(void))hmac_final }, | |
324 | { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS, | |
325 | (void (*)(void))hmac_gettable_ctx_params }, | |
92d9d0ae | 326 | { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))hmac_get_ctx_params }, |
5183ebdc RL |
327 | { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS, |
328 | (void (*)(void))hmac_settable_ctx_params }, | |
92d9d0ae | 329 | { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))hmac_set_ctx_params }, |
5183ebdc RL |
330 | { 0, NULL } |
331 | }; |