Skip to content

Commit

Permalink
Add TestSignatureSchemeWithFileSource (GH #672, GH #1010)
Browse files Browse the repository at this point in the history
  • Loading branch information
noloader committed Mar 19, 2021
1 parent 07951c1 commit 0c88471
Showing 1 changed file with 69 additions and 7 deletions.
76 changes: 69 additions & 7 deletions datatest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ NAMESPACE_BEGIN(Test)

typedef std::map<std::string, std::string> TestData;
static bool s_thorough = false;
const std::string testDataFilename = "cryptest.dat";

class TestFailure : public Exception
{
Expand Down Expand Up @@ -404,8 +405,6 @@ void TestSignatureScheme(TestData &v, unsigned int &totalTests)

if (GetDecodedDatum(v, "Signature") != signature)
SignalTestFailure();

return;
}
else
{
Expand All @@ -415,6 +414,66 @@ void TestSignatureScheme(TestData &v, unsigned int &totalTests)
}
}

// Subset of TestSignatureScheme. We picked the tests that have data that is easy to write to a file.
void TestSignatureSchemeWithFileSource(TestData &v, unsigned int &totalTests)
{
std::string name = GetRequiredDatum(v, "Name");
std::string test = GetRequiredDatum(v, "Test");

if (test != "Sign" && test != "DeterministicSign") { return; }

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()));

// Code coverage
(void)signer->AlgorithmName();
(void)verifier->AlgorithmName();
(void)signer->AlgorithmProvider();
(void)verifier->AlgorithmProvider();

TestDataNameValuePairs pairs(v);

std::string keyFormat = GetRequiredDatum(v, "KeyFormat");

totalTests++; // key format
if (keyFormat == "DER")
verifier->AccessMaterial().Load(StringStore(GetDecodedDatum(v, "PublicKey")).Ref());
else if (keyFormat == "Component")
verifier->AccessMaterial().AssignFrom(pairs);

totalTests++; // key format
if (keyFormat == "DER")
signer->AccessMaterial().Load(StringStore(GetDecodedDatum(v, "PrivateKey")).Ref());
else if (keyFormat == "Component")
signer->AccessMaterial().AssignFrom(pairs);

if (test == "Sign")
{
totalTests++;

SignerFilter f(Test::GlobalRNG(), *signer, new HexEncoder(new FileSink(std::cout)));
StringSource ss(GetDecodedDatum(v, "Message"), true, new FileSink(testDataFilename.c_str()));
FileSource fs(testDataFilename.c_str(), true, new Redirector(f));
SignalTestFailure();
}
else if (test == "DeterministicSign")
{
totalTests++;

// 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 deterministic signatures are added, then the test harness will
// likely need to be extended.
std::string signature;
SignerFilter f(Test::GlobalRNG(), *signer, new StringSink(signature));
StringSource ss(GetDecodedDatum(v, "Message"), true, new FileSink(testDataFilename.c_str()));
FileSource fs(testDataFilename.c_str(), true, new Redirector(f));

if (GetDecodedDatum(v, "Signature") != signature)
SignalTestFailure();
}
}

void TestAsymmetricCipher(TestData &v, unsigned int &totalTests)
{
std::string name = GetRequiredDatum(v, "Name");
Expand Down Expand Up @@ -649,14 +708,15 @@ void TestSymmetricCipher(TestData &v, const NameValuePairs &overrideParameters,
}
}

// TODO: figure out what is going on with chacha_tls.
// Subset of TestSymmetricCipher. We picked the tests that have data that is easy to write to a file.
void TestSymmetricCipherWithFileSource(TestData &v, const NameValuePairs &overrideParameters, unsigned int &totalTests)
{
std::string name = GetRequiredDatum(v, "Name");
std::string test = GetRequiredDatum(v, "Test");

// Limit FileSource tests to Encrypt only.
if (test != "Encrypt") { return; }

totalTests++;

std::string key = GetDecodedDatum(v, "Key");
Expand Down Expand Up @@ -738,10 +798,8 @@ void TestSymmetricCipherWithFileSource(TestData &v, const NameValuePairs &overri
//RandomizedTransfer(pstore, encFilter, true);
//encFilter.MessageEnd();

std::string testFilename = "cryptest.dat";
StringSource(plaintext, true, new FileSink(testFilename.c_str()));

FileSource pstore(testFilename.c_str(), true);
StringSource ss(plaintext, true, new FileSink(testDataFilename.c_str()));
FileSource pstore(testDataFilename.c_str(), true);
RandomizedTransfer(pstore, encFilter, true);
encFilter.MessageEnd();

Expand Down Expand Up @@ -1107,7 +1165,11 @@ void TestDataFile(std::string filename, const NameValuePairs &overrideParameters
try
{
if (algType == "Signature")
{
TestData vv(v); // Used with TestSignatureSchemeWithFileSource
TestSignatureScheme(v, totalTests);
TestSignatureSchemeWithFileSource(vv, totalTests);
}
else if (algType == "SymmetricCipher")
{
TestData vv(v); // Used with TestSymmetricCipherWithFileSource
Expand Down

0 comments on commit 0c88471

Please sign in to comment.