/src/openssl/providers/implementations/keymgmt/mac_legacy_kmgmt.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2020-2025 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 <string.h> |
11 | | #include <openssl/core_dispatch.h> |
12 | | #include <openssl/core_names.h> |
13 | | #include <openssl/params.h> |
14 | | #include <openssl/err.h> |
15 | | #include <openssl/evp.h> |
16 | | #include <openssl/proverr.h> |
17 | | #include <openssl/param_build.h> |
18 | | #include "internal/param_build_set.h" |
19 | | #include "prov/implementations.h" |
20 | | #include "prov/providercommon.h" |
21 | | #include "prov/provider_ctx.h" |
22 | | #include "prov/macsignature.h" |
23 | | |
24 | | static OSSL_FUNC_keymgmt_new_fn mac_new; |
25 | | static OSSL_FUNC_keymgmt_free_fn mac_free; |
26 | | static OSSL_FUNC_keymgmt_gen_init_fn mac_gen_init; |
27 | | static OSSL_FUNC_keymgmt_gen_fn mac_gen; |
28 | | static OSSL_FUNC_keymgmt_gen_cleanup_fn mac_gen_cleanup; |
29 | | static OSSL_FUNC_keymgmt_gen_set_params_fn mac_gen_set_params; |
30 | | static OSSL_FUNC_keymgmt_gen_settable_params_fn mac_gen_settable_params; |
31 | | static OSSL_FUNC_keymgmt_get_params_fn mac_get_params; |
32 | | static OSSL_FUNC_keymgmt_gettable_params_fn mac_gettable_params; |
33 | | static OSSL_FUNC_keymgmt_set_params_fn mac_set_params; |
34 | | static OSSL_FUNC_keymgmt_settable_params_fn mac_settable_params; |
35 | | static OSSL_FUNC_keymgmt_has_fn mac_has; |
36 | | static OSSL_FUNC_keymgmt_match_fn mac_match; |
37 | | static OSSL_FUNC_keymgmt_import_fn mac_import; |
38 | | static OSSL_FUNC_keymgmt_import_types_fn mac_imexport_types; |
39 | | static OSSL_FUNC_keymgmt_export_fn mac_export; |
40 | | static OSSL_FUNC_keymgmt_export_types_fn mac_imexport_types; |
41 | | |
42 | | static OSSL_FUNC_keymgmt_new_fn mac_new_cmac; |
43 | | static OSSL_FUNC_keymgmt_get_params_fn cmac_get_params; |
44 | | static OSSL_FUNC_keymgmt_gettable_params_fn cmac_gettable_params; |
45 | | static OSSL_FUNC_keymgmt_import_fn cmac_import; |
46 | | static OSSL_FUNC_keymgmt_import_types_fn cmac_imexport_types; |
47 | | static OSSL_FUNC_keymgmt_export_types_fn cmac_imexport_types; |
48 | | static OSSL_FUNC_keymgmt_gen_init_fn cmac_gen_init; |
49 | | static OSSL_FUNC_keymgmt_gen_set_params_fn cmac_gen_set_params; |
50 | | static OSSL_FUNC_keymgmt_gen_settable_params_fn cmac_gen_settable_params; |
51 | | |
52 | | struct mac_gen_ctx { |
53 | | OSSL_LIB_CTX *libctx; |
54 | | int selection; |
55 | | unsigned char *priv_key; |
56 | | size_t priv_key_len; |
57 | | PROV_CIPHER cipher; |
58 | | }; |
59 | | |
60 | | MAC_KEY *ossl_mac_key_new(OSSL_LIB_CTX *libctx, int cmac) |
61 | 0 | { |
62 | 0 | MAC_KEY *mackey; |
63 | |
|
64 | 0 | if (!ossl_prov_is_running()) |
65 | 0 | return NULL; |
66 | | |
67 | 0 | mackey = OPENSSL_zalloc(sizeof(*mackey)); |
68 | 0 | if (mackey == NULL) |
69 | 0 | return NULL; |
70 | | |
71 | 0 | if (!CRYPTO_NEW_REF(&mackey->refcnt, 1)) { |
72 | 0 | OPENSSL_free(mackey); |
73 | 0 | return NULL; |
74 | 0 | } |
75 | 0 | mackey->libctx = libctx; |
76 | 0 | mackey->cmac = cmac; |
77 | |
|
78 | 0 | return mackey; |
79 | 0 | } |
80 | | |
81 | | void ossl_mac_key_free(MAC_KEY *mackey) |
82 | 0 | { |
83 | 0 | int ref = 0; |
84 | |
|
85 | 0 | if (mackey == NULL) |
86 | 0 | return; |
87 | | |
88 | 0 | CRYPTO_DOWN_REF(&mackey->refcnt, &ref); |
89 | 0 | if (ref > 0) |
90 | 0 | return; |
91 | | |
92 | 0 | OPENSSL_secure_clear_free(mackey->priv_key, mackey->priv_key_len); |
93 | 0 | OPENSSL_free(mackey->properties); |
94 | 0 | ossl_prov_cipher_reset(&mackey->cipher); |
95 | 0 | CRYPTO_FREE_REF(&mackey->refcnt); |
96 | 0 | OPENSSL_free(mackey); |
97 | 0 | } |
98 | | |
99 | | int ossl_mac_key_up_ref(MAC_KEY *mackey) |
100 | 0 | { |
101 | 0 | int ref = 0; |
102 | | |
103 | | /* This is effectively doing a new operation on the MAC_KEY and should be |
104 | | * adequately guarded again modules' error states. However, both current |
105 | | * calls here are guarded properly in signature/mac_legacy.c. Thus, it |
106 | | * could be removed here. The concern is that something in the future |
107 | | * might call this function without adequate guards. It's a cheap call, |
108 | | * it seems best to leave it even though it is currently redundant. |
109 | | */ |
110 | 0 | if (!ossl_prov_is_running()) |
111 | 0 | return 0; |
112 | | |
113 | 0 | CRYPTO_UP_REF(&mackey->refcnt, &ref); |
114 | 0 | return 1; |
115 | 0 | } |
116 | | |
117 | | static void *mac_new(void *provctx) |
118 | 0 | { |
119 | 0 | return ossl_mac_key_new(PROV_LIBCTX_OF(provctx), 0); |
120 | 0 | } |
121 | | |
122 | | static void *mac_new_cmac(void *provctx) |
123 | 0 | { |
124 | 0 | return ossl_mac_key_new(PROV_LIBCTX_OF(provctx), 1); |
125 | 0 | } |
126 | | |
127 | | static void mac_free(void *mackey) |
128 | 0 | { |
129 | 0 | ossl_mac_key_free(mackey); |
130 | 0 | } |
131 | | |
132 | | static int mac_has(const void *keydata, int selection) |
133 | 0 | { |
134 | 0 | const MAC_KEY *key = keydata; |
135 | 0 | int ok = 0; |
136 | |
|
137 | 0 | if (ossl_prov_is_running() && key != NULL) { |
138 | | /* |
139 | | * MAC keys always have all the parameters they need (i.e. none). |
140 | | * Therefore we always return with 1, if asked about parameters. |
141 | | * Similarly for public keys. |
142 | | */ |
143 | 0 | ok = 1; |
144 | |
|
145 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) |
146 | 0 | ok = key->priv_key != NULL; |
147 | 0 | } |
148 | 0 | return ok; |
149 | 0 | } |
150 | | |
151 | | static int mac_match(const void *keydata1, const void *keydata2, int selection) |
152 | 0 | { |
153 | 0 | const MAC_KEY *key1 = keydata1; |
154 | 0 | const MAC_KEY *key2 = keydata2; |
155 | 0 | int ok = 1; |
156 | |
|
157 | 0 | if (!ossl_prov_is_running()) |
158 | 0 | return 0; |
159 | | |
160 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) { |
161 | 0 | if ((key1->priv_key == NULL && key2->priv_key != NULL) |
162 | 0 | || (key1->priv_key != NULL && key2->priv_key == NULL) |
163 | 0 | || key1->priv_key_len != key2->priv_key_len |
164 | 0 | || (key1->cipher.cipher == NULL && key2->cipher.cipher != NULL) |
165 | 0 | || (key1->cipher.cipher != NULL && key2->cipher.cipher == NULL)) |
166 | 0 | ok = 0; |
167 | 0 | else |
168 | 0 | ok = ok && (key1->priv_key == NULL /* implies key2->privkey == NULL */ |
169 | 0 | || CRYPTO_memcmp(key1->priv_key, key2->priv_key, key1->priv_key_len) == 0); |
170 | 0 | if (key1->cipher.cipher != NULL) |
171 | 0 | ok = ok && EVP_CIPHER_is_a(key1->cipher.cipher, EVP_CIPHER_get0_name(key2->cipher.cipher)); |
172 | 0 | } |
173 | 0 | return ok; |
174 | 0 | } |
175 | | |
176 | | /* Common structure to handle the MAC parameters for various calls */ |
177 | | struct mac_common_params_st { |
178 | | OSSL_PARAM *key; |
179 | | OSSL_PARAM *cipher; /* CMAC */ |
180 | | OSSL_PARAM *propq; |
181 | | }; |
182 | | |
183 | | #define mac_import_st mac_common_params_st |
184 | | #define cmac_import_st mac_common_params_st |
185 | | #define mac_get_params_st mac_common_params_st |
186 | | #define cmac_get_params_st mac_common_params_st |
187 | | #define mac_set_params_st mac_common_params_st |
188 | | #define mac_gen_set_params_st mac_common_params_st |
189 | | #define cmac_gen_set_params_st mac_common_params_st |
190 | | |
191 | | #include "providers/implementations/keymgmt/mac_legacy_kmgmt.inc" |
192 | | |
193 | | static int mac_key_fromdata(MAC_KEY *key, const struct mac_common_params_st *p) |
194 | 0 | { |
195 | 0 | if (p->key != NULL) { |
196 | 0 | if (p->key->data_type != OSSL_PARAM_OCTET_STRING) { |
197 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); |
198 | 0 | return 0; |
199 | 0 | } |
200 | 0 | OPENSSL_secure_clear_free(key->priv_key, key->priv_key_len); |
201 | | /* allocate at least one byte to distinguish empty key from no key set */ |
202 | 0 | key->priv_key = OPENSSL_secure_malloc(p->key->data_size > 0 |
203 | 0 | ? p->key->data_size |
204 | 0 | : 1); |
205 | 0 | if (key->priv_key == NULL) |
206 | 0 | return 0; |
207 | 0 | memcpy(key->priv_key, p->key->data, p->key->data_size); |
208 | 0 | key->priv_key_len = p->key->data_size; |
209 | 0 | } |
210 | | |
211 | 0 | if (p->propq != NULL) { |
212 | 0 | if (p->propq->data_type != OSSL_PARAM_UTF8_STRING) { |
213 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); |
214 | 0 | return 0; |
215 | 0 | } |
216 | 0 | OPENSSL_free(key->properties); |
217 | 0 | key->properties = OPENSSL_strdup(p->propq->data); |
218 | 0 | if (key->properties == NULL) |
219 | 0 | return 0; |
220 | 0 | } |
221 | | |
222 | 0 | if (key->cmac && !ossl_prov_cipher_load(&key->cipher, p->cipher, p->propq, key->libctx)) { |
223 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); |
224 | 0 | return 0; |
225 | 0 | } |
226 | | |
227 | 0 | if (key->priv_key != NULL) |
228 | 0 | return 1; |
229 | | |
230 | 0 | return 0; |
231 | 0 | } |
232 | | |
233 | | static int mac_import(void *keydata, int selection, const OSSL_PARAM params[]) |
234 | 0 | { |
235 | 0 | MAC_KEY *key = keydata; |
236 | 0 | struct mac_common_params_st p; |
237 | |
|
238 | 0 | if (key == NULL |
239 | 0 | || (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) == 0 |
240 | 0 | || !ossl_prov_is_running() |
241 | 0 | || !mac_import_decoder(params, &p)) |
242 | 0 | return 0; |
243 | | |
244 | 0 | return mac_key_fromdata(key, &p); |
245 | 0 | } |
246 | | |
247 | | static const OSSL_PARAM *mac_imexport_types(int selection) |
248 | 0 | { |
249 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) |
250 | 0 | return mac_import_list; |
251 | 0 | return NULL; |
252 | 0 | } |
253 | | |
254 | | static int cmac_import(void *keydata, int selection, const OSSL_PARAM params[]) |
255 | 0 | { |
256 | 0 | MAC_KEY *key = keydata; |
257 | 0 | struct mac_common_params_st p; |
258 | |
|
259 | 0 | if (key == NULL |
260 | 0 | || (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) == 0 |
261 | 0 | || !ossl_prov_is_running() |
262 | 0 | || !cmac_import_decoder(params, &p)) |
263 | 0 | return 0; |
264 | | |
265 | 0 | return mac_key_fromdata(key, &p); |
266 | 0 | } |
267 | | |
268 | | static const OSSL_PARAM *cmac_imexport_types(int selection) |
269 | 0 | { |
270 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) |
271 | 0 | return cmac_import_list; |
272 | 0 | return NULL; |
273 | 0 | } |
274 | | |
275 | | static int key_to_params(MAC_KEY *key, OSSL_PARAM_BLD *tmpl, |
276 | | struct mac_common_params_st *p) |
277 | 0 | { |
278 | 0 | struct mac_common_params_st empty; |
279 | |
|
280 | 0 | if (p == NULL) |
281 | 0 | memset(p = &empty, 0, sizeof(empty)); |
282 | |
|
283 | 0 | if (key->priv_key != NULL |
284 | 0 | && !ossl_param_build_set_octet_string(tmpl, p->key, |
285 | 0 | OSSL_PKEY_PARAM_PRIV_KEY, |
286 | 0 | key->priv_key, key->priv_key_len)) |
287 | 0 | return 0; |
288 | | |
289 | 0 | if (key->cipher.cipher != NULL |
290 | 0 | && !ossl_param_build_set_utf8_string(tmpl, p->cipher, |
291 | 0 | OSSL_PKEY_PARAM_CIPHER, |
292 | 0 | EVP_CIPHER_get0_name(key->cipher.cipher))) |
293 | 0 | return 0; |
294 | | |
295 | 0 | return 1; |
296 | 0 | } |
297 | | |
298 | | static int mac_export(void *keydata, int selection, OSSL_CALLBACK *param_cb, |
299 | | void *cbarg) |
300 | 0 | { |
301 | 0 | MAC_KEY *key = keydata; |
302 | 0 | OSSL_PARAM_BLD *tmpl; |
303 | 0 | OSSL_PARAM *params = NULL; |
304 | 0 | int ret = 0; |
305 | |
|
306 | 0 | if (!ossl_prov_is_running() || key == NULL) |
307 | 0 | return 0; |
308 | | |
309 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) == 0) |
310 | 0 | return 0; |
311 | | |
312 | 0 | tmpl = OSSL_PARAM_BLD_new(); |
313 | 0 | if (tmpl == NULL) |
314 | 0 | return 0; |
315 | | |
316 | 0 | if (!key_to_params(key, tmpl, NULL)) |
317 | 0 | goto err; |
318 | | |
319 | 0 | params = OSSL_PARAM_BLD_to_param(tmpl); |
320 | 0 | if (params == NULL) |
321 | 0 | goto err; |
322 | | |
323 | 0 | ret = param_cb(params, cbarg); |
324 | 0 | OSSL_PARAM_clear_free(params); |
325 | 0 | err: |
326 | 0 | OSSL_PARAM_BLD_free(tmpl); |
327 | 0 | return ret; |
328 | 0 | } |
329 | | |
330 | | static int mac_get_params(void *keydata, OSSL_PARAM params[]) |
331 | 0 | { |
332 | 0 | struct mac_common_params_st p; |
333 | 0 | MAC_KEY *key = keydata; |
334 | |
|
335 | 0 | if (key == NULL || !mac_get_params_decoder(params, &p)) |
336 | 0 | return 0; |
337 | | |
338 | 0 | return key_to_params(key, NULL, &p); |
339 | 0 | } |
340 | | |
341 | | static const OSSL_PARAM *mac_gettable_params(void *provctx) |
342 | 0 | { |
343 | 0 | return mac_get_params_list; |
344 | 0 | } |
345 | | |
346 | | static int cmac_get_params(void *keydata, OSSL_PARAM params[]) |
347 | 0 | { |
348 | 0 | struct mac_common_params_st p; |
349 | 0 | MAC_KEY *key = keydata; |
350 | |
|
351 | 0 | if (key == NULL || !cmac_get_params_decoder(params, &p)) |
352 | 0 | return 0; |
353 | | |
354 | 0 | return key_to_params(key, NULL, &p); |
355 | 0 | } |
356 | | |
357 | | static const OSSL_PARAM *cmac_gettable_params(void *provctx) |
358 | 0 | { |
359 | 0 | return cmac_get_params_list; |
360 | 0 | } |
361 | | |
362 | | static int mac_set_params(void *keydata, const OSSL_PARAM params[]) |
363 | 0 | { |
364 | 0 | MAC_KEY *key = keydata; |
365 | 0 | struct mac_common_params_st p; |
366 | |
|
367 | 0 | if (key == NULL || !mac_set_params_decoder(params, &p)) |
368 | 0 | return 0; |
369 | | |
370 | 0 | if (p.key != NULL) |
371 | 0 | return mac_key_fromdata(key, &p); |
372 | | |
373 | 0 | return 1; |
374 | 0 | } |
375 | | |
376 | | static const OSSL_PARAM *mac_settable_params(void *provctx) |
377 | 0 | { |
378 | 0 | return mac_set_params_list; |
379 | 0 | } |
380 | | |
381 | | static void *mac_gen_init_common(void *provctx, int selection) |
382 | 0 | { |
383 | 0 | OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx); |
384 | 0 | struct mac_gen_ctx *gctx = NULL; |
385 | |
|
386 | 0 | if (!ossl_prov_is_running()) |
387 | 0 | return NULL; |
388 | | |
389 | 0 | if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) { |
390 | 0 | gctx->libctx = libctx; |
391 | 0 | gctx->selection = selection; |
392 | 0 | } |
393 | 0 | return gctx; |
394 | 0 | } |
395 | | |
396 | | static void *mac_gen_init(void *provctx, int selection, |
397 | | const OSSL_PARAM params[]) |
398 | 0 | { |
399 | 0 | struct mac_gen_ctx *gctx = mac_gen_init_common(provctx, selection); |
400 | |
|
401 | 0 | if (gctx != NULL && !mac_gen_set_params(gctx, params)) { |
402 | 0 | mac_gen_cleanup(gctx); |
403 | 0 | gctx = NULL; |
404 | 0 | } |
405 | 0 | return gctx; |
406 | 0 | } |
407 | | |
408 | | static void *cmac_gen_init(void *provctx, int selection, |
409 | | const OSSL_PARAM params[]) |
410 | 0 | { |
411 | 0 | struct mac_gen_ctx *gctx = mac_gen_init_common(provctx, selection); |
412 | |
|
413 | 0 | if (gctx != NULL && !cmac_gen_set_params(gctx, params)) { |
414 | 0 | mac_gen_cleanup(gctx); |
415 | 0 | gctx = NULL; |
416 | 0 | } |
417 | 0 | return gctx; |
418 | 0 | } |
419 | | |
420 | | static int mac_gen_set_params_common(struct mac_gen_ctx *gctx, |
421 | | const struct mac_common_params_st *p) |
422 | 0 | { |
423 | 0 | if (p->key != NULL) { |
424 | 0 | if (p->key->data_type != OSSL_PARAM_OCTET_STRING) { |
425 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); |
426 | 0 | return 0; |
427 | 0 | } |
428 | 0 | gctx->priv_key = OPENSSL_secure_malloc(p->key->data_size); |
429 | 0 | if (gctx->priv_key == NULL) |
430 | 0 | return 0; |
431 | 0 | memcpy(gctx->priv_key, p->key->data, p->key->data_size); |
432 | 0 | gctx->priv_key_len = p->key->data_size; |
433 | 0 | } |
434 | | |
435 | 0 | return 1; |
436 | 0 | } |
437 | | |
438 | | static int mac_gen_set_params(void *genctx, const OSSL_PARAM params[]) |
439 | 0 | { |
440 | 0 | struct mac_gen_ctx *gctx = genctx; |
441 | 0 | struct mac_common_params_st p; |
442 | |
|
443 | 0 | if (gctx == NULL || !mac_gen_set_params_decoder(params, &p)) |
444 | 0 | return 0; |
445 | | |
446 | 0 | return mac_gen_set_params_common(gctx, &p); |
447 | 0 | } |
448 | | |
449 | | static int cmac_gen_set_params(void *genctx, const OSSL_PARAM params[]) |
450 | 0 | { |
451 | 0 | struct mac_gen_ctx *gctx = genctx; |
452 | 0 | struct mac_common_params_st p; |
453 | |
|
454 | 0 | if (gctx == NULL || !cmac_gen_set_params_decoder(params, &p)) |
455 | 0 | return 0; |
456 | | |
457 | 0 | if (!mac_gen_set_params_common(gctx, &p)) |
458 | 0 | return 0; |
459 | | |
460 | 0 | if (!ossl_prov_cipher_load(&gctx->cipher, p.cipher, p.propq, |
461 | 0 | gctx->libctx)) { |
462 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); |
463 | 0 | return 0; |
464 | 0 | } |
465 | | |
466 | 0 | return 1; |
467 | 0 | } |
468 | | |
469 | | static const OSSL_PARAM *mac_gen_settable_params(ossl_unused void *genctx, |
470 | | ossl_unused void *provctx) |
471 | 0 | { |
472 | 0 | return mac_gen_set_params_list; |
473 | 0 | } |
474 | | |
475 | | static const OSSL_PARAM *cmac_gen_settable_params(ossl_unused void *genctx, |
476 | | ossl_unused void *provctx) |
477 | 0 | { |
478 | 0 | return cmac_gen_set_params_list; |
479 | 0 | } |
480 | | |
481 | | static void *mac_gen(void *genctx, OSSL_CALLBACK *cb, void *cbarg) |
482 | 0 | { |
483 | 0 | struct mac_gen_ctx *gctx = genctx; |
484 | 0 | MAC_KEY *key; |
485 | |
|
486 | 0 | if (!ossl_prov_is_running() || gctx == NULL) |
487 | 0 | return NULL; |
488 | | |
489 | 0 | if ((key = ossl_mac_key_new(gctx->libctx, 0)) == NULL) { |
490 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_PROV_LIB); |
491 | 0 | return NULL; |
492 | 0 | } |
493 | | |
494 | | /* If we're doing parameter generation then we just return a blank key */ |
495 | 0 | if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0) |
496 | 0 | return key; |
497 | | |
498 | 0 | if (gctx->priv_key == NULL) { |
499 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY); |
500 | 0 | ossl_mac_key_free(key); |
501 | 0 | return NULL; |
502 | 0 | } |
503 | | |
504 | | /* |
505 | | * This is horrible but required for backwards compatibility. We don't |
506 | | * actually do real key generation at all. We simply copy the key that was |
507 | | * previously set in the gctx. Hopefully at some point in the future all |
508 | | * of this can be removed and we will only support the EVP_KDF APIs. |
509 | | */ |
510 | 0 | if (!ossl_prov_cipher_copy(&key->cipher, &gctx->cipher)) { |
511 | 0 | ossl_mac_key_free(key); |
512 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR); |
513 | 0 | return NULL; |
514 | 0 | } |
515 | 0 | ossl_prov_cipher_reset(&gctx->cipher); |
516 | 0 | key->priv_key = gctx->priv_key; |
517 | 0 | key->priv_key_len = gctx->priv_key_len; |
518 | 0 | gctx->priv_key_len = 0; |
519 | 0 | gctx->priv_key = NULL; |
520 | |
|
521 | 0 | return key; |
522 | 0 | } |
523 | | |
524 | | static void mac_gen_cleanup(void *genctx) |
525 | 0 | { |
526 | 0 | struct mac_gen_ctx *gctx = genctx; |
527 | |
|
528 | 0 | if (gctx == NULL) |
529 | 0 | return; |
530 | | |
531 | 0 | OPENSSL_secure_clear_free(gctx->priv_key, gctx->priv_key_len); |
532 | 0 | ossl_prov_cipher_reset(&gctx->cipher); |
533 | 0 | OPENSSL_free(gctx); |
534 | 0 | } |
535 | | |
536 | | const OSSL_DISPATCH ossl_mac_legacy_keymgmt_functions[] = { |
537 | | { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))mac_new }, |
538 | | { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))mac_free }, |
539 | | { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*)(void))mac_get_params }, |
540 | | { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*)(void))mac_gettable_params }, |
541 | | { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*)(void))mac_set_params }, |
542 | | { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*)(void))mac_settable_params }, |
543 | | { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))mac_has }, |
544 | | { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))mac_match }, |
545 | | { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))mac_import }, |
546 | | { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))mac_imexport_types }, |
547 | | { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))mac_export }, |
548 | | { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))mac_imexport_types }, |
549 | | { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))mac_gen_init }, |
550 | | { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))mac_gen_set_params }, |
551 | | { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, |
552 | | (void (*)(void))mac_gen_settable_params }, |
553 | | { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))mac_gen }, |
554 | | { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))mac_gen_cleanup }, |
555 | | OSSL_DISPATCH_END |
556 | | }; |
557 | | |
558 | | const OSSL_DISPATCH ossl_cmac_legacy_keymgmt_functions[] = { |
559 | | { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))mac_new_cmac }, |
560 | | { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))mac_free }, |
561 | | { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*)(void))cmac_get_params }, |
562 | | { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*)(void))cmac_gettable_params }, |
563 | | { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*)(void))mac_set_params }, |
564 | | { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*)(void))mac_settable_params }, |
565 | | { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))mac_has }, |
566 | | { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))mac_match }, |
567 | | { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))cmac_import }, |
568 | | { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))cmac_imexport_types }, |
569 | | { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))mac_export }, |
570 | | { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))cmac_imexport_types }, |
571 | | { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))cmac_gen_init }, |
572 | | { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))cmac_gen_set_params }, |
573 | | { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, |
574 | | (void (*)(void))cmac_gen_settable_params }, |
575 | | { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))mac_gen }, |
576 | | { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))mac_gen_cleanup }, |
577 | | OSSL_DISPATCH_END |
578 | | }; |