test_crypto.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  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_3[];
  15. extern const char AUTHORITY_SIGNKEY_A_DIGEST[];
  16. extern const char AUTHORITY_SIGNKEY_A_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_3, -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_A_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_A_DIGEST, HEX_DIGEST_LEN);
  479. test_memeq(hex_str(pkey_digests.d[DIGEST_SHA256], DIGEST256_LEN),
  480. AUTHORITY_SIGNKEY_A_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_eq(decrypted_size, 4095);
  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_eq(decrypted_size, 4095);
  658. test_memneq(plain, decrypted2, 4095);
  659. /* Special length case: 1. */
  660. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 1,
  661. plain_1, 1);
  662. test_eq(encrypted_size, 16 + 1);
  663. tt_assert(encrypted_size > 0);
  664. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 1,
  665. encrypted1, encrypted_size);
  666. test_eq(decrypted_size, 1);
  667. tt_assert(decrypted_size > 0);
  668. test_memeq(plain_1, decrypted1, 1);
  669. /* Special length case: 15. */
  670. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 15,
  671. plain_15, 15);
  672. test_eq(encrypted_size, 16 + 15);
  673. tt_assert(encrypted_size > 0);
  674. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 15,
  675. encrypted1, encrypted_size);
  676. test_eq(decrypted_size, 15);
  677. tt_assert(decrypted_size > 0);
  678. test_memeq(plain_15, decrypted1, 15);
  679. /* Special length case: 16. */
  680. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 16,
  681. plain_16, 16);
  682. test_eq(encrypted_size, 16 + 16);
  683. tt_assert(encrypted_size > 0);
  684. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 16,
  685. encrypted1, encrypted_size);
  686. test_eq(decrypted_size, 16);
  687. tt_assert(decrypted_size > 0);
  688. test_memeq(plain_16, decrypted1, 16);
  689. /* Special length case: 17. */
  690. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 17,
  691. plain_17, 17);
  692. test_eq(encrypted_size, 16 + 17);
  693. tt_assert(encrypted_size > 0);
  694. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 17,
  695. encrypted1, encrypted_size);
  696. test_eq(decrypted_size, 17);
  697. tt_assert(decrypted_size > 0);
  698. test_memeq(plain_17, decrypted1, 17);
  699. done:
  700. /* Free memory. */
  701. tor_free(plain);
  702. tor_free(encrypted1);
  703. tor_free(encrypted2);
  704. tor_free(decrypted1);
  705. tor_free(decrypted2);
  706. }
  707. /** Test base32 decoding. */
  708. static void
  709. test_crypto_base32_decode(void)
  710. {
  711. char plain[60], encoded[96 + 1], decoded[60];
  712. int res;
  713. crypto_rand(plain, 60);
  714. /* Encode and decode a random string. */
  715. base32_encode(encoded, 96 + 1, plain, 60);
  716. res = base32_decode(decoded, 60, encoded, 96);
  717. test_eq(res, 0);
  718. test_memeq(plain, decoded, 60);
  719. /* Encode, uppercase, and decode a random string. */
  720. base32_encode(encoded, 96 + 1, plain, 60);
  721. tor_strupper(encoded);
  722. res = base32_decode(decoded, 60, encoded, 96);
  723. test_eq(res, 0);
  724. test_memeq(plain, decoded, 60);
  725. /* Change encoded string and decode. */
  726. if (encoded[0] == 'A' || encoded[0] == 'a')
  727. encoded[0] = 'B';
  728. else
  729. encoded[0] = 'A';
  730. res = base32_decode(decoded, 60, encoded, 96);
  731. test_eq(res, 0);
  732. test_memneq(plain, decoded, 60);
  733. /* Bad encodings. */
  734. encoded[0] = '!';
  735. res = base32_decode(decoded, 60, encoded, 96);
  736. test_assert(res < 0);
  737. done:
  738. ;
  739. }
  740. static void
  741. test_crypto_kdf_TAP(void *arg)
  742. {
  743. uint8_t key_material[100];
  744. int r;
  745. char *mem_op_hex_tmp = NULL;
  746. (void)arg;
  747. #define EXPAND(s) \
  748. r = crypto_expand_key_material_TAP( \
  749. (const uint8_t*)(s), strlen(s), \
  750. key_material, 100)
  751. /* Test vectors generated with a little python script; feel free to write
  752. * your own. */
  753. memset(key_material, 0, sizeof(key_material));
  754. EXPAND("");
  755. tt_int_op(r, ==, 0);
  756. test_memeq_hex(key_material,
  757. "5ba93c9db0cff93f52b521d7420e43f6eda2784fbf8b4530d8"
  758. "d246dd74ac53a13471bba17941dff7c4ea21bb365bbeeaf5f2"
  759. "c654883e56d11e43c44e9842926af7ca0a8cca12604f945414"
  760. "f07b01e13da42c6cf1de3abfdea9b95f34687cbbe92b9a7383");
  761. EXPAND("Tor");
  762. tt_int_op(r, ==, 0);
  763. test_memeq_hex(key_material,
  764. "776c6214fc647aaa5f683c737ee66ec44f03d0372e1cce6922"
  765. "7950f236ddf1e329a7ce7c227903303f525a8c6662426e8034"
  766. "870642a6dabbd41b5d97ec9bf2312ea729992f48f8ea2d0ba8"
  767. "3f45dfda1a80bdc8b80de01b23e3e0ffae099b3e4ccf28dc28");
  768. EXPAND("AN ALARMING ITEM TO FIND ON A MONTHLY AUTO-DEBIT NOTICE");
  769. tt_int_op(r, ==, 0);
  770. test_memeq_hex(key_material,
  771. "a340b5d126086c3ab29c2af4179196dbf95e1c72431419d331"
  772. "4844bf8f6afb6098db952b95581fb6c33625709d6f4400b8e7"
  773. "ace18a70579fad83c0982ef73f89395bcc39493ad53a685854"
  774. "daf2ba9b78733b805d9a6824c907ee1dba5ac27a1e466d4d10");
  775. done:
  776. tor_free(mem_op_hex_tmp);
  777. #undef EXPAND
  778. }
  779. static void
  780. test_crypto_hkdf_sha256(void *arg)
  781. {
  782. uint8_t key_material[100];
  783. const uint8_t salt[] = "ntor-curve25519-sha256-1:key_extract";
  784. const size_t salt_len = strlen((char*)salt);
  785. const uint8_t m_expand[] = "ntor-curve25519-sha256-1:key_expand";
  786. const size_t m_expand_len = strlen((char*)m_expand);
  787. int r;
  788. char *mem_op_hex_tmp = NULL;
  789. (void)arg;
  790. #define EXPAND(s) \
  791. r = crypto_expand_key_material_rfc5869_sha256( \
  792. (const uint8_t*)(s), strlen(s), \
  793. salt, salt_len, \
  794. m_expand, m_expand_len, \
  795. key_material, 100)
  796. /* Test vectors generated with ntor_ref.py */
  797. memset(key_material, 0, sizeof(key_material));
  798. EXPAND("");
  799. tt_int_op(r, ==, 0);
  800. test_memeq_hex(key_material,
  801. "d3490ed48b12a48f9547861583573fe3f19aafe3f81dc7fc75"
  802. "eeed96d741b3290f941576c1f9f0b2d463d1ec7ab2c6bf71cd"
  803. "d7f826c6298c00dbfe6711635d7005f0269493edf6046cc7e7"
  804. "dcf6abe0d20c77cf363e8ffe358927817a3d3e73712cee28d8");
  805. EXPAND("Tor");
  806. tt_int_op(r, ==, 0);
  807. test_memeq_hex(key_material,
  808. "5521492a85139a8d9107a2d5c0d9c91610d0f95989975ebee6"
  809. "c02a4f8d622a6cfdf9b7c7edd3832e2760ded1eac309b76f8d"
  810. "66c4a3c4d6225429b3a016e3c3d45911152fc87bc2de9630c3"
  811. "961be9fdb9f93197ea8e5977180801926d3321fa21513e59ac");
  812. EXPAND("AN ALARMING ITEM TO FIND ON YOUR CREDIT-RATING STATEMENT");
  813. tt_int_op(r, ==, 0);
  814. test_memeq_hex(key_material,
  815. "a2aa9b50da7e481d30463adb8f233ff06e9571a0ca6ab6df0f"
  816. "b206fa34e5bc78d063fc291501beec53b36e5a0e434561200c"
  817. "5f8bd13e0f88b3459600b4dc21d69363e2895321c06184879d"
  818. "94b18f078411be70b767c7fc40679a9440a0c95ea83a23efbf");
  819. done:
  820. tor_free(mem_op_hex_tmp);
  821. #undef EXPAND
  822. }
  823. #ifdef CURVE25519_ENABLED
  824. static void
  825. test_crypto_curve25519_impl(void *arg)
  826. {
  827. /* adapted from curve25519_donna, which adapted it from test-curve25519
  828. version 20050915, by D. J. Bernstein, Public domain. */
  829. const int randomize_high_bit = (arg != NULL);
  830. #ifdef SLOW_CURVE25519_TEST
  831. const int loop_max=10000;
  832. const char e1_expected[] = "4faf81190869fd742a33691b0e0824d5"
  833. "7e0329f4dd2819f5f32d130f1296b500";
  834. const char e2k_expected[] = "05aec13f92286f3a781ccae98995a3b9"
  835. "e0544770bc7de853b38f9100489e3e79";
  836. const char e1e2k_expected[] = "cd6e8269104eb5aaee886bd2071fba88"
  837. "bd13861475516bc2cd2b6e005e805064";
  838. #else
  839. const int loop_max=200;
  840. const char e1_expected[] = "bc7112cde03f97ef7008cad1bdc56be3"
  841. "c6a1037d74cceb3712e9206871dcf654";
  842. const char e2k_expected[] = "dd8fa254fb60bdb5142fe05b1f5de44d"
  843. "8e3ee1a63c7d14274ea5d4c67f065467";
  844. const char e1e2k_expected[] = "7ddb98bd89025d2347776b33901b3e7e"
  845. "c0ee98cb2257a4545c0cfb2ca3e1812b";
  846. #endif
  847. unsigned char e1k[32];
  848. unsigned char e2k[32];
  849. unsigned char e1e2k[32];
  850. unsigned char e2e1k[32];
  851. unsigned char e1[32] = {3};
  852. unsigned char e2[32] = {5};
  853. unsigned char k[32] = {9};
  854. int loop, i;
  855. char *mem_op_hex_tmp = NULL;
  856. for (loop = 0; loop < loop_max; ++loop) {
  857. curve25519_impl(e1k,e1,k);
  858. curve25519_impl(e2e1k,e2,e1k);
  859. curve25519_impl(e2k,e2,k);
  860. if (randomize_high_bit) {
  861. /* We require that the high bit of the public key be ignored. So if
  862. * we're doing this variant test, we randomize the high bit of e2k, and
  863. * make sure that the handshake still works out the same as it would
  864. * otherwise. */
  865. uint8_t byte;
  866. crypto_rand((char*)&byte, 1);
  867. e2k[31] |= (byte & 0x80);
  868. }
  869. curve25519_impl(e1e2k,e1,e2k);
  870. test_memeq(e1e2k, e2e1k, 32);
  871. if (loop == loop_max-1) {
  872. break;
  873. }
  874. for (i = 0;i < 32;++i) e1[i] ^= e2k[i];
  875. for (i = 0;i < 32;++i) e2[i] ^= e1k[i];
  876. for (i = 0;i < 32;++i) k[i] ^= e1e2k[i];
  877. }
  878. test_memeq_hex(e1, e1_expected);
  879. test_memeq_hex(e2k, e2k_expected);
  880. test_memeq_hex(e1e2k, e1e2k_expected);
  881. done:
  882. tor_free(mem_op_hex_tmp);
  883. }
  884. static void
  885. test_crypto_curve25519_wrappers(void *arg)
  886. {
  887. curve25519_public_key_t pubkey1, pubkey2;
  888. curve25519_secret_key_t seckey1, seckey2;
  889. uint8_t output1[CURVE25519_OUTPUT_LEN];
  890. uint8_t output2[CURVE25519_OUTPUT_LEN];
  891. (void)arg;
  892. /* Test a simple handshake, serializing and deserializing some stuff. */
  893. curve25519_secret_key_generate(&seckey1, 0);
  894. curve25519_secret_key_generate(&seckey2, 1);
  895. curve25519_public_key_generate(&pubkey1, &seckey1);
  896. curve25519_public_key_generate(&pubkey2, &seckey2);
  897. test_assert(curve25519_public_key_is_ok(&pubkey1));
  898. test_assert(curve25519_public_key_is_ok(&pubkey2));
  899. curve25519_handshake(output1, &seckey1, &pubkey2);
  900. curve25519_handshake(output2, &seckey2, &pubkey1);
  901. test_memeq(output1, output2, sizeof(output1));
  902. done:
  903. ;
  904. }
  905. static void
  906. test_crypto_curve25519_encode(void *arg)
  907. {
  908. curve25519_secret_key_t seckey;
  909. curve25519_public_key_t key1, key2, key3;
  910. char buf[64];
  911. (void)arg;
  912. curve25519_secret_key_generate(&seckey, 0);
  913. curve25519_public_key_generate(&key1, &seckey);
  914. tt_int_op(0, ==, curve25519_public_to_base64(buf, &key1));
  915. tt_int_op(CURVE25519_BASE64_PADDED_LEN, ==, strlen(buf));
  916. tt_int_op(0, ==, curve25519_public_from_base64(&key2, buf));
  917. test_memeq(key1.public_key, key2.public_key, CURVE25519_PUBKEY_LEN);
  918. buf[CURVE25519_BASE64_PADDED_LEN - 1] = '\0';
  919. tt_int_op(CURVE25519_BASE64_PADDED_LEN-1, ==, strlen(buf));
  920. tt_int_op(0, ==, curve25519_public_from_base64(&key3, buf));
  921. test_memeq(key1.public_key, key3.public_key, CURVE25519_PUBKEY_LEN);
  922. /* Now try bogus parses. */
  923. strlcpy(buf, "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$=", sizeof(buf));
  924. tt_int_op(-1, ==, curve25519_public_from_base64(&key3, buf));
  925. strlcpy(buf, "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$", sizeof(buf));
  926. tt_int_op(-1, ==, curve25519_public_from_base64(&key3, buf));
  927. strlcpy(buf, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", sizeof(buf));
  928. tt_int_op(-1, ==, curve25519_public_from_base64(&key3, buf));
  929. done:
  930. ;
  931. }
  932. static void
  933. test_crypto_curve25519_persist(void *arg)
  934. {
  935. curve25519_keypair_t keypair, keypair2;
  936. char *fname = tor_strdup(get_fname("curve25519_keypair"));
  937. char *tag = NULL;
  938. char *content = NULL;
  939. const char *cp;
  940. struct stat st;
  941. size_t taglen;
  942. (void)arg;
  943. tt_int_op(0,==,curve25519_keypair_generate(&keypair, 0));
  944. tt_int_op(0,==,curve25519_keypair_write_to_file(&keypair, fname, "testing"));
  945. tt_int_op(0,==,curve25519_keypair_read_from_file(&keypair2, &tag, fname));
  946. tt_str_op(tag,==,"testing");
  947. tor_free(tag);
  948. test_memeq(keypair.pubkey.public_key,
  949. keypair2.pubkey.public_key,
  950. CURVE25519_PUBKEY_LEN);
  951. test_memeq(keypair.seckey.secret_key,
  952. keypair2.seckey.secret_key,
  953. CURVE25519_SECKEY_LEN);
  954. content = read_file_to_str(fname, RFTS_BIN, &st);
  955. tt_assert(content);
  956. taglen = strlen("== c25519v1: testing ==");
  957. tt_int_op(st.st_size, ==, 32+CURVE25519_PUBKEY_LEN+CURVE25519_SECKEY_LEN);
  958. tt_assert(fast_memeq(content, "== c25519v1: testing ==", taglen));
  959. tt_assert(tor_mem_is_zero(content+taglen, 32-taglen));
  960. cp = content + 32;
  961. test_memeq(keypair.seckey.secret_key,
  962. cp,
  963. CURVE25519_SECKEY_LEN);
  964. cp += CURVE25519_SECKEY_LEN;
  965. test_memeq(keypair.pubkey.public_key,
  966. cp,
  967. CURVE25519_SECKEY_LEN);
  968. tor_free(fname);
  969. fname = tor_strdup(get_fname("bogus_keypair"));
  970. tt_int_op(-1, ==, curve25519_keypair_read_from_file(&keypair2, &tag, fname));
  971. tor_free(tag);
  972. content[69] ^= 0xff;
  973. tt_int_op(0, ==, write_bytes_to_file(fname, content, (size_t)st.st_size, 1));
  974. tt_int_op(-1, ==, curve25519_keypair_read_from_file(&keypair2, &tag, fname));
  975. done:
  976. tor_free(fname);
  977. tor_free(content);
  978. tor_free(tag);
  979. }
  980. #endif
  981. static void *
  982. pass_data_setup_fn(const struct testcase_t *testcase)
  983. {
  984. return testcase->setup_data;
  985. }
  986. static int
  987. pass_data_cleanup_fn(const struct testcase_t *testcase, void *ptr)
  988. {
  989. (void)ptr;
  990. (void)testcase;
  991. return 1;
  992. }
  993. static const struct testcase_setup_t pass_data = {
  994. pass_data_setup_fn, pass_data_cleanup_fn
  995. };
  996. #define CRYPTO_LEGACY(name) \
  997. { #name, legacy_test_helper, 0, &legacy_setup, test_crypto_ ## name }
  998. struct testcase_t crypto_tests[] = {
  999. CRYPTO_LEGACY(formats),
  1000. CRYPTO_LEGACY(rng),
  1001. { "aes_AES", test_crypto_aes, TT_FORK, &pass_data, (void*)"aes" },
  1002. { "aes_EVP", test_crypto_aes, TT_FORK, &pass_data, (void*)"evp" },
  1003. CRYPTO_LEGACY(sha),
  1004. CRYPTO_LEGACY(pk),
  1005. CRYPTO_LEGACY(digests),
  1006. CRYPTO_LEGACY(dh),
  1007. CRYPTO_LEGACY(s2k),
  1008. { "aes_iv_AES", test_crypto_aes_iv, TT_FORK, &pass_data, (void*)"aes" },
  1009. { "aes_iv_EVP", test_crypto_aes_iv, TT_FORK, &pass_data, (void*)"evp" },
  1010. CRYPTO_LEGACY(base32_decode),
  1011. { "kdf_TAP", test_crypto_kdf_TAP, 0, NULL, NULL },
  1012. { "hkdf_sha256", test_crypto_hkdf_sha256, 0, NULL, NULL },
  1013. #ifdef CURVE25519_ENABLED
  1014. { "curve25519_impl", test_crypto_curve25519_impl, 0, NULL, NULL },
  1015. { "curve25519_impl_hibit", test_crypto_curve25519_impl, 0, NULL, (void*)"y"},
  1016. { "curve25519_wrappers", test_crypto_curve25519_wrappers, 0, NULL, NULL },
  1017. { "curve25519_encode", test_crypto_curve25519_encode, 0, NULL, NULL },
  1018. { "curve25519_persist", test_crypto_curve25519_persist, 0, NULL, NULL },
  1019. #endif
  1020. END_OF_TESTCASES
  1021. };