Add TestSymmetricCipherWithFileSource stub

There are a couple of failures at the moment so it is not enabled
This commit is contained in:
Jeffrey Walton 2021-03-18 03:37:34 -04:00
parent 034297eafa
commit dd59d58c19
No known key found for this signature in database
GPG Key ID: B36AB348921B1838

View File

@ -522,14 +522,15 @@ void TestSymmetricCipher(TestData &v, const NameValuePairs &overrideParameters)
// BlockSize or Tweak, then it becomes latched in testDataPairs. The old
// value is used in subsequent tests, and it could cause a self test
// failure in the next test. The behavior surfaced under Kalyna and
// Threefish. The Kalyna test vectors use NO_PADDING for all tests excpet
// Threefish. The Kalyna test vectors use NO_PADDING for all tests except
// one. For Threefish, using (and not using) a Tweak caused problems as
// we marched through test vectors. For BlockPadding, BlockSize or Tweak,
// unlatch them now, after the key has been set and NameValuePairs have
// been processed. Also note we only unlatch from testDataPairs. If
// overrideParameters are specified, the caller is responsible for
// managing the parameter.
v.erase("Tweak"); v.erase("InitialBlock"); v.erase("BlockSize"); v.erase("BlockPaddingScheme");
v.erase("Tweak"); v.erase("InitialBlock");
v.erase("BlockSize"); v.erase("BlockPaddingScheme");
std::string encrypted, xorDigest, ciphertext, ciphertextXorDigest;
if (test == "EncryptionMCT" || test == "DecryptionMCT")
@ -622,6 +623,127 @@ void TestSymmetricCipher(TestData &v, const NameValuePairs &overrideParameters)
}
}
void TestSymmetricCipherWithFileSource(TestData &v, const NameValuePairs &overrideParameters)
{
std::string name = GetRequiredDatum(v, "Name");
std::string test = GetRequiredDatum(v, "Test");
// Limit FileSource tests to Encrypt only.
if (test != "Encrypt") { return; }
std::string key = GetDecodedDatum(v, "Key");
std::string plaintext = GetDecodedDatum(v, "Plaintext");
TestDataNameValuePairs testDataPairs(v);
CombinedNameValuePairs pairs(overrideParameters, testDataPairs);
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;
// Code coverage
(void)encryptor->AlgorithmName();
(void)decryptor->AlgorithmName();
(void)encryptor->AlgorithmProvider();
(void)decryptor->AlgorithmProvider();
(void)encryptor->MinKeyLength();
(void)decryptor->MinKeyLength();
(void)encryptor->MaxKeyLength();
(void)decryptor->MaxKeyLength();
(void)encryptor->DefaultKeyLength();
(void)decryptor->DefaultKeyLength();
}
// 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);
ConstByteArrayParameter iv;
if (pairs.GetValue(Name::IV(), iv) && iv.size() != encryptor->IVSize())
SignalTestFailure();
encryptor->SetKey(ConstBytePtr(key), BytePtrSize(key), pairs);
decryptor->SetKey(ConstBytePtr(key), BytePtrSize(key), pairs);
word64 seek64 = pairs.GetWord64ValueWithDefault("Seek64", 0);
if (seek64)
{
encryptor->Seek(seek64);
decryptor->Seek(seek64);
}
else
{
int seek = pairs.GetIntValueWithDefault("Seek", 0);
if (seek)
{
encryptor->Seek(seek);
decryptor->Seek(seek);
}
}
// If a per-test vector parameter was set for a test, like BlockPadding,
// BlockSize or Tweak, then it becomes latched in testDataPairs. The old
// value is used in subsequent tests, and it could cause a self test
// failure in the next test. The behavior surfaced under Kalyna and
// Threefish. The Kalyna test vectors use NO_PADDING for all tests except
// one. For Threefish, using (and not using) a Tweak caused problems as
// we marched through test vectors. For BlockPadding, BlockSize or Tweak,
// unlatch them now, after the key has been set and NameValuePairs have
// been processed. Also note we only unlatch from testDataPairs. If
// overrideParameters are specified, the caller is responsible for
// managing the parameter.
v.erase("Tweak"); v.erase("InitialBlock");
v.erase("BlockSize"); v.erase("BlockPaddingScheme");
std::string encrypted, ciphertext;
StreamTransformationFilter encFilter(*encryptor, new StringSink(encrypted),
static_cast<BlockPaddingSchemeDef::BlockPaddingScheme>(paddingScheme));
//StringStore pstore(plaintext);
//RandomizedTransfer(pstore, encFilter, true);
// encFilter.MessageEnd();
std::string testFilename = "test.dat";
StringSource(plaintext, true, new FileSink(testFilename.c_str()));
FileSource(testFilename.c_str(), true, new Redirector(encFilter));
encFilter.MessageEnd();
ciphertext = GetDecodedDatum(v, "Ciphertext");
if (encrypted != ciphertext)
{
std::cout << "\nincorrectly encrypted: ";
StringSource xx(encrypted, false, new HexEncoder(new FileSink(std::cout)));
xx.Pump(2048); xx.Flush(false);
std::cout << "\n";
SignalTestFailure();
}
std::string decrypted;
StreamTransformationFilter decFilter(*decryptor, new StringSink(decrypted),
static_cast<BlockPaddingSchemeDef::BlockPaddingScheme>(paddingScheme));
StringStore cstore(encrypted);
RandomizedTransfer(cstore, decFilter, true);
decFilter.MessageEnd();
if (decrypted != plaintext)
{
std::cout << "\nincorrectly decrypted: ";
StringSource xx(decrypted, false, new HexEncoder(new FileSink(std::cout)));
xx.Pump(256); xx.Flush(false);
std::cout << "\n";
SignalTestFailure();
}
}
void TestAuthenticatedSymmetricCipher(TestData &v, const NameValuePairs &overrideParameters)
{
std::string type = GetRequiredDatum(v, "AlgorithmType");
@ -914,10 +1036,9 @@ void OutputNameValuePairs(const NameValuePairs &v)
void TestDataFile(std::string filename, const NameValuePairs &overrideParameters, unsigned int &totalTests, unsigned int &failedTests)
{
filename = DataDir(filename);
std::ifstream file(filename.c_str());
std::ifstream file(DataDir(filename).c_str());
if (!file.good())
throw Exception(Exception::OTHER_ERROR, "Can not open file " + filename + " for reading");
throw Exception(Exception::OTHER_ERROR, "Can not open file " + DataDir(filename) + " for reading");
TestData v;
s_currentTestData = &v;
@ -951,7 +1072,10 @@ void TestDataFile(std::string filename, const NameValuePairs &overrideParameters
if (algType == "Signature")
TestSignatureScheme(v);
else if (algType == "SymmetricCipher")
{
TestSymmetricCipher(v, overrideParameters);
// TestSymmetricCipherWithFileSource(v, overrideParameters);
}
else if (algType == "AuthenticatedSymmetricCipher")
TestAuthenticatedSymmetricCipher(v, overrideParameters);
else if (algType == "AsymmetricCipher")