test_crypto.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2012, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #define CRYPTO_PRIVATE
  7. #include "or.h"
  8. #include "test.h"
  9. #include "aes.h"
  10. /** Run unit tests for Diffie-Hellman functionality. */
  11. static void
  12. test_crypto_dh(void)
  13. {
  14. crypto_dh_t *dh1 = crypto_dh_new(DH_TYPE_CIRCUIT);
  15. crypto_dh_t *dh2 = crypto_dh_new(DH_TYPE_CIRCUIT);
  16. char p1[DH_BYTES];
  17. char p2[DH_BYTES];
  18. char s1[DH_BYTES];
  19. char s2[DH_BYTES];
  20. ssize_t s1len, s2len;
  21. test_eq(crypto_dh_get_bytes(dh1), DH_BYTES);
  22. test_eq(crypto_dh_get_bytes(dh2), DH_BYTES);
  23. memset(p1, 0, DH_BYTES);
  24. memset(p2, 0, DH_BYTES);
  25. test_memeq(p1, p2, DH_BYTES);
  26. test_assert(! crypto_dh_get_public(dh1, p1, DH_BYTES));
  27. test_memneq(p1, p2, DH_BYTES);
  28. test_assert(! crypto_dh_get_public(dh2, p2, DH_BYTES));
  29. test_memneq(p1, p2, DH_BYTES);
  30. memset(s1, 0, DH_BYTES);
  31. memset(s2, 0xFF, DH_BYTES);
  32. s1len = crypto_dh_compute_secret(LOG_WARN, dh1, p2, DH_BYTES, s1, 50);
  33. s2len = crypto_dh_compute_secret(LOG_WARN, dh2, p1, DH_BYTES, s2, 50);
  34. test_assert(s1len > 0);
  35. test_eq(s1len, s2len);
  36. test_memeq(s1, s2, s1len);
  37. {
  38. /* XXXX Now fabricate some bad values and make sure they get caught,
  39. * Check 0, 1, N-1, >= N, etc.
  40. */
  41. }
  42. done:
  43. crypto_dh_free(dh1);
  44. crypto_dh_free(dh2);
  45. }
  46. /** Run unit tests for our random number generation function and its wrappers.
  47. */
  48. static void
  49. test_crypto_rng(void)
  50. {
  51. int i, j, allok;
  52. char data1[100], data2[100];
  53. double d;
  54. /* Try out RNG. */
  55. test_assert(! crypto_seed_rng(0));
  56. crypto_rand(data1, 100);
  57. crypto_rand(data2, 100);
  58. test_memneq(data1,data2,100);
  59. allok = 1;
  60. for (i = 0; i < 100; ++i) {
  61. uint64_t big;
  62. char *host;
  63. j = crypto_rand_int(100);
  64. if (j < 0 || j >= 100)
  65. allok = 0;
  66. big = crypto_rand_uint64(U64_LITERAL(1)<<40);
  67. if (big >= (U64_LITERAL(1)<<40))
  68. allok = 0;
  69. big = crypto_rand_uint64(U64_LITERAL(5));
  70. if (big >= 5)
  71. allok = 0;
  72. d = crypto_rand_double();
  73. test_assert(d >= 0);
  74. test_assert(d < 1.0);
  75. host = crypto_random_hostname(3,8,"www.",".onion");
  76. if (strcmpstart(host,"www.") ||
  77. strcmpend(host,".onion") ||
  78. strlen(host) < 13 ||
  79. strlen(host) > 18)
  80. allok = 0;
  81. tor_free(host);
  82. }
  83. test_assert(allok);
  84. done:
  85. ;
  86. }
  87. /** Run unit tests for our AES functionality */
  88. static void
  89. test_crypto_aes(void *arg)
  90. {
  91. char *data1 = NULL, *data2 = NULL, *data3 = NULL;
  92. crypto_cipher_t *env1 = NULL, *env2 = NULL;
  93. int i, j;
  94. char *mem_op_hex_tmp=NULL;
  95. int use_evp = !strcmp(arg,"evp");
  96. evaluate_evp_for_aes(use_evp);
  97. evaluate_ctr_for_aes();
  98. data1 = tor_malloc(1024);
  99. data2 = tor_malloc(1024);
  100. data3 = tor_malloc(1024);
  101. /* Now, test encryption and decryption with stream cipher. */
  102. data1[0]='\0';
  103. for (i = 1023; i>0; i -= 35)
  104. strncat(data1, "Now is the time for all good onions", i);
  105. memset(data2, 0, 1024);
  106. memset(data3, 0, 1024);
  107. env1 = crypto_cipher_new(NULL);
  108. test_neq_ptr(env1, 0);
  109. env2 = crypto_cipher_new(crypto_cipher_get_key(env1));
  110. test_neq_ptr(env2, 0);
  111. /* Try encrypting 512 chars. */
  112. crypto_cipher_encrypt(env1, data2, data1, 512);
  113. crypto_cipher_decrypt(env2, data3, data2, 512);
  114. test_memeq(data1, data3, 512);
  115. test_memneq(data1, data2, 512);
  116. /* Now encrypt 1 at a time, and get 1 at a time. */
  117. for (j = 512; j < 560; ++j) {
  118. crypto_cipher_encrypt(env1, data2+j, data1+j, 1);
  119. }
  120. for (j = 512; j < 560; ++j) {
  121. crypto_cipher_decrypt(env2, data3+j, data2+j, 1);
  122. }
  123. test_memeq(data1, data3, 560);
  124. /* Now encrypt 3 at a time, and get 5 at a time. */
  125. for (j = 560; j < 1024-5; j += 3) {
  126. crypto_cipher_encrypt(env1, data2+j, data1+j, 3);
  127. }
  128. for (j = 560; j < 1024-5; j += 5) {
  129. crypto_cipher_decrypt(env2, data3+j, data2+j, 5);
  130. }
  131. test_memeq(data1, data3, 1024-5);
  132. /* Now make sure that when we encrypt with different chunk sizes, we get
  133. the same results. */
  134. crypto_cipher_free(env2);
  135. env2 = NULL;
  136. memset(data3, 0, 1024);
  137. env2 = crypto_cipher_new(crypto_cipher_get_key(env1));
  138. test_neq_ptr(env2, NULL);
  139. for (j = 0; j < 1024-16; j += 17) {
  140. crypto_cipher_encrypt(env2, data3+j, data1+j, 17);
  141. }
  142. for (j= 0; j < 1024-16; ++j) {
  143. if (data2[j] != data3[j]) {
  144. printf("%d: %d\t%d\n", j, (int) data2[j], (int) data3[j]);
  145. }
  146. }
  147. test_memeq(data2, data3, 1024-16);
  148. crypto_cipher_free(env1);
  149. env1 = NULL;
  150. crypto_cipher_free(env2);
  151. env2 = NULL;
  152. /* NIST test vector for aes. */
  153. /* IV starts at 0 */
  154. env1 = crypto_cipher_new("\x80\x00\x00\x00\x00\x00\x00\x00"
  155. "\x00\x00\x00\x00\x00\x00\x00\x00");
  156. crypto_cipher_encrypt(env1, data1,
  157. "\x00\x00\x00\x00\x00\x00\x00\x00"
  158. "\x00\x00\x00\x00\x00\x00\x00\x00", 16);
  159. test_memeq_hex(data1, "0EDD33D3C621E546455BD8BA1418BEC8");
  160. /* Now test rollover. All these values are originally from a python
  161. * script. */
  162. crypto_cipher_free(env1);
  163. env1 = crypto_cipher_new_with_iv(
  164. "\x80\x00\x00\x00\x00\x00\x00\x00"
  165. "\x00\x00\x00\x00\x00\x00\x00\x00",
  166. "\x00\x00\x00\x00\x00\x00\x00\x00"
  167. "\xff\xff\xff\xff\xff\xff\xff\xff");
  168. memset(data2, 0, 1024);
  169. crypto_cipher_encrypt(env1, data1, data2, 32);
  170. test_memeq_hex(data1, "335fe6da56f843199066c14a00a40231"
  171. "cdd0b917dbc7186908a6bfb5ffd574d3");
  172. crypto_cipher_free(env1);
  173. env1 = crypto_cipher_new_with_iv(
  174. "\x80\x00\x00\x00\x00\x00\x00\x00"
  175. "\x00\x00\x00\x00\x00\x00\x00\x00",
  176. "\x00\x00\x00\x00\xff\xff\xff\xff"
  177. "\xff\xff\xff\xff\xff\xff\xff\xff");
  178. memset(data2, 0, 1024);
  179. crypto_cipher_encrypt(env1, data1, data2, 32);
  180. test_memeq_hex(data1, "e627c6423fa2d77832a02b2794094b73"
  181. "3e63c721df790d2c6469cc1953a3ffac");
  182. crypto_cipher_free(env1);
  183. env1 = crypto_cipher_new_with_iv(
  184. "\x80\x00\x00\x00\x00\x00\x00\x00"
  185. "\x00\x00\x00\x00\x00\x00\x00\x00",
  186. "\xff\xff\xff\xff\xff\xff\xff\xff"
  187. "\xff\xff\xff\xff\xff\xff\xff\xff");
  188. memset(data2, 0, 1024);
  189. crypto_cipher_encrypt(env1, data1, data2, 32);
  190. test_memeq_hex(data1, "2aed2bff0de54f9328efd070bf48f70a"
  191. "0EDD33D3C621E546455BD8BA1418BEC8");
  192. /* Now check rollover on inplace cipher. */
  193. crypto_cipher_free(env1);
  194. env1 = crypto_cipher_new_with_iv(
  195. "\x80\x00\x00\x00\x00\x00\x00\x00"
  196. "\x00\x00\x00\x00\x00\x00\x00\x00",
  197. "\xff\xff\xff\xff\xff\xff\xff\xff"
  198. "\xff\xff\xff\xff\xff\xff\xff\xff");
  199. crypto_cipher_crypt_inplace(env1, data2, 64);
  200. test_memeq_hex(data2, "2aed2bff0de54f9328efd070bf48f70a"
  201. "0EDD33D3C621E546455BD8BA1418BEC8"
  202. "93e2c5243d6839eac58503919192f7ae"
  203. "1908e67cafa08d508816659c2e693191");
  204. crypto_cipher_free(env1);
  205. env1 = crypto_cipher_new_with_iv(
  206. "\x80\x00\x00\x00\x00\x00\x00\x00"
  207. "\x00\x00\x00\x00\x00\x00\x00\x00",
  208. "\xff\xff\xff\xff\xff\xff\xff\xff"
  209. "\xff\xff\xff\xff\xff\xff\xff\xff");
  210. crypto_cipher_crypt_inplace(env1, data2, 64);
  211. test_assert(tor_mem_is_zero(data2, 64));
  212. done:
  213. tor_free(mem_op_hex_tmp);
  214. if (env1)
  215. crypto_cipher_free(env1);
  216. if (env2)
  217. crypto_cipher_free(env2);
  218. tor_free(data1);
  219. tor_free(data2);
  220. tor_free(data3);
  221. }
  222. /** Run unit tests for our SHA-1 functionality */
  223. static void
  224. test_crypto_sha(void)
  225. {
  226. crypto_digest_t *d1 = NULL, *d2 = NULL;
  227. int i;
  228. char key[160];
  229. char digest[32];
  230. char data[50];
  231. char d_out1[DIGEST_LEN], d_out2[DIGEST256_LEN];
  232. char *mem_op_hex_tmp=NULL;
  233. /* Test SHA-1 with a test vector from the specification. */
  234. i = crypto_digest(data, "abc", 3);
  235. test_memeq_hex(data, "A9993E364706816ABA3E25717850C26C9CD0D89D");
  236. tt_int_op(i, ==, 0);
  237. /* Test SHA-256 with a test vector from the specification. */
  238. i = crypto_digest256(data, "abc", 3, DIGEST_SHA256);
  239. test_memeq_hex(data, "BA7816BF8F01CFEA414140DE5DAE2223B00361A3"
  240. "96177A9CB410FF61F20015AD");
  241. tt_int_op(i, ==, 0);
  242. /* Test HMAC-SHA-1 with test cases from RFC2202. */
  243. /* Case 1. */
  244. memset(key, 0x0b, 20);
  245. crypto_hmac_sha1(digest, key, 20, "Hi There", 8);
  246. test_streq(hex_str(digest, 20),
  247. "B617318655057264E28BC0B6FB378C8EF146BE00");
  248. /* Case 2. */
  249. crypto_hmac_sha1(digest, "Jefe", 4, "what do ya want for nothing?", 28);
  250. test_streq(hex_str(digest, 20),
  251. "EFFCDF6AE5EB2FA2D27416D5F184DF9C259A7C79");
  252. /* Case 4. */
  253. base16_decode(key, 25,
  254. "0102030405060708090a0b0c0d0e0f10111213141516171819", 50);
  255. memset(data, 0xcd, 50);
  256. crypto_hmac_sha1(digest, key, 25, data, 50);
  257. test_streq(hex_str(digest, 20),
  258. "4C9007F4026250C6BC8414F9BF50C86C2D7235DA");
  259. /* Case 5. */
  260. memset(key, 0xaa, 80);
  261. crypto_hmac_sha1(digest, key, 80,
  262. "Test Using Larger Than Block-Size Key - Hash Key First",
  263. 54);
  264. test_streq(hex_str(digest, 20),
  265. "AA4AE5E15272D00E95705637CE8A3B55ED402112");
  266. /* Test HMAC-SHA256 with test cases from wikipedia and RFC 4231 */
  267. /* Case empty (wikipedia) */
  268. crypto_hmac_sha256(digest, "", 0, "", 0);
  269. test_streq(hex_str(digest, 32),
  270. "B613679A0814D9EC772F95D778C35FC5FF1697C493715653C6C712144292C5AD");
  271. /* Case quick-brown (wikipedia) */
  272. crypto_hmac_sha256(digest, "key", 3,
  273. "The quick brown fox jumps over the lazy dog", 43);
  274. test_streq(hex_str(digest, 32),
  275. "F7BC83F430538424B13298E6AA6FB143EF4D59A14946175997479DBC2D1A3CD8");
  276. /* "Test Case 1" from RFC 4231 */
  277. memset(key, 0x0b, 20);
  278. crypto_hmac_sha256(digest, key, 20, "Hi There", 8);
  279. test_memeq_hex(digest,
  280. "b0344c61d8db38535ca8afceaf0bf12b"
  281. "881dc200c9833da726e9376c2e32cff7");
  282. /* "Test Case 2" from RFC 4231 */
  283. memset(key, 0x0b, 20);
  284. crypto_hmac_sha256(digest, "Jefe", 4, "what do ya want for nothing?", 28);
  285. test_memeq_hex(digest,
  286. "5bdcc146bf60754e6a042426089575c7"
  287. "5a003f089d2739839dec58b964ec3843");
  288. /* "Test case 3" from RFC 4231 */
  289. memset(key, 0xaa, 20);
  290. memset(data, 0xdd, 50);
  291. crypto_hmac_sha256(digest, key, 20, data, 50);
  292. test_memeq_hex(digest,
  293. "773ea91e36800e46854db8ebd09181a7"
  294. "2959098b3ef8c122d9635514ced565fe");
  295. /* "Test case 4" from RFC 4231 */
  296. base16_decode(key, 25,
  297. "0102030405060708090a0b0c0d0e0f10111213141516171819", 50);
  298. memset(data, 0xcd, 50);
  299. crypto_hmac_sha256(digest, key, 25, data, 50);
  300. test_memeq_hex(digest,
  301. "82558a389a443c0ea4cc819899f2083a"
  302. "85f0faa3e578f8077a2e3ff46729665b");
  303. /* "Test case 5" from RFC 4231 */
  304. memset(key, 0x0c, 20);
  305. crypto_hmac_sha256(digest, key, 20, "Test With Truncation", 20);
  306. test_memeq_hex(digest,
  307. "a3b6167473100ee06e0c796c2955552b");
  308. /* "Test case 6" from RFC 4231 */
  309. memset(key, 0xaa, 131);
  310. crypto_hmac_sha256(digest, key, 131,
  311. "Test Using Larger Than Block-Size Key - Hash Key First",
  312. 54);
  313. test_memeq_hex(digest,
  314. "60e431591ee0b67f0d8a26aacbf5b77f"
  315. "8e0bc6213728c5140546040f0ee37f54");
  316. /* "Test case 7" from RFC 4231 */
  317. memset(key, 0xaa, 131);
  318. crypto_hmac_sha256(digest, key, 131,
  319. "This is a test using a larger than block-size key and a "
  320. "larger than block-size data. The key needs to be hashed "
  321. "before being used by the HMAC algorithm.", 152);
  322. test_memeq_hex(digest,
  323. "9b09ffa71b942fcb27635fbcd5b0e944"
  324. "bfdc63644f0713938a7f51535c3a35e2");
  325. /* Incremental digest code. */
  326. d1 = crypto_digest_new();
  327. test_assert(d1);
  328. crypto_digest_add_bytes(d1, "abcdef", 6);
  329. d2 = crypto_digest_dup(d1);
  330. test_assert(d2);
  331. crypto_digest_add_bytes(d2, "ghijkl", 6);
  332. crypto_digest_get_digest(d2, d_out1, sizeof(d_out1));
  333. crypto_digest(d_out2, "abcdefghijkl", 12);
  334. test_memeq(d_out1, d_out2, DIGEST_LEN);
  335. crypto_digest_assign(d2, d1);
  336. crypto_digest_add_bytes(d2, "mno", 3);
  337. crypto_digest_get_digest(d2, d_out1, sizeof(d_out1));
  338. crypto_digest(d_out2, "abcdefmno", 9);
  339. test_memeq(d_out1, d_out2, DIGEST_LEN);
  340. crypto_digest_get_digest(d1, d_out1, sizeof(d_out1));
  341. crypto_digest(d_out2, "abcdef", 6);
  342. test_memeq(d_out1, d_out2, DIGEST_LEN);
  343. crypto_digest_free(d1);
  344. crypto_digest_free(d2);
  345. /* Incremental digest code with sha256 */
  346. d1 = crypto_digest256_new(DIGEST_SHA256);
  347. test_assert(d1);
  348. crypto_digest_add_bytes(d1, "abcdef", 6);
  349. d2 = crypto_digest_dup(d1);
  350. test_assert(d2);
  351. crypto_digest_add_bytes(d2, "ghijkl", 6);
  352. crypto_digest_get_digest(d2, d_out1, sizeof(d_out1));
  353. crypto_digest256(d_out2, "abcdefghijkl", 12, DIGEST_SHA256);
  354. test_memeq(d_out1, d_out2, DIGEST_LEN);
  355. crypto_digest_assign(d2, d1);
  356. crypto_digest_add_bytes(d2, "mno", 3);
  357. crypto_digest_get_digest(d2, d_out1, sizeof(d_out1));
  358. crypto_digest256(d_out2, "abcdefmno", 9, DIGEST_SHA256);
  359. test_memeq(d_out1, d_out2, DIGEST_LEN);
  360. crypto_digest_get_digest(d1, d_out1, sizeof(d_out1));
  361. crypto_digest256(d_out2, "abcdef", 6, DIGEST_SHA256);
  362. test_memeq(d_out1, d_out2, DIGEST_LEN);
  363. done:
  364. if (d1)
  365. crypto_digest_free(d1);
  366. if (d2)
  367. crypto_digest_free(d2);
  368. tor_free(mem_op_hex_tmp);
  369. }
  370. /** Run unit tests for our public key crypto functions */
  371. static void
  372. test_crypto_pk(void)
  373. {
  374. crypto_pk_t *pk1 = NULL, *pk2 = NULL;
  375. char *encoded = NULL;
  376. char data1[1024], data2[1024], data3[1024];
  377. size_t size;
  378. int i, j, p, len;
  379. /* Public-key ciphers */
  380. pk1 = pk_generate(0);
  381. pk2 = crypto_pk_new();
  382. test_assert(pk1 && pk2);
  383. test_assert(! crypto_pk_write_public_key_to_string(pk1, &encoded, &size));
  384. test_assert(! crypto_pk_read_public_key_from_string(pk2, encoded, size));
  385. test_eq(0, crypto_pk_cmp_keys(pk1, pk2));
  386. /* comparison between keys and NULL */
  387. tt_int_op(crypto_pk_cmp_keys(NULL, pk1), <, 0);
  388. tt_int_op(crypto_pk_cmp_keys(NULL, NULL), ==, 0);
  389. tt_int_op(crypto_pk_cmp_keys(pk1, NULL), >, 0);
  390. test_eq(128, crypto_pk_keysize(pk1));
  391. test_eq(1024, crypto_pk_num_bits(pk1));
  392. test_eq(128, crypto_pk_keysize(pk2));
  393. test_eq(1024, crypto_pk_num_bits(pk2));
  394. test_eq(128, crypto_pk_public_encrypt(pk2, data1, sizeof(data1),
  395. "Hello whirled.", 15,
  396. PK_PKCS1_OAEP_PADDING));
  397. test_eq(128, crypto_pk_public_encrypt(pk1, data2, sizeof(data1),
  398. "Hello whirled.", 15,
  399. PK_PKCS1_OAEP_PADDING));
  400. /* oaep padding should make encryption not match */
  401. test_memneq(data1, data2, 128);
  402. test_eq(15, crypto_pk_private_decrypt(pk1, data3, sizeof(data3), data1, 128,
  403. PK_PKCS1_OAEP_PADDING,1));
  404. test_streq(data3, "Hello whirled.");
  405. memset(data3, 0, 1024);
  406. test_eq(15, crypto_pk_private_decrypt(pk1, data3, sizeof(data3), data2, 128,
  407. PK_PKCS1_OAEP_PADDING,1));
  408. test_streq(data3, "Hello whirled.");
  409. /* Can't decrypt with public key. */
  410. test_eq(-1, crypto_pk_private_decrypt(pk2, data3, sizeof(data3), data2, 128,
  411. PK_PKCS1_OAEP_PADDING,1));
  412. /* Try again with bad padding */
  413. memcpy(data2+1, "XYZZY", 5); /* This has fails ~ once-in-2^40 */
  414. test_eq(-1, crypto_pk_private_decrypt(pk1, data3, sizeof(data3), data2, 128,
  415. PK_PKCS1_OAEP_PADDING,1));
  416. /* File operations: save and load private key */
  417. test_assert(! crypto_pk_write_private_key_to_filename(pk1,
  418. get_fname("pkey1")));
  419. /* failing case for read: can't read. */
  420. test_assert(crypto_pk_read_private_key_from_filename(pk2,
  421. get_fname("xyzzy")) < 0);
  422. write_str_to_file(get_fname("xyzzy"), "foobar", 6);
  423. /* Failing case for read: no key. */
  424. test_assert(crypto_pk_read_private_key_from_filename(pk2,
  425. get_fname("xyzzy")) < 0);
  426. test_assert(! crypto_pk_read_private_key_from_filename(pk2,
  427. get_fname("pkey1")));
  428. test_eq(15, crypto_pk_private_decrypt(pk2, data3, sizeof(data3), data1, 128,
  429. PK_PKCS1_OAEP_PADDING,1));
  430. /* Now try signing. */
  431. strlcpy(data1, "Ossifrage", 1024);
  432. test_eq(128, crypto_pk_private_sign(pk1, data2, sizeof(data2), data1, 10));
  433. test_eq(10,
  434. crypto_pk_public_checksig(pk1, data3, sizeof(data3), data2, 128));
  435. test_streq(data3, "Ossifrage");
  436. /* Try signing digests. */
  437. test_eq(128, crypto_pk_private_sign_digest(pk1, data2, sizeof(data2),
  438. data1, 10));
  439. test_eq(20,
  440. crypto_pk_public_checksig(pk1, data3, sizeof(data3), data2, 128));
  441. test_eq(0, crypto_pk_public_checksig_digest(pk1, data1, 10, data2, 128));
  442. test_eq(-1, crypto_pk_public_checksig_digest(pk1, data1, 11, data2, 128));
  443. /*XXXX test failed signing*/
  444. /* Try encoding */
  445. crypto_pk_free(pk2);
  446. pk2 = NULL;
  447. i = crypto_pk_asn1_encode(pk1, data1, 1024);
  448. test_assert(i>0);
  449. pk2 = crypto_pk_asn1_decode(data1, i);
  450. test_assert(crypto_pk_cmp_keys(pk1,pk2) == 0);
  451. /* Try with hybrid encryption wrappers. */
  452. crypto_rand(data1, 1024);
  453. for (i = 0; i < 2; ++i) {
  454. for (j = 85; j < 140; ++j) {
  455. memset(data2,0,1024);
  456. memset(data3,0,1024);
  457. p = (i==0)?PK_PKCS1_PADDING:PK_PKCS1_OAEP_PADDING;
  458. len = crypto_pk_public_hybrid_encrypt(pk1,data2,sizeof(data2),
  459. data1,j,p,0);
  460. test_assert(len>=0);
  461. len = crypto_pk_private_hybrid_decrypt(pk1,data3,sizeof(data3),
  462. data2,len,p,1);
  463. test_eq(len,j);
  464. test_memeq(data1,data3,j);
  465. }
  466. }
  467. /* Try copy_full */
  468. crypto_pk_free(pk2);
  469. pk2 = crypto_pk_copy_full(pk1);
  470. test_assert(pk2 != NULL);
  471. test_neq_ptr(pk1, pk2);
  472. test_assert(crypto_pk_cmp_keys(pk1,pk2) == 0);
  473. done:
  474. if (pk1)
  475. crypto_pk_free(pk1);
  476. if (pk2)
  477. crypto_pk_free(pk2);
  478. tor_free(encoded);
  479. }
  480. /** Run unit tests for misc crypto formatting functionality (base64, base32,
  481. * fingerprints, etc) */
  482. static void
  483. test_crypto_formats(void)
  484. {
  485. char *data1 = NULL, *data2 = NULL, *data3 = NULL;
  486. int i, j, idx;
  487. data1 = tor_malloc(1024);
  488. data2 = tor_malloc(1024);
  489. data3 = tor_malloc(1024);
  490. test_assert(data1 && data2 && data3);
  491. /* Base64 tests */
  492. memset(data1, 6, 1024);
  493. for (idx = 0; idx < 10; ++idx) {
  494. i = base64_encode(data2, 1024, data1, idx);
  495. test_assert(i >= 0);
  496. j = base64_decode(data3, 1024, data2, i);
  497. test_eq(j,idx);
  498. test_memeq(data3, data1, idx);
  499. }
  500. strlcpy(data1, "Test string that contains 35 chars.", 1024);
  501. strlcat(data1, " 2nd string that contains 35 chars.", 1024);
  502. i = base64_encode(data2, 1024, data1, 71);
  503. test_assert(i >= 0);
  504. j = base64_decode(data3, 1024, data2, i);
  505. test_eq(j, 71);
  506. test_streq(data3, data1);
  507. test_assert(data2[i] == '\0');
  508. crypto_rand(data1, DIGEST_LEN);
  509. memset(data2, 100, 1024);
  510. digest_to_base64(data2, data1);
  511. test_eq(BASE64_DIGEST_LEN, strlen(data2));
  512. test_eq(100, data2[BASE64_DIGEST_LEN+2]);
  513. memset(data3, 99, 1024);
  514. test_eq(digest_from_base64(data3, data2), 0);
  515. test_memeq(data1, data3, DIGEST_LEN);
  516. test_eq(99, data3[DIGEST_LEN+1]);
  517. test_assert(digest_from_base64(data3, "###") < 0);
  518. /* Encoding SHA256 */
  519. crypto_rand(data2, DIGEST256_LEN);
  520. memset(data2, 100, 1024);
  521. digest256_to_base64(data2, data1);
  522. test_eq(BASE64_DIGEST256_LEN, strlen(data2));
  523. test_eq(100, data2[BASE64_DIGEST256_LEN+2]);
  524. memset(data3, 99, 1024);
  525. test_eq(digest256_from_base64(data3, data2), 0);
  526. test_memeq(data1, data3, DIGEST256_LEN);
  527. test_eq(99, data3[DIGEST256_LEN+1]);
  528. /* Base32 tests */
  529. strlcpy(data1, "5chrs", 1024);
  530. /* bit pattern is: [35 63 68 72 73] ->
  531. * [00110101 01100011 01101000 01110010 01110011]
  532. * By 5s: [00110 10101 10001 10110 10000 11100 10011 10011]
  533. */
  534. base32_encode(data2, 9, data1, 5);
  535. test_streq(data2, "gvrwq4tt");
  536. strlcpy(data1, "\xFF\xF5\x6D\x44\xAE\x0D\x5C\xC9\x62\xC4", 1024);
  537. base32_encode(data2, 30, data1, 10);
  538. test_streq(data2, "772w2rfobvomsywe");
  539. /* Base16 tests */
  540. strlcpy(data1, "6chrs\xff", 1024);
  541. base16_encode(data2, 13, data1, 6);
  542. test_streq(data2, "3663687273FF");
  543. strlcpy(data1, "f0d678affc000100", 1024);
  544. i = base16_decode(data2, 8, data1, 16);
  545. test_eq(i,0);
  546. test_memeq(data2, "\xf0\xd6\x78\xaf\xfc\x00\x01\x00",8);
  547. /* now try some failing base16 decodes */
  548. test_eq(-1, base16_decode(data2, 8, data1, 15)); /* odd input len */
  549. test_eq(-1, base16_decode(data2, 7, data1, 16)); /* dest too short */
  550. strlcpy(data1, "f0dz!8affc000100", 1024);
  551. test_eq(-1, base16_decode(data2, 8, data1, 16));
  552. tor_free(data1);
  553. tor_free(data2);
  554. tor_free(data3);
  555. /* Add spaces to fingerprint */
  556. {
  557. data1 = tor_strdup("ABCD1234ABCD56780000ABCD1234ABCD56780000");
  558. test_eq(strlen(data1), 40);
  559. data2 = tor_malloc(FINGERPRINT_LEN+1);
  560. add_spaces_to_fp(data2, FINGERPRINT_LEN+1, data1);
  561. test_streq(data2, "ABCD 1234 ABCD 5678 0000 ABCD 1234 ABCD 5678 0000");
  562. tor_free(data1);
  563. tor_free(data2);
  564. }
  565. /* Check fingerprint */
  566. {
  567. test_assert(crypto_pk_check_fingerprint_syntax(
  568. "ABCD 1234 ABCD 5678 0000 ABCD 1234 ABCD 5678 0000"));
  569. test_assert(!crypto_pk_check_fingerprint_syntax(
  570. "ABCD 1234 ABCD 5678 0000 ABCD 1234 ABCD 5678 000"));
  571. test_assert(!crypto_pk_check_fingerprint_syntax(
  572. "ABCD 1234 ABCD 5678 0000 ABCD 1234 ABCD 5678 00000"));
  573. test_assert(!crypto_pk_check_fingerprint_syntax(
  574. "ABCD 1234 ABCD 5678 0000 ABCD1234 ABCD 5678 0000"));
  575. test_assert(!crypto_pk_check_fingerprint_syntax(
  576. "ABCD 1234 ABCD 5678 0000 ABCD1234 ABCD 5678 00000"));
  577. test_assert(!crypto_pk_check_fingerprint_syntax(
  578. "ACD 1234 ABCD 5678 0000 ABCD 1234 ABCD 5678 00000"));
  579. }
  580. done:
  581. tor_free(data1);
  582. tor_free(data2);
  583. tor_free(data3);
  584. }
  585. /** Run unit tests for our secret-to-key passphrase hashing functionality. */
  586. static void
  587. test_crypto_s2k(void)
  588. {
  589. char buf[29];
  590. char buf2[29];
  591. char *buf3 = NULL;
  592. int i;
  593. memset(buf, 0, sizeof(buf));
  594. memset(buf2, 0, sizeof(buf2));
  595. buf3 = tor_malloc(65536);
  596. memset(buf3, 0, 65536);
  597. secret_to_key(buf+9, 20, "", 0, buf);
  598. crypto_digest(buf2+9, buf3, 1024);
  599. test_memeq(buf, buf2, 29);
  600. memcpy(buf,"vrbacrda",8);
  601. memcpy(buf2,"vrbacrda",8);
  602. buf[8] = 96;
  603. buf2[8] = 96;
  604. secret_to_key(buf+9, 20, "12345678", 8, buf);
  605. for (i = 0; i < 65536; i += 16) {
  606. memcpy(buf3+i, "vrbacrda12345678", 16);
  607. }
  608. crypto_digest(buf2+9, buf3, 65536);
  609. test_memeq(buf, buf2, 29);
  610. done:
  611. tor_free(buf3);
  612. }
  613. /** Test AES-CTR encryption and decryption with IV. */
  614. static void
  615. test_crypto_aes_iv(void *arg)
  616. {
  617. char *plain, *encrypted1, *encrypted2, *decrypted1, *decrypted2;
  618. char plain_1[1], plain_15[15], plain_16[16], plain_17[17];
  619. char key1[16], key2[16];
  620. ssize_t encrypted_size, decrypted_size;
  621. int use_evp = !strcmp(arg,"evp");
  622. evaluate_evp_for_aes(use_evp);
  623. plain = tor_malloc(4095);
  624. encrypted1 = tor_malloc(4095 + 1 + 16);
  625. encrypted2 = tor_malloc(4095 + 1 + 16);
  626. decrypted1 = tor_malloc(4095 + 1);
  627. decrypted2 = tor_malloc(4095 + 1);
  628. crypto_rand(plain, 4095);
  629. crypto_rand(key1, 16);
  630. crypto_rand(key2, 16);
  631. crypto_rand(plain_1, 1);
  632. crypto_rand(plain_15, 15);
  633. crypto_rand(plain_16, 16);
  634. crypto_rand(plain_17, 17);
  635. key1[0] = key2[0] + 128; /* Make sure that contents are different. */
  636. /* Encrypt and decrypt with the same key. */
  637. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 4095,
  638. plain, 4095);
  639. test_eq(encrypted_size, 16 + 4095);
  640. tt_assert(encrypted_size > 0); /* This is obviously true, since 4111 is
  641. * greater than 0, but its truth is not
  642. * obvious to all analysis tools. */
  643. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 4095,
  644. encrypted1, encrypted_size);
  645. test_eq(decrypted_size, 4095);
  646. tt_assert(decrypted_size > 0);
  647. test_memeq(plain, decrypted1, 4095);
  648. /* Encrypt a second time (with a new random initialization vector). */
  649. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted2, 16 + 4095,
  650. plain, 4095);
  651. test_eq(encrypted_size, 16 + 4095);
  652. tt_assert(encrypted_size > 0);
  653. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted2, 4095,
  654. encrypted2, encrypted_size);
  655. test_eq(decrypted_size, 4095);
  656. tt_assert(decrypted_size > 0);
  657. test_memeq(plain, decrypted2, 4095);
  658. test_memneq(encrypted1, encrypted2, encrypted_size);
  659. /* Decrypt with the wrong key. */
  660. decrypted_size = crypto_cipher_decrypt_with_iv(key2, decrypted2, 4095,
  661. encrypted1, encrypted_size);
  662. test_memneq(plain, decrypted2, encrypted_size);
  663. /* Alter the initialization vector. */
  664. encrypted1[0] += 42;
  665. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 4095,
  666. encrypted1, encrypted_size);
  667. test_memneq(plain, decrypted2, 4095);
  668. /* Special length case: 1. */
  669. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 1,
  670. plain_1, 1);
  671. test_eq(encrypted_size, 16 + 1);
  672. tt_assert(encrypted_size > 0);
  673. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 1,
  674. encrypted1, encrypted_size);
  675. test_eq(decrypted_size, 1);
  676. tt_assert(decrypted_size > 0);
  677. test_memeq(plain_1, decrypted1, 1);
  678. /* Special length case: 15. */
  679. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 15,
  680. plain_15, 15);
  681. test_eq(encrypted_size, 16 + 15);
  682. tt_assert(encrypted_size > 0);
  683. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 15,
  684. encrypted1, encrypted_size);
  685. test_eq(decrypted_size, 15);
  686. tt_assert(decrypted_size > 0);
  687. test_memeq(plain_15, decrypted1, 15);
  688. /* Special length case: 16. */
  689. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 16,
  690. plain_16, 16);
  691. test_eq(encrypted_size, 16 + 16);
  692. tt_assert(encrypted_size > 0);
  693. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 16,
  694. encrypted1, encrypted_size);
  695. test_eq(decrypted_size, 16);
  696. tt_assert(decrypted_size > 0);
  697. test_memeq(plain_16, decrypted1, 16);
  698. /* Special length case: 17. */
  699. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 17,
  700. plain_17, 17);
  701. test_eq(encrypted_size, 16 + 17);
  702. tt_assert(encrypted_size > 0);
  703. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 17,
  704. encrypted1, encrypted_size);
  705. test_eq(decrypted_size, 17);
  706. tt_assert(decrypted_size > 0);
  707. test_memeq(plain_17, decrypted1, 17);
  708. done:
  709. /* Free memory. */
  710. tor_free(plain);
  711. tor_free(encrypted1);
  712. tor_free(encrypted2);
  713. tor_free(decrypted1);
  714. tor_free(decrypted2);
  715. }
  716. /** Test base32 decoding. */
  717. static void
  718. test_crypto_base32_decode(void)
  719. {
  720. char plain[60], encoded[96 + 1], decoded[60];
  721. int res;
  722. crypto_rand(plain, 60);
  723. /* Encode and decode a random string. */
  724. base32_encode(encoded, 96 + 1, plain, 60);
  725. res = base32_decode(decoded, 60, encoded, 96);
  726. test_eq(res, 0);
  727. test_memeq(plain, decoded, 60);
  728. /* Encode, uppercase, and decode a random string. */
  729. base32_encode(encoded, 96 + 1, plain, 60);
  730. tor_strupper(encoded);
  731. res = base32_decode(decoded, 60, encoded, 96);
  732. test_eq(res, 0);
  733. test_memeq(plain, decoded, 60);
  734. /* Change encoded string and decode. */
  735. if (encoded[0] == 'A' || encoded[0] == 'a')
  736. encoded[0] = 'B';
  737. else
  738. encoded[0] = 'A';
  739. res = base32_decode(decoded, 60, encoded, 96);
  740. test_eq(res, 0);
  741. test_memneq(plain, decoded, 60);
  742. /* Bad encodings. */
  743. encoded[0] = '!';
  744. res = base32_decode(decoded, 60, encoded, 96);
  745. test_assert(res < 0);
  746. done:
  747. ;
  748. }
  749. static void *
  750. pass_data_setup_fn(const struct testcase_t *testcase)
  751. {
  752. return testcase->setup_data;
  753. }
  754. static int
  755. pass_data_cleanup_fn(const struct testcase_t *testcase, void *ptr)
  756. {
  757. (void)ptr;
  758. (void)testcase;
  759. return 1;
  760. }
  761. static const struct testcase_setup_t pass_data = {
  762. pass_data_setup_fn, pass_data_cleanup_fn
  763. };
  764. #define CRYPTO_LEGACY(name) \
  765. { #name, legacy_test_helper, 0, &legacy_setup, test_crypto_ ## name }
  766. struct testcase_t crypto_tests[] = {
  767. CRYPTO_LEGACY(formats),
  768. CRYPTO_LEGACY(rng),
  769. { "aes_AES", test_crypto_aes, TT_FORK, &pass_data, (void*)"aes" },
  770. { "aes_EVP", test_crypto_aes, TT_FORK, &pass_data, (void*)"evp" },
  771. CRYPTO_LEGACY(sha),
  772. CRYPTO_LEGACY(pk),
  773. CRYPTO_LEGACY(dh),
  774. CRYPTO_LEGACY(s2k),
  775. { "aes_iv_AES", test_crypto_aes_iv, TT_FORK, &pass_data, (void*)"aes" },
  776. { "aes_iv_EVP", test_crypto_aes_iv, TT_FORK, &pass_data, (void*)"evp" },
  777. CRYPTO_LEGACY(base32_decode),
  778. END_OF_TESTCASES
  779. };