test_crypto.c 30 KB

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