From 5592ed4333c70373352297c913860c0a49c99312 Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Tue, 6 Apr 2021 11:50:42 -0400 Subject: Add a function for creating an ECC key with arbitrary group This is necessary in order to hide the ECC impl headers since in some cases you may need to use a non-standard group. --- src/lib/pubkey/pk_algs.cpp | 70 ++++++++++++++++++++++++++-------------------- src/lib/pubkey/pk_algs.h | 11 ++++++++ 2 files changed, 51 insertions(+), 30 deletions(-) (limited to 'src/lib') diff --git a/src/lib/pubkey/pk_algs.cpp b/src/lib/pubkey/pk_algs.cpp index a5267b894..f1f6c5974 100644 --- a/src/lib/pubkey/pk_algs.cpp +++ b/src/lib/pubkey/pk_algs.cpp @@ -260,6 +260,45 @@ std::string default_ec_group_for(const std::string& alg_name) #endif +BOTAN_PUBLIC_API(3,0) std::unique_ptr +create_ec_private_key(const std::string& alg_name, + const EC_Group& ec_group, + RandomNumberGenerator& rng) + { +#if defined(BOTAN_HAS_ECDSA) + if(alg_name == "ECDSA") + return std::make_unique(rng, ec_group); +#endif + +#if defined(BOTAN_HAS_ECDH) + if(alg_name == "ECDH") + return std::make_unique(rng, ec_group); +#endif + +#if defined(BOTAN_HAS_ECKCDSA) + if(alg_name == "ECKCDSA") + return std::make_unique(rng, ec_group); +#endif + +#if defined(BOTAN_HAS_GOST_34_10_2001) + if(alg_name == "GOST-34.10" || alg_name == "GOST-34.10-2012-256" || alg_name == "GOST-34.10-2012-512") + return std::make_unique(rng, ec_group); +#endif + +#if defined(BOTAN_HAS_SM2) + if(alg_name == "SM2" || alg_name == "SM2_Sig" || alg_name == "SM2_Enc") + return std::make_unique(rng, ec_group); +#endif + +#if defined(BOTAN_HAS_ECGDSA) + if(alg_name == "ECGDSA") + return std::make_unique(rng, ec_group); +#endif + + return nullptr; + } + + std::unique_ptr create_private_key(const std::string& alg_name, RandomNumberGenerator& rng, @@ -338,36 +377,7 @@ create_private_key(const std::string& alg_name, alg_name == "GOST-34.10-2012-512") { const EC_Group ec_group(params.empty() ? default_ec_group_for(alg_name) : params); - -#if defined(BOTAN_HAS_ECDSA) - if(alg_name == "ECDSA") - return std::make_unique(rng, ec_group); -#endif - -#if defined(BOTAN_HAS_ECDH) - if(alg_name == "ECDH") - return std::make_unique(rng, ec_group); -#endif - -#if defined(BOTAN_HAS_ECKCDSA) - if(alg_name == "ECKCDSA") - return std::make_unique(rng, ec_group); -#endif - -#if defined(BOTAN_HAS_GOST_34_10_2001) - if(alg_name == "GOST-34.10" || alg_name == "GOST-34.10-2012-256" || alg_name == "GOST-34.10-2012-512") - return std::make_unique(rng, ec_group); -#endif - -#if defined(BOTAN_HAS_SM2) - if(alg_name == "SM2" || alg_name == "SM2_Sig" || alg_name == "SM2_Enc") - return std::make_unique(rng, ec_group); -#endif - -#if defined(BOTAN_HAS_ECGDSA) - if(alg_name == "ECGDSA") - return std::make_unique(rng, ec_group); -#endif + return create_ec_private_key(alg_name, ec_group, rng); } #endif diff --git a/src/lib/pubkey/pk_algs.h b/src/lib/pubkey/pk_algs.h index 12514908e..09a03eed4 100644 --- a/src/lib/pubkey/pk_algs.h +++ b/src/lib/pubkey/pk_algs.h @@ -36,6 +36,17 @@ create_private_key(const std::string& algo_name, const std::string& algo_params = "", const std::string& provider = ""); + +/** +* Create a new ECC key +*/ +class EC_Group; + +BOTAN_PUBLIC_API(3,0) std::unique_ptr +create_ec_private_key(const std::string& algo_name, + const EC_Group& group, + RandomNumberGenerator& rng); + BOTAN_PUBLIC_API(2,2) std::vector probe_provider_private_key(const std::string& algo_name, -- cgit v1.2.3