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