From 5a35640912a4848b48e1a76121efd37f7df387f5 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Thu, 30 Jul 2015 10:36:49 -0400 Subject: [PATCH] Added validat0.cpp and moved bit tests into it. Provided tests for SafeConvert. Removed "using namespace std" from test sources (auto_ptr causes a collision becuase std:: provides it in C++03, but CryptoPP:: provides it in C++11 --- GNUmakefile | 2 +- smartptr.h | 26 +- test.cpp | 279 +++++++++++----------- validat0.cpp | 659 +++++++++++++++++++++++++++++++++++++++++++++++++++ validat1.cpp | 147 +----------- validat2.cpp | 9 +- validat3.cpp | 19 +- validate.h | 2 + 8 files changed, 833 insertions(+), 310 deletions(-) create mode 100644 validat0.cpp diff --git a/GNUmakefile b/GNUmakefile index b57345f6..eafa12a4 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -337,7 +337,7 @@ OBJS = $(SRCS:.cpp=.o) TEMPS = $(SRCS:.cpp=.s) $(SRCS:.cpp=.ii) # test.o needs to be after bench.o for cygwin 1.1.4 (possible ld bug?) -TESTOBJS = bench.o bench2.o test.o validat1.o validat2.o validat3.o adhoc.o datatest.o regtest.o fipsalgt.o dlltest.o +TESTOBJS = bench.o bench2.o test.o validat0.o validat1.o validat2.o validat3.o adhoc.o datatest.o regtest.o fipsalgt.o dlltest.o LIBOBJS = $(filter-out $(TESTOBJS),$(OBJS)) DLLSRCS = algebra.cpp algparam.cpp asn.cpp basecode.cpp cbcmac.cpp channels.cpp cryptlib.cpp des.cpp dessp.cpp dh.cpp \ diff --git a/smartptr.h b/smartptr.h index 30d996fa..5b275c2b 100644 --- a/smartptr.h +++ b/smartptr.h @@ -7,25 +7,27 @@ NAMESPACE_BEGIN(CryptoPP) -// Hack ahead. Apple's standard library does not have C++'s unique_ptr. We can't test -// for unique_ptr directly because some of the Clangs on Apple fail the same way. -// However, modern standard libraries have , so we test for it instead. +// Hack ahead. Apple's standard library does not have C++'s unique_ptr in C++11. We can't +// test for unique_ptr directly because some of the non-Apple Clangs on OS X fail the same +// way. However, modern standard libraries have , so we test for it instead. // Thanks to Jonathan Wakely for devising the clever test for modern/ancient versions. #if (__cplusplus >= 201103L) || (_MSC_VER >= 1600) -# if defined(__clang__) && (__has_include()) -# define CRYPTOPP_HAVE_UNIQUE_PTR 1 -# else -# define CRYPTOPP_HAVE_UNIQUE_PTR 1 +# if defined(__clang__) +# if (__has_include()) +# define CRYPTOPP_HAVE_UNIQUE_PTR 1 # endif +# else +# define CRYPTOPP_HAVE_UNIQUE_PTR 1 +# endif #endif +// The result of below is a CryptoPP::auto_ptr in both cases #ifdef CRYPTOPP_HAVE_UNIQUE_PTR -// use unique_ptr instead of auto_ptr -template + template using std::auto_ptr = std::unique_ptr; #else -// do nothing; use auto_ptr + using std::auto_ptr; #endif template class simple_ptr @@ -79,7 +81,7 @@ public: T *old_p = m_p; m_p = 0; return old_p; - } + } void reset(T *p = 0); @@ -175,7 +177,7 @@ protected: }; template counted_ptr::counted_ptr(T *p) - : m_p(p) + : m_p(p) { if (m_p) m_p->m_referenceCount = 1; diff --git a/test.cpp b/test.cpp index b2b437b0..186ee853 100644 --- a/test.cpp +++ b/test.cpp @@ -50,8 +50,8 @@ #endif #ifdef __BORLANDC__ -#pragma comment(lib, "cryptlib_bds.lib") -#pragma comment(lib, "ws2_32.lib") +# pragma comment(lib, "cryptlib_bds.lib") +# pragma comment(lib, "ws2_32.lib") #endif USING_NAMESPACE(CryptoPP) @@ -113,31 +113,31 @@ struct DebugTrapHandler struct sigaction old_handler, new_handler; memset(&old_handler, 0x00, sizeof(old_handler)); memset(&new_handler, 0x00, sizeof(new_handler)); - + do { int ret = 0; - + ret = sigaction (SIGTRAP, NULL, &old_handler); if (ret != 0) break; // Failed - + // Don't step on another's handler if (old_handler.sa_handler != NULL) break; - + // Set up the structure to specify the null action. new_handler.sa_handler = &DebugTrapHandler::NullHandler; new_handler.sa_flags = 0; - + ret = sigemptyset (&new_handler.sa_mask); if (ret != 0) break; // Failed - - // Install it + + // Install i ret = sigaction (SIGTRAP, &new_handler, NULL); if (ret != 0) break; // Failed - + } while(0); } - + static void NullHandler(int /*unused*/) { } }; @@ -170,7 +170,7 @@ int CRYPTOPP_API main(int argc, char *argv[]) #if defined(__MWERKS__) && defined(macintosh) argc = ccommand(&argv); #endif - + CRYPTOPP_UNUSED(argc), CRYPTOPP_UNUSED(argv); try @@ -193,16 +193,16 @@ int CRYPTOPP_API main(int argc, char *argv[]) char seed[1024], privFilename[128], pubFilename[128]; unsigned int keyLength; - cout << "Key length in bits: "; + std::cout << "Key length in bits: "; cin >> keyLength; - cout << "\nSave private key to file: "; + std::cout << "\nSave private key to file: "; cin >> privFilename; - cout << "\nSave public key to file: "; + std::cout << "\nSave public key to file: "; cin >> pubFilename; - cout << "\nRandom Seed: "; + std::cout << "\nRandom Seed: "; ws(cin); cin.getline(seed, 1024); @@ -213,38 +213,38 @@ int CRYPTOPP_API main(int argc, char *argv[]) else if (command == "rv") { bool verified = RSAVerifyFile(argv[2], argv[3], argv[4]); - cout << (verified ? "valid signature" : "invalid signature") << endl; + std::cout << (verified ? "valid signature" : "invalid signature") << std::endl; } else if (command == "r") { char privFilename[128], pubFilename[128]; char seed[1024], message[1024]; - cout << "Private key file: "; + std::cout << "Private key file: "; cin >> privFilename; - cout << "\nPublic key file: "; + std::cout << "\nPublic key file: "; cin >> pubFilename; - cout << "\nRandom Seed: "; + std::cout << "\nRandom Seed: "; ws(cin); cin.getline(seed, 1024); - cout << "\nMessage: "; + std::cout << "\nMessage: "; cin.getline(message, 1024); string ciphertext = RSAEncryptString(pubFilename, seed, message); - cout << "\nCiphertext: " << ciphertext << endl; + std::cout << "\nCiphertext: " << ciphertext << std::endl; string decrypted = RSADecryptString(privFilename, ciphertext.c_str()); - cout << "\nDecrypted: " << decrypted << endl; + std::cout << "\nDecrypted: " << decrypted << std::endl; } else if (command == "mt") { MaurerRandomnessTest mt; FileStore fs(argv[2]); fs.TransferAllTo(mt); - cout << "Maurer Test Value: " << mt.GetTestValue() << endl; + std::cout << "Maurer Test Value: " << mt.GetTestValue() << std::endl; } else if (command == "mac_dll") { @@ -253,7 +253,7 @@ int CRYPTOPP_API main(int argc, char *argv[]) std::ifstream::pos_type fileEnd = dllFile.seekg(0, std::ios_base::end).tellg(); if (fileEnd > 20*1000*1000) { - cerr << "Input file too large (more than 20 MB).\n"; + std::cerr << "Input file too large (more than 20 MB).\n"; return 1; } @@ -269,7 +269,7 @@ int CRYPTOPP_API main(int argc, char *argv[]) word16 optionalHeaderMagic = *(word16 *)(buf+optionalHeaderPos); if (optionalHeaderMagic != 0x10b && optionalHeaderMagic != 0x20b) { - cerr << "Target file is not a PE32 or PE32+ image.\n"; + std::cerr << "Target file is not a PE32 or PE32+ image.\n"; return 3; } word32 checksumPos = optionalHeaderPos + 64; @@ -277,14 +277,14 @@ int CRYPTOPP_API main(int argc, char *argv[]) word32 certificateTablePos = *(word32 *)(buf+certificateTableDirectoryPos); word32 certificateTableSize = *(word32 *)(buf+certificateTableDirectoryPos+4); if (certificateTableSize != 0) - cerr << "Warning: certificate table (IMAGE_DIRECTORY_ENTRY_SECURITY) of target image is not empty.\n"; + std::cerr << "Warning: certificate table (IMAGE_DIRECTORY_ENTRY_SECURITY) of target image is not empty.\n"; // find where to place computed MAC byte mac[] = CRYPTOPP_DUMMY_DLL_MAC; byte *found = std::search(buf.begin(), buf.end(), mac+0, mac+sizeof(mac)); if (found == buf.end()) { - cerr << "MAC placeholder not found. Possibly the actual MAC was already placed.\n"; + std::cerr << "MAC placeholder not found. Possibly the actual MAC was already placed.\n"; return 2; } word32 macPos = (unsigned int)(found-buf.begin()); @@ -300,7 +300,7 @@ int CRYPTOPP_API main(int argc, char *argv[]) f.PutMessageEnd(buf.begin(), buf.size()); // place MAC - cout << "Placing MAC in file " << argv[2] << ", location " << macPos << ".\n"; + std::cout << "Placing MAC in file " << argv[2] << ", location " << macPos << ".\n"; dllFile.seekg(macPos, std::ios_base::beg); dllFile.write((char *)mac, sizeof(mac)); } @@ -318,17 +318,17 @@ int CRYPTOPP_API main(int argc, char *argv[]) // VC60 workaround: use char array instead of std::string to workaround MSVC's getline bug char passPhrase[MAX_PHRASE_LENGTH], plaintext[1024]; - cout << "Passphrase: "; + std::cout << "Passphrase: "; cin.getline(passPhrase, MAX_PHRASE_LENGTH); - cout << "\nPlaintext: "; + std::cout << "\nPlaintext: "; cin.getline(plaintext, 1024); string ciphertext = EncryptString(plaintext, passPhrase); - cout << "\nCiphertext: " << ciphertext << endl; + std::cout << "\nCiphertext: " << ciphertext << std::endl; string decrypted = DecryptString(ciphertext.c_str(), passPhrase); - cout << "\nDecrypted: " << decrypted << endl; + std::cout << "\nDecrypted: " << decrypted << std::endl; return 0; } @@ -343,7 +343,7 @@ int CRYPTOPP_API main(int argc, char *argv[]) else if (command == "e" || command == "d") { char passPhrase[MAX_PHRASE_LENGTH]; - cout << "Passphrase: "; + std::cout << "Passphrase: "; cin.getline(passPhrase, MAX_PHRASE_LENGTH); if (command == "e") EncryptFile(argv[2], argv[3], passPhrase); @@ -353,7 +353,7 @@ int CRYPTOPP_API main(int argc, char *argv[]) else if (command == "ss") { char seed[1024]; - cout << "\nRandom Seed: "; + std::cout << "\nRandom Seed: "; ws(cin); cin.getline(seed, 1024); SecretShareFile(atoi(argv[2]), atoi(argv[3]), argv[4], seed); @@ -386,7 +386,7 @@ int CRYPTOPP_API main(int argc, char *argv[]) return (*AdhocTest)(argc, argv); else { - cerr << "AdhocTest not defined.\n"; + std::cerr << "AdhocTest not defined.\n"; return 1; } } @@ -396,28 +396,28 @@ int CRYPTOPP_API main(int argc, char *argv[]) AES_CTR_Encrypt(argv[2], argv[3], argv[4], argv[5]); else if (command == "h") { - FileSource usage("TestData/usage.dat", true, new FileSink(cout)); + FileSource usage("TestData/usage.dat", true, new FileSink(std::cout)); return 1; } else if (command == "V") { - cout << CRYPTOPP_VERSION / 100 << '.' << (CRYPTOPP_VERSION % 100) / 10 << '.' << CRYPTOPP_VERSION % 10 << endl; + std::cout << CRYPTOPP_VERSION / 100 << '.' << (CRYPTOPP_VERSION % 100) / 10 << '.' << CRYPTOPP_VERSION % 10 << std::endl; } else { - cerr << "Unrecognized command. Run \"cryptest h\" to obtain usage information.\n"; + std::cerr << "Unrecognized command. Run \"cryptest h\" to obtain usage information.\n"; return 1; } return 0; } catch(CryptoPP::Exception &e) { - cout << "\nCryptoPP::Exception caught: " << e.what() << endl; + std::cout << "\nCryptoPP::Exception caught: " << e.what() << std::endl; return -1; } catch(std::exception &e) { - cout << "\nstd::exception caught: " << e.what() << endl; + std::cout << "\nstd::exception caught: " << e.what() << std::endl; return -2; } } @@ -431,7 +431,7 @@ void FIPS140_GenerateRandomFiles() for (unsigned int i=0; i<100000; i++) store.TransferTo(FileSink((IntToString(i) + ".rnd").c_str()).Ref(), 20000); #else - cout << "OS provided RNG not available.\n"; + std::cout << "OS provided RNG not available.\n"; exit(-1); #endif } @@ -530,12 +530,12 @@ void DigestFile(const char *filename) channelSwitch->AddDefaultRoute(*filters[i]); FileSource(filename, true, channelSwitch.release()); - HexEncoder encoder(new FileSink(cout), false); + HexEncoder encoder(new FileSink(std::cout), false); for (i=0; iAlgorithmName() << ": "; + std::cout << filters[i]->AlgorithmName() << ": "; filters[i]->TransferTo(encoder); - cout << "\n"; + std::cout << "\n"; } } @@ -544,7 +544,7 @@ void HmacFile(const char *hexKey, const char *file) member_ptr mac; if (strcmp(hexKey, "selftest") == 0) { - cerr << "Computing HMAC/SHA1 value for self test.\n"; + std::cerr << "Computing HMAC/SHA1 value for self test.\n"; mac.reset(NewIntegrityCheckingMAC()); } else @@ -553,7 +553,7 @@ void HmacFile(const char *hexKey, const char *file) StringSource(hexKey, true, new HexDecoder(new StringSink(decodedKey))); mac.reset(new HMAC((const byte *)decodedKey.data(), decodedKey.size())); } - FileSource(file, true, new HashFilter(*mac, new HexEncoder(new FileSink(cout)))); + FileSource(file, true, new HashFilter(*mac, new HexEncoder(new FileSink(std::cout)))); } void AES_CTR_Encrypt(const char *hexKey, const char *hexIV, const char *infile, const char *outfile) @@ -701,17 +701,17 @@ void InformationRecoverFile(int threshold, const char *outFilename, char *const void GzipFile(const char *in, const char *out, int deflate_level) { -// FileSource(in, true, new Gzip(new FileSink(out), deflate_level)); + // FileSource(in, true, new Gzip(new FileSink(out), deflate_level)); // use a filter graph to compare decompressed data with original // - // Source ----> Gzip ------> Sink - // \ | - // \ Gunzip - // \ | - // \ v + // Source --------> Gzip --------> Sink + // \ | + // \ Gunzip + // \ | + // \ v // > ComparisonFilter - + EqualityComparisonFilter comparison; Gunzip gunzip(new ChannelSwitch(comparison, "0")); @@ -770,18 +770,18 @@ void ForwardTcpPort(const char *sourcePortName, const char *destinationHost, con sockListen.Bind(sourcePort); setsockopt(sockListen, IPPROTO_TCP, TCP_NODELAY, "\x01", 1); - cout << "Listing on port " << sourcePort << ".\n"; + std::cout << "Listing on port " << sourcePort << ".\n"; sockListen.Listen(); sockListen.Accept(sockSource); - cout << "Connection accepted on port " << sourcePort << ".\n"; + std::cout << "Connection accepted on port " << sourcePort << ".\n"; sockListen.CloseSocket(); - cout << "Making connection to " << destinationHost << ", port " << destinationPort << ".\n"; + std::cout << "Making connection to " << destinationHost << ", port " << destinationPort << ".\n"; sockDestination.Create(); sockDestination.Connect(destinationHost, destinationPort); - cout << "Connection made to " << destinationHost << ", starting to forward.\n"; + std::cout << "Connection made to " << destinationHost << ", starting to forward.\n"; SocketSource out(sockSource, false, new SocketSink(sockDestination)); SocketSource in(sockDestination, false, new SocketSink(sockSource)); @@ -799,22 +799,22 @@ void ForwardTcpPort(const char *sourcePortName, const char *destinationHost, con if (!out.SourceExhausted()) { - cout << "o" << flush; + std::cout << "o" << flush; out.PumpAll2(false); if (out.SourceExhausted()) - cout << "EOF received on source socket.\n"; + std::cout << "EOF received on source socket.\n"; } if (!in.SourceExhausted()) { - cout << "i" << flush; + std::cout << "i" << flush; in.PumpAll2(false); if (in.SourceExhausted()) - cout << "EOF received on destination socket.\n"; + std::cout << "EOF received on destination socket.\n"; } } #else - cout << "Socket support was not enabled at compile time.\n"; + std::cout << "Socket support was not enabled at compile time.\n"; exit(-1); #endif } @@ -826,7 +826,7 @@ bool Validate(int alg, bool thorough, const char *seedInput) std::string seed = seedInput ? std::string(seedInput) : IntToString(time(NULL)); seed.resize(16); - cout << "Using seed: " << seed << endl; + std::cout << "Using seed: " << seed << std::endl; s_globalRNG.SetKeyWithIV((byte *)seed.data(), 16, (byte *)seed.data()); #ifdef _OPENMP @@ -836,88 +836,91 @@ bool Validate(int alg, bool thorough, const char *seedInput) tc = omp_get_num_threads(); } - cout << "Using " << tc << " OMP " << (tc == 1 ? "thread" : "threads") << endl; + std::cout << "Using " << tc << " OMP " << (tc == 1 ? "thread" : "threads") << std::endl; #endif - cout << endl; + std::cout << std::endl; switch (alg) { - case 0: result = ValidateAll(thorough); break; - case 1: result = TestSettings(); break; - case 2: result = TestOS_RNG(); break; - case 3: result = ValidateMD5(); break; - case 4: result = ValidateSHA(); break; - case 5: result = ValidateDES(); break; - case 6: result = ValidateIDEA(); break; - case 7: result = ValidateARC4(); break; - case 8: result = ValidateRC5(); break; - case 9: result = ValidateBlowfish(); break; -// case 10: result = ValidateDiamond2(); break; - case 11: result = ValidateThreeWay(); break; - case 12: result = ValidateBBS(); break; - case 13: result = ValidateDH(); break; - case 14: result = ValidateRSA(); break; - case 15: result = ValidateElGamal(); break; - case 16: result = ValidateDSA(thorough); break; -// case 17: result = ValidateHAVAL(); break; - case 18: result = ValidateSAFER(); break; - case 19: result = ValidateLUC(); break; - case 20: result = ValidateRabin(); break; -// case 21: result = ValidateBlumGoldwasser(); break; - case 22: result = ValidateECP(); break; - case 23: result = ValidateEC2N(); break; -// case 24: result = ValidateMD5MAC(); break; - case 25: result = ValidateGOST(); break; - case 26: result = ValidateTiger(); break; - case 27: result = ValidateRIPEMD(); break; - case 28: result = ValidateHMAC(); break; -// case 29: result = ValidateXMACC(); break; - case 30: result = ValidateSHARK(); break; - case 32: result = ValidateLUC_DH(); break; - case 33: result = ValidateLUC_DL(); break; - case 34: result = ValidateSEAL(); break; - case 35: result = ValidateCAST(); break; - case 36: result = ValidateSquare(); break; - case 37: result = ValidateRC2(); break; - case 38: result = ValidateRC6(); break; - case 39: result = ValidateMARS(); break; - case 40: result = ValidateRW(); break; - case 41: result = ValidateMD2(); break; - case 42: result = ValidateNR(); break; - case 43: result = ValidateMQV(); break; - case 44: result = ValidateRijndael(); break; - case 45: result = ValidateTwofish(); break; - case 46: result = ValidateSerpent(); break; - case 47: result = ValidateCipherModes(); break; - case 48: result = ValidateCRC32(); break; - case 49: result = ValidateECDSA(); break; - case 50: result = ValidateXTR_DH(); break; - case 51: result = ValidateSKIPJACK(); break; - case 52: result = ValidateSHA2(); break; - case 53: result = ValidatePanama(); break; - case 54: result = ValidateAdler32(); break; - case 55: result = ValidateMD4(); break; - case 56: result = ValidatePBKDF(); break; - case 57: result = ValidateESIGN(); break; - case 58: result = ValidateDLIES(); break; - case 59: result = ValidateBaseCode(); break; - case 60: result = ValidateSHACAL2(); break; - case 61: result = ValidateCamellia(); break; - case 62: result = ValidateWhirlpool(); break; - case 63: result = ValidateTTMAC(); break; - case 64: result = ValidateSalsa(); break; - case 65: result = ValidateSosemanuk(); break; - case 66: result = ValidateVMAC(); break; - case 67: result = ValidateCCM(); break; - case 68: result = ValidateGCM(); break; - case 69: result = ValidateCMAC(); break; - default: return false; + case 0: result = ValidateAll(thorough); break; + case 1: result = TestSettings(); break; + case 70: result = TestRotate(); break; + case 71: result = TestConversion(); break; + case 2: result = TestOS_RNG(); break; + case 3: result = ValidateMD5(); break; + case 4: result = ValidateSHA(); break; + case 5: result = ValidateDES(); break; + case 6: result = ValidateIDEA(); break; + case 7: result = ValidateARC4(); break; + case 8: result = ValidateRC5(); break; + case 9: result = ValidateBlowfish(); break; + // case 10: result = ValidateDiamond2(); break; + case 11: result = ValidateThreeWay(); break; + case 12: result = ValidateBBS(); break; + case 13: result = ValidateDH(); break; + case 14: result = ValidateRSA(); break; + case 15: result = ValidateElGamal(); break; + case 16: result = ValidateDSA(thorough); break; + // case 17: result = ValidateHAVAL(); break; + case 18: result = ValidateSAFER(); break; + case 19: result = ValidateLUC(); break; + case 20: result = ValidateRabin(); break; + // case 21: result = ValidateBlumGoldwasser(); break; + case 22: result = ValidateECP(); break; + case 23: result = ValidateEC2N(); break; + // case 24: result = ValidateMD5MAC(); break; + case 25: result = ValidateGOST(); break; + case 26: result = ValidateTiger(); break; + case 27: result = ValidateRIPEMD(); break; + case 28: result = ValidateHMAC(); break; + // case 29: result = ValidateXMACC(); break; + case 30: result = ValidateSHARK(); break; + case 32: result = ValidateLUC_DH(); break; + case 33: result = ValidateLUC_DL(); break; + case 34: result = ValidateSEAL(); break; + case 35: result = ValidateCAST(); break; + case 36: result = ValidateSquare(); break; + case 37: result = ValidateRC2(); break; + case 38: result = ValidateRC6(); break; + case 39: result = ValidateMARS(); break; + case 40: result = ValidateRW(); break; + case 41: result = ValidateMD2(); break; + case 42: result = ValidateNR(); break; + case 43: result = ValidateMQV(); break; + case 44: result = ValidateRijndael(); break; + case 45: result = ValidateTwofish(); break; + case 46: result = ValidateSerpent(); break; + case 47: result = ValidateCipherModes(); break; + case 48: result = ValidateCRC32(); break; + case 49: result = ValidateECDSA(); break; + case 50: result = ValidateXTR_DH(); break; + case 51: result = ValidateSKIPJACK(); break; + case 52: result = ValidateSHA2(); break; + case 53: result = ValidatePanama(); break; + case 54: result = ValidateAdler32(); break; + case 55: result = ValidateMD4(); break; + case 56: result = ValidatePBKDF(); break; + case 57: result = ValidateESIGN(); break; + case 58: result = ValidateDLIES(); break; + case 59: result = ValidateBaseCode(); break; + case 60: result = ValidateSHACAL2(); break; + case 61: result = ValidateCamellia(); break; + case 62: result = ValidateWhirlpool(); break; + case 63: result = ValidateTTMAC(); break; + case 64: result = ValidateSalsa(); break; + case 65: result = ValidateSosemanuk(); break; + case 66: result = ValidateVMAC(); break; + case 67: result = ValidateCCM(); break; + case 68: result = ValidateGCM(); break; + case 69: result = ValidateCMAC(); break; + case 72: result = ValidateHKDF(); break; + default: return false; } time_t endTime = time(NULL); - cout << "\nTest ended at " << asctime(localtime(&endTime)); - cout << "Seed used was: " << seed << endl; + std::cout << "\nTest ended at " << asctime(localtime(&endTime)); + std::cout << "Seed used was: " << seed << std::endl; return result; } diff --git a/validat0.cpp b/validat0.cpp new file mode 100644 index 00000000..a612c8b2 --- /dev/null +++ b/validat0.cpp @@ -0,0 +1,659 @@ +// validat0.cpp - written and placed in the public domain by Wei Dai and Jeffrey Walton + +#include "pch.h" + +#include "config.h" +#include "stdcpp.h" +#include "misc.h" +#include "integer.h" + +#include "validate.h" + +#include + +USING_NAMESPACE(CryptoPP) + +#if GCC_DIAGNOSTIC_AWARE +# pragma GCC diagnostic ignored "-Wunused-value" +#endif + +bool TestSettings() +{ + bool pass = true; + + std::cout << "\nTesting Settings...\n\n"; + + word32 w; + memcpy_s(&w, sizeof(w), "\x01\x02\x03\x04", 4); + + if (w == 0x04030201L) + { +#ifdef IS_LITTLE_ENDIAN + std::cout << "passed: "; +#else + std::cout << "FAILED: "; + pass = false; +#endif + std::cout << "Your machine is little endian.\n"; + } + else if (w == 0x01020304L) + { +#ifndef IS_LITTLE_ENDIAN + std::cout << "passed: "; +#else + std::cout << "FAILED: "; + pass = false; +#endif + std::cout << "Your machine is big endian.\n"; + } + else + { + std::cout << "FAILED: Your machine is neither big endian nor little endian.\n"; + pass = false; + } + +#ifdef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS + byte testvals[10] = {1,2,2,3,3,3,3,2,2,1}; + if (*(word32 *)(testvals+3) == 0x03030303 && *(word64 *)(testvals+1) == W64LIT(0x0202030303030202)) + std::cout << "passed: Your machine allows unaligned data access.\n"; + else + { + std::cout << "FAILED: Unaligned data access gave incorrect results.\n"; + pass = false; + } +#else + std::cout << "passed: CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS is not defined. Will restrict to aligned data access.\n"; +#endif + + if (sizeof(byte) == 1) + std::cout << "passed: "; + else + { + std::cout << "FAILED: "; + pass = false; + } + std::cout << "sizeof(byte) == " << sizeof(byte) << std::endl; + + if (sizeof(word16) == 2) + std::cout << "passed: "; + else + { + std::cout << "FAILED: "; + pass = false; + } + std::cout << "sizeof(word16) == " << sizeof(word16) << std::endl; + + if (sizeof(word32) == 4) + std::cout << "passed: "; + else + { + std::cout << "FAILED: "; + pass = false; + } + std::cout << "sizeof(word32) == " << sizeof(word32) << std::endl; + + if (sizeof(word64) == 8) + std::cout << "passed: "; + else + { + std::cout << "FAILED: "; + pass = false; + } + std::cout << "sizeof(word64) == " << sizeof(word64) << std::endl; + +#ifdef CRYPTOPP_WORD128_AVAILABLE + if (sizeof(word128) == 16) + std::cout << "passed: "; + else + { + std::cout << "FAILED: "; + pass = false; + } + std::cout << "sizeof(word128) == " << sizeof(word128) << std::endl; +#endif + + if (sizeof(word) == 2*sizeof(hword) +#ifdef CRYPTOPP_NATIVE_DWORD_AVAILABLE + && sizeof(dword) == 2*sizeof(word) +#endif + ) + std::cout << "passed: "; + else + { + std::cout << "FAILED: "; + pass = false; + } + std::cout << "sizeof(hword) == " << sizeof(hword) << ", sizeof(word) == " << sizeof(word); +#ifdef CRYPTOPP_NATIVE_DWORD_AVAILABLE + std::cout << ", sizeof(dword) == " << sizeof(dword); +#endif + std::cout << std::endl; + +#ifdef CRYPTOPP_CPUID_AVAILABLE + bool hasMMX = HasMMX(); + bool hasISSE = HasSSE(); + bool hasSSE2 = HasSSE2(); + bool hasSSSE3 = HasSSSE3(); + bool isP4 = IsP4(); + int cacheLineSize = GetCacheLineSize(); + + if ((isP4 && (!hasMMX || !hasSSE2)) || (hasSSE2 && !hasMMX) || (cacheLineSize < 16 || cacheLineSize > 256 || !IsPowerOf2(cacheLineSize))) + { + std::cout << "FAILED: "; + pass = false; + } + else + std::cout << "passed: "; + + std::cout << "hasMMX == " << hasMMX << ", hasISSE == " << hasISSE << ", hasSSE2 == " << hasSSE2 << ", hasSSSE3 == " << hasSSSE3 << ", hasAESNI == " << HasAESNI() << ", hasCLMUL == " << HasCLMUL() << ", isP4 == " << isP4 << ", cacheLineSize == " << cacheLineSize; + std::cout << ", AESNI_INTRINSICS == " << CRYPTOPP_BOOL_AESNI_INTRINSICS_AVAILABLE << std::endl; +#endif + + if (!pass) + { + std::cout << "Some critical setting in config.h is in error. Please fix it and recompile." << std::endl; + abort(); + } + return pass; +} + +bool TestRotate() +{ + bool pass = true; + + std::cout << "\nTesting rotate...\n\n"; + std::cout << (!pass ? "FAILED " : "passed ") << " left rotate" << std::endl; + std::cout << (!pass ? "FAILED " : "passed ") << " right rotate" << std::endl; + + return pass; +} + +bool TestConversion() +{ + bool pass = true; + + std::cout << "\nTesting conversions...\n\n"; + + /********** signed char **********/ + { + signed char v1, v2; bool p = true; + v1 = -1; p = SafeConvert(v1, v2) && p; + v1 = 0; p = SafeConvert(v1, v2) && p; + v1 = 1; p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::min(); p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::min() + 1; p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::max(); p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::max() - 1; p = SafeConvert(v1, v2) && p; + + std::cout << (!p ? "FAILED " : "passed ") << " signed char" << std::endl; + pass &= p; + } + + /********** signed char overflow **********/ + { + signed char v; bool p = true; + { + signed short v1 = std::numeric_limits::min(); p = !SafeConvert(v1, v) && p; + signed short v2 = std::numeric_limits::max(); p = !SafeConvert(v2, v) && p; + unsigned short v3 = std::numeric_limits::max(); p = !SafeConvert(v3, v) && p; + } + { + signed int v1 = std::numeric_limits::min(); p = !SafeConvert(v1, v) && p; + signed int v2 = std::numeric_limits::max(); p = !SafeConvert(v2, v) && p; + unsigned int v3 = std::numeric_limits::max(); p = !SafeConvert(v3, v) && p; + } + + { + signed long v1 = std::numeric_limits::min(); p = !SafeConvert(v1, v) && p; + signed long v2 = std::numeric_limits::max(); p = !SafeConvert(v2, v) && p; + unsigned long v3 = std::numeric_limits::max(); p = !SafeConvert(v3, v) && p; + } + { + signed long long v1 = std::numeric_limits::min(); p = !SafeConvert(v1, v) && p; + signed long long v2 = std::numeric_limits::max(); p = !SafeConvert(v2, v) && p; + unsigned long long v3 = std::numeric_limits::max(); p = !SafeConvert(v3, v) && p; + } + + std::cout << (!p ? "FAILED " : "passed ") << " signed char overflow" << std::endl; + pass &= p; + } + + /********** unsigned char **********/ + { + unsigned char v1, v2; bool p = true; + v1 = 0; p = SafeConvert(v1, v2) && p; + v1 = 1; p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::max(); p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::max() - 1; p = SafeConvert(v1, v2) && p; + + std::cout << (!p ? "FAILED " : "passed ") << " unsigned char" << std::endl; + pass &= p; + } + + /********** unsigned char overflow **********/ + { + unsigned char v; bool p = true; + { + unsigned short v1 = std::numeric_limits::max(); p = !SafeConvert(v1, v) && p; + } + { + unsigned int v1 = std::numeric_limits::max(); p = !SafeConvert(v1, v) && p; + } + { + unsigned long v1 = std::numeric_limits::max(); p = !SafeConvert(v1, v) && p; + } + { + unsigned long long v1 = std::numeric_limits::max(); p = !SafeConvert(v1, v) && p; + } + + std::cout << (!p ? "FAILED " : "passed ") << " unsigned char overflow" << std::endl; + pass &= p; + } + + /********** signed short **********/ + { + signed short v1, v2; bool p = true; + v1 = -1; p = SafeConvert(v1, v2) && p; + v1 = 0; p = SafeConvert(v1, v2) && p; + v1 = 1; p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::min(); p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::min() + 1; p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::max(); p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::max() - 1; p = SafeConvert(v1, v2) && p; + + std::cout << (!p ? "FAILED " : "passed ") << " signed short" << std::endl; + pass &= p; + } + + /********** signed short overflow **********/ + { + signed short v; bool p = true; + { + signed int v1 = std::numeric_limits::min(); p = !SafeConvert(v1, v) && p; + signed int v2 = std::numeric_limits::max(); p = !SafeConvert(v2, v) && p; + unsigned int v3 = std::numeric_limits::max(); p = !SafeConvert(v3, v) && p; + } + { + signed long v1 = std::numeric_limits::min(); p = !SafeConvert(v1, v) && p; + signed long v2 = std::numeric_limits::max(); p = !SafeConvert(v2, v) && p; + unsigned long v3 = std::numeric_limits::max(); p = !SafeConvert(v3, v) && p; + } + { + signed long long v1 = std::numeric_limits::min(); p = !SafeConvert(v1, v) && p; + signed long long v2 = std::numeric_limits::max(); p = !SafeConvert(v2, v) && p; + unsigned long long v3 = std::numeric_limits::max(); p = !SafeConvert(v3, v) && p; + } + + std::cout << (!p ? "FAILED " : "passed ") << " signed short overflow" << std::endl; + pass &= p; + } + + /********** unsigned short **********/ + { + unsigned short v1, v2; bool p = true; + v1 = 0; p = SafeConvert(v1, v2) && p; + v1 = 1; p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::max(); p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::max() - 1; p = SafeConvert(v1, v2) && p; + + std::cout << (!p ? "FAILED " : "passed ") << " unsigned short" << std::endl; + pass &= p; + } + + /********** unsigned short overflow **********/ + { + unsigned short v; bool p = true; + { + unsigned int v1 = std::numeric_limits::max(); p = !SafeConvert(v1, v) && p; + } + { + unsigned long v1 = std::numeric_limits::max(); p = !SafeConvert(v1, v) && p; + } + { + unsigned long long v1 = std::numeric_limits::max(); p = !SafeConvert(v1, v) && p; + } + + std::cout << (!p ? "FAILED " : "passed ") << " unsigned short overflow" << std::endl; + pass &= p; + } + + /********** signed int **********/ + { + signed int v1, v2; bool p = true; + v1 = -1; p = SafeConvert(v1, v2) && p; + v1 = 0; p = SafeConvert(v1, v2) && p; + v1 = 1; p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::min(); p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::min() + 1; p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::max(); p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::max() - 1; p = SafeConvert(v1, v2) && p; + + std::cout << (!p ? "FAILED " : "passed ") << " signed int" << std::endl; + pass &= p; + } + + /********** signed int overflow **********/ + { + signed int v; bool p = true; + { + signed long v1 = std::numeric_limits::min(); p = !SafeConvert(v1, v) && p; + signed long v2 = std::numeric_limits::max(); p = !SafeConvert(v2, v) && p; + unsigned long v3 = std::numeric_limits::max(); p = !SafeConvert(v3, v) && p; + } + { + signed long long v1 = std::numeric_limits::min(); p = !SafeConvert(v1, v) && p; + signed long long v2 = std::numeric_limits::max(); p = !SafeConvert(v2, v) && p; + unsigned long long v3 = std::numeric_limits::max(); p = !SafeConvert(v3, v) && p; + } + + std::cout << (!p ? "FAILED " : "passed ") << " signed int overflow" << std::endl; + pass &= p; + } + + /********** unsigned int **********/ + { + unsigned int v1, v2; bool p = true; + v1 = 0; p = SafeConvert(v1, v2) && p; + v1 = 1; p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::max(); p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::max() - 1; p = SafeConvert(v1, v2) && p; + + std::cout << (!p ? "FAILED " : "passed ") << " unsigned int" << std::endl; + pass &= p; + } + + /********** unsigned int overflow **********/ + { + unsigned int v; bool p = true; + { + unsigned long v1 = std::numeric_limits::max(); p = !SafeConvert(v1, v) && p; + } + { + unsigned long long v1 = std::numeric_limits::max(); p = !SafeConvert(v1, v) && p; + } + + std::cout << (!p ? "FAILED " : "passed ") << " unsigned int overflow" << std::endl; + pass &= p; + } + + /********** signed long **********/ + { + signed long v1, v2; bool p = true; + v1 = -1; p = SafeConvert(v1, v2) && p; + v1 = 0; p = SafeConvert(v1, v2) && p; + v1 = 1; p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::min(); p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::min() + 1; p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::max(); p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::max() - 1; p = SafeConvert(v1, v2) && p; + + std::cout << (!p ? "FAILED " : "passed ") << " signed long" << std::endl; + pass &= p; + } + + /********** signed long overflow **********/ + { + if(sizeof(signed long) != sizeof(signed long long)) + { + signed long v; bool p = true; + { + signed long long v1 = std::numeric_limits::min(); p = !SafeConvert(v1, v) && p; + signed long long v2 = std::numeric_limits::max(); p = !SafeConvert(v2, v) && p; + unsigned long long v3 = std::numeric_limits::max(); p = !SafeConvert(v3, v) && p; + } + + std::cout << (!p ? "FAILED " : "passed ") << " signed long overflow" << std::endl; + pass &= p; + } + else + { + std::cout << "passed signed long overflow (skipped due to range of types)" << std::endl; + } + } + + /********** unsigned long **********/ + { + unsigned long v1, v2; bool p = true; + v1 = 0; p = SafeConvert(v1, v2) && p; + v1 = 1; p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::max(); p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::max() - 1; p = SafeConvert(v1, v2) && p; + + std::cout << (!p ? "FAILED " : "passed ") << " unsigned long" << std::endl; + pass &= p; + } + + /********** unsigned long overflow **********/ + { + if(sizeof(unsigned long) != sizeof(unsigned long long)) + { + unsigned long v; bool p = true; + { + unsigned long long v1 = std::numeric_limits::max(); p = !SafeConvert(v1, v) && p; + } + + std::cout << (!p ? "FAILED " : "passed ") << " unsigned long overflow" << std::endl; + pass &= p; + } + else + { + std::cout << "passed unsigned long overflow (skipped due to range of types)" << std::endl; + } + } + + /********** signed long long **********/ + { + signed long long v1, v2; bool p = true; + v1 = -1; p = SafeConvert(v1, v2) && p; + v1 = 0; p = SafeConvert(v1, v2) && p; + v1 = 1; p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::min(); p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::min() + 1; p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::max(); p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::max() - 1; p = SafeConvert(v1, v2) && p; + + std::cout << (!p ? "FAILED " : "passed ") << " signed long long" << std::endl; + pass &= p; + } + + /********** unsigned long long **********/ + { + unsigned long long v1, v2; bool p = true; + v1 = 0; p = SafeConvert(v1, v2) && p; + v1 = 1; p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::max(); p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::max() - 1; p = SafeConvert(v1, v2) && p; + + std::cout << (!p ? "FAILED " : "passed ") << " unsigned long long" << std::endl; + pass &= p; + } + + /********** ssize_t **********/ + { + ssize_t v1, v2; bool p = true; + v1 = -1; p = SafeConvert(v1, v2) && p; + v1 = 0; p = SafeConvert(v1, v2) && p; + v1 = 1; p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::min(); p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::min() + 1; p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::max(); p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::max() - 1; p = SafeConvert(v1, v2) && p; + + std::cout << (!p ? "FAILED " : "passed ") << " ssize_t" << std::endl; + pass &= p; + } + + /********** size_t **********/ + { + size_t v1, v2; bool p = true; + v1 = 0; p = SafeConvert(v1, v2) && p; + v1 = 1; p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::max(); p = SafeConvert(v1, v2) && p; + v1 = std::numeric_limits::max() - 1; p = SafeConvert(v1, v2) && p; + + std::cout << (!p ? "FAILED " : "passed ") << " size_t" << std::endl; + pass &= p; + } + +#if 0 + { + Integer v1; signed char v2; bool p = true; + v1 = -1; p = SafeConvert(v1, v2) && p; + v1 = 0; p = SafeConvert(v1, v2) && p; + v1 = 1; p = SafeConvert(v1, v2) && p; + v1 = Integer((signed long)std::numeric_limits::min()); p = SafeConvert(v1, v2) && p; + v1 = Integer((signed long)std::numeric_limits::min()) + 1; p = SafeConvert(v1, v2) && p; + v1 = Integer((signed long)std::numeric_limits::min()) - 1; p = !SafeConvert(v1, v2) && p; + v1 = Integer((signed long)std::numeric_limits::max()); p = SafeConvert(v1, v2) && p; + v1 = Integer((signed long)std::numeric_limits::max()) - 1; p = SafeConvert(v1, v2) && p; + v1 = Integer((signed long)std::numeric_limits::max()) + 1; p = !SafeConvert(v1, v2) && p; + + std::cout << (!p ? "FAILED " : "passed ") << " Integer to signed char" << std::endl; + pass &= p; + } + + { + Integer v1; unsigned char v2; bool p = true; + v1 = 0; p = SafeConvert(v1, v2) && p; + v1 = 1; p = SafeConvert(v1, v2) && p; + v1 = Integer((signed long)std::numeric_limits::max()); p = SafeConvert(v1, v2) && p; + v1 = Integer((signed long)std::numeric_limits::max()) - 1; p = SafeConvert(v1, v2) && p; + v1 = Integer((signed long)std::numeric_limits::max()) + 1; p = !SafeConvert(v1, v2) && p; + + std::cout << (!p ? "FAILED " : "passed ") << " Integer to unsigned char" << std::endl; + pass &= p; + } + + { + Integer v1; signed short v2; bool p = true; + v1 = -1; p = SafeConvert(v1, v2) && p; + v1 = 0; p = SafeConvert(v1, v2) && p; + v1 = 1; p = SafeConvert(v1, v2) && p; + v1 = Integer((signed long)std::numeric_limits::min()); p = SafeConvert(v1, v2) && p; + v1 = Integer((signed long)std::numeric_limits::min()) + 1; p = SafeConvert(v1, v2) && p; + v1 = Integer((signed long)std::numeric_limits::min()) - 1; p = !SafeConvert(v1, v2) && p; + v1 = Integer((signed long)std::numeric_limits::max()); p = SafeConvert(v1, v2) && p; + v1 = Integer((signed long)std::numeric_limits::max()) - 1; p = SafeConvert(v1, v2) && p; + v1 = Integer((signed long)std::numeric_limits::max()) + 1; p = !SafeConvert(v1, v2) && p; + + std::cout << (!p ? "FAILED " : "passed ") << " Integer to signed short" << std::endl; + pass &= p; + } + + { + Integer v1; unsigned short v2; bool p = true; + v1 = 0; p = SafeConvert(v1, v2) && p; + v1 = 1; p = SafeConvert(v1, v2) && p; + v1 = Integer((signed long)std::numeric_limits::max()); p = SafeConvert(v1, v2) && p; + v1 = Integer((signed long)std::numeric_limits::max()) - 1; p = SafeConvert(v1, v2) && p; + v1 = Integer((signed long)std::numeric_limits::max()) + 1; p = !SafeConvert(v1, v2) && p; + + std::cout << (!p ? "FAILED " : "passed ") << " Integer to unsigned short" << std::endl; + pass &= p; + } + + { + Integer v1; signed int v2; bool p = true; + v1 = -1; p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = 0; p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = 1; p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = Integer((signed long)std::numeric_limits::min()); p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + std::cout << "Limit: " << (int) std::numeric_limits::min() << std::endl; + std::cout << "Digits: " << (int) std::numeric_limits::digits << std::endl; + std::cout << "Value: " << v1 << std::endl; + std::cout << "BitCount: " << v1.BitCount() << std::endl; + + v1 = Integer((signed long)std::numeric_limits::min()) + 1; p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = Integer((signed long)std::numeric_limits::min()) - 1; p = !SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = Integer((signed long)std::numeric_limits::max()); p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = Integer((signed long)std::numeric_limits::max()) - 1; p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = Integer((signed long)std::numeric_limits::max()) + 1; p = !SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + + std::cout << (!p ? "FAILED " : "passed ") << " Integer to signed int" << std::endl; + pass &= p; + } + + { + Integer v1; unsigned int v2; bool p = true; + v1 = 0; p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = 1; p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = Integer((signed long)std::numeric_limits::max()); p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = Integer((signed long)std::numeric_limits::max()) - 1; p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = Integer((signed long)std::numeric_limits::max()) + 1; p = !SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + + std::cout << (!p ? "FAILED " : "passed ") << " Integer to unsigned int" << std::endl; + pass &= p; + } + + { + Integer v1; signed long v2; bool p = true; + v1 = -1; p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = 0; p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = 1; p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = Integer((signed long)std::numeric_limits::min()); p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = Integer((signed long)std::numeric_limits::min()) + 1; p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = Integer((signed long)std::numeric_limits::min()) - 1; p = !SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = Integer((signed long)std::numeric_limits::max()); p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = Integer((signed long)std::numeric_limits::max()) - 1; p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = Integer((signed long)std::numeric_limits::max()) + 1; p = !SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + + std::cout << (!p ? "FAILED " : "passed ") << " Integer to signed long" << std::endl; + pass &= p; + } + + { + Integer v1; unsigned long v2; bool p = true; + v1 = 0; p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = 1; p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = Integer((word64)std::numeric_limits::max()); p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = Integer((word64)std::numeric_limits::max()) - 1; p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = Integer((word64)std::numeric_limits::max()) + 1; p = !SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + + std::cout << (!p ? "FAILED " : "passed ") << " Integer to signed long" << std::endl; + pass &= p; + } + + { + Integer v1; ssize_t v2; bool p = true; + v1 = -1; p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = 0; p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = 1; p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = Integer((signed long)std::numeric_limits::min()); p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = Integer((signed long)std::numeric_limits::min()) + 1; p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = Integer((signed long)std::numeric_limits::min()) - 1; p = !SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = Integer((signed long)std::numeric_limits::max()); p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = Integer((signed long)std::numeric_limits::max()) - 1; p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = Integer((signed long)std::numeric_limits::max()) + 1; p = !SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + + std::cout << (!p ? "FAILED " : "passed ") << " Integer to signed ssize_t" << std::endl; + pass &= p; + } + + { + Integer v1; size_t v2; bool p = true; + v1 = 0; p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = 1; p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = Integer((word64)std::numeric_limits::max()); p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = Integer((word64)std::numeric_limits::max()) - 1; p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = Integer((word64)std::numeric_limits::max()) + 1; p = !SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + + std::cout << (!p ? "FAILED " : "passed ") << " Integer to size_t" << std::endl; + pass &= p; + } + + { + CRYPTOPP_COMPILE_ASSERT(sizeof(word64) >= sizeof(unsigned long long)); + Integer v1; unsigned long long v2; bool p = true; + v1 = 0; p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = 1; p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = Integer((word64)std::numeric_limits::max()); p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = Integer((word64)std::numeric_limits::max()) - 1; p = SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + v1 = Integer((word64)std::numeric_limits::max()) + 1; p = !SafeConvert(v1, v2) && p; CRYPTOPP_ASSERT(p); + + std::cout << (!p ? "FAILED " : "passed ") << " Integer to unsigned long long" << std::endl; + pass &= p; + } +#endif + + return pass; +} diff --git a/validat1.cpp b/validat1.cpp index 7bf3a353..9af17a92 100644 --- a/validat1.cpp +++ b/validat1.cpp @@ -47,12 +47,11 @@ USING_NAMESPACE(CryptoPP) -// using CryptoPP::auto_ptr; -using std::auto_ptr; - bool ValidateAll(bool thorough) { bool pass=TestSettings(); + pass=TestRotate() && pass; + pass=TestConversion() && pass; pass=TestOS_RNG() && pass; pass=ValidateCRC32() && pass; @@ -131,146 +130,6 @@ bool ValidateAll(bool thorough) return pass; } -bool TestSettings() -{ - bool pass = true; - - std::cout << "\nTesting Settings...\n\n"; - - word32 w; - memcpy_s(&w, sizeof(w), "\x01\x02\x03\x04", 4); - - if (w == 0x04030201L) - { -#ifdef IS_LITTLE_ENDIAN - std::cout << "passed: "; -#else - std::cout << "FAILED: "; - pass = false; -#endif - std::cout << "Your machine is little endian.\n"; - } - else if (w == 0x01020304L) - { -#ifndef IS_LITTLE_ENDIAN - std::cout << "passed: "; -#else - std::cout << "FAILED: "; - pass = false; -#endif - std::cout << "Your machine is big endian.\n"; - } - else - { - std::cout << "FAILED: Your machine is neither big endian nor little endian.\n"; - pass = false; - } - -#ifdef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS - byte testvals[10] = {1,2,2,3,3,3,3,2,2,1}; - if (*(word32 *)(testvals+3) == 0x03030303 && *(word64 *)(testvals+1) == W64LIT(0x0202030303030202)) - std::cout << "passed: Your machine allows unaligned data access.\n"; - else - { - std::cout << "FAILED: Unaligned data access gave incorrect results.\n"; - pass = false; - } -#else - std::cout << "passed: CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS is not defined. Will restrict to aligned data access.\n"; -#endif - - if (sizeof(byte) == 1) - std::cout << "passed: "; - else - { - std::cout << "FAILED: "; - pass = false; - } - std::cout << "sizeof(byte) == " << sizeof(byte) << std::endl; - - if (sizeof(word16) == 2) - std::cout << "passed: "; - else - { - std::cout << "FAILED: "; - pass = false; - } - std::cout << "sizeof(word16) == " << sizeof(word16) << std::endl; - - if (sizeof(word32) == 4) - std::cout << "passed: "; - else - { - std::cout << "FAILED: "; - pass = false; - } - std::cout << "sizeof(word32) == " << sizeof(word32) << std::endl; - - if (sizeof(word64) == 8) - std::cout << "passed: "; - else - { - std::cout << "FAILED: "; - pass = false; - } - std::cout << "sizeof(word64) == " << sizeof(word64) << std::endl; - -#ifdef CRYPTOPP_WORD128_AVAILABLE - if (sizeof(word128) == 16) - std::cout << "passed: "; - else - { - std::cout << "FAILED: "; - pass = false; - } - std::cout << "sizeof(word128) == " << sizeof(word128) << std::endl; -#endif - - if (sizeof(word) == 2*sizeof(hword) -#ifdef CRYPTOPP_NATIVE_DWORD_AVAILABLE - && sizeof(dword) == 2*sizeof(word) -#endif - ) - std::cout << "passed: "; - else - { - std::cout << "FAILED: "; - pass = false; - } - std::cout << "sizeof(hword) == " << sizeof(hword) << ", sizeof(word) == " << sizeof(word); -#ifdef CRYPTOPP_NATIVE_DWORD_AVAILABLE - std::cout << ", sizeof(dword) == " << sizeof(dword); -#endif - std::cout << std::endl; - -#ifdef CRYPTOPP_CPUID_AVAILABLE - bool hasMMX = HasMMX(); - bool hasISSE = HasSSE(); - bool hasSSE2 = HasSSE2(); - bool hasSSSE3 = HasSSSE3(); - bool isP4 = IsP4(); - int cacheLineSize = GetCacheLineSize(); - - if ((isP4 && (!hasMMX || !hasSSE2)) || (hasSSE2 && !hasMMX) || (cacheLineSize < 16 || cacheLineSize > 256 || !IsPowerOf2(cacheLineSize))) - { - std::cout << "FAILED: "; - pass = false; - } - else - std::cout << "passed: "; - - std::cout << "hasMMX == " << hasMMX << ", hasISSE == " << hasISSE << ", hasSSE2 == " << hasSSE2 << ", hasSSSE3 == " << hasSSSE3 << ", hasAESNI == " << HasAESNI() << ", hasCLMUL == " << HasCLMUL() << ", isP4 == " << isP4 << ", cacheLineSize == " << cacheLineSize; - std::cout << ", AESNI_INTRINSICS == " << CRYPTOPP_BOOL_AESNI_INTRINSICS_AVAILABLE << std::endl; -#endif - - if (!pass) - { - std::cout << "Some critical setting in config.h is in error. Please fix it and recompile." << std::endl; - abort(); - } - return pass; -} - bool TestOS_RNG() { bool pass = true; @@ -1313,7 +1172,7 @@ bool ValidateBaseCode() "39445230745055316462580A324E6E6132397A6433742F6734654C6A354F586D352B6A7036757673" "3765377638504879382F5431397666342B6672372F50332B0A"; - std::cout << "\nBase64, base32 and hex coding validation suite running...\n\n"; + std::cout << "\nBase64, base32 and std::hex coding validation suite running...\n\n"; fail = !TestFilter(HexEncoder().Ref(), data, 255, (const byte *)hexEncoded, strlen(hexEncoded)); std::cout << (fail ? "FAILED " : "passed "); diff --git a/validat2.cpp b/validat2.cpp index afabe43e..c2eee415 100644 --- a/validat2.cpp +++ b/validat2.cpp @@ -36,7 +36,6 @@ #include "validate.h" USING_NAMESPACE(CryptoPP) -USING_NAMESPACE(std) class FixedRNG : public RandomNumberGenerator { @@ -78,7 +77,7 @@ bool ValidateBBS() std::cout << (fail ? "FAILED " : "passed "); for (j=0;j<20;j++) - std::cout << setw(2) << setfill('0') << hex << (int)buf[j]; + std::cout << std::setw(2) << std::setfill('0') << std::hex << (int)buf[j]; std::cout << std::endl; bbs.Seek(10); @@ -88,7 +87,7 @@ bool ValidateBBS() std::cout << (fail ? "FAILED " : "passed "); for (j=0;j<10;j++) - std::cout << setw(2) << setfill('0') << hex << (int)buf[j]; + std::cout << std::setw(2) << std::setfill('0') << std::hex << (int)buf[j]; std::cout << std::endl; bbs.Seek(1234567); @@ -98,7 +97,7 @@ bool ValidateBBS() std::cout << (fail ? "FAILED " : "passed "); for (j=0;j<20;j++) - std::cout << setw(2) << setfill('0') << hex << (int)buf[j]; + std::cout << std::setw(2) << std::setfill('0') << std::hex << (int)buf[j]; std::cout << std::endl; return pass; @@ -585,7 +584,7 @@ bool ValidateECP() { DL_GroupParameters_EC params(oid); bool fail = !params.Validate(GlobalRNG(), 2); - std::cout << (fail ? "FAILED" : "passed") << " " << dec << params.GetCurve().GetField().MaxElementBitLength() << " bits" << std::endl; + std::cout << (fail ? "FAILED" : "passed") << " " << std::dec << params.GetCurve().GetField().MaxElementBitLength() << " bits" << std::endl; pass = pass && !fail; } diff --git a/validat3.cpp b/validat3.cpp index 7680722b..cc0cee1e 100644 --- a/validat3.cpp +++ b/validat3.cpp @@ -30,7 +30,6 @@ #include "validate.h" USING_NAMESPACE(CryptoPP) -USING_NAMESPACE(std) struct HashTestTuple { @@ -53,19 +52,19 @@ bool HashModuleTest(HashTransformation &md, const HashTestTuple *testSet, unsign for (unsigned int i=0; i