From 49c891a9bfac24a1d95e76d33d44a49426247777 Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Sun, 9 Feb 2020 14:00:57 +0100 Subject: [PATCH] common: Add OpenPGP<->Gcrypt pubkey id mapping functions. * g10/misc.c (map_pk_gcry_to_openpgp): Move to ... * common/openpgp-oid.c (map_gcry_pk_to_openpgp): here and rename. Change all 4 callers. (map_openpgp_pk_to_gcry): New. Signed-off-by: Werner Koch --- common/openpgp-oid.c | 30 ++++++++++++++++++++++++++++++ common/openpgpdefs.h | 8 +++++++- g10/keygen.c | 8 ++++---- g10/main.h | 1 - g10/misc.c | 15 --------------- 5 files changed, 41 insertions(+), 21 deletions(-) diff --git a/common/openpgp-oid.c b/common/openpgp-oid.c index 419471870..802d71162 100644 --- a/common/openpgp-oid.c +++ b/common/openpgp-oid.c @@ -462,3 +462,33 @@ openpgp_is_curve_supported (const char *name, int *r_algo, } return NULL; } + + +/* Map a Gcrypt public key algorithm number to the used by OpenPGP. + * Returns 0 for unknown gcry algorithm. */ +pubkey_algo_t +map_gcry_pk_to_openpgp (enum gcry_pk_algos algo) +{ + switch (algo) + { + case GCRY_PK_EDDSA: return PUBKEY_ALGO_EDDSA; + case GCRY_PK_ECDSA: return PUBKEY_ALGO_ECDSA; + case GCRY_PK_ECDH: return PUBKEY_ALGO_ECDH; + default: return algo < 110 ? (pubkey_algo_t)algo : 0; + } +} + + +/* Map an OpenPGP public key algorithm number to the one used by + * Libgcrypt. Returns 0 for unknown gcry algorithm. */ +enum gcry_pk_algos +map_openpgp_pk_to_gcry (pubkey_algo_t algo) +{ + switch (algo) + { + case PUBKEY_ALGO_EDDSA: return GCRY_PK_EDDSA; + case PUBKEY_ALGO_ECDSA: return GCRY_PK_ECDSA; + case PUBKEY_ALGO_ECDH: return GCRY_PK_ECDH; + default: return algo < 110 ? algo : 0; + } +} diff --git a/common/openpgpdefs.h b/common/openpgpdefs.h index 868e141ce..2962fe2d4 100644 --- a/common/openpgpdefs.h +++ b/common/openpgpdefs.h @@ -209,8 +209,14 @@ compress_algo_t; #define S2K_DECODE_COUNT(_val) ((16ul + ((_val) & 15)) << (((_val) >> 4) + 6)) -/*--openpgp-s2k.c --*/ +/*-- openpgp-s2k.c --*/ unsigned char encode_s2k_iterations (int iterations); +/*-- openpgp-oid.c --*/ +pubkey_algo_t map_gcry_pk_to_openpgp (enum gcry_pk_algos algo); +enum gcry_pk_algos map_openpgp_pk_to_gcry (pubkey_algo_t algo); + + + #endif /*GNUPG_COMMON_OPENPGPDEFS_H*/ diff --git a/g10/keygen.c b/g10/keygen.c index 61682158e..447743fca 100644 --- a/g10/keygen.c +++ b/g10/keygen.c @@ -2048,7 +2048,7 @@ check_keygrip (ctrl_t ctrl, const char *hexgrip) algo = get_pk_algo_from_canon_sexp (public, publiclen); xfree (public); - return map_pk_gcry_to_openpgp (algo); + return map_gcry_pk_to_openpgp (algo); } @@ -2309,7 +2309,7 @@ ask_algo (ctrl_t ctrl, int addmode, int *r_subkey_algo, unsigned int *r_usage, && !(sl->flags & GCRY_PK_USAGE_ENCR)) sl->flags = (PUBKEY_ALGO_EDDSA << 8); else - sl->flags |= (map_pk_gcry_to_openpgp (algoid) << 8); + sl->flags |= (map_gcry_pk_to_openpgp (algoid) << 8); tty_printf (" (%d) %s %s", count, sl->d, algostr); if ((sl->flags & GCRY_PK_USAGE_CERT)) @@ -3451,7 +3451,7 @@ parse_key_parameter_part (ctrl_t ctrl, && !(sl->flags & GCRY_PK_USAGE_ENCR)) algo = PUBKEY_ALGO_EDDSA; else - algo = map_pk_gcry_to_openpgp (algoid); + algo = map_gcry_pk_to_openpgp (algoid); xfree (algostr); xfree (keygrip); @@ -3796,7 +3796,7 @@ get_parameter_algo (ctrl_t ctrl, struct para_data_s *para, enum para_name key, else if (!ascii_strcasecmp (r->u.value, "ECDH")) i = PUBKEY_ALGO_ECDH; else - i = map_pk_gcry_to_openpgp (gcry_pk_map_name (r->u.value)); + i = map_gcry_pk_to_openpgp (gcry_pk_map_name (r->u.value)); if (i == PUBKEY_ALGO_RSA_E || i == PUBKEY_ALGO_RSA_S) i = 0; /* we don't want to allow generation of these algorithms */ diff --git a/g10/main.h b/g10/main.h index 2001c8646..ec58795b0 100644 --- a/g10/main.h +++ b/g10/main.h @@ -138,7 +138,6 @@ gpg_error_t openpgp_aead_algo_info (aead_algo_t algo, enum gcry_cipher_modes *r_mode, unsigned int *r_noncelen); -pubkey_algo_t map_pk_gcry_to_openpgp (enum gcry_pk_algos algo); int openpgp_pk_test_algo (pubkey_algo_t algo); int openpgp_pk_test_algo2 (pubkey_algo_t algo, unsigned int use); int openpgp_pk_algo_usage ( int algo ); diff --git a/g10/misc.c b/g10/misc.c index 07ce1d41d..8ace21f09 100644 --- a/g10/misc.c +++ b/g10/misc.c @@ -523,21 +523,6 @@ map_cipher_gcry_to_openpgp (enum gcry_cipher_algos algo) } } -/* Map Gcrypt public key algorithm numbers to those used by OpenPGP. - FIXME: This mapping is used at only two places - we should get rid - of it. */ -pubkey_algo_t -map_pk_gcry_to_openpgp (enum gcry_pk_algos algo) -{ - switch (algo) - { - case GCRY_PK_EDDSA: return PUBKEY_ALGO_EDDSA; - case GCRY_PK_ECDSA: return PUBKEY_ALGO_ECDSA; - case GCRY_PK_ECDH: return PUBKEY_ALGO_ECDH; - default: return algo < 110 ? (pubkey_algo_t)algo : 0; - } -} - /* Return the block length of an OpenPGP cipher algorithm. */ int