add check for invalid RSA private key given n, e, d

pull/2/head
weidai 2004-04-29 14:48:51 +00:00
parent 24caaf9e8e
commit 5fc752d268
1 changed files with 10 additions and 0 deletions

10
rsa.cpp
View File

@ -147,13 +147,20 @@ void InvertibleRSAFunction::Initialize(RandomNumberGenerator &rng, unsigned int
void InvertibleRSAFunction::Initialize(const Integer &n, const Integer &e, const Integer &d) void InvertibleRSAFunction::Initialize(const Integer &n, const Integer &e, const Integer &d)
{ {
if (n.IsEven() || e.IsEven() | d.IsEven())
throw InvalidArgument("InvertibleRSAFunction: input is not a valid RSA private key");
m_n = n; m_n = n;
m_e = e; m_e = e;
m_d = d; m_d = d;
Integer r = --(d*e); Integer r = --(d*e);
unsigned int s = 0;
while (r.IsEven()) while (r.IsEven())
{
r >>= 1; r >>= 1;
s++;
}
ModularArithmetic modn(n); ModularArithmetic modn(n);
for (Integer i = 2; ; ++i) for (Integer i = 2; ; ++i)
@ -162,6 +169,7 @@ void InvertibleRSAFunction::Initialize(const Integer &n, const Integer &e, const
if (a == 1) if (a == 1)
continue; continue;
Integer b; Integer b;
unsigned int j = 0;
while (a != -1) while (a != -1)
{ {
b = modn.Square(a); b = modn.Square(a);
@ -174,6 +182,8 @@ void InvertibleRSAFunction::Initialize(const Integer &n, const Integer &e, const
m_u = m_q.InverseMod(m_p); m_u = m_q.InverseMod(m_p);
return; return;
} }
if (++j == s)
throw InvalidArgument("InvertibleRSAFunction: input is not a valid RSA private key");
a = b; a = b;
} }
} }