test_crypto.c 39 KB

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