Team Fortress 2 Source Code as on 22/4/2020
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.

305 lines
9.8 KiB

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