Crypto++  5.6.5
Free C++ class library of cryptographic schemes
eccrypto.h
Go to the documentation of this file.
1 // eccrypto.h - written and placed in the public domain by Wei Dai
2 // deterministic signatures added by by Douglas Roark
3 
4 //! \file eccrypto.h
5 //! \brief Classes and functions for Elliptic Curves over prime and binary fields
6 
7 #ifndef CRYPTOPP_ECCRYPTO_H
8 #define CRYPTOPP_ECCRYPTO_H
9 
10 #include "config.h"
11 #include "cryptlib.h"
12 #include "pubkey.h"
13 #include "integer.h"
14 #include "asn.h"
15 #include "hmac.h"
16 #include "sha.h"
17 #include "gfpcrypt.h"
18 #include "dh.h"
19 #include "mqv.h"
20 #include "hmqv.h"
21 #include "fhmqv.h"
22 #include "ecp.h"
23 #include "ec2n.h"
24 
25 NAMESPACE_BEGIN(CryptoPP)
26 
27 //! \brief Elliptic Curve Parameters
28 //! \tparam EC elliptic curve field
29 //! \details This class corresponds to the ASN.1 sequence of the same name
30 //! in ANSI X9.62 and SEC 1. EC is currently defined for ECP and EC2N.
31 template <class EC>
33 {
35 
36 public:
37  typedef EC EllipticCurve;
38  typedef typename EllipticCurve::Point Point;
39  typedef Point Element;
41 
42  virtual ~DL_GroupParameters_EC() {}
43 
44  //! \brief Construct an EC GroupParameters
45  DL_GroupParameters_EC() : m_compress(false), m_encodeAsOID(true) {}
46 
47  //! \brief Construct an EC GroupParameters
48  //! \param oid the OID of a curve
50  : m_compress(false), m_encodeAsOID(true) {Initialize(oid);}
51 
52  //! \brief Construct an EC GroupParameters
53  //! \param ec the elliptic curve
54  //! \param G the base point
55  //! \param n the order of the base point
56  //! \param k the cofactor
57  DL_GroupParameters_EC(const EllipticCurve &ec, const Point &G, const Integer &n, const Integer &k = Integer::Zero())
58  : m_compress(false), m_encodeAsOID(true) {Initialize(ec, G, n, k);}
59 
60  //! \brief Construct an EC GroupParameters
61  //! \param bt BufferedTransformation with group parameters
63  : m_compress(false), m_encodeAsOID(true) {BERDecode(bt);}
64 
65  //! \brief Initialize an EC GroupParameters using {EC,G,n,k}
66  //! \param ec the elliptic curve
67  //! \param G the base point
68  //! \param n the order of the base point
69  //! \param k the cofactor
70  //! \details This Initialize() function overload initializes group parameters from existing parameters.
71  void Initialize(const EllipticCurve &ec, const Point &G, const Integer &n, const Integer &k = Integer::Zero())
72  {
73  this->m_groupPrecomputation.SetCurve(ec);
74  this->SetSubgroupGenerator(G);
75  m_n = n;
76  m_k = k;
77  }
78 
79  //! \brief Initialize a DL_GroupParameters_EC {EC,G,n,k}
80  //! \param oid the OID of a curve
81  //! \details This Initialize() function overload initializes group parameters from existing parameters.
82  void Initialize(const OID &oid);
83 
84  // NameValuePairs
85  bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
86  void AssignFrom(const NameValuePairs &source);
87 
88  // GeneratibleCryptoMaterial interface
89  //! this implementation doesn't actually generate a curve, it just initializes the parameters with existing values
90  /*! parameters: (Curve, SubgroupGenerator, SubgroupOrder, Cofactor (optional)), or (GroupOID) */
91  void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
92 
93  // DL_GroupParameters
94  const DL_FixedBasePrecomputation<Element> & GetBasePrecomputation() const {return this->m_gpc;}
96  const Integer & GetSubgroupOrder() const {return m_n;}
97  Integer GetCofactor() const;
98  bool ValidateGroup(RandomNumberGenerator &rng, unsigned int level) const;
99  bool ValidateElement(unsigned int level, const Element &element, const DL_FixedBasePrecomputation<Element> *precomp) const;
100  bool FastSubgroupCheckAvailable() const {return false;}
101  void EncodeElement(bool reversible, const Element &element, byte *encoded) const
102  {
103  if (reversible)
104  GetCurve().EncodePoint(encoded, element, m_compress);
105  else
106  element.x.Encode(encoded, GetEncodedElementSize(false));
107  }
108  virtual unsigned int GetEncodedElementSize(bool reversible) const
109  {
110  if (reversible)
111  return GetCurve().EncodedPointSize(m_compress);
112  else
113  return GetCurve().GetField().MaxElementByteLength();
114  }
115  Element DecodeElement(const byte *encoded, bool checkForGroupMembership) const
116  {
117  Point result;
118  if (!GetCurve().DecodePoint(result, encoded, GetEncodedElementSize(true)))
119  throw DL_BadElement();
120  if (checkForGroupMembership && !ValidateElement(1, result, NULL))
121  throw DL_BadElement();
122  return result;
123  }
124  Integer ConvertElementToInteger(const Element &element) const;
125  Integer GetMaxExponent() const {return GetSubgroupOrder()-1;}
126  bool IsIdentity(const Element &element) const {return element.identity;}
127  void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const;
128  static std::string CRYPTOPP_API StaticAlgorithmNamePrefix() {return "EC";}
129 
130  // ASN1Key
131  OID GetAlgorithmID() const;
132 
133  // used by MQV
134  Element MultiplyElements(const Element &a, const Element &b) const;
135  Element CascadeExponentiate(const Element &element1, const Integer &exponent1, const Element &element2, const Integer &exponent2) const;
136 
137  // non-inherited
138 
139  // enumerate OIDs for recommended parameters, use OID() to get first one
140  static OID CRYPTOPP_API GetNextRecommendedParametersOID(const OID &oid);
141 
142  void BERDecode(BufferedTransformation &bt);
143  void DEREncode(BufferedTransformation &bt) const;
144 
145  void SetPointCompression(bool compress) {m_compress = compress;}
146  bool GetPointCompression() const {return m_compress;}
147 
148  void SetEncodeAsOID(bool encodeAsOID) {m_encodeAsOID = encodeAsOID;}
149  bool GetEncodeAsOID() const {return m_encodeAsOID;}
150 
151  const EllipticCurve& GetCurve() const {return this->m_groupPrecomputation.GetCurve();}
152 
153  bool operator==(const ThisClass &rhs) const
154  {return this->m_groupPrecomputation.GetCurve() == rhs.m_groupPrecomputation.GetCurve() && this->m_gpc.GetBase(this->m_groupPrecomputation) == rhs.m_gpc.GetBase(rhs.m_groupPrecomputation);}
155 
156  //#ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY
157  //const Point& GetBasePoint() const {return this->GetSubgroupGenerator();}
158  //const Integer& GetBasePointOrder() const {return this->GetSubgroupOrder();}
159  //void LoadRecommendedParameters(const OID &oid) {Initialize(oid);}
160  //#endif
161 
162 protected:
163  unsigned int FieldElementLength() const {return GetCurve().GetField().MaxElementByteLength();}
164  unsigned int ExponentLength() const {return m_n.ByteCount();}
165 
166  OID m_oid; // set if parameters loaded from a recommended curve
167  Integer m_n; // order of base point
168  mutable Integer m_k; // cofactor
169  mutable bool m_compress, m_encodeAsOID; // presentation details
170 };
171 
172 //! \class DL_PublicKey_EC
173 //! \brief Elliptic Curve Discrete Log (DL) public key
174 //! \tparam EC elliptic curve field
175 template <class EC>
176 class DL_PublicKey_EC : public DL_PublicKeyImpl<DL_GroupParameters_EC<EC> >
177 {
178 public:
179  typedef typename EC::Point Element;
180 
181  virtual ~DL_PublicKey_EC() {}
182 
183  //! \brief Initialize an EC Public Key using {GP,Q}
184  //! \param params group parameters
185  //! \param Q the public point
186  //! \details This Initialize() function overload initializes a public key from existing parameters.
187  void Initialize(const DL_GroupParameters_EC<EC> &params, const Element &Q)
188  {this->AccessGroupParameters() = params; this->SetPublicElement(Q);}
189 
190  //! \brief Initialize an EC Public Key using {EC,G,n,Q}
191  //! \param ec the elliptic curve
192  //! \param G the base point
193  //! \param n the order of the base point
194  //! \param Q the public point
195  //! \details This Initialize() function overload initializes a public key from existing parameters.
196  void Initialize(const EC &ec, const Element &G, const Integer &n, const Element &Q)
197  {this->AccessGroupParameters().Initialize(ec, G, n); this->SetPublicElement(Q);}
198 
199  // X509PublicKey
200  void BERDecodePublicKey(BufferedTransformation &bt, bool parametersPresent, size_t size);
202 };
203 
204 //! \class DL_PrivateKey_EC
205 //! \brief Elliptic Curve Discrete Log (DL) private key
206 //! \tparam EC elliptic curve field
207 template <class EC>
208 class DL_PrivateKey_EC : public DL_PrivateKeyImpl<DL_GroupParameters_EC<EC> >
209 {
210 public:
211  typedef typename EC::Point Element;
212 
213  virtual ~DL_PrivateKey_EC() {}
214 
215  //! \brief Initialize an EC Private Key using {GP,x}
216  //! \param params group parameters
217  //! \param x the private exponent
218  //! \details This Initialize() function overload initializes a private key from existing parameters.
219  void Initialize(const DL_GroupParameters_EC<EC> &params, const Integer &x)
220  {this->AccessGroupParameters() = params; this->SetPrivateExponent(x);}
221 
222  //! \brief Initialize an EC Private Key using {EC,G,n,x}
223  //! \param ec the elliptic curve
224  //! \param G the base point
225  //! \param n the order of the base point
226  //! \param x the private exponent
227  //! \details This Initialize() function overload initializes a private key from existing parameters.
228  void Initialize(const EC &ec, const Element &G, const Integer &n, const Integer &x)
229  {this->AccessGroupParameters().Initialize(ec, G, n); this->SetPrivateExponent(x);}
230 
231  //! \brief Create an EC private key
232  //! \param rng a RandomNumberGenerator derived class
233  //! \param params the EC group parameters
234  //! \details This function overload of Initialize() creates a new private key because it
235  //! takes a RandomNumberGenerator() as a parameter. If you have an existing keypair,
236  //! then use one of the other Initialize() overloads.
238  {this->GenerateRandom(rng, params);}
239 
240  //! \brief Create an EC private key
241  //! \param rng a RandomNumberGenerator derived class
242  //! \param ec the elliptic curve
243  //! \param G the base point
244  //! \param n the order of the base point
245  //! \details This function overload of Initialize() creates a new private key because it
246  //! takes a RandomNumberGenerator() as a parameter. If you have an existing keypair,
247  //! then use one of the other Initialize() overloads.
248  void Initialize(RandomNumberGenerator &rng, const EC &ec, const Element &G, const Integer &n)
249  {this->GenerateRandom(rng, DL_GroupParameters_EC<EC>(ec, G, n));}
250 
251  // PKCS8PrivateKey
252  void BERDecodePrivateKey(BufferedTransformation &bt, bool parametersPresent, size_t size);
253  void DEREncodePrivateKey(BufferedTransformation &bt) const;
254 };
255 
256 //! \class ECDH
257 //! \brief Elliptic Curve Diffie-Hellman
258 //! \tparam EC elliptic curve field
259 //! \tparam COFACTOR_OPTION \ref CofactorMultiplicationOption "cofactor multiplication option"
260 //! \sa <a href="http://www.weidai.com/scan-mirror/ka.html#ECDH">Elliptic Curve Diffie-Hellman, AKA ECDH</a>
261 template <class EC, class COFACTOR_OPTION = typename DL_GroupParameters_EC<EC>::DefaultCofactorOption>
262 struct ECDH
263 {
264  typedef DH_Domain<DL_GroupParameters_EC<EC>, COFACTOR_OPTION> Domain;
265 };
266 
267 //! \class ECMQV
268 //! \brief Elliptic Curve Menezes-Qu-Vanstone
269 //! \tparam EC elliptic curve field
270 //! \tparam COFACTOR_OPTION \ref CofactorMultiplicationOption "cofactor multiplication option"
271 /// \sa <a href="http://www.weidai.com/scan-mirror/ka.html#ECMQV">Elliptic Curve Menezes-Qu-Vanstone, AKA ECMQV</a>
272 template <class EC, class COFACTOR_OPTION = typename DL_GroupParameters_EC<EC>::DefaultCofactorOption>
273 struct ECMQV
274 {
275  typedef MQV_Domain<DL_GroupParameters_EC<EC>, COFACTOR_OPTION> Domain;
276 };
277 
278 //! \class ECHMQV
279 //! \brief Hashed Elliptic Curve Menezes-Qu-Vanstone
280 //! \tparam EC elliptic curve field
281 //! \tparam COFACTOR_OPTION \ref CofactorMultiplicationOption "cofactor multiplication option"
282 //! \details This implementation follows Hugo Krawczyk's <a href="http://eprint.iacr.org/2005/176">HMQV: A High-Performance
283 //! Secure Diffie-Hellman Protocol</a>. Note: this implements HMQV only. HMQV-C with Key Confirmation is not provided.
284 template <class EC, class COFACTOR_OPTION = typename DL_GroupParameters_EC<EC>::DefaultCofactorOption, class HASH = SHA256>
285 struct ECHMQV
286 {
287  typedef HMQV_Domain<DL_GroupParameters_EC<EC>, COFACTOR_OPTION, HASH> Domain;
288 };
289 
291 typedef ECHMQV< ECP, DL_GroupParameters_EC< ECP >::DefaultCofactorOption, SHA256 >::Domain ECHMQV256;
292 typedef ECHMQV< ECP, DL_GroupParameters_EC< ECP >::DefaultCofactorOption, SHA384 >::Domain ECHMQV384;
293 typedef ECHMQV< ECP, DL_GroupParameters_EC< ECP >::DefaultCofactorOption, SHA512 >::Domain ECHMQV512;
294 
295 //! \class ECFHMQV
296 //! \brief Fully Hashed Elliptic Curve Menezes-Qu-Vanstone
297 //! \tparam EC elliptic curve field
298 //! \tparam COFACTOR_OPTION \ref CofactorMultiplicationOption "cofactor multiplication option"
299 //! \details This implementation follows Augustin P. Sarr and Philippe Elbaz–Vincent, and Jean–Claude Bajard's
300 //! <a href="http://eprint.iacr.org/2009/408">A Secure and Efficient Authenticated Diffie-Hellman Protocol</a>.
301 //! Note: this is FHMQV, Protocol 5, from page 11; and not FHMQV-C.
302 template <class EC, class COFACTOR_OPTION = typename DL_GroupParameters_EC<EC>::DefaultCofactorOption, class HASH = SHA256>
303 struct ECFHMQV
304 {
305  typedef FHMQV_Domain<DL_GroupParameters_EC<EC>, COFACTOR_OPTION, HASH> Domain;
306 };
307 
309 typedef ECFHMQV< ECP, DL_GroupParameters_EC< ECP >::DefaultCofactorOption, SHA256 >::Domain ECFHMQV256;
310 typedef ECFHMQV< ECP, DL_GroupParameters_EC< ECP >::DefaultCofactorOption, SHA384 >::Domain ECFHMQV384;
311 typedef ECFHMQV< ECP, DL_GroupParameters_EC< ECP >::DefaultCofactorOption, SHA512 >::Domain ECFHMQV512;
312 
313 //! \class DL_Keys_EC
314 //! \brief Elliptic Curve Discrete Log (DL) keys
315 //! \tparam EC elliptic curve field
316 template <class EC>
318 {
321 };
322 
323 // Forward declaration; documented below
324 template <class EC, class H>
325 struct ECDSA;
326 
327 //! \class DL_Keys_ECDSA
328 //! \brief Elliptic Curve DSA keys
329 //! \tparam EC elliptic curve field
330 template <class EC>
332 {
335 };
336 
337 //! \class DL_Algorithm_ECDSA
338 //! \brief Elliptic Curve DSA (ECDSA) signature algorithm
339 //! \tparam EC elliptic curve field
340 template <class EC>
341 class DL_Algorithm_ECDSA : public DL_Algorithm_GDSA<typename EC::Point>
342 {
343 public:
344  CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "ECDSA";}
345 };
346 
347 //! \class DL_Algorithm_ECDSA_RFC6979
348 //! \brief Elliptic Curve DSA (ECDSA) signature algorithm based on RFC 6979
349 //! \tparam EC elliptic curve field
350 //! \sa <a href="http://tools.ietf.org/rfc/rfc6979.txt">RFC 6979, Deterministic Usage of the
351 //! Digital Signature Algorithm (DSA) and Elliptic Curve Digital Signature Algorithm (ECDSA)</a>
352 //! \since Crypto++ 5.7
353 template <class EC, class H>
354 class DL_Algorithm_ECDSA_RFC6979 : public DL_Algorithm_DSA_RFC6979<typename EC::Point, H>
355 {
356 public:
357  CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "ECDSA-RFC6979";}
358 };
359 
360 //! \class DL_Algorithm_ECNR
361 //! \brief Elliptic Curve NR (ECNR) signature algorithm
362 //! \tparam EC elliptic curve field
363 template <class EC>
364 class DL_Algorithm_ECNR : public DL_Algorithm_NR<typename EC::Point>
365 {
366 public:
367  CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "ECNR";}
368 };
369 
370 //! \class ECDSA
371 //! \brief Elliptic Curve DSA (ECDSA) signature scheme
372 //! \tparam EC elliptic curve field
373 //! \tparam H HashTransformation derived class
374 //! \sa <a href="http://www.weidai.com/scan-mirror/sig.html#ECDSA">ECDSA</a>
375 template <class EC, class H>
376 struct ECDSA : public DL_SS<DL_Keys_ECDSA<EC>, DL_Algorithm_ECDSA<EC>, DL_SignatureMessageEncodingMethod_DSA, H>
377 {
378 };
379 
380 //! \class ECDSA_RFC6979
381 //! \brief Elliptic Curve DSA (ECDSA) deterministic signature scheme
382 //! \tparam EC elliptic curve field
383 //! \tparam H HashTransformation derived class
384 //! \sa <a href="http://tools.ietf.org/rfc/rfc6979.txt">Deterministic Usage of the
385 //! Digital Signature Algorithm (DSA) and Elliptic Curve Digital Signature Algorithm (ECDSA)</a>
386 template <class EC, class H>
387 struct ECDSA_RFC6979 : public DL_SS<
388  DL_Keys_ECDSA<EC>,
389  DL_Algorithm_ECDSA_RFC6979<EC, H>,
390  DL_SignatureMessageEncodingMethod_DSA,
391  H,
392  ECDSA_RFC6979<EC,H> >
393 {
394  static std::string CRYPTOPP_API StaticAlgorithmName() {return std::string("ECDSA-RFC6979/") + H::StaticAlgorithmName();}
395 };
396 
397 //! \class ECNR
398 //! \brief Elliptic Curve NR (ECNR) signature scheme
399 //! \tparam EC elliptic curve field
400 //! \tparam H HashTransformation derived class
401 template <class EC, class H = SHA>
402 struct ECNR : public DL_SS<DL_Keys_EC<EC>, DL_Algorithm_ECNR<EC>, DL_SignatureMessageEncodingMethod_NR, H>
403 {
404 };
405 
406 // ******************************************
407 
408 template <class EC>
410 template <class EC>
412 
413 //! \class DL_PrivateKey_ECGDSA_ISO15946
414 //! \brief Elliptic Curve German DSA key for ISO/IEC 15946
415 //! \tparam EC elliptic curve field
416 //! \sa ECGDSA_ISO15946
417 //! \since Crypto++ 5.7
418 template <class EC>
419 class DL_PrivateKey_ECGDSA_ISO15946 : public DL_PrivateKeyImpl<DL_GroupParameters_EC<EC> >
420 {
421 public:
422  typedef typename EC::Point Element;
423 
424  virtual ~DL_PrivateKey_ECGDSA_ISO15946() {}
425 
426  //! \brief Initialize an EC Private Key using {GP,x}
427  //! \param params group parameters
428  //! \param x the private exponent
429  //! \details This Initialize() function overload initializes a private key from existing parameters.
430  void Initialize(const DL_GroupParameters_EC<EC> &params, const Integer &x)
431  {this->AccessGroupParameters() = params; this->SetPrivateExponent(x);}
432 
433  //! \brief Initialize an EC Private Key using {EC,G,n,x}
434  //! \param ec the elliptic curve
435  //! \param G the base point
436  //! \param n the order of the base point
437  //! \param x the private exponent
438  //! \details This Initialize() function overload initializes a private key from existing parameters.
439  void Initialize(const EC &ec, const Element &G, const Integer &n, const Integer &x)
440  {this->AccessGroupParameters().Initialize(ec, G, n); this->SetPrivateExponent(x);}
441 
442  //! \brief Create an EC private key
443  //! \param rng a RandomNumberGenerator derived class
444  //! \param params the EC group parameters
445  //! \details This function overload of Initialize() creates a new private key because it
446  //! takes a RandomNumberGenerator() as a parameter. If you have an existing keypair,
447  //! then use one of the other Initialize() overloads.
449  {this->GenerateRandom(rng, params);}
450 
451  //! \brief Create an EC private key
452  //! \param rng a RandomNumberGenerator derived class
453  //! \param ec the elliptic curve
454  //! \param G the base point
455  //! \param n the order of the base point
456  //! \details This function overload of Initialize() creates a new private key because it
457  //! takes a RandomNumberGenerator() as a parameter. If you have an existing keypair,
458  //! then use one of the other Initialize() overloads.
459  void Initialize(RandomNumberGenerator &rng, const EC &ec, const Element &G, const Integer &n)
460  {this->GenerateRandom(rng, DL_GroupParameters_EC<EC>(ec, G, n));}
461 
462  virtual void MakePublicKey(DL_PublicKey_ECGDSA_ISO15946<EC> &pub) const
463  {
465  pub.AccessAbstractGroupParameters().AssignFrom(params);
466  const Integer &xInv = this->GetPrivateExponent().InverseMod(params.GetGroupOrder());
467  pub.SetPublicElement(params.ExponentiateBase(xInv));
468  }
469 
470  virtual bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
471  {
472  return GetValueHelper<DL_PrivateKey_ECGDSA_ISO15946<EC>,
473  DL_PrivateKey_ECGDSA_ISO15946<EC> >(this, name, valueType, pValue).Assignable();
474  }
475 
476  virtual void AssignFrom(const NameValuePairs &source)
477  {
478  AssignFromHelper<DL_PrivateKey_ECGDSA_ISO15946<EC>,
479  DL_PrivateKey_ECGDSA_ISO15946<EC> >(this, source);
480  }
481 
482  // PKCS8PrivateKey
483  void BERDecodePrivateKey(BufferedTransformation &bt, bool parametersPresent, size_t size);
484  void DEREncodePrivateKey(BufferedTransformation &bt) const;
485 };
486 
487 //! \class DL_PublicKey_ECGDSA_ISO15946
488 //! \brief Elliptic Curve German DSA key for ISO/IEC 15946
489 //! \tparam EC elliptic curve field
490 //! \sa ECGDSA_ISO15946
491 //! \since Crypto++ 5.7
492 template <class EC>
493 class DL_PublicKey_ECGDSA_ISO15946 : public DL_PublicKeyImpl<DL_GroupParameters_EC<EC> >
494 {
495  typedef DL_PublicKey_ECGDSA_ISO15946<EC> ThisClass;
496 
497 public:
498  typedef typename EC::Point Element;
499 
500  virtual ~DL_PublicKey_ECGDSA_ISO15946() {}
501 
502  //! \brief Initialize an EC Public Key using {GP,Q}
503  //! \param params group parameters
504  //! \param Q the public point
505  //! \details This Initialize() function overload initializes a public key from existing parameters.
506  void Initialize(const DL_GroupParameters_EC<EC> &params, const Element &Q)
507  {this->AccessGroupParameters() = params; this->SetPublicElement(Q);}
508 
509  //! \brief Initialize an EC Public Key using {EC,G,n,Q}
510  //! \param ec the elliptic curve
511  //! \param G the base point
512  //! \param n the order of the base point
513  //! \param Q the public point
514  //! \details This Initialize() function overload initializes a public key from existing parameters.
515  void Initialize(const EC &ec, const Element &G, const Integer &n, const Element &Q)
516  {this->AccessGroupParameters().Initialize(ec, G, n); this->SetPublicElement(Q);}
517 
518  virtual void AssignFrom(const NameValuePairs &source)
519  {
520  DL_PrivateKey_ECGDSA_ISO15946<EC> *pPrivateKey = NULL;
521  if (source.GetThisPointer(pPrivateKey))
522  pPrivateKey->MakePublicKey(*this);
523  else
524  {
525  this->AccessAbstractGroupParameters().AssignFrom(source);
526  AssignFromHelper(this, source)
527  CRYPTOPP_SET_FUNCTION_ENTRY(PublicElement);
528  }
529  }
530 
531  // DL_PublicKey<T>
532  virtual void SetPublicElement(const Element &y)
533  {this->AccessPublicPrecomputation().SetBase(this->GetAbstractGroupParameters().GetGroupPrecomputation(), y);}
534 
535  // X509PublicKey
536  void BERDecodePublicKey(BufferedTransformation &bt, bool parametersPresent, size_t size);
538 };
539 
540 //! \class DL_Keys_ECGDSA_ISO15946
541 //! \brief Elliptic Curve German DSA keys for ISO/IEC 15946
542 //! \tparam EC elliptic curve field
543 //! \sa ECGDSA_ISO15946
544 //! \since Crypto++ 5.7
545 template <class EC>
547 {
550 };
551 
552 //! \class DL_Algorithm_ECGDSA_ISO15946
553 //! \brief Elliptic Curve German DSA signature algorithm
554 //! \tparam EC elliptic curve field
555 //! \sa ECGDSA_ISO15946
556 //! \since Crypto++ 5.7
557 template <class EC>
559 {
560 public:
561  CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "ECGDSA";}
562 };
563 
564 //! \class ECGDSA
565 //! \brief Elliptic Curve German Digital Signature Algorithm signature scheme
566 //! \tparam EC elliptic curve field
567 //! \tparam H HashTransformation derived class
568 //! \sa Erwin Hess, Marcus Schafheutle, and Pascale Serf <A HREF="http://www.teletrust.de/fileadmin/files/oid/ecgdsa_final.pdf">The
569 //! Digital Signature Scheme ECGDSA (October 24, 2006)</A>
570 //! \since Crypto++ 5.7
571 template <class EC, class H>
572 struct ECGDSA : public DL_SS<
573  DL_Keys_ECGDSA_ISO15946<EC>,
574  DL_Algorithm_ECGDSA_ISO15946<EC>,
575  DL_SignatureMessageEncodingMethod_DSA,
576  H>
577 {
578  static std::string CRYPTOPP_API StaticAlgorithmName() {return std::string("ECGDSA-ISO15946/") + H::StaticAlgorithmName();}
579 };
580 
581 // ******************************************
582 
583 //! \class ECIES
584 //! \brief Elliptic Curve Integrated Encryption Scheme
585 //! \tparam COFACTOR_OPTION \ref CofactorMultiplicationOption "cofactor multiplication option"
586 //! \tparam HASH HashTransformation derived class used for key drivation and MAC computation
587 //! \tparam DHAES_MODE flag indicating if the MAC includes additional context parameters such as <em>u·V</em>, <em>v·U</em> and label
588 //! \tparam LABEL_OCTETS flag indicating if the label size is specified in octets or bits
589 //! \details ECIES is an Elliptic Curve based Integrated Encryption Scheme (IES). The scheme combines a Key Encapsulation
590 //! Method (KEM) with a Data Encapsulation Method (DEM) and a MAC tag. The scheme is
591 //! <A HREF="http://en.wikipedia.org/wiki/ciphertext_indistinguishability">IND-CCA2</A>, which is a strong notion of security.
592 //! You should prefer an Integrated Encryption Scheme over homegrown schemes.
593 //! \details The library's original implementation is based on an early P1363 draft, which itself appears to be based on an early Certicom
594 //! SEC-1 draft (or an early SEC-1 draft was based on a P1363 draft). Crypto++ 4.2 used the early draft in its Integrated Ecryption
595 //! Schemes with <tt>NoCofactorMultiplication</tt>, <tt>DHAES_MODE=false</tt> and <tt>LABEL_OCTETS=true</tt>.
596 //! \details If you desire an Integrated Encryption Scheme with Crypto++ 4.2 compatibility, then use the ECIES template class with
597 //! <tt>NoCofactorMultiplication</tt>, <tt>DHAES_MODE=false</tt> and <tt>LABEL_OCTETS=true</tt>.
598 //! \details If you desire an Integrated Encryption Scheme with Bouncy Castle 1.54 and Botan 1.11 compatibility, then use the ECIES
599 //! template class with <tt>NoCofactorMultiplication</tt>, <tt>DHAES_MODE=true</tt> and <tt>LABEL_OCTETS=false</tt>.
600 //! \details The default template parameters ensure compatibility with Bouncy Castle 1.54 and Botan 1.11. The combination of
601 //! <tt>IncompatibleCofactorMultiplication</tt> and <tt>DHAES_MODE=true</tt> is recommended for best efficiency and security.
602 //! SHA1 is used for compatibility reasons, but it can be changed if desired. SHA-256 or another hash will likely improve the
603 //! security provided by the MAC. The hash is also used in the key derivation function as a PRF.
604 //! \details Below is an example of constructing a Crypto++ 4.2 compatible ECIES encryptor and decryptor.
605 //! <pre>
606 //! AutoSeededRandomPool prng;
607 //! DL_PrivateKey_EC<ECP> key;
608 //! key.Initialize(prng, ASN1::secp160r1());
609 //!
610 //! ECIES<ECP,SHA1,NoCofactorMultiplication,true,true>::Decryptor decryptor(key);
611 //! ECIES<ECP,SHA1,NoCofactorMultiplication,true,true>::Encryptor encryptor(decryptor);
612 //! </pre>
613 //! \sa DLIES, <a href="http://www.weidai.com/scan-mirror/ca.html#ECIES">Elliptic Curve Integrated Encryption Scheme (ECIES)</a>,
614 //! Martínez, Encinas, and Ávila's <A HREF="http://digital.csic.es/bitstream/10261/32671/1/V2-I2-P7-13.pdf">A Survey of the Elliptic
615 //! Curve Integrated Encryption Schemes</A>
616 //! \since Crypto++ 4.0, Crypto++ 5.7 for Bouncy Castle and Botan compatibility
617 template <class EC, class HASH = SHA1, class COFACTOR_OPTION = NoCofactorMultiplication, bool DHAES_MODE = true, bool LABEL_OCTETS = false>
618 struct ECIES
619  : public DL_ES<
620  DL_Keys_EC<EC>,
621  DL_KeyAgreementAlgorithm_DH<typename EC::Point, COFACTOR_OPTION>,
622  DL_KeyDerivationAlgorithm_P1363<typename EC::Point, DHAES_MODE, P1363_KDF2<HASH> >,
623  DL_EncryptionAlgorithm_Xor<HMAC<HASH>, DHAES_MODE, LABEL_OCTETS>,
624  ECIES<EC> >
625 {
626  // TODO: fix this after name is standardized
627  CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "ECIES";}
628 };
629 
630 NAMESPACE_END
631 
632 #ifdef CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES
633 #include "eccrypto.cpp"
634 #endif
635 
636 NAMESPACE_BEGIN(CryptoPP)
637 
638 CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupParameters_EC<ECP>;
639 CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupParameters_EC<EC2N>;
640 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PublicKeyImpl<DL_GroupParameters_EC<ECP> >;
641 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PublicKeyImpl<DL_GroupParameters_EC<EC2N> >;
642 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PublicKey_EC<ECP>;
643 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PublicKey_EC<EC2N>;
644 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PublicKey_ECGDSA_ISO15946<ECP>;
645 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PublicKey_ECGDSA_ISO15946<EC2N>;
646 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PrivateKeyImpl<DL_GroupParameters_EC<ECP> >;
647 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PrivateKeyImpl<DL_GroupParameters_EC<EC2N> >;
648 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PrivateKey_EC<ECP>;
649 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PrivateKey_EC<EC2N>;
650 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PrivateKey_ECGDSA_ISO15946<ECP>;
651 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PrivateKey_ECGDSA_ISO15946<EC2N>;
652 CRYPTOPP_DLL_TEMPLATE_CLASS DL_Algorithm_GDSA<ECP::Point>;
653 CRYPTOPP_DLL_TEMPLATE_CLASS DL_Algorithm_GDSA<EC2N::Point>;
656 
657 NAMESPACE_END
658 
659 #endif
SHA-384 message digest.
Definition: sha.h:81
Classes for Fully Hashed Menezes-Qu-Vanstone key agreement in GF(p)
virtual void AssignFrom(const NameValuePairs &source)
Assign values to this object.
Definition: eccrypto.h:518
DL_GroupParameters_EC()
Construct an EC GroupParameters.
Definition: eccrypto.h:45
SHA-256 message digest.
Definition: sha.h:39
void Initialize(RandomNumberGenerator &rng, const EC &ec, const Element &G, const Integer &n)
Create an EC private key.
Definition: eccrypto.h:248
This file contains helper classes/functions for implementing public key algorithms.
Elliptic Curve DSA keys.
Definition: eccrypto.h:331
Classes for Elliptic Curves over prime fields.
Fully Hashed Menezes-Qu-Vanstone in GF(p)
Definition: fhmqv.h:24
Fully Hashed Elliptic Curve Menezes-Qu-Vanstone.
Definition: eccrypto.h:303
Interface for Discrete Log (DL) group parameters.
Definition: pubkey.h:736
Elliptic Curve DSA (ECDSA) signature scheme.
Definition: eccrypto.h:325
Converts an enumeration to a type suitable for use as a template parameter.
Definition: cryptlib.h:116
Abstract base classes that provide a uniform interface to this library.
void Initialize(const DL_GroupParameters_EC< EC > &params, const Element &Q)
Initialize an EC Public Key using {GP,Q}.
Definition: eccrypto.h:506
Hashed Menezes-Qu-Vanstone in GF(p)
Definition: hmqv.h:23
Elliptic Curve Discrete Log (DL) keys.
Definition: eccrypto.h:317
Elliptic Curve Discrete Log (DL) public key.
Definition: eccrypto.h:176
DL_FixedBasePrecomputation< Element > & AccessBasePrecomputation()
Retrieves the group precomputation.
Definition: eccrypto.h:95
void Initialize(const EC &ec, const Element &G, const Integer &n, const Integer &x)
Initialize an EC Private Key using {EC,G,n,x}.
Definition: eccrypto.h:439
Library configuration file.
Interface for random number generators.
Definition: cryptlib.h:1188
const DL_GroupParameters< Element > & GetAbstractGroupParameters() const
Definition: pubkey.h:1240
Elliptic Curve Discrete Log (DL) private key.
Definition: eccrypto.h:208
Discrete Log (DL) encryption scheme.
Definition: pubkey.h:2155
Interface for buffered transformations.
Definition: cryptlib.h:1352
bool operator==(const OID &lhs, const OID &rhs)
Compare two OIDs for equality.
Classes for Hashed Menezes-Qu-Vanstone key agreement in GF(p)
Elliptic Curve German Digital Signature Algorithm signature scheme.
Definition: eccrypto.h:572
Discrete Log (DL) signature scheme.
Definition: pubkey.h:2132
Classes for Elliptic Curves over binary fields.
Elliptic Curve German DSA keys for ISO/IEC 15946.
Definition: eccrypto.h:546
void Initialize(const EC &ec, const Element &G, const Integer &n, const Element &Q)
Initialize an EC Public Key using {EC,G,n,Q}.
Definition: eccrypto.h:515
Classes for HMAC message authentication codes.
DL_GroupParameters_EC(const EllipticCurve &ec, const Point &G, const Integer &n, const Integer &k=Integer::Zero())
Construct an EC GroupParameters.
Definition: eccrypto.h:57
bool GetThisPointer(T *&ptr) const
Get a pointer to this object.
Definition: cryptlib.h:324
MQV domain for performing authenticated key agreement.
Definition: mqv.h:27
Hashed Elliptic Curve Menezes-Qu-Vanstone.
Definition: eccrypto.h:285
Elliptic Curve German DSA signature algorithm.
Definition: eccrypto.h:558
Classes for Diffie-Hellman key exchange.
void Initialize(const DL_GroupParameters_EC< EC > &params, const Element &Q)
Initialize an EC Public Key using {GP,Q}.
Definition: eccrypto.h:187
SHA-512 message digest.
Definition: sha.h:69
void Initialize(const EC &ec, const Element &G, const Integer &n, const Integer &x)
Initialize an EC Private Key using {EC,G,n,x}.
Definition: eccrypto.h:228
Integer GetMaxExponent() const
Retrieves the maximum exponent for the group.
Definition: eccrypto.h:125
Elliptic Curve Menezes-Qu-Vanstone.
Definition: eccrypto.h:273
Multiple precision integer with arithmetic operations.
Definition: integer.h:43
Elliptic Curve Integrated Encryption Scheme.
Definition: eccrypto.h:618
SHA-1 message digest.
Definition: sha.h:25
Elliptic Curve NR (ECNR) signature algorithm.
Definition: eccrypto.h:364
Classes and functions for schemes based on Discrete Logs (DL) over GF(p)
void DEREncodePublicKey(BufferedTransformation &bt) const
encode subjectPublicKey part of subjectPublicKeyInfo, without the BIT STRING header ...
Definition: eccrypto.cpp:661
Elliptic Curve DSA (ECDSA) signature algorithm.
Definition: eccrypto.h:341
Exception thrown when an invalid group element is encountered.
Definition: pubkey.h:726
void Initialize(RandomNumberGenerator &rng, const EC &ec, const Element &G, const Integer &n)
Create an EC private key.
Definition: eccrypto.h:459
Diffie-Hellman domain.
Definition: dh.h:25
Elliptic Curve Diffie-Hellman.
Definition: eccrypto.h:262
DL_GroupParameters_EC(const OID &oid)
Construct an EC GroupParameters.
Definition: eccrypto.h:49
Elliptic Curve DSA (ECDSA) signature algorithm based on RFC 6979.
Definition: eccrypto.h:354
Classes and functions for working with ANS.1 objects.
Classes for SHA-1 and SHA-2 family of message digests.
Elliptic Curve Parameters.
Definition: eccrypto.h:32
void Initialize(RandomNumberGenerator &rng, const DL_GroupParameters_EC< EC > &params)
Create an EC private key.
Definition: eccrypto.h:237
DL_GroupParameters< Element > & AccessAbstractGroupParameters()
Definition: pubkey.h:1241
DSA signature algorithm based on RFC 6979.
Definition: gfpcrypt.h:236
void Initialize(const EC &ec, const Element &G, const Integer &n, const Element &Q)
Initialize an EC Public Key using {EC,G,n,Q}.
Definition: eccrypto.h:196
const DL_FixedBasePrecomputation< Element > & GetBasePrecomputation() const
Retrieves the group precomputation.
Definition: eccrypto.h:94
GDSA algorithm.
Definition: gfpcrypt.h:198
Elliptic Curve precomputation.
Definition: ec2n.h:98
virtual unsigned int GetEncodedElementSize(bool reversible) const
Retrieves the encoded element&#39;s size.
Definition: eccrypto.h:108
Elliptic Curve German DSA key for ISO/IEC 15946.
Definition: eccrypto.h:409
DL_GroupParameters_EC(BufferedTransformation &bt)
Construct an EC GroupParameters.
Definition: eccrypto.h:62
Integer InverseMod(const Integer &n) const
calculate multiplicative inverse of *this mod n
Definition: integer.cpp:4370
Element DecodeElement(const byte *encoded, bool checkForGroupMembership) const
Decodes the element.
Definition: eccrypto.h:115
NR algorithm.
Definition: gfpcrypt.h:444
Multiple precision integer with arithmetic operations.
virtual void AssignFrom(const NameValuePairs &source)
Assign values to this object.
Definition: eccrypto.h:476
static const Integer & Zero()
Integer representing 0.
Definition: integer.cpp:3027
Elliptic Curve German DSA key for ISO/IEC 15946.
Definition: eccrypto.h:411
Elliptic Curve DSA (ECDSA) deterministic signature scheme.
Definition: eccrypto.h:387
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}.
Definition: eccrypto.h:71
Crypto++ library namespace.
Base implementation of Discrete Log (DL) group parameters.
Definition: pubkey.h:947
void Initialize(RandomNumberGenerator &rng, const DL_GroupParameters_EC< EC > &params)
Create an EC private key.
Definition: eccrypto.h:448
Classes for Menezes–Qu–Vanstone (MQV) key agreement.
virtual Element ExponentiateBase(const Integer &exponent) const
Retrieves the subgroup generator.
Definition: pubkey.h:803
German Digital Signature Algorithm.
Definition: gfpcrypt.h:402
Object Identifier.
Definition: asn.h:166
const Integer & GetSubgroupOrder() const
Retrieves the subgroup order.
Definition: eccrypto.h:96
const char * PublicElement()
Integer.
Definition: argnames.h:36
void BERDecodePublicKey(BufferedTransformation &bt, bool parametersPresent, size_t size)
decode subjectPublicKey part of subjectPublicKeyInfo, without the BIT STRING header ...
Definition: eccrypto.cpp:650
void Initialize(const DL_GroupParameters_EC< EC > &params, const Integer &x)
Initialize an EC Private Key using {GP,x}.
Definition: eccrypto.h:219
virtual bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Definition: eccrypto.h:470
void Initialize(const DL_GroupParameters_EC< EC > &params, const Integer &x)
Initialize an EC Private Key using {GP,x}.
Definition: eccrypto.h:430
Elliptic Curve NR (ECNR) signature scheme.
Definition: eccrypto.h:402
virtual Integer GetGroupOrder() const
Retrieves the order of the group.
Definition: pubkey.h:843
Interface for retrieving values given their names.
Definition: cryptlib.h:279