Fabcoin Core  0.16.2
P2P Digital Currency
esign.h
Go to the documentation of this file.
1 // esign.h - written and placed in the public domain by Wei Dai
2 
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 
17 
22 {
24 
25 public:
26 
30  void Initialize(const Integer &n, const Integer &e)
31  {m_n = n; m_e = e;}
32 
33  // PublicKey
34  void BERDecode(BufferedTransformation &bt);
35  void DEREncode(BufferedTransformation &bt) const;
36 
37  // CryptoMaterial
38  bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
39  bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
40  void AssignFrom(const NameValuePairs &source);
41 
42  // TrapdoorFunction
43  Integer ApplyFunction(const Integer &x) const;
44  Integer PreimageBound() const {return m_n;}
45  Integer ImageBound() const {return Integer::Power2(GetK());}
46 
47  // non-derived
48  const Integer & GetModulus() const {return m_n;}
49  const Integer & GetPublicExponent() const {return m_e;}
50 
51  void SetModulus(const Integer &n) {m_n = n;}
52  void SetPublicExponent(const Integer &e) {m_e = e;}
53 
54 protected:
55  // Covertiy finding on overflow. The library allows small values for research purposes.
56  unsigned int GetK() const {return SaturatingSubtract(m_n.BitCount()/3, 1U);}
57 
58  Integer m_n, m_e;
59 };
60 
65 {
67 
68 public:
69 
76  void Initialize(const Integer &n, const Integer &e, const Integer &p, const Integer &q)
77  {m_n = n; m_e = e; m_p = p; m_q = q;}
78 
85  void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits)
86  {GenerateRandomWithKeySize(rng, modulusBits);}
87 
89  void DEREncode(BufferedTransformation &bt) const;
90 
92 
93  // GeneratibleCryptoMaterial
94  bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
95  bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
96  void AssignFrom(const NameValuePairs &source);
99 
100  const Integer& GetPrime1() const {return m_p;}
101  const Integer& GetPrime2() const {return m_q;}
102 
103  void SetPrime1(const Integer &p) {m_p = p;}
104  void SetPrime2(const Integer &q) {m_q = q;}
105 
106 protected:
108 };
109 
114 template <class T>
116 {
117 public:
118  CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "EMSA5";}
119 
121  const byte *recoverableMessage, size_t recoverableMessageLength,
122  HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty,
123  byte *representative, size_t representativeBitLength) const
124  {
125  CRYPTOPP_UNUSED(rng), CRYPTOPP_UNUSED(recoverableMessage), CRYPTOPP_UNUSED(recoverableMessageLength);
126  CRYPTOPP_UNUSED(messageEmpty), CRYPTOPP_UNUSED(hashIdentifier);
127  SecByteBlock digest(hash.DigestSize());
128  hash.Final(digest);
129  size_t representativeByteLength = BitsToBytes(representativeBitLength);
130  T mgf;
131  mgf.GenerateAndMask(hash, representative, representativeByteLength, digest, digest.size(), false);
132  if (representativeBitLength % 8 != 0)
133  representative[0] = (byte)Crop(representative[0], representativeBitLength % 8);
134  }
135 };
136 
141 {
143 };
144 
146 {
147  CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "ESIGN";}
150 };
151 
157 template <class H, class STANDARD = P1363_EMSA5>
158 struct ESIGN : public TF_SS<ESIGN_Keys, STANDARD, H>
159 {
160 };
161 
163 
164 #endif
Integer CalculateRandomizedInverse(RandomNumberGenerator &rng, const Integer &x) const
Applies the inverse of the trapdoor function, using random data if required.
Definition: esign.cpp:151
InvertibleESIGNFunction PrivateKey
Definition: esign.h:149
uint8_t byte
Definition: Common.h:57
Utility functions for the Crypto++ library.
#define CRYPTOPP_STATIC_CONSTEXPR
Definition: config.h:892
const Integer & GetModulus() const
Definition: esign.h:48
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:749
unsigned int GetK() const
Definition: esign.h:56
CRYPTOPP_STATIC_CONSTEXPR const char * StaticAlgorithmName()
Definition: esign.h:147
Integer ImageBound() const
Returns the maximum size of a message after the trapdoor function is applied.
Definition: esign.h:45
#define T(i, x)
ESIGN signature scheme, IEEE P1363a.
Definition: esign.h:158
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
Abstract base classes that provide a uniform interface to this library.
CRYPTOPP_STATIC_CONSTEXPR const char * StaticAlgorithmName()
Definition: esign.h:118
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:76
void Initialize(const Integer &n, const Integer &e)
Initialize a ESIGN public key with {n,e}.
Definition: esign.h:30
void DEREncode(BufferedTransformation &bt) const
Encode this object into a BufferedTransformation.
Definition: esign.cpp:141
Interface for random number generators.
Definition: cryptlib.h:1188
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
Definition: esign.cpp:192
Interface for buffered transformations.
Definition: cryptlib.h:1352
Interface for private keys.
Definition: cryptlib.h:2186
Base class for public key signature standard classes.
Definition: pubkey.h:2071
void BERDecode(BufferedTransformation &bt)
Decode this object from a BufferedTransformation.
Definition: esign.cpp:131
#define x(i)
EMSA5 padding method.
Definition: esign.h:115
const char * source
Definition: rpcconsole.cpp:60
Interface for message encoding method for public key signature schemes.
Definition: pubkey.h:404
void SetPublicExponent(const Integer &e)
Definition: esign.h:52
const char * name
Definition: rest.cpp:36
ESIGNFunction PublicKey
Definition: esign.h:148
T Crop(T value, size_t bits)
Truncates the value to the specified number of bits.
Definition: misc.h:737
void AssignFrom(const NameValuePairs &source)
Assign values to this object.
Definition: esign.cpp:213
void SetPrime1(const Integer &p)
Definition: esign.h:103
Integer PreimageBound() const
Returns the maximum size of a message before the trapdoor function is applied.
Definition: esign.h:44
const Integer & GetPrime2() const
Definition: esign.h:101
static Integer CRYPTOPP_API Power2(size_t e)
Exponentiates to a power of 2.
Definition: integer.cpp:3008
bool Validate(int, bool, const char *)
Definition: test.cpp:884
ESIGN trapdoor function using the private key.
Definition: esign.h:64
Multiple precision integer with arithmetic operations.
Definition: integer.h:43
Integer m_e
Definition: esign.h:58
ESIGNFunction ThisClass
Definition: esign.h:23
void ComputeMessageRepresentative(RandomNumberGenerator &rng, const byte *recoverableMessage, size_t recoverableMessageLength, HashTransformation &hash, HashIdentifier hashIdentifier, bool messageEmpty, byte *representative, size_t representativeBitLength) const
Definition: esign.h:120
T1 SaturatingSubtract(const T1 &a, const T2 &b)
Performs a saturating subtract clamped at 0.
Definition: misc.h:847
Integer m_n
Definition: esign.h:58
Applies the trapdoor function.
Definition: pubkey.h:128
EMSA5 padding method, for use with ESIGN.
Definition: esign.h:140
const Integer & GetPublicExponent() const
Definition: esign.h:49
Classes and functions for working with ANS.1 objects.
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Definition: esign.cpp:205
void SetModulus(const Integer &n)
Definition: esign.h:51
virtual unsigned int DigestSize() const =0
Provides the digest size of the hash.
ESIGN trapdoor function using the public key.
Definition: esign.h:21
#define CRYPTOPP_UNUSED(x)
Definition: config.h:741
Interface for hash functions and data processing part of MACs.
Definition: cryptlib.h:930
uint8_t byte
Definition: Common.h:10
void GenerateRandomWithKeySize(RandomNumberGenerator &rng, unsigned int keySize)
Generate a random key or crypto parameters.
Definition: cryptlib.cpp:780
Multiple precision integer with arithmetic operations.
void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits)
Create a ESIGN private key.
Definition: esign.h:85
#define NAMESPACE_END
Definition: config.h:201
InvertibleESIGNFunction ThisClass
Definition: esign.h:66
#define e(i)
Definition: sha.cpp:733
Interface for public keys.
Definition: cryptlib.h:2181
Applies the inverse of the trapdoor function, using random data if required.
Definition: pubkey.h:158
std::pair< const byte *, unsigned int > HashIdentifier
Definition: pubkey.h:314
EMSA5Pad< P1363_MGF1 > SignatureMessageEncodingMethod
Definition: esign.h:142
void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg)
Definition: esign.cpp:86
virtual void Final(byte *digest)
Computes the hash of the current message.
Definition: cryptlib.h:960
void SetPrime2(const Integer &q)
Definition: esign.h:104
Encode and decode ASN.1 objects with additional information.
Definition: asn.h:374
const Integer & GetPrime1() const
Definition: esign.h:100
Interface for retrieving values given their names.
Definition: cryptlib.h:279
Trapdoor Function (TF) Signature Scheme.
Definition: pubkey.h:2105