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.

111 lines
3.2 KiB

  1. // rabin.h - written and placed in the public domain by Wei Dai
  2. //! \file
  3. //! \headerfile rabin.h
  4. //! \brief Classes for Rabin encryption and signature schemes
  5. #ifndef CRYPTOPP_RABIN_H
  6. #define CRYPTOPP_RABIN_H
  7. #include "cryptlib.h"
  8. #include "oaep.h"
  9. #include "pssr.h"
  10. #include "integer.h"
  11. NAMESPACE_BEGIN(CryptoPP)
  12. //! _
  13. class RabinFunction : public TrapdoorFunction, public PublicKey
  14. {
  15. typedef RabinFunction ThisClass;
  16. public:
  17. void Initialize(const Integer &n, const Integer &r, const Integer &s)
  18. {m_n = n; m_r = r; m_s = s;}
  19. void BERDecode(BufferedTransformation &bt);
  20. void DEREncode(BufferedTransformation &bt) const;
  21. Integer ApplyFunction(const Integer &x) const;
  22. Integer PreimageBound() const {return m_n;}
  23. Integer ImageBound() const {return m_n;}
  24. bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
  25. bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
  26. void AssignFrom(const NameValuePairs &source);
  27. const Integer& GetModulus() const {return m_n;}
  28. const Integer& GetQuadraticResidueModPrime1() const {return m_r;}
  29. const Integer& GetQuadraticResidueModPrime2() const {return m_s;}
  30. void SetModulus(const Integer &n) {m_n = n;}
  31. void SetQuadraticResidueModPrime1(const Integer &r) {m_r = r;}
  32. void SetQuadraticResidueModPrime2(const Integer &s) {m_s = s;}
  33. protected:
  34. Integer m_n, m_r, m_s;
  35. };
  36. //! _
  37. class InvertibleRabinFunction : public RabinFunction, public TrapdoorFunctionInverse, public PrivateKey
  38. {
  39. typedef InvertibleRabinFunction ThisClass;
  40. public:
  41. void Initialize(const Integer &n, const Integer &r, const Integer &s,
  42. const Integer &p, const Integer &q, const Integer &u)
  43. {m_n = n; m_r = r; m_s = s; m_p = p; m_q = q; m_u = u;}
  44. void Initialize(RandomNumberGenerator &rng, unsigned int keybits)
  45. {GenerateRandomWithKeySize(rng, keybits);}
  46. void BERDecode(BufferedTransformation &bt);
  47. void DEREncode(BufferedTransformation &bt) const;
  48. Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const;
  49. bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
  50. bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
  51. void AssignFrom(const NameValuePairs &source);
  52. /*! parameters: (ModulusSize) */
  53. void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
  54. const Integer& GetPrime1() const {return m_p;}
  55. const Integer& GetPrime2() const {return m_q;}
  56. const Integer& GetMultiplicativeInverseOfPrime2ModPrime1() const {return m_u;}
  57. void SetPrime1(const Integer &p) {m_p = p;}
  58. void SetPrime2(const Integer &q) {m_q = q;}
  59. void SetMultiplicativeInverseOfPrime2ModPrime1(const Integer &u) {m_u = u;}
  60. protected:
  61. Integer m_p, m_q, m_u;
  62. };
  63. //! Rabin
  64. struct Rabin
  65. {
  66. static std::string StaticAlgorithmName() {return "Rabin-Crypto++Variant";}
  67. typedef RabinFunction PublicKey;
  68. typedef InvertibleRabinFunction PrivateKey;
  69. };
  70. //! Rabin encryption
  71. template <class STANDARD>
  72. struct RabinES : public TF_ES<STANDARD, Rabin>
  73. {
  74. };
  75. //! Rabin signature
  76. template <class STANDARD, class H>
  77. struct RabinSS : public TF_SS<STANDARD, H, Rabin>
  78. {
  79. };
  80. // More typedefs for backwards compatibility
  81. class SHA1;
  82. typedef RabinES<OAEP<SHA1> >::Decryptor RabinDecryptor;
  83. typedef RabinES<OAEP<SHA1> >::Encryptor RabinEncryptor;
  84. NAMESPACE_END
  85. #endif