test_crypto.c 29 KB

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