/src/openssl/providers/implementations/macs/gmac_prov.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2018-2023 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 | | |
11 | | #include <stdlib.h> |
12 | | #include <string.h> |
13 | | #include <openssl/core_dispatch.h> |
14 | | #include <openssl/core_names.h> |
15 | | #include <openssl/params.h> |
16 | | #include <openssl/evp.h> |
17 | | #include <openssl/err.h> |
18 | | #include <openssl/proverr.h> |
19 | | |
20 | | #include "internal/cryptlib.h" |
21 | | #include "prov/implementations.h" |
22 | | #include "prov/provider_ctx.h" |
23 | | #include "prov/provider_util.h" |
24 | | #include "prov/providercommon.h" |
25 | | |
26 | | /* |
27 | | * Forward declaration of everything implemented here. This is not strictly |
28 | | * necessary for the compiler, but provides an assurance that the signatures |
29 | | * of the functions in the dispatch table are correct. |
30 | | */ |
31 | | static OSSL_FUNC_mac_newctx_fn gmac_new; |
32 | | static OSSL_FUNC_mac_dupctx_fn gmac_dup; |
33 | | static OSSL_FUNC_mac_freectx_fn gmac_free; |
34 | | static OSSL_FUNC_mac_gettable_params_fn gmac_gettable_params; |
35 | | static OSSL_FUNC_mac_get_params_fn gmac_get_params; |
36 | | static OSSL_FUNC_mac_settable_ctx_params_fn gmac_settable_ctx_params; |
37 | | static OSSL_FUNC_mac_set_ctx_params_fn gmac_set_ctx_params; |
38 | | static OSSL_FUNC_mac_init_fn gmac_init; |
39 | | static OSSL_FUNC_mac_update_fn gmac_update; |
40 | | static OSSL_FUNC_mac_final_fn gmac_final; |
41 | | |
42 | | /* local GMAC pkey structure */ |
43 | | |
44 | | struct gmac_data_st { |
45 | | void *provctx; |
46 | | EVP_CIPHER_CTX *ctx; /* Cipher context */ |
47 | | PROV_CIPHER cipher; |
48 | | }; |
49 | | |
50 | | static void gmac_free(void *vmacctx) |
51 | 0 | { |
52 | 0 | struct gmac_data_st *macctx = vmacctx; |
53 | |
|
54 | 0 | if (macctx != NULL) { |
55 | 0 | EVP_CIPHER_CTX_free(macctx->ctx); |
56 | 0 | ossl_prov_cipher_reset(&macctx->cipher); |
57 | 0 | OPENSSL_free(macctx); |
58 | 0 | } |
59 | 0 | } |
60 | | |
61 | | static void *gmac_new(void *provctx) |
62 | 0 | { |
63 | 0 | struct gmac_data_st *macctx; |
64 | |
|
65 | 0 | if (!ossl_prov_is_running()) |
66 | 0 | return NULL; |
67 | | |
68 | 0 | if ((macctx = OPENSSL_zalloc(sizeof(*macctx))) == NULL |
69 | 0 | || (macctx->ctx = EVP_CIPHER_CTX_new()) == NULL) { |
70 | 0 | gmac_free(macctx); |
71 | 0 | return NULL; |
72 | 0 | } |
73 | 0 | macctx->provctx = provctx; |
74 | |
|
75 | 0 | return macctx; |
76 | 0 | } |
77 | | |
78 | | static void *gmac_dup(void *vsrc) |
79 | 0 | { |
80 | 0 | struct gmac_data_st *src = vsrc; |
81 | 0 | struct gmac_data_st *dst; |
82 | |
|
83 | 0 | if (!ossl_prov_is_running()) |
84 | 0 | return NULL; |
85 | | |
86 | 0 | dst = gmac_new(src->provctx); |
87 | 0 | if (dst == NULL) |
88 | 0 | return NULL; |
89 | | |
90 | 0 | if (!EVP_CIPHER_CTX_copy(dst->ctx, src->ctx) |
91 | 0 | || !ossl_prov_cipher_copy(&dst->cipher, &src->cipher)) { |
92 | 0 | gmac_free(dst); |
93 | 0 | return NULL; |
94 | 0 | } |
95 | 0 | return dst; |
96 | 0 | } |
97 | | |
98 | | static size_t gmac_size(void) |
99 | 0 | { |
100 | 0 | return EVP_GCM_TLS_TAG_LEN; |
101 | 0 | } |
102 | | |
103 | | static int gmac_setkey(struct gmac_data_st *macctx, |
104 | | const unsigned char *key, size_t keylen) |
105 | 0 | { |
106 | 0 | EVP_CIPHER_CTX *ctx = macctx->ctx; |
107 | |
|
108 | 0 | if (keylen != (size_t)EVP_CIPHER_CTX_get_key_length(ctx)) { |
109 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
110 | 0 | return 0; |
111 | 0 | } |
112 | 0 | if (!EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL)) |
113 | 0 | return 0; |
114 | 0 | return 1; |
115 | 0 | } |
116 | | |
117 | | static int gmac_init(void *vmacctx, const unsigned char *key, |
118 | | size_t keylen, const OSSL_PARAM params[]) |
119 | 0 | { |
120 | 0 | struct gmac_data_st *macctx = vmacctx; |
121 | |
|
122 | 0 | if (!ossl_prov_is_running() || !gmac_set_ctx_params(macctx, params)) |
123 | 0 | return 0; |
124 | 0 | if (key != NULL) |
125 | 0 | return gmac_setkey(macctx, key, keylen); |
126 | 0 | return EVP_EncryptInit_ex(macctx->ctx, NULL, NULL, NULL, NULL); |
127 | 0 | } |
128 | | |
129 | | static int gmac_update(void *vmacctx, const unsigned char *data, |
130 | | size_t datalen) |
131 | 0 | { |
132 | 0 | struct gmac_data_st *macctx = vmacctx; |
133 | 0 | EVP_CIPHER_CTX *ctx = macctx->ctx; |
134 | 0 | int outlen; |
135 | |
|
136 | 0 | if (datalen == 0) |
137 | 0 | return 1; |
138 | | |
139 | 0 | while (datalen > INT_MAX) { |
140 | 0 | if (!EVP_EncryptUpdate(ctx, NULL, &outlen, data, INT_MAX)) |
141 | 0 | return 0; |
142 | 0 | data += INT_MAX; |
143 | 0 | datalen -= INT_MAX; |
144 | 0 | } |
145 | 0 | return EVP_EncryptUpdate(ctx, NULL, &outlen, data, (int)datalen); |
146 | 0 | } |
147 | | |
148 | | static int gmac_final(void *vmacctx, unsigned char *out, size_t *outl, |
149 | | size_t outsize) |
150 | 0 | { |
151 | 0 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; |
152 | 0 | struct gmac_data_st *macctx = vmacctx; |
153 | 0 | int hlen = 0; |
154 | |
|
155 | 0 | if (!ossl_prov_is_running()) |
156 | 0 | return 0; |
157 | | |
158 | 0 | if (!EVP_EncryptFinal_ex(macctx->ctx, out, &hlen)) |
159 | 0 | return 0; |
160 | | |
161 | 0 | hlen = (int)gmac_size(); |
162 | 0 | params[0] = OSSL_PARAM_construct_octet_string(OSSL_CIPHER_PARAM_AEAD_TAG, |
163 | 0 | out, (size_t)hlen); |
164 | 0 | if (!EVP_CIPHER_CTX_get_params(macctx->ctx, params)) |
165 | 0 | return 0; |
166 | | |
167 | 0 | *outl = hlen; |
168 | 0 | return 1; |
169 | 0 | } |
170 | | |
171 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
172 | | #ifndef gmac_get_params_list |
173 | | static const OSSL_PARAM gmac_get_params_list[] = { |
174 | | OSSL_PARAM_size_t(OSSL_MAC_PARAM_SIZE, NULL), |
175 | | OSSL_PARAM_END |
176 | | }; |
177 | | #endif |
178 | | |
179 | | #ifndef gmac_get_params_st |
180 | | struct gmac_get_params_st { |
181 | | OSSL_PARAM *size; |
182 | | }; |
183 | | #endif |
184 | | |
185 | | #ifndef gmac_get_params_decoder |
186 | | static int gmac_get_params_decoder |
187 | | (const OSSL_PARAM *p, struct gmac_get_params_st *r) |
188 | 0 | { |
189 | 0 | const char *s; |
190 | |
|
191 | 0 | memset(r, 0, sizeof(*r)); |
192 | 0 | if (p != NULL) |
193 | 0 | for (; (s = p->key) != NULL; p++) |
194 | 0 | if (ossl_likely(strcmp("size", s + 0) == 0)) { |
195 | | /* MAC_PARAM_SIZE */ |
196 | 0 | if (ossl_unlikely(r->size != NULL)) { |
197 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
198 | 0 | "param %s is repeated", s); |
199 | 0 | return 0; |
200 | 0 | } |
201 | 0 | r->size = (OSSL_PARAM *)p; |
202 | 0 | } |
203 | 0 | return 1; |
204 | 0 | } |
205 | | #endif |
206 | | /* End of machine generated */ |
207 | | |
208 | | static const OSSL_PARAM *gmac_gettable_params(void *provctx) |
209 | 0 | { |
210 | 0 | return gmac_get_params_list; |
211 | 0 | } |
212 | | |
213 | | static int gmac_get_params(OSSL_PARAM params[]) |
214 | 0 | { |
215 | 0 | struct gmac_get_params_st p; |
216 | |
|
217 | 0 | if (!gmac_get_params_decoder(params, &p)) |
218 | 0 | return 0; |
219 | | |
220 | 0 | if (p.size != NULL && !OSSL_PARAM_set_size_t(p.size, gmac_size())) |
221 | 0 | return 0; |
222 | | |
223 | 0 | return 1; |
224 | 0 | } |
225 | | |
226 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
227 | | #ifndef gmac_set_ctx_params_list |
228 | | static const OSSL_PARAM gmac_set_ctx_params_list[] = { |
229 | | OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_CIPHER, NULL, 0), |
230 | | OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_PROPERTIES, NULL, 0), |
231 | | OSSL_PARAM_octet_string(OSSL_MAC_PARAM_KEY, NULL, 0), |
232 | | OSSL_PARAM_octet_string(OSSL_MAC_PARAM_IV, NULL, 0), |
233 | | OSSL_PARAM_END |
234 | | }; |
235 | | #endif |
236 | | |
237 | | #ifndef gmac_set_ctx_params_st |
238 | | struct gmac_set_ctx_params_st { |
239 | | OSSL_PARAM *cipher; |
240 | | OSSL_PARAM *engine; |
241 | | OSSL_PARAM *iv; |
242 | | OSSL_PARAM *key; |
243 | | OSSL_PARAM *propq; |
244 | | }; |
245 | | #endif |
246 | | |
247 | | #ifndef gmac_set_ctx_params_decoder |
248 | | static int gmac_set_ctx_params_decoder |
249 | | (const OSSL_PARAM *p, struct gmac_set_ctx_params_st *r) |
250 | 0 | { |
251 | 0 | const char *s; |
252 | |
|
253 | 0 | memset(r, 0, sizeof(*r)); |
254 | 0 | if (p != NULL) |
255 | 0 | for (; (s = p->key) != NULL; p++) |
256 | 0 | switch(s[0]) { |
257 | 0 | default: |
258 | 0 | break; |
259 | 0 | case 'c': |
260 | 0 | if (ossl_likely(strcmp("ipher", s + 1) == 0)) { |
261 | | /* MAC_PARAM_CIPHER */ |
262 | 0 | if (ossl_unlikely(r->cipher != NULL)) { |
263 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
264 | 0 | "param %s is repeated", s); |
265 | 0 | return 0; |
266 | 0 | } |
267 | 0 | r->cipher = (OSSL_PARAM *)p; |
268 | 0 | } |
269 | 0 | break; |
270 | 0 | case 'e': |
271 | 0 | if (ossl_likely(strcmp("ngine", s + 1) == 0)) { |
272 | | /* ALG_PARAM_ENGINE */ |
273 | 0 | if (ossl_unlikely(r->engine != NULL)) { |
274 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
275 | 0 | "param %s is repeated", s); |
276 | 0 | return 0; |
277 | 0 | } |
278 | 0 | r->engine = (OSSL_PARAM *)p; |
279 | 0 | } |
280 | 0 | break; |
281 | 0 | case 'i': |
282 | 0 | if (ossl_likely(strcmp("v", s + 1) == 0)) { |
283 | | /* MAC_PARAM_IV */ |
284 | 0 | if (ossl_unlikely(r->iv != NULL)) { |
285 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
286 | 0 | "param %s is repeated", s); |
287 | 0 | return 0; |
288 | 0 | } |
289 | 0 | r->iv = (OSSL_PARAM *)p; |
290 | 0 | } |
291 | 0 | break; |
292 | 0 | case 'k': |
293 | 0 | if (ossl_likely(strcmp("ey", s + 1) == 0)) { |
294 | | /* MAC_PARAM_KEY */ |
295 | 0 | if (ossl_unlikely(r->key != NULL)) { |
296 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
297 | 0 | "param %s is repeated", s); |
298 | 0 | return 0; |
299 | 0 | } |
300 | 0 | r->key = (OSSL_PARAM *)p; |
301 | 0 | } |
302 | 0 | break; |
303 | 0 | case 'p': |
304 | 0 | if (ossl_likely(strcmp("roperties", s + 1) == 0)) { |
305 | | /* MAC_PARAM_PROPERTIES */ |
306 | 0 | if (ossl_unlikely(r->propq != NULL)) { |
307 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
308 | 0 | "param %s is repeated", s); |
309 | 0 | return 0; |
310 | 0 | } |
311 | 0 | r->propq = (OSSL_PARAM *)p; |
312 | 0 | } |
313 | 0 | } |
314 | 0 | return 1; |
315 | 0 | } |
316 | | #endif |
317 | | /* End of machine generated */ |
318 | | |
319 | | static const OSSL_PARAM *gmac_settable_ctx_params(ossl_unused void *ctx, |
320 | | ossl_unused void *provctx) |
321 | 0 | { |
322 | 0 | return gmac_set_ctx_params_list; |
323 | 0 | } |
324 | | |
325 | | /* |
326 | | * ALL parameters should be set before init(). |
327 | | */ |
328 | | static int gmac_set_ctx_params(void *vmacctx, const OSSL_PARAM params[]) |
329 | 0 | { |
330 | 0 | struct gmac_data_st *macctx = vmacctx; |
331 | 0 | EVP_CIPHER_CTX *ctx; |
332 | 0 | OSSL_LIB_CTX *provctx; |
333 | 0 | struct gmac_set_ctx_params_st p; |
334 | |
|
335 | 0 | if (macctx == NULL || !gmac_set_ctx_params_decoder(params, &p)) |
336 | 0 | return 0; |
337 | | |
338 | 0 | if ((ctx = macctx->ctx) == NULL) |
339 | 0 | return 0; |
340 | 0 | provctx = PROV_LIBCTX_OF(macctx->provctx); |
341 | |
|
342 | 0 | if (p.cipher != NULL) { |
343 | 0 | if (!ossl_prov_cipher_load(&macctx->cipher, p.cipher, p.propq, |
344 | 0 | p.engine, provctx)) |
345 | 0 | return 0; |
346 | 0 | if (EVP_CIPHER_get_mode(ossl_prov_cipher_cipher(&macctx->cipher)) |
347 | 0 | != EVP_CIPH_GCM_MODE) { |
348 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE); |
349 | 0 | return 0; |
350 | 0 | } |
351 | 0 | if (!EVP_EncryptInit_ex(ctx, ossl_prov_cipher_cipher(&macctx->cipher), |
352 | 0 | ossl_prov_cipher_engine(&macctx->cipher), NULL, |
353 | 0 | NULL)) |
354 | 0 | return 0; |
355 | 0 | } |
356 | | |
357 | 0 | if (p.key != NULL) |
358 | 0 | if (p.key->data_type != OSSL_PARAM_OCTET_STRING |
359 | 0 | || !gmac_setkey(macctx, p.key->data, p.key->data_size)) |
360 | 0 | return 0; |
361 | | |
362 | 0 | if (p.iv != NULL) { |
363 | 0 | if (p.iv->data_type != OSSL_PARAM_OCTET_STRING) |
364 | 0 | return 0; |
365 | | |
366 | 0 | if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, |
367 | 0 | (int)p.iv->data_size, NULL) <= 0 |
368 | 0 | || !EVP_EncryptInit_ex(ctx, NULL, NULL, NULL, p.iv->data)) |
369 | 0 | return 0; |
370 | 0 | } |
371 | 0 | return 1; |
372 | 0 | } |
373 | | |
374 | | const OSSL_DISPATCH ossl_gmac_functions[] = { |
375 | | { OSSL_FUNC_MAC_NEWCTX, (void (*)(void))gmac_new }, |
376 | | { OSSL_FUNC_MAC_DUPCTX, (void (*)(void))gmac_dup }, |
377 | | { OSSL_FUNC_MAC_FREECTX, (void (*)(void))gmac_free }, |
378 | | { OSSL_FUNC_MAC_INIT, (void (*)(void))gmac_init }, |
379 | | { OSSL_FUNC_MAC_UPDATE, (void (*)(void))gmac_update }, |
380 | | { OSSL_FUNC_MAC_FINAL, (void (*)(void))gmac_final }, |
381 | | { OSSL_FUNC_MAC_GETTABLE_PARAMS, (void (*)(void))gmac_gettable_params }, |
382 | | { OSSL_FUNC_MAC_GET_PARAMS, (void (*)(void))gmac_get_params }, |
383 | | { OSSL_FUNC_MAC_SETTABLE_CTX_PARAMS, |
384 | | (void (*)(void))gmac_settable_ctx_params }, |
385 | | { OSSL_FUNC_MAC_SET_CTX_PARAMS, (void (*)(void))gmac_set_ctx_params }, |
386 | | OSSL_DISPATCH_END |
387 | | }; |