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