test_crypto.c 40 KB

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