Back to home page

EIC code displayed by LXR

 
 

    


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

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

0002 
0003 /// \file esign.h

0004 /// \brief Classes providing ESIGN signature schemes as defined in IEEE P1363a

0005 /// \since Crypto++ 5.0

0006 
0007 #ifndef CRYPTOPP_ESIGN_H
0008 #define CRYPTOPP_ESIGN_H
0009 
0010 #include "cryptlib.h"
0011 #include "pubkey.h"
0012 #include "integer.h"
0013 #include "asn.h"
0014 #include "misc.h"
0015 
0016 NAMESPACE_BEGIN(CryptoPP)
0017 
0018 /// \brief ESIGN trapdoor function using the public key

0019 /// \since Crypto++ 5.0

0020 class ESIGNFunction : public TrapdoorFunction, public ASN1CryptoMaterial<PublicKey>
0021 {
0022     typedef ESIGNFunction ThisClass;
0023 
0024 public:
0025 
0026     /// \brief Initialize a ESIGN public key with {n,e}

0027     /// \param n the modulus

0028     /// \param e the public exponent

0029     void Initialize(const Integer &n, const Integer &e)
0030         {m_n = n; m_e = e;}
0031 
0032     // PublicKey

0033     void BERDecode(BufferedTransformation &bt);
0034     void DEREncode(BufferedTransformation &bt) const;
0035 
0036     // CryptoMaterial

0037     bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
0038     bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
0039     void AssignFrom(const NameValuePairs &source);
0040 
0041     // TrapdoorFunction

0042     Integer ApplyFunction(const Integer &x) const;
0043     Integer PreimageBound() const {return m_n;}
0044     Integer ImageBound() const {return Integer::Power2(GetK());}
0045 
0046     // non-derived

0047     const Integer & GetModulus() const {return m_n;}
0048     const Integer & GetPublicExponent() const {return m_e;}
0049 
0050     void SetModulus(const Integer &n) {m_n = n;}
0051     void SetPublicExponent(const Integer &e) {m_e = e;}
0052 
0053 protected:
0054     // Covertiy finding on overflow. The library allows small values for research purposes.

0055     unsigned int GetK() const {return SaturatingSubtract(m_n.BitCount()/3, 1U);}
0056 
0057     Integer m_n, m_e;
0058 };
0059 
0060 /// \brief ESIGN trapdoor function using the private key

0061 /// \since Crypto++ 5.0

0062 class InvertibleESIGNFunction : public ESIGNFunction, public RandomizedTrapdoorFunctionInverse, public PrivateKey
0063 {
0064     typedef InvertibleESIGNFunction ThisClass;
0065 
0066 public:
0067 
0068     /// \brief Initialize a ESIGN private key with {n,e,p,q}

0069     /// \param n modulus

0070     /// \param e public exponent

0071     /// \param p first prime factor

0072     /// \param q second prime factor

0073     /// \details This Initialize() function overload initializes a private key from existing parameters.

0074     void Initialize(const Integer &n, const Integer &e, const Integer &p, const Integer &q)
0075         {m_n = n; m_e = e; m_p = p; m_q = q;}
0076 
0077     /// \brief Create a ESIGN private key

0078     /// \param rng a RandomNumberGenerator derived class

0079     /// \param modulusBits the size of the modulud, in bits

0080     /// \details This function overload of Initialize() creates a new private key because it

0081     ///   takes a RandomNumberGenerator() as a parameter. If you have an existing keypair,

0082     ///   then use one of the other Initialize() overloads.

0083     void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits)
0084         {GenerateRandomWithKeySize(rng, modulusBits);}
0085 
0086     // Squash Visual Studio C4250 warning

0087     void Save(BufferedTransformation &bt) const
0088         {BEREncode(bt);}
0089 
0090     // Squash Visual Studio C4250 warning

0091     void Load(BufferedTransformation &bt)
0092         {BERDecode(bt);}
0093 
0094     void BERDecode(BufferedTransformation &bt);
0095     void DEREncode(BufferedTransformation &bt) const;
0096 
0097     Integer CalculateRandomizedInverse(RandomNumberGenerator &rng, const Integer &x) const;
0098 
0099     // GeneratibleCryptoMaterial

0100     bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
0101     bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
0102     void AssignFrom(const NameValuePairs &source);
0103     /*! parameters: (ModulusSize) */
0104     void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
0105 
0106     const Integer& GetPrime1() const {return m_p;}
0107     const Integer& GetPrime2() const {return m_q;}
0108 
0109     void SetPrime1(const Integer &p) {m_p = p;}
0110     void SetPrime2(const Integer &q) {m_q = q;}
0111 
0112 protected:
0113     Integer m_p, m_q;
0114 };
0115 
0116 /// \brief EMSA5 padding method

0117 /// \tparam T Mask Generation Function

0118 /// \since Crypto++ 5.0

0119 template <class T>
0120 class EMSA5Pad : public PK_DeterministicSignatureMessageEncodingMethod
0121 {
0122 public:
0123     CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "EMSA5";}
0124 
0125     void ComputeMessageRepresentative(RandomNumberGenerator &rng,
0126         const byte *recoverableMessage, size_t recoverableMessageLength,
0127         HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty,
0128         byte *representative, size_t representativeBitLength) const
0129     {
0130         CRYPTOPP_UNUSED(rng), CRYPTOPP_UNUSED(recoverableMessage), CRYPTOPP_UNUSED(recoverableMessageLength);
0131         CRYPTOPP_UNUSED(messageEmpty), CRYPTOPP_UNUSED(hashIdentifier);
0132         SecByteBlock digest(hash.DigestSize());
0133         hash.Final(digest);
0134         size_t representativeByteLength = BitsToBytes(representativeBitLength);
0135         T mgf;
0136         mgf.GenerateAndMask(hash, representative, representativeByteLength, digest, digest.size(), false);
0137         if (representativeBitLength % 8 != 0)
0138             representative[0] = (byte)Crop(representative[0], representativeBitLength % 8);
0139     }
0140 };
0141 
0142 /// \brief EMSA5 padding method, for use with ESIGN

0143 /// \since Crypto++ 5.0

0144 struct P1363_EMSA5 : public SignatureStandard
0145 {
0146     typedef EMSA5Pad<P1363_MGF1> SignatureMessageEncodingMethod;
0147 };
0148 
0149 /// \brief ESIGN keys

0150 /// \since Crypto++ 5.0

0151 struct ESIGN_Keys
0152 {
0153     CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "ESIGN";}
0154     typedef ESIGNFunction PublicKey;
0155     typedef InvertibleESIGNFunction PrivateKey;
0156 };
0157 
0158 /// \brief ESIGN signature scheme, IEEE P1363a

0159 /// \tparam H HashTransformation derived class

0160 /// \tparam STANDARD Signature encoding method

0161 /// \since Crypto++ 5.0

0162 template <class H, class STANDARD = P1363_EMSA5>
0163 struct ESIGN : public TF_SS<ESIGN_Keys, STANDARD, H>
0164 {
0165 };
0166 
0167 NAMESPACE_END
0168 
0169 #endif