summaryrefslogtreecommitdiff
path: root/datatest.cpp
diff options
context:
space:
mode:
authorJeffrey Walton <noloader@gmail.com>2021-03-18 03:37:34 -0400
committerJeffrey Walton <noloader@gmail.com>2021-03-18 03:37:34 -0400
commitdd59d58c195c1f6b82f2ea7589489297200cd362 (patch)
tree8e8629beef34ac8e22fd6b1a7bd59af98be423e2 /datatest.cpp
parent034297eafae0c0b9c1b2917b8798ed20fd981254 (diff)
downloadcryptopp-git-dd59d58c195c1f6b82f2ea7589489297200cd362.tar.gz
Add TestSymmetricCipherWithFileSource stub
There are a couple of failures at the moment so it is not enabled
Diffstat (limited to 'datatest.cpp')
-rw-r--r--datatest.cpp134
1 files changed, 129 insertions, 5 deletions
diff --git a/datatest.cpp b/datatest.cpp
index 2590cb2b..62d2f775 100644
--- a/datatest.cpp
+++ b/datatest.cpp
@@ -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")