14#if defined(CRYPTOPP_DEBUG) && !defined(CRYPTOPP_DOXYGEN_PROCESSING) && !defined(CRYPTOPP_IS_DLL)
17NAMESPACE_BEGIN(CryptoPP)
18void RSA_TestInstantiations()
45#ifndef CRYPTOPP_IMPORTS
47NAMESPACE_BEGIN(CryptoPP)
51 return ASN1::rsaEncryption();
73 return a_exp_b_mod_c(x, m_e, m_n);
78 CRYPTOPP_UNUSED(rng), CRYPTOPP_UNUSED(level);
82 CRYPTOPP_ASSERT(pass);
84 CRYPTOPP_ASSERT(pass);
90 return GetValueHelper(
this, name, valueType, pValue).Assignable()
91 CRYPTOPP_GET_FUNCTION_ENTRY(Modulus)
92 CRYPTOPP_GET_FUNCTION_ENTRY(PublicExponent)
98 AssignFromHelper(
this, source)
99 CRYPTOPP_SET_FUNCTION_ENTRY(Modulus)
100 CRYPTOPP_SET_FUNCTION_ENTRY(PublicExponent)
116 int modulusSize = 2048;
119 CRYPTOPP_ASSERT(modulusSize >= 16);
120 if (modulusSize < 16)
121 throw InvalidArgument(
"InvertibleRSAFunction: specified modulus size is too small");
125 CRYPTOPP_ASSERT(m_e >= 3); CRYPTOPP_ASSERT(!m_e.
IsEven());
126 if (m_e < 3 || m_e.
IsEven())
127 throw InvalidArgument(
"InvertibleRSAFunction: invalid public exponent");
135 (Name::PointerToPrimeSelector(), selector.GetSelectorPointer());
142 m_dp = m_d % (m_p-1);
143 m_dq = m_d % (m_q-1);
148 if (FIPS_140_2_ComplianceEnabled())
152 SignaturePairwiseConsistencyTest_FIPS_140_Only(signer, verifier);
156 EncryptionPairwiseConsistencyTest_FIPS_140_Only(encryptor, decryptor);
167 if (n.
IsEven() || e.IsEven() || d.IsEven())
168 throw InvalidArgument(
"InvertibleRSAFunction: input is not a valid RSA private key");
197 m_dp = m_d % (m_p-1);
198 m_dq = m_d % (m_q-1);
203 throw InvalidArgument(
"InvertibleRSAFunction: input is not a valid RSA private key");
213 BERDecodeUnsigned<word32>(privateKey, version,
INTEGER, 0, 0);
228 DEREncodeUnsigned<word32>(privateKey, 0);
253 Integer y = ModularRoot(re, m_dq, m_dp, m_q, m_p, m_u);
263 CRYPTOPP_ASSERT(pass);
265 CRYPTOPP_ASSERT(pass);
267 CRYPTOPP_ASSERT(pass);
269 CRYPTOPP_ASSERT(pass);
271 CRYPTOPP_ASSERT(pass);
273 CRYPTOPP_ASSERT(pass);
275 CRYPTOPP_ASSERT(pass);
278 pass = pass && m_p * m_q == m_n;
279 CRYPTOPP_ASSERT(pass);
280 pass = pass && m_e*m_d %
LCM(m_p-1, m_q-1) == 1;
281 CRYPTOPP_ASSERT(pass);
282 pass = pass && m_dp == m_d%(m_p-1) && m_dq == m_d%(m_q-1);
283 CRYPTOPP_ASSERT(pass);
284 pass = pass && m_u * m_q % m_p == 1;
285 CRYPTOPP_ASSERT(pass);
289 pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2);
290 CRYPTOPP_ASSERT(pass);
297 return GetValueHelper<RSAFunction>(
this, name, valueType, pValue).Assignable()
298 CRYPTOPP_GET_FUNCTION_ENTRY(Prime1)
299 CRYPTOPP_GET_FUNCTION_ENTRY(Prime2)
300 CRYPTOPP_GET_FUNCTION_ENTRY(PrivateExponent)
301 CRYPTOPP_GET_FUNCTION_ENTRY(ModPrime1PrivateExponent)
302 CRYPTOPP_GET_FUNCTION_ENTRY(ModPrime2PrivateExponent)
303 CRYPTOPP_GET_FUNCTION_ENTRY(MultiplicativeInverseOfPrime2ModPrime1)
309 AssignFromHelper<RSAFunction>(
this, source)
310 CRYPTOPP_SET_FUNCTION_ENTRY(Prime1)
311 CRYPTOPP_SET_FUNCTION_ENTRY(Prime2)
312 CRYPTOPP_SET_FUNCTION_ENTRY(PrivateExponent)
313 CRYPTOPP_SET_FUNCTION_ENTRY(ModPrime1PrivateExponent)
314 CRYPTOPP_SET_FUNCTION_ENTRY(ModPrime2PrivateExponent)
315 CRYPTOPP_SET_FUNCTION_ENTRY(MultiplicativeInverseOfPrime2ModPrime1)
324 return t % 16 == 12 ? t : m_n - t;
Classes for working with NameValuePairs.
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Classes and functions for working with ANS.1 objects.
virtual Element Exponentiate(const Element &a, const Integer &e) const
Raises a base to an exponent in the group.
An object that implements NameValuePairs.
void MessageEnd()
Signals the end of messages to the object.
void DoQuickSanityCheck() const
Perform a quick sanity check.
void MessageEnd()
Signals the end of messages to the object.
Base class for all exceptions thrown by the library.
@ OTHER_ERROR
Some other error occurred not belonging to other categories.
Multiple precision integer with arithmetic operations.
void DEREncode(BufferedTransformation &bt) const
Encode in DER format.
void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs ¶ms=g_nullNameValuePairs)
Generate a random number.
bool IsPositive() const
Determines if the Integer is positive.
void Randomize(RandomNumberGenerator &rng, size_t bitCount)
Set this Integer to random integer.
void BERDecode(const byte *input, size_t inputLen)
Decode from BER format.
static const Integer &CRYPTOPP_API One()
Integer representing 1.
bool IsZero() const
Determines if the Integer is 0.
bool IsOdd() const
Determines if the Integer is odd parity.
Integer InverseMod(const Integer &n) const
Calculate multiplicative inverse.
bool IsEven() const
Determines if the Integer is even parity.
An invalid argument was detected.
Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const
Calculates the inverse of an element.
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
void Initialize(RandomNumberGenerator &rng, unsigned int modulusBits, const Integer &e=17)
Create a RSA private key.
void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg)
Generate a random key or crypto parameters.
void AssignFrom(const NameValuePairs &source)
Assign values to this object.
void DEREncodePrivateKey(BufferedTransformation &bt) const
Encode privateKey part of privateKeyInfo.
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
Integer CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const
Calculates the inverse of an element.
void BERDecodePrivateKey(BufferedTransformation &bt, bool parametersPresent, size_t size)
Decode privateKey part of privateKeyInfo.
Ring of congruence classes modulo n.
const Integer & MultiplicativeInverse(const Integer &a) const
Calculate the multiplicative inverse of an element in the ring.
const Integer & Square(const Integer &a) const
Square an element in the ring.
const Integer & Multiply(const Integer &a, const Integer &b) const
Multiplies elements in the ring.
Interface for retrieving values given their names.
T GetValueWithDefault(const char *name, T defaultValue) const
Get a named value.
CRYPTOPP_DLL bool GetIntValue(const char *name, int &value) const
Get a named value with type int.
Template implementing constructors for public key algorithm classes.
Application callback to signal suitability of a cabdidate prime.
Integer ApplyFunction(const Integer &x) const
Applies the trapdoor.
RSA trapdoor function using the public key.
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
Integer ApplyFunction(const Integer &x) const
Applies the trapdoor.
void BERDecodePublicKey(BufferedTransformation &bt, bool parametersPresent, size_t size)
Decode subjectPublicKey part of subjectPublicKeyInfo.
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
Get a named value.
void DEREncodePublicKey(BufferedTransformation &bt) const
Encode subjectPublicKey part of subjectPublicKeyInfo.
void AssignFrom(const NameValuePairs &source)
Assign values to this object.
Interface for random number generators.
unsigned int word32
32-bit unsigned datatype
Classes and functions for the FIPS 140-2 validated library.
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
Class file for performing modular arithmetic.
Classes and functions for number theoretic operations.
bool RelativelyPrime(const Integer &a, const Integer &b)
Determine relative primality.
Integer GCD(const Integer &a, const Integer &b)
Calculate the greatest common divisor.
Integer LCM(const Integer &a, const Integer &b)
Calculate the least common multiple.
ASN.1 object identifiers for algorithms and schemes.
Classes for PKCS padding schemes.
Classes for probabilistic signature schemes.
Classes for the RSA cryptosystem.
Classes for SHA3 message digests.
Classes for SHA-1 and SHA-2 family of message digests.
RSA encryption algorithm.