Crypto++  5.6.3
Free C++ class library of cryptographic schemes
rsa.cpp
1 // rsa.cpp - written and placed in the public domain by Wei Dai
2 
3 #include "pch.h"
4 #include "rsa.h"
5 #include "asn.h"
6 #include "sha.h"
7 #include "oids.h"
8 #include "modarith.h"
9 #include "nbtheory.h"
10 #include "algparam.h"
11 #include "fips140.h"
12 
13 #if !defined(NDEBUG) && !defined(CRYPTOPP_DOXYGEN_PROCESSING) && !defined(CRYPTOPP_IS_DLL)
14 #include "pssr.h"
15 NAMESPACE_BEGIN(CryptoPP)
16 void RSA_TestInstantiations()
17 {
21  RSASS<PKCS1v15, SHA>::Verifier x4(x2.GetKey());
23 #ifndef __MWERKS__
25  x3 = x2;
26  x6 = x2;
27 #endif
29 #ifndef __GNUC__
31 #endif
32  RSAES<OAEP<SHA> >::Encryptor x9(x2);
33 
34  x4 = x2.GetKey();
35 }
36 NAMESPACE_END
37 #endif
38 
39 #ifndef CRYPTOPP_IMPORTS
40 
41 NAMESPACE_BEGIN(CryptoPP)
42 
43 OID RSAFunction::GetAlgorithmID() const
44 {
45  return ASN1::rsaEncryption();
46 }
47 
49 {
50  BERSequenceDecoder seq(bt);
51  m_n.BERDecode(seq);
52  m_e.BERDecode(seq);
53  seq.MessageEnd();
54 }
55 
57 {
58  DERSequenceEncoder seq(bt);
59  m_n.DEREncode(seq);
60  m_e.DEREncode(seq);
61  seq.MessageEnd();
62 }
63 
65 {
67  return a_exp_b_mod_c(x, m_e, m_n);
68 }
69 
70 bool RSAFunction::Validate(RandomNumberGenerator& rng, unsigned int level) const
71 {
72  CRYPTOPP_UNUSED(rng), CRYPTOPP_UNUSED(level);
73 
74  bool pass = true;
75  pass = pass && m_n > Integer::One() && m_n.IsOdd();
76  pass = pass && m_e > Integer::One() && m_e.IsOdd() && m_e < m_n;
77  return pass;
78 }
79 
80 bool RSAFunction::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
81 {
82  return GetValueHelper(this, name, valueType, pValue).Assignable()
83  CRYPTOPP_GET_FUNCTION_ENTRY(Modulus)
84  CRYPTOPP_GET_FUNCTION_ENTRY(PublicExponent)
85  ;
86 }
87 
89 {
90  AssignFromHelper(this, source)
91  CRYPTOPP_SET_FUNCTION_ENTRY(Modulus)
92  CRYPTOPP_SET_FUNCTION_ENTRY(PublicExponent)
93  ;
94 }
95 
96 // *****************************************************************************
97 
99 {
100 public:
101  RSAPrimeSelector(const Integer &e) : m_e(e) {}
102  bool IsAcceptable(const Integer &candidate) const {return RelativelyPrime(m_e, candidate-Integer::One());}
103  Integer m_e;
104 };
105 
107 {
108  int modulusSize = 2048;
109  alg.GetIntValue(Name::ModulusSize(), modulusSize) || alg.GetIntValue(Name::KeySize(), modulusSize);
110 
111  assert(modulusSize >= 16);
112  if (modulusSize < 16)
113  throw InvalidArgument("InvertibleRSAFunction: specified modulus size is too small");
114 
116 
117  assert(m_e >= 3); assert(!m_e.IsEven());
118  if (m_e < 3 || m_e.IsEven())
119  throw InvalidArgument("InvertibleRSAFunction: invalid public exponent");
120 
121  RSAPrimeSelector selector(m_e);
122  AlgorithmParameters primeParam = MakeParametersForTwoPrimesOfEqualSize(modulusSize)
123  (Name::PointerToPrimeSelector(), selector.GetSelectorPointer());
124  m_p.GenerateRandom(rng, primeParam);
125  m_q.GenerateRandom(rng, primeParam);
126 
127  m_d = m_e.InverseMod(LCM(m_p-1, m_q-1));
128  assert(m_d.IsPositive());
129 
130  m_dp = m_d % (m_p-1);
131  m_dq = m_d % (m_q-1);
132  m_n = m_p * m_q;
133  m_u = m_q.InverseMod(m_p);
134 
136  {
137  RSASS<PKCS1v15, SHA>::Signer signer(*this);
138  RSASS<PKCS1v15, SHA>::Verifier verifier(signer);
139  SignaturePairwiseConsistencyTest_FIPS_140_Only(signer, verifier);
140 
141  RSAES<OAEP<SHA> >::Decryptor decryptor(*this);
142  RSAES<OAEP<SHA> >::Encryptor encryptor(decryptor);
143  EncryptionPairwiseConsistencyTest_FIPS_140_Only(encryptor, decryptor);
144  }
145 }
146 
147 void InvertibleRSAFunction::Initialize(RandomNumberGenerator &rng, unsigned int keybits, const Integer &e)
148 {
150 }
151 
152 void InvertibleRSAFunction::Initialize(const Integer &n, const Integer &e, const Integer &d)
153 {
154  if (n.IsEven() || e.IsEven() | d.IsEven())
155  throw InvalidArgument("InvertibleRSAFunction: input is not a valid RSA private key");
156 
157  m_n = n;
158  m_e = e;
159  m_d = d;
160 
161  Integer r = --(d*e);
162  unsigned int s = 0;
163  while (r.IsEven())
164  {
165  r >>= 1;
166  s++;
167  }
168 
169  ModularArithmetic modn(n);
170  for (Integer i = 2; ; ++i)
171  {
172  Integer a = modn.Exponentiate(i, r);
173  if (a == 1)
174  continue;
175  Integer b;
176  unsigned int j = 0;
177  while (a != n-1)
178  {
179  b = modn.Square(a);
180  if (b == 1)
181  {
182  m_p = GCD(a-1, n);
183  m_q = n/m_p;
184  m_dp = m_d % (m_p-1);
185  m_dq = m_d % (m_q-1);
186  m_u = m_q.InverseMod(m_p);
187  return;
188  }
189  if (++j == s)
190  throw InvalidArgument("InvertibleRSAFunction: input is not a valid RSA private key");
191  a = b;
192  }
193  }
194 }
195 
197 {
198  BERSequenceDecoder privateKey(bt);
199  word32 version;
200  BERDecodeUnsigned<word32>(privateKey, version, INTEGER, 0, 0); // check version
201  m_n.BERDecode(privateKey);
202  m_e.BERDecode(privateKey);
203  m_d.BERDecode(privateKey);
204  m_p.BERDecode(privateKey);
205  m_q.BERDecode(privateKey);
206  m_dp.BERDecode(privateKey);
207  m_dq.BERDecode(privateKey);
208  m_u.BERDecode(privateKey);
209  privateKey.MessageEnd();
210 }
211 
213 {
214  DERSequenceEncoder privateKey(bt);
215  DEREncodeUnsigned<word32>(privateKey, 0); // version
216  m_n.DEREncode(privateKey);
217  m_e.DEREncode(privateKey);
218  m_d.DEREncode(privateKey);
219  m_p.DEREncode(privateKey);
220  m_q.DEREncode(privateKey);
221  m_dp.DEREncode(privateKey);
222  m_dq.DEREncode(privateKey);
223  m_u.DEREncode(privateKey);
224  privateKey.MessageEnd();
225 }
226 
228 {
230  ModularArithmetic modn(m_n);
231  Integer r, rInv;
232  do { // do this in a loop for people using small numbers for testing
233  r.Randomize(rng, Integer::One(), m_n - Integer::One());
234  rInv = modn.MultiplicativeInverse(r);
235  } while (rInv.IsZero());
236  Integer re = modn.Exponentiate(r, m_e);
237  re = modn.Multiply(re, x); // blind
238  // here we follow the notation of PKCS #1 and let u=q inverse mod p
239  // but in ModRoot, u=p inverse mod q, so we reverse the order of p and q
240  Integer y = ModularRoot(re, m_dq, m_dp, m_q, m_p, m_u);
241  y = modn.Multiply(y, rInv); // unblind
242  if (modn.Exponentiate(y, m_e) != x) // check
243  throw Exception(Exception::OTHER_ERROR, "InvertibleRSAFunction: computational error during private key operation");
244  return y;
245 }
246 
247 bool InvertibleRSAFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const
248 {
249  bool pass = RSAFunction::Validate(rng, level);
250  pass = pass && m_p > Integer::One() && m_p.IsOdd() && m_p < m_n;
251  pass = pass && m_q > Integer::One() && m_q.IsOdd() && m_q < m_n;
252  pass = pass && m_d > Integer::One() && m_d.IsOdd() && m_d < m_n;
253  pass = pass && m_dp > Integer::One() && m_dp.IsOdd() && m_dp < m_p;
254  pass = pass && m_dq > Integer::One() && m_dq.IsOdd() && m_dq < m_q;
255  pass = pass && m_u.IsPositive() && m_u < m_p;
256  if (level >= 1)
257  {
258  pass = pass && m_p * m_q == m_n;
259  pass = pass && m_e*m_d % LCM(m_p-1, m_q-1) == 1;
260  pass = pass && m_dp == m_d%(m_p-1) && m_dq == m_d%(m_q-1);
261  pass = pass && m_u * m_q % m_p == 1;
262  }
263  if (level >= 2)
264  pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2);
265  return pass;
266 }
267 
268 bool InvertibleRSAFunction::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
269 {
270  return GetValueHelper<RSAFunction>(this, name, valueType, pValue).Assignable()
271  CRYPTOPP_GET_FUNCTION_ENTRY(Prime1)
272  CRYPTOPP_GET_FUNCTION_ENTRY(Prime2)
273  CRYPTOPP_GET_FUNCTION_ENTRY(PrivateExponent)
274  CRYPTOPP_GET_FUNCTION_ENTRY(ModPrime1PrivateExponent)
275  CRYPTOPP_GET_FUNCTION_ENTRY(ModPrime2PrivateExponent)
276  CRYPTOPP_GET_FUNCTION_ENTRY(MultiplicativeInverseOfPrime2ModPrime1)
277  ;
278 }
279 
281 {
282  AssignFromHelper<RSAFunction>(this, source)
283  CRYPTOPP_SET_FUNCTION_ENTRY(Prime1)
284  CRYPTOPP_SET_FUNCTION_ENTRY(Prime2)
285  CRYPTOPP_SET_FUNCTION_ENTRY(PrivateExponent)
286  CRYPTOPP_SET_FUNCTION_ENTRY(ModPrime1PrivateExponent)
287  CRYPTOPP_SET_FUNCTION_ENTRY(ModPrime2PrivateExponent)
288  CRYPTOPP_SET_FUNCTION_ENTRY(MultiplicativeInverseOfPrime2ModPrime1)
289  ;
290 }
291 
292 // *****************************************************************************
293 
295 {
297  return t % 16 == 12 ? t : m_n - t;
298 }
299 
301 {
303  return STDMIN(t, m_n-t);
304 }
305 
306 NAMESPACE_END
307 
308 #endif
Base class for all exceptions thrown by the library.
Definition: cryptlib.h:139
const char * MultiplicativeInverseOfPrime2ModPrime1()
Integer.
Definition: argnames.h:46
An invalid argument was detected.
Definition: cryptlib.h:182
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 DEREncodePublicKey(BufferedTransformation &bt) const
encode subjectPublicKey part of subjectPublicKeyInfo, without the BIT STRING header ...
Definition: rsa.cpp:56
void BERDecodePrivateKey(BufferedTransformation &bt, bool parametersPresent, size_t size)
decode privateKey part of privateKeyInfo, without the OCTET STRING header
Definition: rsa.cpp:196
void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg)
Definition: rsa.cpp:106
bool IsEven() const
Determines if the Integer is even parity.
Definition: integer.h:330
const Integer & MultiplicativeInverse(const Integer &a) const
Calculate the multiplicative inverse of an element in the ring.
Definition: modarith.h:192
Some other error occurred not belonging to other categories.
Definition: cryptlib.h:158
ASN.1 object identifiers for algorthms and schemes.
const Integer & Square(const Integer &a) const
Square an element in the ring.
Definition: modarith.h:179
Ring of congruence classes modulo n.
Definition: modarith.h:34
Interface for random number generators.
Definition: cryptlib.h:1186
void Randomize(RandomNumberGenerator &rng, size_t bitCount)
Set this Integer to random integer.
Definition: integer.cpp:3427
BER Sequence Decoder.
Definition: asn.h:294
Interface for buffered transformations.
Definition: cryptlib.h:1352
static const Integer & One()
Integer representing 1.
Definition: integer.cpp:3016
bool GetIntValue(const char *name, int &value) const
Get a named value with type int.
Definition: cryptlib.h:371
void BERDecodePublicKey(BufferedTransformation &bt, bool parametersPresent, size_t size)
decode subjectPublicKey part of subjectPublicKeyInfo, without the BIT STRING header ...
Definition: rsa.cpp:48
const char * PrivateExponent()
Integer.
Definition: argnames.h:34
const char * Prime1()
Integer.
Definition: argnames.h:42
bool FIPS_140_2_ComplianceEnabled()
Determines whether the library provides FIPS validated cryptography.
Definition: fips140.cpp:29
const Integer & Multiply(const Integer &a, const Integer &b) const
Multiplies elements in the ring.
Definition: modarith.h:172
bool IsPositive() const
Determines if the Integer is positive.
Definition: integer.h:324
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
Definition: rsa.cpp:247
void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits, const Integer &e=17)
Create a RSA private key.
Definition: rsa.cpp:147
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
void AssignFrom(const NameValuePairs &source)
Assign values to this object.
Definition: rsa.cpp:88
const char * ModPrime1PrivateExponent()
Integer.
Definition: argnames.h:44
Integer ApplyFunction(const Integer &x) const
Applies the trapdoor.
Definition: rsa.cpp:294
Application callback to signal suitability of a cabdidate prime.
Definition: nbtheory.h:80
Multiple precision integer with arithmetic operations.
Definition: integer.h:31
const char * ModPrime2PrivateExponent()
Integer.
Definition: argnames.h:45
Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const
Calculates the inverse of an element.
Definition: rsa.cpp:300
void DEREncodePrivateKey(BufferedTransformation &bt) const
encode privateKey part of privateKeyInfo, without the OCTET STRING header
Definition: rsa.cpp:212
void AssignFrom(const NameValuePairs &source)
Assign values to this object.
Definition: rsa.cpp:280
RandomNumberGenerator & NullRNG()
Random Number Generator that does not produce random numbers.
Definition: cryptlib.cpp:405
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
Definition: misc.h:450
bool IsZero() const
Determines if the Integer is 0.
Definition: integer.h:312
Classes and functions for working with ANS.1 objects.
Classes for SHA-1 and SHA-2 family of message digests.
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Definition: rsa.cpp:80
const char * PointerToPrimeSelector()
const PrimeSelector *
Definition: argnames.h:41
Classes and functions for number theoretic operations.
const char * KeySize()
int, in bits
Definition: argnames.h:28
void DEREncode(BufferedTransformation &bt) const
Encode in DER format.
Definition: integer.cpp:3360
DER Sequence Encoder.
Definition: asn.h:304
Classes for the RSA cryptosystem.
Classes and functions for the FIPS 140-2 validated library.
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Definition: rsa.cpp:268
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:4195
RSA encryption algorithm.
Definition: rsa.h:165
void BERDecode(const byte *input, size_t inputLen)
Decode from BER format.
Definition: integer.cpp:3367
Class file for performing modular arithmetic.
Crypto++ library namespace.
const char * ModulusSize()
int, in bits
Definition: argnames.h:29
Integer ApplyFunction(const Integer &x) const
Applies the trapdoor.
Definition: rsa.cpp:64
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
Definition: rsa.cpp:70
Object Identifier.
Definition: asn.h:158
Classes for probablistic signature schemes.
Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const
Calculates the inverse of an element.
Definition: rsa.cpp:227
virtual Element Exponentiate(const Element &a, const Integer &e) const
Raises a base to an exponent in the group.
Definition: algebra.cpp:316
RSA trapdoor function using the public key.
Definition: rsa.h:23
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