]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/kem.c
CORE: Add an algorithm_description field to OSSL_ALGORITHM
[thirdparty/openssl.git] / crypto / evp / kem.c
1 /*
2 * Copyright 2020-2021 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 "crypto/evp.h"
16 #include "internal/provider.h"
17 #include "evp_local.h"
18
19 static int evp_kem_init(EVP_PKEY_CTX *ctx, int operation,
20 const OSSL_PARAM params[])
21 {
22 int ret = 0;
23 EVP_KEM *kem = NULL;
24 EVP_KEYMGMT *tmp_keymgmt = NULL;
25 void *provkey = NULL;
26 const char *supported_kem = NULL;
27
28 if (ctx == NULL || ctx->keytype == NULL) {
29 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
30 return 0;
31 }
32
33 evp_pkey_ctx_free_old_ops(ctx);
34 ctx->operation = operation;
35
36 /*
37 * Ensure that the key is provided, either natively, or as a cached export.
38 */
39 tmp_keymgmt = ctx->keymgmt;
40 provkey = evp_pkey_export_to_provider(ctx->pkey, ctx->libctx,
41 &tmp_keymgmt, ctx->propquery);
42 if (provkey == NULL
43 || !EVP_KEYMGMT_up_ref(tmp_keymgmt)) {
44 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
45 goto err;
46 }
47 EVP_KEYMGMT_free(ctx->keymgmt);
48 ctx->keymgmt = tmp_keymgmt;
49
50 if (ctx->keymgmt->query_operation_name != NULL)
51 supported_kem = ctx->keymgmt->query_operation_name(OSSL_OP_KEM);
52
53 /*
54 * If we didn't get a supported kem, assume there is one with the
55 * same name as the key type.
56 */
57 if (supported_kem == NULL)
58 supported_kem = ctx->keytype;
59
60 kem = EVP_KEM_fetch(ctx->libctx, supported_kem, ctx->propquery);
61 if (kem == NULL
62 || (EVP_KEYMGMT_provider(ctx->keymgmt) != EVP_KEM_provider(kem))) {
63 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
64 ret = -2;
65 goto err;
66 }
67
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);
73 goto err;
74 }
75
76 switch (operation) {
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);
80 ret = -2;
81 goto err;
82 }
83 ret = kem->encapsulate_init(ctx->op.encap.kemprovctx, provkey, params);
84 break;
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);
88 ret = -2;
89 goto err;
90 }
91 ret = kem->decapsulate_init(ctx->op.encap.kemprovctx, provkey, params);
92 break;
93 default:
94 ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
95 goto err;
96 }
97
98 if (ret > 0)
99 return 1;
100 err:
101 if (ret <= 0) {
102 evp_pkey_ctx_free_old_ops(ctx);
103 ctx->operation = EVP_PKEY_OP_UNDEFINED;
104 }
105 return ret;
106 }
107
108 int EVP_PKEY_encapsulate_init(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
109 {
110 return evp_kem_init(ctx, EVP_PKEY_OP_ENCAPSULATE, params);
111 }
112
113 int EVP_PKEY_encapsulate(EVP_PKEY_CTX *ctx,
114 unsigned char *out, size_t *outlen,
115 unsigned char *secret, size_t *secretlen)
116 {
117 if (ctx == NULL)
118 return 0;
119
120 if (ctx->operation != EVP_PKEY_OP_ENCAPSULATE) {
121 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
122 return -1;
123 }
124
125 if (ctx->op.encap.kemprovctx == NULL) {
126 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
127 return -2;
128 }
129
130 if (out != NULL && secret == NULL)
131 return 0;
132
133 return ctx->op.encap.kem->encapsulate(ctx->op.encap.kemprovctx,
134 out, outlen, secret, secretlen);
135 }
136
137 int EVP_PKEY_decapsulate_init(EVP_PKEY_CTX *ctx, const OSSL_PARAM params[])
138 {
139 return evp_kem_init(ctx, EVP_PKEY_OP_DECAPSULATE, params);
140 }
141
142 int EVP_PKEY_decapsulate(EVP_PKEY_CTX *ctx,
143 unsigned char *secret, size_t *secretlen,
144 const unsigned char *in, size_t inlen)
145 {
146 if (ctx == NULL
147 || (in == NULL || inlen == 0)
148 || (secret == NULL && secretlen == NULL))
149 return 0;
150
151 if (ctx->operation != EVP_PKEY_OP_DECAPSULATE) {
152 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_INITIALIZED);
153 return -1;
154 }
155
156 if (ctx->op.encap.kemprovctx == NULL) {
157 ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
158 return -2;
159 }
160 return ctx->op.encap.kem->decapsulate(ctx->op.encap.kemprovctx,
161 secret, secretlen, in, inlen);
162 }
163
164 static EVP_KEM *evp_kem_new(OSSL_PROVIDER *prov)
165 {
166 EVP_KEM *kem = OPENSSL_zalloc(sizeof(EVP_KEM));
167
168 if (kem == NULL) {
169 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
170 return NULL;
171 }
172
173 kem->lock = CRYPTO_THREAD_lock_new();
174 if (kem->lock == NULL) {
175 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
176 OPENSSL_free(kem);
177 return NULL;
178 }
179 kem->prov = prov;
180 ossl_provider_up_ref(prov);
181 kem->refcnt = 1;
182
183 return kem;
184 }
185
186 static void *evp_kem_from_algorithm(int name_id, const OSSL_ALGORITHM *algodef,
187 OSSL_PROVIDER *prov)
188 {
189 const OSSL_DISPATCH *fns = algodef->implementation;
190 EVP_KEM *kem = NULL;
191 int ctxfncnt = 0, encfncnt = 0, decfncnt = 0;
192 int gparamfncnt = 0, sparamfncnt = 0;
193
194 if ((kem = evp_kem_new(prov)) == NULL) {
195 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
196 goto err;
197 }
198
199 kem->name_id = name_id;
200 kem->description = algodef->algorithm_description;
201
202 for (; fns->function_id != 0; fns++) {
203 switch (fns->function_id) {
204 case OSSL_FUNC_KEM_NEWCTX:
205 if (kem->newctx != NULL)
206 break;
207 kem->newctx = OSSL_FUNC_kem_newctx(fns);
208 ctxfncnt++;
209 break;
210 case OSSL_FUNC_KEM_ENCAPSULATE_INIT:
211 if (kem->encapsulate_init != NULL)
212 break;
213 kem->encapsulate_init = OSSL_FUNC_kem_encapsulate_init(fns);
214 encfncnt++;
215 break;
216 case OSSL_FUNC_KEM_ENCAPSULATE:
217 if (kem->encapsulate != NULL)
218 break;
219 kem->encapsulate = OSSL_FUNC_kem_encapsulate(fns);
220 encfncnt++;
221 break;
222 case OSSL_FUNC_KEM_DECAPSULATE_INIT:
223 if (kem->decapsulate_init != NULL)
224 break;
225 kem->decapsulate_init = OSSL_FUNC_kem_decapsulate_init(fns);
226 decfncnt++;
227 break;
228 case OSSL_FUNC_KEM_DECAPSULATE:
229 if (kem->decapsulate != NULL)
230 break;
231 kem->decapsulate = OSSL_FUNC_kem_decapsulate(fns);
232 decfncnt++;
233 break;
234 case OSSL_FUNC_KEM_FREECTX:
235 if (kem->freectx != NULL)
236 break;
237 kem->freectx = OSSL_FUNC_kem_freectx(fns);
238 ctxfncnt++;
239 break;
240 case OSSL_FUNC_KEM_DUPCTX:
241 if (kem->dupctx != NULL)
242 break;
243 kem->dupctx = OSSL_FUNC_kem_dupctx(fns);
244 break;
245 case OSSL_FUNC_KEM_GET_CTX_PARAMS:
246 if (kem->get_ctx_params != NULL)
247 break;
248 kem->get_ctx_params
249 = OSSL_FUNC_kem_get_ctx_params(fns);
250 gparamfncnt++;
251 break;
252 case OSSL_FUNC_KEM_GETTABLE_CTX_PARAMS:
253 if (kem->gettable_ctx_params != NULL)
254 break;
255 kem->gettable_ctx_params
256 = OSSL_FUNC_kem_gettable_ctx_params(fns);
257 gparamfncnt++;
258 break;
259 case OSSL_FUNC_KEM_SET_CTX_PARAMS:
260 if (kem->set_ctx_params != NULL)
261 break;
262 kem->set_ctx_params
263 = OSSL_FUNC_kem_set_ctx_params(fns);
264 sparamfncnt++;
265 break;
266 case OSSL_FUNC_KEM_SETTABLE_CTX_PARAMS:
267 if (kem->settable_ctx_params != NULL)
268 break;
269 kem->settable_ctx_params
270 = OSSL_FUNC_kem_settable_ctx_params(fns);
271 sparamfncnt++;
272 break;
273 }
274 }
275 if (ctxfncnt != 2
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)) {
281 /*
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.
289 */
290 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
291 goto err;
292 }
293
294 return kem;
295 err:
296 EVP_KEM_free(kem);
297 return NULL;
298 }
299
300 void EVP_KEM_free(EVP_KEM *kem)
301 {
302 if (kem != NULL) {
303 int i;
304
305 CRYPTO_DOWN_REF(&kem->refcnt, &i, kem->lock);
306 if (i > 0)
307 return;
308 ossl_provider_free(kem->prov);
309 CRYPTO_THREAD_lock_free(kem->lock);
310 OPENSSL_free(kem);
311 }
312 }
313
314 int EVP_KEM_up_ref(EVP_KEM *kem)
315 {
316 int ref = 0;
317
318 CRYPTO_UP_REF(&kem->refcnt, &ref, kem->lock);
319 return 1;
320 }
321
322 OSSL_PROVIDER *EVP_KEM_provider(const EVP_KEM *kem)
323 {
324 return kem->prov;
325 }
326
327 EVP_KEM *EVP_KEM_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
328 const char *properties)
329 {
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);
334 }
335
336 int EVP_KEM_is_a(const EVP_KEM *kem, const char *name)
337 {
338 return evp_is_a(kem->prov, kem->name_id, NULL, name);
339 }
340
341 int EVP_KEM_number(const EVP_KEM *kem)
342 {
343 return kem->name_id;
344 }
345
346 void EVP_KEM_do_all_provided(OSSL_LIB_CTX *libctx,
347 void (*fn)(EVP_KEM *kem, void *arg),
348 void *arg)
349 {
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);
353 }
354
355 int EVP_KEM_names_do_all(const EVP_KEM *kem,
356 void (*fn)(const char *name, void *data),
357 void *data)
358 {
359 if (kem->prov != NULL)
360 return evp_names_do_all(kem->prov, kem->name_id, fn, data);
361
362 return 1;
363 }
364
365 const OSSL_PARAM *EVP_KEM_gettable_ctx_params(const EVP_KEM *kem)
366 {
367 void *provctx;
368
369 if (kem == NULL || kem->gettable_ctx_params == NULL)
370 return NULL;
371
372 provctx = ossl_provider_ctx(EVP_KEM_provider(kem));
373 return kem->gettable_ctx_params(NULL, provctx);
374 }
375
376 const OSSL_PARAM *EVP_KEM_settable_ctx_params(const EVP_KEM *kem)
377 {
378 void *provctx;
379
380 if (kem == NULL || kem->settable_ctx_params == NULL)
381 return NULL;
382
383 provctx = ossl_provider_ctx(EVP_KEM_provider(kem));
384 return kem->settable_ctx_params(NULL, provctx);
385 }