Crypto++  8.8
Free C++ class library of cryptographic schemes
esign.h
Go to the documentation of this file.
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 
16 NAMESPACE_BEGIN(CryptoPP)
17 
18 /// \brief ESIGN trapdoor function using the public key
19 /// \since Crypto++ 5.0
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
63 {
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.
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.
83  void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits)
84  {GenerateRandomWithKeySize(rng, modulusBits);}
85 
86  // Squash Visual Studio C4250 warning
87  void Save(BufferedTransformation &bt) const
88  {BEREncode(bt);}
89 
90  // Squash Visual Studio C4250 warning
92  {BERDecode(bt);}
93 
95  void DEREncode(BufferedTransformation &bt) const;
96 
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 
106  const Integer& GetPrime1() const {return m_p;}
107  const Integer& GetPrime2() const {return m_q;}
108 
109  void SetPrime1(const Integer &p) {m_p = p;}
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>
121 {
122 public:
123  CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "EMSA5";}
124 
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
145 {
147 };
148 
149 /// \brief ESIGN keys
150 /// \since Crypto++ 5.0
152 {
153  CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "ESIGN";}
154  typedef ESIGNFunction PublicKey;
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
Classes and functions for working with ANS.1 objects.
Encode and decode ASN.1 objects with additional information.
Definition: asn.h:684
virtual void BEREncode(BufferedTransformation &bt) const
Encode this object into a BufferedTransformation.
Definition: cryptlib.h:3307
Interface for buffered transformations.
Definition: cryptlib.h:1657
EMSA5 padding method.
Definition: esign.h:121
ESIGN trapdoor function using the public key.
Definition: esign.h:21
Integer PreimageBound() const
Returns the maximum size of a message before the trapdoor function is applied.
Definition: esign.h:43
void Initialize(const Integer &n, const Integer &e)
Initialize a ESIGN public key with {n,e}.
Definition: esign.h:29
Integer ImageBound() const
Returns the maximum size of a representation after the trapdoor function is applied.
Definition: esign.h:44
void GenerateRandomWithKeySize(RandomNumberGenerator &rng, unsigned int keySize)
Generate a random key or crypto parameters.
Interface for hash functions and data processing part of MACs.
Definition: cryptlib.h:1118
virtual unsigned int DigestSize() const =0
Provides the digest size of the hash.
virtual void Final(byte *digest)
Computes the hash of the current message.
Definition: cryptlib.h:1147
Multiple precision integer with arithmetic operations.
Definition: integer.h:50
static Integer Power2(size_t e)
Exponentiates to a power of 2.
ESIGN trapdoor function using the private key.
Definition: esign.h:63
Integer CalculateRandomizedInverse(RandomNumberGenerator &rng, const Integer &x) const
Applies the inverse of the trapdoor function, using random data if required.
Definition: esign.cpp:153
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
Definition: esign.cpp:184
void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg)
Definition: esign.cpp:88
void Initialize(const Integer &n, const Integer &e, const Integer &p, const Integer &q)
Initialize a ESIGN private key with {n,e,p,q}.
Definition: esign.h:74
void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits)
Create a ESIGN private key.
Definition: esign.h:83
void Save(BufferedTransformation &bt) const
Saves a key to a BufferedTransformation.
Definition: esign.h:87
void Load(BufferedTransformation &bt)
Loads a key from a BufferedTransformation.
Definition: esign.h:91
void DEREncode(BufferedTransformation &bt) const
Encode this object into a BufferedTransformation.
Definition: esign.cpp:143
void BERDecode(BufferedTransformation &bt)
Decode this object from a BufferedTransformation.
Definition: esign.cpp:133
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Definition: esign.cpp:207
void AssignFrom(const NameValuePairs &source)
Assign values to this object.
Definition: esign.cpp:215
Interface for retrieving values given their names.
Definition: cryptlib.h:327
Interface for message encoding method for public key signature schemes.
Definition: pubkey.h:392
Interface for private keys.
Definition: cryptlib.h:2546
Interface for public keys.
Definition: cryptlib.h:2541
Interface for random number generators.
Definition: cryptlib.h:1440
Applies the inverse of the trapdoor function, using random data if required.
Definition: pubkey.h:155
size_type size() const
Provides the count of elements in the SecBlock.
Definition: secblock.h:867
SecBlock<byte> typedef.
Definition: secblock.h:1226
Trapdoor Function (TF) Signature Scheme.
Definition: pubkey.h:2316
Applies the trapdoor function.
Definition: pubkey.h:126
unsigned char byte
8-bit unsigned datatype
Definition: config_int.h:66
Abstract base classes that provide a uniform interface to this library.
Multiple precision integer with arithmetic operations.
Utility functions for the Crypto++ library.
T1 SaturatingSubtract(const T1 &a, const T2 &b)
Performs a saturating subtract clamped at 0.
Definition: misc.h:1302
T Crop(T value, size_t bits)
Truncates the value to the specified number of bits.
Definition: misc.h:1131
size_t BitsToBytes(size_t bitCount)
Returns the number of 8-bit bytes or octets required for the specified number of bits.
Definition: misc.h:1143
Crypto++ library namespace.
This file contains helper classes/functions for implementing public key algorithms.
ESIGN keys.
Definition: esign.h:152
ESIGN signature scheme, IEEE P1363a.
Definition: esign.h:164
EMSA5 padding method, for use with ESIGN.
Definition: esign.h:145
Base class for public key signature standard classes.
Definition: pubkey.h:2279