test_crypto.c 30 KB

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