luc.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. // luc.h - originally written and placed in the public domain by Wei Dai
  2. /// \file luc.h
  3. /// \brief Classes for the LUC cryptosystem
  4. /// \details This class is here for historical and pedagogical interest. It has no practical advantages over other
  5. /// trapdoor functions and probably shouldn't be used in production software. The discrete log based LUC schemes
  6. /// defined later in this .h file may be of more practical interest.
  7. /// \since Crypto++ 2.1
  8. #ifndef CRYPTOPP_LUC_H
  9. #define CRYPTOPP_LUC_H
  10. #include "cryptlib.h"
  11. #include "gfpcrypt.h"
  12. #include "integer.h"
  13. #include "algebra.h"
  14. #include "secblock.h"
  15. #if CRYPTOPP_MSC_VERSION
  16. # pragma warning(push)
  17. # pragma warning(disable: 4127 4189)
  18. #endif
  19. #include "pkcspad.h"
  20. #include "integer.h"
  21. #include "oaep.h"
  22. #include "dh.h"
  23. #include <limits.h>
  24. NAMESPACE_BEGIN(CryptoPP)
  25. /// \brief The LUC function.
  26. /// \details This class is here for historical and pedagogical interest. It has no practical advantages over other
  27. /// trapdoor functions and probably shouldn't be used in production software. The discrete log based LUC schemes
  28. /// defined later in this .h file may be of more practical interest.
  29. /// \since Crypto++ 2.1
  30. class LUCFunction : public TrapdoorFunction, public PublicKey
  31. {
  32. typedef LUCFunction ThisClass;
  33. public:
  34. virtual ~LUCFunction() {}
  35. /// \brief Initialize a LUC public key with {n,e}
  36. /// \param n the modulus
  37. /// \param e the public exponent
  38. void Initialize(const Integer &n, const Integer &e)
  39. {m_n = n; m_e = e;}
  40. void BERDecode(BufferedTransformation &bt);
  41. void DEREncode(BufferedTransformation &bt) const;
  42. Integer ApplyFunction(const Integer &x) const;
  43. Integer PreimageBound() const {return m_n;}
  44. Integer ImageBound() const {return m_n;}
  45. bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
  46. bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
  47. void AssignFrom(const NameValuePairs &source);
  48. // non-derived interface
  49. const Integer & GetModulus() const {return m_n;}
  50. const Integer & GetPublicExponent() const {return m_e;}
  51. void SetModulus(const Integer &n) {m_n = n;}
  52. void SetPublicExponent(const Integer &e) {m_e = e;}
  53. protected:
  54. Integer m_n, m_e;
  55. };
  56. /// \brief The LUC inverse function.
  57. /// \details This class is here for historical and pedagogical interest. It has no practical advantages over other
  58. /// trapdoor functions and probably shouldn't be used in production software. The discrete log based LUC schemes
  59. /// defined later in this .h file may be of more practical interest.
  60. /// \since Crypto++ 2.1
  61. class InvertibleLUCFunction : public LUCFunction, public TrapdoorFunctionInverse, public PrivateKey
  62. {
  63. typedef InvertibleLUCFunction ThisClass;
  64. public:
  65. virtual ~InvertibleLUCFunction() {}
  66. /// \brief Create a LUC private key
  67. /// \param rng a RandomNumberGenerator derived class
  68. /// \param modulusBits the size of the modulus, in bits
  69. /// \param eStart the desired starting public exponent
  70. /// \details Initialize() creates a new keypair using a starting public exponent of 17.
  71. /// \details This function overload of Initialize() creates a new keypair because it
  72. /// takes a RandomNumberGenerator() as a parameter. If you have an existing keypair,
  73. /// then use one of the other Initialize() overloads.
  74. void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits, const Integer &eStart=17);
  75. /// \brief Initialize a LUC private key with {n,e,p,q,dp,dq,u}
  76. /// \param n modulus
  77. /// \param e public exponent
  78. /// \param p first prime factor
  79. /// \param q second prime factor
  80. /// \param u q<sup>-1</sup> mod p
  81. /// \details This Initialize() function overload initializes a private key from existing parameters.
  82. void Initialize(const Integer &n, const Integer &e, const Integer &p, const Integer &q, const Integer &u)
  83. {m_n = n; m_e = e; m_p = p; m_q = q; m_u = u;}
  84. void BERDecode(BufferedTransformation &bt);
  85. void DEREncode(BufferedTransformation &bt) const;
  86. Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const;
  87. bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
  88. bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
  89. void AssignFrom(const NameValuePairs &source);
  90. /*! parameters: (ModulusSize, PublicExponent (default 17)) */
  91. void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
  92. // non-derived interface
  93. const Integer& GetPrime1() const {return m_p;}
  94. const Integer& GetPrime2() const {return m_q;}
  95. const Integer& GetMultiplicativeInverseOfPrime2ModPrime1() const {return m_u;}
  96. void SetPrime1(const Integer &p) {m_p = p;}
  97. void SetPrime2(const Integer &q) {m_q = q;}
  98. void SetMultiplicativeInverseOfPrime2ModPrime1(const Integer &u) {m_u = u;}
  99. protected:
  100. Integer m_p, m_q, m_u;
  101. };
  102. /// \brief LUC cryptosystem
  103. /// \since Crypto++ 2.1
  104. struct LUC
  105. {
  106. static std::string StaticAlgorithmName() {return "LUC";}
  107. typedef LUCFunction PublicKey;
  108. typedef InvertibleLUCFunction PrivateKey;
  109. };
  110. /// \brief LUC encryption scheme
  111. /// \tparam STANDARD signature standard
  112. /// \details This class is here for historical and pedagogical interest. It has no practical advantages over other
  113. /// trapdoor functions and probably shouldn't be used in production software. The discrete log based LUC schemes
  114. /// defined later in this .h file may be of more practical interest.
  115. /// \since Crypto++ 2.1
  116. template <class STANDARD>
  117. struct LUCES : public TF_ES<LUC, STANDARD>
  118. {
  119. };
  120. /// \brief LUC signature scheme with appendix
  121. /// \tparam STANDARD signature standard
  122. /// \tparam H hash transformation
  123. /// \details This class is here for historical and pedagogical interest. It has no practical advantages over other
  124. /// trapdoor functions and probably shouldn't be used in production software. The discrete log based LUC schemes
  125. /// defined later in this .h file may be of more practical interest.
  126. /// \since Crypto++ 2.1
  127. template <class STANDARD, class H>
  128. struct LUCSS : public TF_SS<LUC, STANDARD, H>
  129. {
  130. };
  131. // analogous to the RSA schemes defined in PKCS #1 v2.0
  132. typedef LUCES<OAEP<SHA1> >::Decryptor LUCES_OAEP_SHA_Decryptor;
  133. typedef LUCES<OAEP<SHA1> >::Encryptor LUCES_OAEP_SHA_Encryptor;
  134. typedef LUCSS<PKCS1v15, SHA1>::Signer LUCSSA_PKCS1v15_SHA_Signer;
  135. typedef LUCSS<PKCS1v15, SHA1>::Verifier LUCSSA_PKCS1v15_SHA_Verifier;
  136. // ********************************************************
  137. /// \brief LUC GroupParameters precomputation
  138. /// \details No actual precomputation is performed
  139. /// \since Crypto++ 2.1
  140. class DL_GroupPrecomputation_LUC : public DL_GroupPrecomputation<Integer>
  141. {
  142. public:
  143. virtual ~DL_GroupPrecomputation_LUC() {}
  144. const AbstractGroup<Element> & GetGroup() const {CRYPTOPP_ASSERT(false); throw 0;}
  145. Element BERDecodeElement(BufferedTransformation &bt) const {return Integer(bt);}
  146. void DEREncodeElement(BufferedTransformation &bt, const Element &v) const {v.DEREncode(bt);}
  147. // non-inherited
  148. void SetModulus(const Integer &v) {m_p = v;}
  149. const Integer & GetModulus() const {return m_p;}
  150. private:
  151. Integer m_p;
  152. };
  153. /// \brief LUC Precomputation
  154. /// \since Crypto++ 2.1
  155. class DL_BasePrecomputation_LUC : public DL_FixedBasePrecomputation<Integer>
  156. {
  157. public:
  158. virtual ~DL_BasePrecomputation_LUC() {}
  159. // DL_FixedBasePrecomputation
  160. bool IsInitialized() const {return m_g.NotZero();}
  161. void SetBase(const DL_GroupPrecomputation<Element> &group, const Integer &base)
  162. {CRYPTOPP_UNUSED(group); m_g = base;}
  163. const Integer & GetBase(const DL_GroupPrecomputation<Element> &group) const
  164. {CRYPTOPP_UNUSED(group); return m_g;}
  165. void Precompute(const DL_GroupPrecomputation<Element> &group, unsigned int maxExpBits, unsigned int storage)
  166. {CRYPTOPP_UNUSED(group); CRYPTOPP_UNUSED(maxExpBits); CRYPTOPP_UNUSED(storage);}
  167. void Load(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation)
  168. {CRYPTOPP_UNUSED(group); CRYPTOPP_UNUSED(storedPrecomputation);}
  169. void Save(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation) const
  170. {CRYPTOPP_UNUSED(group); CRYPTOPP_UNUSED(storedPrecomputation);}
  171. Integer Exponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent) const;
  172. Integer CascadeExponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent, const DL_FixedBasePrecomputation<Integer> &pc2, const Integer &exponent2) const
  173. {
  174. CRYPTOPP_UNUSED(group); CRYPTOPP_UNUSED(exponent); CRYPTOPP_UNUSED(pc2); CRYPTOPP_UNUSED(exponent2);
  175. // shouldn't be called
  176. throw NotImplemented("DL_BasePrecomputation_LUC: CascadeExponentiate not implemented");
  177. }
  178. private:
  179. Integer m_g;
  180. };
  181. /// \brief LUC GroupParameters specialization
  182. /// \since Crypto++ 2.1
  183. class DL_GroupParameters_LUC : public DL_GroupParameters_IntegerBasedImpl<DL_GroupPrecomputation_LUC, DL_BasePrecomputation_LUC>
  184. {
  185. public:
  186. virtual ~DL_GroupParameters_LUC() {}
  187. // DL_GroupParameters
  188. bool IsIdentity(const Integer &element) const {return element == Integer::Two();}
  189. void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const;
  190. Element MultiplyElements(const Element &a, const Element &b) const
  191. {
  192. CRYPTOPP_UNUSED(a); CRYPTOPP_UNUSED(b);
  193. throw NotImplemented("LUC_GroupParameters: MultiplyElements can not be implemented");
  194. }
  195. Element CascadeExponentiate(const Element &element1, const Integer &exponent1, const Element &element2, const Integer &exponent2) const
  196. {
  197. CRYPTOPP_UNUSED(element1); CRYPTOPP_UNUSED(exponent1); CRYPTOPP_UNUSED(element2); CRYPTOPP_UNUSED(exponent2);
  198. throw NotImplemented("LUC_GroupParameters: MultiplyElements can not be implemented");
  199. }
  200. // NameValuePairs interface
  201. bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
  202. {
  203. return GetValueHelper<DL_GroupParameters_IntegerBased>(this, name, valueType, pValue).Assignable();
  204. }
  205. private:
  206. int GetFieldType() const {return 2;}
  207. };
  208. /// \brief GF(p) group parameters that default to safe primes
  209. /// \since Crypto++ 2.1
  210. class DL_GroupParameters_LUC_DefaultSafePrime : public DL_GroupParameters_LUC
  211. {
  212. public:
  213. typedef NoCofactorMultiplication DefaultCofactorOption;
  214. protected:
  215. unsigned int GetDefaultSubgroupOrderSize(unsigned int modulusSize) const {return modulusSize-1;}
  216. };
  217. /// \brief LUC HMP signature algorithm
  218. /// \since Crypto++ 2.1
  219. class DL_Algorithm_LUC_HMP : public DL_ElgamalLikeSignatureAlgorithm<Integer>
  220. {
  221. public:
  222. CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "LUC-HMP";}
  223. virtual ~DL_Algorithm_LUC_HMP() {}
  224. void Sign(const DL_GroupParameters<Integer> &params, const Integer &x, const Integer &k, const Integer &e, Integer &r, Integer &s) const;
  225. bool Verify(const DL_GroupParameters<Integer> &params, const DL_PublicKey<Integer> &publicKey, const Integer &e, const Integer &r, const Integer &s) const;
  226. size_t RLen(const DL_GroupParameters<Integer> &params) const
  227. {return params.GetGroupOrder().ByteCount();}
  228. };
  229. /// \brief LUC signature keys
  230. /// \since Crypto++ 2.1
  231. struct DL_SignatureKeys_LUC
  232. {
  233. typedef DL_GroupParameters_LUC GroupParameters;
  234. typedef DL_PublicKey_GFP<GroupParameters> PublicKey;
  235. typedef DL_PrivateKey_GFP<GroupParameters> PrivateKey;
  236. };
  237. /// \brief LUC-HMP, based on "Digital signature schemes based on Lucas functions" by Patrick Horster, Markus Michels, Holger Petersen
  238. /// \tparam H hash transformation
  239. /// \details This class is here for historical and pedagogical interest. It has no practical advantages over other
  240. /// trapdoor functions and probably shouldn't be used in production software. The discrete log based LUC schemes
  241. /// defined later in this .h file may be of more practical interest.
  242. /// \since Crypto++ 2.1
  243. template <class H>
  244. struct LUC_HMP : public DL_SS<DL_SignatureKeys_LUC, DL_Algorithm_LUC_HMP, DL_SignatureMessageEncodingMethod_DSA, H>
  245. {
  246. };
  247. /// \brief LUC encryption keys
  248. /// \since Crypto++ 2.1
  249. struct DL_CryptoKeys_LUC
  250. {
  251. typedef DL_GroupParameters_LUC_DefaultSafePrime GroupParameters;
  252. typedef DL_PublicKey_GFP<GroupParameters> PublicKey;
  253. typedef DL_PrivateKey_GFP<GroupParameters> PrivateKey;
  254. };
  255. /// \brief LUC Integrated Encryption Scheme
  256. /// \tparam COFACTOR_OPTION cofactor multiplication option
  257. /// \tparam HASH HashTransformation derived class used for key drivation and MAC computation
  258. /// \tparam DHAES_MODE flag indicating if the MAC includes additional context parameters such as <em>u·V</em>, <em>v·U</em> and label
  259. /// \tparam LABEL_OCTETS flag indicating if the label size is specified in octets or bits
  260. /// \sa CofactorMultiplicationOption
  261. /// \since Crypto++ 2.1, Crypto++ 5.7 for Bouncy Castle and Botan compatibility
  262. template <class HASH = SHA1, class COFACTOR_OPTION = NoCofactorMultiplication, bool DHAES_MODE = true, bool LABEL_OCTETS = false>
  263. struct LUC_IES
  264. : public DL_ES<
  265. DL_CryptoKeys_LUC,
  266. DL_KeyAgreementAlgorithm_DH<Integer, COFACTOR_OPTION>,
  267. DL_KeyDerivationAlgorithm_P1363<Integer, DHAES_MODE, P1363_KDF2<HASH> >,
  268. DL_EncryptionAlgorithm_Xor<HMAC<HASH>, DHAES_MODE, LABEL_OCTETS>,
  269. LUC_IES<> >
  270. {
  271. CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "LUC-IES";} // non-standard name
  272. };
  273. // ********************************************************
  274. /// \brief LUC-DH
  275. typedef DH_Domain<DL_GroupParameters_LUC_DefaultSafePrime> LUC_DH;
  276. NAMESPACE_END
  277. #if CRYPTOPP_MSC_VERSION
  278. # pragma warning(pop)
  279. #endif
  280. #endif