2017-01-27 12:05:45 +00:00
|
|
|
// datatest.cpp - originally written and placed in the public domain by Wei Dai
|
2017-02-21 07:03:29 +00:00
|
|
|
// CryptoPP::Test namespace added by JW in February 2017
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
#define CRYPTOPP_DEFAULT_NO_DLL
|
|
|
|
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
|
|
|
|
|
|
|
|
#include "cryptlib.h"
|
|
|
|
#include "factory.h"
|
|
|
|
#include "integer.h"
|
|
|
|
#include "filters.h"
|
|
|
|
#include "hex.h"
|
|
|
|
#include "randpool.h"
|
|
|
|
#include "files.h"
|
|
|
|
#include "trunhash.h"
|
|
|
|
#include "queue.h"
|
|
|
|
#include "smartptr.h"
|
|
|
|
#include "validate.h"
|
|
|
|
#include "hkdf.h"
|
|
|
|
#include "stdcpp.h"
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
// Aggressive stack checking with VS2005 SP1 and above.
|
2017-05-11 16:05:57 +00:00
|
|
|
#if (_MSC_FULL_VER >= 140050727)
|
2015-11-05 06:59:46 +00:00
|
|
|
# pragma strict_gs_check (on)
|
|
|
|
#endif
|
|
|
|
|
2015-11-18 20:32:28 +00:00
|
|
|
#if defined(__COVERITY__)
|
2016-07-24 23:28:01 +00:00
|
|
|
extern "C" void __coverity_tainted_data_sanitize__(void *);
|
2015-11-18 20:32:28 +00:00
|
|
|
#endif
|
|
|
|
|
2017-02-21 07:03:29 +00:00
|
|
|
NAMESPACE_BEGIN(CryptoPP)
|
|
|
|
NAMESPACE_BEGIN(Test)
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
typedef std::map<std::string, std::string> TestData;
|
|
|
|
static bool s_thorough = false;
|
|
|
|
|
|
|
|
class TestFailure : public Exception
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TestFailure() : Exception(OTHER_ERROR, "Validation test failed") {}
|
|
|
|
};
|
|
|
|
|
2017-03-01 11:10:06 +00:00
|
|
|
static const TestData *s_currentTestData = NULLPTR;
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
static void OutputTestData(const TestData &v)
|
|
|
|
{
|
|
|
|
for (TestData::const_iterator i = v.begin(); i != v.end(); ++i)
|
|
|
|
{
|
2017-02-21 07:03:29 +00:00
|
|
|
std::cerr << i->first << ": " << i->second << std::endl;
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SignalTestFailure()
|
|
|
|
{
|
|
|
|
OutputTestData(*s_currentTestData);
|
|
|
|
throw TestFailure();
|
|
|
|
}
|
|
|
|
|
2016-04-09 23:32:57 +00:00
|
|
|
static void SignalUnknownAlgorithmError(const std::string& algType)
|
|
|
|
{
|
|
|
|
OutputTestData(*s_currentTestData);
|
|
|
|
throw Exception(Exception::OTHER_ERROR, "Unknown algorithm " + algType + " during validation test");
|
|
|
|
}
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
static void SignalTestError()
|
|
|
|
{
|
|
|
|
OutputTestData(*s_currentTestData);
|
|
|
|
throw Exception(Exception::OTHER_ERROR, "Unexpected error during validation test");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DataExists(const TestData &data, const char *name)
|
|
|
|
{
|
|
|
|
TestData::const_iterator i = data.find(name);
|
|
|
|
return (i != data.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string & GetRequiredDatum(const TestData &data, const char *name)
|
|
|
|
{
|
|
|
|
TestData::const_iterator i = data.find(name);
|
|
|
|
if (i == data.end())
|
|
|
|
SignalTestError();
|
|
|
|
return i->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RandomizedTransfer(BufferedTransformation &source, BufferedTransformation &target, bool finish, const std::string &channel=DEFAULT_CHANNEL)
|
|
|
|
{
|
|
|
|
while (source.MaxRetrievable() > (finish ? 0 : 4096))
|
|
|
|
{
|
|
|
|
byte buf[4096+64];
|
2017-02-21 07:03:29 +00:00
|
|
|
size_t start = Test::GlobalRNG().GenerateWord32(0, 63);
|
|
|
|
size_t len = Test::GlobalRNG().GenerateWord32(1, UnsignedMin(4096U, 3*source.MaxRetrievable()/2));
|
2015-11-05 06:59:46 +00:00
|
|
|
len = source.Get(buf+start, len);
|
|
|
|
target.ChannelPut(channel, buf+start, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PutDecodedDatumInto(const TestData &data, const char *name, BufferedTransformation &target)
|
|
|
|
{
|
|
|
|
std::string s1 = GetRequiredDatum(data, name), s2;
|
|
|
|
ByteQueue q;
|
|
|
|
|
|
|
|
while (!s1.empty())
|
|
|
|
{
|
|
|
|
while (s1[0] == ' ')
|
|
|
|
{
|
|
|
|
s1 = s1.substr(1);
|
|
|
|
if (s1.empty())
|
|
|
|
goto end; // avoid invalid read if s1 is empty
|
|
|
|
}
|
|
|
|
|
|
|
|
int repeat = 1;
|
|
|
|
if (s1[0] == 'r')
|
|
|
|
{
|
2017-05-12 07:50:37 +00:00
|
|
|
repeat = ::atoi(s1.c_str()+1);
|
2015-11-05 06:59:46 +00:00
|
|
|
s1 = s1.substr(s1.find(' ')+1);
|
|
|
|
}
|
2016-07-24 23:28:01 +00:00
|
|
|
|
2017-05-12 07:50:37 +00:00
|
|
|
s2.clear();
|
2015-11-05 06:59:46 +00:00
|
|
|
if (s1[0] == '\"')
|
|
|
|
{
|
|
|
|
s2 = s1.substr(1, s1.find('\"', 1)-1);
|
|
|
|
s1 = s1.substr(s2.length() + 2);
|
|
|
|
}
|
|
|
|
else if (s1.substr(0, 2) == "0x")
|
|
|
|
{
|
|
|
|
StringSource(s1.substr(2, s1.find(' ')), true, new HexDecoder(new StringSink(s2)));
|
|
|
|
s1 = s1.substr(STDMIN(s1.find(' '), s1.length()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
StringSource(s1.substr(0, s1.find(' ')), true, new HexDecoder(new StringSink(s2)));
|
|
|
|
s1 = s1.substr(STDMIN(s1.find(' '), s1.length()));
|
|
|
|
}
|
|
|
|
|
|
|
|
while (repeat--)
|
|
|
|
{
|
|
|
|
q.Put((const byte *)s2.data(), s2.size());
|
|
|
|
RandomizedTransfer(q, target, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
end:
|
|
|
|
RandomizedTransfer(q, target, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string GetDecodedDatum(const TestData &data, const char *name)
|
|
|
|
{
|
|
|
|
std::string s;
|
|
|
|
PutDecodedDatumInto(data, name, StringSink(s).Ref());
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string GetOptionalDecodedDatum(const TestData &data, const char *name)
|
|
|
|
{
|
|
|
|
std::string s;
|
|
|
|
if (DataExists(data, name))
|
|
|
|
PutDecodedDatumInto(data, name, StringSink(s).Ref());
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
class TestDataNameValuePairs : public NameValuePairs
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TestDataNameValuePairs(const TestData &data) : m_data(data) {}
|
|
|
|
|
|
|
|
virtual bool GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
|
|
|
|
{
|
|
|
|
TestData::const_iterator i = m_data.find(name);
|
|
|
|
if (i == m_data.end())
|
|
|
|
{
|
|
|
|
if (std::string(name) == Name::DigestSize() && valueType == typeid(int))
|
|
|
|
{
|
|
|
|
i = m_data.find("MAC");
|
|
|
|
if (i == m_data.end())
|
|
|
|
i = m_data.find("Digest");
|
|
|
|
if (i == m_data.end())
|
|
|
|
return false;
|
|
|
|
|
2017-02-21 07:03:29 +00:00
|
|
|
m_temp.clear();
|
2015-11-05 06:59:46 +00:00
|
|
|
PutDecodedDatumInto(m_data, i->first.c_str(), StringSink(m_temp).Ref());
|
|
|
|
*reinterpret_cast<int *>(pValue) = (int)m_temp.size();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
2016-07-24 23:28:01 +00:00
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
const std::string &value = i->second;
|
2016-07-24 23:28:01 +00:00
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
if (valueType == typeid(int))
|
|
|
|
*reinterpret_cast<int *>(pValue) = atoi(value.c_str());
|
|
|
|
else if (valueType == typeid(Integer))
|
|
|
|
*reinterpret_cast<Integer *>(pValue) = Integer((std::string(value) + "h").c_str());
|
|
|
|
else if (valueType == typeid(ConstByteArrayParameter))
|
|
|
|
{
|
2017-02-21 07:03:29 +00:00
|
|
|
m_temp.clear();
|
2015-11-05 06:59:46 +00:00
|
|
|
PutDecodedDatumInto(m_data, name, StringSink(m_temp).Ref());
|
|
|
|
reinterpret_cast<ConstByteArrayParameter *>(pValue)->Assign((const byte *)m_temp.data(), m_temp.size(), false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw ValueTypeMismatch(name, typeid(std::string), valueType);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const TestData &m_data;
|
|
|
|
mutable std::string m_temp;
|
|
|
|
};
|
|
|
|
|
|
|
|
void TestKeyPairValidAndConsistent(CryptoMaterial &pub, const CryptoMaterial &priv)
|
|
|
|
{
|
|
|
|
// "!!" converts between bool <-> integral.
|
2017-02-21 07:03:29 +00:00
|
|
|
if (!pub.Validate(Test::GlobalRNG(), 2U+!!s_thorough))
|
2015-11-05 06:59:46 +00:00
|
|
|
SignalTestFailure();
|
2017-02-21 07:03:29 +00:00
|
|
|
if (!priv.Validate(Test::GlobalRNG(), 2U+!!s_thorough))
|
2015-11-05 06:59:46 +00:00
|
|
|
SignalTestFailure();
|
|
|
|
|
|
|
|
ByteQueue bq1, bq2;
|
|
|
|
pub.Save(bq1);
|
|
|
|
pub.AssignFrom(priv);
|
|
|
|
pub.Save(bq2);
|
|
|
|
if (bq1 != bq2)
|
|
|
|
SignalTestFailure();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestSignatureScheme(TestData &v)
|
|
|
|
{
|
|
|
|
std::string name = GetRequiredDatum(v, "Name");
|
|
|
|
std::string test = GetRequiredDatum(v, "Test");
|
|
|
|
|
|
|
|
member_ptr<PK_Signer> signer(ObjectFactoryRegistry<PK_Signer>::Registry().CreateObject(name.c_str()));
|
|
|
|
member_ptr<PK_Verifier> verifier(ObjectFactoryRegistry<PK_Verifier>::Registry().CreateObject(name.c_str()));
|
|
|
|
|
|
|
|
TestDataNameValuePairs pairs(v);
|
|
|
|
|
|
|
|
if (test == "GenerateKey")
|
|
|
|
{
|
2017-02-21 07:03:29 +00:00
|
|
|
signer->AccessPrivateKey().GenerateRandom(Test::GlobalRNG(), pairs);
|
2015-11-05 06:59:46 +00:00
|
|
|
verifier->AccessPublicKey().AssignFrom(signer->AccessPrivateKey());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string keyFormat = GetRequiredDatum(v, "KeyFormat");
|
|
|
|
|
|
|
|
if (keyFormat == "DER")
|
|
|
|
verifier->AccessMaterial().Load(StringStore(GetDecodedDatum(v, "PublicKey")).Ref());
|
|
|
|
else if (keyFormat == "Component")
|
|
|
|
verifier->AccessMaterial().AssignFrom(pairs);
|
|
|
|
|
|
|
|
if (test == "Verify" || test == "NotVerify")
|
|
|
|
{
|
2017-03-01 11:10:06 +00:00
|
|
|
SignatureVerificationFilter verifierFilter(*verifier, NULLPTR, SignatureVerificationFilter::SIGNATURE_AT_BEGIN);
|
2015-11-05 06:59:46 +00:00
|
|
|
PutDecodedDatumInto(v, "Signature", verifierFilter);
|
|
|
|
PutDecodedDatumInto(v, "Message", verifierFilter);
|
|
|
|
verifierFilter.MessageEnd();
|
|
|
|
if (verifierFilter.GetLastResult() == (test == "NotVerify"))
|
|
|
|
SignalTestFailure();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (test == "PublicKeyValid")
|
|
|
|
{
|
2017-02-21 07:03:29 +00:00
|
|
|
if (!verifier->GetMaterial().Validate(Test::GlobalRNG(), 3))
|
2015-11-05 06:59:46 +00:00
|
|
|
SignalTestFailure();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keyFormat == "DER")
|
|
|
|
signer->AccessMaterial().Load(StringStore(GetDecodedDatum(v, "PrivateKey")).Ref());
|
|
|
|
else if (keyFormat == "Component")
|
|
|
|
signer->AccessMaterial().AssignFrom(pairs);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (test == "GenerateKey" || test == "KeyPairValidAndConsistent")
|
|
|
|
{
|
|
|
|
TestKeyPairValidAndConsistent(verifier->AccessMaterial(), signer->GetMaterial());
|
2017-03-01 11:10:06 +00:00
|
|
|
SignatureVerificationFilter verifierFilter(*verifier, NULLPTR, SignatureVerificationFilter::THROW_EXCEPTION);
|
2015-11-05 06:59:46 +00:00
|
|
|
verifierFilter.Put((const byte *)"abc", 3);
|
2017-02-21 07:03:29 +00:00
|
|
|
StringSource ss("abc", true, new SignerFilter(Test::GlobalRNG(), *signer, new Redirector(verifierFilter)));
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
else if (test == "Sign")
|
|
|
|
{
|
2017-02-21 07:03:29 +00:00
|
|
|
SignerFilter f(Test::GlobalRNG(), *signer, new HexEncoder(new FileSink(std::cout)));
|
2015-11-05 06:59:46 +00:00
|
|
|
StringSource ss(GetDecodedDatum(v, "Message"), true, new Redirector(f));
|
|
|
|
SignalTestFailure();
|
|
|
|
}
|
|
|
|
else if (test == "DeterministicSign")
|
|
|
|
{
|
2016-04-10 03:50:33 +00:00
|
|
|
// This test is specialized for RFC 6979. The RFC is a drop-in replacement
|
|
|
|
// for DSA and ECDSA, and access to the seed or secret is not needed. If
|
|
|
|
// additional determinsitic signatures are added, then the test harness will
|
|
|
|
// likely need to be extended.
|
2017-02-21 07:03:29 +00:00
|
|
|
std::string signature;
|
|
|
|
SignerFilter f(Test::GlobalRNG(), *signer, new StringSink(signature));
|
2016-04-10 03:50:33 +00:00
|
|
|
StringSource ss(GetDecodedDatum(v, "Message"), true, new Redirector(f));
|
2016-12-11 06:02:00 +00:00
|
|
|
|
2016-04-10 07:02:02 +00:00
|
|
|
if (GetDecodedDatum(v, "Signature") != signature)
|
|
|
|
SignalTestFailure();
|
2016-12-11 06:02:00 +00:00
|
|
|
|
2016-04-10 07:02:02 +00:00
|
|
|
return;
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
else if (test == "RandomSign")
|
|
|
|
{
|
|
|
|
SignalTestError();
|
2016-09-16 15:27:15 +00:00
|
|
|
CRYPTOPP_ASSERT(false); // TODO: implement
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SignalTestError();
|
2016-09-16 15:27:15 +00:00
|
|
|
CRYPTOPP_ASSERT(false);
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestAsymmetricCipher(TestData &v)
|
|
|
|
{
|
|
|
|
std::string name = GetRequiredDatum(v, "Name");
|
|
|
|
std::string test = GetRequiredDatum(v, "Test");
|
|
|
|
|
|
|
|
member_ptr<PK_Encryptor> encryptor(ObjectFactoryRegistry<PK_Encryptor>::Registry().CreateObject(name.c_str()));
|
|
|
|
member_ptr<PK_Decryptor> decryptor(ObjectFactoryRegistry<PK_Decryptor>::Registry().CreateObject(name.c_str()));
|
|
|
|
|
|
|
|
std::string keyFormat = GetRequiredDatum(v, "KeyFormat");
|
|
|
|
|
|
|
|
if (keyFormat == "DER")
|
|
|
|
{
|
|
|
|
decryptor->AccessMaterial().Load(StringStore(GetDecodedDatum(v, "PrivateKey")).Ref());
|
|
|
|
encryptor->AccessMaterial().Load(StringStore(GetDecodedDatum(v, "PublicKey")).Ref());
|
|
|
|
}
|
|
|
|
else if (keyFormat == "Component")
|
|
|
|
{
|
|
|
|
TestDataNameValuePairs pairs(v);
|
|
|
|
decryptor->AccessMaterial().AssignFrom(pairs);
|
|
|
|
encryptor->AccessMaterial().AssignFrom(pairs);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (test == "DecryptMatch")
|
|
|
|
{
|
|
|
|
std::string decrypted, expected = GetDecodedDatum(v, "Plaintext");
|
2017-02-21 07:03:29 +00:00
|
|
|
StringSource ss(GetDecodedDatum(v, "Ciphertext"), true, new PK_DecryptorFilter(Test::GlobalRNG(), *decryptor, new StringSink(decrypted)));
|
2015-11-05 06:59:46 +00:00
|
|
|
if (decrypted != expected)
|
|
|
|
SignalTestFailure();
|
|
|
|
}
|
|
|
|
else if (test == "KeyPairValidAndConsistent")
|
|
|
|
{
|
|
|
|
TestKeyPairValidAndConsistent(encryptor->AccessMaterial(), decryptor->GetMaterial());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SignalTestError();
|
2016-09-16 15:27:15 +00:00
|
|
|
CRYPTOPP_ASSERT(false);
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestSymmetricCipher(TestData &v, const NameValuePairs &overrideParameters)
|
|
|
|
{
|
|
|
|
std::string name = GetRequiredDatum(v, "Name");
|
|
|
|
std::string test = GetRequiredDatum(v, "Test");
|
|
|
|
|
|
|
|
std::string key = GetDecodedDatum(v, "Key");
|
|
|
|
std::string plaintext = GetDecodedDatum(v, "Plaintext");
|
|
|
|
|
|
|
|
TestDataNameValuePairs testDataPairs(v);
|
|
|
|
CombinedNameValuePairs pairs(overrideParameters, testDataPairs);
|
|
|
|
|
2017-05-10 17:50:36 +00:00
|
|
|
if (test == "Encrypt" || test == "EncryptXorDigest" || test == "Resync" || test == "EncryptionMCT" || test == "DecryptionMCT")
|
2015-11-05 06:59:46 +00:00
|
|
|
{
|
|
|
|
static member_ptr<SymmetricCipher> encryptor, decryptor;
|
|
|
|
static std::string lastName;
|
|
|
|
|
|
|
|
if (name != lastName)
|
|
|
|
{
|
|
|
|
encryptor.reset(ObjectFactoryRegistry<SymmetricCipher, ENCRYPTION>::Registry().CreateObject(name.c_str()));
|
|
|
|
decryptor.reset(ObjectFactoryRegistry<SymmetricCipher, DECRYPTION>::Registry().CreateObject(name.c_str()));
|
|
|
|
lastName = name;
|
|
|
|
}
|
|
|
|
|
2017-05-10 19:26:36 +00:00
|
|
|
// Most block ciphers don't specify BlockSize. Kalyna and Threefish use it.
|
2017-05-10 17:50:36 +00:00
|
|
|
int blockSize = pairs.GetIntValueWithDefault(Name::BlockSize(), 0);
|
2017-05-04 23:11:24 +00:00
|
|
|
|
2017-05-10 19:26:36 +00:00
|
|
|
// Most block ciphers don't specify BlockPaddingScheme. Kalyna uses it in test vectors.
|
|
|
|
// 0 is NoPadding, 1 is ZerosPadding, 2 is PkcsPadding, 3 is OneAndZerosPadding, etc
|
|
|
|
// Note: The machinery is wired such that paddingScheme is effectively latched. An
|
|
|
|
// old paddingScheme may be unintentionally used in a subsequent test.
|
|
|
|
int paddingScheme = pairs.GetIntValueWithDefault(Name::BlockPaddingScheme(), 0);
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
ConstByteArrayParameter iv;
|
2017-05-06 04:24:35 +00:00
|
|
|
if (pairs.GetValue(Name::IV(), iv) && iv.size() != encryptor->IVSize() && (int)iv.size() != blockSize)
|
2015-11-05 06:59:46 +00:00
|
|
|
SignalTestFailure();
|
|
|
|
|
|
|
|
if (test == "Resync")
|
|
|
|
{
|
|
|
|
encryptor->Resynchronize(iv.begin(), (int)iv.size());
|
|
|
|
decryptor->Resynchronize(iv.begin(), (int)iv.size());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
encryptor->SetKey((const byte *)key.data(), key.size(), pairs);
|
|
|
|
decryptor->SetKey((const byte *)key.data(), key.size(), pairs);
|
|
|
|
}
|
|
|
|
|
|
|
|
int seek = pairs.GetIntValueWithDefault("Seek", 0);
|
|
|
|
if (seek)
|
|
|
|
{
|
|
|
|
encryptor->Seek(seek);
|
|
|
|
decryptor->Seek(seek);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string encrypted, xorDigest, ciphertext, ciphertextXorDigest;
|
|
|
|
if (test == "EncryptionMCT" || test == "DecryptionMCT")
|
|
|
|
{
|
|
|
|
SymmetricCipher *cipher = encryptor.get();
|
|
|
|
SecByteBlock buf((byte *)plaintext.data(), plaintext.size()), keybuf((byte *)key.data(), key.size());
|
|
|
|
|
|
|
|
if (test == "DecryptionMCT")
|
|
|
|
{
|
|
|
|
cipher = decryptor.get();
|
|
|
|
ciphertext = GetDecodedDatum(v, "Ciphertext");
|
|
|
|
buf.Assign((byte *)ciphertext.data(), ciphertext.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i=0; i<400; i++)
|
|
|
|
{
|
|
|
|
encrypted.reserve(10000 * plaintext.size());
|
|
|
|
for (int j=0; j<10000; j++)
|
|
|
|
{
|
|
|
|
cipher->ProcessString(buf.begin(), buf.size());
|
|
|
|
encrypted.append((char *)buf.begin(), buf.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
encrypted.erase(0, encrypted.size() - keybuf.size());
|
|
|
|
xorbuf(keybuf.begin(), (const byte *)encrypted.data(), keybuf.size());
|
|
|
|
cipher->SetKey(keybuf, keybuf.size());
|
|
|
|
}
|
|
|
|
encrypted.assign((char *)buf.begin(), buf.size());
|
|
|
|
ciphertext = GetDecodedDatum(v, test == "EncryptionMCT" ? "Ciphertext" : "Plaintext");
|
|
|
|
if (encrypted != ciphertext)
|
|
|
|
{
|
2017-05-04 23:11:24 +00:00
|
|
|
std::cout << "\nincorrectly encrypted: ";
|
2015-11-05 06:59:46 +00:00
|
|
|
StringSource xx(encrypted, false, new HexEncoder(new FileSink(std::cout)));
|
|
|
|
xx.Pump(256); xx.Flush(false);
|
|
|
|
std::cout << "\n";
|
|
|
|
SignalTestFailure();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-10 19:26:36 +00:00
|
|
|
StreamTransformationFilter encFilter(*encryptor, new StringSink(encrypted),
|
|
|
|
static_cast<BlockPaddingSchemeDef::BlockPaddingScheme>(paddingScheme));
|
2015-11-05 06:59:46 +00:00
|
|
|
RandomizedTransfer(StringStore(plaintext).Ref(), encFilter, true);
|
|
|
|
encFilter.MessageEnd();
|
|
|
|
/*{
|
|
|
|
std::string z;
|
|
|
|
encryptor->Seek(seek);
|
2017-05-10 19:26:36 +00:00
|
|
|
StringSource ss(plaintext, false, new StreamTransformationFilter(*encryptor, new StringSink(z),
|
|
|
|
static_cast<BlockPaddingSchemeDef::BlockPaddingScheme>(paddingScheme)));
|
2015-11-05 06:59:46 +00:00
|
|
|
while (ss.Pump(64)) {}
|
|
|
|
ss.PumpAll();
|
|
|
|
for (int i=0; i<z.length(); i++)
|
2016-09-16 15:27:15 +00:00
|
|
|
CRYPTOPP_ASSERT(encrypted[i] == z[i]);
|
2015-11-05 06:59:46 +00:00
|
|
|
}*/
|
|
|
|
if (test != "EncryptXorDigest")
|
|
|
|
ciphertext = GetDecodedDatum(v, "Ciphertext");
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ciphertextXorDigest = GetDecodedDatum(v, "CiphertextXorDigest");
|
|
|
|
xorDigest.append(encrypted, 0, 64);
|
|
|
|
for (size_t i=64; i<encrypted.size(); i++)
|
|
|
|
xorDigest[i%64] ^= encrypted[i];
|
|
|
|
}
|
|
|
|
if (test != "EncryptXorDigest" ? encrypted != ciphertext : xorDigest != ciphertextXorDigest)
|
|
|
|
{
|
2017-05-04 23:11:24 +00:00
|
|
|
std::cout << "\nincorrectly encrypted: ";
|
2015-11-05 06:59:46 +00:00
|
|
|
StringSource xx(encrypted, false, new HexEncoder(new FileSink(std::cout)));
|
|
|
|
xx.Pump(2048); xx.Flush(false);
|
|
|
|
std::cout << "\n";
|
|
|
|
SignalTestFailure();
|
|
|
|
}
|
|
|
|
std::string decrypted;
|
2017-05-10 19:26:36 +00:00
|
|
|
StreamTransformationFilter decFilter(*decryptor, new StringSink(decrypted),
|
|
|
|
static_cast<BlockPaddingSchemeDef::BlockPaddingScheme>(paddingScheme));
|
2015-11-05 06:59:46 +00:00
|
|
|
RandomizedTransfer(StringStore(encrypted).Ref(), decFilter, true);
|
|
|
|
decFilter.MessageEnd();
|
|
|
|
if (decrypted != plaintext)
|
|
|
|
{
|
2017-05-04 23:11:24 +00:00
|
|
|
std::cout << "\nincorrectly decrypted: ";
|
2015-11-05 06:59:46 +00:00
|
|
|
StringSource xx(decrypted, false, new HexEncoder(new FileSink(std::cout)));
|
|
|
|
xx.Pump(256); xx.Flush(false);
|
|
|
|
std::cout << "\n";
|
|
|
|
SignalTestFailure();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-05-04 23:11:24 +00:00
|
|
|
std::cout << "\nunexpected test name\n";
|
2015-11-05 06:59:46 +00:00
|
|
|
SignalTestError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestAuthenticatedSymmetricCipher(TestData &v, const NameValuePairs &overrideParameters)
|
|
|
|
{
|
|
|
|
std::string type = GetRequiredDatum(v, "AlgorithmType");
|
|
|
|
std::string name = GetRequiredDatum(v, "Name");
|
|
|
|
std::string test = GetRequiredDatum(v, "Test");
|
|
|
|
std::string key = GetDecodedDatum(v, "Key");
|
|
|
|
|
|
|
|
std::string plaintext = GetOptionalDecodedDatum(v, "Plaintext");
|
|
|
|
std::string ciphertext = GetOptionalDecodedDatum(v, "Ciphertext");
|
|
|
|
std::string header = GetOptionalDecodedDatum(v, "Header");
|
|
|
|
std::string footer = GetOptionalDecodedDatum(v, "Footer");
|
|
|
|
std::string mac = GetOptionalDecodedDatum(v, "MAC");
|
|
|
|
|
|
|
|
TestDataNameValuePairs testDataPairs(v);
|
|
|
|
CombinedNameValuePairs pairs(overrideParameters, testDataPairs);
|
|
|
|
|
|
|
|
if (test == "Encrypt" || test == "EncryptXorDigest" || test == "NotVerify")
|
|
|
|
{
|
|
|
|
member_ptr<AuthenticatedSymmetricCipher> asc1, asc2;
|
|
|
|
asc1.reset(ObjectFactoryRegistry<AuthenticatedSymmetricCipher, ENCRYPTION>::Registry().CreateObject(name.c_str()));
|
|
|
|
asc2.reset(ObjectFactoryRegistry<AuthenticatedSymmetricCipher, DECRYPTION>::Registry().CreateObject(name.c_str()));
|
|
|
|
asc1->SetKey((const byte *)key.data(), key.size(), pairs);
|
|
|
|
asc2->SetKey((const byte *)key.data(), key.size(), pairs);
|
|
|
|
|
|
|
|
std::string encrypted, decrypted;
|
|
|
|
AuthenticatedEncryptionFilter ef(*asc1, new StringSink(encrypted));
|
2017-02-21 07:03:29 +00:00
|
|
|
bool macAtBegin = !mac.empty() && !Test::GlobalRNG().GenerateBit(); // test both ways randomly
|
2015-11-05 06:59:46 +00:00
|
|
|
AuthenticatedDecryptionFilter df(*asc2, new StringSink(decrypted), macAtBegin ? AuthenticatedDecryptionFilter::MAC_AT_BEGIN : 0);
|
|
|
|
|
|
|
|
if (asc1->NeedsPrespecifiedDataLengths())
|
|
|
|
{
|
|
|
|
asc1->SpecifyDataLengths(header.size(), plaintext.size(), footer.size());
|
|
|
|
asc2->SpecifyDataLengths(header.size(), plaintext.size(), footer.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
StringStore sh(header), sp(plaintext), sc(ciphertext), sf(footer), sm(mac);
|
|
|
|
|
|
|
|
if (macAtBegin)
|
|
|
|
RandomizedTransfer(sm, df, true);
|
|
|
|
sh.CopyTo(df, LWORD_MAX, AAD_CHANNEL);
|
|
|
|
RandomizedTransfer(sc, df, true);
|
|
|
|
sf.CopyTo(df, LWORD_MAX, AAD_CHANNEL);
|
|
|
|
if (!macAtBegin)
|
|
|
|
RandomizedTransfer(sm, df, true);
|
|
|
|
df.MessageEnd();
|
|
|
|
|
|
|
|
RandomizedTransfer(sh, ef, true, AAD_CHANNEL);
|
|
|
|
RandomizedTransfer(sp, ef, true);
|
|
|
|
RandomizedTransfer(sf, ef, true, AAD_CHANNEL);
|
|
|
|
ef.MessageEnd();
|
|
|
|
|
|
|
|
if (test == "Encrypt" && encrypted != ciphertext+mac)
|
|
|
|
{
|
2017-05-04 23:11:24 +00:00
|
|
|
std::cout << "\nincorrectly encrypted: ";
|
2015-11-05 06:59:46 +00:00
|
|
|
StringSource xx(encrypted, false, new HexEncoder(new FileSink(std::cout)));
|
|
|
|
xx.Pump(2048); xx.Flush(false);
|
|
|
|
std::cout << "\n";
|
|
|
|
SignalTestFailure();
|
|
|
|
}
|
|
|
|
if (test == "Encrypt" && decrypted != plaintext)
|
|
|
|
{
|
2017-05-04 23:11:24 +00:00
|
|
|
std::cout << "\nincorrectly decrypted: ";
|
2015-11-05 06:59:46 +00:00
|
|
|
StringSource xx(decrypted, false, new HexEncoder(new FileSink(std::cout)));
|
|
|
|
xx.Pump(256); xx.Flush(false);
|
|
|
|
std::cout << "\n";
|
|
|
|
SignalTestFailure();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ciphertext.size()+mac.size()-plaintext.size() != asc1->DigestSize())
|
|
|
|
{
|
2017-05-04 23:11:24 +00:00
|
|
|
std::cout << "\nbad MAC size\n";
|
2015-11-05 06:59:46 +00:00
|
|
|
SignalTestFailure();
|
|
|
|
}
|
|
|
|
if (df.GetLastResult() != (test == "Encrypt"))
|
|
|
|
{
|
2017-05-04 23:11:24 +00:00
|
|
|
std::cout << "\nMAC incorrectly verified\n";
|
2015-11-05 06:59:46 +00:00
|
|
|
SignalTestFailure();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-05-04 23:11:24 +00:00
|
|
|
std::cout << "\nunexpected test name\n";
|
2015-11-05 06:59:46 +00:00
|
|
|
SignalTestError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestDigestOrMAC(TestData &v, bool testDigest)
|
|
|
|
{
|
|
|
|
std::string name = GetRequiredDatum(v, "Name");
|
|
|
|
std::string test = GetRequiredDatum(v, "Test");
|
|
|
|
const char *digestName = testDigest ? "Digest" : "MAC";
|
|
|
|
|
|
|
|
member_ptr<MessageAuthenticationCode> mac;
|
|
|
|
member_ptr<HashTransformation> hash;
|
2017-03-01 11:10:06 +00:00
|
|
|
HashTransformation *pHash = NULLPTR;
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
TestDataNameValuePairs pairs(v);
|
|
|
|
|
|
|
|
if (testDigest)
|
|
|
|
{
|
|
|
|
hash.reset(ObjectFactoryRegistry<HashTransformation>::Registry().CreateObject(name.c_str()));
|
|
|
|
pHash = hash.get();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mac.reset(ObjectFactoryRegistry<MessageAuthenticationCode>::Registry().CreateObject(name.c_str()));
|
|
|
|
pHash = mac.get();
|
|
|
|
std::string key = GetDecodedDatum(v, "Key");
|
|
|
|
mac->SetKey((const byte *)key.c_str(), key.size(), pairs);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (test == "Verify" || test == "VerifyTruncated" || test == "NotVerify")
|
|
|
|
{
|
|
|
|
int digestSize = -1;
|
|
|
|
if (test == "VerifyTruncated")
|
2015-11-18 20:32:28 +00:00
|
|
|
digestSize = pairs.GetIntValueWithDefault(Name::DigestSize(), digestSize);
|
2017-03-01 11:10:06 +00:00
|
|
|
HashVerificationFilter verifierFilter(*pHash, NULLPTR, HashVerificationFilter::HASH_AT_BEGIN, digestSize);
|
2015-11-05 06:59:46 +00:00
|
|
|
PutDecodedDatumInto(v, digestName, verifierFilter);
|
|
|
|
PutDecodedDatumInto(v, "Message", verifierFilter);
|
|
|
|
verifierFilter.MessageEnd();
|
|
|
|
if (verifierFilter.GetLastResult() == (test == "NotVerify"))
|
|
|
|
SignalTestFailure();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SignalTestError();
|
2016-09-16 15:27:15 +00:00
|
|
|
CRYPTOPP_ASSERT(false);
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestKeyDerivationFunction(TestData &v)
|
2016-04-10 07:02:02 +00:00
|
|
|
{
|
2015-11-05 06:59:46 +00:00
|
|
|
std::string name = GetRequiredDatum(v, "Name");
|
|
|
|
std::string test = GetRequiredDatum(v, "Test");
|
|
|
|
|
|
|
|
if(test == "Skip") return;
|
2016-09-16 15:27:15 +00:00
|
|
|
CRYPTOPP_ASSERT(test == "Verify");
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
std::string key = GetDecodedDatum(v, "Key");
|
|
|
|
std::string salt = GetDecodedDatum(v, "Salt");
|
|
|
|
std::string info = GetDecodedDatum(v, "Info");
|
|
|
|
std::string derived = GetDecodedDatum(v, "DerivedKey");
|
2015-11-23 00:17:15 +00:00
|
|
|
std::string t = GetDecodedDatum(v, "DerivedKeyLength");
|
2016-04-10 07:02:02 +00:00
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
TestDataNameValuePairs pairs(v);
|
2015-11-23 00:17:15 +00:00
|
|
|
unsigned int length = pairs.GetIntValueWithDefault(Name::DerivedKeyLength(), (int)derived.size());
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
member_ptr<KeyDerivationFunction> kdf;
|
|
|
|
kdf.reset(ObjectFactoryRegistry<KeyDerivationFunction>::Registry().CreateObject(name.c_str()));
|
2016-04-10 07:02:02 +00:00
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
std::string calc; calc.resize(length);
|
|
|
|
unsigned int ret = kdf->DeriveKey(reinterpret_cast<byte*>(&calc[0]), calc.size(),
|
|
|
|
reinterpret_cast<const byte*>(key.data()), key.size(),
|
|
|
|
reinterpret_cast<const byte*>(salt.data()), salt.size(),
|
|
|
|
reinterpret_cast<const byte*>(info.data()), info.size());
|
2016-07-24 23:28:01 +00:00
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
if(calc != derived || ret != length)
|
|
|
|
SignalTestFailure();
|
|
|
|
}
|
|
|
|
|
2017-05-12 18:25:25 +00:00
|
|
|
// GetField parses the name/value pairs. The tricky part is the insertion operator
|
|
|
|
// because Unix&Linux uses LF, OS X uses CR, and Windows uses CRLF. If this function
|
|
|
|
// is modified, then run 'cryptest.exe tv rsa_pkcs1_1_5' as a test. Its the parser
|
|
|
|
// file from hell. If it can be parsed without error, then things are likely OK.
|
2015-11-05 06:59:46 +00:00
|
|
|
bool GetField(std::istream &is, std::string &name, std::string &value)
|
|
|
|
{
|
2017-05-12 06:20:44 +00:00
|
|
|
// ***** Name *****
|
2017-02-21 07:03:29 +00:00
|
|
|
name.clear();
|
2015-11-05 06:59:46 +00:00
|
|
|
is >> name;
|
2015-11-18 20:32:28 +00:00
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
if (name.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (name[name.size()-1] != ':')
|
|
|
|
{
|
|
|
|
char c;
|
2017-02-21 07:03:29 +00:00
|
|
|
is >> std::skipws >> c;
|
2015-11-05 06:59:46 +00:00
|
|
|
if (c != ':')
|
|
|
|
SignalTestError();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
name.erase(name.size()-1);
|
|
|
|
|
|
|
|
while (is.peek() == ' ')
|
|
|
|
is.ignore(1);
|
|
|
|
|
2017-05-12 06:20:44 +00:00
|
|
|
// ***** Value *****
|
2017-02-21 07:03:29 +00:00
|
|
|
value.clear();
|
2017-05-12 06:20:44 +00:00
|
|
|
std::string line;
|
|
|
|
bool continueLine = true;
|
2015-11-05 06:59:46 +00:00
|
|
|
|
2017-05-12 06:20:44 +00:00
|
|
|
while (continueLine && std::getline(is, line))
|
2015-11-05 06:59:46 +00:00
|
|
|
{
|
2017-05-12 18:25:25 +00:00
|
|
|
// Unix and Linux may have a stray \r because of Windows
|
|
|
|
if (!line.empty() && (line[line.size() - 1] == '\r' || line[line.size() - 1] == '\n')) {
|
|
|
|
line.erase(line.size()-1);
|
|
|
|
}
|
2017-05-12 07:05:24 +00:00
|
|
|
|
2017-05-12 18:25:25 +00:00
|
|
|
continueLine = false;
|
|
|
|
if (!line.empty())
|
|
|
|
{
|
|
|
|
// Early out for immediate line continuation
|
|
|
|
if (line[0] == '\\') {
|
|
|
|
continueLine = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Check end of line. It must be last character
|
|
|
|
if (line[line.size() - 1] == '\\') {
|
|
|
|
continueLine = true;
|
|
|
|
}
|
2017-05-14 06:41:22 +00:00
|
|
|
// Check for comment. It can be first character
|
|
|
|
if (line[0] == '#') {
|
|
|
|
continue;
|
|
|
|
}
|
2017-05-12 18:25:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Leading, trailing and temp position. The leading position moves right, and
|
|
|
|
// trailing position moves left. The sub-string in the middle is the value for
|
|
|
|
// the name. We leave one space when line continuation is in effect, (and if
|
|
|
|
// present). The value can be an empty string. One Plaintext value is often
|
|
|
|
// empty for algorithm testing.
|
2017-05-14 06:41:22 +00:00
|
|
|
std::string::size_type l=0, t=std::string::npos;
|
2017-05-12 06:20:44 +00:00
|
|
|
const std::string whitespace = " \r\n\t\v\f";
|
2015-11-05 06:59:46 +00:00
|
|
|
|
2017-05-14 06:41:22 +00:00
|
|
|
l = line.find_first_not_of(whitespace, l);
|
2017-05-12 18:25:25 +00:00
|
|
|
if (l == std::string::npos) { l = 0; }
|
2017-05-14 06:41:22 +00:00
|
|
|
t = line.find('#', l);
|
|
|
|
if (t != std::string::npos) { t--; }
|
2017-05-12 18:25:25 +00:00
|
|
|
|
2017-05-14 06:41:22 +00:00
|
|
|
t = line.find_last_not_of(whitespace+"\\", t);
|
|
|
|
if (t != std::string::npos) { t++; }
|
2016-04-10 07:02:02 +00:00
|
|
|
|
2017-05-12 18:25:25 +00:00
|
|
|
CRYPTOPP_ASSERT(t >= l);
|
2017-05-14 06:41:22 +00:00
|
|
|
value += line.substr(l, t - l);
|
|
|
|
|
|
|
|
if (continueLine)
|
|
|
|
value.append(1, ' ');
|
2016-04-10 07:02:02 +00:00
|
|
|
}
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OutputPair(const NameValuePairs &v, const char *name)
|
|
|
|
{
|
|
|
|
Integer x;
|
|
|
|
bool b = v.GetValue(name, x);
|
2016-09-16 15:27:15 +00:00
|
|
|
CRYPTOPP_UNUSED(b); CRYPTOPP_ASSERT(b);
|
2017-02-21 07:03:29 +00:00
|
|
|
std::cout << name << ": \\\n ";
|
|
|
|
x.Encode(HexEncoder(new FileSink(std::cout), false, 64, "\\\n ").Ref(), x.MinEncodedSize());
|
|
|
|
std::cout << std::endl;
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OutputNameValuePairs(const NameValuePairs &v)
|
|
|
|
{
|
|
|
|
std::string names = v.GetValueNames();
|
2017-02-21 07:03:29 +00:00
|
|
|
std::string::size_type i = 0;
|
2015-11-05 06:59:46 +00:00
|
|
|
while (i < names.size())
|
|
|
|
{
|
2017-02-21 07:03:29 +00:00
|
|
|
std::string::size_type j = names.find_first_of (';', i);
|
2015-11-05 06:59:46 +00:00
|
|
|
|
2017-02-21 07:03:29 +00:00
|
|
|
if (j == std::string::npos)
|
2015-11-05 06:59:46 +00:00
|
|
|
return;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string name = names.substr(i, j-i);
|
2017-02-21 07:03:29 +00:00
|
|
|
if (name.find(':') == std::string::npos)
|
2015-11-05 06:59:46 +00:00
|
|
|
OutputPair(v, name.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
i = j + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-26 04:50:28 +00:00
|
|
|
void TestDataFile(std::string filename, const NameValuePairs &overrideParameters, unsigned int &totalTests, unsigned int &failedTests)
|
2015-11-05 06:59:46 +00:00
|
|
|
{
|
2015-12-26 04:50:28 +00:00
|
|
|
static const std::string dataDirectory(CRYPTOPP_DATA_DIR);
|
|
|
|
if (!dataDirectory.empty())
|
|
|
|
{
|
|
|
|
if(dataDirectory != filename.substr(0, dataDirectory.length()))
|
|
|
|
filename.insert(0, dataDirectory);
|
|
|
|
}
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
std::ifstream file(filename.c_str());
|
|
|
|
if (!file.good())
|
|
|
|
throw Exception(Exception::OTHER_ERROR, "Can not open file " + filename + " for reading");
|
2017-05-12 07:50:37 +00:00
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
TestData v;
|
|
|
|
s_currentTestData = &v;
|
|
|
|
std::string name, value, lastAlgName;
|
|
|
|
|
|
|
|
while (file)
|
|
|
|
{
|
|
|
|
while (file.peek() == '#')
|
2016-09-16 15:27:15 +00:00
|
|
|
file.ignore((std::numeric_limits<std::streamsize>::max)(), '\n');
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
if (file.peek() == '\n' || file.peek() == '\r')
|
|
|
|
v.clear();
|
|
|
|
|
|
|
|
if (!GetField(file, name, value))
|
|
|
|
break;
|
2017-05-12 07:05:24 +00:00
|
|
|
|
|
|
|
// Can't assert value. Plaintext is sometimes empty.
|
|
|
|
// CRYPTOPP_ASSERT(!value.empty());
|
2015-11-05 06:59:46 +00:00
|
|
|
v[name] = value;
|
|
|
|
|
|
|
|
if (name == "Test" && (s_thorough || v["SlowTest"] != "1"))
|
|
|
|
{
|
|
|
|
bool failed = true;
|
|
|
|
std::string algType = GetRequiredDatum(v, "AlgorithmType");
|
|
|
|
|
|
|
|
if (lastAlgName != GetRequiredDatum(v, "Name"))
|
|
|
|
{
|
|
|
|
lastAlgName = GetRequiredDatum(v, "Name");
|
2017-02-21 07:03:29 +00:00
|
|
|
std::cout << "\nTesting " << algType.c_str() << " algorithm " << lastAlgName.c_str() << ".\n";
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (algType == "Signature")
|
|
|
|
TestSignatureScheme(v);
|
|
|
|
else if (algType == "SymmetricCipher")
|
|
|
|
TestSymmetricCipher(v, overrideParameters);
|
|
|
|
else if (algType == "AuthenticatedSymmetricCipher")
|
|
|
|
TestAuthenticatedSymmetricCipher(v, overrideParameters);
|
|
|
|
else if (algType == "AsymmetricCipher")
|
|
|
|
TestAsymmetricCipher(v);
|
|
|
|
else if (algType == "MessageDigest")
|
|
|
|
TestDigestOrMAC(v, true);
|
|
|
|
else if (algType == "MAC")
|
|
|
|
TestDigestOrMAC(v, false);
|
|
|
|
else if (algType == "KDF")
|
|
|
|
TestKeyDerivationFunction(v);
|
|
|
|
else if (algType == "FileList")
|
|
|
|
TestDataFile(GetRequiredDatum(v, "Test"), g_nullNameValuePairs, totalTests, failedTests);
|
|
|
|
else
|
2016-04-09 23:32:57 +00:00
|
|
|
SignalUnknownAlgorithmError(algType);
|
2015-11-05 06:59:46 +00:00
|
|
|
failed = false;
|
|
|
|
}
|
2016-04-09 23:18:57 +00:00
|
|
|
catch (const TestFailure &)
|
2015-11-05 06:59:46 +00:00
|
|
|
{
|
2017-02-21 07:03:29 +00:00
|
|
|
std::cout << "\nTest failed.\n";
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
2016-04-09 23:18:57 +00:00
|
|
|
catch (const CryptoPP::Exception &e)
|
2015-11-05 06:59:46 +00:00
|
|
|
{
|
2017-02-21 07:03:29 +00:00
|
|
|
std::cout << "\nCryptoPP::Exception caught: " << e.what() << std::endl;
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
2016-04-09 23:18:57 +00:00
|
|
|
catch (const std::exception &e)
|
2015-11-05 06:59:46 +00:00
|
|
|
{
|
2017-02-21 07:03:29 +00:00
|
|
|
std::cout << "\nstd::exception caught: " << e.what() << std::endl;
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (failed)
|
|
|
|
{
|
2017-02-21 07:03:29 +00:00
|
|
|
std::cout << "Skipping to next test.\n";
|
2015-11-05 06:59:46 +00:00
|
|
|
failedTests++;
|
|
|
|
}
|
|
|
|
else
|
2017-02-21 07:03:29 +00:00
|
|
|
std::cout << "." << std::flush;
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
totalTests++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RunTestDataFile(const char *filename, const NameValuePairs &overrideParameters, bool thorough)
|
|
|
|
{
|
|
|
|
s_thorough = thorough;
|
|
|
|
unsigned int totalTests = 0, failedTests = 0;
|
2015-12-26 04:50:28 +00:00
|
|
|
TestDataFile((filename ? filename : ""), overrideParameters, totalTests, failedTests);
|
2017-02-21 07:03:29 +00:00
|
|
|
std::cout << std::dec << "\nTests complete. Total tests = " << totalTests << ". Failed tests = " << failedTests << "." << std::endl;
|
2015-11-05 06:59:46 +00:00
|
|
|
if (failedTests != 0)
|
2017-02-21 07:03:29 +00:00
|
|
|
std::cout << "SOME TESTS FAILED!\n";
|
2015-11-05 06:59:46 +00:00
|
|
|
return failedTests == 0;
|
|
|
|
}
|
2016-04-10 07:02:02 +00:00
|
|
|
|
2017-02-21 07:03:29 +00:00
|
|
|
NAMESPACE_END // Test
|
|
|
|
NAMESPACE_END // CryptoPP
|