Crypto++ 8.9 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
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.
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
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
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>
120 class EMSA5Pad : public PK_DeterministicSignatureMessageEncodingMethod
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
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 {
153 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
asn.h
Classes and functions for working with ANS.1 objects.
ASN1CryptoMaterial
Encode and decode ASN.1 objects with additional information.
Definition: asn.h:685
ASN1Object::BEREncode
virtual void BEREncode(BufferedTransformation &bt) const
Encode this object into a BufferedTransformation.
Definition: cryptlib.h:3307
BufferedTransformation
Interface for buffered transformations.
Definition: cryptlib.h:1657
EMSA5Pad
EMSA5 padding method.
Definition: esign.h:121
ESIGNFunction
ESIGN trapdoor function using the public key.
Definition: esign.h:21
ESIGNFunction::PreimageBound
Integer PreimageBound() const
Returns the maximum size of a message before the trapdoor function is applied.
Definition: esign.h:43
ESIGNFunction::Initialize
void Initialize(const Integer &n, const Integer &e)
Initialize a ESIGN public key with {n,e}.
Definition: esign.h:29
ESIGNFunction::ImageBound
Integer ImageBound() const
Returns the maximum size of a representation after the trapdoor function is applied.
Definition: esign.h:44
GeneratableCryptoMaterial::GenerateRandomWithKeySize
void GenerateRandomWithKeySize(RandomNumberGenerator &rng, unsigned int keySize)
Generate a random key or crypto parameters.
HashTransformation
Interface for hash functions and data processing part of MACs.
Definition: cryptlib.h:1118
HashTransformation::DigestSize
virtual unsigned int DigestSize() const =0
Provides the digest size of the hash.
HashTransformation::Final
virtual void Final(byte *digest)
Computes the hash of the current message.
Definition: cryptlib.h:1147
Integer
Multiple precision integer with arithmetic operations.
Definition: integer.h:50
Integer::Power2
static Integer Power2(size_t e)
Exponentiates to a power of 2.
InvertibleESIGNFunction
ESIGN trapdoor function using the private key.
Definition: esign.h:63
InvertibleESIGNFunction::CalculateRandomizedInverse
Integer CalculateRandomizedInverse(RandomNumberGenerator &rng, const Integer &x) const
Applies the inverse of the trapdoor function, using random data if required.
Definition: esign.cpp:156
InvertibleESIGNFunction::Validate
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
Definition: esign.cpp:187
InvertibleESIGNFunction::GenerateRandom
void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg)
Definition: esign.cpp:88
InvertibleESIGNFunction::Initialize
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
InvertibleESIGNFunction::Initialize
void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits)
Create a ESIGN private key.
Definition: esign.h:83
InvertibleESIGNFunction::Save
void Save(BufferedTransformation &bt) const
Saves a key to a BufferedTransformation.
Definition: esign.h:87
InvertibleESIGNFunction::Load
void Load(BufferedTransformation &bt)
Loads a key from a BufferedTransformation.
Definition: esign.h:91
InvertibleESIGNFunction::DEREncode
void DEREncode(BufferedTransformation &bt) const
Encode this object into a BufferedTransformation.
Definition: esign.cpp:146
InvertibleESIGNFunction::BERDecode
void BERDecode(BufferedTransformation &bt)
Decode this object from a BufferedTransformation.
Definition: esign.cpp:136
InvertibleESIGNFunction::GetVoidValue
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Definition: esign.cpp:210
InvertibleESIGNFunction::AssignFrom
void AssignFrom(const NameValuePairs &source)
Assign values to this object.
Definition: esign.cpp:218
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:327
PK_DeterministicSignatureMessageEncodingMethod
Interface for message encoding method for public key signature schemes.
Definition: pubkey.h:392
PrivateKey
Interface for private keys.
Definition: cryptlib.h:2546
PublicKey
Interface for public keys.
Definition: cryptlib.h:2541
RandomNumberGenerator
Interface for random number generators.
Definition: cryptlib.h:1440
RandomizedTrapdoorFunctionInverse
Applies the inverse of the trapdoor function, using random data if required.
Definition: pubkey.h:155
SecBlock::size
size_type size() const
Provides the count of elements in the SecBlock.
Definition: secblock.h:867
SecByteBlock
SecBlock<byte> typedef.
Definition: secblock.h:1226
TF_SS
Trapdoor Function (TF) Signature Scheme.
Definition: pubkey.h:2316
TrapdoorFunction
Applies the trapdoor function.
Definition: pubkey.h:126
byte
unsigned char byte
8-bit unsigned datatype
Definition: config_int.h:66
cryptlib.h
Abstract base classes that provide a uniform interface to this library.
integer.h
Multiple precision integer with arithmetic operations.
misc.h
Utility functions for the Crypto++ library.
SaturatingSubtract
T1 SaturatingSubtract(const T1 &a, const T2 &b)
Performs a saturating subtract clamped at 0.
Definition: misc.h:1302
Crop
T Crop(T value, size_t bits)
Truncates the value to the specified number of bits.
Definition: misc.h:1131
BitsToBytes
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
CryptoPP
Crypto++ library namespace.
pubkey.h
This file contains helper classes/functions for implementing public key algorithms.
ESIGN_Keys
ESIGN keys.
Definition: esign.h:152
ESIGN
ESIGN signature scheme, IEEE P1363a.
Definition: esign.h:164
P1363_EMSA5
EMSA5 padding method, for use with ESIGN.
Definition: esign.h:145
SignatureStandard
Base class for public key signature standard classes.
Definition: pubkey.h:2279