1 /* pkglue.c - public key operations glue code
2 * Copyright (C) 2000, 2003, 2010 Free Software Foundation, Inc.
3 * Copyright (C) 2014 Werner Koch
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * GnuPG 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 General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <https://www.gnu.org/licenses/>.
33 /* FIXME: Better change the function name because mpi_ is used by
36 get_mpi_from_sexp (gcry_sexp_t sexp, const char *item, int mpifmt)
41 list = gcry_sexp_find_token (sexp, item, 0);
43 data = gcry_sexp_nth_mpi (list, 1, mpifmt);
45 gcry_sexp_release (list);
52 * Emulate our old PK interface here - sometime in the future we might
53 * change the internal design to directly fit to libgcrypt.
56 pk_verify (pubkey_algo_t pkalgo, gcry_mpi_t hash,
57 gcry_mpi_t *data, gcry_mpi_t *pkey)
59 gcry_sexp_t s_sig, s_hash, s_pkey;
61 unsigned int neededfixedlen = 0;
63 /* Make a sexp from pkey. */
64 if (pkalgo == PUBKEY_ALGO_DSA)
66 rc = gcry_sexp_build (&s_pkey, NULL,
67 "(public-key(dsa(p%m)(q%m)(g%m)(y%m)))",
68 pkey[0], pkey[1], pkey[2], pkey[3]);
70 else if (pkalgo == PUBKEY_ALGO_ELGAMAL_E || pkalgo == PUBKEY_ALGO_ELGAMAL)
72 rc = gcry_sexp_build (&s_pkey, NULL,
73 "(public-key(elg(p%m)(g%m)(y%m)))",
74 pkey[0], pkey[1], pkey[2]);
76 else if (pkalgo == PUBKEY_ALGO_RSA || pkalgo == PUBKEY_ALGO_RSA_S)
78 rc = gcry_sexp_build (&s_pkey, NULL,
79 "(public-key(rsa(n%m)(e%m)))", pkey[0], pkey[1]);
81 else if (pkalgo == PUBKEY_ALGO_ECDSA)
83 char *curve = openpgp_oid_to_str (pkey[0]);
85 rc = gpg_error_from_syserror ();
88 rc = gcry_sexp_build (&s_pkey, NULL,
89 "(public-key(ecdsa(curve %s)(q%m)))",
94 else if (pkalgo == PUBKEY_ALGO_EDDSA)
96 char *curve = openpgp_oid_to_str (pkey[0]);
98 rc = gpg_error_from_syserror ();
101 rc = gcry_sexp_build (&s_pkey, NULL,
102 "(public-key(ecc(curve %s)"
103 "(flags eddsa)(q%m)))",
108 if (openpgp_oid_is_ed25519 (pkey[0]))
109 neededfixedlen = 256 / 8;
112 return GPG_ERR_PUBKEY_ALGO;
115 BUG (); /* gcry_sexp_build should never fail. */
117 /* Put hash into a S-Exp s_hash. */
118 if (pkalgo == PUBKEY_ALGO_EDDSA)
120 if (gcry_sexp_build (&s_hash, NULL,
121 "(data(flags eddsa)(hash-algo sha512)(value %m))",
123 BUG (); /* gcry_sexp_build should never fail. */
127 if (gcry_sexp_build (&s_hash, NULL, "%m", hash))
128 BUG (); /* gcry_sexp_build should never fail. */
131 /* Put data into a S-Exp s_sig. */
133 if (pkalgo == PUBKEY_ALGO_DSA)
135 if (!data[0] || !data[1])
136 rc = gpg_error (GPG_ERR_BAD_MPI);
138 rc = gcry_sexp_build (&s_sig, NULL,
139 "(sig-val(dsa(r%m)(s%m)))", data[0], data[1]);
141 else if (pkalgo == PUBKEY_ALGO_ECDSA)
143 if (!data[0] || !data[1])
144 rc = gpg_error (GPG_ERR_BAD_MPI);
146 rc = gcry_sexp_build (&s_sig, NULL,
147 "(sig-val(ecdsa(r%m)(s%m)))", data[0], data[1]);
149 else if (pkalgo == PUBKEY_ALGO_EDDSA)
151 gcry_mpi_t r = data[0];
152 gcry_mpi_t s = data[1];
153 size_t rlen, slen, n; /* (bytes) */
156 log_assert (neededfixedlen <= sizeof buf);
159 rc = gpg_error (GPG_ERR_BAD_MPI);
160 else if ((rlen = (gcry_mpi_get_nbits (r)+7)/8) > neededfixedlen || !rlen)
161 rc = gpg_error (GPG_ERR_BAD_MPI);
162 else if ((slen = (gcry_mpi_get_nbits (s)+7)/8) > neededfixedlen || !slen)
163 rc = gpg_error (GPG_ERR_BAD_MPI);
166 /* We need to fixup the length in case of leading zeroes.
167 * OpenPGP does not allow leading zeroes and the parser for
168 * the signature packet has no information on the use curve,
169 * thus we need to do it here. We won't do it for opaque
170 * MPIs under the assumption that they are known to be fine;
171 * we won't see them here anyway but the check is anyway
172 * required. Fixme: A nifty feature for gcry_sexp_build
173 * would be a format to left pad the value (e.g. "%*M"). */
176 if (rlen < neededfixedlen
177 && !gcry_mpi_get_flag (r, GCRYMPI_FLAG_OPAQUE)
178 && !(rc=gcry_mpi_print (GCRYMPI_FMT_USG, buf, sizeof buf, &n, r)))
180 log_assert (n < neededfixedlen);
181 memmove (buf + (neededfixedlen - n), buf, n);
182 memset (buf, 0, neededfixedlen - n);
183 r = gcry_mpi_set_opaque_copy (NULL, buf, neededfixedlen * 8);
185 if (slen < neededfixedlen
186 && !gcry_mpi_get_flag (s, GCRYMPI_FLAG_OPAQUE)
187 && !(rc=gcry_mpi_print (GCRYMPI_FMT_USG, buf, sizeof buf, &n, s)))
189 log_assert (n < neededfixedlen);
190 memmove (buf + (neededfixedlen - n), buf, n);
191 memset (buf, 0, neededfixedlen - n);
192 s = gcry_mpi_set_opaque_copy (NULL, buf, neededfixedlen * 8);
196 rc = gcry_sexp_build (&s_sig, NULL,
197 "(sig-val(eddsa(r%M)(s%M)))", r, s);
200 gcry_mpi_release (r);
202 gcry_mpi_release (s);
205 else if (pkalgo == PUBKEY_ALGO_ELGAMAL || pkalgo == PUBKEY_ALGO_ELGAMAL_E)
207 if (!data[0] || !data[1])
208 rc = gpg_error (GPG_ERR_BAD_MPI);
210 rc = gcry_sexp_build (&s_sig, NULL,
211 "(sig-val(elg(r%m)(s%m)))", data[0], data[1]);
213 else if (pkalgo == PUBKEY_ALGO_RSA || pkalgo == PUBKEY_ALGO_RSA_S)
216 rc = gpg_error (GPG_ERR_BAD_MPI);
218 rc = gcry_sexp_build (&s_sig, NULL, "(sig-val(rsa(s%m)))", data[0]);
224 rc = gcry_pk_verify (s_sig, s_hash, s_pkey);
226 gcry_sexp_release (s_sig);
227 gcry_sexp_release (s_hash);
228 gcry_sexp_release (s_pkey);
236 * Emulate our old PK interface here - sometime in the future we might
237 * change the internal design to directly fit to libgcrypt.
238 * PK is only required to compute the fingerprint for ECDH.
241 pk_encrypt (pubkey_algo_t algo, gcry_mpi_t *resarr, gcry_mpi_t data,
242 PKT_public_key *pk, gcry_mpi_t *pkey)
244 gcry_sexp_t s_ciph = NULL;
245 gcry_sexp_t s_data = NULL;
246 gcry_sexp_t s_pkey = NULL;
249 /* Make a sexp from pkey. */
250 if (algo == PUBKEY_ALGO_ELGAMAL || algo == PUBKEY_ALGO_ELGAMAL_E)
252 rc = gcry_sexp_build (&s_pkey, NULL,
253 "(public-key(elg(p%m)(g%m)(y%m)))",
254 pkey[0], pkey[1], pkey[2]);
255 /* Put DATA into a simplified S-expression. */
257 rc = gcry_sexp_build (&s_data, NULL, "%m", data);
259 else if (algo == PUBKEY_ALGO_RSA || algo == PUBKEY_ALGO_RSA_E)
261 rc = gcry_sexp_build (&s_pkey, NULL,
262 "(public-key(rsa(n%m)(e%m)))",
264 /* Put DATA into a simplified S-expression. */
266 rc = gcry_sexp_build (&s_data, NULL, "%m", data);
268 else if (algo == PUBKEY_ALGO_ECDH)
272 rc = pk_ecdh_generate_ephemeral_key (pkey, &k);
277 curve = openpgp_oid_to_str (pkey[0]);
279 rc = gpg_error_from_syserror ();
282 int with_djb_tweak_flag = openpgp_oid_is_cv25519 (pkey[0]);
284 /* Now use the ephemeral secret to compute the shared point. */
285 rc = gcry_sexp_build (&s_pkey, NULL,
286 with_djb_tweak_flag ?
287 "(public-key(ecdh(curve%s)(flags djb-tweak)(q%m)))"
288 : "(public-key(ecdh(curve%s)(q%m)))",
291 /* Put K into a simplified S-expression. */
293 rc = gcry_sexp_build (&s_data, NULL, "%m", k);
295 gcry_mpi_release (k);
299 rc = gpg_error (GPG_ERR_PUBKEY_ALGO);
301 /* Pass it to libgcrypt. */
303 rc = gcry_pk_encrypt (&s_ciph, s_data, s_pkey);
305 gcry_sexp_release (s_data);
306 gcry_sexp_release (s_pkey);
310 else if (algo == PUBKEY_ALGO_ECDH)
312 gcry_mpi_t shared, public, result;
313 byte fp[MAX_FINGERPRINT_LEN];
316 /* Get the shared point and the ephemeral public key. */
317 shared = get_mpi_from_sexp (s_ciph, "s", GCRYMPI_FMT_USG);
318 public = get_mpi_from_sexp (s_ciph, "e", GCRYMPI_FMT_USG);
319 gcry_sexp_release (s_ciph);
323 log_debug ("ECDH ephemeral key:");
324 gcry_mpi_dump (public);
329 fingerprint_from_pk (pk, fp, &fpn);
331 rc = gpg_error (GPG_ERR_INV_LENGTH);
333 rc = pk_ecdh_encrypt_with_shared_point (1 /*=encrypton*/, shared,
334 fp, data, pkey, &result);
335 gcry_mpi_release (shared);
343 gcry_mpi_release (public);
344 gcry_mpi_release (result);
347 else /* Elgamal or RSA case. */
348 { /* Fixme: Add better error handling or make gnupg use
349 S-expressions directly. */
350 resarr[0] = get_mpi_from_sexp (s_ciph, "a", GCRYMPI_FMT_USG);
352 resarr[1] = get_mpi_from_sexp (s_ciph, "b", GCRYMPI_FMT_USG);
355 gcry_sexp_release (s_ciph);
360 /* Check whether SKEY is a suitable secret key. */
362 pk_check_secret_key (pubkey_algo_t pkalgo, gcry_mpi_t *skey)
367 if (pkalgo == PUBKEY_ALGO_DSA)
369 rc = gcry_sexp_build (&s_skey, NULL,
370 "(private-key(dsa(p%m)(q%m)(g%m)(y%m)(x%m)))",
371 skey[0], skey[1], skey[2], skey[3], skey[4]);
373 else if (pkalgo == PUBKEY_ALGO_ELGAMAL || pkalgo == PUBKEY_ALGO_ELGAMAL_E)
375 rc = gcry_sexp_build (&s_skey, NULL,
376 "(private-key(elg(p%m)(g%m)(y%m)(x%m)))",
377 skey[0], skey[1], skey[2], skey[3]);
379 else if (is_RSA (pkalgo))
381 rc = gcry_sexp_build (&s_skey, NULL,
382 "(private-key(rsa(n%m)(e%m)(d%m)(p%m)(q%m)(u%m)))",
383 skey[0], skey[1], skey[2], skey[3], skey[4],
386 else if (pkalgo == PUBKEY_ALGO_ECDSA || pkalgo == PUBKEY_ALGO_ECDH)
388 char *curve = openpgp_oid_to_str (skey[0]);
390 rc = gpg_error_from_syserror ();
393 rc = gcry_sexp_build (&s_skey, NULL,
394 "(private-key(ecc(curve%s)(q%m)(d%m)))",
395 curve, skey[1], skey[2]);
399 else if (pkalgo == PUBKEY_ALGO_EDDSA)
401 char *curve = openpgp_oid_to_str (skey[0]);
403 rc = gpg_error_from_syserror ();
406 rc = gcry_sexp_build (&s_skey, NULL,
407 "(private-key(ecc(curve %s)"
408 "(flags eddsa)(q%m)(d%m)))",
409 curve, skey[1], skey[2]);
414 return GPG_ERR_PUBKEY_ALGO;
418 rc = gcry_pk_testkey (s_skey);
419 gcry_sexp_release (s_skey);