rsa.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // rsa.h - originally written and placed in the public domain by Wei Dai
  2. /// \file rsa.h
  3. /// \brief Classes for the RSA cryptosystem
  4. /// \details This file contains classes that implement the RSA
  5. /// ciphers and signature schemes as defined in PKCS #1 v2.0.
  6. #ifndef CRYPTOPP_RSA_H
  7. #define CRYPTOPP_RSA_H
  8. #include "cryptlib.h"
  9. #include "pubkey.h"
  10. #include "integer.h"
  11. #include "pkcspad.h"
  12. #include "oaep.h"
  13. #include "emsa2.h"
  14. #include "asn.h"
  15. NAMESPACE_BEGIN(CryptoPP)
  16. /// \brief RSA trapdoor function using the public key
  17. /// \since Crypto++ 1.0
  18. class CRYPTOPP_DLL RSAFunction : public TrapdoorFunction, public X509PublicKey
  19. {
  20. typedef RSAFunction ThisClass;
  21. public:
  22. /// \brief Initialize a RSA public key
  23. /// \param n the modulus
  24. /// \param e the public exponent
  25. void Initialize(const Integer &n, const Integer &e)
  26. {m_n = n; m_e = e;}
  27. // X509PublicKey
  28. OID GetAlgorithmID() const;
  29. void BERDecodePublicKey(BufferedTransformation &bt, bool parametersPresent, size_t size);
  30. void DEREncodePublicKey(BufferedTransformation &bt) const;
  31. // CryptoMaterial
  32. bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
  33. bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
  34. void AssignFrom(const NameValuePairs &source);
  35. // TrapdoorFunction
  36. Integer ApplyFunction(const Integer &x) const;
  37. Integer PreimageBound() const {return m_n;}
  38. Integer ImageBound() const {return m_n;}
  39. // non-derived
  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. protected:
  45. Integer m_n, m_e;
  46. };
  47. /// \brief RSA trapdoor function using the private key
  48. /// \since Crypto++ 1.0
  49. class CRYPTOPP_DLL InvertibleRSAFunction : public RSAFunction, public TrapdoorFunctionInverse, public PKCS8PrivateKey
  50. {
  51. typedef InvertibleRSAFunction ThisClass;
  52. public:
  53. /// \brief Create a RSA private key
  54. /// \param rng a RandomNumberGenerator derived class
  55. /// \param modulusBits the size of the modulus, in bits
  56. /// \param e the desired public exponent
  57. /// \details Initialize() creates a new keypair using a public exponent of 17.
  58. /// \details This function overload of Initialize() creates a new private key because it
  59. /// takes a RandomNumberGenerator() as a parameter. If you have an existing keypair,
  60. /// then use one of the other Initialize() overloads.
  61. void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits, const Integer &e = 17);
  62. /// \brief Initialize a RSA private key
  63. /// \param n modulus
  64. /// \param e public exponent
  65. /// \param d private exponent
  66. /// \param p first prime factor
  67. /// \param q second prime factor
  68. /// \param dp d mod p
  69. /// \param dq d mod q
  70. /// \param u q<sup>-1</sup> mod p
  71. /// \details This Initialize() function overload initializes a private key from existing parameters.
  72. void Initialize(const Integer &n, const Integer &e, const Integer &d, const Integer &p, const Integer &q, const Integer &dp, const Integer &dq, const Integer &u)
  73. {m_n = n; m_e = e; m_d = d; m_p = p; m_q = q; m_dp = dp; m_dq = dq; m_u = u;}
  74. /// \brief Initialize a RSA private key
  75. /// \param n modulus
  76. /// \param e public exponent
  77. /// \param d private exponent
  78. /// \details This Initialize() function overload initializes a private key from existing parameters.
  79. /// Initialize() will factor n using d and populate {p,q,dp,dq,u}.
  80. void Initialize(const Integer &n, const Integer &e, const Integer &d);
  81. // PKCS8PrivateKey
  82. void BERDecode(BufferedTransformation &bt)
  83. {PKCS8PrivateKey::BERDecode(bt);}
  84. void DEREncode(BufferedTransformation &bt) const
  85. {PKCS8PrivateKey::DEREncode(bt);}
  86. void Load(BufferedTransformation &bt)
  87. {PKCS8PrivateKey::BERDecode(bt);}
  88. void Save(BufferedTransformation &bt) const
  89. {PKCS8PrivateKey::DEREncode(bt);}
  90. OID GetAlgorithmID() const {return RSAFunction::GetAlgorithmID();}
  91. void BERDecodePrivateKey(BufferedTransformation &bt, bool parametersPresent, size_t size);
  92. void DEREncodePrivateKey(BufferedTransformation &bt) const;
  93. // TrapdoorFunctionInverse
  94. Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const;
  95. // GeneratableCryptoMaterial
  96. bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
  97. // parameters: (ModulusSize, PublicExponent (default 17))
  98. void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
  99. bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
  100. void AssignFrom(const NameValuePairs &source);
  101. // non-derived interface
  102. const Integer& GetPrime1() const {return m_p;}
  103. const Integer& GetPrime2() const {return m_q;}
  104. const Integer& GetPrivateExponent() const {return m_d;}
  105. const Integer& GetModPrime1PrivateExponent() const {return m_dp;}
  106. const Integer& GetModPrime2PrivateExponent() const {return m_dq;}
  107. const Integer& GetMultiplicativeInverseOfPrime2ModPrime1() const {return m_u;}
  108. void SetPrime1(const Integer &p) {m_p = p;}
  109. void SetPrime2(const Integer &q) {m_q = q;}
  110. void SetPrivateExponent(const Integer &d) {m_d = d;}
  111. void SetModPrime1PrivateExponent(const Integer &dp) {m_dp = dp;}
  112. void SetModPrime2PrivateExponent(const Integer &dq) {m_dq = dq;}
  113. void SetMultiplicativeInverseOfPrime2ModPrime1(const Integer &u) {m_u = u;}
  114. protected:
  115. Integer m_d, m_p, m_q, m_dp, m_dq, m_u;
  116. };
  117. /// \brief RSA trapdoor function using the public key
  118. /// \since Crypto++ 1.0
  119. class CRYPTOPP_DLL RSAFunction_ISO : public RSAFunction
  120. {
  121. public:
  122. Integer ApplyFunction(const Integer &x) const;
  123. Integer PreimageBound() const {return ++(m_n>>1);}
  124. };
  125. /// \brief RSA trapdoor function using the private key
  126. /// \since Crypto++ 1.0
  127. class CRYPTOPP_DLL InvertibleRSAFunction_ISO : public InvertibleRSAFunction
  128. {
  129. public:
  130. Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const;
  131. Integer PreimageBound() const {return ++(m_n>>1);}
  132. };
  133. /// \brief RSA algorithm
  134. /// \since Crypto++ 1.0
  135. struct CRYPTOPP_DLL RSA
  136. {
  137. CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "RSA";}
  138. typedef RSAFunction PublicKey;
  139. typedef InvertibleRSAFunction PrivateKey;
  140. };
  141. /// \brief RSA encryption algorithm
  142. /// \tparam STANDARD signature standard
  143. /// \sa <a href="http://www.weidai.com/scan-mirror/ca.html#RSA">RSA cryptosystem</a>
  144. /// \since Crypto++ 1.0
  145. template <class STANDARD>
  146. struct RSAES : public TF_ES<RSA, STANDARD>
  147. {
  148. };
  149. /// \brief RSA signature algorithm
  150. /// \tparam STANDARD signature standard
  151. /// \tparam H hash transformation
  152. /// \details See documentation of PKCS1v15 for a list of hash functions that can be used with it.
  153. /// \sa <a href="http://www.weidai.com/scan-mirror/sig.html#RSA">RSA signature scheme with appendix</a>
  154. /// \since Crypto++ 1.0
  155. template <class STANDARD, class H>
  156. struct RSASS : public TF_SS<RSA, STANDARD, H>
  157. {
  158. };
  159. /// \brief RSA algorithm
  160. /// \since Crypto++ 1.0
  161. struct CRYPTOPP_DLL RSA_ISO
  162. {
  163. CRYPTOPP_STATIC_CONSTEXPR const char* CRYPTOPP_API StaticAlgorithmName() {return "RSA-ISO";}
  164. typedef RSAFunction_ISO PublicKey;
  165. typedef InvertibleRSAFunction_ISO PrivateKey;
  166. };
  167. /// \brief RSA signature algorithm
  168. /// \tparam H hash transformation
  169. /// \since Crypto++ 1.0
  170. template <class H>
  171. struct RSASS_ISO : public TF_SS<RSA_ISO, P1363_EMSA2, H>
  172. {
  173. };
  174. /// \brief \ref RSAES<STANDARD> "RSAES<PKCS1v15>::Decryptor" typedef
  175. /// \details RSA encryption scheme defined in PKCS #1 v2.0
  176. DOCUMENTED_TYPEDEF(RSAES<PKCS1v15>::Decryptor, RSAES_PKCS1v15_Decryptor);
  177. /// \brief \ref RSAES<STANDARD> "RSAES<PKCS1v15>::Encryptor" typedef
  178. /// \details RSA encryption scheme defined in PKCS #1 v2.0
  179. DOCUMENTED_TYPEDEF(RSAES<PKCS1v15>::Encryptor, RSAES_PKCS1v15_Encryptor);
  180. /// \brief \ref RSAES<STANDARD> "RSAES<OAEP<SHA1>>::Decryptor" typedef
  181. /// \details RSA encryption scheme defined in PKCS #1 v2.0
  182. DOCUMENTED_TYPEDEF(RSAES<OAEP<SHA1> >::Decryptor, RSAES_OAEP_SHA_Decryptor);
  183. /// \brief \ref RSAES<STANDARD> "RSAES<OAEP<SHA1>>::Encryptor" typedef
  184. /// \details RSA encryption scheme defined in PKCS #1 v2.0
  185. DOCUMENTED_TYPEDEF(RSAES<OAEP<SHA1> >::Encryptor, RSAES_OAEP_SHA_Encryptor);
  186. #ifdef CRYPTOPP_DOXYGEN_PROCESSING
  187. /// \brief \ref RSASS<STANDARD,HASH> "RSASS<PKCS1v15,SHA1>::Signer" typedef
  188. /// \details RSA signature schemes defined in PKCS #1 v2.0
  189. /// \since Crypto++ 1.0
  190. class RSASSA_PKCS1v15_SHA_Signer : public RSASS<PKCS1v15,SHA1>::Signer {};
  191. /// \brief \ref RSASS<STANDARD,HASH> "RSASS<PKCS1v15,SHA1>::Verifier" typedef
  192. /// \details RSA signature schemes defined in PKCS #1 v2.0
  193. /// \since Crypto++ 1.0
  194. class RSASSA_PKCS1v15_SHA_Verifier : public RSASS<PKCS1v15,SHA1>::Verifier {};
  195. namespace Weak {
  196. /// \brief \ref RSASS<STANDARD,HASH> "RSASS<PKCS1v15, Weak::MD2>::Signer" typedef
  197. /// \details RSA signature schemes defined in PKCS #1 v2.0
  198. /// \since Crypto++ 1.0
  199. class RSASSA_PKCS1v15_MD2_Signer : public RSASS<PKCS1v15, Weak1::MD2>::Signer {};
  200. /// \brief \ref RSASS<STANDARD,HASH> "RSASS<PKCS1v15, Weak::MD2>::Verifier" typedef
  201. /// \details RSA signature schemes defined in PKCS #1 v2.0
  202. /// \since Crypto++ 1.0
  203. class RSASSA_PKCS1v15_MD2_Verifier : public RSASS<PKCS1v15, Weak1::MD2>::Verifier {};
  204. /// \brief \ref RSASS<STANDARD,HASH> "RSASS<PKCS1v15, Weak::MD5>::Signer" typedef
  205. /// \details RSA signature schemes defined in PKCS #1 v2.0
  206. /// \since Crypto++ 1.0
  207. class RSASSA_PKCS1v15_MD5_Signer : public RSASS<PKCS1v15, Weak1::MD5>::Signer {};
  208. /// \brief \ref RSASS<STANDARD,HASH> "RSASS<PKCS1v15, Weak::MD5>::Verifier" typedef
  209. /// \details RSA signature schemes defined in PKCS #1 v2.0
  210. /// \since Crypto++ 1.0
  211. class RSASSA_PKCS1v15_MD5_Verifier : public RSASS<PKCS1v15, Weak1::MD5>::Verifier {};
  212. }
  213. #else
  214. typedef RSASS<PKCS1v15,SHA1>::Signer RSASSA_PKCS1v15_SHA_Signer;
  215. typedef RSASS<PKCS1v15,SHA1>::Verifier RSASSA_PKCS1v15_SHA_Verifier;
  216. namespace Weak {
  217. typedef RSASS<PKCS1v15, Weak1::MD2>::Signer RSASSA_PKCS1v15_MD2_Signer;
  218. typedef RSASS<PKCS1v15, Weak1::MD2>::Verifier RSASSA_PKCS1v15_MD2_Verifier;
  219. typedef RSASS<PKCS1v15, Weak1::MD5>::Signer RSASSA_PKCS1v15_MD5_Signer;
  220. typedef RSASS<PKCS1v15, Weak1::MD5>::Verifier RSASSA_PKCS1v15_MD5_Verifier;
  221. }
  222. #endif // CRYPTOPP_DOXYGEN_PROCESSING
  223. NAMESPACE_END
  224. #endif