4 #ifndef CRYPTOPP_HMQV_H 5 #define CRYPTOPP_HMQV_H 17 template <class GROUP_PARAMETERS, class COFACTOR_OPTION = typename GROUP_PARAMETERS::DefaultCofactorOption, class HASH =
SHA512>
27 typedef typename GroupParameters::Element
Element;
32 HMQV_Domain(
bool clientRole =
true): m_role(clientRole ? RoleClient : RoleServer) {}
34 HMQV_Domain(
const GroupParameters ¶ms,
bool clientRole =
true)
35 : m_role(clientRole ? RoleClient : RoleServer), m_groupParameters(params) {}
38 : m_role(clientRole ? RoleClient : RoleServer)
39 {m_groupParameters.BERDecode(bt);}
43 : m_role(clientRole ? RoleClient : RoleServer)
44 {m_groupParameters.Initialize(v1);}
46 template <
class T1,
class T2>
48 : m_role(clientRole ? RoleClient : RoleServer)
49 {m_groupParameters.Initialize(v1, v2);}
51 template <
class T1,
class T2,
class T3>
53 : m_role(clientRole ? RoleClient : RoleServer)
54 {m_groupParameters.Initialize(v1, v2, v3);}
56 template <
class T1,
class T2,
class T3,
class T4>
58 : m_role(clientRole ? RoleClient : RoleServer)
59 {m_groupParameters.Initialize(v1, v2, v3, v4);}
69 unsigned int AgreedValueLength()
const {
return GetAbstractGroupParameters().GetEncodedElementSize(
false);}
80 x.
Encode(privateKey, StaticPrivateKeyLength());
89 Integer x(privateKey, StaticPrivateKeyLength());
102 x.Encode(privateKey, StaticPrivateKeyLength());
104 params.
EncodeElement(
true, y, privateKey+StaticPrivateKeyLength());
111 memcpy(publicKey, privateKey+StaticPrivateKeyLength(), EphemeralPublicKeyLength());
124 const byte *staticPrivateKey,
const byte *ephemeralPrivateKey,
125 const byte *staticOtherPublicKey,
const byte *ephemeralOtherPublicKey,
126 bool validateStaticOtherPublicKey=
true)
const 128 byte *XX = NULL, *YY = NULL, *AA = NULL, *BB = NULL;
129 size_t xxs = 0, yys = 0, aas = 0, bbs = 0;
139 if(m_role == RoleServer)
141 Integer b(staticPrivateKey, StaticPrivateKeyLength());
145 XX =
const_cast<byte*
>(ephemeralOtherPublicKey);
146 xxs = EphemeralPublicKeyLength();
147 YY =
const_cast<byte*
>(ephemeralPrivateKey) + StaticPrivateKeyLength();
148 yys = EphemeralPublicKeyLength();
149 AA =
const_cast<byte*
>(staticOtherPublicKey);
150 aas = StaticPublicKeyLength();
154 else if(m_role == RoleClient)
156 Integer a(staticPrivateKey, StaticPrivateKeyLength());
160 XX =
const_cast<byte*
>(ephemeralPrivateKey) + StaticPrivateKeyLength();
161 xxs = EphemeralPublicKeyLength();
162 YY =
const_cast<byte*
>(ephemeralOtherPublicKey);
163 yys = EphemeralPublicKeyLength();
166 BB =
const_cast<byte*
>(staticOtherPublicKey);
167 bbs = StaticPublicKeyLength();
178 Element VV1 = params.
DecodeElement(staticOtherPublicKey,
false);
179 if(!params.
ValidateElement(validateStaticOtherPublicKey ? 3 : 1, VV1, NULL))
184 Element VV2 = params.
DecodeElement(ephemeralOtherPublicKey,
false);
190 const unsigned int len = (((q.
BitCount()+1)/2 +7)/8);
196 Hash(NULL, XX, xxs, BB, bbs, dd.BytePtr(), dd.SizeInBytes());
197 d.
Decode(dd.BytePtr(), dd.SizeInBytes());
204 if(m_role == RoleServer)
206 Integer y(ephemeralPrivateKey, StaticPrivateKeyLength());
207 Integer b(staticPrivateKey, StaticPrivateKeyLength());
214 Element
t2 = m_groupParameters.MultiplyElements(X, t1);
221 Integer x(ephemeralPrivateKey, StaticPrivateKeyLength());
222 Integer a(staticPrivateKey, StaticPrivateKeyLength());
229 Element
t2 = m_groupParameters.MultiplyElements(Y, t1);
234 Hash(&sigma, NULL, 0, NULL, 0, agreedValue, AgreedValueLength());
247 inline void Hash(
const Element* sigma,
248 const byte* e1,
size_t e1len,
249 const byte*
s1,
size_t s1len,
250 byte* digest,
size_t dlen)
const 253 size_t idx = 0, req = dlen;
254 size_t blk =
STDMIN(dlen, (
size_t)HASH::DIGESTSIZE);
258 if (e1len != 0 || s1len != 0) {
261 Integer x = GetAbstractGroupParameters().ConvertElementToInteger(*sigma);
263 x.
Encode(sbb.BytePtr(), sbb.SizeInBytes());
264 hash.Update(sbb.BytePtr(), sbb.SizeInBytes());
266 if (e1len == 0 || s1len == 0) {
269 hash.Update(e1, e1len);
270 hash.Update(s1, s1len);
273 hash.TruncatedFinal(digest, blk);
279 hash.Update(&digest[idx], (
size_t)HASH::DIGESTSIZE);
281 idx += (size_t)HASH::DIGESTSIZE;
282 blk =
STDMIN(req, (
size_t)HASH::DIGESTSIZE);
283 hash.TruncatedFinal(&digest[idx], blk);
void GenerateEphemeralPublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
return length of ephemeral public keys in this domain
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...
void GenerateStaticPublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
generate static public key
HMQV_Domain< GROUP_PARAMETERS, COFACTOR_OPTION, HASH > Domain
void Encode(byte *output, size_t outputLen, Signedness sign=UNSIGNED) const
Encode in big-endian format.
GroupParameters::Element Element
#define NAMESPACE_BEGIN(x)
Interface for Discrete Log (DL) group parameters.
unsigned int StaticPrivateKeyLength() const
return length of static private keys in this domain
HMQV_Domain(T1 v1, T2 v2, T3 v3, bool clientRole=true)
Hashed Menezes-Qu-Vanstone in GF(p)
Interface for random number generators.
size_t MinEncodedSize(Signedness sign=UNSIGNED) const
Minimum number of bytes to encode this integer.
Classes for performing mathematics over different fields.
GroupParameters & AccessGroupParameters()
HMQV_Domain(BufferedTransformation &bt, bool clientRole=true)
static const Integer &CRYPTOPP_API One()
Integer representing 1.
DL_GroupParameters< Element > & AccessAbstractGroupParameters()
void GenerateStaticPrivateKey(RandomNumberGenerator &rng, byte *privateKey) const
generate static private key
unsigned int BitCount() const
Determines the number of bits required to represent the Integer.
HMQV_Domain(T1 v1, T2 v2, T3 v3, T4 v4, bool clientRole=true)
GROUP_PARAMETERS GroupParameters
const DL_GroupParameters< Element > & GetAbstractGroupParameters() const
CryptoParameters & AccessCryptoParameters()
Retrieves a reference to Crypto Parameters.
HMQV_Domain(T1 v1, T2 v2, bool clientRole=true)
HMQV_Domain(T1 v1, bool clientRole=true)
unsigned int AgreedValueLength() const
return length of agreed value produced
HMQV_Domain< DL_GroupParameters_GFP_DefaultSafePrime > HMQV
Hashed Menezes-Qu-Vanstone in GF(p)
Multiple precision integer with arithmetic operations.
const GroupParameters & GetGroupParameters() const
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
Classes and functions for schemes based on Discrete Logs (DL) over GF(p)
unsigned int EphemeralPrivateKeyLength() const
Provides the size of ephemeral private key.
virtual Element DecodeElement(const byte *encoded, bool checkForGroupMembership) const =0
Decodes the element.
unsigned int EphemeralPublicKeyLength() const
Provides the size of ephemeral public key.
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)
HMQV_Domain(bool clientRole=true)
virtual bool ValidateElement(unsigned int level, const Element &element, const DL_FixedBasePrecomputation< Element > *precomp) const =0
Check the element for errors.
Classes for SHA-1 and SHA-2 family of message digests.
void * memcpy(void *a, const void *b, size_t c)
#define CRYPTOPP_UNUSED(x)
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.
GroupParameters m_groupParameters
Interface for crypto prameters.
virtual Integer GetMaxExponent() const =0
Retrieves the maximum exponent for the group.
Interface for domains of authenticated key agreement protocols.
void GenerateEphemeralPrivateKey(RandomNumberGenerator &rng, byte *privateKey) const
return length of ephemeral private keys in this domain
size_type SizeInBytes() const
Provides the number of bytes in the SecBlock.
virtual Element ExponentiateBase(const Integer &exponent) const
Retrieves the subgroup generator.
HMQV_Domain(const GroupParameters ¶ms, bool clientRole=true)
virtual Element ExponentiateElement(const Element &base, const Integer &exponent) const
Exponentiates an element.
void Hash(const Element *sigma, const byte *e1, size_t e1len, const byte *s1, size_t s1len, byte *digest, size_t dlen) const
unsigned int StaticPublicKeyLength() const
return length of static public keys in this 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.