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 "internal/provider.h"
16 #include "internal/core.h"
17 #include "crypto/evp.h"
18 #include "evp_local.h"
20 static int evp_kem_init(EVP_PKEY_CTX *ctx, int operation,
21 const OSSL_PARAM params[])
25 EVP_KEYMGMT *tmp_keymgmt = NULL;
27 const char *supported_kem = NULL;
29 if (ctx == NULL || ctx->keytype == NULL) {
30 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
34 evp_pkey_ctx_free_old_ops(ctx);
35 ctx->operation = operation;
38 * Ensure that the key is provided, either natively, or as a cached export.
40 tmp_keymgmt = ctx->keymgmt;
41 provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
42 &tmp_keymgmt, ctx->propquery);
44 || !EVP_KEYMGMT_up_ref(tmp_keymgmt)) {
45 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
48 EVP_KEYMGMT_free(ctx->keymgmt);
49 ctx->keymgmt = tmp_keymgmt;
51 if (ctx->keymgmt->query_operation_name != NULL)
52 supported_kem = ctx->keymgmt->query_operation_name(OSSL_OP_KEM);
55 * If we didn't get a supported kem, assume there is one with the
56 * same name as the key type.
58 if (supported_kem == NULL)
59 supported_kem = ctx->keytype;
61 kem = EVP_KEM_fetch(ctx->libctx, supported_kem, ctx->propquery);
63 || (EVP_KEYMGMT_get0_provider(ctx->keymgmt) != EVP_KEM_get0_provider(kem))) {
64 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
69 ctx->op.encap.kem = kem;
70 ctx->op.encap.algctx = kem->newctx(ossl_provider_ctx(kem->prov));
71 if (ctx->op.encap.algctx == NULL) {
72 /* The provider key can stay in the cache */
73 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
78 case EVP_PKEY_OP_ENCAPSULATE:
79 if (kem->encapsulate_init == NULL) {
80 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
84 ret = kem->encapsulate_init(ctx->op.encap.algctx, provkey, params);
86 case EVP_PKEY_OP_DECAPSULATE:
87 if (kem->decapsulate_init == NULL) {
88 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
92 ret = kem->decapsulate_init(ctx->op.encap.algctx, provkey, params);
95 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
103 evp_pkey_ctx_free_old_ops(ctx);
104 ctx->operation = EVP_PKEY_OP_UNDEFINED;
109 int EVP_PKEY_encapsulate_init(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
111 return evp_kem_init(ctx, EVP_PKEY_OP_ENCAPSULATE, params);
114 int EVP_PKEY_encapsulate(EVP_PKEY_CTX *ctx,
115 unsigned char *out, size_t *outlen,
116 unsigned char *secret, size_t *secretlen)
121 if (ctx->operation != EVP_PKEY_OP_ENCAPSULATE) {
122 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
126 if (ctx->op.encap.algctx == NULL) {
127 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
131 if (out != NULL && secret == NULL)
134 return ctx->op.encap.kem->encapsulate(ctx->op.encap.algctx,
135 out, outlen, secret, secretlen);
138 int EVP_PKEY_decapsulate_init(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
140 return evp_kem_init(ctx, EVP_PKEY_OP_DECAPSULATE, params);
143 int EVP_PKEY_decapsulate(EVP_PKEY_CTX *ctx,
144 unsigned char *secret, size_t *secretlen,
145 const unsigned char *in, size_t inlen)
148 || (in == NULL || inlen == 0)
149 || (secret == NULL && secretlen == NULL))
152 if (ctx->operation != EVP_PKEY_OP_DECAPSULATE) {
153 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
157 if (ctx->op.encap.algctx == NULL) {
158 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
161 return ctx->op.encap.kem->decapsulate(ctx->op.encap.algctx,
162 secret, secretlen, in, inlen);
165 static EVP_KEM *evp_kem_new(OSSL_PROVIDER *prov)
167 EVP_KEM *kem = OPENSSL_zalloc(sizeof(EVP_KEM));
170 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
174 kem->lock = CRYPTO_THREAD_lock_new();
175 if (kem->lock == NULL) {
176 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
181 ossl_provider_up_ref(prov);
187 static void *evp_kem_from_algorithm(int name_id, const OSSL_ALGORITHM *algodef,
190 const OSSL_DISPATCH *fns = algodef->implementation;
192 int ctxfncnt = 0, encfncnt = 0, decfncnt = 0;
193 int gparamfncnt = 0, sparamfncnt = 0;
195 if ((kem = evp_kem_new(prov)) == NULL) {
196 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
200 kem->name_id = name_id;
201 if ((kem->type_name = ossl_algorithm_get1_first_name(algodef)) == NULL)
203 kem->description = algodef->algorithm_description;
205 for (; fns->function_id != 0; fns++) {
206 switch (fns->function_id) {
207 case OSSL_FUNC_KEM_NEWCTX:
208 if (kem->newctx != NULL)
210 kem->newctx = OSSL_FUNC_kem_newctx(fns);
213 case OSSL_FUNC_KEM_ENCAPSULATE_INIT:
214 if (kem->encapsulate_init != NULL)
216 kem->encapsulate_init = OSSL_FUNC_kem_encapsulate_init(fns);
219 case OSSL_FUNC_KEM_ENCAPSULATE:
220 if (kem->encapsulate != NULL)
222 kem->encapsulate = OSSL_FUNC_kem_encapsulate(fns);
225 case OSSL_FUNC_KEM_DECAPSULATE_INIT:
226 if (kem->decapsulate_init != NULL)
228 kem->decapsulate_init = OSSL_FUNC_kem_decapsulate_init(fns);
231 case OSSL_FUNC_KEM_DECAPSULATE:
232 if (kem->decapsulate != NULL)
234 kem->decapsulate = OSSL_FUNC_kem_decapsulate(fns);
237 case OSSL_FUNC_KEM_FREECTX:
238 if (kem->freectx != NULL)
240 kem->freectx = OSSL_FUNC_kem_freectx(fns);
243 case OSSL_FUNC_KEM_DUPCTX:
244 if (kem->dupctx != NULL)
246 kem->dupctx = OSSL_FUNC_kem_dupctx(fns);
248 case OSSL_FUNC_KEM_GET_CTX_PARAMS:
249 if (kem->get_ctx_params != NULL)
252 = OSSL_FUNC_kem_get_ctx_params(fns);
255 case OSSL_FUNC_KEM_GETTABLE_CTX_PARAMS:
256 if (kem->gettable_ctx_params != NULL)
258 kem->gettable_ctx_params
259 = OSSL_FUNC_kem_gettable_ctx_params(fns);
262 case OSSL_FUNC_KEM_SET_CTX_PARAMS:
263 if (kem->set_ctx_params != NULL)
266 = OSSL_FUNC_kem_set_ctx_params(fns);
269 case OSSL_FUNC_KEM_SETTABLE_CTX_PARAMS:
270 if (kem->settable_ctx_params != NULL)
272 kem->settable_ctx_params
273 = OSSL_FUNC_kem_settable_ctx_params(fns);
279 || (encfncnt != 0 && encfncnt != 2)
280 || (decfncnt != 0 && decfncnt != 2)
281 || (encfncnt != 2 && decfncnt != 2)
282 || (gparamfncnt != 0 && gparamfncnt != 2)
283 || (sparamfncnt != 0 && sparamfncnt != 2)) {
285 * In order to be a consistent set of functions we must have at least
286 * a set of context functions (newctx and freectx) as well as a pair of
287 * "kem" functions: (encapsulate_init, encapsulate) or
288 * (decapsulate_init, decapsulate). set_ctx_params and settable_ctx_params are
289 * optional, but if one of them is present then the other one must also
290 * be present. The same applies to get_ctx_params and
291 * gettable_ctx_params. The dupctx function is optional.
293 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
303 void EVP_KEM_free(EVP_KEM *kem)
310 CRYPTO_DOWN_REF(&kem->refcnt, &i, kem->lock);
313 OPENSSL_free(kem->type_name);
314 ossl_provider_free(kem->prov);
315 CRYPTO_THREAD_lock_free(kem->lock);
319 int EVP_KEM_up_ref(EVP_KEM *kem)
323 CRYPTO_UP_REF(&kem->refcnt, &ref, kem->lock);
327 OSSL_PROVIDER *EVP_KEM_get0_provider(const EVP_KEM *kem)
332 EVP_KEM *EVP_KEM_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
333 const char *properties)
335 return evp_generic_fetch(ctx, OSSL_OP_KEM, algorithm, properties,
336 evp_kem_from_algorithm,
337 (int (*)(void *))EVP_KEM_up_ref,
338 (void (*)(void *))EVP_KEM_free);
341 int EVP_KEM_is_a(const EVP_KEM *kem, const char *name)
343 return evp_is_a(kem->prov, kem->name_id, NULL, name);
346 int evp_kem_get_number(const EVP_KEM *kem)
351 const char *EVP_KEM_get0_name(const EVP_KEM *kem)
353 return kem->type_name;
356 const char *EVP_KEM_get0_description(const EVP_KEM *kem)
358 return kem->description;
361 void EVP_KEM_do_all_provided(OSSL_LIB_CTX *libctx,
362 void (*fn)(EVP_KEM *kem, void *arg),
365 evp_generic_do_all(libctx, OSSL_OP_KEM, (void (*)(void *, void *))fn, arg,
366 evp_kem_from_algorithm,
367 (int (*)(void *))EVP_KEM_up_ref,
368 (void (*)(void *))EVP_KEM_free);
371 int EVP_KEM_names_do_all(const EVP_KEM *kem,
372 void (*fn)(const char *name, void *data),
375 if (kem->prov != NULL)
376 return evp_names_do_all(kem->prov, kem->name_id, fn, data);
381 const OSSL_PARAM *EVP_KEM_gettable_ctx_params(const EVP_KEM *kem)
385 if (kem == NULL || kem->gettable_ctx_params == NULL)
388 provctx = ossl_provider_ctx(EVP_KEM_get0_provider(kem));
389 return kem->gettable_ctx_params(NULL, provctx);
392 const OSSL_PARAM *EVP_KEM_settable_ctx_params(const EVP_KEM *kem)
396 if (kem == NULL || kem->settable_ctx_params == NULL)
399 provctx = ossl_provider_ctx(EVP_KEM_get0_provider(kem));
400 return kem->settable_ctx_params(NULL, provctx);