Security Scol plugin
hmqv.h
Go to the documentation of this file.
1// hmqv.h - written and placed in the public domain by Uri Blumenthal
2// Shamelessly based upon Wei Dai's MQV source files
3
4#ifndef CRYPTOPP_HMQV_H
5#define CRYPTOPP_HMQV_H
6
10
11#include "gfpcrypt.h"
12#include "algebra.h"
13#include "sha.h"
14
15NAMESPACE_BEGIN(CryptoPP)
16
17
22template <class GROUP_PARAMETERS, class COFACTOR_OPTION = typename GROUP_PARAMETERS::DefaultCofactorOption, class HASH = SHA512>
24{
25public:
26 typedef GROUP_PARAMETERS GroupParameters;
27 typedef typename GroupParameters::Element Element;
29
30 virtual ~HMQV_Domain() {}
31
36 HMQV_Domain(bool clientRole = true)
37 : m_role(clientRole ? RoleClient : RoleServer) {}
38
44 HMQV_Domain(const GroupParameters &params, bool clientRole = true)
45 : m_role(clientRole ? RoleClient : RoleServer), m_groupParameters(params) {}
46
52 HMQV_Domain(BufferedTransformation &bt, bool clientRole = true)
53 : m_role(clientRole ? RoleClient : RoleServer)
54 {m_groupParameters.BERDecode(bt);}
55
63 template <class T1>
64 HMQV_Domain(T1 v1, bool clientRole = true)
65 : m_role(clientRole ? RoleClient : RoleServer)
66 {m_groupParameters.Initialize(v1);}
67
77 template <class T1, class T2>
78 HMQV_Domain(T1 v1, T2 v2, bool clientRole = true)
79 : m_role(clientRole ? RoleClient : RoleServer)
80 {m_groupParameters.Initialize(v1, v2);}
81
93 template <class T1, class T2, class T3>
94 HMQV_Domain(T1 v1, T2 v2, T3 v3, bool clientRole = true)
95 : m_role(clientRole ? RoleClient : RoleServer)
96 {m_groupParameters.Initialize(v1, v2, v3);}
97
111 template <class T1, class T2, class T3, class T4>
112 HMQV_Domain(T1 v1, T2 v2, T3 v3, T4 v4, bool clientRole = true)
113 : m_role(clientRole ? RoleClient : RoleServer)
114 {m_groupParameters.Initialize(v1, v2, v3, v4);}
115
116public:
117
120 const GroupParameters & GetGroupParameters() const {return m_groupParameters;}
121
124 GroupParameters & AccessGroupParameters() {return m_groupParameters;}
125
128 CryptoParameters & AccessCryptoParameters() {return AccessAbstractGroupParameters();}
129
136 unsigned int AgreedValueLength() const
137 {return GetAbstractGroupParameters().GetEncodedElementSize(false);}
138
142 unsigned int StaticPrivateKeyLength() const
143 {return GetAbstractGroupParameters().GetSubgroupOrder().ByteCount();}
144
151 unsigned int StaticPublicKeyLength() const
152 {return GetAbstractGroupParameters().GetEncodedElementSize(true);}
153
160 void GenerateStaticPrivateKey(RandomNumberGenerator &rng, byte *privateKey) const
161 {
162 Integer x(rng, Integer::One(), GetAbstractGroupParameters().GetMaxExponent());
163 x.Encode(privateKey, StaticPrivateKeyLength());
164 }
165
174 void GenerateStaticPublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
175 {
176 CRYPTOPP_UNUSED(rng);
177 const DL_GroupParameters<Element> &params = GetAbstractGroupParameters();
178 Integer x(privateKey, StaticPrivateKeyLength());
179 Element y = params.ExponentiateBase(x);
180 params.EncodeElement(true, y, publicKey);
181 }
182
188
193 unsigned int EphemeralPublicKeyLength() const{return StaticPublicKeyLength();}
194
199 void GenerateEphemeralPrivateKey(RandomNumberGenerator &rng, byte *privateKey) const
200 {
201 const DL_GroupParameters<Element> &params = GetAbstractGroupParameters();
202 Integer x(rng, Integer::One(), params.GetMaxExponent());
203 x.Encode(privateKey, StaticPrivateKeyLength());
204 Element y = params.ExponentiateBase(x);
205 params.EncodeElement(true, y, privateKey+StaticPrivateKeyLength());
206 }
207
213 void GenerateEphemeralPublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
214 {
215 CRYPTOPP_UNUSED(rng);
216 memcpy(publicKey, privateKey+StaticPrivateKeyLength(), EphemeralPublicKeyLength());
217 }
218
242 bool Agree(byte *agreedValue,
243 const byte *staticPrivateKey, const byte *ephemeralPrivateKey,
244 const byte *staticOtherPublicKey, const byte *ephemeralOtherPublicKey,
245 bool validateStaticOtherPublicKey=true) const
246 {
247 const byte *XX = NULLPTR, *YY = NULLPTR, *AA = NULLPTR, *BB = NULLPTR;
248 size_t xxs = 0, yys = 0, aas = 0, bbs = 0;
249
250 // Depending on the role, this will hold either A's or B's static
251 // (long term) public key. AA or BB will then point into tt.
253
254 try
255 {
257 const DL_GroupParameters<Element> &params = GetAbstractGroupParameters();
258
259 if(m_role == RoleServer)
260 {
261 Integer b(staticPrivateKey, StaticPrivateKeyLength());
262 Element B = params.ExponentiateBase(b);
263 params.EncodeElement(true, B, tt);
264
265 XX = ephemeralOtherPublicKey;
267 YY = ephemeralPrivateKey + StaticPrivateKeyLength();
269 AA = staticOtherPublicKey;
270 aas = StaticPublicKeyLength();
271 BB = tt.BytePtr();
272 bbs = tt.SizeInBytes();
273 }
274 else
275 {
276 Integer a(staticPrivateKey, StaticPrivateKeyLength());
277 Element A = params.ExponentiateBase(a);
278 params.EncodeElement(true, A, tt);
279
280 XX = ephemeralPrivateKey + StaticPrivateKeyLength();
282 YY = ephemeralOtherPublicKey;
284 AA = tt.BytePtr();
285 aas = tt.SizeInBytes();
286 BB = staticOtherPublicKey;
287 bbs = StaticPublicKeyLength();
288 }
289
290 Element VV1 = params.DecodeElement(staticOtherPublicKey, validateStaticOtherPublicKey);
291 Element VV2 = params.DecodeElement(ephemeralOtherPublicKey, true);
292
293 const Integer& q = params.GetSubgroupOrder();
294 const unsigned int len /*bytes*/ = (((q.BitCount()+1)/2 +7)/8);
295 SecByteBlock dd(len), ee(len);
296
297 // Compute $d = \hat{H}(X, \hat{B})$
298 Hash(NULLPTR, XX, xxs, BB, bbs, dd.BytePtr(), dd.SizeInBytes());
299 Integer d(dd.BytePtr(), dd.SizeInBytes());
300
301 // Compute $e = \hat{H}(Y, \hat{A})$
302 Hash(NULLPTR, YY, yys, AA, aas, ee.BytePtr(), ee.SizeInBytes());
303 Integer e(ee.BytePtr(), ee.SizeInBytes());
304
305 Element sigma;
306 if(m_role == RoleServer)
307 {
308 Integer y(ephemeralPrivateKey, StaticPrivateKeyLength());
309 Integer b(staticPrivateKey, StaticPrivateKeyLength());
310 Integer s_B = (y + e * b) % q;
311
312 Element A = params.DecodeElement(AA, false);
313 Element X = params.DecodeElement(XX, false);
314
315 Element t1 = params.ExponentiateElement(A, d);
316 Element t2 = m_groupParameters.MultiplyElements(X, t1);
317
318 // $\sigma_B}=(X \cdot A^{d})^{s_B}
319 sigma = params.ExponentiateElement(t2, s_B);
320 }
321 else
322 {
323 Integer x(ephemeralPrivateKey, StaticPrivateKeyLength());
324 Integer a(staticPrivateKey, StaticPrivateKeyLength());
325 Integer s_A = (x + d * a) % q;
326
327 Element B = params.DecodeElement(BB, false);
328 Element Y = params.DecodeElement(YY, false);
329
330 Element t3 = params.ExponentiateElement(B, e);
331 Element t4 = m_groupParameters.MultiplyElements(Y, t3);
332
333 // $\sigma_A}=(Y \cdot B^{e})^{s_A}
334 sigma = params.ExponentiateElement(t4, s_A);
335 }
336 Hash(&sigma, NULLPTR, 0, NULLPTR, 0, agreedValue, AgreedValueLength());
337 }
338 catch (DL_BadElement &)
339 {
340 CRYPTOPP_ASSERT(0);
341 return false;
342 }
343 return true;
344 }
345
346protected:
347 // Hash invocation by client and server differ only in what keys
348 // each provides.
349
350 inline void Hash(const Element* sigma,
351 const byte* e1, size_t e1len, // Ephemeral key and key length
352 const byte* s1, size_t s1len, // Static key and key length
353 byte* digest, size_t dlen) const
354 {
355 HASH hash;
356 size_t idx = 0, req = dlen;
357 size_t blk = STDMIN(dlen, (size_t)HASH::DIGESTSIZE);
358
359 if(sigma)
360 {
361 if (e1len != 0 || s1len != 0) {
362 CRYPTOPP_ASSERT(0);
363 }
364 //Integer x = GetAbstractGroupParameters().ConvertElementToInteger(*sigma);
365 //SecByteBlock sbb(x.MinEncodedSize());
366 //x.Encode(sbb.BytePtr(), sbb.SizeInBytes());
367 SecByteBlock sbb(GetAbstractGroupParameters().GetEncodedElementSize(false));
368 GetAbstractGroupParameters().EncodeElement(false, *sigma, sbb);
369 hash.Update(sbb.BytePtr(), sbb.SizeInBytes());
370 } else {
371 if (e1len == 0 || s1len == 0) {
372 CRYPTOPP_ASSERT(0);
373 }
374 hash.Update(e1, e1len);
375 hash.Update(s1, s1len);
376 }
377
378 hash.TruncatedFinal(digest, blk);
379 req -= blk;
380
381 // All this to catch tail bytes for large curves and small hashes
382 while(req != 0)
383 {
384 hash.Update(&digest[idx], (size_t)HASH::DIGESTSIZE);
385
386 idx += (size_t)HASH::DIGESTSIZE;
387 blk = STDMIN(req, (size_t)HASH::DIGESTSIZE);
388 hash.TruncatedFinal(&digest[idx], blk);
389
390 req -= blk;
391 }
392 }
393
394private:
395
396 // The paper uses Initiator and Recipient - make it classical.
397 enum KeyAgreementRole { RoleServer = 1, RoleClient };
398
399 DL_GroupParameters<Element> & AccessAbstractGroupParameters()
400 {return m_groupParameters;}
401 const DL_GroupParameters<Element> & GetAbstractGroupParameters() const
402 {return m_groupParameters;}
403
404 GroupParameters m_groupParameters;
405 KeyAgreementRole m_role;
406};
407
414
415NAMESPACE_END
416
417#endif
Classes for performing mathematics over different fields.
Interface for domains of authenticated key agreement protocols.
Definition cryptlib.h:3072
Interface for buffered transformations.
Definition cryptlib.h:1652
void DoQuickSanityCheck() const
Perform a quick sanity check.
Definition cryptlib.h:2493
Interface for crypto parameters.
Definition cryptlib.h:2546
Exception thrown when an invalid group element is encountered.
Definition pubkey.h:772
Interface for Discrete Log (DL) group parameters.
Definition pubkey.h:782
virtual Element ExponentiateElement(const Element &base, const Integer &exponent) const
Exponentiates an element.
Definition pubkey.h:879
virtual void EncodeElement(bool reversible, const Element &element, byte *encoded) const =0
Encodes the element.
virtual Integer GetMaxExponent() const =0
Retrieves the maximum exponent for the group.
virtual const Integer & GetSubgroupOrder() const =0
Retrieves the subgroup order.
virtual Element ExponentiateBase(const Integer &exponent) const
Exponentiates the base.
Definition pubkey.h:869
virtual Element DecodeElement(const byte *encoded, bool checkForGroupMembership) const =0
Decodes the element.
Hashed Menezes-Qu-Vanstone in GF(p)
Definition hmqv.h:24
CryptoParameters & AccessCryptoParameters()
Retrieves the crypto parameters for this domain.
Definition hmqv.h:128
const GroupParameters & GetGroupParameters() const
Retrieves the group parameters for this domain.
Definition hmqv.h:120
void GenerateEphemeralPrivateKey(RandomNumberGenerator &rng, byte *privateKey) const
Generate ephemeral private key in this domain.
Definition hmqv.h:199
void GenerateStaticPrivateKey(RandomNumberGenerator &rng, byte *privateKey) const
Generate static private key in this domain.
Definition hmqv.h:160
HMQV_Domain(T1 v1, bool clientRole=true)
Construct a HMQV domain.
Definition hmqv.h:64
void GenerateEphemeralPublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
Generate ephemeral public key from a private key in this domain.
Definition hmqv.h:213
GroupParameters & AccessGroupParameters()
Retrieves the group parameters for this domain.
Definition hmqv.h:124
HMQV_Domain(BufferedTransformation &bt, bool clientRole=true)
Construct a HMQV domain.
Definition hmqv.h:52
HMQV_Domain(bool clientRole=true)
Construct a HMQV domain.
Definition hmqv.h:36
HMQV_Domain(T1 v1, T2 v2, T3 v3, bool clientRole=true)
Construct a HMQV domain.
Definition hmqv.h:94
unsigned int StaticPrivateKeyLength() const
Provides the size of the static private key.
Definition hmqv.h:142
bool Agree(byte *agreedValue, const byte *staticPrivateKey, const byte *ephemeralPrivateKey, const byte *staticOtherPublicKey, const byte *ephemeralOtherPublicKey, bool validateStaticOtherPublicKey=true) const
Derive agreed value or shared secret.
Definition hmqv.h:242
unsigned int AgreedValueLength() const
Provides the size of the agreed value.
Definition hmqv.h:136
HMQV_Domain(T1 v1, T2 v2, bool clientRole=true)
Construct a HMQV domain.
Definition hmqv.h:78
unsigned int EphemeralPrivateKeyLength() const
Provides the size of the ephemeral private key.
Definition hmqv.h:187
void GenerateStaticPublicKey(RandomNumberGenerator &rng, const byte *privateKey, byte *publicKey) const
Generate a static public key from a private key in this domain.
Definition hmqv.h:174
unsigned int StaticPublicKeyLength() const
Provides the size of the static public key.
Definition hmqv.h:151
unsigned int EphemeralPublicKeyLength() const
Provides the size of the ephemeral public key.
Definition hmqv.h:193
HMQV_Domain(const GroupParameters &params, bool clientRole=true)
Construct a HMQV domain.
Definition hmqv.h:44
HMQV_Domain(T1 v1, T2 v2, T3 v3, T4 v4, bool clientRole=true)
Construct a HMQV domain.
Definition hmqv.h:112
Multiple precision integer with arithmetic operations.
Definition integer.h:50
unsigned int BitCount() const
Determines the number of bits required to represent the Integer.
Definition integer.cpp:3364
static const Integer &CRYPTOPP_API One()
Integer representing 1.
Definition integer.cpp:4920
const CryptoMaterial & GetMaterial() const
Retrieves a reference to Crypto Parameters.
Definition cryptlib.h:2647
Interface for random number generators.
Definition cryptlib.h:1435
size_type SizeInBytes() const
Provides the number of bytes in the SecBlock.
Definition secblock.h:885
byte * BytePtr()
Provides a byte pointer to the first element in the memory block.
Definition secblock.h:876
Classes and functions for schemes based on Discrete Logs (DL) over GF(p)
HMQV_Domain< DL_GroupParameters_GFP_DefaultSafePrime > HMQV
Hashed Menezes-Qu-Vanstone in GF(p)
Definition hmqv.h:413
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
Definition misc.h:655
Classes for SHA-1 and SHA-2 family of message digests.