]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/kem.c
evp: add params arguments to init functions
[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_dispatch(int name_id, const OSSL_DISPATCH *fns,
187 OSSL_PROVIDER *prov)
188 {
189 EVP_KEM *kem = NULL;
190 int ctxfncnt = 0, encfncnt = 0, decfncnt = 0;
191 int gparamfncnt = 0, sparamfncnt = 0;
192
193 if ((kem = evp_kem_new(prov)) == NULL) {
194 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
195 goto err;
196 }
197
198 kem->name_id = name_id;
199
200 for (; fns->function_id != 0; fns++) {
201 switch (fns->function_id) {
202 case OSSL_FUNC_KEM_NEWCTX:
203 if (kem->newctx != NULL)
204 break;
205 kem->newctx = OSSL_FUNC_kem_newctx(fns);
206 ctxfncnt++;
207 break;
208 case OSSL_FUNC_KEM_ENCAPSULATE_INIT:
209 if (kem->encapsulate_init != NULL)
210 break;
211 kem->encapsulate_init = OSSL_FUNC_kem_encapsulate_init(fns);
212 encfncnt++;
213 break;
214 case OSSL_FUNC_KEM_ENCAPSULATE:
215 if (kem->encapsulate != NULL)
216 break;
217 kem->encapsulate = OSSL_FUNC_kem_encapsulate(fns);
218 encfncnt++;
219 break;
220 case OSSL_FUNC_KEM_DECAPSULATE_INIT:
221 if (kem->decapsulate_init != NULL)
222 break;
223 kem->decapsulate_init = OSSL_FUNC_kem_decapsulate_init(fns);
224 decfncnt++;
225 break;
226 case OSSL_FUNC_KEM_DECAPSULATE:
227 if (kem->decapsulate != NULL)
228 break;
229 kem->decapsulate = OSSL_FUNC_kem_decapsulate(fns);
230 decfncnt++;
231 break;
232 case OSSL_FUNC_KEM_FREECTX:
233 if (kem->freectx != NULL)
234 break;
235 kem->freectx = OSSL_FUNC_kem_freectx(fns);
236 ctxfncnt++;
237 break;
238 case OSSL_FUNC_KEM_DUPCTX:
239 if (kem->dupctx != NULL)
240 break;
241 kem->dupctx = OSSL_FUNC_kem_dupctx(fns);
242 break;
243 case OSSL_FUNC_KEM_GET_CTX_PARAMS:
244 if (kem->get_ctx_params != NULL)
245 break;
246 kem->get_ctx_params
247 = OSSL_FUNC_kem_get_ctx_params(fns);
248 gparamfncnt++;
249 break;
250 case OSSL_FUNC_KEM_GETTABLE_CTX_PARAMS:
251 if (kem->gettable_ctx_params != NULL)
252 break;
253 kem->gettable_ctx_params
254 = OSSL_FUNC_kem_gettable_ctx_params(fns);
255 gparamfncnt++;
256 break;
257 case OSSL_FUNC_KEM_SET_CTX_PARAMS:
258 if (kem->set_ctx_params != NULL)
259 break;
260 kem->set_ctx_params
261 = OSSL_FUNC_kem_set_ctx_params(fns);
262 sparamfncnt++;
263 break;
264 case OSSL_FUNC_KEM_SETTABLE_CTX_PARAMS:
265 if (kem->settable_ctx_params != NULL)
266 break;
267 kem->settable_ctx_params
268 = OSSL_FUNC_kem_settable_ctx_params(fns);
269 sparamfncnt++;
270 break;
271 }
272 }
273 if (ctxfncnt != 2
274 || (encfncnt != 0 && encfncnt != 2)
275 || (decfncnt != 0 && decfncnt != 2)
276 || (encfncnt != 2 && decfncnt != 2)
277 || (gparamfncnt != 0 && gparamfncnt != 2)
278 || (sparamfncnt != 0 && sparamfncnt != 2)) {
279 /*
280 * In order to be a consistent set of functions we must have at least
281 * a set of context functions (newctx and freectx) as well as a pair of
282 * "kem" functions: (encapsulate_init, encapsulate) or
283 * (decapsulate_init, decapsulate). set_ctx_params and settable_ctx_params are
284 * optional, but if one of them is present then the other one must also
285 * be present. The same applies to get_ctx_params and
286 * gettable_ctx_params. The dupctx function is optional.
287 */
288 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
289 goto err;
290 }
291
292 return kem;
293 err:
294 EVP_KEM_free(kem);
295 return NULL;
296 }
297
298 void EVP_KEM_free(EVP_KEM *kem)
299 {
300 if (kem != NULL) {
301 int i;
302
303 CRYPTO_DOWN_REF(&kem->refcnt, &i, kem->lock);
304 if (i > 0)
305 return;
306 ossl_provider_free(kem->prov);
307 CRYPTO_THREAD_lock_free(kem->lock);
308 OPENSSL_free(kem);
309 }
310 }
311
312 int EVP_KEM_up_ref(EVP_KEM *kem)
313 {
314 int ref = 0;
315
316 CRYPTO_UP_REF(&kem->refcnt, &ref, kem->lock);
317 return 1;
318 }
319
320 OSSL_PROVIDER *EVP_KEM_provider(const EVP_KEM *kem)
321 {
322 return kem->prov;
323 }
324
325 EVP_KEM *EVP_KEM_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
326 const char *properties)
327 {
328 return evp_generic_fetch(ctx, OSSL_OP_KEM, algorithm, properties,
329 evp_kem_from_dispatch,
330 (int (*)(void *))EVP_KEM_up_ref,
331 (void (*)(void *))EVP_KEM_free);
332 }
333
334 int EVP_KEM_is_a(const EVP_KEM *kem, const char *name)
335 {
336 return evp_is_a(kem->prov, kem->name_id, NULL, name);
337 }
338
339 int EVP_KEM_number(const EVP_KEM *kem)
340 {
341 return kem->name_id;
342 }
343
344 void EVP_KEM_do_all_provided(OSSL_LIB_CTX *libctx,
345 void (*fn)(EVP_KEM *kem, void *arg),
346 void *arg)
347 {
348 evp_generic_do_all(libctx, OSSL_OP_KEM, (void (*)(void *, void *))fn, arg,
349 evp_kem_from_dispatch,
350 (void (*)(void *))EVP_KEM_free);
351 }
352
353 int EVP_KEM_names_do_all(const EVP_KEM *kem,
354 void (*fn)(const char *name, void *data),
355 void *data)
356 {
357 if (kem->prov != NULL)
358 return evp_names_do_all(kem->prov, kem->name_id, fn, data);
359
360 return 1;
361 }
362
363 const OSSL_PARAM *EVP_KEM_gettable_ctx_params(const EVP_KEM *kem)
364 {
365 void *provctx;
366
367 if (kem == NULL || kem->gettable_ctx_params == NULL)
368 return NULL;
369
370 provctx = ossl_provider_ctx(EVP_KEM_provider(kem));
371 return kem->gettable_ctx_params(NULL, provctx);
372 }
373
374 const OSSL_PARAM *EVP_KEM_settable_ctx_params(const EVP_KEM *kem)
375 {
376 void *provctx;
377
378 if (kem == NULL || kem->settable_ctx_params == NULL)
379 return NULL;
380
381 provctx = ossl_provider_ctx(EVP_KEM_provider(kem));
382 return kem->settable_ctx_params(NULL, provctx);
383 }