test_crypto.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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(env1, 0);
  109. env2 = crypto_cipher_new(crypto_cipher_get_key(env1));
  110. test_neq(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(env2, 0);
  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. test_eq(128, crypto_pk_keysize(pk1));
  387. test_eq(1024, crypto_pk_num_bits(pk1));
  388. test_eq(128, crypto_pk_keysize(pk2));
  389. test_eq(1024, crypto_pk_num_bits(pk2));
  390. test_eq(128, crypto_pk_public_encrypt(pk2, data1, sizeof(data1),
  391. "Hello whirled.", 15,
  392. PK_PKCS1_OAEP_PADDING));
  393. test_eq(128, crypto_pk_public_encrypt(pk1, data2, sizeof(data1),
  394. "Hello whirled.", 15,
  395. PK_PKCS1_OAEP_PADDING));
  396. /* oaep padding should make encryption not match */
  397. test_memneq(data1, data2, 128);
  398. test_eq(15, crypto_pk_private_decrypt(pk1, data3, sizeof(data3), data1, 128,
  399. PK_PKCS1_OAEP_PADDING,1));
  400. test_streq(data3, "Hello whirled.");
  401. memset(data3, 0, 1024);
  402. test_eq(15, crypto_pk_private_decrypt(pk1, data3, sizeof(data3), data2, 128,
  403. PK_PKCS1_OAEP_PADDING,1));
  404. test_streq(data3, "Hello whirled.");
  405. /* Can't decrypt with public key. */
  406. test_eq(-1, crypto_pk_private_decrypt(pk2, data3, sizeof(data3), data2, 128,
  407. PK_PKCS1_OAEP_PADDING,1));
  408. /* Try again with bad padding */
  409. memcpy(data2+1, "XYZZY", 5); /* This has fails ~ once-in-2^40 */
  410. test_eq(-1, crypto_pk_private_decrypt(pk1, data3, sizeof(data3), data2, 128,
  411. PK_PKCS1_OAEP_PADDING,1));
  412. /* File operations: save and load private key */
  413. test_assert(! crypto_pk_write_private_key_to_filename(pk1,
  414. get_fname("pkey1")));
  415. /* failing case for read: can't read. */
  416. test_assert(crypto_pk_read_private_key_from_filename(pk2,
  417. get_fname("xyzzy")) < 0);
  418. write_str_to_file(get_fname("xyzzy"), "foobar", 6);
  419. /* Failing case for read: no key. */
  420. test_assert(crypto_pk_read_private_key_from_filename(pk2,
  421. get_fname("xyzzy")) < 0);
  422. test_assert(! crypto_pk_read_private_key_from_filename(pk2,
  423. get_fname("pkey1")));
  424. test_eq(15, crypto_pk_private_decrypt(pk2, data3, sizeof(data3), data1, 128,
  425. PK_PKCS1_OAEP_PADDING,1));
  426. /* Now try signing. */
  427. strlcpy(data1, "Ossifrage", 1024);
  428. test_eq(128, crypto_pk_private_sign(pk1, data2, sizeof(data2), data1, 10));
  429. test_eq(10,
  430. crypto_pk_public_checksig(pk1, data3, sizeof(data3), data2, 128));
  431. test_streq(data3, "Ossifrage");
  432. /* Try signing digests. */
  433. test_eq(128, crypto_pk_private_sign_digest(pk1, data2, sizeof(data2),
  434. data1, 10));
  435. test_eq(20,
  436. crypto_pk_public_checksig(pk1, data3, sizeof(data3), data2, 128));
  437. test_eq(0, crypto_pk_public_checksig_digest(pk1, data1, 10, data2, 128));
  438. test_eq(-1, crypto_pk_public_checksig_digest(pk1, data1, 11, data2, 128));
  439. /*XXXX test failed signing*/
  440. /* Try encoding */
  441. crypto_pk_free(pk2);
  442. pk2 = NULL;
  443. i = crypto_pk_asn1_encode(pk1, data1, 1024);
  444. test_assert(i>0);
  445. pk2 = crypto_pk_asn1_decode(data1, i);
  446. test_assert(crypto_pk_cmp_keys(pk1,pk2) == 0);
  447. /* Try with hybrid encryption wrappers. */
  448. crypto_rand(data1, 1024);
  449. for (i = 0; i < 2; ++i) {
  450. for (j = 85; j < 140; ++j) {
  451. memset(data2,0,1024);
  452. memset(data3,0,1024);
  453. p = (i==0)?PK_PKCS1_PADDING:PK_PKCS1_OAEP_PADDING;
  454. len = crypto_pk_public_hybrid_encrypt(pk1,data2,sizeof(data2),
  455. data1,j,p,0);
  456. test_assert(len>=0);
  457. len = crypto_pk_private_hybrid_decrypt(pk1,data3,sizeof(data3),
  458. data2,len,p,1);
  459. test_eq(len,j);
  460. test_memeq(data1,data3,j);
  461. }
  462. }
  463. /* Try copy_full */
  464. crypto_pk_free(pk2);
  465. pk2 = crypto_pk_copy_full(pk1);
  466. test_assert(pk2 != NULL);
  467. test_neq_ptr(pk1, pk2);
  468. test_assert(crypto_pk_cmp_keys(pk1,pk2) == 0);
  469. done:
  470. if (pk1)
  471. crypto_pk_free(pk1);
  472. if (pk2)
  473. crypto_pk_free(pk2);
  474. tor_free(encoded);
  475. }
  476. /** Run unit tests for misc crypto formatting functionality (base64, base32,
  477. * fingerprints, etc) */
  478. static void
  479. test_crypto_formats(void)
  480. {
  481. char *data1 = NULL, *data2 = NULL, *data3 = NULL;
  482. int i, j, idx;
  483. data1 = tor_malloc(1024);
  484. data2 = tor_malloc(1024);
  485. data3 = tor_malloc(1024);
  486. test_assert(data1 && data2 && data3);
  487. /* Base64 tests */
  488. memset(data1, 6, 1024);
  489. for (idx = 0; idx < 10; ++idx) {
  490. i = base64_encode(data2, 1024, data1, idx);
  491. test_assert(i >= 0);
  492. j = base64_decode(data3, 1024, data2, i);
  493. test_eq(j,idx);
  494. test_memeq(data3, data1, idx);
  495. }
  496. strlcpy(data1, "Test string that contains 35 chars.", 1024);
  497. strlcat(data1, " 2nd string that contains 35 chars.", 1024);
  498. i = base64_encode(data2, 1024, data1, 71);
  499. test_assert(i >= 0);
  500. j = base64_decode(data3, 1024, data2, i);
  501. test_eq(j, 71);
  502. test_streq(data3, data1);
  503. test_assert(data2[i] == '\0');
  504. crypto_rand(data1, DIGEST_LEN);
  505. memset(data2, 100, 1024);
  506. digest_to_base64(data2, data1);
  507. test_eq(BASE64_DIGEST_LEN, strlen(data2));
  508. test_eq(100, data2[BASE64_DIGEST_LEN+2]);
  509. memset(data3, 99, 1024);
  510. test_eq(digest_from_base64(data3, data2), 0);
  511. test_memeq(data1, data3, DIGEST_LEN);
  512. test_eq(99, data3[DIGEST_LEN+1]);
  513. test_assert(digest_from_base64(data3, "###") < 0);
  514. /* Encoding SHA256 */
  515. crypto_rand(data2, DIGEST256_LEN);
  516. memset(data2, 100, 1024);
  517. digest256_to_base64(data2, data1);
  518. test_eq(BASE64_DIGEST256_LEN, strlen(data2));
  519. test_eq(100, data2[BASE64_DIGEST256_LEN+2]);
  520. memset(data3, 99, 1024);
  521. test_eq(digest256_from_base64(data3, data2), 0);
  522. test_memeq(data1, data3, DIGEST256_LEN);
  523. test_eq(99, data3[DIGEST256_LEN+1]);
  524. /* Base32 tests */
  525. strlcpy(data1, "5chrs", 1024);
  526. /* bit pattern is: [35 63 68 72 73] ->
  527. * [00110101 01100011 01101000 01110010 01110011]
  528. * By 5s: [00110 10101 10001 10110 10000 11100 10011 10011]
  529. */
  530. base32_encode(data2, 9, data1, 5);
  531. test_streq(data2, "gvrwq4tt");
  532. strlcpy(data1, "\xFF\xF5\x6D\x44\xAE\x0D\x5C\xC9\x62\xC4", 1024);
  533. base32_encode(data2, 30, data1, 10);
  534. test_streq(data2, "772w2rfobvomsywe");
  535. /* Base16 tests */
  536. strlcpy(data1, "6chrs\xff", 1024);
  537. base16_encode(data2, 13, data1, 6);
  538. test_streq(data2, "3663687273FF");
  539. strlcpy(data1, "f0d678affc000100", 1024);
  540. i = base16_decode(data2, 8, data1, 16);
  541. test_eq(i,0);
  542. test_memeq(data2, "\xf0\xd6\x78\xaf\xfc\x00\x01\x00",8);
  543. /* now try some failing base16 decodes */
  544. test_eq(-1, base16_decode(data2, 8, data1, 15)); /* odd input len */
  545. test_eq(-1, base16_decode(data2, 7, data1, 16)); /* dest too short */
  546. strlcpy(data1, "f0dz!8affc000100", 1024);
  547. test_eq(-1, base16_decode(data2, 8, data1, 16));
  548. tor_free(data1);
  549. tor_free(data2);
  550. tor_free(data3);
  551. /* Add spaces to fingerprint */
  552. {
  553. data1 = tor_strdup("ABCD1234ABCD56780000ABCD1234ABCD56780000");
  554. test_eq(strlen(data1), 40);
  555. data2 = tor_malloc(FINGERPRINT_LEN+1);
  556. add_spaces_to_fp(data2, FINGERPRINT_LEN+1, data1);
  557. test_streq(data2, "ABCD 1234 ABCD 5678 0000 ABCD 1234 ABCD 5678 0000");
  558. tor_free(data1);
  559. tor_free(data2);
  560. }
  561. /* Check fingerprint */
  562. {
  563. test_assert(crypto_pk_check_fingerprint_syntax(
  564. "ABCD 1234 ABCD 5678 0000 ABCD 1234 ABCD 5678 0000"));
  565. test_assert(!crypto_pk_check_fingerprint_syntax(
  566. "ABCD 1234 ABCD 5678 0000 ABCD 1234 ABCD 5678 000"));
  567. test_assert(!crypto_pk_check_fingerprint_syntax(
  568. "ABCD 1234 ABCD 5678 0000 ABCD 1234 ABCD 5678 00000"));
  569. test_assert(!crypto_pk_check_fingerprint_syntax(
  570. "ABCD 1234 ABCD 5678 0000 ABCD1234 ABCD 5678 0000"));
  571. test_assert(!crypto_pk_check_fingerprint_syntax(
  572. "ABCD 1234 ABCD 5678 0000 ABCD1234 ABCD 5678 00000"));
  573. test_assert(!crypto_pk_check_fingerprint_syntax(
  574. "ACD 1234 ABCD 5678 0000 ABCD 1234 ABCD 5678 00000"));
  575. }
  576. done:
  577. tor_free(data1);
  578. tor_free(data2);
  579. tor_free(data3);
  580. }
  581. /** Run unit tests for our secret-to-key passphrase hashing functionality. */
  582. static void
  583. test_crypto_s2k(void)
  584. {
  585. char buf[29];
  586. char buf2[29];
  587. char *buf3 = NULL;
  588. int i;
  589. memset(buf, 0, sizeof(buf));
  590. memset(buf2, 0, sizeof(buf2));
  591. buf3 = tor_malloc(65536);
  592. memset(buf3, 0, 65536);
  593. secret_to_key(buf+9, 20, "", 0, buf);
  594. crypto_digest(buf2+9, buf3, 1024);
  595. test_memeq(buf, buf2, 29);
  596. memcpy(buf,"vrbacrda",8);
  597. memcpy(buf2,"vrbacrda",8);
  598. buf[8] = 96;
  599. buf2[8] = 96;
  600. secret_to_key(buf+9, 20, "12345678", 8, buf);
  601. for (i = 0; i < 65536; i += 16) {
  602. memcpy(buf3+i, "vrbacrda12345678", 16);
  603. }
  604. crypto_digest(buf2+9, buf3, 65536);
  605. test_memeq(buf, buf2, 29);
  606. done:
  607. tor_free(buf3);
  608. }
  609. /** Test AES-CTR encryption and decryption with IV. */
  610. static void
  611. test_crypto_aes_iv(void *arg)
  612. {
  613. char *plain, *encrypted1, *encrypted2, *decrypted1, *decrypted2;
  614. char plain_1[1], plain_15[15], plain_16[16], plain_17[17];
  615. char key1[16], key2[16];
  616. ssize_t encrypted_size, decrypted_size;
  617. int use_evp = !strcmp(arg,"evp");
  618. evaluate_evp_for_aes(use_evp);
  619. plain = tor_malloc(4095);
  620. encrypted1 = tor_malloc(4095 + 1 + 16);
  621. encrypted2 = tor_malloc(4095 + 1 + 16);
  622. decrypted1 = tor_malloc(4095 + 1);
  623. decrypted2 = tor_malloc(4095 + 1);
  624. crypto_rand(plain, 4095);
  625. crypto_rand(key1, 16);
  626. crypto_rand(key2, 16);
  627. crypto_rand(plain_1, 1);
  628. crypto_rand(plain_15, 15);
  629. crypto_rand(plain_16, 16);
  630. crypto_rand(plain_17, 17);
  631. key1[0] = key2[0] + 128; /* Make sure that contents are different. */
  632. /* Encrypt and decrypt with the same key. */
  633. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 4095,
  634. plain, 4095);
  635. test_eq(encrypted_size, 16 + 4095);
  636. tt_assert(encrypted_size > 0); /* This is obviously true, since 4111 is
  637. * greater than 0, but its truth is not
  638. * obvious to all analysis tools. */
  639. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 4095,
  640. encrypted1, encrypted_size);
  641. test_eq(decrypted_size, 4095);
  642. tt_assert(decrypted_size > 0);
  643. test_memeq(plain, decrypted1, 4095);
  644. /* Encrypt a second time (with a new random initialization vector). */
  645. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted2, 16 + 4095,
  646. plain, 4095);
  647. test_eq(encrypted_size, 16 + 4095);
  648. tt_assert(encrypted_size > 0);
  649. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted2, 4095,
  650. encrypted2, encrypted_size);
  651. test_eq(decrypted_size, 4095);
  652. tt_assert(decrypted_size > 0);
  653. test_memeq(plain, decrypted2, 4095);
  654. test_memneq(encrypted1, encrypted2, encrypted_size);
  655. /* Decrypt with the wrong key. */
  656. decrypted_size = crypto_cipher_decrypt_with_iv(key2, decrypted2, 4095,
  657. encrypted1, encrypted_size);
  658. test_memneq(plain, decrypted2, encrypted_size);
  659. /* Alter the initialization vector. */
  660. encrypted1[0] += 42;
  661. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 4095,
  662. encrypted1, encrypted_size);
  663. test_memneq(plain, decrypted2, 4095);
  664. /* Special length case: 1. */
  665. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 1,
  666. plain_1, 1);
  667. test_eq(encrypted_size, 16 + 1);
  668. tt_assert(encrypted_size > 0);
  669. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 1,
  670. encrypted1, encrypted_size);
  671. test_eq(decrypted_size, 1);
  672. tt_assert(decrypted_size > 0);
  673. test_memeq(plain_1, decrypted1, 1);
  674. /* Special length case: 15. */
  675. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 15,
  676. plain_15, 15);
  677. test_eq(encrypted_size, 16 + 15);
  678. tt_assert(encrypted_size > 0);
  679. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 15,
  680. encrypted1, encrypted_size);
  681. test_eq(decrypted_size, 15);
  682. tt_assert(decrypted_size > 0);
  683. test_memeq(plain_15, decrypted1, 15);
  684. /* Special length case: 16. */
  685. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 16,
  686. plain_16, 16);
  687. test_eq(encrypted_size, 16 + 16);
  688. tt_assert(encrypted_size > 0);
  689. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 16,
  690. encrypted1, encrypted_size);
  691. test_eq(decrypted_size, 16);
  692. tt_assert(decrypted_size > 0);
  693. test_memeq(plain_16, decrypted1, 16);
  694. /* Special length case: 17. */
  695. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 17,
  696. plain_17, 17);
  697. test_eq(encrypted_size, 16 + 17);
  698. tt_assert(encrypted_size > 0);
  699. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 17,
  700. encrypted1, encrypted_size);
  701. test_eq(decrypted_size, 17);
  702. tt_assert(decrypted_size > 0);
  703. test_memeq(plain_17, decrypted1, 17);
  704. done:
  705. /* Free memory. */
  706. tor_free(plain);
  707. tor_free(encrypted1);
  708. tor_free(encrypted2);
  709. tor_free(decrypted1);
  710. tor_free(decrypted2);
  711. }
  712. /** Test base32 decoding. */
  713. static void
  714. test_crypto_base32_decode(void)
  715. {
  716. char plain[60], encoded[96 + 1], decoded[60];
  717. int res;
  718. crypto_rand(plain, 60);
  719. /* Encode and decode a random string. */
  720. base32_encode(encoded, 96 + 1, plain, 60);
  721. res = base32_decode(decoded, 60, encoded, 96);
  722. test_eq(res, 0);
  723. test_memeq(plain, decoded, 60);
  724. /* Encode, uppercase, and decode a random string. */
  725. base32_encode(encoded, 96 + 1, plain, 60);
  726. tor_strupper(encoded);
  727. res = base32_decode(decoded, 60, encoded, 96);
  728. test_eq(res, 0);
  729. test_memeq(plain, decoded, 60);
  730. /* Change encoded string and decode. */
  731. if (encoded[0] == 'A' || encoded[0] == 'a')
  732. encoded[0] = 'B';
  733. else
  734. encoded[0] = 'A';
  735. res = base32_decode(decoded, 60, encoded, 96);
  736. test_eq(res, 0);
  737. test_memneq(plain, decoded, 60);
  738. /* Bad encodings. */
  739. encoded[0] = '!';
  740. res = base32_decode(decoded, 60, encoded, 96);
  741. test_assert(res < 0);
  742. done:
  743. ;
  744. }
  745. static void *
  746. pass_data_setup_fn(const struct testcase_t *testcase)
  747. {
  748. return testcase->setup_data;
  749. }
  750. static int
  751. pass_data_cleanup_fn(const struct testcase_t *testcase, void *ptr)
  752. {
  753. (void)ptr;
  754. (void)testcase;
  755. return 1;
  756. }
  757. static const struct testcase_setup_t pass_data = {
  758. pass_data_setup_fn, pass_data_cleanup_fn
  759. };
  760. #define CRYPTO_LEGACY(name) \
  761. { #name, legacy_test_helper, 0, &legacy_setup, test_crypto_ ## name }
  762. struct testcase_t crypto_tests[] = {
  763. CRYPTO_LEGACY(formats),
  764. CRYPTO_LEGACY(rng),
  765. { "aes_AES", test_crypto_aes, TT_FORK, &pass_data, (void*)"aes" },
  766. { "aes_EVP", test_crypto_aes, TT_FORK, &pass_data, (void*)"evp" },
  767. CRYPTO_LEGACY(sha),
  768. CRYPTO_LEGACY(pk),
  769. CRYPTO_LEGACY(dh),
  770. CRYPTO_LEGACY(s2k),
  771. { "aes_iv_AES", test_crypto_aes_iv, TT_FORK, &pass_data, (void*)"aes" },
  772. { "aes_iv_EVP", test_crypto_aes_iv, TT_FORK, &pass_data, (void*)"evp" },
  773. CRYPTO_LEGACY(base32_decode),
  774. END_OF_TESTCASES
  775. };