Crypto++  5.6.5
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 
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 /// \brief LUC cryptosystem
127 struct LUC
128 {
129  static std::string StaticAlgorithmName() {return "LUC";}
130  typedef LUCFunction PublicKey;
132 };
133 
134 /// \brief LUC encryption scheme
135 /// \tparam STANDARD signature standard
136 /// \details This class is here for historical and pedagogical interest. It has no practical advantages over other
137 /// trapdoor functions and probably shouldn't be used in production software. The discrete log based LUC schemes
138 /// defined later in this .h file may be of more practical interest.
139 template <class STANDARD>
140 struct LUCES : public TF_ES<LUC, STANDARD>
141 {
142 };
143 
144 /// \brief LUC signature scheme with appendix
145 /// \tparam STANDARD signature standard
146 /// \tparam H hash transformation
147 /// \details This class is here for historical and pedagogical interest. It has no practical advantages over other
148 /// trapdoor functions and probably shouldn't be used in production software. The discrete log based LUC schemes
149 /// defined later in this .h file may be of more practical interest.
150 template <class STANDARD, class H>
151 struct LUCSS : public TF_SS<LUC, STANDARD, H>
152 {
153 };
154 
155 // analogous to the RSA schemes defined in PKCS #1 v2.0
156 typedef LUCES<OAEP<SHA1> >::Decryptor LUCES_OAEP_SHA_Decryptor;
157 typedef LUCES<OAEP<SHA1> >::Encryptor LUCES_OAEP_SHA_Encryptor;
158 
161 
162 // ********************************************************
163 
164 // no actual precomputation
166 {
167 public:
168  virtual ~DL_GroupPrecomputation_LUC() {}
169 
170  const AbstractGroup<Element> & GetGroup() const {CRYPTOPP_ASSERT(false); throw 0;}
171  Element BERDecodeElement(BufferedTransformation &bt) const {return Integer(bt);}
172  void DEREncodeElement(BufferedTransformation &bt, const Element &v) const {v.DEREncode(bt);}
173 
174  // non-inherited
175  void SetModulus(const Integer &v) {m_p = v;}
176  const Integer & GetModulus() const {return m_p;}
177 
178 private:
179  Integer m_p;
180 };
181 
182 /// _
184 {
185 public:
186  virtual ~DL_BasePrecomputation_LUC() {}
187 
188  // DL_FixedBasePrecomputation
189  bool IsInitialized() const {return m_g.NotZero();}
190  void SetBase(const DL_GroupPrecomputation<Element> &group, const Integer &base)
191  {CRYPTOPP_UNUSED(group); m_g = base;}
192  const Integer & GetBase(const DL_GroupPrecomputation<Element> &group) const
193  {CRYPTOPP_UNUSED(group); return m_g;}
194  void Precompute(const DL_GroupPrecomputation<Element> &group, unsigned int maxExpBits, unsigned int storage)
195  {CRYPTOPP_UNUSED(group); CRYPTOPP_UNUSED(maxExpBits); CRYPTOPP_UNUSED(storage);}
196  void Load(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation)
197  {CRYPTOPP_UNUSED(group); CRYPTOPP_UNUSED(storedPrecomputation);}
198  void Save(const DL_GroupPrecomputation<Element> &group, BufferedTransformation &storedPrecomputation) const
199  {CRYPTOPP_UNUSED(group); CRYPTOPP_UNUSED(storedPrecomputation);}
200  Integer Exponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent) const;
201  Integer CascadeExponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent, const DL_FixedBasePrecomputation<Integer> &pc2, const Integer &exponent2) const
202  {
203  CRYPTOPP_UNUSED(group); CRYPTOPP_UNUSED(exponent); CRYPTOPP_UNUSED(pc2); CRYPTOPP_UNUSED(exponent2);
204  // shouldn't be called
205  throw NotImplemented("DL_BasePrecomputation_LUC: CascadeExponentiate not implemented");
206  }
207 
208 private:
209  Integer m_g;
210 };
211 
212 /// \class DL_GroupParameters_LUC
213 /// \brief LUC GroupParameters specialization
214 class DL_GroupParameters_LUC : public DL_GroupParameters_IntegerBasedImpl<DL_GroupPrecomputation_LUC, DL_BasePrecomputation_LUC>
215 {
216 public:
217  virtual ~DL_GroupParameters_LUC() {}
218 
219  // DL_GroupParameters
220  bool IsIdentity(const Integer &element) const {return element == Integer::Two();}
221  void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const;
222  Element MultiplyElements(const Element &a, const Element &b) const
223  {
224  CRYPTOPP_UNUSED(a); CRYPTOPP_UNUSED(b);
225  throw NotImplemented("LUC_GroupParameters: MultiplyElements can not be implemented");
226  }
227  Element CascadeExponentiate(const Element &element1, const Integer &exponent1, const Element &element2, const Integer &exponent2) const
228  {
229  CRYPTOPP_UNUSED(element1); CRYPTOPP_UNUSED(exponent1); CRYPTOPP_UNUSED(element2); CRYPTOPP_UNUSED(exponent2);
230  throw NotImplemented("LUC_GroupParameters: MultiplyElements can not be implemented");
231  }
232 
233  // NameValuePairs interface
234  bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
235  {
236  return GetValueHelper<DL_GroupParameters_IntegerBased>(this, name, valueType, pValue).Assignable();
237  }
238 
239 private:
240  int GetFieldType() const {return 2;}
241 };
242 
243 /// \class DL_GroupParameters_LUC_DefaultSafePrime
244 /// \brief GF(p) group parameters that default to safe primes
246 {
247 public:
249 
250 protected:
251  unsigned int GetDefaultSubgroupOrderSize(unsigned int modulusSize) const {return modulusSize-1;}
252 };
253 
254 /// \class DL_Algorithm_LUC_HMP
255 /// \brief LUC HMP signature algorithm
257 {
258 public:
259  CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "LUC-HMP";}
260 
261  virtual ~DL_Algorithm_LUC_HMP() {}
262 
263  void Sign(const DL_GroupParameters<Integer> &params, const Integer &x, const Integer &k, const Integer &e, Integer &r, Integer &s) const;
264  bool Verify(const DL_GroupParameters<Integer> &params, const DL_PublicKey<Integer> &publicKey, const Integer &e, const Integer &r, const Integer &s) const;
265 
266  size_t RLen(const DL_GroupParameters<Integer> &params) const
267  {return params.GetGroupOrder().ByteCount();}
268 };
269 
270 /// \brief LUC signature keys
272 {
276 };
277 
278 /// \brief LUC-HMP, based on "Digital signature schemes based on Lucas functions" by Patrick Horster, Markus Michels, Holger Petersen
279 /// \tparam H hash transformation
280 /// \details This class is here for historical and pedagogical interest. It has no practical advantages over other
281 /// trapdoor functions and probably shouldn't be used in production software. The discrete log based LUC schemes
282 /// defined later in this .h file may be of more practical interest.
283 template <class H>
284 struct LUC_HMP : public DL_SS<DL_SignatureKeys_LUC, DL_Algorithm_LUC_HMP, DL_SignatureMessageEncodingMethod_DSA, H>
285 {
286 };
287 
288 /// \brief LUC encryption keys
290 {
294 };
295 
296 /// \class LUC_IES
297 /// \brief LUC Integrated Encryption Scheme
298 /// \tparam COFACTOR_OPTION cofactor multiplication option
299 /// \tparam HASH HashTransformation derived class used for key drivation and MAC computation
300 /// \tparam DHAES_MODE flag indicating if the MAC includes additional context parameters such as <em>u·V</em>, <em>v·U</em> and label
301 /// \tparam LABEL_OCTETS flag indicating if the label size is specified in octets or bits
302 /// \sa CofactorMultiplicationOption
303 /// \since Crypto++ 4.0, Crypto++ 5.7 for Bouncy Castle and Botan compatibility
304 template <class HASH = SHA1, class COFACTOR_OPTION = NoCofactorMultiplication, bool DHAES_MODE = true, bool LABEL_OCTETS = false>
305 struct LUC_IES
306  : public DL_ES<
307  DL_CryptoKeys_LUC,
308  DL_KeyAgreementAlgorithm_DH<Integer, COFACTOR_OPTION>,
309  DL_KeyDerivationAlgorithm_P1363<Integer, DHAES_MODE, P1363_KDF2<HASH> >,
310  DL_EncryptionAlgorithm_Xor<HMAC<HASH>, DHAES_MODE, LABEL_OCTETS>,
311  LUC_IES<> >
312 {
313  CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "LUC-IES";} // non-standard name
314 };
315 
316 // ********************************************************
317 
318 /// LUC-DH
320 
321 NAMESPACE_END
322 
323 #if CRYPTOPP_MSC_VERSION
324 # pragma warning(pop)
325 #endif
326 
327 #endif
virtual void Precompute(unsigned int precomputationStorage)
Perform precomputation.
Definition: cryptlib.h:2256
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
Trapdoor Function (TF) encryption scheme.
Definition: pubkey.h:2239
LUC signature keys.
Definition: luc.h:271
LUC HMP signature algorithm.
Definition: luc.h:256
void DEREncode(BufferedTransformation &bt) const
Encode in DER format.
Definition: integer.cpp:3401
virtual void Load(BufferedTransformation &bt)
Loads a key from a BufferedTransformation.
Definition: cryptlib.h:2240
LUC Integrated Encryption Scheme.
Definition: luc.h:305
Converts an enumeration to a type suitable for use as a template parameter.
Definition: cryptlib.h:133
Abstract base classes that provide a uniform interface to this library.
LUC GroupParameters specialization.
Definition: luc.h:214
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Definition: luc.cpp:213
virtual Integer GetGroupOrder() const
Retrieves the order of the group.
Definition: pubkey.h:894
Interface for random number generators.
Definition: cryptlib.h:1339
LUC cryptosystem.
Definition: luc.h:127
Discrete Log (DL) encryption scheme.
Definition: pubkey.h:2316
Classes for performing mathematics over different fields.
Interface for buffered transformations.
Definition: cryptlib.h:1486
Interface for private keys.
Definition: cryptlib.h:2317
Interface for Discrete Log (DL) public keys.
Definition: pubkey.h:1043
LUC encryption keys.
Definition: luc.h:289
GF(p) group parameters that default to safe primes.
Definition: luc.h:245
Discrete Log (DL) signature scheme.
Definition: pubkey.h:2293
unsigned int ByteCount() const
Determines the number of bytes required to represent the Integer.
Definition: integer.cpp:3306
Classes and functions for secure memory allocations.
Applies the inverse of the trapdoor function.
Definition: pubkey.h:183
LUC signature scheme with appendix.
Definition: luc.h:151
Classes for PKCS padding schemes.
A method was called which was not implemented.
Definition: cryptlib.h:222
Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const
Calculates the inverse of an element.
Definition: luc.cpp:172
LUC encryption scheme.
Definition: luc.h:140
Interface for Elgamal-like signature algorithms.
Definition: pubkey.h:1381
Classes for Diffie-Hellman key exchange.
Multiple precision integer with arithmetic operations.
Definition: integer.h:49
Integer-based GroupParameters default implementation.
Definition: gfpcrypt.h:121
static const Integer & Two()
Integer representing 2.
Definition: integer.cpp:4818
Applies the trapdoor function.
Definition: pubkey.h:128
Classes and functions for schemes based on Discrete Logs (DL) over GF(p)
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
Definition: luc.cpp:180
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Definition: trap.h:60
Diffie-Hellman domain.
Definition: dh.h:26
Abstract group.
Definition: algebra.h:26
The LUC inverse function.
Definition: luc.h:75
Discrete Log (DL) public key in GF(p) groups.
Definition: gfpcrypt.h:479
Integer PreimageBound() const
Returns the maximum size of a message before the trapdoor function is applied.
Definition: luc.h:53
Integer ImageBound() const
Returns the maximum size of a message after the trapdoor function is applied.
Definition: luc.h:54
LUC-HMP, based on "Digital signature schemes based on Lucas functions" by Patrick Horster...
Definition: luc.h:284
Discrete Log (DL) private key in GF(p) groups.
Definition: gfpcrypt.h:516
void AssignFrom(const NameValuePairs &source)
Assign values to this object.
Definition: luc.cpp:222
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:2312
Crypto++ library namespace.
size_t RLen(const DL_GroupParameters< Integer > &params) const
Retrieve R length.
Definition: luc.h:266
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Definition: luc.h:234
virtual void Save(BufferedTransformation &bt) const
Saves a key to a BufferedTransformation.
Definition: cryptlib.h:2223
DH_Domain< DL_GroupParameters_LUC_DefaultSafePrime > LUC_DH
LUC-DH.
Definition: luc.h:319
bool IsIdentity(const Integer &element) const
Determines if an element is an identity.
Definition: luc.h:220
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:294
Template implementing constructors for public key algorithm classes.
Definition: pubkey.h:2150
Trapdoor Function (TF) Signature Scheme.
Definition: pubkey.h:2266