1 // esign.h - originally written and placed in the public domain by Wei Dai
2
3 /// \file esign.h
4 /// \brief Classes providing ESIGN signature schemes as defined in IEEE P1363a
5 /// \since Crypto++ 5.0
6
7 #ifndef CRYPTOPP_ESIGN_H
8 #define CRYPTOPP_ESIGN_H
9
10 #include "cryptlib.h"
11 #include "pubkey.h"
12 #include "integer.h"
13 #include "asn.h"
14 #include "misc.h"
15
NAMESPACE_BEGIN(CryptoPP)16 NAMESPACE_BEGIN(CryptoPP)
17
18 /// \brief ESIGN trapdoor function using the public key
19 /// \since Crypto++ 5.0
20 class ESIGNFunction : public TrapdoorFunction, public ASN1CryptoMaterial<PublicKey>
21 {
22 typedef ESIGNFunction ThisClass;
23
24 public:
25
26 /// \brief Initialize a ESIGN public key with {n,e}
27 /// \param n the modulus
28 /// \param e the public exponent
29 void Initialize(const Integer &n, const Integer &e)
30 {m_n = n; m_e = e;}
31
32 // PublicKey
33 void BERDecode(BufferedTransformation &bt);
34 void DEREncode(BufferedTransformation &bt) const;
35
36 // CryptoMaterial
37 bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
38 bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
39 void AssignFrom(const NameValuePairs &source);
40
41 // TrapdoorFunction
42 Integer ApplyFunction(const Integer &x) const;
43 Integer PreimageBound() const {return m_n;}
44 Integer ImageBound() const {return Integer::Power2(GetK());}
45
46 // non-derived
47 const Integer & GetModulus() const {return m_n;}
48 const Integer & GetPublicExponent() const {return m_e;}
49
50 void SetModulus(const Integer &n) {m_n = n;}
51 void SetPublicExponent(const Integer &e) {m_e = e;}
52
53 protected:
54 // Covertiy finding on overflow. The library allows small values for research purposes.
55 unsigned int GetK() const {return SaturatingSubtract(m_n.BitCount()/3, 1U);}
56
57 Integer m_n, m_e;
58 };
59
60 /// \brief ESIGN trapdoor function using the private key
61 /// \since Crypto++ 5.0
62 class InvertibleESIGNFunction : public ESIGNFunction, public RandomizedTrapdoorFunctionInverse, public PrivateKey
63 {
64 typedef InvertibleESIGNFunction ThisClass;
65
66 public:
67
68 /// \brief Initialize a ESIGN private key with {n,e,p,q}
69 /// \param n modulus
70 /// \param e public exponent
71 /// \param p first prime factor
72 /// \param q second prime factor
73 /// \details This Initialize() function overload initializes a private key from existing parameters.
Initialize(const Integer & n,const Integer & e,const Integer & p,const Integer & q)74 void Initialize(const Integer &n, const Integer &e, const Integer &p, const Integer &q)
75 {m_n = n; m_e = e; m_p = p; m_q = q;}
76
77 /// \brief Create a ESIGN private key
78 /// \param rng a RandomNumberGenerator derived class
79 /// \param modulusBits the size of the modulud, in bits
80 /// \details This function overload of Initialize() creates a new private key because it
81 /// takes a RandomNumberGenerator() as a parameter. If you have an existing keypair,
82 /// then use one of the other Initialize() overloads.
Initialize(RandomNumberGenerator & rng,unsigned int modulusBits)83 void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits)
84 {GenerateRandomWithKeySize(rng, modulusBits);}
85
86 // Squash Visual Studio C4250 warning
Save(BufferedTransformation & bt)87 void Save(BufferedTransformation &bt) const
88 {BEREncode(bt);}
89
90 // Squash Visual Studio C4250 warning
Load(BufferedTransformation & bt)91 void Load(BufferedTransformation &bt)
92 {BERDecode(bt);}
93
94 void BERDecode(BufferedTransformation &bt);
95 void DEREncode(BufferedTransformation &bt) const;
96
97 Integer CalculateRandomizedInverse(RandomNumberGenerator &rng, const Integer &x) const;
98
99 // GeneratibleCryptoMaterial
100 bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
101 bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
102 void AssignFrom(const NameValuePairs &source);
103 /*! parameters: (ModulusSize) */
104 void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
105
GetPrime1()106 const Integer& GetPrime1() const {return m_p;}
GetPrime2()107 const Integer& GetPrime2() const {return m_q;}
108
SetPrime1(const Integer & p)109 void SetPrime1(const Integer &p) {m_p = p;}
SetPrime2(const Integer & q)110 void SetPrime2(const Integer &q) {m_q = q;}
111
112 protected:
113 Integer m_p, m_q;
114 };
115
116 /// \brief EMSA5 padding method
117 /// \tparam T Mask Generation Function
118 /// \since Crypto++ 5.0
119 template <class T>
120 class EMSA5Pad : public PK_DeterministicSignatureMessageEncodingMethod
121 {
122 public:
StaticAlgorithmName()123 CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "EMSA5";}
124
ComputeMessageRepresentative(RandomNumberGenerator & rng,const byte * recoverableMessage,size_t recoverableMessageLength,HashTransformation & hash,HashIdentifier hashIdentifier,bool messageEmpty,byte * representative,size_t representativeBitLength)125 void ComputeMessageRepresentative(RandomNumberGenerator &rng,
126 const byte *recoverableMessage, size_t recoverableMessageLength,
127 HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty,
128 byte *representative, size_t representativeBitLength) const
129 {
130 CRYPTOPP_UNUSED(rng), CRYPTOPP_UNUSED(recoverableMessage), CRYPTOPP_UNUSED(recoverableMessageLength);
131 CRYPTOPP_UNUSED(messageEmpty), CRYPTOPP_UNUSED(hashIdentifier);
132 SecByteBlock digest(hash.DigestSize());
133 hash.Final(digest);
134 size_t representativeByteLength = BitsToBytes(representativeBitLength);
135 T mgf;
136 mgf.GenerateAndMask(hash, representative, representativeByteLength, digest, digest.size(), false);
137 if (representativeBitLength % 8 != 0)
138 representative[0] = (byte)Crop(representative[0], representativeBitLength % 8);
139 }
140 };
141
142 /// \brief EMSA5 padding method, for use with ESIGN
143 /// \since Crypto++ 5.0
144 struct P1363_EMSA5 : public SignatureStandard
145 {
146 typedef EMSA5Pad<P1363_MGF1> SignatureMessageEncodingMethod;
147 };
148
149 /// \brief ESIGN keys
150 /// \since Crypto++ 5.0
151 struct ESIGN_Keys
152 {
StaticAlgorithmNameESIGN_Keys153 CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "ESIGN";}
154 typedef ESIGNFunction PublicKey;
155 typedef InvertibleESIGNFunction PrivateKey;
156 };
157
158 /// \brief ESIGN signature scheme, IEEE P1363a
159 /// \tparam H HashTransformation derived class
160 /// \tparam STANDARD Signature encoding method
161 /// \since Crypto++ 5.0
162 template <class H, class STANDARD = P1363_EMSA5>
163 struct ESIGN : public TF_SS<ESIGN_Keys, STANDARD, H>
164 {
165 };
166
167 NAMESPACE_END
168
169 #endif
170