test_crypto.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2013, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #define CRYPTO_PRIVATE
  7. #define CRYPTO_CURVE25519_PRIVATE
  8. #include "or.h"
  9. #include "test.h"
  10. #include "aes.h"
  11. #include "util.h"
  12. #ifdef CURVE25519_ENABLED
  13. #include "crypto_curve25519.h"
  14. #endif
  15. /** Run unit tests for Diffie-Hellman functionality. */
  16. static void
  17. test_crypto_dh(void)
  18. {
  19. crypto_dh_t *dh1 = crypto_dh_new(DH_TYPE_CIRCUIT);
  20. crypto_dh_t *dh2 = crypto_dh_new(DH_TYPE_CIRCUIT);
  21. char p1[DH_BYTES];
  22. char p2[DH_BYTES];
  23. char s1[DH_BYTES];
  24. char s2[DH_BYTES];
  25. ssize_t s1len, s2len;
  26. test_eq(crypto_dh_get_bytes(dh1), DH_BYTES);
  27. test_eq(crypto_dh_get_bytes(dh2), DH_BYTES);
  28. memset(p1, 0, DH_BYTES);
  29. memset(p2, 0, DH_BYTES);
  30. test_memeq(p1, p2, DH_BYTES);
  31. test_assert(! crypto_dh_get_public(dh1, p1, DH_BYTES));
  32. test_memneq(p1, p2, DH_BYTES);
  33. test_assert(! crypto_dh_get_public(dh2, p2, DH_BYTES));
  34. test_memneq(p1, p2, DH_BYTES);
  35. memset(s1, 0, DH_BYTES);
  36. memset(s2, 0xFF, DH_BYTES);
  37. s1len = crypto_dh_compute_secret(LOG_WARN, dh1, p2, DH_BYTES, s1, 50);
  38. s2len = crypto_dh_compute_secret(LOG_WARN, dh2, p1, DH_BYTES, s2, 50);
  39. test_assert(s1len > 0);
  40. test_eq(s1len, s2len);
  41. test_memeq(s1, s2, s1len);
  42. {
  43. /* XXXX Now fabricate some bad values and make sure they get caught,
  44. * Check 0, 1, N-1, >= N, etc.
  45. */
  46. }
  47. done:
  48. crypto_dh_free(dh1);
  49. crypto_dh_free(dh2);
  50. }
  51. /** Run unit tests for our random number generation function and its wrappers.
  52. */
  53. static void
  54. test_crypto_rng(void)
  55. {
  56. int i, j, allok;
  57. char data1[100], data2[100];
  58. double d;
  59. /* Try out RNG. */
  60. test_assert(! crypto_seed_rng(0));
  61. crypto_rand(data1, 100);
  62. crypto_rand(data2, 100);
  63. test_memneq(data1,data2,100);
  64. allok = 1;
  65. for (i = 0; i < 100; ++i) {
  66. uint64_t big;
  67. char *host;
  68. j = crypto_rand_int(100);
  69. if (j < 0 || j >= 100)
  70. allok = 0;
  71. big = crypto_rand_uint64(U64_LITERAL(1)<<40);
  72. if (big >= (U64_LITERAL(1)<<40))
  73. allok = 0;
  74. big = crypto_rand_uint64(U64_LITERAL(5));
  75. if (big >= 5)
  76. allok = 0;
  77. d = crypto_rand_double();
  78. test_assert(d >= 0);
  79. test_assert(d < 1.0);
  80. host = crypto_random_hostname(3,8,"www.",".onion");
  81. if (strcmpstart(host,"www.") ||
  82. strcmpend(host,".onion") ||
  83. strlen(host) < 13 ||
  84. strlen(host) > 18)
  85. allok = 0;
  86. tor_free(host);
  87. }
  88. test_assert(allok);
  89. done:
  90. ;
  91. }
  92. /** Run unit tests for our AES functionality */
  93. static void
  94. test_crypto_aes(void *arg)
  95. {
  96. char *data1 = NULL, *data2 = NULL, *data3 = NULL;
  97. crypto_cipher_t *env1 = NULL, *env2 = NULL;
  98. int i, j;
  99. char *mem_op_hex_tmp=NULL;
  100. int use_evp = !strcmp(arg,"evp");
  101. evaluate_evp_for_aes(use_evp);
  102. evaluate_ctr_for_aes();
  103. data1 = tor_malloc(1024);
  104. data2 = tor_malloc(1024);
  105. data3 = tor_malloc(1024);
  106. /* Now, test encryption and decryption with stream cipher. */
  107. data1[0]='\0';
  108. for (i = 1023; i>0; i -= 35)
  109. strncat(data1, "Now is the time for all good onions", i);
  110. memset(data2, 0, 1024);
  111. memset(data3, 0, 1024);
  112. env1 = crypto_cipher_new(NULL);
  113. test_neq_ptr(env1, 0);
  114. env2 = crypto_cipher_new(crypto_cipher_get_key(env1));
  115. test_neq_ptr(env2, 0);
  116. /* Try encrypting 512 chars. */
  117. crypto_cipher_encrypt(env1, data2, data1, 512);
  118. crypto_cipher_decrypt(env2, data3, data2, 512);
  119. test_memeq(data1, data3, 512);
  120. test_memneq(data1, data2, 512);
  121. /* Now encrypt 1 at a time, and get 1 at a time. */
  122. for (j = 512; j < 560; ++j) {
  123. crypto_cipher_encrypt(env1, data2+j, data1+j, 1);
  124. }
  125. for (j = 512; j < 560; ++j) {
  126. crypto_cipher_decrypt(env2, data3+j, data2+j, 1);
  127. }
  128. test_memeq(data1, data3, 560);
  129. /* Now encrypt 3 at a time, and get 5 at a time. */
  130. for (j = 560; j < 1024-5; j += 3) {
  131. crypto_cipher_encrypt(env1, data2+j, data1+j, 3);
  132. }
  133. for (j = 560; j < 1024-5; j += 5) {
  134. crypto_cipher_decrypt(env2, data3+j, data2+j, 5);
  135. }
  136. test_memeq(data1, data3, 1024-5);
  137. /* Now make sure that when we encrypt with different chunk sizes, we get
  138. the same results. */
  139. crypto_cipher_free(env2);
  140. env2 = NULL;
  141. memset(data3, 0, 1024);
  142. env2 = crypto_cipher_new(crypto_cipher_get_key(env1));
  143. test_neq_ptr(env2, NULL);
  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_cipher_free(env1);
  154. env1 = NULL;
  155. crypto_cipher_free(env2);
  156. env2 = NULL;
  157. /* NIST test vector for aes. */
  158. /* IV starts at 0 */
  159. env1 = crypto_cipher_new("\x80\x00\x00\x00\x00\x00\x00\x00"
  160. "\x00\x00\x00\x00\x00\x00\x00\x00");
  161. crypto_cipher_encrypt(env1, data1,
  162. "\x00\x00\x00\x00\x00\x00\x00\x00"
  163. "\x00\x00\x00\x00\x00\x00\x00\x00", 16);
  164. test_memeq_hex(data1, "0EDD33D3C621E546455BD8BA1418BEC8");
  165. /* Now test rollover. All these values are originally from a python
  166. * script. */
  167. crypto_cipher_free(env1);
  168. env1 = crypto_cipher_new_with_iv(
  169. "\x80\x00\x00\x00\x00\x00\x00\x00"
  170. "\x00\x00\x00\x00\x00\x00\x00\x00",
  171. "\x00\x00\x00\x00\x00\x00\x00\x00"
  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, "335fe6da56f843199066c14a00a40231"
  176. "cdd0b917dbc7186908a6bfb5ffd574d3");
  177. crypto_cipher_free(env1);
  178. env1 = crypto_cipher_new_with_iv(
  179. "\x80\x00\x00\x00\x00\x00\x00\x00"
  180. "\x00\x00\x00\x00\x00\x00\x00\x00",
  181. "\x00\x00\x00\x00\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, "e627c6423fa2d77832a02b2794094b73"
  186. "3e63c721df790d2c6469cc1953a3ffac");
  187. crypto_cipher_free(env1);
  188. env1 = crypto_cipher_new_with_iv(
  189. "\x80\x00\x00\x00\x00\x00\x00\x00"
  190. "\x00\x00\x00\x00\x00\x00\x00\x00",
  191. "\xff\xff\xff\xff\xff\xff\xff\xff"
  192. "\xff\xff\xff\xff\xff\xff\xff\xff");
  193. memset(data2, 0, 1024);
  194. crypto_cipher_encrypt(env1, data1, data2, 32);
  195. test_memeq_hex(data1, "2aed2bff0de54f9328efd070bf48f70a"
  196. "0EDD33D3C621E546455BD8BA1418BEC8");
  197. /* Now check rollover on inplace cipher. */
  198. crypto_cipher_free(env1);
  199. env1 = crypto_cipher_new_with_iv(
  200. "\x80\x00\x00\x00\x00\x00\x00\x00"
  201. "\x00\x00\x00\x00\x00\x00\x00\x00",
  202. "\xff\xff\xff\xff\xff\xff\xff\xff"
  203. "\xff\xff\xff\xff\xff\xff\xff\xff");
  204. crypto_cipher_crypt_inplace(env1, data2, 64);
  205. test_memeq_hex(data2, "2aed2bff0de54f9328efd070bf48f70a"
  206. "0EDD33D3C621E546455BD8BA1418BEC8"
  207. "93e2c5243d6839eac58503919192f7ae"
  208. "1908e67cafa08d508816659c2e693191");
  209. crypto_cipher_free(env1);
  210. env1 = crypto_cipher_new_with_iv(
  211. "\x80\x00\x00\x00\x00\x00\x00\x00"
  212. "\x00\x00\x00\x00\x00\x00\x00\x00",
  213. "\xff\xff\xff\xff\xff\xff\xff\xff"
  214. "\xff\xff\xff\xff\xff\xff\xff\xff");
  215. crypto_cipher_crypt_inplace(env1, data2, 64);
  216. test_assert(tor_mem_is_zero(data2, 64));
  217. done:
  218. tor_free(mem_op_hex_tmp);
  219. if (env1)
  220. crypto_cipher_free(env1);
  221. if (env2)
  222. crypto_cipher_free(env2);
  223. tor_free(data1);
  224. tor_free(data2);
  225. tor_free(data3);
  226. }
  227. /** Run unit tests for our SHA-1 functionality */
  228. static void
  229. test_crypto_sha(void)
  230. {
  231. crypto_digest_t *d1 = NULL, *d2 = NULL;
  232. int i;
  233. char key[160];
  234. char digest[32];
  235. char data[50];
  236. char d_out1[DIGEST_LEN], d_out2[DIGEST256_LEN];
  237. char *mem_op_hex_tmp=NULL;
  238. /* Test SHA-1 with a test vector from the specification. */
  239. i = crypto_digest(data, "abc", 3);
  240. test_memeq_hex(data, "A9993E364706816ABA3E25717850C26C9CD0D89D");
  241. tt_int_op(i, ==, 0);
  242. /* Test SHA-256 with a test vector from the specification. */
  243. i = crypto_digest256(data, "abc", 3, DIGEST_SHA256);
  244. test_memeq_hex(data, "BA7816BF8F01CFEA414140DE5DAE2223B00361A3"
  245. "96177A9CB410FF61F20015AD");
  246. tt_int_op(i, ==, 0);
  247. /* Test HMAC-SHA256 with test cases from wikipedia and RFC 4231 */
  248. /* Case empty (wikipedia) */
  249. crypto_hmac_sha256(digest, "", 0, "", 0);
  250. test_streq(hex_str(digest, 32),
  251. "B613679A0814D9EC772F95D778C35FC5FF1697C493715653C6C712144292C5AD");
  252. /* Case quick-brown (wikipedia) */
  253. crypto_hmac_sha256(digest, "key", 3,
  254. "The quick brown fox jumps over the lazy dog", 43);
  255. test_streq(hex_str(digest, 32),
  256. "F7BC83F430538424B13298E6AA6FB143EF4D59A14946175997479DBC2D1A3CD8");
  257. /* "Test Case 1" from RFC 4231 */
  258. memset(key, 0x0b, 20);
  259. crypto_hmac_sha256(digest, key, 20, "Hi There", 8);
  260. test_memeq_hex(digest,
  261. "b0344c61d8db38535ca8afceaf0bf12b"
  262. "881dc200c9833da726e9376c2e32cff7");
  263. /* "Test Case 2" from RFC 4231 */
  264. memset(key, 0x0b, 20);
  265. crypto_hmac_sha256(digest, "Jefe", 4, "what do ya want for nothing?", 28);
  266. test_memeq_hex(digest,
  267. "5bdcc146bf60754e6a042426089575c7"
  268. "5a003f089d2739839dec58b964ec3843");
  269. /* "Test case 3" from RFC 4231 */
  270. memset(key, 0xaa, 20);
  271. memset(data, 0xdd, 50);
  272. crypto_hmac_sha256(digest, key, 20, data, 50);
  273. test_memeq_hex(digest,
  274. "773ea91e36800e46854db8ebd09181a7"
  275. "2959098b3ef8c122d9635514ced565fe");
  276. /* "Test case 4" from RFC 4231 */
  277. base16_decode(key, 25,
  278. "0102030405060708090a0b0c0d0e0f10111213141516171819", 50);
  279. memset(data, 0xcd, 50);
  280. crypto_hmac_sha256(digest, key, 25, data, 50);
  281. test_memeq_hex(digest,
  282. "82558a389a443c0ea4cc819899f2083a"
  283. "85f0faa3e578f8077a2e3ff46729665b");
  284. /* "Test case 5" from RFC 4231 */
  285. memset(key, 0x0c, 20);
  286. crypto_hmac_sha256(digest, key, 20, "Test With Truncation", 20);
  287. test_memeq_hex(digest,
  288. "a3b6167473100ee06e0c796c2955552b");
  289. /* "Test case 6" from RFC 4231 */
  290. memset(key, 0xaa, 131);
  291. crypto_hmac_sha256(digest, key, 131,
  292. "Test Using Larger Than Block-Size Key - Hash Key First",
  293. 54);
  294. test_memeq_hex(digest,
  295. "60e431591ee0b67f0d8a26aacbf5b77f"
  296. "8e0bc6213728c5140546040f0ee37f54");
  297. /* "Test case 7" from RFC 4231 */
  298. memset(key, 0xaa, 131);
  299. crypto_hmac_sha256(digest, key, 131,
  300. "This is a test using a larger than block-size key and a "
  301. "larger than block-size data. The key needs to be hashed "
  302. "before being used by the HMAC algorithm.", 152);
  303. test_memeq_hex(digest,
  304. "9b09ffa71b942fcb27635fbcd5b0e944"
  305. "bfdc63644f0713938a7f51535c3a35e2");
  306. /* Incremental digest code. */
  307. d1 = crypto_digest_new();
  308. test_assert(d1);
  309. crypto_digest_add_bytes(d1, "abcdef", 6);
  310. d2 = crypto_digest_dup(d1);
  311. test_assert(d2);
  312. crypto_digest_add_bytes(d2, "ghijkl", 6);
  313. crypto_digest_get_digest(d2, d_out1, sizeof(d_out1));
  314. crypto_digest(d_out2, "abcdefghijkl", 12);
  315. test_memeq(d_out1, d_out2, DIGEST_LEN);
  316. crypto_digest_assign(d2, d1);
  317. crypto_digest_add_bytes(d2, "mno", 3);
  318. crypto_digest_get_digest(d2, d_out1, sizeof(d_out1));
  319. crypto_digest(d_out2, "abcdefmno", 9);
  320. test_memeq(d_out1, d_out2, DIGEST_LEN);
  321. crypto_digest_get_digest(d1, d_out1, sizeof(d_out1));
  322. crypto_digest(d_out2, "abcdef", 6);
  323. test_memeq(d_out1, d_out2, DIGEST_LEN);
  324. crypto_digest_free(d1);
  325. crypto_digest_free(d2);
  326. /* Incremental digest code with sha256 */
  327. d1 = crypto_digest256_new(DIGEST_SHA256);
  328. test_assert(d1);
  329. crypto_digest_add_bytes(d1, "abcdef", 6);
  330. d2 = crypto_digest_dup(d1);
  331. test_assert(d2);
  332. crypto_digest_add_bytes(d2, "ghijkl", 6);
  333. crypto_digest_get_digest(d2, d_out1, sizeof(d_out1));
  334. crypto_digest256(d_out2, "abcdefghijkl", 12, DIGEST_SHA256);
  335. test_memeq(d_out1, d_out2, DIGEST_LEN);
  336. crypto_digest_assign(d2, d1);
  337. crypto_digest_add_bytes(d2, "mno", 3);
  338. crypto_digest_get_digest(d2, d_out1, sizeof(d_out1));
  339. crypto_digest256(d_out2, "abcdefmno", 9, DIGEST_SHA256);
  340. test_memeq(d_out1, d_out2, DIGEST_LEN);
  341. crypto_digest_get_digest(d1, d_out1, sizeof(d_out1));
  342. crypto_digest256(d_out2, "abcdef", 6, DIGEST_SHA256);
  343. test_memeq(d_out1, d_out2, DIGEST_LEN);
  344. done:
  345. if (d1)
  346. crypto_digest_free(d1);
  347. if (d2)
  348. crypto_digest_free(d2);
  349. tor_free(mem_op_hex_tmp);
  350. }
  351. /** Run unit tests for our public key crypto functions */
  352. static void
  353. test_crypto_pk(void)
  354. {
  355. crypto_pk_t *pk1 = NULL, *pk2 = NULL;
  356. char *encoded = NULL;
  357. char data1[1024], data2[1024], data3[1024];
  358. size_t size;
  359. int i, j, p, len;
  360. /* Public-key ciphers */
  361. pk1 = pk_generate(0);
  362. pk2 = crypto_pk_new();
  363. test_assert(pk1 && pk2);
  364. test_assert(! crypto_pk_write_public_key_to_string(pk1, &encoded, &size));
  365. test_assert(! crypto_pk_read_public_key_from_string(pk2, encoded, size));
  366. test_eq(0, crypto_pk_cmp_keys(pk1, pk2));
  367. /* comparison between keys and NULL */
  368. tt_int_op(crypto_pk_cmp_keys(NULL, pk1), <, 0);
  369. tt_int_op(crypto_pk_cmp_keys(NULL, NULL), ==, 0);
  370. tt_int_op(crypto_pk_cmp_keys(pk1, NULL), >, 0);
  371. test_eq(128, crypto_pk_keysize(pk1));
  372. test_eq(1024, crypto_pk_num_bits(pk1));
  373. test_eq(128, crypto_pk_keysize(pk2));
  374. test_eq(1024, crypto_pk_num_bits(pk2));
  375. test_eq(128, crypto_pk_public_encrypt(pk2, data1, sizeof(data1),
  376. "Hello whirled.", 15,
  377. PK_PKCS1_OAEP_PADDING));
  378. test_eq(128, crypto_pk_public_encrypt(pk1, data2, sizeof(data1),
  379. "Hello whirled.", 15,
  380. PK_PKCS1_OAEP_PADDING));
  381. /* oaep padding should make encryption not match */
  382. test_memneq(data1, data2, 128);
  383. test_eq(15, crypto_pk_private_decrypt(pk1, data3, sizeof(data3), data1, 128,
  384. PK_PKCS1_OAEP_PADDING,1));
  385. test_streq(data3, "Hello whirled.");
  386. memset(data3, 0, 1024);
  387. test_eq(15, crypto_pk_private_decrypt(pk1, data3, sizeof(data3), data2, 128,
  388. PK_PKCS1_OAEP_PADDING,1));
  389. test_streq(data3, "Hello whirled.");
  390. /* Can't decrypt with public key. */
  391. test_eq(-1, crypto_pk_private_decrypt(pk2, data3, sizeof(data3), data2, 128,
  392. PK_PKCS1_OAEP_PADDING,1));
  393. /* Try again with bad padding */
  394. memcpy(data2+1, "XYZZY", 5); /* This has fails ~ once-in-2^40 */
  395. test_eq(-1, crypto_pk_private_decrypt(pk1, data3, sizeof(data3), data2, 128,
  396. PK_PKCS1_OAEP_PADDING,1));
  397. /* File operations: save and load private key */
  398. test_assert(! crypto_pk_write_private_key_to_filename(pk1,
  399. get_fname("pkey1")));
  400. /* failing case for read: can't read. */
  401. test_assert(crypto_pk_read_private_key_from_filename(pk2,
  402. get_fname("xyzzy")) < 0);
  403. write_str_to_file(get_fname("xyzzy"), "foobar", 6);
  404. /* Failing case for read: no key. */
  405. test_assert(crypto_pk_read_private_key_from_filename(pk2,
  406. get_fname("xyzzy")) < 0);
  407. test_assert(! crypto_pk_read_private_key_from_filename(pk2,
  408. get_fname("pkey1")));
  409. test_eq(15, crypto_pk_private_decrypt(pk2, data3, sizeof(data3), data1, 128,
  410. PK_PKCS1_OAEP_PADDING,1));
  411. /* Now try signing. */
  412. strlcpy(data1, "Ossifrage", 1024);
  413. test_eq(128, crypto_pk_private_sign(pk1, data2, sizeof(data2), data1, 10));
  414. test_eq(10,
  415. crypto_pk_public_checksig(pk1, data3, sizeof(data3), data2, 128));
  416. test_streq(data3, "Ossifrage");
  417. /* Try signing digests. */
  418. test_eq(128, crypto_pk_private_sign_digest(pk1, data2, sizeof(data2),
  419. data1, 10));
  420. test_eq(20,
  421. crypto_pk_public_checksig(pk1, data3, sizeof(data3), data2, 128));
  422. test_eq(0, crypto_pk_public_checksig_digest(pk1, data1, 10, data2, 128));
  423. test_eq(-1, crypto_pk_public_checksig_digest(pk1, data1, 11, data2, 128));
  424. /*XXXX test failed signing*/
  425. /* Try encoding */
  426. crypto_pk_free(pk2);
  427. pk2 = NULL;
  428. i = crypto_pk_asn1_encode(pk1, data1, 1024);
  429. test_assert(i>0);
  430. pk2 = crypto_pk_asn1_decode(data1, i);
  431. test_assert(crypto_pk_cmp_keys(pk1,pk2) == 0);
  432. /* Try with hybrid encryption wrappers. */
  433. crypto_rand(data1, 1024);
  434. for (i = 0; i < 2; ++i) {
  435. for (j = 85; j < 140; ++j) {
  436. memset(data2,0,1024);
  437. memset(data3,0,1024);
  438. p = (i==0)?PK_PKCS1_PADDING:PK_PKCS1_OAEP_PADDING;
  439. len = crypto_pk_public_hybrid_encrypt(pk1,data2,sizeof(data2),
  440. data1,j,p,0);
  441. test_assert(len>=0);
  442. len = crypto_pk_private_hybrid_decrypt(pk1,data3,sizeof(data3),
  443. data2,len,p,1);
  444. test_eq(len,j);
  445. test_memeq(data1,data3,j);
  446. }
  447. }
  448. /* Try copy_full */
  449. crypto_pk_free(pk2);
  450. pk2 = crypto_pk_copy_full(pk1);
  451. test_assert(pk2 != NULL);
  452. test_neq_ptr(pk1, pk2);
  453. test_assert(crypto_pk_cmp_keys(pk1,pk2) == 0);
  454. done:
  455. if (pk1)
  456. crypto_pk_free(pk1);
  457. if (pk2)
  458. crypto_pk_free(pk2);
  459. tor_free(encoded);
  460. }
  461. /** Run unit tests for misc crypto formatting functionality (base64, base32,
  462. * fingerprints, etc) */
  463. static void
  464. test_crypto_formats(void)
  465. {
  466. char *data1 = NULL, *data2 = NULL, *data3 = NULL;
  467. int i, j, idx;
  468. data1 = tor_malloc(1024);
  469. data2 = tor_malloc(1024);
  470. data3 = tor_malloc(1024);
  471. test_assert(data1 && data2 && data3);
  472. /* Base64 tests */
  473. memset(data1, 6, 1024);
  474. for (idx = 0; idx < 10; ++idx) {
  475. i = base64_encode(data2, 1024, data1, idx);
  476. test_assert(i >= 0);
  477. j = base64_decode(data3, 1024, data2, i);
  478. test_eq(j,idx);
  479. test_memeq(data3, data1, idx);
  480. }
  481. strlcpy(data1, "Test string that contains 35 chars.", 1024);
  482. strlcat(data1, " 2nd string that contains 35 chars.", 1024);
  483. i = base64_encode(data2, 1024, data1, 71);
  484. test_assert(i >= 0);
  485. j = base64_decode(data3, 1024, data2, i);
  486. test_eq(j, 71);
  487. test_streq(data3, data1);
  488. test_assert(data2[i] == '\0');
  489. crypto_rand(data1, DIGEST_LEN);
  490. memset(data2, 100, 1024);
  491. digest_to_base64(data2, data1);
  492. test_eq(BASE64_DIGEST_LEN, strlen(data2));
  493. test_eq(100, data2[BASE64_DIGEST_LEN+2]);
  494. memset(data3, 99, 1024);
  495. test_eq(digest_from_base64(data3, data2), 0);
  496. test_memeq(data1, data3, DIGEST_LEN);
  497. test_eq(99, data3[DIGEST_LEN+1]);
  498. test_assert(digest_from_base64(data3, "###") < 0);
  499. /* Encoding SHA256 */
  500. crypto_rand(data2, DIGEST256_LEN);
  501. memset(data2, 100, 1024);
  502. digest256_to_base64(data2, data1);
  503. test_eq(BASE64_DIGEST256_LEN, strlen(data2));
  504. test_eq(100, data2[BASE64_DIGEST256_LEN+2]);
  505. memset(data3, 99, 1024);
  506. test_eq(digest256_from_base64(data3, data2), 0);
  507. test_memeq(data1, data3, DIGEST256_LEN);
  508. test_eq(99, data3[DIGEST256_LEN+1]);
  509. /* Base32 tests */
  510. strlcpy(data1, "5chrs", 1024);
  511. /* bit pattern is: [35 63 68 72 73] ->
  512. * [00110101 01100011 01101000 01110010 01110011]
  513. * By 5s: [00110 10101 10001 10110 10000 11100 10011 10011]
  514. */
  515. base32_encode(data2, 9, data1, 5);
  516. test_streq(data2, "gvrwq4tt");
  517. strlcpy(data1, "\xFF\xF5\x6D\x44\xAE\x0D\x5C\xC9\x62\xC4", 1024);
  518. base32_encode(data2, 30, data1, 10);
  519. test_streq(data2, "772w2rfobvomsywe");
  520. /* Base16 tests */
  521. strlcpy(data1, "6chrs\xff", 1024);
  522. base16_encode(data2, 13, data1, 6);
  523. test_streq(data2, "3663687273FF");
  524. strlcpy(data1, "f0d678affc000100", 1024);
  525. i = base16_decode(data2, 8, data1, 16);
  526. test_eq(i,0);
  527. test_memeq(data2, "\xf0\xd6\x78\xaf\xfc\x00\x01\x00",8);
  528. /* now try some failing base16 decodes */
  529. test_eq(-1, base16_decode(data2, 8, data1, 15)); /* odd input len */
  530. test_eq(-1, base16_decode(data2, 7, data1, 16)); /* dest too short */
  531. strlcpy(data1, "f0dz!8affc000100", 1024);
  532. test_eq(-1, base16_decode(data2, 8, data1, 16));
  533. tor_free(data1);
  534. tor_free(data2);
  535. tor_free(data3);
  536. /* Add spaces to fingerprint */
  537. {
  538. data1 = tor_strdup("ABCD1234ABCD56780000ABCD1234ABCD56780000");
  539. test_eq(strlen(data1), 40);
  540. data2 = tor_malloc(FINGERPRINT_LEN+1);
  541. add_spaces_to_fp(data2, FINGERPRINT_LEN+1, data1);
  542. test_streq(data2, "ABCD 1234 ABCD 5678 0000 ABCD 1234 ABCD 5678 0000");
  543. tor_free(data1);
  544. tor_free(data2);
  545. }
  546. done:
  547. tor_free(data1);
  548. tor_free(data2);
  549. tor_free(data3);
  550. }
  551. /** Run unit tests for our secret-to-key passphrase hashing functionality. */
  552. static void
  553. test_crypto_s2k(void)
  554. {
  555. char buf[29];
  556. char buf2[29];
  557. char *buf3 = NULL;
  558. int i;
  559. memset(buf, 0, sizeof(buf));
  560. memset(buf2, 0, sizeof(buf2));
  561. buf3 = tor_malloc(65536);
  562. memset(buf3, 0, 65536);
  563. secret_to_key(buf+9, 20, "", 0, buf);
  564. crypto_digest(buf2+9, buf3, 1024);
  565. test_memeq(buf, buf2, 29);
  566. memcpy(buf,"vrbacrda",8);
  567. memcpy(buf2,"vrbacrda",8);
  568. buf[8] = 96;
  569. buf2[8] = 96;
  570. secret_to_key(buf+9, 20, "12345678", 8, buf);
  571. for (i = 0; i < 65536; i += 16) {
  572. memcpy(buf3+i, "vrbacrda12345678", 16);
  573. }
  574. crypto_digest(buf2+9, buf3, 65536);
  575. test_memeq(buf, buf2, 29);
  576. done:
  577. tor_free(buf3);
  578. }
  579. /** Test AES-CTR encryption and decryption with IV. */
  580. static void
  581. test_crypto_aes_iv(void *arg)
  582. {
  583. char *plain, *encrypted1, *encrypted2, *decrypted1, *decrypted2;
  584. char plain_1[1], plain_15[15], plain_16[16], plain_17[17];
  585. char key1[16], key2[16];
  586. ssize_t encrypted_size, decrypted_size;
  587. int use_evp = !strcmp(arg,"evp");
  588. evaluate_evp_for_aes(use_evp);
  589. plain = tor_malloc(4095);
  590. encrypted1 = tor_malloc(4095 + 1 + 16);
  591. encrypted2 = tor_malloc(4095 + 1 + 16);
  592. decrypted1 = tor_malloc(4095 + 1);
  593. decrypted2 = tor_malloc(4095 + 1);
  594. crypto_rand(plain, 4095);
  595. crypto_rand(key1, 16);
  596. crypto_rand(key2, 16);
  597. crypto_rand(plain_1, 1);
  598. crypto_rand(plain_15, 15);
  599. crypto_rand(plain_16, 16);
  600. crypto_rand(plain_17, 17);
  601. key1[0] = key2[0] + 128; /* Make sure that contents are different. */
  602. /* Encrypt and decrypt with the same key. */
  603. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 4095,
  604. plain, 4095);
  605. test_eq(encrypted_size, 16 + 4095);
  606. tt_assert(encrypted_size > 0); /* This is obviously true, since 4111 is
  607. * greater than 0, but its truth is not
  608. * obvious to all analysis tools. */
  609. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 4095,
  610. encrypted1, encrypted_size);
  611. test_eq(decrypted_size, 4095);
  612. tt_assert(decrypted_size > 0);
  613. test_memeq(plain, decrypted1, 4095);
  614. /* Encrypt a second time (with a new random initialization vector). */
  615. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted2, 16 + 4095,
  616. plain, 4095);
  617. test_eq(encrypted_size, 16 + 4095);
  618. tt_assert(encrypted_size > 0);
  619. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted2, 4095,
  620. encrypted2, encrypted_size);
  621. test_eq(decrypted_size, 4095);
  622. tt_assert(decrypted_size > 0);
  623. test_memeq(plain, decrypted2, 4095);
  624. test_memneq(encrypted1, encrypted2, encrypted_size);
  625. /* Decrypt with the wrong key. */
  626. decrypted_size = crypto_cipher_decrypt_with_iv(key2, decrypted2, 4095,
  627. encrypted1, encrypted_size);
  628. test_memneq(plain, decrypted2, decrypted_size);
  629. /* Alter the initialization vector. */
  630. encrypted1[0] += 42;
  631. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 4095,
  632. encrypted1, encrypted_size);
  633. test_memneq(plain, decrypted2, 4095);
  634. /* Special length case: 1. */
  635. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 1,
  636. plain_1, 1);
  637. test_eq(encrypted_size, 16 + 1);
  638. tt_assert(encrypted_size > 0);
  639. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 1,
  640. encrypted1, encrypted_size);
  641. test_eq(decrypted_size, 1);
  642. tt_assert(decrypted_size > 0);
  643. test_memeq(plain_1, decrypted1, 1);
  644. /* Special length case: 15. */
  645. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 15,
  646. plain_15, 15);
  647. test_eq(encrypted_size, 16 + 15);
  648. tt_assert(encrypted_size > 0);
  649. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 15,
  650. encrypted1, encrypted_size);
  651. test_eq(decrypted_size, 15);
  652. tt_assert(decrypted_size > 0);
  653. test_memeq(plain_15, decrypted1, 15);
  654. /* Special length case: 16. */
  655. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 16,
  656. plain_16, 16);
  657. test_eq(encrypted_size, 16 + 16);
  658. tt_assert(encrypted_size > 0);
  659. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 16,
  660. encrypted1, encrypted_size);
  661. test_eq(decrypted_size, 16);
  662. tt_assert(decrypted_size > 0);
  663. test_memeq(plain_16, decrypted1, 16);
  664. /* Special length case: 17. */
  665. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 17,
  666. plain_17, 17);
  667. test_eq(encrypted_size, 16 + 17);
  668. tt_assert(encrypted_size > 0);
  669. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 17,
  670. encrypted1, encrypted_size);
  671. test_eq(decrypted_size, 17);
  672. tt_assert(decrypted_size > 0);
  673. test_memeq(plain_17, decrypted1, 17);
  674. done:
  675. /* Free memory. */
  676. tor_free(plain);
  677. tor_free(encrypted1);
  678. tor_free(encrypted2);
  679. tor_free(decrypted1);
  680. tor_free(decrypted2);
  681. }
  682. /** Test base32 decoding. */
  683. static void
  684. test_crypto_base32_decode(void)
  685. {
  686. char plain[60], encoded[96 + 1], decoded[60];
  687. int res;
  688. crypto_rand(plain, 60);
  689. /* Encode and decode a random string. */
  690. base32_encode(encoded, 96 + 1, plain, 60);
  691. res = base32_decode(decoded, 60, encoded, 96);
  692. test_eq(res, 0);
  693. test_memeq(plain, decoded, 60);
  694. /* Encode, uppercase, and decode a random string. */
  695. base32_encode(encoded, 96 + 1, plain, 60);
  696. tor_strupper(encoded);
  697. res = base32_decode(decoded, 60, encoded, 96);
  698. test_eq(res, 0);
  699. test_memeq(plain, decoded, 60);
  700. /* Change encoded string and decode. */
  701. if (encoded[0] == 'A' || encoded[0] == 'a')
  702. encoded[0] = 'B';
  703. else
  704. encoded[0] = 'A';
  705. res = base32_decode(decoded, 60, encoded, 96);
  706. test_eq(res, 0);
  707. test_memneq(plain, decoded, 60);
  708. /* Bad encodings. */
  709. encoded[0] = '!';
  710. res = base32_decode(decoded, 60, encoded, 96);
  711. test_assert(res < 0);
  712. done:
  713. ;
  714. }
  715. static void
  716. test_crypto_kdf_TAP(void *arg)
  717. {
  718. uint8_t key_material[100];
  719. int r;
  720. char *mem_op_hex_tmp = NULL;
  721. (void)arg;
  722. #define EXPAND(s) \
  723. r = crypto_expand_key_material_TAP( \
  724. (const uint8_t*)(s), strlen(s), \
  725. key_material, 100)
  726. /* Test vectors generated with a little python script; feel free to write
  727. * your own. */
  728. memset(key_material, 0, sizeof(key_material));
  729. EXPAND("");
  730. tt_int_op(r, ==, 0);
  731. test_memeq_hex(key_material,
  732. "5ba93c9db0cff93f52b521d7420e43f6eda2784fbf8b4530d8"
  733. "d246dd74ac53a13471bba17941dff7c4ea21bb365bbeeaf5f2"
  734. "c654883e56d11e43c44e9842926af7ca0a8cca12604f945414"
  735. "f07b01e13da42c6cf1de3abfdea9b95f34687cbbe92b9a7383");
  736. EXPAND("Tor");
  737. tt_int_op(r, ==, 0);
  738. test_memeq_hex(key_material,
  739. "776c6214fc647aaa5f683c737ee66ec44f03d0372e1cce6922"
  740. "7950f236ddf1e329a7ce7c227903303f525a8c6662426e8034"
  741. "870642a6dabbd41b5d97ec9bf2312ea729992f48f8ea2d0ba8"
  742. "3f45dfda1a80bdc8b80de01b23e3e0ffae099b3e4ccf28dc28");
  743. EXPAND("AN ALARMING ITEM TO FIND ON A MONTHLY AUTO-DEBIT NOTICE");
  744. tt_int_op(r, ==, 0);
  745. test_memeq_hex(key_material,
  746. "a340b5d126086c3ab29c2af4179196dbf95e1c72431419d331"
  747. "4844bf8f6afb6098db952b95581fb6c33625709d6f4400b8e7"
  748. "ace18a70579fad83c0982ef73f89395bcc39493ad53a685854"
  749. "daf2ba9b78733b805d9a6824c907ee1dba5ac27a1e466d4d10");
  750. done:
  751. tor_free(mem_op_hex_tmp);
  752. #undef EXPAND
  753. }
  754. static void
  755. test_crypto_hkdf_sha256(void *arg)
  756. {
  757. uint8_t key_material[100];
  758. const uint8_t salt[] = "ntor-curve25519-sha256-1:key_extract";
  759. const size_t salt_len = strlen((char*)salt);
  760. const uint8_t m_expand[] = "ntor-curve25519-sha256-1:key_expand";
  761. const size_t m_expand_len = strlen((char*)m_expand);
  762. int r;
  763. char *mem_op_hex_tmp = NULL;
  764. (void)arg;
  765. #define EXPAND(s) \
  766. r = crypto_expand_key_material_rfc5869_sha256( \
  767. (const uint8_t*)(s), strlen(s), \
  768. salt, salt_len, \
  769. m_expand, m_expand_len, \
  770. key_material, 100)
  771. /* Test vectors generated with ntor_ref.py */
  772. memset(key_material, 0, sizeof(key_material));
  773. EXPAND("");
  774. tt_int_op(r, ==, 0);
  775. test_memeq_hex(key_material,
  776. "d3490ed48b12a48f9547861583573fe3f19aafe3f81dc7fc75"
  777. "eeed96d741b3290f941576c1f9f0b2d463d1ec7ab2c6bf71cd"
  778. "d7f826c6298c00dbfe6711635d7005f0269493edf6046cc7e7"
  779. "dcf6abe0d20c77cf363e8ffe358927817a3d3e73712cee28d8");
  780. EXPAND("Tor");
  781. tt_int_op(r, ==, 0);
  782. test_memeq_hex(key_material,
  783. "5521492a85139a8d9107a2d5c0d9c91610d0f95989975ebee6"
  784. "c02a4f8d622a6cfdf9b7c7edd3832e2760ded1eac309b76f8d"
  785. "66c4a3c4d6225429b3a016e3c3d45911152fc87bc2de9630c3"
  786. "961be9fdb9f93197ea8e5977180801926d3321fa21513e59ac");
  787. EXPAND("AN ALARMING ITEM TO FIND ON YOUR CREDIT-RATING STATEMENT");
  788. tt_int_op(r, ==, 0);
  789. test_memeq_hex(key_material,
  790. "a2aa9b50da7e481d30463adb8f233ff06e9571a0ca6ab6df0f"
  791. "b206fa34e5bc78d063fc291501beec53b36e5a0e434561200c"
  792. "5f8bd13e0f88b3459600b4dc21d69363e2895321c06184879d"
  793. "94b18f078411be70b767c7fc40679a9440a0c95ea83a23efbf");
  794. done:
  795. tor_free(mem_op_hex_tmp);
  796. #undef EXPAND
  797. }
  798. #ifdef CURVE25519_ENABLED
  799. static void
  800. test_crypto_curve25519_impl(void *arg)
  801. {
  802. /* adapted from curve25519_donna, which adapted it from test-curve25519
  803. version 20050915, by D. J. Bernstein, Public domain. */
  804. const int randomize_high_bit = (arg != NULL);
  805. #ifdef SLOW_CURVE25519_TEST
  806. const int loop_max=10000;
  807. const char e1_expected[] = "4faf81190869fd742a33691b0e0824d5"
  808. "7e0329f4dd2819f5f32d130f1296b500";
  809. const char e2k_expected[] = "05aec13f92286f3a781ccae98995a3b9"
  810. "e0544770bc7de853b38f9100489e3e79";
  811. const char e1e2k_expected[] = "cd6e8269104eb5aaee886bd2071fba88"
  812. "bd13861475516bc2cd2b6e005e805064";
  813. #else
  814. const int loop_max=200;
  815. const char e1_expected[] = "bc7112cde03f97ef7008cad1bdc56be3"
  816. "c6a1037d74cceb3712e9206871dcf654";
  817. const char e2k_expected[] = "dd8fa254fb60bdb5142fe05b1f5de44d"
  818. "8e3ee1a63c7d14274ea5d4c67f065467";
  819. const char e1e2k_expected[] = "7ddb98bd89025d2347776b33901b3e7e"
  820. "c0ee98cb2257a4545c0cfb2ca3e1812b";
  821. #endif
  822. unsigned char e1k[32];
  823. unsigned char e2k[32];
  824. unsigned char e1e2k[32];
  825. unsigned char e2e1k[32];
  826. unsigned char e1[32] = {3};
  827. unsigned char e2[32] = {5};
  828. unsigned char k[32] = {9};
  829. int loop, i;
  830. char *mem_op_hex_tmp = NULL;
  831. for (loop = 0; loop < loop_max; ++loop) {
  832. curve25519_impl(e1k,e1,k);
  833. curve25519_impl(e2e1k,e2,e1k);
  834. curve25519_impl(e2k,e2,k);
  835. if (randomize_high_bit) {
  836. /* We require that the high bit of the public key be ignored. So if
  837. * we're doing this variant test, we randomize the high bit of e2k, and
  838. * make sure that the handshake still works out the same as it would
  839. * otherwise. */
  840. uint8_t byte;
  841. crypto_rand((char*)&byte, 1);
  842. e2k[31] |= (byte & 0x80);
  843. }
  844. curve25519_impl(e1e2k,e1,e2k);
  845. test_memeq(e1e2k, e2e1k, 32);
  846. if (loop == loop_max-1) {
  847. break;
  848. }
  849. for (i = 0;i < 32;++i) e1[i] ^= e2k[i];
  850. for (i = 0;i < 32;++i) e2[i] ^= e1k[i];
  851. for (i = 0;i < 32;++i) k[i] ^= e1e2k[i];
  852. }
  853. test_memeq_hex(e1, e1_expected);
  854. test_memeq_hex(e2k, e2k_expected);
  855. test_memeq_hex(e1e2k, e1e2k_expected);
  856. done:
  857. tor_free(mem_op_hex_tmp);
  858. }
  859. static void
  860. test_crypto_curve25519_wrappers(void *arg)
  861. {
  862. curve25519_public_key_t pubkey1, pubkey2;
  863. curve25519_secret_key_t seckey1, seckey2;
  864. uint8_t output1[CURVE25519_OUTPUT_LEN];
  865. uint8_t output2[CURVE25519_OUTPUT_LEN];
  866. (void)arg;
  867. /* Test a simple handshake, serializing and deserializing some stuff. */
  868. curve25519_secret_key_generate(&seckey1, 0);
  869. curve25519_secret_key_generate(&seckey2, 1);
  870. curve25519_public_key_generate(&pubkey1, &seckey1);
  871. curve25519_public_key_generate(&pubkey2, &seckey2);
  872. test_assert(curve25519_public_key_is_ok(&pubkey1));
  873. test_assert(curve25519_public_key_is_ok(&pubkey2));
  874. curve25519_handshake(output1, &seckey1, &pubkey2);
  875. curve25519_handshake(output2, &seckey2, &pubkey1);
  876. test_memeq(output1, output2, sizeof(output1));
  877. done:
  878. ;
  879. }
  880. static void
  881. test_crypto_curve25519_encode(void *arg)
  882. {
  883. curve25519_secret_key_t seckey;
  884. curve25519_public_key_t key1, key2, key3;
  885. char buf[64];
  886. (void)arg;
  887. curve25519_secret_key_generate(&seckey, 0);
  888. curve25519_public_key_generate(&key1, &seckey);
  889. tt_int_op(0, ==, curve25519_public_to_base64(buf, &key1));
  890. tt_int_op(CURVE25519_BASE64_PADDED_LEN, ==, strlen(buf));
  891. tt_int_op(0, ==, curve25519_public_from_base64(&key2, buf));
  892. test_memeq(key1.public_key, key2.public_key, CURVE25519_PUBKEY_LEN);
  893. buf[CURVE25519_BASE64_PADDED_LEN - 1] = '\0';
  894. tt_int_op(CURVE25519_BASE64_PADDED_LEN-1, ==, strlen(buf));
  895. tt_int_op(0, ==, curve25519_public_from_base64(&key3, buf));
  896. test_memeq(key1.public_key, key3.public_key, CURVE25519_PUBKEY_LEN);
  897. /* Now try bogus parses. */
  898. strlcpy(buf, "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$=", sizeof(buf));
  899. tt_int_op(-1, ==, curve25519_public_from_base64(&key3, buf));
  900. strlcpy(buf, "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$", sizeof(buf));
  901. tt_int_op(-1, ==, curve25519_public_from_base64(&key3, buf));
  902. strlcpy(buf, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", sizeof(buf));
  903. tt_int_op(-1, ==, curve25519_public_from_base64(&key3, buf));
  904. done:
  905. ;
  906. }
  907. static void
  908. test_crypto_curve25519_persist(void *arg)
  909. {
  910. curve25519_keypair_t keypair, keypair2;
  911. char *fname = tor_strdup(get_fname("curve25519_keypair"));
  912. char *tag = NULL;
  913. char *content = NULL;
  914. const char *cp;
  915. struct stat st;
  916. size_t taglen;
  917. (void)arg;
  918. tt_int_op(0,==,curve25519_keypair_generate(&keypair, 0));
  919. tt_int_op(0,==,curve25519_keypair_write_to_file(&keypair, fname, "testing"));
  920. tt_int_op(0,==,curve25519_keypair_read_from_file(&keypair2, &tag, fname));
  921. tt_str_op(tag,==,"testing");
  922. tor_free(tag);
  923. test_memeq(keypair.pubkey.public_key,
  924. keypair2.pubkey.public_key,
  925. CURVE25519_PUBKEY_LEN);
  926. test_memeq(keypair.seckey.secret_key,
  927. keypair2.seckey.secret_key,
  928. CURVE25519_SECKEY_LEN);
  929. content = read_file_to_str(fname, RFTS_BIN, &st);
  930. tt_assert(content);
  931. taglen = strlen("== c25519v1: testing ==");
  932. tt_int_op(st.st_size, ==, 32+CURVE25519_PUBKEY_LEN+CURVE25519_SECKEY_LEN);
  933. tt_assert(fast_memeq(content, "== c25519v1: testing ==", taglen));
  934. tt_assert(tor_mem_is_zero(content+taglen, 32-taglen));
  935. cp = content + 32;
  936. test_memeq(keypair.seckey.secret_key,
  937. cp,
  938. CURVE25519_SECKEY_LEN);
  939. cp += CURVE25519_SECKEY_LEN;
  940. test_memeq(keypair.pubkey.public_key,
  941. cp,
  942. CURVE25519_SECKEY_LEN);
  943. tor_free(fname);
  944. fname = tor_strdup(get_fname("bogus_keypair"));
  945. tt_int_op(-1, ==, curve25519_keypair_read_from_file(&keypair2, &tag, fname));
  946. tor_free(tag);
  947. content[69] ^= 0xff;
  948. tt_int_op(0, ==, write_bytes_to_file(fname, content, (size_t)st.st_size, 1));
  949. tt_int_op(-1, ==, curve25519_keypair_read_from_file(&keypair2, &tag, fname));
  950. done:
  951. tor_free(fname);
  952. tor_free(content);
  953. tor_free(tag);
  954. }
  955. #endif
  956. static void *
  957. pass_data_setup_fn(const struct testcase_t *testcase)
  958. {
  959. return testcase->setup_data;
  960. }
  961. static int
  962. pass_data_cleanup_fn(const struct testcase_t *testcase, void *ptr)
  963. {
  964. (void)ptr;
  965. (void)testcase;
  966. return 1;
  967. }
  968. static const struct testcase_setup_t pass_data = {
  969. pass_data_setup_fn, pass_data_cleanup_fn
  970. };
  971. #define CRYPTO_LEGACY(name) \
  972. { #name, legacy_test_helper, 0, &legacy_setup, test_crypto_ ## name }
  973. struct testcase_t crypto_tests[] = {
  974. CRYPTO_LEGACY(formats),
  975. CRYPTO_LEGACY(rng),
  976. { "aes_AES", test_crypto_aes, TT_FORK, &pass_data, (void*)"aes" },
  977. { "aes_EVP", test_crypto_aes, TT_FORK, &pass_data, (void*)"evp" },
  978. CRYPTO_LEGACY(sha),
  979. CRYPTO_LEGACY(pk),
  980. CRYPTO_LEGACY(dh),
  981. CRYPTO_LEGACY(s2k),
  982. { "aes_iv_AES", test_crypto_aes_iv, TT_FORK, &pass_data, (void*)"aes" },
  983. { "aes_iv_EVP", test_crypto_aes_iv, TT_FORK, &pass_data, (void*)"evp" },
  984. CRYPTO_LEGACY(base32_decode),
  985. { "kdf_TAP", test_crypto_kdf_TAP, 0, NULL, NULL },
  986. { "hkdf_sha256", test_crypto_hkdf_sha256, 0, NULL, NULL },
  987. #ifdef CURVE25519_ENABLED
  988. { "curve25519_impl", test_crypto_curve25519_impl, 0, NULL, NULL },
  989. { "curve25519_impl_hibit", test_crypto_curve25519_impl, 0, NULL, (void*)"y"},
  990. { "curve25519_wrappers", test_crypto_curve25519_wrappers, 0, NULL, NULL },
  991. { "curve25519_encode", test_crypto_curve25519_encode, 0, NULL, NULL },
  992. { "curve25519_persist", test_crypto_curve25519_persist, 0, NULL, NULL },
  993. #endif
  994. END_OF_TESTCASES
  995. };