test_crypto.c 35 KB

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