/src/openssl/providers/implementations/kem/ec_kem.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * Copyright 2022-2025 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  |  | /*  | 
11  |  |  * The following implementation is part of RFC 9180 related to DHKEM using  | 
12  |  |  * EC keys (i.e. P-256, P-384 and P-521)  | 
13  |  |  * References to Sections in the comments below refer to RFC 9180.  | 
14  |  |  */  | 
15  |  |  | 
16  |  | #include "internal/deprecated.h"  | 
17  |  |  | 
18  |  | #include <openssl/crypto.h>  | 
19  |  | #include <openssl/evp.h>  | 
20  |  | #include <openssl/core_dispatch.h>  | 
21  |  | #include <openssl/core_names.h>  | 
22  |  | #include <openssl/ec.h>  | 
23  |  | #include <openssl/params.h>  | 
24  |  | #include <openssl/err.h>  | 
25  |  | #include <openssl/proverr.h>  | 
26  |  | #include <openssl/kdf.h>  | 
27  |  | #include <openssl/rand.h>  | 
28  |  | #include "internal/cryptlib.h"  | 
29  |  | #include "prov/provider_ctx.h"  | 
30  |  | #include "prov/implementations.h"  | 
31  |  | #include "prov/securitycheck.h"  | 
32  |  | #include "prov/providercommon.h"  | 
33  |  |  | 
34  |  | #include <openssl/hpke.h>  | 
35  |  | #include "internal/hpke_util.h"  | 
36  |  | #include "crypto/ec.h"  | 
37  |  | #include "prov/ecx.h"  | 
38  |  | #include "prov/eckem.h"  | 
39  |  | #include "providers/implementations/kem/ec_kem.inc"  | 
40  |  |  | 
41  |  | typedef struct { | 
42  |  |     EC_KEY *recipient_key;  | 
43  |  |     EC_KEY *sender_authkey;  | 
44  |  |     OSSL_LIB_CTX *libctx;  | 
45  |  |     char *propq;  | 
46  |  |     unsigned int mode;  | 
47  |  |     unsigned int op;  | 
48  |  |     unsigned char *ikm;  | 
49  |  |     size_t ikmlen;  | 
50  |  |     const char *kdfname;  | 
51  |  |     const OSSL_HPKE_KEM_INFO *info;  | 
52  |  | } PROV_EC_CTX;  | 
53  |  |  | 
54  |  | static OSSL_FUNC_kem_newctx_fn eckem_newctx;  | 
55  |  | static OSSL_FUNC_kem_encapsulate_init_fn eckem_encapsulate_init;  | 
56  |  | static OSSL_FUNC_kem_auth_encapsulate_init_fn eckem_auth_encapsulate_init;  | 
57  |  | static OSSL_FUNC_kem_encapsulate_fn eckem_encapsulate;  | 
58  |  | static OSSL_FUNC_kem_decapsulate_init_fn eckem_decapsulate_init;  | 
59  |  | static OSSL_FUNC_kem_auth_decapsulate_init_fn eckem_auth_decapsulate_init;  | 
60  |  | static OSSL_FUNC_kem_decapsulate_fn eckem_decapsulate;  | 
61  |  | static OSSL_FUNC_kem_freectx_fn eckem_freectx;  | 
62  |  | static OSSL_FUNC_kem_set_ctx_params_fn eckem_set_ctx_params;  | 
63  |  | static OSSL_FUNC_kem_settable_ctx_params_fn eckem_settable_ctx_params;  | 
64  |  |  | 
65  |  | /* ASCII: "KEM", in hex for EBCDIC compatibility */  | 
66  |  | static const char LABEL_KEM[] = "\x4b\x45\x4d";  | 
67  |  |  | 
68  |  | static int eckey_check(const EC_KEY *ec, int requires_privatekey)  | 
69  | 0  | { | 
70  | 0  |     int rv = 0;  | 
71  | 0  |     BN_CTX *bnctx = NULL;  | 
72  | 0  |     BIGNUM *rem = NULL;  | 
73  | 0  |     const BIGNUM *priv = EC_KEY_get0_private_key(ec);  | 
74  | 0  |     const EC_POINT *pub = EC_KEY_get0_public_key(ec);  | 
75  |  |  | 
76  |  |     /* Keys always require a public component */  | 
77  | 0  |     if (pub == NULL) { | 
78  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);  | 
79  | 0  |         return 0;  | 
80  | 0  |     }  | 
81  | 0  |     if (priv == NULL) { | 
82  | 0  |         return (requires_privatekey == 0);  | 
83  | 0  |     } else { | 
84  |  |         /* If there is a private key, check that is non zero (mod order) */  | 
85  | 0  |         const EC_GROUP *group = EC_KEY_get0_group(ec);  | 
86  | 0  |         const BIGNUM *order = EC_GROUP_get0_order(group);  | 
87  |  | 
  | 
88  | 0  |         bnctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(ec));  | 
89  | 0  |         rem = BN_new();  | 
90  |  | 
  | 
91  | 0  |         if (order != NULL && rem != NULL && bnctx != NULL) { | 
92  | 0  |              rv = BN_mod(rem, priv, order, bnctx)  | 
93  | 0  |                   && !BN_is_zero(rem);  | 
94  | 0  |         }  | 
95  | 0  |     }  | 
96  | 0  |     BN_free(rem);  | 
97  | 0  |     BN_CTX_free(bnctx);  | 
98  | 0  |     return rv;  | 
99  | 0  | }  | 
100  |  |  | 
101  |  | /* Returns NULL if the curve is not supported */  | 
102  |  | static const char *ec_curvename_get0(const EC_KEY *ec)  | 
103  | 0  | { | 
104  | 0  |     const EC_GROUP *group = EC_KEY_get0_group(ec);  | 
105  |  | 
  | 
106  | 0  |     return EC_curve_nid2nist(EC_GROUP_get_curve_name(group));  | 
107  | 0  | }  | 
108  |  |  | 
109  |  | /*  | 
110  |  |  * Set the recipient key, and free any existing key.  | 
111  |  |  * ec can be NULL.  | 
112  |  |  * The ec key may have only a private or public component  | 
113  |  |  * (but it must have a group).  | 
114  |  |  */  | 
115  |  | static int recipient_key_set(PROV_EC_CTX *ctx, EC_KEY *ec)  | 
116  | 0  | { | 
117  | 0  |     EC_KEY_free(ctx->recipient_key);  | 
118  | 0  |     ctx->recipient_key = NULL;  | 
119  |  | 
  | 
120  | 0  |     if (ec != NULL) { | 
121  | 0  |         const char *curve = ec_curvename_get0(ec);  | 
122  |  | 
  | 
123  | 0  |         if (curve == NULL)  | 
124  | 0  |             return -2;  | 
125  | 0  |         ctx->info = ossl_HPKE_KEM_INFO_find_curve(curve);  | 
126  | 0  |         if (ctx->info == NULL)  | 
127  | 0  |             return -2;  | 
128  | 0  |         if (!EC_KEY_up_ref(ec))  | 
129  | 0  |             return 0;  | 
130  | 0  |         ctx->recipient_key = ec;  | 
131  | 0  |         ctx->kdfname = "HKDF";  | 
132  | 0  |     }  | 
133  | 0  |     return 1;  | 
134  | 0  | }  | 
135  |  |  | 
136  |  | /*  | 
137  |  |  * Set the senders auth key, and free any existing auth key.  | 
138  |  |  * ec can be NULL.  | 
139  |  |  */  | 
140  |  | static int sender_authkey_set(PROV_EC_CTX *ctx, EC_KEY *ec)  | 
141  | 0  | { | 
142  | 0  |     EC_KEY_free(ctx->sender_authkey);  | 
143  | 0  |     ctx->sender_authkey = NULL;  | 
144  |  | 
  | 
145  | 0  |     if (ec != NULL) { | 
146  | 0  |         if (!EC_KEY_up_ref(ec))  | 
147  | 0  |             return 0;  | 
148  | 0  |         ctx->sender_authkey = ec;  | 
149  | 0  |     }  | 
150  | 0  |     return 1;  | 
151  | 0  | }  | 
152  |  |  | 
153  |  | /*  | 
154  |  |  * Serializes a encoded public key buffer into a EC public key.  | 
155  |  |  * Params:  | 
156  |  |  *     in Contains the group.  | 
157  |  |  *     pubbuf The encoded public key buffer  | 
158  |  |  * Returns: The created public EC key, or NULL if there is an error.  | 
159  |  |  */  | 
160  |  | static EC_KEY *eckey_frompub(EC_KEY *in,  | 
161  |  |                              const unsigned char *pubbuf, size_t pubbuflen)  | 
162  | 0  | { | 
163  | 0  |     EC_KEY *key;  | 
164  |  | 
  | 
165  | 0  |     key = EC_KEY_new_ex(ossl_ec_key_get_libctx(in), ossl_ec_key_get0_propq(in));  | 
166  | 0  |     if (key == NULL)  | 
167  | 0  |         goto err;  | 
168  | 0  |     if (!EC_KEY_set_group(key, EC_KEY_get0_group(in)))  | 
169  | 0  |         goto err;  | 
170  | 0  |     if (!EC_KEY_oct2key(key, pubbuf, pubbuflen, NULL))  | 
171  | 0  |         goto err;  | 
172  | 0  |     return key;  | 
173  | 0  | err:  | 
174  | 0  |     EC_KEY_free(key);  | 
175  | 0  |     return NULL;  | 
176  | 0  | }  | 
177  |  |  | 
178  |  | /*  | 
179  |  |  * Deserialises a EC public key into a encoded byte array.  | 
180  |  |  * Returns: 1 if successful or 0 otherwise.  | 
181  |  |  */  | 
182  |  | static int ecpubkey_todata(const EC_KEY *ec, unsigned char *out, size_t *outlen,  | 
183  |  |                            size_t maxoutlen)  | 
184  | 0  | { | 
185  | 0  |     const EC_POINT *pub;  | 
186  | 0  |     const EC_GROUP *group;  | 
187  |  | 
  | 
188  | 0  |     group = EC_KEY_get0_group(ec);  | 
189  | 0  |     pub = EC_KEY_get0_public_key(ec);  | 
190  | 0  |     *outlen = EC_POINT_point2oct(group, pub, POINT_CONVERSION_UNCOMPRESSED,  | 
191  | 0  |                                  out, maxoutlen, NULL);  | 
192  | 0  |     return *outlen != 0;  | 
193  | 0  | }  | 
194  |  |  | 
195  |  | static void *eckem_newctx(void *provctx)  | 
196  | 0  | { | 
197  | 0  |     PROV_EC_CTX *ctx =  OPENSSL_zalloc(sizeof(PROV_EC_CTX));  | 
198  |  | 
  | 
199  | 0  |     if (ctx == NULL)  | 
200  | 0  |         return NULL;  | 
201  | 0  |     ctx->libctx = PROV_LIBCTX_OF(provctx);  | 
202  | 0  |     ctx->mode = KEM_MODE_DHKEM;  | 
203  |  | 
  | 
204  | 0  |     return ctx;  | 
205  | 0  | }  | 
206  |  |  | 
207  |  | static void eckem_freectx(void *vectx)  | 
208  | 0  | { | 
209  | 0  |     PROV_EC_CTX *ctx = (PROV_EC_CTX *)vectx;  | 
210  |  | 
  | 
211  | 0  |     OPENSSL_clear_free(ctx->ikm, ctx->ikmlen);  | 
212  | 0  |     recipient_key_set(ctx, NULL);  | 
213  | 0  |     sender_authkey_set(ctx, NULL);  | 
214  | 0  |     OPENSSL_free(ctx);  | 
215  | 0  | }  | 
216  |  |  | 
217  |  | static int ossl_ec_match_params(const EC_KEY *key1, const EC_KEY *key2)  | 
218  | 0  | { | 
219  | 0  |     int ret;  | 
220  | 0  |     BN_CTX *ctx = NULL;  | 
221  | 0  |     const EC_GROUP *group1 = EC_KEY_get0_group(key1);  | 
222  | 0  |     const EC_GROUP *group2 = EC_KEY_get0_group(key2);  | 
223  |  | 
  | 
224  | 0  |     ctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(key1));  | 
225  | 0  |     if (ctx == NULL)  | 
226  | 0  |         return 0;  | 
227  |  |  | 
228  | 0  |     ret = group1 != NULL  | 
229  | 0  |           && group2 != NULL  | 
230  | 0  |           && EC_GROUP_cmp(group1, group2, ctx) == 0;  | 
231  | 0  |     if (!ret)  | 
232  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_MISMATCHING_DOMAIN_PARAMETERS);  | 
233  | 0  |     BN_CTX_free(ctx);  | 
234  | 0  |     return ret;  | 
235  | 0  | }  | 
236  |  |  | 
237  |  | static int eckem_init(void *vctx, int operation, void *vec, void *vauth,  | 
238  |  |                       const OSSL_PARAM params[])  | 
239  | 0  | { | 
240  | 0  |     int rv;  | 
241  | 0  |     PROV_EC_CTX *ctx = (PROV_EC_CTX *)vctx;  | 
242  | 0  |     EC_KEY *ec = vec;  | 
243  | 0  |     EC_KEY *auth = vauth;  | 
244  |  | 
  | 
245  | 0  |     if (!ossl_prov_is_running())  | 
246  | 0  |         return 0;  | 
247  |  |  | 
248  | 0  |     if (!eckey_check(ec, operation == EVP_PKEY_OP_DECAPSULATE))  | 
249  | 0  |         return 0;  | 
250  | 0  |     rv = recipient_key_set(ctx, ec);  | 
251  | 0  |     if (rv <= 0)  | 
252  | 0  |         return rv;  | 
253  |  |  | 
254  | 0  |     if (auth != NULL) { | 
255  | 0  |         if (!ossl_ec_match_params(ec, auth)  | 
256  | 0  |             || !eckey_check(auth, operation == EVP_PKEY_OP_ENCAPSULATE)  | 
257  | 0  |             || !sender_authkey_set(ctx, auth))  | 
258  | 0  |         return 0;  | 
259  | 0  |     }  | 
260  |  |  | 
261  | 0  |     ctx->op = operation;  | 
262  | 0  |     return eckem_set_ctx_params(vctx, params);  | 
263  | 0  | }  | 
264  |  |  | 
265  |  | static int eckem_encapsulate_init(void *vctx, void *vec,  | 
266  |  |                                    const OSSL_PARAM params[])  | 
267  | 0  | { | 
268  | 0  |     return eckem_init(vctx, EVP_PKEY_OP_ENCAPSULATE, vec, NULL, params);  | 
269  | 0  | }  | 
270  |  |  | 
271  |  | static int eckem_decapsulate_init(void *vctx, void *vec,  | 
272  |  |                                    const OSSL_PARAM params[])  | 
273  | 0  | { | 
274  | 0  |     return eckem_init(vctx, EVP_PKEY_OP_DECAPSULATE, vec, NULL, params);  | 
275  | 0  | }  | 
276  |  |  | 
277  |  | static int eckem_auth_encapsulate_init(void *vctx, void *vecx, void *vauthpriv,  | 
278  |  |                                        const OSSL_PARAM params[])  | 
279  | 0  | { | 
280  | 0  |     return eckem_init(vctx, EVP_PKEY_OP_ENCAPSULATE, vecx, vauthpriv, params);  | 
281  | 0  | }  | 
282  |  |  | 
283  |  | static int eckem_auth_decapsulate_init(void *vctx, void *vecx, void *vauthpub,  | 
284  |  |                                        const OSSL_PARAM params[])  | 
285  | 0  | { | 
286  | 0  |     return eckem_init(vctx, EVP_PKEY_OP_DECAPSULATE, vecx, vauthpub, params);  | 
287  | 0  | }  | 
288  |  |  | 
289  |  | static int eckem_set_ctx_params(void *vctx, const OSSL_PARAM params[])  | 
290  | 0  | { | 
291  | 0  |     PROV_EC_CTX *ctx = (PROV_EC_CTX *)vctx;  | 
292  | 0  |     struct eckem_set_ctx_params_st p;  | 
293  | 0  |     int mode;  | 
294  |  | 
  | 
295  | 0  |     if (ctx == NULL || !eckem_set_ctx_params_decoder(params, &p))  | 
296  | 0  |         return 0;  | 
297  |  |  | 
298  | 0  |     if (p.ikme != NULL) { | 
299  | 0  |         void *tmp = NULL;  | 
300  | 0  |         size_t tmplen = 0;  | 
301  |  | 
  | 
302  | 0  |         if (p.ikme->data != NULL && p.ikme->data_size != 0) { | 
303  | 0  |             if (!OSSL_PARAM_get_octet_string(p.ikme, &tmp, 0, &tmplen))  | 
304  | 0  |                 return 0;  | 
305  | 0  |         }  | 
306  | 0  |         OPENSSL_clear_free(ctx->ikm, ctx->ikmlen);  | 
307  |  |         /* Set the ephemeral seed */  | 
308  | 0  |         ctx->ikm = tmp;  | 
309  | 0  |         ctx->ikmlen = tmplen;  | 
310  | 0  |     }  | 
311  |  |  | 
312  | 0  |     if (p.op != NULL) { | 
313  | 0  |         if (p.op->data_type != OSSL_PARAM_UTF8_STRING)  | 
314  | 0  |             return 0;  | 
315  | 0  |         mode = ossl_eckem_modename2id(p.op->data);  | 
316  | 0  |         if (mode == KEM_MODE_UNDEFINED)  | 
317  | 0  |             return 0;  | 
318  | 0  |         ctx->mode = mode;  | 
319  | 0  |     }  | 
320  | 0  |     return 1;  | 
321  | 0  | }  | 
322  |  |  | 
323  |  | static const OSSL_PARAM *eckem_settable_ctx_params(ossl_unused void *vctx,  | 
324  |  |                                                    ossl_unused void *provctx)  | 
325  | 0  | { | 
326  | 0  |     return eckem_set_ctx_params_list;  | 
327  | 0  | }  | 
328  |  |  | 
329  |  | /*  | 
330  |  |  * See Section 4.1 DH-Based KEM (DHKEM) ExtractAndExpand  | 
331  |  |  */  | 
332  |  | static int dhkem_extract_and_expand(EVP_KDF_CTX *kctx,  | 
333  |  |                                     unsigned char *okm, size_t okmlen,  | 
334  |  |                                     uint16_t kemid,  | 
335  |  |                                     const unsigned char *dhkm, size_t dhkmlen,  | 
336  |  |                                     const unsigned char *kemctx,  | 
337  |  |                                     size_t kemctxlen)  | 
338  | 0  | { | 
339  | 0  |     uint8_t suiteid[2];  | 
340  | 0  |     uint8_t prk[EVP_MAX_MD_SIZE];  | 
341  | 0  |     size_t prklen = okmlen;  | 
342  | 0  |     int ret;  | 
343  |  | 
  | 
344  | 0  |     if (prklen > sizeof(prk))  | 
345  | 0  |         return 0;  | 
346  |  |  | 
347  | 0  |     suiteid[0] = (kemid >> 8) & 0xff;  | 
348  | 0  |     suiteid[1] = kemid & 0xff;  | 
349  |  | 
  | 
350  | 0  |     ret = ossl_hpke_labeled_extract(kctx, prk, prklen,  | 
351  | 0  |                                     NULL, 0, LABEL_KEM, suiteid, sizeof(suiteid),  | 
352  | 0  |                                     OSSL_DHKEM_LABEL_EAE_PRK, dhkm, dhkmlen)  | 
353  | 0  |           && ossl_hpke_labeled_expand(kctx, okm, okmlen, prk, prklen,  | 
354  | 0  |                                       LABEL_KEM, suiteid, sizeof(suiteid),  | 
355  | 0  |                                       OSSL_DHKEM_LABEL_SHARED_SECRET,  | 
356  | 0  |                                       kemctx, kemctxlen);  | 
357  | 0  |     OPENSSL_cleanse(prk, prklen);  | 
358  | 0  |     return ret;  | 
359  | 0  | }  | 
360  |  |  | 
361  |  | /*  | 
362  |  |  * See Section 7.1.3 DeriveKeyPair.  | 
363  |  |  *  | 
364  |  |  * This function is used by ec keygen.  | 
365  |  |  * (For this reason it does not use any of the state stored in PROV_EC_CTX).  | 
366  |  |  *  | 
367  |  |  * Params:  | 
368  |  |  *     ec An initialized ec key.  | 
369  |  |  *     priv The buffer to store the generated private key into (it is assumed  | 
370  |  |  *          this is of length alg->encodedprivlen).  | 
371  |  |  *     ikm buffer containing the input key material (seed). This must be set.  | 
372  |  |  *     ikmlen size of the ikm buffer in bytes  | 
373  |  |  * Returns:  | 
374  |  |  *     1 if successful or 0 otherwise.  | 
375  |  |  */  | 
376  |  | int ossl_ec_dhkem_derive_private(EC_KEY *ec, BIGNUM *priv,  | 
377  |  |                                  const unsigned char *ikm, size_t ikmlen)  | 
378  | 0  | { | 
379  | 0  |     int ret = 0;  | 
380  | 0  |     EVP_KDF_CTX *kdfctx = NULL;  | 
381  | 0  |     uint8_t suiteid[2];  | 
382  | 0  |     unsigned char prk[OSSL_HPKE_MAX_SECRET];  | 
383  | 0  |     unsigned char privbuf[OSSL_HPKE_MAX_PRIVATE];  | 
384  | 0  |     const BIGNUM *order;  | 
385  | 0  |     unsigned char counter = 0;  | 
386  | 0  |     const char *curve = ec_curvename_get0(ec);  | 
387  | 0  |     const OSSL_HPKE_KEM_INFO *info;  | 
388  |  | 
  | 
389  | 0  |     if (curve == NULL)  | 
390  | 0  |         return -2;  | 
391  |  |  | 
392  | 0  |     info = ossl_HPKE_KEM_INFO_find_curve(curve);  | 
393  | 0  |     if (info == NULL)  | 
394  | 0  |         return -2;  | 
395  |  |  | 
396  | 0  |     kdfctx = ossl_kdf_ctx_create("HKDF", info->mdname, | 
397  | 0  |                                  ossl_ec_key_get_libctx(ec),  | 
398  | 0  |                                  ossl_ec_key_get0_propq(ec));  | 
399  | 0  |     if (kdfctx == NULL)  | 
400  | 0  |         return 0;  | 
401  |  |  | 
402  |  |     /* ikmlen should have a length of at least Nsk */  | 
403  | 0  |     if (ikmlen < info->Nsk) { | 
404  | 0  |         ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_INPUT_LENGTH,  | 
405  | 0  |                        "ikm length is :%zu, should be at least %zu",  | 
406  | 0  |                        ikmlen, info->Nsk);  | 
407  | 0  |         goto err;  | 
408  | 0  |     }  | 
409  |  |  | 
410  | 0  |     suiteid[0] = info->kem_id / 256;  | 
411  | 0  |     suiteid[1] = info->kem_id % 256;  | 
412  |  | 
  | 
413  | 0  |     if (!ossl_hpke_labeled_extract(kdfctx, prk, info->Nsecret,  | 
414  | 0  |                                    NULL, 0, LABEL_KEM, suiteid, sizeof(suiteid),  | 
415  | 0  |                                    OSSL_DHKEM_LABEL_DKP_PRK, ikm, ikmlen))  | 
416  | 0  |         goto err;  | 
417  |  |  | 
418  | 0  |     order = EC_GROUP_get0_order(EC_KEY_get0_group(ec));  | 
419  | 0  |     do { | 
420  | 0  |         if (!ossl_hpke_labeled_expand(kdfctx, privbuf, info->Nsk,  | 
421  | 0  |                                       prk, info->Nsecret,  | 
422  | 0  |                                       LABEL_KEM, suiteid, sizeof(suiteid),  | 
423  | 0  |                                       OSSL_DHKEM_LABEL_CANDIDATE,  | 
424  | 0  |                                       &counter, 1))  | 
425  | 0  |             goto err;  | 
426  | 0  |         privbuf[0] &= info->bitmask;  | 
427  | 0  |         if (BN_bin2bn(privbuf, (int)info->Nsk, priv) == NULL)  | 
428  | 0  |             goto err;  | 
429  | 0  |         if (counter == 0xFF) { | 
430  | 0  |             ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY);  | 
431  | 0  |             goto err;  | 
432  | 0  |         }  | 
433  | 0  |         counter++;  | 
434  | 0  |     } while (BN_is_zero(priv) || BN_cmp(priv, order) >= 0);  | 
435  | 0  |     ret = 1;  | 
436  | 0  | err:  | 
437  | 0  |     OPENSSL_cleanse(prk, sizeof(prk));  | 
438  | 0  |     OPENSSL_cleanse(privbuf, sizeof(privbuf));  | 
439  | 0  |     EVP_KDF_CTX_free(kdfctx);  | 
440  | 0  |     return ret;  | 
441  | 0  | }  | 
442  |  |  | 
443  |  | /*  | 
444  |  |  * Do a keygen operation without having to use EVP_PKEY.  | 
445  |  |  * Params:  | 
446  |  |  *     ctx Context object  | 
447  |  |  *     ikm The seed material - if this is NULL, then a random seed is used.  | 
448  |  |  * Returns:  | 
449  |  |  *     The generated EC key, or NULL on failure.  | 
450  |  |  */  | 
451  |  | static EC_KEY *derivekey(PROV_EC_CTX *ctx,  | 
452  |  |                          const unsigned char *ikm, size_t ikmlen)  | 
453  | 0  | { | 
454  | 0  |     int ret = 0;  | 
455  | 0  |     EC_KEY *key;  | 
456  | 0  |     unsigned char *seed = (unsigned char *)ikm;  | 
457  | 0  |     size_t seedlen = ikmlen;  | 
458  | 0  |     unsigned char tmpbuf[OSSL_HPKE_MAX_PRIVATE];  | 
459  |  | 
  | 
460  | 0  |     key = EC_KEY_new_ex(ctx->libctx, ctx->propq);  | 
461  | 0  |     if (key == NULL)  | 
462  | 0  |         goto err;  | 
463  | 0  |     if (!EC_KEY_set_group(key, EC_KEY_get0_group(ctx->recipient_key)))  | 
464  | 0  |         goto err;  | 
465  |  |  | 
466  |  |     /* Generate a random seed if there is no input ikm */  | 
467  | 0  |     if (seed == NULL || seedlen == 0) { | 
468  | 0  |         seedlen = ctx->info->Nsk;  | 
469  | 0  |         if (seedlen > sizeof(tmpbuf))  | 
470  | 0  |             goto err;  | 
471  | 0  |         if (RAND_priv_bytes_ex(ctx->libctx, tmpbuf, seedlen, 0) <= 0)  | 
472  | 0  |             goto err;  | 
473  | 0  |         seed = tmpbuf;  | 
474  | 0  |     }  | 
475  | 0  |     ret = ossl_ec_generate_key_dhkem(key, seed, seedlen);  | 
476  | 0  | err:  | 
477  | 0  |     if (seed != ikm)  | 
478  | 0  |         OPENSSL_cleanse(seed, seedlen);  | 
479  | 0  |     if (ret <= 0) { | 
480  | 0  |         EC_KEY_free(key);  | 
481  | 0  |         key = NULL;  | 
482  | 0  |     }  | 
483  | 0  |     return key;  | 
484  | 0  | }  | 
485  |  |  | 
486  |  | /*  | 
487  |  |  * Before doing a key exchange the public key of the peer needs to be checked  | 
488  |  |  * Note that the group check is not done here as we have already checked  | 
489  |  |  * that it only uses one of the approved curve names when the key was set.  | 
490  |  |  *  | 
491  |  |  * Returns 1 if the public key is valid, or 0 if it fails.  | 
492  |  |  */  | 
493  |  | static int check_publickey(const EC_KEY *pub)  | 
494  | 0  | { | 
495  | 0  |     int ret = 0;  | 
496  | 0  |     BN_CTX *bnctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(pub));  | 
497  |  | 
  | 
498  | 0  |     if (bnctx == NULL)  | 
499  | 0  |         return 0;  | 
500  | 0  |     ret = ossl_ec_key_public_check(pub, bnctx);  | 
501  | 0  |     BN_CTX_free(bnctx);  | 
502  |  | 
  | 
503  | 0  |     return ret;  | 
504  | 0  | }  | 
505  |  |  | 
506  |  | /*  | 
507  |  |  * Do an ecdh key exchange.  | 
508  |  |  * dhkm = DH(sender, peer)  | 
509  |  |  *  | 
510  |  |  * NOTE: Instead of using EVP_PKEY_derive() API's, we use EC_KEY operations  | 
511  |  |  *       to avoid messy conversions back to EVP_PKEY.  | 
512  |  |  *  | 
513  |  |  * Returns the size of the secret if successful, or 0 otherwise,  | 
514  |  |  */  | 
515  |  | static int generate_ecdhkm(const EC_KEY *sender, const EC_KEY *peer,  | 
516  |  |                            unsigned char *out, size_t maxout,  | 
517  |  |                            unsigned int secretsz)  | 
518  | 0  | { | 
519  | 0  |     const EC_GROUP *group = EC_KEY_get0_group(sender);  | 
520  | 0  |     size_t secretlen = (EC_GROUP_get_degree(group) + 7) / 8;  | 
521  |  | 
  | 
522  | 0  |     if (secretlen != secretsz || secretlen > maxout) { | 
523  | 0  |         ERR_raise_data(ERR_LIB_PROV,  PROV_R_BAD_LENGTH, "secretsz invalid");  | 
524  | 0  |         return 0;  | 
525  | 0  |     }  | 
526  |  |  | 
527  | 0  |     if (!check_publickey(peer))  | 
528  | 0  |         return 0;  | 
529  | 0  |     return ECDH_compute_key(out, secretlen, EC_KEY_get0_public_key(peer),  | 
530  | 0  |                             sender, NULL) > 0;  | 
531  | 0  | }  | 
532  |  |  | 
533  |  | /*  | 
534  |  |  * Derive a secret using ECDH (code is shared by the encap and decap)  | 
535  |  |  *  | 
536  |  |  * dhkm = Concat(ecdh(privkey1, peerkey1), ecdh(privkey2, peerkey2)  | 
537  |  |  * kemctx = Concat(sender_pub, recipient_pub, ctx->sender_authkey)  | 
538  |  |  * secret = dhkem_extract_and_expand(kemid, dhkm, kemctx);  | 
539  |  |  *  | 
540  |  |  * Params:  | 
541  |  |  *     ctx Object that contains algorithm state and constants.  | 
542  |  |  *     secret The returned secret (with a length ctx->alg->secretlen bytes).  | 
543  |  |  *     privkey1 A private key used for ECDH key derivation.  | 
544  |  |  *     peerkey1 A public key used for ECDH key derivation with privkey1  | 
545  |  |  *     privkey2 A optional private key used for a second ECDH key derivation.  | 
546  |  |  *              It can be NULL.  | 
547  |  |  *     peerkey2 A optional public key used for a second ECDH key derivation  | 
548  |  |  *              with privkey2,. It can be NULL.  | 
549  |  |  *     sender_pub The senders public key in encoded form.  | 
550  |  |  *     recipient_pub The recipients public key in encoded form.  | 
551  |  |  * Notes:  | 
552  |  |  *     The second ecdh() is only used for the HPKE auth modes when both privkey2  | 
553  |  |  *     and peerkey2 are non NULL (i.e. ctx->sender_authkey is not NULL).  | 
554  |  |  */  | 
555  |  | static int derive_secret(PROV_EC_CTX *ctx, unsigned char *secret,  | 
556  |  |                          const EC_KEY *privkey1, const EC_KEY *peerkey1,  | 
557  |  |                          const EC_KEY *privkey2, const EC_KEY *peerkey2,  | 
558  |  |                          const unsigned char *sender_pub,  | 
559  |  |                          const unsigned char *recipient_pub)  | 
560  | 0  | { | 
561  | 0  |     int ret = 0;  | 
562  | 0  |     EVP_KDF_CTX *kdfctx = NULL;  | 
563  | 0  |     unsigned char sender_authpub[OSSL_HPKE_MAX_PUBLIC];  | 
564  | 0  |     unsigned char dhkm[OSSL_HPKE_MAX_PRIVATE * 2];  | 
565  | 0  |     unsigned char kemctx[OSSL_HPKE_MAX_PUBLIC * 3];  | 
566  | 0  |     size_t sender_authpublen;  | 
567  | 0  |     size_t kemctxlen = 0, dhkmlen = 0;  | 
568  | 0  |     const OSSL_HPKE_KEM_INFO *info = ctx->info;  | 
569  | 0  |     size_t encodedpublen = info->Npk;  | 
570  | 0  |     size_t encodedprivlen = info->Nsk;  | 
571  | 0  |     int auth = ctx->sender_authkey != NULL;  | 
572  |  | 
  | 
573  | 0  |     if (!generate_ecdhkm(privkey1, peerkey1, dhkm, sizeof(dhkm),  | 
574  | 0  |                          (unsigned int)encodedprivlen))  | 
575  | 0  |         goto err;  | 
576  | 0  |     dhkmlen = encodedprivlen;  | 
577  | 0  |     kemctxlen = 2 * encodedpublen;  | 
578  |  |  | 
579  |  |     /* Concat the optional second ECDH (used for Auth) */  | 
580  | 0  |     if (auth) { | 
581  |  |         /* Get the public key of the auth sender in encoded form */  | 
582  | 0  |         if (!ecpubkey_todata(ctx->sender_authkey, sender_authpub,  | 
583  | 0  |                              &sender_authpublen, sizeof(sender_authpub)))  | 
584  | 0  |             goto err;  | 
585  | 0  |         if (sender_authpublen != encodedpublen) { | 
586  | 0  |             ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY,  | 
587  | 0  |                            "Invalid sender auth public key");  | 
588  | 0  |             goto err;  | 
589  | 0  |         }  | 
590  | 0  |         if (!generate_ecdhkm(privkey2, peerkey2,  | 
591  | 0  |                              dhkm + dhkmlen, sizeof(dhkm) - dhkmlen,  | 
592  | 0  |                              (unsigned int)encodedprivlen))  | 
593  | 0  |             goto err;  | 
594  | 0  |         dhkmlen += encodedprivlen;  | 
595  | 0  |         kemctxlen += encodedpublen;  | 
596  | 0  |     }  | 
597  | 0  |     if (kemctxlen > sizeof(kemctx))  | 
598  | 0  |         goto err;  | 
599  |  |  | 
600  |  |     /* kemctx is the concat of both sides encoded public key */  | 
601  | 0  |     memcpy(kemctx, sender_pub, info->Npk);  | 
602  | 0  |     memcpy(kemctx + info->Npk, recipient_pub, info->Npk);  | 
603  | 0  |     if (auth)  | 
604  | 0  |         memcpy(kemctx + 2 * encodedpublen, sender_authpub, encodedpublen);  | 
605  | 0  |     kdfctx = ossl_kdf_ctx_create(ctx->kdfname, info->mdname,  | 
606  | 0  |                                  ctx->libctx, ctx->propq);  | 
607  | 0  |     if (kdfctx == NULL)  | 
608  | 0  |         goto err;  | 
609  | 0  |     if (!dhkem_extract_and_expand(kdfctx, secret, info->Nsecret,  | 
610  | 0  |                                   info->kem_id, dhkm, dhkmlen,  | 
611  | 0  |                                   kemctx, kemctxlen))  | 
612  | 0  |         goto err;  | 
613  | 0  |     ret = 1;  | 
614  | 0  | err:  | 
615  | 0  |     OPENSSL_cleanse(dhkm, dhkmlen);  | 
616  | 0  |     EVP_KDF_CTX_free(kdfctx);  | 
617  | 0  |     return ret;  | 
618  | 0  | }  | 
619  |  |  | 
620  |  | /*  | 
621  |  |  * Do a DHKEM encapsulate operation.  | 
622  |  |  *  | 
623  |  |  * See Section 4.1 Encap() and AuthEncap()  | 
624  |  |  *  | 
625  |  |  * Params:  | 
626  |  |  *     ctx A context object holding the recipients public key and the  | 
627  |  |  *         optional senders auth private key.  | 
628  |  |  *     enc A buffer to return the senders ephemeral public key.  | 
629  |  |  *         Setting this to NULL allows the enclen and secretlen to return  | 
630  |  |  *         values, without calculating the secret.  | 
631  |  |  *     enclen Passes in the max size of the enc buffer and returns the  | 
632  |  |  *            encoded public key length.  | 
633  |  |  *     secret A buffer to return the calculated shared secret.  | 
634  |  |  *     secretlen Passes in the max size of the secret buffer and returns the  | 
635  |  |  *               secret length.  | 
636  |  |  * Returns: 1 on success or 0 otherwise.  | 
637  |  |  */  | 
638  |  | static int dhkem_encap(PROV_EC_CTX *ctx,  | 
639  |  |                        unsigned char *enc, size_t *enclen,  | 
640  |  |                        unsigned char *secret, size_t *secretlen)  | 
641  | 0  | { | 
642  | 0  |     int ret = 0;  | 
643  | 0  |     EC_KEY *sender_ephemkey = NULL;  | 
644  | 0  |     unsigned char sender_pub[OSSL_HPKE_MAX_PUBLIC];  | 
645  | 0  |     unsigned char recipient_pub[OSSL_HPKE_MAX_PUBLIC];  | 
646  | 0  |     size_t sender_publen, recipient_publen;  | 
647  | 0  |     const OSSL_HPKE_KEM_INFO *info = ctx->info;  | 
648  |  | 
  | 
649  | 0  |     if (enc == NULL) { | 
650  | 0  |         if (enclen == NULL && secretlen == NULL)  | 
651  | 0  |             return 0;  | 
652  | 0  |         if (enclen != NULL)  | 
653  | 0  |             *enclen = info->Nenc;  | 
654  | 0  |         if (secretlen != NULL)  | 
655  | 0  |             *secretlen = info->Nsecret;  | 
656  | 0  |        return 1;  | 
657  | 0  |     }  | 
658  |  |  | 
659  | 0  |     if (*secretlen < info->Nsecret) { | 
660  | 0  |         ERR_raise_data(ERR_LIB_PROV, PROV_R_BAD_LENGTH, "*secretlen too small");  | 
661  | 0  |         return 0;  | 
662  | 0  |     }  | 
663  | 0  |     if (*enclen < info->Nenc) { | 
664  | 0  |         ERR_raise_data(ERR_LIB_PROV, PROV_R_BAD_LENGTH, "*enclen too small");  | 
665  | 0  |         return 0;  | 
666  | 0  |     }  | 
667  |  |  | 
668  |  |     /* Create an ephemeral key */  | 
669  | 0  |     sender_ephemkey = derivekey(ctx, ctx->ikm, ctx->ikmlen);  | 
670  | 0  |     if (sender_ephemkey == NULL)  | 
671  | 0  |         goto err;  | 
672  | 0  |     if (!ecpubkey_todata(sender_ephemkey, sender_pub, &sender_publen,  | 
673  | 0  |                          sizeof(sender_pub))  | 
674  | 0  |             || !ecpubkey_todata(ctx->recipient_key, recipient_pub,  | 
675  | 0  |                                 &recipient_publen, sizeof(recipient_pub)))  | 
676  | 0  |         goto err;  | 
677  |  |  | 
678  | 0  |     if (sender_publen != info->Npk  | 
679  | 0  |             || recipient_publen != sender_publen) { | 
680  | 0  |         ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY, "Invalid public key");  | 
681  | 0  |         goto err;  | 
682  | 0  |     }  | 
683  |  |  | 
684  | 0  |     if (!derive_secret(ctx, secret,  | 
685  | 0  |                        sender_ephemkey, ctx->recipient_key,  | 
686  | 0  |                        ctx->sender_authkey, ctx->recipient_key,  | 
687  | 0  |                        sender_pub, recipient_pub))  | 
688  | 0  |         goto err;  | 
689  |  |  | 
690  |  |     /* Return the senders ephemeral public key in encoded form */  | 
691  | 0  |     memcpy(enc, sender_pub, sender_publen);  | 
692  | 0  |     *enclen = sender_publen;  | 
693  | 0  |     *secretlen = info->Nsecret;  | 
694  | 0  |     ret = 1;  | 
695  | 0  | err:  | 
696  | 0  |     EC_KEY_free(sender_ephemkey);  | 
697  | 0  |     return ret;  | 
698  | 0  | }  | 
699  |  |  | 
700  |  | /*  | 
701  |  |  * Do a DHKEM decapsulate operation.  | 
702  |  |  * See Section 4.1 Decap() and Auth Decap()  | 
703  |  |  *  | 
704  |  |  * Params:  | 
705  |  |  *     ctx A context object holding the recipients private key and the  | 
706  |  |  *         optional senders auth public key.  | 
707  |  |  *     secret A buffer to return the calculated shared secret. Setting this to  | 
708  |  |  *            NULL can be used to return the secretlen.  | 
709  |  |  *     secretlen Passes in the max size of the secret buffer and returns the  | 
710  |  |  *               secret length.  | 
711  |  |  *     enc A buffer containing the senders ephemeral public key that was returned  | 
712  |  |  *         from dhkem_encap().  | 
713  |  |  *     enclen The length in bytes of enc.  | 
714  |  |  * Returns: 1 If the shared secret is returned or 0 on error.  | 
715  |  |  */  | 
716  |  | static int dhkem_decap(PROV_EC_CTX *ctx,  | 
717  |  |                        unsigned char *secret, size_t *secretlen,  | 
718  |  |                        const unsigned char *enc, size_t enclen)  | 
719  | 0  | { | 
720  | 0  |     int ret = 0;  | 
721  | 0  |     EC_KEY *sender_ephempubkey = NULL;  | 
722  | 0  |     const OSSL_HPKE_KEM_INFO *info = ctx->info;  | 
723  | 0  |     unsigned char recipient_pub[OSSL_HPKE_MAX_PUBLIC];  | 
724  | 0  |     size_t recipient_publen;  | 
725  | 0  |     size_t encodedpublen = info->Npk;  | 
726  |  | 
  | 
727  | 0  |     if (secret == NULL) { | 
728  | 0  |         *secretlen = info->Nsecret;  | 
729  | 0  |         return 1;  | 
730  | 0  |     }  | 
731  |  |  | 
732  | 0  |     if (*secretlen < info->Nsecret) { | 
733  | 0  |         ERR_raise_data(ERR_LIB_PROV, PROV_R_BAD_LENGTH, "*secretlen too small");  | 
734  | 0  |         return 0;  | 
735  | 0  |     }  | 
736  | 0  |     if (enclen != encodedpublen) { | 
737  | 0  |         ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY, "Invalid enc public key");  | 
738  | 0  |         return 0;  | 
739  | 0  |     }  | 
740  |  |  | 
741  | 0  |     sender_ephempubkey = eckey_frompub(ctx->recipient_key, enc, enclen);  | 
742  | 0  |     if (sender_ephempubkey == NULL)  | 
743  | 0  |         goto err;  | 
744  | 0  |     if (!ecpubkey_todata(ctx->recipient_key, recipient_pub, &recipient_publen,  | 
745  | 0  |                          sizeof(recipient_pub)))  | 
746  | 0  |         goto err;  | 
747  | 0  |     if (recipient_publen != encodedpublen) { | 
748  | 0  |         ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY, "Invalid recipient public key");  | 
749  | 0  |         goto err;  | 
750  | 0  |     }  | 
751  |  |  | 
752  | 0  |     if (!derive_secret(ctx, secret,  | 
753  | 0  |                        ctx->recipient_key, sender_ephempubkey,  | 
754  | 0  |                        ctx->recipient_key, ctx->sender_authkey,  | 
755  | 0  |                        enc, recipient_pub))  | 
756  | 0  |         goto err;  | 
757  | 0  |     *secretlen = info->Nsecret;  | 
758  | 0  |     ret = 1;  | 
759  | 0  | err:  | 
760  | 0  |     EC_KEY_free(sender_ephempubkey);  | 
761  | 0  |     return ret;  | 
762  | 0  | }  | 
763  |  |  | 
764  |  | static int eckem_encapsulate(void *vctx, unsigned char *out, size_t *outlen,  | 
765  |  |                              unsigned char *secret, size_t *secretlen)  | 
766  | 0  | { | 
767  | 0  |     PROV_EC_CTX *ctx = (PROV_EC_CTX *)vctx;  | 
768  |  | 
  | 
769  | 0  |     switch (ctx->mode) { | 
770  | 0  |         case KEM_MODE_DHKEM:  | 
771  | 0  |             return dhkem_encap(ctx, out, outlen, secret, secretlen);  | 
772  | 0  |         default:  | 
773  | 0  |             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);  | 
774  | 0  |             return -2;  | 
775  | 0  |     }  | 
776  | 0  | }  | 
777  |  |  | 
778  |  | static int eckem_decapsulate(void *vctx, unsigned char *out, size_t *outlen,  | 
779  |  |                              const unsigned char *in, size_t inlen)  | 
780  | 0  | { | 
781  | 0  |     PROV_EC_CTX *ctx = (PROV_EC_CTX *)vctx;  | 
782  |  | 
  | 
783  | 0  |     switch (ctx->mode) { | 
784  | 0  |         case KEM_MODE_DHKEM:  | 
785  | 0  |             return dhkem_decap(ctx, out, outlen, in, inlen);  | 
786  | 0  |         default:  | 
787  |  |             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE);  | 
788  | 0  |             return -2;  | 
789  | 0  |     }  | 
790  | 0  | }  | 
791  |  |  | 
792  |  | const OSSL_DISPATCH ossl_ec_asym_kem_functions[] = { | 
793  |  |     { OSSL_FUNC_KEM_NEWCTX, (void (*)(void))eckem_newctx }, | 
794  |  |     { OSSL_FUNC_KEM_ENCAPSULATE_INIT, | 
795  |  |       (void (*)(void))eckem_encapsulate_init },  | 
796  |  |     { OSSL_FUNC_KEM_ENCAPSULATE, (void (*)(void))eckem_encapsulate }, | 
797  |  |     { OSSL_FUNC_KEM_DECAPSULATE_INIT, | 
798  |  |       (void (*)(void))eckem_decapsulate_init },  | 
799  |  |     { OSSL_FUNC_KEM_DECAPSULATE, (void (*)(void))eckem_decapsulate }, | 
800  |  |     { OSSL_FUNC_KEM_FREECTX, (void (*)(void))eckem_freectx }, | 
801  |  |     { OSSL_FUNC_KEM_SET_CTX_PARAMS, | 
802  |  |       (void (*)(void))eckem_set_ctx_params },  | 
803  |  |     { OSSL_FUNC_KEM_SETTABLE_CTX_PARAMS, | 
804  |  |       (void (*)(void))eckem_settable_ctx_params },  | 
805  |  |     { OSSL_FUNC_KEM_AUTH_ENCAPSULATE_INIT, | 
806  |  |       (void (*)(void))eckem_auth_encapsulate_init },  | 
807  |  |     { OSSL_FUNC_KEM_AUTH_DECAPSULATE_INIT, | 
808  |  |       (void (*)(void))eckem_auth_decapsulate_init },  | 
809  |  |     OSSL_DISPATCH_END  | 
810  |  | };  |