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.

1354 lines
37 KiB

  1. /*
  2. Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; version 2 of the License.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; see the file COPYING. If not, write to the
  12. Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  13. MA 02110-1301 USA.
  14. */
  15. // test.cpp
  16. // test taocrypt functionality
  17. #include <string.h>
  18. #include <stdio.h>
  19. #include "runtime.hpp"
  20. #include "sha.hpp"
  21. #include "md5.hpp"
  22. #include "md2.hpp"
  23. #include "md4.hpp"
  24. #include "ripemd.hpp"
  25. #include "hmac.hpp"
  26. #include "arc4.hpp"
  27. #include "des.hpp"
  28. #include "rsa.hpp"
  29. #include "dsa.hpp"
  30. #include "aes.hpp"
  31. #include "twofish.hpp"
  32. #include "blowfish.hpp"
  33. #include "asn.hpp"
  34. #include "dh.hpp"
  35. #include "coding.hpp"
  36. #include "random.hpp"
  37. #include "pwdbased.hpp"
  38. #include "rabbit.hpp"
  39. #include "hc128.hpp"
  40. using TaoCrypt::byte;
  41. using TaoCrypt::word32;
  42. using TaoCrypt::SHA;
  43. using TaoCrypt::SHA256;
  44. using TaoCrypt::SHA224;
  45. #ifdef WORD64_AVAILABLE
  46. using TaoCrypt::SHA512;
  47. using TaoCrypt::SHA384;
  48. #endif
  49. using TaoCrypt::MD5;
  50. using TaoCrypt::MD2;
  51. using TaoCrypt::MD4;
  52. using TaoCrypt::RIPEMD160;
  53. using TaoCrypt::HMAC;
  54. using TaoCrypt::ARC4;
  55. using TaoCrypt::DES_EDE3_CBC_Encryption;
  56. using TaoCrypt::DES_EDE3_CBC_Decryption;
  57. using TaoCrypt::DES_CBC_Encryption;
  58. using TaoCrypt::DES_CBC_Decryption;
  59. using TaoCrypt::DES_ECB_Encryption;
  60. using TaoCrypt::DES_ECB_Decryption;
  61. using TaoCrypt::AES_CBC_Encryption;
  62. using TaoCrypt::AES_CBC_Decryption;
  63. using TaoCrypt::AES_ECB_Encryption;
  64. using TaoCrypt::AES_ECB_Decryption;
  65. using TaoCrypt::Twofish_CBC_Encryption;
  66. using TaoCrypt::Twofish_CBC_Decryption;
  67. using TaoCrypt::Twofish_ECB_Encryption;
  68. using TaoCrypt::Twofish_ECB_Decryption;
  69. using TaoCrypt::Blowfish_CBC_Encryption;
  70. using TaoCrypt::Blowfish_CBC_Decryption;
  71. using TaoCrypt::Blowfish_ECB_Encryption;
  72. using TaoCrypt::Blowfish_ECB_Decryption;
  73. using TaoCrypt::RSA_PrivateKey;
  74. using TaoCrypt::RSA_PublicKey;
  75. using TaoCrypt::DSA_PrivateKey;
  76. using TaoCrypt::DSA_PublicKey;
  77. using TaoCrypt::DSA_Signer;
  78. using TaoCrypt::DSA_Verifier;
  79. using TaoCrypt::RSAES_Encryptor;
  80. using TaoCrypt::RSAES_Decryptor;
  81. using TaoCrypt::Source;
  82. using TaoCrypt::FileSource;
  83. using TaoCrypt::FileSource;
  84. using TaoCrypt::HexDecoder;
  85. using TaoCrypt::HexEncoder;
  86. using TaoCrypt::Base64Decoder;
  87. using TaoCrypt::Base64Encoder;
  88. using TaoCrypt::CertDecoder;
  89. using TaoCrypt::DH;
  90. using TaoCrypt::EncodeDSA_Signature;
  91. using TaoCrypt::DecodeDSA_Signature;
  92. using TaoCrypt::PBKDF2_HMAC;
  93. using TaoCrypt::tcArrayDelete;
  94. using TaoCrypt::GetCert;
  95. using TaoCrypt::GetPKCS_Cert;
  96. using TaoCrypt::Rabbit;
  97. using TaoCrypt::HC128;
  98. struct testVector {
  99. byte* input_;
  100. byte* output_;
  101. word32 inLen_;
  102. word32 outLen_;
  103. testVector(const char* in, const char* out) : input_((byte*)in),
  104. output_((byte*)out), inLen_((word32)strlen(in)),
  105. outLen_((word32)strlen(out)) {}
  106. };
  107. int sha_test();
  108. int sha256_test();
  109. #ifdef WORD64_AVAILABLE
  110. int sha512_test();
  111. int sha384_test();
  112. #endif
  113. int sha224_test();
  114. int md5_test();
  115. int md2_test();
  116. int md4_test();
  117. int ripemd_test();
  118. int hmac_test();
  119. int arc4_test();
  120. int des_test();
  121. int aes_test();
  122. int twofish_test();
  123. int blowfish_test();
  124. int rsa_test();
  125. int dsa_test();
  126. int dh_test();
  127. int pwdbased_test();
  128. int pkcs12_test();
  129. int rabbit_test();
  130. int hc128_test();
  131. TaoCrypt::RandomNumberGenerator rng;
  132. void err_sys(const char* msg, int es)
  133. {
  134. printf("%s\n", msg);
  135. exit(es);
  136. }
  137. // func_args from test.hpp, so don't have to pull in other junk
  138. struct func_args {
  139. int argc;
  140. char** argv;
  141. int return_code;
  142. };
  143. /*
  144. DES, AES, Blowfish, and Twofish need aligned (4 byte) input/output for
  145. processing, can turn this off by setting gpBlock(assumeAligned = false)
  146. but would hurt performance. yaSSL always uses dynamic memory so we have
  147. at least 8 byte alignment. This test tried to force alignment for stack
  148. variables (for convenience) but some compiler versions and optimizations
  149. seemed to be off. So we have msgTmp variable which we copy into dynamic
  150. memory at runtime to ensure proper alignment, along with plain/cipher.
  151. Whew!
  152. */
  153. const byte msgTmp[] = { // "now is the time for all " w/o trailing 0
  154. 0x6e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  155. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  156. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  157. };
  158. byte* msg = 0; // for block cipher input
  159. byte* plain = 0; // for cipher decrypt comparison
  160. byte* cipher = 0; // block output
  161. void taocrypt_test(void* args)
  162. {
  163. ((func_args*)args)->return_code = -1; // error state
  164. msg = NEW_TC byte[24];
  165. plain = NEW_TC byte[24];
  166. cipher = NEW_TC byte[24];
  167. memcpy(msg, msgTmp, 24);
  168. int ret = 0;
  169. if ( (ret = sha_test()) )
  170. err_sys("SHA test failed!\n", ret);
  171. else
  172. printf( "SHA test passed!\n");
  173. if ( (ret = sha256_test()) )
  174. err_sys("SHA-256 test failed!\n", ret);
  175. else
  176. printf( "SHA-256 test passed!\n");
  177. if ( (ret = sha224_test()) )
  178. err_sys("SHA-224 test failed!\n", ret);
  179. else
  180. printf( "SHA-224 test passed!\n");
  181. #ifdef WORD64_AVAILABLE
  182. if ( (ret = sha512_test()) )
  183. err_sys("SHA-512 test failed!\n", ret);
  184. else
  185. printf( "SHA-512 test passed!\n");
  186. if ( (ret = sha384_test()) )
  187. err_sys("SHA-384 test failed!\n", ret);
  188. else
  189. printf( "SHA-384 test passed!\n");
  190. #endif
  191. if ( (ret = md5_test()) )
  192. err_sys("MD5 test failed!\n", ret);
  193. else
  194. printf( "MD5 test passed!\n");
  195. if ( (ret = md2_test()) )
  196. err_sys("MD2 test failed!\n", ret);
  197. else
  198. printf( "MD2 test passed!\n");
  199. if ( (ret = md4_test()) )
  200. err_sys("MD4 test failed!\n", ret);
  201. else
  202. printf( "MD4 test passed!\n");
  203. if ( (ret = ripemd_test()) )
  204. err_sys("RIPEMD test failed!\n", ret);
  205. else
  206. printf( "RIPEMD test passed!\n");
  207. if ( ( ret = hmac_test()) )
  208. err_sys("HMAC test failed!\n", ret);
  209. else
  210. printf( "HMAC test passed!\n");
  211. if ( (ret = arc4_test()) )
  212. err_sys("ARC4 test failed!\n", ret);
  213. else
  214. printf( "ARC4 test passed!\n");
  215. if ( (ret = rabbit_test()) )
  216. err_sys("Rabbit test failed!\n", ret);
  217. else
  218. printf( "Rabbit test passed!\n");
  219. if ( (ret = hc128_test()) )
  220. err_sys("HC128 test failed!\n", ret);
  221. else
  222. printf( "HC128 test passed!\n");
  223. if ( (ret = des_test()) )
  224. err_sys("DES test failed!\n", ret);
  225. else
  226. printf( "DES test passed!\n");
  227. if ( (ret = aes_test()) )
  228. err_sys("AES test failed!\n", ret);
  229. else
  230. printf( "AES test passed!\n");
  231. if ( (ret = twofish_test()) )
  232. err_sys("Twofish test failed!\n", ret);
  233. else
  234. printf( "Twofish test passed!\n");
  235. if ( (ret = blowfish_test()) )
  236. err_sys("Blowfish test failed!\n", ret);
  237. else
  238. printf( "Blowfish test passed!\n");
  239. if ( (ret = rsa_test()) )
  240. err_sys("RSA test failed!\n", ret);
  241. else
  242. printf( "RSA test passed!\n");
  243. if ( (ret = dh_test()) )
  244. err_sys("DH test failed!\n", ret);
  245. else
  246. printf( "DH test passed!\n");
  247. if ( (ret = dsa_test()) )
  248. err_sys("DSA test failed!\n", ret);
  249. else
  250. printf( "DSA test passed!\n");
  251. if ( (ret = pwdbased_test()) )
  252. err_sys("PBKDF2 test failed!\n", ret);
  253. else
  254. printf( "PBKDF2 test passed!\n");
  255. /* not ready yet
  256. if ( (ret = pkcs12_test()) )
  257. err_sys("PKCS12 test failed!\n", ret);
  258. else
  259. printf( "PKCS12 test passed!\n");
  260. */
  261. tcArrayDelete(cipher);
  262. tcArrayDelete(plain);
  263. tcArrayDelete(msg);
  264. ((func_args*)args)->return_code = ret;
  265. }
  266. // so overall tests can pull in test function
  267. #ifndef NO_MAIN_DRIVER
  268. int main(int argc, char** argv)
  269. {
  270. func_args args;
  271. args.argc = argc;
  272. args.argv = argv;
  273. taocrypt_test(&args);
  274. TaoCrypt::CleanUp();
  275. return args.return_code;
  276. }
  277. #endif // NO_MAIN_DRIVER
  278. void file_test(const char* file, byte* check)
  279. {
  280. FILE* f;
  281. int i = 0;
  282. MD5 md5;
  283. byte buf[1024];
  284. byte md5sum[MD5::DIGEST_SIZE];
  285. if( !( f = fopen( file, "rb" ) )) {
  286. printf("Can't open %s\n", file);
  287. return;
  288. }
  289. while( ( i = (int)fread(buf, 1, sizeof(buf), f )) > 0 )
  290. md5.Update(buf, i);
  291. md5.Final(md5sum);
  292. memcpy(check, md5sum, sizeof(md5sum));
  293. for(int j = 0; j < MD5::DIGEST_SIZE; ++j )
  294. printf( "%02x", md5sum[j] );
  295. printf(" %s\n", file);
  296. fclose(f);
  297. }
  298. int sha_test()
  299. {
  300. SHA sha;
  301. byte hash[SHA::DIGEST_SIZE];
  302. testVector test_sha[] =
  303. {
  304. testVector("abc",
  305. "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  306. "\x6C\x9C\xD0\xD8\x9D"),
  307. testVector("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
  308. "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE\x4A\xA1\xF9\x51\x29"
  309. "\xE5\xE5\x46\x70\xF1"),
  310. testVector("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  311. "aaaaaa",
  312. "\x00\x98\xBA\x82\x4B\x5C\x16\x42\x7B\xD7\xA1\x12\x2A\x5A\x44"
  313. "\x2A\x25\xEC\x64\x4D"),
  314. testVector("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  315. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  316. "aaaaaaaaaa",
  317. "\xAD\x5B\x3F\xDB\xCB\x52\x67\x78\xC2\x83\x9D\x2F\x15\x1E\xA7"
  318. "\x53\x99\x5E\x26\xA0")
  319. };
  320. int times( sizeof(test_sha) / sizeof(testVector) );
  321. for (int i = 0; i < times; ++i) {
  322. sha.Update(test_sha[i].input_, test_sha[i].inLen_);
  323. sha.Final(hash);
  324. if (memcmp(hash, test_sha[i].output_, SHA::DIGEST_SIZE) != 0)
  325. return -1 - i;
  326. }
  327. return 0;
  328. }
  329. int sha256_test()
  330. {
  331. SHA256 sha;
  332. byte hash[SHA256::DIGEST_SIZE];
  333. testVector test_sha[] =
  334. {
  335. testVector("abc",
  336. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  337. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  338. "\x15\xAD"),
  339. testVector("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
  340. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  341. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  342. "\x06\xC1")
  343. };
  344. int times( sizeof(test_sha) / sizeof(testVector) );
  345. for (int i = 0; i < times; ++i) {
  346. sha.Update(test_sha[i].input_, test_sha[i].inLen_);
  347. sha.Final(hash);
  348. if (memcmp(hash, test_sha[i].output_, SHA256::DIGEST_SIZE) != 0)
  349. return -1 - i;
  350. }
  351. return 0;
  352. }
  353. #ifdef WORD64_AVAILABLE
  354. int sha512_test()
  355. {
  356. SHA512 sha;
  357. byte hash[SHA512::DIGEST_SIZE];
  358. testVector test_sha[] =
  359. {
  360. testVector("abc",
  361. "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  362. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  363. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  364. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  365. "\xa5\x4c\xa4\x9f"),
  366. testVector("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  367. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
  368. "\x8e\x95\x9b\x75\xda\xe3\x13\xda\x8c\xf4\xf7\x28\x14\xfc\x14"
  369. "\x3f\x8f\x77\x79\xc6\xeb\x9f\x7f\xa1\x72\x99\xae\xad\xb6\x88"
  370. "\x90\x18\x50\x1d\x28\x9e\x49\x00\xf7\xe4\x33\x1b\x99\xde\xc4"
  371. "\xb5\x43\x3a\xc7\xd3\x29\xee\xb6\xdd\x26\x54\x5e\x96\xe5\x5b"
  372. "\x87\x4b\xe9\x09")
  373. };
  374. int times( sizeof(test_sha) / sizeof(testVector) );
  375. for (int i = 0; i < times; ++i) {
  376. sha.Update(test_sha[i].input_, test_sha[i].inLen_);
  377. sha.Final(hash);
  378. if (memcmp(hash, test_sha[i].output_, SHA512::DIGEST_SIZE) != 0)
  379. return -1 - i;
  380. }
  381. return 0;
  382. }
  383. int sha384_test()
  384. {
  385. SHA384 sha;
  386. byte hash[SHA384::DIGEST_SIZE];
  387. testVector test_sha[] =
  388. {
  389. testVector("abc",
  390. "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  391. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  392. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  393. "\xc8\x25\xa7"),
  394. testVector("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhi"
  395. "jklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
  396. "\x09\x33\x0c\x33\xf7\x11\x47\xe8\x3d\x19\x2f\xc7\x82\xcd\x1b"
  397. "\x47\x53\x11\x1b\x17\x3b\x3b\x05\xd2\x2f\xa0\x80\x86\xe3\xb0"
  398. "\xf7\x12\xfc\xc7\xc7\x1a\x55\x7e\x2d\xb9\x66\xc3\xe9\xfa\x91"
  399. "\x74\x60\x39")
  400. };
  401. int times( sizeof(test_sha) / sizeof(testVector) );
  402. for (int i = 0; i < times; ++i) {
  403. sha.Update(test_sha[i].input_, test_sha[i].inLen_);
  404. sha.Final(hash);
  405. if (memcmp(hash, test_sha[i].output_, SHA384::DIGEST_SIZE) != 0)
  406. return -1 - i;
  407. }
  408. return 0;
  409. }
  410. #endif // WORD64_AVAILABLE
  411. int sha224_test()
  412. {
  413. SHA224 sha;
  414. byte hash[SHA224::DIGEST_SIZE];
  415. testVector test_sha[] =
  416. {
  417. testVector("abc",
  418. "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2\x55"
  419. "\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7"),
  420. testVector("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
  421. "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  422. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25")
  423. };
  424. int times( sizeof(test_sha) / sizeof(testVector) );
  425. for (int i = 0; i < times; ++i) {
  426. sha.Update(test_sha[i].input_, test_sha[i].inLen_);
  427. sha.Final(hash);
  428. if (memcmp(hash, test_sha[i].output_, SHA224::DIGEST_SIZE) != 0)
  429. return -1 - i;
  430. }
  431. return 0;
  432. }
  433. int md5_test()
  434. {
  435. MD5 md5;
  436. byte hash[MD5::DIGEST_SIZE];
  437. testVector test_md5[] =
  438. {
  439. testVector("abc",
  440. "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  441. "\x72"),
  442. testVector("message digest",
  443. "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61"
  444. "\xd0"),
  445. testVector("abcdefghijklmnopqrstuvwxyz",
  446. "\xc3\xfc\xd3\xd7\x61\x92\xe4\x00\x7d\xfb\x49\x6c\xca\x67\xe1"
  447. "\x3b"),
  448. testVector("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  449. "6789",
  450. "\xd1\x74\xab\x98\xd2\x77\xd9\xf5\xa5\x61\x1c\x2c\x9f\x41\x9d"
  451. "\x9f"),
  452. testVector("1234567890123456789012345678901234567890123456789012345678"
  453. "9012345678901234567890",
  454. "\x57\xed\xf4\xa2\x2b\xe3\xc9\x55\xac\x49\xda\x2e\x21\x07\xb6"
  455. "\x7a")
  456. };
  457. int times( sizeof(test_md5) / sizeof(testVector) );
  458. for (int i = 0; i < times; ++i) {
  459. md5.Update(test_md5[i].input_, test_md5[i].inLen_);
  460. md5.Final(hash);
  461. if (memcmp(hash, test_md5[i].output_, MD5::DIGEST_SIZE) != 0)
  462. return -5 - i;
  463. }
  464. return 0;
  465. }
  466. int md4_test()
  467. {
  468. MD4 md4;
  469. byte hash[MD4::DIGEST_SIZE];
  470. testVector test_md4[] =
  471. {
  472. testVector("",
  473. "\x31\xd6\xcf\xe0\xd1\x6a\xe9\x31\xb7\x3c\x59\xd7\xe0\xc0\x89"
  474. "\xc0"),
  475. testVector("a",
  476. "\xbd\xe5\x2c\xb3\x1d\xe3\x3e\x46\x24\x5e\x05\xfb\xdb\xd6\xfb"
  477. "\x24"),
  478. testVector("abc",
  479. "\xa4\x48\x01\x7a\xaf\x21\xd8\x52\x5f\xc1\x0a\xe8\x7a\xa6\x72"
  480. "\x9d"),
  481. testVector("message digest",
  482. "\xd9\x13\x0a\x81\x64\x54\x9f\xe8\x18\x87\x48\x06\xe1\xc7\x01"
  483. "\x4b"),
  484. testVector("abcdefghijklmnopqrstuvwxyz",
  485. "\xd7\x9e\x1c\x30\x8a\xa5\xbb\xcd\xee\xa8\xed\x63\xdf\x41\x2d"
  486. "\xa9"),
  487. testVector("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz012345"
  488. "6789",
  489. "\x04\x3f\x85\x82\xf2\x41\xdb\x35\x1c\xe6\x27\xe1\x53\xe7\xf0"
  490. "\xe4"),
  491. testVector("1234567890123456789012345678901234567890123456789012345678"
  492. "9012345678901234567890",
  493. "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f\xcc\x05"
  494. "\x36")
  495. };
  496. int times( sizeof(test_md4) / sizeof(testVector) );
  497. for (int i = 0; i < times; ++i) {
  498. md4.Update(test_md4[i].input_, test_md4[i].inLen_);
  499. md4.Final(hash);
  500. if (memcmp(hash, test_md4[i].output_, MD4::DIGEST_SIZE) != 0)
  501. return -5 - i;
  502. }
  503. return 0;
  504. }
  505. int md2_test()
  506. {
  507. MD2 md5;
  508. byte hash[MD2::DIGEST_SIZE];
  509. testVector test_md2[] =
  510. {
  511. testVector("",
  512. "\x83\x50\xe5\xa3\xe2\x4c\x15\x3d\xf2\x27\x5c\x9f\x80\x69"
  513. "\x27\x73"),
  514. testVector("a",
  515. "\x32\xec\x01\xec\x4a\x6d\xac\x72\xc0\xab\x96\xfb\x34\xc0"
  516. "\xb5\xd1"),
  517. testVector("abc",
  518. "\xda\x85\x3b\x0d\x3f\x88\xd9\x9b\x30\x28\x3a\x69\xe6\xde"
  519. "\xd6\xbb"),
  520. testVector("message digest",
  521. "\xab\x4f\x49\x6b\xfb\x2a\x53\x0b\x21\x9f\xf3\x30\x31\xfe"
  522. "\x06\xb0"),
  523. testVector("abcdefghijklmnopqrstuvwxyz",
  524. "\x4e\x8d\xdf\xf3\x65\x02\x92\xab\x5a\x41\x08\xc3\xaa\x47"
  525. "\x94\x0b"),
  526. testVector("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
  527. "0123456789",
  528. "\xda\x33\xde\xf2\xa4\x2d\xf1\x39\x75\x35\x28\x46\xc3\x03"
  529. "\x38\xcd"),
  530. testVector("12345678901234567890123456789012345678901234567890123456"
  531. "789012345678901234567890",
  532. "\xd5\x97\x6f\x79\xd8\x3d\x3a\x0d\xc9\x80\x6c\x3c\x66\xf3"
  533. "\xef\xd8")
  534. };
  535. int times( sizeof(test_md2) / sizeof(testVector) );
  536. for (int i = 0; i < times; ++i) {
  537. md5.Update(test_md2[i].input_, test_md2[i].inLen_);
  538. md5.Final(hash);
  539. if (memcmp(hash, test_md2[i].output_, MD2::DIGEST_SIZE) != 0)
  540. return -10 - i;
  541. }
  542. return 0;
  543. }
  544. int ripemd_test()
  545. {
  546. RIPEMD160 ripe160;
  547. byte hash[RIPEMD160::DIGEST_SIZE];
  548. testVector test_ripemd[] =
  549. {
  550. testVector("",
  551. "\x9c\x11\x85\xa5\xc5\xe9\xfc\x54\x61\x28\x08\x97\x7e\xe8"
  552. "\xf5\x48\xb2\x25\x8d\x31"),
  553. testVector("a",
  554. "\x0b\xdc\x9d\x2d\x25\x6b\x3e\xe9\xda\xae\x34\x7b\xe6\xf4"
  555. "\xdc\x83\x5a\x46\x7f\xfe"),
  556. testVector("abc",
  557. "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  558. "\xb0\x87\xf1\x5a\x0b\xfc"),
  559. testVector("message digest",
  560. "\x5d\x06\x89\xef\x49\xd2\xfa\xe5\x72\xb8\x81\xb1\x23\xa8"
  561. "\x5f\xfa\x21\x59\x5f\x36"),
  562. testVector("abcdefghijklmnopqrstuvwxyz",
  563. "\xf7\x1c\x27\x10\x9c\x69\x2c\x1b\x56\xbb\xdc\xeb\x5b\x9d"
  564. "\x28\x65\xb3\x70\x8d\xbc"),
  565. testVector("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
  566. "\x12\xa0\x53\x38\x4a\x9c\x0c\x88\xe4\x05\xa0\x6c\x27\xdc"
  567. "\xf4\x9a\xda\x62\xeb\x2b"),
  568. testVector("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123"
  569. "456789",
  570. "\xb0\xe2\x0b\x6e\x31\x16\x64\x02\x86\xed\x3a\x87\xa5\x71"
  571. "\x30\x79\xb2\x1f\x51\x89"),
  572. testVector("12345678901234567890123456789012345678901234567890123456"
  573. "789012345678901234567890",
  574. "\x9b\x75\x2e\x45\x57\x3d\x4b\x39\xf4\xdb\xd3\x32\x3c\xab"
  575. "\x82\xbf\x63\x32\x6b\xfb"),
  576. };
  577. int times( sizeof(test_ripemd) / sizeof(testVector) );
  578. for (int i = 0; i < times; ++i) {
  579. ripe160.Update(test_ripemd[i].input_, test_ripemd[i].inLen_);
  580. ripe160.Final(hash);
  581. if (memcmp(hash, test_ripemd[i].output_, RIPEMD160::DIGEST_SIZE) != 0)
  582. return -100 - i;
  583. }
  584. return 0;
  585. }
  586. int hmac_test()
  587. {
  588. HMAC<MD5> hmacMD5;
  589. byte hash[MD5::DIGEST_SIZE];
  590. const char* keys[]=
  591. {
  592. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  593. "Jefe",
  594. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  595. };
  596. testVector test_hmacMD5[] =
  597. {
  598. testVector("Hi There",
  599. "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  600. "\x9d"),
  601. testVector("what do ya want for nothing?",
  602. "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7"
  603. "\x38"),
  604. testVector("\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  605. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  606. "\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD\xDD"
  607. "\xDD\xDD\xDD\xDD\xDD\xDD",
  608. "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3"
  609. "\xf6")
  610. };
  611. int times( sizeof(test_hmacMD5) / sizeof(testVector) );
  612. for (int i = 0; i < times; ++i) {
  613. hmacMD5.SetKey((byte*)keys[i], (word32)strlen(keys[i]));
  614. hmacMD5.Update(test_hmacMD5[i].input_, test_hmacMD5[i].inLen_);
  615. hmacMD5.Final(hash);
  616. if (memcmp(hash, test_hmacMD5[i].output_, MD5::DIGEST_SIZE) != 0)
  617. return -20 - i;
  618. }
  619. return 0;
  620. }
  621. int arc4_test()
  622. {
  623. byte cipher[16];
  624. byte plain[16];
  625. const char* keys[] =
  626. {
  627. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  628. "\x01\x23\x45\x67\x89\xab\xcd\xef",
  629. "\x00\x00\x00\x00\x00\x00\x00\x00",
  630. "\xef\x01\x23\x45"
  631. };
  632. testVector test_arc4[] =
  633. {
  634. testVector("\x01\x23\x45\x67\x89\xab\xcd\xef",
  635. "\x75\xb7\x87\x80\x99\xe0\xc5\x96"),
  636. testVector("\x00\x00\x00\x00\x00\x00\x00\x00",
  637. "\x74\x94\xc2\xe7\x10\x4b\x08\x79"),
  638. testVector("\x00\x00\x00\x00\x00\x00\x00\x00",
  639. "\xde\x18\x89\x41\xa3\x37\x5d\x3a"),
  640. testVector("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  641. "\xd6\xa1\x41\xa7\xec\x3c\x38\xdf\xbd\x61")
  642. };
  643. int times( sizeof(test_arc4) / sizeof(testVector) );
  644. for (int i = 0; i < times; ++i) {
  645. ARC4::Encryption enc;
  646. ARC4::Decryption dec;
  647. enc.SetKey((byte*)keys[i], (word32)strlen(keys[i]));
  648. dec.SetKey((byte*)keys[i], (word32)strlen(keys[i]));
  649. enc.Process(cipher, test_arc4[i].input_, test_arc4[i].outLen_);
  650. dec.Process(plain, cipher, test_arc4[i].outLen_);
  651. if (memcmp(plain, test_arc4[i].input_, test_arc4[i].outLen_))
  652. return -30 - i;
  653. if (memcmp(cipher, test_arc4[i].output_, test_arc4[i].outLen_))
  654. return -40 - i;
  655. }
  656. return 0;
  657. }
  658. int rabbit_test()
  659. {
  660. byte cipher[16];
  661. byte plain[16];
  662. const char* keys[] =
  663. {
  664. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  665. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  666. "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B\xFE\x36\x3D\x2E\x29\x13\x28\x91"
  667. };
  668. const char* ivs[] =
  669. {
  670. "\x00\x00\x00\x00\x00\x00\x00\x00",
  671. "\x59\x7E\x26\xC1\x75\xF5\x73\xC3",
  672. 0
  673. };
  674. testVector test_rabbit[] =
  675. {
  676. testVector("\x00\x00\x00\x00\x00\x00\x00\x00",
  677. "\xED\xB7\x05\x67\x37\x5D\xCD\x7C"),
  678. testVector("\x00\x00\x00\x00\x00\x00\x00\x00",
  679. "\x6D\x7D\x01\x22\x92\xCC\xDC\xE0"),
  680. testVector("\x00\x00\x00\x00\x00\x00\x00\x00",
  681. "\x9C\x51\xE2\x87\x84\xC3\x7F\xE9")
  682. };
  683. int times( sizeof(test_rabbit) / sizeof(testVector) );
  684. for (int i = 0; i < times; ++i) {
  685. Rabbit::Encryption enc;
  686. Rabbit::Decryption dec;
  687. enc.SetKey((byte*)keys[i], (byte*)ivs[i]);
  688. dec.SetKey((byte*)keys[i], (byte*)ivs[i]);
  689. enc.Process(cipher, test_rabbit[i].input_, test_rabbit[i].outLen_);
  690. dec.Process(plain, cipher, test_rabbit[i].outLen_);
  691. if (memcmp(plain, test_rabbit[i].input_, test_rabbit[i].outLen_))
  692. return -230 - i;
  693. if (memcmp(cipher, test_rabbit[i].output_, test_rabbit[i].outLen_))
  694. return -240 - i;
  695. }
  696. return 0;
  697. }
  698. int hc128_test()
  699. {
  700. byte cipher[16];
  701. byte plain[16];
  702. const char* keys[] =
  703. {
  704. "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  705. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  706. "\x00\x53\xA6\xF9\x4C\x9F\xF2\x45\x98\xEB\x3E\x91\xE4\x37\x8A\xDD",
  707. "\x0F\x62\xB5\x08\x5B\xAE\x01\x54\xA7\xFA\x4D\xA0\xF3\x46\x99\xEC"
  708. };
  709. const char* ivs[] =
  710. {
  711. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  712. "\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  713. "\x0D\x74\xDB\x42\xA9\x10\x77\xDE\x45\xAC\x13\x7A\xE1\x48\xAF\x16",
  714. "\x28\x8F\xF6\x5D\xC4\x2B\x92\xF9\x60\xC7\x2E\x95\xFC\x63\xCA\x31"
  715. };
  716. testVector test_hc128[] =
  717. {
  718. testVector("\x00\x00\x00\x00\x00\x00\x00\x00",
  719. "\x37\x86\x02\xB9\x8F\x32\xA7\x48"),
  720. testVector("\x00\x00\x00\x00\x00\x00\x00\x00",
  721. "\x33\x7F\x86\x11\xC6\xED\x61\x5F"),
  722. testVector("\x00\x00\x00\x00\x00\x00\x00\x00",
  723. "\x2E\x1E\xD1\x2A\x85\x51\xC0\x5A"),
  724. testVector("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
  725. "\x1C\xD8\xAE\xDD\xFE\x52\xE2\x17\xE8\x35\xD0\xB7\xE8\x4E\x29")
  726. };
  727. int times( sizeof(test_hc128) / sizeof(testVector) );
  728. for (int i = 0; i < times; ++i) {
  729. HC128::Encryption enc;
  730. HC128::Decryption dec;
  731. enc.SetKey((byte*)keys[i], (byte*)ivs[i]);
  732. dec.SetKey((byte*)keys[i], (byte*)ivs[i]);
  733. enc.Process(cipher, test_hc128[i].input_, test_hc128[i].outLen_);
  734. dec.Process(plain, cipher, test_hc128[i].outLen_);
  735. if (memcmp(plain, test_hc128[i].input_, test_hc128[i].outLen_))
  736. return -330 - i;
  737. if (memcmp(cipher, test_hc128[i].output_, test_hc128[i].outLen_))
  738. return -340 - i;
  739. }
  740. return 0;
  741. }
  742. int des_test()
  743. {
  744. //ECB mode
  745. DES_ECB_Encryption enc;
  746. DES_ECB_Decryption dec;
  747. const int sz = TaoCrypt::DES_BLOCK_SIZE * 3;
  748. const byte key[] = { 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef };
  749. const byte iv[] = { 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef };
  750. enc.SetKey(key, sizeof(key));
  751. enc.Process(cipher, msg, sz);
  752. dec.SetKey(key, sizeof(key));
  753. dec.Process(plain, cipher, sz);
  754. if (memcmp(plain, msg, sz))
  755. return -50;
  756. const byte verify1[] =
  757. {
  758. 0xf9,0x99,0xb8,0x8e,0xaf,0xea,0x71,0x53,
  759. 0x6a,0x27,0x17,0x87,0xab,0x88,0x83,0xf9,
  760. 0x89,0x3d,0x51,0xec,0x4b,0x56,0x3b,0x53
  761. };
  762. if (memcmp(cipher, verify1, sz))
  763. return -51;
  764. // CBC mode
  765. DES_CBC_Encryption enc2;
  766. DES_CBC_Decryption dec2;
  767. enc2.SetKey(key, sizeof(key), iv);
  768. enc2.Process(cipher, msg, sz);
  769. dec2.SetKey(key, sizeof(key), iv);
  770. dec2.Process(plain, cipher, sz);
  771. if (memcmp(plain, msg, sz))
  772. return -52;
  773. const byte verify2[] =
  774. {
  775. 0x8b,0x7c,0x52,0xb0,0x01,0x2b,0x6c,0xb8,
  776. 0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
  777. 0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
  778. };
  779. if (memcmp(cipher, verify2, sz))
  780. return -53;
  781. // EDE3 CBC mode
  782. DES_EDE3_CBC_Encryption enc3;
  783. DES_EDE3_CBC_Decryption dec3;
  784. const byte key3[] =
  785. {
  786. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  787. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  788. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  789. };
  790. const byte iv3[] =
  791. {
  792. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  793. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  794. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  795. };
  796. enc3.SetKey(key3, sizeof(key3), iv3);
  797. enc3.Process(cipher, msg, sz);
  798. dec3.SetKey(key3, sizeof(key3), iv3);
  799. dec3.Process(plain, cipher, sz);
  800. if (memcmp(plain, msg, sz))
  801. return -54;
  802. const byte verify3[] =
  803. {
  804. 0x08,0x8a,0xae,0xe6,0x9a,0xa9,0xc1,0x13,
  805. 0x93,0x7d,0xf7,0x3a,0x11,0x56,0x66,0xb3,
  806. 0x18,0xbc,0xbb,0x6d,0xd2,0xb1,0x16,0xda
  807. };
  808. if (memcmp(cipher, verify3, sz))
  809. return -55;
  810. return 0;
  811. }
  812. int aes_test()
  813. {
  814. AES_CBC_Encryption enc;
  815. AES_CBC_Decryption dec;
  816. const int bs(TaoCrypt::AES::BLOCK_SIZE);
  817. byte key[] = "0123456789abcdef "; // align
  818. byte iv[] = "1234567890abcdef "; // align
  819. enc.SetKey(key, bs, iv);
  820. dec.SetKey(key, bs, iv);
  821. enc.Process(cipher, msg, bs);
  822. dec.Process(plain, cipher, bs);
  823. if (memcmp(plain, msg, bs))
  824. return -60;
  825. const byte verify[] =
  826. {
  827. 0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
  828. 0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
  829. };
  830. if (memcmp(cipher, verify, bs))
  831. return -61;
  832. AES_ECB_Encryption enc2;
  833. AES_ECB_Decryption dec2;
  834. enc2.SetKey(key, bs, iv);
  835. dec2.SetKey(key, bs, iv);
  836. enc2.Process(cipher, msg, bs);
  837. dec2.Process(plain, cipher, bs);
  838. if (memcmp(plain, msg, bs))
  839. return -62;
  840. const byte verify2[] =
  841. {
  842. 0xd0,0xc9,0xd9,0xc9,0x40,0xe8,0x97,0xb6,
  843. 0xc8,0x8c,0x33,0x3b,0xb5,0x8f,0x85,0xd1
  844. };
  845. if (memcmp(cipher, verify2, bs))
  846. return -63;
  847. return 0;
  848. }
  849. int twofish_test()
  850. {
  851. Twofish_CBC_Encryption enc;
  852. Twofish_CBC_Decryption dec;
  853. const int bs(TaoCrypt::Twofish::BLOCK_SIZE);
  854. byte key[] = "0123456789abcdef "; // align
  855. byte iv[] = "1234567890abcdef "; // align
  856. enc.SetKey(key, bs, iv);
  857. dec.SetKey(key, bs, iv);
  858. enc.Process(cipher, msg, bs);
  859. dec.Process(plain, cipher, bs);
  860. if (memcmp(plain, msg, bs))
  861. return -60;
  862. const byte verify[] =
  863. {
  864. 0xD2,0xD7,0x47,0x47,0x4A,0x65,0x4E,0x16,
  865. 0x21,0x03,0x58,0x79,0x5F,0x02,0x27,0x2C
  866. };
  867. if (memcmp(cipher, verify, bs))
  868. return -61;
  869. Twofish_ECB_Encryption enc2;
  870. Twofish_ECB_Decryption dec2;
  871. enc2.SetKey(key, bs, iv);
  872. dec2.SetKey(key, bs, iv);
  873. enc2.Process(cipher, msg, bs);
  874. dec2.Process(plain, cipher, bs);
  875. if (memcmp(plain, msg, bs))
  876. return -62;
  877. const byte verify2[] =
  878. {
  879. 0x3B,0x6C,0x63,0x10,0x34,0xAB,0xB2,0x87,
  880. 0xC4,0xCD,0x6B,0x91,0x14,0xC5,0x3A,0x09
  881. };
  882. if (memcmp(cipher, verify2, bs))
  883. return -63;
  884. return 0;
  885. }
  886. int blowfish_test()
  887. {
  888. Blowfish_CBC_Encryption enc;
  889. Blowfish_CBC_Decryption dec;
  890. const int bs(TaoCrypt::Blowfish::BLOCK_SIZE);
  891. byte key[] = "0123456789abcdef "; // align
  892. byte iv[] = "1234567890abcdef "; // align
  893. enc.SetKey(key, 16, iv);
  894. dec.SetKey(key, 16, iv);
  895. enc.Process(cipher, msg, bs * 2);
  896. dec.Process(plain, cipher, bs * 2);
  897. if (memcmp(plain, msg, bs))
  898. return -60;
  899. const byte verify[] =
  900. {
  901. 0x0E,0x26,0xAA,0x29,0x11,0x25,0xAB,0xB5,
  902. 0xBC,0xD9,0x08,0xC4,0x94,0x6C,0x89,0xA3
  903. };
  904. if (memcmp(cipher, verify, bs))
  905. return -61;
  906. Blowfish_ECB_Encryption enc2;
  907. Blowfish_ECB_Decryption dec2;
  908. enc2.SetKey(key, 16, iv);
  909. dec2.SetKey(key, 16, iv);
  910. enc2.Process(cipher, msg, bs * 2);
  911. dec2.Process(plain, cipher, bs * 2);
  912. if (memcmp(plain, msg, bs))
  913. return -62;
  914. const byte verify2[] =
  915. {
  916. 0xE7,0x42,0xB9,0x37,0xC8,0x7D,0x93,0xCA,
  917. 0x8F,0xCE,0x39,0x32,0xDE,0xD7,0xBC,0x5B
  918. };
  919. if (memcmp(cipher, verify2, bs))
  920. return -63;
  921. return 0;
  922. }
  923. int rsa_test()
  924. {
  925. Source source;
  926. FileSource("../certs/client-key.der", source);
  927. if (source.size() == 0) {
  928. FileSource("../../certs/client-key.der", source); // for testsuite
  929. if (source.size() == 0) {
  930. FileSource("../../../certs/client-key.der", source); // Debug dir
  931. if (source.size() == 0)
  932. err_sys("where's your certs dir?", -79);
  933. }
  934. }
  935. RSA_PrivateKey priv(source);
  936. RSAES_Encryptor enc(priv);
  937. byte message[] = "Everyone gets Friday off.";
  938. const word32 len = (word32)strlen((char*)message);
  939. byte cipher[512];
  940. enc.Encrypt(message, len, cipher, rng);
  941. RSAES_Decryptor dec(priv);
  942. byte plain[512];
  943. dec.Decrypt(cipher, priv.FixedCiphertextLength(), plain, rng);
  944. if (memcmp(plain, message, len))
  945. return -70;
  946. dec.SSL_Sign(message, len, cipher, rng);
  947. if (!enc.SSL_Verify(message, len, cipher))
  948. return -71;
  949. // test decode
  950. Source source2;
  951. FileSource("../certs/client-cert.der", source2);
  952. if (source2.size() == 0) {
  953. FileSource("../../certs/client-cert.der", source2); // for testsuite
  954. if (source2.size() == 0) {
  955. FileSource("../../../certs/client-cert.der", source2); // Debug dir
  956. if (source2.size() == 0)
  957. err_sys("where's your certs dir?", -79);
  958. }
  959. }
  960. CertDecoder cd(source2, true, 0, false, CertDecoder::CA);
  961. if (cd.GetError().What())
  962. err_sys("cert error", -80);
  963. Source source3(cd.GetPublicKey().GetKey(), cd.GetPublicKey().size());
  964. RSA_PublicKey pub(source3);
  965. return 0;
  966. }
  967. int dh_test()
  968. {
  969. Source source;
  970. FileSource("../certs/dh1024.dat", source);
  971. if (source.size() == 0) {
  972. FileSource("../../certs/dh1024.dat", source); // for testsuite
  973. if (source.size() == 0) {
  974. FileSource("../../../certs/dh1024.dat", source); // win32 Debug dir
  975. if (source.size() == 0)
  976. err_sys("where's your certs dir?", -79);
  977. }
  978. }
  979. HexDecoder hDec(source);
  980. DH dh(source);
  981. byte pub[128];
  982. byte priv[128];
  983. byte agree[128];
  984. byte pub2[128];
  985. byte priv2[128];
  986. byte agree2[128];
  987. DH dh2(dh);
  988. dh.GenerateKeyPair(rng, priv, pub);
  989. dh2.GenerateKeyPair(rng, priv2, pub2);
  990. dh.Agree(agree, priv, pub2);
  991. dh2.Agree(agree2, priv2, pub);
  992. if ( memcmp(agree, agree2, dh.GetByteLength()) )
  993. return -80;
  994. return 0;
  995. }
  996. int dsa_test()
  997. {
  998. Source source;
  999. FileSource("../certs/dsa1024.der", source);
  1000. if (source.size() == 0) {
  1001. FileSource("../../certs/dsa1024.der", source); // for testsuite
  1002. if (source.size() == 0) {
  1003. FileSource("../../../certs/dsa1024.der", source); // win32 Debug dir
  1004. if (source.size() == 0)
  1005. err_sys("where's your certs dir?", -89);
  1006. }
  1007. }
  1008. const char msg[] = "this is the message";
  1009. byte signature[40];
  1010. DSA_PrivateKey priv(source);
  1011. DSA_Signer signer(priv);
  1012. SHA sha;
  1013. byte digest[SHA::DIGEST_SIZE];
  1014. sha.Update((byte*)msg, sizeof(msg));
  1015. sha.Final(digest);
  1016. signer.Sign(digest, signature, rng);
  1017. byte encoded[sizeof(signature) + 6];
  1018. byte decoded[40];
  1019. word32 encSz = EncodeDSA_Signature(signer.GetR(), signer.GetS(), encoded);
  1020. DecodeDSA_Signature(decoded, encoded, encSz);
  1021. DSA_PublicKey pub(priv);
  1022. DSA_Verifier verifier(pub);
  1023. if (!verifier.Verify(digest, decoded))
  1024. return -90;
  1025. if (!verifier.Verify(digest, signature))
  1026. return -91;
  1027. return 0;
  1028. }
  1029. int pwdbased_test()
  1030. {
  1031. PBKDF2_HMAC<SHA> pb;
  1032. byte derived[32];
  1033. const byte pwd1[] = "password "; // align
  1034. const byte salt[] = { 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12 };
  1035. pb.DeriveKey(derived, 8, pwd1, 8, salt, sizeof(salt), 5);
  1036. const byte verify1[] = { 0xD1, 0xDA, 0xA7, 0x86, 0x15, 0xF2, 0x87, 0xE6 };
  1037. if ( memcmp(derived, verify1, sizeof(verify1)) )
  1038. return -101;
  1039. const byte pwd2[] = "All n-entities must communicate with other n-entities"
  1040. " via n-1 entiteeheehees "; // align
  1041. pb.DeriveKey(derived, 24, pwd2, 76, salt, sizeof(salt), 500);
  1042. const byte verify2[] = { 0x6A, 0x89, 0x70, 0xBF, 0x68, 0xC9, 0x2C, 0xAE,
  1043. 0xA8, 0x4A, 0x8D, 0xF2, 0x85, 0x10, 0x85, 0x86,
  1044. 0x07, 0x12, 0x63, 0x80, 0xCC, 0x47, 0xAB, 0x2D
  1045. };
  1046. if ( memcmp(derived, verify2, sizeof(verify2)) )
  1047. return -102;
  1048. return 0;
  1049. }
  1050. /*
  1051. int pkcs12_test()
  1052. {
  1053. Source cert;
  1054. FileSource("../certs/server-cert.pem", cert);
  1055. if (cert.size() == 0) {
  1056. FileSource("../../certs/server-cert.pem", cert); // for testsuite
  1057. if (cert.size() == 0) {
  1058. FileSource("../../../certs/server-cert.pem", cert); // Debug dir
  1059. if (cert.size() == 0)
  1060. err_sys("where's your certs dir?", -109);
  1061. }
  1062. }
  1063. if (GetCert(cert) != 0)
  1064. return -110;
  1065. Source source;
  1066. FileSource("../certs/server.p12", source);
  1067. if (source.size() == 0) {
  1068. FileSource("../../certs/server.p12", source); // for testsuite
  1069. if (source.size() == 0) {
  1070. FileSource("../../../certs/server.p12", source); // Debug dir
  1071. if (source.size() == 0)
  1072. err_sys("where's your certs dir?", -111);
  1073. }
  1074. }
  1075. if (GetPKCS_Cert("password", source) != 0)
  1076. return -112;
  1077. return 0;
  1078. }
  1079. */