/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 | return CRYPTO_UP_REF(&mackey->refcnt, &ref); |
114 | 0 | } |
115 | | |
116 | | static void *mac_new(void *provctx) |
117 | 0 | { |
118 | 0 | return ossl_mac_key_new(PROV_LIBCTX_OF(provctx), 0); |
119 | 0 | } |
120 | | |
121 | | static void *mac_new_cmac(void *provctx) |
122 | 0 | { |
123 | 0 | return ossl_mac_key_new(PROV_LIBCTX_OF(provctx), 1); |
124 | 0 | } |
125 | | |
126 | | static void mac_free(void *mackey) |
127 | 0 | { |
128 | 0 | ossl_mac_key_free(mackey); |
129 | 0 | } |
130 | | |
131 | | static int mac_has(const void *keydata, int selection) |
132 | 0 | { |
133 | 0 | const MAC_KEY *key = keydata; |
134 | 0 | int ok = 0; |
135 | |
|
136 | 0 | if (ossl_prov_is_running() && key != NULL) { |
137 | | /* |
138 | | * MAC keys always have all the parameters they need (i.e. none). |
139 | | * Therefore we always return with 1, if asked about parameters. |
140 | | * Similarly for public keys. |
141 | | */ |
142 | 0 | ok = 1; |
143 | |
|
144 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) |
145 | 0 | ok = key->priv_key != NULL; |
146 | 0 | } |
147 | 0 | return ok; |
148 | 0 | } |
149 | | |
150 | | static int mac_match(const void *keydata1, const void *keydata2, int selection) |
151 | 0 | { |
152 | 0 | const MAC_KEY *key1 = keydata1; |
153 | 0 | const MAC_KEY *key2 = keydata2; |
154 | 0 | int ok = 1; |
155 | |
|
156 | 0 | if (!ossl_prov_is_running()) |
157 | 0 | return 0; |
158 | | |
159 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) { |
160 | 0 | if ((key1->priv_key == NULL && key2->priv_key != NULL) |
161 | 0 | || (key1->priv_key != NULL && key2->priv_key == NULL) |
162 | 0 | || key1->priv_key_len != key2->priv_key_len |
163 | 0 | || (key1->cipher.cipher == NULL && key2->cipher.cipher != NULL) |
164 | 0 | || (key1->cipher.cipher != NULL && key2->cipher.cipher == NULL)) |
165 | 0 | ok = 0; |
166 | 0 | else |
167 | 0 | ok = ok && (key1->priv_key == NULL /* implies key2->privkey == NULL */ |
168 | 0 | || CRYPTO_memcmp(key1->priv_key, key2->priv_key, key1->priv_key_len) == 0); |
169 | 0 | if (key1->cipher.cipher != NULL) |
170 | 0 | ok = ok && EVP_CIPHER_is_a(key1->cipher.cipher, EVP_CIPHER_get0_name(key2->cipher.cipher)); |
171 | 0 | } |
172 | 0 | return ok; |
173 | 0 | } |
174 | | |
175 | | /* Common structure to handle the MAC parameters for various calls */ |
176 | | struct mac_common_params_st { |
177 | | OSSL_PARAM *key; |
178 | | OSSL_PARAM *cipher; /* CMAC */ |
179 | | OSSL_PARAM *propq; |
180 | | }; |
181 | | |
182 | | #define mac_import_st mac_common_params_st |
183 | | #define cmac_import_st mac_common_params_st |
184 | | #define mac_get_params_st mac_common_params_st |
185 | | #define cmac_get_params_st mac_common_params_st |
186 | | #define mac_set_params_st mac_common_params_st |
187 | | #define mac_gen_set_params_st mac_common_params_st |
188 | | #define cmac_gen_set_params_st mac_common_params_st |
189 | | |
190 | | #include "providers/implementations/keymgmt/mac_legacy_kmgmt.inc" |
191 | | |
192 | | static int mac_key_fromdata(MAC_KEY *key, const struct mac_common_params_st *p) |
193 | 0 | { |
194 | 0 | if (p->key != NULL) { |
195 | 0 | if (p->key->data_type != OSSL_PARAM_OCTET_STRING) { |
196 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); |
197 | 0 | return 0; |
198 | 0 | } |
199 | 0 | OPENSSL_secure_clear_free(key->priv_key, key->priv_key_len); |
200 | | /* allocate at least one byte to distinguish empty key from no key set */ |
201 | 0 | key->priv_key = OPENSSL_secure_malloc(p->key->data_size > 0 |
202 | 0 | ? p->key->data_size |
203 | 0 | : 1); |
204 | 0 | if (key->priv_key == NULL) |
205 | 0 | return 0; |
206 | 0 | memcpy(key->priv_key, p->key->data, p->key->data_size); |
207 | 0 | key->priv_key_len = p->key->data_size; |
208 | 0 | } |
209 | | |
210 | 0 | if (p->propq != NULL) { |
211 | 0 | if (p->propq->data_type != OSSL_PARAM_UTF8_STRING) { |
212 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); |
213 | 0 | return 0; |
214 | 0 | } |
215 | 0 | OPENSSL_free(key->properties); |
216 | 0 | key->properties = OPENSSL_strdup(p->propq->data); |
217 | 0 | if (key->properties == NULL) |
218 | 0 | return 0; |
219 | 0 | } |
220 | | |
221 | 0 | if (key->cmac && !ossl_prov_cipher_load(&key->cipher, p->cipher, p->propq, key->libctx)) { |
222 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); |
223 | 0 | return 0; |
224 | 0 | } |
225 | | |
226 | 0 | if (key->priv_key != NULL) |
227 | 0 | return 1; |
228 | | |
229 | 0 | return 0; |
230 | 0 | } |
231 | | |
232 | | static int mac_import(void *keydata, int selection, const OSSL_PARAM params[]) |
233 | 0 | { |
234 | 0 | MAC_KEY *key = keydata; |
235 | 0 | struct mac_common_params_st p; |
236 | |
|
237 | 0 | if (key == NULL |
238 | 0 | || (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) == 0 |
239 | 0 | || !ossl_prov_is_running() |
240 | 0 | || !mac_import_decoder(params, &p)) |
241 | 0 | return 0; |
242 | | |
243 | 0 | return mac_key_fromdata(key, &p); |
244 | 0 | } |
245 | | |
246 | | static const OSSL_PARAM *mac_imexport_types(int selection) |
247 | 0 | { |
248 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) |
249 | 0 | return mac_import_list; |
250 | 0 | return NULL; |
251 | 0 | } |
252 | | |
253 | | static int cmac_import(void *keydata, int selection, const OSSL_PARAM params[]) |
254 | 0 | { |
255 | 0 | MAC_KEY *key = keydata; |
256 | 0 | struct mac_common_params_st p; |
257 | |
|
258 | 0 | if (key == NULL |
259 | 0 | || (selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) == 0 |
260 | 0 | || !ossl_prov_is_running() |
261 | 0 | || !cmac_import_decoder(params, &p)) |
262 | 0 | return 0; |
263 | | |
264 | 0 | return mac_key_fromdata(key, &p); |
265 | 0 | } |
266 | | |
267 | | static const OSSL_PARAM *cmac_imexport_types(int selection) |
268 | 0 | { |
269 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) |
270 | 0 | return cmac_import_list; |
271 | 0 | return NULL; |
272 | 0 | } |
273 | | |
274 | | static int key_to_params(MAC_KEY *key, OSSL_PARAM_BLD *tmpl, |
275 | | struct mac_common_params_st *p) |
276 | 0 | { |
277 | 0 | struct mac_common_params_st empty; |
278 | |
|
279 | 0 | if (p == NULL) |
280 | 0 | memset(p = &empty, 0, sizeof(empty)); |
281 | |
|
282 | 0 | if (key->priv_key != NULL |
283 | 0 | && !ossl_param_build_set_octet_string(tmpl, p->key, |
284 | 0 | OSSL_PKEY_PARAM_PRIV_KEY, |
285 | 0 | key->priv_key, key->priv_key_len)) |
286 | 0 | return 0; |
287 | | |
288 | 0 | if (key->cipher.cipher != NULL |
289 | 0 | && !ossl_param_build_set_utf8_string(tmpl, p->cipher, |
290 | 0 | OSSL_PKEY_PARAM_CIPHER, |
291 | 0 | EVP_CIPHER_get0_name(key->cipher.cipher))) |
292 | 0 | return 0; |
293 | | |
294 | 0 | return 1; |
295 | 0 | } |
296 | | |
297 | | static int mac_export(void *keydata, int selection, OSSL_CALLBACK *param_cb, |
298 | | void *cbarg) |
299 | 0 | { |
300 | 0 | MAC_KEY *key = keydata; |
301 | 0 | OSSL_PARAM_BLD *tmpl; |
302 | 0 | OSSL_PARAM *params = NULL; |
303 | 0 | int ret = 0; |
304 | |
|
305 | 0 | if (!ossl_prov_is_running() || key == NULL) |
306 | 0 | return 0; |
307 | | |
308 | 0 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) == 0) |
309 | 0 | return 0; |
310 | | |
311 | 0 | tmpl = OSSL_PARAM_BLD_new(); |
312 | 0 | if (tmpl == NULL) |
313 | 0 | return 0; |
314 | | |
315 | 0 | if (!key_to_params(key, tmpl, NULL)) |
316 | 0 | goto err; |
317 | | |
318 | 0 | params = OSSL_PARAM_BLD_to_param(tmpl); |
319 | 0 | if (params == NULL) |
320 | 0 | goto err; |
321 | | |
322 | 0 | ret = param_cb(params, cbarg); |
323 | 0 | OSSL_PARAM_clear_free(params); |
324 | 0 | err: |
325 | 0 | OSSL_PARAM_BLD_free(tmpl); |
326 | 0 | return ret; |
327 | 0 | } |
328 | | |
329 | | static int mac_get_params(void *keydata, OSSL_PARAM params[]) |
330 | 0 | { |
331 | 0 | struct mac_common_params_st p; |
332 | 0 | MAC_KEY *key = keydata; |
333 | |
|
334 | 0 | if (key == NULL || !mac_get_params_decoder(params, &p)) |
335 | 0 | return 0; |
336 | | |
337 | 0 | return key_to_params(key, NULL, &p); |
338 | 0 | } |
339 | | |
340 | | static const OSSL_PARAM *mac_gettable_params(void *provctx) |
341 | 0 | { |
342 | 0 | return mac_get_params_list; |
343 | 0 | } |
344 | | |
345 | | static int cmac_get_params(void *keydata, OSSL_PARAM params[]) |
346 | 0 | { |
347 | 0 | struct mac_common_params_st p; |
348 | 0 | MAC_KEY *key = keydata; |
349 | |
|
350 | 0 | if (key == NULL || !cmac_get_params_decoder(params, &p)) |
351 | 0 | return 0; |
352 | | |
353 | 0 | return key_to_params(key, NULL, &p); |
354 | 0 | } |
355 | | |
356 | | static const OSSL_PARAM *cmac_gettable_params(void *provctx) |
357 | 0 | { |
358 | 0 | return cmac_get_params_list; |
359 | 0 | } |
360 | | |
361 | | static int mac_set_params(void *keydata, const OSSL_PARAM params[]) |
362 | 0 | { |
363 | 0 | MAC_KEY *key = keydata; |
364 | 0 | struct mac_common_params_st p; |
365 | |
|
366 | 0 | if (key == NULL || !mac_set_params_decoder(params, &p)) |
367 | 0 | return 0; |
368 | | |
369 | 0 | if (p.key != NULL) |
370 | 0 | return mac_key_fromdata(key, &p); |
371 | | |
372 | 0 | return 1; |
373 | 0 | } |
374 | | |
375 | | static const OSSL_PARAM *mac_settable_params(void *provctx) |
376 | 0 | { |
377 | 0 | return mac_set_params_list; |
378 | 0 | } |
379 | | |
380 | | static void *mac_gen_init_common(void *provctx, int selection) |
381 | 0 | { |
382 | 0 | OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx); |
383 | 0 | struct mac_gen_ctx *gctx = NULL; |
384 | |
|
385 | 0 | if (!ossl_prov_is_running()) |
386 | 0 | return NULL; |
387 | | |
388 | 0 | if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL) { |
389 | 0 | gctx->libctx = libctx; |
390 | 0 | gctx->selection = selection; |
391 | 0 | } |
392 | 0 | return gctx; |
393 | 0 | } |
394 | | |
395 | | static void *mac_gen_init(void *provctx, int selection, |
396 | | const OSSL_PARAM params[]) |
397 | 0 | { |
398 | 0 | struct mac_gen_ctx *gctx = mac_gen_init_common(provctx, selection); |
399 | |
|
400 | 0 | if (gctx != NULL && !mac_gen_set_params(gctx, params)) { |
401 | 0 | mac_gen_cleanup(gctx); |
402 | 0 | gctx = NULL; |
403 | 0 | } |
404 | 0 | return gctx; |
405 | 0 | } |
406 | | |
407 | | static void *cmac_gen_init(void *provctx, int selection, |
408 | | const OSSL_PARAM params[]) |
409 | 0 | { |
410 | 0 | struct mac_gen_ctx *gctx = mac_gen_init_common(provctx, selection); |
411 | |
|
412 | 0 | if (gctx != NULL && !cmac_gen_set_params(gctx, params)) { |
413 | 0 | mac_gen_cleanup(gctx); |
414 | 0 | gctx = NULL; |
415 | 0 | } |
416 | 0 | return gctx; |
417 | 0 | } |
418 | | |
419 | | static int mac_gen_set_params_common(struct mac_gen_ctx *gctx, |
420 | | const struct mac_common_params_st *p) |
421 | 0 | { |
422 | 0 | if (p->key != NULL) { |
423 | 0 | if (p->key->data_type != OSSL_PARAM_OCTET_STRING) { |
424 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); |
425 | 0 | return 0; |
426 | 0 | } |
427 | 0 | gctx->priv_key = OPENSSL_secure_malloc(p->key->data_size); |
428 | 0 | if (gctx->priv_key == NULL) |
429 | 0 | return 0; |
430 | 0 | memcpy(gctx->priv_key, p->key->data, p->key->data_size); |
431 | 0 | gctx->priv_key_len = p->key->data_size; |
432 | 0 | } |
433 | | |
434 | 0 | return 1; |
435 | 0 | } |
436 | | |
437 | | static int mac_gen_set_params(void *genctx, const OSSL_PARAM params[]) |
438 | 0 | { |
439 | 0 | struct mac_gen_ctx *gctx = genctx; |
440 | 0 | struct mac_common_params_st p; |
441 | |
|
442 | 0 | if (gctx == NULL || !mac_gen_set_params_decoder(params, &p)) |
443 | 0 | return 0; |
444 | | |
445 | 0 | return mac_gen_set_params_common(gctx, &p); |
446 | 0 | } |
447 | | |
448 | | static int cmac_gen_set_params(void *genctx, const OSSL_PARAM params[]) |
449 | 0 | { |
450 | 0 | struct mac_gen_ctx *gctx = genctx; |
451 | 0 | struct mac_common_params_st p; |
452 | |
|
453 | 0 | if (gctx == NULL || !cmac_gen_set_params_decoder(params, &p)) |
454 | 0 | return 0; |
455 | | |
456 | 0 | if (!mac_gen_set_params_common(gctx, &p)) |
457 | 0 | return 0; |
458 | | |
459 | 0 | if (!ossl_prov_cipher_load(&gctx->cipher, p.cipher, p.propq, |
460 | 0 | gctx->libctx)) { |
461 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); |
462 | 0 | return 0; |
463 | 0 | } |
464 | | |
465 | 0 | return 1; |
466 | 0 | } |
467 | | |
468 | | static const OSSL_PARAM *mac_gen_settable_params(ossl_unused void *genctx, |
469 | | ossl_unused void *provctx) |
470 | 0 | { |
471 | 0 | return mac_gen_set_params_list; |
472 | 0 | } |
473 | | |
474 | | static const OSSL_PARAM *cmac_gen_settable_params(ossl_unused void *genctx, |
475 | | ossl_unused void *provctx) |
476 | 0 | { |
477 | 0 | return cmac_gen_set_params_list; |
478 | 0 | } |
479 | | |
480 | | static void *mac_gen(void *genctx, OSSL_CALLBACK *cb, void *cbarg) |
481 | 0 | { |
482 | 0 | struct mac_gen_ctx *gctx = genctx; |
483 | 0 | MAC_KEY *key; |
484 | |
|
485 | 0 | if (!ossl_prov_is_running() || gctx == NULL) |
486 | 0 | return NULL; |
487 | | |
488 | 0 | if ((key = ossl_mac_key_new(gctx->libctx, 0)) == NULL) { |
489 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_PROV_LIB); |
490 | 0 | return NULL; |
491 | 0 | } |
492 | | |
493 | | /* If we're doing parameter generation then we just return a blank key */ |
494 | 0 | if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0) |
495 | 0 | return key; |
496 | | |
497 | 0 | if (gctx->priv_key == NULL) { |
498 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY); |
499 | 0 | ossl_mac_key_free(key); |
500 | 0 | return NULL; |
501 | 0 | } |
502 | | |
503 | | /* |
504 | | * This is horrible but required for backwards compatibility. We don't |
505 | | * actually do real key generation at all. We simply copy the key that was |
506 | | * previously set in the gctx. Hopefully at some point in the future all |
507 | | * of this can be removed and we will only support the EVP_KDF APIs. |
508 | | */ |
509 | 0 | if (!ossl_prov_cipher_copy(&key->cipher, &gctx->cipher)) { |
510 | 0 | ossl_mac_key_free(key); |
511 | 0 | ERR_raise(ERR_LIB_PROV, ERR_R_INTERNAL_ERROR); |
512 | 0 | return NULL; |
513 | 0 | } |
514 | 0 | ossl_prov_cipher_reset(&gctx->cipher); |
515 | 0 | key->priv_key = gctx->priv_key; |
516 | 0 | key->priv_key_len = gctx->priv_key_len; |
517 | 0 | gctx->priv_key_len = 0; |
518 | 0 | gctx->priv_key = NULL; |
519 | |
|
520 | 0 | return key; |
521 | 0 | } |
522 | | |
523 | | static void mac_gen_cleanup(void *genctx) |
524 | 0 | { |
525 | 0 | struct mac_gen_ctx *gctx = genctx; |
526 | |
|
527 | 0 | if (gctx == NULL) |
528 | 0 | return; |
529 | | |
530 | 0 | OPENSSL_secure_clear_free(gctx->priv_key, gctx->priv_key_len); |
531 | 0 | ossl_prov_cipher_reset(&gctx->cipher); |
532 | 0 | OPENSSL_free(gctx); |
533 | 0 | } |
534 | | |
535 | | const OSSL_DISPATCH ossl_mac_legacy_keymgmt_functions[] = { |
536 | | { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))mac_new }, |
537 | | { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))mac_free }, |
538 | | { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*)(void))mac_get_params }, |
539 | | { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*)(void))mac_gettable_params }, |
540 | | { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*)(void))mac_set_params }, |
541 | | { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*)(void))mac_settable_params }, |
542 | | { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))mac_has }, |
543 | | { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))mac_match }, |
544 | | { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))mac_import }, |
545 | | { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))mac_imexport_types }, |
546 | | { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))mac_export }, |
547 | | { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))mac_imexport_types }, |
548 | | { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))mac_gen_init }, |
549 | | { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))mac_gen_set_params }, |
550 | | { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, |
551 | | (void (*)(void))mac_gen_settable_params }, |
552 | | { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))mac_gen }, |
553 | | { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))mac_gen_cleanup }, |
554 | | OSSL_DISPATCH_END |
555 | | }; |
556 | | |
557 | | const OSSL_DISPATCH ossl_cmac_legacy_keymgmt_functions[] = { |
558 | | { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))mac_new_cmac }, |
559 | | { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))mac_free }, |
560 | | { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*)(void))cmac_get_params }, |
561 | | { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*)(void))cmac_gettable_params }, |
562 | | { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*)(void))mac_set_params }, |
563 | | { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*)(void))mac_settable_params }, |
564 | | { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))mac_has }, |
565 | | { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))mac_match }, |
566 | | { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))cmac_import }, |
567 | | { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))cmac_imexport_types }, |
568 | | { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))mac_export }, |
569 | | { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))cmac_imexport_types }, |
570 | | { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))cmac_gen_init }, |
571 | | { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))cmac_gen_set_params }, |
572 | | { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS, |
573 | | (void (*)(void))cmac_gen_settable_params }, |
574 | | { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))mac_gen }, |
575 | | { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))mac_gen_cleanup }, |
576 | | OSSL_DISPATCH_END |
577 | | }; |