Crypto++ 8.9
Free C++ class library of cryptographic schemes
luc.h
Go to the documentation of this file.
1// luc.h - originally written and placed in the public domain by Wei Dai
2
3/// \file luc.h
4/// \brief Classes for the LUC cryptosystem
5/// \details This class is here for historical and pedagogical interest. It has no practical advantages over other
6/// trapdoor functions and probably shouldn't be used in production software. The discrete log based LUC schemes
7/// defined later in this .h file may be of more practical interest.
8/// \since Crypto++ 2.1
9
10#ifndef CRYPTOPP_LUC_H
11#define CRYPTOPP_LUC_H
12
13#include "cryptlib.h"
14#include "gfpcrypt.h"
15#include "integer.h"
16#include "algebra.h"
17#include "secblock.h"
18
19#if CRYPTOPP_MSC_VERSION
20# pragma warning(push)
21# pragma warning(disable: 4127 4189)
22#endif
23
24#include "pkcspad.h"
25#include "integer.h"
26#include "oaep.h"
27#include "dh.h"
28
29#include <limits.h>
30
31NAMESPACE_BEGIN(CryptoPP)
32
33/// \brief The LUC function.
34/// \details This class is here for historical and pedagogical interest. It has no practical advantages over other
35/// trapdoor functions and probably shouldn't be used in production software. The discrete log based LUC schemes
36/// defined later in this .h file may be of more practical interest.
37/// \since Crypto++ 2.1
39{
40 typedef LUCFunction ThisClass;
41
42public:
43 virtual ~LUCFunction() {}
44
45 /// \brief Initialize a LUC public key with {n,e}
46 /// \param n the modulus
47 /// \param e the public exponent
48 void Initialize(const Integer &n, const Integer &e)
49 {m_n = n; m_e = e;}
50
51 void BERDecode(BufferedTransformation &bt);
52 void DEREncode(BufferedTransformation &bt) const;
53
54 Integer ApplyFunction(const Integer &x) const;
55 Integer PreimageBound() const {return m_n;}
56 Integer ImageBound() const {return m_n;}
57
58 bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
59 bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
60 void AssignFrom(const NameValuePairs &source);
61
62 // non-derived interface
63 const Integer & GetModulus() const {return m_n;}
64 const Integer & GetPublicExponent() const {return m_e;}
65
66 void SetModulus(const Integer &n) {m_n = n;}
67 void SetPublicExponent(const Integer &e) {m_e = e;}
68
69protected:
70 Integer m_n, m_e;
71};
72
73/// \brief The LUC inverse function.
74/// \details This class is here for historical and pedagogical interest. It has no practical advantages over other
75/// trapdoor functions and probably shouldn't be used in production software. The discrete log based LUC schemes
76/// defined later in this .h file may be of more practical interest.
77/// \since Crypto++ 2.1
79{
81
82public:
83 virtual ~InvertibleLUCFunction() {}
84
85 /// \brief Create a LUC private key
86 /// \param rng a RandomNumberGenerator derived class
87 /// \param modulusBits the size of the modulus, in bits
88 /// \param eStart the desired starting public exponent
89 /// \details Initialize() creates a new keypair using a starting public exponent of 17.
90 /// \details This function overload of Initialize() creates a new keypair because it
91 /// takes a RandomNumberGenerator() as a parameter. If you have an existing keypair,
92 /// then use one of the other Initialize() overloads.
93 void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits, const Integer &eStart=17);
94
95 /// \brief Initialize a LUC private key with {n,e,p,q,dp,dq,u}
96 /// \param n modulus
97 /// \param e public exponent
98 /// \param p first prime factor
99 /// \param q second prime factor
100 /// \param u q<sup>-1</sup> mod p
101 /// \details This Initialize() function overload initializes a private key from existing parameters.
102 void Initialize(const Integer &n, const Integer &e, const Integer &p, const Integer &q, const Integer &u)
103 {m_n = n; m_e = e; m_p = p; m_q = q; m_u = u;}
104
105 void BERDecode(BufferedTransformation &bt);
106 void DEREncode(BufferedTransformation &bt) const;
107
109
110 bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
111 bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
112 void AssignFrom(const NameValuePairs &source);
113 /*! parameters: (ModulusSize, PublicExponent (default 17)) */
115
116 // non-derived interface
117 const Integer& GetPrime1() const {return m_p;}
118 const Integer& GetPrime2() const {return m_q;}
119 const Integer& GetMultiplicativeInverseOfPrime2ModPrime1() const {return m_u;}
120
121 void SetPrime1(const Integer &p) {m_p = p;}
122 void SetPrime2(const Integer &q) {m_q = q;}
123 void SetMultiplicativeInverseOfPrime2ModPrime1(const Integer &u) {m_u = u;}
124
125protected:
126 Integer m_p, m_q, m_u;
127};
128
129/// \brief LUC cryptosystem
130/// \since Crypto++ 2.1
131struct LUC
132{
133 static std::string StaticAlgorithmName() {return "LUC";}
134 typedef LUCFunction PublicKey;
136};
137
138/// \brief LUC encryption scheme
139/// \tparam STANDARD signature standard
140/// \details This class is here for historical and pedagogical interest. It has no practical advantages over other
141/// trapdoor functions and probably shouldn't be used in production software. The discrete log based LUC schemes
142/// defined later in this .h file may be of more practical interest.
143/// \since Crypto++ 2.1
144template <class STANDARD>
145struct LUCES : public TF_ES<LUC, STANDARD>
146{
147};
148
149/// \brief LUC signature scheme with appendix
150/// \tparam STANDARD signature standard
151/// \tparam H hash transformation
152/// \details This class is here for historical and pedagogical interest. It has no practical advantages over other
153/// trapdoor functions and probably shouldn't be used in production software. The discrete log based LUC schemes
154/// defined later in this .h file may be of more practical interest.
155/// \since Crypto++ 2.1
156template <class STANDARD, class H>
157struct LUCSS : public TF_SS<LUC, STANDARD, H>
158{
159};
160
161// analogous to the RSA schemes defined in PKCS #1 v2.0
162typedef LUCES<OAEP<SHA1> >::Decryptor LUCES_OAEP_SHA_Decryptor;
163typedef LUCES<OAEP<SHA1> >::Encryptor LUCES_OAEP_SHA_Encryptor;
164
167
168// ********************************************************
169
170/// \brief LUC GroupParameters precomputation
171/// \details No actual precomputation is performed
172/// \since Crypto++ 2.1
174{
175public:
176 virtual ~DL_GroupPrecomputation_LUC() {}
177
178 const AbstractGroup<Element> & GetGroup() const {CRYPTOPP_ASSERT(false); throw 0;}
180 void DEREncodeElement(BufferedTransformation &bt, const Element &v) const {v.DEREncode(bt);}
181
182 // non-inherited
183 void SetModulus(const Integer &v) {m_p = v;}
184 const Integer & GetModulus() const {return m_p;}
185
186private:
187 Integer m_p;
188};
189
190/// \brief LUC Precomputation
191/// \since Crypto++ 2.1
193{
194public:
195 virtual ~DL_BasePrecomputation_LUC() {}
196
197 // DL_FixedBasePrecomputation
198 bool IsInitialized() const {return m_g.NotZero();}
199 void SetBase(const DL_GroupPrecomputation<Element> &group, const Integer &base)
200 {CRYPTOPP_UNUSED(group); m_g = base;}
202 {CRYPTOPP_UNUSED(group); return m_g;}
203 void Precompute(const DL_GroupPrecomputation<Element> &group, unsigned int maxExpBits, unsigned int storage)
204 {CRYPTOPP_UNUSED(group); CRYPTOPP_UNUSED(maxExpBits); CRYPTOPP_UNUSED(storage);}
205 void Load(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation)
206 {CRYPTOPP_UNUSED(group); CRYPTOPP_UNUSED(storedPrecomputation);}
207 void Save(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation) const
208 {CRYPTOPP_UNUSED(group); CRYPTOPP_UNUSED(storedPrecomputation);}
209 Integer Exponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent) const;
211 {
212 CRYPTOPP_UNUSED(group); CRYPTOPP_UNUSED(exponent); CRYPTOPP_UNUSED(pc2); CRYPTOPP_UNUSED(exponent2);
213 // shouldn't be called
214 throw NotImplemented("DL_BasePrecomputation_LUC: CascadeExponentiate not implemented");
215 }
216
217private:
218 Integer m_g;
219};
220
221/// \brief LUC GroupParameters specialization
222/// \since Crypto++ 2.1
223class DL_GroupParameters_LUC : public DL_GroupParameters_IntegerBasedImpl<DL_GroupPrecomputation_LUC, DL_BasePrecomputation_LUC>
224{
225public:
226 virtual ~DL_GroupParameters_LUC() {}
227
228 // DL_GroupParameters
229 bool IsIdentity(const Integer &element) const {return element == Integer::Two();}
230 void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const;
231 Element MultiplyElements(const Element &a, const Element &b) const
232 {
233 CRYPTOPP_UNUSED(a); CRYPTOPP_UNUSED(b);
234 throw NotImplemented("LUC_GroupParameters: MultiplyElements can not be implemented");
235 }
236 Element CascadeExponentiate(const Element &element1, const Integer &exponent1, const Element &element2, const Integer &exponent2) const
237 {
238 CRYPTOPP_UNUSED(element1); CRYPTOPP_UNUSED(exponent1); CRYPTOPP_UNUSED(element2); CRYPTOPP_UNUSED(exponent2);
239 throw NotImplemented("LUC_GroupParameters: MultiplyElements can not be implemented");
240 }
241
242 // NameValuePairs interface
243 bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
244 {
245 return GetValueHelper<DL_GroupParameters_IntegerBased>(this, name, valueType, pValue).Assignable();
246 }
247
248private:
249 int GetFieldType() const {return 2;}
250};
251
252/// \brief GF(p) group parameters that default to safe primes
253/// \since Crypto++ 2.1
255{
256public:
258
259protected:
260 unsigned int GetDefaultSubgroupOrderSize(unsigned int modulusSize) const {return modulusSize-1;}
261};
262
263/// \brief LUC HMP signature algorithm
264/// \since Crypto++ 2.1
266{
267public:
268 CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "LUC-HMP";}
269
270 virtual ~DL_Algorithm_LUC_HMP() {}
271
272 void Sign(const DL_GroupParameters<Integer> &params, const Integer &x, const Integer &k, const Integer &e, Integer &r, Integer &s) const;
273 bool Verify(const DL_GroupParameters<Integer> &params, const DL_PublicKey<Integer> &publicKey, const Integer &e, const Integer &r, const Integer &s) const;
274
275 size_t RLen(const DL_GroupParameters<Integer> &params) const
276 {return params.GetGroupOrder().ByteCount();}
277};
278
279/// \brief LUC signature keys
280/// \since Crypto++ 2.1
287
288/// \brief LUC-HMP, based on "Digital signature schemes based on Lucas functions" by Patrick Horster, Markus Michels, Holger Petersen
289/// \tparam H hash transformation
290/// \details This class is here for historical and pedagogical interest. It has no practical advantages over other
291/// trapdoor functions and probably shouldn't be used in production software. The discrete log based LUC schemes
292/// defined later in this .h file may be of more practical interest.
293/// \since Crypto++ 2.1
294template <class H>
295struct LUC_HMP : public DL_SS<DL_SignatureKeys_LUC, DL_Algorithm_LUC_HMP, DL_SignatureMessageEncodingMethod_DSA, H>
296{
297};
298
299/// \brief LUC encryption keys
300/// \since Crypto++ 2.1
307
308/// \brief LUC Integrated Encryption Scheme
309/// \tparam COFACTOR_OPTION cofactor multiplication option
310/// \tparam HASH HashTransformation derived class used for key drivation and MAC computation
311/// \tparam DHAES_MODE flag indicating if the MAC includes additional context parameters such as <em>u·V</em>, <em>v·U</em> and label
312/// \tparam LABEL_OCTETS flag indicating if the label size is specified in octets or bits
313/// \sa CofactorMultiplicationOption
314/// \since Crypto++ 2.1, Crypto++ 5.7 for Bouncy Castle and Botan compatibility
315template <class HASH = SHA1, class COFACTOR_OPTION = NoCofactorMultiplication, bool DHAES_MODE = true, bool LABEL_OCTETS = false>
317 : public DL_ES<
318 DL_CryptoKeys_LUC,
319 DL_KeyAgreementAlgorithm_DH<Integer, COFACTOR_OPTION>,
320 DL_KeyDerivationAlgorithm_P1363<Integer, DHAES_MODE, P1363_KDF2<HASH> >,
321 DL_EncryptionAlgorithm_Xor<HMAC<HASH>, DHAES_MODE, LABEL_OCTETS>,
322 LUC_IES<> >
323{
324 CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "LUC-IES";} // non-standard name
325};
326
327// ********************************************************
328
329/// \brief LUC-DH
331
332NAMESPACE_END
333
334#if CRYPTOPP_MSC_VERSION
335# pragma warning(pop)
336#endif
337
338#endif
Classes for performing mathematics over different fields.
Abstract group.
Definition algebra.h:27
Interface for buffered transformations.
Definition cryptlib.h:1657
Diffie-Hellman domain.
Definition dh.h:26
LUC HMP signature algorithm.
Definition luc.h:266
bool Verify(const DL_GroupParameters< Integer > &params, const DL_PublicKey< Integer > &publicKey, const Integer &e, const Integer &r, const Integer &s) const
Verify a message using a public key.
Definition luc.cpp:30
size_t RLen(const DL_GroupParameters< Integer > &params) const
Retrieve R length.
Definition luc.h:275
void Sign(const DL_GroupParameters< Integer > &params, const Integer &x, const Integer &k, const Integer &e, Integer &r, Integer &s) const
Sign a message using a private key.
Definition luc.cpp:23
LUC Precomputation.
Definition luc.h:193
void Load(const DL_GroupPrecomputation< Element > &group, BufferedTransformation &storedPrecomputation)
Retrieve previously saved precomputation.
Definition luc.h:205
Integer Exponentiate(const DL_GroupPrecomputation< Element > &group, const Integer &exponent) const
Exponentiates an element.
Definition luc.cpp:40
void Save(const DL_GroupPrecomputation< Element > &group, BufferedTransformation &storedPrecomputation) const
Save precomputation for later use.
Definition luc.h:207
void Precompute(const DL_GroupPrecomputation< Element > &group, unsigned int maxExpBits, unsigned int storage)
Perform precomputation.
Definition luc.h:203
void SetBase(const DL_GroupPrecomputation< Element > &group, const Integer &base)
Set the base element.
Definition luc.h:199
Integer CascadeExponentiate(const DL_GroupPrecomputation< Element > &group, const Integer &exponent, const DL_FixedBasePrecomputation< Integer > &pc2, const Integer &exponent2) const
Exponentiates an element.
Definition luc.h:210
bool IsInitialized() const
Determines whether this object is initialized.
Definition luc.h:198
const Integer & GetBase(const DL_GroupPrecomputation< Element > &group) const
Get the base element.
Definition luc.h:201
Discrete Log (DL) encryption scheme.
Definition pubkey.h:2362
Interface for Elgamal-like signature algorithms.
Definition pubkey.h:1407
DL_FixedBasePrecomputation interface.
Definition eprecomp.h:61
Integer-based GroupParameters default implementation.
Definition gfpcrypt.h:183
GF(p) group parameters that default to safe primes.
Definition luc.h:255
LUC GroupParameters specialization.
Definition luc.h:224
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Definition luc.h:243
Interface for Discrete Log (DL) group parameters.
Definition pubkey.h:782
virtual Integer GetGroupOrder() const
Retrieves the order of the group.
Definition pubkey.h:909
LUC GroupParameters precomputation.
Definition luc.h:174
const AbstractGroup< Element > & GetGroup() const
Retrieves AbstractGroup interface.
Definition luc.h:178
void DEREncodeElement(BufferedTransformation &bt, const Element &v) const
Encodes element in DER format.
Definition luc.h:180
Element BERDecodeElement(BufferedTransformation &bt) const
Decodes element in DER format.
Definition luc.h:179
DL_GroupPrecomputation interface.
Definition eprecomp.h:20
Discrete Log (DL) private key in GF(p) groups.
Definition gfpcrypt.h:614
Discrete Log (DL) public key in GF(p) groups.
Definition gfpcrypt.h:578
Interface for Discrete Log (DL) public keys.
Definition pubkey.h:1059
Discrete Log (DL) signature scheme.
Definition pubkey.h:2342
Multiple precision integer with arithmetic operations.
Definition integer.h:50
void DEREncode(BufferedTransformation &bt) const
Encode in DER format.
bool NotZero() const
Determines if the Integer is non-0.
Definition integer.h:338
static const Integer & Two()
Integer representing 2.
unsigned int ByteCount() const
Determines the number of bytes required to represent the Integer.
The LUC inverse function.
Definition luc.h:79
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Definition luc.cpp:213
void Initialize(const Integer &n, const Integer &e, const Integer &p, const Integer &q, const Integer &u)
Initialize a LUC private key with {n,e,p,q,dp,dq,u}.
Definition luc.h:102
void AssignFrom(const NameValuePairs &source)
Assign values to this object.
Definition luc.cpp:222
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
Definition luc.cpp:180
Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const
Calculates the inverse of an element.
Definition luc.cpp:172
void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits, const Integer &eStart=17)
Create a LUC private key.
Definition luc.cpp:137
void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg)
Definition luc.cpp:114
The LUC function.
Definition luc.h:39
Integer PreimageBound() const
Returns the maximum size of a message before the trapdoor function is applied.
Definition luc.h:55
void Initialize(const Integer &n, const Integer &e)
Initialize a LUC public key with {n,e}.
Definition luc.h:48
Integer ImageBound() const
Returns the maximum size of a representation after the trapdoor function is applied.
Definition luc.h:56
Interface for retrieving values given their names.
Definition cryptlib.h:327
A method was called which was not implemented.
Definition cryptlib.h:238
Template implementing constructors for public key algorithm classes.
Definition pubkey.h:2198
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
Trapdoor Function (TF) encryption scheme.
Definition pubkey.h:2290
Trapdoor Function (TF) Signature Scheme.
Definition pubkey.h:2316
Applies the trapdoor function.
Definition pubkey.h:126
Applies the inverse of the trapdoor function.
Definition pubkey.h:179
Abstract base classes that provide a uniform interface to this library.
Classes for Diffie-Hellman key exchange.
Classes and functions for schemes based on Discrete Logs (DL) over GF(p)
Multiple precision integer with arithmetic operations.
DH_Domain< DL_GroupParameters_LUC_DefaultSafePrime > LUC_DH
LUC-DH.
Definition luc.h:330
Crypto++ library namespace.
Classes for optimal asymmetric encryption padding.
Classes for PKCS padding schemes.
Classes and functions for secure memory allocations.
LUC encryption keys.
Definition luc.h:302
LUC signature keys.
Definition luc.h:282
Converts an enumeration to a type suitable for use as a template parameter.
Definition cryptlib.h:141
LUC-HMP, based on "Digital signature schemes based on Lucas functions" by Patrick Horster,...
Definition luc.h:296
LUC Integrated Encryption Scheme.
Definition luc.h:323
LUC encryption scheme.
Definition luc.h:146
LUC cryptosystem.
Definition luc.h:132
LUC signature scheme with appendix.
Definition luc.h:158
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Definition trap.h:68