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.

764 lines
24 KiB

  1. // validat2.cpp - written and placed in the public domain by Wei Dai
  2. #include "pch.h"
  3. #define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
  4. #include "blumshub.h"
  5. #include "rsa.h"
  6. #include "md2.h"
  7. #include "elgamal.h"
  8. #include "nr.h"
  9. #include "dsa.h"
  10. #include "dh.h"
  11. #include "mqv.h"
  12. #include "luc.h"
  13. #include "xtrcrypt.h"
  14. #include "rabin.h"
  15. #include "rw.h"
  16. #include "eccrypto.h"
  17. #include "ecp.h"
  18. #include "ec2n.h"
  19. #include "asn.h"
  20. #include "rng.h"
  21. #include "files.h"
  22. #include "hex.h"
  23. #include "oids.h"
  24. #include "esign.h"
  25. #include "osrng.h"
  26. #include <iostream>
  27. #include <iomanip>
  28. #include "validate.h"
  29. USING_NAMESPACE(CryptoPP)
  30. USING_NAMESPACE(std)
  31. class FixedRNG : public RandomNumberGenerator
  32. {
  33. public:
  34. FixedRNG(BufferedTransformation &source) : m_source(source) {}
  35. void GenerateBlock(byte *output, size_t size)
  36. {
  37. m_source.Get(output, size);
  38. }
  39. private:
  40. BufferedTransformation &m_source;
  41. };
  42. bool ValidateBBS()
  43. {
  44. cout << "\nBlumBlumShub validation suite running...\n\n";
  45. Integer p("212004934506826557583707108431463840565872545889679278744389317666981496005411448865750399674653351");
  46. Integer q("100677295735404212434355574418077394581488455772477016953458064183204108039226017738610663984508231");
  47. Integer seed("63239752671357255800299643604761065219897634268887145610573595874544114193025997412441121667211431");
  48. BlumBlumShub bbs(p, q, seed);
  49. bool pass = true, fail;
  50. int j;
  51. const byte output1[] = {
  52. 0x49,0xEA,0x2C,0xFD,0xB0,0x10,0x64,0xA0,0xBB,0xB9,
  53. 0x2A,0xF1,0x01,0xDA,0xC1,0x8A,0x94,0xF7,0xB7,0xCE};
  54. const byte output2[] = {
  55. 0x74,0x45,0x48,0xAE,0xAC,0xB7,0x0E,0xDF,0xAF,0xD7,
  56. 0xD5,0x0E,0x8E,0x29,0x83,0x75,0x6B,0x27,0x46,0xA1};
  57. byte buf[20];
  58. bbs.GenerateBlock(buf, 20);
  59. fail = memcmp(output1, buf, 20) != 0;
  60. pass = pass && !fail;
  61. cout << (fail ? "FAILED " : "passed ");
  62. for (j=0;j<20;j++)
  63. cout << setw(2) << setfill('0') << hex << (int)buf[j];
  64. cout << endl;
  65. bbs.Seek(10);
  66. bbs.GenerateBlock(buf, 10);
  67. fail = memcmp(output1+10, buf, 10) != 0;
  68. pass = pass && !fail;
  69. cout << (fail ? "FAILED " : "passed ");
  70. for (j=0;j<10;j++)
  71. cout << setw(2) << setfill('0') << hex << (int)buf[j];
  72. cout << endl;
  73. bbs.Seek(1234567);
  74. bbs.GenerateBlock(buf, 20);
  75. fail = memcmp(output2, buf, 20) != 0;
  76. pass = pass && !fail;
  77. cout << (fail ? "FAILED " : "passed ");
  78. for (j=0;j<20;j++)
  79. cout << setw(2) << setfill('0') << hex << (int)buf[j];
  80. cout << endl;
  81. return pass;
  82. }
  83. bool SignatureValidate(PK_Signer &priv, PK_Verifier &pub, bool thorough = false)
  84. {
  85. bool pass = true, fail;
  86. fail = !pub.GetMaterial().Validate(GlobalRNG(), thorough ? 3 : 2) || !priv.GetMaterial().Validate(GlobalRNG(), thorough ? 3 : 2);
  87. pass = pass && !fail;
  88. cout << (fail ? "FAILED " : "passed ");
  89. cout << "signature key validation\n";
  90. const byte *message = (byte *)"test message";
  91. const int messageLen = 12;
  92. SecByteBlock signature(priv.MaxSignatureLength());
  93. size_t signatureLength = priv.SignMessage(GlobalRNG(), message, messageLen, signature);
  94. fail = !pub.VerifyMessage(message, messageLen, signature, signatureLength);
  95. pass = pass && !fail;
  96. cout << (fail ? "FAILED " : "passed ");
  97. cout << "signature and verification\n";
  98. ++signature[0];
  99. fail = pub.VerifyMessage(message, messageLen, signature, signatureLength);
  100. pass = pass && !fail;
  101. cout << (fail ? "FAILED " : "passed ");
  102. cout << "checking invalid signature" << endl;
  103. if (priv.MaxRecoverableLength() > 0)
  104. {
  105. signatureLength = priv.SignMessageWithRecovery(GlobalRNG(), message, messageLen, NULL, 0, signature);
  106. SecByteBlock recovered(priv.MaxRecoverableLengthFromSignatureLength(signatureLength));
  107. DecodingResult result = pub.RecoverMessage(recovered, NULL, 0, signature, signatureLength);
  108. fail = !(result.isValidCoding && result.messageLength == messageLen && memcmp(recovered, message, messageLen) == 0);
  109. pass = pass && !fail;
  110. cout << (fail ? "FAILED " : "passed ");
  111. cout << "signature and verification with recovery" << endl;
  112. ++signature[0];
  113. result = pub.RecoverMessage(recovered, NULL, 0, signature, signatureLength);
  114. fail = result.isValidCoding;
  115. pass = pass && !fail;
  116. cout << (fail ? "FAILED " : "passed ");
  117. cout << "recovery with invalid signature" << endl;
  118. }
  119. return pass;
  120. }
  121. bool CryptoSystemValidate(PK_Decryptor &priv, PK_Encryptor &pub, bool thorough = false)
  122. {
  123. bool pass = true, fail;
  124. fail = !pub.GetMaterial().Validate(GlobalRNG(), thorough ? 3 : 2) || !priv.GetMaterial().Validate(GlobalRNG(), thorough ? 3 : 2);
  125. pass = pass && !fail;
  126. cout << (fail ? "FAILED " : "passed ");
  127. cout << "cryptosystem key validation\n";
  128. const byte *message = (byte *)"test message";
  129. const int messageLen = 12;
  130. SecByteBlock ciphertext(priv.CiphertextLength(messageLen));
  131. SecByteBlock plaintext(priv.MaxPlaintextLength(ciphertext.size()));
  132. pub.Encrypt(GlobalRNG(), message, messageLen, ciphertext);
  133. fail = priv.Decrypt(GlobalRNG(), ciphertext, priv.CiphertextLength(messageLen), plaintext) != DecodingResult(messageLen);
  134. fail = fail || memcmp(message, plaintext, messageLen);
  135. pass = pass && !fail;
  136. cout << (fail ? "FAILED " : "passed ");
  137. cout << "encryption and decryption\n";
  138. return pass;
  139. }
  140. bool SimpleKeyAgreementValidate(SimpleKeyAgreementDomain &d)
  141. {
  142. if (d.GetCryptoParameters().Validate(GlobalRNG(), 3))
  143. cout << "passed simple key agreement domain parameters validation" << endl;
  144. else
  145. {
  146. cout << "FAILED simple key agreement domain parameters invalid" << endl;
  147. return false;
  148. }
  149. SecByteBlock priv1(d.PrivateKeyLength()), priv2(d.PrivateKeyLength());
  150. SecByteBlock pub1(d.PublicKeyLength()), pub2(d.PublicKeyLength());
  151. SecByteBlock val1(d.AgreedValueLength()), val2(d.AgreedValueLength());
  152. d.GenerateKeyPair(GlobalRNG(), priv1, pub1);
  153. d.GenerateKeyPair(GlobalRNG(), priv2, pub2);
  154. memset(val1.begin(), 0x10, val1.size());
  155. memset(val2.begin(), 0x11, val2.size());
  156. if (!(d.Agree(val1, priv1, pub2) && d.Agree(val2, priv2, pub1)))
  157. {
  158. cout << "FAILED simple key agreement failed" << endl;
  159. return false;
  160. }
  161. if (memcmp(val1.begin(), val2.begin(), d.AgreedValueLength()))
  162. {
  163. cout << "FAILED simple agreed values not equal" << endl;
  164. return false;
  165. }
  166. cout << "passed simple key agreement" << endl;
  167. return true;
  168. }
  169. bool AuthenticatedKeyAgreementValidate(AuthenticatedKeyAgreementDomain &d)
  170. {
  171. if (d.GetCryptoParameters().Validate(GlobalRNG(), 3))
  172. cout << "passed authenticated key agreement domain parameters validation" << endl;
  173. else
  174. {
  175. cout << "FAILED authenticated key agreement domain parameters invalid" << endl;
  176. return false;
  177. }
  178. SecByteBlock spriv1(d.StaticPrivateKeyLength()), spriv2(d.StaticPrivateKeyLength());
  179. SecByteBlock epriv1(d.EphemeralPrivateKeyLength()), epriv2(d.EphemeralPrivateKeyLength());
  180. SecByteBlock spub1(d.StaticPublicKeyLength()), spub2(d.StaticPublicKeyLength());
  181. SecByteBlock epub1(d.EphemeralPublicKeyLength()), epub2(d.EphemeralPublicKeyLength());
  182. SecByteBlock val1(d.AgreedValueLength()), val2(d.AgreedValueLength());
  183. d.GenerateStaticKeyPair(GlobalRNG(), spriv1, spub1);
  184. d.GenerateStaticKeyPair(GlobalRNG(), spriv2, spub2);
  185. d.GenerateEphemeralKeyPair(GlobalRNG(), epriv1, epub1);
  186. d.GenerateEphemeralKeyPair(GlobalRNG(), epriv2, epub2);
  187. memset(val1.begin(), 0x10, val1.size());
  188. memset(val2.begin(), 0x11, val2.size());
  189. if (!(d.Agree(val1, spriv1, epriv1, spub2, epub2) && d.Agree(val2, spriv2, epriv2, spub1, epub1)))
  190. {
  191. cout << "FAILED authenticated key agreement failed" << endl;
  192. return false;
  193. }
  194. if (memcmp(val1.begin(), val2.begin(), d.AgreedValueLength()))
  195. {
  196. cout << "FAILED authenticated agreed values not equal" << endl;
  197. return false;
  198. }
  199. cout << "passed authenticated key agreement" << endl;
  200. return true;
  201. }
  202. bool ValidateRSA()
  203. {
  204. cout << "\nRSA validation suite running...\n\n";
  205. byte out[100], outPlain[100];
  206. bool pass = true, fail;
  207. {
  208. const char *plain = "Everyone gets Friday off.";
  209. byte *signature = (byte *)
  210. "\x05\xfa\x6a\x81\x2f\xc7\xdf\x8b\xf4\xf2\x54\x25\x09\xe0\x3e\x84"
  211. "\x6e\x11\xb9\xc6\x20\xbe\x20\x09\xef\xb4\x40\xef\xbc\xc6\x69\x21"
  212. "\x69\x94\xac\x04\xf3\x41\xb5\x7d\x05\x20\x2d\x42\x8f\xb2\xa2\x7b"
  213. "\x5c\x77\xdf\xd9\xb1\x5b\xfc\x3d\x55\x93\x53\x50\x34\x10\xc1\xe1";
  214. FileSource keys("TestData/rsa512a.dat", true, new HexDecoder);
  215. Weak::RSASSA_PKCS1v15_MD2_Signer rsaPriv(keys);
  216. Weak::RSASSA_PKCS1v15_MD2_Verifier rsaPub(rsaPriv);
  217. size_t signatureLength = rsaPriv.SignMessage(GlobalRNG(), (byte *)plain, strlen(plain), out);
  218. fail = memcmp(signature, out, 64) != 0;
  219. pass = pass && !fail;
  220. cout << (fail ? "FAILED " : "passed ");
  221. cout << "signature check against test vector\n";
  222. fail = !rsaPub.VerifyMessage((byte *)plain, strlen(plain), out, signatureLength);
  223. pass = pass && !fail;
  224. cout << (fail ? "FAILED " : "passed ");
  225. cout << "verification check against test vector\n";
  226. out[10]++;
  227. fail = rsaPub.VerifyMessage((byte *)plain, strlen(plain), out, signatureLength);
  228. pass = pass && !fail;
  229. cout << (fail ? "FAILED " : "passed ");
  230. cout << "invalid signature verification\n";
  231. }
  232. {
  233. FileSource keys("TestData/rsa1024.dat", true, new HexDecoder);
  234. RSAES_PKCS1v15_Decryptor rsaPriv(keys);
  235. RSAES_PKCS1v15_Encryptor rsaPub(rsaPriv);
  236. pass = CryptoSystemValidate(rsaPriv, rsaPub) && pass;
  237. }
  238. {
  239. RSAES<OAEP<SHA> >::Decryptor rsaPriv(GlobalRNG(), 512);
  240. RSAES<OAEP<SHA> >::Encryptor rsaPub(rsaPriv);
  241. pass = CryptoSystemValidate(rsaPriv, rsaPub) && pass;
  242. }
  243. {
  244. byte *plain = (byte *)
  245. "\x54\x85\x9b\x34\x2c\x49\xea\x2a";
  246. byte *encrypted = (byte *)
  247. "\x14\xbd\xdd\x28\xc9\x83\x35\x19\x23\x80\xe8\xe5\x49\xb1\x58\x2a"
  248. "\x8b\x40\xb4\x48\x6d\x03\xa6\xa5\x31\x1f\x1f\xd5\xf0\xa1\x80\xe4"
  249. "\x17\x53\x03\x29\xa9\x34\x90\x74\xb1\x52\x13\x54\x29\x08\x24\x52"
  250. "\x62\x51";
  251. byte *oaepSeed = (byte *)
  252. "\xaa\xfd\x12\xf6\x59\xca\xe6\x34\x89\xb4\x79\xe5\x07\x6d\xde\xc2"
  253. "\xf0\x6c\xb5\x8f";
  254. ByteQueue bq;
  255. bq.Put(oaepSeed, 20);
  256. FixedRNG rng(bq);
  257. FileSource privFile("TestData/rsa400pv.dat", true, new HexDecoder);
  258. FileSource pubFile("TestData/rsa400pb.dat", true, new HexDecoder);
  259. RSAES_OAEP_SHA_Decryptor rsaPriv;
  260. rsaPriv.AccessKey().BERDecodePrivateKey(privFile, false, 0);
  261. RSAES_OAEP_SHA_Encryptor rsaPub(pubFile);
  262. memset(out, 0, 50);
  263. memset(outPlain, 0, 8);
  264. rsaPub.Encrypt(rng, plain, 8, out);
  265. DecodingResult result = rsaPriv.FixedLengthDecrypt(GlobalRNG(), encrypted, outPlain);
  266. fail = !result.isValidCoding || (result.messageLength!=8) || memcmp(out, encrypted, 50) || memcmp(plain, outPlain, 8);
  267. pass = pass && !fail;
  268. cout << (fail ? "FAILED " : "passed ");
  269. cout << "PKCS 2.0 encryption and decryption\n";
  270. }
  271. return pass;
  272. }
  273. bool ValidateDH()
  274. {
  275. cout << "\nDH validation suite running...\n\n";
  276. FileSource f("TestData/dh1024.dat", true, new HexDecoder());
  277. DH dh(f);
  278. return SimpleKeyAgreementValidate(dh);
  279. }
  280. bool ValidateMQV()
  281. {
  282. cout << "\nMQV validation suite running...\n\n";
  283. FileSource f("TestData/mqv1024.dat", true, new HexDecoder());
  284. MQV mqv(f);
  285. return AuthenticatedKeyAgreementValidate(mqv);
  286. }
  287. bool ValidateLUC_DH()
  288. {
  289. cout << "\nLUC-DH validation suite running...\n\n";
  290. FileSource f("TestData/lucd512.dat", true, new HexDecoder());
  291. LUC_DH dh(f);
  292. return SimpleKeyAgreementValidate(dh);
  293. }
  294. bool ValidateXTR_DH()
  295. {
  296. cout << "\nXTR-DH validation suite running...\n\n";
  297. FileSource f("TestData/xtrdh171.dat", true, new HexDecoder());
  298. XTR_DH dh(f);
  299. return SimpleKeyAgreementValidate(dh);
  300. }
  301. bool ValidateElGamal()
  302. {
  303. cout << "\nElGamal validation suite running...\n\n";
  304. bool pass = true;
  305. {
  306. FileSource fc("TestData/elgc1024.dat", true, new HexDecoder);
  307. ElGamalDecryptor privC(fc);
  308. ElGamalEncryptor pubC(privC);
  309. privC.AccessKey().Precompute();
  310. ByteQueue queue;
  311. privC.AccessKey().SavePrecomputation(queue);
  312. privC.AccessKey().LoadPrecomputation(queue);
  313. pass = CryptoSystemValidate(privC, pubC) && pass;
  314. }
  315. return pass;
  316. }
  317. bool ValidateDLIES()
  318. {
  319. cout << "\nDLIES validation suite running...\n\n";
  320. bool pass = true;
  321. {
  322. FileSource fc("TestData/dlie1024.dat", true, new HexDecoder);
  323. DLIES<>::Decryptor privC(fc);
  324. DLIES<>::Encryptor pubC(privC);
  325. pass = CryptoSystemValidate(privC, pubC) && pass;
  326. }
  327. {
  328. cout << "Generating new encryption key..." << endl;
  329. DLIES<>::GroupParameters gp;
  330. gp.GenerateRandomWithKeySize(GlobalRNG(), 128);
  331. DLIES<>::Decryptor decryptor;
  332. decryptor.AccessKey().GenerateRandom(GlobalRNG(), gp);
  333. DLIES<>::Encryptor encryptor(decryptor);
  334. pass = CryptoSystemValidate(decryptor, encryptor) && pass;
  335. }
  336. return pass;
  337. }
  338. bool ValidateNR()
  339. {
  340. cout << "\nNR validation suite running...\n\n";
  341. bool pass = true;
  342. {
  343. FileSource f("TestData/nr2048.dat", true, new HexDecoder);
  344. NR<SHA>::Signer privS(f);
  345. privS.AccessKey().Precompute();
  346. NR<SHA>::Verifier pubS(privS);
  347. pass = SignatureValidate(privS, pubS) && pass;
  348. }
  349. {
  350. cout << "Generating new signature key..." << endl;
  351. NR<SHA>::Signer privS(GlobalRNG(), 256);
  352. NR<SHA>::Verifier pubS(privS);
  353. pass = SignatureValidate(privS, pubS) && pass;
  354. }
  355. return pass;
  356. }
  357. bool ValidateDSA(bool thorough)
  358. {
  359. cout << "\nDSA validation suite running...\n\n";
  360. bool pass = true, fail;
  361. {
  362. FileSource fs("TestData/dsa512.dat", true, new HexDecoder());
  363. GDSA<SHA>::Signer priv(fs);
  364. priv.AccessKey().Precompute(16);
  365. GDSA<SHA>::Verifier pub(priv);
  366. byte seed[]={0xd5, 0x01, 0x4e, 0x4b, 0x60, 0xef, 0x2b, 0xa8, 0xb6, 0x21,
  367. 0x1b, 0x40, 0x62, 0xba, 0x32, 0x24, 0xe0, 0x42, 0x7d, 0xd3};
  368. Integer k("358dad57 1462710f 50e254cf 1a376b2b deaadfbfh");
  369. Integer h("a9993e36 4706816a ba3e2571 7850c26c 9cd0d89dh");
  370. byte sig[]={0x8b, 0xac, 0x1a, 0xb6, 0x64, 0x10, 0x43, 0x5c, 0xb7, 0x18,
  371. 0x1f, 0x95, 0xb1, 0x6a, 0xb9, 0x7c, 0x92, 0xb3, 0x41, 0xc0,
  372. 0x41, 0xe2, 0x34, 0x5f, 0x1f, 0x56, 0xdf, 0x24, 0x58, 0xf4,
  373. 0x26, 0xd1, 0x55, 0xb4, 0xba, 0x2d, 0xb6, 0xdc, 0xd8, 0xc8};
  374. Integer r(sig, 20);
  375. Integer s(sig+20, 20);
  376. Integer pGen, qGen, rOut, sOut;
  377. int c;
  378. fail = !DSA::GeneratePrimes(seed, 160, c, pGen, 512, qGen);
  379. fail = fail || (pGen != pub.GetKey().GetGroupParameters().GetModulus()) || (qGen != pub.GetKey().GetGroupParameters().GetSubgroupOrder());
  380. pass = pass && !fail;
  381. cout << (fail ? "FAILED " : "passed ");
  382. cout << "prime generation test\n";
  383. priv.RawSign(k, h, rOut, sOut);
  384. fail = (rOut != r) || (sOut != s);
  385. pass = pass && !fail;
  386. cout << (fail ? "FAILED " : "passed ");
  387. cout << "signature check against test vector\n";
  388. fail = !pub.VerifyMessage((byte *)"abc", 3, sig, sizeof(sig));
  389. pass = pass && !fail;
  390. cout << (fail ? "FAILED " : "passed ");
  391. cout << "verification check against test vector\n";
  392. fail = pub.VerifyMessage((byte *)"xyz", 3, sig, sizeof(sig));
  393. pass = pass && !fail;
  394. }
  395. FileSource fs1("TestData/dsa1024.dat", true, new HexDecoder());
  396. DSA::Signer priv(fs1);
  397. DSA::Verifier pub(priv);
  398. FileSource fs2("TestData/dsa1024b.dat", true, new HexDecoder());
  399. DSA::Verifier pub1(fs2);
  400. assert(pub.GetKey() == pub1.GetKey());
  401. pass = SignatureValidate(priv, pub, thorough) && pass;
  402. return pass;
  403. }
  404. bool ValidateLUC()
  405. {
  406. cout << "\nLUC validation suite running...\n\n";
  407. bool pass=true;
  408. {
  409. FileSource f("TestData/luc1024.dat", true, new HexDecoder);
  410. LUCSSA_PKCS1v15_SHA_Signer priv(f);
  411. LUCSSA_PKCS1v15_SHA_Verifier pub(priv);
  412. pass = SignatureValidate(priv, pub) && pass;
  413. }
  414. {
  415. LUCES_OAEP_SHA_Decryptor priv(GlobalRNG(), 512);
  416. LUCES_OAEP_SHA_Encryptor pub(priv);
  417. pass = CryptoSystemValidate(priv, pub) && pass;
  418. }
  419. return pass;
  420. }
  421. bool ValidateLUC_DL()
  422. {
  423. cout << "\nLUC-HMP validation suite running...\n\n";
  424. FileSource f("TestData/lucs512.dat", true, new HexDecoder);
  425. LUC_HMP<SHA>::Signer privS(f);
  426. LUC_HMP<SHA>::Verifier pubS(privS);
  427. bool pass = SignatureValidate(privS, pubS);
  428. cout << "\nLUC-IES validation suite running...\n\n";
  429. FileSource fc("TestData/lucc512.dat", true, new HexDecoder);
  430. LUC_IES<>::Decryptor privC(fc);
  431. LUC_IES<>::Encryptor pubC(privC);
  432. pass = CryptoSystemValidate(privC, pubC) && pass;
  433. return pass;
  434. }
  435. bool ValidateRabin()
  436. {
  437. cout << "\nRabin validation suite running...\n\n";
  438. bool pass=true;
  439. {
  440. FileSource f("TestData/rabi1024.dat", true, new HexDecoder);
  441. RabinSS<PSSR, SHA>::Signer priv(f);
  442. RabinSS<PSSR, SHA>::Verifier pub(priv);
  443. pass = SignatureValidate(priv, pub) && pass;
  444. }
  445. {
  446. RabinES<OAEP<SHA> >::Decryptor priv(GlobalRNG(), 512);
  447. RabinES<OAEP<SHA> >::Encryptor pub(priv);
  448. pass = CryptoSystemValidate(priv, pub) && pass;
  449. }
  450. return pass;
  451. }
  452. bool ValidateRW()
  453. {
  454. cout << "\nRW validation suite running...\n\n";
  455. FileSource f("TestData/rw1024.dat", true, new HexDecoder);
  456. RWSS<PSSR, SHA>::Signer priv(f);
  457. RWSS<PSSR, SHA>::Verifier pub(priv);
  458. return SignatureValidate(priv, pub);
  459. }
  460. /*
  461. bool ValidateBlumGoldwasser()
  462. {
  463. cout << "\nBlumGoldwasser validation suite running...\n\n";
  464. FileSource f("TestData/blum512.dat", true, new HexDecoder);
  465. BlumGoldwasserPrivateKey priv(f);
  466. BlumGoldwasserPublicKey pub(priv);
  467. return CryptoSystemValidate(priv, pub);
  468. }
  469. */
  470. bool ValidateECP()
  471. {
  472. cout << "\nECP validation suite running...\n\n";
  473. ECIES<ECP>::Decryptor cpriv(GlobalRNG(), ASN1::secp192r1());
  474. ECIES<ECP>::Encryptor cpub(cpriv);
  475. ByteQueue bq;
  476. cpriv.GetKey().DEREncode(bq);
  477. cpub.AccessKey().AccessGroupParameters().SetEncodeAsOID(true);
  478. cpub.GetKey().DEREncode(bq);
  479. ECDSA<ECP, SHA>::Signer spriv(bq);
  480. ECDSA<ECP, SHA>::Verifier spub(bq);
  481. ECDH<ECP>::Domain ecdhc(ASN1::secp192r1());
  482. ECMQV<ECP>::Domain ecmqvc(ASN1::secp192r1());
  483. spriv.AccessKey().Precompute();
  484. ByteQueue queue;
  485. spriv.AccessKey().SavePrecomputation(queue);
  486. spriv.AccessKey().LoadPrecomputation(queue);
  487. bool pass = SignatureValidate(spriv, spub);
  488. cpub.AccessKey().Precompute();
  489. cpriv.AccessKey().Precompute();
  490. pass = CryptoSystemValidate(cpriv, cpub) && pass;
  491. pass = SimpleKeyAgreementValidate(ecdhc) && pass;
  492. pass = AuthenticatedKeyAgreementValidate(ecmqvc) && pass;
  493. cout << "Turning on point compression..." << endl;
  494. cpriv.AccessKey().AccessGroupParameters().SetPointCompression(true);
  495. cpub.AccessKey().AccessGroupParameters().SetPointCompression(true);
  496. ecdhc.AccessGroupParameters().SetPointCompression(true);
  497. ecmqvc.AccessGroupParameters().SetPointCompression(true);
  498. pass = CryptoSystemValidate(cpriv, cpub) && pass;
  499. pass = SimpleKeyAgreementValidate(ecdhc) && pass;
  500. pass = AuthenticatedKeyAgreementValidate(ecmqvc) && pass;
  501. cout << "Testing SEC 2, NIST, and Brainpool recommended curves..." << endl;
  502. OID oid;
  503. while (!(oid = DL_GroupParameters_EC<ECP>::GetNextRecommendedParametersOID(oid)).m_values.empty())
  504. {
  505. DL_GroupParameters_EC<ECP> params(oid);
  506. bool fail = !params.Validate(GlobalRNG(), 2);
  507. cout << (fail ? "FAILED" : "passed") << " " << dec << params.GetCurve().GetField().MaxElementBitLength() << " bits" << endl;
  508. pass = pass && !fail;
  509. }
  510. return pass;
  511. }
  512. bool ValidateEC2N()
  513. {
  514. cout << "\nEC2N validation suite running...\n\n";
  515. ECIES<EC2N>::Decryptor cpriv(GlobalRNG(), ASN1::sect193r1());
  516. ECIES<EC2N>::Encryptor cpub(cpriv);
  517. ByteQueue bq;
  518. cpriv.DEREncode(bq);
  519. cpub.AccessKey().AccessGroupParameters().SetEncodeAsOID(true);
  520. cpub.DEREncode(bq);
  521. ECDSA<EC2N, SHA>::Signer spriv(bq);
  522. ECDSA<EC2N, SHA>::Verifier spub(bq);
  523. ECDH<EC2N>::Domain ecdhc(ASN1::sect193r1());
  524. ECMQV<EC2N>::Domain ecmqvc(ASN1::sect193r1());
  525. spriv.AccessKey().Precompute();
  526. ByteQueue queue;
  527. spriv.AccessKey().SavePrecomputation(queue);
  528. spriv.AccessKey().LoadPrecomputation(queue);
  529. bool pass = SignatureValidate(spriv, spub);
  530. pass = CryptoSystemValidate(cpriv, cpub) && pass;
  531. pass = SimpleKeyAgreementValidate(ecdhc) && pass;
  532. pass = AuthenticatedKeyAgreementValidate(ecmqvc) && pass;
  533. cout << "Turning on point compression..." << endl;
  534. cpriv.AccessKey().AccessGroupParameters().SetPointCompression(true);
  535. cpub.AccessKey().AccessGroupParameters().SetPointCompression(true);
  536. ecdhc.AccessGroupParameters().SetPointCompression(true);
  537. ecmqvc.AccessGroupParameters().SetPointCompression(true);
  538. pass = CryptoSystemValidate(cpriv, cpub) && pass;
  539. pass = SimpleKeyAgreementValidate(ecdhc) && pass;
  540. pass = AuthenticatedKeyAgreementValidate(ecmqvc) && pass;
  541. #if 0 // TODO: turn this back on when I make EC2N faster for pentanomial basis
  542. cout << "Testing SEC 2 recommended curves..." << endl;
  543. OID oid;
  544. while (!(oid = DL_GroupParameters_EC<EC2N>::GetNextRecommendedParametersOID(oid)).m_values.empty())
  545. {
  546. DL_GroupParameters_EC<EC2N> params(oid);
  547. bool fail = !params.Validate(GlobalRNG(), 2);
  548. cout << (fail ? "FAILED" : "passed") << " " << params.GetCurve().GetField().MaxElementBitLength() << " bits" << endl;
  549. pass = pass && !fail;
  550. }
  551. #endif
  552. return pass;
  553. }
  554. bool ValidateECDSA()
  555. {
  556. cout << "\nECDSA validation suite running...\n\n";
  557. // from Sample Test Vectors for P1363
  558. GF2NT gf2n(191, 9, 0);
  559. byte a[]="\x28\x66\x53\x7B\x67\x67\x52\x63\x6A\x68\xF5\x65\x54\xE1\x26\x40\x27\x6B\x64\x9E\xF7\x52\x62\x67";
  560. byte b[]="\x2E\x45\xEF\x57\x1F\x00\x78\x6F\x67\xB0\x08\x1B\x94\x95\xA3\xD9\x54\x62\xF5\xDE\x0A\xA1\x85\xEC";
  561. EC2N ec(gf2n, PolynomialMod2(a,24), PolynomialMod2(b,24));
  562. EC2N::Point P;
  563. ec.DecodePoint(P, (byte *)"\x04\x36\xB3\xDA\xF8\xA2\x32\x06\xF9\xC4\xF2\x99\xD7\xB2\x1A\x9C\x36\x91\x37\xF2\xC8\x4A\xE1\xAA\x0D"
  564. "\x76\x5B\xE7\x34\x33\xB3\xF9\x5E\x33\x29\x32\xE7\x0E\xA2\x45\xCA\x24\x18\xEA\x0E\xF9\x80\x18\xFB", ec.EncodedPointSize());
  565. Integer n("40000000000000000000000004a20e90c39067c893bbb9a5H");
  566. Integer d("340562e1dda332f9d2aec168249b5696ee39d0ed4d03760fH");
  567. EC2N::Point Q(ec.Multiply(d, P));
  568. ECDSA<EC2N, SHA>::Signer priv(ec, P, n, d);
  569. ECDSA<EC2N, SHA>::Verifier pub(priv);
  570. Integer h("A9993E364706816ABA3E25717850C26C9CD0D89DH");
  571. Integer k("3eeace72b4919d991738d521879f787cb590aff8189d2b69H");
  572. byte sig[]="\x03\x8e\x5a\x11\xfb\x55\xe4\xc6\x54\x71\xdc\xd4\x99\x84\x52\xb1\xe0\x2d\x8a\xf7\x09\x9b\xb9\x30"
  573. "\x0c\x9a\x08\xc3\x44\x68\xc2\x44\xb4\xe5\xd6\xb2\x1b\x3c\x68\x36\x28\x07\x41\x60\x20\x32\x8b\x6e";
  574. Integer r(sig, 24);
  575. Integer s(sig+24, 24);
  576. Integer rOut, sOut;
  577. bool fail, pass=true;
  578. priv.RawSign(k, h, rOut, sOut);
  579. fail = (rOut != r) || (sOut != s);
  580. pass = pass && !fail;
  581. cout << (fail ? "FAILED " : "passed ");
  582. cout << "signature check against test vector\n";
  583. fail = !pub.VerifyMessage((byte *)"abc", 3, sig, sizeof(sig));
  584. pass = pass && !fail;
  585. cout << (fail ? "FAILED " : "passed ");
  586. cout << "verification check against test vector\n";
  587. fail = pub.VerifyMessage((byte *)"xyz", 3, sig, sizeof(sig));
  588. pass = pass && !fail;
  589. pass = SignatureValidate(priv, pub) && pass;
  590. return pass;
  591. }
  592. bool ValidateESIGN()
  593. {
  594. cout << "\nESIGN validation suite running...\n\n";
  595. bool pass = true, fail;
  596. const char *plain = "test";
  597. const byte *signature = (byte *)
  598. "\xA3\xE3\x20\x65\xDE\xDA\xE7\xEC\x05\xC1\xBF\xCD\x25\x79\x7D\x99\xCD\xD5\x73\x9D\x9D\xF3\xA4\xAA\x9A\xA4\x5A\xC8\x23\x3D\x0D\x37\xFE\xBC\x76\x3F\xF1\x84\xF6\x59"
  599. "\x14\x91\x4F\x0C\x34\x1B\xAE\x9A\x5C\x2E\x2E\x38\x08\x78\x77\xCB\xDC\x3C\x7E\xA0\x34\x44\x5B\x0F\x67\xD9\x35\x2A\x79\x47\x1A\x52\x37\x71\xDB\x12\x67\xC1\xB6\xC6"
  600. "\x66\x73\xB3\x40\x2E\xD6\xF2\x1A\x84\x0A\xB6\x7B\x0F\xEB\x8B\x88\xAB\x33\xDD\xE4\x83\x21\x90\x63\x2D\x51\x2A\xB1\x6F\xAB\xA7\x5C\xFD\x77\x99\xF2\xE1\xEF\x67\x1A"
  601. "\x74\x02\x37\x0E\xED\x0A\x06\xAD\xF4\x15\x65\xB8\xE1\xD1\x45\xAE\x39\x19\xB4\xFF\x5D\xF1\x45\x7B\xE0\xFE\x72\xED\x11\x92\x8F\x61\x41\x4F\x02\x00\xF2\x76\x6F\x7C"
  602. "\x79\xA2\xE5\x52\x20\x5D\x97\x5E\xFE\x39\xAE\x21\x10\xFB\x35\xF4\x80\x81\x41\x13\xDD\xE8\x5F\xCA\x1E\x4F\xF8\x9B\xB2\x68\xFB\x28";
  603. FileSource keys("TestData/esig1536.dat", true, new HexDecoder);
  604. ESIGN<SHA>::Signer signer(keys);
  605. ESIGN<SHA>::Verifier verifier(signer);
  606. fail = !SignatureValidate(signer, verifier);
  607. pass = pass && !fail;
  608. fail = !verifier.VerifyMessage((byte *)plain, strlen(plain), signature, verifier.SignatureLength());
  609. pass = pass && !fail;
  610. cout << (fail ? "FAILED " : "passed ");
  611. cout << "verification check against test vector\n";
  612. cout << "Generating signature key from seed..." << endl;
  613. signer.AccessKey().GenerateRandom(GlobalRNG(), MakeParameters("Seed", ConstByteArrayParameter((const byte *)"test", 4))("KeySize", 3*512));
  614. verifier = signer;
  615. fail = !SignatureValidate(signer, verifier);
  616. pass = pass && !fail;
  617. return pass;
  618. }