2017-03-08 22:15:16 +00:00
|
|
|
// validate.h - originally written and placed in the public domain by Wei Dai
|
|
|
|
// CryptoPP::Test namespace added by JW in February 2017
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
#ifndef CRYPTOPP_VALIDATE_H
|
|
|
|
#define CRYPTOPP_VALIDATE_H
|
|
|
|
|
|
|
|
#include "cryptlib.h"
|
2017-05-20 03:29:59 +00:00
|
|
|
#include "integer.h"
|
|
|
|
#include "misc.h"
|
2017-03-08 22:15:16 +00:00
|
|
|
|
2015-12-29 12:16:44 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <iomanip>
|
2017-03-08 22:15:16 +00:00
|
|
|
#include <ctime>
|
|
|
|
#include <cctype>
|
2015-11-05 06:59:46 +00:00
|
|
|
|
2017-02-21 07:03:29 +00:00
|
|
|
NAMESPACE_BEGIN(CryptoPP)
|
|
|
|
NAMESPACE_BEGIN(Test)
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
bool ValidateAll(bool thorough);
|
|
|
|
bool TestSettings();
|
|
|
|
bool TestOS_RNG();
|
2017-02-07 13:51:58 +00:00
|
|
|
// bool TestSecRandom();
|
2017-05-10 05:28:52 +00:00
|
|
|
bool TestRandomPool();
|
2017-05-10 07:50:32 +00:00
|
|
|
#if !defined(NO_OS_DEPENDENCE)
|
2016-01-03 17:53:46 +00:00
|
|
|
bool TestAutoSeededX917();
|
2017-05-10 07:50:32 +00:00
|
|
|
#endif
|
2015-11-05 06:59:46 +00:00
|
|
|
bool TestRDRAND();
|
|
|
|
bool TestRDSEED();
|
|
|
|
|
2015-11-18 20:32:28 +00:00
|
|
|
bool ValidateBaseCode();
|
2015-11-05 06:59:46 +00:00
|
|
|
bool ValidateCRC32();
|
2016-04-24 17:24:45 +00:00
|
|
|
bool ValidateCRC32C();
|
2015-11-05 06:59:46 +00:00
|
|
|
bool ValidateAdler32();
|
|
|
|
bool ValidateMD2();
|
|
|
|
bool ValidateMD4();
|
|
|
|
bool ValidateMD5();
|
|
|
|
bool ValidateSHA();
|
|
|
|
bool ValidateSHA2();
|
|
|
|
bool ValidateTiger();
|
|
|
|
bool ValidateRIPEMD();
|
|
|
|
bool ValidatePanama();
|
|
|
|
bool ValidateWhirlpool();
|
2016-11-27 20:31:50 +00:00
|
|
|
|
2016-04-18 06:39:10 +00:00
|
|
|
bool ValidateBLAKE2s();
|
|
|
|
bool ValidateBLAKE2b();
|
2016-12-17 08:58:36 +00:00
|
|
|
bool ValidatePoly1305();
|
|
|
|
bool ValidateSipHash();
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
bool ValidateHMAC();
|
|
|
|
bool ValidateTTMAC();
|
|
|
|
|
|
|
|
bool ValidateCipherModes();
|
|
|
|
bool ValidatePBKDF();
|
|
|
|
bool ValidateHKDF();
|
|
|
|
|
|
|
|
bool ValidateDES();
|
|
|
|
bool ValidateIDEA();
|
|
|
|
bool ValidateSAFER();
|
|
|
|
bool ValidateRC2();
|
|
|
|
bool ValidateARC4();
|
|
|
|
|
|
|
|
bool ValidateRC5();
|
|
|
|
bool ValidateBlowfish();
|
|
|
|
bool ValidateThreeWay();
|
|
|
|
bool ValidateGOST();
|
|
|
|
bool ValidateSHARK();
|
|
|
|
bool ValidateSEAL();
|
|
|
|
bool ValidateCAST();
|
|
|
|
bool ValidateSquare();
|
|
|
|
bool ValidateSKIPJACK();
|
|
|
|
bool ValidateRC6();
|
|
|
|
bool ValidateMARS();
|
|
|
|
bool ValidateRijndael();
|
|
|
|
bool ValidateTwofish();
|
|
|
|
bool ValidateSerpent();
|
|
|
|
bool ValidateSHACAL2();
|
2017-04-10 14:52:40 +00:00
|
|
|
bool ValidateARIA();
|
2015-11-05 06:59:46 +00:00
|
|
|
bool ValidateCamellia();
|
|
|
|
bool ValidateSalsa();
|
|
|
|
bool ValidateSosemanuk();
|
|
|
|
bool ValidateVMAC();
|
|
|
|
bool ValidateCCM();
|
|
|
|
bool ValidateGCM();
|
|
|
|
bool ValidateCMAC();
|
|
|
|
|
|
|
|
bool ValidateBBS();
|
|
|
|
bool ValidateDH();
|
|
|
|
bool ValidateMQV();
|
2016-07-04 17:05:38 +00:00
|
|
|
bool ValidateHMQV();
|
|
|
|
bool ValidateFHMQV();
|
2015-11-05 06:59:46 +00:00
|
|
|
bool ValidateRSA();
|
|
|
|
bool ValidateElGamal();
|
|
|
|
bool ValidateDLIES();
|
|
|
|
bool ValidateNR();
|
|
|
|
bool ValidateDSA(bool thorough);
|
|
|
|
bool ValidateLUC();
|
|
|
|
bool ValidateLUC_DL();
|
|
|
|
bool ValidateLUC_DH();
|
|
|
|
bool ValidateXTR_DH();
|
|
|
|
bool ValidateRabin();
|
|
|
|
bool ValidateRW();
|
|
|
|
bool ValidateECP();
|
|
|
|
bool ValidateEC2N();
|
|
|
|
bool ValidateECDSA();
|
2016-12-13 21:20:41 +00:00
|
|
|
bool ValidateECGDSA();
|
2015-11-05 06:59:46 +00:00
|
|
|
bool ValidateESIGN();
|
|
|
|
|
2017-02-07 13:51:58 +00:00
|
|
|
bool ValidateHashDRBG();
|
|
|
|
bool ValidateHmacDRBG();
|
|
|
|
|
2017-05-06 03:16:28 +00:00
|
|
|
// If CRYPTOPP_DEBUG or CRYPTOPP_COVERAGE is in effect, then perform additional tests
|
2017-05-07 02:54:49 +00:00
|
|
|
#if (defined(CRYPTOPP_DEBUG) || defined(CRYPTOPP_COVERAGE) || defined(CRYPTOPP_VALGRIND)) && !defined(CRYPTOPP_IMPORTS)
|
2017-05-06 03:16:28 +00:00
|
|
|
# define CRYPTOPP_EXTENDED_VALIDATION 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(CRYPTOPP_EXTENDED_VALIDATION)
|
2016-12-24 09:56:34 +00:00
|
|
|
// http://github.com/weidai11/cryptopp/issues/92
|
2015-12-28 05:11:52 +00:00
|
|
|
bool TestSecBlock();
|
2016-12-24 09:56:34 +00:00
|
|
|
// http://github.com/weidai11/cryptopp/issues/64
|
2015-11-18 20:32:28 +00:00
|
|
|
bool TestPolynomialMod2();
|
2016-12-24 09:56:34 +00:00
|
|
|
// http://github.com/weidai11/cryptopp/issues/336
|
2016-12-07 01:56:57 +00:00
|
|
|
bool TestIntegerBitops();
|
2017-01-11 03:09:38 +00:00
|
|
|
// http://github.com/weidai11/cryptopp/issues/360
|
|
|
|
bool TestRounding();
|
2016-12-24 09:56:34 +00:00
|
|
|
// http://github.com/weidai11/cryptopp/issues/242
|
2016-08-21 07:18:26 +00:00
|
|
|
bool TestHuffmanCodes();
|
2016-12-24 09:56:34 +00:00
|
|
|
// http://github.com/weidai11/cryptopp/issues/346
|
|
|
|
bool TestASN1Parse();
|
2017-05-05 21:21:08 +00:00
|
|
|
// Additional tests due to no coverage
|
2017-05-06 23:20:57 +00:00
|
|
|
bool TestCompressors();
|
|
|
|
bool TestEncryptors();
|
2017-05-05 21:21:08 +00:00
|
|
|
bool TestMersenne();
|
2017-05-09 22:44:45 +00:00
|
|
|
bool TestSharing();
|
2015-11-18 20:32:28 +00:00
|
|
|
#endif
|
|
|
|
|
2017-03-25 03:24:46 +00:00
|
|
|
#if 1
|
2017-03-08 22:15:16 +00:00
|
|
|
// Coverity findings in benchmark and validation routines
|
2015-12-29 12:16:44 +00:00
|
|
|
class StreamState
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
StreamState(std::ostream& out)
|
2017-03-18 12:18:05 +00:00
|
|
|
: m_out(out), m_prec(out.precision()), m_width(out.width()), m_fmt(out.flags()), m_fill(out.fill())
|
2015-12-29 12:16:44 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~StreamState()
|
|
|
|
{
|
2017-03-18 09:08:04 +00:00
|
|
|
m_out.fill(m_fill);
|
2017-03-18 12:18:05 +00:00
|
|
|
m_out.flags(m_fmt);
|
|
|
|
m_out.width(m_width);
|
|
|
|
m_out.precision(m_prec);
|
2015-12-29 12:16:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::ostream& m_out;
|
|
|
|
std::streamsize m_prec;
|
2017-03-18 12:18:05 +00:00
|
|
|
std::streamsize m_width;
|
2017-03-18 12:11:44 +00:00
|
|
|
std::ios_base::fmtflags m_fmt;
|
|
|
|
std::ostream::char_type m_fill;
|
2015-12-29 12:16:44 +00:00
|
|
|
};
|
2017-03-18 13:48:54 +00:00
|
|
|
#endif
|
|
|
|
|
2017-03-25 03:24:46 +00:00
|
|
|
#if 0
|
2017-03-18 13:48:54 +00:00
|
|
|
class StreamState
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
StreamState(std::ostream& out)
|
|
|
|
: m_out(out), m_state(NULLPTR)
|
|
|
|
{
|
|
|
|
m_state.copyfmt(m_out);
|
|
|
|
}
|
|
|
|
|
|
|
|
~StreamState()
|
|
|
|
{
|
|
|
|
m_out.copyfmt(m_state);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::ostream& m_out;
|
|
|
|
std::ios m_state;
|
|
|
|
};
|
2017-03-25 03:24:46 +00:00
|
|
|
#endif
|
2015-12-29 12:16:44 +00:00
|
|
|
|
2017-03-08 22:15:16 +00:00
|
|
|
// Safer functions on Windows for C&A, https://github.com/weidai11/cryptopp/issues/55
|
2017-05-16 07:52:03 +00:00
|
|
|
inline std::string TimeToString(const time_t& t)
|
2017-03-08 22:15:16 +00:00
|
|
|
{
|
|
|
|
#if (CRYPTOPP_MSC_VERSION >= 1400)
|
|
|
|
tm localTime = {};
|
|
|
|
char timeBuf[64];
|
|
|
|
errno_t err;
|
|
|
|
|
|
|
|
err = ::localtime_s(&localTime, &t);
|
|
|
|
CRYPTOPP_ASSERT(err == 0);
|
|
|
|
err = ::asctime_s(timeBuf, sizeof(timeBuf), &localTime);
|
|
|
|
CRYPTOPP_ASSERT(err == 0);
|
|
|
|
|
|
|
|
std::string str(timeBuf);
|
|
|
|
#else
|
|
|
|
std::string str(::asctime(::localtime(&t)));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Cleanup whitespace
|
|
|
|
std::string::size_type pos = 0;
|
|
|
|
while (!str.empty() && std::isspace(*(str.end()-1)))
|
|
|
|
{str.erase(str.end()-1);}
|
|
|
|
while (!str.empty() && std::string::npos != (pos = str.find(" ", pos)))
|
|
|
|
{ str.erase(pos, 1); }
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2017-05-20 03:29:59 +00:00
|
|
|
// Coverity finding
|
|
|
|
template <class T, bool NON_NEGATIVE>
|
|
|
|
inline T StringToValue(const std::string& str)
|
|
|
|
{
|
|
|
|
std::istringstream iss(str);
|
|
|
|
|
|
|
|
// Arbitrary, but we need to clear a Coverity finding TAINTED_SCALAR
|
|
|
|
if (iss.str().length() > 25)
|
|
|
|
throw InvalidArgument(str + "' is too long");
|
|
|
|
|
|
|
|
T value;
|
|
|
|
iss >> std::noskipws >> value;
|
|
|
|
|
|
|
|
// Use fail(), not bad()
|
|
|
|
if (iss.fail() || !iss.eof())
|
|
|
|
throw InvalidArgument(str + "' is not a value");
|
|
|
|
|
|
|
|
if (NON_NEGATIVE && value < 0)
|
|
|
|
throw InvalidArgument(str + "' is negative");
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Coverity finding
|
|
|
|
template<>
|
|
|
|
inline int StringToValue<int, true>(const std::string& str)
|
|
|
|
{
|
|
|
|
Integer n(str.c_str());
|
|
|
|
long l = n.ConvertToLong();
|
|
|
|
|
|
|
|
int r;
|
|
|
|
if (!SafeConvert(l, r))
|
|
|
|
throw InvalidArgument(str + "' is not an integer value");
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2015-11-18 20:32:28 +00:00
|
|
|
// Functions that need a RNG; uses AES inf CFB mode with Seed.
|
2015-11-05 06:59:46 +00:00
|
|
|
CryptoPP::RandomNumberGenerator & GlobalRNG();
|
2015-11-18 20:32:28 +00:00
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
bool RunTestDataFile(const char *filename, const CryptoPP::NameValuePairs &overrideParameters=CryptoPP::g_nullNameValuePairs, bool thorough=true);
|
|
|
|
|
2017-02-21 07:03:29 +00:00
|
|
|
NAMESPACE_END // Test
|
|
|
|
NAMESPACE_END // CryptoPP
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
#endif
|