Crypto++  5.6.4 Free C++ class library of cryptographic schemes
mqv.h
Go to the documentation of this file.
1 // mqv.h - written and placed in the public domain by Wei Dai
2
3 //! \file mqv.h
4 //! \brief Classes for Menezesâ€“Quâ€“Vanstone (MQV) key agreement
5
6 #ifndef CRYPTOPP_MQV_H
7 #define CRYPTOPP_MQV_H
8
9 #include "cryptlib.h"
10 #include "gfpcrypt.h"
11 #include "modarith.h"
12 #include "integer.h"
13 #include "algebra.h"
14 #include "misc.h"
15
16 NAMESPACE_BEGIN(CryptoPP)
17
18 //! \class MQV_Domain
19 //! \brief MQV domain for performing authenticated key agreement
20 //! \tparam GROUP_PARAMETERS doamin parameters
21 //! \tparam COFACTOR_OPTION cofactor option
22 //! \details GROUP_PARAMETERS paramters include the curve coefcients and the base point.
23 //! Binary curves use a polynomial to represent its characteristic, while prime curves
24 //! use a prime number.
25 //! \sa MQV, HMQV, FHMQV, and AuthenticatedKeyAgreementDomain
26 template <class GROUP_PARAMETERS, class COFACTOR_OPTION = CPP_TYPENAME GROUP_PARAMETERS::DefaultCofactorOption>
28 {
29 public:
30  typedef GROUP_PARAMETERS GroupParameters;
31  typedef typename GroupParameters::Element Element;
33
34  //! \brief Construct a MQV domain
36
37  //! \brief Construct a MQV domain
38  //! \param params group parameters and options
39  MQV_Domain(const GroupParameters &params)
40  : m_groupParameters(params) {}
41
42  //! \brief Construct a MQV domain
43  //! \param bt BufferedTransformation with group parameters and options
45  {m_groupParameters.BERDecode(bt);}
46
47  //! \brief Construct a MQV domain
48  //! \tparam T1 template parameter used as a constructor parameter
49  //! \tparam T2 template parameter used as a constructor parameter
50  //! \param v1 first parameter
51  //! \param v2 second parameter
52  //! \details v1 and v2 are passed directly to the GROUP_PARAMETERS object.
53  template <class T1, class T2>
54  MQV_Domain(T1 v1, T2 v2)
55  {m_groupParameters.Initialize(v1, v2);}
56
57  //! \brief Construct a MQV domain
58  //! \tparam T1 template parameter used as a constructor parameter
59  //! \tparam T2 template parameter used as a constructor parameter
60  //! \tparam T3 template parameter used as a constructor parameter
61  //! \param v1 first parameter
62  //! \param v2 second parameter
63  //! \param v3 third parameter
64  //! \details v1, v2 and v3 are passed directly to the GROUP_PARAMETERS object.
65  template <class T1, class T2, class T3>
66  MQV_Domain(T1 v1, T2 v2, T3 v3)
67  {m_groupParameters.Initialize(v1, v2, v3);}
68
69  //! \brief Construct a MQV domain
70  //! \tparam T1 template parameter used as a constructor parameter
71  //! \tparam T2 template parameter used as a constructor parameter
72  //! \tparam T3 template parameter used as a constructor parameter
73  //! \tparam T4 template parameter used as a constructor parameter
74  //! \param v1 first parameter
75  //! \param v2 second parameter
76  //! \param v3 third parameter
77  //! \param v4 third parameter
78  //! \details v1, v2, v3 and v4 are passed directly to the GROUP_PARAMETERS object.
79  template <class T1, class T2, class T3, class T4>
80  MQV_Domain(T1 v1, T2 v2, T3 v3, T4 v4)
81  {m_groupParameters.Initialize(v1, v2, v3, v4);}
82
83  //! \brief Retrieves the group parameters for this domain
84  //! \return the group parameters for this domain as a const reference
85  const GroupParameters & GetGroupParameters() const {return m_groupParameters;}
86
87  //! \brief Retrieves the group parameters for this domain
88  //! \return the group parameters for this domain as a non-const reference
89  GroupParameters & AccessGroupParameters() {return m_groupParameters;}
90
91  //! \brief Retrieves the crypto parameters for this domain
92  //! \return the crypto parameters for this domain as a non-const reference
93  CryptoParameters & AccessCryptoParameters() {return AccessAbstractGroupParameters();}
94
95  //! \brief Provides the size of the agreed value
96  //! \return size of agreed value produced in this domain
97  //! \details The length is calculated using <tt>GetEncodedElementSize(false)</tt>, which means the
98  //! element is encoded in a non-reversible format. A non-reversible format means its a raw byte array,
99  //! and it lacks presentation format like an ASN.1 BIT_STRING or OCTET_STRING.
100  unsigned int AgreedValueLength() const {return GetAbstractGroupParameters().GetEncodedElementSize(false);}
101
102  //! \brief Provides the size of the static private key
103  //! \return size of static private keys in this domain
104  //! \details The length is calculated using the byte count of the subgroup order.
105  unsigned int StaticPrivateKeyLength() const {return GetAbstractGroupParameters().GetSubgroupOrder().ByteCount();}
106
107  //! \brief Provides the size of the static public key
108  //! \return size of static public keys in this domain
109  //! \details The length is calculated using <tt>GetEncodedElementSize(true)</tt>, which means the
110  //! element is encoded in a reversible format. A reversible format means it has a presentation format,
111  //! and its an ANS.1 encoded element or point.
112  unsigned int StaticPublicKeyLength() const {return GetAbstractGroupParameters().GetEncodedElementSize(true);}
113
114  //! \brief Generate static private key in this domain
115  //! \param rng a RandomNumberGenerator derived class
116  //! \param privateKey a byte buffer for the generated private key in this domain
117  //! \details The private key is a random scalar used as an exponent in the range <tt>[1,MaxExponent()]</tt>.
118  //! \pre <tt>COUNTOF(privateKey) == PrivateStaticKeyLength()</tt>
119  void GenerateStaticPrivateKey(RandomNumberGenerator &rng, byte *privateKey) const
120  {
121  Integer x(rng, Integer::One(), GetAbstractGroupParameters().GetMaxExponent());
122  x.Encode(privateKey, StaticPrivateKeyLength());
123  }
124
125  //! \brief Generate a static public key from a private key in this domain
126  //! \param rng a RandomNumberGenerator derived class
127  //! \param privateKey a byte buffer with the previously generated private key
128  //! \param publicKey a byte buffer for the generated public key in this domain
129  //! \details The public key is an element or point on the curve, and its stored in a revrsible format.
130  //! A reversible format means it has a presentation format, and its an ANS.1 encoded element or point.
131  //! \pre <tt>COUNTOF(publicKey) == PublicStaticKeyLength()</tt>
132  void GenerateStaticPublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
133  {
134  CRYPTOPP_UNUSED(rng);
135  const DL_GroupParameters<Element> &params = GetAbstractGroupParameters();
136  Integer x(privateKey, StaticPrivateKeyLength());
137  Element y = params.ExponentiateBase(x);
138  params.EncodeElement(true, y, publicKey);
139  }
140
141  unsigned int EphemeralPrivateKeyLength() const {return StaticPrivateKeyLength() + StaticPublicKeyLength();}
142  unsigned int EphemeralPublicKeyLength() const {return StaticPublicKeyLength();}
143
144  void GenerateEphemeralPrivateKey(RandomNumberGenerator &rng, byte *privateKey) const
145  {
146  const DL_GroupParameters<Element> &params = GetAbstractGroupParameters();
147  Integer x(rng, Integer::One(), params.GetMaxExponent());
148  x.Encode(privateKey, StaticPrivateKeyLength());
149  Element y = params.ExponentiateBase(x);
150  params.EncodeElement(true, y, privateKey+StaticPrivateKeyLength());
151  }
152
153  void GenerateEphemeralPublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
154  {
155  CRYPTOPP_UNUSED(rng);
156  memcpy(publicKey, privateKey+StaticPrivateKeyLength(), EphemeralPublicKeyLength());
157  }
158
159  bool Agree(byte *agreedValue,
160  const byte *staticPrivateKey, const byte *ephemeralPrivateKey,
161  const byte *staticOtherPublicKey, const byte *ephemeralOtherPublicKey,
162  bool validateStaticOtherPublicKey=true) const
163  {
164  try
165  {
166  const DL_GroupParameters<Element> &params = GetAbstractGroupParameters();
167  Element WW = params.DecodeElement(staticOtherPublicKey, validateStaticOtherPublicKey);
168  Element VV = params.DecodeElement(ephemeralOtherPublicKey, true);
169
170  Integer s(staticPrivateKey, StaticPrivateKeyLength());
171  Integer u(ephemeralPrivateKey, StaticPrivateKeyLength());
172  Element V = params.DecodeElement(ephemeralPrivateKey+StaticPrivateKeyLength(), false);
173
174  const Integer &r = params.GetSubgroupOrder();
175  Integer h2 = Integer::Power2((r.BitCount()+1)/2);
176  Integer e = ((h2+params.ConvertElementToInteger(V)%h2)*s+u) % r;
177  Integer tt = h2 + params.ConvertElementToInteger(VV) % h2;
178
179  if (COFACTOR_OPTION::ToEnum() == NO_COFACTOR_MULTIPLICTION)
180  {
181  Element P = params.ExponentiateElement(WW, tt);
182  P = m_groupParameters.MultiplyElements(P, VV);
183  Element R[2];
184  const Integer e2[2] = {r, e};
185  params.SimultaneousExponentiate(R, P, e2, 2);
186  if (!params.IsIdentity(R[0]) || params.IsIdentity(R[1]))
187  return false;
188  params.EncodeElement(false, R[1], agreedValue);
189  }
190  else
191  {
192  const Integer &k = params.GetCofactor();
193  if (COFACTOR_OPTION::ToEnum() == COMPATIBLE_COFACTOR_MULTIPLICTION)
194  e = ModularArithmetic(r).Divide(e, k);
195  Element P = m_groupParameters.CascadeExponentiate(VV, k*e, WW, k*(e*tt%r));
196  if (params.IsIdentity(P))
197  return false;
198  params.EncodeElement(false, P, agreedValue);
199  }
200  }
202  {
203  return false;
204  }
205  return true;
206  }
207
208 private:
209  DL_GroupParameters<Element> & AccessAbstractGroupParameters() {return m_groupParameters;}
210  const DL_GroupParameters<Element> & GetAbstractGroupParameters() const {return m_groupParameters;}
211
212  GroupParameters m_groupParameters;
213 };
214
215 //! Menezes-Qu-Vanstone in GF(p) with key validation, AKA <a href="http://www.weidai.com/scan-mirror/ka.html#MQV">MQV</a>
216 //! \sa MQV, HMQV_Domain, FHMQV_Domain, AuthenticatedKeyAgreementDomain
218
219 NAMESPACE_END
220
221 #endif
virtual void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const =0
Exponentiates a base to multiple exponents.
MQV_Domain(T1 v1, T2 v2)
Construct a MQV domain.
Definition: mqv.h:54
Utility functions for the Crypto++ library.
void Encode(byte *output, size_t outputLen, Signedness sign=UNSIGNED) const
Encode in big-endian format.
Definition: integer.cpp:3341
void GenerateStaticPublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
Generate a static public key from a private key in this domain.
Definition: mqv.h:132
virtual Integer ConvertElementToInteger(const Element &element) const =0
Converts an element to an Integer.
Interface for Discrete Log (DL) group parameters.
Definition: pubkey.h:758
MQV_Domain(T1 v1, T2 v2, T3 v3, T4 v4)
Construct a MQV domain.
Definition: mqv.h:80
unsigned int EphemeralPublicKeyLength() const
Provides the size of ephemeral public key.
Definition: mqv.h:142
Abstract base classes that provide a uniform interface to this library.
virtual Integer GetMaxExponent() const =0
Retrieves the maximum exponent for the group.
Ring of congruence classes modulo n.
Definition: modarith.h:34
Interface for random number generators.
Definition: cryptlib.h:1193
MQV_Domain(BufferedTransformation &bt)
Construct a MQV domain.
Definition: mqv.h:44
Classes for performing mathematics over different fields.
Interface for buffered transformations.
Definition: cryptlib.h:1359
static const Integer & One()
Integer representing 1.
Definition: integer.cpp:3019
MQV_Domain< DL_GroupParameters_GFP_DefaultSafePrime > MQV
Menezes-Qu-Vanstone in GF(p) with key validation, AKA MQV
Definition: mqv.h:217
unsigned int BitCount() const
Determines the number of bits required to represent the Integer.
Definition: integer.cpp:3280
virtual void EncodeElement(bool reversible, const Element &element, byte *encoded) const =0
Encodes the element.
Cofactor multiplication compatible with ordinary Diffie-Hellman.
Definition: pubkey.h:1967
MQV domain for performing authenticated key agreement.
Definition: mqv.h:27
No cofactor multiplication applied.
Definition: pubkey.h:1963
static Integer Power2(size_t e)
Exponentiates to a power of 2.
Definition: integer.cpp:2998
unsigned int EphemeralPrivateKeyLength() const
Provides the size of ephemeral private key.
Definition: mqv.h:141
unsigned int StaticPrivateKeyLength() const
Provides the size of the static private key.
Definition: mqv.h:105
Multiple precision integer with arithmetic operations.
Definition: integer.h:45
unsigned int AgreedValueLength() const
Provides the size of the agreed value.
Definition: mqv.h:100
void GenerateStaticPrivateKey(RandomNumberGenerator &rng, byte *privateKey) const
Generate static private key in this domain.
Definition: mqv.h:119
Implementation of schemes based on DL over GF(p)
virtual const Integer & GetSubgroupOrder() const =0
Retrieves the subgroup order.
virtual bool IsIdentity(const Element &element) const =0
Determines if an element is an identity.
Exception thrown when an invalid group element is encountered.
Definition: pubkey.h:748
virtual Integer GetCofactor() const
Retrieves the cofactor.
Definition: pubkey.h:872
void GenerateEphemeralPublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
Generate ephemeral public key.
Definition: mqv.h:153
const Integer & Divide(const Integer &a, const Integer &b) const
Divides elements in the ring.
Definition: modarith.h:200
virtual Element DecodeElement(const byte *encoded, bool checkForGroupMembership) const =0
Decodes the element.
MQV_Domain(const GroupParameters &params)
Construct a MQV domain.
Definition: mqv.h:39
Multiple precision integer with arithmetic operations.
MQV_Domain(T1 v1, T2 v2, T3 v3)
Construct a MQV domain.
Definition: mqv.h:66
MQV_Domain()
Construct a MQV domain.
Definition: mqv.h:35
Interface for crypto prameters.
Definition: cryptlib.h:2203
CryptoParameters & AccessCryptoParameters()
Retrieves the crypto parameters for this domain.
Definition: mqv.h:93
Class file for performing modular arithmetic.
Crypto++ library namespace.
const GroupParameters & GetGroupParameters() const
Retrieves the group parameters for this domain.
Definition: mqv.h:85
Interface for domains of authenticated key agreement protocols.
Definition: cryptlib.h:2754
GroupParameters & AccessGroupParameters()
Retrieves the group parameters for this domain.
Definition: mqv.h:89
void GenerateEphemeralPrivateKey(RandomNumberGenerator &rng, byte *privateKey) const
Generate ephemeral private key.
Definition: mqv.h:144
virtual Element ExponentiateBase(const Integer &exponent) const
Retrieves the subgroup generator.
Definition: pubkey.h:827
bool Agree(byte *agreedValue, const byte *staticPrivateKey, const byte *ephemeralPrivateKey, const byte *staticOtherPublicKey, const byte *ephemeralOtherPublicKey, bool validateStaticOtherPublicKey=true) const
Derive agreed value.
Definition: mqv.h:159
unsigned int StaticPublicKeyLength() const
Provides the size of the static public key.
Definition: mqv.h:112
virtual Element ExponentiateElement(const Element &base, const Integer &exponent) const
Exponentiates an element.
Definition: pubkey.h:837