Crypto++  5.6.5
Free C++ class library of cryptographic schemes
luc.cpp
1 // luc.cpp - originally written and placed in the public domain by Wei Dai
2 
3 #include "pch.h"
4 #include "luc.h"
5 #include "asn.h"
6 #include "sha.h"
7 #include "integer.h"
8 #include "nbtheory.h"
9 #include "algparam.h"
10 
11 NAMESPACE_BEGIN(CryptoPP)
12 
13 #if defined(CRYPTOPP_DEBUG) && !defined(CRYPTOPP_DOXYGEN_PROCESSING)
14 void LUC_TestInstantiations()
15 {
17  LUCFunction t2;
19 }
20 #endif
21 
22 void DL_Algorithm_LUC_HMP::Sign(const DL_GroupParameters<Integer> &params, const Integer &x, const Integer &k, const Integer &e, Integer &r, Integer &s) const
23 {
24  const Integer &q = params.GetSubgroupOrder();
25  r = params.ExponentiateBase(k);
26  s = (k + x*(r+e)) % q;
27 }
28 
29 bool DL_Algorithm_LUC_HMP::Verify(const DL_GroupParameters<Integer> &params, const DL_PublicKey<Integer> &publicKey, const Integer &e, const Integer &r, const Integer &s) const
30 {
31  const Integer p = params.GetGroupOrder()-1;
32  const Integer &q = params.GetSubgroupOrder();
33 
34  Integer Vsg = params.ExponentiateBase(s);
35  Integer Vry = publicKey.ExponentiatePublicElement((r+e)%q);
36  return (Vsg*Vsg + Vry*Vry + r*r) % p == (Vsg * Vry * r + 4) % p;
37 }
38 
39 Integer DL_BasePrecomputation_LUC::Exponentiate(const DL_GroupPrecomputation<Element> &group, const Integer &exponent) const
40 {
41  return Lucas(exponent, m_g, static_cast<const DL_GroupPrecomputation_LUC &>(group).GetModulus());
42 }
43 
44 void DL_GroupParameters_LUC::SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const
45 {
46  for (unsigned int i=0; i<exponentsCount; i++)
47  results[i] = Lucas(exponents[i], base, GetModulus());
48 }
49 
50 void LUCFunction::BERDecode(BufferedTransformation &bt)
51 {
52  BERSequenceDecoder seq(bt);
53  m_n.BERDecode(seq);
54  m_e.BERDecode(seq);
55  seq.MessageEnd();
56 }
57 
58 void LUCFunction::DEREncode(BufferedTransformation &bt) const
59 {
60  DERSequenceEncoder seq(bt);
61  m_n.DEREncode(seq);
62  m_e.DEREncode(seq);
63  seq.MessageEnd();
64 }
65 
67 {
68  DoQuickSanityCheck();
69  return Lucas(m_e, x, m_n);
70 }
71 
72 bool LUCFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const
73 {
74  CRYPTOPP_UNUSED(rng), CRYPTOPP_UNUSED(level);
75  bool pass = true;
76  pass = pass && m_n > Integer::One() && m_n.IsOdd();
77  CRYPTOPP_ASSERT(pass);
78  pass = pass && m_e > Integer::One() && m_e.IsOdd() && m_e < m_n;
79  CRYPTOPP_ASSERT(pass);
80  return pass;
81 }
82 
83 bool LUCFunction::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
84 {
85  return GetValueHelper(this, name, valueType, pValue).Assignable()
86  CRYPTOPP_GET_FUNCTION_ENTRY(Modulus)
87  CRYPTOPP_GET_FUNCTION_ENTRY(PublicExponent)
88  ;
89 }
90 
92 {
93  AssignFromHelper(this, source)
94  CRYPTOPP_SET_FUNCTION_ENTRY(Modulus)
95  CRYPTOPP_SET_FUNCTION_ENTRY(PublicExponent)
96  ;
97 }
98 
99 // *****************************************************************************
100 // private key operations:
101 
103 {
104 public:
105  LUCPrimeSelector(const Integer &e) : m_e(e) {}
106  bool IsAcceptable(const Integer &candidate) const
107  {
108  return RelativelyPrime(m_e, candidate+1) && RelativelyPrime(m_e, candidate-1);
109  }
110  Integer m_e;
111 };
112 
114 {
115  int modulusSize = 2048;
116  alg.GetIntValue("ModulusSize", modulusSize) || alg.GetIntValue("KeySize", modulusSize);
117 
118  if (modulusSize < 16)
119  throw InvalidArgument("InvertibleLUCFunction: specified modulus size is too small");
120 
121  m_e = alg.GetValueWithDefault("PublicExponent", Integer(17));
122 
123  if (m_e < 5 || m_e.IsEven())
124  throw InvalidArgument("InvertibleLUCFunction: invalid public exponent");
125 
126  LUCPrimeSelector selector(m_e);
127  AlgorithmParameters primeParam = MakeParametersForTwoPrimesOfEqualSize(modulusSize)
128  ("PointerToPrimeSelector", selector.GetSelectorPointer());
129  m_p.GenerateRandom(rng, primeParam);
130  m_q.GenerateRandom(rng, primeParam);
131 
132  m_n = m_p * m_q;
133  m_u = m_q.InverseMod(m_p);
134 }
135 
136 void InvertibleLUCFunction::Initialize(RandomNumberGenerator &rng, unsigned int keybits, const Integer &e)
137 {
138  GenerateRandom(rng, MakeParameters("ModulusSize", (int)keybits)("PublicExponent", e));
139 }
140 
141 void InvertibleLUCFunction::BERDecode(BufferedTransformation &bt)
142 {
143  BERSequenceDecoder seq(bt);
144 
145  Integer version(seq);
146  if (!!version) // make sure version is 0
147  BERDecodeError();
148 
149  m_n.BERDecode(seq);
150  m_e.BERDecode(seq);
151  m_p.BERDecode(seq);
152  m_q.BERDecode(seq);
153  m_u.BERDecode(seq);
154  seq.MessageEnd();
155 }
156 
157 void InvertibleLUCFunction::DEREncode(BufferedTransformation &bt) const
158 {
159  DERSequenceEncoder seq(bt);
160 
161  const byte version[] = {INTEGER, 1, 0};
162  seq.Put(version, sizeof(version));
163  m_n.DEREncode(seq);
164  m_e.DEREncode(seq);
165  m_p.DEREncode(seq);
166  m_q.DEREncode(seq);
167  m_u.DEREncode(seq);
168  seq.MessageEnd();
169 }
170 
172 {
173  // not clear how to do blinding with LUC
174  CRYPTOPP_UNUSED(rng);
175  DoQuickSanityCheck();
176  return InverseLucas(m_e, x, m_q, m_p, m_u);
177 }
178 
179 bool InvertibleLUCFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const
180 {
181  bool pass = LUCFunction::Validate(rng, level);
182  CRYPTOPP_ASSERT(pass);
183  pass = pass && m_p > Integer::One() && m_p.IsOdd() && m_p < m_n;
184  CRYPTOPP_ASSERT(pass);
185  pass = pass && m_q > Integer::One() && m_q.IsOdd() && m_q < m_n;
186  CRYPTOPP_ASSERT(pass);
187  pass = pass && m_u.IsPositive() && m_u < m_p;
188  CRYPTOPP_ASSERT(pass);
189  if (level >= 1)
190  {
191  pass = pass && m_p * m_q == m_n;
192  CRYPTOPP_ASSERT(pass);
193  pass = pass && RelativelyPrime(m_e, m_p+1);
194  CRYPTOPP_ASSERT(pass);
195  pass = pass && RelativelyPrime(m_e, m_p-1);
196  CRYPTOPP_ASSERT(pass);
197  pass = pass && RelativelyPrime(m_e, m_q+1);
198  CRYPTOPP_ASSERT(pass);
199  pass = pass && RelativelyPrime(m_e, m_q-1);
200  CRYPTOPP_ASSERT(pass);
201  pass = pass && m_u * m_q % m_p == 1;
202  CRYPTOPP_ASSERT(pass);
203  }
204  if (level >= 2)
205  {
206  pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2);
207  CRYPTOPP_ASSERT(pass);
208  }
209  return pass;
210 }
211 
212 bool InvertibleLUCFunction::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
213 {
214  return GetValueHelper<LUCFunction>(this, name, valueType, pValue).Assignable()
215  CRYPTOPP_GET_FUNCTION_ENTRY(Prime1)
216  CRYPTOPP_GET_FUNCTION_ENTRY(Prime2)
217  CRYPTOPP_GET_FUNCTION_ENTRY(MultiplicativeInverseOfPrime2ModPrime1)
218  ;
219 }
220 
222 {
223  AssignFromHelper<LUCFunction>(this, source)
224  CRYPTOPP_SET_FUNCTION_ENTRY(Prime1)
225  CRYPTOPP_SET_FUNCTION_ENTRY(Prime2)
226  CRYPTOPP_SET_FUNCTION_ENTRY(MultiplicativeInverseOfPrime2ModPrime1)
227  ;
228 }
229 
230 NAMESPACE_END
const char * MultiplicativeInverseOfPrime2ModPrime1()
Integer.
Definition: argnames.h:47
void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits, const Integer &eStart=17)
Create a LUC private key.
Definition: luc.cpp:136
An invalid argument was detected.
Definition: cryptlib.h:194
void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg)
Definition: luc.cpp:113
Classes for working with NameValuePairs.
const char * Prime2()
Integer.
Definition: argnames.h:44
T GetValueWithDefault(const char *name, T defaultValue) const
Get a named value.
Definition: cryptlib.h:356
void AssignFrom(const NameValuePairs &source)
Assign values to this object.
Definition: luc.cpp:91
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Definition: luc.cpp:212
virtual Integer GetGroupOrder() const
Retrieves the order of the group.
Definition: pubkey.h:843
Interface for random number generators.
Definition: cryptlib.h:1188
BER Sequence Decoder.
Definition: asn.h:303
Interface for buffered transformations.
Definition: cryptlib.h:1343
virtual Element ExponentiateBase(const Integer &exponent) const
Retrieves the subgroup generator.
Definition: pubkey.h:803
static const Integer & One()
Integer representing 1.
Definition: integer.cpp:3096
Interface for Discrete Log (DL) public keys.
Definition: pubkey.h:992
Classes for the LUC cryptosystem.
const char * Prime1()
Integer.
Definition: argnames.h:43
size_t Put(byte inByte, bool blocking=true)
Input a byte for processing.
Definition: cryptlib.h:1364
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:502
const char * PublicExponent()
Integer.
Definition: argnames.h:34
Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const
Calculates the inverse of an element.
Definition: luc.cpp:171
bool VerifyPrime(RandomNumberGenerator &rng, const Integer &p, unsigned int level=1)
Verifies a prime number.
Definition: nbtheory.cpp:249
Integer ApplyFunction(const Integer &x) const
Applies the trapdoor.
Definition: luc.cpp:66
Application callback to signal suitability of a cabdidate prime.
Definition: nbtheory.h:80
Multiple precision integer with arithmetic operations.
Definition: integer.h:49
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
Definition: luc.cpp:179
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
Definition: luc.cpp:72
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Definition: trap.h:60
void BERDecodeError()
Raises a BERDecodeErr.
Definition: asn.h:69
Classes and functions for working with ANS.1 objects.
The LUC inverse function.
Definition: luc.h:75
Classes for SHA-1 and SHA-2 family of message digests.
Classes and functions for number theoretic operations.
DER Sequence Encoder.
Definition: asn.h:313
void AssignFrom(const NameValuePairs &source)
Assign values to this object.
Definition: luc.cpp:221
An object that implements NameValuePairs.
Definition: algparam.h:437
const char * Modulus()
Integer.
Definition: argnames.h:33
Multiple precision integer with arithmetic operations.
The LUC function.
Definition: luc.h:36
Crypto++ library namespace.
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Definition: luc.cpp:83
bool GetIntValue(const char *name, int &value) const
Get a named value with type int.
Definition: cryptlib.h:379
bool IsOdd() const
Determines if the Integer is odd parity.
Definition: integer.h:351
Interface for retrieving values given their names.
Definition: cryptlib.h:285
Template implementing constructors for public key algorithm classes.
Definition: pubkey.h:1989
virtual const Integer & GetSubgroupOrder() const =0
Retrieves the subgroup order.