test_crypto.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2015, 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. #include "crypto_curve25519.h"
  13. #include "crypto_ed25519.h"
  14. #include "ed25519_vectors.inc"
  15. #include <openssl/evp.h>
  16. extern const char AUTHORITY_SIGNKEY_3[];
  17. extern const char AUTHORITY_SIGNKEY_A_DIGEST[];
  18. extern const char AUTHORITY_SIGNKEY_A_DIGEST256[];
  19. /** Run unit tests for Diffie-Hellman functionality. */
  20. static void
  21. test_crypto_dh(void *arg)
  22. {
  23. crypto_dh_t *dh1 = crypto_dh_new(DH_TYPE_CIRCUIT);
  24. crypto_dh_t *dh2 = crypto_dh_new(DH_TYPE_CIRCUIT);
  25. char p1[DH_BYTES];
  26. char p2[DH_BYTES];
  27. char s1[DH_BYTES];
  28. char s2[DH_BYTES];
  29. ssize_t s1len, s2len;
  30. (void)arg;
  31. tt_int_op(crypto_dh_get_bytes(dh1),OP_EQ, DH_BYTES);
  32. tt_int_op(crypto_dh_get_bytes(dh2),OP_EQ, DH_BYTES);
  33. memset(p1, 0, DH_BYTES);
  34. memset(p2, 0, DH_BYTES);
  35. tt_mem_op(p1,OP_EQ, p2, DH_BYTES);
  36. tt_assert(! crypto_dh_get_public(dh1, p1, DH_BYTES));
  37. tt_mem_op(p1,OP_NE, p2, DH_BYTES);
  38. tt_assert(! crypto_dh_get_public(dh2, p2, DH_BYTES));
  39. tt_mem_op(p1,OP_NE, p2, DH_BYTES);
  40. memset(s1, 0, DH_BYTES);
  41. memset(s2, 0xFF, DH_BYTES);
  42. s1len = crypto_dh_compute_secret(LOG_WARN, dh1, p2, DH_BYTES, s1, 50);
  43. s2len = crypto_dh_compute_secret(LOG_WARN, dh2, p1, DH_BYTES, s2, 50);
  44. tt_assert(s1len > 0);
  45. tt_int_op(s1len,OP_EQ, s2len);
  46. tt_mem_op(s1,OP_EQ, s2, s1len);
  47. {
  48. /* XXXX Now fabricate some bad values and make sure they get caught,
  49. * Check 0, 1, N-1, >= N, etc.
  50. */
  51. }
  52. done:
  53. crypto_dh_free(dh1);
  54. crypto_dh_free(dh2);
  55. }
  56. /** Run unit tests for our random number generation function and its wrappers.
  57. */
  58. static void
  59. test_crypto_rng(void *arg)
  60. {
  61. int i, j, allok;
  62. char data1[100], data2[100];
  63. double d;
  64. /* Try out RNG. */
  65. (void)arg;
  66. tt_assert(! crypto_seed_rng());
  67. crypto_rand(data1, 100);
  68. crypto_rand(data2, 100);
  69. tt_mem_op(data1,OP_NE, data2,100);
  70. allok = 1;
  71. for (i = 0; i < 100; ++i) {
  72. uint64_t big;
  73. char *host;
  74. j = crypto_rand_int(100);
  75. if (j < 0 || j >= 100)
  76. allok = 0;
  77. big = crypto_rand_uint64(U64_LITERAL(1)<<40);
  78. if (big >= (U64_LITERAL(1)<<40))
  79. allok = 0;
  80. big = crypto_rand_uint64(U64_LITERAL(5));
  81. if (big >= 5)
  82. allok = 0;
  83. d = crypto_rand_double();
  84. tt_assert(d >= 0);
  85. tt_assert(d < 1.0);
  86. host = crypto_random_hostname(3,8,"www.",".onion");
  87. if (strcmpstart(host,"www.") ||
  88. strcmpend(host,".onion") ||
  89. strlen(host) < 13 ||
  90. strlen(host) > 18)
  91. allok = 0;
  92. tor_free(host);
  93. }
  94. tt_assert(allok);
  95. done:
  96. ;
  97. }
  98. static void
  99. test_crypto_rng_range(void *arg)
  100. {
  101. int got_smallest = 0, got_largest = 0;
  102. int i;
  103. (void)arg;
  104. for (i = 0; i < 1000; ++i) {
  105. int x = crypto_rand_int_range(5,9);
  106. tt_int_op(x, OP_GE, 5);
  107. tt_int_op(x, OP_LT, 9);
  108. if (x == 5)
  109. got_smallest = 1;
  110. if (x == 8)
  111. got_largest = 1;
  112. }
  113. /* These fail with probability 1/10^603. */
  114. tt_assert(got_smallest);
  115. tt_assert(got_largest);
  116. done:
  117. ;
  118. }
  119. /** Run unit tests for our AES functionality */
  120. static void
  121. test_crypto_aes(void *arg)
  122. {
  123. char *data1 = NULL, *data2 = NULL, *data3 = NULL;
  124. crypto_cipher_t *env1 = NULL, *env2 = NULL;
  125. int i, j;
  126. char *mem_op_hex_tmp=NULL;
  127. int use_evp = !strcmp(arg,"evp");
  128. evaluate_evp_for_aes(use_evp);
  129. evaluate_ctr_for_aes();
  130. data1 = tor_malloc(1024);
  131. data2 = tor_malloc(1024);
  132. data3 = tor_malloc(1024);
  133. /* Now, test encryption and decryption with stream cipher. */
  134. data1[0]='\0';
  135. for (i = 1023; i>0; i -= 35)
  136. strncat(data1, "Now is the time for all good onions", i);
  137. memset(data2, 0, 1024);
  138. memset(data3, 0, 1024);
  139. env1 = crypto_cipher_new(NULL);
  140. tt_ptr_op(env1, OP_NE, NULL);
  141. env2 = crypto_cipher_new(crypto_cipher_get_key(env1));
  142. tt_ptr_op(env2, OP_NE, NULL);
  143. /* Try encrypting 512 chars. */
  144. crypto_cipher_encrypt(env1, data2, data1, 512);
  145. crypto_cipher_decrypt(env2, data3, data2, 512);
  146. tt_mem_op(data1,OP_EQ, data3, 512);
  147. tt_mem_op(data1,OP_NE, data2, 512);
  148. /* Now encrypt 1 at a time, and get 1 at a time. */
  149. for (j = 512; j < 560; ++j) {
  150. crypto_cipher_encrypt(env1, data2+j, data1+j, 1);
  151. }
  152. for (j = 512; j < 560; ++j) {
  153. crypto_cipher_decrypt(env2, data3+j, data2+j, 1);
  154. }
  155. tt_mem_op(data1,OP_EQ, data3, 560);
  156. /* Now encrypt 3 at a time, and get 5 at a time. */
  157. for (j = 560; j < 1024-5; j += 3) {
  158. crypto_cipher_encrypt(env1, data2+j, data1+j, 3);
  159. }
  160. for (j = 560; j < 1024-5; j += 5) {
  161. crypto_cipher_decrypt(env2, data3+j, data2+j, 5);
  162. }
  163. tt_mem_op(data1,OP_EQ, data3, 1024-5);
  164. /* Now make sure that when we encrypt with different chunk sizes, we get
  165. the same results. */
  166. crypto_cipher_free(env2);
  167. env2 = NULL;
  168. memset(data3, 0, 1024);
  169. env2 = crypto_cipher_new(crypto_cipher_get_key(env1));
  170. tt_ptr_op(env2, OP_NE, NULL);
  171. for (j = 0; j < 1024-16; j += 17) {
  172. crypto_cipher_encrypt(env2, data3+j, data1+j, 17);
  173. }
  174. for (j= 0; j < 1024-16; ++j) {
  175. if (data2[j] != data3[j]) {
  176. printf("%d: %d\t%d\n", j, (int) data2[j], (int) data3[j]);
  177. }
  178. }
  179. tt_mem_op(data2,OP_EQ, data3, 1024-16);
  180. crypto_cipher_free(env1);
  181. env1 = NULL;
  182. crypto_cipher_free(env2);
  183. env2 = NULL;
  184. /* NIST test vector for aes. */
  185. /* IV starts at 0 */
  186. env1 = crypto_cipher_new("\x80\x00\x00\x00\x00\x00\x00\x00"
  187. "\x00\x00\x00\x00\x00\x00\x00\x00");
  188. crypto_cipher_encrypt(env1, data1,
  189. "\x00\x00\x00\x00\x00\x00\x00\x00"
  190. "\x00\x00\x00\x00\x00\x00\x00\x00", 16);
  191. test_memeq_hex(data1, "0EDD33D3C621E546455BD8BA1418BEC8");
  192. /* Now test rollover. All these values are originally from a python
  193. * script. */
  194. crypto_cipher_free(env1);
  195. env1 = crypto_cipher_new_with_iv(
  196. "\x80\x00\x00\x00\x00\x00\x00\x00"
  197. "\x00\x00\x00\x00\x00\x00\x00\x00",
  198. "\x00\x00\x00\x00\x00\x00\x00\x00"
  199. "\xff\xff\xff\xff\xff\xff\xff\xff");
  200. memset(data2, 0, 1024);
  201. crypto_cipher_encrypt(env1, data1, data2, 32);
  202. test_memeq_hex(data1, "335fe6da56f843199066c14a00a40231"
  203. "cdd0b917dbc7186908a6bfb5ffd574d3");
  204. crypto_cipher_free(env1);
  205. env1 = crypto_cipher_new_with_iv(
  206. "\x80\x00\x00\x00\x00\x00\x00\x00"
  207. "\x00\x00\x00\x00\x00\x00\x00\x00",
  208. "\x00\x00\x00\x00\xff\xff\xff\xff"
  209. "\xff\xff\xff\xff\xff\xff\xff\xff");
  210. memset(data2, 0, 1024);
  211. crypto_cipher_encrypt(env1, data1, data2, 32);
  212. test_memeq_hex(data1, "e627c6423fa2d77832a02b2794094b73"
  213. "3e63c721df790d2c6469cc1953a3ffac");
  214. crypto_cipher_free(env1);
  215. env1 = crypto_cipher_new_with_iv(
  216. "\x80\x00\x00\x00\x00\x00\x00\x00"
  217. "\x00\x00\x00\x00\x00\x00\x00\x00",
  218. "\xff\xff\xff\xff\xff\xff\xff\xff"
  219. "\xff\xff\xff\xff\xff\xff\xff\xff");
  220. memset(data2, 0, 1024);
  221. crypto_cipher_encrypt(env1, data1, data2, 32);
  222. test_memeq_hex(data1, "2aed2bff0de54f9328efd070bf48f70a"
  223. "0EDD33D3C621E546455BD8BA1418BEC8");
  224. /* Now check rollover on inplace cipher. */
  225. crypto_cipher_free(env1);
  226. env1 = crypto_cipher_new_with_iv(
  227. "\x80\x00\x00\x00\x00\x00\x00\x00"
  228. "\x00\x00\x00\x00\x00\x00\x00\x00",
  229. "\xff\xff\xff\xff\xff\xff\xff\xff"
  230. "\xff\xff\xff\xff\xff\xff\xff\xff");
  231. crypto_cipher_crypt_inplace(env1, data2, 64);
  232. test_memeq_hex(data2, "2aed2bff0de54f9328efd070bf48f70a"
  233. "0EDD33D3C621E546455BD8BA1418BEC8"
  234. "93e2c5243d6839eac58503919192f7ae"
  235. "1908e67cafa08d508816659c2e693191");
  236. crypto_cipher_free(env1);
  237. env1 = crypto_cipher_new_with_iv(
  238. "\x80\x00\x00\x00\x00\x00\x00\x00"
  239. "\x00\x00\x00\x00\x00\x00\x00\x00",
  240. "\xff\xff\xff\xff\xff\xff\xff\xff"
  241. "\xff\xff\xff\xff\xff\xff\xff\xff");
  242. crypto_cipher_crypt_inplace(env1, data2, 64);
  243. tt_assert(tor_mem_is_zero(data2, 64));
  244. done:
  245. tor_free(mem_op_hex_tmp);
  246. if (env1)
  247. crypto_cipher_free(env1);
  248. if (env2)
  249. crypto_cipher_free(env2);
  250. tor_free(data1);
  251. tor_free(data2);
  252. tor_free(data3);
  253. }
  254. /** Run unit tests for our SHA-1 functionality */
  255. static void
  256. test_crypto_sha(void *arg)
  257. {
  258. crypto_digest_t *d1 = NULL, *d2 = NULL;
  259. int i;
  260. #define RFC_4231_MAX_KEY_SIZE 131
  261. char key[RFC_4231_MAX_KEY_SIZE];
  262. char digest[DIGEST256_LEN];
  263. char data[DIGEST512_LEN];
  264. char d_out1[DIGEST512_LEN], d_out2[DIGEST512_LEN];
  265. char *mem_op_hex_tmp=NULL;
  266. /* Test SHA-1 with a test vector from the specification. */
  267. (void)arg;
  268. i = crypto_digest(data, "abc", 3);
  269. test_memeq_hex(data, "A9993E364706816ABA3E25717850C26C9CD0D89D");
  270. tt_int_op(i, OP_EQ, 0);
  271. /* Test SHA-256 with a test vector from the specification. */
  272. i = crypto_digest256(data, "abc", 3, DIGEST_SHA256);
  273. test_memeq_hex(data, "BA7816BF8F01CFEA414140DE5DAE2223B00361A3"
  274. "96177A9CB410FF61F20015AD");
  275. tt_int_op(i, OP_EQ, 0);
  276. /* Test SHA-512 with a test vector from the specification. */
  277. i = crypto_digest512(data, "abc", 3, DIGEST_SHA512);
  278. test_memeq_hex(data, "ddaf35a193617abacc417349ae20413112e6fa4e89a97"
  279. "ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3"
  280. "feebbd454d4423643ce80e2a9ac94fa54ca49f");
  281. tt_int_op(i, OP_EQ, 0);
  282. /* Test HMAC-SHA256 with test cases from wikipedia and RFC 4231 */
  283. /* Case empty (wikipedia) */
  284. crypto_hmac_sha256(digest, "", 0, "", 0);
  285. tt_str_op(hex_str(digest, 32),OP_EQ,
  286. "B613679A0814D9EC772F95D778C35FC5FF1697C493715653C6C712144292C5AD");
  287. /* Case quick-brown (wikipedia) */
  288. crypto_hmac_sha256(digest, "key", 3,
  289. "The quick brown fox jumps over the lazy dog", 43);
  290. tt_str_op(hex_str(digest, 32),OP_EQ,
  291. "F7BC83F430538424B13298E6AA6FB143EF4D59A14946175997479DBC2D1A3CD8");
  292. /* "Test Case 1" from RFC 4231 */
  293. memset(key, 0x0b, 20);
  294. crypto_hmac_sha256(digest, key, 20, "Hi There", 8);
  295. test_memeq_hex(digest,
  296. "b0344c61d8db38535ca8afceaf0bf12b"
  297. "881dc200c9833da726e9376c2e32cff7");
  298. /* "Test Case 2" from RFC 4231 */
  299. memset(key, 0x0b, 20);
  300. crypto_hmac_sha256(digest, "Jefe", 4, "what do ya want for nothing?", 28);
  301. test_memeq_hex(digest,
  302. "5bdcc146bf60754e6a042426089575c7"
  303. "5a003f089d2739839dec58b964ec3843");
  304. /* "Test case 3" from RFC 4231 */
  305. memset(key, 0xaa, 20);
  306. memset(data, 0xdd, 50);
  307. crypto_hmac_sha256(digest, key, 20, data, 50);
  308. test_memeq_hex(digest,
  309. "773ea91e36800e46854db8ebd09181a7"
  310. "2959098b3ef8c122d9635514ced565fe");
  311. /* "Test case 4" from RFC 4231 */
  312. base16_decode(key, 25,
  313. "0102030405060708090a0b0c0d0e0f10111213141516171819", 50);
  314. memset(data, 0xcd, 50);
  315. crypto_hmac_sha256(digest, key, 25, data, 50);
  316. test_memeq_hex(digest,
  317. "82558a389a443c0ea4cc819899f2083a"
  318. "85f0faa3e578f8077a2e3ff46729665b");
  319. /* "Test case 5" from RFC 4231 */
  320. memset(key, 0x0c, 20);
  321. crypto_hmac_sha256(digest, key, 20, "Test With Truncation", 20);
  322. test_memeq_hex(digest,
  323. "a3b6167473100ee06e0c796c2955552b");
  324. /* "Test case 6" from RFC 4231 */
  325. memset(key, 0xaa, 131);
  326. crypto_hmac_sha256(digest, key, 131,
  327. "Test Using Larger Than Block-Size Key - Hash Key First",
  328. 54);
  329. test_memeq_hex(digest,
  330. "60e431591ee0b67f0d8a26aacbf5b77f"
  331. "8e0bc6213728c5140546040f0ee37f54");
  332. /* "Test case 7" from RFC 4231 */
  333. memset(key, 0xaa, 131);
  334. crypto_hmac_sha256(digest, key, 131,
  335. "This is a test using a larger than block-size key and a "
  336. "larger than block-size data. The key needs to be hashed "
  337. "before being used by the HMAC algorithm.", 152);
  338. test_memeq_hex(digest,
  339. "9b09ffa71b942fcb27635fbcd5b0e944"
  340. "bfdc63644f0713938a7f51535c3a35e2");
  341. /* Incremental digest code. */
  342. d1 = crypto_digest_new();
  343. tt_assert(d1);
  344. crypto_digest_add_bytes(d1, "abcdef", 6);
  345. d2 = crypto_digest_dup(d1);
  346. tt_assert(d2);
  347. crypto_digest_add_bytes(d2, "ghijkl", 6);
  348. crypto_digest_get_digest(d2, d_out1, DIGEST_LEN);
  349. crypto_digest(d_out2, "abcdefghijkl", 12);
  350. tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST_LEN);
  351. crypto_digest_assign(d2, d1);
  352. crypto_digest_add_bytes(d2, "mno", 3);
  353. crypto_digest_get_digest(d2, d_out1, DIGEST_LEN);
  354. crypto_digest(d_out2, "abcdefmno", 9);
  355. tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST_LEN);
  356. crypto_digest_get_digest(d1, d_out1, DIGEST_LEN);
  357. crypto_digest(d_out2, "abcdef", 6);
  358. tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST_LEN);
  359. crypto_digest_free(d1);
  360. crypto_digest_free(d2);
  361. /* Incremental digest code with sha256 */
  362. d1 = crypto_digest256_new(DIGEST_SHA256);
  363. tt_assert(d1);
  364. crypto_digest_add_bytes(d1, "abcdef", 6);
  365. d2 = crypto_digest_dup(d1);
  366. tt_assert(d2);
  367. crypto_digest_add_bytes(d2, "ghijkl", 6);
  368. crypto_digest_get_digest(d2, d_out1, DIGEST256_LEN);
  369. crypto_digest256(d_out2, "abcdefghijkl", 12, DIGEST_SHA256);
  370. tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST256_LEN);
  371. crypto_digest_assign(d2, d1);
  372. crypto_digest_add_bytes(d2, "mno", 3);
  373. crypto_digest_get_digest(d2, d_out1, DIGEST256_LEN);
  374. crypto_digest256(d_out2, "abcdefmno", 9, DIGEST_SHA256);
  375. tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST256_LEN);
  376. crypto_digest_get_digest(d1, d_out1, DIGEST256_LEN);
  377. crypto_digest256(d_out2, "abcdef", 6, DIGEST_SHA256);
  378. tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST256_LEN);
  379. crypto_digest_free(d1);
  380. crypto_digest_free(d2);
  381. /* Incremental digest code with sha512 */
  382. d1 = crypto_digest512_new(DIGEST_SHA512);
  383. tt_assert(d1);
  384. crypto_digest_add_bytes(d1, "abcdef", 6);
  385. d2 = crypto_digest_dup(d1);
  386. tt_assert(d2);
  387. crypto_digest_add_bytes(d2, "ghijkl", 6);
  388. crypto_digest_get_digest(d2, d_out1, DIGEST512_LEN);
  389. crypto_digest512(d_out2, "abcdefghijkl", 12, DIGEST_SHA512);
  390. tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST512_LEN);
  391. crypto_digest_assign(d2, d1);
  392. crypto_digest_add_bytes(d2, "mno", 3);
  393. crypto_digest_get_digest(d2, d_out1, DIGEST512_LEN);
  394. crypto_digest512(d_out2, "abcdefmno", 9, DIGEST_SHA512);
  395. tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST512_LEN);
  396. crypto_digest_get_digest(d1, d_out1, DIGEST512_LEN);
  397. crypto_digest512(d_out2, "abcdef", 6, DIGEST_SHA512);
  398. tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST512_LEN);
  399. done:
  400. if (d1)
  401. crypto_digest_free(d1);
  402. if (d2)
  403. crypto_digest_free(d2);
  404. tor_free(mem_op_hex_tmp);
  405. }
  406. static void
  407. test_crypto_sha3(void *arg)
  408. {
  409. crypto_digest_t *d1 = NULL, *d2 = NULL;
  410. int i;
  411. char data[DIGEST512_LEN];
  412. char d_out1[DIGEST512_LEN], d_out2[DIGEST512_LEN];
  413. char *mem_op_hex_tmp=NULL;
  414. (void)arg;
  415. /* Test SHA3-[256,512] with a test vectors from the Keccak Code Package.
  416. *
  417. * NB: The code package's test vectors have length expressed in bits.
  418. */
  419. /* Len = 8, Msg = CC */
  420. const uint8_t keccak_kat_msg8[] = { 0xcc };
  421. i = crypto_digest256(data, (const char*)keccak_kat_msg8, 1, DIGEST_SHA3_256);
  422. test_memeq_hex(data, "677035391CD3701293D385F037BA3279"
  423. "6252BB7CE180B00B582DD9B20AAAD7F0");
  424. tt_int_op(i, OP_EQ, 0);
  425. i = crypto_digest512(data, (const char*)keccak_kat_msg8, 1, DIGEST_SHA3_512);
  426. test_memeq_hex(data, "3939FCC8B57B63612542DA31A834E5DC"
  427. "C36E2EE0F652AC72E02624FA2E5ADEEC"
  428. "C7DD6BB3580224B4D6138706FC6E8059"
  429. "7B528051230B00621CC2B22999EAA205");
  430. tt_int_op(i, OP_EQ, 0);
  431. /* Len = 24, Msg = 1F877C */
  432. const uint8_t keccak_kat_msg24[] = { 0x1f, 0x87, 0x7c };
  433. i = crypto_digest256(data, (const char*)keccak_kat_msg24, 3, DIGEST_SHA3_256);
  434. test_memeq_hex(data, "BC22345E4BD3F792A341CF18AC0789F1"
  435. "C9C966712A501B19D1B6632CCD408EC5");
  436. tt_int_op(i, OP_EQ, 0);
  437. i = crypto_digest512(data, (const char*)keccak_kat_msg24, 3, DIGEST_SHA3_512);
  438. test_memeq_hex(data, "CB20DCF54955F8091111688BECCEF48C"
  439. "1A2F0D0608C3A575163751F002DB30F4"
  440. "0F2F671834B22D208591CFAF1F5ECFE4"
  441. "3C49863A53B3225BDFD7C6591BA7658B");
  442. tt_int_op(i, OP_EQ, 0);
  443. /* Len = 1080, Msg = B771D5CEF... ...C35AC81B5 (SHA3-256 rate - 1) */
  444. const uint8_t keccak_kat_msg1080[] = {
  445. 0xB7, 0x71, 0xD5, 0xCE, 0xF5, 0xD1, 0xA4, 0x1A, 0x93, 0xD1,
  446. 0x56, 0x43, 0xD7, 0x18, 0x1D, 0x2A, 0x2E, 0xF0, 0xA8, 0xE8,
  447. 0x4D, 0x91, 0x81, 0x2F, 0x20, 0xED, 0x21, 0xF1, 0x47, 0xBE,
  448. 0xF7, 0x32, 0xBF, 0x3A, 0x60, 0xEF, 0x40, 0x67, 0xC3, 0x73,
  449. 0x4B, 0x85, 0xBC, 0x8C, 0xD4, 0x71, 0x78, 0x0F, 0x10, 0xDC,
  450. 0x9E, 0x82, 0x91, 0xB5, 0x83, 0x39, 0xA6, 0x77, 0xB9, 0x60,
  451. 0x21, 0x8F, 0x71, 0xE7, 0x93, 0xF2, 0x79, 0x7A, 0xEA, 0x34,
  452. 0x94, 0x06, 0x51, 0x28, 0x29, 0x06, 0x5D, 0x37, 0xBB, 0x55,
  453. 0xEA, 0x79, 0x6F, 0xA4, 0xF5, 0x6F, 0xD8, 0x89, 0x6B, 0x49,
  454. 0xB2, 0xCD, 0x19, 0xB4, 0x32, 0x15, 0xAD, 0x96, 0x7C, 0x71,
  455. 0x2B, 0x24, 0xE5, 0x03, 0x2D, 0x06, 0x52, 0x32, 0xE0, 0x2C,
  456. 0x12, 0x74, 0x09, 0xD2, 0xED, 0x41, 0x46, 0xB9, 0xD7, 0x5D,
  457. 0x76, 0x3D, 0x52, 0xDB, 0x98, 0xD9, 0x49, 0xD3, 0xB0, 0xFE,
  458. 0xD6, 0xA8, 0x05, 0x2F, 0xBB,
  459. };
  460. i = crypto_digest256(data, (const char*)keccak_kat_msg1080, 135, DIGEST_SHA3_256);
  461. test_memeq_hex(data, "A19EEE92BB2097B64E823D597798AA18"
  462. "BE9B7C736B8059ABFD6779AC35AC81B5");
  463. tt_int_op(i, OP_EQ, 0);
  464. i = crypto_digest512(data, (const char*)keccak_kat_msg1080, 135, DIGEST_SHA3_512);
  465. test_memeq_hex(data, "7575A1FB4FC9A8F9C0466BD5FCA496D1"
  466. "CB78696773A212A5F62D02D14E3259D1"
  467. "92A87EBA4407DD83893527331407B6DA"
  468. "DAAD920DBC46489B677493CE5F20B595");
  469. tt_int_op(i, OP_EQ, 0);
  470. /* Len = 1088, Msg = B32D95B0... ...8E380C04 (SHA3-256 rate) */
  471. const uint8_t keccak_kat_msg1088[] = {
  472. 0xB3, 0x2D, 0x95, 0xB0, 0xB9, 0xAA, 0xD2, 0xA8, 0x81, 0x6D,
  473. 0xE6, 0xD0, 0x6D, 0x1F, 0x86, 0x00, 0x85, 0x05, 0xBD, 0x8C,
  474. 0x14, 0x12, 0x4F, 0x6E, 0x9A, 0x16, 0x3B, 0x5A, 0x2A, 0xDE,
  475. 0x55, 0xF8, 0x35, 0xD0, 0xEC, 0x38, 0x80, 0xEF, 0x50, 0x70,
  476. 0x0D, 0x3B, 0x25, 0xE4, 0x2C, 0xC0, 0xAF, 0x05, 0x0C, 0xCD,
  477. 0x1B, 0xE5, 0xE5, 0x55, 0xB2, 0x30, 0x87, 0xE0, 0x4D, 0x7B,
  478. 0xF9, 0x81, 0x36, 0x22, 0x78, 0x0C, 0x73, 0x13, 0xA1, 0x95,
  479. 0x4F, 0x87, 0x40, 0xB6, 0xEE, 0x2D, 0x3F, 0x71, 0xF7, 0x68,
  480. 0xDD, 0x41, 0x7F, 0x52, 0x04, 0x82, 0xBD, 0x3A, 0x08, 0xD4,
  481. 0xF2, 0x22, 0xB4, 0xEE, 0x9D, 0xBD, 0x01, 0x54, 0x47, 0xB3,
  482. 0x35, 0x07, 0xDD, 0x50, 0xF3, 0xAB, 0x42, 0x47, 0xC5, 0xDE,
  483. 0x9A, 0x8A, 0xBD, 0x62, 0xA8, 0xDE, 0xCE, 0xA0, 0x1E, 0x3B,
  484. 0x87, 0xC8, 0xB9, 0x27, 0xF5, 0xB0, 0x8B, 0xEB, 0x37, 0x67,
  485. 0x4C, 0x6F, 0x8E, 0x38, 0x0C, 0x04,
  486. };
  487. i = crypto_digest256(data, (const char*)keccak_kat_msg1088, 136, DIGEST_SHA3_256);
  488. test_memeq_hex(data, "DF673F4105379FF6B755EEAB20CEB0DC"
  489. "77B5286364FE16C59CC8A907AFF07732");
  490. tt_int_op(i, OP_EQ, 0);
  491. i = crypto_digest512(data, (const char*)keccak_kat_msg1088, 136, DIGEST_SHA3_512);
  492. test_memeq_hex(data, "2E293765022D48996CE8EFF0BE54E87E"
  493. "FB94A14C72DE5ACD10D0EB5ECE029CAD"
  494. "FA3BA17A40B2FFA2163991B17786E51C"
  495. "ABA79E5E0FFD34CF085E2A098BE8BACB");
  496. tt_int_op(i, OP_EQ, 0);
  497. /* Len = 1096, Msg = 04410E310... ...601016A0D (SHA3-256 rate + 1) */
  498. const uint8_t keccak_kat_msg1096[] = {
  499. 0x04, 0x41, 0x0E, 0x31, 0x08, 0x2A, 0x47, 0x58, 0x4B, 0x40,
  500. 0x6F, 0x05, 0x13, 0x98, 0xA6, 0xAB, 0xE7, 0x4E, 0x4D, 0xA5,
  501. 0x9B, 0xB6, 0xF8, 0x5E, 0x6B, 0x49, 0xE8, 0xA1, 0xF7, 0xF2,
  502. 0xCA, 0x00, 0xDF, 0xBA, 0x54, 0x62, 0xC2, 0xCD, 0x2B, 0xFD,
  503. 0xE8, 0xB6, 0x4F, 0xB2, 0x1D, 0x70, 0xC0, 0x83, 0xF1, 0x13,
  504. 0x18, 0xB5, 0x6A, 0x52, 0xD0, 0x3B, 0x81, 0xCA, 0xC5, 0xEE,
  505. 0xC2, 0x9E, 0xB3, 0x1B, 0xD0, 0x07, 0x8B, 0x61, 0x56, 0x78,
  506. 0x6D, 0xA3, 0xD6, 0xD8, 0xC3, 0x30, 0x98, 0xC5, 0xC4, 0x7B,
  507. 0xB6, 0x7A, 0xC6, 0x4D, 0xB1, 0x41, 0x65, 0xAF, 0x65, 0xB4,
  508. 0x45, 0x44, 0xD8, 0x06, 0xDD, 0xE5, 0xF4, 0x87, 0xD5, 0x37,
  509. 0x3C, 0x7F, 0x97, 0x92, 0xC2, 0x99, 0xE9, 0x68, 0x6B, 0x7E,
  510. 0x58, 0x21, 0xE7, 0xC8, 0xE2, 0x45, 0x83, 0x15, 0xB9, 0x96,
  511. 0xB5, 0x67, 0x7D, 0x92, 0x6D, 0xAC, 0x57, 0xB3, 0xF2, 0x2D,
  512. 0xA8, 0x73, 0xC6, 0x01, 0x01, 0x6A, 0x0D,
  513. };
  514. i = crypto_digest256(data, (const char*)keccak_kat_msg1096, 137, DIGEST_SHA3_256);
  515. test_memeq_hex(data, "D52432CF3B6B4B949AA848E058DCD62D"
  516. "735E0177279222E7AC0AF8504762FAA0");
  517. tt_int_op(i, OP_EQ, 0);
  518. i = crypto_digest512(data, (const char*)keccak_kat_msg1096, 137, DIGEST_SHA3_512);
  519. test_memeq_hex(data, "BE8E14B6757FFE53C9B75F6DDE9A7B6C"
  520. "40474041DE83D4A60645A826D7AF1ABE"
  521. "1EEFCB7B74B62CA6A514E5F2697D585B"
  522. "FECECE12931BBE1D4ED7EBF7B0BE660E");
  523. tt_int_op(i, OP_EQ, 0);
  524. /* Len = 1144, Msg = EA40E83C... ...66DFAFEC (SHA3-512 rate *2 - 1) */
  525. const uint8_t keccak_kat_msg1144[] = {
  526. 0xEA, 0x40, 0xE8, 0x3C, 0xB1, 0x8B, 0x3A, 0x24, 0x2C, 0x1E,
  527. 0xCC, 0x6C, 0xCD, 0x0B, 0x78, 0x53, 0xA4, 0x39, 0xDA, 0xB2,
  528. 0xC5, 0x69, 0xCF, 0xC6, 0xDC, 0x38, 0xA1, 0x9F, 0x5C, 0x90,
  529. 0xAC, 0xBF, 0x76, 0xAE, 0xF9, 0xEA, 0x37, 0x42, 0xFF, 0x3B,
  530. 0x54, 0xEF, 0x7D, 0x36, 0xEB, 0x7C, 0xE4, 0xFF, 0x1C, 0x9A,
  531. 0xB3, 0xBC, 0x11, 0x9C, 0xFF, 0x6B, 0xE9, 0x3C, 0x03, 0xE2,
  532. 0x08, 0x78, 0x33, 0x35, 0xC0, 0xAB, 0x81, 0x37, 0xBE, 0x5B,
  533. 0x10, 0xCD, 0xC6, 0x6F, 0xF3, 0xF8, 0x9A, 0x1B, 0xDD, 0xC6,
  534. 0xA1, 0xEE, 0xD7, 0x4F, 0x50, 0x4C, 0xBE, 0x72, 0x90, 0x69,
  535. 0x0B, 0xB2, 0x95, 0xA8, 0x72, 0xB9, 0xE3, 0xFE, 0x2C, 0xEE,
  536. 0x9E, 0x6C, 0x67, 0xC4, 0x1D, 0xB8, 0xEF, 0xD7, 0xD8, 0x63,
  537. 0xCF, 0x10, 0xF8, 0x40, 0xFE, 0x61, 0x8E, 0x79, 0x36, 0xDA,
  538. 0x3D, 0xCA, 0x5C, 0xA6, 0xDF, 0x93, 0x3F, 0x24, 0xF6, 0x95,
  539. 0x4B, 0xA0, 0x80, 0x1A, 0x12, 0x94, 0xCD, 0x8D, 0x7E, 0x66,
  540. 0xDF, 0xAF, 0xEC,
  541. };
  542. i = crypto_digest512(data, (const char*)keccak_kat_msg1144, 143, DIGEST_SHA3_512);
  543. test_memeq_hex(data, "3A8E938C45F3F177991296B24565D9A6"
  544. "605516615D96A062C8BE53A0D6C5A648"
  545. "7BE35D2A8F3CF6620D0C2DBA2C560D68"
  546. "295F284BE7F82F3B92919033C9CE5D80");
  547. tt_int_op(i, OP_EQ, 0);
  548. i = crypto_digest256(data, (const char*)keccak_kat_msg1144, 143, DIGEST_SHA3_256);
  549. test_memeq_hex(data, "E58A947E98D6DD7E932D2FE02D9992E6"
  550. "118C0C2C606BDCDA06E7943D2C95E0E5");
  551. tt_int_op(i, OP_EQ, 0);
  552. /* Len = 1152, Msg = 157D5B7E... ...79EE00C63 (SHA3-512 rate * 2) */
  553. const uint8_t keccak_kat_msg1152[] = {
  554. 0x15, 0x7D, 0x5B, 0x7E, 0x45, 0x07, 0xF6, 0x6D, 0x9A, 0x26,
  555. 0x74, 0x76, 0xD3, 0x38, 0x31, 0xE7, 0xBB, 0x76, 0x8D, 0x4D,
  556. 0x04, 0xCC, 0x34, 0x38, 0xDA, 0x12, 0xF9, 0x01, 0x02, 0x63,
  557. 0xEA, 0x5F, 0xCA, 0xFB, 0xDE, 0x25, 0x79, 0xDB, 0x2F, 0x6B,
  558. 0x58, 0xF9, 0x11, 0xD5, 0x93, 0xD5, 0xF7, 0x9F, 0xB0, 0x5F,
  559. 0xE3, 0x59, 0x6E, 0x3F, 0xA8, 0x0F, 0xF2, 0xF7, 0x61, 0xD1,
  560. 0xB0, 0xE5, 0x70, 0x80, 0x05, 0x5C, 0x11, 0x8C, 0x53, 0xE5,
  561. 0x3C, 0xDB, 0x63, 0x05, 0x52, 0x61, 0xD7, 0xC9, 0xB2, 0xB3,
  562. 0x9B, 0xD9, 0x0A, 0xCC, 0x32, 0x52, 0x0C, 0xBB, 0xDB, 0xDA,
  563. 0x2C, 0x4F, 0xD8, 0x85, 0x6D, 0xBC, 0xEE, 0x17, 0x31, 0x32,
  564. 0xA2, 0x67, 0x91, 0x98, 0xDA, 0xF8, 0x30, 0x07, 0xA9, 0xB5,
  565. 0xC5, 0x15, 0x11, 0xAE, 0x49, 0x76, 0x6C, 0x79, 0x2A, 0x29,
  566. 0x52, 0x03, 0x88, 0x44, 0x4E, 0xBE, 0xFE, 0x28, 0x25, 0x6F,
  567. 0xB3, 0x3D, 0x42, 0x60, 0x43, 0x9C, 0xBA, 0x73, 0xA9, 0x47,
  568. 0x9E, 0xE0, 0x0C, 0x63,
  569. };
  570. i = crypto_digest512(data, (const char*)keccak_kat_msg1152, 144, DIGEST_SHA3_512);
  571. test_memeq_hex(data, "FE45289874879720CE2A844AE34BB735"
  572. "22775DCB6019DCD22B8885994672A088"
  573. "9C69E8115C641DC8B83E39F7311815A1"
  574. "64DC46E0BA2FCA344D86D4BC2EF2532C");
  575. tt_int_op(i, OP_EQ, 0);
  576. i = crypto_digest256(data, (const char*)keccak_kat_msg1152, 144, DIGEST_SHA3_256);
  577. test_memeq_hex(data, "A936FB9AF87FB67857B3EAD5C76226AD"
  578. "84DA47678F3C2FFE5A39FDB5F7E63FFB");
  579. tt_int_op(i, OP_EQ, 0);
  580. /* Len = 1160, Msg = 836B34B5... ...11044C53 (SHA3-512 rate * 2 + 1) */
  581. const uint8_t keccak_kat_msg1160[] = {
  582. 0x83, 0x6B, 0x34, 0xB5, 0x15, 0x47, 0x6F, 0x61, 0x3F, 0xE4,
  583. 0x47, 0xA4, 0xE0, 0xC3, 0xF3, 0xB8, 0xF2, 0x09, 0x10, 0xAC,
  584. 0x89, 0xA3, 0x97, 0x70, 0x55, 0xC9, 0x60, 0xD2, 0xD5, 0xD2,
  585. 0xB7, 0x2B, 0xD8, 0xAC, 0xC7, 0x15, 0xA9, 0x03, 0x53, 0x21,
  586. 0xB8, 0x67, 0x03, 0xA4, 0x11, 0xDD, 0xE0, 0x46, 0x6D, 0x58,
  587. 0xA5, 0x97, 0x69, 0x67, 0x2A, 0xA6, 0x0A, 0xD5, 0x87, 0xB8,
  588. 0x48, 0x1D, 0xE4, 0xBB, 0xA5, 0x52, 0xA1, 0x64, 0x57, 0x79,
  589. 0x78, 0x95, 0x01, 0xEC, 0x53, 0xD5, 0x40, 0xB9, 0x04, 0x82,
  590. 0x1F, 0x32, 0xB0, 0xBD, 0x18, 0x55, 0xB0, 0x4E, 0x48, 0x48,
  591. 0xF9, 0xF8, 0xCF, 0xE9, 0xEB, 0xD8, 0x91, 0x1B, 0xE9, 0x57,
  592. 0x81, 0xA7, 0x59, 0xD7, 0xAD, 0x97, 0x24, 0xA7, 0x10, 0x2D,
  593. 0xBE, 0x57, 0x67, 0x76, 0xB7, 0xC6, 0x32, 0xBC, 0x39, 0xB9,
  594. 0xB5, 0xE1, 0x90, 0x57, 0xE2, 0x26, 0x55, 0x2A, 0x59, 0x94,
  595. 0xC1, 0xDB, 0xB3, 0xB5, 0xC7, 0x87, 0x1A, 0x11, 0xF5, 0x53,
  596. 0x70, 0x11, 0x04, 0x4C, 0x53,
  597. };
  598. i = crypto_digest512(data, (const char*)keccak_kat_msg1160, 145, DIGEST_SHA3_512);
  599. test_memeq_hex(data, "AFF61C6E11B98E55AC213B1A0BC7DE04"
  600. "05221AC5EFB1229842E4614F4A029C9B"
  601. "D14A0ED7FD99AF3681429F3F309FDB53"
  602. "166AA9A3CD9F1F1223D04B4A9015E94A");
  603. tt_int_op(i, OP_EQ, 0);
  604. i = crypto_digest256(data, (const char*)keccak_kat_msg1160, 145, DIGEST_SHA3_256);
  605. test_memeq_hex(data, "3A654B88F88086C2751EDAE6D3924814"
  606. "3CF6235C6B0B7969342C45A35194B67E");
  607. tt_int_op(i, OP_EQ, 0);
  608. /* SHA3-[256,512] Empty case (wikipedia) */
  609. i = crypto_digest256(data, "", 0, DIGEST_SHA3_256);
  610. test_memeq_hex(data, "a7ffc6f8bf1ed76651c14756a061d662"
  611. "f580ff4de43b49fa82d80a4b80f8434a");
  612. tt_int_op(i, OP_EQ, 0);
  613. i = crypto_digest512(data, "", 0, DIGEST_SHA3_512);
  614. test_memeq_hex(data, "a69f73cca23a9ac5c8b567dc185a756e"
  615. "97c982164fe25859e0d1dcc1475c80a6"
  616. "15b2123af1f5f94c11e3e9402c3ac558"
  617. "f500199d95b6d3e301758586281dcd26");
  618. tt_int_op(i, OP_EQ, 0);
  619. /* Incremental digest code with SHA3-256 */
  620. d1 = crypto_digest256_new(DIGEST_SHA3_256);
  621. tt_assert(d1);
  622. crypto_digest_add_bytes(d1, "abcdef", 6);
  623. d2 = crypto_digest_dup(d1);
  624. tt_assert(d2);
  625. crypto_digest_add_bytes(d2, "ghijkl", 6);
  626. crypto_digest_get_digest(d2, d_out1, DIGEST256_LEN);
  627. crypto_digest256(d_out2, "abcdefghijkl", 12, DIGEST_SHA3_256);
  628. tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST256_LEN);
  629. crypto_digest_assign(d2, d1);
  630. crypto_digest_add_bytes(d2, "mno", 3);
  631. crypto_digest_get_digest(d2, d_out1, DIGEST256_LEN);
  632. crypto_digest256(d_out2, "abcdefmno", 9, DIGEST_SHA3_256);
  633. tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST256_LEN);
  634. crypto_digest_get_digest(d1, d_out1, DIGEST256_LEN);
  635. crypto_digest256(d_out2, "abcdef", 6, DIGEST_SHA3_256);
  636. tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST256_LEN);
  637. crypto_digest_free(d1);
  638. crypto_digest_free(d2);
  639. /* Incremental digest code with SHA3-512 */
  640. d1 = crypto_digest512_new(DIGEST_SHA3_512);
  641. tt_assert(d1);
  642. crypto_digest_add_bytes(d1, "abcdef", 6);
  643. d2 = crypto_digest_dup(d1);
  644. tt_assert(d2);
  645. crypto_digest_add_bytes(d2, "ghijkl", 6);
  646. crypto_digest_get_digest(d2, d_out1, DIGEST512_LEN);
  647. crypto_digest512(d_out2, "abcdefghijkl", 12, DIGEST_SHA3_512);
  648. tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST512_LEN);
  649. crypto_digest_assign(d2, d1);
  650. crypto_digest_add_bytes(d2, "mno", 3);
  651. crypto_digest_get_digest(d2, d_out1, DIGEST512_LEN);
  652. crypto_digest512(d_out2, "abcdefmno", 9, DIGEST_SHA3_512);
  653. tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST512_LEN);
  654. crypto_digest_get_digest(d1, d_out1, DIGEST512_LEN);
  655. crypto_digest512(d_out2, "abcdef", 6, DIGEST_SHA3_512);
  656. tt_mem_op(d_out1,OP_EQ, d_out2, DIGEST512_LEN);
  657. done:
  658. if (d1)
  659. crypto_digest_free(d1);
  660. if (d2)
  661. crypto_digest_free(d2);
  662. tor_free(mem_op_hex_tmp);
  663. }
  664. /** Run unit tests for our public key crypto functions */
  665. static void
  666. test_crypto_pk(void *arg)
  667. {
  668. crypto_pk_t *pk1 = NULL, *pk2 = NULL;
  669. char *encoded = NULL;
  670. char data1[1024], data2[1024], data3[1024];
  671. size_t size;
  672. int i, len;
  673. /* Public-key ciphers */
  674. (void)arg;
  675. pk1 = pk_generate(0);
  676. pk2 = crypto_pk_new();
  677. tt_assert(pk1 && pk2);
  678. tt_assert(! crypto_pk_write_public_key_to_string(pk1, &encoded, &size));
  679. tt_assert(! crypto_pk_read_public_key_from_string(pk2, encoded, size));
  680. tt_int_op(0,OP_EQ, crypto_pk_cmp_keys(pk1, pk2));
  681. /* comparison between keys and NULL */
  682. tt_int_op(crypto_pk_cmp_keys(NULL, pk1), OP_LT, 0);
  683. tt_int_op(crypto_pk_cmp_keys(NULL, NULL), OP_EQ, 0);
  684. tt_int_op(crypto_pk_cmp_keys(pk1, NULL), OP_GT, 0);
  685. tt_int_op(128,OP_EQ, crypto_pk_keysize(pk1));
  686. tt_int_op(1024,OP_EQ, crypto_pk_num_bits(pk1));
  687. tt_int_op(128,OP_EQ, crypto_pk_keysize(pk2));
  688. tt_int_op(1024,OP_EQ, crypto_pk_num_bits(pk2));
  689. tt_int_op(128,OP_EQ, crypto_pk_public_encrypt(pk2, data1, sizeof(data1),
  690. "Hello whirled.", 15,
  691. PK_PKCS1_OAEP_PADDING));
  692. tt_int_op(128,OP_EQ, crypto_pk_public_encrypt(pk1, data2, sizeof(data1),
  693. "Hello whirled.", 15,
  694. PK_PKCS1_OAEP_PADDING));
  695. /* oaep padding should make encryption not match */
  696. tt_mem_op(data1,OP_NE, data2, 128);
  697. tt_int_op(15,OP_EQ,
  698. crypto_pk_private_decrypt(pk1, data3, sizeof(data3), data1, 128,
  699. PK_PKCS1_OAEP_PADDING,1));
  700. tt_str_op(data3,OP_EQ, "Hello whirled.");
  701. memset(data3, 0, 1024);
  702. tt_int_op(15,OP_EQ,
  703. crypto_pk_private_decrypt(pk1, data3, sizeof(data3), data2, 128,
  704. PK_PKCS1_OAEP_PADDING,1));
  705. tt_str_op(data3,OP_EQ, "Hello whirled.");
  706. /* Can't decrypt with public key. */
  707. tt_int_op(-1,OP_EQ,
  708. crypto_pk_private_decrypt(pk2, data3, sizeof(data3), data2, 128,
  709. PK_PKCS1_OAEP_PADDING,1));
  710. /* Try again with bad padding */
  711. memcpy(data2+1, "XYZZY", 5); /* This has fails ~ once-in-2^40 */
  712. tt_int_op(-1,OP_EQ,
  713. crypto_pk_private_decrypt(pk1, data3, sizeof(data3), data2, 128,
  714. PK_PKCS1_OAEP_PADDING,1));
  715. /* File operations: save and load private key */
  716. tt_assert(! crypto_pk_write_private_key_to_filename(pk1,
  717. get_fname("pkey1")));
  718. /* failing case for read: can't read. */
  719. tt_assert(crypto_pk_read_private_key_from_filename(pk2,
  720. get_fname("xyzzy")) < 0);
  721. write_str_to_file(get_fname("xyzzy"), "foobar", 6);
  722. /* Failing case for read: no key. */
  723. tt_assert(crypto_pk_read_private_key_from_filename(pk2,
  724. get_fname("xyzzy")) < 0);
  725. tt_assert(! crypto_pk_read_private_key_from_filename(pk2,
  726. get_fname("pkey1")));
  727. tt_int_op(15,OP_EQ,
  728. crypto_pk_private_decrypt(pk2, data3, sizeof(data3), data1, 128,
  729. PK_PKCS1_OAEP_PADDING,1));
  730. /* Now try signing. */
  731. strlcpy(data1, "Ossifrage", 1024);
  732. tt_int_op(128,OP_EQ,
  733. crypto_pk_private_sign(pk1, data2, sizeof(data2), data1, 10));
  734. tt_int_op(10,OP_EQ,
  735. crypto_pk_public_checksig(pk1, data3, sizeof(data3), data2, 128));
  736. tt_str_op(data3,OP_EQ, "Ossifrage");
  737. /* Try signing digests. */
  738. tt_int_op(128,OP_EQ, crypto_pk_private_sign_digest(pk1, data2, sizeof(data2),
  739. data1, 10));
  740. tt_int_op(20,OP_EQ,
  741. crypto_pk_public_checksig(pk1, data3, sizeof(data3), data2, 128));
  742. tt_int_op(0,OP_EQ,
  743. crypto_pk_public_checksig_digest(pk1, data1, 10, data2, 128));
  744. tt_int_op(-1,OP_EQ,
  745. crypto_pk_public_checksig_digest(pk1, data1, 11, data2, 128));
  746. /*XXXX test failed signing*/
  747. /* Try encoding */
  748. crypto_pk_free(pk2);
  749. pk2 = NULL;
  750. i = crypto_pk_asn1_encode(pk1, data1, 1024);
  751. tt_int_op(i, OP_GT, 0);
  752. pk2 = crypto_pk_asn1_decode(data1, i);
  753. tt_assert(crypto_pk_cmp_keys(pk1,pk2) == 0);
  754. /* Try with hybrid encryption wrappers. */
  755. crypto_rand(data1, 1024);
  756. for (i = 85; i < 140; ++i) {
  757. memset(data2,0,1024);
  758. memset(data3,0,1024);
  759. len = crypto_pk_public_hybrid_encrypt(pk1,data2,sizeof(data2),
  760. data1,i,PK_PKCS1_OAEP_PADDING,0);
  761. tt_int_op(len, OP_GE, 0);
  762. len = crypto_pk_private_hybrid_decrypt(pk1,data3,sizeof(data3),
  763. data2,len,PK_PKCS1_OAEP_PADDING,1);
  764. tt_int_op(len,OP_EQ, i);
  765. tt_mem_op(data1,OP_EQ, data3,i);
  766. }
  767. /* Try copy_full */
  768. crypto_pk_free(pk2);
  769. pk2 = crypto_pk_copy_full(pk1);
  770. tt_assert(pk2 != NULL);
  771. tt_ptr_op(pk1, OP_NE, pk2);
  772. tt_assert(crypto_pk_cmp_keys(pk1,pk2) == 0);
  773. done:
  774. if (pk1)
  775. crypto_pk_free(pk1);
  776. if (pk2)
  777. crypto_pk_free(pk2);
  778. tor_free(encoded);
  779. }
  780. static void
  781. test_crypto_pk_fingerprints(void *arg)
  782. {
  783. crypto_pk_t *pk = NULL;
  784. char encoded[512];
  785. char d[DIGEST_LEN], d2[DIGEST_LEN];
  786. char fingerprint[FINGERPRINT_LEN+1];
  787. int n;
  788. unsigned i;
  789. char *mem_op_hex_tmp=NULL;
  790. (void)arg;
  791. pk = pk_generate(1);
  792. tt_assert(pk);
  793. n = crypto_pk_asn1_encode(pk, encoded, sizeof(encoded));
  794. tt_int_op(n, OP_GT, 0);
  795. tt_int_op(n, OP_GT, 128);
  796. tt_int_op(n, OP_LT, 256);
  797. /* Is digest as expected? */
  798. crypto_digest(d, encoded, n);
  799. tt_int_op(0, OP_EQ, crypto_pk_get_digest(pk, d2));
  800. tt_mem_op(d,OP_EQ, d2, DIGEST_LEN);
  801. /* Is fingerprint right? */
  802. tt_int_op(0, OP_EQ, crypto_pk_get_fingerprint(pk, fingerprint, 0));
  803. tt_int_op(strlen(fingerprint), OP_EQ, DIGEST_LEN * 2);
  804. test_memeq_hex(d, fingerprint);
  805. /* Are spaces right? */
  806. tt_int_op(0, OP_EQ, crypto_pk_get_fingerprint(pk, fingerprint, 1));
  807. for (i = 4; i < strlen(fingerprint); i += 5) {
  808. tt_int_op(fingerprint[i], OP_EQ, ' ');
  809. }
  810. tor_strstrip(fingerprint, " ");
  811. tt_int_op(strlen(fingerprint), OP_EQ, DIGEST_LEN * 2);
  812. test_memeq_hex(d, fingerprint);
  813. /* Now hash again and check crypto_pk_get_hashed_fingerprint. */
  814. crypto_digest(d2, d, sizeof(d));
  815. tt_int_op(0, OP_EQ, crypto_pk_get_hashed_fingerprint(pk, fingerprint));
  816. tt_int_op(strlen(fingerprint), OP_EQ, DIGEST_LEN * 2);
  817. test_memeq_hex(d2, fingerprint);
  818. done:
  819. crypto_pk_free(pk);
  820. tor_free(mem_op_hex_tmp);
  821. }
  822. static void
  823. test_crypto_pk_base64(void *arg)
  824. {
  825. crypto_pk_t *pk1 = NULL;
  826. crypto_pk_t *pk2 = NULL;
  827. char *encoded = NULL;
  828. (void)arg;
  829. /* Test Base64 encoding a key. */
  830. pk1 = pk_generate(0);
  831. tt_assert(pk1);
  832. tt_int_op(0, OP_EQ, crypto_pk_base64_encode(pk1, &encoded));
  833. tt_assert(encoded);
  834. /* Test decoding a valid key. */
  835. pk2 = crypto_pk_base64_decode(encoded, strlen(encoded));
  836. tt_assert(pk2);
  837. tt_assert(crypto_pk_cmp_keys(pk1,pk2) == 0);
  838. crypto_pk_free(pk2);
  839. /* Test decoding a invalid key (not Base64). */
  840. static const char *invalid_b64 = "The key is in another castle!";
  841. pk2 = crypto_pk_base64_decode(invalid_b64, strlen(invalid_b64));
  842. tt_assert(!pk2);
  843. /* Test decoding a truncated Base64 blob. */
  844. pk2 = crypto_pk_base64_decode(encoded, strlen(encoded)/2);
  845. tt_assert(!pk2);
  846. done:
  847. crypto_pk_free(pk1);
  848. crypto_pk_free(pk2);
  849. tor_free(encoded);
  850. }
  851. /** Sanity check for crypto pk digests */
  852. static void
  853. test_crypto_digests(void *arg)
  854. {
  855. crypto_pk_t *k = NULL;
  856. ssize_t r;
  857. digests_t pkey_digests;
  858. char digest[DIGEST_LEN];
  859. (void)arg;
  860. k = crypto_pk_new();
  861. tt_assert(k);
  862. r = crypto_pk_read_private_key_from_string(k, AUTHORITY_SIGNKEY_3, -1);
  863. tt_assert(!r);
  864. r = crypto_pk_get_digest(k, digest);
  865. tt_assert(r == 0);
  866. tt_mem_op(hex_str(digest, DIGEST_LEN),OP_EQ,
  867. AUTHORITY_SIGNKEY_A_DIGEST, HEX_DIGEST_LEN);
  868. r = crypto_pk_get_all_digests(k, &pkey_digests);
  869. tt_mem_op(hex_str(pkey_digests.d[DIGEST_SHA1], DIGEST_LEN),OP_EQ,
  870. AUTHORITY_SIGNKEY_A_DIGEST, HEX_DIGEST_LEN);
  871. tt_mem_op(hex_str(pkey_digests.d[DIGEST_SHA256], DIGEST256_LEN),OP_EQ,
  872. AUTHORITY_SIGNKEY_A_DIGEST256, HEX_DIGEST256_LEN);
  873. done:
  874. crypto_pk_free(k);
  875. }
  876. /** Encode src into dest with OpenSSL's EVP Encode interface, returning the
  877. * length of the encoded data in bytes.
  878. */
  879. static int
  880. base64_encode_evp(char *dest, char *src, size_t srclen)
  881. {
  882. const unsigned char *s = (unsigned char*)src;
  883. EVP_ENCODE_CTX ctx;
  884. int len, ret;
  885. EVP_EncodeInit(&ctx);
  886. EVP_EncodeUpdate(&ctx, (unsigned char *)dest, &len, s, (int)srclen);
  887. EVP_EncodeFinal(&ctx, (unsigned char *)(dest + len), &ret);
  888. return ret+ len;
  889. }
  890. /** Run unit tests for misc crypto formatting functionality (base64, base32,
  891. * fingerprints, etc) */
  892. static void
  893. test_crypto_formats(void *arg)
  894. {
  895. char *data1 = NULL, *data2 = NULL, *data3 = NULL;
  896. int i, j, idx;
  897. (void)arg;
  898. data1 = tor_malloc(1024);
  899. data2 = tor_malloc(1024);
  900. data3 = tor_malloc(1024);
  901. tt_assert(data1 && data2 && data3);
  902. /* Base64 tests */
  903. memset(data1, 6, 1024);
  904. for (idx = 0; idx < 10; ++idx) {
  905. i = base64_encode(data2, 1024, data1, idx, 0);
  906. tt_int_op(i, OP_GE, 0);
  907. tt_int_op(i, OP_EQ, strlen(data2));
  908. j = base64_decode(data3, 1024, data2, i);
  909. tt_int_op(j,OP_EQ, idx);
  910. tt_mem_op(data3,OP_EQ, data1, idx);
  911. i = base64_encode_nopad(data2, 1024, (uint8_t*)data1, idx);
  912. tt_int_op(i, OP_GE, 0);
  913. tt_int_op(i, OP_EQ, strlen(data2));
  914. tt_assert(! strchr(data2, '='));
  915. j = base64_decode_nopad((uint8_t*)data3, 1024, data2, i);
  916. tt_int_op(j, OP_EQ, idx);
  917. tt_mem_op(data3,OP_EQ, data1, idx);
  918. }
  919. strlcpy(data1, "Test string that contains 35 chars.", 1024);
  920. strlcat(data1, " 2nd string that contains 35 chars.", 1024);
  921. i = base64_encode(data2, 1024, data1, 71, 0);
  922. tt_int_op(i, OP_GE, 0);
  923. j = base64_decode(data3, 1024, data2, i);
  924. tt_int_op(j,OP_EQ, 71);
  925. tt_str_op(data3,OP_EQ, data1);
  926. tt_int_op(data2[i], OP_EQ, '\0');
  927. crypto_rand(data1, DIGEST_LEN);
  928. memset(data2, 100, 1024);
  929. digest_to_base64(data2, data1);
  930. tt_int_op(BASE64_DIGEST_LEN,OP_EQ, strlen(data2));
  931. tt_int_op(100,OP_EQ, data2[BASE64_DIGEST_LEN+2]);
  932. memset(data3, 99, 1024);
  933. tt_int_op(digest_from_base64(data3, data2),OP_EQ, 0);
  934. tt_mem_op(data1,OP_EQ, data3, DIGEST_LEN);
  935. tt_int_op(99,OP_EQ, data3[DIGEST_LEN+1]);
  936. tt_assert(digest_from_base64(data3, "###") < 0);
  937. for (i = 0; i < 256; i++) {
  938. /* Test the multiline format Base64 encoder with 0 .. 256 bytes of
  939. * output against OpenSSL.
  940. */
  941. const size_t enclen = base64_encode_size(i, BASE64_ENCODE_MULTILINE);
  942. data1[i] = i;
  943. j = base64_encode(data2, 1024, data1, i, BASE64_ENCODE_MULTILINE);
  944. tt_int_op(j, OP_EQ, enclen);
  945. j = base64_encode_evp(data3, data1, i);
  946. tt_int_op(j, OP_EQ, enclen);
  947. tt_mem_op(data2, OP_EQ, data3, enclen);
  948. tt_int_op(j, OP_EQ, strlen(data2));
  949. }
  950. /* Encoding SHA256 */
  951. crypto_rand(data2, DIGEST256_LEN);
  952. memset(data2, 100, 1024);
  953. digest256_to_base64(data2, data1);
  954. tt_int_op(BASE64_DIGEST256_LEN,OP_EQ, strlen(data2));
  955. tt_int_op(100,OP_EQ, data2[BASE64_DIGEST256_LEN+2]);
  956. memset(data3, 99, 1024);
  957. tt_int_op(digest256_from_base64(data3, data2),OP_EQ, 0);
  958. tt_mem_op(data1,OP_EQ, data3, DIGEST256_LEN);
  959. tt_int_op(99,OP_EQ, data3[DIGEST256_LEN+1]);
  960. /* Base32 tests */
  961. strlcpy(data1, "5chrs", 1024);
  962. /* bit pattern is: [35 63 68 72 73] ->
  963. * [00110101 01100011 01101000 01110010 01110011]
  964. * By 5s: [00110 10101 10001 10110 10000 11100 10011 10011]
  965. */
  966. base32_encode(data2, 9, data1, 5);
  967. tt_str_op(data2,OP_EQ, "gvrwq4tt");
  968. strlcpy(data1, "\xFF\xF5\x6D\x44\xAE\x0D\x5C\xC9\x62\xC4", 1024);
  969. base32_encode(data2, 30, data1, 10);
  970. tt_str_op(data2,OP_EQ, "772w2rfobvomsywe");
  971. /* Base16 tests */
  972. strlcpy(data1, "6chrs\xff", 1024);
  973. base16_encode(data2, 13, data1, 6);
  974. tt_str_op(data2,OP_EQ, "3663687273FF");
  975. strlcpy(data1, "f0d678affc000100", 1024);
  976. i = base16_decode(data2, 8, data1, 16);
  977. tt_int_op(i,OP_EQ, 0);
  978. tt_mem_op(data2,OP_EQ, "\xf0\xd6\x78\xaf\xfc\x00\x01\x00",8);
  979. /* now try some failing base16 decodes */
  980. tt_int_op(-1,OP_EQ, base16_decode(data2, 8, data1, 15)); /* odd input len */
  981. tt_int_op(-1,OP_EQ, base16_decode(data2, 7, data1, 16)); /* dest too short */
  982. strlcpy(data1, "f0dz!8affc000100", 1024);
  983. tt_int_op(-1,OP_EQ, base16_decode(data2, 8, data1, 16));
  984. tor_free(data1);
  985. tor_free(data2);
  986. tor_free(data3);
  987. /* Add spaces to fingerprint */
  988. {
  989. data1 = tor_strdup("ABCD1234ABCD56780000ABCD1234ABCD56780000");
  990. tt_int_op(strlen(data1),OP_EQ, 40);
  991. data2 = tor_malloc(FINGERPRINT_LEN+1);
  992. crypto_add_spaces_to_fp(data2, FINGERPRINT_LEN+1, data1);
  993. tt_str_op(data2, OP_EQ,
  994. "ABCD 1234 ABCD 5678 0000 ABCD 1234 ABCD 5678 0000");
  995. tor_free(data1);
  996. tor_free(data2);
  997. }
  998. done:
  999. tor_free(data1);
  1000. tor_free(data2);
  1001. tor_free(data3);
  1002. }
  1003. /** Test AES-CTR encryption and decryption with IV. */
  1004. static void
  1005. test_crypto_aes_iv(void *arg)
  1006. {
  1007. char *plain, *encrypted1, *encrypted2, *decrypted1, *decrypted2;
  1008. char plain_1[1], plain_15[15], plain_16[16], plain_17[17];
  1009. char key1[16], key2[16];
  1010. ssize_t encrypted_size, decrypted_size;
  1011. int use_evp = !strcmp(arg,"evp");
  1012. evaluate_evp_for_aes(use_evp);
  1013. plain = tor_malloc(4095);
  1014. encrypted1 = tor_malloc(4095 + 1 + 16);
  1015. encrypted2 = tor_malloc(4095 + 1 + 16);
  1016. decrypted1 = tor_malloc(4095 + 1);
  1017. decrypted2 = tor_malloc(4095 + 1);
  1018. crypto_rand(plain, 4095);
  1019. crypto_rand(key1, 16);
  1020. crypto_rand(key2, 16);
  1021. crypto_rand(plain_1, 1);
  1022. crypto_rand(plain_15, 15);
  1023. crypto_rand(plain_16, 16);
  1024. crypto_rand(plain_17, 17);
  1025. key1[0] = key2[0] + 128; /* Make sure that contents are different. */
  1026. /* Encrypt and decrypt with the same key. */
  1027. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 4095,
  1028. plain, 4095);
  1029. tt_int_op(encrypted_size,OP_EQ, 16 + 4095);
  1030. tt_assert(encrypted_size > 0); /* This is obviously true, since 4111 is
  1031. * greater than 0, but its truth is not
  1032. * obvious to all analysis tools. */
  1033. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 4095,
  1034. encrypted1, encrypted_size);
  1035. tt_int_op(decrypted_size,OP_EQ, 4095);
  1036. tt_assert(decrypted_size > 0);
  1037. tt_mem_op(plain,OP_EQ, decrypted1, 4095);
  1038. /* Encrypt a second time (with a new random initialization vector). */
  1039. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted2, 16 + 4095,
  1040. plain, 4095);
  1041. tt_int_op(encrypted_size,OP_EQ, 16 + 4095);
  1042. tt_assert(encrypted_size > 0);
  1043. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted2, 4095,
  1044. encrypted2, encrypted_size);
  1045. tt_int_op(decrypted_size,OP_EQ, 4095);
  1046. tt_assert(decrypted_size > 0);
  1047. tt_mem_op(plain,OP_EQ, decrypted2, 4095);
  1048. tt_mem_op(encrypted1,OP_NE, encrypted2, encrypted_size);
  1049. /* Decrypt with the wrong key. */
  1050. decrypted_size = crypto_cipher_decrypt_with_iv(key2, decrypted2, 4095,
  1051. encrypted1, encrypted_size);
  1052. tt_int_op(decrypted_size,OP_EQ, 4095);
  1053. tt_mem_op(plain,OP_NE, decrypted2, decrypted_size);
  1054. /* Alter the initialization vector. */
  1055. encrypted1[0] += 42;
  1056. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 4095,
  1057. encrypted1, encrypted_size);
  1058. tt_int_op(decrypted_size,OP_EQ, 4095);
  1059. tt_mem_op(plain,OP_NE, decrypted2, 4095);
  1060. /* Special length case: 1. */
  1061. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 1,
  1062. plain_1, 1);
  1063. tt_int_op(encrypted_size,OP_EQ, 16 + 1);
  1064. tt_assert(encrypted_size > 0);
  1065. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 1,
  1066. encrypted1, encrypted_size);
  1067. tt_int_op(decrypted_size,OP_EQ, 1);
  1068. tt_assert(decrypted_size > 0);
  1069. tt_mem_op(plain_1,OP_EQ, decrypted1, 1);
  1070. /* Special length case: 15. */
  1071. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 15,
  1072. plain_15, 15);
  1073. tt_int_op(encrypted_size,OP_EQ, 16 + 15);
  1074. tt_assert(encrypted_size > 0);
  1075. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 15,
  1076. encrypted1, encrypted_size);
  1077. tt_int_op(decrypted_size,OP_EQ, 15);
  1078. tt_assert(decrypted_size > 0);
  1079. tt_mem_op(plain_15,OP_EQ, decrypted1, 15);
  1080. /* Special length case: 16. */
  1081. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 16,
  1082. plain_16, 16);
  1083. tt_int_op(encrypted_size,OP_EQ, 16 + 16);
  1084. tt_assert(encrypted_size > 0);
  1085. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 16,
  1086. encrypted1, encrypted_size);
  1087. tt_int_op(decrypted_size,OP_EQ, 16);
  1088. tt_assert(decrypted_size > 0);
  1089. tt_mem_op(plain_16,OP_EQ, decrypted1, 16);
  1090. /* Special length case: 17. */
  1091. encrypted_size = crypto_cipher_encrypt_with_iv(key1, encrypted1, 16 + 17,
  1092. plain_17, 17);
  1093. tt_int_op(encrypted_size,OP_EQ, 16 + 17);
  1094. tt_assert(encrypted_size > 0);
  1095. decrypted_size = crypto_cipher_decrypt_with_iv(key1, decrypted1, 17,
  1096. encrypted1, encrypted_size);
  1097. tt_int_op(decrypted_size,OP_EQ, 17);
  1098. tt_assert(decrypted_size > 0);
  1099. tt_mem_op(plain_17,OP_EQ, decrypted1, 17);
  1100. done:
  1101. /* Free memory. */
  1102. tor_free(plain);
  1103. tor_free(encrypted1);
  1104. tor_free(encrypted2);
  1105. tor_free(decrypted1);
  1106. tor_free(decrypted2);
  1107. }
  1108. /** Test base32 decoding. */
  1109. static void
  1110. test_crypto_base32_decode(void *arg)
  1111. {
  1112. char plain[60], encoded[96 + 1], decoded[60];
  1113. int res;
  1114. (void)arg;
  1115. crypto_rand(plain, 60);
  1116. /* Encode and decode a random string. */
  1117. base32_encode(encoded, 96 + 1, plain, 60);
  1118. res = base32_decode(decoded, 60, encoded, 96);
  1119. tt_int_op(res,OP_EQ, 0);
  1120. tt_mem_op(plain,OP_EQ, decoded, 60);
  1121. /* Encode, uppercase, and decode a random string. */
  1122. base32_encode(encoded, 96 + 1, plain, 60);
  1123. tor_strupper(encoded);
  1124. res = base32_decode(decoded, 60, encoded, 96);
  1125. tt_int_op(res,OP_EQ, 0);
  1126. tt_mem_op(plain,OP_EQ, decoded, 60);
  1127. /* Change encoded string and decode. */
  1128. if (encoded[0] == 'A' || encoded[0] == 'a')
  1129. encoded[0] = 'B';
  1130. else
  1131. encoded[0] = 'A';
  1132. res = base32_decode(decoded, 60, encoded, 96);
  1133. tt_int_op(res,OP_EQ, 0);
  1134. tt_mem_op(plain,OP_NE, decoded, 60);
  1135. /* Bad encodings. */
  1136. encoded[0] = '!';
  1137. res = base32_decode(decoded, 60, encoded, 96);
  1138. tt_int_op(0, OP_GT, res);
  1139. done:
  1140. ;
  1141. }
  1142. static void
  1143. test_crypto_kdf_TAP(void *arg)
  1144. {
  1145. uint8_t key_material[100];
  1146. int r;
  1147. char *mem_op_hex_tmp = NULL;
  1148. (void)arg;
  1149. #define EXPAND(s) \
  1150. r = crypto_expand_key_material_TAP( \
  1151. (const uint8_t*)(s), strlen(s), \
  1152. key_material, 100)
  1153. /* Test vectors generated with a little python script; feel free to write
  1154. * your own. */
  1155. memset(key_material, 0, sizeof(key_material));
  1156. EXPAND("");
  1157. tt_int_op(r, OP_EQ, 0);
  1158. test_memeq_hex(key_material,
  1159. "5ba93c9db0cff93f52b521d7420e43f6eda2784fbf8b4530d8"
  1160. "d246dd74ac53a13471bba17941dff7c4ea21bb365bbeeaf5f2"
  1161. "c654883e56d11e43c44e9842926af7ca0a8cca12604f945414"
  1162. "f07b01e13da42c6cf1de3abfdea9b95f34687cbbe92b9a7383");
  1163. EXPAND("Tor");
  1164. tt_int_op(r, OP_EQ, 0);
  1165. test_memeq_hex(key_material,
  1166. "776c6214fc647aaa5f683c737ee66ec44f03d0372e1cce6922"
  1167. "7950f236ddf1e329a7ce7c227903303f525a8c6662426e8034"
  1168. "870642a6dabbd41b5d97ec9bf2312ea729992f48f8ea2d0ba8"
  1169. "3f45dfda1a80bdc8b80de01b23e3e0ffae099b3e4ccf28dc28");
  1170. EXPAND("AN ALARMING ITEM TO FIND ON A MONTHLY AUTO-DEBIT NOTICE");
  1171. tt_int_op(r, OP_EQ, 0);
  1172. test_memeq_hex(key_material,
  1173. "a340b5d126086c3ab29c2af4179196dbf95e1c72431419d331"
  1174. "4844bf8f6afb6098db952b95581fb6c33625709d6f4400b8e7"
  1175. "ace18a70579fad83c0982ef73f89395bcc39493ad53a685854"
  1176. "daf2ba9b78733b805d9a6824c907ee1dba5ac27a1e466d4d10");
  1177. done:
  1178. tor_free(mem_op_hex_tmp);
  1179. #undef EXPAND
  1180. }
  1181. static void
  1182. test_crypto_hkdf_sha256(void *arg)
  1183. {
  1184. uint8_t key_material[100];
  1185. const uint8_t salt[] = "ntor-curve25519-sha256-1:key_extract";
  1186. const size_t salt_len = strlen((char*)salt);
  1187. const uint8_t m_expand[] = "ntor-curve25519-sha256-1:key_expand";
  1188. const size_t m_expand_len = strlen((char*)m_expand);
  1189. int r;
  1190. char *mem_op_hex_tmp = NULL;
  1191. (void)arg;
  1192. #define EXPAND(s) \
  1193. r = crypto_expand_key_material_rfc5869_sha256( \
  1194. (const uint8_t*)(s), strlen(s), \
  1195. salt, salt_len, \
  1196. m_expand, m_expand_len, \
  1197. key_material, 100)
  1198. /* Test vectors generated with ntor_ref.py */
  1199. memset(key_material, 0, sizeof(key_material));
  1200. EXPAND("");
  1201. tt_int_op(r, OP_EQ, 0);
  1202. test_memeq_hex(key_material,
  1203. "d3490ed48b12a48f9547861583573fe3f19aafe3f81dc7fc75"
  1204. "eeed96d741b3290f941576c1f9f0b2d463d1ec7ab2c6bf71cd"
  1205. "d7f826c6298c00dbfe6711635d7005f0269493edf6046cc7e7"
  1206. "dcf6abe0d20c77cf363e8ffe358927817a3d3e73712cee28d8");
  1207. EXPAND("Tor");
  1208. tt_int_op(r, OP_EQ, 0);
  1209. test_memeq_hex(key_material,
  1210. "5521492a85139a8d9107a2d5c0d9c91610d0f95989975ebee6"
  1211. "c02a4f8d622a6cfdf9b7c7edd3832e2760ded1eac309b76f8d"
  1212. "66c4a3c4d6225429b3a016e3c3d45911152fc87bc2de9630c3"
  1213. "961be9fdb9f93197ea8e5977180801926d3321fa21513e59ac");
  1214. EXPAND("AN ALARMING ITEM TO FIND ON YOUR CREDIT-RATING STATEMENT");
  1215. tt_int_op(r, OP_EQ, 0);
  1216. test_memeq_hex(key_material,
  1217. "a2aa9b50da7e481d30463adb8f233ff06e9571a0ca6ab6df0f"
  1218. "b206fa34e5bc78d063fc291501beec53b36e5a0e434561200c"
  1219. "5f8bd13e0f88b3459600b4dc21d69363e2895321c06184879d"
  1220. "94b18f078411be70b767c7fc40679a9440a0c95ea83a23efbf");
  1221. done:
  1222. tor_free(mem_op_hex_tmp);
  1223. #undef EXPAND
  1224. }
  1225. static void
  1226. test_crypto_curve25519_impl(void *arg)
  1227. {
  1228. /* adapted from curve25519_donna, which adapted it from test-curve25519
  1229. version 20050915, by D. J. Bernstein, Public domain. */
  1230. const int randomize_high_bit = (arg != NULL);
  1231. #ifdef SLOW_CURVE25519_TEST
  1232. const int loop_max=10000;
  1233. const char e1_expected[] = "4faf81190869fd742a33691b0e0824d5"
  1234. "7e0329f4dd2819f5f32d130f1296b500";
  1235. const char e2k_expected[] = "05aec13f92286f3a781ccae98995a3b9"
  1236. "e0544770bc7de853b38f9100489e3e79";
  1237. const char e1e2k_expected[] = "cd6e8269104eb5aaee886bd2071fba88"
  1238. "bd13861475516bc2cd2b6e005e805064";
  1239. #else
  1240. const int loop_max=200;
  1241. const char e1_expected[] = "bc7112cde03f97ef7008cad1bdc56be3"
  1242. "c6a1037d74cceb3712e9206871dcf654";
  1243. const char e2k_expected[] = "dd8fa254fb60bdb5142fe05b1f5de44d"
  1244. "8e3ee1a63c7d14274ea5d4c67f065467";
  1245. const char e1e2k_expected[] = "7ddb98bd89025d2347776b33901b3e7e"
  1246. "c0ee98cb2257a4545c0cfb2ca3e1812b";
  1247. #endif
  1248. unsigned char e1k[32];
  1249. unsigned char e2k[32];
  1250. unsigned char e1e2k[32];
  1251. unsigned char e2e1k[32];
  1252. unsigned char e1[32] = {3};
  1253. unsigned char e2[32] = {5};
  1254. unsigned char k[32] = {9};
  1255. int loop, i;
  1256. char *mem_op_hex_tmp = NULL;
  1257. for (loop = 0; loop < loop_max; ++loop) {
  1258. curve25519_impl(e1k,e1,k);
  1259. curve25519_impl(e2e1k,e2,e1k);
  1260. curve25519_impl(e2k,e2,k);
  1261. if (randomize_high_bit) {
  1262. /* We require that the high bit of the public key be ignored. So if
  1263. * we're doing this variant test, we randomize the high bit of e2k, and
  1264. * make sure that the handshake still works out the same as it would
  1265. * otherwise. */
  1266. uint8_t byte;
  1267. crypto_rand((char*)&byte, 1);
  1268. e2k[31] |= (byte & 0x80);
  1269. }
  1270. curve25519_impl(e1e2k,e1,e2k);
  1271. tt_mem_op(e1e2k,OP_EQ, e2e1k, 32);
  1272. if (loop == loop_max-1) {
  1273. break;
  1274. }
  1275. for (i = 0;i < 32;++i) e1[i] ^= e2k[i];
  1276. for (i = 0;i < 32;++i) e2[i] ^= e1k[i];
  1277. for (i = 0;i < 32;++i) k[i] ^= e1e2k[i];
  1278. }
  1279. test_memeq_hex(e1, e1_expected);
  1280. test_memeq_hex(e2k, e2k_expected);
  1281. test_memeq_hex(e1e2k, e1e2k_expected);
  1282. done:
  1283. tor_free(mem_op_hex_tmp);
  1284. }
  1285. static void
  1286. test_crypto_curve25519_basepoint(void *arg)
  1287. {
  1288. uint8_t secret[32];
  1289. uint8_t public1[32];
  1290. uint8_t public2[32];
  1291. const int iters = 2048;
  1292. int i;
  1293. (void) arg;
  1294. for (i = 0; i < iters; ++i) {
  1295. crypto_rand((char*)secret, 32);
  1296. curve25519_set_impl_params(1); /* Use optimization */
  1297. curve25519_basepoint_impl(public1, secret);
  1298. curve25519_set_impl_params(0); /* Disable optimization */
  1299. curve25519_basepoint_impl(public2, secret);
  1300. tt_mem_op(public1, OP_EQ, public2, 32);
  1301. }
  1302. done:
  1303. ;
  1304. }
  1305. static void
  1306. test_crypto_curve25519_wrappers(void *arg)
  1307. {
  1308. curve25519_public_key_t pubkey1, pubkey2;
  1309. curve25519_secret_key_t seckey1, seckey2;
  1310. uint8_t output1[CURVE25519_OUTPUT_LEN];
  1311. uint8_t output2[CURVE25519_OUTPUT_LEN];
  1312. (void)arg;
  1313. /* Test a simple handshake, serializing and deserializing some stuff. */
  1314. curve25519_secret_key_generate(&seckey1, 0);
  1315. curve25519_secret_key_generate(&seckey2, 1);
  1316. curve25519_public_key_generate(&pubkey1, &seckey1);
  1317. curve25519_public_key_generate(&pubkey2, &seckey2);
  1318. tt_assert(curve25519_public_key_is_ok(&pubkey1));
  1319. tt_assert(curve25519_public_key_is_ok(&pubkey2));
  1320. curve25519_handshake(output1, &seckey1, &pubkey2);
  1321. curve25519_handshake(output2, &seckey2, &pubkey1);
  1322. tt_mem_op(output1,OP_EQ, output2, sizeof(output1));
  1323. done:
  1324. ;
  1325. }
  1326. static void
  1327. test_crypto_curve25519_encode(void *arg)
  1328. {
  1329. curve25519_secret_key_t seckey;
  1330. curve25519_public_key_t key1, key2, key3;
  1331. char buf[64];
  1332. (void)arg;
  1333. curve25519_secret_key_generate(&seckey, 0);
  1334. curve25519_public_key_generate(&key1, &seckey);
  1335. tt_int_op(0, OP_EQ, curve25519_public_to_base64(buf, &key1));
  1336. tt_int_op(CURVE25519_BASE64_PADDED_LEN, OP_EQ, strlen(buf));
  1337. tt_int_op(0, OP_EQ, curve25519_public_from_base64(&key2, buf));
  1338. tt_mem_op(key1.public_key,OP_EQ, key2.public_key, CURVE25519_PUBKEY_LEN);
  1339. buf[CURVE25519_BASE64_PADDED_LEN - 1] = '\0';
  1340. tt_int_op(CURVE25519_BASE64_PADDED_LEN-1, OP_EQ, strlen(buf));
  1341. tt_int_op(0, OP_EQ, curve25519_public_from_base64(&key3, buf));
  1342. tt_mem_op(key1.public_key,OP_EQ, key3.public_key, CURVE25519_PUBKEY_LEN);
  1343. /* Now try bogus parses. */
  1344. strlcpy(buf, "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$=", sizeof(buf));
  1345. tt_int_op(-1, OP_EQ, curve25519_public_from_base64(&key3, buf));
  1346. strlcpy(buf, "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$", sizeof(buf));
  1347. tt_int_op(-1, OP_EQ, curve25519_public_from_base64(&key3, buf));
  1348. strlcpy(buf, "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", sizeof(buf));
  1349. tt_int_op(-1, OP_EQ, curve25519_public_from_base64(&key3, buf));
  1350. done:
  1351. ;
  1352. }
  1353. static void
  1354. test_crypto_curve25519_persist(void *arg)
  1355. {
  1356. curve25519_keypair_t keypair, keypair2;
  1357. char *fname = tor_strdup(get_fname("curve25519_keypair"));
  1358. char *tag = NULL;
  1359. char *content = NULL;
  1360. const char *cp;
  1361. struct stat st;
  1362. size_t taglen;
  1363. (void)arg;
  1364. tt_int_op(0,OP_EQ,curve25519_keypair_generate(&keypair, 0));
  1365. tt_int_op(0,OP_EQ,
  1366. curve25519_keypair_write_to_file(&keypair, fname, "testing"));
  1367. tt_int_op(0,OP_EQ,curve25519_keypair_read_from_file(&keypair2, &tag, fname));
  1368. tt_str_op(tag,OP_EQ,"testing");
  1369. tor_free(tag);
  1370. tt_mem_op(keypair.pubkey.public_key,OP_EQ,
  1371. keypair2.pubkey.public_key,
  1372. CURVE25519_PUBKEY_LEN);
  1373. tt_mem_op(keypair.seckey.secret_key,OP_EQ,
  1374. keypair2.seckey.secret_key,
  1375. CURVE25519_SECKEY_LEN);
  1376. content = read_file_to_str(fname, RFTS_BIN, &st);
  1377. tt_assert(content);
  1378. taglen = strlen("== c25519v1: testing ==");
  1379. tt_u64_op((uint64_t)st.st_size, OP_EQ,
  1380. 32+CURVE25519_PUBKEY_LEN+CURVE25519_SECKEY_LEN);
  1381. tt_assert(fast_memeq(content, "== c25519v1: testing ==", taglen));
  1382. tt_assert(tor_mem_is_zero(content+taglen, 32-taglen));
  1383. cp = content + 32;
  1384. tt_mem_op(keypair.seckey.secret_key,OP_EQ,
  1385. cp,
  1386. CURVE25519_SECKEY_LEN);
  1387. cp += CURVE25519_SECKEY_LEN;
  1388. tt_mem_op(keypair.pubkey.public_key,OP_EQ,
  1389. cp,
  1390. CURVE25519_SECKEY_LEN);
  1391. tor_free(fname);
  1392. fname = tor_strdup(get_fname("bogus_keypair"));
  1393. tt_int_op(-1, OP_EQ,
  1394. curve25519_keypair_read_from_file(&keypair2, &tag, fname));
  1395. tor_free(tag);
  1396. content[69] ^= 0xff;
  1397. tt_int_op(0, OP_EQ,
  1398. write_bytes_to_file(fname, content, (size_t)st.st_size, 1));
  1399. tt_int_op(-1, OP_EQ,
  1400. curve25519_keypair_read_from_file(&keypair2, &tag, fname));
  1401. done:
  1402. tor_free(fname);
  1403. tor_free(content);
  1404. tor_free(tag);
  1405. }
  1406. static void
  1407. test_crypto_ed25519_simple(void *arg)
  1408. {
  1409. ed25519_keypair_t kp1, kp2;
  1410. ed25519_public_key_t pub1, pub2;
  1411. ed25519_secret_key_t sec1, sec2;
  1412. ed25519_signature_t sig1, sig2;
  1413. const uint8_t msg[] =
  1414. "GNU will be able to run Unix programs, "
  1415. "but will not be identical to Unix.";
  1416. const uint8_t msg2[] =
  1417. "Microsoft Windows extends the features of the DOS operating system, "
  1418. "yet is compatible with most existing applications that run under DOS.";
  1419. size_t msg_len = strlen((const char*)msg);
  1420. size_t msg2_len = strlen((const char*)msg2);
  1421. (void)arg;
  1422. tt_int_op(0, OP_EQ, ed25519_secret_key_generate(&sec1, 0));
  1423. tt_int_op(0, OP_EQ, ed25519_secret_key_generate(&sec2, 1));
  1424. tt_int_op(0, OP_EQ, ed25519_public_key_generate(&pub1, &sec1));
  1425. tt_int_op(0, OP_EQ, ed25519_public_key_generate(&pub2, &sec1));
  1426. tt_mem_op(pub1.pubkey, OP_EQ, pub2.pubkey, sizeof(pub1.pubkey));
  1427. tt_assert(ed25519_pubkey_eq(&pub1, &pub2));
  1428. tt_assert(ed25519_pubkey_eq(&pub1, &pub1));
  1429. memcpy(&kp1.pubkey, &pub1, sizeof(pub1));
  1430. memcpy(&kp1.seckey, &sec1, sizeof(sec1));
  1431. tt_int_op(0, OP_EQ, ed25519_sign(&sig1, msg, msg_len, &kp1));
  1432. tt_int_op(0, OP_EQ, ed25519_sign(&sig2, msg, msg_len, &kp1));
  1433. /* Ed25519 signatures are deterministic */
  1434. tt_mem_op(sig1.sig, OP_EQ, sig2.sig, sizeof(sig1.sig));
  1435. /* Basic signature is valid. */
  1436. tt_int_op(0, OP_EQ, ed25519_checksig(&sig1, msg, msg_len, &pub1));
  1437. /* Altered signature doesn't work. */
  1438. sig1.sig[0] ^= 3;
  1439. tt_int_op(-1, OP_EQ, ed25519_checksig(&sig1, msg, msg_len, &pub1));
  1440. /* Wrong public key doesn't work. */
  1441. tt_int_op(0, OP_EQ, ed25519_public_key_generate(&pub2, &sec2));
  1442. tt_int_op(-1, OP_EQ, ed25519_checksig(&sig2, msg, msg_len, &pub2));
  1443. tt_assert(! ed25519_pubkey_eq(&pub1, &pub2));
  1444. /* Wrong message doesn't work. */
  1445. tt_int_op(0, OP_EQ, ed25519_checksig(&sig2, msg, msg_len, &pub1));
  1446. tt_int_op(-1, OP_EQ, ed25519_checksig(&sig2, msg, msg_len-1, &pub1));
  1447. tt_int_op(-1, OP_EQ, ed25519_checksig(&sig2, msg2, msg2_len, &pub1));
  1448. /* Batch signature checking works with some bad. */
  1449. tt_int_op(0, OP_EQ, ed25519_keypair_generate(&kp2, 0));
  1450. tt_int_op(0, OP_EQ, ed25519_sign(&sig1, msg, msg_len, &kp2));
  1451. {
  1452. ed25519_checkable_t ch[] = {
  1453. { &pub1, sig2, msg, msg_len }, /*ok*/
  1454. { &pub1, sig2, msg, msg_len-1 }, /*bad*/
  1455. { &kp2.pubkey, sig2, msg2, msg2_len }, /*bad*/
  1456. { &kp2.pubkey, sig1, msg, msg_len }, /*ok*/
  1457. };
  1458. int okay[4];
  1459. tt_int_op(-2, OP_EQ, ed25519_checksig_batch(okay, ch, 4));
  1460. tt_int_op(okay[0], OP_EQ, 1);
  1461. tt_int_op(okay[1], OP_EQ, 0);
  1462. tt_int_op(okay[2], OP_EQ, 0);
  1463. tt_int_op(okay[3], OP_EQ, 1);
  1464. tt_int_op(-2, OP_EQ, ed25519_checksig_batch(NULL, ch, 4));
  1465. }
  1466. /* Batch signature checking works with all good. */
  1467. {
  1468. ed25519_checkable_t ch[] = {
  1469. { &pub1, sig2, msg, msg_len }, /*ok*/
  1470. { &kp2.pubkey, sig1, msg, msg_len }, /*ok*/
  1471. };
  1472. int okay[2];
  1473. tt_int_op(0, OP_EQ, ed25519_checksig_batch(okay, ch, 2));
  1474. tt_int_op(okay[0], OP_EQ, 1);
  1475. tt_int_op(okay[1], OP_EQ, 1);
  1476. tt_int_op(0, OP_EQ, ed25519_checksig_batch(NULL, ch, 2));
  1477. }
  1478. done:
  1479. ;
  1480. }
  1481. static void
  1482. test_crypto_ed25519_test_vectors(void *arg)
  1483. {
  1484. char *mem_op_hex_tmp=NULL;
  1485. int i;
  1486. struct {
  1487. const char *sk;
  1488. const char *pk;
  1489. const char *sig;
  1490. const char *msg;
  1491. } items[] = {
  1492. /* These test vectors were generated with the "ref" implementation of
  1493. * ed25519 from SUPERCOP-20130419 */
  1494. { "4c6574277320686f706520746865726520617265206e6f206275677320696e20",
  1495. "f3e0e493b30f56e501aeb868fc912fe0c8b76621efca47a78f6d75875193dd87",
  1496. "b5d7fd6fd3adf643647ce1fe87a2931dedd1a4e38e6c662bedd35cdd80bfac51"
  1497. "1b2c7d1ee6bd929ac213014e1a8dc5373854c7b25dbe15ec96bf6c94196fae06",
  1498. "506c6561736520657863757365206d7920667269656e642e2048652069736e2774"
  1499. "204e554c2d7465726d696e617465642e"
  1500. },
  1501. { "74686520696d706c656d656e746174696f6e20776869636820617265206e6f74",
  1502. "407f0025a1e1351a4cb68e92f5c0ebaf66e7aaf93a4006a4d1a66e3ede1cfeac",
  1503. "02884fde1c3c5944d0ecf2d133726fc820c303aae695adceabf3a1e01e95bf28"
  1504. "da88c0966f5265e9c6f8edc77b3b96b5c91baec3ca993ccd21a3f64203600601",
  1505. "506c6561736520657863757365206d7920667269656e642e2048652069736e2774"
  1506. "204e554c2d7465726d696e617465642e"
  1507. },
  1508. { "6578706f73656420627920456e676c697368207465787420617320696e707574",
  1509. "61681cb5fbd69f9bc5a462a21a7ab319011237b940bc781cdc47fcbe327e7706",
  1510. "6a127d0414de7510125d4bc214994ffb9b8857a46330832d05d1355e882344ad"
  1511. "f4137e3ca1f13eb9cc75c887ef2309b98c57528b4acd9f6376c6898889603209",
  1512. "506c6561736520657863757365206d7920667269656e642e2048652069736e2774"
  1513. "204e554c2d7465726d696e617465642e"
  1514. },
  1515. /* These come from "sign.input" in ed25519's page */
  1516. { "5b5a619f8ce1c66d7ce26e5a2ae7b0c04febcd346d286c929e19d0d5973bfef9",
  1517. "6fe83693d011d111131c4f3fbaaa40a9d3d76b30012ff73bb0e39ec27ab18257",
  1518. "0f9ad9793033a2fa06614b277d37381e6d94f65ac2a5a94558d09ed6ce922258"
  1519. "c1a567952e863ac94297aec3c0d0c8ddf71084e504860bb6ba27449b55adc40e",
  1520. "5a8d9d0a22357e6655f9c785"
  1521. },
  1522. { "940c89fe40a81dafbdb2416d14ae469119869744410c3303bfaa0241dac57800",
  1523. "a2eb8c0501e30bae0cf842d2bde8dec7386f6b7fc3981b8c57c9792bb94cf2dd",
  1524. "d8bb64aad8c9955a115a793addd24f7f2b077648714f49c4694ec995b330d09d"
  1525. "640df310f447fd7b6cb5c14f9fe9f490bcf8cfadbfd2169c8ac20d3b8af49a0c",
  1526. "b87d3813e03f58cf19fd0b6395"
  1527. },
  1528. { "9acad959d216212d789a119252ebfe0c96512a23c73bd9f3b202292d6916a738",
  1529. "cf3af898467a5b7a52d33d53bc037e2642a8da996903fc252217e9c033e2f291",
  1530. "6ee3fe81e23c60eb2312b2006b3b25e6838e02106623f844c44edb8dafd66ab0"
  1531. "671087fd195df5b8f58a1d6e52af42908053d55c7321010092748795ef94cf06",
  1532. "55c7fa434f5ed8cdec2b7aeac173",
  1533. },
  1534. { "d5aeee41eeb0e9d1bf8337f939587ebe296161e6bf5209f591ec939e1440c300",
  1535. "fd2a565723163e29f53c9de3d5e8fbe36a7ab66e1439ec4eae9c0a604af291a5",
  1536. "f68d04847e5b249737899c014d31c805c5007a62c0a10d50bb1538c5f3550395"
  1537. "1fbc1e08682f2cc0c92efe8f4985dec61dcbd54d4b94a22547d24451271c8b00",
  1538. "0a688e79be24f866286d4646b5d81c"
  1539. },
  1540. { NULL, NULL, NULL, NULL}
  1541. };
  1542. (void)arg;
  1543. for (i = 0; items[i].pk; ++i) {
  1544. ed25519_keypair_t kp;
  1545. ed25519_signature_t sig;
  1546. uint8_t sk_seed[32];
  1547. uint8_t *msg;
  1548. size_t msg_len;
  1549. base16_decode((char*)sk_seed, sizeof(sk_seed),
  1550. items[i].sk, 64);
  1551. ed25519_secret_key_from_seed(&kp.seckey, sk_seed);
  1552. tt_int_op(0, OP_EQ, ed25519_public_key_generate(&kp.pubkey, &kp.seckey));
  1553. test_memeq_hex(kp.pubkey.pubkey, items[i].pk);
  1554. msg_len = strlen(items[i].msg) / 2;
  1555. msg = tor_malloc(msg_len);
  1556. base16_decode((char*)msg, msg_len, items[i].msg, strlen(items[i].msg));
  1557. tt_int_op(0, OP_EQ, ed25519_sign(&sig, msg, msg_len, &kp));
  1558. test_memeq_hex(sig.sig, items[i].sig);
  1559. tor_free(msg);
  1560. }
  1561. done:
  1562. tor_free(mem_op_hex_tmp);
  1563. }
  1564. static void
  1565. test_crypto_ed25519_encode(void *arg)
  1566. {
  1567. char buf[ED25519_SIG_BASE64_LEN+1];
  1568. ed25519_keypair_t kp;
  1569. ed25519_public_key_t pk;
  1570. ed25519_signature_t sig1, sig2;
  1571. char *mem_op_hex_tmp = NULL;
  1572. (void) arg;
  1573. /* Test roundtrip. */
  1574. tt_int_op(0, OP_EQ, ed25519_keypair_generate(&kp, 0));
  1575. tt_int_op(0, OP_EQ, ed25519_public_to_base64(buf, &kp.pubkey));
  1576. tt_int_op(ED25519_BASE64_LEN, OP_EQ, strlen(buf));
  1577. tt_int_op(0, OP_EQ, ed25519_public_from_base64(&pk, buf));
  1578. tt_mem_op(kp.pubkey.pubkey, OP_EQ, pk.pubkey, ED25519_PUBKEY_LEN);
  1579. tt_int_op(0, OP_EQ, ed25519_sign(&sig1, (const uint8_t*)"ABC", 3, &kp));
  1580. tt_int_op(0, OP_EQ, ed25519_signature_to_base64(buf, &sig1));
  1581. tt_int_op(0, OP_EQ, ed25519_signature_from_base64(&sig2, buf));
  1582. tt_mem_op(sig1.sig, OP_EQ, sig2.sig, ED25519_SIG_LEN);
  1583. /* Test known value. */
  1584. tt_int_op(0, OP_EQ, ed25519_public_from_base64(&pk,
  1585. "lVIuIctLjbGZGU5wKMNXxXlSE3cW4kaqkqm04u6pxvM"));
  1586. test_memeq_hex(pk.pubkey,
  1587. "95522e21cb4b8db199194e7028c357c57952137716e246aa92a9b4e2eea9c6f3");
  1588. done:
  1589. tor_free(mem_op_hex_tmp);
  1590. }
  1591. static void
  1592. test_crypto_ed25519_convert(void *arg)
  1593. {
  1594. const uint8_t msg[] =
  1595. "The eyes are not here / There are no eyes here.";
  1596. const int N = 30;
  1597. int i;
  1598. (void)arg;
  1599. for (i = 0; i < N; ++i) {
  1600. curve25519_keypair_t curve25519_keypair;
  1601. ed25519_keypair_t ed25519_keypair;
  1602. ed25519_public_key_t ed25519_pubkey;
  1603. int bit=0;
  1604. ed25519_signature_t sig;
  1605. tt_int_op(0,OP_EQ,curve25519_keypair_generate(&curve25519_keypair, i&1));
  1606. tt_int_op(0,OP_EQ,ed25519_keypair_from_curve25519_keypair(
  1607. &ed25519_keypair, &bit, &curve25519_keypair));
  1608. tt_int_op(0,OP_EQ,ed25519_public_key_from_curve25519_public_key(
  1609. &ed25519_pubkey, &curve25519_keypair.pubkey, bit));
  1610. tt_mem_op(ed25519_pubkey.pubkey, OP_EQ, ed25519_keypair.pubkey.pubkey, 32);
  1611. tt_int_op(0,OP_EQ,ed25519_sign(&sig, msg, sizeof(msg), &ed25519_keypair));
  1612. tt_int_op(0,OP_EQ,ed25519_checksig(&sig, msg, sizeof(msg),
  1613. &ed25519_pubkey));
  1614. tt_int_op(-1,OP_EQ,ed25519_checksig(&sig, msg, sizeof(msg)-1,
  1615. &ed25519_pubkey));
  1616. sig.sig[0] ^= 15;
  1617. tt_int_op(-1,OP_EQ,ed25519_checksig(&sig, msg, sizeof(msg),
  1618. &ed25519_pubkey));
  1619. }
  1620. done:
  1621. ;
  1622. }
  1623. static void
  1624. test_crypto_ed25519_blinding(void *arg)
  1625. {
  1626. const uint8_t msg[] =
  1627. "Eyes I dare not meet in dreams / In death's dream kingdom";
  1628. const int N = 30;
  1629. int i;
  1630. (void)arg;
  1631. for (i = 0; i < N; ++i) {
  1632. uint8_t blinding[32];
  1633. ed25519_keypair_t ed25519_keypair;
  1634. ed25519_keypair_t ed25519_keypair_blinded;
  1635. ed25519_public_key_t ed25519_pubkey_blinded;
  1636. ed25519_signature_t sig;
  1637. crypto_rand((char*) blinding, sizeof(blinding));
  1638. tt_int_op(0,OP_EQ,ed25519_keypair_generate(&ed25519_keypair, 0));
  1639. tt_int_op(0,OP_EQ,ed25519_keypair_blind(&ed25519_keypair_blinded,
  1640. &ed25519_keypair, blinding));
  1641. tt_int_op(0,OP_EQ,ed25519_public_blind(&ed25519_pubkey_blinded,
  1642. &ed25519_keypair.pubkey, blinding));
  1643. tt_mem_op(ed25519_pubkey_blinded.pubkey, OP_EQ,
  1644. ed25519_keypair_blinded.pubkey.pubkey, 32);
  1645. tt_int_op(0,OP_EQ,ed25519_sign(&sig, msg, sizeof(msg),
  1646. &ed25519_keypair_blinded));
  1647. tt_int_op(0,OP_EQ,ed25519_checksig(&sig, msg, sizeof(msg),
  1648. &ed25519_pubkey_blinded));
  1649. tt_int_op(-1,OP_EQ,ed25519_checksig(&sig, msg, sizeof(msg)-1,
  1650. &ed25519_pubkey_blinded));
  1651. sig.sig[0] ^= 15;
  1652. tt_int_op(-1,OP_EQ,ed25519_checksig(&sig, msg, sizeof(msg),
  1653. &ed25519_pubkey_blinded));
  1654. }
  1655. done:
  1656. ;
  1657. }
  1658. static void
  1659. test_crypto_ed25519_testvectors(void *arg)
  1660. {
  1661. unsigned i;
  1662. char *mem_op_hex_tmp = NULL;
  1663. (void)arg;
  1664. for (i = 0; i < ARRAY_LENGTH(ED25519_SECRET_KEYS); ++i) {
  1665. uint8_t sk[32];
  1666. ed25519_secret_key_t esk;
  1667. ed25519_public_key_t pk, blind_pk, pkfromcurve;
  1668. ed25519_keypair_t keypair, blind_keypair;
  1669. curve25519_keypair_t curvekp;
  1670. uint8_t blinding_param[32];
  1671. ed25519_signature_t sig;
  1672. int sign;
  1673. #define DECODE(p,s) base16_decode((char*)(p),sizeof(p),(s),strlen(s))
  1674. #define EQ(a,h) test_memeq_hex((const char*)(a), (h))
  1675. tt_int_op(0, OP_EQ, DECODE(sk, ED25519_SECRET_KEYS[i]));
  1676. tt_int_op(0, OP_EQ, DECODE(blinding_param, ED25519_BLINDING_PARAMS[i]));
  1677. tt_int_op(0, OP_EQ, ed25519_secret_key_from_seed(&esk, sk));
  1678. EQ(esk.seckey, ED25519_EXPANDED_SECRET_KEYS[i]);
  1679. tt_int_op(0, OP_EQ, ed25519_public_key_generate(&pk, &esk));
  1680. EQ(pk.pubkey, ED25519_PUBLIC_KEYS[i]);
  1681. memcpy(&curvekp.seckey.secret_key, esk.seckey, 32);
  1682. curve25519_public_key_generate(&curvekp.pubkey, &curvekp.seckey);
  1683. tt_int_op(0, OP_EQ,
  1684. ed25519_keypair_from_curve25519_keypair(&keypair, &sign, &curvekp));
  1685. tt_int_op(0, OP_EQ, ed25519_public_key_from_curve25519_public_key(
  1686. &pkfromcurve, &curvekp.pubkey, sign));
  1687. tt_mem_op(keypair.pubkey.pubkey, OP_EQ, pkfromcurve.pubkey, 32);
  1688. EQ(curvekp.pubkey.public_key, ED25519_CURVE25519_PUBLIC_KEYS[i]);
  1689. /* Self-signing */
  1690. memcpy(&keypair.seckey, &esk, sizeof(esk));
  1691. memcpy(&keypair.pubkey, &pk, sizeof(pk));
  1692. tt_int_op(0, OP_EQ, ed25519_sign(&sig, pk.pubkey, 32, &keypair));
  1693. EQ(sig.sig, ED25519_SELF_SIGNATURES[i]);
  1694. /* Blinding */
  1695. tt_int_op(0, OP_EQ,
  1696. ed25519_keypair_blind(&blind_keypair, &keypair, blinding_param));
  1697. tt_int_op(0, OP_EQ,
  1698. ed25519_public_blind(&blind_pk, &pk, blinding_param));
  1699. EQ(blind_keypair.seckey.seckey, ED25519_BLINDED_SECRET_KEYS[i]);
  1700. EQ(blind_pk.pubkey, ED25519_BLINDED_PUBLIC_KEYS[i]);
  1701. tt_mem_op(blind_pk.pubkey, OP_EQ, blind_keypair.pubkey.pubkey, 32);
  1702. #undef DECODE
  1703. #undef EQ
  1704. }
  1705. done:
  1706. tor_free(mem_op_hex_tmp);
  1707. }
  1708. static void
  1709. test_crypto_ed25519_fuzz_donna(void *arg)
  1710. {
  1711. const unsigned iters = 1024;
  1712. uint8_t msg[1024];
  1713. unsigned i;
  1714. (void)arg;
  1715. tt_assert(sizeof(msg) == iters);
  1716. crypto_rand((char*) msg, sizeof(msg));
  1717. /* Fuzz Ed25519-donna vs ref10, alternating the implementation used to
  1718. * generate keys/sign per iteration.
  1719. */
  1720. for (i = 0; i < iters; ++i) {
  1721. const int use_donna = i & 1;
  1722. uint8_t blinding[32];
  1723. curve25519_keypair_t ckp;
  1724. ed25519_keypair_t kp, kp_blind, kp_curve25519;
  1725. ed25519_public_key_t pk, pk_blind, pk_curve25519;
  1726. ed25519_signature_t sig, sig_blind;
  1727. int bit = 0;
  1728. crypto_rand((char*) blinding, sizeof(blinding));
  1729. /* Impl. A:
  1730. * 1. Generate a keypair.
  1731. * 2. Blinded the keypair.
  1732. * 3. Sign a message (unblinded).
  1733. * 4. Sign a message (blinded).
  1734. * 5. Generate a curve25519 keypair, and convert it to Ed25519.
  1735. */
  1736. ed25519_set_impl_params(use_donna);
  1737. tt_int_op(0, OP_EQ, ed25519_keypair_generate(&kp, i&1));
  1738. tt_int_op(0, OP_EQ, ed25519_keypair_blind(&kp_blind, &kp, blinding));
  1739. tt_int_op(0, OP_EQ, ed25519_sign(&sig, msg, i, &kp));
  1740. tt_int_op(0, OP_EQ, ed25519_sign(&sig_blind, msg, i, &kp_blind));
  1741. tt_int_op(0, OP_EQ, curve25519_keypair_generate(&ckp, i&1));
  1742. tt_int_op(0, OP_EQ, ed25519_keypair_from_curve25519_keypair(
  1743. &kp_curve25519, &bit, &ckp));
  1744. /* Impl. B:
  1745. * 1. Validate the public key by rederiving it.
  1746. * 2. Validate the blinded public key by rederiving it.
  1747. * 3. Validate the unblinded signature (and test a invalid signature).
  1748. * 4. Validate the blinded signature.
  1749. * 5. Validate the public key (from Curve25519) by rederiving it.
  1750. */
  1751. ed25519_set_impl_params(!use_donna);
  1752. tt_int_op(0, OP_EQ, ed25519_public_key_generate(&pk, &kp.seckey));
  1753. tt_mem_op(pk.pubkey, OP_EQ, kp.pubkey.pubkey, 32);
  1754. tt_int_op(0, OP_EQ, ed25519_public_blind(&pk_blind, &kp.pubkey, blinding));
  1755. tt_mem_op(pk_blind.pubkey, OP_EQ, kp_blind.pubkey.pubkey, 32);
  1756. tt_int_op(0, OP_EQ, ed25519_checksig(&sig, msg, i, &pk));
  1757. sig.sig[0] ^= 15;
  1758. tt_int_op(-1, OP_EQ, ed25519_checksig(&sig, msg, sizeof(msg), &pk));
  1759. tt_int_op(0, OP_EQ, ed25519_checksig(&sig_blind, msg, i, &pk_blind));
  1760. tt_int_op(0, OP_EQ, ed25519_public_key_from_curve25519_public_key(
  1761. &pk_curve25519, &ckp.pubkey, bit));
  1762. tt_mem_op(pk_curve25519.pubkey, OP_EQ, kp_curve25519.pubkey.pubkey, 32);
  1763. }
  1764. done:
  1765. ;
  1766. }
  1767. static void
  1768. test_crypto_siphash(void *arg)
  1769. {
  1770. /* From the reference implementation, taking
  1771. k = 00 01 02 ... 0f
  1772. and in = 00; 00 01; 00 01 02; ...
  1773. */
  1774. const uint8_t VECTORS[64][8] =
  1775. {
  1776. { 0x31, 0x0e, 0x0e, 0xdd, 0x47, 0xdb, 0x6f, 0x72, },
  1777. { 0xfd, 0x67, 0xdc, 0x93, 0xc5, 0x39, 0xf8, 0x74, },
  1778. { 0x5a, 0x4f, 0xa9, 0xd9, 0x09, 0x80, 0x6c, 0x0d, },
  1779. { 0x2d, 0x7e, 0xfb, 0xd7, 0x96, 0x66, 0x67, 0x85, },
  1780. { 0xb7, 0x87, 0x71, 0x27, 0xe0, 0x94, 0x27, 0xcf, },
  1781. { 0x8d, 0xa6, 0x99, 0xcd, 0x64, 0x55, 0x76, 0x18, },
  1782. { 0xce, 0xe3, 0xfe, 0x58, 0x6e, 0x46, 0xc9, 0xcb, },
  1783. { 0x37, 0xd1, 0x01, 0x8b, 0xf5, 0x00, 0x02, 0xab, },
  1784. { 0x62, 0x24, 0x93, 0x9a, 0x79, 0xf5, 0xf5, 0x93, },
  1785. { 0xb0, 0xe4, 0xa9, 0x0b, 0xdf, 0x82, 0x00, 0x9e, },
  1786. { 0xf3, 0xb9, 0xdd, 0x94, 0xc5, 0xbb, 0x5d, 0x7a, },
  1787. { 0xa7, 0xad, 0x6b, 0x22, 0x46, 0x2f, 0xb3, 0xf4, },
  1788. { 0xfb, 0xe5, 0x0e, 0x86, 0xbc, 0x8f, 0x1e, 0x75, },
  1789. { 0x90, 0x3d, 0x84, 0xc0, 0x27, 0x56, 0xea, 0x14, },
  1790. { 0xee, 0xf2, 0x7a, 0x8e, 0x90, 0xca, 0x23, 0xf7, },
  1791. { 0xe5, 0x45, 0xbe, 0x49, 0x61, 0xca, 0x29, 0xa1, },
  1792. { 0xdb, 0x9b, 0xc2, 0x57, 0x7f, 0xcc, 0x2a, 0x3f, },
  1793. { 0x94, 0x47, 0xbe, 0x2c, 0xf5, 0xe9, 0x9a, 0x69, },
  1794. { 0x9c, 0xd3, 0x8d, 0x96, 0xf0, 0xb3, 0xc1, 0x4b, },
  1795. { 0xbd, 0x61, 0x79, 0xa7, 0x1d, 0xc9, 0x6d, 0xbb, },
  1796. { 0x98, 0xee, 0xa2, 0x1a, 0xf2, 0x5c, 0xd6, 0xbe, },
  1797. { 0xc7, 0x67, 0x3b, 0x2e, 0xb0, 0xcb, 0xf2, 0xd0, },
  1798. { 0x88, 0x3e, 0xa3, 0xe3, 0x95, 0x67, 0x53, 0x93, },
  1799. { 0xc8, 0xce, 0x5c, 0xcd, 0x8c, 0x03, 0x0c, 0xa8, },
  1800. { 0x94, 0xaf, 0x49, 0xf6, 0xc6, 0x50, 0xad, 0xb8, },
  1801. { 0xea, 0xb8, 0x85, 0x8a, 0xde, 0x92, 0xe1, 0xbc, },
  1802. { 0xf3, 0x15, 0xbb, 0x5b, 0xb8, 0x35, 0xd8, 0x17, },
  1803. { 0xad, 0xcf, 0x6b, 0x07, 0x63, 0x61, 0x2e, 0x2f, },
  1804. { 0xa5, 0xc9, 0x1d, 0xa7, 0xac, 0xaa, 0x4d, 0xde, },
  1805. { 0x71, 0x65, 0x95, 0x87, 0x66, 0x50, 0xa2, 0xa6, },
  1806. { 0x28, 0xef, 0x49, 0x5c, 0x53, 0xa3, 0x87, 0xad, },
  1807. { 0x42, 0xc3, 0x41, 0xd8, 0xfa, 0x92, 0xd8, 0x32, },
  1808. { 0xce, 0x7c, 0xf2, 0x72, 0x2f, 0x51, 0x27, 0x71, },
  1809. { 0xe3, 0x78, 0x59, 0xf9, 0x46, 0x23, 0xf3, 0xa7, },
  1810. { 0x38, 0x12, 0x05, 0xbb, 0x1a, 0xb0, 0xe0, 0x12, },
  1811. { 0xae, 0x97, 0xa1, 0x0f, 0xd4, 0x34, 0xe0, 0x15, },
  1812. { 0xb4, 0xa3, 0x15, 0x08, 0xbe, 0xff, 0x4d, 0x31, },
  1813. { 0x81, 0x39, 0x62, 0x29, 0xf0, 0x90, 0x79, 0x02, },
  1814. { 0x4d, 0x0c, 0xf4, 0x9e, 0xe5, 0xd4, 0xdc, 0xca, },
  1815. { 0x5c, 0x73, 0x33, 0x6a, 0x76, 0xd8, 0xbf, 0x9a, },
  1816. { 0xd0, 0xa7, 0x04, 0x53, 0x6b, 0xa9, 0x3e, 0x0e, },
  1817. { 0x92, 0x59, 0x58, 0xfc, 0xd6, 0x42, 0x0c, 0xad, },
  1818. { 0xa9, 0x15, 0xc2, 0x9b, 0xc8, 0x06, 0x73, 0x18, },
  1819. { 0x95, 0x2b, 0x79, 0xf3, 0xbc, 0x0a, 0xa6, 0xd4, },
  1820. { 0xf2, 0x1d, 0xf2, 0xe4, 0x1d, 0x45, 0x35, 0xf9, },
  1821. { 0x87, 0x57, 0x75, 0x19, 0x04, 0x8f, 0x53, 0xa9, },
  1822. { 0x10, 0xa5, 0x6c, 0xf5, 0xdf, 0xcd, 0x9a, 0xdb, },
  1823. { 0xeb, 0x75, 0x09, 0x5c, 0xcd, 0x98, 0x6c, 0xd0, },
  1824. { 0x51, 0xa9, 0xcb, 0x9e, 0xcb, 0xa3, 0x12, 0xe6, },
  1825. { 0x96, 0xaf, 0xad, 0xfc, 0x2c, 0xe6, 0x66, 0xc7, },
  1826. { 0x72, 0xfe, 0x52, 0x97, 0x5a, 0x43, 0x64, 0xee, },
  1827. { 0x5a, 0x16, 0x45, 0xb2, 0x76, 0xd5, 0x92, 0xa1, },
  1828. { 0xb2, 0x74, 0xcb, 0x8e, 0xbf, 0x87, 0x87, 0x0a, },
  1829. { 0x6f, 0x9b, 0xb4, 0x20, 0x3d, 0xe7, 0xb3, 0x81, },
  1830. { 0xea, 0xec, 0xb2, 0xa3, 0x0b, 0x22, 0xa8, 0x7f, },
  1831. { 0x99, 0x24, 0xa4, 0x3c, 0xc1, 0x31, 0x57, 0x24, },
  1832. { 0xbd, 0x83, 0x8d, 0x3a, 0xaf, 0xbf, 0x8d, 0xb7, },
  1833. { 0x0b, 0x1a, 0x2a, 0x32, 0x65, 0xd5, 0x1a, 0xea, },
  1834. { 0x13, 0x50, 0x79, 0xa3, 0x23, 0x1c, 0xe6, 0x60, },
  1835. { 0x93, 0x2b, 0x28, 0x46, 0xe4, 0xd7, 0x06, 0x66, },
  1836. { 0xe1, 0x91, 0x5f, 0x5c, 0xb1, 0xec, 0xa4, 0x6c, },
  1837. { 0xf3, 0x25, 0x96, 0x5c, 0xa1, 0x6d, 0x62, 0x9f, },
  1838. { 0x57, 0x5f, 0xf2, 0x8e, 0x60, 0x38, 0x1b, 0xe5, },
  1839. { 0x72, 0x45, 0x06, 0xeb, 0x4c, 0x32, 0x8a, 0x95, }
  1840. };
  1841. const struct sipkey K = { U64_LITERAL(0x0706050403020100),
  1842. U64_LITERAL(0x0f0e0d0c0b0a0908) };
  1843. uint8_t input[64];
  1844. int i, j;
  1845. (void)arg;
  1846. for (i = 0; i < 64; ++i)
  1847. input[i] = i;
  1848. for (i = 0; i < 64; ++i) {
  1849. uint64_t r = siphash24(input, i, &K);
  1850. for (j = 0; j < 8; ++j) {
  1851. tt_int_op( (r >> (j*8)) & 0xff, OP_EQ, VECTORS[i][j]);
  1852. }
  1853. }
  1854. done:
  1855. ;
  1856. }
  1857. /* We want the likelihood that the random buffer exhibits any regular pattern
  1858. * to be far less than the memory bit error rate in the int return value.
  1859. * Using 2048 bits provides a failure rate of 1/(3 * 10^616), and we call
  1860. * 3 functions, leading to an overall error rate of 1/10^616.
  1861. * This is comparable with the 1/10^603 failure rate of test_crypto_rng_range.
  1862. */
  1863. #define FAILURE_MODE_BUFFER_SIZE (2048/8)
  1864. /** Check crypto_rand for a failure mode where it does nothing to the buffer,
  1865. * or it sets the buffer to all zeroes. Return 0 when the check passes,
  1866. * or -1 when it fails. */
  1867. static int
  1868. crypto_rand_check_failure_mode_zero(void)
  1869. {
  1870. char buf[FAILURE_MODE_BUFFER_SIZE];
  1871. memset(buf, 0, FAILURE_MODE_BUFFER_SIZE);
  1872. crypto_rand(buf, FAILURE_MODE_BUFFER_SIZE);
  1873. for (size_t i = 0; i < FAILURE_MODE_BUFFER_SIZE; i++) {
  1874. if (buf[i] != 0) {
  1875. return 0;
  1876. }
  1877. }
  1878. return -1;
  1879. }
  1880. /** Check crypto_rand for a failure mode where every int64_t in the buffer is
  1881. * the same. Return 0 when the check passes, or -1 when it fails. */
  1882. static int
  1883. crypto_rand_check_failure_mode_identical(void)
  1884. {
  1885. /* just in case the buffer size isn't a multiple of sizeof(int64_t) */
  1886. #define FAILURE_MODE_BUFFER_SIZE_I64 \
  1887. (FAILURE_MODE_BUFFER_SIZE/SIZEOF_INT64_T)
  1888. #define FAILURE_MODE_BUFFER_SIZE_I64_BYTES \
  1889. (FAILURE_MODE_BUFFER_SIZE_I64*SIZEOF_INT64_T)
  1890. #if FAILURE_MODE_BUFFER_SIZE_I64 < 2
  1891. #error FAILURE_MODE_BUFFER_SIZE needs to be at least 2*SIZEOF_INT64_T
  1892. #endif
  1893. int64_t buf[FAILURE_MODE_BUFFER_SIZE_I64];
  1894. memset(buf, 0, FAILURE_MODE_BUFFER_SIZE_I64_BYTES);
  1895. crypto_rand((char *)buf, FAILURE_MODE_BUFFER_SIZE_I64_BYTES);
  1896. for (size_t i = 1; i < FAILURE_MODE_BUFFER_SIZE_I64; i++) {
  1897. if (buf[i] != buf[i-1]) {
  1898. return 0;
  1899. }
  1900. }
  1901. return -1;
  1902. }
  1903. /** Check crypto_rand for a failure mode where it increments the "random"
  1904. * value by 1 for every byte in the buffer. (This is OpenSSL's PREDICT mode.)
  1905. * Return 0 when the check passes, or -1 when it fails. */
  1906. static int
  1907. crypto_rand_check_failure_mode_predict(void)
  1908. {
  1909. unsigned char buf[FAILURE_MODE_BUFFER_SIZE];
  1910. memset(buf, 0, FAILURE_MODE_BUFFER_SIZE);
  1911. crypto_rand((char *)buf, FAILURE_MODE_BUFFER_SIZE);
  1912. for (size_t i = 1; i < FAILURE_MODE_BUFFER_SIZE; i++) {
  1913. /* check if the last byte was incremented by 1, including integer
  1914. * wrapping */
  1915. if (buf[i] - buf[i-1] != 1 && buf[i-1] - buf[i] != 255) {
  1916. return 0;
  1917. }
  1918. }
  1919. return -1;
  1920. }
  1921. #undef FAILURE_MODE_BUFFER_SIZE
  1922. static void
  1923. test_crypto_failure_modes(void *arg)
  1924. {
  1925. int rv = 0;
  1926. (void)arg;
  1927. rv = crypto_early_init();
  1928. tt_assert(rv == 0);
  1929. /* Check random works */
  1930. rv = crypto_rand_check_failure_mode_zero();
  1931. tt_assert(rv == 0);
  1932. rv = crypto_rand_check_failure_mode_identical();
  1933. tt_assert(rv == 0);
  1934. rv = crypto_rand_check_failure_mode_predict();
  1935. tt_assert(rv == 0);
  1936. done:
  1937. ;
  1938. }
  1939. #define CRYPTO_LEGACY(name) \
  1940. { #name, test_crypto_ ## name , 0, NULL, NULL }
  1941. struct testcase_t crypto_tests[] = {
  1942. CRYPTO_LEGACY(formats),
  1943. CRYPTO_LEGACY(rng),
  1944. { "rng_range", test_crypto_rng_range, 0, NULL, NULL },
  1945. { "aes_AES", test_crypto_aes, TT_FORK, &passthrough_setup, (void*)"aes" },
  1946. { "aes_EVP", test_crypto_aes, TT_FORK, &passthrough_setup, (void*)"evp" },
  1947. CRYPTO_LEGACY(sha),
  1948. CRYPTO_LEGACY(pk),
  1949. { "pk_fingerprints", test_crypto_pk_fingerprints, TT_FORK, NULL, NULL },
  1950. { "pk_base64", test_crypto_pk_base64, TT_FORK, NULL, NULL },
  1951. CRYPTO_LEGACY(digests),
  1952. { "sha3", test_crypto_sha3, TT_FORK, NULL, NULL},
  1953. CRYPTO_LEGACY(dh),
  1954. { "aes_iv_AES", test_crypto_aes_iv, TT_FORK, &passthrough_setup,
  1955. (void*)"aes" },
  1956. { "aes_iv_EVP", test_crypto_aes_iv, TT_FORK, &passthrough_setup,
  1957. (void*)"evp" },
  1958. CRYPTO_LEGACY(base32_decode),
  1959. { "kdf_TAP", test_crypto_kdf_TAP, 0, NULL, NULL },
  1960. { "hkdf_sha256", test_crypto_hkdf_sha256, 0, NULL, NULL },
  1961. { "curve25519_impl", test_crypto_curve25519_impl, 0, NULL, NULL },
  1962. { "curve25519_impl_hibit", test_crypto_curve25519_impl, 0, NULL, (void*)"y"},
  1963. { "curve25519_basepoint",
  1964. test_crypto_curve25519_basepoint, TT_FORK, NULL, NULL },
  1965. { "curve25519_wrappers", test_crypto_curve25519_wrappers, 0, NULL, NULL },
  1966. { "curve25519_encode", test_crypto_curve25519_encode, 0, NULL, NULL },
  1967. { "curve25519_persist", test_crypto_curve25519_persist, 0, NULL, NULL },
  1968. { "ed25519_simple", test_crypto_ed25519_simple, 0, NULL, NULL },
  1969. { "ed25519_test_vectors", test_crypto_ed25519_test_vectors, 0, NULL, NULL },
  1970. { "ed25519_encode", test_crypto_ed25519_encode, 0, NULL, NULL },
  1971. { "ed25519_convert", test_crypto_ed25519_convert, 0, NULL, NULL },
  1972. { "ed25519_blinding", test_crypto_ed25519_blinding, 0, NULL, NULL },
  1973. { "ed25519_testvectors", test_crypto_ed25519_testvectors, 0, NULL, NULL },
  1974. { "ed25519_fuzz_donna", test_crypto_ed25519_fuzz_donna, TT_FORK, NULL,
  1975. NULL },
  1976. { "siphash", test_crypto_siphash, 0, NULL, NULL },
  1977. { "failure_modes", test_crypto_failure_modes, TT_FORK, NULL, NULL },
  1978. END_OF_TESTCASES
  1979. };