test_crypto.c 30 KB

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