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.

106 lines
2.9 KiB

  1. // rw.h - written and placed in the public domain by Wei Dai
  2. //! \file rw.h
  3. //! \brief Classes for Rabin-Williams signature schemes
  4. //! \details Rabin-Williams signature schemes as defined in IEEE P1363.
  5. #ifndef CRYPTOPP_RW_H
  6. #define CRYPTOPP_RW_H
  7. #include "cryptlib.h"
  8. #include "pubkey.h"
  9. #include "integer.h"
  10. NAMESPACE_BEGIN(CryptoPP)
  11. //! _
  12. class CRYPTOPP_DLL RWFunction : public TrapdoorFunction, public PublicKey
  13. {
  14. typedef RWFunction ThisClass;
  15. public:
  16. void Initialize(const Integer &n)
  17. {m_n = n;}
  18. void BERDecode(BufferedTransformation &bt);
  19. void DEREncode(BufferedTransformation &bt) const;
  20. void Save(BufferedTransformation &bt) const
  21. {DEREncode(bt);}
  22. void Load(BufferedTransformation &bt)
  23. {BERDecode(bt);}
  24. Integer ApplyFunction(const Integer &x) const;
  25. Integer PreimageBound() const {return ++(m_n>>1);}
  26. Integer ImageBound() const {return m_n;}
  27. bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
  28. bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
  29. void AssignFrom(const NameValuePairs &source);
  30. const Integer& GetModulus() const {return m_n;}
  31. void SetModulus(const Integer &n) {m_n = n;}
  32. protected:
  33. Integer m_n;
  34. };
  35. //! _
  36. class CRYPTOPP_DLL InvertibleRWFunction : public RWFunction, public TrapdoorFunctionInverse, public PrivateKey
  37. {
  38. typedef InvertibleRWFunction ThisClass;
  39. public:
  40. void Initialize(const Integer &n, const Integer &p, const Integer &q, const Integer &u)
  41. {m_n = n; m_p = p; m_q = q; m_u = u;}
  42. // generate a random private key
  43. void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits)
  44. {GenerateRandomWithKeySize(rng, modulusBits);}
  45. void BERDecode(BufferedTransformation &bt);
  46. void DEREncode(BufferedTransformation &bt) const;
  47. void Save(BufferedTransformation &bt) const
  48. {DEREncode(bt);}
  49. void Load(BufferedTransformation &bt)
  50. {BERDecode(bt);}
  51. Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const;
  52. // GeneratibleCryptoMaterial
  53. bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
  54. bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
  55. void AssignFrom(const NameValuePairs &source);
  56. /*! parameters: (ModulusSize) */
  57. void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
  58. const Integer& GetPrime1() const {return m_p;}
  59. const Integer& GetPrime2() const {return m_q;}
  60. const Integer& GetMultiplicativeInverseOfPrime2ModPrime1() const {return m_u;}
  61. void SetPrime1(const Integer &p) {m_p = p;}
  62. void SetPrime2(const Integer &q) {m_q = q;}
  63. void SetMultiplicativeInverseOfPrime2ModPrime1(const Integer &u) {m_u = u;}
  64. protected:
  65. Integer m_p, m_q, m_u;
  66. };
  67. //! RW
  68. struct RW
  69. {
  70. static std::string StaticAlgorithmName() {return "RW";}
  71. typedef RWFunction PublicKey;
  72. typedef InvertibleRWFunction PrivateKey;
  73. };
  74. //! RWSS
  75. template <class STANDARD, class H>
  76. struct RWSS : public TF_SS<STANDARD, H, RW>
  77. {
  78. };
  79. NAMESPACE_END
  80. #endif