Crypto++ 8.9 Free C++ class library of cryptographic schemes

rabin.cpp
1 // rabin.cpp - originally written and placed in the public domain by Wei Dai
2
3 #include "pch.h"
4 #include "rabin.h"
5 #include "integer.h"
6 #include "nbtheory.h"
7 #include "modarith.h"
8 #include "asn.h"
9 #include "sha.h"
10 #include "trap.h"
11
12 NAMESPACE_BEGIN(CryptoPP)
13
14 void RabinFunction::BERDecode(BufferedTransformation &bt)
15 {
16 BERSequenceDecoder seq(bt);
17 m_n.BERDecode(seq);
18 m_r.BERDecode(seq);
19 m_s.BERDecode(seq);
20 seq.MessageEnd();
21 }
22
23 void RabinFunction::DEREncode(BufferedTransformation &bt) const
24 {
25 DERSequenceEncoder seq(bt);
26 m_n.DEREncode(seq);
27 m_r.DEREncode(seq);
28 m_s.DEREncode(seq);
29 seq.MessageEnd();
30 }
31
32 Integer RabinFunction::ApplyFunction(const Integer &in) const
33 {
34 DoQuickSanityCheck();
35
36 Integer out = in.Squared()%m_n;
37 if (in.IsOdd())
38 out = out*m_r%m_n;
39 if (Jacobi(in, m_n)==-1)
40 out = out*m_s%m_n;
41 return out;
42 }
43
44 bool RabinFunction::Validate(RandomNumberGenerator& /*rng*/, unsigned int level) const
45 {
46 bool pass = true;
47 pass = pass && m_n > Integer::One() && m_n%4 == 1;
48 CRYPTOPP_ASSERT(pass);
49 pass = pass && m_r > Integer::One() && m_r < m_n;
50 CRYPTOPP_ASSERT(pass);
51 pass = pass && m_s > Integer::One() && m_s < m_n;
52 CRYPTOPP_ASSERT(pass);
53 if (level >= 1)
54 {
55 pass = pass && Jacobi(m_r, m_n) == -1 && Jacobi(m_s, m_n) == -1;
56 CRYPTOPP_ASSERT(pass);
57 }
58 return pass;
59 }
60
61 bool RabinFunction::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
62 {
63 return GetValueHelper(this, name, valueType, pValue).Assignable()
64 CRYPTOPP_GET_FUNCTION_ENTRY(Modulus)
65 CRYPTOPP_GET_FUNCTION_ENTRY(QuadraticResidueModPrime1)
66 CRYPTOPP_GET_FUNCTION_ENTRY(QuadraticResidueModPrime2)
67 ;
68 }
69
70 void RabinFunction::AssignFrom(const NameValuePairs &source)
71 {
72 AssignFromHelper(this, source)
73 CRYPTOPP_SET_FUNCTION_ENTRY(Modulus)
74 CRYPTOPP_SET_FUNCTION_ENTRY(QuadraticResidueModPrime1)
75 CRYPTOPP_SET_FUNCTION_ENTRY(QuadraticResidueModPrime2)
76 ;
77 }
78
79 // *****************************************************************************
80 // private key operations:
81
82 // generate a random private key
83 void InvertibleRabinFunction::GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg)
84 {
85 int modulusSize = 2048;
86 alg.GetIntValue("ModulusSize", modulusSize) || alg.GetIntValue("KeySize", modulusSize);
87
88 if (modulusSize < 16)
89 throw InvalidArgument("InvertibleRabinFunction: specified modulus size is too small");
90
91 // VC70 workaround: putting these after primeParam causes overlapped stack allocation
92 bool rFound=false, sFound=false;
93 Integer t=2;
94
95 AlgorithmParameters primeParam = MakeParametersForTwoPrimesOfEqualSize(modulusSize)
96 ("EquivalentTo", 3)("Mod", 4);
97 m_p.GenerateRandom(rng, primeParam);
98 m_q.GenerateRandom(rng, primeParam);
99
100 while (!(rFound && sFound))
101 {
102 int jp = Jacobi(t, m_p);
103 int jq = Jacobi(t, m_q);
104
105 if (!rFound && jp==1 && jq==-1)
106 {
107 m_r = t;
108 rFound = true;
109 }
110
111 if (!sFound && jp==-1 && jq==1)
112 {
113 m_s = t;
114 sFound = true;
115 }
116
117 ++t;
118 }
119
120 m_n = m_p * m_q;
121 m_u = m_q.InverseMod(m_p);
122 }
123
124 void InvertibleRabinFunction::BERDecode(BufferedTransformation &bt)
125 {
126 BERSequenceDecoder seq(bt);
127 m_n.BERDecode(seq);
128 m_r.BERDecode(seq);
129 m_s.BERDecode(seq);
130 m_p.BERDecode(seq);
131 m_q.BERDecode(seq);
132 m_u.BERDecode(seq);
133 seq.MessageEnd();
134
135 CRYPTOPP_ASSERT(IsPrime(m_p));
136 CRYPTOPP_ASSERT(IsPrime(m_q));
137 }
138
139 void InvertibleRabinFunction::DEREncode(BufferedTransformation &bt) const
140 {
141 DERSequenceEncoder seq(bt);
142 m_n.DEREncode(seq);
143 m_r.DEREncode(seq);
144 m_s.DEREncode(seq);
145 m_p.DEREncode(seq);
146 m_q.DEREncode(seq);
147 m_u.DEREncode(seq);
148 seq.MessageEnd();
149 }
150
151 Integer InvertibleRabinFunction::CalculateInverse(RandomNumberGenerator &rng, const Integer &in) const
152 {
153 CRYPTOPP_ASSERT(IsPrime(m_p));
154 CRYPTOPP_ASSERT(IsPrime(m_q));
155
156 DoQuickSanityCheck();
157
158 ModularArithmetic modn(m_n);
159 Integer r(rng, Integer::One(), m_n - Integer::One());
160 r = modn.Square(r);
161 Integer r2 = modn.Square(r);
162 Integer c = modn.Multiply(in, r2); // blind
163
164 Integer cp=c%m_p, cq=c%m_q;
165
166 int jp = Jacobi(cp, m_p);
167 int jq = Jacobi(cq, m_q);
168
169 if (jq==-1)
170 {
171 cp = cp*EuclideanMultiplicativeInverse(m_r, m_p)%m_p;
172 cq = cq*EuclideanMultiplicativeInverse(m_r, m_q)%m_q;
173 }
174
175 if (jp==-1)
176 {
177 cp = cp*EuclideanMultiplicativeInverse(m_s, m_p)%m_p;
178 cq = cq*EuclideanMultiplicativeInverse(m_s, m_q)%m_q;
179 }
180
181 cp = ModularSquareRoot(cp, m_p);
182 cq = ModularSquareRoot(cq, m_q);
183
184 if (jp==-1)
185 cp = m_p-cp;
186
187 Integer out = CRT(cq, m_q, cp, m_p, m_u);
188
189 out = modn.Divide(out, r); // unblind
190
191 if ((jq==-1 && out.IsEven()) || (jq==1 && out.IsOdd()))
192 out = m_n-out;
193
194 return out;
195 }
196
197 bool InvertibleRabinFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const
198 {
199 bool pass = RabinFunction::Validate(rng, level);
200 CRYPTOPP_ASSERT(pass);
201 pass = pass && m_p > Integer::One() && m_p%4 == 3 && m_p < m_n;
202 CRYPTOPP_ASSERT(pass);
203 pass = pass && m_q > Integer::One() && m_q%4 == 3 && m_q < m_n;
204 CRYPTOPP_ASSERT(pass);
205 pass = pass && m_u.IsPositive() && m_u < m_p;
206 CRYPTOPP_ASSERT(pass);
207 if (level >= 1)
208 {
209 pass = pass && m_p * m_q == m_n;
210 CRYPTOPP_ASSERT(pass);
211 pass = pass && m_u * m_q % m_p == 1;
212 CRYPTOPP_ASSERT(pass);
213 pass = pass && Jacobi(m_r, m_p) == 1;
214 CRYPTOPP_ASSERT(pass);
215 pass = pass && Jacobi(m_r, m_q) == -1;
216 CRYPTOPP_ASSERT(pass);
217 pass = pass && Jacobi(m_s, m_p) == -1;
218 CRYPTOPP_ASSERT(pass);
219 pass = pass && Jacobi(m_s, m_q) == 1;
220 CRYPTOPP_ASSERT(pass);
221 }
222 if (level >= 2)
223 {
224 pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2);
225 CRYPTOPP_ASSERT(pass);
226 }
227 return pass;
228 }
229
230 bool InvertibleRabinFunction::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
231 {
232 return GetValueHelper<RabinFunction>(this, name, valueType, pValue).Assignable()
233 CRYPTOPP_GET_FUNCTION_ENTRY(Prime1)
234 CRYPTOPP_GET_FUNCTION_ENTRY(Prime2)
235 CRYPTOPP_GET_FUNCTION_ENTRY(MultiplicativeInverseOfPrime2ModPrime1)
236 ;
237 }
238
239 void InvertibleRabinFunction::AssignFrom(const NameValuePairs &source)
240 {
241 AssignFromHelper<RabinFunction>(this, source)
242 CRYPTOPP_SET_FUNCTION_ENTRY(Prime1)
243 CRYPTOPP_SET_FUNCTION_ENTRY(Prime2)
244 CRYPTOPP_SET_FUNCTION_ENTRY(MultiplicativeInverseOfPrime2ModPrime1)
245 ;
246 }
247
248 NAMESPACE_END
asn.h
Classes and functions for working with ANS.1 objects.
AlgorithmParameters
An object that implements NameValuePairs.
Definition: algparam.h:426
BERSequenceDecoder
BER Sequence Decoder.
Definition: asn.h:526
BufferedTransformation
Interface for buffered transformations.
Definition: cryptlib.h:1657
CryptoMaterial::DoQuickSanityCheck
void DoQuickSanityCheck() const
Perform a quick sanity check.
Definition: cryptlib.h:2498
DERSequenceEncoder
DER Sequence Encoder.
Definition: asn.h:558
Integer
Multiple precision integer with arithmetic operations.
Definition: integer.h:50
Integer::DEREncode
void DEREncode(BufferedTransformation &bt) const
Encode in DER format.
Integer::GenerateRandom
void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &params=g_nullNameValuePairs)
Generate a random number.
Definition: integer.h:509
Integer::IsPositive
bool IsPositive() const
Determines if the Integer is positive.
Definition: integer.h:347
Integer::Squared
Integer Squared() const
Multiply this integer by itself.
Definition: integer.h:634
Integer::BERDecode
void BERDecode(const byte *input, size_t inputLen)
Decode from BER format.
Integer::One
static const Integer & One()
Integer representing 1.
Integer::IsOdd
bool IsOdd() const
Determines if the Integer is odd parity.
Definition: integer.h:356
Integer::InverseMod
Integer InverseMod(const Integer &n) const
Calculate multiplicative inverse.
Integer::IsEven
bool IsEven() const
Determines if the Integer is even parity.
Definition: integer.h:353
InvalidArgument
An invalid argument was detected.
Definition: cryptlib.h:208
InvertibleRabinFunction::CalculateInverse
Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const
Calculates the inverse of an element.
Definition: rabin.cpp:151
InvertibleRabinFunction::GenerateRandom
void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg)
Definition: rabin.cpp:83
InvertibleRabinFunction::AssignFrom
void AssignFrom(const NameValuePairs &source)
Assign values to this object.
Definition: rabin.cpp:239
InvertibleRabinFunction::Validate
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
Definition: rabin.cpp:197
InvertibleRabinFunction::GetVoidValue
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Definition: rabin.cpp:230
ModularArithmetic
Ring of congruence classes modulo n.
Definition: modarith.h:44
ModularArithmetic::Multiply
const Integer & Multiply(const Integer &a, const Integer &b) const
Multiplies elements in the ring.
Definition: modarith.h:190
ModularArithmetic::Divide
const Integer & Divide(const Integer &a, const Integer &b) const
Divides elements in the ring.
Definition: modarith.h:218
ModularArithmetic::Square
const Integer & Square(const Integer &a) const
Square an element in the ring.
Definition: modarith.h:197
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:327
NameValuePairs::GetIntValue
CRYPTOPP_DLL bool GetIntValue(const char *name, int &value) const
Get a named value with type int.
Definition: cryptlib.h:420
RabinFunction::GetVoidValue
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Definition: rabin.cpp:61
RabinFunction::Validate
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
Definition: rabin.cpp:44
RabinFunction::AssignFrom
void AssignFrom(const NameValuePairs &source)
Assign values to this object.
Definition: rabin.cpp:70
RabinFunction::ApplyFunction
Integer ApplyFunction(const Integer &x) const
Applies the trapdoor.
Definition: rabin.cpp:32
RandomNumberGenerator
Interface for random number generators.
Definition: cryptlib.h:1440
integer.h
Multiple precision integer with arithmetic operations.
modarith.h
Class file for performing modular arithmetic.
CryptoPP
Crypto++ library namespace.
Name::Prime1
const char * Prime1()
Integer.
Definition: argnames.h:43
Name::Modulus
const char * Modulus()
Integer.
Definition: argnames.h:33
Name::QuadraticResidueModPrime1
const char * QuadraticResidueModPrime1()
Integer.
Definition: argnames.h:48
Name::MultiplicativeInverseOfPrime2ModPrime1
const char * MultiplicativeInverseOfPrime2ModPrime1()
Integer.
Definition: argnames.h:47
Name::QuadraticResidueModPrime2
const char * QuadraticResidueModPrime2()
Integer.
Definition: argnames.h:49
Name::Prime2
const char * Prime2()
Integer.
Definition: argnames.h:44
nbtheory.h
Classes and functions for number theoretic operations.
Jacobi
CRYPTOPP_DLL int Jacobi(const Integer &a, const Integer &b)
Calculate the Jacobi symbol.
IsPrime
CRYPTOPP_DLL bool IsPrime(const Integer &p)
Verifies a number is probably prime.
ModularSquareRoot
CRYPTOPP_DLL Integer ModularSquareRoot(const Integer &a, const Integer &p)
Extract a modular square root.
VerifyPrime
CRYPTOPP_DLL bool VerifyPrime(RandomNumberGenerator &rng, const Integer &p, unsigned int level=1)
Verifies a number is probably prime.
EuclideanMultiplicativeInverse
Integer EuclideanMultiplicativeInverse(const Integer &a, const Integer &b)
Calculate multiplicative inverse.
Definition: nbtheory.h:169
CRT
CRYPTOPP_DLL Integer CRT(const Integer &xp, const Integer &p, const Integer &xq, const Integer &q, const Integer &u)
Chinese Remainder Theorem.
pch.h
Precompiled header file.
rabin.h
Classes for Rabin encryption and signature schemes.
sha.h
Classes for SHA-1 and SHA-2 family of message digests.
trap.h
Debugging and diagnostic assertions.
CRYPTOPP_ASSERT
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Definition: trap.h:68