Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 lines
8.1 KiB

  1. #ifndef CRYPTOPP_LUC_H
  2. #define CRYPTOPP_LUC_H
  3. /** \file
  4. */
  5. #include "pkcspad.h"
  6. #include "oaep.h"
  7. #include "integer.h"
  8. #include "dh.h"
  9. #include <limits.h>
  10. NAMESPACE_BEGIN(CryptoPP)
  11. //! The LUC function.
  12. /*! This class is here for historical and pedagogical interest. It has no
  13. practical advantages over other trapdoor functions and probably shouldn't
  14. be used in production software. The discrete log based LUC schemes
  15. defined later in this .h file may be of more practical interest.
  16. */
  17. class LUCFunction : public TrapdoorFunction, public PublicKey
  18. {
  19. typedef LUCFunction ThisClass;
  20. public:
  21. void Initialize(const Integer &n, const Integer &e)
  22. {m_n = n; m_e = e;}
  23. void BERDecode(BufferedTransformation &bt);
  24. void DEREncode(BufferedTransformation &bt) const;
  25. Integer ApplyFunction(const Integer &x) const;
  26. Integer PreimageBound() const {return m_n;}
  27. Integer ImageBound() const {return m_n;}
  28. bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
  29. bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
  30. void AssignFrom(const NameValuePairs &source);
  31. // non-derived interface
  32. const Integer & GetModulus() const {return m_n;}
  33. const Integer & GetPublicExponent() const {return m_e;}
  34. void SetModulus(const Integer &n) {m_n = n;}
  35. void SetPublicExponent(const Integer &e) {m_e = e;}
  36. protected:
  37. Integer m_n, m_e;
  38. };
  39. //! _
  40. class InvertibleLUCFunction : public LUCFunction, public TrapdoorFunctionInverse, public PrivateKey
  41. {
  42. typedef InvertibleLUCFunction ThisClass;
  43. public:
  44. void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits, const Integer &eStart=17);
  45. void Initialize(const Integer &n, const Integer &e, const Integer &p, const Integer &q, const Integer &u)
  46. {m_n = n; m_e = e; m_p = p; m_q = q; m_u = u;}
  47. void BERDecode(BufferedTransformation &bt);
  48. void DEREncode(BufferedTransformation &bt) const;
  49. Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const;
  50. bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
  51. bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
  52. void AssignFrom(const NameValuePairs &source);
  53. /*! parameters: (ModulusSize, PublicExponent (default 17)) */
  54. void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
  55. // non-derived interface
  56. const Integer& GetPrime1() const {return m_p;}
  57. const Integer& GetPrime2() const {return m_q;}
  58. const Integer& GetMultiplicativeInverseOfPrime2ModPrime1() const {return m_u;}
  59. void SetPrime1(const Integer &p) {m_p = p;}
  60. void SetPrime2(const Integer &q) {m_q = q;}
  61. void SetMultiplicativeInverseOfPrime2ModPrime1(const Integer &u) {m_u = u;}
  62. protected:
  63. Integer m_p, m_q, m_u;
  64. };
  65. struct LUC
  66. {
  67. static std::string StaticAlgorithmName() {return "LUC";}
  68. typedef LUCFunction PublicKey;
  69. typedef InvertibleLUCFunction PrivateKey;
  70. };
  71. //! LUC cryptosystem
  72. template <class STANDARD>
  73. struct LUCES : public TF_ES<STANDARD, LUC>
  74. {
  75. };
  76. //! LUC signature scheme with appendix
  77. template <class STANDARD, class H>
  78. struct LUCSS : public TF_SS<STANDARD, H, LUC>
  79. {
  80. };
  81. // analagous to the RSA schemes defined in PKCS #1 v2.0
  82. typedef LUCES<OAEP<SHA> >::Decryptor LUCES_OAEP_SHA_Decryptor;
  83. typedef LUCES<OAEP<SHA> >::Encryptor LUCES_OAEP_SHA_Encryptor;
  84. typedef LUCSS<PKCS1v15, SHA>::Signer LUCSSA_PKCS1v15_SHA_Signer;
  85. typedef LUCSS<PKCS1v15, SHA>::Verifier LUCSSA_PKCS1v15_SHA_Verifier;
  86. // ********************************************************
  87. // no actual precomputation
  88. class DL_GroupPrecomputation_LUC : public DL_GroupPrecomputation<Integer>
  89. {
  90. public:
  91. const AbstractGroup<Element> & GetGroup() const {assert(false); throw 0;}
  92. Element BERDecodeElement(BufferedTransformation &bt) const {return Integer(bt);}
  93. void DEREncodeElement(BufferedTransformation &bt, const Element &v) const {v.DEREncode(bt);}
  94. // non-inherited
  95. void SetModulus(const Integer &v) {m_p = v;}
  96. const Integer & GetModulus() const {return m_p;}
  97. private:
  98. Integer m_p;
  99. };
  100. //! _
  101. class DL_BasePrecomputation_LUC : public DL_FixedBasePrecomputation<Integer>
  102. {
  103. public:
  104. // DL_FixedBasePrecomputation
  105. bool IsInitialized() const {return m_g.NotZero();}
  106. void SetBase(const DL_GroupPrecomputation<Element> &group, const Integer &base) {m_g = base;}
  107. const Integer & GetBase(const DL_GroupPrecomputation<Element> &group) const {return m_g;}
  108. void Precompute(const DL_GroupPrecomputation<Element> &group, unsigned int maxExpBits, unsigned int storage) {}
  109. void Load(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation) {}
  110. void Save(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation) const {}
  111. Integer Exponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent) const;
  112. Integer CascadeExponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent, const DL_FixedBasePrecomputation<Integer> &pc2, const Integer &exponent2) const
  113. {throw NotImplemented("DL_BasePrecomputation_LUC: CascadeExponentiate not implemented");} // shouldn't be called
  114. private:
  115. Integer m_g;
  116. };
  117. //! _
  118. class DL_GroupParameters_LUC : public DL_GroupParameters_IntegerBasedImpl<DL_GroupPrecomputation_LUC, DL_BasePrecomputation_LUC>
  119. {
  120. public:
  121. // DL_GroupParameters
  122. bool IsIdentity(const Integer &element) const {return element == Integer::Two();}
  123. void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const;
  124. Element MultiplyElements(const Element &a, const Element &b) const
  125. {throw NotImplemented("LUC_GroupParameters: MultiplyElements can not be implemented");}
  126. Element CascadeExponentiate(const Element &element1, const Integer &exponent1, const Element &element2, const Integer &exponent2) const
  127. {throw NotImplemented("LUC_GroupParameters: MultiplyElements can not be implemented");}
  128. // NameValuePairs interface
  129. bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
  130. {
  131. return GetValueHelper<DL_GroupParameters_IntegerBased>(this, name, valueType, pValue).Assignable();
  132. }
  133. private:
  134. int GetFieldType() const {return 2;}
  135. };
  136. //! _
  137. class DL_GroupParameters_LUC_DefaultSafePrime : public DL_GroupParameters_LUC
  138. {
  139. public:
  140. typedef NoCofactorMultiplication DefaultCofactorOption;
  141. protected:
  142. unsigned int GetDefaultSubgroupOrderSize(unsigned int modulusSize) const {return modulusSize-1;}
  143. };
  144. //! _
  145. class DL_Algorithm_LUC_HMP : public DL_ElgamalLikeSignatureAlgorithm<Integer>
  146. {
  147. public:
  148. static const char * StaticAlgorithmName() {return "LUC-HMP";}
  149. void Sign(const DL_GroupParameters<Integer> &params, const Integer &x, const Integer &k, const Integer &e, Integer &r, Integer &s) const;
  150. bool Verify(const DL_GroupParameters<Integer> &params, const DL_PublicKey<Integer> &publicKey, const Integer &e, const Integer &r, const Integer &s) const;
  151. size_t RLen(const DL_GroupParameters<Integer> &params) const
  152. {return params.GetGroupOrder().ByteCount();}
  153. };
  154. //! _
  155. struct DL_SignatureKeys_LUC
  156. {
  157. typedef DL_GroupParameters_LUC GroupParameters;
  158. typedef DL_PublicKey_GFP<GroupParameters> PublicKey;
  159. typedef DL_PrivateKey_GFP<GroupParameters> PrivateKey;
  160. };
  161. //! LUC-HMP, based on "Digital signature schemes based on Lucas functions" by Patrick Horster, Markus Michels, Holger Petersen
  162. template <class H>
  163. struct LUC_HMP : public DL_SS<DL_SignatureKeys_LUC, DL_Algorithm_LUC_HMP, DL_SignatureMessageEncodingMethod_DSA, H>
  164. {
  165. };
  166. //! _
  167. struct DL_CryptoKeys_LUC
  168. {
  169. typedef DL_GroupParameters_LUC_DefaultSafePrime GroupParameters;
  170. typedef DL_PublicKey_GFP<GroupParameters> PublicKey;
  171. typedef DL_PrivateKey_GFP<GroupParameters> PrivateKey;
  172. };
  173. //! LUC-IES
  174. template <class COFACTOR_OPTION = NoCofactorMultiplication, bool DHAES_MODE = true>
  175. struct LUC_IES
  176. : public DL_ES<
  177. DL_CryptoKeys_LUC,
  178. DL_KeyAgreementAlgorithm_DH<Integer, COFACTOR_OPTION>,
  179. DL_KeyDerivationAlgorithm_P1363<Integer, DHAES_MODE, P1363_KDF2<SHA1> >,
  180. DL_EncryptionAlgorithm_Xor<HMAC<SHA1>, DHAES_MODE>,
  181. LUC_IES<> >
  182. {
  183. static std::string StaticAlgorithmName() {return "LUC-IES";} // non-standard name
  184. };
  185. // ********************************************************
  186. //! LUC-DH
  187. typedef DH_Domain<DL_GroupParameters_LUC_DefaultSafePrime> LUC_DH;
  188. NAMESPACE_END
  189. #endif