Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:55:10

0001 // validate.h - originally written and placed in the public domain by Wei Dai

0002 //              CryptoPP::Test namespace added by JW in February 2017

0003 
0004 #ifndef CRYPTOPP_VALIDATE_H
0005 #define CRYPTOPP_VALIDATE_H
0006 
0007 #include "cryptlib.h"
0008 #include "misc.h"
0009 #include "files.h"
0010 #include "argnames.h"
0011 #include "algparam.h"
0012 #include "hex.h"
0013 
0014 #include <iostream>
0015 #include <sstream>
0016 #include <iomanip>
0017 #include <ctime>
0018 #include <cctype>
0019 
0020 NAMESPACE_BEGIN(CryptoPP)
0021 NAMESPACE_BEGIN(Test)
0022 
0023 // A hint to help locate TestData/ and TestVectors/ after install. Due to

0024 // execve the path can be malicious. If the path is ficticous then we move

0025 // onto the next potential path. Also note we only read from the path; we

0026 // never write through it. Storage for the string is in test.cpp.

0027 extern std::string g_argvPathHint;
0028 
0029 bool ValidateAll(bool thorough);
0030 bool TestSettings();
0031 bool TestOS_RNG();
0032 // bool TestSecRandom();

0033 bool TestRandomPool();
0034 #if !defined(NO_OS_DEPENDENCE)
0035 bool TestAutoSeededX917();
0036 #endif
0037 #if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
0038 bool TestRDRAND();
0039 bool TestRDSEED();
0040 bool TestPadlockRNG();
0041 #endif
0042 #if (CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64)
0043 bool TestDARN();
0044 #endif
0045 bool ValidateBaseCode();
0046 bool ValidateEncoder();
0047 bool ValidateCRC32();
0048 bool ValidateCRC32C();
0049 bool ValidateAdler32();
0050 bool ValidateMD2();
0051 bool ValidateMD4();
0052 bool ValidateMD5();
0053 bool ValidateSHA();
0054 bool ValidateSHA2();
0055 bool ValidateSHA3();
0056 bool ValidateSHAKE();      // output <= r, where r is blocksize

0057 bool ValidateSHAKE_XOF();  // output > r, needs hand crafted tests

0058 bool ValidateKeccak();
0059 bool ValidateTiger();
0060 bool ValidateRIPEMD();
0061 bool ValidatePanama();
0062 bool ValidateWhirlpool();
0063 bool ValidateLSH();
0064 
0065 bool ValidateSM3();
0066 bool ValidateBLAKE2s();
0067 bool ValidateBLAKE2b();
0068 bool ValidatePoly1305();
0069 bool ValidateSipHash();
0070 
0071 bool ValidateHMAC();
0072 bool ValidateTTMAC();
0073 
0074 bool ValidateCipherModes();
0075 bool ValidatePBKDF();
0076 bool ValidateHKDF();
0077 bool ValidateScrypt();
0078 
0079 bool ValidateDES();
0080 bool ValidateIDEA();
0081 bool ValidateSAFER();
0082 bool ValidateRC2();
0083 bool ValidateARC4();
0084 
0085 bool ValidateRC5();
0086 bool ValidateBlowfish();
0087 bool ValidateBlowfishCompat();
0088 bool ValidateThreeWay();
0089 bool ValidateGOST();
0090 bool ValidateSHARK();
0091 bool ValidateSEAL();
0092 bool ValidateCAST();
0093 bool ValidateSquare();
0094 bool ValidateSKIPJACK();
0095 bool ValidateRC6();
0096 bool ValidateMARS();
0097 bool ValidateRijndael();
0098 bool ValidateTwofish();
0099 bool ValidateSerpent();
0100 bool ValidateSHACAL2();
0101 bool ValidateARIA();
0102 bool ValidateSIMECK();
0103 bool ValidateCHAM();
0104 bool ValidateHIGHT();
0105 bool ValidateLEA();
0106 bool ValidateSIMON();
0107 bool ValidateSPECK();
0108 bool ValidateCamellia();
0109 
0110 bool ValidateHC128();
0111 bool ValidateHC256();
0112 bool ValidateRabbit();
0113 bool ValidateSalsa();
0114 bool ValidateChaCha();
0115 bool ValidateChaChaTLS();
0116 bool ValidateSosemanuk();
0117 
0118 bool ValidateVMAC();
0119 bool ValidateCCM();
0120 bool ValidateGCM();
0121 bool ValidateXTS();
0122 bool ValidateCMAC();
0123 
0124 bool ValidateBBS();
0125 bool ValidateDH();
0126 bool ValidateMQV();
0127 bool ValidateHMQV();
0128 bool ValidateFHMQV();
0129 bool ValidateRSA();
0130 bool ValidateElGamal();
0131 bool ValidateDLIES();
0132 bool ValidateNR();
0133 bool ValidateDSA(bool thorough);
0134 bool ValidateLUC();
0135 bool ValidateLUC_DL();
0136 bool ValidateLUC_DH();
0137 bool ValidateXTR_DH();
0138 bool ValidateRabin();
0139 bool ValidateRW();
0140 bool ValidateECP();
0141 bool ValidateEC2N();
0142 bool ValidateECDSA();
0143 bool ValidateECDSA_RFC6979();
0144 bool ValidateECGDSA(bool thorough);
0145 bool ValidateESIGN();
0146 
0147 bool ValidateHashDRBG();
0148 bool ValidateHmacDRBG();
0149 
0150 bool TestX25519();
0151 bool TestEd25519();
0152 bool ValidateX25519();
0153 bool ValidateEd25519();
0154 bool ValidateNaCl();
0155 
0156 // If CRYPTOPP_DEBUG or CRYPTOPP_COVERAGE is in effect, then perform additional tests

0157 #if (defined(CRYPTOPP_DEBUG) || defined(CRYPTOPP_COVERAGE)) && !defined(CRYPTOPP_IMPORTS)
0158 # define CRYPTOPP_EXTENDED_VALIDATION 1
0159 #endif
0160 
0161 #if defined(CRYPTOPP_EXTENDED_VALIDATION)
0162 // http://github.com/weidai11/cryptopp/issues/92

0163 bool TestSecBlock();
0164 // http://github.com/weidai11/cryptopp/issues/64

0165 bool TestPolynomialMod2();
0166 // http://github.com/weidai11/cryptopp/issues/336

0167 bool TestIntegerBitops();
0168 // http://github.com/weidai11/cryptopp/issues/602

0169 bool TestIntegerOps();
0170 // http://github.com/weidai11/cryptopp/issues/360

0171 bool TestRounding();
0172 // http://github.com/weidai11/cryptopp/issues/242

0173 bool TestHuffmanCodes();
0174 // http://github.com/weidai11/cryptopp/issues/346

0175 bool TestASN1Parse();
0176 bool TestASN1Functions();
0177 // https://github.com/weidai11/cryptopp/pull/334

0178 bool TestStringSink();
0179 // Additional tests due to no coverage

0180 bool TestCompressors();
0181 bool TestEncryptors();
0182 bool TestMersenne();
0183 bool TestSharing();
0184 # if defined(CRYPTOPP_ALTIVEC_AVAILABLE)
0185 bool TestAltivecOps();
0186 # endif
0187 #endif
0188 
0189 class FixedRNG : public RandomNumberGenerator
0190 {
0191 public:
0192     FixedRNG(BufferedTransformation &source) : m_source(source) {}
0193 
0194     void GenerateBlock(byte *output, size_t size)
0195     {
0196         m_source.Get(output, size);
0197     }
0198 
0199 private:
0200     BufferedTransformation &m_source;
0201 };
0202 
0203 // Safer functions on Windows for C&A, http://github.com/weidai11/cryptopp/issues/55

0204 inline std::string TimeToString(const time_t& t)
0205 {
0206 #if (CRYPTOPP_MSC_VERSION >= 1400)
0207     tm localTime;
0208     char timeBuf[64];
0209     errno_t err;
0210 
0211     err = ::localtime_s(&localTime, &t);
0212     CRYPTOPP_ASSERT(err == 0);
0213     err = ::asctime_s(timeBuf, sizeof(timeBuf), &localTime);
0214     CRYPTOPP_ASSERT(err == 0);
0215 
0216     std::string str(err == 0 ? timeBuf : "");
0217 #elif defined(__MINGW32__) || defined(__MINGW64__)
0218     char* timeString = ::asctime(::localtime(&t));
0219     std::string str(timeString ? timeString : "");
0220 #elif (_POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE || defined(_POSIX_SOURCE))
0221     tm localTime;
0222     char timeBuf[64];
0223     char* timeString = ::asctime_r(::localtime_r(&t, &localTime), timeBuf);
0224     std::string str(timeString ? timeString : "");
0225 #else
0226     char* timeString = ::asctime(::localtime(&t));
0227     std::string str(timeString ? timeString : "");
0228 #endif
0229 
0230     // Cleanup whitespace

0231     std::string::size_type pos = 0;
0232     while (!str.empty() && std::isspace(str[str.length()-1]))
0233         {str.erase(str.end()-1);}
0234     while (!str.empty() && std::string::npos != (pos = str.find("  ", pos)))
0235         {str.erase(pos, 1);}
0236 
0237     return str;
0238 }
0239 
0240 // Coverity finding

0241 template <class T, bool NON_NEGATIVE>
0242 inline T StringToValue(const std::string& str)
0243 {
0244     std::istringstream iss(str);
0245 
0246     // Arbitrary, but we need to clear a Coverity finding TAINTED_SCALAR

0247     if (iss.str().length() > 25)
0248         throw InvalidArgument(str + "' is too long");
0249 
0250     T value;
0251     iss >> std::noskipws >> value;
0252 
0253     // Use fail(), not bad()

0254     if (iss.fail())
0255         throw InvalidArgument(str + "' is not a value");
0256 
0257     if (NON_NEGATIVE && value < 0)
0258         throw InvalidArgument(str + "' is negative");
0259 
0260     return value;
0261 }
0262 
0263 // Coverity finding

0264 template<>
0265 inline int StringToValue<int, true>(const std::string& str)
0266 {
0267     Integer n(str.c_str());
0268     long l = n.ConvertToLong();
0269 
0270     int r;
0271     if (!SafeConvert(l, r))
0272         throw InvalidArgument(str + "' is not an integer value");
0273 
0274     return r;
0275 }
0276 
0277 inline std::string AddSeparator(std::string str)
0278 {
0279     if (str.empty()) return "";
0280     const char last = str[str.length()-1];
0281     if (last != '/' && last != '\\')
0282         return str + "/";
0283     return str;
0284 }
0285 
0286 // Use CRYPTOPP_DATA_DIR last. The problem this sidesteps is, finding an

0287 // old version of Crypto++ library in CRYPTOPP_DATA_DIR when the library

0288 // has been staged in DESTDIR. Using CRYPTOPP_DATA_DIR first only works

0289 // as expected when CRYPTOPP_DATA_DIR is empty before an install. We

0290 // encountered this problem rather quickly during testing of Crypto++ 8.1

0291 // when Crypto++ 8.0 was installed locally. It took some time to realize

0292 // where the old test data was coming from.

0293 static std::string GetDataDir()
0294 {
0295     std::ifstream file;
0296     std::string name, filename = "TestData/usage.dat";
0297 
0298 #ifndef CRYPTOPP_DISABLE_DATA_DIR_SEARCH
0299     // Look in $ORIGIN/../share/. This is likely a Linux install directory.

0300     name = AddSeparator(g_argvPathHint) + std::string("../share/cryptopp/") + filename;
0301     file.open(name.c_str());
0302     if (file.is_open())
0303         return AddSeparator(g_argvPathHint) + std::string("../share/cryptopp/");
0304 #endif
0305 #ifndef CRYPTOPP_DISABLE_DATA_DIR_SEARCH
0306     // Look in current working directory

0307     name = AddSeparator(g_argvPathHint) + filename;
0308     file.open(name.c_str());
0309     if (file.is_open())
0310         return AddSeparator(g_argvPathHint);
0311 #endif
0312 #ifdef CRYPTOPP_DATA_DIR
0313     // Honor CRYPTOPP_DATA_DIR. This is likely an install directory if it is not "./".

0314     name = AddSeparator(CRYPTOPP_DATA_DIR) + filename;
0315     file.open(name.c_str());
0316     if (file.is_open())
0317         return AddSeparator(CRYPTOPP_DATA_DIR);
0318 #endif
0319     return "./";
0320 }
0321 
0322 inline std::string DataDir(const std::string& filename)
0323 {
0324     std::string name;
0325     std::ifstream file;
0326 
0327 #if CRYPTOPP_CXX11_STATIC_INIT
0328     static std::string path = AddSeparator(GetDataDir());
0329     name = path + filename;
0330     file.open(name.c_str());
0331     if (file.is_open())
0332         return name;
0333 #else
0334     // Avoid static initialization problems

0335     name = AddSeparator(GetDataDir()) + filename;
0336     file.open(name.c_str());
0337     if (file.is_open())
0338         return name;
0339 #endif
0340 
0341     // This will cause the expected exception in the caller

0342     return filename;
0343 }
0344 
0345 // Definition in test.cpp

0346 RandomNumberGenerator& GlobalRNG();
0347 
0348 // Definition in datatest.cpp

0349 bool RunTestDataFile(const char *filename, const NameValuePairs &overrideParameters=g_nullNameValuePairs, bool thorough=true);
0350 
0351 // Definitions in validat6.cpp

0352 bool CryptoSystemValidate(PK_Decryptor &priv, PK_Encryptor &pub, bool thorough = false);
0353 bool SimpleKeyAgreementValidate(SimpleKeyAgreementDomain &d);
0354 bool AuthenticatedKeyAgreementWithRolesValidate(AuthenticatedKeyAgreementDomain &initiator, AuthenticatedKeyAgreementDomain &recipient);
0355 bool AuthenticatedKeyAgreementValidate(AuthenticatedKeyAgreementDomain &d);
0356 bool SignatureValidate(PK_Signer &priv, PK_Verifier &pub, bool thorough = false);
0357 
0358 // Miscellaneous PK definitions in validat6.cpp

0359 // Key Agreement definitions in validat7.cpp

0360 // Encryption and Decryption definitions in validat8.cpp

0361 // Sign and Verify definitions in validat9.cpp

0362 
0363 bool ValidateECP();
0364 bool ValidateEC2N();
0365 
0366 bool ValidateRSA_Encrypt();
0367 bool ValidateRSA_Sign();
0368 
0369 bool ValidateLUC_Encrypt();
0370 bool ValidateLUC_Sign();
0371 
0372 bool ValidateLUC_DL_Encrypt();
0373 bool ValidateLUC_DL_Sign();
0374 
0375 bool ValidateRabin_Encrypt();
0376 bool ValidateRabin_Sign();
0377 
0378 bool ValidateECP();
0379 bool ValidateECP_Agreement();
0380 bool ValidateECP_Encrypt();
0381 bool ValidateECP_Sign();
0382 
0383 bool ValidateECP_Legacy_Encrypt();
0384 bool ValidateEC2N_Legacy_Encrypt();
0385 bool ValidateECP_NULLDigest_Encrypt();
0386 
0387 bool ValidateEC2N();
0388 bool ValidateEC2N_Agreement();
0389 bool ValidateEC2N_Encrypt();
0390 bool ValidateEC2N_Sign();
0391 
0392 NAMESPACE_END  // Test

0393 NAMESPACE_END  // CryptoPP

0394 
0395 #endif