test_crypto.c 39 KB

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