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