2 * Copyright 2020-2021 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
,
20 const OSSL_PARAM params
[])
24 EVP_KEYMGMT
*tmp_keymgmt
= NULL
;
26 const char *supported_kem
= NULL
;
28 if (ctx
== NULL
|| ctx
->keytype
== NULL
) {
29 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
33 evp_pkey_ctx_free_old_ops(ctx
);
34 ctx
->operation
= operation
;
37 * Ensure that the key is provided, either natively, or as a cached export.
39 tmp_keymgmt
= ctx
->keymgmt
;
40 provkey
= evp_pkey_export_to_provider(ctx
->pkey
, ctx
->libctx
,
41 &tmp_keymgmt
, ctx
->propquery
);
43 || !EVP_KEYMGMT_up_ref(tmp_keymgmt
)) {
44 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
47 EVP_KEYMGMT_free(ctx
->keymgmt
);
48 ctx
->keymgmt
= tmp_keymgmt
;
50 if (ctx
->keymgmt
->query_operation_name
!= NULL
)
51 supported_kem
= ctx
->keymgmt
->query_operation_name(OSSL_OP_KEM
);
54 * If we didn't get a supported kem, assume there is one with the
55 * same name as the key type.
57 if (supported_kem
== NULL
)
58 supported_kem
= ctx
->keytype
;
60 kem
= EVP_KEM_fetch(ctx
->libctx
, supported_kem
, ctx
->propquery
);
62 || (EVP_KEYMGMT_provider(ctx
->keymgmt
) != EVP_KEM_provider(kem
))) {
63 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
68 ctx
->op
.encap
.kem
= kem
;
69 ctx
->op
.encap
.kemprovctx
= kem
->newctx(ossl_provider_ctx(kem
->prov
));
70 if (ctx
->op
.encap
.kemprovctx
== NULL
) {
71 /* The provider key can stay in the cache */
72 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
77 case EVP_PKEY_OP_ENCAPSULATE
:
78 if (kem
->encapsulate_init
== NULL
) {
79 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
83 ret
= kem
->encapsulate_init(ctx
->op
.encap
.kemprovctx
, provkey
, params
);
85 case EVP_PKEY_OP_DECAPSULATE
:
86 if (kem
->decapsulate_init
== NULL
) {
87 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
91 ret
= kem
->decapsulate_init(ctx
->op
.encap
.kemprovctx
, provkey
, params
);
94 ERR_raise(ERR_LIB_EVP
, EVP_R_INITIALIZATION_ERROR
);
102 evp_pkey_ctx_free_old_ops(ctx
);
103 ctx
->operation
= EVP_PKEY_OP_UNDEFINED
;
108 int EVP_PKEY_encapsulate_init(EVP_PKEY_CTX
*ctx
, const OSSL_PARAM params
[])
110 return evp_kem_init(ctx
, EVP_PKEY_OP_ENCAPSULATE
, params
);
113 int EVP_PKEY_encapsulate(EVP_PKEY_CTX
*ctx
,
114 unsigned char *out
, size_t *outlen
,
115 unsigned char *secret
, size_t *secretlen
)
120 if (ctx
->operation
!= EVP_PKEY_OP_ENCAPSULATE
) {
121 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_INITIALIZED
);
125 if (ctx
->op
.encap
.kemprovctx
== NULL
) {
126 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
130 if (out
!= NULL
&& secret
== NULL
)
133 return ctx
->op
.encap
.kem
->encapsulate(ctx
->op
.encap
.kemprovctx
,
134 out
, outlen
, secret
, secretlen
);
137 int EVP_PKEY_decapsulate_init(EVP_PKEY_CTX
*ctx
, const OSSL_PARAM params
[])
139 return evp_kem_init(ctx
, EVP_PKEY_OP_DECAPSULATE
, params
);
142 int EVP_PKEY_decapsulate(EVP_PKEY_CTX
*ctx
,
143 unsigned char *secret
, size_t *secretlen
,
144 const unsigned char *in
, size_t inlen
)
147 || (in
== NULL
|| inlen
== 0)
148 || (secret
== NULL
&& secretlen
== NULL
))
151 if (ctx
->operation
!= EVP_PKEY_OP_DECAPSULATE
) {
152 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_INITIALIZED
);
156 if (ctx
->op
.encap
.kemprovctx
== NULL
) {
157 ERR_raise(ERR_LIB_EVP
, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE
);
160 return ctx
->op
.encap
.kem
->decapsulate(ctx
->op
.encap
.kemprovctx
,
161 secret
, secretlen
, in
, inlen
);
164 static EVP_KEM
*evp_kem_new(OSSL_PROVIDER
*prov
)
166 EVP_KEM
*kem
= OPENSSL_zalloc(sizeof(EVP_KEM
));
169 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
173 kem
->lock
= CRYPTO_THREAD_lock_new();
174 if (kem
->lock
== NULL
) {
175 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
180 ossl_provider_up_ref(prov
);
186 static void *evp_kem_from_algorithm(int name_id
, const OSSL_ALGORITHM
*algodef
,
189 const OSSL_DISPATCH
*fns
= algodef
->implementation
;
191 int ctxfncnt
= 0, encfncnt
= 0, decfncnt
= 0;
192 int gparamfncnt
= 0, sparamfncnt
= 0;
194 if ((kem
= evp_kem_new(prov
)) == NULL
) {
195 ERR_raise(ERR_LIB_EVP
, ERR_R_MALLOC_FAILURE
);
199 kem
->name_id
= name_id
;
200 kem
->description
= algodef
->algorithm_description
;
202 for (; fns
->function_id
!= 0; fns
++) {
203 switch (fns
->function_id
) {
204 case OSSL_FUNC_KEM_NEWCTX
:
205 if (kem
->newctx
!= NULL
)
207 kem
->newctx
= OSSL_FUNC_kem_newctx(fns
);
210 case OSSL_FUNC_KEM_ENCAPSULATE_INIT
:
211 if (kem
->encapsulate_init
!= NULL
)
213 kem
->encapsulate_init
= OSSL_FUNC_kem_encapsulate_init(fns
);
216 case OSSL_FUNC_KEM_ENCAPSULATE
:
217 if (kem
->encapsulate
!= NULL
)
219 kem
->encapsulate
= OSSL_FUNC_kem_encapsulate(fns
);
222 case OSSL_FUNC_KEM_DECAPSULATE_INIT
:
223 if (kem
->decapsulate_init
!= NULL
)
225 kem
->decapsulate_init
= OSSL_FUNC_kem_decapsulate_init(fns
);
228 case OSSL_FUNC_KEM_DECAPSULATE
:
229 if (kem
->decapsulate
!= NULL
)
231 kem
->decapsulate
= OSSL_FUNC_kem_decapsulate(fns
);
234 case OSSL_FUNC_KEM_FREECTX
:
235 if (kem
->freectx
!= NULL
)
237 kem
->freectx
= OSSL_FUNC_kem_freectx(fns
);
240 case OSSL_FUNC_KEM_DUPCTX
:
241 if (kem
->dupctx
!= NULL
)
243 kem
->dupctx
= OSSL_FUNC_kem_dupctx(fns
);
245 case OSSL_FUNC_KEM_GET_CTX_PARAMS
:
246 if (kem
->get_ctx_params
!= NULL
)
249 = OSSL_FUNC_kem_get_ctx_params(fns
);
252 case OSSL_FUNC_KEM_GETTABLE_CTX_PARAMS
:
253 if (kem
->gettable_ctx_params
!= NULL
)
255 kem
->gettable_ctx_params
256 = OSSL_FUNC_kem_gettable_ctx_params(fns
);
259 case OSSL_FUNC_KEM_SET_CTX_PARAMS
:
260 if (kem
->set_ctx_params
!= NULL
)
263 = OSSL_FUNC_kem_set_ctx_params(fns
);
266 case OSSL_FUNC_KEM_SETTABLE_CTX_PARAMS
:
267 if (kem
->settable_ctx_params
!= NULL
)
269 kem
->settable_ctx_params
270 = OSSL_FUNC_kem_settable_ctx_params(fns
);
276 || (encfncnt
!= 0 && encfncnt
!= 2)
277 || (decfncnt
!= 0 && decfncnt
!= 2)
278 || (encfncnt
!= 2 && decfncnt
!= 2)
279 || (gparamfncnt
!= 0 && gparamfncnt
!= 2)
280 || (sparamfncnt
!= 0 && sparamfncnt
!= 2)) {
282 * In order to be a consistent set of functions we must have at least
283 * a set of context functions (newctx and freectx) as well as a pair of
284 * "kem" functions: (encapsulate_init, encapsulate) or
285 * (decapsulate_init, decapsulate). set_ctx_params and settable_ctx_params are
286 * optional, but if one of them is present then the other one must also
287 * be present. The same applies to get_ctx_params and
288 * gettable_ctx_params. The dupctx function is optional.
290 ERR_raise(ERR_LIB_EVP
, EVP_R_INVALID_PROVIDER_FUNCTIONS
);
300 void EVP_KEM_free(EVP_KEM
*kem
)
305 CRYPTO_DOWN_REF(&kem
->refcnt
, &i
, kem
->lock
);
308 ossl_provider_free(kem
->prov
);
309 CRYPTO_THREAD_lock_free(kem
->lock
);
314 int EVP_KEM_up_ref(EVP_KEM
*kem
)
318 CRYPTO_UP_REF(&kem
->refcnt
, &ref
, kem
->lock
);
322 OSSL_PROVIDER
*EVP_KEM_provider(const EVP_KEM
*kem
)
327 EVP_KEM
*EVP_KEM_fetch(OSSL_LIB_CTX
*ctx
, const char *algorithm
,
328 const char *properties
)
330 return evp_generic_fetch(ctx
, OSSL_OP_KEM
, algorithm
, properties
,
331 evp_kem_from_algorithm
,
332 (int (*)(void *))EVP_KEM_up_ref
,
333 (void (*)(void *))EVP_KEM_free
);
336 int EVP_KEM_is_a(const EVP_KEM
*kem
, const char *name
)
338 return evp_is_a(kem
->prov
, kem
->name_id
, NULL
, name
);
341 int EVP_KEM_number(const EVP_KEM
*kem
)
346 void EVP_KEM_do_all_provided(OSSL_LIB_CTX
*libctx
,
347 void (*fn
)(EVP_KEM
*kem
, void *arg
),
350 evp_generic_do_all(libctx
, OSSL_OP_KEM
, (void (*)(void *, void *))fn
, arg
,
351 evp_kem_from_algorithm
,
352 (void (*)(void *))EVP_KEM_free
);
355 int EVP_KEM_names_do_all(const EVP_KEM
*kem
,
356 void (*fn
)(const char *name
, void *data
),
359 if (kem
->prov
!= NULL
)
360 return evp_names_do_all(kem
->prov
, kem
->name_id
, fn
, data
);
365 const OSSL_PARAM
*EVP_KEM_gettable_ctx_params(const EVP_KEM
*kem
)
369 if (kem
== NULL
|| kem
->gettable_ctx_params
== NULL
)
372 provctx
= ossl_provider_ctx(EVP_KEM_provider(kem
));
373 return kem
->gettable_ctx_params(NULL
, provctx
);
376 const OSSL_PARAM
*EVP_KEM_settable_ctx_params(const EVP_KEM
*kem
)
380 if (kem
== NULL
|| kem
->settable_ctx_params
== NULL
)
383 provctx
= ossl_provider_ctx(EVP_KEM_provider(kem
));
384 return kem
->settable_ctx_params(NULL
, provctx
);