Fabcoin Core  0.16.2
P2P Digital Currency
mqv.h
Go to the documentation of this file.
1 // mqv.h - written and placed in the public domain by Wei Dai
2 
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 
17 
18 template <class GROUP_PARAMETERS, class COFACTOR_OPTION = typename GROUP_PARAMETERS::DefaultCofactorOption>
28 {
29 public:
30  typedef GROUP_PARAMETERS GroupParameters;
31  typedef typename GroupParameters::Element Element;
33 
36 
39  MQV_Domain(const GroupParameters &params)
40  : m_groupParameters(params) {}
41 
45  {m_groupParameters.BERDecode(bt);}
46 
53  template <class T1, class T2>
54  MQV_Domain(T1 v1, T2 v2)
55  {m_groupParameters.Initialize(v1, v2);}
56 
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 
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 
85  const GroupParameters & GetGroupParameters() const {return m_groupParameters;}
86 
89  GroupParameters & AccessGroupParameters() {return m_groupParameters;}
90 
93  CryptoParameters & AccessCryptoParameters() {return AccessAbstractGroupParameters();}
94 
100  unsigned int AgreedValueLength() const {return GetAbstractGroupParameters().GetEncodedElementSize(false);}
101 
105  unsigned int StaticPrivateKeyLength() const {return GetAbstractGroupParameters().GetSubgroupOrder().ByteCount();}
106 
112  unsigned int StaticPublicKeyLength() const {return GetAbstractGroupParameters().GetEncodedElementSize(true);}
113 
120  {
121  Integer x(rng, Integer::One(), GetAbstractGroupParameters().GetMaxExponent());
122  x.Encode(privateKey, StaticPrivateKeyLength());
123  }
124 
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 
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  }
201  catch (DL_BadElement &)
202  {
203  return false;
204  }
205  return true;
206  }
207 
208 private:
210  const DL_GroupParameters<Element> & GetAbstractGroupParameters() const {return m_groupParameters;}
211 
212  GroupParameters m_groupParameters;
213 };
214 
218 
220 
221 #endif
#define T1
Definition: integer.cpp:2170
MQV_Domain(T1 v1, T2 v2)
Construct a MQV domain.
Definition: mqv.h:54
uint8_t byte
Definition: Common.h:57
#define T2
Definition: integer.cpp:2171
Utility functions for the Crypto++ library.
GroupParameters m_groupParameters
Definition: mqv.h:212
void Encode(byte *output, size_t outputLen, Signedness sign=UNSIGNED) const
Encode in big-endian format.
Definition: integer.cpp:3369
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
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
Interface for Discrete Log (DL) group parameters.
Definition: pubkey.h:736
MQV_Domain(T1 v1, T2 v2, T3 v3, T4 v4)
Construct a MQV domain.
Definition: mqv.h:80
const DL_GroupParameters< Element > & GetAbstractGroupParameters() const
Definition: mqv.h:210
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.
#define R(a, b)
virtual Integer ConvertElementToInteger(const Element &element) const =0
Converts an element to an Integer.
Ring of congruence classes modulo n.
Definition: modarith.h:34
Interface for random number generators.
Definition: cryptlib.h:1188
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:1352
static const Integer &CRYPTOPP_API One()
Integer representing 1.
Definition: integer.cpp:3035
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:3305
#define x(i)
MQV domain for performing authenticated key agreement.
Definition: mqv.h:27
No cofactor multiplication applied.
Definition: pubkey.h:1917
GROUP_PARAMETERS GroupParameters
Definition: mqv.h:30
virtual void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const =0
Exponentiates a base to multiple exponents.
static Integer CRYPTOPP_API Power2(size_t e)
Exponentiates to a power of 2.
Definition: integer.cpp:3008
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:43
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
GroupParameters::Element Element
Definition: mqv.h:31
MQV_Domain< GROUP_PARAMETERS, COFACTOR_OPTION > Domain
Definition: mqv.h:32
Classes and functions for schemes based on Discrete Logs (DL) over GF(p)
virtual Element DecodeElement(const byte *encoded, bool checkForGroupMembership) const =0
Decodes the element.
Exception thrown when an invalid group element is encountered.
Definition: pubkey.h:726
virtual Integer GetCofactor() const
Retrieves the cofactor.
Definition: pubkey.h:848
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:198
DL_GroupParameters< Element > & AccessAbstractGroupParameters()
Definition: mqv.h:209
MQV_Domain(const GroupParameters &params)
Construct a MQV domain.
Definition: mqv.h:39
void * memcpy(void *a, const void *b, size_t c)
#define CRYPTOPP_UNUSED(x)
Definition: config.h:741
#define P
virtual void EncodeElement(bool reversible, const Element &element, byte *encoded) const =0
Encodes the element.
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
#define NAMESPACE_END
Definition: config.h:201
Interface for crypto prameters.
Definition: cryptlib.h:2191
virtual Integer GetMaxExponent() const =0
Retrieves the maximum exponent for the group.
CryptoParameters & AccessCryptoParameters()
Retrieves the crypto parameters for this domain.
Definition: mqv.h:93
#define e(i)
Definition: sha.cpp:733
Class file for performing modular arithmetic.
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:2727
GroupParameters & AccessGroupParameters()
Retrieves the group parameters for this domain.
Definition: mqv.h:89
#define h2(tab, w)
Definition: skipjack.cpp:74
void GenerateEphemeralPrivateKey(RandomNumberGenerator &rng, byte *privateKey) const
Generate ephemeral private key.
Definition: mqv.h:144
#define T3
Definition: integer.cpp:2172
virtual Element ExponentiateBase(const Integer &exponent) const
Retrieves the subgroup generator.
Definition: pubkey.h:803
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
Cofactor multiplication compatible with ordinary Diffie-Hellman.
Definition: pubkey.h:1921
virtual Element ExponentiateElement(const Element &base, const Integer &exponent) const
Exponentiates an element.
Definition: pubkey.h:813
virtual bool IsIdentity(const Element &element) const =0
Determines if an element is an identity.
virtual const Integer & GetSubgroupOrder() const =0
Retrieves the subgroup order.