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