]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/common/macs/hmac_prov.c
d5f6db79de0494d1d9aa085dd63f45431b2341d9
[thirdparty/openssl.git] / providers / common / macs / hmac_prov.c
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_get_ctx_params_fn hmac_get_ctx_params;
30 static OSSL_OP_mac_settable_ctx_params_fn hmac_settable_ctx_params;
31 static OSSL_OP_mac_set_ctx_params_fn hmac_set_ctx_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_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_SIZE, NULL),
155 OSSL_PARAM_END
156 };
157 static const OSSL_PARAM *hmac_gettable_ctx_params(void)
158 {
159 return known_gettable_ctx_params;
160 }
161
162 static int hmac_get_ctx_params(void *vmacctx, OSSL_PARAM params[])
163 {
164 OSSL_PARAM *p;
165
166 if ((p = OSSL_PARAM_locate(params, OSSL_MAC_PARAM_SIZE)) != NULL)
167 return OSSL_PARAM_set_size_t(p, hmac_size(vmacctx));
168
169 return 1;
170 }
171
172 static const OSSL_PARAM known_settable_ctx_params[] = {
173 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST, NULL, 0),
174 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_ENGINE, NULL, 0),
175 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0),
176 OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0),
177 OSSL_PARAM_int(OSSL_MAC_PARAM_FLAGS, NULL),
178 OSSL_PARAM_END
179 };
180 static const OSSL_PARAM *hmac_settable_ctx_params(void)
181 {
182 return known_settable_ctx_params;
183 }
184
185 /*
186 * ALL parameters should be set before init().
187 */
188 static int hmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[])
189 {
190 struct hmac_data_st *macctx = vmacctx;
191 const OSSL_PARAM *p;
192
193 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_DIGEST)) != NULL) {
194 if (p->data_type != OSSL_PARAM_UTF8_STRING)
195 return 0;
196
197 {
198 const char *algoname = p->data;
199 const char *propquery = NULL;
200
201 /* Inside the FIPS module, we don't support engines */
202 #if !defined(FIPS_MODE) && !defined(OPENSSL_NO_ENGINE)
203 ENGINE_finish(macctx->tmpengine);
204 macctx->tmpengine = NULL;
205
206 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_ENGINE))
207 != NULL) {
208 if (p->data_type != OSSL_PARAM_UTF8_STRING)
209 return 0;
210
211 macctx->tmpengine = ENGINE_by_id(p->data);
212 if (macctx->tmpengine == NULL)
213 return 0;
214 }
215 #endif
216 if ((p = OSSL_PARAM_locate_const(params,
217 OSSL_MAC_PARAM_PROPERTIES))
218 != NULL) {
219 if (p->data_type != OSSL_PARAM_UTF8_STRING)
220 return 0;
221
222 propquery = p->data;
223 }
224
225 EVP_MD_free(macctx->alloc_md);
226
227 macctx->tmpmd = macctx->alloc_md =
228 EVP_MD_fetch(PROV_LIBRARY_CONTEXT_OF(macctx->provctx),
229 algoname, propquery);
230
231 #ifndef FIPS_MODE /* Inside the FIPS module, we don't support legacy digests */
232 /* TODO(3.0) BEGIN legacy stuff, to be removed */
233 if (macctx->tmpmd == NULL)
234 macctx->tmpmd = EVP_get_digestbyname(algoname);
235 /* TODO(3.0) END of legacy stuff */
236 #endif
237
238 if (macctx->tmpmd == NULL)
239 return 0;
240 }
241 }
242 /* TODO(3.0) formalize the meaning of "flags", perhaps as other params */
243 if ((p = OSSL_PARAM_locate_const(params,
244 OSSL_MAC_PARAM_FLAGS)) != NULL) {
245 int flags = 0;
246
247 if (!OSSL_PARAM_get_int(p, &flags))
248 return 0;
249 HMAC_CTX_set_flags(macctx->ctx, flags);
250 }
251 if ((p = OSSL_PARAM_locate_const(params, OSSL_MAC_PARAM_KEY)) != NULL) {
252 if (p->data_type != OSSL_PARAM_OCTET_STRING)
253 return 0;
254
255 if (!HMAC_Init_ex(macctx->ctx, p->data, p->data_size,
256 macctx->tmpmd, NULL /* ENGINE */))
257 return 0;
258
259 macctx->tmpmd = NULL;
260 macctx->tmpengine = NULL;
261 }
262 return 1;
263 }
264
265 const OSSL_DISPATCH hmac_functions[] = {
266 { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))hmac_new },
267 { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))hmac_dup },
268 { OSSL_FUNC_MAC_FREECTX, (void (*)(void))hmac_free },
269 { OSSL_FUNC_MAC_INIT, (void (*)(void))hmac_init },
270 { OSSL_FUNC_MAC_UPDATE, (void (*)(void))hmac_update },
271 { OSSL_FUNC_MAC_FINAL, (void (*)(void))hmac_final },
272 { OSSL_FUNC_MAC_GETTABLE_CTX_PARAMS,
273 (void (*)(void))hmac_gettable_ctx_params },
274 { OSSL_FUNC_MAC_GET_CTX_PARAMS, (void (*)(void))hmac_get_ctx_params },
275 { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS,
276 (void (*)(void))hmac_settable_ctx_params },
277 { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))hmac_set_ctx_params },
278 { 0, NULL }
279 };