From 2b08e8484921dc965e91c1592cabcd03ec99f068 Mon Sep 17 00:00:00 2001 From: NIIBE Yutaka Date: Thu, 21 Oct 2021 14:07:30 +0900 Subject: [PATCH] experiment: Support keygen for new Ed448/X448 keys. Signed-off-by: NIIBE Yutaka --- g10/keygen.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/g10/keygen.c b/g10/keygen.c index cb6487ea3..c90e95be5 100644 --- a/g10/keygen.c +++ b/g10/keygen.c @@ -1283,6 +1283,47 @@ write_keybinding (ctrl_t ctrl, kbnode_t root, } +static gpg_error_t +sos_fixup_pubkey_448 (int algo, gcry_mpi_t *p_pubkey) +{ + gcry_mpi_t pubkey_mpi; + gcry_mpi_t a; + unsigned char *p; + const unsigned char *p_key; + unsigned int nbits; + unsigned int len; + + pubkey_mpi = *p_pubkey; + *p_pubkey = NULL; + p_key = gcry_mpi_get_opaque (pubkey_mpi, &nbits); + len = (nbits+7)/8; + if ((algo == PUBKEY_ALGO_ECDH && len != 56) + || (algo == PUBKEY_ALGO_EDDSA && len != 57) + || (algo != PUBKEY_ALGO_ECDH && algo != PUBKEY_ALGO_EDDSA)) + { + gcry_mpi_release (pubkey_mpi); + return gpg_error (GPG_ERR_BAD_PUBKEY); + } + + p = xtrymalloc (1 + len); + if (!p) + { + gcry_mpi_release (pubkey_mpi); + return gpg_error_from_syserror (); + } + + p[0] = 0x40; + memcpy (p+1, p_key, len); + + a = gcry_mpi_set_opaque (NULL, p, 0); + gcry_mpi_set_flag (a, GCRYMPI_FLAG_USER2); + *p_pubkey = a; + gcry_mpi_release (pubkey_mpi); + + return 0; +} + + static gpg_error_t ecckey_from_sexp (gcry_mpi_t *array, gcry_sexp_t sexp, int algo) { @@ -1335,6 +1376,14 @@ ecckey_from_sexp (gcry_mpi_t *array, gcry_sexp_t sexp, int algo) if (err) goto leave; + if (openpgp_oid_is_ed448 (array[0]) + || openpgp_oid_is_cv448 (array[0])) + { + err = sos_fixup_pubkey_448 (algo, &array[1]); + if (err) + goto leave; + } + gcry_sexp_release (list); if (algo == PUBKEY_ALGO_ECDH)