Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:55:03

0001 // gfpcrypt.h - originally written and placed in the public domain by Wei Dai

0002 //              RFC6979 deterministic signatures added by Douglas Roark

0003 //              ECGDSA added by Jeffrey Walton

0004 
0005 /// \file gfpcrypt.h

0006 /// \brief Classes and functions for schemes based on Discrete Logs (DL) over GF(p)

0007 
0008 #ifndef CRYPTOPP_GFPCRYPT_H
0009 #define CRYPTOPP_GFPCRYPT_H
0010 
0011 #include "config.h"
0012 
0013 #if CRYPTOPP_MSC_VERSION
0014 # pragma warning(push)
0015 # pragma warning(disable: 4189 4231 4275)
0016 #endif
0017 
0018 #include "cryptlib.h"
0019 #include "pubkey.h"
0020 #include "integer.h"
0021 #include "modexppc.h"
0022 #include "algparam.h"
0023 #include "smartptr.h"
0024 #include "sha.h"
0025 #include "asn.h"
0026 #include "hmac.h"
0027 #include "misc.h"
0028 
0029 NAMESPACE_BEGIN(CryptoPP)
0030 
0031 CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupParameters<Integer>;
0032 
0033 /// \brief Integer-based GroupParameters specialization

0034 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE DL_GroupParameters_IntegerBased : public ASN1CryptoMaterial<DL_GroupParameters<Integer> >
0035 {
0036     typedef DL_GroupParameters_IntegerBased ThisClass;
0037 
0038 public:
0039     virtual ~DL_GroupParameters_IntegerBased() {}
0040 
0041     /// \brief Initialize a group parameters over integers

0042     /// \param params the group parameters

0043     void Initialize(const DL_GroupParameters_IntegerBased &params)
0044         {Initialize(params.GetModulus(), params.GetSubgroupOrder(), params.GetSubgroupGenerator());}
0045 
0046     /// \brief Create a group parameters over integers

0047     /// \param rng a RandomNumberGenerator derived class

0048     /// \param pbits the size of p, in bits

0049     /// \details This function overload of Initialize() creates a new private key because it

0050     ///  takes a RandomNumberGenerator() as a parameter. If you have an existing keypair,

0051     ///  then use one of the other Initialize() overloads.

0052     void Initialize(RandomNumberGenerator &rng, unsigned int pbits)
0053         {GenerateRandom(rng, MakeParameters("ModulusSize", (int)pbits));}
0054 
0055     /// \brief Initialize a group parameters over integers

0056     /// \param p the modulus

0057     /// \param g the generator

0058     void Initialize(const Integer &p, const Integer &g)
0059         {SetModulusAndSubgroupGenerator(p, g); SetSubgroupOrder(ComputeGroupOrder(p)/2);}
0060 
0061     /// \brief Initialize a group parameters over integers

0062     /// \param p the modulus

0063     /// \param q the subgroup order

0064     /// \param g the generator

0065     void Initialize(const Integer &p, const Integer &q, const Integer &g)
0066         {SetModulusAndSubgroupGenerator(p, g); SetSubgroupOrder(q);}
0067 
0068     // ASN1Object interface

0069     void BERDecode(BufferedTransformation &bt);
0070     void DEREncode(BufferedTransformation &bt) const;
0071 
0072     /// \brief Generate a random key

0073     /// \param rng a RandomNumberGenerator to produce keying material

0074     /// \param alg additional initialization parameters

0075     /// \details Recognised NameValuePairs are ModulusSize and

0076     ///  SubgroupOrderSize (optional)

0077     /// \throw KeyingErr if a key can't be generated or algorithm parameters

0078     ///  are invalid

0079     void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
0080 
0081     /// \brief Get a named value

0082     /// \param name the name of the object or value to retrieve

0083     /// \param valueType reference to a variable that receives the value

0084     /// \param pValue void pointer to a variable that receives the value

0085     /// \return true if the value was retrieved, false otherwise

0086     /// \details GetVoidValue() retrieves the value of name if it exists.

0087     /// \note GetVoidValue() is an internal function and should be implemented

0088     ///  by derived classes. Users should use one of the other functions instead.

0089     /// \sa GetValue(), GetValueWithDefault(), GetIntValue(), GetIntValueWithDefault(),

0090     ///  GetRequiredParameter() and GetRequiredIntParameter()

0091     bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
0092 
0093     /// \brief Initialize or reinitialize this key

0094     /// \param source NameValuePairs to assign

0095     void AssignFrom(const NameValuePairs &source);
0096 
0097     // DL_GroupParameters

0098     const Integer & GetSubgroupOrder() const {return m_q;}
0099     Integer GetGroupOrder() const {return GetFieldType() == 1 ? GetModulus()-Integer::One() : GetModulus()+Integer::One();}
0100     bool ValidateGroup(RandomNumberGenerator &rng, unsigned int level) const;
0101     bool ValidateElement(unsigned int level, const Integer &element, const DL_FixedBasePrecomputation<Integer> *precomp) const;
0102 
0103     /// \brief Determine if subgroup membership check is fast

0104     /// \return true or false

0105     bool FastSubgroupCheckAvailable() const {return GetCofactor() == 2;}
0106 
0107     /// \brief Encodes the element

0108     /// \param reversible flag indicating the encoding format

0109     /// \param element reference to the element to encode

0110     /// \param encoded destination byte array for the encoded element

0111     /// \details EncodeElement() must be implemented in a derived class.

0112     /// \pre <tt>COUNTOF(encoded) == GetEncodedElementSize()</tt>

0113     /// \sa GetEncodedElementSize(), DecodeElement(), <A

0114     ///  HREF="http://github.com/weidai11/cryptopp/issues/40">Cygwin

0115     ///  i386 crash at -O3</A>

0116     void EncodeElement(bool reversible, const Element &element, byte *encoded) const;
0117 
0118     /// \brief Retrieve the encoded element's size

0119     /// \param reversible flag indicating the encoding format

0120     /// \return encoded element's size, in bytes

0121     /// \details The format of the encoded element varies by the underlying

0122     ///  type of the element and the reversible flag.

0123     /// \sa EncodeElement(), DecodeElement()

0124     unsigned int GetEncodedElementSize(bool reversible) const;
0125 
0126     /// \brief Decodes the element

0127     /// \param encoded byte array with the encoded element

0128     /// \param checkForGroupMembership flag indicating if the element should be validated

0129     /// \return Element after decoding

0130     /// \details DecodeElement() must be implemented in a derived class.

0131     /// \pre <tt>COUNTOF(encoded) == GetEncodedElementSize()</tt>

0132     /// \sa GetEncodedElementSize(), EncodeElement()

0133     Integer DecodeElement(const byte *encoded, bool checkForGroupMembership) const;
0134 
0135     /// \brief Converts an element to an Integer

0136     /// \param element the element to convert to an Integer

0137     /// \return Element after converting to an Integer

0138     /// \details ConvertElementToInteger() must be implemented in a derived class.

0139     Integer ConvertElementToInteger(const Element &element) const
0140         {return element;}
0141 
0142     /// \brief Retrieve the maximum exponent for the group

0143     /// \return the maximum exponent for the group

0144     Integer GetMaxExponent() const;
0145 
0146     /// \brief Retrieve the OID of the algorithm

0147     /// \return OID of the algorithm

0148     OID GetAlgorithmID() const;
0149 
0150     /// \brief Retrieve the modulus for the group

0151     /// \return the modulus for the group

0152     virtual const Integer & GetModulus() const =0;
0153 
0154     /// \brief Set group parameters

0155     /// \param p the prime modulus

0156     /// \param g the group generator

0157     virtual void SetModulusAndSubgroupGenerator(const Integer &p, const Integer &g) =0;
0158 
0159     /// \brief Set subgroup order

0160     /// \param q the subgroup order

0161     void SetSubgroupOrder(const Integer &q)
0162         {m_q = q; ParametersChanged();}
0163 
0164     static std::string CRYPTOPP_API StaticAlgorithmNamePrefix() {return "";}
0165 
0166 protected:
0167     Integer ComputeGroupOrder(const Integer &modulus) const
0168         {return modulus-(GetFieldType() == 1 ? 1 : -1);}
0169 
0170     // GF(p) = 1, GF(p^2) = 2

0171     virtual int GetFieldType() const =0;
0172     virtual unsigned int GetDefaultSubgroupOrderSize(unsigned int modulusSize) const;
0173 
0174 private:
0175     Integer m_q;
0176 };
0177 
0178 /// \brief Integer-based GroupParameters default implementation

0179 /// \tparam GROUP_PRECOMP group parameters precomputation specialization

0180 /// \tparam BASE_PRECOMP base class precomputation specialization

0181 template <class GROUP_PRECOMP, class BASE_PRECOMP = DL_FixedBasePrecomputationImpl<typename GROUP_PRECOMP::Element> >
0182 class CRYPTOPP_NO_VTABLE DL_GroupParameters_IntegerBasedImpl : public DL_GroupParametersImpl<GROUP_PRECOMP, BASE_PRECOMP, DL_GroupParameters_IntegerBased>
0183 {
0184     typedef DL_GroupParameters_IntegerBasedImpl<GROUP_PRECOMP, BASE_PRECOMP> ThisClass;
0185 
0186 public:
0187     typedef typename GROUP_PRECOMP::Element Element;
0188 
0189     virtual ~DL_GroupParameters_IntegerBasedImpl() {}
0190 
0191     // GeneratibleCryptoMaterial interface

0192     bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
0193         {return GetValueHelper<DL_GroupParameters_IntegerBased>(this, name, valueType, pValue).Assignable();}
0194 
0195     void AssignFrom(const NameValuePairs &source)
0196         {AssignFromHelper<DL_GroupParameters_IntegerBased>(this, source);}
0197 
0198     // DL_GroupParameters

0199     const DL_FixedBasePrecomputation<Element> & GetBasePrecomputation() const {return this->m_gpc;}
0200     DL_FixedBasePrecomputation<Element> & AccessBasePrecomputation() {return this->m_gpc;}
0201 
0202     // IntegerGroupParameters

0203     /// \brief Retrieve the modulus for the group

0204     /// \return the modulus for the group

0205     const Integer & GetModulus() const {return this->m_groupPrecomputation.GetModulus();}
0206 
0207     /// \brief Retrieves a reference to the group generator

0208     /// \return const reference to the group generator

0209     const Integer & GetGenerator() const {return this->m_gpc.GetBase(this->GetGroupPrecomputation());}
0210 
0211     void SetModulusAndSubgroupGenerator(const Integer &p, const Integer &g)        // these have to be set together

0212         {this->m_groupPrecomputation.SetModulus(p); this->m_gpc.SetBase(this->GetGroupPrecomputation(), g); this->ParametersChanged();}
0213 
0214     // non-inherited

0215     bool operator==(const DL_GroupParameters_IntegerBasedImpl<GROUP_PRECOMP, BASE_PRECOMP> &rhs) const
0216         {return GetModulus() == rhs.GetModulus() && GetGenerator() == rhs.GetGenerator() && this->GetSubgroupOrder() == rhs.GetSubgroupOrder();}
0217     bool operator!=(const DL_GroupParameters_IntegerBasedImpl<GROUP_PRECOMP, BASE_PRECOMP> &rhs) const
0218         {return !operator==(rhs);}
0219 };
0220 
0221 CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupParameters_IntegerBasedImpl<ModExpPrecomputation>;
0222 
0223 /// \brief GF(p) group parameters

0224 class CRYPTOPP_DLL DL_GroupParameters_GFP : public DL_GroupParameters_IntegerBasedImpl<ModExpPrecomputation>
0225 {
0226 public:
0227     virtual ~DL_GroupParameters_GFP() {}
0228 
0229     /// \brief Determines if an element is an identity

0230     /// \param element element to check

0231     /// \return true if the element is an identity, false otherwise

0232     /// \details The identity element or or neutral element is a special element

0233     ///  in a group that leaves other elements unchanged when combined with it.

0234     /// \details IsIdentity() must be implemented in a derived class.

0235     bool IsIdentity(const Integer &element) const {return element == Integer::One();}
0236 
0237     /// \brief Exponentiates a base to multiple exponents

0238     /// \param results an array of Elements

0239     /// \param base the base to raise to the exponents

0240     /// \param exponents an array of exponents

0241     /// \param exponentsCount the number of exponents in the array

0242     /// \details SimultaneousExponentiate() raises the base to each exponent in

0243     ///  the exponents array and stores the result at the respective position in

0244     ///  the results array.

0245     /// \details SimultaneousExponentiate() must be implemented in a derived class.

0246     /// \pre <tt>COUNTOF(results) == exponentsCount</tt>

0247     /// \pre <tt>COUNTOF(exponents) == exponentsCount</tt>

0248     void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const;
0249 
0250     /// \brief Get a named value

0251     /// \param name the name of the object or value to retrieve

0252     /// \param valueType reference to a variable that receives the value

0253     /// \param pValue void pointer to a variable that receives the value

0254     /// \return true if the value was retrieved, false otherwise

0255     /// \details GetVoidValue() retrieves the value of name if it exists.

0256     /// \note GetVoidValue() is an internal function and should be implemented

0257     ///  by derived classes. Users should use one of the other functions instead.

0258     /// \sa GetValue(), GetValueWithDefault(), GetIntValue(), GetIntValueWithDefault(),

0259     ///  GetRequiredParameter() and GetRequiredIntParameter()

0260     bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
0261     {
0262         return GetValueHelper<DL_GroupParameters_IntegerBased>(this, name, valueType, pValue).Assignable();
0263     }
0264 
0265     // used by MQV

0266     Element MultiplyElements(const Element &a, const Element &b) const;
0267     Element CascadeExponentiate(const Element &element1, const Integer &exponent1, const Element &element2, const Integer &exponent2) const;
0268 
0269 protected:
0270     int GetFieldType() const {return 1;}
0271 };
0272 
0273 /// \brief GF(p) group parameters that default to safe primes

0274 class CRYPTOPP_DLL DL_GroupParameters_GFP_DefaultSafePrime : public DL_GroupParameters_GFP
0275 {
0276 public:
0277     typedef NoCofactorMultiplication DefaultCofactorOption;
0278 
0279     virtual ~DL_GroupParameters_GFP_DefaultSafePrime() {}
0280 
0281 protected:
0282     unsigned int GetDefaultSubgroupOrderSize(unsigned int modulusSize) const {return modulusSize-1;}
0283 };
0284 
0285 /// ElGamal encryption for safe interop

0286 /// \sa <A HREF="https://eprint.iacr.org/2021/923.pdf">On the

0287 ///  (in)security of ElGamal in OpenPGP</A>,

0288 ///  <A HREF="https://github.com/weidai11/cryptopp/issues/1059">Issue 1059</A>,

0289 ///  <A HREF="https://nvd.nist.gov/vuln/detail/CVE-2021-40530">CVE-2021-40530</A>

0290 /// \since Crypto++ 8.6

0291 class CRYPTOPP_DLL DL_GroupParameters_ElGamal : public DL_GroupParameters_GFP_DefaultSafePrime
0292 {
0293 public:
0294     typedef NoCofactorMultiplication DefaultCofactorOption;
0295 
0296     virtual ~DL_GroupParameters_ElGamal() {}
0297 
0298     Integer GetMaxExponent() const
0299     {
0300         return GetSubgroupOrder()-1;
0301     }
0302 };
0303 
0304 /// \brief GDSA algorithm

0305 /// \tparam T FieldElement type or class

0306 /// \details FieldElement <tt>T</tt> can be Integer, ECP or EC2N.

0307 template <class T>
0308 class DL_Algorithm_GDSA : public DL_ElgamalLikeSignatureAlgorithm<T>
0309 {
0310 public:
0311     CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "DSA-1363";}
0312 
0313     virtual ~DL_Algorithm_GDSA() {}
0314 
0315     void Sign(const DL_GroupParameters<T> &params, const Integer &x, const Integer &k, const Integer &e, Integer &r, Integer &s) const
0316     {
0317         const Integer &q = params.GetSubgroupOrder();
0318         r %= q;
0319         Integer kInv = k.InverseMod(q);
0320         s = (kInv * (x*r + e)) % q;
0321         CRYPTOPP_ASSERT(!!r && !!s);
0322     }
0323 
0324     bool Verify(const DL_GroupParameters<T> &params, const DL_PublicKey<T> &publicKey, const Integer &e, const Integer &r, const Integer &s) const
0325     {
0326         const Integer &q = params.GetSubgroupOrder();
0327         if (r>=q || r<1 || s>=q || s<1)
0328             return false;
0329 
0330         Integer w = s.InverseMod(q);
0331         Integer u1 = (e * w) % q;
0332         Integer u2 = (r * w) % q;
0333         // verify r == (g^u1 * y^u2 mod p) mod q

0334         return r == params.ConvertElementToInteger(publicKey.CascadeExponentiateBaseAndPublicElement(u1, u2)) % q;
0335     }
0336 };
0337 
0338 /// \brief DSA signature algorithm based on RFC 6979

0339 /// \tparam T FieldElement type or class

0340 /// \tparam H HashTransformation derived class

0341 /// \details FieldElement <tt>T</tt> can be Integer, ECP or EC2N.

0342 /// \sa <a href="http://tools.ietf.org/rfc/rfc6979.txt">RFC 6979, Deterministic Usage of the

0343 ///  Digital Signature Algorithm (DSA) and Elliptic Curve Digital Signature Algorithm (ECDSA)</a>

0344 /// \since Crypto++ 6.0

0345 template <class T, class H>
0346 class DL_Algorithm_DSA_RFC6979 : public DL_Algorithm_GDSA<T>, public DeterministicSignatureAlgorithm
0347 {
0348 public:
0349     CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "DSA-RFC6979";}
0350 
0351     virtual ~DL_Algorithm_DSA_RFC6979() {}
0352 
0353     bool IsProbabilistic() const
0354         {return false;}
0355     bool IsDeterministic() const
0356         {return true;}
0357 
0358     // Deterministic K

0359     Integer GenerateRandom(const Integer &x, const Integer &q, const Integer &e) const
0360     {
0361         static const byte zero = 0, one = 1;
0362         const size_t qlen = q.BitCount();
0363         const size_t rlen = BitsToBytes(qlen);
0364 
0365         // Step (a) - formatted E(m)

0366         SecByteBlock BH(e.MinEncodedSize());
0367         e.Encode(BH, BH.size());
0368         BH = bits2octets(BH, q);
0369 
0370         // Step (a) - private key to byte array

0371         SecByteBlock BX(STDMAX(rlen, x.MinEncodedSize()));
0372         x.Encode(BX, BX.size());
0373 
0374         // Step (b)

0375         SecByteBlock V(H::DIGESTSIZE);
0376         std::fill(V.begin(), V.begin()+H::DIGESTSIZE, one);
0377 
0378         // Step (c)

0379         SecByteBlock K(H::DIGESTSIZE);
0380         std::fill(K.begin(), K.begin()+H::DIGESTSIZE, zero);
0381 
0382         // Step (d)

0383         m_hmac.SetKey(K, K.size());
0384         m_hmac.Update(V, V.size());
0385         m_hmac.Update(&zero, 1);
0386         m_hmac.Update(BX, BX.size());
0387         m_hmac.Update(BH, BH.size());
0388         m_hmac.TruncatedFinal(K, K.size());
0389 
0390         // Step (e)

0391         m_hmac.SetKey(K, K.size());
0392         m_hmac.Update(V, V.size());
0393         m_hmac.TruncatedFinal(V, V.size());
0394 
0395         // Step (f)

0396         m_hmac.SetKey(K, K.size());
0397         m_hmac.Update(V, V.size());
0398         m_hmac.Update(&one, 1);
0399         m_hmac.Update(BX, BX.size());
0400         m_hmac.Update(BH, BH.size());
0401         m_hmac.TruncatedFinal(K, K.size());
0402 
0403         // Step (g)

0404         m_hmac.SetKey(K, K.size());
0405         m_hmac.Update(V, V.size());
0406         m_hmac.TruncatedFinal(V, V.size());
0407 
0408         Integer k;
0409         SecByteBlock temp(rlen);
0410         for (;;)
0411         {
0412             // We want qlen bits, but we support only hash functions with an output length

0413             //   multiple of 8; hence, we will gather rlen bits, i.e., rolen octets.

0414             size_t toff = 0;
0415             while (toff < rlen)
0416             {
0417                 m_hmac.Update(V, V.size());
0418                 m_hmac.TruncatedFinal(V, V.size());
0419 
0420                 size_t cc = STDMIN(V.size(), temp.size() - toff);
0421                 memcpy_s(temp+toff, temp.size() - toff, V, cc);
0422                 toff += cc;
0423             }
0424 
0425             k = bits2int(temp, qlen);
0426             if (k > 0 && k < q)
0427                 break;
0428 
0429             // k is not in the proper range; update K and V, and loop.

0430             m_hmac.Update(V, V.size());
0431             m_hmac.Update(&zero, 1);
0432             m_hmac.TruncatedFinal(K, K.size());
0433 
0434             m_hmac.SetKey(K, K.size());
0435             m_hmac.Update(V, V.size());
0436             m_hmac.TruncatedFinal(V, V.size());
0437         }
0438 
0439         return k;
0440     }
0441 
0442 protected:
0443 
0444     Integer bits2int(const SecByteBlock& bits, size_t qlen) const
0445     {
0446         Integer ret(bits, bits.size());
0447         size_t blen = bits.size()*8;
0448 
0449         if (blen > qlen)
0450             ret >>= blen - qlen;
0451 
0452         return ret;
0453     }
0454 
0455     // RFC 6979 support function. Takes an integer and converts it into bytes that

0456     // are the same length as an elliptic curve's order.

0457     SecByteBlock int2octets(const Integer& val, size_t rlen) const
0458     {
0459         SecByteBlock block(val.MinEncodedSize());
0460         val.Encode(block, val.MinEncodedSize());
0461 
0462         if (block.size() == rlen)
0463             return block;
0464 
0465         // The least significant bytes are the ones we need to preserve.

0466         SecByteBlock t(rlen);
0467         if (block.size() > rlen)
0468         {
0469             size_t offset = block.size() - rlen;
0470             std::memcpy(t, block + offset, rlen);
0471         }
0472         else // block.size() < rlen

0473         {
0474             size_t offset = rlen - block.size();
0475             std::memset(t, '\x00', offset);
0476             std::memcpy(t + offset, block, rlen - offset);
0477         }
0478 
0479         return t;
0480     }
0481 
0482     // Turn a stream of bits into a set of bytes with the same length as an elliptic

0483     // curve's order.

0484     SecByteBlock bits2octets(const SecByteBlock& in, const Integer& q) const
0485     {
0486         Integer b2 = bits2int(in, q.BitCount());
0487         Integer b1 = b2 - q;
0488         return int2octets(b1.IsNegative() ? b2 : b1, q.ByteCount());
0489     }
0490 
0491 private:
0492     mutable H m_hash;
0493     mutable HMAC<H> m_hmac;
0494 };
0495 
0496 /// \brief German Digital Signature Algorithm

0497 /// \tparam T FieldElement type or class

0498 /// \details FieldElement <tt>T</tt> can be Integer, ECP or EC2N.

0499 /// \details The Digital Signature Scheme ECGDSA does not define the algorithm over integers. Rather, the

0500 ///  signature algorithm is only defined over elliptic curves. However, the library design is such that the

0501 ///  generic algorithm reside in <tt>gfpcrypt.h</tt>.

0502 /// \sa Erwin Hess, Marcus Schafheutle, and Pascale Serf <A HREF="http://www.teletrust.de/fileadmin/files/oid/ecgdsa_final.pdf">

0503 ///  The Digital Signature Scheme ECGDSA (October 24, 2006)</A>

0504 template <class T>
0505 class DL_Algorithm_GDSA_ISO15946 : public DL_ElgamalLikeSignatureAlgorithm<T>
0506 {
0507 public:
0508     CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "GDSA-ISO15946";}
0509 
0510     virtual ~DL_Algorithm_GDSA_ISO15946() {}
0511 
0512     void Sign(const DL_GroupParameters<T> &params, const Integer &x, const Integer &k, const Integer &e, Integer &r, Integer &s) const
0513     {
0514         const Integer &q = params.GetSubgroupOrder();
0515         // r = x(k * G) mod q

0516         r = params.ConvertElementToInteger(params.ExponentiateBase(k)) % q;
0517         // s = (k * r - h(m)) * d_A mod q

0518         s = (k * r - e) * x % q;
0519         CRYPTOPP_ASSERT(!!r && !!s);
0520     }
0521 
0522     bool Verify(const DL_GroupParameters<T> &params, const DL_PublicKey<T> &publicKey, const Integer &e, const Integer &r, const Integer &s) const
0523     {
0524         const Integer &q = params.GetSubgroupOrder();
0525         if (r>=q || r<1 || s>=q || s<1)
0526             return false;
0527 
0528         const Integer& rInv = r.InverseMod(q);
0529         const Integer u1 = (rInv * e) % q;
0530         const Integer u2 = (rInv * s) % q;
0531         // verify x(G^u1 + P_A^u2) mod q

0532         return r == params.ConvertElementToInteger(publicKey.CascadeExponentiateBaseAndPublicElement(u1, u2)) % q;
0533     }
0534 };
0535 
0536 CRYPTOPP_DLL_TEMPLATE_CLASS DL_Algorithm_GDSA<Integer>;
0537 CRYPTOPP_DLL_TEMPLATE_CLASS DL_Algorithm_DSA_RFC6979<Integer, SHA1>;
0538 CRYPTOPP_DLL_TEMPLATE_CLASS DL_Algorithm_DSA_RFC6979<Integer, SHA224>;
0539 CRYPTOPP_DLL_TEMPLATE_CLASS DL_Algorithm_DSA_RFC6979<Integer, SHA256>;
0540 CRYPTOPP_DLL_TEMPLATE_CLASS DL_Algorithm_DSA_RFC6979<Integer, SHA384>;
0541 CRYPTOPP_DLL_TEMPLATE_CLASS DL_Algorithm_DSA_RFC6979<Integer, SHA512>;
0542 
0543 /// \brief NR algorithm

0544 /// \tparam T FieldElement type or class

0545 /// \details FieldElement <tt>T</tt> can be Integer, ECP or EC2N.

0546 template <class T>
0547 class DL_Algorithm_NR : public DL_ElgamalLikeSignatureAlgorithm<T>
0548 {
0549 public:
0550     CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "NR";}
0551 
0552     virtual ~DL_Algorithm_NR() {}
0553 
0554     void Sign(const DL_GroupParameters<T> &params, const Integer &x, const Integer &k, const Integer &e, Integer &r, Integer &s) const
0555     {
0556         const Integer &q = params.GetSubgroupOrder();
0557         r = (r + e) % q;
0558         s = (k - x*r) % q;
0559         CRYPTOPP_ASSERT(!!r);
0560     }
0561 
0562     bool Verify(const DL_GroupParameters<T> &params, const DL_PublicKey<T> &publicKey, const Integer &e, const Integer &r, const Integer &s) const
0563     {
0564         const Integer &q = params.GetSubgroupOrder();
0565         if (r>=q || r<1 || s>=q)
0566             return false;
0567 
0568         // check r == (m_g^s * m_y^r + m) mod m_q

0569         return r == (params.ConvertElementToInteger(publicKey.CascadeExponentiateBaseAndPublicElement(s, r)) + e) % q;
0570     }
0571 };
0572 
0573 /// \brief Discrete Log (DL) public key in GF(p) groups

0574 /// \tparam GP GroupParameters derived class

0575 /// \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.

0576 template <class GP>
0577 class DL_PublicKey_GFP : public DL_PublicKeyImpl<GP>
0578 {
0579 public:
0580     virtual ~DL_PublicKey_GFP() {}
0581 
0582     /// \brief Initialize a public key over GF(p)

0583     /// \param params the group parameters

0584     /// \param y the public element

0585     void Initialize(const DL_GroupParameters_IntegerBased &params, const Integer &y)
0586         {this->AccessGroupParameters().Initialize(params); this->SetPublicElement(y);}
0587 
0588     /// \brief Initialize a public key over GF(p)

0589     /// \param p the modulus

0590     /// \param g the generator

0591     /// \param y the public element

0592     void Initialize(const Integer &p, const Integer &g, const Integer &y)
0593         {this->AccessGroupParameters().Initialize(p, g); this->SetPublicElement(y);}
0594 
0595     /// \brief Initialize a public key over GF(p)

0596     /// \param p the modulus

0597     /// \param q the subgroup order

0598     /// \param g the generator

0599     /// \param y the public element

0600     void Initialize(const Integer &p, const Integer &q, const Integer &g, const Integer &y)
0601         {this->AccessGroupParameters().Initialize(p, q, g); this->SetPublicElement(y);}
0602 
0603     // X509PublicKey

0604     void BERDecodePublicKey(BufferedTransformation &bt, bool, size_t)
0605         {this->SetPublicElement(Integer(bt));}
0606     void DEREncodePublicKey(BufferedTransformation &bt) const
0607         {this->GetPublicElement().DEREncode(bt);}
0608 };
0609 
0610 /// \brief Discrete Log (DL) private key in GF(p) groups

0611 /// \tparam GP GroupParameters derived class

0612 template <class GP>
0613 class DL_PrivateKey_GFP : public DL_PrivateKeyImpl<GP>
0614 {
0615 public:
0616     virtual ~DL_PrivateKey_GFP();
0617 
0618     /// \brief Create a private key

0619     /// \param rng a RandomNumberGenerator derived class

0620     /// \param modulusBits the size of the modulus, in bits

0621     /// \details This function overload of Initialize() creates a new private key because it

0622     ///  takes a RandomNumberGenerator() as a parameter. If you have an existing keypair,

0623     ///  then use one of the other Initialize() overloads.

0624     void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits)
0625         {this->GenerateRandomWithKeySize(rng, modulusBits);}
0626 
0627     /// \brief Create a private key

0628     /// \param rng a RandomNumberGenerator derived class

0629     /// \param p the modulus

0630     /// \param g the generator

0631     /// \details This function overload of Initialize() creates a new private key because it

0632     ///  takes a RandomNumberGenerator() as a parameter. If you have an existing keypair,

0633     ///  then use one of the other Initialize() overloads.

0634     void Initialize(RandomNumberGenerator &rng, const Integer &p, const Integer &g)
0635         {this->GenerateRandom(rng, MakeParameters("Modulus", p)("SubgroupGenerator", g));}
0636 
0637     /// \brief Create a private key

0638     /// \param rng a RandomNumberGenerator derived class

0639     /// \param p the modulus

0640     /// \param q the subgroup order

0641     /// \param g the generator

0642     /// \details This function overload of Initialize() creates a new private key because it

0643     ///  takes a RandomNumberGenerator() as a parameter. If you have an existing keypair,

0644     ///  then use one of the other Initialize() overloads.

0645     void Initialize(RandomNumberGenerator &rng, const Integer &p, const Integer &q, const Integer &g)
0646         {this->GenerateRandom(rng, MakeParameters("Modulus", p)("SubgroupOrder", q)("SubgroupGenerator", g));}
0647 
0648     /// \brief Initialize a private key over GF(p)

0649     /// \param params the group parameters

0650     /// \param x the private exponent

0651     void Initialize(const DL_GroupParameters_IntegerBased &params, const Integer &x)
0652         {this->AccessGroupParameters().Initialize(params); this->SetPrivateExponent(x);}
0653 
0654     /// \brief Initialize a private key over GF(p)

0655     /// \param p the modulus

0656     /// \param g the generator

0657     /// \param x the private exponent

0658     void Initialize(const Integer &p, const Integer &g, const Integer &x)
0659         {this->AccessGroupParameters().Initialize(p, g); this->SetPrivateExponent(x);}
0660 
0661     /// \brief Initialize a private key over GF(p)

0662     /// \param p the modulus

0663     /// \param q the subgroup order

0664     /// \param g the generator

0665     /// \param x the private exponent

0666     void Initialize(const Integer &p, const Integer &q, const Integer &g, const Integer &x)
0667         {this->AccessGroupParameters().Initialize(p, q, g); this->SetPrivateExponent(x);}
0668 };
0669 
0670 // Out-of-line dtor due to AIX and GCC, http://github.com/weidai11/cryptopp/issues/499

0671 template <class GP>
0672 DL_PrivateKey_GFP<GP>::~DL_PrivateKey_GFP() {}
0673 
0674 /// \brief Discrete Log (DL) signing/verification keys in GF(p) groups

0675 struct DL_SignatureKeys_GFP
0676 {
0677     typedef DL_GroupParameters_GFP GroupParameters;
0678     typedef DL_PublicKey_GFP<GroupParameters> PublicKey;
0679     typedef DL_PrivateKey_GFP<GroupParameters> PrivateKey;
0680 };
0681 
0682 /// \brief Discrete Log (DL) encryption/decryption keys in GF(p) groups

0683 struct DL_CryptoKeys_GFP
0684 {
0685     typedef DL_GroupParameters_GFP_DefaultSafePrime GroupParameters;
0686     typedef DL_PublicKey_GFP<GroupParameters> PublicKey;
0687     typedef DL_PrivateKey_GFP<GroupParameters> PrivateKey;
0688 };
0689 
0690 /// ElGamal encryption keys for safe interop

0691 /// \sa <A HREF="https://eprint.iacr.org/2021/923.pdf">On the

0692 ///  (in)security of ElGamal in OpenPGP</A>,

0693 ///  <A HREF="https://github.com/weidai11/cryptopp/issues/1059">Issue 1059</A>,

0694 ///  <A HREF="https://nvd.nist.gov/vuln/detail/CVE-2021-40530">CVE-2021-40530</A>

0695 /// \since Crypto++ 8.6

0696 struct DL_CryptoKeys_ElGamal
0697 {
0698     typedef DL_GroupParameters_ElGamal GroupParameters;
0699     typedef DL_PublicKey_GFP<GroupParameters> PublicKey;
0700     typedef DL_PrivateKey_GFP<GroupParameters> PrivateKey;
0701 };
0702 
0703 /// \brief DSA signature scheme

0704 /// \tparam H HashTransformation derived class

0705 /// \sa <a href="http://www.weidai.com/scan-mirror/sig.html#DSA-1363">DSA-1363</a>

0706 /// \since Crypto++ 1.0 for DSA, Crypto++ 5.6.2 for DSA2

0707 template <class H>
0708 struct GDSA : public DL_SS<
0709     DL_SignatureKeys_GFP,
0710     DL_Algorithm_GDSA<Integer>,
0711     DL_SignatureMessageEncodingMethod_DSA,
0712     H>
0713 {
0714 };
0715 
0716 /// \brief NR signature scheme

0717 /// \tparam H HashTransformation derived class

0718 /// \sa <a href="http://www.weidai.com/scan-mirror/sig.html#NR">NR</a>

0719 template <class H>
0720 struct NR : public DL_SS<
0721     DL_SignatureKeys_GFP,
0722     DL_Algorithm_NR<Integer>,
0723     DL_SignatureMessageEncodingMethod_NR,
0724     H>
0725 {
0726 };
0727 
0728 /// \brief DSA group parameters

0729 /// \details These are GF(p) group parameters that are allowed by the DSA standard

0730 /// \sa DL_Keys_DSA

0731 /// \since Crypto++ 1.0

0732 class CRYPTOPP_DLL DL_GroupParameters_DSA : public DL_GroupParameters_GFP
0733 {
0734 public:
0735     virtual ~DL_GroupParameters_DSA() {}
0736 
0737     /// \brief Check the group for errors

0738     /// \param rng RandomNumberGenerator for objects which use randomized testing

0739     /// \param level level of thoroughness

0740     /// \return true if the tests succeed, false otherwise

0741     /// \details ValidateGroup() also checks that the lengths of p and q are allowed

0742     ///  by the DSA standard.

0743     /// \details There are four levels of thoroughness:

0744     ///  <ul>

0745     ///  <li>0 - using this object won't cause a crash or exception

0746     ///  <li>1 - this object will probably function, and encrypt, sign, other operations correctly

0747     ///  <li>2 - ensure this object will function correctly, and perform reasonable security checks

0748     ///  <li>3 - perform reasonable security checks, and do checks that may take a long time

0749     ///  </ul>

0750     /// \details Level 0 does not require a RandomNumberGenerator. A NullRNG() can be used for level 0.

0751     ///  Level 1 may not check for weak keys and such. Levels 2 and 3 are recommended.

0752     bool ValidateGroup(RandomNumberGenerator &rng, unsigned int level) const;
0753 
0754     /// \brief Generate a random key or crypto parameters

0755     /// \param rng a RandomNumberGenerator to produce keying material

0756     /// \param alg additional initialization parameters

0757     /// \details NameValuePairs can be ModulusSize alone; or Modulus, SubgroupOrder, and

0758     ///  SubgroupGenerator. ModulusSize must be between <tt>DSA::MIN_PRIME_LENGTH</tt> and

0759     ///  <tt>DSA::MAX_PRIME_LENGTH</tt>, and divisible by <tt>DSA::PRIME_LENGTH_MULTIPLE</tt>.

0760     /// \details An example of changing the modulus size using NameValuePairs is shown below.

0761     /// <pre>

0762     ///  AlgorithmParameters params = MakeParameters

0763     ///    (Name::ModulusSize(), 2048);

0764     ///

0765     ///  DL_GroupParameters_DSA groupParams;

0766     ///  groupParams.GenerateRandom(prng, params);

0767     /// </pre>

0768     /// \throw KeyingErr if a key can't be generated or algorithm parameters are invalid.

0769     void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
0770 
0771     /// \brief Check the prime length for errors

0772     /// \param pbits number of bits in the prime number

0773     /// \return true if the tests succeed, false otherwise

0774     static bool CRYPTOPP_API IsValidPrimeLength(unsigned int pbits)
0775         {return pbits >= MIN_PRIME_LENGTH && pbits <= MAX_PRIME_LENGTH && pbits % PRIME_LENGTH_MULTIPLE == 0;}
0776 
0777     /// \brief DSA prime length

0778     enum {
0779         /// \brief Minimum prime length

0780         MIN_PRIME_LENGTH = 1024,
0781         /// \brief Maximum prime length

0782         MAX_PRIME_LENGTH = 3072,
0783         /// \brief Prime length multiple

0784         PRIME_LENGTH_MULTIPLE = 1024
0785     };
0786 };
0787 
0788 template <class H>
0789 class DSA2;
0790 
0791 /// \brief DSA keys

0792 /// \sa DL_GroupParameters_DSA

0793 /// \since Crypto++ 1.0

0794 struct DL_Keys_DSA
0795 {
0796     typedef DL_PublicKey_GFP<DL_GroupParameters_DSA> PublicKey;
0797     typedef DL_PrivateKey_WithSignaturePairwiseConsistencyTest<DL_PrivateKey_GFP<DL_GroupParameters_DSA>, DSA2<SHA1> > PrivateKey;
0798 };
0799 
0800 /// \brief DSA signature scheme

0801 /// \tparam H HashTransformation derived class

0802 /// \details The class is named DSA2 instead of DSA for backwards compatibility because

0803 ///  DSA was a non-template class.

0804 /// \details DSA default method GenerateRandom uses a 2048-bit modulus and a 224-bit subgoup by default.

0805 ///  The modulus can be changed using the following code:

0806 /// <pre>

0807 ///  DSA::PrivateKey privateKey;

0808 ///  privateKey.GenerateRandomWithKeySize(prng, 2048);

0809 /// </pre>

0810 /// \details The subgroup order can be changed using the following code:

0811 /// <pre>

0812 ///  AlgorithmParameters params = MakeParameters

0813 ///    (Name::ModulusSize(), 2048)

0814 ///    (Name::SubgroupOrderSize(), 256);

0815 ///

0816 ///  DSA::PrivateKey privateKey;

0817 ///  privateKey.GenerateRandom(prng, params);

0818 /// </pre>

0819 /// \sa <a href="http://en.wikipedia.org/wiki/Digital_Signature_Algorithm">DSA</a>, as specified in FIPS 186-3,

0820 ///  <a href="https://www.cryptopp.com/wiki/Digital_Signature_Algorithm">Digital Signature Algorithm</a> on the wiki, and

0821 ///  <a href="https://www.cryptopp.com/wiki/NameValuePairs">NameValuePairs</a> on the wiki.

0822 /// \since Crypto++ 1.0 for DSA, Crypto++ 5.6.2 for DSA2, Crypto++ 6.1 for 2048-bit modulus.

0823 template <class H>
0824 class DSA2 : public DL_SS<
0825     DL_Keys_DSA,
0826     DL_Algorithm_GDSA<Integer>,
0827     DL_SignatureMessageEncodingMethod_DSA,
0828     H,
0829     DSA2<H> >
0830 {
0831 public:
0832     static std::string CRYPTOPP_API StaticAlgorithmName() {return "DSA/" + (std::string)H::StaticAlgorithmName();}
0833 };
0834 
0835 /// \brief DSA deterministic signature scheme

0836 /// \tparam H HashTransformation derived class

0837 /// \sa <a href="http://www.weidai.com/scan-mirror/sig.html#DSA-1363">DSA-1363</a>

0838 /// \since Crypto++ 1.0 for DSA, Crypto++ 5.6.2 for DSA2

0839 template <class H>
0840 struct DSA_RFC6979 : public DL_SS<
0841     DL_SignatureKeys_GFP,
0842     DL_Algorithm_DSA_RFC6979<Integer, H>,
0843     DL_SignatureMessageEncodingMethod_DSA,
0844     H,
0845     DSA_RFC6979<H> >
0846 {
0847     static std::string CRYPTOPP_API StaticAlgorithmName() {return std::string("DSA-RFC6979/") + H::StaticAlgorithmName();}
0848 };
0849 
0850 /// DSA with SHA-1, typedef'd for backwards compatibility

0851 typedef DSA2<SHA1> DSA;
0852 
0853 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PublicKey_GFP<DL_GroupParameters_DSA>;
0854 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PrivateKey_GFP<DL_GroupParameters_DSA>;
0855 CRYPTOPP_DLL_TEMPLATE_CLASS DL_PrivateKey_WithSignaturePairwiseConsistencyTest<DL_PrivateKey_GFP<DL_GroupParameters_DSA>, DSA2<SHA1> >;
0856 
0857 /// \brief P1363 based XOR Encryption Method

0858 /// \tparam MAC MessageAuthenticationCode derived class used for MAC computation

0859 /// \tparam DHAES_MODE flag indicating DHAES mode

0860 /// \tparam LABEL_OCTETS flag indicating the label is octet count

0861 /// \details DL_EncryptionAlgorithm_Xor is based on an early P1363 draft, which itself appears to be based on an

0862 ///  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

0863 ///  Ecryption Schemes with <tt>NoCofactorMultiplication</tt>, <tt>DHAES_MODE=false</tt> and <tt>LABEL_OCTETS=true</tt>.

0864 /// \details If you need this method for Crypto++ 4.2 compatibility, then use the ECIES template class with

0865 ///  <tt>NoCofactorMultiplication</tt>, <tt>DHAES_MODE=false</tt> and <tt>LABEL_OCTETS=true</tt>.

0866 /// \details If you need this method for Bouncy Castle 1.54 and Botan 1.11 compatibility, then use the ECIES template class with

0867 ///  <tt>NoCofactorMultiplication</tt>, <tt>DHAES_MODE=true</tt> and <tt>LABEL_OCTETS=false</tt>.

0868 /// \details Bouncy Castle 1.54 and Botan 1.11 compatibility are the default template parameters.

0869 /// \since Crypto++ 4.0

0870 template <class MAC, bool DHAES_MODE, bool LABEL_OCTETS=false>
0871 class DL_EncryptionAlgorithm_Xor : public DL_SymmetricEncryptionAlgorithm
0872 {
0873 public:
0874     virtual ~DL_EncryptionAlgorithm_Xor() {}
0875 
0876     bool ParameterSupported(const char *name) const {return strcmp(name, Name::EncodingParameters()) == 0;}
0877     size_t GetSymmetricKeyLength(size_t plaintextLength) const
0878         {return plaintextLength + static_cast<size_t>(MAC::DEFAULT_KEYLENGTH);}
0879     size_t GetSymmetricCiphertextLength(size_t plaintextLength) const
0880         {return plaintextLength + static_cast<size_t>(MAC::DIGESTSIZE);}
0881     size_t GetMaxSymmetricPlaintextLength(size_t ciphertextLength) const
0882         {return SaturatingSubtract(ciphertextLength, static_cast<size_t>(MAC::DIGESTSIZE));}
0883     void SymmetricEncrypt(RandomNumberGenerator &rng, const byte *key, const byte *plaintext, size_t plaintextLength, byte *ciphertext, const NameValuePairs &parameters) const
0884     {
0885         CRYPTOPP_UNUSED(rng);
0886         const byte *cipherKey = NULLPTR, *macKey = NULLPTR;
0887         if (DHAES_MODE)
0888         {
0889             macKey = key;
0890             cipherKey = key + MAC::DEFAULT_KEYLENGTH;
0891         }
0892         else
0893         {
0894             cipherKey = key;
0895             macKey = key + plaintextLength;
0896         }
0897 
0898         ConstByteArrayParameter encodingParameters;
0899         parameters.GetValue(Name::EncodingParameters(), encodingParameters);
0900 
0901         if (plaintextLength)    // Coverity finding

0902             xorbuf(ciphertext, plaintext, cipherKey, plaintextLength);
0903 
0904         MAC mac(macKey);
0905         mac.Update(ciphertext, plaintextLength);
0906         mac.Update(encodingParameters.begin(), encodingParameters.size());
0907         if (DHAES_MODE)
0908         {
0909             byte L[8];
0910             PutWord(false, BIG_ENDIAN_ORDER, L, (LABEL_OCTETS ? word64(encodingParameters.size()) : 8 * word64(encodingParameters.size())));
0911             mac.Update(L, 8);
0912         }
0913         mac.Final(ciphertext + plaintextLength);
0914     }
0915     DecodingResult SymmetricDecrypt(const byte *key, const byte *ciphertext, size_t ciphertextLength, byte *plaintext, const NameValuePairs &parameters) const
0916     {
0917         size_t plaintextLength = GetMaxSymmetricPlaintextLength(ciphertextLength);
0918         const byte *cipherKey, *macKey;
0919         if (DHAES_MODE)
0920         {
0921             macKey = key;
0922             cipherKey = key + MAC::DEFAULT_KEYLENGTH;
0923         }
0924         else
0925         {
0926             cipherKey = key;
0927             macKey = key + plaintextLength;
0928         }
0929 
0930         ConstByteArrayParameter encodingParameters;
0931         parameters.GetValue(Name::EncodingParameters(), encodingParameters);
0932 
0933         MAC mac(macKey);
0934         mac.Update(ciphertext, plaintextLength);
0935         mac.Update(encodingParameters.begin(), encodingParameters.size());
0936         if (DHAES_MODE)
0937         {
0938             byte L[8];
0939             PutWord(false, BIG_ENDIAN_ORDER, L, (LABEL_OCTETS ? word64(encodingParameters.size()) : 8 * word64(encodingParameters.size())));
0940             mac.Update(L, 8);
0941         }
0942         if (!mac.Verify(ciphertext + plaintextLength))
0943             return DecodingResult();
0944 
0945         if (plaintextLength)    // Coverity finding

0946             xorbuf(plaintext, ciphertext, cipherKey, plaintextLength);
0947 
0948         return DecodingResult(plaintextLength);
0949     }
0950 };
0951 
0952 /// \brief P1363 based Key Derivation Method

0953 /// \tparam T FieldElement type or class

0954 /// \tparam DHAES_MODE flag indicating DHAES mode

0955 /// \tparam KDF key derivation function

0956 /// \details FieldElement <tt>T</tt> can be Integer, ECP or EC2N.

0957 template <class T, bool DHAES_MODE, class KDF>
0958 class DL_KeyDerivationAlgorithm_P1363 : public DL_KeyDerivationAlgorithm<T>
0959 {
0960 public:
0961     virtual ~DL_KeyDerivationAlgorithm_P1363() {}
0962 
0963     bool ParameterSupported(const char *name) const {return strcmp(name, Name::KeyDerivationParameters()) == 0;}
0964     void Derive(const DL_GroupParameters<T> &params, byte *derivedKey, size_t derivedLength, const T &agreedElement, const T &ephemeralPublicKey, const NameValuePairs &parameters) const
0965     {
0966         SecByteBlock agreedSecret;
0967         if (DHAES_MODE)
0968         {
0969             agreedSecret.New(params.GetEncodedElementSize(true) + params.GetEncodedElementSize(false));
0970             params.EncodeElement(true, ephemeralPublicKey, agreedSecret);
0971             params.EncodeElement(false, agreedElement, agreedSecret + params.GetEncodedElementSize(true));
0972         }
0973         else
0974         {
0975             agreedSecret.New(params.GetEncodedElementSize(false));
0976             params.EncodeElement(false, agreedElement, agreedSecret);
0977         }
0978 
0979         ConstByteArrayParameter derivationParameters;
0980         parameters.GetValue(Name::KeyDerivationParameters(), derivationParameters);
0981         KDF::DeriveKey(derivedKey, derivedLength, agreedSecret, agreedSecret.size(), derivationParameters.begin(), derivationParameters.size());
0982     }
0983 };
0984 
0985 /// \brief Discrete Log Integrated Encryption Scheme

0986 /// \tparam COFACTOR_OPTION cofactor multiplication option

0987 /// \tparam HASH HashTransformation derived class used for key drivation and MAC computation

0988 /// \tparam DHAES_MODE flag indicating if the MAC includes addition context parameters such as the label

0989 /// \tparam LABEL_OCTETS flag indicating if the label size is specified in octets or bits

0990 /// \details DLIES is an Integer based Integrated Encryption Scheme (IES). The scheme combines a Key Encapsulation Method (KEM)

0991 ///  with a Data Encapsulation Method (DEM) and a MAC tag. The scheme is

0992 ///  <A HREF="http://en.wikipedia.org/wiki/ciphertext_indistinguishability">IND-CCA2</A>, which is a strong notion of security.

0993 ///  You should prefer an Integrated Encryption Scheme over homegrown schemes.

0994 /// \details The library's original implementation is based on an early P1363 draft, which itself appears to be based on an early Certicom

0995 ///  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

0996 ///  Schemes with <tt>NoCofactorMultiplication</tt>, <tt>DHAES_MODE=false</tt> and <tt>LABEL_OCTETS=true</tt>.

0997 /// \details If you desire an Integrated Encryption Scheme with Crypto++ 4.2 compatibility, then use the DLIES template class with

0998 ///  <tt>NoCofactorMultiplication</tt>, <tt>DHAES_MODE=false</tt> and <tt>LABEL_OCTETS=true</tt>.

0999 /// \details If you desire an Integrated Encryption Scheme with Bouncy Castle 1.54 and Botan 1.11 compatibility, then use the DLIES

1000 ///  template class with <tt>NoCofactorMultiplication</tt>, <tt>DHAES_MODE=true</tt> and <tt>LABEL_OCTETS=false</tt>.

1001 /// \details The default template parameters ensure compatibility with Bouncy Castle 1.54 and Botan 1.11. The combination of

1002 ///  <tt>IncompatibleCofactorMultiplication</tt> and <tt>DHAES_MODE=true</tt> is recommended for best efficiency and security.

1003 ///  SHA1 is used for compatibility reasons, but it can be changed if desired. SHA-256 or another hash will likely improve the

1004 ///  security provided by the MAC. The hash is also used in the key derivation function as a PRF.

1005 /// \details Below is an example of constructing a Crypto++ 4.2 compatible DLIES encryptor and decryptor.

1006 /// <pre>

1007 ///    AutoSeededRandomPool prng;

1008 ///    DL_PrivateKey_GFP<DL_GroupParameters_GFP> key;

1009 ///    key.Initialize(prng, 2048);

1010 ///

1011 ///    DLIES<SHA1,NoCofactorMultiplication,true,true>::Decryptor decryptor(key);

1012 ///    DLIES<SHA1,NoCofactorMultiplication,true,true>::Encryptor encryptor(decryptor);

1013 /// </pre>

1014 /// \sa ECIES, <a href="http://www.weidai.com/scan-mirror/ca.html#DLIES">Discrete Log Integrated Encryption Scheme (DLIES)</a>,

1015 ///  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

1016 ///  Curve Integrated Encryption Schemes</A>

1017 /// \since Crypto++ 4.0, Crypto++ 5.7 for Bouncy Castle and Botan compatibility

1018 template <class HASH = SHA1, class COFACTOR_OPTION = NoCofactorMultiplication, bool DHAES_MODE = true, bool LABEL_OCTETS=false>
1019 struct DLIES
1020     : public DL_ES<
1021         DL_CryptoKeys_GFP,
1022         DL_KeyAgreementAlgorithm_DH<Integer, COFACTOR_OPTION>,
1023         DL_KeyDerivationAlgorithm_P1363<Integer, DHAES_MODE, P1363_KDF2<HASH> >,
1024         DL_EncryptionAlgorithm_Xor<HMAC<HASH>, DHAES_MODE, LABEL_OCTETS>,
1025         DLIES<> >
1026 {
1027     static std::string CRYPTOPP_API StaticAlgorithmName() {return "DLIES";}    // TODO: fix this after name is standardized

1028 };
1029 
1030 NAMESPACE_END
1031 
1032 #if CRYPTOPP_MSC_VERSION
1033 # pragma warning(pop)
1034 #endif
1035 
1036 #endif