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