Line | Count | Source |
1 | | /* pkglue.c - public key operations glue code |
2 | | * Copyright (C) 2000, 2003, 2010 Free Software Foundation, Inc. |
3 | | * Copyright (C) 2014 Werner Koch |
4 | | * Copyright (C) 2024 g10 Code GmbH. |
5 | | * |
6 | | * This file is part of GnuPG. |
7 | | * |
8 | | * GnuPG is free software; you can redistribute it and/or modify |
9 | | * it under the terms of the GNU General Public License as published by |
10 | | * the Free Software Foundation; either version 3 of the License, or |
11 | | * (at your option) any later version. |
12 | | * |
13 | | * GnuPG is distributed in the hope that it will be useful, |
14 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | * GNU General Public License for more details. |
17 | | * |
18 | | * You should have received a copy of the GNU General Public License |
19 | | * along with this program; if not, see <https://www.gnu.org/licenses/>. |
20 | | * SPDX-License-Identifier: GPL-3.0-or-later |
21 | | */ |
22 | | |
23 | | #include <config.h> |
24 | | #include <stdio.h> |
25 | | #include <stdlib.h> |
26 | | #include <string.h> |
27 | | #include <errno.h> |
28 | | |
29 | | #include "gpg.h" |
30 | | #include "../common/util.h" |
31 | | #include "../common/kemhelp.h" |
32 | | #include "pkglue.h" |
33 | | #include "main.h" |
34 | | #include "options.h" |
35 | | |
36 | | |
37 | | /* FIXME: Better change the function name because mpi_ is used by |
38 | | gcrypt macros. */ |
39 | | gcry_mpi_t |
40 | | get_mpi_from_sexp (gcry_sexp_t sexp, const char *item, int mpifmt) |
41 | 0 | { |
42 | 0 | gcry_sexp_t list; |
43 | 0 | gcry_mpi_t data; |
44 | |
|
45 | 0 | list = gcry_sexp_find_token (sexp, item, 0); |
46 | 0 | log_assert (list); |
47 | 0 | data = gcry_sexp_nth_mpi (list, 1, mpifmt); |
48 | 0 | log_assert (data); |
49 | 0 | gcry_sexp_release (list); |
50 | 0 | return data; |
51 | 0 | } |
52 | | |
53 | | |
54 | | /* This is the same as get_mpi_from_sexp but removes a 0x40 prefix |
55 | | * from the requested parameter. An opaque MPI is returned on |
56 | | * success, NULL on error. */ |
57 | | gcry_mpi_t |
58 | | get_mpi_from_sexp_strip_0x40 (gcry_sexp_t sexp, const char *item) |
59 | 0 | { |
60 | 0 | gcry_sexp_t list; |
61 | 0 | size_t len; |
62 | 0 | const char *p; |
63 | 0 | char *buffer; |
64 | 0 | gcry_mpi_t result = NULL; |
65 | |
|
66 | 0 | list = gcry_sexp_find_token (sexp, item, 0); |
67 | 0 | if (!list) |
68 | 0 | return NULL; |
69 | | |
70 | 0 | p = gcry_sexp_nth_data (list, 1, &len); |
71 | 0 | if (!p || !len) |
72 | 0 | goto leave; |
73 | | |
74 | | /* If we have a parameter of at least 256 bits with an odd length in |
75 | | * octets and the first octet is 0x40 we remove that octet. The |
76 | | * 0x40 indicates native point format and is for example used when |
77 | | * we create Curve25519 key. */ |
78 | 0 | if ((len & 1) && len > 32 && *p == 0x40) |
79 | 0 | { |
80 | 0 | p++; |
81 | 0 | len--; |
82 | 0 | } |
83 | 0 | buffer = xtrymalloc (len); |
84 | 0 | if (!buffer) |
85 | 0 | goto leave; |
86 | 0 | memcpy (buffer, p, len); |
87 | 0 | result = gcry_mpi_set_opaque (NULL, buffer, len*8); |
88 | 0 | buffer = NULL; |
89 | |
|
90 | 0 | leave: |
91 | 0 | gcry_sexp_release (list); |
92 | 0 | return result; |
93 | 0 | } |
94 | | |
95 | | |
96 | | /* Return an opaque MPI with the concatenated values of the "r" and |
97 | | * "s" parameters from SEXP. Return NULL on error*/ |
98 | | gcry_mpi_t |
99 | | get_r_s_mpi_from_sexp (gcry_sexp_t sexp) |
100 | 0 | { |
101 | 0 | gcry_sexp_t rlist, slist; |
102 | 0 | size_t rlen, slen; |
103 | 0 | const char *r, *s; |
104 | 0 | char *buffer; |
105 | 0 | gcry_mpi_t result = NULL; |
106 | |
|
107 | 0 | rlist = gcry_sexp_find_token (sexp, "r", 0); |
108 | 0 | if (!rlist) |
109 | 0 | return NULL; |
110 | 0 | slist = gcry_sexp_find_token (sexp, "s", 0); |
111 | 0 | if (!slist) |
112 | 0 | goto leave; |
113 | | |
114 | 0 | r = gcry_sexp_nth_data (rlist, 1, &rlen); |
115 | 0 | s = gcry_sexp_nth_data (slist, 1, &slen); |
116 | 0 | if (!r || !s || !rlen || !slen || rlen != slen) |
117 | 0 | goto leave; |
118 | | |
119 | 0 | buffer = xtrymalloc (rlen+slen); |
120 | 0 | if (!buffer) |
121 | 0 | goto leave; |
122 | 0 | memcpy (buffer, r, rlen); |
123 | 0 | memcpy (buffer+rlen, s, slen); |
124 | 0 | result = gcry_mpi_set_opaque (NULL, buffer, (rlen+slen)*8); |
125 | 0 | buffer = NULL; |
126 | |
|
127 | 0 | leave: |
128 | 0 | gcry_sexp_release (rlist); |
129 | 0 | gcry_sexp_release (slist); |
130 | 0 | return result; |
131 | 0 | } |
132 | | |
133 | | |
134 | | /* |
135 | | * SOS (Simply, Octet String) is an attempt to handle opaque octet |
136 | | * string in OpenPGP, where well-formed MPI cannot represent octet |
137 | | * string with leading zero octets. |
138 | | * |
139 | | * To retain maximum compatibility to existing MPI handling, SOS |
140 | | * has same structure, but allows leading zero octets. When there |
141 | | * is no leading zero octets, SOS representation is as same as MPI one. |
142 | | * With leading zero octets, NBITS is 8*(length of octets), regardless |
143 | | * of leading zero bits. |
144 | | */ |
145 | | /* Extract SOS representation from SEXP for PARAM, return the result |
146 | | * in R_SOS. It is represented by opaque MPI with GCRYMPI_FLAG_USER2 |
147 | | * flag. */ |
148 | | gpg_error_t |
149 | | sexp_extract_param_sos (gcry_sexp_t sexp, const char *param, gcry_mpi_t *r_sos) |
150 | 0 | { |
151 | 0 | gpg_error_t err; |
152 | 0 | gcry_sexp_t l2 = gcry_sexp_find_token (sexp, param, 0); |
153 | |
|
154 | 0 | *r_sos = NULL; |
155 | 0 | if (!l2) |
156 | 0 | err = gpg_error (GPG_ERR_NO_OBJ); |
157 | 0 | else |
158 | 0 | { |
159 | 0 | size_t buflen; |
160 | 0 | void *p0 = gcry_sexp_nth_buffer (l2, 1, &buflen); |
161 | |
|
162 | 0 | if (!p0) |
163 | 0 | err = gpg_error_from_syserror (); |
164 | 0 | else |
165 | 0 | { |
166 | 0 | gcry_mpi_t sos; |
167 | 0 | unsigned int nbits = buflen*8; |
168 | 0 | unsigned char *p = p0; |
169 | |
|
170 | 0 | if (*p && nbits >= 8 && !(*p & 0x80)) |
171 | 0 | if (--nbits >= 7 && !(*p & 0x40)) |
172 | 0 | if (--nbits >= 6 && !(*p & 0x20)) |
173 | 0 | if (--nbits >= 5 && !(*p & 0x10)) |
174 | 0 | if (--nbits >= 4 && !(*p & 0x08)) |
175 | 0 | if (--nbits >= 3 && !(*p & 0x04)) |
176 | 0 | if (--nbits >= 2 && !(*p & 0x02)) |
177 | 0 | if (--nbits >= 1 && !(*p & 0x01)) |
178 | 0 | --nbits; |
179 | |
|
180 | 0 | sos = gcry_mpi_set_opaque (NULL, p0, nbits); |
181 | 0 | if (sos) |
182 | 0 | { |
183 | 0 | gcry_mpi_set_flag (sos, GCRYMPI_FLAG_USER2); |
184 | 0 | *r_sos = sos; |
185 | 0 | err = 0; |
186 | 0 | } |
187 | 0 | else |
188 | 0 | err = gpg_error_from_syserror (); |
189 | 0 | } |
190 | 0 | gcry_sexp_release (l2); |
191 | 0 | } |
192 | |
|
193 | 0 | return err; |
194 | 0 | } |
195 | | |
196 | | |
197 | | /* "No leading zero octets" (nlz) version of the function above. |
198 | | * |
199 | | * This routine is used for backward compatibility to existing |
200 | | * implementation with the weird handling of little endian integer |
201 | | * representation with leading zero octets. For the sake of |
202 | | * "well-fomed" MPI, which is designed for big endian integer, leading |
203 | | * zero octets are removed when output, and they are recovered at |
204 | | * input. |
205 | | * |
206 | | * Extract SOS representation from SEXP for PARAM, removing leading |
207 | | * zeros, return the result in R_SOS. */ |
208 | | gpg_error_t |
209 | | sexp_extract_param_sos_nlz (gcry_sexp_t sexp, const char *param, |
210 | | gcry_mpi_t *r_sos) |
211 | 0 | { |
212 | 0 | gpg_error_t err; |
213 | 0 | gcry_sexp_t l2 = gcry_sexp_find_token (sexp, param, 0); |
214 | |
|
215 | 0 | *r_sos = NULL; |
216 | 0 | if (!l2) |
217 | 0 | err = gpg_error (GPG_ERR_NO_OBJ); |
218 | 0 | else |
219 | 0 | { |
220 | 0 | size_t buflen; |
221 | 0 | const void *p0 = gcry_sexp_nth_data (l2, 1, &buflen); |
222 | |
|
223 | 0 | if (!p0) |
224 | 0 | err = gpg_error_from_syserror (); |
225 | 0 | else |
226 | 0 | { |
227 | 0 | gcry_mpi_t sos; |
228 | 0 | unsigned int nbits = buflen*8; |
229 | 0 | const unsigned char *p = p0; |
230 | | |
231 | | /* Strip leading zero bits. */ |
232 | 0 | for (; nbits >= 8 && !*p; p++, nbits -= 8) |
233 | 0 | ; |
234 | |
|
235 | 0 | if (nbits >= 8 && !(*p & 0x80)) |
236 | 0 | if (--nbits >= 7 && !(*p & 0x40)) |
237 | 0 | if (--nbits >= 6 && !(*p & 0x20)) |
238 | 0 | if (--nbits >= 5 && !(*p & 0x10)) |
239 | 0 | if (--nbits >= 4 && !(*p & 0x08)) |
240 | 0 | if (--nbits >= 3 && !(*p & 0x04)) |
241 | 0 | if (--nbits >= 2 && !(*p & 0x02)) |
242 | 0 | if (--nbits >= 1 && !(*p & 0x01)) |
243 | 0 | --nbits; |
244 | |
|
245 | 0 | sos = gcry_mpi_set_opaque_copy (NULL, p, nbits); |
246 | 0 | if (sos) |
247 | 0 | { |
248 | 0 | gcry_mpi_set_flag (sos, GCRYMPI_FLAG_USER2); |
249 | 0 | *r_sos = sos; |
250 | 0 | err = 0; |
251 | 0 | } |
252 | 0 | else |
253 | 0 | err = gpg_error_from_syserror (); |
254 | 0 | } |
255 | 0 | gcry_sexp_release (l2); |
256 | 0 | } |
257 | |
|
258 | 0 | return err; |
259 | 0 | } |
260 | | |
261 | | |
262 | | /**************** |
263 | | * Emulate our old PK interface here - sometime in the future we might |
264 | | * change the internal design to directly fit to libgcrypt. |
265 | | */ |
266 | | int |
267 | | pk_verify (pubkey_algo_t pkalgo, gcry_mpi_t hash, |
268 | | gcry_mpi_t *data, gcry_mpi_t *pkey) |
269 | 0 | { |
270 | 0 | gcry_sexp_t s_sig, s_hash, s_pkey; |
271 | 0 | int rc; |
272 | | |
273 | | /* Make a sexp from pkey. */ |
274 | 0 | if (pkalgo == PUBKEY_ALGO_DSA) |
275 | 0 | { |
276 | 0 | rc = gcry_sexp_build (&s_pkey, NULL, |
277 | 0 | "(public-key(dsa(p%m)(q%m)(g%m)(y%m)))", |
278 | 0 | pkey[0], pkey[1], pkey[2], pkey[3]); |
279 | 0 | } |
280 | 0 | else if (pkalgo == PUBKEY_ALGO_ELGAMAL_E || pkalgo == PUBKEY_ALGO_ELGAMAL) |
281 | 0 | { |
282 | 0 | rc = gcry_sexp_build (&s_pkey, NULL, |
283 | 0 | "(public-key(elg(p%m)(g%m)(y%m)))", |
284 | 0 | pkey[0], pkey[1], pkey[2]); |
285 | 0 | } |
286 | 0 | else if (pkalgo == PUBKEY_ALGO_RSA || pkalgo == PUBKEY_ALGO_RSA_S) |
287 | 0 | { |
288 | 0 | rc = gcry_sexp_build (&s_pkey, NULL, |
289 | 0 | "(public-key(rsa(n%m)(e%m)))", pkey[0], pkey[1]); |
290 | 0 | } |
291 | 0 | else if (pkalgo == PUBKEY_ALGO_ECDSA) |
292 | 0 | { |
293 | 0 | char *curve = openpgp_oid_to_str (pkey[0]); |
294 | 0 | if (!curve) |
295 | 0 | rc = gpg_error_from_syserror (); |
296 | 0 | else |
297 | 0 | { |
298 | 0 | rc = gcry_sexp_build (&s_pkey, NULL, |
299 | 0 | "(public-key(ecdsa(curve %s)(q%m)))", |
300 | 0 | curve, pkey[1]); |
301 | 0 | xfree (curve); |
302 | 0 | } |
303 | 0 | } |
304 | 0 | else if (pkalgo == PUBKEY_ALGO_EDDSA) |
305 | 0 | { |
306 | 0 | char *curve = openpgp_oid_to_str (pkey[0]); |
307 | 0 | if (!curve) |
308 | 0 | rc = gpg_error_from_syserror (); |
309 | 0 | else |
310 | 0 | { |
311 | 0 | const char *fmt; |
312 | |
|
313 | 0 | if (openpgp_oid_is_ed25519 (pkey[0])) |
314 | 0 | fmt = "(public-key(ecc(curve %s)(flags eddsa)(q%m)))"; |
315 | 0 | else |
316 | 0 | fmt = "(public-key(ecc(curve %s)(q%m)))"; |
317 | |
|
318 | 0 | rc = gcry_sexp_build (&s_pkey, NULL, fmt, curve, pkey[1]); |
319 | 0 | xfree (curve); |
320 | 0 | } |
321 | 0 | } |
322 | 0 | else if (pkalgo == PUBKEY_ALGO_ED25519) |
323 | 0 | { |
324 | 0 | rc = gcry_sexp_build (&s_pkey, NULL, |
325 | 0 | "(public-key(ecc(curve Ed25519)" |
326 | 0 | "(flags eddsa)(q%m)))", |
327 | 0 | pkey[0]); |
328 | 0 | } |
329 | 0 | else |
330 | 0 | return GPG_ERR_PUBKEY_ALGO; |
331 | | |
332 | 0 | if (rc) |
333 | 0 | BUG (); /* gcry_sexp_build should never fail. */ |
334 | | |
335 | | /* Put hash into a S-Exp s_hash. */ |
336 | 0 | if (pkalgo == PUBKEY_ALGO_EDDSA) |
337 | 0 | { |
338 | 0 | const char *fmt; |
339 | |
|
340 | 0 | if (openpgp_oid_is_ed25519 (pkey[0])) |
341 | 0 | fmt = "(data(flags eddsa)(hash-algo sha512)(value %m))"; |
342 | 0 | else |
343 | 0 | fmt = "(data(value %m))"; |
344 | |
|
345 | 0 | if (gcry_sexp_build (&s_hash, NULL, fmt, hash)) |
346 | 0 | BUG (); /* gcry_sexp_build should never fail. */ |
347 | 0 | } |
348 | 0 | else if (pkalgo == PUBKEY_ALGO_ED25519) |
349 | 0 | { |
350 | 0 | if (gcry_sexp_build (&s_hash, NULL, |
351 | 0 | "(data(flags eddsa)(hash-algo sha512)(value %m))", |
352 | 0 | hash)) |
353 | 0 | BUG (); /* gcry_sexp_build should never fail. */ |
354 | 0 | } |
355 | 0 | else |
356 | 0 | { |
357 | 0 | if (gcry_sexp_build (&s_hash, NULL, "%m", hash)) |
358 | 0 | BUG (); /* gcry_sexp_build should never fail. */ |
359 | 0 | } |
360 | | |
361 | | /* Put data into a S-Exp s_sig. */ |
362 | 0 | s_sig = NULL; |
363 | 0 | if (pkalgo == PUBKEY_ALGO_DSA) |
364 | 0 | { |
365 | 0 | if (!data[0] || !data[1]) |
366 | 0 | rc = gpg_error (GPG_ERR_BAD_MPI); |
367 | 0 | else |
368 | 0 | rc = gcry_sexp_build (&s_sig, NULL, |
369 | 0 | "(sig-val(dsa(r%m)(s%m)))", data[0], data[1]); |
370 | 0 | } |
371 | 0 | else if (pkalgo == PUBKEY_ALGO_ECDSA) |
372 | 0 | { |
373 | 0 | if (!data[0] || !data[1]) |
374 | 0 | rc = gpg_error (GPG_ERR_BAD_MPI); |
375 | 0 | else |
376 | 0 | rc = gcry_sexp_build (&s_sig, NULL, |
377 | 0 | "(sig-val(ecdsa(r%m)(s%m)))", data[0], data[1]); |
378 | 0 | } |
379 | 0 | else if (pkalgo == PUBKEY_ALGO_EDDSA) |
380 | 0 | { |
381 | 0 | gcry_mpi_t r = data[0]; |
382 | 0 | gcry_mpi_t s = data[1]; |
383 | |
|
384 | 0 | if (openpgp_oid_is_ed25519 (pkey[0])) |
385 | 0 | { |
386 | 0 | size_t rlen, slen, n; /* (bytes) */ |
387 | 0 | char buf[64]; |
388 | 0 | unsigned int nbits; |
389 | 0 | unsigned int neededfixedlen = 256 / 8; |
390 | |
|
391 | 0 | log_assert (neededfixedlen <= sizeof buf); |
392 | | |
393 | 0 | if (!r || !s) |
394 | 0 | rc = gpg_error (GPG_ERR_BAD_MPI); |
395 | 0 | else if ((rlen = (gcry_mpi_get_nbits (r)+7)/8) > neededfixedlen || !rlen) |
396 | 0 | rc = gpg_error (GPG_ERR_BAD_MPI); |
397 | 0 | else if ((slen = (gcry_mpi_get_nbits (s)+7)/8) > neededfixedlen || !slen) |
398 | 0 | rc = gpg_error (GPG_ERR_BAD_MPI); |
399 | 0 | else |
400 | 0 | { |
401 | 0 | r = gcry_mpi_copy (r); |
402 | 0 | s = gcry_mpi_copy (s); |
403 | |
|
404 | 0 | if (!r || !s) |
405 | 0 | { |
406 | 0 | rc = gpg_error_from_syserror (); |
407 | 0 | goto leave; |
408 | 0 | } |
409 | | |
410 | | /* We need to fixup the length in case of leading zeroes. |
411 | | * OpenPGP does not allow leading zeroes and the parser for |
412 | | * the signature packet has no information on the used curve, |
413 | | * thus we need to do it here. We won't do it for opaque |
414 | | * MPIs under the assumption that they are known to be fine; |
415 | | * we won't see them here anyway but the check is anyway |
416 | | * required. Fixme: A nifty feature for gcry_sexp_build |
417 | | * would be a format to left pad the value (e.g. "%*M"). */ |
418 | 0 | rc = 0; |
419 | |
|
420 | 0 | if (rlen < neededfixedlen |
421 | 0 | && !gcry_mpi_get_flag (r, GCRYMPI_FLAG_OPAQUE) |
422 | 0 | && !(rc=gcry_mpi_print (GCRYMPI_FMT_USG, |
423 | 0 | buf, sizeof buf, &n, r))) |
424 | 0 | { |
425 | 0 | log_assert (n < neededfixedlen); |
426 | 0 | memmove (buf + (neededfixedlen - n), buf, n); |
427 | 0 | memset (buf, 0, neededfixedlen - n); |
428 | 0 | gcry_mpi_set_opaque_copy (r, buf, neededfixedlen * 8); |
429 | 0 | } |
430 | 0 | else if (rlen < neededfixedlen |
431 | 0 | && gcry_mpi_get_flag (r, GCRYMPI_FLAG_OPAQUE)) |
432 | 0 | { |
433 | 0 | const unsigned char *p; |
434 | |
|
435 | 0 | p = gcry_mpi_get_opaque (r, &nbits); |
436 | 0 | n = (nbits+7)/8; |
437 | 0 | memcpy (buf + (neededfixedlen - n), p, n); |
438 | 0 | memset (buf, 0, neededfixedlen - n); |
439 | 0 | gcry_mpi_set_opaque_copy (r, buf, neededfixedlen * 8); |
440 | 0 | } |
441 | | |
442 | 0 | if (rc) |
443 | 0 | ; |
444 | 0 | else if (slen < neededfixedlen |
445 | 0 | && !gcry_mpi_get_flag (s, GCRYMPI_FLAG_OPAQUE) |
446 | 0 | && !(rc=gcry_mpi_print (GCRYMPI_FMT_USG, |
447 | 0 | buf, sizeof buf, &n, s))) |
448 | 0 | { |
449 | 0 | log_assert (n < neededfixedlen); |
450 | 0 | memmove (buf + (neededfixedlen - n), buf, n); |
451 | 0 | memset (buf, 0, neededfixedlen - n); |
452 | 0 | gcry_mpi_set_opaque_copy (s, buf, neededfixedlen * 8); |
453 | 0 | } |
454 | 0 | else if (slen < neededfixedlen |
455 | 0 | && gcry_mpi_get_flag (s, GCRYMPI_FLAG_OPAQUE)) |
456 | 0 | { |
457 | 0 | const unsigned char *p; |
458 | |
|
459 | 0 | p = gcry_mpi_get_opaque (s, &nbits); |
460 | 0 | n = (nbits+7)/8; |
461 | 0 | memcpy (buf + (neededfixedlen - n), p, n); |
462 | 0 | memset (buf, 0, neededfixedlen - n); |
463 | 0 | gcry_mpi_set_opaque_copy (s, buf, neededfixedlen * 8); |
464 | 0 | } |
465 | 0 | } |
466 | 0 | } |
467 | 0 | else |
468 | 0 | rc = 0; |
469 | | |
470 | 0 | if (!rc) |
471 | 0 | rc = gcry_sexp_build (&s_sig, NULL, |
472 | 0 | "(sig-val(eddsa(r%M)(s%M)))", r, s); |
473 | |
|
474 | 0 | if (r != data[0]) |
475 | 0 | gcry_mpi_release (r); |
476 | 0 | if (s != data[1]) |
477 | 0 | gcry_mpi_release (s); |
478 | 0 | } |
479 | 0 | else if (pkalgo == PUBKEY_ALGO_ED25519) |
480 | 0 | { |
481 | 0 | const unsigned char *p; |
482 | 0 | unsigned int nbits; |
483 | |
|
484 | 0 | if (!gcry_mpi_get_flag (data[0], GCRYMPI_FLAG_OPAQUE)) |
485 | 0 | rc = gpg_error (GPG_ERR_BAD_MPI); |
486 | 0 | else |
487 | 0 | { |
488 | 0 | p = gcry_mpi_get_opaque (data[0], &nbits); |
489 | 0 | if ((nbits+7)/8 != 64) |
490 | 0 | rc = gpg_error (GPG_ERR_BAD_MPI); |
491 | 0 | else |
492 | 0 | rc = gcry_sexp_build (&s_sig, NULL, "(sig-val(eddsa(r%b)(s%b)))", |
493 | 0 | 32, p, 32, p+32); |
494 | 0 | } |
495 | 0 | } |
496 | 0 | else if (pkalgo == PUBKEY_ALGO_ELGAMAL || pkalgo == PUBKEY_ALGO_ELGAMAL_E) |
497 | 0 | { |
498 | 0 | if (!data[0] || !data[1]) |
499 | 0 | rc = gpg_error (GPG_ERR_BAD_MPI); |
500 | 0 | else |
501 | 0 | rc = gcry_sexp_build (&s_sig, NULL, |
502 | 0 | "(sig-val(elg(r%m)(s%m)))", data[0], data[1]); |
503 | 0 | } |
504 | 0 | else if (pkalgo == PUBKEY_ALGO_RSA || pkalgo == PUBKEY_ALGO_RSA_S) |
505 | 0 | { |
506 | 0 | if (!data[0]) |
507 | 0 | rc = gpg_error (GPG_ERR_BAD_MPI); |
508 | 0 | else |
509 | 0 | rc = gcry_sexp_build (&s_sig, NULL, "(sig-val(rsa(s%m)))", data[0]); |
510 | 0 | } |
511 | 0 | else |
512 | 0 | BUG (); |
513 | | |
514 | 0 | if (!rc) |
515 | 0 | rc = gcry_pk_verify (s_sig, s_hash, s_pkey); |
516 | |
|
517 | 0 | leave: |
518 | 0 | gcry_sexp_release (s_sig); |
519 | 0 | gcry_sexp_release (s_hash); |
520 | 0 | gcry_sexp_release (s_pkey); |
521 | 0 | return rc; |
522 | 0 | } |
523 | | |
524 | | |
525 | | #if GCRY_KEM_MLKEM1024_ENCAPS_LEN < GCRY_KEM_MLKEM768_ENCAPS_LEN \ |
526 | | || GCRY_KEM_MLKEM1024_SHARED_LEN < GCRY_KEM_MLKEM768_SHARED_LEN |
527 | | # error Bad Kyber constants in Libgcrypt |
528 | | #endif |
529 | | |
530 | | /* Core of the encryption for KEM algorithms. See pk_decrypt for a |
531 | | * description of the arguments. */ |
532 | | static gpg_error_t |
533 | | do_encrypt_kem (PKT_public_key *pk, gcry_mpi_t data, int seskey_algo, |
534 | | gcry_mpi_t *resarr) |
535 | 0 | { |
536 | 0 | gpg_error_t err; |
537 | 0 | int i; |
538 | 0 | unsigned int nbits, n; |
539 | 0 | gcry_sexp_t s_data = NULL; |
540 | 0 | gcry_cipher_hd_t hd = NULL; |
541 | 0 | char *ecc_oid = NULL; |
542 | 0 | const char *curve; |
543 | 0 | const struct gnupg_ecc_params *ecc; |
544 | 0 | enum gcry_kem_algos kyber_algo; |
545 | 0 | int is_rfc9980, only_ecc; |
546 | |
|
547 | 0 | const unsigned char *ecc_pubkey; |
548 | 0 | size_t ecc_pubkey_len; |
549 | 0 | const unsigned char *kyber_pubkey; |
550 | 0 | size_t kyber_pubkey_len; |
551 | 0 | const unsigned char *seskey; |
552 | 0 | size_t seskey_len; |
553 | 0 | unsigned char *enc_seskey = NULL; |
554 | 0 | size_t enc_seskey_len; |
555 | 0 | int ecc_hash_algo; |
556 | |
|
557 | 0 | unsigned char ecc_ct[ECC_POINT_LEN_MAX]; |
558 | 0 | unsigned char ecc_ecdh[ECC_POINT_LEN_MAX]; |
559 | 0 | unsigned char ecc_ss[ECC_HASH_LEN_MAX]; |
560 | 0 | size_t ecc_ct_len, ecc_ecdh_len, ecc_ss_len; |
561 | |
|
562 | 0 | unsigned char kyber_ct[GCRY_KEM_MLKEM1024_ENCAPS_LEN]; |
563 | 0 | unsigned char kyber_ss[GCRY_KEM_MLKEM1024_SHARED_LEN]; |
564 | 0 | size_t kyber_ct_len, kyber_ss_len; |
565 | |
|
566 | 0 | unsigned char kek[32]; /* AES-256 is mandatory. */ |
567 | 0 | size_t kek_len = 32; |
568 | | |
569 | | /* For later error checking we make sure the array is cleared. */ |
570 | 0 | resarr[0] = resarr[1] = resarr[2] = NULL; |
571 | |
|
572 | 0 | only_ecc = 0; |
573 | 0 | if (pk->pubkey_algo == PUBKEY_ALGO_X25519) |
574 | 0 | is_rfc9980 = only_ecc = 1; |
575 | 0 | else if (IS_PUBKEY_ALGO_MLK (pk->pubkey_algo)) |
576 | 0 | is_rfc9980 = 1; |
577 | 0 | else |
578 | 0 | is_rfc9980 = 0; |
579 | | |
580 | | /* As of now we use KEM only for the combined Kyber and thus a |
581 | | * second public key is expected. Right now we take the keys |
582 | | * directly from the PK->data elements. */ |
583 | |
|
584 | 0 | if (is_rfc9980) |
585 | 0 | { |
586 | | /* Note: We need t use the canonical names here. */ |
587 | 0 | switch (pk->pubkey_algo) |
588 | 0 | { |
589 | 0 | case PUBKEY_ALGO_X25519: |
590 | 0 | case PUBKEY_ALGO_MLK768_25519: curve = "ietf25"; break; |
591 | 0 | case PUBKEY_ALGO_MLK768_NP384: curve = "NIST P-384"; break; |
592 | 0 | case PUBKEY_ALGO_MLK768_BP384: curve = "brainpoolP384r1"; break; |
593 | 0 | case PUBKEY_ALGO_MLK1024_448: curve = "X448"; break; |
594 | 0 | case PUBKEY_ALGO_MLK1024_NP521: curve = "NIST P-521"; break; |
595 | 0 | case PUBKEY_ALGO_MLK1024_BP512: curve = "brainpoolP512r1"; break; |
596 | 0 | default: curve = "e_no_such_curve"; break; |
597 | 0 | } |
598 | 0 | } |
599 | 0 | else |
600 | 0 | { |
601 | 0 | ecc_oid = openpgp_oid_to_str (pk->pkey[0]); |
602 | 0 | if (!ecc_oid) |
603 | 0 | { |
604 | 0 | err = gpg_error_from_syserror (); |
605 | 0 | log_error ("%s: error getting OID for ECC key\n", __func__); |
606 | 0 | goto leave; |
607 | 0 | } |
608 | 0 | curve = openpgp_oid_to_curve (ecc_oid, 1); |
609 | 0 | if (!curve) |
610 | 0 | { |
611 | 0 | err = gpg_error (GPG_ERR_INV_DATA); |
612 | 0 | log_error ("%s: error getting curve for ECC key\n", __func__); |
613 | 0 | goto leave; |
614 | 0 | } |
615 | 0 | } |
616 | 0 | ecc = gnupg_get_ecc_params (curve); |
617 | 0 | if (!ecc) |
618 | 0 | { |
619 | 0 | if (opt.verbose) |
620 | 0 | log_info ("%s: ECC curve %s not supported\n", __func__, curve); |
621 | 0 | err = gpg_error (GPG_ERR_INV_DATA); |
622 | 0 | goto leave; |
623 | 0 | } |
624 | 0 | ecc_ct_len = ecc_ecdh_len = ecc->point_len; |
625 | 0 | ecc_hash_algo = ecc->hash_algo; |
626 | 0 | ecc_ss_len = gcry_md_get_algo_dlen (ecc_hash_algo); |
627 | |
|
628 | 0 | ecc_pubkey = gcry_mpi_get_opaque (pk->pkey[is_rfc9980?0:1], &nbits); |
629 | 0 | ecc_pubkey_len = (nbits+7)/8; |
630 | 0 | if (ecc_pubkey_len != ecc->pubkey_len) |
631 | 0 | { |
632 | 0 | if (ecc->kem_algo == GCRY_KEM_RAW_X25519 |
633 | 0 | && ecc_pubkey_len == ecc->pubkey_len - 1) |
634 | 0 | { |
635 | | /* For Curve25519, we also accept no prefix in the point |
636 | | * representation. */ |
637 | 0 | } |
638 | 0 | else |
639 | 0 | { |
640 | 0 | if (opt.verbose) |
641 | 0 | log_info ("%s: ECC public key length invalid (%zu)\n", |
642 | 0 | __func__, ecc_pubkey_len); |
643 | 0 | err = gpg_error (GPG_ERR_INV_DATA); |
644 | 0 | goto leave; |
645 | 0 | } |
646 | 0 | } |
647 | | |
648 | 0 | if (ecc->kem_algo == GCRY_KEM_RAW_X25519) |
649 | 0 | { |
650 | 0 | if (ecc_oid && !strcmp (ecc_oid, "1.3.6.1.4.1.3029.1.5.1")) |
651 | 0 | log_info ("Warning: " |
652 | 0 | "legacy OID for cv25519 accepted during development\n"); |
653 | | /* Optional prefix handling */ |
654 | 0 | if (ecc_pubkey_len == 33 && *ecc_pubkey == 0x40) |
655 | 0 | { |
656 | 0 | ecc_pubkey++; /* Remove the 0x40 prefix. */ |
657 | 0 | ecc_pubkey_len--; |
658 | 0 | } |
659 | 0 | } |
660 | |
|
661 | 0 | if (DBG_CRYPTO) |
662 | 0 | { |
663 | 0 | log_debug ("KEM algo: %d\n", ecc->kem_algo); |
664 | 0 | log_debug ("ECC curve: %s\n", ecc_oid? ecc_oid:curve); |
665 | 0 | log_printhex (ecc_pubkey, ecc_pubkey_len, "ECC pubkey:"); |
666 | 0 | } |
667 | |
|
668 | 0 | err = gcry_kem_encap (ecc->kem_algo, |
669 | 0 | ecc_pubkey, ecc_pubkey_len, |
670 | 0 | ecc_ct, ecc_ct_len, |
671 | 0 | ecc_ecdh, ecc_ecdh_len, |
672 | 0 | NULL, 0); |
673 | 0 | if (err) |
674 | 0 | { |
675 | 0 | if (opt.verbose) |
676 | 0 | log_info ("%s: gcry_kem_encap for ECC (%s) failed\n", |
677 | 0 | __func__, ecc_oid? ecc_oid:curve); |
678 | 0 | goto leave; |
679 | 0 | } |
680 | 0 | if (DBG_CRYPTO) |
681 | 0 | { |
682 | 0 | log_printhex (ecc_ct, ecc_ct_len, "ECC ephem:"); |
683 | 0 | log_printhex (ecc_ecdh, ecc_ecdh_len, "ECC ecdh:"); |
684 | 0 | } |
685 | 0 | if (!is_rfc9980) |
686 | 0 | { |
687 | 0 | err = gnupg_ecc_kem_simple_kdf (ecc_ss, ecc_ss_len, |
688 | 0 | ecc_hash_algo, |
689 | 0 | ecc_ecdh, ecc_ecdh_len, |
690 | 0 | ecc_ct, ecc_ct_len, |
691 | 0 | ecc_pubkey, ecc_pubkey_len); |
692 | 0 | if (err) |
693 | 0 | { |
694 | 0 | if (opt.verbose) |
695 | 0 | log_info ("%s: kdf for ECC failed\n", __func__); |
696 | 0 | goto leave; |
697 | 0 | } |
698 | 0 | if (DBG_CRYPTO) |
699 | 0 | log_printhex (ecc_ss, ecc_ss_len, "ECC shared:"); |
700 | 0 | } |
701 | | |
702 | 0 | if (only_ecc) |
703 | 0 | { |
704 | 0 | kek_len = gcry_cipher_get_algo_keylen (seskey_algo); |
705 | 0 | if (ecc_ct_len != 32 || ecc_pubkey_len != 32 || ecc_ecdh_len != 32 |
706 | 0 | || kek_len > sizeof kek) |
707 | 0 | err = gpg_error (GPG_ERR_INV_LENGTH); |
708 | 0 | else |
709 | 0 | { |
710 | 0 | gcry_kdf_hd_t kdfhd; |
711 | 0 | const char *kdfinfo; |
712 | 0 | unsigned long kdfparam[1]; |
713 | 0 | unsigned char inputbuf[32+32+32]; |
714 | |
|
715 | 0 | kdfparam[0] = kek_len; |
716 | 0 | kdfinfo = "OpenPGP X25519"; |
717 | 0 | memcpy (inputbuf, ecc_ct, 32); |
718 | 0 | memcpy (inputbuf+32, ecc_pubkey, 32); |
719 | 0 | memcpy (inputbuf+64, ecc_ecdh, 32); |
720 | 0 | err = gcry_kdf_open (&kdfhd, GCRY_KDF_HKDF, GCRY_MAC_HMAC_SHA256, |
721 | 0 | kdfparam, 1, |
722 | 0 | inputbuf, sizeof inputbuf, |
723 | 0 | NULL, 0, NULL, 0, |
724 | 0 | kdfinfo, strlen (kdfinfo)); |
725 | 0 | if (!err) |
726 | 0 | { |
727 | 0 | err = gcry_kdf_compute (kdfhd, NULL); |
728 | 0 | if (!err) |
729 | 0 | err = gcry_kdf_final (kdfhd, kek_len, kek); |
730 | 0 | gcry_kdf_close (kdfhd); |
731 | 0 | } |
732 | 0 | wipememory (inputbuf, sizeof inputbuf); |
733 | 0 | } |
734 | 0 | kyber_ct_len = 0; /* Avoid false compiler warning. Its use is |
735 | | * actually controlled by the only_ecc |
736 | | * condition. */ |
737 | 0 | } |
738 | 0 | else /* !only_ecc */ |
739 | 0 | { |
740 | 0 | kyber_pubkey = gcry_mpi_get_opaque (pk->pkey[is_rfc9980?1:2], &nbits); |
741 | 0 | kyber_pubkey_len = (nbits+7)/8; |
742 | 0 | if (kyber_pubkey_len == GCRY_KEM_MLKEM768_PUBKEY_LEN) |
743 | 0 | { |
744 | 0 | kyber_algo = GCRY_KEM_MLKEM768; |
745 | 0 | kyber_ct_len = GCRY_KEM_MLKEM768_ENCAPS_LEN; |
746 | 0 | kyber_ss_len = GCRY_KEM_MLKEM768_SHARED_LEN; |
747 | 0 | } |
748 | 0 | else if (kyber_pubkey_len == GCRY_KEM_MLKEM1024_PUBKEY_LEN) |
749 | 0 | { |
750 | 0 | kyber_algo = GCRY_KEM_MLKEM1024; |
751 | 0 | kyber_ct_len = GCRY_KEM_MLKEM1024_ENCAPS_LEN; |
752 | 0 | kyber_ss_len = GCRY_KEM_MLKEM1024_SHARED_LEN; |
753 | 0 | } |
754 | 0 | else |
755 | 0 | { |
756 | 0 | if (opt.verbose) |
757 | 0 | log_info ("%s: Kyber public key length invalid (%zu)\n", |
758 | 0 | __func__, kyber_pubkey_len); |
759 | 0 | err = gpg_error (GPG_ERR_INV_DATA); |
760 | 0 | goto leave; |
761 | 0 | } |
762 | 0 | if (DBG_CRYPTO) |
763 | 0 | log_printhex (kyber_pubkey, kyber_pubkey_len, "|!trunc|Kyber pubkey:"); |
764 | |
|
765 | 0 | err = gcry_kem_encap (kyber_algo, |
766 | 0 | kyber_pubkey, kyber_pubkey_len, |
767 | 0 | kyber_ct, kyber_ct_len, |
768 | 0 | kyber_ss, kyber_ss_len, |
769 | 0 | NULL, 0); |
770 | 0 | if (err) |
771 | 0 | { |
772 | 0 | if (opt.verbose) |
773 | 0 | log_info ("%s: gcry_kem_encap for Kyber failed\n", __func__); |
774 | 0 | goto leave; |
775 | 0 | } |
776 | | |
777 | 0 | if (DBG_CRYPTO) |
778 | 0 | { |
779 | 0 | log_printhex (kyber_ct, kyber_ct_len, "|!trunc|Kyber ephem:"); |
780 | 0 | log_printhex (kyber_ss, kyber_ss_len, "Kyber shared:"); |
781 | 0 | } |
782 | |
|
783 | 0 | if (is_rfc9980) |
784 | 0 | { |
785 | 0 | char fixedinfo[1+22]; /* algid || domSep || len(domSep) */ |
786 | |
|
787 | 0 | fixedinfo[0] = pk->pubkey_algo; |
788 | 0 | memcpy (fixedinfo+1, "OpenPGPCompositeKDFv1\x15", 22); |
789 | |
|
790 | 0 | err = gnupg_kem_combiner_sha3_256 (kek, kek_len, |
791 | 0 | ecc_ecdh, ecc_ct_len, |
792 | 0 | ecc_ct, ecc_ct_len, |
793 | 0 | ecc_pubkey, ecc_pubkey_len, |
794 | 0 | kyber_ss, kyber_ss_len, |
795 | 0 | fixedinfo, sizeof fixedinfo); |
796 | 0 | } |
797 | 0 | else |
798 | 0 | { |
799 | 0 | char fixedinfo[1+MAX_FINGERPRINT_LEN]; |
800 | 0 | int fixedlen; |
801 | |
|
802 | 0 | fixedinfo[0] = seskey_algo; |
803 | 0 | v5_fingerprint_from_pk (pk, fixedinfo+1, NULL); |
804 | 0 | fixedlen = 33; |
805 | |
|
806 | 0 | err = gnupg_kem_combiner (kek, kek_len, |
807 | 0 | ecc_ss, ecc_ss_len, |
808 | 0 | ecc_ct, ecc_ct_len, |
809 | 0 | kyber_ss, kyber_ss_len, |
810 | 0 | kyber_ct, kyber_ct_len, |
811 | 0 | fixedinfo, fixedlen); |
812 | 0 | } |
813 | |
|
814 | 0 | if (err) |
815 | 0 | { |
816 | 0 | if (opt.verbose) |
817 | 0 | log_info ("%s: KEM combiner failed\n", __func__); |
818 | 0 | goto leave; |
819 | 0 | } |
820 | 0 | } /* !only_ecc */ |
821 | | |
822 | 0 | if (DBG_CRYPTO) |
823 | 0 | log_printhex (kek, kek_len, "KEK:"); |
824 | |
|
825 | 0 | err = gcry_cipher_open (&hd, GCRY_CIPHER_AES256, |
826 | 0 | GCRY_CIPHER_MODE_AESWRAP, 0); |
827 | 0 | if (!err) |
828 | 0 | err = gcry_cipher_setkey (hd, kek, kek_len); |
829 | 0 | if (err) |
830 | 0 | { |
831 | 0 | if (opt.verbose) |
832 | 0 | log_error ("%s: failed to initialize AESWRAP: %s\n", __func__, |
833 | 0 | gpg_strerror (err)); |
834 | 0 | goto leave; |
835 | 0 | } |
836 | | |
837 | 0 | err = gcry_sexp_build (&s_data, NULL, "%m", data); |
838 | 0 | if (err) |
839 | 0 | goto leave; |
840 | | |
841 | 0 | n = gcry_cipher_get_algo_keylen (seskey_algo); |
842 | 0 | seskey = gcry_mpi_get_opaque (data, &nbits); |
843 | 0 | seskey_len = (nbits+7)/8; |
844 | 0 | if (seskey_len != n) |
845 | 0 | { |
846 | 0 | if (opt.verbose) |
847 | 0 | log_info ("%s: session key length %zu" |
848 | 0 | " does not match the length for algo %d\n", |
849 | 0 | __func__, seskey_len, seskey_algo); |
850 | 0 | err = gpg_error (GPG_ERR_INV_DATA); |
851 | 0 | goto leave; |
852 | 0 | } |
853 | 0 | if (DBG_CRYPTO) |
854 | 0 | log_printhex (seskey, seskey_len, "seskey:"); |
855 | |
|
856 | 0 | if (is_rfc9980) |
857 | 0 | { |
858 | 0 | enc_seskey_len = seskey_len + 8; |
859 | 0 | enc_seskey = xtrymalloc (enc_seskey_len); |
860 | 0 | if (!enc_seskey) |
861 | 0 | { |
862 | 0 | err = gpg_error_from_syserror (); |
863 | 0 | goto leave; |
864 | 0 | } |
865 | | |
866 | 0 | err = gcry_cipher_encrypt (hd, enc_seskey, enc_seskey_len, |
867 | 0 | seskey, seskey_len); |
868 | 0 | } |
869 | 0 | else |
870 | 0 | { |
871 | 0 | enc_seskey_len = 1 + seskey_len + 8; |
872 | 0 | enc_seskey = xtrymalloc (enc_seskey_len); |
873 | 0 | if (!enc_seskey || enc_seskey_len > 254) |
874 | 0 | { |
875 | 0 | err = gpg_error_from_syserror (); |
876 | 0 | goto leave; |
877 | 0 | } |
878 | | |
879 | 0 | enc_seskey[0] = enc_seskey_len - 1; |
880 | 0 | err = gcry_cipher_encrypt (hd, enc_seskey+1, enc_seskey_len-1, |
881 | 0 | seskey, seskey_len); |
882 | 0 | } |
883 | 0 | if (err) |
884 | 0 | { |
885 | 0 | log_error ("%s: wrapping session key failed\n", __func__); |
886 | 0 | goto leave; |
887 | 0 | } |
888 | 0 | if (DBG_CRYPTO) |
889 | 0 | log_printhex (enc_seskey, enc_seskey_len, "enc_seskey:"); |
890 | |
|
891 | 0 | i= 0; |
892 | 0 | resarr[i++] = gcry_mpi_set_opaque_copy (NULL, ecc_ct, 8 * ecc_ct_len); |
893 | 0 | if (!only_ecc) |
894 | 0 | resarr[i++] = gcry_mpi_set_opaque_copy (NULL, kyber_ct, 8 * kyber_ct_len); |
895 | 0 | resarr[i++] = gcry_mpi_set_opaque_copy (NULL, enc_seskey, 8 * enc_seskey_len); |
896 | |
|
897 | 0 | if (!resarr[0] || !resarr[1] || !(resarr[2] || only_ecc)) |
898 | 0 | { |
899 | 0 | err = gpg_error_from_syserror (); |
900 | 0 | for (i=0; i < 3; i++) |
901 | 0 | gcry_mpi_release (resarr[i]), resarr[i] = NULL; |
902 | 0 | } |
903 | |
|
904 | 0 | leave: |
905 | 0 | wipememory (ecc_ct, sizeof ecc_ct); |
906 | 0 | wipememory (ecc_ecdh, sizeof ecc_ecdh); |
907 | 0 | wipememory (ecc_ss, sizeof ecc_ss); |
908 | 0 | wipememory (kyber_ct, sizeof kyber_ct); |
909 | 0 | wipememory (kyber_ss, sizeof kyber_ss); |
910 | 0 | wipememory (kek, kek_len); |
911 | 0 | xfree (enc_seskey); |
912 | 0 | gcry_cipher_close (hd); |
913 | 0 | xfree (ecc_oid); |
914 | 0 | return err; |
915 | 0 | } |
916 | | |
917 | | |
918 | | /* Core of the encryption for the ECDH algorithms. See pk_decrypt for |
919 | | * a description of the arguments. */ |
920 | | static gpg_error_t |
921 | | do_encrypt_ecdh (PKT_public_key *pk, gcry_mpi_t data, gcry_mpi_t *resarr) |
922 | 0 | { |
923 | 0 | gpg_error_t err; |
924 | 0 | unsigned int nbits; |
925 | 0 | gcry_cipher_hd_t hd = NULL; |
926 | 0 | char *ecc_oid = NULL; |
927 | 0 | const char *curve; |
928 | 0 | const struct gnupg_ecc_params *ecc; |
929 | |
|
930 | 0 | const unsigned char *ecc_pubkey; |
931 | 0 | size_t ecc_pubkey_len; |
932 | 0 | const unsigned char *seskey; |
933 | 0 | size_t seskey_len; |
934 | 0 | unsigned char *enc_seskey = NULL; |
935 | 0 | size_t enc_seskey_len; |
936 | |
|
937 | 0 | unsigned char ecc_ct[ECC_POINT_LEN_MAX]; |
938 | 0 | unsigned char ecc_ecdh[ECC_POINT_LEN_MAX]; |
939 | 0 | size_t ecc_ct_len, ecc_ecdh_len; |
940 | 0 | const char *shared_secret; |
941 | 0 | size_t shared_secretlen; |
942 | 0 | const char *ephemeral_pubkey; |
943 | 0 | size_t ephemeral_pubkeylen; |
944 | |
|
945 | 0 | unsigned char *kek = NULL; |
946 | 0 | size_t kek_len; |
947 | |
|
948 | 0 | const unsigned char *kdf_params_spec; |
949 | 0 | byte fp[MAX_FINGERPRINT_LEN]; |
950 | 0 | int keywrap_cipher_algo; |
951 | 0 | int kdf_hash_algo; |
952 | 0 | unsigned char *kdf_params = NULL; |
953 | 0 | size_t kdf_params_len = 0; |
954 | |
|
955 | 0 | fingerprint_from_pk (pk, fp, NULL); |
956 | |
|
957 | 0 | ecc_oid = openpgp_oid_to_str (pk->pkey[0]); |
958 | 0 | if (!ecc_oid) |
959 | 0 | { |
960 | 0 | err = gpg_error_from_syserror (); |
961 | 0 | log_error ("%s: error getting OID for ECC key\n", __func__); |
962 | 0 | goto leave; |
963 | 0 | } |
964 | 0 | curve = openpgp_oid_to_curve (ecc_oid, 1); |
965 | 0 | if (!curve) |
966 | 0 | { |
967 | 0 | err = gpg_error (GPG_ERR_INV_DATA); |
968 | 0 | log_error ("%s: error getting curve for ECC key\n", __func__); |
969 | 0 | goto leave; |
970 | 0 | } |
971 | 0 | ecc = gnupg_get_ecc_params (curve); |
972 | 0 | if (!ecc) |
973 | 0 | { |
974 | 0 | if (opt.verbose) |
975 | 0 | log_info ("%s: ECC curve %s not supported\n", __func__, curve); |
976 | 0 | err = gpg_error (GPG_ERR_INV_DATA); |
977 | 0 | goto leave; |
978 | 0 | } |
979 | 0 | ecc_ct_len = ecc_ecdh_len = ecc->point_len; |
980 | |
|
981 | 0 | ecc_pubkey = gcry_mpi_get_opaque (pk->pkey[1], &nbits); |
982 | 0 | ecc_pubkey_len = (nbits+7)/8; |
983 | 0 | if (ecc_pubkey_len != ecc->pubkey_len) |
984 | 0 | { |
985 | 0 | if (ecc->kem_algo == GCRY_KEM_RAW_X25519 |
986 | 0 | && ecc_pubkey_len == ecc->pubkey_len - 1) |
987 | | /* For Curve25519, we also accept no prefix in the point |
988 | | * representation. */ |
989 | 0 | ; |
990 | 0 | else |
991 | 0 | { |
992 | 0 | if (opt.verbose) |
993 | 0 | log_info ("%s: ECC public key length invalid (%zu)\n", |
994 | 0 | __func__, ecc_pubkey_len); |
995 | 0 | err = gpg_error (GPG_ERR_INV_DATA); |
996 | 0 | goto leave; |
997 | 0 | } |
998 | 0 | } |
999 | | |
1000 | 0 | if (ecc->kem_algo == GCRY_KEM_RAW_X25519) |
1001 | 0 | { |
1002 | | /* Note: Legacy OID is OK here. */ |
1003 | | /* Optional prefix handling */ |
1004 | 0 | if (ecc_pubkey_len == 33 && *ecc_pubkey == 0x40) |
1005 | 0 | { |
1006 | 0 | ecc_pubkey++; /* Remove the 0x40 prefix. */ |
1007 | 0 | ecc_pubkey_len--; |
1008 | 0 | } |
1009 | 0 | } |
1010 | |
|
1011 | 0 | if (DBG_CRYPTO) |
1012 | 0 | { |
1013 | 0 | log_debug ("ECC curve: %s\n", ecc_oid); |
1014 | 0 | log_printhex (ecc_pubkey, ecc_pubkey_len, "ECC pubkey:"); |
1015 | 0 | } |
1016 | |
|
1017 | 0 | err = gcry_kem_encap (ecc->kem_algo, |
1018 | 0 | ecc_pubkey, ecc_pubkey_len, |
1019 | 0 | ecc_ct, ecc_ct_len, |
1020 | 0 | ecc_ecdh, ecc_ecdh_len, |
1021 | 0 | NULL, 0); |
1022 | 0 | if (err) |
1023 | 0 | { |
1024 | 0 | if (opt.verbose) |
1025 | 0 | log_info ("%s: gcry_kem_encap for ECC (%s) failed\n", |
1026 | 0 | __func__, ecc_oid); |
1027 | 0 | goto leave; |
1028 | 0 | } |
1029 | 0 | if (DBG_CRYPTO) |
1030 | 0 | { |
1031 | 0 | log_printhex (ecc_ct, ecc_ct_len, "ECC ephem:"); |
1032 | 0 | log_printhex (ecc_ecdh, ecc_ecdh_len, "ECC ecdh:"); |
1033 | 0 | } |
1034 | |
|
1035 | 0 | if (ecc->is_weierstrauss) |
1036 | 0 | { |
1037 | 0 | shared_secret = ecc_ecdh + 1; |
1038 | 0 | shared_secretlen = (ecc_ecdh_len - 1) / 2; |
1039 | 0 | ephemeral_pubkey = ecc_ct; |
1040 | 0 | ephemeral_pubkeylen = ecc_ct_len; |
1041 | 0 | } |
1042 | 0 | else |
1043 | 0 | { |
1044 | 0 | shared_secret = ecc_ecdh; |
1045 | 0 | shared_secretlen = ecc_ecdh_len; |
1046 | |
|
1047 | 0 | if (ecc->may_have_prefix) |
1048 | 0 | { |
1049 | 0 | ephemeral_pubkeylen = ecc_ct_len + 1; |
1050 | 0 | memmove (ecc_ct + 1, ecc_ct, ecc_ct_len); |
1051 | 0 | ecc_ct[0] = 0x40; |
1052 | 0 | } |
1053 | 0 | else |
1054 | 0 | ephemeral_pubkeylen = ecc_ct_len; |
1055 | |
|
1056 | 0 | ephemeral_pubkey = ecc_ct; |
1057 | 0 | } |
1058 | |
|
1059 | 0 | err = ecc_build_kdf_params (&kdf_params, &kdf_params_len, |
1060 | 0 | &kdf_params_spec, pk->pkey, fp); |
1061 | 0 | if (err) |
1062 | 0 | return err; |
1063 | | |
1064 | 0 | keywrap_cipher_algo = kdf_params_spec[3]; |
1065 | 0 | kdf_hash_algo = kdf_params_spec[2]; |
1066 | |
|
1067 | 0 | if (DBG_CRYPTO) |
1068 | 0 | log_debug ("ecdh KDF algorithms %s+%s with aeswrap\n", |
1069 | 0 | openpgp_md_algo_name (kdf_hash_algo), |
1070 | 0 | openpgp_cipher_algo_name (keywrap_cipher_algo)); |
1071 | |
|
1072 | 0 | if (kdf_hash_algo != GCRY_MD_SHA256 |
1073 | 0 | && kdf_hash_algo != GCRY_MD_SHA384 |
1074 | 0 | && kdf_hash_algo != GCRY_MD_SHA512) |
1075 | 0 | { |
1076 | 0 | err = gpg_error (GPG_ERR_BAD_PUBKEY); |
1077 | 0 | goto leave; |
1078 | 0 | } |
1079 | | |
1080 | 0 | if (keywrap_cipher_algo != CIPHER_ALGO_AES |
1081 | 0 | && keywrap_cipher_algo != CIPHER_ALGO_AES192 |
1082 | 0 | && keywrap_cipher_algo != CIPHER_ALGO_AES256) |
1083 | 0 | { |
1084 | 0 | err = gpg_error (GPG_ERR_BAD_PUBKEY); |
1085 | 0 | goto leave; |
1086 | 0 | } |
1087 | | |
1088 | 0 | kek_len = gcry_cipher_get_algo_keylen (keywrap_cipher_algo); |
1089 | 0 | if (kek_len > gcry_md_get_algo_dlen (kdf_hash_algo)) |
1090 | 0 | { |
1091 | 0 | err = gpg_error (GPG_ERR_BAD_PUBKEY); |
1092 | 0 | goto leave; |
1093 | 0 | } |
1094 | | |
1095 | 0 | kek = xtrymalloc (kek_len); |
1096 | 0 | if (!kek) |
1097 | 0 | { |
1098 | 0 | err = gpg_error_from_syserror (); |
1099 | 0 | goto leave; |
1100 | 0 | } |
1101 | | |
1102 | 0 | err = gnupg_ecc_kem_kdf (kek, kek_len, GCRY_KDF_ONESTEP_KDF, kdf_hash_algo, |
1103 | 0 | shared_secret, shared_secretlen, |
1104 | 0 | kdf_params, kdf_params_len); |
1105 | 0 | xfree (kdf_params); |
1106 | 0 | if (err) |
1107 | 0 | { |
1108 | 0 | if (opt.verbose) |
1109 | 0 | log_info ("%s: kdf for ECC failed\n", __func__); |
1110 | 0 | goto leave; |
1111 | 0 | } |
1112 | | |
1113 | 0 | if (DBG_CRYPTO) |
1114 | 0 | log_printhex (kek, kek_len, "KEK:"); |
1115 | |
|
1116 | 0 | err = gcry_cipher_open (&hd, keywrap_cipher_algo, |
1117 | 0 | GCRY_CIPHER_MODE_AESWRAP, 0); |
1118 | 0 | if (!err) |
1119 | 0 | err = gcry_cipher_setkey (hd, kek, kek_len); |
1120 | 0 | if (err) |
1121 | 0 | { |
1122 | 0 | if (opt.verbose) |
1123 | 0 | log_error ("%s: failed to initialize AESWRAP: %s\n", __func__, |
1124 | 0 | gpg_strerror (err)); |
1125 | 0 | goto leave; |
1126 | 0 | } |
1127 | | |
1128 | 0 | seskey = gcry_mpi_get_opaque (data, &nbits); |
1129 | 0 | seskey_len = (nbits+7)/8; |
1130 | |
|
1131 | 0 | enc_seskey_len = 1 + seskey_len + 8; |
1132 | 0 | enc_seskey = xtrymalloc (enc_seskey_len); |
1133 | 0 | if (!enc_seskey || enc_seskey_len > 254) |
1134 | 0 | { |
1135 | 0 | err = gpg_error_from_syserror (); |
1136 | 0 | goto leave; |
1137 | 0 | } |
1138 | | |
1139 | 0 | enc_seskey[0] = enc_seskey_len - 1; |
1140 | 0 | err = gcry_cipher_encrypt (hd, enc_seskey+1, enc_seskey_len-1, |
1141 | 0 | seskey, seskey_len); |
1142 | 0 | if (err) |
1143 | 0 | { |
1144 | 0 | log_error ("%s: wrapping session key failed\n", __func__); |
1145 | 0 | goto leave; |
1146 | 0 | } |
1147 | 0 | if (DBG_CRYPTO) |
1148 | 0 | log_printhex (enc_seskey, enc_seskey_len, "enc_seskey:"); |
1149 | |
|
1150 | 0 | resarr[0] = gcry_mpi_set_opaque_copy (NULL, ephemeral_pubkey, |
1151 | 0 | 8 * ephemeral_pubkeylen); |
1152 | 0 | if (!resarr[0]) |
1153 | 0 | { |
1154 | 0 | err = gpg_error_from_syserror (); |
1155 | 0 | goto leave; |
1156 | 0 | } |
1157 | | |
1158 | 0 | resarr[1] = gcry_mpi_set_opaque_copy (NULL, enc_seskey, 8 * enc_seskey_len); |
1159 | 0 | if (!resarr[1]) |
1160 | 0 | { |
1161 | 0 | err = gpg_error_from_syserror (); |
1162 | 0 | gcry_mpi_release (resarr[0]); |
1163 | 0 | } |
1164 | |
|
1165 | 0 | leave: |
1166 | 0 | xfree (enc_seskey); |
1167 | 0 | gcry_cipher_close (hd); |
1168 | 0 | xfree (kek); |
1169 | 0 | wipememory (ecc_ct, sizeof ecc_ct); |
1170 | 0 | wipememory (ecc_ecdh, sizeof ecc_ecdh); |
1171 | 0 | xfree (ecc_oid); |
1172 | 0 | return err; |
1173 | 0 | } |
1174 | | |
1175 | | |
1176 | | /* Core of the encryption for RSA and Elgamal algorithms. See |
1177 | | * pk_decrypt for a description of the arguments. */ |
1178 | | static gpg_error_t |
1179 | | do_encrypt_rsa_elg (PKT_public_key *pk, gcry_mpi_t data, gcry_mpi_t *resarr) |
1180 | 0 | { |
1181 | 0 | pubkey_algo_t algo = pk->pubkey_algo; |
1182 | 0 | gcry_mpi_t *pkey = pk->pkey; |
1183 | 0 | gcry_sexp_t s_ciph = NULL; |
1184 | 0 | gcry_sexp_t s_data = NULL; |
1185 | 0 | gcry_sexp_t s_pkey = NULL; |
1186 | 0 | gpg_error_t err; |
1187 | |
|
1188 | 0 | if (algo == PUBKEY_ALGO_ELGAMAL || algo == PUBKEY_ALGO_ELGAMAL_E) |
1189 | 0 | err = gcry_sexp_build (&s_pkey, NULL, |
1190 | 0 | "(public-key(elg(p%m)(g%m)(y%m)))", |
1191 | 0 | pkey[0], pkey[1], pkey[2]); |
1192 | 0 | else |
1193 | 0 | err = gcry_sexp_build (&s_pkey, NULL, |
1194 | 0 | "(public-key(rsa(n%m)(e%m)))", |
1195 | 0 | pkey[0], pkey[1]); |
1196 | 0 | if (err) |
1197 | 0 | goto leave; |
1198 | | |
1199 | 0 | err = gcry_sexp_build (&s_data, NULL, "%m", data); |
1200 | 0 | if (err) |
1201 | 0 | goto leave; |
1202 | | |
1203 | 0 | err = gcry_pk_encrypt (&s_ciph, s_data, s_pkey); |
1204 | 0 | if (err) |
1205 | 0 | goto leave; |
1206 | | |
1207 | 0 | gcry_sexp_release (s_data); s_data = NULL; |
1208 | 0 | gcry_sexp_release (s_pkey); s_pkey = NULL; |
1209 | |
|
1210 | 0 | resarr[0] = get_mpi_from_sexp (s_ciph, "a", GCRYMPI_FMT_USG); |
1211 | 0 | if (!is_RSA (algo)) |
1212 | 0 | resarr[1] = get_mpi_from_sexp (s_ciph, "b", GCRYMPI_FMT_USG); |
1213 | |
|
1214 | 0 | leave: |
1215 | 0 | gcry_sexp_release (s_data); |
1216 | 0 | gcry_sexp_release (s_pkey); |
1217 | 0 | gcry_sexp_release (s_ciph); |
1218 | 0 | return err; |
1219 | 0 | } |
1220 | | |
1221 | | |
1222 | | /* |
1223 | | * Emulate our old PK interface here - sometime in the future we might |
1224 | | * change the internal design to directly fit to libgcrypt. PK is is |
1225 | | * the OpenPGP public key packet, DATA is an MPI with the to be |
1226 | | * encrypted data, and RESARR receives the encrypted data. RESARRAY |
1227 | | * is expected to be an two/three item array which will be filled with |
1228 | | * newly allocated MPIs. SESKEY_ALGO is required for public key |
1229 | | * algorithms which do not encode it in DATA. |
1230 | | */ |
1231 | | gpg_error_t |
1232 | | pk_encrypt (PKT_public_key *pk, gcry_mpi_t data, int seskey_algo, |
1233 | | gcry_mpi_t *resarr) |
1234 | 0 | { |
1235 | 0 | pubkey_algo_t algo = pk->pubkey_algo; |
1236 | |
|
1237 | 0 | if (algo == PUBKEY_ALGO_KYBER) |
1238 | 0 | return do_encrypt_kem (pk, data, seskey_algo, resarr); |
1239 | 0 | else if (algo == PUBKEY_ALGO_ECDH) |
1240 | 0 | return do_encrypt_ecdh (pk, data, resarr); |
1241 | 0 | else if (algo == PUBKEY_ALGO_ELGAMAL || algo == PUBKEY_ALGO_ELGAMAL_E) |
1242 | 0 | return do_encrypt_rsa_elg (pk, data, resarr); |
1243 | 0 | else if (algo == PUBKEY_ALGO_RSA || algo == PUBKEY_ALGO_RSA_E) |
1244 | 0 | return do_encrypt_rsa_elg (pk, data, resarr); |
1245 | 0 | else if (RFC9980 && (algo == PUBKEY_ALGO_X25519 |
1246 | 0 | || IS_PUBKEY_ALGO_MLK (algo))) |
1247 | 0 | return do_encrypt_kem (pk, data, seskey_algo, resarr); |
1248 | 0 | else |
1249 | 0 | return gpg_error (GPG_ERR_PUBKEY_ALGO); |
1250 | 0 | } |
1251 | | |
1252 | | |
1253 | | /* Check whether SKEY is a suitable secret key. */ |
1254 | | int |
1255 | | pk_check_secret_key (pubkey_algo_t pkalgo, gcry_mpi_t *skey) |
1256 | 0 | { |
1257 | 0 | gcry_sexp_t s_skey; |
1258 | 0 | int rc; |
1259 | |
|
1260 | 0 | if (pkalgo == PUBKEY_ALGO_DSA) |
1261 | 0 | { |
1262 | 0 | rc = gcry_sexp_build (&s_skey, NULL, |
1263 | 0 | "(private-key(dsa(p%m)(q%m)(g%m)(y%m)(x%m)))", |
1264 | 0 | skey[0], skey[1], skey[2], skey[3], skey[4]); |
1265 | 0 | } |
1266 | 0 | else if (pkalgo == PUBKEY_ALGO_ELGAMAL || pkalgo == PUBKEY_ALGO_ELGAMAL_E) |
1267 | 0 | { |
1268 | 0 | rc = gcry_sexp_build (&s_skey, NULL, |
1269 | 0 | "(private-key(elg(p%m)(g%m)(y%m)(x%m)))", |
1270 | 0 | skey[0], skey[1], skey[2], skey[3]); |
1271 | 0 | } |
1272 | 0 | else if (is_RSA (pkalgo)) |
1273 | 0 | { |
1274 | 0 | rc = gcry_sexp_build (&s_skey, NULL, |
1275 | 0 | "(private-key(rsa(n%m)(e%m)(d%m)(p%m)(q%m)(u%m)))", |
1276 | 0 | skey[0], skey[1], skey[2], skey[3], skey[4], |
1277 | 0 | skey[5]); |
1278 | 0 | } |
1279 | 0 | else if (pkalgo == PUBKEY_ALGO_ECDSA || pkalgo == PUBKEY_ALGO_ECDH) |
1280 | 0 | { |
1281 | 0 | char *curve = openpgp_oid_to_str (skey[0]); |
1282 | 0 | if (!curve) |
1283 | 0 | rc = gpg_error_from_syserror (); |
1284 | 0 | else |
1285 | 0 | { |
1286 | 0 | rc = gcry_sexp_build (&s_skey, NULL, |
1287 | 0 | "(private-key(ecc(curve%s)(q%m)(d%m)))", |
1288 | 0 | curve, skey[1], skey[2]); |
1289 | 0 | xfree (curve); |
1290 | 0 | } |
1291 | 0 | } |
1292 | 0 | else if (pkalgo == PUBKEY_ALGO_EDDSA) |
1293 | 0 | { |
1294 | 0 | char *curve = openpgp_oid_to_str (skey[0]); |
1295 | 0 | if (!curve) |
1296 | 0 | rc = gpg_error_from_syserror (); |
1297 | 0 | else |
1298 | 0 | { |
1299 | 0 | const char *fmt; |
1300 | |
|
1301 | 0 | if (openpgp_oid_is_ed25519 (skey[0])) |
1302 | 0 | fmt = "(private-key(ecc(curve %s)(flags eddsa)(q%m)(d%m)))"; |
1303 | 0 | else |
1304 | 0 | fmt = "(private-key(ecc(curve %s)(q%m)(d%m)))"; |
1305 | |
|
1306 | 0 | rc = gcry_sexp_build (&s_skey, NULL, fmt, curve, skey[1], skey[2]); |
1307 | 0 | xfree (curve); |
1308 | 0 | } |
1309 | 0 | } |
1310 | 0 | else |
1311 | 0 | return GPG_ERR_PUBKEY_ALGO; |
1312 | | |
1313 | 0 | if (!rc) |
1314 | 0 | { |
1315 | 0 | rc = gcry_pk_testkey (s_skey); |
1316 | 0 | gcry_sexp_release (s_skey); |
1317 | 0 | } |
1318 | 0 | return rc; |
1319 | 0 | } |