]>
Commit | Line | Data |
---|---|---|
5183ebdc RL |
1 | /* |
2 | * Copyright 2018 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 | #include <openssl/core_numbers.h> | |
11 | #include <openssl/core_names.h> | |
12 | #include <openssl/params.h> | |
13 | #include <openssl/engine.h> | |
14 | #include <openssl/evp.h> | |
15 | #include <openssl/hmac.h> | |
16 | ||
17 | #include "internal/provider_algs.h" | |
18 | #include "internal/provider_ctx.h" | |
19 | ||
20 | /* | |
21 | * Forward declaration of everything implemented here. This is not strictly | |
22 | * necessary for the compiler, but provides an assurance that the signatures | |
23 | * of the functions in the dispatch table are correct. | |
24 | */ | |
25 | static OSSL_OP_mac_newctx_fn hmac_new; | |
26 | static OSSL_OP_mac_dupctx_fn hmac_dup; | |
27 | static OSSL_OP_mac_freectx_fn hmac_free; | |
28 | static OSSL_OP_mac_gettable_ctx_params_fn hmac_gettable_ctx_params; | |
29 | static OSSL_OP_mac_ctx_get_params_fn hmac_ctx_get_params; | |
30 | static OSSL_OP_mac_settable_ctx_params_fn hmac_settable_ctx_params; | |
31 | static OSSL_OP_mac_ctx_set_params_fn hmac_ctx_set_params; | |
32 | static OSSL_OP_mac_init_fn hmac_init; | |
33 | static OSSL_OP_mac_update_fn hmac_update; | |
34 | static OSSL_OP_mac_final_fn hmac_final; | |
35 | ||
36 | /* local HMAC context structure */ | |
37 | ||
38 | /* typedef EVP_MAC_IMPL */ | |
39 | struct hmac_data_st { | |
40 | void *provctx; | |
41 | HMAC_CTX *ctx; /* HMAC context */ | |
42 | ||
43 | /* | |
44 | * References to the underlying digest implementation. tmpmd caches | |
45 | * the md, always. alloc_md only holds a reference to an explicitly | |
46 | * fetched digest. | |
47 | * tmpmd is cleared after a CMAC_Init call. | |
48 | */ | |
49 | const EVP_MD *tmpmd; /* HMAC digest */ | |
50 | EVP_MD *alloc_md; /* fetched digest */ | |
51 | ||
52 | /* | |
53 | * Conditions for legacy EVP_MD uses. | |
54 | * tmpengine is cleared after a CMAC_Init call. | |
55 | */ | |
56 | ENGINE *tmpengine; /* HMAC digest engine */ | |
57 | }; | |
58 | ||
59 | static size_t hmac_size(void *vmacctx); | |
60 | ||
61 | static void *hmac_new(void *provctx) | |
62 | { | |
63 | struct hmac_data_st *macctx; | |
64 | ||
65 | if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL | |
66 | || (macctx->ctx = HMAC_CTX_new()) == NULL) { | |
67 | OPENSSL_free(macctx); | |
68 | return NULL; | |
69 | } | |
70 | /* TODO(3.0) Should we do something more with that context? */ | |
71 | macctx->provctx = provctx; | |
72 | ||
73 | return macctx; | |
74 | } | |
75 | ||
76 | static void hmac_free(void *vmacctx) | |
77 | { | |
78 | struct hmac_data_st *macctx = vmacctx; | |
79 | ||
80 | if (macctx != NULL) { | |
81 | HMAC_CTX_free(macctx->ctx); | |
82 | EVP_MD_meth_free(macctx->alloc_md); | |
83 | OPENSSL_free(macctx); | |
84 | } | |
85 | } | |
86 | ||
87 | static void *hmac_dup(void *vsrc) | |
88 | { | |
89 | struct hmac_data_st *src = vsrc; | |
90 | struct hmac_data_st *dst = hmac_new(src->provctx); | |
91 | ||
92 | if (dst == NULL) | |
93 | return NULL; | |
94 | ||
95 | if (!HMAC_CTX_copy(dst->ctx, src->ctx)) { | |
96 | hmac_free(dst); | |
97 | return NULL; | |
98 | } | |
99 | ||
100 | if (src->alloc_md != NULL && !EVP_MD_up_ref(src->alloc_md)) { | |
101 | hmac_free(dst); | |
102 | return NULL; | |
103 | } | |
104 | ||
105 | dst->tmpengine = src->tmpengine; | |
106 | dst->tmpmd = src->tmpmd; | |
107 | dst->alloc_md = src->alloc_md; | |
108 | return dst; | |
109 | } | |
110 | ||
111 | static size_t hmac_size(void *vmacctx) | |
112 | { | |
113 | struct hmac_data_st *macctx = vmacctx; | |
114 | ||
115 | return HMAC_size(macctx->ctx); | |
116 | } | |
117 | ||
118 | static int hmac_init(void *vmacctx) | |
119 | { | |
120 | struct hmac_data_st *macctx = vmacctx; | |
121 | int rv = 1; | |
122 | ||
123 | /* HMAC_Init_ex doesn't tolerate all zero params, so we must be careful */ | |
124 | if (macctx->tmpmd != NULL) | |
125 | rv = HMAC_Init_ex(macctx->ctx, NULL, 0, macctx->tmpmd, | |
126 | (ENGINE * )macctx->tmpengine); | |
127 | macctx->tmpengine = NULL; | |
128 | macctx->tmpmd = NULL; | |
129 | return rv; | |
130 | } | |
131 | ||
132 | static int hmac_update(void *vmacctx, const unsigned char *data, | |
133 | size_t datalen) | |
134 | { | |
135 | struct hmac_data_st *macctx = vmacctx; | |
136 | ||
137 | return HMAC_Update(macctx->ctx, data, datalen); | |
138 | } | |
139 | ||
140 | static int hmac_final(void *vmacctx, unsigned char *out, size_t *outl, | |
141 | size_t outsize) | |
142 | { | |
143 | unsigned int hlen; | |
144 | struct hmac_data_st *macctx = vmacctx; | |
145 | ||
146 | if (!HMAC_Final(macctx->ctx, out, &hlen)) | |
147 | return 0; | |
148 | if (outl != NULL) | |
149 | *outl = hlen; | |
150 | return 1; | |
151 | } | |
152 | ||
153 | static const OSSL_PARAM known_gettable_ctx_params[] = { | |
154 | OSSL_PARAM_size_t(OSSL_MAC_PARAM_OUTLEN, NULL), | |
155 | OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL), /* Same as "outlen" */ | |
156 | OSSL_PARAM_END | |
157 | }; | |
158 | static const OSSL_PARAM *hmac_gettable_ctx_params(void) | |
159 | { | |
160 | return known_gettable_ctx_params; | |
161 | } | |
162 | ||
163 | static int hmac_ctx_get_params(void *vmacctx, OSSL_PARAM params[]) | |
164 | { | |
165 | OSSL_PARAM *p; | |
166 | ||
167 | if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_OUTLEN)) != NULL | |
168 | || (p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL) | |
169 | return OSSL_PARAM_set_size_t(p, hmac_size(vmacctx)); | |
170 | ||
171 | return 1; | |
172 | } | |
173 | ||
174 | static const OSSL_PARAM known_settable_ctx_params[] = { | |
175 | /* "algorithm" and "digest" are the same parameter */ | |
176 | OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_ALGORITHM, NULL, 0), | |
177 | OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST, NULL, 0), | |
178 | OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_ENGINE, NULL, 0), | |
179 | OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0), | |
180 | OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0), | |
181 | OSSL_PARAM_int(OSSL_MAC_PARAM_FLAGS, NULL), | |
182 | OSSL_PARAM_END | |
183 | }; | |
184 | static const OSSL_PARAM *hmac_settable_ctx_params(void) | |
185 | { | |
186 | return known_settable_ctx_params; | |
187 | } | |
188 | ||
189 | /* | |
190 | * ALL parameters should be set before init(). | |
191 | */ | |
192 | static int hmac_ctx_set_params(void *vmacctx, const OSSL_PARAM params[]) | |
193 | { | |
194 | struct hmac_data_st *macctx = vmacctx; | |
195 | const OSSL_PARAM *p; | |
196 | ||
197 | if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_DIGEST)) != NULL | |
198 | || (p = OSSL_PARAM_locate_const(params, | |
199 | OSSL_MAC_PARAM_ALGORITHM)) != NULL) { | |
200 | if (p->data_type != OSSL_PARAM_UTF8_STRING) | |
201 | return 0; | |
202 | ||
203 | { | |
204 | const char *algoname = p->data; | |
205 | const char *propquery = NULL; | |
206 | ||
207 | #ifndef FIPS_MODE /* Inside the FIPS module, we don't support engines */ | |
208 | ENGINE_finish(macctx->tmpengine); | |
209 | macctx->tmpengine = NULL; | |
210 | ||
211 | if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_ENGINE)) | |
212 | != NULL) { | |
213 | if (p->data_type != OSSL_PARAM_UTF8_STRING) | |
214 | return 0; | |
215 | ||
216 | macctx->tmpengine = ENGINE_by_id(p->data); | |
217 | if (macctx->tmpengine == NULL) | |
218 | return 0; | |
219 | } | |
220 | #endif | |
221 | if ((p = OSSL_PARAM_locate_const(params, | |
222 | OSSL_MAC_PARAM_PROPERTIES)) | |
223 | != NULL) { | |
224 | if (p->data_type != OSSL_PARAM_UTF8_STRING) | |
225 | return 0; | |
226 | ||
227 | propquery = p->data; | |
228 | } | |
229 | ||
230 | EVP_MD_meth_free(macctx->alloc_md); | |
231 | ||
232 | macctx->tmpmd = macctx->alloc_md = | |
233 | EVP_MD_fetch(PROV_LIBRARY_CONTEXT_OF(macctx->provctx), | |
234 | algoname, propquery); | |
235 | ||
236 | #ifndef FIPS_MODE /* Inside the FIPS module, we don't support legacy digests */ | |
237 | /* TODO(3.0) BEGIN legacy stuff, to be removed */ | |
238 | if (macctx->tmpmd == NULL) | |
239 | macctx->tmpmd = EVP_get_digestbyname(algoname); | |
240 | /* TODO(3.0) END of legacy stuff */ | |
241 | #endif | |
242 | ||
243 | if (macctx->tmpmd == NULL) | |
244 | return 0; | |
245 | } | |
246 | } | |
247 | /* TODO(3.0) formalize the meaning of "flags", perhaps as other params */ | |
248 | if ((p = OSSL_PARAM_locate_const(params, | |
249 | OSSL_MAC_PARAM_FLAGS)) != NULL) { | |
250 | int flags = 0; | |
251 | ||
252 | if (!OSSL_PARAM_get_int(p, &flags)) | |
253 | return 0; | |
254 | HMAC_CTX_set_flags(macctx->ctx, flags); | |
255 | } | |
256 | if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) { | |
257 | if (p->data_type != OSSL_PARAM_OCTET_STRING) | |
258 | return 0; | |
259 | ||
260 | if (!HMAC_Init_ex(macctx->ctx, p->data, p->data_size, | |
261 | macctx->tmpmd, NULL /* ENGINE */)) | |
262 | return 0; | |
263 | ||
264 | macctx->tmpmd = NULL; | |
265 | macctx->tmpengine = NULL; | |
266 | } | |
267 | return 1; | |
268 | } | |
269 | ||
270 | const OSSL_DISPATCH hmac_functions[] = { | |
271 | { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))hmac_new }, | |
272 | { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))hmac_dup }, | |
273 | { OSSL_FUNC_MAC_FREECTX, (void (*)(void))hmac_free }, | |
274 | { OSSL_FUNC_MAC_INIT, (void (*)(void))hmac_init }, | |
275 | { OSSL_FUNC_MAC_UPDATE, (void (*)(void))hmac_update }, | |
276 | { OSSL_FUNC_MAC_FINAL, (void (*)(void))hmac_final }, | |
277 | { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS, | |
278 | (void (*)(void))hmac_gettable_ctx_params }, | |
279 | { OSSL_FUNC_MAC_CTX_GET_PARAMS, (void (*)(void))hmac_ctx_get_params }, | |
280 | { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS, | |
281 | (void (*)(void))hmac_settable_ctx_params }, | |
282 | { OSSL_FUNC_MAC_CTX_SET_PARAMS, (void (*)(void))hmac_ctx_set_params }, | |
283 | { 0, NULL } | |
284 | }; |