pssr.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // pssr.h - originally written and placed in the public domain by Wei Dai
  2. /// \file pssr.h
  3. /// \brief Classes for probabilistic signature schemes
  4. /// \since Crypto++ 2.1
  5. #ifndef CRYPTOPP_PSSR_H
  6. #define CRYPTOPP_PSSR_H
  7. #include "cryptlib.h"
  8. #include "pubkey.h"
  9. #include "emsa2.h"
  10. #ifdef CRYPTOPP_IS_DLL
  11. #include "sha.h"
  12. #endif
  13. NAMESPACE_BEGIN(CryptoPP)
  14. /// \brief PSSR Message Encoding Method interface
  15. /// \since Crypto++ 2.1
  16. class CRYPTOPP_DLL PSSR_MEM_Base : public PK_RecoverableSignatureMessageEncodingMethod
  17. {
  18. public:
  19. virtual ~PSSR_MEM_Base() {}
  20. protected:
  21. virtual bool AllowRecovery() const =0;
  22. virtual size_t SaltLen(size_t hashLen) const =0;
  23. virtual size_t MinPadLen(size_t hashLen) const =0;
  24. virtual const MaskGeneratingFunction & GetMGF() const =0;
  25. private:
  26. size_t MinRepresentativeBitLength(size_t hashIdentifierLength, size_t digestLength) const;
  27. size_t MaxRecoverableLength(size_t representativeBitLength, size_t hashIdentifierLength, size_t digestLength) const;
  28. bool IsProbabilistic() const;
  29. bool AllowNonrecoverablePart() const;
  30. bool RecoverablePartFirst() const;
  31. void ComputeMessageRepresentative(RandomNumberGenerator &rng,
  32. const byte *recoverableMessage, size_t recoverableMessageLength,
  33. HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty,
  34. byte *representative, size_t representativeBitLength) const;
  35. DecodingResult RecoverMessageFromRepresentative(
  36. HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty,
  37. byte *representative, size_t representativeBitLength,
  38. byte *recoverableMessage) const;
  39. };
  40. /// \brief PSSR Message Encoding Method with Hash Identifier
  41. /// \tparam USE_HASH_ID flag indicating whether the HashId is used
  42. /// \since Crypto++ 2.1
  43. template <bool USE_HASH_ID> class PSSR_MEM_BaseWithHashId;
  44. /// \brief PSSR Message Encoding Method with Hash Identifier
  45. /// \details If USE_HASH_ID is true, then EMSA2HashIdLookup<PSSR_MEM_Base> is used for the base class
  46. template<> class PSSR_MEM_BaseWithHashId<true> : public EMSA2HashIdLookup<PSSR_MEM_Base> {};
  47. /// \brief PSSR Message Encoding Method without Hash Identifier
  48. /// \details If USE_HASH_ID is false, then PSSR_MEM_Base is used for the base class
  49. /// \since Crypto++ 2.1
  50. template<> class PSSR_MEM_BaseWithHashId<false> : public PSSR_MEM_Base {};
  51. /// \brief PSSR Message Encoding Method
  52. /// \tparam ALLOW_RECOVERY flag indicating whether the scheme provides message recovery
  53. /// \tparam MGF mask generation function
  54. /// \tparam SALT_LEN length of the salt
  55. /// \tparam MIN_PAD_LEN minimum length of the pad
  56. /// \tparam USE_HASH_ID flag indicating whether the HashId is used
  57. /// \details If ALLOW_RECOVERY is true, the signature scheme provides message recovery. If
  58. /// ALLOW_RECOVERY is false, the signature scheme is appendix, and the message must be
  59. /// provided during verification.
  60. /// \since Crypto++ 2.1
  61. template <bool ALLOW_RECOVERY, class MGF=P1363_MGF1, int SALT_LEN=-1, int MIN_PAD_LEN=0, bool USE_HASH_ID=false>
  62. class PSSR_MEM : public PSSR_MEM_BaseWithHashId<USE_HASH_ID>
  63. {
  64. virtual bool AllowRecovery() const {return ALLOW_RECOVERY;}
  65. virtual size_t SaltLen(size_t hashLen) const {return SALT_LEN < 0 ? hashLen : SALT_LEN;}
  66. virtual size_t MinPadLen(size_t hashLen) const {return MIN_PAD_LEN < 0 ? hashLen : MIN_PAD_LEN;}
  67. virtual const MaskGeneratingFunction & GetMGF() const {static MGF mgf; return mgf;}
  68. public:
  69. static std::string CRYPTOPP_API StaticAlgorithmName() {return std::string(ALLOW_RECOVERY ? "PSSR-" : "PSS-") + MGF::StaticAlgorithmName();}
  70. };
  71. /// \brief Probabilistic Signature Scheme with Recovery
  72. /// \details Signature Schemes with Recovery encode the message with the signature.
  73. /// \sa <a href="http://www.weidai.com/scan-mirror/sig.html#sem_PSSR-MGF1">PSSR-MGF1</a>
  74. /// \since Crypto++ 2.1
  75. struct PSSR : public SignatureStandard
  76. {
  77. typedef PSSR_MEM<true> SignatureMessageEncodingMethod;
  78. };
  79. /// \brief Probabilistic Signature Scheme with Appendix
  80. /// \details Signature Schemes with Appendix require the message to be provided during verification.
  81. /// \sa <a href="http://www.weidai.com/scan-mirror/sig.html#sem_PSS-MGF1">PSS-MGF1</a>
  82. /// \since Crypto++ 2.1
  83. struct PSS : public SignatureStandard
  84. {
  85. typedef PSSR_MEM<false> SignatureMessageEncodingMethod;
  86. };
  87. NAMESPACE_END
  88. #endif