Add variable block size support to test and benchmarks

CRYPTOPP_COVERAGE was added at 9614307ab7 to increase code coverage support. This commit enables additional validation routines when  CRYPTOPP_COVERAGE is in effect.
pull/416/head
Jeffrey Walton 2017-05-04 19:11:24 -04:00
parent 9614307ab7
commit 5c1de7b5a5
No known key found for this signature in database
GPG Key ID: B36AB348921B1838
9 changed files with 40 additions and 32 deletions

View File

@ -57,6 +57,7 @@ PublicElement - the public element when KeyFormat=Component
PrivateExponent - the private exponent when KeyFormat=Component
Message - encoded string, message to be signed or verified
Signature - encoded string, signature to be verified or compared with
BlockSize - encoded string, block size for vaiable block ciphers
Plaintext - encoded string
Ciphertext - encoded string
Header - encoded string
@ -79,7 +80,8 @@ Verify - signature/digest/MAC verification should pass
VerifyTruncated - truncated digest/MAC verification should pass
NotVerify - signature/digest/MAC verification should not pass
DeterministicSign - sign message using given seed, and the resulting
signature should be equal to the given signature
signature should equal the given signature
Encrypt - plaintext encrypts to ciphertext
EncryptBlockSize - plaintext encrypts to ciphertext under a key and blocksize
DecryptMatch - ciphertext decrypts to plaintext
(more to come here)

View File

@ -271,7 +271,6 @@ void BenchMarkKeying(SimpleKeyingInterface &c, size_t keyLength, const NameValue
template <class T_FactoryOutput, class T_Interface>
void BenchMarkByName2(const char *factoryName, size_t keyLength = 0, const char *displayName=NULLPTR, const NameValuePairs &params = g_nullNameValuePairs)
{
CRYPTOPP_UNUSED(params);
std::string name(factoryName ? factoryName : "");
member_ptr<T_FactoryOutput> obj(ObjectFactoryRegistry<T_FactoryOutput>::Registry().CreateObject(name.c_str()));
@ -283,9 +282,10 @@ void BenchMarkByName2(const char *factoryName, size_t keyLength = 0, const char
else if (keyLength)
name += " (" + IntToString(keyLength * 8) + "-bit key)";
obj->SetKey(defaultKey, keyLength, CombinedNameValuePairs(params, MakeParameters(Name::IV(), ConstByteArrayParameter(defaultKey, obj->IVSize()), false)));
const int blockSize = params.GetIntValueWithDefault(Name::BlockSize(), 0);
obj->SetKey(defaultKey, keyLength, CombinedNameValuePairs(params, MakeParameters(Name::IV(), ConstByteArrayParameter(defaultKey, blockSize ? blockSize : obj->IVSize()), false)));
BenchMark(name.c_str(), *static_cast<T_Interface *>(obj.get()), g_allocatedTime);
BenchMarkKeying(*obj, keyLength, CombinedNameValuePairs(params, MakeParameters(Name::IV(), ConstByteArrayParameter(defaultKey, obj->IVSize()), false)));
BenchMarkKeying(*obj, keyLength, CombinedNameValuePairs(params, MakeParameters(Name::IV(), ConstByteArrayParameter(defaultKey, blockSize ? blockSize : obj->IVSize()), false)));
}
template <class T_FactoryOutput>
@ -559,6 +559,11 @@ void Benchmark2(double t, double hertz)
BenchMarkByName<SymmetricCipher>("CAST-128/CTR");
BenchMarkByName<SymmetricCipher>("SKIPJACK/CTR");
BenchMarkByName<SymmetricCipher>("SEED/CTR", 0, "SEED/CTR (1/2 K table)");
// BenchMarkByName<SymmetricCipher>("Kalyna/CTR", 16, "Kalyna-128(128)", MakeParameters(Name::BlockSize(), 16));
// BenchMarkByName<SymmetricCipher>("Kalyna/CTR", 32, "Kalyna-256(128)", MakeParameters(Name::BlockSize(), 16));
// BenchMarkByName<SymmetricCipher>("Kalyna/CTR", 32, "Kalyna-256(256)", MakeParameters(Name::BlockSize(), 32));
// BenchMarkByName<SymmetricCipher>("Kalyna/CTR", 64, "Kalyna-512(256)", MakeParameters(Name::BlockSize(), 32));
// BenchMarkByName<SymmetricCipher>("Kalyna/CTR", 64, "Kalyna-512(512)", MakeParameters(Name::BlockSize(), 64));
}
std::cout << "\n<TBODY style=\"background: yellow;\">";

View File

@ -363,7 +363,7 @@ void TestSymmetricCipher(TestData &v, const NameValuePairs &overrideParameters)
TestDataNameValuePairs testDataPairs(v);
CombinedNameValuePairs pairs(overrideParameters, testDataPairs);
if (test == "Encrypt" || test == "EncryptXorDigest" || test == "Resync" || test == "EncryptionMCT" || test == "DecryptionMCT")
if (test == "Encrypt" || test == "EncryptBlockSize" || test == "EncryptXorDigest" || test == "Resync" || test == "EncryptionMCT" || test == "DecryptionMCT")
{
static member_ptr<SymmetricCipher> encryptor, decryptor;
static std::string lastName;
@ -375,8 +375,12 @@ void TestSymmetricCipher(TestData &v, const NameValuePairs &overrideParameters)
lastName = name;
}
int blockSize = 0;
if (test == "EncryptBlockSize" && !pairs.GetValue(Name::BlockSize(), blockSize))
SignalTestFailure();
ConstByteArrayParameter iv;
if (pairs.GetValue(Name::IV(), iv) && iv.size() != encryptor->IVSize())
if (pairs.GetValue(Name::IV(), iv) && iv.size() != encryptor->IVSize() && iv.size() != blockSize)
SignalTestFailure();
if (test == "Resync")
@ -427,7 +431,7 @@ void TestSymmetricCipher(TestData &v, const NameValuePairs &overrideParameters)
ciphertext = GetDecodedDatum(v, test == "EncryptionMCT" ? "Ciphertext" : "Plaintext");
if (encrypted != ciphertext)
{
std::cout << "incorrectly encrypted: ";
std::cout << "\nincorrectly encrypted: ";
StringSource xx(encrypted, false, new HexEncoder(new FileSink(std::cout)));
xx.Pump(256); xx.Flush(false);
std::cout << "\n";
@ -459,7 +463,7 @@ void TestSymmetricCipher(TestData &v, const NameValuePairs &overrideParameters)
}
if (test != "EncryptXorDigest" ? encrypted != ciphertext : xorDigest != ciphertextXorDigest)
{
std::cout << "incorrectly encrypted: ";
std::cout << "\nincorrectly encrypted: ";
StringSource xx(encrypted, false, new HexEncoder(new FileSink(std::cout)));
xx.Pump(2048); xx.Flush(false);
std::cout << "\n";
@ -471,7 +475,7 @@ void TestSymmetricCipher(TestData &v, const NameValuePairs &overrideParameters)
decFilter.MessageEnd();
if (decrypted != plaintext)
{
std::cout << "incorrectly decrypted: ";
std::cout << "\nincorrectly decrypted: ";
StringSource xx(decrypted, false, new HexEncoder(new FileSink(std::cout)));
xx.Pump(256); xx.Flush(false);
std::cout << "\n";
@ -480,7 +484,7 @@ void TestSymmetricCipher(TestData &v, const NameValuePairs &overrideParameters)
}
else
{
std::cout << "unexpected test name\n";
std::cout << "\nunexpected test name\n";
SignalTestError();
}
}
@ -538,7 +542,7 @@ void TestAuthenticatedSymmetricCipher(TestData &v, const NameValuePairs &overrid
if (test == "Encrypt" && encrypted != ciphertext+mac)
{
std::cout << "incorrectly encrypted: ";
std::cout << "\nincorrectly encrypted: ";
StringSource xx(encrypted, false, new HexEncoder(new FileSink(std::cout)));
xx.Pump(2048); xx.Flush(false);
std::cout << "\n";
@ -546,7 +550,7 @@ void TestAuthenticatedSymmetricCipher(TestData &v, const NameValuePairs &overrid
}
if (test == "Encrypt" && decrypted != plaintext)
{
std::cout << "incorrectly decrypted: ";
std::cout << "\nincorrectly decrypted: ";
StringSource xx(decrypted, false, new HexEncoder(new FileSink(std::cout)));
xx.Pump(256); xx.Flush(false);
std::cout << "\n";
@ -555,18 +559,18 @@ void TestAuthenticatedSymmetricCipher(TestData &v, const NameValuePairs &overrid
if (ciphertext.size()+mac.size()-plaintext.size() != asc1->DigestSize())
{
std::cout << "bad MAC size\n";
std::cout << "\nbad MAC size\n";
SignalTestFailure();
}
if (df.GetLastResult() != (test == "Encrypt"))
{
std::cout << "MAC incorrectly verified\n";
std::cout << "\nMAC incorrectly verified\n";
SignalTestFailure();
}
}
else
{
std::cout << "unexpected test name\n";
std::cout << "\nunexpected test name\n";
SignalTestError();
}
}

View File

@ -134,6 +134,8 @@ void RegisterFactories2()
RegisterSymmetricCipherDefaultFactories<CTR_Mode<Blowfish> >();
RegisterSymmetricCipherDefaultFactories<ECB_Mode<SEED> >();
RegisterSymmetricCipherDefaultFactories<CTR_Mode<SEED> >();
// RegisterSymmetricCipherDefaultFactories<ECB_Mode<Kalyna> >(); // Test Vectors
// RegisterSymmetricCipherDefaultFactories<CTR_Mode<Kalyna> >(); // Benchmarks
RegisterDefaultFactoryFor<KeyDerivationFunction, HKDF<SHA1> >();
RegisterDefaultFactoryFor<KeyDerivationFunction, HKDF<SHA256> >();

View File

@ -25,7 +25,6 @@
#include "stdcpp.h"
#include "ossig.h"
#include "trap.h"
#include "aria.h"
#include "validate.h"
#include "bench.h"
@ -181,10 +180,6 @@ int CRYPTOPP_API main(int argc, char *argv[])
_CrtSetDbgFlag( tempflag );
#endif
#if defined(__MWERKS__) && defined(macintosh)
argc = ccommand(&argv);
#endif
try
{
RegisterFactories(Test::All);
@ -462,7 +457,7 @@ int CRYPTOPP_API main(int argc, char *argv[])
std::cout << "\nstd::exception caught: " << e.what() << std::endl;
return -2;
}
} // End main()
} // main()
void FIPS140_GenerateRandomFiles()
{
@ -994,7 +989,7 @@ bool Validate(int alg, bool thorough, const char *seedInput)
case 78: result = Test::ValidateHashDRBG(); break;
case 79: result = Test::ValidateHmacDRBG(); break;
#if defined(CRYPTOPP_DEBUG) && !defined(CRYPTOPP_IMPORTS)
#if (defined(CRYPTOPP_DEBUG) || defined(CRYPTOPP_COVERAGE)) && !defined(CRYPTOPP_IMPORTS)
// http://github.com/weidai11/cryptopp/issues/92
case 9999: result = Test::TestSecBlock(); break;
// http://github.com/weidai11/cryptopp/issues/64

View File

@ -24,7 +24,7 @@
NAMESPACE_BEGIN(CryptoPP)
NAMESPACE_BEGIN(Test)
#if defined(CRYPTOPP_DEBUG) && !defined(CRYPTOPP_IMPORTS)
#if (defined(CRYPTOPP_DEBUG) || defined(CRYPTOPP_COVERAGE)) && !defined(CRYPTOPP_IMPORTS)
bool TestRounding()
{
std::cout << "\nTesting RoundUpToMultipleOf/RoundDownToMultipleOf...\n\n";
@ -470,7 +470,7 @@ bool TestRounding()
}
#endif
#if defined(CRYPTOPP_DEBUG) && !defined(CRYPTOPP_IMPORTS)
#if (defined(CRYPTOPP_DEBUG) || defined(CRYPTOPP_COVERAGE)) && !defined(CRYPTOPP_IMPORTS)
struct ASN1_TestTuple
{
int disposition;
@ -721,7 +721,7 @@ bool TestASN1Parse()
}
#endif
#if defined(CRYPTOPP_DEBUG) && !defined(CRYPTOPP_IMPORTS)
#if (defined(CRYPTOPP_DEBUG) || defined(CRYPTOPP_COVERAGE)) && !defined(CRYPTOPP_IMPORTS)
bool TestSecBlock()
{
std::cout << "\nTesting SecBlock...\n\n";
@ -1685,7 +1685,7 @@ bool TestSecBlock()
}
#endif
#if defined(CRYPTOPP_DEBUG) && !defined(CRYPTOPP_IMPORTS)
#if (defined(CRYPTOPP_DEBUG) || defined(CRYPTOPP_COVERAGE)) && !defined(CRYPTOPP_IMPORTS)
bool TestHuffmanCodes()
{
std::cout << "\nTesting Huffman codes...\n\n";
@ -1723,7 +1723,7 @@ bool TestHuffmanCodes()
}
#endif
#if defined(CRYPTOPP_DEBUG) && !defined(CRYPTOPP_IMPORTS)
#if (defined(CRYPTOPP_DEBUG) || defined(CRYPTOPP_COVERAGE)) && !defined(CRYPTOPP_IMPORTS)
bool TestIntegerBitops()
{
std::cout << "\nTesting Integer bitops...\n\n";

View File

@ -78,7 +78,7 @@ bool ValidateAll(bool thorough)
pass=TestRDSEED() && pass;
#endif
#if defined(CRYPTOPP_DEBUG) && !defined(CRYPTOPP_IMPORTS)
#if (defined(CRYPTOPP_DEBUG) || defined(CRYPTOPP_COVERAGE)) && !defined(CRYPTOPP_IMPORTS)
// http://github.com/weidai11/cryptopp/issues/92
pass=TestSecBlock() && pass;
// http://github.com/weidai11/cryptopp/issues/336
@ -229,7 +229,7 @@ bool TestSettings()
pass = false;
}
#if defined(CRYPTOPP_DEBUG) && !defined(CRYPTOPP_IMPORTS)
#if (defined(CRYPTOPP_DEBUG) || defined(CRYPTOPP_COVERAGE)) && !defined(CRYPTOPP_IMPORTS)
// App and library versions, http://github.com/weidai11/cryptopp/issues/371
const int v1 = LibraryVersion();
const int v2 = HeaderVersion();

View File

@ -793,7 +793,7 @@ bool ValidateBlumGoldwasser()
}
*/
#if defined(CRYPTOPP_DEBUG) && !defined(CRYPTOPP_IMPORTS)
#if (defined(CRYPTOPP_DEBUG) || defined(CRYPTOPP_COVERAGE)) && !defined(CRYPTOPP_IMPORTS)
// Issue 64: "PolynomialMod2::operator<<=", http://github.com/weidai11/cryptopp/issues/64
bool TestPolynomialMod2()
{

View File

@ -107,7 +107,7 @@ bool ValidateESIGN();
bool ValidateHashDRBG();
bool ValidateHmacDRBG();
#if defined(CRYPTOPP_DEBUG) && !defined(CRYPTOPP_IMPORTS)
#if (defined(CRYPTOPP_DEBUG) || defined(CRYPTOPP_COVERAGE)) && !defined(CRYPTOPP_IMPORTS)
// http://github.com/weidai11/cryptopp/issues/92
bool TestSecBlock();
// http://github.com/weidai11/cryptopp/issues/64