rabin.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // rabin.h - originally written and placed in the public domain by Wei Dai
  2. /// \file rabin.h
  3. /// \brief Classes for Rabin encryption and signature schemes
  4. #ifndef CRYPTOPP_RABIN_H
  5. #define CRYPTOPP_RABIN_H
  6. #include "cryptlib.h"
  7. #include "oaep.h"
  8. #include "pssr.h"
  9. #include "integer.h"
  10. NAMESPACE_BEGIN(CryptoPP)
  11. /// \brief Rabin trapdoor function using the public key
  12. /// \since Crypto++ 2.0
  13. class RabinFunction : public TrapdoorFunction, public PublicKey
  14. {
  15. typedef RabinFunction ThisClass;
  16. public:
  17. /// \brief Initialize a Rabin public key
  18. /// \param n the modulus
  19. /// \param r element r
  20. /// \param s element s
  21. void Initialize(const Integer &n, const Integer &r, const Integer &s)
  22. {m_n = n; m_r = r; m_s = s;}
  23. void BERDecode(BufferedTransformation &bt);
  24. void DEREncode(BufferedTransformation &bt) const;
  25. Integer ApplyFunction(const Integer &x) const;
  26. Integer PreimageBound() const {return m_n;}
  27. Integer ImageBound() const {return m_n;}
  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. const Integer& GetModulus() const {return m_n;}
  32. const Integer& GetQuadraticResidueModPrime1() const {return m_r;}
  33. const Integer& GetQuadraticResidueModPrime2() const {return m_s;}
  34. void SetModulus(const Integer &n) {m_n = n;}
  35. void SetQuadraticResidueModPrime1(const Integer &r) {m_r = r;}
  36. void SetQuadraticResidueModPrime2(const Integer &s) {m_s = s;}
  37. protected:
  38. Integer m_n, m_r, m_s;
  39. };
  40. /// \brief Rabin trapdoor function using the private key
  41. /// \since Crypto++ 2.0
  42. class InvertibleRabinFunction : public RabinFunction, public TrapdoorFunctionInverse, public PrivateKey
  43. {
  44. typedef InvertibleRabinFunction ThisClass;
  45. public:
  46. /// \brief Initialize a Rabin private key
  47. /// \param n modulus
  48. /// \param r element r
  49. /// \param s element s
  50. /// \param p first prime factor
  51. /// \param q second prime factor
  52. /// \param u q<sup>-1</sup> mod p
  53. /// \details This Initialize() function overload initializes a private key from existing parameters.
  54. void Initialize(const Integer &n, const Integer &r, const Integer &s, const Integer &p, const Integer &q, const Integer &u)
  55. {m_n = n; m_r = r; m_s = s; m_p = p; m_q = q; m_u = u;}
  56. /// \brief Create a Rabin private key
  57. /// \param rng a RandomNumberGenerator derived class
  58. /// \param keybits the size of the key, in bits
  59. /// \details This function overload of Initialize() creates a new private key because it
  60. /// takes a RandomNumberGenerator() as a parameter. If you have an existing keypair,
  61. /// then use one of the other Initialize() overloads.
  62. void Initialize(RandomNumberGenerator &rng, unsigned int keybits)
  63. {GenerateRandomWithKeySize(rng, keybits);}
  64. void BERDecode(BufferedTransformation &bt);
  65. void DEREncode(BufferedTransformation &bt) const;
  66. Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const;
  67. bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
  68. bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
  69. void AssignFrom(const NameValuePairs &source);
  70. /*! parameters: (ModulusSize) */
  71. void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
  72. const Integer& GetPrime1() const {return m_p;}
  73. const Integer& GetPrime2() const {return m_q;}
  74. const Integer& GetMultiplicativeInverseOfPrime2ModPrime1() const {return m_u;}
  75. void SetPrime1(const Integer &p) {m_p = p;}
  76. void SetPrime2(const Integer &q) {m_q = q;}
  77. void SetMultiplicativeInverseOfPrime2ModPrime1(const Integer &u) {m_u = u;}
  78. protected:
  79. Integer m_p, m_q, m_u;
  80. };
  81. /// \brief Rabin keys
  82. struct Rabin
  83. {
  84. static std::string StaticAlgorithmName() {return "Rabin-Crypto++Variant";}
  85. typedef RabinFunction PublicKey;
  86. typedef InvertibleRabinFunction PrivateKey;
  87. };
  88. /// \brief Rabin encryption scheme
  89. /// \tparam STANDARD encryption standard
  90. template <class STANDARD>
  91. struct RabinES : public TF_ES<Rabin, STANDARD>
  92. {
  93. };
  94. /// \brief Rabin signature scheme
  95. /// \tparam STANDARD signature standard
  96. /// \tparam H hash transformation
  97. template <class STANDARD, class H>
  98. struct RabinSS : public TF_SS<Rabin, STANDARD, H>
  99. {
  100. };
  101. // More typedefs for backwards compatibility
  102. class SHA1;
  103. typedef RabinES<OAEP<SHA1> >::Decryptor RabinDecryptor;
  104. typedef RabinES<OAEP<SHA1> >::Encryptor RabinEncryptor;
  105. NAMESPACE_END
  106. #endif