2 * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
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
12 #include <openssl/objects.h>
13 #include <openssl/evp.h>
14 #include "internal/cryptlib.h"
15 #include "crypto/evp.h"
16 #include "internal/provider.h"
17 #include "evp_local.h"
19 static int evp_kem_init(EVP_PKEY_CTX
*ctx
, int operation
)
23 EVP_KEYMGMT
*tmp_keymgmt
= NULL
;
25 const char *supported_kem
= NULL
;
27 if (ctx
== NULL
|| ctx
->keytype
== NULL
) {
28 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
32 evp_pkey_ctx_free_old_ops(ctx
);
33 ctx
->operation
= operation
;
36 * Ensure that the key is provided, either natively, or as a cached export.
38 tmp_keymgmt
= ctx
->keymgmt
;
39 provkey
= evp_pkey_export_to_provider(ctx
->pkey
, ctx
->libctx
,
40 &tmp_keymgmt
, ctx
->propquery
);
42 || !EVP_KEYMGMT_up_ref(tmp_keymgmt
)) {
43 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
46 EVP_KEYMGMT_free(ctx
->keymgmt
);
47 ctx
->keymgmt
= tmp_keymgmt
;
49 if (ctx
->keymgmt
->query_operation_name
!= NULL
)
50 supported_kem
= ctx
->keymgmt
->query_operation_name(OSSL_OP_KEM
);
53 * If we didn't get a supported kem, assume there is one with the
54 * same name as the key type.
56 if (supported_kem
== NULL
)
57 supported_kem
= ctx
->keytype
;
59 kem
= EVP_KEM_fetch(ctx
->libctx
, supported_kem
, ctx
->propquery
);
61 || (EVP_KEYMGMT_provider(ctx
->keymgmt
) != EVP_KEM_provider(kem
))) {
62 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
67 ctx
->op
.encap
.kem
= kem
;
68 ctx
->op
.encap
.kemprovctx
= kem
->newctx(ossl_provider_ctx(kem
->prov
));
69 if (ctx
->op
.encap
.kemprovctx
== NULL
) {
70 /* The provider key can stay in the cache */
71 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
76 case EVP_PKEY_OP_ENCAPSULATE
:
77 if (kem
->encapsulate_init
== NULL
) {
78 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
82 ret
= kem
->encapsulate_init(ctx
->op
.encap
.kemprovctx
, provkey
);
84 case EVP_PKEY_OP_DECAPSULATE
:
85 if (kem
->decapsulate_init
== NULL
) {
86 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
90 ret
= kem
->decapsulate_init(ctx
->op
.encap
.kemprovctx
, provkey
);
93 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
101 evp_pkey_ctx_free_old_ops(ctx
);
102 ctx
->operation
= EVP_PKEY_OP_UNDEFINED
;
107 int EVP_PKEY_encapsulate_init(EVP_PKEY_CTX
*ctx
)
109 return evp_kem_init(ctx
, EVP_PKEY_OP_ENCAPSULATE
);
112 int EVP_PKEY_encapsulate(EVP_PKEY_CTX
*ctx
,
113 unsigned char *out
, size_t *outlen
,
114 unsigned char *secret
, size_t *secretlen
)
119 if (ctx
->operation
!= EVP_PKEY_OP_ENCAPSULATE
) {
120 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATON_NOT_INITIALIZED
);
124 if (ctx
->op
.encap
.kemprovctx
== NULL
) {
125 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
129 if (out
!= NULL
&& secret
== NULL
)
132 return ctx
->op
.encap
.kem
->encapsulate(ctx
->op
.encap
.kemprovctx
,
133 out
, outlen
, secret
, secretlen
);
136 int EVP_PKEY_decapsulate_init(EVP_PKEY_CTX
*ctx
)
138 return evp_kem_init(ctx
, EVP_PKEY_OP_DECAPSULATE
);
141 int EVP_PKEY_decapsulate(EVP_PKEY_CTX
*ctx
,
142 unsigned char *secret
, size_t *secretlen
,
143 const unsigned char *in
, size_t inlen
)
146 || (in
== NULL
|| inlen
== 0)
147 || (secret
== NULL
&& secretlen
== NULL
))
150 if (ctx
->operation
!= EVP_PKEY_OP_DECAPSULATE
) {
151 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATON_NOT_INITIALIZED
);
155 if (ctx
->op
.encap
.kemprovctx
== NULL
) {
156 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
159 return ctx
->op
.encap
.kem
->decapsulate(ctx
->op
.encap
.kemprovctx
,
160 secret
, secretlen
, in
, inlen
);
163 static EVP_KEM
*evp_kem_new(OSSL_PROVIDER
*prov
)
165 EVP_KEM
*kem
= OPENSSL_zalloc(sizeof(EVP_KEM
));
168 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
172 kem
->lock
= CRYPTO_THREAD_lock_new();
173 if (kem
->lock
== NULL
) {
174 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
179 ossl_provider_up_ref(prov
);
185 static void *evp_kem_from_dispatch(int name_id
, const OSSL_DISPATCH
*fns
,
189 int ctxfncnt
= 0, encfncnt
= 0, decfncnt
= 0;
190 int gparamfncnt
= 0, sparamfncnt
= 0;
192 if ((kem
= evp_kem_new(prov
)) == NULL
) {
193 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
197 kem
->name_id
= name_id
;
199 for (; fns
->function_id
!= 0; fns
++) {
200 switch (fns
->function_id
) {
201 case OSSL_FUNC_KEM_NEWCTX
:
202 if (kem
->newctx
!= NULL
)
204 kem
->newctx
= OSSL_FUNC_kem_newctx(fns
);
207 case OSSL_FUNC_KEM_ENCAPSULATE_INIT
:
208 if (kem
->encapsulate_init
!= NULL
)
210 kem
->encapsulate_init
= OSSL_FUNC_kem_encapsulate_init(fns
);
213 case OSSL_FUNC_KEM_ENCAPSULATE
:
214 if (kem
->encapsulate
!= NULL
)
216 kem
->encapsulate
= OSSL_FUNC_kem_encapsulate(fns
);
219 case OSSL_FUNC_KEM_DECAPSULATE_INIT
:
220 if (kem
->decapsulate_init
!= NULL
)
222 kem
->decapsulate_init
= OSSL_FUNC_kem_decapsulate_init(fns
);
225 case OSSL_FUNC_KEM_DECAPSULATE
:
226 if (kem
->decapsulate
!= NULL
)
228 kem
->decapsulate
= OSSL_FUNC_kem_decapsulate(fns
);
231 case OSSL_FUNC_KEM_FREECTX
:
232 if (kem
->freectx
!= NULL
)
234 kem
->freectx
= OSSL_FUNC_kem_freectx(fns
);
237 case OSSL_FUNC_KEM_DUPCTX
:
238 if (kem
->dupctx
!= NULL
)
240 kem
->dupctx
= OSSL_FUNC_kem_dupctx(fns
);
242 case OSSL_FUNC_KEM_GET_CTX_PARAMS
:
243 if (kem
->get_ctx_params
!= NULL
)
246 = OSSL_FUNC_kem_get_ctx_params(fns
);
249 case OSSL_FUNC_KEM_GETTABLE_CTX_PARAMS
:
250 if (kem
->gettable_ctx_params
!= NULL
)
252 kem
->gettable_ctx_params
253 = OSSL_FUNC_kem_gettable_ctx_params(fns
);
256 case OSSL_FUNC_KEM_SET_CTX_PARAMS
:
257 if (kem
->set_ctx_params
!= NULL
)
260 = OSSL_FUNC_kem_set_ctx_params(fns
);
263 case OSSL_FUNC_KEM_SETTABLE_CTX_PARAMS
:
264 if (kem
->settable_ctx_params
!= NULL
)
266 kem
->settable_ctx_params
267 = OSSL_FUNC_kem_settable_ctx_params(fns
);
273 || (encfncnt
!= 0 && encfncnt
!= 2)
274 || (decfncnt
!= 0 && decfncnt
!= 2)
275 || (encfncnt
!= 2 && decfncnt
!= 2)
276 || (gparamfncnt
!= 0 && gparamfncnt
!= 2)
277 || (sparamfncnt
!= 0 && sparamfncnt
!= 2)) {
279 * In order to be a consistent set of functions we must have at least
280 * a set of context functions (newctx and freectx) as well as a pair of
281 * "kem" functions: (encapsulate_init, encapsulate) or
282 * (decapsulate_init, decapsulate). set_ctx_params and settable_ctx_params are
283 * optional, but if one of them is present then the other one must also
284 * be present. The same applies to get_ctx_params and
285 * gettable_ctx_params. The dupctx function is optional.
287 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_PROVIDER_FUNCTIONS
);
297 void EVP_KEM_free(EVP_KEM
*kem
)
302 CRYPTO_DOWN_REF(&kem
->refcnt
, &i
, kem
->lock
);
305 ossl_provider_free(kem
->prov
);
306 CRYPTO_THREAD_lock_free(kem
->lock
);
311 int EVP_KEM_up_ref(EVP_KEM
*kem
)
315 CRYPTO_UP_REF(&kem
->refcnt
, &ref
, kem
->lock
);
319 OSSL_PROVIDER
*EVP_KEM_provider(const EVP_KEM
*kem
)
324 EVP_KEM
*EVP_KEM_fetch(OSSL_LIB_CTX
*ctx
, const char *algorithm
,
325 const char *properties
)
327 return evp_generic_fetch(ctx
, OSSL_OP_KEM
, algorithm
, properties
,
328 evp_kem_from_dispatch
,
329 (int (*)(void *))EVP_KEM_up_ref
,
330 (void (*)(void *))EVP_KEM_free
);
333 int EVP_KEM_is_a(const EVP_KEM
*kem
, const char *name
)
335 return evp_is_a(kem
->prov
, kem
->name_id
, NULL
, name
);
338 int EVP_KEM_number(const EVP_KEM
*kem
)
343 void EVP_KEM_do_all_provided(OSSL_LIB_CTX
*libctx
,
344 void (*fn
)(EVP_KEM
*kem
, void *arg
),
347 evp_generic_do_all(libctx
, OSSL_OP_KEM
, (void (*)(void *, void *))fn
, arg
,
348 evp_kem_from_dispatch
,
349 (void (*)(void *))EVP_KEM_free
);
352 int EVP_KEM_names_do_all(const EVP_KEM
*kem
,
353 void (*fn
)(const char *name
, void *data
),
356 if (kem
->prov
!= NULL
)
357 return evp_names_do_all(kem
->prov
, kem
->name_id
, fn
, data
);
362 const OSSL_PARAM
*EVP_KEM_gettable_ctx_params(const EVP_KEM
*kem
)
366 if (kem
== NULL
|| kem
->gettable_ctx_params
== NULL
)
369 provctx
= ossl_provider_ctx(EVP_KEM_provider(kem
));
370 return kem
->gettable_ctx_params(provctx
);
373 const OSSL_PARAM
*EVP_KEM_settable_ctx_params(const EVP_KEM
*kem
)
377 if (kem
== NULL
|| kem
->settable_ctx_params
== NULL
)
380 provctx
= ossl_provider_ctx(EVP_KEM_provider(kem
));
381 return kem
->settable_ctx_params(provctx
);