diff options
Diffstat (limited to 'src/pubkey')
-rw-r--r-- | src/pubkey/ec_group/ec_group.cpp (renamed from src/pubkey/ec_dompar/ec_dompar.cpp) | 26 | ||||
-rw-r--r-- | src/pubkey/ec_group/ec_group.h (renamed from src/pubkey/ec_dompar/ec_dompar.h) | 23 | ||||
-rw-r--r-- | src/pubkey/ec_group/info.txt (renamed from src/pubkey/ec_dompar/info.txt) | 7 | ||||
-rw-r--r-- | src/pubkey/ecc_key/ecc_key.cpp | 36 | ||||
-rw-r--r-- | src/pubkey/ecc_key/ecc_key.h | 26 | ||||
-rw-r--r-- | src/pubkey/ecc_key/info.txt | 3 | ||||
-rw-r--r-- | src/pubkey/ecdh/ecdh.h | 8 | ||||
-rw-r--r-- | src/pubkey/ecdh/info.txt | 2 | ||||
-rw-r--r-- | src/pubkey/ecdsa/ecdsa.cpp | 5 | ||||
-rw-r--r-- | src/pubkey/ecdsa/ecdsa.h | 22 | ||||
-rw-r--r-- | src/pubkey/ecdsa/info.txt | 2 | ||||
-rw-r--r-- | src/pubkey/gost_3410/gost_3410.cpp | 14 | ||||
-rw-r--r-- | src/pubkey/gost_3410/gost_3410.h | 16 | ||||
-rw-r--r-- | src/pubkey/gost_3410/info.txt | 2 |
14 files changed, 93 insertions, 99 deletions
diff --git a/src/pubkey/ec_dompar/ec_dompar.cpp b/src/pubkey/ec_group/ec_group.cpp index deb512518..fe4fae885 100644 --- a/src/pubkey/ec_dompar/ec_dompar.cpp +++ b/src/pubkey/ec_group/ec_group.cpp @@ -7,7 +7,7 @@ * Distributed under the terms of the Botan license */ -#include <botan/ec_dompar.h> +#include <botan/ec_group.h> #include <botan/ber_dec.h> #include <botan/der_enc.h> #include <botan/libstate.h> @@ -16,7 +16,7 @@ namespace Botan { -EC_Domain_Params::EC_Domain_Params(const OID& domain_oid) +EC_Group::EC_Group(const OID& domain_oid) { std::string pem = global_state().get("ec", OIDS::lookup(domain_oid)); @@ -24,31 +24,31 @@ EC_Domain_Params::EC_Domain_Params(const OID& domain_oid) if(pem == "") throw Lookup_Error("No ECC domain data for " + domain_oid.as_string()); - *this = EC_Domain_Params(pem); + *this = EC_Group(pem); oid = domain_oid.as_string(); } -EC_Domain_Params::EC_Domain_Params(const std::string& pem) +EC_Group::EC_Group(const std::string& str) { - if(pem == "") + if(str == "") return; // no initialization / uninitialized try { - DataSource_Memory input(pem); + DataSource_Memory input(str); SecureVector<byte> ber = PEM_Code::decode_check_label(input, "EC PARAMETERS"); - *this = EC_Domain_Params(ber); + *this = EC_Group(ber); } catch(Decoding_Error) // hmm, not PEM? { - *this = EC_Domain_Params(OID(pem)); + *this = EC_Group(OIDS::lookup(str)); } } -EC_Domain_Params::EC_Domain_Params(const MemoryRegion<byte>& ber_data) +EC_Group::EC_Group(const MemoryRegion<byte>& ber_data) { BER_Decoder ber(ber_data); BER_Object obj = ber.get_next_object(); @@ -59,7 +59,7 @@ EC_Domain_Params::EC_Domain_Params(const MemoryRegion<byte>& ber_data) { OID dom_par_oid; BER_Decoder(ber_data).decode(dom_par_oid); - *this = EC_Domain_Params(dom_par_oid); + *this = EC_Group(dom_par_oid); } else if(obj.type_tag == SEQUENCE) { @@ -92,7 +92,7 @@ EC_Domain_Params::EC_Domain_Params(const MemoryRegion<byte>& ber_data) } SecureVector<byte> -EC_Domain_Params::DER_encode(EC_Domain_Params_Encoding form) const +EC_Group::DER_encode(EC_Group_Encoding form) const { if(form == EC_DOMPAR_ENC_EXPLICIT) { @@ -125,10 +125,10 @@ EC_Domain_Params::DER_encode(EC_Domain_Params_Encoding form) const else if(form == EC_DOMPAR_ENC_IMPLICITCA) return DER_Encoder().encode_null().get_contents(); else - throw Internal_Error("EC_Domain_Params::DER_encode: Unknown encoding"); + throw Internal_Error("EC_Group::DER_encode: Unknown encoding"); } -std::string EC_Domain_Params::PEM_encode() const +std::string EC_Group::PEM_encode() const { SecureVector<byte> der = DER_encode(EC_DOMPAR_ENC_EXPLICIT); return PEM_Code::encode(der, "EC PARAMETERS"); diff --git a/src/pubkey/ec_dompar/ec_dompar.h b/src/pubkey/ec_group/ec_group.h index 2508d5a2d..b7b09985e 100644 --- a/src/pubkey/ec_dompar/ec_dompar.h +++ b/src/pubkey/ec_group/ec_group.h @@ -19,7 +19,7 @@ namespace Botan { /** * This class represents elliptic curce domain parameters */ -enum EC_Domain_Params_Encoding { +enum EC_Group_Encoding { EC_DOMPAR_ENC_EXPLICIT = 0, EC_DOMPAR_ENC_IMPLICITCA = 1, EC_DOMPAR_ENC_OID = 2 @@ -28,7 +28,7 @@ enum EC_Domain_Params_Encoding { /** * Class representing an elliptic curve */ -class BOTAN_DLL EC_Domain_Params +class BOTAN_DLL EC_Group { public: @@ -39,7 +39,7 @@ class BOTAN_DLL EC_Domain_Params * @param order the order of the base point * @param cofactor the cofactor */ - EC_Domain_Params(const CurveGFp& curve, + EC_Group(const CurveGFp& curve, const PointGFp& base_point, const BigInt& order, const BigInt& cofactor) : @@ -54,27 +54,27 @@ class BOTAN_DLL EC_Domain_Params * Decode a BER encoded ECC domain parameter set * @param ber_encoding the bytes of the BER encoding */ - EC_Domain_Params(const MemoryRegion<byte>& ber_encoding); + EC_Group(const MemoryRegion<byte>& ber_encoding); /** * Create an EC domain by OID (or throw if unknown) * @param oid the OID of the EC domain to create */ - EC_Domain_Params(const OID& oid); + EC_Group(const OID& oid); /** * Create an EC domain from PEM encoding (as from PEM_encode), * or from an OID name (eg "secp16r1", or "1.3.132.0.8") * @param pem_or_oid PEM-encoded data, or an OID */ - EC_Domain_Params(const std::string& pem_or_oid = ""); + EC_Group(const std::string& pem_or_oid = ""); /** * Create the DER encoding of this domain * @param form of encoding to use * @returns bytes encododed as DER */ - SecureVector<byte> DER_encode(EC_Domain_Params_Encoding form) const; + SecureVector<byte> DER_encode(EC_Group_Encoding form) const; /** * Return the PEM encoding (always in explicit form) @@ -114,7 +114,7 @@ class BOTAN_DLL EC_Domain_Params */ std::string get_oid() const { return oid; } - bool operator==(const EC_Domain_Params& other) const + bool operator==(const EC_Group& other) const { return ((get_curve() == other.get_curve()) && (get_base_point() == other.get_base_point()) && @@ -129,12 +129,15 @@ class BOTAN_DLL EC_Domain_Params std::string oid; }; -inline bool operator!=(const EC_Domain_Params& lhs, - const EC_Domain_Params& rhs) +inline bool operator!=(const EC_Group& lhs, + const EC_Group& rhs) { return !(lhs == rhs); } +// For compatability with 1.8 +typedef EC_Group EC_Domain_Params; + } #endif diff --git a/src/pubkey/ec_dompar/info.txt b/src/pubkey/ec_group/info.txt index ae6c328e2..9a686feeb 100644 --- a/src/pubkey/ec_dompar/info.txt +++ b/src/pubkey/ec_group/info.txt @@ -1,9 +1,10 @@ -define ECC_DOMAIN_PARAMATERS +define ECC_GROUP <requires> asn1 -numbertheory -pem +ec_gfp libstate +numbertheory oid_lookup +pem </requires> diff --git a/src/pubkey/ecc_key/ecc_key.cpp b/src/pubkey/ecc_key/ecc_key.cpp index bd04e3197..991446f07 100644 --- a/src/pubkey/ecc_key/ecc_key.cpp +++ b/src/pubkey/ecc_key/ecc_key.cpp @@ -18,7 +18,7 @@ namespace Botan { -EC_PublicKey::EC_PublicKey(const EC_Domain_Params& dom_par, +EC_PublicKey::EC_PublicKey(const EC_Group& dom_par, const PointGFp& pub_point) : domain_params(dom_par), public_key(pub_point), domain_encoding(EC_DOMPAR_ENC_EXPLICIT) @@ -30,7 +30,7 @@ EC_PublicKey::EC_PublicKey(const EC_Domain_Params& dom_par, EC_PublicKey::EC_PublicKey(const AlgorithmIdentifier& alg_id, const MemoryRegion<byte>& key_bits) { - domain_params = EC_Domain_Params(alg_id.parameters); + domain_params = EC_Group(alg_id.parameters); domain_encoding = EC_DOMPAR_ENC_EXPLICIT; public_key = OS2ECP(key_bits, domain().get_curve()); @@ -52,7 +52,7 @@ MemoryVector<byte> EC_PublicKey::x509_subject_public_key() const return EC2OSP(public_point(), PointGFp::COMPRESSED); } -void EC_PublicKey::set_parameter_encoding(EC_Domain_Params_Encoding form) +void EC_PublicKey::set_parameter_encoding(EC_Group_Encoding form) { if(form != EC_DOMPAR_ENC_EXPLICIT && form != EC_DOMPAR_ENC_IMPLICITCA && @@ -76,32 +76,24 @@ const BigInt& EC_PrivateKey::private_value() const } /** -* EC_PrivateKey generator -*/ -EC_PrivateKey::EC_PrivateKey(const EC_Domain_Params& dom_par, - const BigInt& priv_key) - { - domain_params = dom_par; - domain_encoding = EC_DOMPAR_ENC_EXPLICIT; - - public_key = domain().get_base_point() * priv_key; - private_key = priv_key; - } - -/** -* EC_PrivateKey generator +* EC_PrivateKey constructor */ EC_PrivateKey::EC_PrivateKey(RandomNumberGenerator& rng, - const EC_Domain_Params& dom_par) + const EC_Group& ec_group, + const BigInt& x) { - domain_params = dom_par; + domain_params = ec_group; domain_encoding = EC_DOMPAR_ENC_EXPLICIT; - private_key = BigInt::random_integer(rng, 1, domain().get_order()); + if(x == 0) + private_key = BigInt::random_integer(rng, 1, domain().get_order()); + else + private_key = x; + public_key = domain().get_base_point() * private_key; BOTAN_ASSERT(public_key.on_the_curve(), - "generated ECC private key was not on the curve"); + "ECC private key was not on the curve"); } MemoryVector<byte> EC_PrivateKey::pkcs8_private_key() const @@ -118,7 +110,7 @@ MemoryVector<byte> EC_PrivateKey::pkcs8_private_key() const EC_PrivateKey::EC_PrivateKey(const AlgorithmIdentifier& alg_id, const MemoryRegion<byte>& key_bits) { - domain_params = EC_Domain_Params(alg_id.parameters); + domain_params = EC_Group(alg_id.parameters); domain_encoding = EC_DOMPAR_ENC_EXPLICIT; BER_Decoder(key_bits) diff --git a/src/pubkey/ecc_key/ecc_key.h b/src/pubkey/ecc_key/ecc_key.h index a20516ec6..cccc8d53c 100644 --- a/src/pubkey/ecc_key/ecc_key.h +++ b/src/pubkey/ecc_key/ecc_key.h @@ -10,7 +10,7 @@ #ifndef BOTAN_ECC_PUBLIC_KEY_BASE_H__ #define BOTAN_ECC_PUBLIC_KEY_BASE_H__ -#include <botan/ec_dompar.h> +#include <botan/ec_group.h> #include <botan/pk_keys.h> #include <botan/x509_key.h> #include <botan/pkcs8.h> @@ -18,7 +18,7 @@ namespace Botan { /** -* This class represents abstract EC Public Keys. When encoding a key +* This class represents abstract ECC public keys. When encoding a key * via an encoder that can be accessed via the corresponding member * functions, the key will decide upon its internally stored encoding * information whether to encode itself with or without domain @@ -30,7 +30,7 @@ namespace Botan { class BOTAN_DLL EC_PublicKey : public virtual Public_Key { public: - EC_PublicKey(const EC_Domain_Params& dom_par, + EC_PublicKey(const EC_Group& dom_par, const PointGFp& pub_point); EC_PublicKey(const AlgorithmIdentifier& alg_id, @@ -57,13 +57,13 @@ class BOTAN_DLL EC_PublicKey : public virtual Public_Key * domain parameters of this point are not set * @result the domain parameters of this key */ - const EC_Domain_Params& domain() const { return domain_params; } + const EC_Group& domain() const { return domain_params; } /** * Set the domain parameter encoding to be used when encoding this key. * @param enc the encoding to use */ - void set_parameter_encoding(EC_Domain_Params_Encoding enc); + void set_parameter_encoding(EC_Group_Encoding enc); /** * Return the DER encoding of this keys domain in whatever format @@ -76,28 +76,26 @@ class BOTAN_DLL EC_PublicKey : public virtual Public_Key * Get the domain parameter encoding to be used when encoding this key. * @result the encoding to use */ - EC_Domain_Params_Encoding domain_format() const + EC_Group_Encoding domain_format() const { return domain_encoding; } protected: EC_PublicKey() : domain_encoding(EC_DOMPAR_ENC_EXPLICIT) {} - EC_Domain_Params domain_params; + EC_Group domain_params; PointGFp public_key; - EC_Domain_Params_Encoding domain_encoding; + EC_Group_Encoding domain_encoding; }; /** -* This abstract class represents general EC Private Keys +* This abstract class represents ECC private keys */ class BOTAN_DLL EC_PrivateKey : public virtual EC_PublicKey, public virtual Private_Key { public: - EC_PrivateKey(const EC_Domain_Params& domain, - const BigInt& private_key); - - EC_PrivateKey(RandomNumberGenerator& rng, - const EC_Domain_Params& domain); + EC_PrivateKey(RandomNumberGenerator& rng, + const EC_Group& domain, + const BigInt& private_key); EC_PrivateKey(const AlgorithmIdentifier& alg_id, const MemoryRegion<byte>& key_bits); diff --git a/src/pubkey/ecc_key/info.txt b/src/pubkey/ecc_key/info.txt index e08a4231d..ceb98a79e 100644 --- a/src/pubkey/ecc_key/info.txt +++ b/src/pubkey/ecc_key/info.txt @@ -4,6 +4,7 @@ define ECC_PUBLIC_KEY_CRYPTO alloc asn1 bigint -ec_dompar +ec_gfp +ec_group numbertheory </requires> diff --git a/src/pubkey/ecdh/ecdh.h b/src/pubkey/ecdh/ecdh.h index 301bb1591..2edbfe86d 100644 --- a/src/pubkey/ecdh/ecdh.h +++ b/src/pubkey/ecdh/ecdh.h @@ -32,7 +32,7 @@ class BOTAN_DLL ECDH_PublicKey : public virtual EC_PublicKey * @param dom_par the domain parameters associated with this key * @param public_point the public point defining this key */ - ECDH_PublicKey(const EC_Domain_Params& dom_par, + ECDH_PublicKey(const EC_Group& dom_par, const PointGFp& public_point) : EC_PublicKey(dom_par, public_point) {} @@ -77,10 +77,12 @@ class BOTAN_DLL ECDH_PrivateKey : public ECDH_PublicKey, * Generate a new private key * @param rng a random number generator * @param domain parameters to used for this key + * @param x the private key; if zero, a new random key is generated */ ECDH_PrivateKey(RandomNumberGenerator& rng, - const EC_Domain_Params& domain) : - EC_PrivateKey(rng, domain) {} + const EC_Group& domain, + const BigInt& x = 0) : + EC_PrivateKey(rng, domain, x) {} MemoryVector<byte> public_value() const { return ECDH_PublicKey::public_value(); } diff --git a/src/pubkey/ecdh/info.txt b/src/pubkey/ecdh/info.txt index 12826c81b..7e7d50fef 100644 --- a/src/pubkey/ecdh/info.txt +++ b/src/pubkey/ecdh/info.txt @@ -3,7 +3,7 @@ define ECDH <requires> alloc asn1 -ec_dompar +ec_group ecc_key libstate numbertheory diff --git a/src/pubkey/ecdsa/ecdsa.cpp b/src/pubkey/ecdsa/ecdsa.cpp index 9a3510c33..5c45c5ed3 100644 --- a/src/pubkey/ecdsa/ecdsa.cpp +++ b/src/pubkey/ecdsa/ecdsa.cpp @@ -80,12 +80,13 @@ bool ECDSA_Verification_Operation::verify(const byte msg[], size_t msg_len, BigInt r(sig, sig_len / 2); BigInt s(sig + sig_len / 2, sig_len / 2); - if(r < 0 || r >= order || s < 0 || s >= order) + if(r <= 0 || r >= order || s <= 0 || s >= order) return false; BigInt w = inverse_mod(s, order); - PointGFp R = w * (e * base_point + r * public_point); + PointGFp R = w * multi_exponentiate(base_point, e, + public_point, r); if(R.is_zero()) return false; diff --git a/src/pubkey/ecdsa/ecdsa.h b/src/pubkey/ecdsa/ecdsa.h index 6d62a168d..f0834abd8 100644 --- a/src/pubkey/ecdsa/ecdsa.h +++ b/src/pubkey/ecdsa/ecdsa.h @@ -28,7 +28,7 @@ class BOTAN_DLL ECDSA_PublicKey : public virtual EC_PublicKey * @param dom_par the domain parameters associated with this key * @param public_point the public point defining this key */ - ECDSA_PublicKey(const EC_Domain_Params& dom_par, + ECDSA_PublicKey(const EC_Group& dom_par, const PointGFp& public_point) : EC_PublicKey(dom_par, public_point) {} @@ -66,6 +66,11 @@ class BOTAN_DLL ECDSA_PrivateKey : public ECDSA_PublicKey, { public: + /** + * Load a private key + * @param alg_id the X.509 algorithm identifier + * @param key_bits PKCS #8 structure + */ ECDSA_PrivateKey(const AlgorithmIdentifier& alg_id, const MemoryRegion<byte>& key_bits) : EC_PrivateKey(alg_id, key_bits) {} @@ -74,19 +79,12 @@ class BOTAN_DLL ECDSA_PrivateKey : public ECDSA_PublicKey, * Generate a new private key * @param rng a random number generator * @param domain parameters to used for this key + * @param x the private key (if zero, generate a ney random key) */ ECDSA_PrivateKey(RandomNumberGenerator& rng, - const EC_Domain_Params& domain) : - EC_PrivateKey(rng, domain) {} - - /** - * Load a private key - * @param domain parameters - * @param x the private key - */ - ECDSA_PrivateKey(const EC_Domain_Params& domain, - const BigInt& x) : - EC_PrivateKey(domain, x) {} + const EC_Group& domain, + const BigInt& x = 0) : + EC_PrivateKey(rng, domain, x) {} bool check_key(RandomNumberGenerator& rng, bool) const; }; diff --git a/src/pubkey/ecdsa/info.txt b/src/pubkey/ecdsa/info.txt index ca2694ad1..7a2113a30 100644 --- a/src/pubkey/ecdsa/info.txt +++ b/src/pubkey/ecdsa/info.txt @@ -2,7 +2,7 @@ define ECDSA <requires> asn1 -ec_dompar +ec_group ecc_key numbertheory rng diff --git a/src/pubkey/gost_3410/gost_3410.cpp b/src/pubkey/gost_3410/gost_3410.cpp index fa72d0673..f97f83aa0 100644 --- a/src/pubkey/gost_3410/gost_3410.cpp +++ b/src/pubkey/gost_3410/gost_3410.cpp @@ -17,8 +17,8 @@ namespace Botan { MemoryVector<byte> GOST_3410_PublicKey::x509_subject_public_key() const { // Trust CryptoPro to come up with something obnoxious - const BigInt& x = public_point().get_affine_x(); - const BigInt& y = public_point().get_affine_y(); + const BigInt x = public_point().get_affine_x(); + const BigInt y = public_point().get_affine_y(); size_t part_size = std::max(x.bytes(), y.bytes()); @@ -56,7 +56,7 @@ GOST_3410_PublicKey::GOST_3410_PublicKey(const AlgorithmIdentifier& alg_id, // Also includes hash and cipher OIDs... brilliant design guys BER_Decoder(alg_id.parameters).start_cons(SEQUENCE).decode(ecc_param_id); - domain_params = EC_Domain_Params(ecc_param_id); + domain_params = EC_Group(ecc_param_id); SecureVector<byte> bits; BER_Decoder(key_bits).decode(bits, OCTET_STRING); @@ -153,7 +153,7 @@ bool GOST_3410_Verification_Operation::verify(const byte msg[], size_t msg_len, BigInt s(sig, sig_len / 2); BigInt r(sig + sig_len / 2, sig_len / 2); - if(r < 0 || r >= order || s < 0 || s >= order) + if(r <= 0 || r >= order || s <= 0 || s >= order) return false; e %= order; @@ -165,7 +165,11 @@ bool GOST_3410_Verification_Operation::verify(const byte msg[], size_t msg_len, BigInt z1 = (s*v) % order; BigInt z2 = (-r*v) % order; - PointGFp R = (z1 * base_point + z2 * public_point); + PointGFp R = multi_exponentiate(base_point, z1, + public_point, z2); + + if(R.is_zero()) + return false; return (R.get_affine_x() == r); } diff --git a/src/pubkey/gost_3410/gost_3410.h b/src/pubkey/gost_3410/gost_3410.h index 4fb7b42c3..7b638d7b5 100644 --- a/src/pubkey/gost_3410/gost_3410.h +++ b/src/pubkey/gost_3410/gost_3410.h @@ -27,7 +27,7 @@ class BOTAN_DLL GOST_3410_PublicKey : public virtual EC_PublicKey * @param dom_par the domain parameters associated with this key * @param public_point the public point defining this key */ - GOST_3410_PublicKey(const EC_Domain_Params& dom_par, + GOST_3410_PublicKey(const EC_Group& dom_par, const PointGFp& public_point) : EC_PublicKey(dom_par, public_point) {} @@ -80,18 +80,12 @@ class BOTAN_DLL GOST_3410_PrivateKey : public GOST_3410_PublicKey, * Generate a new private key * @param rng a random number generator * @param domain parameters to used for this key + * @param x the private key; if zero, a new random key is generated */ GOST_3410_PrivateKey(RandomNumberGenerator& rng, - const EC_Domain_Params& domain) : - EC_PrivateKey(rng, domain) {} - - /** - * Load a private key - * @param domain parameters - * @param x the private key - */ - GOST_3410_PrivateKey(const EC_Domain_Params& domain, const BigInt& x) : - EC_PrivateKey(domain, x) {} + const EC_Group& domain, + const BigInt& x = 0) : + EC_PrivateKey(rng, domain, x) {} AlgorithmIdentifier pkcs8_algorithm_identifier() const { return EC_PublicKey::algorithm_identifier(); } diff --git a/src/pubkey/gost_3410/info.txt b/src/pubkey/gost_3410/info.txt index 05df445ec..9fbc3099f 100644 --- a/src/pubkey/gost_3410/info.txt +++ b/src/pubkey/gost_3410/info.txt @@ -5,7 +5,7 @@ load_on auto <requires> alloc asn1 -ec_dompar +ec_group ecc_key libstate numbertheory |