14#if defined(CRYPTOPP_DEBUG) && !defined(CRYPTOPP_DOXYGEN_PROCESSING) && !defined(CRYPTOPP_IS_DLL)
18void RSA_TestInstantiations()
20 RSASS<PKCS1v15, SHA1>::Verifier x1(1, 1);
21 RSASS<PKCS1v15, SHA1>::Signer x2(
NullRNG(), 1);
22 RSASS<PKCS1v15, SHA1>::Verifier x3(x2);
23 RSASS<PKCS1v15, SHA1>::Verifier x4(x2.GetKey());
24 RSASS<PSS, SHA1>::Verifier x5(x3);
26 RSASS<PSSR, SHA1>::Signer x6 = x2;
30 RSAES<PKCS1v15>::Encryptor x7(x2);
32 RSAES<PKCS1v15>::Encryptor x8(x3);
37 RSASS<PKCS1v15, SHA3_256>::Verifier x10(1, 1);
38 RSASS<PKCS1v15, SHA3_256>::Signer x11(
NullRNG(), 1);
39 RSASS<PKCS1v15, SHA3_256>::Verifier x12(x11);
40 RSASS<PKCS1v15, SHA3_256>::Verifier x13(x11.GetKey());
45#ifndef CRYPTOPP_IMPORTS
51 return ASN1::rsaEncryption();
56 BERSequenceDecoder seq(bt);
64 DERSequenceEncoder seq(bt);
73 return a_exp_b_mod_c(x, m_e, m_n);
78 CRYPTOPP_UNUSED(rng), CRYPTOPP_UNUSED(level);
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)
109 RSAPrimeSelector(
const Integer &e) : m_e(e) {}
116 int modulusSize = 2048;
120 if (modulusSize < 16)
121 throw InvalidArgument(
"InvertibleRSAFunction: specified modulus size is too small");
126 if (m_e < 3 || m_e.IsEven())
127 throw InvalidArgument(
"InvertibleRSAFunction: invalid public exponent");
133 RSAPrimeSelector selector(m_e);
134 AlgorithmParameters primeParam = MakeParametersForTwoPrimesOfEqualSize(modulusSize)
136 m_p.GenerateRandom(rng, primeParam);
137 m_q.GenerateRandom(rng, primeParam);
139 m_d = m_e.InverseMod(
LCM(m_p-1, m_q-1));
142 m_dp = m_d % (m_p-1);
143 m_dq = m_d % (m_q-1);
145 m_u = m_q.InverseMod(m_p);
146 }
while (m_u.IsZero());
152 SignaturePairwiseConsistencyTest_FIPS_140_Only(signer, verifier);
154 RSAES<OAEP<SHA1> >::Decryptor decryptor(*
this);
155 RSAES<OAEP<SHA1> >::Encryptor encryptor(decryptor);
156 EncryptionPairwiseConsistencyTest_FIPS_140_Only(encryptor, decryptor);
168 throw InvalidArgument(
"InvertibleRSAFunction: input is not a valid RSA private key");
182 ModularArithmetic modn(n);
183 for (Integer i = 2; ; ++i)
185 Integer a = modn.Exponentiate(i, r);
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");
211 BERSequenceDecoder privateKey(bt);
214 m_n.BERDecode(privateKey);
215 m_e.BERDecode(privateKey);
216 m_d.BERDecode(privateKey);
217 m_p.BERDecode(privateKey);
218 m_q.BERDecode(privateKey);
219 m_dp.BERDecode(privateKey);
220 m_dq.BERDecode(privateKey);
221 m_u.BERDecode(privateKey);
222 privateKey.MessageEnd();
227 DERSequenceEncoder privateKey(bt);
229 m_n.DEREncode(privateKey);
230 m_e.DEREncode(privateKey);
231 m_d.DEREncode(privateKey);
232 m_p.DEREncode(privateKey);
233 m_q.DEREncode(privateKey);
234 m_dp.DEREncode(privateKey);
235 m_dq.DEREncode(privateKey);
236 m_u.DEREncode(privateKey);
237 privateKey.MessageEnd();
243 ModularArithmetic modn(m_n);
249 Integer re = modn.Exponentiate(r, m_e);
250 re = modn.Multiply(re, x);
253 Integer y =
ModularRoot(re, m_dq, m_dp, m_q, m_p, m_u);
254 y = modn.Multiply(y, rInv);
255 if (modn.Exponentiate(y, m_e) != x)
256 throw Exception(
Exception::OTHER_ERROR,
"InvertibleRSAFunction: computational error during private key operation");
274 pass = pass && m_u.IsPositive() && m_u < m_p;
278 pass = pass && m_p * m_q == m_n;
280 pass = pass && m_e*m_d %
LCM(m_p-1, m_q-1) == 1;
282 pass = pass && m_dp == m_d%(m_p-1) && m_dq == m_d%(m_q-1);
284 pass = pass && m_u * m_q % m_p == 1;
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.
void BERDecodeUnsigned(BufferedTransformation &in, T &w, byte asnTag=INTEGER, T minValue=0, T maxValue=T(0xffffffff))
BER Decode unsigned value.
size_t DEREncodeUnsigned(BufferedTransformation &out, T w, byte asnTag=INTEGER)
DER Encode unsigned value.
void DoQuickSanityCheck() const
Perform a quick sanity check.
@ OTHER_ERROR
Some other error occurred not belonging to other categories.
Multiple precision integer with arithmetic operations.
void Randomize(RandomNumberGenerator &rng, size_t bitCount)
Set this Integer to random integer.
bool IsZero() const
Determines if the Integer is 0.
Integer MultiplicativeInverse() const
Calculate multiplicative inverse.
bool IsOdd() const
Determines if the Integer is odd parity.
Integer InverseMod(const Integer &n) const
Calculate multiplicative inverse.
static const Integer & One()
Integer representing 1.
bool IsEven() const
Determines if the Integer is even parity.
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.
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.
Application callback to signal suitability of a cabdidate prime.
Integer ApplyFunction(const Integer &x) const
Applies the trapdoor.
bool Validate(RandomNumberGenerator &rng, unsigned int level) const
Check this object for errors.
OID GetAlgorithmID() const
Retrieves the OID of the algorithm.
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.
PK_FinalTemplate< TF_SignerImpl< SchemeOptions > > Signer
PK_FinalTemplate< TF_VerifierImpl< SchemeOptions > > Verifier
unsigned int word32
32-bit unsigned datatype
CRYPTOPP_DLL RandomNumberGenerator & NullRNG()
Random Number Generator that does not produce random numbers.
Classes and functions for the FIPS 140-2 validated library.
CRYPTOPP_DLL bool FIPS_140_2_ComplianceEnabled()
Determines whether the library provides FIPS validated cryptography.
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
Class file for performing modular arithmetic.
Crypto++ library namespace.
const char * PointerToPrimeSelector()
const PrimeSelector *
const char * KeySize()
int, in bits
const char * PublicExponent()
Integer.
const char * ModulusSize()
int, in bits
Classes and functions for number theoretic operations.
bool RelativelyPrime(const Integer &a, const Integer &b)
Determine relative primality.
CRYPTOPP_DLL bool VerifyPrime(RandomNumberGenerator &rng, const Integer &p, unsigned int level=1)
Verifies a number is probably prime.
CRYPTOPP_DLL Integer ModularRoot(const Integer &a, const Integer &dp, const Integer &dq, const Integer &p, const Integer &q, const Integer &u)
Extract a modular root.
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.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.