test_crypto.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307
  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. #include "siphash.h"
  12. #ifdef CURVE25519_ENABLED
  13. #include "crypto_curve25519.h"
  14. #endif
  15. extern const char AUTHORITY_SIGNKEY_3[];
  16. extern const char AUTHORITY_SIGNKEY_A_DIGEST[];
  17. extern const char AUTHORITY_SIGNKEY_A_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. static void
  462. test_crypto_pk_fingerprints(void *arg)
  463. {
  464. crypto_pk_t *pk = NULL;
  465. char encoded[512];
  466. char d[DIGEST_LEN], d2[DIGEST_LEN];
  467. char fingerprint[FINGERPRINT_LEN+1];
  468. int n;
  469. unsigned i;
  470. char *mem_op_hex_tmp=NULL;
  471. (void)arg;
  472. pk = pk_generate(1);
  473. tt_assert(pk);
  474. n = crypto_pk_asn1_encode(pk, encoded, sizeof(encoded));
  475. tt_int_op(n, >, 0);
  476. tt_int_op(n, >, 128);
  477. tt_int_op(n, <, 256);
  478. /* Is digest as expected? */
  479. crypto_digest(d, encoded, n);
  480. tt_int_op(0, ==, crypto_pk_get_digest(pk, d2));
  481. test_memeq(d, d2, DIGEST_LEN);
  482. /* Is fingerprint right? */
  483. tt_int_op(0, ==, crypto_pk_get_fingerprint(pk, fingerprint, 0));
  484. tt_int_op(strlen(fingerprint), ==, DIGEST_LEN * 2);
  485. test_memeq_hex(d, fingerprint);
  486. /* Are spaces right? */
  487. tt_int_op(0, ==, crypto_pk_get_fingerprint(pk, fingerprint, 1));
  488. for (i = 4; i < strlen(fingerprint); i += 5) {
  489. tt_int_op(fingerprint[i], ==, ' ');
  490. }
  491. tor_strstrip(fingerprint, " ");
  492. tt_int_op(strlen(fingerprint), ==, DIGEST_LEN * 2);
  493. test_memeq_hex(d, fingerprint);
  494. /* Now hash again and check crypto_pk_get_hashed_fingerprint. */
  495. crypto_digest(d2, d, sizeof(d));
  496. tt_int_op(0, ==, crypto_pk_get_hashed_fingerprint(pk, fingerprint));
  497. tt_int_op(strlen(fingerprint), ==, DIGEST_LEN * 2);
  498. test_memeq_hex(d2, fingerprint);
  499. done:
  500. crypto_pk_free(pk);
  501. tor_free(mem_op_hex_tmp);
  502. }
  503. /** Sanity check for crypto pk digests */
  504. static void
  505. test_crypto_digests(void)
  506. {
  507. crypto_pk_t *k = NULL;
  508. ssize_t r;
  509. digests_t pkey_digests;
  510. char digest[DIGEST_LEN];
  511. k = crypto_pk_new();
  512. test_assert(k);
  513. r = crypto_pk_read_private_key_from_string(k, AUTHORITY_SIGNKEY_3, -1);
  514. test_assert(!r);
  515. r = crypto_pk_get_digest(k, digest);
  516. test_assert(r == 0);
  517. test_memeq(hex_str(digest, DIGEST_LEN),
  518. AUTHORITY_SIGNKEY_A_DIGEST, HEX_DIGEST_LEN);
  519. r = crypto_pk_get_all_digests(k, &pkey_digests);
  520. test_memeq(hex_str(pkey_digests.d[DIGEST_SHA1], DIGEST_LEN),
  521. AUTHORITY_SIGNKEY_A_DIGEST, HEX_DIGEST_LEN);
  522. test_memeq(hex_str(pkey_digests.d[DIGEST_SHA256], DIGEST256_LEN),
  523. AUTHORITY_SIGNKEY_A_DIGEST256, HEX_DIGEST256_LEN);
  524. done:
  525. crypto_pk_free(k);
  526. }
  527. /** Run unit tests for misc crypto formatting functionality (base64, base32,
  528. * fingerprints, etc) */
  529. static void
  530. test_crypto_formats(void)
  531. {
  532. char *data1 = NULL, *data2 = NULL, *data3 = NULL;
  533. int i, j, idx;
  534. data1 = tor_malloc(1024);
  535. data2 = tor_malloc(1024);
  536. data3 = tor_malloc(1024);
  537. test_assert(data1 && data2 && data3);
  538. /* Base64 tests */
  539. memset(data1, 6, 1024);
  540. for (idx = 0; idx < 10; ++idx) {
  541. i = base64_encode(data2, 1024, data1, idx);
  542. test_assert(i >= 0);
  543. j = base64_decode(data3, 1024, data2, i);
  544. test_eq(j,idx);
  545. test_memeq(data3, data1, idx);
  546. }
  547. strlcpy(data1, "Test string that contains 35 chars.", 1024);
  548. strlcat(data1, " 2nd string that contains 35 chars.", 1024);
  549. i = base64_encode(data2, 1024, data1, 71);
  550. test_assert(i >= 0);
  551. j = base64_decode(data3, 1024, data2, i);
  552. test_eq(j, 71);
  553. test_streq(data3, data1);
  554. test_assert(data2[i] == '\0');
  555. crypto_rand(data1, DIGEST_LEN);
  556. memset(data2, 100, 1024);
  557. digest_to_base64(data2, data1);
  558. test_eq(BASE64_DIGEST_LEN, strlen(data2));
  559. test_eq(100, data2[BASE64_DIGEST_LEN+2]);
  560. memset(data3, 99, 1024);
  561. test_eq(digest_from_base64(data3, data2), 0);
  562. test_memeq(data1, data3, DIGEST_LEN);
  563. test_eq(99, data3[DIGEST_LEN+1]);
  564. test_assert(digest_from_base64(data3, "###") < 0);
  565. /* Encoding SHA256 */
  566. crypto_rand(data2, DIGEST256_LEN);
  567. memset(data2, 100, 1024);
  568. digest256_to_base64(data2, data1);
  569. test_eq(BASE64_DIGEST256_LEN, strlen(data2));
  570. test_eq(100, data2[BASE64_DIGEST256_LEN+2]);
  571. memset(data3, 99, 1024);
  572. test_eq(digest256_from_base64(data3, data2), 0);
  573. test_memeq(data1, data3, DIGEST256_LEN);
  574. test_eq(99, data3[DIGEST256_LEN+1]);
  575. /* Base32 tests */
  576. strlcpy(data1, "5chrs", 1024);
  577. /* bit pattern is: [35 63 68 72 73] ->
  578. * [00110101 01100011 01101000 01110010 01110011]
  579. * By 5s: [00110 10101 10001 10110 10000 11100 10011 10011]
  580. */
  581. base32_encode(data2, 9, data1, 5);
  582. test_streq(data2, "gvrwq4tt");
  583. strlcpy(data1, "\xFF\xF5\x6D\x44\xAE\x0D\x5C\xC9\x62\xC4", 1024);
  584. base32_encode(data2, 30, data1, 10);
  585. test_streq(data2, "772w2rfobvomsywe");
  586. /* Base16 tests */
  587. strlcpy(data1, "6chrs\xff", 1024);
  588. base16_encode(data2, 13, data1, 6);
  589. test_streq(data2, "3663687273FF");
  590. strlcpy(data1, "f0d678affc000100", 1024);
  591. i = base16_decode(data2, 8, data1, 16);
  592. test_eq(i,0);
  593. test_memeq(data2, "\xf0\xd6\x78\xaf\xfc\x00\x01\x00",8);
  594. /* now try some failing base16 decodes */
  595. test_eq(-1, base16_decode(data2, 8, data1, 15)); /* odd input len */
  596. test_eq(-1, base16_decode(data2, 7, data1, 16)); /* dest too short */
  597. strlcpy(data1, "f0dz!8affc000100", 1024);
  598. test_eq(-1, base16_decode(data2, 8, data1, 16));
  599. tor_free(data1);
  600. tor_free(data2);
  601. tor_free(data3);
  602. /* Add spaces to fingerprint */
  603. {
  604. data1 = tor_strdup("ABCD1234ABCD56780000ABCD1234ABCD56780000");
  605. test_eq(strlen(data1), 40);
  606. data2 = tor_malloc(FINGERPRINT_LEN+1);
  607. crypto_add_spaces_to_fp(data2, FINGERPRINT_LEN+1, data1);
  608. test_streq(data2, "ABCD 1234 ABCD 5678 0000 ABCD 1234 ABCD 5678 0000");
  609. tor_free(data1);
  610. tor_free(data2);
  611. }
  612. done:
  613. tor_free(data1);
  614. tor_free(data2);
  615. tor_free(data3);
  616. }
  617. /** Run unit tests for our secret-to-key passphrase hashing functionality. */
  618. static void
  619. test_crypto_s2k(void)
  620. {
  621. char buf[29];
  622. char buf2[29];
  623. char *buf3 = NULL;
  624. int i;
  625. memset(buf, 0, sizeof(buf));
  626. memset(buf2, 0, sizeof(buf2));
  627. buf3 = tor_malloc(65536);
  628. memset(buf3, 0, 65536);
  629. secret_to_key(buf+9, 20, "", 0, buf);
  630. crypto_digest(buf2+9, buf3, 1024);
  631. test_memeq(buf, buf2, 29);
  632. memcpy(buf,"vrbacrda",8);
  633. memcpy(buf2,"vrbacrda",8);
  634. buf[8] = 96;
  635. buf2[8] = 96;
  636. secret_to_key(buf+9, 20, "12345678", 8, buf);
  637. for (i = 0; i < 65536; i += 16) {
  638. memcpy(buf3+i, "vrbacrda12345678", 16);
  639. }
  640. crypto_digest(buf2+9, buf3, 65536);
  641. test_memeq(buf, buf2, 29);
  642. done:
  643. tor_free(buf3);
  644. }
  645. /** Test AES-CTR encryption and decryption with IV. */
  646. static void
  647. test_crypto_aes_iv(void *arg)
  648. {
  649. char *plain, *encrypted1, *encrypted2, *decrypted1, *decrypted2;
  650. char plain_1[1], plain_15[15], plain_16[16], plain_17[17];
  651. char key1[16], key2[16];
  652. ssize_t encrypted_size, decrypted_size;
  653. int use_evp = !strcmp(arg,"evp");
  654. evaluate_evp_for_aes(use_evp);
  655. plain = tor_malloc(4095);
  656. encrypted1 = tor_malloc(4095 + 1 + 16);
  657. encrypted2 = tor_malloc(4095 + 1 + 16);
  658. decrypted1 = tor_malloc(4095 + 1);
  659. decrypted2 = tor_malloc(4095 + 1);
  660. crypto_rand(plain, 4095);
  661. crypto_rand(key1, 16);
  662. crypto_rand(key2, 16);
  663. crypto_rand(plain_1, 1);
  664. crypto_rand(plain_15, 15);
  665. crypto_rand(plain_16, 16);
  666. crypto_rand(plain_17, 17);
  667. key1[0] = key2[0] + 128; /* Make sure that contents are different. */
  668. /* Encrypt and decrypt with the same key. */
  669. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 4095,
  670. plain, 4095);
  671. test_eq(encrypted_size, 16 + 4095);
  672. tt_assert(encrypted_size > 0); /* This is obviously true, since 4111 is
  673. * greater than 0, but its truth is not
  674. * obvious to all analysis tools. */
  675. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 4095,
  676. encrypted1, encrypted_size);
  677. test_eq(decrypted_size, 4095);
  678. tt_assert(decrypted_size > 0);
  679. test_memeq(plain, decrypted1, 4095);
  680. /* Encrypt a second time (with a new random initialization vector). */
  681. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted2, 16 + 4095,
  682. plain, 4095);
  683. test_eq(encrypted_size, 16 + 4095);
  684. tt_assert(encrypted_size > 0);
  685. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted2, 4095,
  686. encrypted2, encrypted_size);
  687. test_eq(decrypted_size, 4095);
  688. tt_assert(decrypted_size > 0);
  689. test_memeq(plain, decrypted2, 4095);
  690. test_memneq(encrypted1, encrypted2, encrypted_size);
  691. /* Decrypt with the wrong key. */
  692. decrypted_size = crypto_cipher_decrypt_with_iv(key2, decrypted2, 4095,
  693. encrypted1, encrypted_size);
  694. test_eq(decrypted_size, 4095);
  695. test_memneq(plain, decrypted2, decrypted_size);
  696. /* Alter the initialization vector. */
  697. encrypted1[0] += 42;
  698. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 4095,
  699. encrypted1, encrypted_size);
  700. test_eq(decrypted_size, 4095);
  701. test_memneq(plain, decrypted2, 4095);
  702. /* Special length case: 1. */
  703. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 1,
  704. plain_1, 1);
  705. test_eq(encrypted_size, 16 + 1);
  706. tt_assert(encrypted_size > 0);
  707. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 1,
  708. encrypted1, encrypted_size);
  709. test_eq(decrypted_size, 1);
  710. tt_assert(decrypted_size > 0);
  711. test_memeq(plain_1, decrypted1, 1);
  712. /* Special length case: 15. */
  713. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 15,
  714. plain_15, 15);
  715. test_eq(encrypted_size, 16 + 15);
  716. tt_assert(encrypted_size > 0);
  717. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 15,
  718. encrypted1, encrypted_size);
  719. test_eq(decrypted_size, 15);
  720. tt_assert(decrypted_size > 0);
  721. test_memeq(plain_15, decrypted1, 15);
  722. /* Special length case: 16. */
  723. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 16,
  724. plain_16, 16);
  725. test_eq(encrypted_size, 16 + 16);
  726. tt_assert(encrypted_size > 0);
  727. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 16,
  728. encrypted1, encrypted_size);
  729. test_eq(decrypted_size, 16);
  730. tt_assert(decrypted_size > 0);
  731. test_memeq(plain_16, decrypted1, 16);
  732. /* Special length case: 17. */
  733. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 17,
  734. plain_17, 17);
  735. test_eq(encrypted_size, 16 + 17);
  736. tt_assert(encrypted_size > 0);
  737. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 17,
  738. encrypted1, encrypted_size);
  739. test_eq(decrypted_size, 17);
  740. tt_assert(decrypted_size > 0);
  741. test_memeq(plain_17, decrypted1, 17);
  742. done:
  743. /* Free memory. */
  744. tor_free(plain);
  745. tor_free(encrypted1);
  746. tor_free(encrypted2);
  747. tor_free(decrypted1);
  748. tor_free(decrypted2);
  749. }
  750. /** Test base32 decoding. */
  751. static void
  752. test_crypto_base32_decode(void)
  753. {
  754. char plain[60], encoded[96 + 1], decoded[60];
  755. int res;
  756. crypto_rand(plain, 60);
  757. /* Encode and decode a random string. */
  758. base32_encode(encoded, 96 + 1, plain, 60);
  759. res = base32_decode(decoded, 60, encoded, 96);
  760. test_eq(res, 0);
  761. test_memeq(plain, decoded, 60);
  762. /* Encode, uppercase, and decode a random string. */
  763. base32_encode(encoded, 96 + 1, plain, 60);
  764. tor_strupper(encoded);
  765. res = base32_decode(decoded, 60, encoded, 96);
  766. test_eq(res, 0);
  767. test_memeq(plain, decoded, 60);
  768. /* Change encoded string and decode. */
  769. if (encoded[0] == 'A' || encoded[0] == 'a')
  770. encoded[0] = 'B';
  771. else
  772. encoded[0] = 'A';
  773. res = base32_decode(decoded, 60, encoded, 96);
  774. test_eq(res, 0);
  775. test_memneq(plain, decoded, 60);
  776. /* Bad encodings. */
  777. encoded[0] = '!';
  778. res = base32_decode(decoded, 60, encoded, 96);
  779. test_assert(res < 0);
  780. done:
  781. ;
  782. }
  783. static void
  784. test_crypto_kdf_TAP(void *arg)
  785. {
  786. uint8_t key_material[100];
  787. int r;
  788. char *mem_op_hex_tmp = NULL;
  789. (void)arg;
  790. #define EXPAND(s) \
  791. r = crypto_expand_key_material_TAP( \
  792. (const uint8_t*)(s), strlen(s), \
  793. key_material, 100)
  794. /* Test vectors generated with a little python script; feel free to write
  795. * your own. */
  796. memset(key_material, 0, sizeof(key_material));
  797. EXPAND("");
  798. tt_int_op(r, ==, 0);
  799. test_memeq_hex(key_material,
  800. "5ba93c9db0cff93f52b521d7420e43f6eda2784fbf8b4530d8"
  801. "d246dd74ac53a13471bba17941dff7c4ea21bb365bbeeaf5f2"
  802. "c654883e56d11e43c44e9842926af7ca0a8cca12604f945414"
  803. "f07b01e13da42c6cf1de3abfdea9b95f34687cbbe92b9a7383");
  804. EXPAND("Tor");
  805. tt_int_op(r, ==, 0);
  806. test_memeq_hex(key_material,
  807. "776c6214fc647aaa5f683c737ee66ec44f03d0372e1cce6922"
  808. "7950f236ddf1e329a7ce7c227903303f525a8c6662426e8034"
  809. "870642a6dabbd41b5d97ec9bf2312ea729992f48f8ea2d0ba8"
  810. "3f45dfda1a80bdc8b80de01b23e3e0ffae099b3e4ccf28dc28");
  811. EXPAND("AN ALARMING ITEM TO FIND ON A MONTHLY AUTO-DEBIT NOTICE");
  812. tt_int_op(r, ==, 0);
  813. test_memeq_hex(key_material,
  814. "a340b5d126086c3ab29c2af4179196dbf95e1c72431419d331"
  815. "4844bf8f6afb6098db952b95581fb6c33625709d6f4400b8e7"
  816. "ace18a70579fad83c0982ef73f89395bcc39493ad53a685854"
  817. "daf2ba9b78733b805d9a6824c907ee1dba5ac27a1e466d4d10");
  818. done:
  819. tor_free(mem_op_hex_tmp);
  820. #undef EXPAND
  821. }
  822. static void
  823. test_crypto_hkdf_sha256(void *arg)
  824. {
  825. uint8_t key_material[100];
  826. const uint8_t salt[] = "ntor-curve25519-sha256-1:key_extract";
  827. const size_t salt_len = strlen((char*)salt);
  828. const uint8_t m_expand[] = "ntor-curve25519-sha256-1:key_expand";
  829. const size_t m_expand_len = strlen((char*)m_expand);
  830. int r;
  831. char *mem_op_hex_tmp = NULL;
  832. (void)arg;
  833. #define EXPAND(s) \
  834. r = crypto_expand_key_material_rfc5869_sha256( \
  835. (const uint8_t*)(s), strlen(s), \
  836. salt, salt_len, \
  837. m_expand, m_expand_len, \
  838. key_material, 100)
  839. /* Test vectors generated with ntor_ref.py */
  840. memset(key_material, 0, sizeof(key_material));
  841. EXPAND("");
  842. tt_int_op(r, ==, 0);
  843. test_memeq_hex(key_material,
  844. "d3490ed48b12a48f9547861583573fe3f19aafe3f81dc7fc75"
  845. "eeed96d741b3290f941576c1f9f0b2d463d1ec7ab2c6bf71cd"
  846. "d7f826c6298c00dbfe6711635d7005f0269493edf6046cc7e7"
  847. "dcf6abe0d20c77cf363e8ffe358927817a3d3e73712cee28d8");
  848. EXPAND("Tor");
  849. tt_int_op(r, ==, 0);
  850. test_memeq_hex(key_material,
  851. "5521492a85139a8d9107a2d5c0d9c91610d0f95989975ebee6"
  852. "c02a4f8d622a6cfdf9b7c7edd3832e2760ded1eac309b76f8d"
  853. "66c4a3c4d6225429b3a016e3c3d45911152fc87bc2de9630c3"
  854. "961be9fdb9f93197ea8e5977180801926d3321fa21513e59ac");
  855. EXPAND("AN ALARMING ITEM TO FIND ON YOUR CREDIT-RATING STATEMENT");
  856. tt_int_op(r, ==, 0);
  857. test_memeq_hex(key_material,
  858. "a2aa9b50da7e481d30463adb8f233ff06e9571a0ca6ab6df0f"
  859. "b206fa34e5bc78d063fc291501beec53b36e5a0e434561200c"
  860. "5f8bd13e0f88b3459600b4dc21d69363e2895321c06184879d"
  861. "94b18f078411be70b767c7fc40679a9440a0c95ea83a23efbf");
  862. done:
  863. tor_free(mem_op_hex_tmp);
  864. #undef EXPAND
  865. }
  866. #ifdef CURVE25519_ENABLED
  867. static void
  868. test_crypto_curve25519_impl(void *arg)
  869. {
  870. /* adapted from curve25519_donna, which adapted it from test-curve25519
  871. version 20050915, by D. J. Bernstein, Public domain. */
  872. const int randomize_high_bit = (arg != NULL);
  873. #ifdef SLOW_CURVE25519_TEST
  874. const int loop_max=10000;
  875. const char e1_expected[] = "4faf81190869fd742a33691b0e0824d5"
  876. "7e0329f4dd2819f5f32d130f1296b500";
  877. const char e2k_expected[] = "05aec13f92286f3a781ccae98995a3b9"
  878. "e0544770bc7de853b38f9100489e3e79";
  879. const char e1e2k_expected[] = "cd6e8269104eb5aaee886bd2071fba88"
  880. "bd13861475516bc2cd2b6e005e805064";
  881. #else
  882. const int loop_max=200;
  883. const char e1_expected[] = "bc7112cde03f97ef7008cad1bdc56be3"
  884. "c6a1037d74cceb3712e9206871dcf654";
  885. const char e2k_expected[] = "dd8fa254fb60bdb5142fe05b1f5de44d"
  886. "8e3ee1a63c7d14274ea5d4c67f065467";
  887. const char e1e2k_expected[] = "7ddb98bd89025d2347776b33901b3e7e"
  888. "c0ee98cb2257a4545c0cfb2ca3e1812b";
  889. #endif
  890. unsigned char e1k[32];
  891. unsigned char e2k[32];
  892. unsigned char e1e2k[32];
  893. unsigned char e2e1k[32];
  894. unsigned char e1[32] = {3};
  895. unsigned char e2[32] = {5};
  896. unsigned char k[32] = {9};
  897. int loop, i;
  898. char *mem_op_hex_tmp = NULL;
  899. for (loop = 0; loop < loop_max; ++loop) {
  900. curve25519_impl(e1k,e1,k);
  901. curve25519_impl(e2e1k,e2,e1k);
  902. curve25519_impl(e2k,e2,k);
  903. if (randomize_high_bit) {
  904. /* We require that the high bit of the public key be ignored. So if
  905. * we're doing this variant test, we randomize the high bit of e2k, and
  906. * make sure that the handshake still works out the same as it would
  907. * otherwise. */
  908. uint8_t byte;
  909. crypto_rand((char*)&byte, 1);
  910. e2k[31] |= (byte & 0x80);
  911. }
  912. curve25519_impl(e1e2k,e1,e2k);
  913. test_memeq(e1e2k, e2e1k, 32);
  914. if (loop == loop_max-1) {
  915. break;
  916. }
  917. for (i = 0;i < 32;++i) e1[i] ^= e2k[i];
  918. for (i = 0;i < 32;++i) e2[i] ^= e1k[i];
  919. for (i = 0;i < 32;++i) k[i] ^= e1e2k[i];
  920. }
  921. test_memeq_hex(e1, e1_expected);
  922. test_memeq_hex(e2k, e2k_expected);
  923. test_memeq_hex(e1e2k, e1e2k_expected);
  924. done:
  925. tor_free(mem_op_hex_tmp);
  926. }
  927. static void
  928. test_crypto_curve25519_wrappers(void *arg)
  929. {
  930. curve25519_public_key_t pubkey1, pubkey2;
  931. curve25519_secret_key_t seckey1, seckey2;
  932. uint8_t output1[CURVE25519_OUTPUT_LEN];
  933. uint8_t output2[CURVE25519_OUTPUT_LEN];
  934. (void)arg;
  935. /* Test a simple handshake, serializing and deserializing some stuff. */
  936. curve25519_secret_key_generate(&seckey1, 0);
  937. curve25519_secret_key_generate(&seckey2, 1);
  938. curve25519_public_key_generate(&pubkey1, &seckey1);
  939. curve25519_public_key_generate(&pubkey2, &seckey2);
  940. test_assert(curve25519_public_key_is_ok(&pubkey1));
  941. test_assert(curve25519_public_key_is_ok(&pubkey2));
  942. curve25519_handshake(output1, &seckey1, &pubkey2);
  943. curve25519_handshake(output2, &seckey2, &pubkey1);
  944. test_memeq(output1, output2, sizeof(output1));
  945. done:
  946. ;
  947. }
  948. static void
  949. test_crypto_curve25519_encode(void *arg)
  950. {
  951. curve25519_secret_key_t seckey;
  952. curve25519_public_key_t key1, key2, key3;
  953. char buf[64];
  954. (void)arg;
  955. curve25519_secret_key_generate(&seckey, 0);
  956. curve25519_public_key_generate(&key1, &seckey);
  957. tt_int_op(0, ==, curve25519_public_to_base64(buf, &key1));
  958. tt_int_op(CURVE25519_BASE64_PADDED_LEN, ==, strlen(buf));
  959. tt_int_op(0, ==, curve25519_public_from_base64(&key2, buf));
  960. test_memeq(key1.public_key, key2.public_key, CURVE25519_PUBKEY_LEN);
  961. buf[CURVE25519_BASE64_PADDED_LEN - 1] = '\0';
  962. tt_int_op(CURVE25519_BASE64_PADDED_LEN-1, ==, strlen(buf));
  963. tt_int_op(0, ==, curve25519_public_from_base64(&key3, buf));
  964. test_memeq(key1.public_key, key3.public_key, CURVE25519_PUBKEY_LEN);
  965. /* Now try bogus parses. */
  966. strlcpy(buf, "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$=", sizeof(buf));
  967. tt_int_op(-1, ==, curve25519_public_from_base64(&key3, buf));
  968. strlcpy(buf, "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$", sizeof(buf));
  969. tt_int_op(-1, ==, curve25519_public_from_base64(&key3, buf));
  970. strlcpy(buf, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", sizeof(buf));
  971. tt_int_op(-1, ==, curve25519_public_from_base64(&key3, buf));
  972. done:
  973. ;
  974. }
  975. static void
  976. test_crypto_curve25519_persist(void *arg)
  977. {
  978. curve25519_keypair_t keypair, keypair2;
  979. char *fname = tor_strdup(get_fname("curve25519_keypair"));
  980. char *tag = NULL;
  981. char *content = NULL;
  982. const char *cp;
  983. struct stat st;
  984. size_t taglen;
  985. (void)arg;
  986. tt_int_op(0,==,curve25519_keypair_generate(&keypair, 0));
  987. tt_int_op(0,==,curve25519_keypair_write_to_file(&keypair, fname, "testing"));
  988. tt_int_op(0,==,curve25519_keypair_read_from_file(&keypair2, &tag, fname));
  989. tt_str_op(tag,==,"testing");
  990. tor_free(tag);
  991. test_memeq(keypair.pubkey.public_key,
  992. keypair2.pubkey.public_key,
  993. CURVE25519_PUBKEY_LEN);
  994. test_memeq(keypair.seckey.secret_key,
  995. keypair2.seckey.secret_key,
  996. CURVE25519_SECKEY_LEN);
  997. content = read_file_to_str(fname, RFTS_BIN, &st);
  998. tt_assert(content);
  999. taglen = strlen("== c25519v1: testing ==");
  1000. tt_u64_op((uint64_t)st.st_size, ==,
  1001. 32+CURVE25519_PUBKEY_LEN+CURVE25519_SECKEY_LEN);
  1002. tt_assert(fast_memeq(content, "== c25519v1: testing ==", taglen));
  1003. tt_assert(tor_mem_is_zero(content+taglen, 32-taglen));
  1004. cp = content + 32;
  1005. test_memeq(keypair.seckey.secret_key,
  1006. cp,
  1007. CURVE25519_SECKEY_LEN);
  1008. cp += CURVE25519_SECKEY_LEN;
  1009. test_memeq(keypair.pubkey.public_key,
  1010. cp,
  1011. CURVE25519_SECKEY_LEN);
  1012. tor_free(fname);
  1013. fname = tor_strdup(get_fname("bogus_keypair"));
  1014. tt_int_op(-1, ==, curve25519_keypair_read_from_file(&keypair2, &tag, fname));
  1015. tor_free(tag);
  1016. content[69] ^= 0xff;
  1017. tt_int_op(0, ==, write_bytes_to_file(fname, content, (size_t)st.st_size, 1));
  1018. tt_int_op(-1, ==, curve25519_keypair_read_from_file(&keypair2, &tag, fname));
  1019. done:
  1020. tor_free(fname);
  1021. tor_free(content);
  1022. tor_free(tag);
  1023. }
  1024. #endif
  1025. static void
  1026. test_crypto_siphash(void *arg)
  1027. {
  1028. /* From the reference implementation, taking
  1029. k = 00 01 02 ... 0f
  1030. and in = 00; 00 01; 00 01 02; ...
  1031. */
  1032. const uint8_t VECTORS[64][8] =
  1033. {
  1034. { 0x31, 0x0e, 0x0e, 0xdd, 0x47, 0xdb, 0x6f, 0x72, },
  1035. { 0xfd, 0x67, 0xdc, 0x93, 0xc5, 0x39, 0xf8, 0x74, },
  1036. { 0x5a, 0x4f, 0xa9, 0xd9, 0x09, 0x80, 0x6c, 0x0d, },
  1037. { 0x2d, 0x7e, 0xfb, 0xd7, 0x96, 0x66, 0x67, 0x85, },
  1038. { 0xb7, 0x87, 0x71, 0x27, 0xe0, 0x94, 0x27, 0xcf, },
  1039. { 0x8d, 0xa6, 0x99, 0xcd, 0x64, 0x55, 0x76, 0x18, },
  1040. { 0xce, 0xe3, 0xfe, 0x58, 0x6e, 0x46, 0xc9, 0xcb, },
  1041. { 0x37, 0xd1, 0x01, 0x8b, 0xf5, 0x00, 0x02, 0xab, },
  1042. { 0x62, 0x24, 0x93, 0x9a, 0x79, 0xf5, 0xf5, 0x93, },
  1043. { 0xb0, 0xe4, 0xa9, 0x0b, 0xdf, 0x82, 0x00, 0x9e, },
  1044. { 0xf3, 0xb9, 0xdd, 0x94, 0xc5, 0xbb, 0x5d, 0x7a, },
  1045. { 0xa7, 0xad, 0x6b, 0x22, 0x46, 0x2f, 0xb3, 0xf4, },
  1046. { 0xfb, 0xe5, 0x0e, 0x86, 0xbc, 0x8f, 0x1e, 0x75, },
  1047. { 0x90, 0x3d, 0x84, 0xc0, 0x27, 0x56, 0xea, 0x14, },
  1048. { 0xee, 0xf2, 0x7a, 0x8e, 0x90, 0xca, 0x23, 0xf7, },
  1049. { 0xe5, 0x45, 0xbe, 0x49, 0x61, 0xca, 0x29, 0xa1, },
  1050. { 0xdb, 0x9b, 0xc2, 0x57, 0x7f, 0xcc, 0x2a, 0x3f, },
  1051. { 0x94, 0x47, 0xbe, 0x2c, 0xf5, 0xe9, 0x9a, 0x69, },
  1052. { 0x9c, 0xd3, 0x8d, 0x96, 0xf0, 0xb3, 0xc1, 0x4b, },
  1053. { 0xbd, 0x61, 0x79, 0xa7, 0x1d, 0xc9, 0x6d, 0xbb, },
  1054. { 0x98, 0xee, 0xa2, 0x1a, 0xf2, 0x5c, 0xd6, 0xbe, },
  1055. { 0xc7, 0x67, 0x3b, 0x2e, 0xb0, 0xcb, 0xf2, 0xd0, },
  1056. { 0x88, 0x3e, 0xa3, 0xe3, 0x95, 0x67, 0x53, 0x93, },
  1057. { 0xc8, 0xce, 0x5c, 0xcd, 0x8c, 0x03, 0x0c, 0xa8, },
  1058. { 0x94, 0xaf, 0x49, 0xf6, 0xc6, 0x50, 0xad, 0xb8, },
  1059. { 0xea, 0xb8, 0x85, 0x8a, 0xde, 0x92, 0xe1, 0xbc, },
  1060. { 0xf3, 0x15, 0xbb, 0x5b, 0xb8, 0x35, 0xd8, 0x17, },
  1061. { 0xad, 0xcf, 0x6b, 0x07, 0x63, 0x61, 0x2e, 0x2f, },
  1062. { 0xa5, 0xc9, 0x1d, 0xa7, 0xac, 0xaa, 0x4d, 0xde, },
  1063. { 0x71, 0x65, 0x95, 0x87, 0x66, 0x50, 0xa2, 0xa6, },
  1064. { 0x28, 0xef, 0x49, 0x5c, 0x53, 0xa3, 0x87, 0xad, },
  1065. { 0x42, 0xc3, 0x41, 0xd8, 0xfa, 0x92, 0xd8, 0x32, },
  1066. { 0xce, 0x7c, 0xf2, 0x72, 0x2f, 0x51, 0x27, 0x71, },
  1067. { 0xe3, 0x78, 0x59, 0xf9, 0x46, 0x23, 0xf3, 0xa7, },
  1068. { 0x38, 0x12, 0x05, 0xbb, 0x1a, 0xb0, 0xe0, 0x12, },
  1069. { 0xae, 0x97, 0xa1, 0x0f, 0xd4, 0x34, 0xe0, 0x15, },
  1070. { 0xb4, 0xa3, 0x15, 0x08, 0xbe, 0xff, 0x4d, 0x31, },
  1071. { 0x81, 0x39, 0x62, 0x29, 0xf0, 0x90, 0x79, 0x02, },
  1072. { 0x4d, 0x0c, 0xf4, 0x9e, 0xe5, 0xd4, 0xdc, 0xca, },
  1073. { 0x5c, 0x73, 0x33, 0x6a, 0x76, 0xd8, 0xbf, 0x9a, },
  1074. { 0xd0, 0xa7, 0x04, 0x53, 0x6b, 0xa9, 0x3e, 0x0e, },
  1075. { 0x92, 0x59, 0x58, 0xfc, 0xd6, 0x42, 0x0c, 0xad, },
  1076. { 0xa9, 0x15, 0xc2, 0x9b, 0xc8, 0x06, 0x73, 0x18, },
  1077. { 0x95, 0x2b, 0x79, 0xf3, 0xbc, 0x0a, 0xa6, 0xd4, },
  1078. { 0xf2, 0x1d, 0xf2, 0xe4, 0x1d, 0x45, 0x35, 0xf9, },
  1079. { 0x87, 0x57, 0x75, 0x19, 0x04, 0x8f, 0x53, 0xa9, },
  1080. { 0x10, 0xa5, 0x6c, 0xf5, 0xdf, 0xcd, 0x9a, 0xdb, },
  1081. { 0xeb, 0x75, 0x09, 0x5c, 0xcd, 0x98, 0x6c, 0xd0, },
  1082. { 0x51, 0xa9, 0xcb, 0x9e, 0xcb, 0xa3, 0x12, 0xe6, },
  1083. { 0x96, 0xaf, 0xad, 0xfc, 0x2c, 0xe6, 0x66, 0xc7, },
  1084. { 0x72, 0xfe, 0x52, 0x97, 0x5a, 0x43, 0x64, 0xee, },
  1085. { 0x5a, 0x16, 0x45, 0xb2, 0x76, 0xd5, 0x92, 0xa1, },
  1086. { 0xb2, 0x74, 0xcb, 0x8e, 0xbf, 0x87, 0x87, 0x0a, },
  1087. { 0x6f, 0x9b, 0xb4, 0x20, 0x3d, 0xe7, 0xb3, 0x81, },
  1088. { 0xea, 0xec, 0xb2, 0xa3, 0x0b, 0x22, 0xa8, 0x7f, },
  1089. { 0x99, 0x24, 0xa4, 0x3c, 0xc1, 0x31, 0x57, 0x24, },
  1090. { 0xbd, 0x83, 0x8d, 0x3a, 0xaf, 0xbf, 0x8d, 0xb7, },
  1091. { 0x0b, 0x1a, 0x2a, 0x32, 0x65, 0xd5, 0x1a, 0xea, },
  1092. { 0x13, 0x50, 0x79, 0xa3, 0x23, 0x1c, 0xe6, 0x60, },
  1093. { 0x93, 0x2b, 0x28, 0x46, 0xe4, 0xd7, 0x06, 0x66, },
  1094. { 0xe1, 0x91, 0x5f, 0x5c, 0xb1, 0xec, 0xa4, 0x6c, },
  1095. { 0xf3, 0x25, 0x96, 0x5c, 0xa1, 0x6d, 0x62, 0x9f, },
  1096. { 0x57, 0x5f, 0xf2, 0x8e, 0x60, 0x38, 0x1b, 0xe5, },
  1097. { 0x72, 0x45, 0x06, 0xeb, 0x4c, 0x32, 0x8a, 0x95, }
  1098. };
  1099. const struct sipkey K = { U64_LITERAL(0x0706050403020100),
  1100. U64_LITERAL(0x0f0e0d0c0b0a0908) };
  1101. uint8_t input[64];
  1102. int i, j;
  1103. (void)arg;
  1104. for (i = 0; i < 64; ++i)
  1105. input[i] = i;
  1106. for (i = 0; i < 64; ++i) {
  1107. uint64_t r = siphash24(input, i, &K);
  1108. for (j = 0; j < 8; ++j) {
  1109. tt_int_op( (r >> (j*8)) & 0xff, ==, VECTORS[i][j]);
  1110. }
  1111. }
  1112. done:
  1113. ;
  1114. }
  1115. static void *
  1116. pass_data_setup_fn(const struct testcase_t *testcase)
  1117. {
  1118. return testcase->setup_data;
  1119. }
  1120. static int
  1121. pass_data_cleanup_fn(const struct testcase_t *testcase, void *ptr)
  1122. {
  1123. (void)ptr;
  1124. (void)testcase;
  1125. return 1;
  1126. }
  1127. static const struct testcase_setup_t pass_data = {
  1128. pass_data_setup_fn, pass_data_cleanup_fn
  1129. };
  1130. #define CRYPTO_LEGACY(name) \
  1131. { #name, legacy_test_helper, 0, &legacy_setup, test_crypto_ ## name }
  1132. struct testcase_t crypto_tests[] = {
  1133. CRYPTO_LEGACY(formats),
  1134. CRYPTO_LEGACY(rng),
  1135. { "aes_AES", test_crypto_aes, TT_FORK, &pass_data, (void*)"aes" },
  1136. { "aes_EVP", test_crypto_aes, TT_FORK, &pass_data, (void*)"evp" },
  1137. CRYPTO_LEGACY(sha),
  1138. CRYPTO_LEGACY(pk),
  1139. { "pk_fingerprints", test_crypto_pk_fingerprints, TT_FORK, NULL, NULL },
  1140. CRYPTO_LEGACY(digests),
  1141. CRYPTO_LEGACY(dh),
  1142. CRYPTO_LEGACY(s2k),
  1143. { "aes_iv_AES", test_crypto_aes_iv, TT_FORK, &pass_data, (void*)"aes" },
  1144. { "aes_iv_EVP", test_crypto_aes_iv, TT_FORK, &pass_data, (void*)"evp" },
  1145. CRYPTO_LEGACY(base32_decode),
  1146. { "kdf_TAP", test_crypto_kdf_TAP, 0, NULL, NULL },
  1147. { "hkdf_sha256", test_crypto_hkdf_sha256, 0, NULL, NULL },
  1148. #ifdef CURVE25519_ENABLED
  1149. { "curve25519_impl", test_crypto_curve25519_impl, 0, NULL, NULL },
  1150. { "curve25519_impl_hibit", test_crypto_curve25519_impl, 0, NULL, (void*)"y"},
  1151. { "curve25519_wrappers", test_crypto_curve25519_wrappers, 0, NULL, NULL },
  1152. { "curve25519_encode", test_crypto_curve25519_encode, 0, NULL, NULL },
  1153. { "curve25519_persist", test_crypto_curve25519_persist, 0, NULL, NULL },
  1154. #endif
  1155. { "siphash", test_crypto_siphash, 0, NULL, NULL },
  1156. END_OF_TESTCASES
  1157. };