/src/openssl/crypto/evp/kem.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2020-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 | | |
10 | | #include <stdio.h> |
11 | | #include <stdlib.h> |
12 | | #include <openssl/objects.h> |
13 | | #include <openssl/evp.h> |
14 | | #include "internal/cryptlib.h" |
15 | | #include "internal/provider.h" |
16 | | #include "internal/core.h" |
17 | | #include "crypto/evp.h" |
18 | | #include "evp_local.h" |
19 | | |
20 | | static void evp_kem_free(void *data) |
21 | 0 | { |
22 | 0 | EVP_KEM *kem = (EVP_KEM *)data; |
23 | 0 | int i; |
24 | |
|
25 | 0 | if (kem == NULL) |
26 | 0 | return; |
27 | | |
28 | 0 | CRYPTO_DOWN_REF(&kem->refcnt, &i); |
29 | 0 | if (i > 0) |
30 | 0 | return; |
31 | 0 | OPENSSL_free(kem->type_name); |
32 | 0 | ossl_provider_free(kem->prov); |
33 | 0 | CRYPTO_FREE_REF(&kem->refcnt); |
34 | 0 | OPENSSL_free(kem); |
35 | 0 | } |
36 | | |
37 | | static int evp_kem_up_ref(void *data) |
38 | 0 | { |
39 | 0 | EVP_KEM *kem = (EVP_KEM *)data; |
40 | 0 | int ref = 0; |
41 | |
|
42 | 0 | return CRYPTO_UP_REF(&kem->refcnt, &ref); |
43 | 0 | } |
44 | | |
45 | | static int evp_kem_init(EVP_PKEY_CTX *ctx, int operation, |
46 | | const OSSL_PARAM params[], EVP_PKEY *authkey) |
47 | 0 | { |
48 | 0 | int ret = 0; |
49 | 0 | EVP_KEM *kem = NULL; |
50 | 0 | EVP_KEYMGMT *tmp_keymgmt = NULL; |
51 | 0 | const OSSL_PROVIDER *tmp_prov = NULL; |
52 | 0 | void *provkey = NULL, *provauthkey = NULL; |
53 | 0 | const char *supported_kem = NULL; |
54 | 0 | int iter; |
55 | |
|
56 | 0 | if (ctx == NULL || ctx->keytype == NULL) { |
57 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
58 | 0 | return 0; |
59 | 0 | } |
60 | | |
61 | 0 | evp_pkey_ctx_free_old_ops(ctx); |
62 | 0 | ctx->operation = operation; |
63 | |
|
64 | 0 | if (ctx->pkey == NULL) { |
65 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET); |
66 | 0 | goto err; |
67 | 0 | } |
68 | 0 | if (authkey != NULL && authkey->type != ctx->pkey->type) { |
69 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES); |
70 | 0 | return 0; |
71 | 0 | } |
72 | | /* |
73 | | * Try to derive the supported kem from |ctx->keymgmt|. |
74 | | */ |
75 | 0 | if (!ossl_assert(ctx->pkey->keymgmt == NULL |
76 | 0 | || ctx->pkey->keymgmt == ctx->keymgmt)) { |
77 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); |
78 | 0 | goto err; |
79 | 0 | } |
80 | 0 | supported_kem = evp_keymgmt_util_query_operation_name(ctx->keymgmt, |
81 | 0 | OSSL_OP_KEM); |
82 | 0 | if (supported_kem == NULL) { |
83 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
84 | 0 | goto err; |
85 | 0 | } |
86 | | |
87 | | /* |
88 | | * Because we cleared out old ops, we shouldn't need to worry about |
89 | | * checking if kem is already there. |
90 | | * We perform two iterations: |
91 | | * |
92 | | * 1. Do the normal kem fetch, using the fetching data given by |
93 | | * the EVP_PKEY_CTX. |
94 | | * 2. Do the provider specific kem fetch, from the same provider |
95 | | * as |ctx->keymgmt| |
96 | | * |
97 | | * We then try to fetch the keymgmt from the same provider as the |
98 | | * kem, and try to export |ctx->pkey| to that keymgmt (when this |
99 | | * keymgmt happens to be the same as |ctx->keymgmt|, the export is |
100 | | * a no-op, but we call it anyway to not complicate the code even |
101 | | * more). |
102 | | * If the export call succeeds (returns a non-NULL provider key pointer), |
103 | | * we're done and can perform the operation itself. If not, we perform |
104 | | * the second iteration, or jump to legacy. |
105 | | */ |
106 | 0 | for (iter = 1, provkey = NULL; iter < 3 && provkey == NULL; iter++) { |
107 | 0 | EVP_KEYMGMT *tmp_keymgmt_tofree = NULL; |
108 | | |
109 | | /* |
110 | | * If we're on the second iteration, free the results from the first. |
111 | | * They are NULL on the first iteration, so no need to check what |
112 | | * iteration we're on. |
113 | | */ |
114 | 0 | EVP_KEM_free(kem); |
115 | 0 | kem = NULL; |
116 | 0 | EVP_KEYMGMT_free(tmp_keymgmt); |
117 | 0 | tmp_keymgmt = NULL; |
118 | |
|
119 | 0 | switch (iter) { |
120 | 0 | case 1: |
121 | 0 | kem = EVP_KEM_fetch(ctx->libctx, supported_kem, ctx->propquery); |
122 | 0 | if (kem != NULL) |
123 | 0 | tmp_prov = EVP_KEM_get0_provider(kem); |
124 | 0 | break; |
125 | 0 | case 2: |
126 | 0 | tmp_prov = EVP_KEYMGMT_get0_provider(ctx->keymgmt); |
127 | 0 | kem = evp_kem_fetch_from_prov((OSSL_PROVIDER *)tmp_prov, |
128 | 0 | supported_kem, ctx->propquery); |
129 | |
|
130 | 0 | if (kem == NULL) { |
131 | 0 | ERR_raise(ERR_LIB_EVP, |
132 | 0 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
133 | 0 | ret = -2; |
134 | 0 | goto err; |
135 | 0 | } |
136 | 0 | } |
137 | 0 | if (kem == NULL) |
138 | 0 | continue; |
139 | | |
140 | | /* |
141 | | * Ensure that the key is provided, either natively, or as a cached |
142 | | * export. We start by fetching the keymgmt with the same name as |
143 | | * |ctx->pkey|, but from the provider of the kem method, using the |
144 | | * same property query as when fetching the kem method. |
145 | | * With the keymgmt we found (if we did), we try to export |ctx->pkey| |
146 | | * to it (evp_pkey_export_to_provider() is smart enough to only actually |
147 | | * export it if |tmp_keymgmt| is different from |ctx->pkey|'s keymgmt) |
148 | | */ |
149 | 0 | tmp_keymgmt_tofree = tmp_keymgmt = evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov, |
150 | 0 | EVP_KEYMGMT_get0_name(ctx->keymgmt), |
151 | 0 | ctx->propquery); |
152 | 0 | if (tmp_keymgmt != NULL) { |
153 | 0 | provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx, |
154 | 0 | &tmp_keymgmt, ctx->propquery); |
155 | 0 | if (provkey != NULL && authkey != NULL) { |
156 | 0 | provauthkey = evp_pkey_export_to_provider(authkey, ctx->libctx, |
157 | 0 | &tmp_keymgmt, |
158 | 0 | ctx->propquery); |
159 | 0 | if (provauthkey == NULL) { |
160 | 0 | EVP_KEM_free(kem); |
161 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
162 | 0 | goto err; |
163 | 0 | } |
164 | 0 | } |
165 | 0 | } |
166 | 0 | if (tmp_keymgmt == NULL) |
167 | 0 | EVP_KEYMGMT_free(tmp_keymgmt_tofree); |
168 | 0 | } |
169 | | |
170 | 0 | if (provkey == NULL) { |
171 | 0 | EVP_KEM_free(kem); |
172 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
173 | 0 | goto err; |
174 | 0 | } |
175 | | |
176 | 0 | ctx->op.encap.kem = kem; |
177 | 0 | ctx->op.encap.algctx = kem->newctx(ossl_provider_ctx(kem->prov)); |
178 | 0 | if (ctx->op.encap.algctx == NULL) { |
179 | | /* The provider key can stay in the cache */ |
180 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
181 | 0 | goto err; |
182 | 0 | } |
183 | | |
184 | 0 | switch (operation) { |
185 | 0 | case EVP_PKEY_OP_ENCAPSULATE: |
186 | 0 | if (provauthkey != NULL && kem->auth_encapsulate_init != NULL) { |
187 | 0 | ret = kem->auth_encapsulate_init(ctx->op.encap.algctx, provkey, |
188 | 0 | provauthkey, params); |
189 | 0 | } else if (provauthkey == NULL && kem->encapsulate_init != NULL) { |
190 | 0 | ret = kem->encapsulate_init(ctx->op.encap.algctx, provkey, params); |
191 | 0 | } else { |
192 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
193 | 0 | ret = -2; |
194 | 0 | goto err; |
195 | 0 | } |
196 | 0 | break; |
197 | 0 | case EVP_PKEY_OP_DECAPSULATE: |
198 | 0 | if (provauthkey != NULL && kem->auth_decapsulate_init != NULL) { |
199 | 0 | ret = kem->auth_decapsulate_init(ctx->op.encap.algctx, provkey, |
200 | 0 | provauthkey, params); |
201 | 0 | } else if (provauthkey == NULL && kem->decapsulate_init != NULL) { |
202 | 0 | ret = kem->decapsulate_init(ctx->op.encap.algctx, provkey, params); |
203 | 0 | } else { |
204 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
205 | 0 | ret = -2; |
206 | 0 | goto err; |
207 | 0 | } |
208 | 0 | break; |
209 | 0 | default: |
210 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
211 | 0 | goto err; |
212 | 0 | } |
213 | | |
214 | 0 | EVP_KEYMGMT_free(tmp_keymgmt); |
215 | 0 | tmp_keymgmt = NULL; |
216 | |
|
217 | 0 | if (ret > 0) |
218 | 0 | return 1; |
219 | 0 | err: |
220 | 0 | if (ret <= 0) { |
221 | 0 | evp_pkey_ctx_free_old_ops(ctx); |
222 | 0 | ctx->operation = EVP_PKEY_OP_UNDEFINED; |
223 | 0 | } |
224 | 0 | EVP_KEYMGMT_free(tmp_keymgmt); |
225 | 0 | return ret; |
226 | 0 | } |
227 | | |
228 | | int EVP_PKEY_auth_encapsulate_init(EVP_PKEY_CTX *ctx, EVP_PKEY *authpriv, |
229 | | const OSSL_PARAM params[]) |
230 | 0 | { |
231 | 0 | if (authpriv == NULL) |
232 | 0 | return 0; |
233 | 0 | return evp_kem_init(ctx, EVP_PKEY_OP_ENCAPSULATE, params, authpriv); |
234 | 0 | } |
235 | | |
236 | | int EVP_PKEY_encapsulate_init(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[]) |
237 | 0 | { |
238 | 0 | return evp_kem_init(ctx, EVP_PKEY_OP_ENCAPSULATE, params, NULL); |
239 | 0 | } |
240 | | |
241 | | int EVP_PKEY_encapsulate(EVP_PKEY_CTX *ctx, |
242 | | unsigned char *out, size_t *outlen, |
243 | | unsigned char *secret, size_t *secretlen) |
244 | 0 | { |
245 | 0 | if (ctx == NULL) |
246 | 0 | return 0; |
247 | | |
248 | 0 | if (ctx->operation != EVP_PKEY_OP_ENCAPSULATE) { |
249 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED); |
250 | 0 | return -1; |
251 | 0 | } |
252 | | |
253 | 0 | if (ctx->op.encap.algctx == NULL) { |
254 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
255 | 0 | return -2; |
256 | 0 | } |
257 | | |
258 | 0 | if (out != NULL && secret == NULL) |
259 | 0 | return 0; |
260 | | |
261 | 0 | return ctx->op.encap.kem->encapsulate(ctx->op.encap.algctx, |
262 | 0 | out, outlen, secret, secretlen); |
263 | 0 | } |
264 | | |
265 | | int EVP_PKEY_decapsulate_init(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[]) |
266 | 0 | { |
267 | 0 | return evp_kem_init(ctx, EVP_PKEY_OP_DECAPSULATE, params, NULL); |
268 | 0 | } |
269 | | |
270 | | int EVP_PKEY_auth_decapsulate_init(EVP_PKEY_CTX *ctx, EVP_PKEY *authpub, |
271 | | const OSSL_PARAM params[]) |
272 | 0 | { |
273 | 0 | if (authpub == NULL) |
274 | 0 | return 0; |
275 | 0 | return evp_kem_init(ctx, EVP_PKEY_OP_DECAPSULATE, params, authpub); |
276 | 0 | } |
277 | | |
278 | | int EVP_PKEY_decapsulate(EVP_PKEY_CTX *ctx, |
279 | | unsigned char *secret, size_t *secretlen, |
280 | | const unsigned char *in, size_t inlen) |
281 | 0 | { |
282 | 0 | if (ctx == NULL |
283 | 0 | || (in == NULL || inlen == 0) |
284 | 0 | || (secret == NULL && secretlen == NULL)) |
285 | 0 | return 0; |
286 | | |
287 | 0 | if (ctx->operation != EVP_PKEY_OP_DECAPSULATE) { |
288 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED); |
289 | 0 | return -1; |
290 | 0 | } |
291 | | |
292 | 0 | if (ctx->op.encap.algctx == NULL) { |
293 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
294 | 0 | return -2; |
295 | 0 | } |
296 | 0 | return ctx->op.encap.kem->decapsulate(ctx->op.encap.algctx, |
297 | 0 | secret, secretlen, in, inlen); |
298 | 0 | } |
299 | | |
300 | | static EVP_KEM *evp_kem_new(OSSL_PROVIDER *prov) |
301 | 0 | { |
302 | 0 | EVP_KEM *kem = OPENSSL_zalloc(sizeof(EVP_KEM)); |
303 | |
|
304 | 0 | if (kem == NULL) |
305 | 0 | return NULL; |
306 | | |
307 | 0 | if (!CRYPTO_NEW_REF(&kem->refcnt, 1) |
308 | 0 | || !ossl_provider_up_ref(prov)) { |
309 | 0 | CRYPTO_FREE_REF(&kem->refcnt); |
310 | 0 | OPENSSL_free(kem); |
311 | 0 | return NULL; |
312 | 0 | } |
313 | 0 | kem->prov = prov; |
314 | |
|
315 | 0 | return kem; |
316 | 0 | } |
317 | | |
318 | | static void *evp_kem_from_algorithm(int name_id, const OSSL_ALGORITHM *algodef, |
319 | | OSSL_PROVIDER *prov, int no_store) |
320 | 0 | { |
321 | 0 | const OSSL_DISPATCH *fns = algodef->implementation; |
322 | 0 | EVP_KEM *kem = NULL; |
323 | 0 | int ctxfncnt = 0, encfncnt = 0, decfncnt = 0; |
324 | 0 | int gparamfncnt = 0, sparamfncnt = 0; |
325 | |
|
326 | 0 | if ((kem = evp_kem_new(prov)) == NULL) { |
327 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB); |
328 | 0 | goto err; |
329 | 0 | } |
330 | | |
331 | 0 | kem->name_id = name_id; |
332 | 0 | kem->no_store = no_store; |
333 | |
|
334 | 0 | if ((kem->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL) |
335 | 0 | goto err; |
336 | 0 | kem->description = algodef->algorithm_description; |
337 | |
|
338 | 0 | for (; fns->function_id != 0; fns++) { |
339 | 0 | switch (fns->function_id) { |
340 | 0 | case OSSL_FUNC_KEM_NEWCTX: |
341 | 0 | if (kem->newctx != NULL) |
342 | 0 | break; |
343 | 0 | kem->newctx = OSSL_FUNC_kem_newctx(fns); |
344 | 0 | ctxfncnt++; |
345 | 0 | break; |
346 | 0 | case OSSL_FUNC_KEM_ENCAPSULATE_INIT: |
347 | 0 | if (kem->encapsulate_init != NULL) |
348 | 0 | break; |
349 | 0 | kem->encapsulate_init = OSSL_FUNC_kem_encapsulate_init(fns); |
350 | 0 | encfncnt++; |
351 | 0 | break; |
352 | 0 | case OSSL_FUNC_KEM_AUTH_ENCAPSULATE_INIT: |
353 | 0 | if (kem->auth_encapsulate_init != NULL) |
354 | 0 | break; |
355 | 0 | kem->auth_encapsulate_init = OSSL_FUNC_kem_auth_encapsulate_init(fns); |
356 | 0 | encfncnt++; |
357 | 0 | break; |
358 | 0 | case OSSL_FUNC_KEM_ENCAPSULATE: |
359 | 0 | if (kem->encapsulate != NULL) |
360 | 0 | break; |
361 | 0 | kem->encapsulate = OSSL_FUNC_kem_encapsulate(fns); |
362 | 0 | encfncnt++; |
363 | 0 | break; |
364 | 0 | case OSSL_FUNC_KEM_DECAPSULATE_INIT: |
365 | 0 | if (kem->decapsulate_init != NULL) |
366 | 0 | break; |
367 | 0 | kem->decapsulate_init = OSSL_FUNC_kem_decapsulate_init(fns); |
368 | 0 | decfncnt++; |
369 | 0 | break; |
370 | 0 | case OSSL_FUNC_KEM_AUTH_DECAPSULATE_INIT: |
371 | 0 | if (kem->auth_decapsulate_init != NULL) |
372 | 0 | break; |
373 | 0 | kem->auth_decapsulate_init = OSSL_FUNC_kem_auth_decapsulate_init(fns); |
374 | 0 | decfncnt++; |
375 | 0 | break; |
376 | 0 | case OSSL_FUNC_KEM_DECAPSULATE: |
377 | 0 | if (kem->decapsulate != NULL) |
378 | 0 | break; |
379 | 0 | kem->decapsulate = OSSL_FUNC_kem_decapsulate(fns); |
380 | 0 | decfncnt++; |
381 | 0 | break; |
382 | 0 | case OSSL_FUNC_KEM_FREECTX: |
383 | 0 | if (kem->freectx != NULL) |
384 | 0 | break; |
385 | 0 | kem->freectx = OSSL_FUNC_kem_freectx(fns); |
386 | 0 | ctxfncnt++; |
387 | 0 | break; |
388 | 0 | case OSSL_FUNC_KEM_DUPCTX: |
389 | 0 | if (kem->dupctx != NULL) |
390 | 0 | break; |
391 | 0 | kem->dupctx = OSSL_FUNC_kem_dupctx(fns); |
392 | 0 | break; |
393 | 0 | case OSSL_FUNC_KEM_GET_CTX_PARAMS: |
394 | 0 | if (kem->get_ctx_params != NULL) |
395 | 0 | break; |
396 | 0 | kem->get_ctx_params |
397 | 0 | = OSSL_FUNC_kem_get_ctx_params(fns); |
398 | 0 | gparamfncnt++; |
399 | 0 | break; |
400 | 0 | case OSSL_FUNC_KEM_GETTABLE_CTX_PARAMS: |
401 | 0 | if (kem->gettable_ctx_params != NULL) |
402 | 0 | break; |
403 | 0 | kem->gettable_ctx_params |
404 | 0 | = OSSL_FUNC_kem_gettable_ctx_params(fns); |
405 | 0 | gparamfncnt++; |
406 | 0 | break; |
407 | 0 | case OSSL_FUNC_KEM_SET_CTX_PARAMS: |
408 | 0 | if (kem->set_ctx_params != NULL) |
409 | 0 | break; |
410 | 0 | kem->set_ctx_params |
411 | 0 | = OSSL_FUNC_kem_set_ctx_params(fns); |
412 | 0 | sparamfncnt++; |
413 | 0 | break; |
414 | 0 | case OSSL_FUNC_KEM_SETTABLE_CTX_PARAMS: |
415 | 0 | if (kem->settable_ctx_params != NULL) |
416 | 0 | break; |
417 | 0 | kem->settable_ctx_params |
418 | 0 | = OSSL_FUNC_kem_settable_ctx_params(fns); |
419 | 0 | sparamfncnt++; |
420 | 0 | break; |
421 | 0 | } |
422 | 0 | } |
423 | 0 | if (ctxfncnt != 2 |
424 | 0 | || (encfncnt != 0 && encfncnt != 2 && encfncnt != 3) |
425 | 0 | || (decfncnt != 0 && decfncnt != 2 && decfncnt != 3) |
426 | 0 | || (encfncnt != decfncnt) |
427 | 0 | || (gparamfncnt != 0 && gparamfncnt != 2) |
428 | 0 | || (sparamfncnt != 0 && sparamfncnt != 2)) { |
429 | | /* |
430 | | * In order to be a consistent set of functions we must have at least |
431 | | * a set of context functions (newctx and freectx) as well as a pair |
432 | | * (or triplet) of "kem" functions: |
433 | | * (encapsulate_init, (and/or auth_encapsulate_init), encapsulate) or |
434 | | * (decapsulate_init, (and/or auth_decapsulate_init), decapsulate). |
435 | | * set_ctx_params and settable_ctx_params are optional, but if one of |
436 | | * them is present then the other one must also be present. The same |
437 | | * applies to get_ctx_params and gettable_ctx_params. |
438 | | * The dupctx function is optional. |
439 | | */ |
440 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS); |
441 | 0 | goto err; |
442 | 0 | } |
443 | | |
444 | 0 | return kem; |
445 | 0 | err: |
446 | 0 | evp_kem_free(kem); |
447 | 0 | return NULL; |
448 | 0 | } |
449 | | |
450 | | void EVP_KEM_free(EVP_KEM *kem) |
451 | 0 | { |
452 | | #ifdef OPENSSL_NO_CACHED_FETCH |
453 | | evp_kem_free(kem); |
454 | | #else |
455 | 0 | if (kem != NULL && (kem->no_store != 0)) |
456 | 0 | evp_kem_free(kem); |
457 | 0 | #endif |
458 | 0 | } |
459 | | |
460 | | int EVP_KEM_up_ref(EVP_KEM *kem) |
461 | 0 | { |
462 | | #ifdef OPENSSL_NO_CACHED_FETCH |
463 | | return evp_kem_up_ref(kem); |
464 | | #else |
465 | 0 | if (kem->no_store != 0) |
466 | 0 | return evp_kem_up_ref(kem); |
467 | 0 | return 1; |
468 | 0 | #endif |
469 | 0 | } |
470 | | |
471 | | OSSL_PROVIDER *EVP_KEM_get0_provider(const EVP_KEM *kem) |
472 | 0 | { |
473 | 0 | return kem->prov; |
474 | 0 | } |
475 | | |
476 | | EVP_KEM *EVP_KEM_fetch(OSSL_LIB_CTX *ctx, const char *algorithm, |
477 | | const char *properties) |
478 | 0 | { |
479 | 0 | return evp_generic_fetch(ctx, OSSL_OP_KEM, algorithm, properties, |
480 | 0 | evp_kem_from_algorithm, |
481 | 0 | evp_kem_up_ref, |
482 | 0 | evp_kem_free); |
483 | 0 | } |
484 | | |
485 | | EVP_KEM *evp_kem_fetch_from_prov(OSSL_PROVIDER *prov, const char *algorithm, |
486 | | const char *properties) |
487 | 0 | { |
488 | 0 | return evp_generic_fetch_from_prov(prov, OSSL_OP_KEM, algorithm, properties, |
489 | 0 | evp_kem_from_algorithm, |
490 | 0 | evp_kem_up_ref, |
491 | 0 | evp_kem_free); |
492 | 0 | } |
493 | | |
494 | | int EVP_KEM_is_a(const EVP_KEM *kem, const char *name) |
495 | 0 | { |
496 | 0 | return kem != NULL && evp_is_a(kem->prov, kem->name_id, NULL, name); |
497 | 0 | } |
498 | | |
499 | | int evp_kem_get_number(const EVP_KEM *kem) |
500 | 0 | { |
501 | 0 | return kem->name_id; |
502 | 0 | } |
503 | | |
504 | | const char *EVP_KEM_get0_name(const EVP_KEM *kem) |
505 | 0 | { |
506 | 0 | return kem->type_name; |
507 | 0 | } |
508 | | |
509 | | const char *EVP_KEM_get0_description(const EVP_KEM *kem) |
510 | 0 | { |
511 | 0 | return kem->description; |
512 | 0 | } |
513 | | |
514 | | void EVP_KEM_do_all_provided(OSSL_LIB_CTX *libctx, |
515 | | void (*fn)(EVP_KEM *kem, void *arg), |
516 | | void *arg) |
517 | 0 | { |
518 | 0 | struct EVP_KEM_do_all_provided_thunk t; |
519 | |
|
520 | 0 | t.fn = fn; |
521 | 0 | t.arg = arg; |
522 | 0 | evp_generic_do_all(libctx, OSSL_OP_KEM, EVP_KEM_do_all_provided_thunk, &t, |
523 | 0 | evp_kem_from_algorithm, |
524 | 0 | evp_kem_up_ref, |
525 | 0 | evp_kem_free); |
526 | 0 | } |
527 | | |
528 | | int EVP_KEM_names_do_all(const EVP_KEM *kem, |
529 | | void (*fn)(const char *name, void *data), |
530 | | void *data) |
531 | 0 | { |
532 | 0 | if (kem->prov != NULL) |
533 | 0 | return evp_names_do_all(kem->prov, kem->name_id, fn, data); |
534 | | |
535 | 0 | return 1; |
536 | 0 | } |
537 | | |
538 | | const OSSL_PARAM *EVP_KEM_gettable_ctx_params(const EVP_KEM *kem) |
539 | 0 | { |
540 | 0 | void *provctx; |
541 | |
|
542 | 0 | if (kem == NULL || kem->gettable_ctx_params == NULL) |
543 | 0 | return NULL; |
544 | | |
545 | 0 | provctx = ossl_provider_ctx(EVP_KEM_get0_provider(kem)); |
546 | 0 | return kem->gettable_ctx_params(NULL, provctx); |
547 | 0 | } |
548 | | |
549 | | const OSSL_PARAM *EVP_KEM_settable_ctx_params(const EVP_KEM *kem) |
550 | 0 | { |
551 | 0 | void *provctx; |
552 | |
|
553 | 0 | if (kem == NULL || kem->settable_ctx_params == NULL) |
554 | 0 | return NULL; |
555 | | |
556 | 0 | provctx = ossl_provider_ctx(EVP_KEM_get0_provider(kem)); |
557 | | return kem->settable_ctx_params(NULL, provctx); |
558 | 0 | } |