Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1290 lines
35 KiB

  1. // fipsalgt.cpp - written and placed in the public domain by Wei Dai
  2. // This file implements the various algorithm tests needed to pass FIPS 140 validation.
  3. // They're preserved here (commented out) in case Crypto++ needs to be revalidated.
  4. #if 0
  5. #ifndef CRYPTOPP_IMPORTS
  6. #define CRYPTOPP_DEFAULT_NO_DLL
  7. #endif
  8. #include "dll.h"
  9. #include "oids.h"
  10. USING_NAMESPACE(CryptoPP)
  11. USING_NAMESPACE(std)
  12. class LineBreakParser : public AutoSignaling<Bufferless<Filter> >
  13. {
  14. public:
  15. LineBreakParser(BufferedTransformation *attachment=NULL, byte lineEnd='\n')
  16. : m_lineEnd(lineEnd) {Detach(attachment);}
  17. size_t Put2(const byte *begin, size_t length, int messageEnd, bool blocking)
  18. {
  19. if (!blocking)
  20. throw BlockingInputOnly("LineBreakParser");
  21. unsigned int i, last = 0;
  22. for (i=0; i<length; i++)
  23. {
  24. if (begin[i] == m_lineEnd)
  25. {
  26. AttachedTransformation()->Put2(begin+last, i-last, GetAutoSignalPropagation(), blocking);
  27. last = i+1;
  28. }
  29. }
  30. if (last != i)
  31. AttachedTransformation()->Put2(begin+last, i-last, 0, blocking);
  32. if (messageEnd && GetAutoSignalPropagation())
  33. {
  34. AttachedTransformation()->MessageEnd(GetAutoSignalPropagation()-1, blocking);
  35. AttachedTransformation()->MessageSeriesEnd(GetAutoSignalPropagation()-1, blocking);
  36. }
  37. return 0;
  38. }
  39. private:
  40. byte m_lineEnd;
  41. };
  42. class TestDataParser : public Unflushable<FilterWithInputQueue>
  43. {
  44. public:
  45. enum DataType {OTHER, COUNT, KEY_T, IV, INPUT, OUTPUT};
  46. TestDataParser(std::string algorithm, std::string test, std::string mode, unsigned int feedbackSize, bool encrypt, BufferedTransformation *attachment)
  47. : m_algorithm(algorithm), m_test(test), m_mode(mode), m_feedbackSize(feedbackSize)
  48. , m_firstLine(true), m_blankLineTransition(0)
  49. {
  50. Detach(attachment);
  51. m_typeToName[COUNT] = "COUNT";
  52. m_nameToType["COUNT"] = COUNT;
  53. m_nameToType["KEY"] = KEY_T;
  54. m_nameToType["KEYs"] = KEY_T;
  55. m_nameToType["key"] = KEY_T;
  56. m_nameToType["Key"] = KEY_T;
  57. m_nameToType["IV"] = IV;
  58. m_nameToType["IV1"] = IV;
  59. m_nameToType["CV"] = IV;
  60. m_nameToType["CV1"] = IV;
  61. m_nameToType["IB"] = IV;
  62. m_nameToType["TEXT"] = INPUT;
  63. m_nameToType["RESULT"] = OUTPUT;
  64. m_nameToType["Msg"] = INPUT;
  65. m_nameToType["Seed"] = INPUT;
  66. m_nameToType["V"] = INPUT;
  67. m_nameToType["DT"] = IV;
  68. SetEncrypt(encrypt);
  69. if (m_algorithm == "DSA" || m_algorithm == "ECDSA")
  70. {
  71. if (m_test == "PKV")
  72. m_trigger = "Qy";
  73. else if (m_test == "KeyPair")
  74. m_trigger = "N";
  75. else if (m_test == "SigGen")
  76. m_trigger = "Msg";
  77. else if (m_test == "SigVer")
  78. m_trigger = "S";
  79. else if (m_test == "PQGGen")
  80. m_trigger = "N";
  81. else if (m_test == "PQGVer")
  82. m_trigger = "H";
  83. }
  84. else if (m_algorithm == "HMAC")
  85. m_trigger = "Msg";
  86. else if (m_algorithm == "SHA")
  87. m_trigger = (m_test == "MONTE") ? "Seed" : "Msg";
  88. else if (m_algorithm == "RNG")
  89. m_trigger = "V";
  90. else if (m_algorithm == "RSA")
  91. m_trigger = (m_test == "Ver") ? "S" : "Msg";
  92. }
  93. void SetEncrypt(bool encrypt)
  94. {
  95. m_encrypt = encrypt;
  96. if (encrypt)
  97. {
  98. m_nameToType["PLAINTEXT"] = INPUT;
  99. m_nameToType["CIPHERTEXT"] = OUTPUT;
  100. m_nameToType["PT"] = INPUT;
  101. m_nameToType["CT"] = OUTPUT;
  102. }
  103. else
  104. {
  105. m_nameToType["PLAINTEXT"] = OUTPUT;
  106. m_nameToType["CIPHERTEXT"] = INPUT;
  107. m_nameToType["PT"] = OUTPUT;
  108. m_nameToType["CT"] = INPUT;
  109. }
  110. if (m_algorithm == "AES" || m_algorithm == "TDES")
  111. {
  112. if (encrypt)
  113. {
  114. m_trigger = "PLAINTEXT";
  115. m_typeToName[OUTPUT] = "CIPHERTEXT";
  116. }
  117. else
  118. {
  119. m_trigger = "CIPHERTEXT";
  120. m_typeToName[OUTPUT] = "PLAINTEXT";
  121. }
  122. m_count = 0;
  123. }
  124. }
  125. protected:
  126. void OutputData(std::string &output, const std::string &key, const std::string &data)
  127. {
  128. output += key;
  129. output += "= ";
  130. output += data;
  131. output += "\n";
  132. }
  133. void OutputData(std::string &output, const std::string &key, int data)
  134. {
  135. OutputData(output, key, IntToString(data));
  136. }
  137. void OutputData(std::string &output, const std::string &key, const SecByteBlock &data)
  138. {
  139. output += key;
  140. output += "= ";
  141. HexEncoder(new StringSink(output), false).Put(data, data.size());
  142. output += "\n";
  143. }
  144. void OutputData(std::string &output, const std::string &key, const Integer &data, int size=-1)
  145. {
  146. SecByteBlock s(size < 0 ? data.MinEncodedSize() : size);
  147. data.Encode(s, s.size());
  148. OutputData(output, key, s);
  149. }
  150. void OutputData(std::string &output, const std::string &key, const PolynomialMod2 &data, int size=-1)
  151. {
  152. SecByteBlock s(size < 0 ? data.MinEncodedSize() : size);
  153. data.Encode(s, s.size());
  154. OutputData(output, key, s);
  155. }
  156. void OutputData(std::string &output, DataType t, const std::string &data)
  157. {
  158. if (m_algorithm == "SKIPJACK")
  159. {
  160. if (m_test == "KAT")
  161. {
  162. if (t == OUTPUT)
  163. output = m_line + data + "\n";
  164. }
  165. else
  166. {
  167. if (t != COUNT)
  168. {
  169. output += m_typeToName[t];
  170. output += "=";
  171. }
  172. output += data;
  173. output += t == OUTPUT ? "\n" : " ";
  174. }
  175. }
  176. else if (m_algorithm == "TDES" && t == KEY_T && m_typeToName[KEY_T].empty())
  177. {
  178. output += "KEY1 = ";
  179. output += data.substr(0, 16);
  180. output += "\nKEY2 = ";
  181. output += data.size() > 16 ? data.substr(16, 16) : data.substr(0, 16);
  182. output += "\nKEY3 = ";
  183. output += data.size() > 32 ? data.substr(32, 16) : data.substr(0, 16);
  184. output += "\n";
  185. }
  186. else
  187. {
  188. output += m_typeToName[t];
  189. output += " = ";
  190. output += data;
  191. output += "\n";
  192. }
  193. }
  194. void OutputData(std::string &output, DataType t, int i)
  195. {
  196. OutputData(output, t, IntToString(i));
  197. }
  198. void OutputData(std::string &output, DataType t, const SecByteBlock &data)
  199. {
  200. std::string hexData;
  201. StringSource(data.begin(), data.size(), true, new HexEncoder(new StringSink(hexData), false));
  202. OutputData(output, t, hexData);
  203. }
  204. void OutputGivenData(std::string &output, DataType t, bool optional = false)
  205. {
  206. if (m_data.find(m_typeToName[t]) == m_data.end())
  207. {
  208. if (optional)
  209. return;
  210. throw Exception(Exception::OTHER_ERROR, "TestDataParser: key not found: " + m_typeToName[t]);
  211. }
  212. OutputData(output, t, m_data[m_typeToName[t]]);
  213. }
  214. template <class T>
  215. BlockCipher * NewBT(T *)
  216. {
  217. if (!m_encrypt && (m_mode == "ECB" || m_mode == "CBC"))
  218. return new typename T::Decryption;
  219. else
  220. return new typename T::Encryption;
  221. }
  222. template <class T>
  223. SymmetricCipher * NewMode(T *, BlockCipher &bt, const byte *iv)
  224. {
  225. if (!m_encrypt)
  226. return new typename T::Decryption(bt, iv, m_feedbackSize/8);
  227. else
  228. return new typename T::Encryption(bt, iv, m_feedbackSize/8);
  229. }
  230. static inline void Xor(SecByteBlock &z, const SecByteBlock &x, const SecByteBlock &y)
  231. {
  232. assert(x.size() == y.size());
  233. z.resize(x.size());
  234. xorbuf(z, x, y, x.size());
  235. }
  236. SecByteBlock UpdateKey(SecByteBlock key, const SecByteBlock *text)
  237. {
  238. unsigned int innerCount = (m_algorithm == "AES") ? 1000 : 10000;
  239. int keySize = key.size(), blockSize = text[0].size();
  240. SecByteBlock x(keySize);
  241. for (int k=0; k<keySize;)
  242. {
  243. int pos = innerCount * blockSize - keySize + k;
  244. memcpy(x + k, text[pos / blockSize] + pos % blockSize, blockSize - pos % blockSize);
  245. k += blockSize - pos % blockSize;
  246. }
  247. if (m_algorithm == "TDES" || m_algorithm == "DES")
  248. {
  249. for (int i=0; i<keySize; i+=8)
  250. {
  251. xorbuf(key+i, x+keySize-8-i, 8);
  252. DES::CorrectKeyParityBits(key+i);
  253. }
  254. }
  255. else
  256. xorbuf(key, x, keySize);
  257. return key;
  258. }
  259. static inline void AssignLeftMostBits(SecByteBlock &z, const SecByteBlock &x, unsigned int K)
  260. {
  261. z.Assign(x, K/8);
  262. }
  263. template <class EC>
  264. void EC_KeyPair(string &output, int n, const OID &oid)
  265. {
  266. DL_GroupParameters_EC<EC> params(oid);
  267. for (int i=0; i<n; i++)
  268. {
  269. DL_PrivateKey_EC<EC> priv;
  270. DL_PublicKey_EC<EC> pub;
  271. priv.Initialize(m_rng, params);
  272. priv.MakePublicKey(pub);
  273. OutputData(output, "d ", priv.GetPrivateExponent());
  274. OutputData(output, "Qx ", pub.GetPublicElement().x, params.GetCurve().GetField().MaxElementByteLength());
  275. OutputData(output, "Qy ", pub.GetPublicElement().y, params.GetCurve().GetField().MaxElementByteLength());
  276. }
  277. }
  278. template <class EC>
  279. void EC_SigGen(string &output, const OID &oid)
  280. {
  281. DL_GroupParameters_EC<EC> params(oid);
  282. typename ECDSA<EC, SHA1>::PrivateKey priv;
  283. typename ECDSA<EC, SHA1>::PublicKey pub;
  284. priv.Initialize(m_rng, params);
  285. priv.MakePublicKey(pub);
  286. typename ECDSA<EC, SHA1>::Signer signer(priv);
  287. SecByteBlock sig(signer.SignatureLength());
  288. StringSource(m_data["Msg"], true, new HexDecoder(new SignerFilter(m_rng, signer, new ArraySink(sig, sig.size()))));
  289. SecByteBlock R(sig, sig.size()/2), S(sig+sig.size()/2, sig.size()/2);
  290. OutputData(output, "Qx ", pub.GetPublicElement().x, params.GetCurve().GetField().MaxElementByteLength());
  291. OutputData(output, "Qy ", pub.GetPublicElement().y, params.GetCurve().GetField().MaxElementByteLength());
  292. OutputData(output, "R ", R);
  293. OutputData(output, "S ", S);
  294. }
  295. template <class EC>
  296. void EC_SigVer(string &output, const OID &oid)
  297. {
  298. SecByteBlock x(DecodeHex(m_data["Qx"]));
  299. SecByteBlock y(DecodeHex(m_data["Qy"]));
  300. Integer r((m_data["R"]+"h").c_str());
  301. Integer s((m_data["S"]+"h").c_str());
  302. typename EC::FieldElement Qx(x, x.size());
  303. typename EC::FieldElement Qy(y, y.size());
  304. typename EC::Element Q(Qx, Qy);
  305. DL_GroupParameters_EC<EC> params(oid);
  306. typename ECDSA<EC, SHA1>::PublicKey pub;
  307. pub.Initialize(params, Q);
  308. typename ECDSA<EC, SHA1>::Verifier verifier(pub);
  309. SecByteBlock sig(verifier.SignatureLength());
  310. r.Encode(sig, sig.size()/2);
  311. s.Encode(sig+sig.size()/2, sig.size()/2);
  312. SignatureVerificationFilter filter(verifier);
  313. filter.Put(sig, sig.size());
  314. StringSource(m_data["Msg"], true, new HexDecoder(new Redirector(filter, Redirector::DATA_ONLY)));
  315. filter.MessageEnd();
  316. byte b;
  317. filter.Get(b);
  318. OutputData(output, "Result ", b ? "P" : "F");
  319. }
  320. template <class EC>
  321. static bool EC_PKV(RandomNumberGenerator &rng, const SecByteBlock &x, const SecByteBlock &y, const OID &oid)
  322. {
  323. typename EC::FieldElement Qx(x, x.size());
  324. typename EC::FieldElement Qy(y, y.size());
  325. typename EC::Element Q(Qx, Qy);
  326. DL_GroupParameters_EC<EC> params(oid);
  327. typename ECDSA<EC, SHA1>::PublicKey pub;
  328. pub.Initialize(params, Q);
  329. return pub.Validate(rng, 3);
  330. }
  331. template <class H, class Result>
  332. Result * CreateRSA2(const std::string &standard)
  333. {
  334. if (typeid(Result) == typeid(PK_Verifier))
  335. {
  336. if (standard == "R")
  337. return (Result *) new typename RSASS_ISO<H>::Verifier;
  338. else if (standard == "P")
  339. return (Result *) new typename RSASS<PSS, H>::Verifier;
  340. else if (standard == "1")
  341. return (Result *) new typename RSASS<PKCS1v15, H>::Verifier;
  342. }
  343. else if (typeid(Result) == typeid(PK_Signer))
  344. {
  345. if (standard == "R")
  346. return (Result *) new typename RSASS_ISO<H>::Signer;
  347. else if (standard == "P")
  348. return (Result *) new typename RSASS<PSS, H>::Signer;
  349. else if (standard == "1")
  350. return (Result *) new typename RSASS<PKCS1v15, H>::Signer;
  351. }
  352. return NULL;
  353. }
  354. template <class Result>
  355. Result * CreateRSA(const std::string &standard, const std::string &hash)
  356. {
  357. if (hash == "1")
  358. return CreateRSA2<SHA1, Result>(standard);
  359. else if (hash == "224")
  360. return CreateRSA2<SHA224, Result>(standard);
  361. else if (hash == "256")
  362. return CreateRSA2<SHA256, Result>(standard);
  363. else if (hash == "384")
  364. return CreateRSA2<SHA384, Result>(standard);
  365. else if (hash == "512")
  366. return CreateRSA2<SHA512, Result>(standard);
  367. else
  368. return NULL;
  369. }
  370. virtual void DoTest()
  371. {
  372. std::string output;
  373. if (m_algorithm == "DSA")
  374. {
  375. if (m_test == "KeyPair")
  376. {
  377. DL_GroupParameters_DSA pqg;
  378. int modLen = atol(m_bracketString.substr(6).c_str());
  379. pqg.GenerateRandomWithKeySize(m_rng, modLen);
  380. OutputData(output, "P ", pqg.GetModulus());
  381. OutputData(output, "Q ", pqg.GetSubgroupOrder());
  382. OutputData(output, "G ", pqg.GetSubgroupGenerator());
  383. int n = atol(m_data["N"].c_str());
  384. for (int i=0; i<n; i++)
  385. {
  386. DSA::Signer priv;
  387. priv.AccessKey().GenerateRandom(m_rng, pqg);
  388. DSA::Verifier pub(priv);
  389. OutputData(output, "X ", priv.GetKey().GetPrivateExponent());
  390. OutputData(output, "Y ", pub.GetKey().GetPublicElement());
  391. AttachedTransformation()->Put((byte *)output.data(), output.size());
  392. output.resize(0);
  393. }
  394. }
  395. else if (m_test == "PQGGen")
  396. {
  397. int n = atol(m_data["N"].c_str());
  398. for (int i=0; i<n; i++)
  399. {
  400. Integer p, q, h, g;
  401. int counter;
  402. SecByteBlock seed(SHA::DIGESTSIZE);
  403. do
  404. {
  405. m_rng.GenerateBlock(seed, seed.size());
  406. }
  407. while (!DSA::GeneratePrimes(seed, seed.size()*8, counter, p, 1024, q));
  408. h.Randomize(m_rng, 2, p-2);
  409. g = a_exp_b_mod_c(h, (p-1)/q, p);
  410. OutputData(output, "P ", p);
  411. OutputData(output, "Q ", q);
  412. OutputData(output, "G ", g);
  413. OutputData(output, "Seed ", seed);
  414. OutputData(output, "c ", counter);
  415. OutputData(output, "H ", h, p.ByteCount());
  416. AttachedTransformation()->Put((byte *)output.data(), output.size());
  417. output.resize(0);
  418. }
  419. }
  420. else if (m_test == "SigGen")
  421. {
  422. std::string &encodedKey = m_data["PrivKey"];
  423. int modLen = atol(m_bracketString.substr(6).c_str());
  424. DSA::PrivateKey priv;
  425. if (!encodedKey.empty())
  426. {
  427. StringStore s(encodedKey);
  428. priv.BERDecode(s);
  429. if (priv.GetGroupParameters().GetModulus().BitCount() != modLen)
  430. encodedKey.clear();
  431. }
  432. if (encodedKey.empty())
  433. {
  434. priv.Initialize(m_rng, modLen);
  435. StringSink s(encodedKey);
  436. priv.DEREncode(s);
  437. OutputData(output, "P ", priv.GetGroupParameters().GetModulus());
  438. OutputData(output, "Q ", priv.GetGroupParameters().GetSubgroupOrder());
  439. OutputData(output, "G ", priv.GetGroupParameters().GetSubgroupGenerator());
  440. }
  441. DSA::Signer signer(priv);
  442. DSA::Verifier pub(signer);
  443. OutputData(output, "Msg ", m_data["Msg"]);
  444. OutputData(output, "Y ", pub.GetKey().GetPublicElement());
  445. SecByteBlock sig(signer.SignatureLength());
  446. StringSource(m_data["Msg"], true, new HexDecoder(new SignerFilter(m_rng, signer, new ArraySink(sig, sig.size()))));
  447. SecByteBlock R(sig, sig.size()/2), S(sig+sig.size()/2, sig.size()/2);
  448. OutputData(output, "R ", R);
  449. OutputData(output, "S ", S);
  450. AttachedTransformation()->Put((byte *)output.data(), output.size());
  451. output.resize(0);
  452. }
  453. else if (m_test == "SigVer")
  454. {
  455. Integer p((m_data["P"] + "h").c_str());
  456. Integer q((m_data["Q"] + "h").c_str());
  457. Integer g((m_data["G"] + "h").c_str());
  458. Integer y((m_data["Y"] + "h").c_str());
  459. DSA::Verifier verifier(p, q, g, y);
  460. HexDecoder filter(new SignatureVerificationFilter(verifier));
  461. StringSource(m_data["R"], true, new Redirector(filter, Redirector::DATA_ONLY));
  462. StringSource(m_data["S"], true, new Redirector(filter, Redirector::DATA_ONLY));
  463. StringSource(m_data["Msg"], true, new Redirector(filter, Redirector::DATA_ONLY));
  464. filter.MessageEnd();
  465. byte b;
  466. filter.Get(b);
  467. OutputData(output, "Result ", b ? "P" : "F");
  468. AttachedTransformation()->Put((byte *)output.data(), output.size());
  469. output.resize(0);
  470. }
  471. else if (m_test == "PQGVer")
  472. {
  473. Integer p((m_data["P"] + "h").c_str());
  474. Integer q((m_data["Q"] + "h").c_str());
  475. Integer g((m_data["G"] + "h").c_str());
  476. Integer h((m_data["H"] + "h").c_str());
  477. int c = atol(m_data["c"].c_str());
  478. SecByteBlock seed(m_data["Seed"].size()/2);
  479. StringSource(m_data["Seed"], true, new HexDecoder(new ArraySink(seed, seed.size())));
  480. Integer p1, q1;
  481. bool result = DSA::GeneratePrimes(seed, seed.size()*8, c, p1, 1024, q1, true);
  482. result = result && (p1 == p && q1 == q);
  483. result = result && g == a_exp_b_mod_c(h, (p-1)/q, p);
  484. OutputData(output, "Result ", result ? "P" : "F");
  485. AttachedTransformation()->Put((byte *)output.data(), output.size());
  486. output.resize(0);
  487. }
  488. return;
  489. }
  490. if (m_algorithm == "ECDSA")
  491. {
  492. std::map<std::string, OID> name2oid;
  493. name2oid["P-192"] = ASN1::secp192r1();
  494. name2oid["P-224"] = ASN1::secp224r1();
  495. name2oid["P-256"] = ASN1::secp256r1();
  496. name2oid["P-384"] = ASN1::secp384r1();
  497. name2oid["P-521"] = ASN1::secp521r1();
  498. name2oid["K-163"] = ASN1::sect163k1();
  499. name2oid["K-233"] = ASN1::sect233k1();
  500. name2oid["K-283"] = ASN1::sect283k1();
  501. name2oid["K-409"] = ASN1::sect409k1();
  502. name2oid["K-571"] = ASN1::sect571k1();
  503. name2oid["B-163"] = ASN1::sect163r2();
  504. name2oid["B-233"] = ASN1::sect233r1();
  505. name2oid["B-283"] = ASN1::sect283r1();
  506. name2oid["B-409"] = ASN1::sect409r1();
  507. name2oid["B-571"] = ASN1::sect571r1();
  508. if (m_test == "PKV")
  509. {
  510. bool pass;
  511. if (m_bracketString[0] == 'P')
  512. pass = EC_PKV<ECP>(m_rng, DecodeHex(m_data["Qx"]), DecodeHex(m_data["Qy"]), name2oid[m_bracketString]);
  513. else
  514. pass = EC_PKV<EC2N>(m_rng, DecodeHex(m_data["Qx"]), DecodeHex(m_data["Qy"]), name2oid[m_bracketString]);
  515. OutputData(output, "Result ", pass ? "P" : "F");
  516. }
  517. else if (m_test == "KeyPair")
  518. {
  519. if (m_bracketString[0] == 'P')
  520. EC_KeyPair<ECP>(output, atol(m_data["N"].c_str()), name2oid[m_bracketString]);
  521. else
  522. EC_KeyPair<EC2N>(output, atol(m_data["N"].c_str()), name2oid[m_bracketString]);
  523. }
  524. else if (m_test == "SigGen")
  525. {
  526. if (m_bracketString[0] == 'P')
  527. EC_SigGen<ECP>(output, name2oid[m_bracketString]);
  528. else
  529. EC_SigGen<EC2N>(output, name2oid[m_bracketString]);
  530. }
  531. else if (m_test == "SigVer")
  532. {
  533. if (m_bracketString[0] == 'P')
  534. EC_SigVer<ECP>(output, name2oid[m_bracketString]);
  535. else
  536. EC_SigVer<EC2N>(output, name2oid[m_bracketString]);
  537. }
  538. AttachedTransformation()->Put((byte *)output.data(), output.size());
  539. output.resize(0);
  540. return;
  541. }
  542. if (m_algorithm == "RSA")
  543. {
  544. std::string shaAlg = m_data["SHAAlg"].substr(3);
  545. if (m_test == "Ver")
  546. {
  547. Integer n((m_data["n"] + "h").c_str());
  548. Integer e((m_data["e"] + "h").c_str());
  549. RSA::PublicKey pub;
  550. pub.Initialize(n, e);
  551. member_ptr<PK_Verifier> pV(CreateRSA<PK_Verifier>(m_mode, shaAlg));
  552. pV->AccessMaterial().AssignFrom(pub);
  553. HexDecoder filter(new SignatureVerificationFilter(*pV));
  554. for (unsigned int i=m_data["S"].size(); i<pV->SignatureLength()*2; i++)
  555. filter.Put('0');
  556. StringSource(m_data["S"], true, new Redirector(filter, Redirector::DATA_ONLY));
  557. StringSource(m_data["Msg"], true, new Redirector(filter, Redirector::DATA_ONLY));
  558. filter.MessageEnd();
  559. byte b;
  560. filter.Get(b);
  561. OutputData(output, "Result ", b ? "P" : "F");
  562. }
  563. else
  564. {
  565. assert(m_test == "Gen");
  566. int modLen = atol(m_bracketString.substr(6).c_str());
  567. std::string &encodedKey = m_data["PrivKey"];
  568. RSA::PrivateKey priv;
  569. if (!encodedKey.empty())
  570. {
  571. StringStore s(encodedKey);
  572. priv.BERDecode(s);
  573. if (priv.GetModulus().BitCount() != modLen)
  574. encodedKey.clear();
  575. }
  576. if (encodedKey.empty())
  577. {
  578. priv.Initialize(m_rng, modLen);
  579. StringSink s(encodedKey);
  580. priv.DEREncode(s);
  581. OutputData(output, "n ", priv.GetModulus());
  582. OutputData(output, "e ", priv.GetPublicExponent(), modLen/8);
  583. }
  584. member_ptr<PK_Signer> pS(CreateRSA<PK_Signer>(m_mode, shaAlg));
  585. pS->AccessMaterial().AssignFrom(priv);
  586. SecByteBlock sig(pS->SignatureLength());
  587. StringSource(m_data["Msg"], true, new HexDecoder(new SignerFilter(m_rng, *pS, new ArraySink(sig, sig.size()))));
  588. OutputData(output, "SHAAlg ", m_data["SHAAlg"]);
  589. OutputData(output, "Msg ", m_data["Msg"]);
  590. OutputData(output, "S ", sig);
  591. }
  592. AttachedTransformation()->Put((byte *)output.data(), output.size());
  593. output.resize(0);
  594. return;
  595. }
  596. if (m_algorithm == "SHA")
  597. {
  598. member_ptr<HashFunction> pHF;
  599. if (m_mode == "1")
  600. pHF.reset(new SHA1);
  601. else if (m_mode == "224")
  602. pHF.reset(new SHA224);
  603. else if (m_mode == "256")
  604. pHF.reset(new SHA256);
  605. else if (m_mode == "384")
  606. pHF.reset(new SHA384);
  607. else if (m_mode == "512")
  608. pHF.reset(new SHA512);
  609. if (m_test == "MONTE")
  610. {
  611. SecByteBlock seed = m_data2[INPUT];
  612. SecByteBlock MD[1003];
  613. int i,j;
  614. for (j=0; j<100; j++)
  615. {
  616. MD[0] = MD[1] = MD[2] = seed;
  617. for (i=3; i<1003; i++)
  618. {
  619. SecByteBlock Mi = MD[i-3] + MD[i-2] + MD[i-1];
  620. MD[i].resize(pHF->DigestSize());
  621. pHF->CalculateDigest(MD[i], Mi, Mi.size());
  622. }
  623. seed = MD[1002];
  624. OutputData(output, "COUNT ", j);
  625. OutputData(output, "MD ", seed);
  626. AttachedTransformation()->Put((byte *)output.data(), output.size());
  627. output.resize(0);
  628. }
  629. }
  630. else
  631. {
  632. SecByteBlock tag(pHF->DigestSize());
  633. SecByteBlock &msg(m_data2[INPUT]);
  634. int len = atol(m_data["Len"].c_str());
  635. StringSource(msg.begin(), len/8, true, new HashFilter(*pHF, new ArraySink(tag, tag.size())));
  636. OutputData(output, "MD ", tag);
  637. AttachedTransformation()->Put((byte *)output.data(), output.size());
  638. output.resize(0);
  639. }
  640. return;
  641. }
  642. SecByteBlock &key = m_data2[KEY_T];
  643. if (m_algorithm == "TDES")
  644. {
  645. if (!m_data["KEY1"].empty())
  646. {
  647. const std::string keys[3] = {m_data["KEY1"], m_data["KEY2"], m_data["KEY3"]};
  648. key.resize(24);
  649. HexDecoder hexDec(new ArraySink(key, key.size()));
  650. for (int i=0; i<3; i++)
  651. hexDec.Put((byte *)keys[i].data(), keys[i].size());
  652. if (keys[0] == keys[2])
  653. {
  654. if (keys[0] == keys[1])
  655. key.resize(8);
  656. else
  657. key.resize(16);
  658. }
  659. else
  660. key.resize(24);
  661. }
  662. }
  663. if (m_algorithm == "RNG")
  664. {
  665. key.resize(24);
  666. StringSource(m_data["Key1"] + m_data["Key2"] + m_data["Key3"], true, new HexDecoder(new ArraySink(key, key.size())));
  667. SecByteBlock seed(m_data2[INPUT]), dt(m_data2[IV]), r(8);
  668. X917RNG rng(new DES_EDE3::Encryption(key, key.size()), seed, dt);
  669. if (m_test == "MCT")
  670. {
  671. for (int i=0; i<10000; i++)
  672. rng.GenerateBlock(r, r.size());
  673. }
  674. else
  675. {
  676. rng.GenerateBlock(r, r.size());
  677. }
  678. OutputData(output, "R ", r);
  679. AttachedTransformation()->Put((byte *)output.data(), output.size());
  680. output.resize(0);
  681. return;
  682. }
  683. if (m_algorithm == "HMAC")
  684. {
  685. member_ptr<MessageAuthenticationCode> pMAC;
  686. if (m_bracketString == "L=20")
  687. pMAC.reset(new HMAC<SHA1>);
  688. else if (m_bracketString == "L=28")
  689. pMAC.reset(new HMAC<SHA224>);
  690. else if (m_bracketString == "L=32")
  691. pMAC.reset(new HMAC<SHA256>);
  692. else if (m_bracketString == "L=48")
  693. pMAC.reset(new HMAC<SHA384>);
  694. else if (m_bracketString == "L=64")
  695. pMAC.reset(new HMAC<SHA512>);
  696. else
  697. throw Exception(Exception::OTHER_ERROR, "TestDataParser: unexpected HMAC bracket string: " + m_bracketString);
  698. pMAC->SetKey(key, key.size());
  699. int Tlen = atol(m_data["Tlen"].c_str());
  700. SecByteBlock tag(Tlen);
  701. StringSource(m_data["Msg"], true, new HexDecoder(new HashFilter(*pMAC, new ArraySink(tag, Tlen), false, Tlen)));
  702. OutputData(output, "Mac ", tag);
  703. AttachedTransformation()->Put((byte *)output.data(), output.size());
  704. output.resize(0);
  705. return;
  706. }
  707. member_ptr<BlockCipher> pBT;
  708. if (m_algorithm == "DES")
  709. pBT.reset(NewBT((DES*)0));
  710. else if (m_algorithm == "TDES")
  711. {
  712. if (key.size() == 8)
  713. pBT.reset(NewBT((DES*)0));
  714. else if (key.size() == 16)
  715. pBT.reset(NewBT((DES_EDE2*)0));
  716. else
  717. pBT.reset(NewBT((DES_EDE3*)0));
  718. }
  719. else if (m_algorithm == "SKIPJACK")
  720. pBT.reset(NewBT((SKIPJACK*)0));
  721. else if (m_algorithm == "AES")
  722. pBT.reset(NewBT((AES*)0));
  723. else
  724. throw Exception(Exception::OTHER_ERROR, "TestDataParser: unexpected algorithm: " + m_algorithm);
  725. if (!pBT->IsValidKeyLength(key.size()))
  726. key.CleanNew(pBT->DefaultKeyLength()); // for Scbcvrct
  727. pBT->SetKey(key.data(), key.size());
  728. SecByteBlock &iv = m_data2[IV];
  729. if (iv.empty())
  730. iv.CleanNew(pBT->BlockSize());
  731. member_ptr<SymmetricCipher> pCipher;
  732. unsigned int K = m_feedbackSize;
  733. if (m_mode == "ECB")
  734. pCipher.reset(NewMode((ECB_Mode_ExternalCipher*)0, *pBT, iv));
  735. else if (m_mode == "CBC")
  736. pCipher.reset(NewMode((CBC_Mode_ExternalCipher*)0, *pBT, iv));
  737. else if (m_mode == "CFB")
  738. pCipher.reset(NewMode((CFB_Mode_ExternalCipher*)0, *pBT, iv));
  739. else if (m_mode == "OFB")
  740. pCipher.reset(NewMode((OFB_Mode_ExternalCipher*)0, *pBT, iv));
  741. else
  742. throw Exception(Exception::OTHER_ERROR, "TestDataParser: unexpected mode: " + m_mode);
  743. bool encrypt = m_encrypt;
  744. if (m_test == "MONTE")
  745. {
  746. SecByteBlock KEY[401];
  747. KEY[0] = key;
  748. int keySize = key.size();
  749. int blockSize = pBT->BlockSize();
  750. std::vector<SecByteBlock> IB(10001), OB(10001), PT(10001), CT(10001), RESULT(10001), TXT(10001), CV(10001);
  751. PT[0] = GetData("PLAINTEXT");
  752. CT[0] = GetData("CIPHERTEXT");
  753. CV[0] = IB[0] = iv;
  754. TXT[0] = GetData("TEXT");
  755. int outerCount = (m_algorithm == "AES") ? 100 : 400;
  756. int innerCount = (m_algorithm == "AES") ? 1000 : 10000;
  757. for (int i=0; i<outerCount; i++)
  758. {
  759. pBT->SetKey(KEY[i], keySize);
  760. for (int j=0; j<innerCount; j++)
  761. {
  762. if (m_mode == "ECB")
  763. {
  764. if (encrypt)
  765. {
  766. IB[j] = PT[j];
  767. CT[j].resize(blockSize);
  768. pBT->ProcessBlock(IB[j], CT[j]);
  769. PT[j+1] = CT[j];
  770. }
  771. else
  772. {
  773. IB[j] = CT[j];
  774. PT[j].resize(blockSize);
  775. pBT->ProcessBlock(IB[j], PT[j]);
  776. CT[j+1] = PT[j];
  777. }
  778. }
  779. else if (m_mode == "OFB")
  780. {
  781. OB[j].resize(blockSize);
  782. pBT->ProcessBlock(IB[j], OB[j]);
  783. Xor(RESULT[j], OB[j], TXT[j]);
  784. TXT[j+1] = IB[j];
  785. IB[j+1] = OB[j];
  786. }
  787. else if (m_mode == "CBC")
  788. {
  789. if (encrypt)
  790. {
  791. Xor(IB[j], PT[j], CV[j]);
  792. CT[j].resize(blockSize);
  793. pBT->ProcessBlock(IB[j], CT[j]);
  794. PT[j+1] = CV[j];
  795. CV[j+1] = CT[j];
  796. }
  797. else
  798. {
  799. IB[j] = CT[j];
  800. OB[j].resize(blockSize);
  801. pBT->ProcessBlock(IB[j], OB[j]);
  802. Xor(PT[j], OB[j], CV[j]);
  803. CV[j+1] = CT[j];
  804. CT[j+1] = PT[j];
  805. }
  806. }
  807. else if (m_mode == "CFB")
  808. {
  809. if (encrypt)
  810. {
  811. OB[j].resize(blockSize);
  812. pBT->ProcessBlock(IB[j], OB[j]);
  813. AssignLeftMostBits(CT[j], OB[j], K);
  814. Xor(CT[j], CT[j], PT[j]);
  815. AssignLeftMostBits(PT[j+1], IB[j], K);
  816. IB[j+1].resize(blockSize);
  817. memcpy(IB[j+1], IB[j]+K/8, blockSize-K/8);
  818. memcpy(IB[j+1]+blockSize-K/8, CT[j], K/8);
  819. }
  820. else
  821. {
  822. OB[j].resize(blockSize);
  823. pBT->ProcessBlock(IB[j], OB[j]);
  824. AssignLeftMostBits(PT[j], OB[j], K);
  825. Xor(PT[j], PT[j], CT[j]);
  826. IB[j+1].resize(blockSize);
  827. memcpy(IB[j+1], IB[j]+K/8, blockSize-K/8);
  828. memcpy(IB[j+1]+blockSize-K/8, CT[j], K/8);
  829. AssignLeftMostBits(CT[j+1], OB[j], K);
  830. }
  831. }
  832. else
  833. throw Exception(Exception::OTHER_ERROR, "TestDataParser: unexpected mode: " + m_mode);
  834. }
  835. OutputData(output, COUNT, IntToString(i));
  836. OutputData(output, KEY_T, KEY[i]);
  837. if (m_mode == "CBC")
  838. OutputData(output, IV, CV[0]);
  839. if (m_mode == "OFB" || m_mode == "CFB")
  840. OutputData(output, IV, IB[0]);
  841. if (m_mode == "ECB" || m_mode == "CBC" || m_mode == "CFB")
  842. {
  843. if (encrypt)
  844. {
  845. OutputData(output, INPUT, PT[0]);
  846. OutputData(output, OUTPUT, CT[innerCount-1]);
  847. KEY[i+1] = UpdateKey(KEY[i], &CT[0]);
  848. }
  849. else
  850. {
  851. OutputData(output, INPUT, CT[0]);
  852. OutputData(output, OUTPUT, PT[innerCount-1]);
  853. KEY[i+1] = UpdateKey(KEY[i], &PT[0]);
  854. }
  855. PT[0] = PT[innerCount];
  856. IB[0] = IB[innerCount];
  857. CV[0] = CV[innerCount];
  858. CT[0] = CT[innerCount];
  859. }
  860. else if (m_mode == "OFB")
  861. {
  862. OutputData(output, INPUT, TXT[0]);
  863. OutputData(output, OUTPUT, RESULT[innerCount-1]);
  864. KEY[i+1] = UpdateKey(KEY[i], &RESULT[0]);
  865. Xor(TXT[0], TXT[0], IB[innerCount-1]);
  866. IB[0] = OB[innerCount-1];
  867. }
  868. output += "\n";
  869. AttachedTransformation()->Put((byte *)output.data(), output.size());
  870. output.resize(0);
  871. }
  872. }
  873. else if (m_test == "MCT")
  874. {
  875. SecByteBlock KEY[101];
  876. KEY[0] = key;
  877. int keySize = key.size();
  878. int blockSize = pBT->BlockSize();
  879. SecByteBlock ivs[101], inputs[1001], outputs[1001];
  880. ivs[0] = iv;
  881. inputs[0] = m_data2[INPUT];
  882. for (int i=0; i<100; i++)
  883. {
  884. pCipher->SetKey(KEY[i], keySize, MakeParameters(Name::IV(), (const byte *)ivs[i])(Name::FeedbackSize(), (int)K/8, false));
  885. for (int j=0; j<1000; j++)
  886. {
  887. outputs[j] = inputs[j];
  888. pCipher->ProcessString(outputs[j], outputs[j].size());
  889. if (K==8 && m_mode == "CFB")
  890. {
  891. if (j<16)
  892. inputs[j+1].Assign(ivs[i]+j, 1);
  893. else
  894. inputs[j+1] = outputs[j-16];
  895. }
  896. else if (m_mode == "ECB")
  897. inputs[j+1] = outputs[j];
  898. else if (j == 0)
  899. inputs[j+1] = ivs[i];
  900. else
  901. inputs[j+1] = outputs[j-1];
  902. }
  903. if (m_algorithm == "AES")
  904. OutputData(output, COUNT, m_count++);
  905. OutputData(output, KEY_T, KEY[i]);
  906. if (m_mode != "ECB")
  907. OutputData(output, IV, ivs[i]);
  908. OutputData(output, INPUT, inputs[0]);
  909. OutputData(output, OUTPUT, outputs[999]);
  910. output += "\n";
  911. AttachedTransformation()->Put((byte *)output.data(), output.size());
  912. output.resize(0);
  913. KEY[i+1] = UpdateKey(KEY[i], outputs);
  914. ivs[i+1].CleanNew(pCipher->IVSize());
  915. ivs[i+1] = UpdateKey(ivs[i+1], outputs);
  916. if (K==8 && m_mode == "CFB")
  917. inputs[0] = outputs[999-16];
  918. else if (m_mode == "ECB")
  919. inputs[0] = outputs[999];
  920. else
  921. inputs[0] = outputs[998];
  922. }
  923. }
  924. else
  925. {
  926. assert(m_test == "KAT");
  927. SecByteBlock &input = m_data2[INPUT];
  928. SecByteBlock result(input.size());
  929. member_ptr<Filter> pFilter(new StreamTransformationFilter(*pCipher, new ArraySink(result, result.size()), StreamTransformationFilter::NO_PADDING));
  930. StringSource(input.data(), input.size(), true, pFilter.release());
  931. OutputGivenData(output, COUNT, true);
  932. OutputData(output, KEY_T, key);
  933. OutputGivenData(output, IV, true);
  934. OutputGivenData(output, INPUT);
  935. OutputData(output, OUTPUT, result);
  936. output += "\n";
  937. AttachedTransformation()->Put((byte *)output.data(), output.size());
  938. }
  939. }
  940. std::vector<std::string> Tokenize(const std::string &line)
  941. {
  942. std::vector<std::string> result;
  943. std::string s;
  944. for (unsigned int i=0; i<line.size(); i++)
  945. {
  946. if (isalnum(line[i]) || line[i] == '^')
  947. s += line[i];
  948. else if (!s.empty())
  949. {
  950. result.push_back(s);
  951. s = "";
  952. }
  953. if (line[i] == '=')
  954. result.push_back("=");
  955. }
  956. if (!s.empty())
  957. result.push_back(s);
  958. return result;
  959. }
  960. bool IsolatedMessageEnd(bool blocking)
  961. {
  962. if (!blocking)
  963. throw BlockingInputOnly("TestDataParser");
  964. m_line.resize(0);
  965. m_inQueue.TransferTo(StringSink(m_line).Ref());
  966. if (m_line[0] == '#')
  967. return false;
  968. bool copyLine = false;
  969. if (m_line[0] == '[')
  970. {
  971. m_bracketString = m_line.substr(1, m_line.size()-2);
  972. if (m_bracketString == "ENCRYPT")
  973. SetEncrypt(true);
  974. if (m_bracketString == "DECRYPT")
  975. SetEncrypt(false);
  976. copyLine = true;
  977. }
  978. if (m_line.substr(0, 2) == "H>")
  979. {
  980. assert(m_test == "sha");
  981. m_bracketString = m_line.substr(2, m_line.size()-4);
  982. m_line = m_line.substr(0, 13) + "Hashes<H";
  983. copyLine = true;
  984. }
  985. if (m_line == "D>")
  986. copyLine = true;
  987. if (m_line == "<D")
  988. {
  989. m_line += "\n";
  990. copyLine = true;
  991. }
  992. if (copyLine)
  993. {
  994. m_line += '\n';
  995. AttachedTransformation()->Put((byte *)m_line.data(), m_line.size(), blocking);
  996. return false;
  997. }
  998. std::vector<std::string> tokens = Tokenize(m_line);
  999. if (m_algorithm == "DSA" && m_test == "sha")
  1000. {
  1001. for (unsigned int i = 0; i < tokens.size(); i++)
  1002. {
  1003. if (tokens[i] == "^")
  1004. DoTest();
  1005. else if (tokens[i] != "")
  1006. m_compactString.push_back(atol(tokens[i].c_str()));
  1007. }
  1008. }
  1009. else
  1010. {
  1011. if (!m_line.empty() && ((m_algorithm == "RSA" && m_test != "Gen") || m_algorithm == "RNG" || m_algorithm == "HMAC" || m_algorithm == "SHA" || (m_algorithm == "ECDSA" && m_test != "KeyPair") || (m_algorithm == "DSA" && (m_test == "PQGVer" || m_test == "SigVer"))))
  1012. {
  1013. // copy input to output
  1014. std::string output = m_line + '\n';
  1015. AttachedTransformation()->Put((byte *)output.data(), output.size());
  1016. }
  1017. for (unsigned int i = 0; i < tokens.size(); i++)
  1018. {
  1019. if (m_firstLine && m_algorithm != "DSA")
  1020. {
  1021. if (tokens[i] == "Encrypt" || tokens[i] == "OFB")
  1022. SetEncrypt(true);
  1023. else if (tokens[i] == "Decrypt")
  1024. SetEncrypt(false);
  1025. else if (tokens[i] == "Modes")
  1026. m_test = "MONTE";
  1027. }
  1028. else
  1029. {
  1030. if (tokens[i] != "=")
  1031. continue;
  1032. if (i == 0)
  1033. throw Exception(Exception::OTHER_ERROR, "TestDataParser: unexpected data: " + m_line);
  1034. const std::string &key = tokens[i-1];
  1035. std::string &data = m_data[key];
  1036. data = (tokens.size() > i+1) ? tokens[i+1] : "";
  1037. DataType t = m_nameToType[key];
  1038. m_typeToName[t] = key;
  1039. m_data2[t] = DecodeHex(data);
  1040. if (key == m_trigger || (t == OUTPUT && !m_data2[INPUT].empty() && !isspace(m_line[0])))
  1041. DoTest();
  1042. }
  1043. }
  1044. }
  1045. m_firstLine = false;
  1046. return false;
  1047. }
  1048. inline const SecByteBlock & GetData(const std::string &key)
  1049. {
  1050. return m_data2[m_nameToType[key]];
  1051. }
  1052. static SecByteBlock DecodeHex(const std::string &data)
  1053. {
  1054. SecByteBlock data2(data.size() / 2);
  1055. StringSource(data, true, new HexDecoder(new ArraySink(data2, data2.size())));
  1056. return data2;
  1057. }
  1058. std::string m_algorithm, m_test, m_mode, m_line, m_bracketString, m_trigger;
  1059. unsigned int m_feedbackSize, m_blankLineTransition;
  1060. bool m_encrypt, m_firstLine;
  1061. typedef std::map<std::string, DataType> NameToTypeMap;
  1062. NameToTypeMap m_nameToType;
  1063. typedef std::map<DataType, std::string> TypeToNameMap;
  1064. TypeToNameMap m_typeToName;
  1065. typedef std::map<std::string, std::string> Map;
  1066. Map m_data; // raw data
  1067. typedef std::map<DataType, SecByteBlock> Map2;
  1068. Map2 m_data2;
  1069. int m_count;
  1070. AutoSeededX917RNG<AES> m_rng;
  1071. std::vector<unsigned int> m_compactString;
  1072. };
  1073. int FIPS_140_AlgorithmTest(int argc, char **argv)
  1074. {
  1075. argc--;
  1076. argv++;
  1077. std::string algorithm = argv[1];
  1078. std::string pathname = argv[2];
  1079. unsigned int i = pathname.find_last_of("\\/");
  1080. std::string filename = pathname.substr(i == std::string::npos ? 0 : i+1);
  1081. std::string dirname = pathname.substr(0, i);
  1082. if (algorithm == "auto")
  1083. {
  1084. string algTable[] = {"AES", "ECDSA", "DSA", "HMAC", "RNG", "RSA", "TDES", "SKIPJACK", "SHA"}; // order is important here
  1085. for (i=0; i<sizeof(algTable)/sizeof(algTable[0]); i++)
  1086. {
  1087. if (dirname.find(algTable[i]) != std::string::npos)
  1088. {
  1089. algorithm = algTable[i];
  1090. break;
  1091. }
  1092. }
  1093. }
  1094. try
  1095. {
  1096. std::string mode;
  1097. if (algorithm == "SHA")
  1098. mode = IntToString(atol(filename.substr(3, 3).c_str()));
  1099. else if (algorithm == "RSA")
  1100. mode = filename.substr(6, 1);
  1101. else if (filename[0] == 'S' || filename[0] == 'T')
  1102. mode = filename.substr(1, 3);
  1103. else
  1104. mode = filename.substr(0, 3);
  1105. for (i = 0; i<mode.size(); i++)
  1106. mode[i] = toupper(mode[i]);
  1107. unsigned int feedbackSize = mode == "CFB" ? atoi(filename.substr(filename.find_first_of("0123456789")).c_str()) : 0;
  1108. std::string test;
  1109. if (algorithm == "DSA" || algorithm == "ECDSA")
  1110. test = filename.substr(0, filename.size() - 4);
  1111. else if (algorithm == "RSA")
  1112. test = filename.substr(3, 3);
  1113. else if (filename.find("Monte") != std::string::npos)
  1114. test = "MONTE";
  1115. else if (filename.find("MCT") != std::string::npos)
  1116. test = "MCT";
  1117. else
  1118. test = "KAT";
  1119. bool encrypt = (filename.find("vrct") == std::string::npos);
  1120. BufferedTransformation *pSink = NULL;
  1121. if (argc > 3)
  1122. {
  1123. std::string outDir = argv[3];
  1124. if (outDir == "auto")
  1125. {
  1126. if (dirname.substr(dirname.size()-3) == "req")
  1127. outDir = dirname.substr(0, dirname.size()-3) + "resp";
  1128. }
  1129. if (*outDir.rbegin() != '\\' && *outDir.rbegin() != '/')
  1130. outDir += '/';
  1131. std::string outPathname = outDir + filename.substr(0, filename.size() - 3) + "rsp";
  1132. pSink = new FileSink(outPathname.c_str(), false);
  1133. }
  1134. else
  1135. pSink = new FileSink(cout);
  1136. FileSource(pathname.c_str(), true, new LineBreakParser(new TestDataParser(algorithm, test, mode, feedbackSize, encrypt, pSink)), false);
  1137. }
  1138. catch (...)
  1139. {
  1140. cout << "file: " << filename << endl;
  1141. throw;
  1142. }
  1143. return 0;
  1144. }
  1145. extern int (*AdhocTest)(int argc, char *argv[]);
  1146. static int s_i = (AdhocTest = &FIPS_140_AlgorithmTest, 0);
  1147. #endif