Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:55:07

0001 // pssr.h - originally written and placed in the public domain by Wei Dai

0002 
0003 /// \file pssr.h

0004 /// \brief Classes for probabilistic signature schemes

0005 /// \since Crypto++ 2.1

0006 
0007 #ifndef CRYPTOPP_PSSR_H
0008 #define CRYPTOPP_PSSR_H
0009 
0010 #include "cryptlib.h"
0011 #include "pubkey.h"
0012 #include "emsa2.h"
0013 
0014 #ifdef CRYPTOPP_IS_DLL
0015 #include "sha.h"
0016 #endif
0017 
0018 NAMESPACE_BEGIN(CryptoPP)
0019 
0020 /// \brief PSSR Message Encoding Method interface

0021 /// \since Crypto++ 2.1

0022 class CRYPTOPP_DLL PSSR_MEM_Base : public PK_RecoverableSignatureMessageEncodingMethod
0023 {
0024 public:
0025     virtual ~PSSR_MEM_Base() {}
0026 
0027 protected:
0028     virtual bool AllowRecovery() const =0;
0029     virtual size_t SaltLen(size_t hashLen) const =0;
0030     virtual size_t MinPadLen(size_t hashLen) const =0;
0031     virtual const MaskGeneratingFunction & GetMGF() const =0;
0032 
0033 private:
0034     size_t MinRepresentativeBitLength(size_t hashIdentifierLength, size_t digestLength) const;
0035     size_t MaxRecoverableLength(size_t representativeBitLength, size_t hashIdentifierLength, size_t digestLength) const;
0036     bool IsProbabilistic() const;
0037     bool AllowNonrecoverablePart() const;
0038     bool RecoverablePartFirst() const;
0039     void ComputeMessageRepresentative(RandomNumberGenerator &rng,
0040         const byte *recoverableMessage, size_t recoverableMessageLength,
0041         HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty,
0042         byte *representative, size_t representativeBitLength) const;
0043     DecodingResult RecoverMessageFromRepresentative(
0044         HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty,
0045         byte *representative, size_t representativeBitLength,
0046         byte *recoverableMessage) const;
0047 };
0048 
0049 /// \brief PSSR Message Encoding Method with Hash Identifier

0050 /// \tparam USE_HASH_ID flag indicating whether the HashId is used

0051 /// \since Crypto++ 2.1

0052 template <bool USE_HASH_ID> class PSSR_MEM_BaseWithHashId;
0053 
0054 /// \brief PSSR Message Encoding Method with Hash Identifier

0055 /// \details If USE_HASH_ID is true, then EMSA2HashIdLookup<PSSR_MEM_Base> is used for the base class

0056 template<> class PSSR_MEM_BaseWithHashId<true> : public EMSA2HashIdLookup<PSSR_MEM_Base> {};
0057 
0058 /// \brief PSSR Message Encoding Method without Hash Identifier

0059 /// \details If USE_HASH_ID is false, then PSSR_MEM_Base is used for the base class

0060 /// \since Crypto++ 2.1

0061 template<> class PSSR_MEM_BaseWithHashId<false> : public PSSR_MEM_Base {};
0062 
0063 /// \brief PSSR Message Encoding Method

0064 /// \tparam ALLOW_RECOVERY flag indicating whether the scheme provides message recovery

0065 /// \tparam MGF mask generation function

0066 /// \tparam SALT_LEN length of the salt

0067 /// \tparam MIN_PAD_LEN minimum length of the pad

0068 /// \tparam USE_HASH_ID flag indicating whether the HashId is used

0069 /// \details If ALLOW_RECOVERY is true, the signature scheme provides message recovery. If

0070 ///  ALLOW_RECOVERY is false, the signature scheme is appendix, and the message must be

0071 ///  provided during verification.

0072 /// \since Crypto++ 2.1

0073 template <bool ALLOW_RECOVERY, class MGF=P1363_MGF1, int SALT_LEN=-1, int MIN_PAD_LEN=0, bool USE_HASH_ID=false>
0074 class PSSR_MEM : public PSSR_MEM_BaseWithHashId<USE_HASH_ID>
0075 {
0076     virtual bool AllowRecovery() const {return ALLOW_RECOVERY;}
0077     virtual size_t SaltLen(size_t hashLen) const {return SALT_LEN < 0 ? hashLen : SALT_LEN;}
0078     virtual size_t MinPadLen(size_t hashLen) const {return MIN_PAD_LEN < 0 ? hashLen : MIN_PAD_LEN;}
0079     virtual const MaskGeneratingFunction & GetMGF() const {static MGF mgf; return mgf;}
0080 
0081 public:
0082     static std::string CRYPTOPP_API StaticAlgorithmName() {return std::string(ALLOW_RECOVERY ? "PSSR-" : "PSS-") + MGF::StaticAlgorithmName();}
0083 };
0084 
0085 /// \brief Probabilistic Signature Scheme with Recovery

0086 /// \details Signature Schemes with Recovery encode the message with the signature.

0087 /// \sa <a href="http://www.weidai.com/scan-mirror/sig.html#sem_PSSR-MGF1">PSSR-MGF1</a>

0088 /// \since Crypto++ 2.1

0089 struct PSSR : public SignatureStandard
0090 {
0091     typedef PSSR_MEM<true> SignatureMessageEncodingMethod;
0092 };
0093 
0094 /// \brief Probabilistic Signature Scheme with Appendix

0095 /// \details Signature Schemes with Appendix require the message to be provided during verification.

0096 /// \sa <a href="http://www.weidai.com/scan-mirror/sig.html#sem_PSS-MGF1">PSS-MGF1</a>

0097 /// \since Crypto++ 2.1

0098 struct PSS : public SignatureStandard
0099 {
0100     typedef PSSR_MEM<false> SignatureMessageEncodingMethod;
0101 };
0102 
0103 NAMESPACE_END
0104 
0105 #endif