validate.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. // validate.h - originally written and placed in the public domain by Wei Dai
  2. // CryptoPP::Test namespace added by JW in February 2017
  3. #ifndef CRYPTOPP_VALIDATE_H
  4. #define CRYPTOPP_VALIDATE_H
  5. #include "cryptlib.h"
  6. #include "misc.h"
  7. #include "files.h"
  8. #include "argnames.h"
  9. #include "algparam.h"
  10. #include "hex.h"
  11. #include <iostream>
  12. #include <sstream>
  13. #include <iomanip>
  14. #include <ctime>
  15. #include <cctype>
  16. NAMESPACE_BEGIN(CryptoPP)
  17. NAMESPACE_BEGIN(Test)
  18. // A hint to help locate TestData/ and TestVectors/ after install. Due to
  19. // execve the path can be malicious. If the path is ficticous then we move
  20. // onto the next potential path. Also note we only read from the path; we
  21. // never write through it. Storage for the string is in test.cpp.
  22. extern std::string g_argvPathHint;
  23. bool ValidateAll(bool thorough);
  24. bool TestSettings();
  25. bool TestOS_RNG();
  26. // bool TestSecRandom();
  27. bool TestRandomPool();
  28. #if !defined(NO_OS_DEPENDENCE)
  29. bool TestAutoSeededX917();
  30. #endif
  31. #if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
  32. bool TestRDRAND();
  33. bool TestRDSEED();
  34. bool TestPadlockRNG();
  35. #endif
  36. #if (CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64)
  37. bool TestDARN();
  38. #endif
  39. bool ValidateBaseCode();
  40. bool ValidateEncoder();
  41. bool ValidateCRC32();
  42. bool ValidateCRC32C();
  43. bool ValidateAdler32();
  44. bool ValidateMD2();
  45. bool ValidateMD4();
  46. bool ValidateMD5();
  47. bool ValidateSHA();
  48. bool ValidateSHA2();
  49. bool ValidateSHA3();
  50. bool ValidateSHAKE(); // output <= r, where r is blocksize
  51. bool ValidateSHAKE_XOF(); // output > r, needs hand crafted tests
  52. bool ValidateKeccak();
  53. bool ValidateTiger();
  54. bool ValidateRIPEMD();
  55. bool ValidatePanama();
  56. bool ValidateWhirlpool();
  57. bool ValidateLSH();
  58. bool ValidateSM3();
  59. bool ValidateBLAKE2s();
  60. bool ValidateBLAKE2b();
  61. bool ValidatePoly1305();
  62. bool ValidateSipHash();
  63. bool ValidateHMAC();
  64. bool ValidateTTMAC();
  65. bool ValidateCipherModes();
  66. bool ValidatePBKDF();
  67. bool ValidateHKDF();
  68. bool ValidateScrypt();
  69. bool ValidateDES();
  70. bool ValidateIDEA();
  71. bool ValidateSAFER();
  72. bool ValidateRC2();
  73. bool ValidateARC4();
  74. bool ValidateRC5();
  75. bool ValidateBlowfish();
  76. bool ValidateBlowfishCompat();
  77. bool ValidateThreeWay();
  78. bool ValidateGOST();
  79. bool ValidateSHARK();
  80. bool ValidateSEAL();
  81. bool ValidateCAST();
  82. bool ValidateSquare();
  83. bool ValidateSKIPJACK();
  84. bool ValidateRC6();
  85. bool ValidateMARS();
  86. bool ValidateRijndael();
  87. bool ValidateTwofish();
  88. bool ValidateSerpent();
  89. bool ValidateSHACAL2();
  90. bool ValidateARIA();
  91. bool ValidateSIMECK();
  92. bool ValidateCHAM();
  93. bool ValidateHIGHT();
  94. bool ValidateLEA();
  95. bool ValidateSIMON();
  96. bool ValidateSPECK();
  97. bool ValidateCamellia();
  98. bool ValidateHC128();
  99. bool ValidateHC256();
  100. bool ValidateRabbit();
  101. bool ValidateSalsa();
  102. bool ValidateChaCha();
  103. bool ValidateChaChaTLS();
  104. bool ValidateSosemanuk();
  105. bool ValidateVMAC();
  106. bool ValidateCCM();
  107. bool ValidateGCM();
  108. bool ValidateXTS();
  109. bool ValidateCMAC();
  110. bool ValidateBBS();
  111. bool ValidateDH();
  112. bool ValidateMQV();
  113. bool ValidateHMQV();
  114. bool ValidateFHMQV();
  115. bool ValidateRSA();
  116. bool ValidateElGamal();
  117. bool ValidateDLIES();
  118. bool ValidateNR();
  119. bool ValidateDSA(bool thorough);
  120. bool ValidateLUC();
  121. bool ValidateLUC_DL();
  122. bool ValidateLUC_DH();
  123. bool ValidateXTR_DH();
  124. bool ValidateRabin();
  125. bool ValidateRW();
  126. bool ValidateECP();
  127. bool ValidateEC2N();
  128. bool ValidateECDSA();
  129. bool ValidateECDSA_RFC6979();
  130. bool ValidateECGDSA(bool thorough);
  131. bool ValidateESIGN();
  132. bool ValidateHashDRBG();
  133. bool ValidateHmacDRBG();
  134. bool TestX25519();
  135. bool TestEd25519();
  136. bool ValidateX25519();
  137. bool ValidateEd25519();
  138. bool ValidateNaCl();
  139. // If CRYPTOPP_DEBUG or CRYPTOPP_COVERAGE is in effect, then perform additional tests
  140. #if (defined(CRYPTOPP_DEBUG) || defined(CRYPTOPP_COVERAGE)) && !defined(CRYPTOPP_IMPORTS)
  141. # define CRYPTOPP_EXTENDED_VALIDATION 1
  142. #endif
  143. #if defined(CRYPTOPP_EXTENDED_VALIDATION)
  144. // http://github.com/weidai11/cryptopp/issues/92
  145. bool TestSecBlock();
  146. // http://github.com/weidai11/cryptopp/issues/64
  147. bool TestPolynomialMod2();
  148. // http://github.com/weidai11/cryptopp/issues/336
  149. bool TestIntegerBitops();
  150. // http://github.com/weidai11/cryptopp/issues/602
  151. bool TestIntegerOps();
  152. // http://github.com/weidai11/cryptopp/issues/360
  153. bool TestRounding();
  154. // http://github.com/weidai11/cryptopp/issues/242
  155. bool TestHuffmanCodes();
  156. // http://github.com/weidai11/cryptopp/issues/346
  157. bool TestASN1Parse();
  158. bool TestASN1Functions();
  159. // https://github.com/weidai11/cryptopp/pull/334
  160. bool TestStringSink();
  161. // Additional tests due to no coverage
  162. bool TestCompressors();
  163. bool TestEncryptors();
  164. bool TestMersenne();
  165. bool TestSharing();
  166. # if defined(CRYPTOPP_ALTIVEC_AVAILABLE)
  167. bool TestAltivecOps();
  168. # endif
  169. #endif
  170. class FixedRNG : public RandomNumberGenerator
  171. {
  172. public:
  173. FixedRNG(BufferedTransformation &source) : m_source(source) {}
  174. void GenerateBlock(byte *output, size_t size)
  175. {
  176. m_source.Get(output, size);
  177. }
  178. private:
  179. BufferedTransformation &m_source;
  180. };
  181. // Safer functions on Windows for C&A, http://github.com/weidai11/cryptopp/issues/55
  182. inline std::string TimeToString(const time_t& t)
  183. {
  184. #if (CRYPTOPP_MSC_VERSION >= 1400)
  185. tm localTime;
  186. char timeBuf[64];
  187. errno_t err;
  188. err = ::localtime_s(&localTime, &t);
  189. CRYPTOPP_ASSERT(err == 0);
  190. err = ::asctime_s(timeBuf, sizeof(timeBuf), &localTime);
  191. CRYPTOPP_ASSERT(err == 0);
  192. std::string str(err == 0 ? timeBuf : "");
  193. #elif defined(__MINGW32__) || defined(__MINGW64__)
  194. char* timeString = ::asctime(::localtime(&t));
  195. std::string str(timeString ? timeString : "");
  196. #elif (_POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE || defined(_POSIX_SOURCE))
  197. tm localTime;
  198. char timeBuf[64];
  199. char* timeString = ::asctime_r(::localtime_r(&t, &localTime), timeBuf);
  200. std::string str(timeString ? timeString : "");
  201. #else
  202. char* timeString = ::asctime(::localtime(&t));
  203. std::string str(timeString ? timeString : "");
  204. #endif
  205. // Cleanup whitespace
  206. std::string::size_type pos = 0;
  207. while (!str.empty() && std::isspace(str[str.length()-1]))
  208. {str.erase(str.end()-1);}
  209. while (!str.empty() && std::string::npos != (pos = str.find(" ", pos)))
  210. {str.erase(pos, 1);}
  211. return str;
  212. }
  213. // Coverity finding
  214. template <class T, bool NON_NEGATIVE>
  215. inline T StringToValue(const std::string& str)
  216. {
  217. std::istringstream iss(str);
  218. // Arbitrary, but we need to clear a Coverity finding TAINTED_SCALAR
  219. if (iss.str().length() > 25)
  220. throw InvalidArgument(str + "' is too long");
  221. T value;
  222. iss >> std::noskipws >> value;
  223. // Use fail(), not bad()
  224. if (iss.fail())
  225. throw InvalidArgument(str + "' is not a value");
  226. if (NON_NEGATIVE && value < 0)
  227. throw InvalidArgument(str + "' is negative");
  228. return value;
  229. }
  230. // Coverity finding
  231. template<>
  232. inline int StringToValue<int, true>(const std::string& str)
  233. {
  234. Integer n(str.c_str());
  235. long l = n.ConvertToLong();
  236. int r;
  237. if (!SafeConvert(l, r))
  238. throw InvalidArgument(str + "' is not an integer value");
  239. return r;
  240. }
  241. inline std::string AddSeparator(std::string str)
  242. {
  243. if (str.empty()) return "";
  244. const char last = str[str.length()-1];
  245. if (last != '/' && last != '\\')
  246. return str + "/";
  247. return str;
  248. }
  249. // Use CRYPTOPP_DATA_DIR last. The problem this sidesteps is, finding an
  250. // old version of Crypto++ library in CRYPTOPP_DATA_DIR when the library
  251. // has been staged in DESTDIR. Using CRYPTOPP_DATA_DIR first only works
  252. // as expected when CRYPTOPP_DATA_DIR is empty before an install. We
  253. // encountered this problem rather quickly during testing of Crypto++ 8.1
  254. // when Crypto++ 8.0 was installed locally. It took some time to realize
  255. // where the old test data was coming from.
  256. static std::string GetDataDir()
  257. {
  258. std::ifstream file;
  259. std::string name, filename = "TestData/usage.dat";
  260. #ifndef CRYPTOPP_DISABLE_DATA_DIR_SEARCH
  261. // Look in $ORIGIN/../share/. This is likely a Linux install directory.
  262. name = AddSeparator(g_argvPathHint) + std::string("../share/cryptopp/") + filename;
  263. file.open(name.c_str());
  264. if (file.is_open())
  265. return AddSeparator(g_argvPathHint) + std::string("../share/cryptopp/");
  266. #endif
  267. #ifndef CRYPTOPP_DISABLE_DATA_DIR_SEARCH
  268. // Look in current working directory
  269. name = AddSeparator(g_argvPathHint) + filename;
  270. file.open(name.c_str());
  271. if (file.is_open())
  272. return AddSeparator(g_argvPathHint);
  273. #endif
  274. #ifdef CRYPTOPP_DATA_DIR
  275. // Honor CRYPTOPP_DATA_DIR. This is likely an install directory if it is not "./".
  276. name = AddSeparator(CRYPTOPP_DATA_DIR) + filename;
  277. file.open(name.c_str());
  278. if (file.is_open())
  279. return AddSeparator(CRYPTOPP_DATA_DIR);
  280. #endif
  281. return "./";
  282. }
  283. inline std::string DataDir(const std::string& filename)
  284. {
  285. std::string name;
  286. std::ifstream file;
  287. #if CRYPTOPP_CXX11_STATIC_INIT
  288. static std::string path = AddSeparator(GetDataDir());
  289. name = path + filename;
  290. file.open(name.c_str());
  291. if (file.is_open())
  292. return name;
  293. #else
  294. // Avoid static initialization problems
  295. name = AddSeparator(GetDataDir()) + filename;
  296. file.open(name.c_str());
  297. if (file.is_open())
  298. return name;
  299. #endif
  300. // This will cause the expected exception in the caller
  301. return filename;
  302. }
  303. // Definition in test.cpp
  304. RandomNumberGenerator& GlobalRNG();
  305. // Definition in datatest.cpp
  306. bool RunTestDataFile(const char *filename, const NameValuePairs &overrideParameters=g_nullNameValuePairs, bool thorough=true);
  307. // Definitions in validat6.cpp
  308. bool CryptoSystemValidate(PK_Decryptor &priv, PK_Encryptor &pub, bool thorough = false);
  309. bool SimpleKeyAgreementValidate(SimpleKeyAgreementDomain &d);
  310. bool AuthenticatedKeyAgreementWithRolesValidate(AuthenticatedKeyAgreementDomain &initiator, AuthenticatedKeyAgreementDomain &recipient);
  311. bool AuthenticatedKeyAgreementValidate(AuthenticatedKeyAgreementDomain &d);
  312. bool SignatureValidate(PK_Signer &priv, PK_Verifier &pub, bool thorough = false);
  313. // Miscellaneous PK definitions in validat6.cpp
  314. // Key Agreement definitions in validat7.cpp
  315. // Encryption and Decryption definitions in validat8.cpp
  316. // Sign and Verify definitions in validat9.cpp
  317. bool ValidateECP();
  318. bool ValidateEC2N();
  319. bool ValidateRSA_Encrypt();
  320. bool ValidateRSA_Sign();
  321. bool ValidateLUC_Encrypt();
  322. bool ValidateLUC_Sign();
  323. bool ValidateLUC_DL_Encrypt();
  324. bool ValidateLUC_DL_Sign();
  325. bool ValidateRabin_Encrypt();
  326. bool ValidateRabin_Sign();
  327. bool ValidateECP();
  328. bool ValidateECP_Agreement();
  329. bool ValidateECP_Encrypt();
  330. bool ValidateECP_Sign();
  331. bool ValidateECP_Legacy_Encrypt();
  332. bool ValidateEC2N_Legacy_Encrypt();
  333. bool ValidateECP_NULLDigest_Encrypt();
  334. bool ValidateEC2N();
  335. bool ValidateEC2N_Agreement();
  336. bool ValidateEC2N_Encrypt();
  337. bool ValidateEC2N_Sign();
  338. NAMESPACE_END // Test
  339. NAMESPACE_END // CryptoPP
  340. #endif