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.

178 lines
6.0 KiB

  1. // rsa.h - 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. //! _
  17. class CRYPTOPP_DLL RSAFunction : public TrapdoorFunction, public X509PublicKey
  18. {
  19. typedef RSAFunction ThisClass;
  20. public:
  21. void Initialize(const Integer &n, const Integer &e)
  22. {m_n = n; m_e = e;}
  23. // X509PublicKey
  24. OID GetAlgorithmID() const;
  25. void BERDecodePublicKey(BufferedTransformation &bt, bool parametersPresent, size_t size);
  26. void DEREncodePublicKey(BufferedTransformation &bt) const;
  27. // CryptoMaterial
  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. // TrapdoorFunction
  32. Integer ApplyFunction(const Integer &x) const;
  33. Integer PreimageBound() const {return m_n;}
  34. Integer ImageBound() const {return m_n;}
  35. // non-derived
  36. const Integer & GetModulus() const {return m_n;}
  37. const Integer & GetPublicExponent() const {return m_e;}
  38. void SetModulus(const Integer &n) {m_n = n;}
  39. void SetPublicExponent(const Integer &e) {m_e = e;}
  40. protected:
  41. Integer m_n, m_e;
  42. };
  43. //! _
  44. class CRYPTOPP_DLL InvertibleRSAFunction : public RSAFunction, public TrapdoorFunctionInverse, public PKCS8PrivateKey
  45. {
  46. typedef InvertibleRSAFunction ThisClass;
  47. public:
  48. void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits, const Integer &e = 17);
  49. 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)
  50. {m_n = n; m_e = e; m_d = d; m_p = p; m_q = q; m_dp = dp; m_dq = dq; m_u = u;}
  51. //! factor n given private exponent
  52. void Initialize(const Integer &n, const Integer &e, const Integer &d);
  53. // PKCS8PrivateKey
  54. void BERDecode(BufferedTransformation &bt)
  55. {PKCS8PrivateKey::BERDecode(bt);}
  56. void DEREncode(BufferedTransformation &bt) const
  57. {PKCS8PrivateKey::DEREncode(bt);}
  58. void Load(BufferedTransformation &bt)
  59. {PKCS8PrivateKey::BERDecode(bt);}
  60. void Save(BufferedTransformation &bt) const
  61. {PKCS8PrivateKey::DEREncode(bt);}
  62. OID GetAlgorithmID() const {return RSAFunction::GetAlgorithmID();}
  63. void BERDecodePrivateKey(BufferedTransformation &bt, bool parametersPresent, size_t size);
  64. void DEREncodePrivateKey(BufferedTransformation &bt) const;
  65. // TrapdoorFunctionInverse
  66. Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const;
  67. // GeneratableCryptoMaterial
  68. bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
  69. /*! parameters: (ModulusSize, PublicExponent (default 17)) */
  70. void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
  71. bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
  72. void AssignFrom(const NameValuePairs &source);
  73. // non-derived interface
  74. const Integer& GetPrime1() const {return m_p;}
  75. const Integer& GetPrime2() const {return m_q;}
  76. const Integer& GetPrivateExponent() const {return m_d;}
  77. const Integer& GetModPrime1PrivateExponent() const {return m_dp;}
  78. const Integer& GetModPrime2PrivateExponent() const {return m_dq;}
  79. const Integer& GetMultiplicativeInverseOfPrime2ModPrime1() const {return m_u;}
  80. void SetPrime1(const Integer &p) {m_p = p;}
  81. void SetPrime2(const Integer &q) {m_q = q;}
  82. void SetPrivateExponent(const Integer &d) {m_d = d;}
  83. void SetModPrime1PrivateExponent(const Integer &dp) {m_dp = dp;}
  84. void SetModPrime2PrivateExponent(const Integer &dq) {m_dq = dq;}
  85. void SetMultiplicativeInverseOfPrime2ModPrime1(const Integer &u) {m_u = u;}
  86. protected:
  87. Integer m_d, m_p, m_q, m_dp, m_dq, m_u;
  88. };
  89. class CRYPTOPP_DLL RSAFunction_ISO : public RSAFunction
  90. {
  91. public:
  92. Integer ApplyFunction(const Integer &x) const;
  93. Integer PreimageBound() const {return ++(m_n>>1);}
  94. };
  95. class CRYPTOPP_DLL InvertibleRSAFunction_ISO : public InvertibleRSAFunction
  96. {
  97. public:
  98. Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const;
  99. Integer PreimageBound() const {return ++(m_n>>1);}
  100. };
  101. //! RSA
  102. struct CRYPTOPP_DLL RSA
  103. {
  104. static const char * CRYPTOPP_API StaticAlgorithmName() {return "RSA";}
  105. typedef RSAFunction PublicKey;
  106. typedef InvertibleRSAFunction PrivateKey;
  107. };
  108. //! <a href="http://www.weidai.com/scan-mirror/ca.html#RSA">RSA cryptosystem</a>
  109. template <class STANDARD>
  110. struct RSAES : public TF_ES<STANDARD, RSA>
  111. {
  112. };
  113. //! <a href="http://www.weidai.com/scan-mirror/sig.html#RSA">RSA signature scheme with appendix</a>
  114. /*! See documentation of PKCS1v15 for a list of hash functions that can be used with it. */
  115. template <class STANDARD, class H>
  116. struct RSASS : public TF_SS<STANDARD, H, RSA>
  117. {
  118. };
  119. struct CRYPTOPP_DLL RSA_ISO
  120. {
  121. static const char * CRYPTOPP_API StaticAlgorithmName() {return "RSA-ISO";}
  122. typedef RSAFunction_ISO PublicKey;
  123. typedef InvertibleRSAFunction_ISO PrivateKey;
  124. };
  125. template <class H>
  126. struct RSASS_ISO : public TF_SS<P1363_EMSA2, H, RSA_ISO>
  127. {
  128. };
  129. // The two RSA encryption schemes defined in PKCS #1 v2.0
  130. typedef RSAES<PKCS1v15>::Decryptor RSAES_PKCS1v15_Decryptor;
  131. typedef RSAES<PKCS1v15>::Encryptor RSAES_PKCS1v15_Encryptor;
  132. typedef RSAES<OAEP<SHA> >::Decryptor RSAES_OAEP_SHA_Decryptor;
  133. typedef RSAES<OAEP<SHA> >::Encryptor RSAES_OAEP_SHA_Encryptor;
  134. // The three RSA signature schemes defined in PKCS #1 v2.0
  135. typedef RSASS<PKCS1v15, SHA>::Signer RSASSA_PKCS1v15_SHA_Signer;
  136. typedef RSASS<PKCS1v15, SHA>::Verifier RSASSA_PKCS1v15_SHA_Verifier;
  137. namespace Weak {
  138. typedef RSASS<PKCS1v15, Weak1::MD2>::Signer RSASSA_PKCS1v15_MD2_Signer;
  139. typedef RSASS<PKCS1v15, Weak1::MD2>::Verifier RSASSA_PKCS1v15_MD2_Verifier;
  140. typedef RSASS<PKCS1v15, Weak1::MD5>::Signer RSASSA_PKCS1v15_MD5_Signer;
  141. typedef RSASS<PKCS1v15, Weak1::MD5>::Verifier RSASSA_PKCS1v15_MD5_Verifier;
  142. }
  143. NAMESPACE_END
  144. #endif