Debian Codegraph @

cipher/kem.c

raw
1 /* kem.c - Key Encapsulation Mechanisms
2 * Copyright (C) 2023 Simon Josefsson <[email protected]>
3 * Copyright (C) 2023 g10 Code GmbH
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, see <https://www.gnu.org/licenses/>.
19 * SPDX-License-Identifier: LGPL-2.1-or-later
20 */
21
22 #include <config.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27
28 #include "g10lib.h"
29 #include "cipher.h"
30 #include "sntrup761.h"
31 #include "mceliece6688128f.h"
32 #if USE_KYBER
33 #include "kyber.h"
34 #endif
35 #include "kem-ecc.h"
36
37
38 /* Information about the the KEM algoithms for use by the s-expression
39 * interface. */
40 static const struct
41 {
42 const char *name; /* Name of the algo. */
43 unsigned int namelen; /* Only here to avoid strlen calls. */
44 int algo; /* KEM algo number. */
45 unsigned int nbits; /* Number of bits. */
46 unsigned int fips:1; /* True if this is a FIPS140-3 approved KEM. */
47 int pubkey_len; /* Length of the public key. */
48 int seckey_len; /* Length of the secret key. */
49 } kem_infos[] =
50 {
51 { "sntrup761", 9, GCRY_KEM_SNTRUP761, 761, 0,
52 GCRY_KEM_SNTRUP761_PUBKEY_LEN, GCRY_KEM_SNTRUP761_SECKEY_LEN },
53 { "kyber512", 8, GCRY_KEM_MLKEM512, 512, 0,
54 GCRY_KEM_MLKEM512_PUBKEY_LEN, GCRY_KEM_MLKEM512_SECKEY_LEN },
55 { "kyber768", 8, GCRY_KEM_MLKEM768, 768, 1,
56 GCRY_KEM_MLKEM768_PUBKEY_LEN, GCRY_KEM_MLKEM768_SECKEY_LEN },
57 { "kyber1024", 9, GCRY_KEM_MLKEM1024, 1024, 1,
58 GCRY_KEM_MLKEM1024_PUBKEY_LEN, GCRY_KEM_MLKEM1024_SECKEY_LEN },
59 { NULL }
60 };
61
62 /* This is a short version of kem_infos from above. It is required
63 * for the algoithm module interface. Keep in sync. */
64 static const char *kem_names[] =
65 {
66 "sntrup761",
67 "kyber512",
68 "kyber768",
69 "kyber1024",
70 NULL
71 };
72
73
74
75
76 /* Helper for sntrup761. */
77 static void
78 sntrup761_random (void *ctx, size_t length, uint8_t *dst)
79 {
80 (void)ctx;
81
82 _gcry_randomize (dst, length, GCRY_STRONG_RANDOM);
83 }
84
85
86 gcry_err_code_t
87 _gcry_kem_genkey (int algo,
88 void *pubkey, size_t pubkey_len,
89 void *seckey, size_t seckey_len,
90 const void *optional, size_t optional_len)
91 {
92 switch (algo)
93 {
94 case GCRY_KEM_SNTRUP761:
95 if (seckey_len != GCRY_KEM_SNTRUP761_SECKEY_LEN
96 || pubkey_len != GCRY_KEM_SNTRUP761_PUBKEY_LEN)
97 return GPG_ERR_INV_ARG;
98 sntrup761_keypair (pubkey, seckey, NULL, sntrup761_random);
99 _gcry_burn_stack (SNTRUP761_KEYPAIR_STACK_BURN);
100 return 0;
101
102 case GCRY_KEM_CM6688128F:
103 mceliece6688128f_keypair (pubkey, seckey);
104 _gcry_burn_stack (MCELIECE6688128F_KEYPAIR_STACK_BURN);
105 return 0;
106
107 #if USE_KYBER
108 case GCRY_KEM_MLKEM512:
109 if (seckey_len != GCRY_KEM_MLKEM512_SECKEY_LEN
110 || pubkey_len != GCRY_KEM_MLKEM512_PUBKEY_LEN
111 || (optional && optional_len != GCRY_KEM_MLKEM_RANDOM_LEN*2))
112 return GPG_ERR_INV_ARG;
113 kyber_keypair (algo, pubkey, seckey, optional);
114 _gcry_burn_stack (KYBER_KEYPAIR_STACK_BURN (algo));
115 return 0;
116
117 case GCRY_KEM_MLKEM768:
118 if (seckey_len != GCRY_KEM_MLKEM768_SECKEY_LEN
119 || pubkey_len != GCRY_KEM_MLKEM768_PUBKEY_LEN
120 || (optional && optional_len != GCRY_KEM_MLKEM_RANDOM_LEN*2))
121 return GPG_ERR_INV_ARG;
122 kyber_keypair (algo, pubkey, seckey, optional);
123 _gcry_burn_stack (KYBER_KEYPAIR_STACK_BURN (algo));
124 return 0;
125
126 case GCRY_KEM_MLKEM1024:
127 if (seckey_len != GCRY_KEM_MLKEM1024_SECKEY_LEN
128 || pubkey_len != GCRY_KEM_MLKEM1024_PUBKEY_LEN
129 || (optional && optional_len != GCRY_KEM_MLKEM_RANDOM_LEN*2))
130 return GPG_ERR_INV_ARG;
131 kyber_keypair (algo, pubkey, seckey, optional);
132 _gcry_burn_stack (KYBER_KEYPAIR_STACK_BURN (algo));
133 return 0;
134 #endif
135
136 case GCRY_KEM_RAW_X25519:
137 case GCRY_KEM_RAW_X448:
138 case GCRY_KEM_RAW_BP256:
139 case GCRY_KEM_RAW_BP384:
140 case GCRY_KEM_RAW_BP512:
141 case GCRY_KEM_RAW_P256R1:
142 case GCRY_KEM_RAW_P384R1:
143 case GCRY_KEM_RAW_P521R1:
144 case GCRY_KEM_DHKEM25519:
145 case GCRY_KEM_DHKEM448:
146 return _gcry_ecc_raw_keypair (algo, pubkey, pubkey_len,
147 seckey, seckey_len);
148
149 default:
150 return GPG_ERR_UNKNOWN_ALGORITHM;
151 }
152
153 return GPG_ERR_UNKNOWN_ALGORITHM;
154 }
155
156
157 gcry_err_code_t
158 _gcry_kem_encap (int algo,
159 const void *pubkey, size_t pubkey_len,
160 void *ciphertext, size_t ciphertext_len,
161 void *shared, size_t shared_len,
162 const void *optional, size_t optional_len)
163 {
164 switch (algo)
165 {
166 case GCRY_KEM_SNTRUP761:
167 if (optional != NULL || optional_len != 0)
168 return GPG_ERR_INV_VALUE;
169 if (pubkey_len != GCRY_KEM_SNTRUP761_PUBKEY_LEN
170 || ciphertext_len != GCRY_KEM_SNTRUP761_ENCAPS_LEN
171 || shared_len != GCRY_KEM_SNTRUP761_SHARED_LEN)
172 return GPG_ERR_INV_VALUE;
173 sntrup761_enc (ciphertext, shared, pubkey, NULL, sntrup761_random);
174 _gcry_burn_stack (SNTRUP761_ENC_STACK_BURN);
175 return 0;
176
177 case GCRY_KEM_CM6688128F:
178 if (optional != NULL)
179 return GPG_ERR_INV_VALUE;
180 mceliece6688128f_enc (ciphertext, shared, pubkey);
181 _gcry_burn_stack (MCELIECE6688128F_ENC_STACK_BURN);
182 return 0;
183
184 #if USE_KYBER
185 case GCRY_KEM_MLKEM512:
186 case GCRY_KEM_MLKEM768:
187 case GCRY_KEM_MLKEM1024:
188 if (optional && optional_len != GCRY_KEM_MLKEM_RANDOM_LEN)
189 return GPG_ERR_INV_VALUE;
190 kyber_encap (algo, ciphertext, shared, pubkey, optional);
191 _gcry_burn_stack (KYBER_ENCAP_STACK_BURN (algo));
192 return 0;
193 #endif
194
195 case GCRY_KEM_RAW_X25519:
196 case GCRY_KEM_RAW_X448:
197 case GCRY_KEM_RAW_BP256:
198 case GCRY_KEM_RAW_BP384:
199 case GCRY_KEM_RAW_BP512:
200 case GCRY_KEM_RAW_P256R1:
201 case GCRY_KEM_RAW_P384R1:
202 case GCRY_KEM_RAW_P521R1:
203 if (optional != NULL)
204 return GPG_ERR_INV_VALUE;
205 return _gcry_ecc_raw_encap (algo, pubkey, pubkey_len,
206 ciphertext, ciphertext_len,
207 shared, shared_len);
208
209 case GCRY_KEM_DHKEM25519:
210 case GCRY_KEM_DHKEM448:
211 if (optional != NULL)
212 return GPG_ERR_INV_VALUE;
213 return _gcry_ecc_dhkem_encap (algo, pubkey, ciphertext, shared);
214
215 default:
216 return GPG_ERR_UNKNOWN_ALGORITHM;
217 }
218 return GPG_ERR_UNKNOWN_ALGORITHM;
219 }
220
221
222 gcry_err_code_t
223 _gcry_kem_decap (int algo,
224 const void *seckey, size_t seckey_len,
225 const void *ciphertext, size_t ciphertext_len,
226 void *shared, size_t shared_len,
227 const void *optional, size_t optional_len)
228 {
229 switch (algo)
230 {
231 case GCRY_KEM_SNTRUP761:
232 if (optional != NULL || optional_len != 0)
233 return GPG_ERR_INV_VALUE;
234 if (seckey_len != GCRY_KEM_SNTRUP761_SECKEY_LEN
235 || ciphertext_len != GCRY_KEM_SNTRUP761_ENCAPS_LEN
236 || shared_len != GCRY_KEM_SNTRUP761_SHARED_LEN)
237 return GPG_ERR_INV_VALUE;
238 sntrup761_dec (shared, ciphertext, seckey);
239 _gcry_burn_stack (SNTRUP761_DEC_STACK_BURN);
240 return 0;
241
242 case GCRY_KEM_CM6688128F:
243 if (optional != NULL)
244 return GPG_ERR_INV_VALUE;
245 mceliece6688128f_dec (shared, ciphertext, seckey);
246 _gcry_burn_stack (MCELIECE6688128F_DEC_STACK_BURN);
247 return 0;
248
249 #if USE_KYBER
250 case GCRY_KEM_MLKEM512:
251 case GCRY_KEM_MLKEM768:
252 case GCRY_KEM_MLKEM1024:
253 if (optional != NULL)
254 return GPG_ERR_INV_VALUE;
255 kyber_decap (algo, shared, ciphertext, seckey);
256 _gcry_burn_stack (KYBER_DECAP_STACK_BURN (algo));
257 return 0;
258 #endif
259
260 case GCRY_KEM_RAW_X25519:
261 case GCRY_KEM_RAW_X448:
262 case GCRY_KEM_RAW_BP256:
263 case GCRY_KEM_RAW_BP384:
264 case GCRY_KEM_RAW_BP512:
265 case GCRY_KEM_RAW_P256R1:
266 case GCRY_KEM_RAW_P384R1:
267 case GCRY_KEM_RAW_P521R1:
268 if (optional != NULL)
269 return GPG_ERR_INV_VALUE;
270 return _gcry_ecc_raw_decap (algo, seckey, seckey_len,
271 ciphertext, ciphertext_len,
272 shared, shared_len);
273
274 case GCRY_KEM_DHKEM25519:
275 case GCRY_KEM_DHKEM448:
276 return _gcry_ecc_dhkem_decap (algo, seckey, ciphertext, shared,
277 optional);
278
279 default:
280 return GPG_ERR_UNKNOWN_ALGORITHM;
281 }
282 return GPG_ERR_UNKNOWN_ALGORITHM;
283 }
284
285
286
287 /* Generate a KEM keypair using the s-expression interface. The
288 * GENPARAMS is prety simple in this case because it has only the
289 * algorithm name. For example:
290 * (kyber768)
291 */
292 static gcry_err_code_t
293 kem_generate (const gcry_sexp_t genparms, gcry_sexp_t *r_skey)
294 {
295 gpg_err_code_t ec;
296 const char *algo;
297 size_t algolen;
298 const char *name;
299 int i;
300 int algoid;
301 void *pubkey = NULL;
302 void *seckey = NULL;
303 size_t pubkey_len, seckey_len;
304
305 algo = sexp_nth_data (genparms, 0, &algolen);
306 if (!algo || !algolen)
307 return GPG_ERR_PUBKEY_ALGO;
308 for (i=0; (name=kem_infos[i].name); i++)
309 if (kem_infos[i].namelen == algolen && !memcmp (name, algo, algolen))
310 break;
311 if (!name)
312 return GPG_ERR_WRONG_PUBKEY_ALGO;
313 algoid = kem_infos[i].algo;
314 pubkey_len = kem_infos[i].pubkey_len;
315 seckey_len = kem_infos[i].seckey_len;
316 /* (from here on we can jump to leave for cleanup) */
317
318 /* Allocate buffers for the created key. */
319 seckey = xtrycalloc_secure (1, seckey_len);
320 if (!seckey)
321 {
322 ec = gpg_err_code_from_syserror ();
323 goto leave;
324 }
325 pubkey = xtrycalloc (1, pubkey_len);
326 if (!pubkey)
327 {
328 ec = gpg_err_code_from_syserror ();
329 goto leave;
330 }
331
332 /* Generate key. */
333 ec = _gcry_kem_genkey (algoid, pubkey, pubkey_len, seckey, seckey_len,
334 NULL, 0);
335 if (ec)
336 goto leave;
337
338 /* Put the key into an s-expression. */
339 ec = sexp_build (r_skey, NULL,
340 "(key-data"
341 " (public-key"
342 " (%s(p%b)))"
343 " (private-key"
344 " (%s(p%b)(s%b))))",
345 name,
346 (int)pubkey_len, pubkey,
347 name,
348 (int)pubkey_len, pubkey,
349 (int)seckey_len, seckey);
350
351
352 /* FIXME: Add FIPS selftest. */
353
354 leave:
355 if (seckey)
356 {
357 wipememory (seckey, seckey_len);
358 xfree (seckey);
359 }
360 xfree (pubkey);
361 return ec;
362 }
363
364
365 /* Compute a keygrip. MD is the hash context which we are going to
366 * update. KEYPARAM is an S-expression with the key parameters, this
367 * is usually a public key but may also be a secret key. An example
368 * of such an S-expression is:
369 *
370 * (kyber768
371 * (p #4243...#)
372 * (s #1718...#))
373 *
374 * What we hash is the algorithm name, \x00 and the value of p.
375 * Including the algorithm name allows us to see a different key
376 * despite that it uses the same parameters. Whether this is a good
377 * decision is not clear - but it should not harm.
378 */
379 static gpg_err_code_t
380 kem_compute_keygrip (gcry_md_hd_t md, gcry_sexp_t keyparam)
381 {
382 gcry_sexp_t l1;
383 const char *algo, *data;
384 size_t algolen, datalen;
385 const char *name;
386 int i;
387
388 algo = sexp_nth_data (keyparam, 0, &algolen);
389 if (!algo || !algolen)
390 return GPG_ERR_PUBKEY_ALGO;
391 for (i=0; (name=kem_infos[i].name); i++)
392 if (kem_infos[i].namelen == algolen && !memcmp (name, algo, algolen))
393 break;
394 if (!name)
395 return GPG_ERR_WRONG_PUBKEY_ALGO;
396
397 _gcry_md_write (md, name, algolen+1); /* (also hash the nul) */
398
399 l1 = sexp_find_token (keyparam, "p", 1);
400 if (!l1)
401 return GPG_ERR_NO_OBJ;
402
403 data = sexp_nth_data (l1, 1, &datalen);
404 if (!data)
405 {
406 sexp_release (l1);
407 return GPG_ERR_NO_OBJ;
408 }
409
410 _gcry_md_write (md, data, datalen);
411 sexp_release (l1);
412
413 return 0;
414 }
415
416
417 /* Return the number of bits for the key described by PARMS. On error
418 * 0 is returned. */
419 static unsigned int
420 kem_get_nbits (gcry_sexp_t keyparam)
421 {
422 const char *algo;
423 size_t algolen;
424 const char *name;
425 int i;
426
427 algo = sexp_nth_data (keyparam, 0, &algolen);
428 if (!algo || !algolen)
429 return 0; /* GPG_ERR_PUBKEY_ALGO */
430 for (i=0; (name=kem_infos[i].name); i++)
431 if (kem_infos[i].namelen == algolen && !memcmp (name, algo, algolen))
432 break;
433 if (!name)
434 return 0; /* GPG_ERR_WRONG_PUBKEY_ALGO */
435
436 return kem_infos[i].nbits;
437 }
438
439
440 /* Generic structure to represent some KEM algorithms in our public
441 * key system. */
442 gcry_pk_spec_t _gcry_pubkey_spec_kem =
443 {
444 GCRY_PK_KEM, { 0, 0 },
445 GCRY_PK_USAGE_ENCR,
446 "KEM", kem_names,
447 "p", "s", "k", "", "p",
448 kem_generate,
449 NULL, /* kem_check_secret_key */
450 NULL, /* encrypt_raw - Use gcry_kem_encap instead. */
451 NULL, /* decrypt_raw - Use gcry_kem_decap unstead. */
452 NULL, /* sign */
453 NULL, /* verify */
454 kem_get_nbits,
455 NULL, /* selftests */
456 kem_compute_keygrip,
457 NULL, /* get_curve */
458 NULL /* get_curve_param */
459 };