4 #ifndef CRYPTOPP_FHMQV_H 5 #define CRYPTOPP_FHMQV_H 17 template <class GROUP_PARAMETERS, class COFACTOR_OPTION = typename GROUP_PARAMETERS::DefaultCofactorOption, class HASH =
SHA512>
28 typedef typename GroupParameters::Element
Element;
33 FHMQV_Domain(
bool clientRole =
true): m_role(clientRole ? RoleClient : RoleServer) {}
35 FHMQV_Domain(
const GroupParameters ¶ms,
bool clientRole =
true)
36 : m_role(clientRole ? RoleClient : RoleServer), m_groupParameters(params) {}
39 : m_role(clientRole ? RoleClient : RoleServer)
40 {m_groupParameters.BERDecode(bt);}
44 : m_role(clientRole ? RoleClient : RoleServer)
45 {m_groupParameters.Initialize(v1);}
47 template <
class T1,
class T2>
49 : m_role(clientRole ? RoleClient : RoleServer)
50 {m_groupParameters.Initialize(v1, v2);}
52 template <
class T1,
class T2,
class T3>
54 : m_role(clientRole ? RoleClient : RoleServer)
55 {m_groupParameters.Initialize(v1, v2, v3);}
57 template <
class T1,
class T2,
class T3,
class T4>
59 : m_role(clientRole ? RoleClient : RoleServer)
60 {m_groupParameters.Initialize(v1, v2, v3, v4);}
70 unsigned int AgreedValueLength()
const {
return GetAbstractGroupParameters().GetEncodedElementSize(
false);}
81 x.
Encode(privateKey, StaticPrivateKeyLength());
90 Integer x(privateKey, StaticPrivateKeyLength());
103 x.Encode(privateKey, StaticPrivateKeyLength());
105 params.
EncodeElement(
true, y, privateKey+StaticPrivateKeyLength());
112 memcpy(publicKey, privateKey+StaticPrivateKeyLength(), EphemeralPublicKeyLength());
125 const byte *staticPrivateKey,
const byte *ephemeralPrivateKey,
126 const byte *staticOtherPublicKey,
const byte *ephemeralOtherPublicKey,
127 bool validateStaticOtherPublicKey=
true)
const 129 byte *XX = NULL, *YY = NULL, *AA = NULL, *BB = NULL;
130 size_t xxs = 0, yys = 0, aas = 0, bbs = 0;
140 if(m_role == RoleServer)
142 Integer b(staticPrivateKey, StaticPrivateKeyLength());
146 XX =
const_cast<byte*
>(ephemeralOtherPublicKey);
147 xxs = EphemeralPublicKeyLength();
148 YY =
const_cast<byte*
>(ephemeralPrivateKey) + StaticPrivateKeyLength();
149 yys = EphemeralPublicKeyLength();
150 AA =
const_cast<byte*
>(staticOtherPublicKey);
151 aas = StaticPublicKeyLength();
155 else if(m_role == RoleClient)
157 Integer a(staticPrivateKey, StaticPrivateKeyLength());
161 XX =
const_cast<byte*
>(ephemeralPrivateKey) + StaticPrivateKeyLength();
162 xxs = EphemeralPublicKeyLength();
163 YY =
const_cast<byte*
>(ephemeralOtherPublicKey);
164 yys = EphemeralPublicKeyLength();
167 BB =
const_cast<byte*
>(staticOtherPublicKey);
168 bbs = StaticPublicKeyLength();
179 Element VV1 = params.
DecodeElement(staticOtherPublicKey,
false);
180 if(!params.
ValidateElement(validateStaticOtherPublicKey ? 3 : 1, VV1, NULL))
185 Element VV2 = params.
DecodeElement(ephemeralOtherPublicKey,
false);
190 const unsigned int len = (((q.
BitCount()+1)/2 +7)/8);
195 Hash(NULL, XX, xxs, YY, yys, AA, aas, BB, bbs, dd.BytePtr(), dd.SizeInBytes());
196 d.
Decode(dd.BytePtr(), dd.SizeInBytes());
198 Hash(NULL, YY, yys, XX, xxs, AA, aas, BB, bbs, ee.
BytePtr(), ee.
SizeInBytes());
202 if(m_role == RoleServer)
204 Integer y(ephemeralPrivateKey, StaticPrivateKeyLength());
205 Integer b(staticPrivateKey, StaticPrivateKeyLength());
212 Element
t2 = m_groupParameters.MultiplyElements(X, t1);
218 Integer x(ephemeralPrivateKey, StaticPrivateKeyLength());
219 Integer a(staticPrivateKey, StaticPrivateKeyLength());
226 Element
t2 = m_groupParameters.MultiplyElements(Y, t1);
231 Hash(&sigma, XX, xxs, YY, yys, AA, aas, BB, bbs, agreedValue, AgreedValueLength());
242 inline void Hash(
const Element* sigma,
243 const byte* e1,
size_t e1len,
const byte* e2,
size_t e2len,
244 const byte*
s1,
size_t s1len,
const byte*
s2,
size_t s2len,
245 byte* digest,
size_t dlen)
const 248 size_t idx = 0, req = dlen;
249 size_t blk =
STDMIN(dlen, (
size_t)HASH::DIGESTSIZE);
253 Integer x = GetAbstractGroupParameters().ConvertElementToInteger(*sigma);
255 x.
Encode(sbb.BytePtr(), sbb.SizeInBytes());
256 hash.Update(sbb.BytePtr(), sbb.SizeInBytes());
259 hash.Update(e1, e1len);
260 hash.Update(e2, e2len);
261 hash.Update(s1, s1len);
262 hash.Update(s2, s2len);
264 hash.TruncatedFinal(digest, blk);
270 hash.Update(&digest[idx], (
size_t)HASH::DIGESTSIZE);
272 idx += (size_t)HASH::DIGESTSIZE;
273 blk =
STDMIN(req, (
size_t)HASH::DIGESTSIZE);
274 hash.TruncatedFinal(&digest[idx], blk);
const DL_GroupParameters< Element > & GetAbstractGroupParameters() const
void Encode(byte *output, size_t outputLen, Signedness sign=UNSIGNED) const
Encode in big-endian format.
static const std::string s2("AAD")
Fully Hashed Menezes-Qu-Vanstone in GF(p)
#define NAMESPACE_BEGIN(x)
unsigned int AgreedValueLength() const
return length of agreed value produced
Interface for Discrete Log (DL) group parameters.
DL_GroupParameters< Element > & AccessAbstractGroupParameters()
unsigned int EphemeralPublicKeyLength() const
Provides the size of ephemeral public key.
Interface for random number generators.
size_t MinEncodedSize(Signedness sign=UNSIGNED) const
Minimum number of bytes to encode this integer.
FHMQV_Domain(T1 v1, bool clientRole=true)
Classes for performing mathematics over different fields.
FHMQV_Domain(T1 v1, T2 v2, T3 v3, bool clientRole=true)
static const Integer &CRYPTOPP_API One()
Integer representing 1.
unsigned int BitCount() const
Determines the number of bits required to represent the Integer.
GROUP_PARAMETERS GroupParameters
unsigned int EphemeralPrivateKeyLength() const
Provides the size of ephemeral private key.
void GenerateStaticPublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
generate static public key
Multiple precision integer with arithmetic operations.
unsigned int StaticPublicKeyLength() const
return length of static public keys in this domain
CryptoParameters & AccessCryptoParameters()
Retrieves a reference to Crypto Parameters.
GroupParameters::Element Element
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
void Hash(const Element *sigma, const byte *e1, size_t e1len, const byte *e2, size_t e2len, const byte *s1, size_t s1len, const byte *s2, size_t s2len, byte *digest, size_t dlen) const
unsigned int StaticPrivateKeyLength() const
return length of static private keys in this domain
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.
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
#define CRYPTOPP_ASSERT(exp)
virtual bool ValidateElement(unsigned int level, const Element &element, const DL_FixedBasePrecomputation< Element > *precomp) const =0
Check the element for errors.
GroupParameters & AccessGroupParameters()
Classes for SHA-1 and SHA-2 family of message digests.
FHMQV_Domain(T1 v1, T2 v2, bool clientRole=true)
FHMQV_Domain(bool clientRole=true)
void * memcpy(void *a, const void *b, size_t c)
#define CRYPTOPP_UNUSED(x)
void GenerateEphemeralPublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
return length of ephemeral public keys in this domain
virtual void EncodeElement(bool reversible, const Element &element, byte *encoded) const =0
Encodes the element.
void Decode(const byte *input, size_t inputLen, Signedness sign=UNSIGNED)
Decode from big-endian byte array.
const GroupParameters & GetGroupParameters() const
GroupParameters m_groupParameters
FHMQV_Domain(T1 v1, T2 v2, T3 v3, T4 v4, bool clientRole=true)
Interface for crypto prameters.
virtual Integer GetMaxExponent() const =0
Retrieves the maximum exponent for the group.
bool Agree(byte *agreedValue, const byte *staticPrivateKey, const byte *ephemeralPrivateKey, const byte *staticOtherPublicKey, const byte *ephemeralOtherPublicKey, bool validateStaticOtherPublicKey=true) const
derive agreed value from your private keys and couterparty's public keys, return false in case of fai...
Interface for domains of authenticated key agreement protocols.
size_type SizeInBytes() const
Provides the number of bytes in the SecBlock.
virtual Element ExponentiateBase(const Integer &exponent) const
Retrieves the subgroup generator.
FHMQV_Domain(BufferedTransformation &bt, bool clientRole=true)
void GenerateEphemeralPrivateKey(RandomNumberGenerator &rng, byte *privateKey) const
return length of ephemeral private keys in this domain
FHMQV_Domain< DL_GroupParameters_GFP_DefaultSafePrime > FHMQV
Fully Hashed Menezes-Qu-Vanstone in GF(p)
virtual Element ExponentiateElement(const Element &base, const Integer &exponent) const
Exponentiates an element.
void GenerateStaticPrivateKey(RandomNumberGenerator &rng, byte *privateKey) const
generate static private key
FHMQV_Domain(const GroupParameters ¶ms, bool clientRole=true)
FHMQV_Domain< GROUP_PARAMETERS, COFACTOR_OPTION, HASH > Domain
byte * BytePtr()
Provides a byte pointer to the first element in the memory block.
virtual const Integer & GetSubgroupOrder() const =0
Retrieves the subgroup order.