2002-10-04 17:31:41 +00:00
|
|
|
// rsa.cpp - written and placed in the public domain by Wei Dai
|
|
|
|
|
|
|
|
#include "pch.h"
|
|
|
|
#include "rsa.h"
|
|
|
|
#include "asn.h"
|
|
|
|
#include "oids.h"
|
2003-03-20 01:24:12 +00:00
|
|
|
#include "modarith.h"
|
2002-10-04 17:31:41 +00:00
|
|
|
#include "nbtheory.h"
|
|
|
|
#include "sha.h"
|
|
|
|
#include "algparam.h"
|
|
|
|
#include "fips140.h"
|
|
|
|
|
2003-07-11 19:16:35 +00:00
|
|
|
#if !defined(NDEBUG) && !defined(CRYPTOPP_IS_DLL)
|
2003-03-20 01:24:12 +00:00
|
|
|
#include "pssr.h"
|
2002-10-04 17:31:41 +00:00
|
|
|
NAMESPACE_BEGIN(CryptoPP)
|
|
|
|
void RSA_TestInstantiations()
|
|
|
|
{
|
2003-03-20 01:24:12 +00:00
|
|
|
RSASS<PKCS1v15, SHA>::Verifier x1(1, 1);
|
|
|
|
RSASS<PKCS1v15, SHA>::Signer x2(NullRNG(), 1);
|
|
|
|
RSASS<PKCS1v15, SHA>::Verifier x3(x2);
|
|
|
|
RSASS<PKCS1v15, SHA>::Verifier x4(x2.GetKey());
|
|
|
|
RSASS<PSS, SHA>::Verifier x5(x3);
|
2003-03-20 20:39:59 +00:00
|
|
|
#ifndef __MWERKS__
|
2003-03-20 01:24:12 +00:00
|
|
|
RSASS<PSSR, SHA>::Signer x6 = x2;
|
2003-03-20 20:39:59 +00:00
|
|
|
x3 = x2;
|
|
|
|
x6 = x2;
|
|
|
|
#endif
|
2002-10-04 17:31:41 +00:00
|
|
|
RSAES<PKCS1v15>::Encryptor x7(x2);
|
2003-03-20 01:24:12 +00:00
|
|
|
#ifndef __GNUC__
|
2002-10-04 17:31:41 +00:00
|
|
|
RSAES<PKCS1v15>::Encryptor x8(x3);
|
2003-03-20 01:24:12 +00:00
|
|
|
#endif
|
2002-10-04 17:31:41 +00:00
|
|
|
RSAES<OAEP<SHA> >::Encryptor x9(x2);
|
|
|
|
|
|
|
|
x4 = x2.GetKey();
|
|
|
|
}
|
2003-07-18 04:35:30 +00:00
|
|
|
NAMESPACE_END
|
2003-03-20 01:24:12 +00:00
|
|
|
#endif
|
2002-10-04 17:31:41 +00:00
|
|
|
|
2003-07-04 00:17:37 +00:00
|
|
|
#ifndef CRYPTOPP_IMPORTS
|
2002-10-04 17:31:41 +00:00
|
|
|
|
2003-07-18 04:35:30 +00:00
|
|
|
NAMESPACE_BEGIN(CryptoPP)
|
|
|
|
|
2002-10-04 17:31:41 +00:00
|
|
|
OID RSAFunction::GetAlgorithmID() const
|
|
|
|
{
|
|
|
|
return ASN1::rsaEncryption();
|
|
|
|
}
|
|
|
|
|
2006-12-18 02:34:33 +00:00
|
|
|
void RSAFunction::BERDecodePublicKey(BufferedTransformation &bt, bool, size_t)
|
2002-10-04 17:31:41 +00:00
|
|
|
{
|
|
|
|
BERSequenceDecoder seq(bt);
|
|
|
|
m_n.BERDecode(seq);
|
|
|
|
m_e.BERDecode(seq);
|
|
|
|
seq.MessageEnd();
|
|
|
|
}
|
|
|
|
|
2006-12-18 02:34:33 +00:00
|
|
|
void RSAFunction::DEREncodePublicKey(BufferedTransformation &bt) const
|
2002-10-04 17:31:41 +00:00
|
|
|
{
|
|
|
|
DERSequenceEncoder seq(bt);
|
|
|
|
m_n.DEREncode(seq);
|
|
|
|
m_e.DEREncode(seq);
|
|
|
|
seq.MessageEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
Integer RSAFunction::ApplyFunction(const Integer &x) const
|
|
|
|
{
|
|
|
|
DoQuickSanityCheck();
|
|
|
|
return a_exp_b_mod_c(x, m_e, m_n);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RSAFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const
|
|
|
|
{
|
|
|
|
bool pass = true;
|
|
|
|
pass = pass && m_n > Integer::One() && m_n.IsOdd();
|
|
|
|
pass = pass && m_e > Integer::One() && m_e.IsOdd() && m_e < m_n;
|
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RSAFunction::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
|
|
|
|
{
|
|
|
|
return GetValueHelper(this, name, valueType, pValue).Assignable()
|
|
|
|
CRYPTOPP_GET_FUNCTION_ENTRY(Modulus)
|
|
|
|
CRYPTOPP_GET_FUNCTION_ENTRY(PublicExponent)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RSAFunction::AssignFrom(const NameValuePairs &source)
|
|
|
|
{
|
|
|
|
AssignFromHelper(this, source)
|
|
|
|
CRYPTOPP_SET_FUNCTION_ENTRY(Modulus)
|
|
|
|
CRYPTOPP_SET_FUNCTION_ENTRY(PublicExponent)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
// *****************************************************************************
|
|
|
|
|
|
|
|
class RSAPrimeSelector : public PrimeSelector
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RSAPrimeSelector(const Integer &e) : m_e(e) {}
|
2015-07-17 14:38:44 +00:00
|
|
|
virtual ~RSAPrimeSelector() { }
|
|
|
|
|
2002-10-04 17:31:41 +00:00
|
|
|
bool IsAcceptable(const Integer &candidate) const {return RelativelyPrime(m_e, candidate-Integer::One());}
|
|
|
|
Integer m_e;
|
|
|
|
};
|
|
|
|
|
|
|
|
void InvertibleRSAFunction::GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg)
|
|
|
|
{
|
|
|
|
int modulusSize = 2048;
|
2003-07-16 01:53:45 +00:00
|
|
|
alg.GetIntValue(Name::ModulusSize(), modulusSize) || alg.GetIntValue(Name::KeySize(), modulusSize);
|
2002-10-04 17:31:41 +00:00
|
|
|
|
|
|
|
if (modulusSize < 16)
|
|
|
|
throw InvalidArgument("InvertibleRSAFunction: specified modulus size is too small");
|
|
|
|
|
2003-07-16 01:53:45 +00:00
|
|
|
m_e = alg.GetValueWithDefault(Name::PublicExponent(), Integer(17));
|
2002-10-04 17:31:41 +00:00
|
|
|
|
|
|
|
if (m_e < 3 || m_e.IsEven())
|
|
|
|
throw InvalidArgument("InvertibleRSAFunction: invalid public exponent");
|
|
|
|
|
|
|
|
RSAPrimeSelector selector(m_e);
|
2009-02-13 12:18:26 +00:00
|
|
|
AlgorithmParameters primeParam = MakeParametersForTwoPrimesOfEqualSize(modulusSize)
|
2003-07-16 01:53:45 +00:00
|
|
|
(Name::PointerToPrimeSelector(), selector.GetSelectorPointer());
|
2002-10-04 17:31:41 +00:00
|
|
|
m_p.GenerateRandom(rng, primeParam);
|
|
|
|
m_q.GenerateRandom(rng, primeParam);
|
|
|
|
|
2009-03-02 02:39:17 +00:00
|
|
|
m_d = m_e.InverseMod(LCM(m_p-1, m_q-1));
|
2002-10-04 17:31:41 +00:00
|
|
|
assert(m_d.IsPositive());
|
|
|
|
|
|
|
|
m_dp = m_d % (m_p-1);
|
|
|
|
m_dq = m_d % (m_q-1);
|
|
|
|
m_n = m_p * m_q;
|
|
|
|
m_u = m_q.InverseMod(m_p);
|
|
|
|
|
|
|
|
if (FIPS_140_2_ComplianceEnabled())
|
|
|
|
{
|
2003-03-20 01:24:12 +00:00
|
|
|
RSASS<PKCS1v15, SHA>::Signer signer(*this);
|
|
|
|
RSASS<PKCS1v15, SHA>::Verifier verifier(signer);
|
|
|
|
SignaturePairwiseConsistencyTest_FIPS_140_Only(signer, verifier);
|
2002-10-04 17:31:41 +00:00
|
|
|
|
|
|
|
RSAES<OAEP<SHA> >::Decryptor decryptor(*this);
|
|
|
|
RSAES<OAEP<SHA> >::Encryptor encryptor(decryptor);
|
2003-03-20 01:24:12 +00:00
|
|
|
EncryptionPairwiseConsistencyTest_FIPS_140_Only(encryptor, decryptor);
|
2002-10-04 17:31:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InvertibleRSAFunction::Initialize(RandomNumberGenerator &rng, unsigned int keybits, const Integer &e)
|
|
|
|
{
|
2003-07-16 01:53:45 +00:00
|
|
|
GenerateRandom(rng, MakeParameters(Name::ModulusSize(), (int)keybits)(Name::PublicExponent(), e+e.IsEven()));
|
2002-10-04 17:31:41 +00:00
|
|
|
}
|
|
|
|
|
2003-03-20 01:24:12 +00:00
|
|
|
void InvertibleRSAFunction::Initialize(const Integer &n, const Integer &e, const Integer &d)
|
|
|
|
{
|
2004-04-29 14:48:51 +00:00
|
|
|
if (n.IsEven() || e.IsEven() | d.IsEven())
|
|
|
|
throw InvalidArgument("InvertibleRSAFunction: input is not a valid RSA private key");
|
|
|
|
|
2003-03-20 01:24:12 +00:00
|
|
|
m_n = n;
|
|
|
|
m_e = e;
|
|
|
|
m_d = d;
|
|
|
|
|
|
|
|
Integer r = --(d*e);
|
2004-04-29 14:48:51 +00:00
|
|
|
unsigned int s = 0;
|
2003-03-20 01:24:12 +00:00
|
|
|
while (r.IsEven())
|
2004-04-29 14:48:51 +00:00
|
|
|
{
|
2003-03-20 01:24:12 +00:00
|
|
|
r >>= 1;
|
2004-04-29 14:48:51 +00:00
|
|
|
s++;
|
|
|
|
}
|
2003-03-20 01:24:12 +00:00
|
|
|
|
|
|
|
ModularArithmetic modn(n);
|
|
|
|
for (Integer i = 2; ; ++i)
|
|
|
|
{
|
|
|
|
Integer a = modn.Exponentiate(i, r);
|
|
|
|
if (a == 1)
|
|
|
|
continue;
|
|
|
|
Integer b;
|
2004-04-29 14:48:51 +00:00
|
|
|
unsigned int j = 0;
|
2004-05-03 18:15:11 +00:00
|
|
|
while (a != n-1)
|
2003-03-20 01:24:12 +00:00
|
|
|
{
|
|
|
|
b = modn.Square(a);
|
|
|
|
if (b == 1)
|
|
|
|
{
|
|
|
|
m_p = GCD(a-1, n);
|
|
|
|
m_q = n/m_p;
|
|
|
|
m_dp = m_d % (m_p-1);
|
|
|
|
m_dq = m_d % (m_q-1);
|
|
|
|
m_u = m_q.InverseMod(m_p);
|
|
|
|
return;
|
|
|
|
}
|
2004-04-29 14:48:51 +00:00
|
|
|
if (++j == s)
|
|
|
|
throw InvalidArgument("InvertibleRSAFunction: input is not a valid RSA private key");
|
2003-03-20 01:24:12 +00:00
|
|
|
a = b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-18 02:34:33 +00:00
|
|
|
void InvertibleRSAFunction::BERDecodePrivateKey(BufferedTransformation &bt, bool, size_t)
|
2002-10-04 17:31:41 +00:00
|
|
|
{
|
|
|
|
BERSequenceDecoder privateKey(bt);
|
|
|
|
word32 version;
|
|
|
|
BERDecodeUnsigned<word32>(privateKey, version, INTEGER, 0, 0); // check version
|
|
|
|
m_n.BERDecode(privateKey);
|
|
|
|
m_e.BERDecode(privateKey);
|
|
|
|
m_d.BERDecode(privateKey);
|
|
|
|
m_p.BERDecode(privateKey);
|
|
|
|
m_q.BERDecode(privateKey);
|
|
|
|
m_dp.BERDecode(privateKey);
|
|
|
|
m_dq.BERDecode(privateKey);
|
|
|
|
m_u.BERDecode(privateKey);
|
|
|
|
privateKey.MessageEnd();
|
|
|
|
}
|
|
|
|
|
2006-12-18 02:34:33 +00:00
|
|
|
void InvertibleRSAFunction::DEREncodePrivateKey(BufferedTransformation &bt) const
|
2002-10-04 17:31:41 +00:00
|
|
|
{
|
|
|
|
DERSequenceEncoder privateKey(bt);
|
|
|
|
DEREncodeUnsigned<word32>(privateKey, 0); // version
|
|
|
|
m_n.DEREncode(privateKey);
|
|
|
|
m_e.DEREncode(privateKey);
|
|
|
|
m_d.DEREncode(privateKey);
|
|
|
|
m_p.DEREncode(privateKey);
|
|
|
|
m_q.DEREncode(privateKey);
|
|
|
|
m_dp.DEREncode(privateKey);
|
|
|
|
m_dq.DEREncode(privateKey);
|
|
|
|
m_u.DEREncode(privateKey);
|
|
|
|
privateKey.MessageEnd();
|
|
|
|
}
|
|
|
|
|
2003-03-20 01:24:12 +00:00
|
|
|
Integer InvertibleRSAFunction::CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const
|
2002-10-04 17:31:41 +00:00
|
|
|
{
|
|
|
|
DoQuickSanityCheck();
|
2003-03-20 01:24:12 +00:00
|
|
|
ModularArithmetic modn(m_n);
|
2003-03-25 02:11:53 +00:00
|
|
|
Integer r, rInv;
|
2007-04-16 00:13:46 +00:00
|
|
|
do { // do this in a loop for people using small numbers for testing
|
2003-03-25 02:11:53 +00:00
|
|
|
r.Randomize(rng, Integer::One(), m_n - Integer::One());
|
|
|
|
rInv = modn.MultiplicativeInverse(r);
|
|
|
|
} while (rInv.IsZero());
|
2003-03-20 01:24:12 +00:00
|
|
|
Integer re = modn.Exponentiate(r, m_e);
|
|
|
|
re = modn.Multiply(re, x); // blind
|
2002-10-04 17:31:41 +00:00
|
|
|
// here we follow the notation of PKCS #1 and let u=q inverse mod p
|
|
|
|
// but in ModRoot, u=p inverse mod q, so we reverse the order of p and q
|
2003-03-20 01:24:12 +00:00
|
|
|
Integer y = ModularRoot(re, m_dq, m_dp, m_q, m_p, m_u);
|
2003-03-25 02:11:53 +00:00
|
|
|
y = modn.Multiply(y, rInv); // unblind
|
2003-03-20 01:24:12 +00:00
|
|
|
if (modn.Exponentiate(y, m_e) != x) // check
|
|
|
|
throw Exception(Exception::OTHER_ERROR, "InvertibleRSAFunction: computational error during private key operation");
|
|
|
|
return y;
|
2002-10-04 17:31:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool InvertibleRSAFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const
|
|
|
|
{
|
|
|
|
bool pass = RSAFunction::Validate(rng, level);
|
|
|
|
pass = pass && m_p > Integer::One() && m_p.IsOdd() && m_p < m_n;
|
|
|
|
pass = pass && m_q > Integer::One() && m_q.IsOdd() && m_q < m_n;
|
|
|
|
pass = pass && m_d > Integer::One() && m_d.IsOdd() && m_d < m_n;
|
|
|
|
pass = pass && m_dp > Integer::One() && m_dp.IsOdd() && m_dp < m_p;
|
|
|
|
pass = pass && m_dq > Integer::One() && m_dq.IsOdd() && m_dq < m_q;
|
|
|
|
pass = pass && m_u.IsPositive() && m_u < m_p;
|
|
|
|
if (level >= 1)
|
|
|
|
{
|
|
|
|
pass = pass && m_p * m_q == m_n;
|
|
|
|
pass = pass && m_e*m_d % LCM(m_p-1, m_q-1) == 1;
|
|
|
|
pass = pass && m_dp == m_d%(m_p-1) && m_dq == m_d%(m_q-1);
|
|
|
|
pass = pass && m_u * m_q % m_p == 1;
|
|
|
|
}
|
|
|
|
if (level >= 2)
|
|
|
|
pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2);
|
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InvertibleRSAFunction::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
|
|
|
|
{
|
|
|
|
return GetValueHelper<RSAFunction>(this, name, valueType, pValue).Assignable()
|
|
|
|
CRYPTOPP_GET_FUNCTION_ENTRY(Prime1)
|
|
|
|
CRYPTOPP_GET_FUNCTION_ENTRY(Prime2)
|
|
|
|
CRYPTOPP_GET_FUNCTION_ENTRY(PrivateExponent)
|
|
|
|
CRYPTOPP_GET_FUNCTION_ENTRY(ModPrime1PrivateExponent)
|
|
|
|
CRYPTOPP_GET_FUNCTION_ENTRY(ModPrime2PrivateExponent)
|
|
|
|
CRYPTOPP_GET_FUNCTION_ENTRY(MultiplicativeInverseOfPrime2ModPrime1)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
void InvertibleRSAFunction::AssignFrom(const NameValuePairs &source)
|
|
|
|
{
|
|
|
|
AssignFromHelper<RSAFunction>(this, source)
|
|
|
|
CRYPTOPP_SET_FUNCTION_ENTRY(Prime1)
|
|
|
|
CRYPTOPP_SET_FUNCTION_ENTRY(Prime2)
|
|
|
|
CRYPTOPP_SET_FUNCTION_ENTRY(PrivateExponent)
|
|
|
|
CRYPTOPP_SET_FUNCTION_ENTRY(ModPrime1PrivateExponent)
|
|
|
|
CRYPTOPP_SET_FUNCTION_ENTRY(ModPrime2PrivateExponent)
|
|
|
|
CRYPTOPP_SET_FUNCTION_ENTRY(MultiplicativeInverseOfPrime2ModPrime1)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2005-01-20 04:19:35 +00:00
|
|
|
// *****************************************************************************
|
|
|
|
|
|
|
|
Integer RSAFunction_ISO::ApplyFunction(const Integer &x) const
|
|
|
|
{
|
|
|
|
Integer t = RSAFunction::ApplyFunction(x);
|
|
|
|
return t % 16 == 12 ? t : m_n - t;
|
|
|
|
}
|
|
|
|
|
|
|
|
Integer InvertibleRSAFunction_ISO::CalculateInverse(RandomNumberGenerator &rng, const Integer &x) const
|
|
|
|
{
|
|
|
|
Integer t = InvertibleRSAFunction::CalculateInverse(rng, x);
|
|
|
|
return STDMIN(t, m_n-t);
|
|
|
|
}
|
|
|
|
|
2002-10-04 17:31:41 +00:00
|
|
|
NAMESPACE_END
|
2003-07-18 04:35:30 +00:00
|
|
|
|
|
|
|
#endif
|