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.

174 lines
5.8 KiB

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