/src/openssl/providers/implementations/kem/ecx_kem.c
Line | Count | Source (jump to first uncovered line) |
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 | | /* |
12 | | * The following implementation is part of RFC 9180 related to DHKEM using |
13 | | * ECX keys (i.e. X25519 and X448) |
14 | | * References to Sections in the comments below refer to RFC 9180. |
15 | | */ |
16 | | |
17 | | #include "internal/deprecated.h" |
18 | | |
19 | | #include <string.h> |
20 | | #include <openssl/crypto.h> |
21 | | #include <openssl/evp.h> |
22 | | #include <openssl/core_dispatch.h> |
23 | | #include <openssl/core_names.h> |
24 | | #include <openssl/params.h> |
25 | | #include <openssl/kdf.h> |
26 | | #include <openssl/err.h> |
27 | | #include <openssl/sha.h> |
28 | | #include <openssl/rand.h> |
29 | | #include <openssl/proverr.h> |
30 | | #include "internal/cryptlib.h" |
31 | | #include "prov/provider_ctx.h" |
32 | | #include "prov/implementations.h" |
33 | | #include "prov/securitycheck.h" |
34 | | #include "prov/providercommon.h" |
35 | | #include "prov/ecx.h" |
36 | | #include "crypto/ecx.h" |
37 | | #include <openssl/hpke.h> |
38 | | #include "internal/hpke_util.h" |
39 | | #include "prov/eckem.h" |
40 | | |
41 | | #define MAX_ECX_KEYLEN X448_KEYLEN |
42 | | |
43 | | /* KEM identifiers from Section 7.1 "Table 2 KEM IDs" */ |
44 | | #define KEMID_X25519_HKDF_SHA256 0x20 |
45 | | #define KEMID_X448_HKDF_SHA512 0x21 |
46 | | |
47 | | /* ASCII: "KEM", in hex for EBCDIC compatibility */ |
48 | | static const char LABEL_KEM[] = "\x4b\x45\x4d"; |
49 | | |
50 | | typedef struct { |
51 | | ECX_KEY *recipient_key; |
52 | | ECX_KEY *sender_authkey; |
53 | | OSSL_LIB_CTX *libctx; |
54 | | char *propq; |
55 | | unsigned int mode; |
56 | | unsigned int op; |
57 | | unsigned char *ikm; |
58 | | size_t ikmlen; |
59 | | const char *kdfname; |
60 | | const OSSL_HPKE_KEM_INFO *info; |
61 | | } PROV_ECX_CTX; |
62 | | |
63 | | static OSSL_FUNC_kem_newctx_fn ecxkem_newctx; |
64 | | static OSSL_FUNC_kem_encapsulate_init_fn ecxkem_encapsulate_init; |
65 | | static OSSL_FUNC_kem_encapsulate_fn ecxkem_encapsulate; |
66 | | static OSSL_FUNC_kem_decapsulate_init_fn ecxkem_decapsulate_init; |
67 | | static OSSL_FUNC_kem_decapsulate_fn ecxkem_decapsulate; |
68 | | static OSSL_FUNC_kem_freectx_fn ecxkem_freectx; |
69 | | static OSSL_FUNC_kem_set_ctx_params_fn ecxkem_set_ctx_params; |
70 | | static OSSL_FUNC_kem_auth_encapsulate_init_fn ecxkem_auth_encapsulate_init; |
71 | | static OSSL_FUNC_kem_auth_decapsulate_init_fn ecxkem_auth_decapsulate_init; |
72 | | |
73 | | /* |
74 | | * Set KEM values as specified in Section 7.1 "Table 2 KEM IDs" |
75 | | * There is only one set of values for X25519 and X448. |
76 | | * Additional values could be set via set_params if required. |
77 | | */ |
78 | | static const OSSL_HPKE_KEM_INFO *get_kem_info(ECX_KEY *ecx) |
79 | 0 | { |
80 | 0 | const char *name = NULL; |
81 | |
|
82 | 0 | if (ecx->type == ECX_KEY_TYPE_X25519) |
83 | 0 | name = SN_X25519; |
84 | 0 | else |
85 | 0 | name = SN_X448; |
86 | 0 | return ossl_HPKE_KEM_INFO_find_curve(name); |
87 | 0 | } |
88 | | |
89 | | /* |
90 | | * Set the recipient key, and free any existing key. |
91 | | * ecx can be NULL. The ecx key may have only a private or public component. |
92 | | */ |
93 | | static int recipient_key_set(PROV_ECX_CTX *ctx, ECX_KEY *ecx) |
94 | 0 | { |
95 | 0 | ossl_ecx_key_free(ctx->recipient_key); |
96 | 0 | ctx->recipient_key = NULL; |
97 | 0 | if (ecx != NULL) { |
98 | 0 | ctx->info = get_kem_info(ecx); |
99 | 0 | if (ctx->info == NULL) |
100 | 0 | return -2; |
101 | 0 | ctx->kdfname = "HKDF"; |
102 | 0 | if (!ossl_ecx_key_up_ref(ecx)) |
103 | 0 | return 0; |
104 | 0 | ctx->recipient_key = ecx; |
105 | 0 | } |
106 | 0 | return 1; |
107 | 0 | } |
108 | | |
109 | | /* |
110 | | * Set the senders auth key, and free any existing auth key. |
111 | | * ecx can be NULL. |
112 | | */ |
113 | | static int sender_authkey_set(PROV_ECX_CTX *ctx, ECX_KEY *ecx) |
114 | 0 | { |
115 | 0 | ossl_ecx_key_free(ctx->sender_authkey); |
116 | 0 | ctx->sender_authkey = NULL; |
117 | |
|
118 | 0 | if (ecx != NULL) { |
119 | 0 | if (!ossl_ecx_key_up_ref(ecx)) |
120 | 0 | return 0; |
121 | 0 | ctx->sender_authkey = ecx; |
122 | 0 | } |
123 | 0 | return 1; |
124 | 0 | } |
125 | | |
126 | | /* |
127 | | * Serialize a public key from byte array's for the encoded public keys. |
128 | | * ctx is used to access the key type. |
129 | | * Returns: The created ECX_KEY or NULL on error. |
130 | | */ |
131 | | static ECX_KEY *ecxkey_pubfromdata(PROV_ECX_CTX *ctx, |
132 | | const unsigned char *pubbuf, size_t pubbuflen) |
133 | 0 | { |
134 | 0 | ECX_KEY *ecx = NULL; |
135 | 0 | OSSL_PARAM pub; |
136 | |
|
137 | 0 | pub = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PUB_KEY, |
138 | 0 | (char *)pubbuf, pubbuflen); |
139 | |
|
140 | 0 | ecx = ossl_ecx_key_new(ctx->libctx, ctx->recipient_key->type, 1, ctx->propq); |
141 | 0 | if (ecx == NULL) |
142 | 0 | return NULL; |
143 | 0 | if (ossl_ecx_key_fromdata(ecx, &pub, NULL, 0) <= 0) { |
144 | 0 | ossl_ecx_key_free(ecx); |
145 | 0 | ecx = NULL; |
146 | 0 | } |
147 | 0 | return ecx; |
148 | 0 | } |
149 | | |
150 | | static unsigned char *ecx_pubkey(ECX_KEY *ecx) |
151 | 0 | { |
152 | 0 | if (ecx == NULL || !ecx->haspubkey) { |
153 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY); |
154 | 0 | return 0; |
155 | 0 | } |
156 | 0 | return ecx->pubkey; |
157 | 0 | } |
158 | | |
159 | | static void *ecxkem_newctx(void *provctx) |
160 | 0 | { |
161 | 0 | PROV_ECX_CTX *ctx = OPENSSL_zalloc(sizeof(PROV_ECX_CTX)); |
162 | |
|
163 | 0 | if (ctx == NULL) |
164 | 0 | return NULL; |
165 | 0 | ctx->libctx = PROV_LIBCTX_OF(provctx); |
166 | 0 | ctx->mode = KEM_MODE_DHKEM; |
167 | |
|
168 | 0 | return ctx; |
169 | 0 | } |
170 | | |
171 | | static void ecxkem_freectx(void *vectx) |
172 | 0 | { |
173 | 0 | PROV_ECX_CTX *ctx = (PROV_ECX_CTX *)vectx; |
174 | |
|
175 | 0 | OPENSSL_clear_free(ctx->ikm, ctx->ikmlen); |
176 | 0 | recipient_key_set(ctx, NULL); |
177 | 0 | sender_authkey_set(ctx, NULL); |
178 | 0 | OPENSSL_free(ctx); |
179 | 0 | } |
180 | | |
181 | | static int ecx_match_params(const ECX_KEY *key1, const ECX_KEY *key2) |
182 | 0 | { |
183 | 0 | return (key1->type == key2->type && key1->keylen == key2->keylen); |
184 | 0 | } |
185 | | |
186 | | static int ecx_key_check(const ECX_KEY *ecx, int requires_privatekey) |
187 | 0 | { |
188 | 0 | if (ecx->privkey == NULL) |
189 | 0 | return (requires_privatekey == 0); |
190 | 0 | return 1; |
191 | 0 | } |
192 | | |
193 | | static int ecxkem_init(void *vecxctx, int operation, void *vecx, void *vauth, |
194 | | ossl_unused const OSSL_PARAM params[]) |
195 | 0 | { |
196 | 0 | int rv; |
197 | 0 | PROV_ECX_CTX *ctx = (PROV_ECX_CTX *)vecxctx; |
198 | 0 | ECX_KEY *ecx = vecx; |
199 | 0 | ECX_KEY *auth = vauth; |
200 | |
|
201 | 0 | if (!ossl_prov_is_running()) |
202 | 0 | return 0; |
203 | | |
204 | 0 | if (!ecx_key_check(ecx, operation == EVP_PKEY_OP_DECAPSULATE)) |
205 | 0 | return 0; |
206 | 0 | rv = recipient_key_set(ctx, ecx); |
207 | 0 | if (rv <= 0) |
208 | 0 | return rv; |
209 | | |
210 | 0 | if (auth != NULL) { |
211 | 0 | if (!ecx_match_params(auth, ctx->recipient_key) |
212 | 0 | || !ecx_key_check(auth, operation == EVP_PKEY_OP_ENCAPSULATE) |
213 | 0 | || !sender_authkey_set(ctx, auth)) |
214 | 0 | return 0; |
215 | 0 | } |
216 | | |
217 | 0 | ctx->op = operation; |
218 | 0 | return ecxkem_set_ctx_params(vecxctx, params); |
219 | 0 | } |
220 | | |
221 | | static int ecxkem_encapsulate_init(void *vecxctx, void *vecx, |
222 | | const OSSL_PARAM params[]) |
223 | 0 | { |
224 | 0 | return ecxkem_init(vecxctx, EVP_PKEY_OP_ENCAPSULATE, vecx, NULL, params); |
225 | 0 | } |
226 | | |
227 | | static int ecxkem_decapsulate_init(void *vecxctx, void *vecx, |
228 | | const OSSL_PARAM params[]) |
229 | 0 | { |
230 | 0 | return ecxkem_init(vecxctx, EVP_PKEY_OP_DECAPSULATE, vecx, NULL, params); |
231 | 0 | } |
232 | | |
233 | | static int ecxkem_auth_encapsulate_init(void *vctx, void *vecx, void *vauthpriv, |
234 | | const OSSL_PARAM params[]) |
235 | 0 | { |
236 | 0 | return ecxkem_init(vctx, EVP_PKEY_OP_ENCAPSULATE, vecx, vauthpriv, params); |
237 | 0 | } |
238 | | |
239 | | static int ecxkem_auth_decapsulate_init(void *vctx, void *vecx, void *vauthpub, |
240 | | const OSSL_PARAM params[]) |
241 | 0 | { |
242 | 0 | return ecxkem_init(vctx, EVP_PKEY_OP_DECAPSULATE, vecx, vauthpub, params); |
243 | 0 | } |
244 | | |
245 | | /* Machine generated by util/perl/OpenSSL/paramnames.pm */ |
246 | | #ifndef ecxkem_set_ctx_params_list |
247 | | static const OSSL_PARAM ecxkem_set_ctx_params_list[] = { |
248 | | OSSL_PARAM_utf8_string(OSSL_KEM_PARAM_OPERATION, NULL, 0), |
249 | | OSSL_PARAM_octet_string(OSSL_KEM_PARAM_IKME, NULL, 0), |
250 | | OSSL_PARAM_END |
251 | | }; |
252 | | #endif |
253 | | |
254 | | #ifndef ecxkem_set_ctx_params_st |
255 | | struct ecxkem_set_ctx_params_st { |
256 | | OSSL_PARAM *ikme; |
257 | | OSSL_PARAM *op; |
258 | | }; |
259 | | #endif |
260 | | |
261 | | #ifndef ecxkem_set_ctx_params_decoder |
262 | | static int ecxkem_set_ctx_params_decoder |
263 | | (const OSSL_PARAM *p, struct ecxkem_set_ctx_params_st *r) |
264 | 0 | { |
265 | 0 | const char *s; |
266 | |
|
267 | 0 | memset(r, 0, sizeof(*r)); |
268 | 0 | if (p != NULL) |
269 | 0 | for (; (s = p->key) != NULL; p++) |
270 | 0 | switch(s[0]) { |
271 | 0 | default: |
272 | 0 | break; |
273 | 0 | case 'i': |
274 | 0 | if (ossl_likely(strcmp("kme", s + 1) == 0)) { |
275 | | /* KEM_PARAM_IKME */ |
276 | 0 | if (ossl_unlikely(r->ikme != NULL)) { |
277 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
278 | 0 | "param %s is repeated", s); |
279 | 0 | return 0; |
280 | 0 | } |
281 | 0 | r->ikme = (OSSL_PARAM *)p; |
282 | 0 | } |
283 | 0 | break; |
284 | 0 | case 'o': |
285 | 0 | if (ossl_likely(strcmp("peration", s + 1) == 0)) { |
286 | | /* KEM_PARAM_OPERATION */ |
287 | 0 | if (ossl_unlikely(r->op != NULL)) { |
288 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER, |
289 | 0 | "param %s is repeated", s); |
290 | 0 | return 0; |
291 | 0 | } |
292 | 0 | r->op = (OSSL_PARAM *)p; |
293 | 0 | } |
294 | 0 | } |
295 | 0 | return 1; |
296 | 0 | } |
297 | | #endif |
298 | | /* End of machine generated */ |
299 | | |
300 | | static int ecxkem_set_ctx_params(void *vctx, const OSSL_PARAM params[]) |
301 | 0 | { |
302 | 0 | PROV_ECX_CTX *ctx = (PROV_ECX_CTX *)vctx; |
303 | 0 | struct ecxkem_set_ctx_params_st p; |
304 | 0 | int mode; |
305 | |
|
306 | 0 | if (ctx == NULL || !ecxkem_set_ctx_params_decoder(params, &p)) |
307 | 0 | return 0; |
308 | | |
309 | 0 | if (p.ikme != NULL) { |
310 | 0 | void *tmp = NULL; |
311 | 0 | size_t tmplen = 0; |
312 | |
|
313 | 0 | if (p.ikme->data != NULL && p.ikme->data_size != 0) { |
314 | 0 | if (!OSSL_PARAM_get_octet_string(p.ikme, &tmp, 0, &tmplen)) |
315 | 0 | return 0; |
316 | 0 | } |
317 | 0 | OPENSSL_clear_free(ctx->ikm, ctx->ikmlen); |
318 | 0 | ctx->ikm = tmp; |
319 | 0 | ctx->ikmlen = tmplen; |
320 | 0 | } |
321 | | |
322 | 0 | if (p.op != NULL) { |
323 | 0 | if (p.op->data_type != OSSL_PARAM_UTF8_STRING) |
324 | 0 | return 0; |
325 | 0 | mode = ossl_eckem_modename2id(p.op->data); |
326 | 0 | if (mode == KEM_MODE_UNDEFINED) |
327 | 0 | return 0; |
328 | 0 | ctx->mode = mode; |
329 | 0 | } |
330 | 0 | return 1; |
331 | 0 | } |
332 | | |
333 | | static const OSSL_PARAM *ecxkem_settable_ctx_params(ossl_unused void *vctx, |
334 | | ossl_unused void *provctx) |
335 | 0 | { |
336 | 0 | return ecxkem_set_ctx_params_list; |
337 | 0 | } |
338 | | |
339 | | /* |
340 | | * See Section 4.1 DH-Based KEM (DHKEM) ExtractAndExpand |
341 | | */ |
342 | | static int dhkem_extract_and_expand(EVP_KDF_CTX *kctx, |
343 | | unsigned char *okm, size_t okmlen, |
344 | | uint16_t kemid, |
345 | | const unsigned char *dhkm, size_t dhkmlen, |
346 | | const unsigned char *kemctx, |
347 | | size_t kemctxlen) |
348 | 0 | { |
349 | 0 | uint8_t suiteid[2]; |
350 | 0 | uint8_t prk[EVP_MAX_MD_SIZE]; |
351 | 0 | size_t prklen = okmlen; /* Nh */ |
352 | 0 | int ret; |
353 | |
|
354 | 0 | if (prklen > sizeof(prk)) |
355 | 0 | return 0; |
356 | | |
357 | 0 | suiteid[0] = (kemid >> 8) &0xff; |
358 | 0 | suiteid[1] = kemid & 0xff; |
359 | |
|
360 | 0 | ret = ossl_hpke_labeled_extract(kctx, prk, prklen, |
361 | 0 | NULL, 0, LABEL_KEM, suiteid, sizeof(suiteid), |
362 | 0 | OSSL_DHKEM_LABEL_EAE_PRK, dhkm, dhkmlen) |
363 | 0 | && ossl_hpke_labeled_expand(kctx, okm, okmlen, prk, prklen, |
364 | 0 | LABEL_KEM, suiteid, sizeof(suiteid), |
365 | 0 | OSSL_DHKEM_LABEL_SHARED_SECRET, |
366 | 0 | kemctx, kemctxlen); |
367 | 0 | OPENSSL_cleanse(prk, prklen); |
368 | 0 | return ret; |
369 | 0 | } |
370 | | |
371 | | /* |
372 | | * See Section 7.1.3 DeriveKeyPair. |
373 | | * |
374 | | * This function is used by ecx keygen. |
375 | | * (For this reason it does not use any of the state stored in PROV_ECX_CTX). |
376 | | * |
377 | | * Params: |
378 | | * ecx An initialized ecx key. |
379 | | * privout The buffer to store the generated private key into (it is assumed |
380 | | * this is of length ecx->keylen). |
381 | | * ikm buffer containing the input key material (seed). This must be non NULL. |
382 | | * ikmlen size of the ikm buffer in bytes |
383 | | * Returns: |
384 | | * 1 if successful or 0 otherwise. |
385 | | */ |
386 | | int ossl_ecx_dhkem_derive_private(ECX_KEY *ecx, unsigned char *privout, |
387 | | const unsigned char *ikm, size_t ikmlen) |
388 | 0 | { |
389 | 0 | int ret = 0; |
390 | 0 | EVP_KDF_CTX *kdfctx = NULL; |
391 | 0 | unsigned char prk[EVP_MAX_MD_SIZE]; |
392 | 0 | uint8_t suiteid[2]; |
393 | 0 | const OSSL_HPKE_KEM_INFO *info = get_kem_info(ecx); |
394 | | |
395 | | /* ikmlen should have a length of at least Nsk */ |
396 | 0 | if (ikmlen < info->Nsk) { |
397 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_INPUT_LENGTH, |
398 | 0 | "ikm length is :%zu, should be at least %zu", |
399 | 0 | ikmlen, info->Nsk); |
400 | 0 | goto err; |
401 | 0 | } |
402 | | |
403 | 0 | kdfctx = ossl_kdf_ctx_create("HKDF", info->mdname, ecx->libctx, ecx->propq); |
404 | 0 | if (kdfctx == NULL) |
405 | 0 | return 0; |
406 | | |
407 | 0 | suiteid[0] = info->kem_id / 256; |
408 | 0 | suiteid[1] = info->kem_id % 256; |
409 | |
|
410 | 0 | if (!ossl_hpke_labeled_extract(kdfctx, prk, info->Nsecret, |
411 | 0 | NULL, 0, LABEL_KEM, suiteid, sizeof(suiteid), |
412 | 0 | OSSL_DHKEM_LABEL_DKP_PRK, ikm, ikmlen)) |
413 | 0 | goto err; |
414 | | |
415 | 0 | if (!ossl_hpke_labeled_expand(kdfctx, privout, info->Nsk, prk, info->Nsecret, |
416 | 0 | LABEL_KEM, suiteid, sizeof(suiteid), |
417 | 0 | OSSL_DHKEM_LABEL_SK, NULL, 0)) |
418 | 0 | goto err; |
419 | 0 | ret = 1; |
420 | 0 | err: |
421 | 0 | OPENSSL_cleanse(prk, sizeof(prk)); |
422 | 0 | EVP_KDF_CTX_free(kdfctx); |
423 | 0 | return ret; |
424 | 0 | } |
425 | | |
426 | | /* |
427 | | * Do a keygen operation without having to use EVP_PKEY. |
428 | | * Params: |
429 | | * ctx Context object |
430 | | * ikm The seed material - if this is NULL, then a random seed is used. |
431 | | * Returns: |
432 | | * The generated ECX key, or NULL on failure. |
433 | | */ |
434 | | static ECX_KEY *derivekey(PROV_ECX_CTX *ctx, |
435 | | const unsigned char *ikm, size_t ikmlen) |
436 | 0 | { |
437 | 0 | int ok = 0; |
438 | 0 | ECX_KEY *key; |
439 | 0 | unsigned char *privkey; |
440 | 0 | unsigned char *seed = (unsigned char *)ikm; |
441 | 0 | size_t seedlen = ikmlen; |
442 | 0 | unsigned char tmpbuf[OSSL_HPKE_MAX_PRIVATE]; |
443 | 0 | const OSSL_HPKE_KEM_INFO *info = ctx->info; |
444 | |
|
445 | 0 | key = ossl_ecx_key_new(ctx->libctx, ctx->recipient_key->type, 0, ctx->propq); |
446 | 0 | if (key == NULL) |
447 | 0 | return NULL; |
448 | 0 | privkey = ossl_ecx_key_allocate_privkey(key); |
449 | 0 | if (privkey == NULL) |
450 | 0 | goto err; |
451 | | |
452 | | /* Generate a random seed if there is no input ikm */ |
453 | 0 | if (seed == NULL || seedlen == 0) { |
454 | 0 | if (info->Nsk > sizeof(tmpbuf)) |
455 | 0 | goto err; |
456 | 0 | if (RAND_priv_bytes_ex(ctx->libctx, tmpbuf, info->Nsk, 0) <= 0) |
457 | 0 | goto err; |
458 | 0 | seed = tmpbuf; |
459 | 0 | seedlen = info->Nsk; |
460 | 0 | } |
461 | 0 | if (!ossl_ecx_dhkem_derive_private(key, privkey, seed, seedlen)) |
462 | 0 | goto err; |
463 | 0 | if (!ossl_ecx_public_from_private(key)) |
464 | 0 | goto err; |
465 | 0 | key->haspubkey = 1; |
466 | 0 | ok = 1; |
467 | 0 | err: |
468 | 0 | if (!ok) { |
469 | 0 | ossl_ecx_key_free(key); |
470 | 0 | key = NULL; |
471 | 0 | } |
472 | 0 | if (seed != ikm) |
473 | 0 | OPENSSL_cleanse(seed, seedlen); |
474 | 0 | return key; |
475 | 0 | } |
476 | | |
477 | | /* |
478 | | * Do an ecxdh key exchange. |
479 | | * dhkm = DH(sender, peer) |
480 | | * |
481 | | * NOTE: Instead of using EVP_PKEY_derive() API's, we use ECX_KEY operations |
482 | | * to avoid messy conversions back to EVP_PKEY. |
483 | | * |
484 | | * Returns the size of the secret if successful, or 0 otherwise, |
485 | | */ |
486 | | static int generate_ecxdhkm(const ECX_KEY *sender, const ECX_KEY *peer, |
487 | | unsigned char *out, size_t maxout, |
488 | | unsigned int secretsz) |
489 | 0 | { |
490 | 0 | size_t len = 0; |
491 | | |
492 | | /* NOTE: ossl_ecx_compute_key checks for shared secret being all zeros */ |
493 | 0 | return ossl_ecx_compute_key((ECX_KEY *)peer, (ECX_KEY *)sender, |
494 | 0 | sender->keylen, out, &len, maxout); |
495 | 0 | } |
496 | | |
497 | | /* |
498 | | * Derive a secret using ECXDH (code is shared by the encap and decap) |
499 | | * |
500 | | * dhkm = Concat(ecxdh(privkey1, peerkey1), ecdh(privkey2, peerkey2) |
501 | | * kemctx = Concat(sender_pub, recipient_pub, ctx->sender_authkey) |
502 | | * secret = dhkem_extract_and_expand(kemid, dhkm, kemctx); |
503 | | * |
504 | | * Params: |
505 | | * ctx Object that contains algorithm state and constants. |
506 | | * secret The returned secret (with a length ctx->alg->secretlen bytes). |
507 | | * privkey1 A private key used for ECXDH key derivation. |
508 | | * peerkey1 A public key used for ECXDH key derivation with privkey1 |
509 | | * privkey2 A optional private key used for a second ECXDH key derivation. |
510 | | * It can be NULL. |
511 | | * peerkey2 A optional public key used for a second ECXDH key derivation |
512 | | * with privkey2,. It can be NULL. |
513 | | * sender_pub The senders public key in encoded form. |
514 | | * recipient_pub The recipients public key in encoded form. |
515 | | * Notes: |
516 | | * The second ecdh() is only used for the HPKE auth modes when both privkey2 |
517 | | * and peerkey2 are non NULL (i.e. ctx->sender_authkey is not NULL). |
518 | | */ |
519 | | static int derive_secret(PROV_ECX_CTX *ctx, unsigned char *secret, |
520 | | const ECX_KEY *privkey1, const ECX_KEY *peerkey1, |
521 | | const ECX_KEY *privkey2, const ECX_KEY *peerkey2, |
522 | | const unsigned char *sender_pub, |
523 | | const unsigned char *recipient_pub) |
524 | 0 | { |
525 | 0 | int ret = 0; |
526 | 0 | EVP_KDF_CTX *kdfctx = NULL; |
527 | 0 | unsigned char *sender_authpub = NULL; |
528 | 0 | unsigned char dhkm[MAX_ECX_KEYLEN * 2]; |
529 | 0 | unsigned char kemctx[MAX_ECX_KEYLEN * 3]; |
530 | 0 | size_t kemctxlen = 0, dhkmlen = 0; |
531 | 0 | const OSSL_HPKE_KEM_INFO *info = ctx->info; |
532 | 0 | int auth = ctx->sender_authkey != NULL; |
533 | 0 | size_t encodedkeylen = info->Npk; |
534 | |
|
535 | 0 | if (!generate_ecxdhkm(privkey1, peerkey1, dhkm, sizeof(dhkm), |
536 | 0 | (unsigned int)encodedkeylen)) |
537 | 0 | goto err; |
538 | 0 | dhkmlen = encodedkeylen; |
539 | | |
540 | | /* Concat the optional second ECXDH (used for Auth) */ |
541 | 0 | if (auth) { |
542 | 0 | if (!generate_ecxdhkm(privkey2, peerkey2, |
543 | 0 | dhkm + dhkmlen, sizeof(dhkm) - dhkmlen, |
544 | 0 | (unsigned int)encodedkeylen)) |
545 | 0 | goto err; |
546 | | /* Get the public key of the auth sender in encoded form */ |
547 | 0 | sender_authpub = ecx_pubkey(ctx->sender_authkey); |
548 | 0 | if (sender_authpub == NULL) |
549 | 0 | goto err; |
550 | 0 | dhkmlen += encodedkeylen; |
551 | 0 | } |
552 | 0 | kemctxlen = encodedkeylen + dhkmlen; |
553 | 0 | if (kemctxlen > sizeof(kemctx)) |
554 | 0 | goto err; |
555 | | |
556 | | /* kemctx is the concat of both sides encoded public key */ |
557 | 0 | memcpy(kemctx, sender_pub, encodedkeylen); |
558 | 0 | memcpy(kemctx + encodedkeylen, recipient_pub, encodedkeylen); |
559 | 0 | if (auth) |
560 | 0 | memcpy(kemctx + 2 * encodedkeylen, sender_authpub, encodedkeylen); |
561 | 0 | kdfctx = ossl_kdf_ctx_create(ctx->kdfname, info->mdname, |
562 | 0 | ctx->libctx, ctx->propq); |
563 | 0 | if (kdfctx == NULL) |
564 | 0 | goto err; |
565 | 0 | if (!dhkem_extract_and_expand(kdfctx, secret, info->Nsecret, |
566 | 0 | info->kem_id, dhkm, dhkmlen, |
567 | 0 | kemctx, kemctxlen)) |
568 | 0 | goto err; |
569 | 0 | ret = 1; |
570 | 0 | err: |
571 | 0 | OPENSSL_cleanse(dhkm, dhkmlen); |
572 | 0 | EVP_KDF_CTX_free(kdfctx); |
573 | 0 | return ret; |
574 | 0 | } |
575 | | |
576 | | /* |
577 | | * Do a DHKEM encapsulate operation. |
578 | | * |
579 | | * See Section 4.1 Encap() and AuthEncap() |
580 | | * |
581 | | * Params: |
582 | | * ctx A context object holding the recipients public key and the |
583 | | * optional senders auth private key. |
584 | | * enc A buffer to return the senders ephemeral public key. |
585 | | * Setting this to NULL allows the enclen and secretlen to return |
586 | | * values, without calculating the secret. |
587 | | * enclen Passes in the max size of the enc buffer and returns the |
588 | | * encoded public key length. |
589 | | * secret A buffer to return the calculated shared secret. |
590 | | * secretlen Passes in the max size of the secret buffer and returns the |
591 | | * secret length. |
592 | | * Returns: 1 on success or 0 otherwise. |
593 | | */ |
594 | | static int dhkem_encap(PROV_ECX_CTX *ctx, |
595 | | unsigned char *enc, size_t *enclen, |
596 | | unsigned char *secret, size_t *secretlen) |
597 | 0 | { |
598 | 0 | int ret = 0; |
599 | 0 | ECX_KEY *sender_ephemkey = NULL; |
600 | 0 | unsigned char *sender_ephempub, *recipient_pub; |
601 | 0 | const OSSL_HPKE_KEM_INFO *info = ctx->info; |
602 | |
|
603 | 0 | if (enc == NULL) { |
604 | 0 | if (enclen == NULL && secretlen == NULL) |
605 | 0 | return 0; |
606 | 0 | if (enclen != NULL) |
607 | 0 | *enclen = info->Nenc; |
608 | 0 | if (secretlen != NULL) |
609 | 0 | *secretlen = info->Nsecret; |
610 | 0 | return 1; |
611 | 0 | } |
612 | | |
613 | 0 | if (*secretlen < info->Nsecret) { |
614 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_BAD_LENGTH, "*secretlen too small"); |
615 | 0 | return 0; |
616 | 0 | } |
617 | 0 | if (*enclen < info->Nenc) { |
618 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_BAD_LENGTH, "*enclen too small"); |
619 | 0 | return 0; |
620 | 0 | } |
621 | | |
622 | | /* Create an ephemeral key */ |
623 | 0 | sender_ephemkey = derivekey(ctx, ctx->ikm, ctx->ikmlen); |
624 | |
|
625 | 0 | sender_ephempub = ecx_pubkey(sender_ephemkey); |
626 | 0 | recipient_pub = ecx_pubkey(ctx->recipient_key); |
627 | 0 | if (sender_ephempub == NULL || recipient_pub == NULL) |
628 | 0 | goto err; |
629 | | |
630 | 0 | if (!derive_secret(ctx, secret, |
631 | 0 | sender_ephemkey, ctx->recipient_key, |
632 | 0 | ctx->sender_authkey, ctx->recipient_key, |
633 | 0 | sender_ephempub, recipient_pub)) |
634 | 0 | goto err; |
635 | | |
636 | | /* Return the public part of the ephemeral key */ |
637 | 0 | memcpy(enc, sender_ephempub, info->Nenc); |
638 | 0 | *enclen = info->Nenc; |
639 | 0 | *secretlen = info->Nsecret; |
640 | 0 | ret = 1; |
641 | 0 | err: |
642 | 0 | ossl_ecx_key_free(sender_ephemkey); |
643 | 0 | return ret; |
644 | 0 | } |
645 | | |
646 | | /* |
647 | | * Do a DHKEM decapsulate operation. |
648 | | * See Section 4.1 Decap() and Auth Decap() |
649 | | * |
650 | | * Params: |
651 | | * ctx A context object holding the recipients private key and the |
652 | | * optional senders auth public key. |
653 | | * secret A buffer to return the calculated shared secret. Setting this to |
654 | | * NULL can be used to return the secretlen. |
655 | | * secretlen Passes in the max size of the secret buffer and returns the |
656 | | * secret length. |
657 | | * enc A buffer containing the senders ephemeral public key that was returned |
658 | | * from dhkem_encap(). |
659 | | * enclen The length in bytes of enc. |
660 | | * Returns: 1 If the shared secret is returned or 0 on error. |
661 | | */ |
662 | | static int dhkem_decap(PROV_ECX_CTX *ctx, |
663 | | unsigned char *secret, size_t *secretlen, |
664 | | const unsigned char *enc, size_t enclen) |
665 | 0 | { |
666 | 0 | int ret = 0; |
667 | 0 | ECX_KEY *recipient_privkey = ctx->recipient_key; |
668 | 0 | ECX_KEY *sender_ephempubkey = NULL; |
669 | 0 | const OSSL_HPKE_KEM_INFO *info = ctx->info; |
670 | 0 | unsigned char *recipient_pub; |
671 | |
|
672 | 0 | if (secret == NULL) { |
673 | 0 | *secretlen = info->Nsecret; |
674 | 0 | return 1; |
675 | 0 | } |
676 | 0 | if (*secretlen < info->Nsecret) { |
677 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_BAD_LENGTH, "*secretlen too small"); |
678 | 0 | return 0; |
679 | 0 | } |
680 | 0 | if (enclen != info->Nenc) { |
681 | 0 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_KEY, "Invalid enc public key"); |
682 | 0 | return 0; |
683 | 0 | } |
684 | | |
685 | | /* Get the public part of the ephemeral key created by encap */ |
686 | 0 | sender_ephempubkey = ecxkey_pubfromdata(ctx, enc, enclen); |
687 | 0 | if (sender_ephempubkey == NULL) |
688 | 0 | goto err; |
689 | | |
690 | 0 | recipient_pub = ecx_pubkey(recipient_privkey); |
691 | 0 | if (recipient_pub == NULL) |
692 | 0 | goto err; |
693 | | |
694 | 0 | if (!derive_secret(ctx, secret, |
695 | 0 | ctx->recipient_key, sender_ephempubkey, |
696 | 0 | ctx->recipient_key, ctx->sender_authkey, |
697 | 0 | enc, recipient_pub)) |
698 | 0 | goto err; |
699 | | |
700 | 0 | *secretlen = info->Nsecret; |
701 | 0 | ret = 1; |
702 | 0 | err: |
703 | 0 | ossl_ecx_key_free(sender_ephempubkey); |
704 | 0 | return ret; |
705 | 0 | } |
706 | | |
707 | | static int ecxkem_encapsulate(void *vctx, unsigned char *out, size_t *outlen, |
708 | | unsigned char *secret, size_t *secretlen) |
709 | 0 | { |
710 | 0 | PROV_ECX_CTX *ctx = (PROV_ECX_CTX *)vctx; |
711 | |
|
712 | 0 | switch (ctx->mode) { |
713 | 0 | case KEM_MODE_DHKEM: |
714 | 0 | return dhkem_encap(ctx, out, outlen, secret, secretlen); |
715 | 0 | default: |
716 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE); |
717 | 0 | return -2; |
718 | 0 | } |
719 | 0 | } |
720 | | |
721 | | static int ecxkem_decapsulate(void *vctx, unsigned char *out, size_t *outlen, |
722 | | const unsigned char *in, size_t inlen) |
723 | 0 | { |
724 | 0 | PROV_ECX_CTX *ctx = (PROV_ECX_CTX *)vctx; |
725 | |
|
726 | 0 | switch (ctx->mode) { |
727 | 0 | case KEM_MODE_DHKEM: |
728 | 0 | return dhkem_decap(vctx, out, outlen, in, inlen); |
729 | 0 | default: |
730 | 0 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_MODE); |
731 | 0 | return -2; |
732 | 0 | } |
733 | 0 | } |
734 | | |
735 | | const OSSL_DISPATCH ossl_ecx_asym_kem_functions[] = { |
736 | | { OSSL_FUNC_KEM_NEWCTX, (void (*)(void))ecxkem_newctx }, |
737 | | { OSSL_FUNC_KEM_ENCAPSULATE_INIT, |
738 | | (void (*)(void))ecxkem_encapsulate_init }, |
739 | | { OSSL_FUNC_KEM_ENCAPSULATE, (void (*)(void))ecxkem_encapsulate }, |
740 | | { OSSL_FUNC_KEM_DECAPSULATE_INIT, |
741 | | (void (*)(void))ecxkem_decapsulate_init }, |
742 | | { OSSL_FUNC_KEM_DECAPSULATE, (void (*)(void))ecxkem_decapsulate }, |
743 | | { OSSL_FUNC_KEM_FREECTX, (void (*)(void))ecxkem_freectx }, |
744 | | { OSSL_FUNC_KEM_SET_CTX_PARAMS, |
745 | | (void (*)(void))ecxkem_set_ctx_params }, |
746 | | { OSSL_FUNC_KEM_SETTABLE_CTX_PARAMS, |
747 | | (void (*)(void))ecxkem_settable_ctx_params }, |
748 | | { OSSL_FUNC_KEM_AUTH_ENCAPSULATE_INIT, |
749 | | (void (*)(void))ecxkem_auth_encapsulate_init }, |
750 | | { OSSL_FUNC_KEM_AUTH_DECAPSULATE_INIT, |
751 | | (void (*)(void))ecxkem_auth_decapsulate_init }, |
752 | | OSSL_DISPATCH_END |
753 | | }; |