rw.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // rw.h - originally written and placed in the public domain by Wei Dai
  2. /// \file rw.h
  3. /// \brief Classes for Rabin-Williams signature scheme
  4. /// \details The implementation provides Rabin-Williams signature schemes as defined in
  5. /// IEEE P1363. It uses Bernstein's tweaked square roots in place of square roots to
  6. /// speedup calculations.
  7. /// \sa <A HREF="http://cr.yp.to/sigs/rwsota-20080131.pdf">RSA signatures and Rabin–Williams
  8. /// signatures: the state of the art (20080131)</A>, Section 6, <em>The tweaks e and f</em>.
  9. /// \since Crypto++ 3.0
  10. #ifndef CRYPTOPP_RW_H
  11. #define CRYPTOPP_RW_H
  12. #include "cryptlib.h"
  13. #include "pubkey.h"
  14. #include "integer.h"
  15. NAMESPACE_BEGIN(CryptoPP)
  16. /// \brief Rabin-Williams trapdoor function using the public key
  17. /// \since Crypto++ 3.0, Tweaked roots using <em>e</em> and <em>f</em> since Crypto++ 5.6.4
  18. class CRYPTOPP_DLL RWFunction : public TrapdoorFunction, public PublicKey
  19. {
  20. typedef RWFunction ThisClass;
  21. public:
  22. /// \brief Initialize a Rabin-Williams public key
  23. /// \param n the modulus
  24. void Initialize(const Integer &n)
  25. {m_n = n;}
  26. void BERDecode(BufferedTransformation &bt);
  27. void DEREncode(BufferedTransformation &bt) const;
  28. void Save(BufferedTransformation &bt) const
  29. {DEREncode(bt);}
  30. void Load(BufferedTransformation &bt)
  31. {BERDecode(bt);}
  32. Integer ApplyFunction(const Integer &x) const;
  33. Integer PreimageBound() const {return ++(m_n>>1);}
  34. Integer ImageBound() const {return m_n;}
  35. bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
  36. bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
  37. void AssignFrom(const NameValuePairs &source);
  38. const Integer& GetModulus() const {return m_n;}
  39. void SetModulus(const Integer &n) {m_n = n;}
  40. protected:
  41. Integer m_n;
  42. };
  43. /// \brief Rabin-Williams trapdoor function using the private key
  44. /// \since Crypto++ 3.0, Tweaked roots using <em>e</em> and <em>f</em> since Crypto++ 5.6.4
  45. class CRYPTOPP_DLL InvertibleRWFunction : public RWFunction, public TrapdoorFunctionInverse, public PrivateKey
  46. {
  47. typedef InvertibleRWFunction ThisClass;
  48. public:
  49. /// \brief Construct an InvertibleRWFunction
  50. InvertibleRWFunction() : m_precompute(false) {}
  51. /// \brief Initialize a Rabin-Williams private key
  52. /// \param n modulus
  53. /// \param p first prime factor
  54. /// \param q second prime factor
  55. /// \param u q<sup>-1</sup> mod p
  56. /// \details This Initialize() function overload initializes a private key from existing parameters.
  57. void Initialize(const Integer &n, const Integer &p, const Integer &q, const Integer &u);
  58. /// \brief Create a Rabin-Williams private key
  59. /// \param rng a RandomNumberGenerator derived class
  60. /// \param modulusBits the size of the modulus, in bits
  61. /// \details This function overload of Initialize() creates a new private key because it
  62. /// takes a RandomNumberGenerator() as a parameter. If you have an existing keypair,
  63. /// then use one of the other Initialize() overloads.
  64. void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits)
  65. {GenerateRandomWithKeySize(rng, modulusBits);}
  66. void BERDecode(BufferedTransformation &bt);
  67. void DEREncode(BufferedTransformation &bt) const;
  68. void Save(BufferedTransformation &bt) const
  69. {DEREncode(bt);}
  70. void Load(BufferedTransformation &bt)
  71. {BERDecode(bt);}
  72. Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const;
  73. // GeneratibleCryptoMaterial
  74. bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
  75. bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
  76. void AssignFrom(const NameValuePairs &source);
  77. /*! parameters: (ModulusSize) */
  78. void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
  79. const Integer& GetPrime1() const {return m_p;}
  80. const Integer& GetPrime2() const {return m_q;}
  81. const Integer& GetMultiplicativeInverseOfPrime2ModPrime1() const {return m_u;}
  82. void SetPrime1(const Integer &p) {m_p = p;}
  83. void SetPrime2(const Integer &q) {m_q = q;}
  84. void SetMultiplicativeInverseOfPrime2ModPrime1(const Integer &u) {m_u = u;}
  85. virtual bool SupportsPrecomputation() const {return true;}
  86. virtual void Precompute(unsigned int unused = 0) {CRYPTOPP_UNUSED(unused); PrecomputeTweakedRoots();}
  87. virtual void Precompute(unsigned int unused = 0) const {CRYPTOPP_UNUSED(unused); PrecomputeTweakedRoots();}
  88. virtual void LoadPrecomputation(BufferedTransformation &storedPrecomputation);
  89. virtual void SavePrecomputation(BufferedTransformation &storedPrecomputation) const;
  90. protected:
  91. void PrecomputeTweakedRoots() const;
  92. protected:
  93. Integer m_p, m_q, m_u;
  94. mutable Integer m_pre_2_9p, m_pre_2_3q, m_pre_q_p;
  95. mutable bool m_precompute;
  96. };
  97. /// \brief Rabin-Williams keys
  98. /// \since Crypto++ 3.0
  99. struct RW
  100. {
  101. CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "RW";}
  102. typedef RWFunction PublicKey;
  103. typedef InvertibleRWFunction PrivateKey;
  104. };
  105. /// \brief Rabin-Williams signature scheme
  106. /// \tparam STANDARD signature standard
  107. /// \tparam H hash transformation
  108. /// \since Crypto++ 3.0
  109. template <class STANDARD, class H>
  110. struct RWSS : public TF_SS<RW, STANDARD, H>
  111. {
  112. };
  113. NAMESPACE_END
  114. #endif