Crypto++
8.6
Free C++ class library of cryptographic schemes
|
Go to the documentation of this file.
7 #ifndef CRYPTOPP_ECCRYPTO_H
8 #define CRYPTOPP_ECCRYPTO_H
27 #if CRYPTOPP_MSC_VERSION
28 # pragma warning(push)
29 # pragma warning(disable: 4231 4275)
44 typedef EC EllipticCurve;
45 typedef typename EllipticCurve::Point Point;
46 typedef Point Element;
57 : m_compress(false), m_encodeAsOID(true) {
Initialize(oid);}
65 : m_compress(false), m_encodeAsOID(true) {
Initialize(ec, G, n, k);}
70 : m_compress(false), m_encodeAsOID(true) {BERDecode(bt);}
80 this->m_groupPrecomputation.SetCurve(ec);
92 bool GetVoidValue(
const char *name,
const std::type_info &valueType,
void *pValue)
const;
103 const Integer & GetSubgroupOrder()
const {
return m_n;}
107 bool FastSubgroupCheckAvailable()
const {
return false;}
108 void EncodeElement(
bool reversible,
const Element &element,
byte *encoded)
const
111 GetCurve().EncodePoint(encoded, element, m_compress);
118 return GetCurve().EncodedPointSize(m_compress);
120 return GetCurve().GetField().MaxElementByteLength();
122 Element
DecodeElement(
const byte *encoded,
bool checkForGroupMembership)
const
127 if (checkForGroupMembership && !ValidateElement(1, result, NULLPTR))
131 Integer ConvertElementToInteger(
const Element &element)
const;
132 Integer GetMaxExponent()
const {
return GetSubgroupOrder()-1;}
133 bool IsIdentity(
const Element &element)
const {
return element.identity;}
134 void SimultaneousExponentiate(Element *results,
const Element &base,
const Integer *exponents,
unsigned int exponentsCount)
const;
135 static std::string
CRYPTOPP_API StaticAlgorithmNamePrefix() {
return "EC";}
138 OID GetAlgorithmID()
const;
141 Element MultiplyElements(
const Element &a,
const Element &b)
const;
142 Element CascadeExponentiate(
const Element &element1,
const Integer &exponent1,
const Element &element2,
const Integer &exponent2)
const;
152 void SetPointCompression(
bool compress) {m_compress = compress;}
153 bool GetPointCompression()
const {
return m_compress;}
155 void SetEncodeAsOID(
bool encodeAsOID) {m_encodeAsOID = encodeAsOID;}
156 bool GetEncodeAsOID()
const {
return m_encodeAsOID;}
158 const EllipticCurve& GetCurve()
const {
return this->m_groupPrecomputation.GetCurve();}
160 bool operator==(
const ThisClass &rhs)
const
161 {
return this->m_groupPrecomputation.GetCurve() == rhs.m_groupPrecomputation.GetCurve() && this->m_gpc.GetBase(this->m_groupPrecomputation) == rhs.m_gpc.GetBase(rhs.m_groupPrecomputation);}
164 unsigned int FieldElementLength()
const {
return GetCurve().GetField().MaxElementByteLength();}
165 unsigned int ExponentLength()
const {
return m_n.
ByteCount();}
170 mutable bool m_compress, m_encodeAsOID;
173 inline std::ostream&
operator<<(std::ostream& os,
const DL_GroupParameters_EC<ECP>::Element& obj);
181 typedef typename EC::Point Element;
199 {this->AccessGroupParameters().Initialize(ec, G, n); this->
SetPublicElement(Q);}
212 typedef typename EC::Point Element;
266 template <class EC, class COFACTOR_OPTION = typename DL_GroupParameters_EC<EC>::DefaultCofactorOption>
276 template <class EC, class COFACTOR_OPTION = typename DL_GroupParameters_EC<EC>::DefaultCofactorOption>
288 template <class EC, class COFACTOR_OPTION = typename DL_GroupParameters_EC<EC>::DefaultCofactorOption,
class HASH =
SHA256>
306 template <class EC, class COFACTOR_OPTION = typename DL_GroupParameters_EC<EC>::DefaultCofactorOption,
class HASH =
SHA256>
327 template <
class EC,
class H>
347 CRYPTOPP_STATIC_CONSTEXPR
const char*
CRYPTOPP_API StaticAlgorithmName() {
return "ECDSA";}
355 template <
class EC,
class H>
359 CRYPTOPP_STATIC_CONSTEXPR
const char*
CRYPTOPP_API StaticAlgorithmName() {
return "ECDSA-RFC6979";}
368 CRYPTOPP_STATIC_CONSTEXPR
const char*
CRYPTOPP_API StaticAlgorithmName() {
return "ECNR";}
376 template <
class EC,
class H>
377 struct ECDSA :
public DL_SS<DL_Keys_ECDSA<EC>, DL_Algorithm_ECDSA<EC>, DL_SignatureMessageEncodingMethod_DSA, H>
387 template <
class EC,
class H>
390 DL_Algorithm_ECDSA_RFC6979<EC, H>,
391 DL_SignatureMessageEncodingMethod_DSA,
393 ECDSA_RFC6979<EC,H> >
395 static std::string
CRYPTOPP_API StaticAlgorithmName() {
return std::string(
"ECDSA-RFC6979/") + H::StaticAlgorithmName();}
401 template <
class EC,
class H = SHA1>
402 struct ECNR :
public DL_SS<DL_Keys_EC<EC>, DL_Algorithm_ECNR<EC>, DL_SignatureMessageEncodingMethod_NR, H>
421 typedef typename EC::Point Element;
431 this->AccessGroupParameters() = params;
432 this->SetPrivateExponent(x);
444 this->AccessGroupParameters().Initialize(ec, G, n);
445 this->SetPrivateExponent(x);
446 CRYPTOPP_ASSERT(x>=1 && x<=this->AccessGroupParameters().GetSubgroupOrder()-1);
456 {this->GenerateRandom(rng, params);}
472 pub.AccessAbstractGroupParameters().AssignFrom(params);
478 virtual bool GetVoidValue(
const char *name,
const std::type_info &valueType,
void *pValue)
const
480 return GetValueHelper<DL_PrivateKey_ECGDSA<EC>,
486 AssignFromHelper<DL_PrivateKey_ECGDSA<EC>,
505 typedef typename EC::Point Element;
514 {this->AccessGroupParameters() = params; this->SetPublicElement(Q);}
523 {this->AccessGroupParameters().Initialize(ec, G, n); this->SetPublicElement(Q);}
529 pPrivateKey->MakePublicKey(*
this);
532 this->AccessAbstractGroupParameters().AssignFrom(source);
533 AssignFromHelper(
this, source)
534 CRYPTOPP_SET_FUNCTION_ENTRY(PublicElement);
539 virtual void SetPublicElement(
const Element &y)
540 {this->AccessPublicPrecomputation().SetBase(this->GetAbstractGroupParameters().GetGroupPrecomputation(), y);}
566 CRYPTOPP_STATIC_CONSTEXPR
const char*
CRYPTOPP_API StaticAlgorithmName() {
return "ECGDSA";}
576 template <
class EC,
class H>
579 DL_Algorithm_ECGDSA<EC>,
580 DL_SignatureMessageEncodingMethod_DSA,
583 static std::string
CRYPTOPP_API StaticAlgorithmName() {
return std::string(
"ECGDSA-ISO15946/") + H::StaticAlgorithmName();}
607 template <
class EC,
class HASH = SHA1,
class COFACTOR_OPTION = NoCofactorMultiplication,
bool DHAES_MODE = true,
bool LABEL_OCTETS = false>
611 DL_KeyAgreementAlgorithm_DH<typename EC::Point, COFACTOR_OPTION>,
612 DL_KeyDerivationAlgorithm_P1363<typename EC::Point, DHAES_MODE, P1363_KDF2<HASH> >,
613 DL_EncryptionAlgorithm_Xor<HMAC<HASH>, DHAES_MODE, LABEL_OCTETS>,
617 CRYPTOPP_STATIC_CONSTEXPR
const char*
CRYPTOPP_API StaticAlgorithmName() {
return "ECIES";}
640 template <
class EC,
class HASH = SHA1,
class COFACTOR_OPTION = NoCofactorMultiplication>
644 DL_KeyAgreementAlgorithm_DH<typename EC::Point, COFACTOR_OPTION>,
645 DL_KeyDerivationAlgorithm_P1363<typename EC::Point, false, P1363_KDF2<HASH> >,
646 DL_EncryptionAlgorithm_Xor<HMAC<HASH>, false, true>,
650 CRYPTOPP_STATIC_CONSTEXPR
const char*
CRYPTOPP_API StaticAlgorithmName() {
return "ECIES-P1363";}
655 #ifdef CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES
656 #include "eccrypto.cpp"
682 #if CRYPTOPP_MSC_VERSION
683 # pragma warning(pop)
Classes for Fully Hashed Menezes-Qu-Vanstone key agreement in GF(p)
virtual void SetSubgroupGenerator(const Element &base)
Sets the subgroup generator.
void Initialize(const EC &ec, const Element &G, const Integer &n, const Element &Q)
Initialize an EC Public Key using {EC,G,n,Q}.
void AssignFrom(const NameValuePairs &source)
Assign values to this object.
void SetPrivateExponent(const Integer &x)
Integer InverseMod(const Integer &n) const
Calculate multiplicative inverse.
Converts an enumeration to a type suitable for use as a template parameter.
Classes for Elliptic Curves over prime fields.
Discrete Log (DL) private key base implementation.
Elliptic Curve Discrete Log (DL) public key.
Elliptic Curve Menezes-Qu-Vanstone.
Elliptic Curve NR (ECNR) signature scheme.
bool NotZero() const
Determines if the Integer is non-0.
void Initialize(RandomNumberGenerator &rng, const DL_GroupParameters_EC< EC > ¶ms)
Create an EC private key.
bool ValidateGroup(RandomNumberGenerator &rng, unsigned int level) const
Check the group for errors.
Discrete Log (DL) encryption scheme.
void Initialize(const DL_GroupParameters_EC< EC > ¶ms, const Element &Q)
Initialize an EC Public Key using {GP,Q}.
Elliptic Curve Integrated Encryption Scheme.
DL_GroupParameters_EC()
Construct an EC GroupParameters.
Discrete Log (DL) signature scheme.
DL_FixedBasePrecomputation< Element > & AccessBasePrecomputation()
Retrieves the group precomputation.
void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg)
this implementation doesn't actually generate a curve, it just initializes the parameters with existi...
Elliptic Curve DSA (ECDSA) signature scheme.
DL_FixedBasePrecomputation interface.
Fully Hashed Elliptic Curve Menezes-Qu-Vanstone.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
virtual Element ExponentiateBase(const Integer &exponent) const
Exponentiates the base.
This file contains helper classes/functions for implementing public key algorithms.
Fully Hashed Menezes-Qu-Vanstone in GF(p)
Classes and functions for schemes based on Discrete Logs (DL) over GF(p)
void Initialize(const EC &ec, const Element &G, const Integer &n, const Integer &x)
Initialize an EC Private Key using {EC,G,n,x}.
virtual void SetPublicElement(const Element &y)
Sets the public element.
void DEREncodePublicKey(BufferedTransformation &bt) const
Encode subjectPublicKey part of subjectPublicKeyInfo.
void Initialize(RandomNumberGenerator &rng, const DL_GroupParameters_EC< EC > ¶ms)
Create an EC private key.
Elliptic Curve Integrated Encryption Scheme for P1363.
Elliptic Curve Parameters.
Classes for Diffie-Hellman key exchange.
DL_GroupParameters_EC(const EllipticCurve &ec, const Point &G, const Integer &n, const Integer &k=Integer::Zero())
Construct an EC GroupParameters.
Interface for random number generators.
unsigned int ByteCount() const
Determines the number of bytes required to represent the Integer.
Elliptic Curve DSA (ECDSA) signature algorithm.
Hashed Elliptic Curve Menezes-Qu-Vanstone.
DSA signature algorithm based on RFC 6979.
Classes for SHA-1 and SHA-2 family of message digests.
void Initialize(const EC &ec, const Element &G, const Integer &n, const Element &Q)
Initialize an EC Public Key using {EC,G,n,Q}.
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
MQV domain for performing authenticated key agreement.
Hashed Menezes-Qu-Vanstone in GF(p)
virtual unsigned int GetEncodedElementSize(bool reversible) const
Retrieves the encoded element's size.
static const Integer & Zero()
Integer representing 0.
Classes and functions for working with ANS.1 objects.
void Initialize(const DL_GroupParameters_EC< EC > ¶ms, const Element &Q)
Initialize an EC Public Key using {GP,Q}.
Elliptic Curve DSA (ECDSA) deterministic signature scheme.
Elliptic Curve Discrete Log (DL) private key.
void Initialize(RandomNumberGenerator &rng, const EC &ec, const Element &G, const Integer &n)
Create an EC private key.
Elliptic Curve Diffie-Hellman.
void Initialize(const DL_GroupParameters_EC< EC > ¶ms, const Integer &x)
Initialize an EC Private Key using {GP,x}.
void BERDecodePrivateKey(BufferedTransformation &bt, bool parametersPresent, size_t size)
Decode privateKey part of privateKeyInfo.
void Initialize(const DL_GroupParameters_EC< EC > ¶ms, const Integer &x)
Initialize an EC Private Key using {GP,x}.
Element DecodeElement(const byte *encoded, bool checkForGroupMembership) const
Decodes the element.
Classes for Menezes–Qu–Vanstone (MQV) key agreement.
std::ostream & operator<<(std::ostream &out, const OID &oid)
Print a OID value.
DL_GroupParameters_EC(const OID &oid)
Construct an EC GroupParameters.
void Initialize(RandomNumberGenerator &rng, const EC &ec, const Element &G, const Integer &n)
Create an EC private key.
DL_GroupParameters_EC(BufferedTransformation &bt)
Construct an EC GroupParameters.
void Initialize(const EC &ec, const Element &G, const Integer &n, const Integer &x)
Initialize an EC Private Key using {EC,G,n,x}.
void Initialize(const EllipticCurve &ec, const Point &G, const Integer &n, const Integer &k=Integer::Zero())
Initialize an EC GroupParameters using {EC,G,n,k}.
Crypto++ library namespace.
Elliptic Curve German DSA signature algorithm.
void BERDecodePublicKey(BufferedTransformation &bt, bool parametersPresent, size_t size)
Decode subjectPublicKey part of subjectPublicKeyInfo.
#define CRYPTOPP_API
Win32 calling convention.
#define CRYPTOPP_DLL_TEMPLATE_CLASS
Instantiate templates in a dynamic library.
void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs ¶ms)
Elliptic Curve German DSA key for ISO/IEC 15946.
Elliptic Curve German DSA keys for ISO/IEC 15946.
Elliptic Curve Discrete Log (DL) keys.
Library configuration file.
virtual const Integer & GetSubgroupOrder() const =0
Retrieves the subgroup order.
Elliptic Curve DSA (ECDSA) signature algorithm based on RFC 6979.
German Digital Signature Algorithm.
Interface for Discrete Log (DL) group parameters.
Exception thrown when an invalid group element is encountered.
Classes for Hashed Menezes-Qu-Vanstone key agreement in GF(p)
void DEREncodePrivateKey(BufferedTransformation &bt) const
Encode privateKey part of privateKeyInfo.
Base implementation of Discrete Log (DL) group parameters.
Classes for Elliptic Curves over binary fields.
Elliptic Curve German Digital Signature Algorithm signature scheme.
Interface for retrieving values given their names.
Abstract base classes that provide a uniform interface to this library.
bool GetThisPointer(T *&ptr) const
Get a pointer to this object.
Multiple precision integer with arithmetic operations.
Classes for HMAC message authentication codes.
Elliptic Curve German DSA key for ISO/IEC 15946.
Multiple precision integer with arithmetic operations.
Elliptic Curve NR (ECNR) signature algorithm.
Discrete Log (DL) public key base implementation.