gfpcrypt.h 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. // gfpcrypt.h - originally written and placed in the public domain by Wei Dai
  2. // RFC6979 deterministic signatures added by Douglas Roark
  3. // ECGDSA added by Jeffrey Walton
  4. /// \file gfpcrypt.h
  5. /// \brief Classes and functions for schemes based on Discrete Logs (DL) over GF(p)
  6. #ifndef CRYPTOPP_GFPCRYPT_H
  7. #define CRYPTOPP_GFPCRYPT_H
  8. #include "config.h"
  9. #if CRYPTOPP_MSC_VERSION
  10. # pragma warning(push)
  11. # pragma warning(disable: 4189 4231 4275)
  12. #endif
  13. #include "cryptlib.h"
  14. #include "pubkey.h"
  15. #include "integer.h"
  16. #include "modexppc.h"
  17. #include "algparam.h"
  18. #include "smartptr.h"
  19. #include "sha.h"
  20. #include "asn.h"
  21. #include "hmac.h"
  22. #include "misc.h"
  23. NAMESPACE_BEGIN(CryptoPP)
  24. CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupParameters<Integer>;
  25. /// \brief Integer-based GroupParameters specialization
  26. class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE DL_GroupParameters_IntegerBased : public ASN1CryptoMaterial<DL_GroupParameters<Integer> >
  27. {
  28. typedef DL_GroupParameters_IntegerBased ThisClass;
  29. public:
  30. virtual ~DL_GroupParameters_IntegerBased() {}
  31. /// \brief Initialize a group parameters over integers
  32. /// \param params the group parameters
  33. void Initialize(const DL_GroupParameters_IntegerBased &params)
  34. {Initialize(params.GetModulus(), params.GetSubgroupOrder(), params.GetSubgroupGenerator());}
  35. /// \brief Create a group parameters over integers
  36. /// \param rng a RandomNumberGenerator derived class
  37. /// \param pbits the size of p, in bits
  38. /// \details This function overload of Initialize() creates a new private key because it
  39. /// takes a RandomNumberGenerator() as a parameter. If you have an existing keypair,
  40. /// then use one of the other Initialize() overloads.
  41. void Initialize(RandomNumberGenerator &rng, unsigned int pbits)
  42. {GenerateRandom(rng, MakeParameters("ModulusSize", (int)pbits));}
  43. /// \brief Initialize a group parameters over integers
  44. /// \param p the modulus
  45. /// \param g the generator
  46. void Initialize(const Integer &p, const Integer &g)
  47. {SetModulusAndSubgroupGenerator(p, g); SetSubgroupOrder(ComputeGroupOrder(p)/2);}
  48. /// \brief Initialize a group parameters over integers
  49. /// \param p the modulus
  50. /// \param q the subgroup order
  51. /// \param g the generator
  52. void Initialize(const Integer &p, const Integer &q, const Integer &g)
  53. {SetModulusAndSubgroupGenerator(p, g); SetSubgroupOrder(q);}
  54. // ASN1Object interface
  55. void BERDecode(BufferedTransformation &bt);
  56. void DEREncode(BufferedTransformation &bt) const;
  57. /// \brief Generate a random key
  58. /// \param rng a RandomNumberGenerator to produce keying material
  59. /// \param alg additional initialization parameters
  60. /// \details Recognised NameValuePairs are ModulusSize and
  61. /// SubgroupOrderSize (optional)
  62. /// \throw KeyingErr if a key can't be generated or algorithm parameters
  63. /// are invalid
  64. void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
  65. /// \brief Get a named value
  66. /// \param name the name of the object or value to retrieve
  67. /// \param valueType reference to a variable that receives the value
  68. /// \param pValue void pointer to a variable that receives the value
  69. /// \return true if the value was retrieved, false otherwise
  70. /// \details GetVoidValue() retrieves the value of name if it exists.
  71. /// \note GetVoidValue() is an internal function and should be implemented
  72. /// by derived classes. Users should use one of the other functions instead.
  73. /// \sa GetValue(), GetValueWithDefault(), GetIntValue(), GetIntValueWithDefault(),
  74. /// GetRequiredParameter() and GetRequiredIntParameter()
  75. bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
  76. /// \brief Initialize or reinitialize this key
  77. /// \param source NameValuePairs to assign
  78. void AssignFrom(const NameValuePairs &source);
  79. // DL_GroupParameters
  80. const Integer & GetSubgroupOrder() const {return m_q;}
  81. Integer GetGroupOrder() const {return GetFieldType() == 1 ? GetModulus()-Integer::One() : GetModulus()+Integer::One();}
  82. bool ValidateGroup(RandomNumberGenerator &rng, unsigned int level) const;
  83. bool ValidateElement(unsigned int level, const Integer &element, const DL_FixedBasePrecomputation<Integer> *precomp) const;
  84. /// \brief Determine if subgroup membership check is fast
  85. /// \return true or false
  86. bool FastSubgroupCheckAvailable() const {return GetCofactor() == 2;}
  87. /// \brief Encodes the element
  88. /// \param reversible flag indicating the encoding format
  89. /// \param element reference to the element to encode
  90. /// \param encoded destination byte array for the encoded element
  91. /// \details EncodeElement() must be implemented in a derived class.
  92. /// \pre <tt>COUNTOF(encoded) == GetEncodedElementSize()</tt>
  93. /// \sa GetEncodedElementSize(), DecodeElement(), <A
  94. /// HREF="http://github.com/weidai11/cryptopp/issues/40">Cygwin
  95. /// i386 crash at -O3</A>
  96. void EncodeElement(bool reversible, const Element &element, byte *encoded) const;
  97. /// \brief Retrieve the encoded element's size
  98. /// \param reversible flag indicating the encoding format
  99. /// \return encoded element's size, in bytes
  100. /// \details The format of the encoded element varies by the underlying
  101. /// type of the element and the reversible flag.
  102. /// \sa EncodeElement(), DecodeElement()
  103. unsigned int GetEncodedElementSize(bool reversible) const;
  104. /// \brief Decodes the element
  105. /// \param encoded byte array with the encoded element
  106. /// \param checkForGroupMembership flag indicating if the element should be validated
  107. /// \return Element after decoding
  108. /// \details DecodeElement() must be implemented in a derived class.
  109. /// \pre <tt>COUNTOF(encoded) == GetEncodedElementSize()</tt>
  110. /// \sa GetEncodedElementSize(), EncodeElement()
  111. Integer DecodeElement(const byte *encoded, bool checkForGroupMembership) const;
  112. /// \brief Converts an element to an Integer
  113. /// \param element the element to convert to an Integer
  114. /// \return Element after converting to an Integer
  115. /// \details ConvertElementToInteger() must be implemented in a derived class.
  116. Integer ConvertElementToInteger(const Element &element) const
  117. {return element;}
  118. /// \brief Retrieve the maximum exponent for the group
  119. /// \return the maximum exponent for the group
  120. Integer GetMaxExponent() const;
  121. /// \brief Retrieve the OID of the algorithm
  122. /// \return OID of the algorithm
  123. OID GetAlgorithmID() const;
  124. /// \brief Retrieve the modulus for the group
  125. /// \return the modulus for the group
  126. virtual const Integer & GetModulus() const =0;
  127. /// \brief Set group parameters
  128. /// \param p the prime modulus
  129. /// \param g the group generator
  130. virtual void SetModulusAndSubgroupGenerator(const Integer &p, const Integer &g) =0;
  131. /// \brief Set subgroup order
  132. /// \param q the subgroup order
  133. void SetSubgroupOrder(const Integer &q)
  134. {m_q = q; ParametersChanged();}
  135. static std::string CRYPTOPP_API StaticAlgorithmNamePrefix() {return "";}
  136. protected:
  137. Integer ComputeGroupOrder(const Integer &modulus) const
  138. {return modulus-(GetFieldType() == 1 ? 1 : -1);}
  139. // GF(p) = 1, GF(p^2) = 2
  140. virtual int GetFieldType() const =0;
  141. virtual unsigned int GetDefaultSubgroupOrderSize(unsigned int modulusSize) const;
  142. private:
  143. Integer m_q;
  144. };
  145. /// \brief Integer-based GroupParameters default implementation
  146. /// \tparam GROUP_PRECOMP group parameters precomputation specialization
  147. /// \tparam BASE_PRECOMP base class precomputation specialization
  148. template <class GROUP_PRECOMP, class BASE_PRECOMP = DL_FixedBasePrecomputationImpl<typename GROUP_PRECOMP::Element> >
  149. class CRYPTOPP_NO_VTABLE DL_GroupParameters_IntegerBasedImpl : public DL_GroupParametersImpl<GROUP_PRECOMP, BASE_PRECOMP, DL_GroupParameters_IntegerBased>
  150. {
  151. typedef DL_GroupParameters_IntegerBasedImpl<GROUP_PRECOMP, BASE_PRECOMP> ThisClass;
  152. public:
  153. typedef typename GROUP_PRECOMP::Element Element;
  154. virtual ~DL_GroupParameters_IntegerBasedImpl() {}
  155. // GeneratibleCryptoMaterial interface
  156. bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
  157. {return GetValueHelper<DL_GroupParameters_IntegerBased>(this, name, valueType, pValue).Assignable();}
  158. void AssignFrom(const NameValuePairs &source)
  159. {AssignFromHelper<DL_GroupParameters_IntegerBased>(this, source);}
  160. // DL_GroupParameters
  161. const DL_FixedBasePrecomputation<Element> & GetBasePrecomputation() const {return this->m_gpc;}
  162. DL_FixedBasePrecomputation<Element> & AccessBasePrecomputation() {return this->m_gpc;}
  163. // IntegerGroupParameters
  164. /// \brief Retrieve the modulus for the group
  165. /// \return the modulus for the group
  166. const Integer & GetModulus() const {return this->m_groupPrecomputation.GetModulus();}
  167. /// \brief Retrieves a reference to the group generator
  168. /// \return const reference to the group generator
  169. const Integer & GetGenerator() const {return this->m_gpc.GetBase(this->GetGroupPrecomputation());}
  170. void SetModulusAndSubgroupGenerator(const Integer &p, const Integer &g) // these have to be set together
  171. {this->m_groupPrecomputation.SetModulus(p); this->m_gpc.SetBase(this->GetGroupPrecomputation(), g); this->ParametersChanged();}
  172. // non-inherited
  173. bool operator==(const DL_GroupParameters_IntegerBasedImpl<GROUP_PRECOMP, BASE_PRECOMP> &rhs) const
  174. {return GetModulus() == rhs.GetModulus() && GetGenerator() == rhs.GetGenerator() && this->GetSubgroupOrder() == rhs.GetSubgroupOrder();}
  175. bool operator!=(const DL_GroupParameters_IntegerBasedImpl<GROUP_PRECOMP, BASE_PRECOMP> &rhs) const
  176. {return !operator==(rhs);}
  177. };
  178. CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupParameters_IntegerBasedImpl<ModExpPrecomputation>;
  179. /// \brief GF(p) group parameters
  180. class CRYPTOPP_DLL DL_GroupParameters_GFP : public DL_GroupParameters_IntegerBasedImpl<ModExpPrecomputation>
  181. {
  182. public:
  183. virtual ~DL_GroupParameters_GFP() {}
  184. /// \brief Determines if an element is an identity
  185. /// \param element element to check
  186. /// \return true if the element is an identity, false otherwise
  187. /// \details The identity element or or neutral element is a special element
  188. /// in a group that leaves other elements unchanged when combined with it.
  189. /// \details IsIdentity() must be implemented in a derived class.
  190. bool IsIdentity(const Integer &element) const {return element == Integer::One();}
  191. /// \brief Exponentiates a base to multiple exponents
  192. /// \param results an array of Elements
  193. /// \param base the base to raise to the exponents
  194. /// \param exponents an array of exponents
  195. /// \param exponentsCount the number of exponents in the array
  196. /// \details SimultaneousExponentiate() raises the base to each exponent in
  197. /// the exponents array and stores the result at the respective position in
  198. /// the results array.
  199. /// \details SimultaneousExponentiate() must be implemented in a derived class.
  200. /// \pre <tt>COUNTOF(results) == exponentsCount</tt>
  201. /// \pre <tt>COUNTOF(exponents) == exponentsCount</tt>
  202. void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const;
  203. /// \brief Get a named value
  204. /// \param name the name of the object or value to retrieve
  205. /// \param valueType reference to a variable that receives the value
  206. /// \param pValue void pointer to a variable that receives the value
  207. /// \return true if the value was retrieved, false otherwise
  208. /// \details GetVoidValue() retrieves the value of name if it exists.
  209. /// \note GetVoidValue() is an internal function and should be implemented
  210. /// by derived classes. Users should use one of the other functions instead.
  211. /// \sa GetValue(), GetValueWithDefault(), GetIntValue(), GetIntValueWithDefault(),
  212. /// GetRequiredParameter() and GetRequiredIntParameter()
  213. bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
  214. {
  215. return GetValueHelper<DL_GroupParameters_IntegerBased>(this, name, valueType, pValue).Assignable();
  216. }
  217. // used by MQV
  218. Element MultiplyElements(const Element &a, const Element &b) const;
  219. Element CascadeExponentiate(const Element &element1, const Integer &exponent1, const Element &element2, const Integer &exponent2) const;
  220. protected:
  221. int GetFieldType() const {return 1;}
  222. };
  223. /// \brief GF(p) group parameters that default to safe primes
  224. class CRYPTOPP_DLL DL_GroupParameters_GFP_DefaultSafePrime : public DL_GroupParameters_GFP
  225. {
  226. public:
  227. typedef NoCofactorMultiplication DefaultCofactorOption;
  228. virtual ~DL_GroupParameters_GFP_DefaultSafePrime() {}
  229. protected:
  230. unsigned int GetDefaultSubgroupOrderSize(unsigned int modulusSize) const {return modulusSize-1;}
  231. };
  232. /// ElGamal encryption for safe interop
  233. /// \sa <A HREF="https://eprint.iacr.org/2021/923.pdf">On the
  234. /// (in)security of ElGamal in OpenPGP</A>,
  235. /// <A HREF="https://github.com/weidai11/cryptopp/issues/1059">Issue 1059</A>,
  236. /// <A HREF="https://nvd.nist.gov/vuln/detail/CVE-2021-40530">CVE-2021-40530</A>
  237. /// \since Crypto++ 8.6
  238. class CRYPTOPP_DLL DL_GroupParameters_ElGamal : public DL_GroupParameters_GFP_DefaultSafePrime
  239. {
  240. public:
  241. typedef NoCofactorMultiplication DefaultCofactorOption;
  242. virtual ~DL_GroupParameters_ElGamal() {}
  243. Integer GetMaxExponent() const
  244. {
  245. return GetSubgroupOrder()-1;
  246. }
  247. };
  248. /// \brief GDSA algorithm
  249. /// \tparam T FieldElement type or class
  250. /// \details FieldElement <tt>T</tt> can be Integer, ECP or EC2N.
  251. template <class T>
  252. class DL_Algorithm_GDSA : public DL_ElgamalLikeSignatureAlgorithm<T>
  253. {
  254. public:
  255. CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "DSA-1363";}
  256. virtual ~DL_Algorithm_GDSA() {}
  257. void Sign(const DL_GroupParameters<T> &params, const Integer &x, const Integer &k, const Integer &e, Integer &r, Integer &s) const
  258. {
  259. const Integer &q = params.GetSubgroupOrder();
  260. r %= q;
  261. Integer kInv = k.InverseMod(q);
  262. s = (kInv * (x*r + e)) % q;
  263. CRYPTOPP_ASSERT(!!r && !!s);
  264. }
  265. bool Verify(const DL_GroupParameters<T> &params, const DL_PublicKey<T> &publicKey, const Integer &e, const Integer &r, const Integer &s) const
  266. {
  267. const Integer &q = params.GetSubgroupOrder();
  268. if (r>=q || r<1 || s>=q || s<1)
  269. return false;
  270. Integer w = s.InverseMod(q);
  271. Integer u1 = (e * w) % q;
  272. Integer u2 = (r * w) % q;
  273. // verify r == (g^u1 * y^u2 mod p) mod q
  274. return r == params.ConvertElementToInteger(publicKey.CascadeExponentiateBaseAndPublicElement(u1, u2)) % q;
  275. }
  276. };
  277. /// \brief DSA signature algorithm based on RFC 6979
  278. /// \tparam T FieldElement type or class
  279. /// \tparam H HashTransformation derived class
  280. /// \details FieldElement <tt>T</tt> can be Integer, ECP or EC2N.
  281. /// \sa <a href="http://tools.ietf.org/rfc/rfc6979.txt">RFC 6979, Deterministic Usage of the
  282. /// Digital Signature Algorithm (DSA) and Elliptic Curve Digital Signature Algorithm (ECDSA)</a>
  283. /// \since Crypto++ 6.0
  284. template <class T, class H>
  285. class DL_Algorithm_DSA_RFC6979 : public DL_Algorithm_GDSA<T>, public DeterministicSignatureAlgorithm
  286. {
  287. public:
  288. CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "DSA-RFC6979";}
  289. virtual ~DL_Algorithm_DSA_RFC6979() {}
  290. bool IsProbabilistic() const
  291. {return false;}
  292. bool IsDeterministic() const
  293. {return true;}
  294. // Deterministic K
  295. Integer GenerateRandom(const Integer &x, const Integer &q, const Integer &e) const
  296. {
  297. static const byte zero = 0, one = 1;
  298. const size_t qlen = q.BitCount();
  299. const size_t rlen = BitsToBytes(qlen);
  300. // Step (a) - formatted E(m)
  301. SecByteBlock BH(e.MinEncodedSize());
  302. e.Encode(BH, BH.size());
  303. BH = bits2octets(BH, q);
  304. // Step (a) - private key to byte array
  305. SecByteBlock BX(STDMAX(rlen, x.MinEncodedSize()));
  306. x.Encode(BX, BX.size());
  307. // Step (b)
  308. SecByteBlock V(H::DIGESTSIZE);
  309. std::fill(V.begin(), V.begin()+H::DIGESTSIZE, one);
  310. // Step (c)
  311. SecByteBlock K(H::DIGESTSIZE);
  312. std::fill(K.begin(), K.begin()+H::DIGESTSIZE, zero);
  313. // Step (d)
  314. m_hmac.SetKey(K, K.size());
  315. m_hmac.Update(V, V.size());
  316. m_hmac.Update(&zero, 1);
  317. m_hmac.Update(BX, BX.size());
  318. m_hmac.Update(BH, BH.size());
  319. m_hmac.TruncatedFinal(K, K.size());
  320. // Step (e)
  321. m_hmac.SetKey(K, K.size());
  322. m_hmac.Update(V, V.size());
  323. m_hmac.TruncatedFinal(V, V.size());
  324. // Step (f)
  325. m_hmac.SetKey(K, K.size());
  326. m_hmac.Update(V, V.size());
  327. m_hmac.Update(&one, 1);
  328. m_hmac.Update(BX, BX.size());
  329. m_hmac.Update(BH, BH.size());
  330. m_hmac.TruncatedFinal(K, K.size());
  331. // Step (g)
  332. m_hmac.SetKey(K, K.size());
  333. m_hmac.Update(V, V.size());
  334. m_hmac.TruncatedFinal(V, V.size());
  335. Integer k;
  336. SecByteBlock temp(rlen);
  337. for (;;)
  338. {
  339. // We want qlen bits, but we support only hash functions with an output length
  340. // multiple of 8; hence, we will gather rlen bits, i.e., rolen octets.
  341. size_t toff = 0;
  342. while (toff < rlen)
  343. {
  344. m_hmac.Update(V, V.size());
  345. m_hmac.TruncatedFinal(V, V.size());
  346. size_t cc = STDMIN(V.size(), temp.size() - toff);
  347. memcpy_s(temp+toff, temp.size() - toff, V, cc);
  348. toff += cc;
  349. }
  350. k = bits2int(temp, qlen);
  351. if (k > 0 && k < q)
  352. break;
  353. // k is not in the proper range; update K and V, and loop.
  354. m_hmac.Update(V, V.size());
  355. m_hmac.Update(&zero, 1);
  356. m_hmac.TruncatedFinal(K, K.size());
  357. m_hmac.SetKey(K, K.size());
  358. m_hmac.Update(V, V.size());
  359. m_hmac.TruncatedFinal(V, V.size());
  360. }
  361. return k;
  362. }
  363. protected:
  364. Integer bits2int(const SecByteBlock& bits, size_t qlen) const
  365. {
  366. Integer ret(bits, bits.size());
  367. size_t blen = bits.size()*8;
  368. if (blen > qlen)
  369. ret >>= blen - qlen;
  370. return ret;
  371. }
  372. // RFC 6979 support function. Takes an integer and converts it into bytes that
  373. // are the same length as an elliptic curve's order.
  374. SecByteBlock int2octets(const Integer& val, size_t rlen) const
  375. {
  376. SecByteBlock block(val.MinEncodedSize());
  377. val.Encode(block, val.MinEncodedSize());
  378. if (block.size() == rlen)
  379. return block;
  380. // The least significant bytes are the ones we need to preserve.
  381. SecByteBlock t(rlen);
  382. if (block.size() > rlen)
  383. {
  384. size_t offset = block.size() - rlen;
  385. std::memcpy(t, block + offset, rlen);
  386. }
  387. else // block.size() < rlen
  388. {
  389. size_t offset = rlen - block.size();
  390. memset(t, '\x00', offset);
  391. std::memcpy(t + offset, block, rlen - offset);
  392. }
  393. return t;
  394. }
  395. // Turn a stream of bits into a set of bytes with the same length as an elliptic
  396. // curve's order.
  397. SecByteBlock bits2octets(const SecByteBlock& in, const Integer& q) const
  398. {
  399. Integer b2 = bits2int(in, q.BitCount());
  400. Integer b1 = b2 - q;
  401. return int2octets(b1.IsNegative() ? b2 : b1, q.ByteCount());
  402. }
  403. private:
  404. mutable H m_hash;
  405. mutable HMAC<H> m_hmac;
  406. };
  407. /// \brief German Digital Signature Algorithm
  408. /// \tparam T FieldElement type or class
  409. /// \details FieldElement <tt>T</tt> can be Integer, ECP or EC2N.
  410. /// \details The Digital Signature Scheme ECGDSA does not define the algorithm over integers. Rather, the
  411. /// signature algorithm is only defined over elliptic curves. However, the library design is such that the
  412. /// generic algorithm reside in <tt>gfpcrypt.h</tt>.
  413. /// \sa Erwin Hess, Marcus Schafheutle, and Pascale Serf <A HREF="http://www.teletrust.de/fileadmin/files/oid/ecgdsa_final.pdf">
  414. /// The Digital Signature Scheme ECGDSA (October 24, 2006)</A>
  415. template <class T>
  416. class DL_Algorithm_GDSA_ISO15946 : public DL_ElgamalLikeSignatureAlgorithm<T>
  417. {
  418. public:
  419. CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "GDSA-ISO15946";}
  420. virtual ~DL_Algorithm_GDSA_ISO15946() {}
  421. void Sign(const DL_GroupParameters<T> &params, const Integer &x, const Integer &k, const Integer &e, Integer &r, Integer &s) const
  422. {
  423. const Integer &q = params.GetSubgroupOrder();
  424. // r = x(k * G) mod q
  425. r = params.ConvertElementToInteger(params.ExponentiateBase(k)) % q;
  426. // s = (k * r - h(m)) * d_A mod q
  427. s = (k * r - e) * x % q;
  428. CRYPTOPP_ASSERT(!!r && !!s);
  429. }
  430. bool Verify(const DL_GroupParameters<T> &params, const DL_PublicKey<T> &publicKey, const Integer &e, const Integer &r, const Integer &s) const
  431. {
  432. const Integer &q = params.GetSubgroupOrder();
  433. if (r>=q || r<1 || s>=q || s<1)
  434. return false;
  435. const Integer& rInv = r.InverseMod(q);
  436. const Integer u1 = (rInv * e) % q;
  437. const Integer u2 = (rInv * s) % q;
  438. // verify x(G^u1 + P_A^u2) mod q
  439. return r == params.ConvertElementToInteger(publicKey.CascadeExponentiateBaseAndPublicElement(u1, u2)) % q;
  440. }
  441. };
  442. CRYPTOPP_DLL_TEMPLATE_CLASS DL_Algorithm_GDSA<Integer>;
  443. CRYPTOPP_DLL_TEMPLATE_CLASS DL_Algorithm_DSA_RFC6979<Integer, SHA1>;
  444. CRYPTOPP_DLL_TEMPLATE_CLASS DL_Algorithm_DSA_RFC6979<Integer, SHA224>;
  445. CRYPTOPP_DLL_TEMPLATE_CLASS DL_Algorithm_DSA_RFC6979<Integer, SHA256>;
  446. CRYPTOPP_DLL_TEMPLATE_CLASS DL_Algorithm_DSA_RFC6979<Integer, SHA384>;
  447. CRYPTOPP_DLL_TEMPLATE_CLASS DL_Algorithm_DSA_RFC6979<Integer, SHA512>;
  448. /// \brief NR algorithm
  449. /// \tparam T FieldElement type or class
  450. /// \details FieldElement <tt>T</tt> can be Integer, ECP or EC2N.
  451. template <class T>
  452. class DL_Algorithm_NR : public DL_ElgamalLikeSignatureAlgorithm<T>
  453. {
  454. public:
  455. CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "NR";}
  456. virtual ~DL_Algorithm_NR() {}
  457. void Sign(const DL_GroupParameters<T> &params, const Integer &x, const Integer &k, const Integer &e, Integer &r, Integer &s) const
  458. {
  459. const Integer &q = params.GetSubgroupOrder();
  460. r = (r + e) % q;
  461. s = (k - x*r) % q;
  462. CRYPTOPP_ASSERT(!!r);
  463. }
  464. bool Verify(const DL_GroupParameters<T> &params, const DL_PublicKey<T> &publicKey, const Integer &e, const Integer &r, const Integer &s) const
  465. {
  466. const Integer &q = params.GetSubgroupOrder();
  467. if (r>=q || r<1 || s>=q)
  468. return false;
  469. // check r == (m_g^s * m_y^r + m) mod m_q
  470. return r == (params.ConvertElementToInteger(publicKey.CascadeExponentiateBaseAndPublicElement(s, r)) + e) % q;
  471. }
  472. };
  473. /// \brief Discrete Log (DL) public key in GF(p) groups
  474. /// \tparam GP GroupParameters derived class
  475. /// \details DSA public key format is defined in 7.3.3 of RFC 2459. The private key format is defined in 12.9 of PKCS #11 v2.10.
  476. template <class GP>
  477. class DL_PublicKey_GFP : public DL_PublicKeyImpl<GP>
  478. {
  479. public:
  480. virtual ~DL_PublicKey_GFP() {}
  481. /// \brief Initialize a public key over GF(p)
  482. /// \param params the group parameters
  483. /// \param y the public element
  484. void Initialize(const DL_GroupParameters_IntegerBased &params, const Integer &y)
  485. {this->AccessGroupParameters().Initialize(params); this->SetPublicElement(y);}
  486. /// \brief Initialize a public key over GF(p)
  487. /// \param p the modulus
  488. /// \param g the generator
  489. /// \param y the public element
  490. void Initialize(const Integer &p, const Integer &g, const Integer &y)
  491. {this->AccessGroupParameters().Initialize(p, g); this->SetPublicElement(y);}
  492. /// \brief Initialize a public key over GF(p)
  493. /// \param p the modulus
  494. /// \param q the subgroup order
  495. /// \param g the generator
  496. /// \param y the public element
  497. void Initialize(const Integer &p, const Integer &q, const Integer &g, const Integer &y)
  498. {this->AccessGroupParameters().Initialize(p, q, g); this->SetPublicElement(y);}
  499. // X509PublicKey
  500. void BERDecodePublicKey(BufferedTransformation &bt, bool, size_t)
  501. {this->SetPublicElement(Integer(bt));}
  502. void DEREncodePublicKey(BufferedTransformation &bt) const
  503. {this->GetPublicElement().DEREncode(bt);}
  504. };
  505. /// \brief Discrete Log (DL) private key in GF(p) groups
  506. /// \tparam GP GroupParameters derived class
  507. template <class GP>
  508. class DL_PrivateKey_GFP : public DL_PrivateKeyImpl<GP>
  509. {
  510. public:
  511. virtual ~DL_PrivateKey_GFP();
  512. /// \brief Create a private key
  513. /// \param rng a RandomNumberGenerator derived class
  514. /// \param modulusBits the size of the modulus, in bits
  515. /// \details This function overload of Initialize() creates a new private key because it
  516. /// takes a RandomNumberGenerator() as a parameter. If you have an existing keypair,
  517. /// then use one of the other Initialize() overloads.
  518. void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits)
  519. {this->GenerateRandomWithKeySize(rng, modulusBits);}
  520. /// \brief Create a private key
  521. /// \param rng a RandomNumberGenerator derived class
  522. /// \param p the modulus
  523. /// \param g the generator
  524. /// \details This function overload of Initialize() creates a new private key because it
  525. /// takes a RandomNumberGenerator() as a parameter. If you have an existing keypair,
  526. /// then use one of the other Initialize() overloads.
  527. void Initialize(RandomNumberGenerator &rng, const Integer &p, const Integer &g)
  528. {this->GenerateRandom(rng, MakeParameters("Modulus", p)("SubgroupGenerator", g));}
  529. /// \brief Create a private key
  530. /// \param rng a RandomNumberGenerator derived class
  531. /// \param p the modulus
  532. /// \param q the subgroup order
  533. /// \param g the generator
  534. /// \details This function overload of Initialize() creates a new private key because it
  535. /// takes a RandomNumberGenerator() as a parameter. If you have an existing keypair,
  536. /// then use one of the other Initialize() overloads.
  537. void Initialize(RandomNumberGenerator &rng, const Integer &p, const Integer &q, const Integer &g)
  538. {this->GenerateRandom(rng, MakeParameters("Modulus", p)("SubgroupOrder", q)("SubgroupGenerator", g));}
  539. /// \brief Initialize a private key over GF(p)
  540. /// \param params the group parameters
  541. /// \param x the private exponent
  542. void Initialize(const DL_GroupParameters_IntegerBased &params, const Integer &x)
  543. {this->AccessGroupParameters().Initialize(params); this->SetPrivateExponent(x);}
  544. /// \brief Initialize a private key over GF(p)
  545. /// \param p the modulus
  546. /// \param g the generator
  547. /// \param x the private exponent
  548. void Initialize(const Integer &p, const Integer &g, const Integer &x)
  549. {this->AccessGroupParameters().Initialize(p, g); this->SetPrivateExponent(x);}
  550. /// \brief Initialize a private key over GF(p)
  551. /// \param p the modulus
  552. /// \param q the subgroup order
  553. /// \param g the generator
  554. /// \param x the private exponent
  555. void Initialize(const Integer &p, const Integer &q, const Integer &g, const Integer &x)
  556. {this->AccessGroupParameters().Initialize(p, q, g); this->SetPrivateExponent(x);}
  557. };
  558. // Out-of-line dtor due to AIX and GCC, http://github.com/weidai11/cryptopp/issues/499
  559. template <class GP>
  560. DL_PrivateKey_GFP<GP>::~DL_PrivateKey_GFP() {}
  561. /// \brief Discrete Log (DL) signing/verification keys in GF(p) groups
  562. struct DL_SignatureKeys_GFP
  563. {
  564. typedef DL_GroupParameters_GFP GroupParameters;
  565. typedef DL_PublicKey_GFP<GroupParameters> PublicKey;
  566. typedef DL_PrivateKey_GFP<GroupParameters> PrivateKey;
  567. };
  568. /// \brief Discrete Log (DL) encryption/decryption keys in GF(p) groups
  569. struct DL_CryptoKeys_GFP
  570. {
  571. typedef DL_GroupParameters_GFP_DefaultSafePrime GroupParameters;
  572. typedef DL_PublicKey_GFP<GroupParameters> PublicKey;
  573. typedef DL_PrivateKey_GFP<GroupParameters> PrivateKey;
  574. };
  575. /// ElGamal encryption keys for safe interop
  576. /// \sa <A HREF="https://eprint.iacr.org/2021/923.pdf">On the
  577. /// (in)security of ElGamal in OpenPGP</A>,
  578. /// <A HREF="https://github.com/weidai11/cryptopp/issues/1059">Issue 1059</A>,
  579. /// <A HREF="https://nvd.nist.gov/vuln/detail/CVE-2021-40530">CVE-2021-40530</A>
  580. /// \since Crypto++ 8.6
  581. struct DL_CryptoKeys_ElGamal
  582. {
  583. typedef DL_GroupParameters_ElGamal GroupParameters;
  584. typedef DL_PublicKey_GFP<GroupParameters> PublicKey;
  585. typedef DL_PrivateKey_GFP<GroupParameters> PrivateKey;
  586. };
  587. /// \brief DSA signature scheme
  588. /// \tparam H HashTransformation derived class
  589. /// \sa <a href="http://www.weidai.com/scan-mirror/sig.html#DSA-1363">DSA-1363</a>
  590. /// \since Crypto++ 1.0 for DSA, Crypto++ 5.6.2 for DSA2
  591. template <class H>
  592. struct GDSA : public DL_SS<
  593. DL_SignatureKeys_GFP,
  594. DL_Algorithm_GDSA<Integer>,
  595. DL_SignatureMessageEncodingMethod_DSA,
  596. H>
  597. {
  598. };
  599. /// \brief NR signature scheme
  600. /// \tparam H HashTransformation derived class
  601. /// \sa <a href="http://www.weidai.com/scan-mirror/sig.html#NR">NR</a>
  602. template <class H>
  603. struct NR : public DL_SS<
  604. DL_SignatureKeys_GFP,
  605. DL_Algorithm_NR<Integer>,
  606. DL_SignatureMessageEncodingMethod_NR,
  607. H>
  608. {
  609. };
  610. /// \brief DSA group parameters
  611. /// \details These are GF(p) group parameters that are allowed by the DSA standard
  612. /// \sa DL_Keys_DSA
  613. /// \since Crypto++ 1.0
  614. class CRYPTOPP_DLL DL_GroupParameters_DSA : public DL_GroupParameters_GFP
  615. {
  616. public:
  617. virtual ~DL_GroupParameters_DSA() {}
  618. /// \brief Check the group for errors
  619. /// \param rng RandomNumberGenerator for objects which use randomized testing
  620. /// \param level level of thoroughness
  621. /// \return true if the tests succeed, false otherwise
  622. /// \details ValidateGroup() also checks that the lengths of p and q are allowed
  623. /// by the DSA standard.
  624. /// \details There are four levels of thoroughness:
  625. /// <ul>
  626. /// <li>0 - using this object won't cause a crash or exception
  627. /// <li>1 - this object will probably function, and encrypt, sign, other operations correctly
  628. /// <li>2 - ensure this object will function correctly, and perform reasonable security checks
  629. /// <li>3 - perform reasonable security checks, and do checks that may take a long time
  630. /// </ul>
  631. /// \details Level 0 does not require a RandomNumberGenerator. A NullRNG() can be used for level 0.
  632. /// Level 1 may not check for weak keys and such. Levels 2 and 3 are recommended.
  633. bool ValidateGroup(RandomNumberGenerator &rng, unsigned int level) const;
  634. /// \brief Generate a random key or crypto parameters
  635. /// \param rng a RandomNumberGenerator to produce keying material
  636. /// \param alg additional initialization parameters
  637. /// \details NameValuePairs can be ModulusSize alone; or Modulus, SubgroupOrder, and
  638. /// SubgroupGenerator. ModulusSize must be between <tt>DSA::MIN_PRIME_LENGTH</tt> and
  639. /// <tt>DSA::MAX_PRIME_LENGTH</tt>, and divisible by <tt>DSA::PRIME_LENGTH_MULTIPLE</tt>.
  640. /// \details An example of changing the modulus size using NameValuePairs is shown below.
  641. /// <pre>
  642. /// AlgorithmParameters params = MakeParameters
  643. /// (Name::ModulusSize(), 2048);
  644. ///
  645. /// DL_GroupParameters_DSA groupParams;
  646. /// groupParams.GenerateRandom(prng, params);
  647. /// </pre>
  648. /// \throw KeyingErr if a key can't be generated or algorithm parameters are invalid.
  649. void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
  650. /// \brief Check the prime length for errors
  651. /// \param pbits number of bits in the prime number
  652. /// \return true if the tests succeed, false otherwise
  653. static bool CRYPTOPP_API IsValidPrimeLength(unsigned int pbits)
  654. {return pbits >= MIN_PRIME_LENGTH && pbits <= MAX_PRIME_LENGTH && pbits % PRIME_LENGTH_MULTIPLE == 0;}
  655. /// \brief DSA prime length
  656. enum {
  657. /// \brief Minimum prime length
  658. MIN_PRIME_LENGTH = 1024,
  659. /// \brief Maximum prime length
  660. MAX_PRIME_LENGTH = 3072,
  661. /// \brief Prime length multiple
  662. PRIME_LENGTH_MULTIPLE = 1024
  663. };
  664. };
  665. template <class H>
  666. class DSA2;
  667. /// \brief DSA keys
  668. /// \sa DL_GroupParameters_DSA
  669. /// \since Crypto++ 1.0
  670. struct DL_Keys_DSA
  671. {
  672. typedef DL_PublicKey_GFP<DL_GroupParameters_DSA> PublicKey;
  673. typedef DL_PrivateKey_WithSignaturePairwiseConsistencyTest<DL_PrivateKey_GFP<DL_GroupParameters_DSA>, DSA2<SHA1> > PrivateKey;
  674. };
  675. /// \brief DSA signature scheme
  676. /// \tparam H HashTransformation derived class
  677. /// \details The class is named DSA2 instead of DSA for backwards compatibility because
  678. /// DSA was a non-template class.
  679. /// \details DSA default method GenerateRandom uses a 2048-bit modulus and a 224-bit subgoup by default.
  680. /// The modulus can be changed using the following code:
  681. /// <pre>
  682. /// DSA::PrivateKey privateKey;
  683. /// privateKey.GenerateRandomWithKeySize(prng, 2048);
  684. /// </pre>
  685. /// \details The subgroup order can be changed using the following code:
  686. /// <pre>
  687. /// AlgorithmParameters params = MakeParameters
  688. /// (Name::ModulusSize(), 2048)
  689. /// (Name::SubgroupOrderSize(), 256);
  690. ///
  691. /// DSA::PrivateKey privateKey;
  692. /// privateKey.GenerateRandom(prng, params);
  693. /// </pre>
  694. /// \sa <a href="http://en.wikipedia.org/wiki/Digital_Signature_Algorithm">DSA</a>, as specified in FIPS 186-3,
  695. /// <a href="https://www.cryptopp.com/wiki/Digital_Signature_Algorithm">Digital Signature Algorithm</a> on the wiki, and
  696. /// <a href="https://www.cryptopp.com/wiki/NameValuePairs">NameValuePairs</a> on the wiki.
  697. /// \since Crypto++ 1.0 for DSA, Crypto++ 5.6.2 for DSA2, Crypto++ 6.1 for 2048-bit modulus.
  698. template <class H>
  699. class DSA2 : public DL_SS<
  700. DL_Keys_DSA,
  701. DL_Algorithm_GDSA<Integer>,
  702. DL_SignatureMessageEncodingMethod_DSA,
  703. H,
  704. DSA2<H> >
  705. {
  706. public:
  707. static std::string CRYPTOPP_API StaticAlgorithmName() {return "DSA/" + (std::string)H::StaticAlgorithmName();}
  708. };
  709. /// \brief DSA deterministic signature scheme
  710. /// \tparam H HashTransformation derived class
  711. /// \sa <a href="http://www.weidai.com/scan-mirror/sig.html#DSA-1363">DSA-1363</a>
  712. /// \since Crypto++ 1.0 for DSA, Crypto++ 5.6.2 for DSA2
  713. template <class H>
  714. struct DSA_RFC6979 : public DL_SS<
  715. DL_SignatureKeys_GFP,
  716. DL_Algorithm_DSA_RFC6979<Integer, H>,
  717. DL_SignatureMessageEncodingMethod_DSA,
  718. H,
  719. DSA_RFC6979<H> >
  720. {
  721. static std::string CRYPTOPP_API StaticAlgorithmName() {return std::string("DSA-RFC6979/") + H::StaticAlgorithmName();}
  722. };
  723. /// DSA with SHA-1, typedef'd for backwards compatibility
  724. typedef DSA2<SHA1> DSA;
  725. CRYPTOPP_DLL_TEMPLATE_CLASS DL_PublicKey_GFP<DL_GroupParameters_DSA>;
  726. CRYPTOPP_DLL_TEMPLATE_CLASS DL_PrivateKey_GFP<DL_GroupParameters_DSA>;
  727. CRYPTOPP_DLL_TEMPLATE_CLASS DL_PrivateKey_WithSignaturePairwiseConsistencyTest<DL_PrivateKey_GFP<DL_GroupParameters_DSA>, DSA2<SHA1> >;
  728. /// \brief P1363 based XOR Encryption Method
  729. /// \tparam MAC MessageAuthenticationCode derived class used for MAC computation
  730. /// \tparam DHAES_MODE flag indicating DHAES mode
  731. /// \tparam LABEL_OCTETS flag indicating the label is octet count
  732. /// \details DL_EncryptionAlgorithm_Xor is based on an early P1363 draft, which itself appears to be based on an
  733. /// early Certicom SEC-1 draft (or an early SEC-1 draft was based on a P1363 draft). Crypto++ 4.2 used it in its Integrated
  734. /// Ecryption Schemes with <tt>NoCofactorMultiplication</tt>, <tt>DHAES_MODE=false</tt> and <tt>LABEL_OCTETS=true</tt>.
  735. /// \details If you need this method for Crypto++ 4.2 compatibility, then use the ECIES template class with
  736. /// <tt>NoCofactorMultiplication</tt>, <tt>DHAES_MODE=false</tt> and <tt>LABEL_OCTETS=true</tt>.
  737. /// \details If you need this method for Bouncy Castle 1.54 and Botan 1.11 compatibility, then use the ECIES template class with
  738. /// <tt>NoCofactorMultiplication</tt>, <tt>DHAES_MODE=true</tt> and <tt>LABEL_OCTETS=false</tt>.
  739. /// \details Bouncy Castle 1.54 and Botan 1.11 compatibility are the default template parameters.
  740. /// \since Crypto++ 4.0
  741. template <class MAC, bool DHAES_MODE, bool LABEL_OCTETS=false>
  742. class DL_EncryptionAlgorithm_Xor : public DL_SymmetricEncryptionAlgorithm
  743. {
  744. public:
  745. virtual ~DL_EncryptionAlgorithm_Xor() {}
  746. bool ParameterSupported(const char *name) const {return strcmp(name, Name::EncodingParameters()) == 0;}
  747. size_t GetSymmetricKeyLength(size_t plaintextLength) const
  748. {return plaintextLength + static_cast<size_t>(MAC::DEFAULT_KEYLENGTH);}
  749. size_t GetSymmetricCiphertextLength(size_t plaintextLength) const
  750. {return plaintextLength + static_cast<size_t>(MAC::DIGESTSIZE);}
  751. size_t GetMaxSymmetricPlaintextLength(size_t ciphertextLength) const
  752. {return SaturatingSubtract(ciphertextLength, static_cast<size_t>(MAC::DIGESTSIZE));}
  753. void SymmetricEncrypt(RandomNumberGenerator &rng, const byte *key, const byte *plaintext, size_t plaintextLength, byte *ciphertext, const NameValuePairs &parameters) const
  754. {
  755. CRYPTOPP_UNUSED(rng);
  756. const byte *cipherKey = NULLPTR, *macKey = NULLPTR;
  757. if (DHAES_MODE)
  758. {
  759. macKey = key;
  760. cipherKey = key + MAC::DEFAULT_KEYLENGTH;
  761. }
  762. else
  763. {
  764. cipherKey = key;
  765. macKey = key + plaintextLength;
  766. }
  767. ConstByteArrayParameter encodingParameters;
  768. parameters.GetValue(Name::EncodingParameters(), encodingParameters);
  769. if (plaintextLength) // Coverity finding
  770. xorbuf(ciphertext, plaintext, cipherKey, plaintextLength);
  771. MAC mac(macKey);
  772. mac.Update(ciphertext, plaintextLength);
  773. mac.Update(encodingParameters.begin(), encodingParameters.size());
  774. if (DHAES_MODE)
  775. {
  776. byte L[8];
  777. PutWord(false, BIG_ENDIAN_ORDER, L, (LABEL_OCTETS ? word64(encodingParameters.size()) : 8 * word64(encodingParameters.size())));
  778. mac.Update(L, 8);
  779. }
  780. mac.Final(ciphertext + plaintextLength);
  781. }
  782. DecodingResult SymmetricDecrypt(const byte *key, const byte *ciphertext, size_t ciphertextLength, byte *plaintext, const NameValuePairs &parameters) const
  783. {
  784. size_t plaintextLength = GetMaxSymmetricPlaintextLength(ciphertextLength);
  785. const byte *cipherKey, *macKey;
  786. if (DHAES_MODE)
  787. {
  788. macKey = key;
  789. cipherKey = key + MAC::DEFAULT_KEYLENGTH;
  790. }
  791. else
  792. {
  793. cipherKey = key;
  794. macKey = key + plaintextLength;
  795. }
  796. ConstByteArrayParameter encodingParameters;
  797. parameters.GetValue(Name::EncodingParameters(), encodingParameters);
  798. MAC mac(macKey);
  799. mac.Update(ciphertext, plaintextLength);
  800. mac.Update(encodingParameters.begin(), encodingParameters.size());
  801. if (DHAES_MODE)
  802. {
  803. byte L[8];
  804. PutWord(false, BIG_ENDIAN_ORDER, L, (LABEL_OCTETS ? word64(encodingParameters.size()) : 8 * word64(encodingParameters.size())));
  805. mac.Update(L, 8);
  806. }
  807. if (!mac.Verify(ciphertext + plaintextLength))
  808. return DecodingResult();
  809. if (plaintextLength) // Coverity finding
  810. xorbuf(plaintext, ciphertext, cipherKey, plaintextLength);
  811. return DecodingResult(plaintextLength);
  812. }
  813. };
  814. /// \brief P1363 based Key Derivation Method
  815. /// \tparam T FieldElement type or class
  816. /// \tparam DHAES_MODE flag indicating DHAES mode
  817. /// \tparam KDF key derivation function
  818. /// \details FieldElement <tt>T</tt> can be Integer, ECP or EC2N.
  819. template <class T, bool DHAES_MODE, class KDF>
  820. class DL_KeyDerivationAlgorithm_P1363 : public DL_KeyDerivationAlgorithm<T>
  821. {
  822. public:
  823. virtual ~DL_KeyDerivationAlgorithm_P1363() {}
  824. bool ParameterSupported(const char *name) const {return strcmp(name, Name::KeyDerivationParameters()) == 0;}
  825. void Derive(const DL_GroupParameters<T> &params, byte *derivedKey, size_t derivedLength, const T &agreedElement, const T &ephemeralPublicKey, const NameValuePairs &parameters) const
  826. {
  827. SecByteBlock agreedSecret;
  828. if (DHAES_MODE)
  829. {
  830. agreedSecret.New(params.GetEncodedElementSize(true) + params.GetEncodedElementSize(false));
  831. params.EncodeElement(true, ephemeralPublicKey, agreedSecret);
  832. params.EncodeElement(false, agreedElement, agreedSecret + params.GetEncodedElementSize(true));
  833. }
  834. else
  835. {
  836. agreedSecret.New(params.GetEncodedElementSize(false));
  837. params.EncodeElement(false, agreedElement, agreedSecret);
  838. }
  839. ConstByteArrayParameter derivationParameters;
  840. parameters.GetValue(Name::KeyDerivationParameters(), derivationParameters);
  841. KDF::DeriveKey(derivedKey, derivedLength, agreedSecret, agreedSecret.size(), derivationParameters.begin(), derivationParameters.size());
  842. }
  843. };
  844. /// \brief Discrete Log Integrated Encryption Scheme
  845. /// \tparam COFACTOR_OPTION cofactor multiplication option
  846. /// \tparam HASH HashTransformation derived class used for key drivation and MAC computation
  847. /// \tparam DHAES_MODE flag indicating if the MAC includes addition context parameters such as the label
  848. /// \tparam LABEL_OCTETS flag indicating if the label size is specified in octets or bits
  849. /// \details DLIES is an Integer based Integrated Encryption Scheme (IES). The scheme combines a Key Encapsulation Method (KEM)
  850. /// with a Data Encapsulation Method (DEM) and a MAC tag. The scheme is
  851. /// <A HREF="http://en.wikipedia.org/wiki/ciphertext_indistinguishability">IND-CCA2</A>, which is a strong notion of security.
  852. /// You should prefer an Integrated Encryption Scheme over homegrown schemes.
  853. /// \details The library's original implementation is based on an early P1363 draft, which itself appears to be based on an early Certicom
  854. /// SEC-1 draft (or an early SEC-1 draft was based on a P1363 draft). Crypto++ 4.2 used the early draft in its Integrated Ecryption
  855. /// Schemes with <tt>NoCofactorMultiplication</tt>, <tt>DHAES_MODE=false</tt> and <tt>LABEL_OCTETS=true</tt>.
  856. /// \details If you desire an Integrated Encryption Scheme with Crypto++ 4.2 compatibility, then use the DLIES template class with
  857. /// <tt>NoCofactorMultiplication</tt>, <tt>DHAES_MODE=false</tt> and <tt>LABEL_OCTETS=true</tt>.
  858. /// \details If you desire an Integrated Encryption Scheme with Bouncy Castle 1.54 and Botan 1.11 compatibility, then use the DLIES
  859. /// template class with <tt>NoCofactorMultiplication</tt>, <tt>DHAES_MODE=true</tt> and <tt>LABEL_OCTETS=false</tt>.
  860. /// \details The default template parameters ensure compatibility with Bouncy Castle 1.54 and Botan 1.11. The combination of
  861. /// <tt>IncompatibleCofactorMultiplication</tt> and <tt>DHAES_MODE=true</tt> is recommended for best efficiency and security.
  862. /// SHA1 is used for compatibility reasons, but it can be changed if desired. SHA-256 or another hash will likely improve the
  863. /// security provided by the MAC. The hash is also used in the key derivation function as a PRF.
  864. /// \details Below is an example of constructing a Crypto++ 4.2 compatible DLIES encryptor and decryptor.
  865. /// <pre>
  866. /// AutoSeededRandomPool prng;
  867. /// DL_PrivateKey_GFP<DL_GroupParameters_GFP> key;
  868. /// key.Initialize(prng, 2048);
  869. ///
  870. /// DLIES<SHA1,NoCofactorMultiplication,true,true>::Decryptor decryptor(key);
  871. /// DLIES<SHA1,NoCofactorMultiplication,true,true>::Encryptor encryptor(decryptor);
  872. /// </pre>
  873. /// \sa ECIES, <a href="http://www.weidai.com/scan-mirror/ca.html#DLIES">Discrete Log Integrated Encryption Scheme (DLIES)</a>,
  874. /// Martínez, Encinas, and Ávila's <A HREF="http://digital.csic.es/bitstream/10261/32671/1/V2-I2-P7-13.pdf">A Survey of the Elliptic
  875. /// Curve Integrated Encryption Schemes</A>
  876. /// \since Crypto++ 4.0, Crypto++ 5.7 for Bouncy Castle and Botan compatibility
  877. template <class HASH = SHA1, class COFACTOR_OPTION = NoCofactorMultiplication, bool DHAES_MODE = true, bool LABEL_OCTETS=false>
  878. struct DLIES
  879. : public DL_ES<
  880. DL_CryptoKeys_GFP,
  881. DL_KeyAgreementAlgorithm_DH<Integer, COFACTOR_OPTION>,
  882. DL_KeyDerivationAlgorithm_P1363<Integer, DHAES_MODE, P1363_KDF2<HASH> >,
  883. DL_EncryptionAlgorithm_Xor<HMAC<HASH>, DHAES_MODE, LABEL_OCTETS>,
  884. DLIES<> >
  885. {
  886. static std::string CRYPTOPP_API StaticAlgorithmName() {return "DLIES";} // TODO: fix this after name is standardized
  887. };
  888. NAMESPACE_END
  889. #if CRYPTOPP_MSC_VERSION
  890. # pragma warning(pop)
  891. #endif
  892. #endif