Add asserts to validation routines

This commit is contained in:
Jeffrey Walton 2017-01-29 04:58:56 -05:00
parent 7c7e8aa804
commit b8adc91ce8
No known key found for this signature in database
GPG Key ID: B36AB348921B1838
8 changed files with 85 additions and 3 deletions

View File

@ -3705,13 +3705,13 @@ static void BLAKE2_NEON_Compress32(const byte* input, BLAKE2_State<word32, false
t0 = vext_u32(vget_high_u32(m2), vget_high_u32(m3), 1); \ t0 = vext_u32(vget_high_u32(m2), vget_high_u32(m3), 1); \
t1 = vzip_u32(vget_low_u32(m3), vget_low_u32(m0)).val[0]; \ t1 = vzip_u32(vget_low_u32(m3), vget_low_u32(m0)).val[0]; \
buf = vcombine_u32(t0, t1); } while(0) buf = vcombine_u32(t0, t1); } while(0)
#define vrorq_n_u32_16(x) vreinterpretq_u32_u16(vrev32q_u16(vreinterpretq_u16_u32(x))) #define vrorq_n_u32_16(x) vreinterpretq_u32_u16(vrev32q_u16(vreinterpretq_u16_u32(x)))
#define vrorq_n_u32_8(x) vsriq_n_u32(vshlq_n_u32((x), 24), (x), 8) #define vrorq_n_u32_8(x) vsriq_n_u32(vshlq_n_u32((x), 24), (x), 8)
#define vrorq_n_u32(x, c) vsriq_n_u32(vshlq_n_u32((x), 32-(c)), (x), (c)) #define vrorq_n_u32(x, c) vsriq_n_u32(vshlq_n_u32((x), 32-(c)), (x), (c))
#define BLAKE2S_G1(row1,row2,row3,row4,buf) \ #define BLAKE2S_G1(row1,row2,row3,row4,buf) \
do { \ do { \
row1 = vaddq_u32(vaddq_u32(row1, buf), row2); row4 = veorq_u32(row4, row1); \ row1 = vaddq_u32(vaddq_u32(row1, buf), row2); row4 = veorq_u32(row4, row1); \

View File

@ -61,7 +61,9 @@ bool ESIGNFunction::Validate(RandomNumberGenerator& rng, unsigned int level) con
CRYPTOPP_UNUSED(rng), CRYPTOPP_UNUSED(level); CRYPTOPP_UNUSED(rng), CRYPTOPP_UNUSED(level);
bool pass = true; bool pass = true;
pass = pass && m_n > Integer::One() && m_n.IsOdd(); pass = pass && m_n > Integer::One() && m_n.IsOdd();
CRYPTOPP_ASSERT(pass);
pass = pass && m_e >= 8 && m_e < m_n; pass = pass && m_e >= 8 && m_e < m_n;
CRYPTOPP_ASSERT(pass);
return pass; return pass;
} }
@ -192,13 +194,23 @@ Integer InvertibleESIGNFunction::CalculateRandomizedInverse(RandomNumberGenerato
bool InvertibleESIGNFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const bool InvertibleESIGNFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const
{ {
bool pass = ESIGNFunction::Validate(rng, level); bool pass = ESIGNFunction::Validate(rng, level);
CRYPTOPP_ASSERT(pass);
pass = pass && m_p > Integer::One() && m_p.IsOdd() && m_p < m_n; pass = pass && m_p > Integer::One() && m_p.IsOdd() && m_p < m_n;
CRYPTOPP_ASSERT(pass);
pass = pass && m_q > Integer::One() && m_q.IsOdd() && m_q < m_n; pass = pass && m_q > Integer::One() && m_q.IsOdd() && m_q < m_n;
CRYPTOPP_ASSERT(pass);
pass = pass && m_p.BitCount() == m_q.BitCount(); pass = pass && m_p.BitCount() == m_q.BitCount();
CRYPTOPP_ASSERT(pass);
if (level >= 1) if (level >= 1)
{
pass = pass && m_p * m_p * m_q == m_n; pass = pass && m_p * m_p * m_q == m_n;
CRYPTOPP_ASSERT(pass);
}
if (level >= 2) if (level >= 2)
{
pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2); pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2);
CRYPTOPP_ASSERT(pass);
}
return pass; return pass;
} }

15
luc.cpp
View File

@ -74,7 +74,9 @@ bool LUCFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const
CRYPTOPP_UNUSED(rng), CRYPTOPP_UNUSED(level); CRYPTOPP_UNUSED(rng), CRYPTOPP_UNUSED(level);
bool pass = true; bool pass = true;
pass = pass && m_n > Integer::One() && m_n.IsOdd(); pass = pass && m_n > Integer::One() && m_n.IsOdd();
CRYPTOPP_ASSERT(pass);
pass = pass && m_e > Integer::One() && m_e.IsOdd() && m_e < m_n; pass = pass && m_e > Integer::One() && m_e.IsOdd() && m_e < m_n;
CRYPTOPP_ASSERT(pass);
return pass; return pass;
} }
@ -177,20 +179,33 @@ Integer InvertibleLUCFunction::CalculateInverse(RandomNumberGenerator &rng, cons
bool InvertibleLUCFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const bool InvertibleLUCFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const
{ {
bool pass = LUCFunction::Validate(rng, level); bool pass = LUCFunction::Validate(rng, level);
CRYPTOPP_ASSERT(pass);
pass = pass && m_p > Integer::One() && m_p.IsOdd() && m_p < m_n; pass = pass && m_p > Integer::One() && m_p.IsOdd() && m_p < m_n;
CRYPTOPP_ASSERT(pass);
pass = pass && m_q > Integer::One() && m_q.IsOdd() && m_q < m_n; pass = pass && m_q > Integer::One() && m_q.IsOdd() && m_q < m_n;
CRYPTOPP_ASSERT(pass);
pass = pass && m_u.IsPositive() && m_u < m_p; pass = pass && m_u.IsPositive() && m_u < m_p;
CRYPTOPP_ASSERT(pass);
if (level >= 1) if (level >= 1)
{ {
pass = pass && m_p * m_q == m_n; pass = pass && m_p * m_q == m_n;
CRYPTOPP_ASSERT(pass);
pass = pass && RelativelyPrime(m_e, m_p+1); pass = pass && RelativelyPrime(m_e, m_p+1);
CRYPTOPP_ASSERT(pass);
pass = pass && RelativelyPrime(m_e, m_p-1); pass = pass && RelativelyPrime(m_e, m_p-1);
CRYPTOPP_ASSERT(pass);
pass = pass && RelativelyPrime(m_e, m_q+1); pass = pass && RelativelyPrime(m_e, m_q+1);
CRYPTOPP_ASSERT(pass);
pass = pass && RelativelyPrime(m_e, m_q-1); pass = pass && RelativelyPrime(m_e, m_q-1);
CRYPTOPP_ASSERT(pass);
pass = pass && m_u * m_q % m_p == 1; pass = pass && m_u * m_q % m_p == 1;
CRYPTOPP_ASSERT(pass);
} }
if (level >= 2) if (level >= 2)
{
pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2); pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2);
CRYPTOPP_ASSERT(pass);
}
return pass; return pass;
} }

View File

@ -44,10 +44,16 @@ bool RabinFunction::Validate(RandomNumberGenerator& /*rng*/, unsigned int level)
{ {
bool pass = true; bool pass = true;
pass = pass && m_n > Integer::One() && m_n%4 == 1; pass = pass && m_n > Integer::One() && m_n%4 == 1;
CRYPTOPP_ASSERT(pass);
pass = pass && m_r > Integer::One() && m_r < m_n; pass = pass && m_r > Integer::One() && m_r < m_n;
CRYPTOPP_ASSERT(pass);
pass = pass && m_s > Integer::One() && m_s < m_n; pass = pass && m_s > Integer::One() && m_s < m_n;
CRYPTOPP_ASSERT(pass);
if (level >= 1) if (level >= 1)
{
pass = pass && Jacobi(m_r, m_n) == -1 && Jacobi(m_s, m_n) == -1; pass = pass && Jacobi(m_r, m_n) == -1 && Jacobi(m_s, m_n) == -1;
CRYPTOPP_ASSERT(pass);
}
return pass; return pass;
} }
@ -184,20 +190,33 @@ Integer InvertibleRabinFunction::CalculateInverse(RandomNumberGenerator &rng, co
bool InvertibleRabinFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const bool InvertibleRabinFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const
{ {
bool pass = RabinFunction::Validate(rng, level); bool pass = RabinFunction::Validate(rng, level);
CRYPTOPP_ASSERT(pass);
pass = pass && m_p > Integer::One() && m_p%4 == 3 && m_p < m_n; pass = pass && m_p > Integer::One() && m_p%4 == 3 && m_p < m_n;
CRYPTOPP_ASSERT(pass);
pass = pass && m_q > Integer::One() && m_q%4 == 3 && m_q < m_n; pass = pass && m_q > Integer::One() && m_q%4 == 3 && m_q < m_n;
CRYPTOPP_ASSERT(pass);
pass = pass && m_u.IsPositive() && m_u < m_p; pass = pass && m_u.IsPositive() && m_u < m_p;
CRYPTOPP_ASSERT(pass);
if (level >= 1) if (level >= 1)
{ {
pass = pass && m_p * m_q == m_n; pass = pass && m_p * m_q == m_n;
CRYPTOPP_ASSERT(pass);
pass = pass && m_u * m_q % m_p == 1; pass = pass && m_u * m_q % m_p == 1;
CRYPTOPP_ASSERT(pass);
pass = pass && Jacobi(m_r, m_p) == 1; pass = pass && Jacobi(m_r, m_p) == 1;
CRYPTOPP_ASSERT(pass);
pass = pass && Jacobi(m_r, m_q) == -1; pass = pass && Jacobi(m_r, m_q) == -1;
CRYPTOPP_ASSERT(pass);
pass = pass && Jacobi(m_s, m_p) == -1; pass = pass && Jacobi(m_s, m_p) == -1;
CRYPTOPP_ASSERT(pass);
pass = pass && Jacobi(m_s, m_q) == 1; pass = pass && Jacobi(m_s, m_q) == 1;
CRYPTOPP_ASSERT(pass);
} }
if (level >= 2) if (level >= 2)
{
pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2); pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2);
CRYPTOPP_ASSERT(pass);
}
return pass; return pass;
} }

16
rsa.cpp
View File

@ -73,7 +73,9 @@ bool RSAFunction::Validate(RandomNumberGenerator& rng, unsigned int level) const
bool pass = true; bool pass = true;
pass = pass && m_n > Integer::One() && m_n.IsOdd(); pass = pass && m_n > Integer::One() && m_n.IsOdd();
CRYPTOPP_ASSERT(pass);
pass = pass && m_e > Integer::One() && m_e.IsOdd() && m_e < m_n; pass = pass && m_e > Integer::One() && m_e.IsOdd() && m_e < m_n;
CRYPTOPP_ASSERT(pass);
return pass; return pass;
} }
@ -247,21 +249,35 @@ Integer InvertibleRSAFunction::CalculateInverse(RandomNumberGenerator &rng, cons
bool InvertibleRSAFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const bool InvertibleRSAFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const
{ {
bool pass = RSAFunction::Validate(rng, level); bool pass = RSAFunction::Validate(rng, level);
CRYPTOPP_ASSERT(pass);
pass = pass && m_p > Integer::One() && m_p.IsOdd() && m_p < m_n; pass = pass && m_p > Integer::One() && m_p.IsOdd() && m_p < m_n;
CRYPTOPP_ASSERT(pass);
pass = pass && m_q > Integer::One() && m_q.IsOdd() && m_q < m_n; pass = pass && m_q > Integer::One() && m_q.IsOdd() && m_q < m_n;
CRYPTOPP_ASSERT(pass);
pass = pass && m_d > Integer::One() && m_d.IsOdd() && m_d < m_n; pass = pass && m_d > Integer::One() && m_d.IsOdd() && m_d < m_n;
CRYPTOPP_ASSERT(pass);
pass = pass && m_dp > Integer::One() && m_dp.IsOdd() && m_dp < m_p; pass = pass && m_dp > Integer::One() && m_dp.IsOdd() && m_dp < m_p;
CRYPTOPP_ASSERT(pass);
pass = pass && m_dq > Integer::One() && m_dq.IsOdd() && m_dq < m_q; pass = pass && m_dq > Integer::One() && m_dq.IsOdd() && m_dq < m_q;
CRYPTOPP_ASSERT(pass);
pass = pass && m_u.IsPositive() && m_u < m_p; pass = pass && m_u.IsPositive() && m_u < m_p;
CRYPTOPP_ASSERT(pass);
if (level >= 1) if (level >= 1)
{ {
pass = pass && m_p * m_q == m_n; pass = pass && m_p * m_q == m_n;
CRYPTOPP_ASSERT(pass);
pass = pass && m_e*m_d % LCM(m_p-1, m_q-1) == 1; pass = pass && m_e*m_d % LCM(m_p-1, m_q-1) == 1;
CRYPTOPP_ASSERT(pass);
pass = pass && m_dp == m_d%(m_p-1) && m_dq == m_d%(m_q-1); pass = pass && m_dp == m_d%(m_p-1) && m_dq == m_d%(m_q-1);
CRYPTOPP_ASSERT(pass);
pass = pass && m_u * m_q % m_p == 1; pass = pass && m_u * m_q % m_p == 1;
CRYPTOPP_ASSERT(pass);
} }
if (level >= 2) if (level >= 2)
{
pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2); pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2);
CRYPTOPP_ASSERT(pass);
}
return pass; return pass;
} }

2
rsa.h
View File

@ -115,7 +115,7 @@ public:
// GeneratableCryptoMaterial // GeneratableCryptoMaterial
bool Validate(RandomNumberGenerator &rng, unsigned int level) const; bool Validate(RandomNumberGenerator &rng, unsigned int level) const;
/*! parameters: (ModulusSize, PublicExponent (default 17)) */ // parameters: (ModulusSize, PublicExponent (default 17))
void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg); void GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg);
bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const; bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const;
void AssignFrom(const NameValuePairs &source); void AssignFrom(const NameValuePairs &source);

10
rw.cpp
View File

@ -75,6 +75,7 @@ bool RWFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const
CRYPTOPP_UNUSED(rng), CRYPTOPP_UNUSED(level); CRYPTOPP_UNUSED(rng), CRYPTOPP_UNUSED(level);
bool pass = true; bool pass = true;
pass = pass && m_n > Integer::One() && m_n%8 == 5; pass = pass && m_n > Integer::One() && m_n%8 == 5;
CRYPTOPP_ASSERT(pass);
return pass; return pass;
} }
@ -252,16 +253,25 @@ Integer InvertibleRWFunction::CalculateInverse(RandomNumberGenerator &rng, const
bool InvertibleRWFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const bool InvertibleRWFunction::Validate(RandomNumberGenerator &rng, unsigned int level) const
{ {
bool pass = RWFunction::Validate(rng, level); bool pass = RWFunction::Validate(rng, level);
CRYPTOPP_ASSERT(pass);
pass = pass && m_p > Integer::One() && m_p%8 == 3 && m_p < m_n; pass = pass && m_p > Integer::One() && m_p%8 == 3 && m_p < m_n;
CRYPTOPP_ASSERT(pass);
pass = pass && m_q > Integer::One() && m_q%8 == 7 && m_q < m_n; pass = pass && m_q > Integer::One() && m_q%8 == 7 && m_q < m_n;
CRYPTOPP_ASSERT(pass);
pass = pass && m_u.IsPositive() && m_u < m_p; pass = pass && m_u.IsPositive() && m_u < m_p;
CRYPTOPP_ASSERT(pass);
if (level >= 1) if (level >= 1)
{ {
pass = pass && m_p * m_q == m_n; pass = pass && m_p * m_q == m_n;
CRYPTOPP_ASSERT(pass);
pass = pass && m_u * m_q % m_p == 1; pass = pass && m_u * m_q % m_p == 1;
CRYPTOPP_ASSERT(pass);
} }
if (level >= 2) if (level >= 2)
{
pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2); pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2);
CRYPTOPP_ASSERT(pass);
}
return pass; return pass;
} }

View File

@ -45,16 +45,26 @@ bool XTR_DH::Validate(RandomNumberGenerator &rng, unsigned int level) const
{ {
bool pass = true; bool pass = true;
pass = pass && m_p > Integer::One() && m_p.IsOdd(); pass = pass && m_p > Integer::One() && m_p.IsOdd();
CRYPTOPP_ASSERT(pass);
pass = pass && m_q > Integer::One() && m_q.IsOdd(); pass = pass && m_q > Integer::One() && m_q.IsOdd();
CRYPTOPP_ASSERT(pass);
GFP2Element three = GFP2_ONB<ModularArithmetic>(m_p).ConvertIn(3); GFP2Element three = GFP2_ONB<ModularArithmetic>(m_p).ConvertIn(3);
CRYPTOPP_ASSERT(pass);
pass = pass && !(m_g.c1.IsNegative() || m_g.c2.IsNegative() || m_g.c1 >= m_p || m_g.c2 >= m_p || m_g == three); pass = pass && !(m_g.c1.IsNegative() || m_g.c2.IsNegative() || m_g.c1 >= m_p || m_g.c2 >= m_p || m_g == three);
CRYPTOPP_ASSERT(pass);
if (level >= 1) if (level >= 1)
{
pass = pass && ((m_p.Squared()-m_p+1)%m_q).IsZero(); pass = pass && ((m_p.Squared()-m_p+1)%m_q).IsZero();
CRYPTOPP_ASSERT(pass);
}
if (level >= 2) if (level >= 2)
{ {
pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2); pass = pass && VerifyPrime(rng, m_p, level-2) && VerifyPrime(rng, m_q, level-2);
CRYPTOPP_ASSERT(pass);
pass = pass && XTR_Exponentiate(m_g, (m_p.Squared()-m_p+1)/m_q, m_p) != three; pass = pass && XTR_Exponentiate(m_g, (m_p.Squared()-m_p+1)/m_q, m_p) != three;
CRYPTOPP_ASSERT(pass);
pass = pass && XTR_Exponentiate(m_g, m_q, m_p) == three; pass = pass && XTR_Exponentiate(m_g, m_q, m_p) == three;
CRYPTOPP_ASSERT(pass);
} }
return pass; return pass;
} }