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 PrivateExponent - the private exponent when KeyFormat=Component
Message - encoded string, message to be signed or verified Message - encoded string, message to be signed or verified
Signature - encoded string, signature to be verified or compared with Signature - encoded string, signature to be verified or compared with
BlockSize - encoded string, block size for vaiable block ciphers
Plaintext - encoded string Plaintext - encoded string
Ciphertext - encoded string Ciphertext - encoded string
Header - encoded string Header - encoded string
@ -79,7 +80,8 @@ Verify - signature/digest/MAC verification should pass
VerifyTruncated - truncated digest/MAC verification should pass VerifyTruncated - truncated digest/MAC verification should pass
NotVerify - signature/digest/MAC verification should not pass NotVerify - signature/digest/MAC verification should not pass
DeterministicSign - sign message using given seed, and the resulting 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 DecryptMatch - ciphertext decrypts to plaintext
(more to come here) (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> 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) 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 : ""); std::string name(factoryName ? factoryName : "");
member_ptr<T_FactoryOutput> obj(ObjectFactoryRegistry<T_FactoryOutput>::Registry().CreateObject(name.c_str())); 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) else if (keyLength)
name += " (" + IntToString(keyLength * 8) + "-bit key)"; 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); 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> template <class T_FactoryOutput>
@ -559,6 +559,11 @@ void Benchmark2(double t, double hertz)
BenchMarkByName<SymmetricCipher>("CAST-128/CTR"); BenchMarkByName<SymmetricCipher>("CAST-128/CTR");
BenchMarkByName<SymmetricCipher>("SKIPJACK/CTR"); BenchMarkByName<SymmetricCipher>("SKIPJACK/CTR");
BenchMarkByName<SymmetricCipher>("SEED/CTR", 0, "SEED/CTR (1/2 K table)"); 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;\">"; std::cout << "\n<TBODY style=\"background: yellow;\">";

View File

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

View File

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

View File

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

View File

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

View File

@ -78,7 +78,7 @@ bool ValidateAll(bool thorough)
pass=TestRDSEED() && pass; pass=TestRDSEED() && pass;
#endif #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 // http://github.com/weidai11/cryptopp/issues/92
pass=TestSecBlock() && pass; pass=TestSecBlock() && pass;
// http://github.com/weidai11/cryptopp/issues/336 // http://github.com/weidai11/cryptopp/issues/336
@ -229,7 +229,7 @@ bool TestSettings()
pass = false; 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 // App and library versions, http://github.com/weidai11/cryptopp/issues/371
const int v1 = LibraryVersion(); const int v1 = LibraryVersion();
const int v2 = HeaderVersion(); 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 // Issue 64: "PolynomialMod2::operator<<=", http://github.com/weidai11/cryptopp/issues/64
bool TestPolynomialMod2() bool TestPolynomialMod2()
{ {

View File

@ -107,7 +107,7 @@ bool ValidateESIGN();
bool ValidateHashDRBG(); bool ValidateHashDRBG();
bool ValidateHmacDRBG(); 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 // http://github.com/weidai11/cryptopp/issues/92
bool TestSecBlock(); bool TestSecBlock();
// http://github.com/weidai11/cryptopp/issues/64 // http://github.com/weidai11/cryptopp/issues/64