13 #if !defined(NDEBUG) && !defined(CRYPTOPP_DOXYGEN_PROCESSING) && !defined(CRYPTOPP_IS_DLL) 16 void RSA_TestInstantiations()
39 #ifndef CRYPTOPP_IMPORTS 45 return ASN1::rsaEncryption();
67 return a_exp_b_mod_c(x, m_e, m_n);
72 CRYPTOPP_UNUSED(rng), CRYPTOPP_UNUSED(level);
76 pass = pass && m_e >
Integer::One() && m_e.IsOdd() && m_e < m_n;
82 return GetValueHelper(
this, name, valueType, pValue).Assignable()
83 CRYPTOPP_GET_FUNCTION_ENTRY(
Modulus)
90 AssignFromHelper(
this, source)
91 CRYPTOPP_SET_FUNCTION_ENTRY(
Modulus)
102 bool IsAcceptable(
const Integer &candidate)
const {
return RelativelyPrime(m_e, candidate-
Integer::One());}
108 int modulusSize = 2048;
111 assert(modulusSize >= 16);
112 if (modulusSize < 16)
113 throw InvalidArgument(
"InvertibleRSAFunction: specified modulus size is too small");
117 assert(m_e >= 3); assert(!m_e.IsEven());
118 if (m_e < 3 || m_e.IsEven())
119 throw InvalidArgument(
"InvertibleRSAFunction: invalid public exponent");
124 m_p.GenerateRandom(rng, primeParam);
125 m_q.GenerateRandom(rng, primeParam);
127 m_d = m_e.InverseMod(LCM(m_p-1, m_q-1));
128 assert(m_d.IsPositive());
130 m_dp = m_d % (m_p-1);
131 m_dq = m_d % (m_q-1);
133 m_u = m_q.InverseMod(m_p);
139 SignaturePairwiseConsistencyTest_FIPS_140_Only(signer, verifier);
143 EncryptionPairwiseConsistencyTest_FIPS_140_Only(encryptor, decryptor);
154 if (n.IsEven() || e.IsEven() | d.IsEven())
155 throw InvalidArgument(
"InvertibleRSAFunction: input is not a valid RSA private key");
172 Integer a = modn.Exponentiate(i, r);
184 m_dp = m_d % (m_p-1);
185 m_dq = m_d % (m_q-1);
190 throw InvalidArgument(
"InvertibleRSAFunction: input is not a valid RSA private key");
200 BERDecodeUnsigned<word32>(privateKey, version, INTEGER, 0, 0);
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();
215 DEREncodeUnsigned<word32>(privateKey, 0);
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();
229 DoQuickSanityCheck();
234 rInv = modn.MultiplicativeInverse(r);
235 }
while (rInv.IsZero());
236 Integer re = modn.Exponentiate(r, m_e);
237 re = modn.Multiply(re, x);
240 Integer y = ModularRoot(re, m_dq, m_dp, m_q, m_p, m_u);
241 y = modn.Multiply(y, rInv);
242 if (modn.Exponentiate(y, m_e) != x)
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;
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;
270 return GetValueHelper<RSAFunction>(
this, name, valueType, pValue).Assignable()
271 CRYPTOPP_GET_FUNCTION_ENTRY(
Prime1)
272 CRYPTOPP_GET_FUNCTION_ENTRY(
Prime2)
282 AssignFromHelper<RSAFunction>(
this, source)
283 CRYPTOPP_SET_FUNCTION_ENTRY(
Prime1)
284 CRYPTOPP_SET_FUNCTION_ENTRY(
Prime2)
297 return t % 16 == 12 ? t : m_n - t;
302 Integer t = InvertibleRSAFunction::CalculateInverse(rng, x);
Base class for all exceptions thrown by Crypto++.
const char * MultiplicativeInverseOfPrime2ModPrime1()
Integer.
An invalid argument was detected.
Classes for working with NameValuePairs.
const char * Prime2()
Integer.
T GetValueWithDefault(const char *name, T defaultValue) const
Get a named value.
void DEREncodePublicKey(BufferedTransformation &bt) const
encode subjectPublicKey part of subjectPublicKeyInfo, without the BIT STRING header ...
void BERDecodePrivateKey(BufferedTransformation &bt, bool parametersPresent, size_t size)
decode privateKey part of privateKeyInfo, without the OCTET STRING header
void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg)
Some other error occurred not belong to any of the above categories.
Object identifiers for algorthms and schemes.
Ring of congruence classes modulo n.
Interface for random number generators.
void Randomize(RandomNumberGenerator &rng, size_t bitCount)
Set this Integer to random integer.
static const Integer & One()
Integer representing 1.
bool GetIntValue(const char *name, int &value) const
Get a named value with type int.
void BERDecodePublicKey(BufferedTransformation &bt, bool parametersPresent, size_t size)
decode subjectPublicKey part of subjectPublicKeyInfo, without the BIT STRING header ...
const char * PrivateExponent()
Integer.
const char * Prime1()
Integer.
bool FIPS_140_2_ComplianceEnabled()
Determines whether the library provides FIPS validated cryptography.
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
const char * PublicExponent()
Integer.
bool VerifyPrime(RandomNumberGenerator &rng, const Integer &p, unsigned int level=1)
Verifies a prime number.
void AssignFrom(const NameValuePairs &source)
Assign values to this object.
const char * ModPrime1PrivateExponent()
Integer.
Integer ApplyFunction(const Integer &x) const
Applies the trapdoor.
Application callback to signal suitability of a cabdidate prime.
Multiple precision integer with arithmetic operations.
const char * ModPrime2PrivateExponent()
Integer.
void DEREncodePrivateKey(BufferedTransformation &bt) const
encode privateKey part of privateKeyInfo, without the OCTET STRING header
void AssignFrom(const NameValuePairs &source)
Assign values to this object.
RandomNumberGenerator & NullRNG()
Random Number Generator that does not produce random numbers.
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
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.
const char * PointerToPrimeSelector()
const PrimeSelector *
Classes and functions for number theoretic operations.
const char * KeySize()
int, in bits
void DEREncode(BufferedTransformation &bt) const
Encode in DER format.
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.
An object that implements NameValuePairs.
const char * Modulus()
Integer.
Integer InverseMod(const Integer &n) const
calculate multiplicative inverse of *this mod n
void BERDecode(const byte *input, size_t inputLen)
Decode from BER format.
Class file for performing modular arithmetic.
Crypto++ library namespace.
const char * ModulusSize()
int, in bits
Integer ApplyFunction(const Integer &x) const
Applies the trapdoor.
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
Classes for probablistic signature schemes.
Interface for retrieving values given their names.
A template implementing constructors for public key algorithm classes.