From f2b54b3309519c693cc8e7a1d2008722a34a1e3c Mon Sep 17 00:00:00 2001 From: NIIBE Yutaka Date: Fri, 8 Oct 2021 22:07:14 +0900 Subject: [PATCH] experiment: Add forgotten open-misc.c. Signed-off-by: NIIBE Yutaka --- common/openpgp-misc.c | 61 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 common/openpgp-misc.c diff --git a/common/openpgp-misc.c b/common/openpgp-misc.c new file mode 100644 index 000000000..7c6c56a69 --- /dev/null +++ b/common/openpgp-misc.c @@ -0,0 +1,61 @@ +#include +#include +#include +#include +#include + +#include "util.h" +#include "openpgpdefs.h" + +gcry_mpi_t +openpgp_ecc_parse_pubkey (pubkey_algo_t pkalgo, const char *curve_oid, + gcry_mpi_t pubkey) +{ + unsigned int nbits = 0; + unsigned char *buf = NULL; + const char *curve = openpgp_oid_to_curve (curve_oid, 1); + + if (curve == NULL) + curve = curve_oid; + + if ((pkalgo == PUBKEY_ALGO_EDDSA && !strcmp (curve, "Ed448")) + || (pkalgo == PUBKEY_ALGO_ECDH && !strcmp (curve, "X448"))) + buf = gcry_mpi_get_opaque (pubkey, &nbits); + + if (nbits == 0 + || (pkalgo == PUBKEY_ALGO_EDDSA && (nbits+7)/8 == (448 + 8)/8) + || (pkalgo == PUBKEY_ALGO_ECDH && (nbits+7)/8 == 448/8)) + return gcry_mpi_copy (pubkey); + + if (pkalgo == PUBKEY_ALGO_EDDSA) + return gcry_mpi_set_opaque_copy (NULL, buf+1, 8 + 448); + else + return gcry_mpi_set_opaque_copy (NULL, buf+1, 448); +} + + +gcry_mpi_t +openpgp_ecc_parse_seckey (pubkey_algo_t pkalgo, const char *curve_oid, + gcry_mpi_t seckey) +{ + unsigned int nbits = 0; + unsigned char *buf = NULL; + const char *curve = openpgp_oid_to_curve (curve_oid, 1); + + if (curve == NULL) + curve = curve_oid; + + if ((pkalgo == PUBKEY_ALGO_EDDSA && !strcmp (curve, "Ed448")) + || (pkalgo == PUBKEY_ALGO_ECDH && !strcmp (curve, "X448"))) + buf = gcry_mpi_get_opaque (seckey, &nbits); + + if (nbits == 0 + || (pkalgo == PUBKEY_ALGO_EDDSA && (nbits+7)/8 == (448 + 8)/8) + || (pkalgo == PUBKEY_ALGO_ECDH && (nbits+7)/8 == 448/8)) + return gcry_mpi_copy (seckey); + + if (pkalgo == PUBKEY_ALGO_EDDSA) + return gcry_mpi_set_opaque_copy (NULL, buf+1, 8 + 448); + else + return gcry_mpi_set_opaque_copy (NULL, buf+1, 448); +}