// eccrypto.h - written and placed in the public domain by Wei Dai //! \file eccrypto.h //! \brief Classes and functions for Elliptic Curves over prime and binary fields #ifndef CRYPTOPP_ECCRYPTO_H #define CRYPTOPP_ECCRYPTO_H #include "config.h" #include "cryptlib.h" #include "pubkey.h" #include "integer.h" #include "asn.h" #include "hmac.h" #include "sha.h" #include "gfpcrypt.h" #include "dh.h" #include "mqv.h" #include "hmqv.h" #include "fhmqv.h" #include "ecp.h" #include "ec2n.h" NAMESPACE_BEGIN(CryptoPP) //! \brief Elliptic Curve Parameters //! \tparam EC elliptic curve field //! \details This class corresponds to the ASN.1 sequence of the same name //! in ANSI X9.62 and SEC 1. EC is currently defined for ECP and EC2N. template class DL_GroupParameters_EC : public DL_GroupParametersImpl > { typedef DL_GroupParameters_EC ThisClass; public: typedef EC EllipticCurve; typedef typename EllipticCurve::Point Point; typedef Point Element; typedef IncompatibleCofactorMultiplication DefaultCofactorOption; #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 virtual ~DL_GroupParameters_EC() {} #endif DL_GroupParameters_EC() : m_compress(false), m_encodeAsOID(true) {} DL_GroupParameters_EC(const OID &oid) : m_compress(false), m_encodeAsOID(true) {Initialize(oid);} DL_GroupParameters_EC(const EllipticCurve &ec, const Point &G, const Integer &n, const Integer &k = Integer::Zero()) : m_compress(false), m_encodeAsOID(true) {Initialize(ec, G, n, k);} DL_GroupParameters_EC(BufferedTransformation &bt) : m_compress(false), m_encodeAsOID(true) {BERDecode(bt);} void Initialize(const EllipticCurve &ec, const Point &G, const Integer &n, const Integer &k = Integer::Zero()) { this->m_groupPrecomputation.SetCurve(ec); this->SetSubgroupGenerator(G); m_n = n; m_k = k; } void Initialize(const OID &oid); // NameValuePairs bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const; void AssignFrom(const NameValuePairs &source); // GeneratibleCryptoMaterial interface //! this implementation doesn't actually generate a curve, it just initializes the parameters with existing values /*! parameters: (Curve, SubgroupGenerator, SubgroupOrder, Cofactor (optional)), or (GroupOID) */ void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg); // DL_GroupParameters const DL_FixedBasePrecomputation & GetBasePrecomputation() const {return this->m_gpc;} DL_FixedBasePrecomputation & AccessBasePrecomputation() {return this->m_gpc;} const Integer & GetSubgroupOrder() const {return m_n;} Integer GetCofactor() const; bool ValidateGroup(RandomNumberGenerator &rng, unsigned int level) const; bool ValidateElement(unsigned int level, const Element &element, const DL_FixedBasePrecomputation *precomp) const; bool FastSubgroupCheckAvailable() const {return false;} void EncodeElement(bool reversible, const Element &element, byte *encoded) const { if (reversible) GetCurve().EncodePoint(encoded, element, m_compress); else element.x.Encode(encoded, GetEncodedElementSize(false)); } virtual unsigned int GetEncodedElementSize(bool reversible) const { if (reversible) return GetCurve().EncodedPointSize(m_compress); else return GetCurve().GetField().MaxElementByteLength(); } Element DecodeElement(const byte *encoded, bool checkForGroupMembership) const { Point result; if (!GetCurve().DecodePoint(result, encoded, GetEncodedElementSize(true))) throw DL_BadElement(); if (checkForGroupMembership && !ValidateElement(1, result, NULL)) throw DL_BadElement(); return result; } Integer ConvertElementToInteger(const Element &element) const; Integer GetMaxExponent() const {return GetSubgroupOrder()-1;} bool IsIdentity(const Element &element) const {return element.identity;} void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const; static std::string CRYPTOPP_API StaticAlgorithmNamePrefix() {return "EC";} // ASN1Key OID GetAlgorithmID() const; // used by MQV Element MultiplyElements(const Element &a, const Element &b) const; Element CascadeExponentiate(const Element &element1, const Integer &exponent1, const Element &element2, const Integer &exponent2) const; // non-inherited // enumerate OIDs for recommended parameters, use OID() to get first one static OID CRYPTOPP_API GetNextRecommendedParametersOID(const OID &oid); void BERDecode(BufferedTransformation &bt); void DEREncode(BufferedTransformation &bt) const; void SetPointCompression(bool compress) {m_compress = compress;} bool GetPointCompression() const {return m_compress;} void SetEncodeAsOID(bool encodeAsOID) {m_encodeAsOID = encodeAsOID;} bool GetEncodeAsOID() const {return m_encodeAsOID;} const EllipticCurve& GetCurve() const {return this->m_groupPrecomputation.GetCurve();} bool operator==(const ThisClass &rhs) const {return this->m_groupPrecomputation.GetCurve() == rhs.m_groupPrecomputation.GetCurve() && this->m_gpc.GetBase(this->m_groupPrecomputation) == rhs.m_gpc.GetBase(rhs.m_groupPrecomputation);} #ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY const Point& GetBasePoint() const {return this->GetSubgroupGenerator();} const Integer& GetBasePointOrder() const {return this->GetSubgroupOrder();} void LoadRecommendedParameters(const OID &oid) {Initialize(oid);} #endif protected: unsigned int FieldElementLength() const {return GetCurve().GetField().MaxElementByteLength();} unsigned int ExponentLength() const {return m_n.ByteCount();} OID m_oid; // set if parameters loaded from a recommended curve Integer m_n; // order of base point mutable Integer m_k; // cofactor mutable bool m_compress, m_encodeAsOID; // presentation details }; //! EC public key template class DL_PublicKey_EC : public DL_PublicKeyImpl > { public: typedef typename EC::Point Element; #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 virtual ~DL_PublicKey_EC() {} #endif void Initialize(const DL_GroupParameters_EC ¶ms, const Element &Q) {this->AccessGroupParameters() = params; this->SetPublicElement(Q);} void Initialize(const EC &ec, const Element &G, const Integer &n, const Element &Q) {this->AccessGroupParameters().Initialize(ec, G, n); this->SetPublicElement(Q);} // X509PublicKey void BERDecodePublicKey(BufferedTransformation &bt, bool parametersPresent, size_t size); void DEREncodePublicKey(BufferedTransformation &bt) const; }; //! EC private key template class DL_PrivateKey_EC : public DL_PrivateKeyImpl > { public: typedef typename EC::Point Element; #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 virtual ~DL_PrivateKey_EC() {} #endif void Initialize(const DL_GroupParameters_EC ¶ms, const Integer &x) {this->AccessGroupParameters() = params; this->SetPrivateExponent(x);} void Initialize(const EC &ec, const Element &G, const Integer &n, const Integer &x) {this->AccessGroupParameters().Initialize(ec, G, n); this->SetPrivateExponent(x);} void Initialize(RandomNumberGenerator &rng, const DL_GroupParameters_EC ¶ms) {this->GenerateRandom(rng, params);} void Initialize(RandomNumberGenerator &rng, const EC &ec, const Element &G, const Integer &n) {this->GenerateRandom(rng, DL_GroupParameters_EC(ec, G, n));} // PKCS8PrivateKey void BERDecodePrivateKey(BufferedTransformation &bt, bool parametersPresent, size_t size); void DEREncodePrivateKey(BufferedTransformation &bt) const; }; //! Elliptic Curve Diffie-Hellman, AKA ECDH template ::DefaultCofactorOption> struct ECDH { typedef DH_Domain, COFACTOR_OPTION> Domain; #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 virtual ~ECDH() {} #endif }; /// Elliptic Curve Menezes-Qu-Vanstone, AKA ECMQV template ::DefaultCofactorOption> struct ECMQV { typedef MQV_Domain, COFACTOR_OPTION> Domain; #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 virtual ~ECMQV() {} #endif }; //! \brief Hashed Menezes-Qu-Vanstone in ECP or EC2N //! \details This implementation follows Hugo Krawczyk's HMQV: A High-Performance //! Secure Diffie-Hellman Protocol. Note: this implements HMQV only. HMQV-C with Key Confirmation is not provided. template ::DefaultCofactorOption, class HASH = SHA256> struct ECHMQV { typedef HMQV_Domain, COFACTOR_OPTION, HASH> Domain; #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 virtual ~ECHMQV() {} #endif }; typedef ECHMQV< ECP, DL_GroupParameters_EC< ECP >::DefaultCofactorOption, SHA1 >::Domain ECHMQV160; typedef ECHMQV< ECP, DL_GroupParameters_EC< ECP >::DefaultCofactorOption, SHA256 >::Domain ECHMQV256; typedef ECHMQV< ECP, DL_GroupParameters_EC< ECP >::DefaultCofactorOption, SHA384 >::Domain ECHMQV384; typedef ECHMQV< ECP, DL_GroupParameters_EC< ECP >::DefaultCofactorOption, SHA512 >::Domain ECHMQV512; //! \brief Fully Hashed Menezes-Qu-Vanstone in ECP or EC2N //! \details This implementation follows Augustin P. Sarr and Philippe Elbaz–Vincent, and Jean–Claude Bajard's //! A Secure and Efficient Authenticated Diffie-Hellman Protocol. //! Note: this is FHMQV, Protocol 5, from page 11; and not FHMQV-C. template ::DefaultCofactorOption, class HASH = SHA256> struct ECFHMQV { typedef FHMQV_Domain, COFACTOR_OPTION, HASH> Domain; #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 virtual ~ECFHMQV() {} #endif }; typedef ECFHMQV< ECP, DL_GroupParameters_EC< ECP >::DefaultCofactorOption, SHA1 >::Domain ECFHMQV160; typedef ECFHMQV< ECP, DL_GroupParameters_EC< ECP >::DefaultCofactorOption, SHA256 >::Domain ECFHMQV256; typedef ECFHMQV< ECP, DL_GroupParameters_EC< ECP >::DefaultCofactorOption, SHA384 >::Domain ECFHMQV384; typedef ECFHMQV< ECP, DL_GroupParameters_EC< ECP >::DefaultCofactorOption, SHA512 >::Domain ECFHMQV512; //! EC keys template struct DL_Keys_EC { typedef DL_PublicKey_EC PublicKey; typedef DL_PrivateKey_EC PrivateKey; #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 virtual ~DL_Keys_EC() {} #endif }; template struct ECDSA; //! ECDSA keys template struct DL_Keys_ECDSA { typedef DL_PublicKey_EC PublicKey; typedef DL_PrivateKey_WithSignaturePairwiseConsistencyTest, ECDSA > PrivateKey; #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 virtual ~DL_Keys_ECDSA() {} #endif }; //! ECDSA algorithm template class DL_Algorithm_ECDSA : public DL_Algorithm_GDSA { public: CRYPTOPP_CONSTEXPR static const char * CRYPTOPP_API StaticAlgorithmName() {return "ECDSA";} #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 virtual ~DL_Algorithm_ECDSA() {} #endif }; //! ECNR algorithm template class DL_Algorithm_ECNR : public DL_Algorithm_NR { public: CRYPTOPP_CONSTEXPR static const char * CRYPTOPP_API StaticAlgorithmName() {return "ECNR";} #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 virtual ~DL_Algorithm_ECNR() {} #endif }; //! ECDSA template struct ECDSA : public DL_SS, DL_Algorithm_ECDSA, DL_SignatureMessageEncodingMethod_DSA, H> { #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 virtual ~ECDSA() {} #endif }; //! ECNR template struct ECNR : public DL_SS, DL_Algorithm_ECNR, DL_SignatureMessageEncodingMethod_NR, H> { #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 virtual ~ECNR() {} #endif }; //! Elliptic Curve Integrated Encryption Scheme, AKA ECIES /*! Default to (NoCofactorMultiplication and DHAES_MODE = false) for compatibilty with SEC1 and Crypto++ 4.2. The combination of (IncompatibleCofactorMultiplication and DHAES_MODE = true) is recommended for best efficiency and security. */ template struct ECIES : public DL_ES< DL_Keys_EC, DL_KeyAgreementAlgorithm_DH, DL_KeyDerivationAlgorithm_P1363 >, DL_EncryptionAlgorithm_Xor, DHAES_MODE>, ECIES > { static std::string CRYPTOPP_API StaticAlgorithmName() {return "ECIES";} // TODO: fix this after name is standardized #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 virtual ~ECIES() {} #endif } CRYPTOPP_DEPRECATED ("ECIES will be changing in the near future due to an interop issue"); NAMESPACE_END #ifdef CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES #include "eccrypto.cpp" #endif NAMESPACE_BEGIN(CryptoPP) CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupParameters_EC; CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupParameters_EC; CRYPTOPP_DLL_TEMPLATE_CLASS DL_PublicKeyImpl >; CRYPTOPP_DLL_TEMPLATE_CLASS DL_PublicKeyImpl >; CRYPTOPP_DLL_TEMPLATE_CLASS DL_PublicKey_EC; CRYPTOPP_DLL_TEMPLATE_CLASS DL_PublicKey_EC; CRYPTOPP_DLL_TEMPLATE_CLASS DL_PrivateKeyImpl >; CRYPTOPP_DLL_TEMPLATE_CLASS DL_PrivateKeyImpl >; CRYPTOPP_DLL_TEMPLATE_CLASS DL_PrivateKey_EC; CRYPTOPP_DLL_TEMPLATE_CLASS DL_PrivateKey_EC; CRYPTOPP_DLL_TEMPLATE_CLASS DL_Algorithm_GDSA; CRYPTOPP_DLL_TEMPLATE_CLASS DL_Algorithm_GDSA; CRYPTOPP_DLL_TEMPLATE_CLASS DL_PrivateKey_WithSignaturePairwiseConsistencyTest, ECDSA >; CRYPTOPP_DLL_TEMPLATE_CLASS DL_PrivateKey_WithSignaturePairwiseConsistencyTest, ECDSA >; NAMESPACE_END #endif