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