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