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 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>
32 class DL_GroupParameters_EC : public DL_GroupParametersImpl<EcPrecomputation<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) */
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, NULLPTR))
121  throw DL_BadElement();
122  return result;
123  }
124  Integer ConvertElementToInteger(const Element &element) const;
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);
201  void DEREncodePublicKey(BufferedTransformation &bt) const;
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++ 6.0
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 = 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 //! \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++ 6.0
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  {
432  this->AccessGroupParameters() = params;
433  this->SetPrivateExponent(x);
434  CRYPTOPP_ASSERT(x>=1 && x<=params.GetSubgroupOrder()-1);
435  }
436 
437  //! \brief Initialize an EC Private Key using {EC,G,n,x}
438  //! \param ec the elliptic curve
439  //! \param G the base point
440  //! \param n the order of the base point
441  //! \param x the private exponent
442  //! \details This Initialize() function overload initializes a private key from existing parameters.
443  void Initialize(const EC &ec, const Element &G, const Integer &n, const Integer &x)
444  {
445  this->AccessGroupParameters().Initialize(ec, G, n);
446  this->SetPrivateExponent(x);
447  CRYPTOPP_ASSERT(x>=1 && x<=this->AccessGroupParameters().GetSubgroupOrder()-1);
448  }
449 
450  //! \brief Create an EC private key
451  //! \param rng a RandomNumberGenerator derived class
452  //! \param params the EC group parameters
453  //! \details This function overload of Initialize() creates a new private key because it
454  //! takes a RandomNumberGenerator() as a parameter. If you have an existing keypair,
455  //! then use one of the other Initialize() overloads.
457  {this->GenerateRandom(rng, params);}
458 
459  //! \brief Create an EC private key
460  //! \param rng a RandomNumberGenerator derived class
461  //! \param ec the elliptic curve
462  //! \param G the base point
463  //! \param n the order of the base point
464  //! \details This function overload of Initialize() creates a new private key because it
465  //! takes a RandomNumberGenerator() as a parameter. If you have an existing keypair,
466  //! then use one of the other Initialize() overloads.
467  void Initialize(RandomNumberGenerator &rng, const EC &ec, const Element &G, const Integer &n)
468  {this->GenerateRandom(rng, DL_GroupParameters_EC<EC>(ec, G, n));}
469 
470  virtual void MakePublicKey(DL_PublicKey_ECGDSA_ISO15946<EC> &pub) const
471  {
472  const DL_GroupParameters<Element>& params = this->GetAbstractGroupParameters();
473  pub.AccessAbstractGroupParameters().AssignFrom(params);
474  const Integer &xInv = this->GetPrivateExponent().InverseMod(params.GetSubgroupOrder());
475  pub.SetPublicElement(params.ExponentiateBase(xInv));
476  CRYPTOPP_ASSERT(xInv.NotZero());
477  }
478 
479  virtual bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
480  {
481  return GetValueHelper<DL_PrivateKey_ECGDSA_ISO15946<EC>,
482  DL_PrivateKey_ECGDSA_ISO15946<EC> >(this, name, valueType, pValue).Assignable();
483  }
484 
485  virtual void AssignFrom(const NameValuePairs &source)
486  {
487  AssignFromHelper<DL_PrivateKey_ECGDSA_ISO15946<EC>,
488  DL_PrivateKey_ECGDSA_ISO15946<EC> >(this, source);
489  }
490 
491  // PKCS8PrivateKey
492  void BERDecodePrivateKey(BufferedTransformation &bt, bool parametersPresent, size_t size);
493  void DEREncodePrivateKey(BufferedTransformation &bt) const;
494 };
495 
496 //! \class DL_PublicKey_ECGDSA_ISO15946
497 //! \brief Elliptic Curve German DSA key for ISO/IEC 15946
498 //! \tparam EC elliptic curve field
499 //! \sa ECGDSA_ISO15946
500 //! \since Crypto++ 6.0
501 template <class EC>
502 class DL_PublicKey_ECGDSA_ISO15946 : public DL_PublicKeyImpl<DL_GroupParameters_EC<EC> >
503 {
504  typedef DL_PublicKey_ECGDSA_ISO15946<EC> ThisClass;
505 
506 public:
507  typedef typename EC::Point Element;
508 
509  virtual ~DL_PublicKey_ECGDSA_ISO15946() {}
510 
511  //! \brief Initialize an EC Public Key using {GP,Q}
512  //! \param params group parameters
513  //! \param Q the public point
514  //! \details This Initialize() function overload initializes a public key from existing parameters.
515  void Initialize(const DL_GroupParameters_EC<EC> &params, const Element &Q)
516  {this->AccessGroupParameters() = params; this->SetPublicElement(Q);}
517 
518  //! \brief Initialize an EC Public Key using {EC,G,n,Q}
519  //! \param ec the elliptic curve
520  //! \param G the base point
521  //! \param n the order of the base point
522  //! \param Q the public point
523  //! \details This Initialize() function overload initializes a public key from existing parameters.
524  void Initialize(const EC &ec, const Element &G, const Integer &n, const Element &Q)
525  {this->AccessGroupParameters().Initialize(ec, G, n); this->SetPublicElement(Q);}
526 
527  virtual void AssignFrom(const NameValuePairs &source)
528  {
529  DL_PrivateKey_ECGDSA_ISO15946<EC> *pPrivateKey = NULLPTR;
530  if (source.GetThisPointer(pPrivateKey))
531  pPrivateKey->MakePublicKey(*this);
532  else
533  {
534  this->AccessAbstractGroupParameters().AssignFrom(source);
535  AssignFromHelper(this, source)
536  CRYPTOPP_SET_FUNCTION_ENTRY(PublicElement);
537  }
538  }
539 
540  // DL_PublicKey<T>
541  virtual void SetPublicElement(const Element &y)
542  {this->AccessPublicPrecomputation().SetBase(this->GetAbstractGroupParameters().GetGroupPrecomputation(), y);}
543 
544  // X509PublicKey
545  void BERDecodePublicKey(BufferedTransformation &bt, bool parametersPresent, size_t size);
546  void DEREncodePublicKey(BufferedTransformation &bt) const;
547 };
548 
549 //! \class DL_Keys_ECGDSA_ISO15946
550 //! \brief Elliptic Curve German DSA keys for ISO/IEC 15946
551 //! \tparam EC elliptic curve field
552 //! \sa ECGDSA_ISO15946
553 //! \since Crypto++ 6.0
554 template <class EC>
556 {
559 };
560 
561 //! \class DL_Algorithm_ECGDSA_ISO15946
562 //! \brief Elliptic Curve German DSA signature algorithm
563 //! \tparam EC elliptic curve field
564 //! \sa ECGDSA_ISO15946
565 //! \since Crypto++ 6.0
566 template <class EC>
568 {
569 public:
570  CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "ECGDSA";}
571 };
572 
573 //! \class ECGDSA
574 //! \brief Elliptic Curve German Digital Signature Algorithm signature scheme
575 //! \tparam EC elliptic curve field
576 //! \tparam H HashTransformation derived class
577 //! \sa Erwin Hess, Marcus Schafheutle, and Pascale Serf <A HREF="http://www.teletrust.de/fileadmin/files/oid/ecgdsa_final.pdf">The
578 //! Digital Signature Scheme ECGDSA (October 24, 2006)</A>
579 //! \since Crypto++ 6.0
580 template <class EC, class H>
581 struct ECGDSA : public DL_SS<
582  DL_Keys_ECGDSA_ISO15946<EC>,
583  DL_Algorithm_ECGDSA_ISO15946<EC>,
584  DL_SignatureMessageEncodingMethod_DSA,
585  H>
586 {
587  static std::string CRYPTOPP_API StaticAlgorithmName() {return std::string("ECGDSA-ISO15946/") + H::StaticAlgorithmName();}
588 };
589 
590 // ******************************************
591 
592 //! \class ECIES
593 //! \brief Elliptic Curve Integrated Encryption Scheme
594 //! \tparam COFACTOR_OPTION \ref CofactorMultiplicationOption "cofactor multiplication option"
595 //! \tparam HASH HashTransformation derived class used for key drivation and MAC computation
596 //! \tparam DHAES_MODE flag indicating if the MAC includes additional context parameters such as <em>u·V</em>, <em>v·U</em> and label
597 //! \tparam LABEL_OCTETS flag indicating if the label size is specified in octets or bits
598 //! \details ECIES is an Elliptic Curve based Integrated Encryption Scheme (IES). The scheme combines a Key Encapsulation
599 //! Method (KEM) with a Data Encapsulation Method (DEM) and a MAC tag. The scheme is
600 //! <A HREF="http://en.wikipedia.org/wiki/ciphertext_indistinguishability">IND-CCA2</A>, which is a strong notion of security.
601 //! You should prefer an Integrated Encryption Scheme over homegrown schemes.
602 //! \details The library's original implementation is based on an early P1363 draft, which itself appears to be based on an early Certicom
603 //! 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
604 //! Schemes with <tt>NoCofactorMultiplication</tt>, <tt>DHAES_MODE=false</tt> and <tt>LABEL_OCTETS=true</tt>.
605 //! \details If you desire an Integrated Encryption Scheme with Crypto++ 4.2 compatibility, then use the ECIES template class with
606 //! <tt>NoCofactorMultiplication</tt>, <tt>DHAES_MODE=false</tt> and <tt>LABEL_OCTETS=true</tt>.
607 //! \details If you desire an Integrated Encryption Scheme with Bouncy Castle 1.54 and Botan 1.11 compatibility, then use the ECIES
608 //! template class with <tt>NoCofactorMultiplication</tt>, <tt>DHAES_MODE=true</tt> and <tt>LABEL_OCTETS=false</tt>.
609 //! \details The default template parameters ensure compatibility with Bouncy Castle 1.54 and Botan 1.11. The combination of
610 //! <tt>IncompatibleCofactorMultiplication</tt> and <tt>DHAES_MODE=true</tt> is recommended for best efficiency and security.
611 //! SHA1 is used for compatibility reasons, but it can be changed if desired. SHA-256 or another hash will likely improve the
612 //! security provided by the MAC. The hash is also used in the key derivation function as a PRF.
613 //! \details Below is an example of constructing a Crypto++ 4.2 compatible ECIES encryptor and decryptor.
614 //! <pre>
615 //! AutoSeededRandomPool prng;
616 //! DL_PrivateKey_EC<ECP> key;
617 //! key.Initialize(prng, ASN1::secp160r1());
618 //!
619 //! ECIES<ECP,SHA1,NoCofactorMultiplication,true,true>::Decryptor decryptor(key);
620 //! ECIES<ECP,SHA1,NoCofactorMultiplication,true,true>::Encryptor encryptor(decryptor);
621 //! </pre>
622 //! \sa DLIES, <a href="http://www.weidai.com/scan-mirror/ca.html#ECIES">Elliptic Curve Integrated Encryption Scheme (ECIES)</a>,
623 //! 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
624 //! Curve Integrated Encryption Schemes</A>
625 //! \since Crypto++ 4.0, Crypto++ 5.7 for Bouncy Castle and Botan compatibility
626 template <class EC, class HASH = SHA1, class COFACTOR_OPTION = NoCofactorMultiplication, bool DHAES_MODE = true, bool LABEL_OCTETS = false>
627 struct ECIES
628  : public DL_ES<
629  DL_Keys_EC<EC>,
630  DL_KeyAgreementAlgorithm_DH<typename EC::Point, COFACTOR_OPTION>,
631  DL_KeyDerivationAlgorithm_P1363<typename EC::Point, DHAES_MODE, P1363_KDF2<HASH> >,
632  DL_EncryptionAlgorithm_Xor<HMAC<HASH>, DHAES_MODE, LABEL_OCTETS>,
633  ECIES<EC> >
634 {
635  // TODO: fix this after name is standardized
636  CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "ECIES";}
637 };
638 
639 NAMESPACE_END
640 
641 #ifdef CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES
642 #include "eccrypto.cpp"
643 #endif
644 
645 NAMESPACE_BEGIN(CryptoPP)
646 
647 CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupParameters_EC<ECP>;
648 CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupParameters_EC<EC2N>;
649 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PublicKeyImpl<DL_GroupParameters_EC<ECP> >;
650 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PublicKeyImpl<DL_GroupParameters_EC<EC2N> >;
651 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PublicKey_EC<ECP>;
652 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PublicKey_EC<EC2N>;
653 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PublicKey_ECGDSA_ISO15946<ECP>;
654 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PublicKey_ECGDSA_ISO15946<EC2N>;
655 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PrivateKeyImpl<DL_GroupParameters_EC<ECP> >;
656 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PrivateKeyImpl<DL_GroupParameters_EC<EC2N> >;
657 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PrivateKey_EC<ECP>;
658 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PrivateKey_EC<EC2N>;
659 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PrivateKey_ECGDSA_ISO15946<ECP>;
660 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PrivateKey_ECGDSA_ISO15946<EC2N>;
661 CRYPTOPP_DLL_TEMPLATE_CLASS DL_Algorithm_GDSA<ECP::Point>;
662 CRYPTOPP_DLL_TEMPLATE_CLASS DL_Algorithm_GDSA<EC2N::Point>;
665 
666 NAMESPACE_END
667 
668 #endif
SHA-384 message digest.
Definition: sha.h:82
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:527
bool ValidateGroup(RandomNumberGenerator &rng, unsigned int level) const
Check the group for errors.
Definition: eccrypto.cpp:586
DL_GroupParameters_EC()
Construct an EC GroupParameters.
Definition: eccrypto.h:45
void AssignFrom(const NameValuePairs &source)
Assign values to this object.
Definition: eccrypto.cpp:486
SHA-256 message digest.
Definition: sha.h:40
Integer GetMaxExponent() const
Retrieves the maximum exponent for the group.
Definition: eccrypto.h:125
const DL_GroupPrecomputation< Element > & GetGroupPrecomputation() const
Retrieves the group precomputation.
Definition: pubkey.h:958
virtual bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Definition: eccrypto.h:479
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.
virtual void SetSubgroupGenerator(const Element &base)
Set the subgroup generator.
Definition: pubkey.h:798
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:126
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:515
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:443
Library configuration file.
Interface for random number generators.
Definition: cryptlib.h:1188
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:507
Elliptic Curve Discrete Log (DL) private key.
Definition: eccrypto.h:208
Integer InverseMod(const Integer &n) const
calculate multiplicative inverse of *this mod n
Definition: integer.cpp:4440
Discrete Log (DL) encryption scheme.
Definition: pubkey.h:2155
const Integer & GetSubgroupOrder() const
Retrieves the subgroup order.
Definition: eccrypto.h:96
Interface for buffered transformations.
Definition: cryptlib.h:1343
virtual Element ExponentiateBase(const Integer &exponent) const
Retrieves the subgroup generator.
Definition: pubkey.h:803
Classes for Hashed Menezes-Qu-Vanstone key agreement in GF(p)
Elliptic Curve German Digital Signature Algorithm signature scheme.
Definition: eccrypto.h:581
Integer GetCofactor() const
Retrieves the cofactor.
Definition: eccrypto.cpp:567
Discrete Log (DL) signature scheme.
Definition: pubkey.h:2132
Classes for Elliptic Curves over binary fields.
unsigned int ByteCount() const
Determines the number of bytes required to represent the Integer.
Definition: integer.cpp:3362
Elliptic Curve German DSA keys for ISO/IEC 15946.
Definition: eccrypto.h:555
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:524
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:469
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
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:567
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:70
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
Elliptic Curve Menezes-Qu-Vanstone.
Definition: eccrypto.h:273
Multiple precision integer with arithmetic operations.
Definition: integer.h:49
Elliptic Curve Integrated Encryption Scheme.
Definition: eccrypto.h:627
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)
virtual unsigned int GetEncodedElementSize(bool reversible) const
Retrieves the encoded element&#39;s size.
Definition: eccrypto.h:108
Element DecodeElement(const byte *encoded, bool checkForGroupMembership) const
Decodes the element.
Definition: eccrypto.h:115
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:467
#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: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:237
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
GDSA algorithm.
Definition: gfpcrypt.h:199
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
NR algorithm.
Definition: gfpcrypt.h:448
Multiple precision integer with arithmetic operations.
virtual void AssignFrom(const NameValuePairs &source)
Assign values to this object.
Definition: eccrypto.h:485
static const Integer & Zero()
Integer representing 0.
Definition: integer.cpp:3087
Elliptic Curve German DSA key for ISO/IEC 15946.
Definition: eccrypto.h:411
Elliptic Curve DSA (ECDSA) deterministic signature scheme.
Definition: eccrypto.h:387
bool GetThisPointer(T *&ptr) const
Get a pointer to this object.
Definition: cryptlib.h:330
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.
const DL_FixedBasePrecomputation< Element > & GetBasePrecomputation() const
Retrieves the group precomputation.
Definition: eccrypto.h:94
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:456
Classes for Menezes–Qu–Vanstone (MQV) key agreement.
German Digital Signature Algorithm.
Definition: gfpcrypt.h:406
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:219
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
Interface for retrieving values given their names.
Definition: cryptlib.h:285
virtual const Integer & GetSubgroupOrder() const =0
Retrieves the subgroup order.