Crypto++  5.6.3
Free C++ class library of cryptographic schemes
luc.cpp
1 // luc.cpp - 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(NDEBUG) && !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 {
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  pass = pass && m_e > Integer::One() && m_e.IsOdd() && m_e < m_n;
78  return pass;
79 }
80 
81 bool LUCFunction::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
82 {
83  return GetValueHelper(this, name, valueType, pValue).Assignable()
84  CRYPTOPP_GET_FUNCTION_ENTRY(Modulus)
85  CRYPTOPP_GET_FUNCTION_ENTRY(PublicExponent)
86  ;
87 }
88 
90 {
91  AssignFromHelper(this, source)
92  CRYPTOPP_SET_FUNCTION_ENTRY(Modulus)
93  CRYPTOPP_SET_FUNCTION_ENTRY(PublicExponent)
94  ;
95 }
96 
97 // *****************************************************************************
98 // private key operations:
99 
101 {
102 public:
103  LUCPrimeSelector(const Integer &e) : m_e(e) {}
104  bool IsAcceptable(const Integer &candidate) const
105  {
106  return RelativelyPrime(m_e, candidate+1) && RelativelyPrime(m_e, candidate-1);
107  }
108  Integer m_e;
109 };
110 
112 {
113  int modulusSize = 2048;
114  alg.GetIntValue("ModulusSize", modulusSize) || alg.GetIntValue("KeySize", modulusSize);
115 
116  if (modulusSize < 16)
117  throw InvalidArgument("InvertibleLUCFunction: specified modulus size is too small");
118 
119  m_e = alg.GetValueWithDefault("PublicExponent", Integer(17));
120 
121  if (m_e < 5 || m_e.IsEven())
122  throw InvalidArgument("InvertibleLUCFunction: invalid public exponent");
123 
124  LUCPrimeSelector selector(m_e);
125  AlgorithmParameters primeParam = MakeParametersForTwoPrimesOfEqualSize(modulusSize)
126  ("PointerToPrimeSelector", selector.GetSelectorPointer());
127  m_p.GenerateRandom(rng, primeParam);
128  m_q.GenerateRandom(rng, primeParam);
129 
130  m_n = m_p * m_q;
131  m_u = m_q.InverseMod(m_p);
132 }
133 
134 void InvertibleLUCFunction::Initialize(RandomNumberGenerator &rng, unsigned int keybits, const Integer &e)
135 {
136  GenerateRandom(rng, MakeParameters("ModulusSize", (int)keybits)("PublicExponent", e));
137 }
138 
139 void InvertibleLUCFunction::BERDecode(BufferedTransformation &bt)
140 {
141  BERSequenceDecoder seq(bt);
142 
143  Integer version(seq);
144  if (!!version) // make sure version is 0
145  BERDecodeError();
146 
147  m_n.BERDecode(seq);
148  m_e.BERDecode(seq);
149  m_p.BERDecode(seq);
150  m_q.BERDecode(seq);
151  m_u.BERDecode(seq);
152  seq.MessageEnd();
153 }
154 
155 void InvertibleLUCFunction::DEREncode(BufferedTransformation &bt) const
156 {
157  DERSequenceEncoder seq(bt);
158 
159  const byte version[] = {INTEGER, 1, 0};
160  seq.Put(version, sizeof(version));
161  m_n.DEREncode(seq);
162  m_e.DEREncode(seq);
163  m_p.DEREncode(seq);
164  m_q.DEREncode(seq);
165  m_u.DEREncode(seq);
166  seq.MessageEnd();
167 }
168 
170 {
171  // not clear how to do blinding with LUC
172  CRYPTOPP_UNUSED(rng);
174  return InverseLucas(m_e, x, m_q, m_p, m_u);
175 }
176 
177 bool InvertibleLUCFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const
178 {
179  bool pass = LUCFunction::Validate(rng, level);
180  pass = pass && m_p > Integer::One() && m_p.IsOdd() && m_p < m_n;
181  pass = pass && m_q > Integer::One() && m_q.IsOdd() && m_q < m_n;
182  pass = pass && m_u.IsPositive() && m_u < m_p;
183  if (level >= 1)
184  {
185  pass = pass && m_p * m_q == m_n;
186  pass = pass && RelativelyPrime(m_e, m_p+1);
187  pass = pass && RelativelyPrime(m_e, m_p-1);
188  pass = pass && RelativelyPrime(m_e, m_q+1);
189  pass = pass && RelativelyPrime(m_e, m_q-1);
190  pass = pass && m_u * m_q % m_p == 1;
191  }
192  if (level >= 2)
193  pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2);
194  return pass;
195 }
196 
197 bool InvertibleLUCFunction::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
198 {
199  return GetValueHelper<LUCFunction>(this, name, valueType, pValue).Assignable()
200  CRYPTOPP_GET_FUNCTION_ENTRY(Prime1)
201  CRYPTOPP_GET_FUNCTION_ENTRY(Prime2)
202  CRYPTOPP_GET_FUNCTION_ENTRY(MultiplicativeInverseOfPrime2ModPrime1)
203  ;
204 }
205 
207 {
208  AssignFromHelper<LUCFunction>(this, source)
209  CRYPTOPP_SET_FUNCTION_ENTRY(Prime1)
210  CRYPTOPP_SET_FUNCTION_ENTRY(Prime2)
211  CRYPTOPP_SET_FUNCTION_ENTRY(MultiplicativeInverseOfPrime2ModPrime1)
212  ;
213 }
214 
215 NAMESPACE_END
const char * MultiplicativeInverseOfPrime2ModPrime1()
Integer.
Definition: argnames.h:46
An invalid argument was detected.
Definition: cryptlib.h:182
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Definition: luc.cpp:81
void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg)
Definition: luc.cpp:111
Classes for working with NameValuePairs.
const char * Prime2()
Integer.
Definition: argnames.h:43
bool IsOdd() const
Determines if the Integer is odd parity.
Definition: integer.h:333
T GetValueWithDefault(const char *name, T defaultValue) const
Get a named value.
Definition: cryptlib.h:348
void AssignFrom(const NameValuePairs &source)
Assign values to this object.
Definition: luc.cpp:89
Interface for random number generators.
Definition: cryptlib.h:1186
BER Sequence Decoder.
Definition: asn.h:294
Interface for buffered transformations.
Definition: cryptlib.h:1352
Integer ApplyFunction(const Integer &x) const
Applies the trapdoor.
Definition: luc.cpp:66
static const Integer & One()
Integer representing 1.
Definition: integer.cpp:2948
bool GetIntValue(const char *name, int &value) const
Get a named value with type int.
Definition: cryptlib.h:371
Interface for Discrete Log (DL) public keys.
Definition: pubkey.h:1014
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Definition: luc.cpp:197
const char * Prime1()
Integer.
Definition: argnames.h:42
Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const
Calculates the inverse of an element.
Definition: luc.cpp:169
bool IsPositive() const
Determines if the Integer is positive.
Definition: integer.h:324
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:554
const char * PublicExponent()
Integer.
Definition: argnames.h:33
bool VerifyPrime(RandomNumberGenerator &rng, const Integer &p, unsigned int level=1)
Verifies a prime number.
Definition: nbtheory.cpp:249
Application callback to signal suitability of a cabdidate prime.
Definition: nbtheory.h:80
Multiple precision integer with arithmetic operations.
Definition: integer.h:31
virtual const Integer & GetSubgroupOrder() const =0
Retrieves the subgroup order.
void BERDecodeError()
Raises a BERDecodeErr.
Definition: asn.h:61
Classes and functions for working with ANS.1 objects.
Classes for SHA-1 and SHA-2 family of message digests.
Classes and functions for number theoretic operations.
void DEREncode(BufferedTransformation &bt) const
Encode in DER format.
Definition: integer.cpp:3292
DER Sequence Encoder.
Definition: asn.h:304
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
An object that implements NameValuePairs.
Definition: algparam.h:489
const char * Modulus()
Integer.
Definition: argnames.h:32
Integer InverseMod(const Integer &n) const
calculate multiplicative inverse of *this mod n
Definition: integer.cpp:4127
The LUC function.
Definition: luc.h:33
void BERDecode(const byte *input, size_t inputLen)
Decode from BER format.
Definition: integer.cpp:3299
Crypto++ library namespace.
virtual Element ExponentiateBase(const Integer &exponent) const
Retrieves the subgroup generator.
Definition: pubkey.h:821
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
Definition: luc.cpp:72
virtual Integer GetGroupOrder() const
Retrieves the order of the group.
Definition: pubkey.h:861
Interface for retrieving values given their names.
Definition: cryptlib.h:277
void DoQuickSanityCheck() const
Perform a quick sanity check.
Definition: cryptlib.h:2141
Template implementing constructors for public key algorithm classes.
Definition: pubkey.h:2030
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
Definition: luc.cpp:177