testsc.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005
  1. /*
  2. * testsc: run PuTTY's crypto primitives under instrumentation that
  3. * checks for cache and timing side channels.
  4. *
  5. * The idea is: cryptographic code should avoid leaking secret data
  6. * through timing information, or through traces of its activity left
  7. * in the caches.
  8. *
  9. * (This property is sometimes called 'constant-time', although really
  10. * that's a misnomer. It would be impossible to avoid the execution
  11. * time varying for any number of reasons outside the code's control,
  12. * such as the prior contents of caches and branch predictors,
  13. * temperature-based CPU throttling, system load, etc. And in any case
  14. * you don't _need_ the execution time to be literally constant: you
  15. * just need it to be independent of your secrets. It can vary as much
  16. * as it likes based on anything else.)
  17. *
  18. * To avoid this, you need to ensure that various aspects of the
  19. * code's behaviour do not depend on the secret data. The control
  20. * flow, for a start - no conditional branches based on secrets - and
  21. * also the memory access pattern (no using secret data as an index
  22. * into a lookup table). A couple of other kinds of CPU instruction
  23. * also can't be trusted to run in constant time: we check for
  24. * register-controlled shifts and hardware divisions. (But, again,
  25. * it's perfectly fine to _use_ those instructions in the course of
  26. * crypto code. You just can't use a secret as any time-affecting
  27. * operand.)
  28. *
  29. * This test program works by running the same crypto primitive
  30. * multiple times, with different secret input data. The relevant
  31. * details of each run is logged to a file via the DynamoRIO-based
  32. * instrumentation system living in the subdirectory test/sclog. Then
  33. * we check over all the files and ensure they're identical.
  34. *
  35. * This program itself (testsc) is built by the ordinary PuTTY
  36. * makefiles. But run by itself, it will do nothing useful: it needs
  37. * to be run under DynamoRIO, with the sclog instrumentation library.
  38. *
  39. * Here's an example of how I built it:
  40. *
  41. * Download the DynamoRIO source. I did this by cloning
  42. * https://github.com/DynamoRIO/dynamorio.git, and at the time of
  43. * writing this, 259c182a75ce80112bcad329c97ada8d56ba854d was the head
  44. * commit.
  45. *
  46. * In the DynamoRIO checkout:
  47. *
  48. * mkdir build
  49. * cd build
  50. * cmake -G Ninja ..
  51. * ninja
  52. *
  53. * Now set the shell variable DRBUILD to be the location of the build
  54. * directory you did that in. (Or not, if you prefer, but the example
  55. * build commands below will assume that that's where the DynamoRIO
  56. * libraries, headers and runtime can be found.)
  57. *
  58. * Then, in test/sclog:
  59. *
  60. * cmake -G Ninja -DCMAKE_PREFIX_PATH=$DRBUILD/cmake .
  61. * ninja
  62. *
  63. * Finally, to run the actual test, set SCTMP to some temp directory
  64. * you don't mind filling with large temp files (several GB at a
  65. * time), and in the main PuTTY source directory (assuming that's
  66. * where testsc has been built):
  67. *
  68. * $DRBUILD/bin64/drrun -c test/sclog/libsclog.so -- ./testsc -O $SCTMP
  69. */
  70. #include <assert.h>
  71. #include <stdio.h>
  72. #include <stdlib.h>
  73. #include <string.h>
  74. #include <errno.h>
  75. #include "defs.h"
  76. #include "putty.h"
  77. #include "ssh.h"
  78. #include "sshkeygen.h"
  79. #include "misc.h"
  80. #include "mpint.h"
  81. #include "crypto/ecc.h"
  82. #include "crypto/ntru.h"
  83. static NORETURN PRINTF_LIKE(1, 2) void fatal_error(const char *p, ...)
  84. {
  85. va_list ap;
  86. fprintf(stderr, "testsc: ");
  87. va_start(ap, p);
  88. vfprintf(stderr, p, ap);
  89. va_end(ap);
  90. fputc('\n', stderr);
  91. exit(1);
  92. }
  93. void out_of_memory(void) { fatal_error("out of memory"); }
  94. /*
  95. * A simple deterministic PRNG, without any of the Fortuna
  96. * complexities, for generating test inputs in a way that's repeatable
  97. * between runs of the program, even if only a subset of test cases is
  98. * run.
  99. */
  100. static uint64_t random_counter = 0;
  101. static const char *random_seedstr = NULL;
  102. static uint8_t random_buf[MAX_HASH_LEN];
  103. static size_t random_buf_limit = 0;
  104. static ssh_hash *random_hash;
  105. static void random_seed(const char *seedstr)
  106. {
  107. random_seedstr = seedstr;
  108. random_counter = 0;
  109. random_buf_limit = 0;
  110. }
  111. static void random_advance_counter(void)
  112. {
  113. ssh_hash_reset(random_hash);
  114. put_asciz(random_hash, random_seedstr);
  115. put_uint64(random_hash, random_counter);
  116. random_counter++;
  117. random_buf_limit = ssh_hash_alg(random_hash)->hlen;
  118. ssh_hash_digest(random_hash, random_buf);
  119. }
  120. void random_read(void *vbuf, size_t size)
  121. {
  122. assert(random_seedstr);
  123. uint8_t *buf = (uint8_t *)vbuf;
  124. while (size-- > 0) {
  125. if (random_buf_limit == 0)
  126. random_advance_counter();
  127. *buf++ = random_buf[random_buf_limit--];
  128. }
  129. }
  130. struct random_state {
  131. const char *seedstr;
  132. uint64_t counter;
  133. size_t limit;
  134. uint8_t buf[MAX_HASH_LEN];
  135. };
  136. static struct random_state random_get_state(void)
  137. {
  138. struct random_state st;
  139. st.seedstr = random_seedstr;
  140. st.counter = random_counter;
  141. st.limit = random_buf_limit;
  142. memcpy(st.buf, random_buf, sizeof(st.buf));
  143. return st;
  144. }
  145. static void random_set_state(struct random_state st)
  146. {
  147. random_seedstr = st.seedstr;
  148. random_counter = st.counter;
  149. random_buf_limit = st.limit;
  150. memcpy(random_buf, st.buf, sizeof(random_buf));
  151. }
  152. /*
  153. * Macro that defines a function, and also a volatile function pointer
  154. * pointing to it. Callers indirect through the function pointer
  155. * instead of directly calling the function, to ensure that the
  156. * compiler doesn't try to get clever by eliminating the call
  157. * completely, or inlining it.
  158. *
  159. * This is used to mark functions that DynamoRIO will look for to
  160. * intercept, and also to inhibit inlining and unrolling where they'd
  161. * cause a failure of experimental control in the main test.
  162. */
  163. #define VOLATILE_WRAPPED_DEFN(qualifier, rettype, fn, params) \
  164. qualifier rettype fn##_real params; \
  165. qualifier rettype (*volatile fn) params = fn##_real; \
  166. qualifier rettype fn##_real params
  167. VOLATILE_WRAPPED_DEFN(, void, log_to_file, (const char *filename))
  168. {
  169. /*
  170. * This function is intercepted by the DynamoRIO side of the
  171. * mechanism. We use it to send instructions to the DR wrapper,
  172. * namely, 'please start logging to this file' or 'please stop
  173. * logging' (if filename == NULL). But we don't have to actually
  174. * do anything in _this_ program - all the functionality is in the
  175. * DR wrapper.
  176. */
  177. }
  178. static const char *outdir = NULL;
  179. char *log_filename(const char *basename, size_t index)
  180. {
  181. return dupprintf("%s/%s.%04"SIZEu, outdir, basename, index);
  182. }
  183. static char *last_filename;
  184. static const char *test_basename;
  185. static size_t test_index = 0;
  186. void log_start(void)
  187. {
  188. last_filename = log_filename(test_basename, test_index++);
  189. log_to_file(last_filename);
  190. }
  191. void log_end(void)
  192. {
  193. log_to_file(NULL);
  194. sfree(last_filename);
  195. }
  196. static bool test_skipped = false;
  197. VOLATILE_WRAPPED_DEFN(, intptr_t, dry_run, (void))
  198. {
  199. /*
  200. * This is another function intercepted by DynamoRIO. In this
  201. * case, DR overrides this function to return 0 rather than 1, so
  202. * we can use it as a check for whether we're running under
  203. * instrumentation, or whether this is just a dry run which goes
  204. * through the motions but doesn't expect to find any log files
  205. * created.
  206. */
  207. return 1;
  208. }
  209. static void mp_random_bits_into(mp_int *r, size_t bits)
  210. {
  211. mp_int *x = mp_random_bits(bits);
  212. mp_copy_into(r, x);
  213. mp_free(x);
  214. }
  215. static void mp_random_fill(mp_int *r)
  216. {
  217. mp_random_bits_into(r, mp_max_bits(r));
  218. }
  219. VOLATILE_WRAPPED_DEFN(static, size_t, looplimit, (size_t x))
  220. {
  221. /*
  222. * looplimit() is the identity function on size_t, but the
  223. * compiler isn't allowed to rely on it being that. I use it to
  224. * make loops in the test functions look less attractive to
  225. * compilers' unrolling heuristics.
  226. */
  227. return x;
  228. }
  229. #if HAVE_AES_NI
  230. #define IF_AES_NI(x) x
  231. #else
  232. #define IF_AES_NI(x)
  233. #endif
  234. #if HAVE_SHA_NI
  235. #define IF_SHA_NI(x) x
  236. #else
  237. #define IF_SHA_NI(x)
  238. #endif
  239. #if HAVE_CLMUL
  240. #define IF_CLMUL(x) x
  241. #else
  242. #define IF_CLMUL(x)
  243. #endif
  244. #if HAVE_NEON_CRYPTO
  245. #define IF_NEON_CRYPTO(x) x
  246. #else
  247. #define IF_NEON_CRYPTO(x)
  248. #endif
  249. #if HAVE_NEON_SHA512
  250. #define IF_NEON_SHA512(x) x
  251. #else
  252. #define IF_NEON_SHA512(x)
  253. #endif
  254. #if HAVE_NEON_PMULL
  255. #define IF_NEON_PMULL(x) x
  256. #else
  257. #define IF_NEON_PMULL(x)
  258. #endif
  259. /* Ciphers that we expect to pass this test. Blowfish and Arcfour are
  260. * intentionally omitted, because we already know they don't. */
  261. #define CIPHERS(X, Y) \
  262. X(Y, ssh_3des_ssh1) \
  263. X(Y, ssh_3des_ssh2_ctr) \
  264. X(Y, ssh_3des_ssh2) \
  265. X(Y, ssh_des) \
  266. X(Y, ssh_des_sshcom_ssh2) \
  267. X(Y, ssh_aes256_sdctr) \
  268. X(Y, ssh_aes256_gcm) \
  269. X(Y, ssh_aes256_cbc) \
  270. X(Y, ssh_aes192_sdctr) \
  271. X(Y, ssh_aes192_gcm) \
  272. X(Y, ssh_aes192_cbc) \
  273. X(Y, ssh_aes128_sdctr) \
  274. X(Y, ssh_aes128_gcm) \
  275. X(Y, ssh_aes128_cbc) \
  276. X(Y, ssh_aes256_sdctr_sw) \
  277. X(Y, ssh_aes256_gcm_sw) \
  278. X(Y, ssh_aes256_cbc_sw) \
  279. X(Y, ssh_aes192_sdctr_sw) \
  280. X(Y, ssh_aes192_gcm_sw) \
  281. X(Y, ssh_aes192_cbc_sw) \
  282. X(Y, ssh_aes128_sdctr_sw) \
  283. X(Y, ssh_aes128_gcm_sw) \
  284. X(Y, ssh_aes128_cbc_sw) \
  285. IF_AES_NI(X(Y, ssh_aes256_sdctr_ni)) \
  286. IF_AES_NI(X(Y, ssh_aes256_gcm_ni)) \
  287. IF_AES_NI(X(Y, ssh_aes256_cbc_ni)) \
  288. IF_AES_NI(X(Y, ssh_aes192_sdctr_ni)) \
  289. IF_AES_NI(X(Y, ssh_aes192_gcm_ni)) \
  290. IF_AES_NI(X(Y, ssh_aes192_cbc_ni)) \
  291. IF_AES_NI(X(Y, ssh_aes128_sdctr_ni)) \
  292. IF_AES_NI(X(Y, ssh_aes128_gcm_ni)) \
  293. IF_AES_NI(X(Y, ssh_aes128_cbc_ni)) \
  294. IF_NEON_CRYPTO(X(Y, ssh_aes256_sdctr_neon)) \
  295. IF_NEON_CRYPTO(X(Y, ssh_aes256_gcm_neon)) \
  296. IF_NEON_CRYPTO(X(Y, ssh_aes256_cbc_neon)) \
  297. IF_NEON_CRYPTO(X(Y, ssh_aes192_sdctr_neon)) \
  298. IF_NEON_CRYPTO(X(Y, ssh_aes192_gcm_neon)) \
  299. IF_NEON_CRYPTO(X(Y, ssh_aes192_cbc_neon)) \
  300. IF_NEON_CRYPTO(X(Y, ssh_aes128_sdctr_neon)) \
  301. IF_NEON_CRYPTO(X(Y, ssh_aes128_gcm_neon)) \
  302. IF_NEON_CRYPTO(X(Y, ssh_aes128_cbc_neon)) \
  303. X(Y, ssh2_chacha20_poly1305) \
  304. /* end of list */
  305. #define CIPHER_TESTLIST(X, name) X(cipher_ ## name)
  306. #define SIMPLE_MACS(X, Y) \
  307. X(Y, ssh_hmac_md5) \
  308. X(Y, ssh_hmac_sha1) \
  309. X(Y, ssh_hmac_sha1_buggy) \
  310. X(Y, ssh_hmac_sha1_96) \
  311. X(Y, ssh_hmac_sha1_96_buggy) \
  312. X(Y, ssh_hmac_sha256) \
  313. X(Y, ssh_hmac_sha512) \
  314. /* end of list */
  315. #define ALL_MACS(X, Y) \
  316. SIMPLE_MACS(X, Y) \
  317. X(Y, poly1305) \
  318. X(Y, aesgcm_sw_sw) \
  319. X(Y, aesgcm_sw_refpoly) \
  320. IF_AES_NI(X(Y, aesgcm_ni_sw)) \
  321. IF_NEON_CRYPTO(X(Y, aesgcm_neon_sw)) \
  322. IF_CLMUL(X(Y, aesgcm_sw_clmul)) \
  323. IF_NEON_PMULL(X(Y, aesgcm_sw_neon)) \
  324. IF_AES_NI(IF_CLMUL(X(Y, aesgcm_ni_clmul))) \
  325. IF_NEON_CRYPTO(IF_NEON_PMULL(X(Y, aesgcm_neon_neon))) \
  326. /* end of list */
  327. #define MAC_TESTLIST(X, name) X(mac_ ## name)
  328. #define HASHES(X, Y) \
  329. X(Y, ssh_md5) \
  330. X(Y, ssh_sha1) \
  331. X(Y, ssh_sha1_sw) \
  332. X(Y, ssh_sha256) \
  333. X(Y, ssh_sha256_sw) \
  334. X(Y, ssh_sha384) \
  335. X(Y, ssh_sha512) \
  336. X(Y, ssh_sha384_sw) \
  337. X(Y, ssh_sha512_sw) \
  338. IF_SHA_NI(X(Y, ssh_sha256_ni)) \
  339. IF_SHA_NI(X(Y, ssh_sha1_ni)) \
  340. IF_NEON_CRYPTO(X(Y, ssh_sha256_neon)) \
  341. IF_NEON_CRYPTO(X(Y, ssh_sha1_neon)) \
  342. IF_NEON_SHA512(X(Y, ssh_sha384_neon)) \
  343. IF_NEON_SHA512(X(Y, ssh_sha512_neon)) \
  344. X(Y, ssh_sha3_224) \
  345. X(Y, ssh_sha3_256) \
  346. X(Y, ssh_sha3_384) \
  347. X(Y, ssh_sha3_512) \
  348. X(Y, ssh_shake256_114bytes) \
  349. X(Y, ssh_blake2b) \
  350. /* end of list */
  351. #define HASH_TESTLIST(X, name) X(hash_ ## name)
  352. #define TESTLIST(X) \
  353. X(mp_get_nbits) \
  354. X(mp_from_decimal) \
  355. X(mp_from_hex) \
  356. X(mp_get_decimal) \
  357. X(mp_get_hex) \
  358. X(mp_cmp_hs) \
  359. X(mp_cmp_eq) \
  360. X(mp_min) \
  361. X(mp_max) \
  362. X(mp_select_into) \
  363. X(mp_cond_swap) \
  364. X(mp_cond_clear) \
  365. X(mp_add) \
  366. X(mp_sub) \
  367. X(mp_mul) \
  368. X(mp_rshift_safe) \
  369. X(mp_divmod) \
  370. X(mp_nthroot) \
  371. X(mp_modadd) \
  372. X(mp_modsub) \
  373. X(mp_modmul) \
  374. X(mp_modpow) \
  375. X(mp_invert_mod_2to) \
  376. X(mp_invert) \
  377. X(mp_modsqrt) \
  378. X(ecc_weierstrass_add) \
  379. X(ecc_weierstrass_double) \
  380. X(ecc_weierstrass_add_general) \
  381. X(ecc_weierstrass_multiply) \
  382. X(ecc_weierstrass_is_identity) \
  383. X(ecc_weierstrass_get_affine) \
  384. X(ecc_weierstrass_decompress) \
  385. X(ecc_montgomery_diff_add) \
  386. X(ecc_montgomery_double) \
  387. X(ecc_montgomery_multiply) \
  388. X(ecc_montgomery_get_affine) \
  389. X(ecc_edwards_add) \
  390. X(ecc_edwards_multiply) \
  391. X(ecc_edwards_eq) \
  392. X(ecc_edwards_get_affine) \
  393. X(ecc_edwards_decompress) \
  394. CIPHERS(CIPHER_TESTLIST, X) \
  395. ALL_MACS(MAC_TESTLIST, X) \
  396. HASHES(HASH_TESTLIST, X) \
  397. X(argon2) \
  398. X(primegen_probabilistic) \
  399. X(ntru) \
  400. X(rfc6979_setup) \
  401. X(rfc6979_attempt) \
  402. /* end of list */
  403. static void test_mp_get_nbits(void)
  404. {
  405. mp_int *z = mp_new(512);
  406. static const size_t bitposns[] = {
  407. 0, 1, 5, 16, 23, 32, 67, 123, 234, 511
  408. };
  409. mp_int *prev = mp_from_integer(0);
  410. for (size_t i = 0; i < looplimit(lenof(bitposns)); i++) {
  411. mp_int *x = mp_power_2(bitposns[i]);
  412. mp_add_into(z, x, prev);
  413. mp_free(prev);
  414. prev = x;
  415. log_start();
  416. mp_get_nbits(z);
  417. log_end();
  418. }
  419. mp_free(prev);
  420. mp_free(z);
  421. }
  422. static void test_mp_from_decimal(void)
  423. {
  424. char dec[64];
  425. static const size_t starts[] = { 0, 1, 5, 16, 23, 32, 63, 64 };
  426. for (size_t i = 0; i < looplimit(lenof(starts)); i++) {
  427. memset(dec, '0', lenof(dec));
  428. for (size_t j = starts[i]; j < lenof(dec); j++) {
  429. uint8_t r[4];
  430. random_read(r, 4);
  431. dec[j] = '0' + GET_32BIT_MSB_FIRST(r) % 10;
  432. }
  433. log_start();
  434. mp_int *x = mp_from_decimal_pl(make_ptrlen(dec, lenof(dec)));
  435. log_end();
  436. mp_free(x);
  437. }
  438. }
  439. static void test_mp_from_hex(void)
  440. {
  441. char hex[64];
  442. static const size_t starts[] = { 0, 1, 5, 16, 23, 32, 63, 64 };
  443. static const char digits[] = "0123456789abcdefABCDEF";
  444. for (size_t i = 0; i < looplimit(lenof(starts)); i++) {
  445. memset(hex, '0', lenof(hex));
  446. for (size_t j = starts[i]; j < lenof(hex); j++) {
  447. uint8_t r[4];
  448. random_read(r, 4);
  449. hex[j] = digits[GET_32BIT_MSB_FIRST(r) % lenof(digits)];
  450. }
  451. log_start();
  452. mp_int *x = mp_from_hex_pl(make_ptrlen(hex, lenof(hex)));
  453. log_end();
  454. mp_free(x);
  455. }
  456. }
  457. static void test_mp_string_format(char *(*mp_format)(mp_int *x))
  458. {
  459. mp_int *z = mp_new(512);
  460. static const size_t bitposns[] = {
  461. 0, 1, 5, 16, 23, 32, 67, 123, 234, 511
  462. };
  463. for (size_t i = 0; i < looplimit(lenof(bitposns)); i++) {
  464. mp_random_bits_into(z, bitposns[i]);
  465. log_start();
  466. char *formatted = mp_format(z);
  467. log_end();
  468. sfree(formatted);
  469. }
  470. mp_free(z);
  471. }
  472. static void test_mp_get_decimal(void)
  473. {
  474. test_mp_string_format(mp_get_decimal);
  475. }
  476. static void test_mp_get_hex(void)
  477. {
  478. test_mp_string_format(mp_get_hex);
  479. }
  480. static void test_mp_cmp(unsigned (*mp_cmp)(mp_int *a, mp_int *b))
  481. {
  482. mp_int *a = mp_new(512), *b = mp_new(512);
  483. static const size_t bitposns[] = {
  484. 0, 1, 5, 16, 23, 32, 67, 123, 234, 511
  485. };
  486. for (size_t i = 0; i < looplimit(lenof(bitposns)); i++) {
  487. mp_random_fill(b);
  488. mp_int *x = mp_random_bits(bitposns[i]);
  489. mp_xor_into(a, b, x);
  490. mp_free(x);
  491. log_start();
  492. mp_cmp(a, b);
  493. log_end();
  494. }
  495. mp_free(a);
  496. mp_free(b);
  497. }
  498. static void test_mp_cmp_hs(void)
  499. {
  500. test_mp_cmp(mp_cmp_hs);
  501. }
  502. static void test_mp_cmp_eq(void)
  503. {
  504. test_mp_cmp(mp_cmp_eq);
  505. }
  506. static void test_mp_minmax(
  507. void (*mp_minmax_into)(mp_int *r, mp_int *x, mp_int *y))
  508. {
  509. mp_int *a = mp_new(256), *b = mp_new(256);
  510. for (size_t i = 0; i < looplimit(10); i++) {
  511. uint8_t lens[2];
  512. random_read(lens, 2);
  513. mp_int *x = mp_random_bits(lens[0]);
  514. mp_copy_into(a, x);
  515. mp_free(x);
  516. mp_int *y = mp_random_bits(lens[1]);
  517. mp_copy_into(a, y);
  518. mp_free(y);
  519. log_start();
  520. mp_minmax_into(a, a, b);
  521. log_end();
  522. }
  523. mp_free(a);
  524. mp_free(b);
  525. }
  526. static void test_mp_max(void)
  527. {
  528. test_mp_minmax(mp_max_into);
  529. }
  530. static void test_mp_min(void)
  531. {
  532. test_mp_minmax(mp_min_into);
  533. }
  534. static void test_mp_select_into(void)
  535. {
  536. mp_int *a = mp_random_bits(256);
  537. mp_int *b = mp_random_bits(512);
  538. mp_int *r = mp_new(384);
  539. for (size_t i = 0; i < looplimit(16); i++) {
  540. log_start();
  541. mp_select_into(r, a, b, i & 1);
  542. log_end();
  543. }
  544. mp_free(a);
  545. mp_free(b);
  546. mp_free(r);
  547. }
  548. static void test_mp_cond_swap(void)
  549. {
  550. mp_int *a = mp_random_bits(512);
  551. mp_int *b = mp_random_bits(512);
  552. for (size_t i = 0; i < looplimit(16); i++) {
  553. log_start();
  554. mp_cond_swap(a, b, i & 1);
  555. log_end();
  556. }
  557. mp_free(a);
  558. mp_free(b);
  559. }
  560. static void test_mp_cond_clear(void)
  561. {
  562. mp_int *a = mp_random_bits(512);
  563. mp_int *x = mp_copy(a);
  564. for (size_t i = 0; i < looplimit(16); i++) {
  565. mp_copy_into(x, a);
  566. log_start();
  567. mp_cond_clear(a, i & 1);
  568. log_end();
  569. }
  570. mp_free(a);
  571. mp_free(x);
  572. }
  573. static void test_mp_arithmetic(mp_int *(*mp_arith)(mp_int *x, mp_int *y))
  574. {
  575. mp_int *a = mp_new(256), *b = mp_new(512);
  576. for (size_t i = 0; i < looplimit(16); i++) {
  577. mp_random_fill(a);
  578. mp_random_fill(b);
  579. log_start();
  580. mp_int *r = mp_arith(a, b);
  581. log_end();
  582. mp_free(r);
  583. }
  584. mp_free(a);
  585. mp_free(b);
  586. }
  587. static void test_mp_add(void)
  588. {
  589. test_mp_arithmetic(mp_add);
  590. }
  591. static void test_mp_sub(void)
  592. {
  593. test_mp_arithmetic(mp_sub);
  594. }
  595. static void test_mp_mul(void)
  596. {
  597. test_mp_arithmetic(mp_mul);
  598. }
  599. static void test_mp_invert(void)
  600. {
  601. test_mp_arithmetic(mp_invert);
  602. }
  603. static void test_mp_rshift_safe(void)
  604. {
  605. mp_int *x = mp_random_bits(256);
  606. for (size_t i = 0; i < looplimit(mp_max_bits(x)+1); i++) {
  607. log_start();
  608. mp_int *r = mp_rshift_safe(x, i);
  609. log_end();
  610. mp_free(r);
  611. }
  612. mp_free(x);
  613. }
  614. static void test_mp_divmod(void)
  615. {
  616. mp_int *n = mp_new(256), *d = mp_new(256);
  617. mp_int *q = mp_new(256), *r = mp_new(256);
  618. for (size_t i = 0; i < looplimit(32); i++) {
  619. uint8_t sizes[2];
  620. random_read(sizes, 2);
  621. mp_random_bits_into(n, sizes[0]);
  622. mp_random_bits_into(d, sizes[1]);
  623. log_start();
  624. mp_divmod_into(n, d, q, r);
  625. log_end();
  626. }
  627. mp_free(n);
  628. mp_free(d);
  629. mp_free(q);
  630. mp_free(r);
  631. }
  632. static void test_mp_nthroot(void)
  633. {
  634. mp_int *x = mp_new(256), *remainder = mp_new(256);
  635. for (size_t i = 0; i < looplimit(32); i++) {
  636. uint8_t sizes[1];
  637. random_read(sizes, 1);
  638. mp_random_bits_into(x, sizes[0]);
  639. log_start();
  640. mp_free(mp_nthroot(x, 3, remainder));
  641. log_end();
  642. }
  643. mp_free(x);
  644. mp_free(remainder);
  645. }
  646. static void test_mp_modarith(
  647. mp_int *(*mp_modarith)(mp_int *x, mp_int *y, mp_int *modulus))
  648. {
  649. mp_int *base = mp_new(256);
  650. mp_int *exponent = mp_new(256);
  651. mp_int *modulus = mp_new(256);
  652. for (size_t i = 0; i < looplimit(8); i++) {
  653. mp_random_fill(base);
  654. mp_random_fill(exponent);
  655. mp_random_fill(modulus);
  656. mp_set_bit(modulus, 0, 1); /* we only support odd moduli */
  657. log_start();
  658. mp_int *out = mp_modarith(base, exponent, modulus);
  659. log_end();
  660. mp_free(out);
  661. }
  662. mp_free(base);
  663. mp_free(exponent);
  664. mp_free(modulus);
  665. }
  666. static void test_mp_modadd(void)
  667. {
  668. test_mp_modarith(mp_modadd);
  669. }
  670. static void test_mp_modsub(void)
  671. {
  672. test_mp_modarith(mp_modsub);
  673. }
  674. static void test_mp_modmul(void)
  675. {
  676. test_mp_modarith(mp_modmul);
  677. }
  678. static void test_mp_modpow(void)
  679. {
  680. test_mp_modarith(mp_modpow);
  681. }
  682. static void test_mp_invert_mod_2to(void)
  683. {
  684. mp_int *x = mp_new(512);
  685. for (size_t i = 0; i < looplimit(32); i++) {
  686. mp_random_fill(x);
  687. mp_set_bit(x, 0, 1); /* input should be odd */
  688. log_start();
  689. mp_int *out = mp_invert_mod_2to(x, 511);
  690. log_end();
  691. mp_free(out);
  692. }
  693. mp_free(x);
  694. }
  695. static void test_mp_modsqrt(void)
  696. {
  697. /* The prime isn't secret in this function (and in any case
  698. * finding a non-square on the fly would be prohibitively
  699. * annoying), so I hardcode a fixed one, selected to have a lot of
  700. * factors of two in p-1 so as to exercise lots of choices in the
  701. * algorithm. */
  702. mp_int *p =
  703. MP_LITERAL(0xb56a517b206a88c73cfa9ec6f704c7030d18212cace82401);
  704. mp_int *nonsquare = MP_LITERAL(0x5);
  705. size_t bits = mp_max_bits(p);
  706. ModsqrtContext *sc = modsqrt_new(p, nonsquare);
  707. mp_free(p);
  708. mp_free(nonsquare);
  709. mp_int *x = mp_new(bits);
  710. unsigned success;
  711. /* Do one initial call to cause the lazily initialised sub-context
  712. * to be set up. This will take a while, but it can't be helped. */
  713. mp_int *unwanted = mp_modsqrt(sc, x, &success);
  714. mp_free(unwanted);
  715. for (size_t i = 0; i < looplimit(8); i++) {
  716. mp_random_bits_into(x, bits - 1);
  717. log_start();
  718. mp_int *out = mp_modsqrt(sc, x, &success);
  719. log_end();
  720. mp_free(out);
  721. }
  722. mp_free(x);
  723. modsqrt_free(sc);
  724. }
  725. static WeierstrassCurve *wcurve(void)
  726. {
  727. mp_int *p = MP_LITERAL(0xc19337603dc856acf31e01375a696fdf5451);
  728. mp_int *a = MP_LITERAL(0x864946f50eecca4cde7abad4865e34be8f67);
  729. mp_int *b = MP_LITERAL(0x6a5bf56db3a03ba91cfbf3241916c90feeca);
  730. mp_int *nonsquare = mp_from_integer(3);
  731. WeierstrassCurve *wc = ecc_weierstrass_curve(p, a, b, nonsquare);
  732. mp_free(p);
  733. mp_free(a);
  734. mp_free(b);
  735. mp_free(nonsquare);
  736. return wc;
  737. }
  738. static WeierstrassPoint *wpoint(WeierstrassCurve *wc, size_t index)
  739. {
  740. mp_int *x = NULL, *y = NULL;
  741. WeierstrassPoint *wp;
  742. switch (index) {
  743. case 0:
  744. break;
  745. case 1:
  746. x = MP_LITERAL(0x12345);
  747. y = MP_LITERAL(0x3c2c799a365b53d003ef37dab65860bf80ae);
  748. break;
  749. case 2:
  750. x = MP_LITERAL(0x4e1c77e3c00f7c3b15869e6a4e5f86b3ee53);
  751. y = MP_LITERAL(0x5bde01693130591400b5c9d257d8325a44a5);
  752. break;
  753. case 3:
  754. x = MP_LITERAL(0xb5f0e722b2f0f7e729f55ba9f15511e3b399);
  755. y = MP_LITERAL(0x033d636b855c931cfe679f0b18db164a0d64);
  756. break;
  757. case 4:
  758. x = MP_LITERAL(0xb5f0e722b2f0f7e729f55ba9f15511e3b399);
  759. y = MP_LITERAL(0xbe55d3f4b86bc38ff4b6622c418e599546ed);
  760. break;
  761. default:
  762. unreachable("only 5 example Weierstrass points defined");
  763. }
  764. if (x && y) {
  765. wp = ecc_weierstrass_point_new(wc, x, y);
  766. } else {
  767. wp = ecc_weierstrass_point_new_identity(wc);
  768. }
  769. if (x)
  770. mp_free(x);
  771. if (y)
  772. mp_free(y);
  773. return wp;
  774. }
  775. static void test_ecc_weierstrass_add(void)
  776. {
  777. WeierstrassCurve *wc = wcurve();
  778. WeierstrassPoint *a = ecc_weierstrass_point_new_identity(wc);
  779. WeierstrassPoint *b = ecc_weierstrass_point_new_identity(wc);
  780. for (size_t i = 0; i < looplimit(5); i++) {
  781. for (size_t j = 0; j < looplimit(5); j++) {
  782. if (i == 0 || j == 0 || i == j ||
  783. (i==3 && j==4) || (i==4 && j==3))
  784. continue; /* difficult cases */
  785. WeierstrassPoint *A = wpoint(wc, i), *B = wpoint(wc, j);
  786. ecc_weierstrass_point_copy_into(a, A);
  787. ecc_weierstrass_point_copy_into(b, B);
  788. ecc_weierstrass_point_free(A);
  789. ecc_weierstrass_point_free(B);
  790. log_start();
  791. WeierstrassPoint *r = ecc_weierstrass_add(a, b);
  792. log_end();
  793. ecc_weierstrass_point_free(r);
  794. }
  795. }
  796. ecc_weierstrass_point_free(a);
  797. ecc_weierstrass_point_free(b);
  798. ecc_weierstrass_curve_free(wc);
  799. }
  800. static void test_ecc_weierstrass_double(void)
  801. {
  802. WeierstrassCurve *wc = wcurve();
  803. WeierstrassPoint *a = ecc_weierstrass_point_new_identity(wc);
  804. for (size_t i = 0; i < looplimit(5); i++) {
  805. WeierstrassPoint *A = wpoint(wc, i);
  806. ecc_weierstrass_point_copy_into(a, A);
  807. ecc_weierstrass_point_free(A);
  808. log_start();
  809. WeierstrassPoint *r = ecc_weierstrass_double(a);
  810. log_end();
  811. ecc_weierstrass_point_free(r);
  812. }
  813. ecc_weierstrass_point_free(a);
  814. ecc_weierstrass_curve_free(wc);
  815. }
  816. static void test_ecc_weierstrass_add_general(void)
  817. {
  818. WeierstrassCurve *wc = wcurve();
  819. WeierstrassPoint *a = ecc_weierstrass_point_new_identity(wc);
  820. WeierstrassPoint *b = ecc_weierstrass_point_new_identity(wc);
  821. for (size_t i = 0; i < looplimit(5); i++) {
  822. for (size_t j = 0; j < looplimit(5); j++) {
  823. WeierstrassPoint *A = wpoint(wc, i), *B = wpoint(wc, j);
  824. ecc_weierstrass_point_copy_into(a, A);
  825. ecc_weierstrass_point_copy_into(b, B);
  826. ecc_weierstrass_point_free(A);
  827. ecc_weierstrass_point_free(B);
  828. log_start();
  829. WeierstrassPoint *r = ecc_weierstrass_add_general(a, b);
  830. log_end();
  831. ecc_weierstrass_point_free(r);
  832. }
  833. }
  834. ecc_weierstrass_point_free(a);
  835. ecc_weierstrass_point_free(b);
  836. ecc_weierstrass_curve_free(wc);
  837. }
  838. static void test_ecc_weierstrass_multiply(void)
  839. {
  840. WeierstrassCurve *wc = wcurve();
  841. WeierstrassPoint *a = ecc_weierstrass_point_new_identity(wc);
  842. mp_int *exponent = mp_new(56);
  843. for (size_t i = 1; i < looplimit(5); i++) {
  844. WeierstrassPoint *A = wpoint(wc, i);
  845. ecc_weierstrass_point_copy_into(a, A);
  846. ecc_weierstrass_point_free(A);
  847. mp_random_fill(exponent);
  848. log_start();
  849. WeierstrassPoint *r = ecc_weierstrass_multiply(a, exponent);
  850. log_end();
  851. ecc_weierstrass_point_free(r);
  852. }
  853. ecc_weierstrass_point_free(a);
  854. ecc_weierstrass_curve_free(wc);
  855. mp_free(exponent);
  856. }
  857. static void test_ecc_weierstrass_is_identity(void)
  858. {
  859. WeierstrassCurve *wc = wcurve();
  860. WeierstrassPoint *a = ecc_weierstrass_point_new_identity(wc);
  861. for (size_t i = 1; i < looplimit(5); i++) {
  862. WeierstrassPoint *A = wpoint(wc, i);
  863. ecc_weierstrass_point_copy_into(a, A);
  864. ecc_weierstrass_point_free(A);
  865. log_start();
  866. ecc_weierstrass_is_identity(a);
  867. log_end();
  868. }
  869. ecc_weierstrass_point_free(a);
  870. ecc_weierstrass_curve_free(wc);
  871. }
  872. static void test_ecc_weierstrass_get_affine(void)
  873. {
  874. WeierstrassCurve *wc = wcurve();
  875. WeierstrassPoint *r = ecc_weierstrass_point_new_identity(wc);
  876. for (size_t i = 0; i < looplimit(4); i++) {
  877. WeierstrassPoint *A = wpoint(wc, i), *B = wpoint(wc, i+1);
  878. WeierstrassPoint *R = ecc_weierstrass_add_general(A, B);
  879. ecc_weierstrass_point_copy_into(r, R);
  880. ecc_weierstrass_point_free(A);
  881. ecc_weierstrass_point_free(B);
  882. ecc_weierstrass_point_free(R);
  883. log_start();
  884. mp_int *x, *y;
  885. ecc_weierstrass_get_affine(r, &x, &y);
  886. log_end();
  887. mp_free(x);
  888. mp_free(y);
  889. }
  890. ecc_weierstrass_point_free(r);
  891. ecc_weierstrass_curve_free(wc);
  892. }
  893. static void test_ecc_weierstrass_decompress(void)
  894. {
  895. WeierstrassCurve *wc = wcurve();
  896. /* As in the mp_modsqrt test, prime the lazy initialisation of the
  897. * ModsqrtContext */
  898. mp_int *x = mp_new(144);
  899. WeierstrassPoint *a = ecc_weierstrass_point_new_from_x(wc, x, 0);
  900. if (a) /* don't care whether this one succeeded */
  901. ecc_weierstrass_point_free(a);
  902. for (size_t p = 0; p < looplimit(2); p++) {
  903. for (size_t i = 1; i < looplimit(5); i++) {
  904. WeierstrassPoint *A = wpoint(wc, i);
  905. mp_int *X;
  906. ecc_weierstrass_get_affine(A, &X, NULL);
  907. mp_copy_into(x, X);
  908. mp_free(X);
  909. ecc_weierstrass_point_free(A);
  910. log_start();
  911. WeierstrassPoint *a = ecc_weierstrass_point_new_from_x(wc, x, p);
  912. log_end();
  913. ecc_weierstrass_point_free(a);
  914. }
  915. }
  916. mp_free(x);
  917. ecc_weierstrass_curve_free(wc);
  918. }
  919. static MontgomeryCurve *mcurve(void)
  920. {
  921. mp_int *p = MP_LITERAL(0xde978eb1db35236a5792e9f0c04d86000659);
  922. mp_int *a = MP_LITERAL(0x799b62a612b1b30e1c23cea6d67b2e33c51a);
  923. mp_int *b = MP_LITERAL(0x944bf9042b56821a8c9e0b49b636c2502b2b);
  924. MontgomeryCurve *mc = ecc_montgomery_curve(p, a, b);
  925. mp_free(p);
  926. mp_free(a);
  927. mp_free(b);
  928. return mc;
  929. }
  930. static MontgomeryPoint *mpoint(MontgomeryCurve *wc, size_t index)
  931. {
  932. mp_int *x = NULL;
  933. MontgomeryPoint *mp;
  934. switch (index) {
  935. case 0:
  936. x = MP_LITERAL(31415);
  937. break;
  938. case 1:
  939. x = MP_LITERAL(0x4d352c654c06eecfe19104118857b38398e8);
  940. break;
  941. case 2:
  942. x = MP_LITERAL(0x03fca2a73983bc3434caae3134599cd69cce);
  943. break;
  944. case 3:
  945. x = MP_LITERAL(0xa0fd735ce9b3406498b5f035ee655bda4e15);
  946. break;
  947. case 4:
  948. x = MP_LITERAL(0x7c7f46a00cc286dbe47db39b6d8f5efd920e);
  949. break;
  950. case 5:
  951. x = MP_LITERAL(0x07a6dc30d3b320448e6f8999be417e6b7c6b);
  952. break;
  953. case 6:
  954. x = MP_LITERAL(0x7832da5fc16dfbd358170b2b96896cd3cd06);
  955. break;
  956. default:
  957. unreachable("only 7 example Weierstrass points defined");
  958. }
  959. mp = ecc_montgomery_point_new(wc, x);
  960. mp_free(x);
  961. return mp;
  962. }
  963. static void test_ecc_montgomery_diff_add(void)
  964. {
  965. MontgomeryCurve *wc = mcurve();
  966. MontgomeryPoint *a = NULL, *b = NULL, *c = NULL;
  967. for (size_t i = 0; i < looplimit(5); i++) {
  968. MontgomeryPoint *A = mpoint(wc, i);
  969. MontgomeryPoint *B = mpoint(wc, i);
  970. MontgomeryPoint *C = mpoint(wc, i);
  971. if (!a) {
  972. a = A;
  973. b = B;
  974. c = C;
  975. } else {
  976. ecc_montgomery_point_copy_into(a, A);
  977. ecc_montgomery_point_copy_into(b, B);
  978. ecc_montgomery_point_copy_into(c, C);
  979. ecc_montgomery_point_free(A);
  980. ecc_montgomery_point_free(B);
  981. ecc_montgomery_point_free(C);
  982. }
  983. log_start();
  984. MontgomeryPoint *r = ecc_montgomery_diff_add(b, c, a);
  985. log_end();
  986. ecc_montgomery_point_free(r);
  987. }
  988. ecc_montgomery_point_free(a);
  989. ecc_montgomery_point_free(b);
  990. ecc_montgomery_point_free(c);
  991. ecc_montgomery_curve_free(wc);
  992. }
  993. static void test_ecc_montgomery_double(void)
  994. {
  995. MontgomeryCurve *wc = mcurve();
  996. MontgomeryPoint *a = NULL;
  997. for (size_t i = 0; i < looplimit(7); i++) {
  998. MontgomeryPoint *A = mpoint(wc, i);
  999. if (!a) {
  1000. a = A;
  1001. } else {
  1002. ecc_montgomery_point_copy_into(a, A);
  1003. ecc_montgomery_point_free(A);
  1004. }
  1005. log_start();
  1006. MontgomeryPoint *r = ecc_montgomery_double(a);
  1007. log_end();
  1008. ecc_montgomery_point_free(r);
  1009. }
  1010. ecc_montgomery_point_free(a);
  1011. ecc_montgomery_curve_free(wc);
  1012. }
  1013. static void test_ecc_montgomery_multiply(void)
  1014. {
  1015. MontgomeryCurve *wc = mcurve();
  1016. MontgomeryPoint *a = NULL;
  1017. mp_int *exponent = mp_new(56);
  1018. for (size_t i = 0; i < looplimit(7); i++) {
  1019. MontgomeryPoint *A = mpoint(wc, i);
  1020. if (!a) {
  1021. a = A;
  1022. } else {
  1023. ecc_montgomery_point_copy_into(a, A);
  1024. ecc_montgomery_point_free(A);
  1025. }
  1026. mp_random_fill(exponent);
  1027. log_start();
  1028. MontgomeryPoint *r = ecc_montgomery_multiply(a, exponent);
  1029. log_end();
  1030. ecc_montgomery_point_free(r);
  1031. }
  1032. ecc_montgomery_point_free(a);
  1033. ecc_montgomery_curve_free(wc);
  1034. mp_free(exponent);
  1035. }
  1036. static void test_ecc_montgomery_get_affine(void)
  1037. {
  1038. MontgomeryCurve *wc = mcurve();
  1039. MontgomeryPoint *r = NULL;
  1040. for (size_t i = 0; i < looplimit(5); i++) {
  1041. MontgomeryPoint *A = mpoint(wc, i);
  1042. MontgomeryPoint *B = mpoint(wc, i);
  1043. MontgomeryPoint *C = mpoint(wc, i);
  1044. MontgomeryPoint *R = ecc_montgomery_diff_add(B, C, A);
  1045. ecc_montgomery_point_free(A);
  1046. ecc_montgomery_point_free(B);
  1047. ecc_montgomery_point_free(C);
  1048. if (!r) {
  1049. r = R;
  1050. } else {
  1051. ecc_montgomery_point_copy_into(r, R);
  1052. ecc_montgomery_point_free(R);
  1053. }
  1054. log_start();
  1055. mp_int *x;
  1056. ecc_montgomery_get_affine(r, &x);
  1057. log_end();
  1058. mp_free(x);
  1059. }
  1060. ecc_montgomery_point_free(r);
  1061. ecc_montgomery_curve_free(wc);
  1062. }
  1063. static EdwardsCurve *ecurve(void)
  1064. {
  1065. mp_int *p = MP_LITERAL(0xfce2dac1704095de0b5c48876c45063cd475);
  1066. mp_int *d = MP_LITERAL(0xbd4f77401c3b14ae1742a7d1d367adac8f3e);
  1067. mp_int *a = MP_LITERAL(0x51d0845da3fa871aaac4341adea53b861919);
  1068. mp_int *nonsquare = mp_from_integer(2);
  1069. EdwardsCurve *ec = ecc_edwards_curve(p, d, a, nonsquare);
  1070. mp_free(p);
  1071. mp_free(d);
  1072. mp_free(a);
  1073. mp_free(nonsquare);
  1074. return ec;
  1075. }
  1076. static EdwardsPoint *epoint(EdwardsCurve *wc, size_t index)
  1077. {
  1078. mp_int *x, *y;
  1079. EdwardsPoint *ep;
  1080. switch (index) {
  1081. case 0:
  1082. x = MP_LITERAL(0x0);
  1083. y = MP_LITERAL(0x1);
  1084. break;
  1085. case 1:
  1086. x = MP_LITERAL(0x3d8aef0294a67c1c7e8e185d987716250d7c);
  1087. y = MP_LITERAL(0x27184);
  1088. break;
  1089. case 2:
  1090. x = MP_LITERAL(0xf44ed5b8a6debfd3ab24b7874cd2589fd672);
  1091. y = MP_LITERAL(0xd635d8d15d367881c8a3af472c8fe487bf40);
  1092. break;
  1093. case 3:
  1094. x = MP_LITERAL(0xde114ecc8b944684415ef81126a07269cd30);
  1095. y = MP_LITERAL(0xbe0fd45ff67ebba047ed0ec5a85d22e688a1);
  1096. break;
  1097. case 4:
  1098. x = MP_LITERAL(0x76bd2f90898d271b492c9c20dd7bbfe39fe5);
  1099. y = MP_LITERAL(0xbf1c82698b4a5a12c1057631c1ebdc216ae2);
  1100. break;
  1101. default:
  1102. unreachable("only 5 example Edwards points defined");
  1103. }
  1104. ep = ecc_edwards_point_new(wc, x, y);
  1105. mp_free(x);
  1106. mp_free(y);
  1107. return ep;
  1108. }
  1109. static void test_ecc_edwards_add(void)
  1110. {
  1111. EdwardsCurve *ec = ecurve();
  1112. EdwardsPoint *a = NULL, *b = NULL;
  1113. for (size_t i = 0; i < looplimit(5); i++) {
  1114. for (size_t j = 0; j < looplimit(5); j++) {
  1115. EdwardsPoint *A = epoint(ec, i), *B = epoint(ec, j);
  1116. if (!a) {
  1117. a = A;
  1118. b = B;
  1119. } else {
  1120. ecc_edwards_point_copy_into(a, A);
  1121. ecc_edwards_point_copy_into(b, B);
  1122. ecc_edwards_point_free(A);
  1123. ecc_edwards_point_free(B);
  1124. }
  1125. log_start();
  1126. EdwardsPoint *r = ecc_edwards_add(a, b);
  1127. log_end();
  1128. ecc_edwards_point_free(r);
  1129. }
  1130. }
  1131. ecc_edwards_point_free(a);
  1132. ecc_edwards_point_free(b);
  1133. ecc_edwards_curve_free(ec);
  1134. }
  1135. static void test_ecc_edwards_multiply(void)
  1136. {
  1137. EdwardsCurve *ec = ecurve();
  1138. EdwardsPoint *a = NULL;
  1139. mp_int *exponent = mp_new(56);
  1140. for (size_t i = 1; i < looplimit(5); i++) {
  1141. EdwardsPoint *A = epoint(ec, i);
  1142. if (!a) {
  1143. a = A;
  1144. } else {
  1145. ecc_edwards_point_copy_into(a, A);
  1146. ecc_edwards_point_free(A);
  1147. }
  1148. mp_random_fill(exponent);
  1149. log_start();
  1150. EdwardsPoint *r = ecc_edwards_multiply(a, exponent);
  1151. log_end();
  1152. ecc_edwards_point_free(r);
  1153. }
  1154. ecc_edwards_point_free(a);
  1155. ecc_edwards_curve_free(ec);
  1156. mp_free(exponent);
  1157. }
  1158. static void test_ecc_edwards_eq(void)
  1159. {
  1160. EdwardsCurve *ec = ecurve();
  1161. EdwardsPoint *a = NULL, *b = NULL;
  1162. for (size_t i = 0; i < looplimit(5); i++) {
  1163. for (size_t j = 0; j < looplimit(5); j++) {
  1164. EdwardsPoint *A = epoint(ec, i), *B = epoint(ec, j);
  1165. if (!a) {
  1166. a = A;
  1167. b = B;
  1168. } else {
  1169. ecc_edwards_point_copy_into(a, A);
  1170. ecc_edwards_point_copy_into(b, B);
  1171. ecc_edwards_point_free(A);
  1172. ecc_edwards_point_free(B);
  1173. }
  1174. log_start();
  1175. ecc_edwards_eq(a, b);
  1176. log_end();
  1177. }
  1178. }
  1179. ecc_edwards_point_free(a);
  1180. ecc_edwards_point_free(b);
  1181. ecc_edwards_curve_free(ec);
  1182. }
  1183. static void test_ecc_edwards_get_affine(void)
  1184. {
  1185. EdwardsCurve *ec = ecurve();
  1186. EdwardsPoint *r = NULL;
  1187. for (size_t i = 0; i < looplimit(4); i++) {
  1188. EdwardsPoint *A = epoint(ec, i), *B = epoint(ec, i+1);
  1189. EdwardsPoint *R = ecc_edwards_add(A, B);
  1190. ecc_edwards_point_free(A);
  1191. ecc_edwards_point_free(B);
  1192. if (!r) {
  1193. r = R;
  1194. } else {
  1195. ecc_edwards_point_copy_into(r, R);
  1196. ecc_edwards_point_free(R);
  1197. }
  1198. log_start();
  1199. mp_int *x, *y;
  1200. ecc_edwards_get_affine(r, &x, &y);
  1201. log_end();
  1202. mp_free(x);
  1203. mp_free(y);
  1204. }
  1205. ecc_edwards_point_free(r);
  1206. ecc_edwards_curve_free(ec);
  1207. }
  1208. static void test_ecc_edwards_decompress(void)
  1209. {
  1210. EdwardsCurve *ec = ecurve();
  1211. /* As in the mp_modsqrt test, prime the lazy initialisation of the
  1212. * ModsqrtContext */
  1213. mp_int *y = mp_new(144);
  1214. EdwardsPoint *a = ecc_edwards_point_new_from_y(ec, y, 0);
  1215. if (a) /* don't care whether this one succeeded */
  1216. ecc_edwards_point_free(a);
  1217. for (size_t p = 0; p < looplimit(2); p++) {
  1218. for (size_t i = 0; i < looplimit(5); i++) {
  1219. EdwardsPoint *A = epoint(ec, i);
  1220. mp_int *Y;
  1221. ecc_edwards_get_affine(A, NULL, &Y);
  1222. mp_copy_into(y, Y);
  1223. mp_free(Y);
  1224. ecc_edwards_point_free(A);
  1225. log_start();
  1226. EdwardsPoint *a = ecc_edwards_point_new_from_y(ec, y, p);
  1227. log_end();
  1228. ecc_edwards_point_free(a);
  1229. }
  1230. }
  1231. mp_free(y);
  1232. ecc_edwards_curve_free(ec);
  1233. }
  1234. static void test_cipher(const ssh_cipheralg *calg)
  1235. {
  1236. ssh_cipher *c = ssh_cipher_new(calg);
  1237. if (!c) {
  1238. test_skipped = true;
  1239. return;
  1240. }
  1241. const ssh2_macalg *malg = calg->required_mac;
  1242. ssh2_mac *m = NULL;
  1243. if (malg) {
  1244. m = ssh2_mac_new(malg, c);
  1245. if (!m) {
  1246. ssh_cipher_free(c);
  1247. test_skipped = true;
  1248. return;
  1249. }
  1250. }
  1251. uint8_t *ckey = snewn(calg->padded_keybytes, uint8_t);
  1252. uint8_t *civ = snewn(calg->blksize, uint8_t);
  1253. uint8_t *mkey = malg ? snewn(malg->keylen, uint8_t) : NULL;
  1254. size_t datalen = calg->blksize * 8;
  1255. size_t maclen = malg ? malg->len : 0;
  1256. uint8_t *data = snewn(datalen + maclen, uint8_t);
  1257. size_t lenlen = 4;
  1258. uint8_t *lendata = snewn(lenlen, uint8_t);
  1259. for (size_t i = 0; i < looplimit(16); i++) {
  1260. random_read(ckey, calg->padded_keybytes);
  1261. if (malg)
  1262. random_read(mkey, malg->keylen);
  1263. random_read(data, datalen);
  1264. random_read(lendata, lenlen);
  1265. if (i == 0) {
  1266. /* Ensure one of our test IVs will cause SDCTR wraparound */
  1267. memset(civ, 0xFF, calg->blksize);
  1268. } else {
  1269. random_read(civ, calg->blksize);
  1270. }
  1271. uint8_t seqbuf[4];
  1272. random_read(seqbuf, 4);
  1273. uint32_t seq = GET_32BIT_MSB_FIRST(seqbuf);
  1274. log_start();
  1275. ssh_cipher_setkey(c, ckey);
  1276. ssh_cipher_setiv(c, civ);
  1277. if (m)
  1278. ssh2_mac_setkey(m, make_ptrlen(mkey, malg->keylen));
  1279. if (calg->flags & SSH_CIPHER_SEPARATE_LENGTH)
  1280. ssh_cipher_encrypt_length(c, data, datalen, seq);
  1281. ssh_cipher_encrypt(c, data, datalen);
  1282. if (m) {
  1283. ssh2_mac_generate(m, data, datalen, seq);
  1284. ssh2_mac_verify(m, data, datalen, seq);
  1285. }
  1286. if (calg->flags & SSH_CIPHER_SEPARATE_LENGTH)
  1287. ssh_cipher_decrypt_length(c, data, datalen, seq);
  1288. ssh_cipher_decrypt(c, data, datalen);
  1289. log_end();
  1290. }
  1291. sfree(ckey);
  1292. sfree(civ);
  1293. sfree(mkey);
  1294. sfree(data);
  1295. sfree(lendata);
  1296. if (m)
  1297. ssh2_mac_free(m);
  1298. ssh_cipher_free(c);
  1299. }
  1300. #define CIPHER_TESTFN(Y_unused, cipher) \
  1301. static void test_cipher_##cipher(void) { test_cipher(&cipher); }
  1302. CIPHERS(CIPHER_TESTFN, Y_unused)
  1303. static void test_mac(const ssh2_macalg *malg, const ssh_cipheralg *calg)
  1304. {
  1305. ssh_cipher *c = NULL;
  1306. if (calg) {
  1307. c = ssh_cipher_new(calg);
  1308. if (!c) {
  1309. test_skipped = true;
  1310. return;
  1311. }
  1312. }
  1313. ssh2_mac *m = ssh2_mac_new(malg, c);
  1314. if (!m) {
  1315. test_skipped = true;
  1316. if (c)
  1317. ssh_cipher_free(c);
  1318. return;
  1319. }
  1320. size_t ckeylen = calg ? calg->padded_keybytes : 0;
  1321. size_t civlen = calg ? calg->blksize : 0;
  1322. uint8_t *ckey = snewn(ckeylen, uint8_t);
  1323. uint8_t *civ = snewn(civlen, uint8_t);
  1324. uint8_t *mkey = snewn(malg->keylen, uint8_t);
  1325. size_t datalen = 256;
  1326. size_t maclen = malg->len;
  1327. uint8_t *data = snewn(datalen + maclen, uint8_t);
  1328. for (size_t i = 0; i < looplimit(16); i++) {
  1329. random_read(ckey, ckeylen);
  1330. random_read(civ, civlen);
  1331. random_read(mkey, malg->keylen);
  1332. random_read(data, datalen);
  1333. uint8_t seqbuf[4];
  1334. random_read(seqbuf, 4);
  1335. uint32_t seq = GET_32BIT_MSB_FIRST(seqbuf);
  1336. log_start();
  1337. if (c) {
  1338. ssh_cipher_setkey(c, ckey);
  1339. ssh_cipher_setiv(c, civ);
  1340. }
  1341. ssh2_mac_setkey(m, make_ptrlen(mkey, malg->keylen));
  1342. ssh2_mac_generate(m, data, datalen, seq);
  1343. ssh2_mac_verify(m, data, datalen, seq);
  1344. log_end();
  1345. }
  1346. sfree(ckey);
  1347. sfree(civ);
  1348. sfree(mkey);
  1349. sfree(data);
  1350. ssh2_mac_free(m);
  1351. if (c)
  1352. ssh_cipher_free(c);
  1353. }
  1354. #define MAC_TESTFN(Y_unused, mac) \
  1355. static void test_mac_##mac(void) { test_mac(&mac, NULL); }
  1356. SIMPLE_MACS(MAC_TESTFN, Y_unused)
  1357. static void test_mac_poly1305(void)
  1358. {
  1359. test_mac(&ssh2_poly1305, &ssh2_chacha20_poly1305);
  1360. }
  1361. static void test_mac_aesgcm_sw_sw(void)
  1362. {
  1363. test_mac(&ssh2_aesgcm_mac_sw, &ssh_aes128_gcm_sw);
  1364. }
  1365. static void test_mac_aesgcm_sw_refpoly(void)
  1366. {
  1367. test_mac(&ssh2_aesgcm_mac_ref_poly, &ssh_aes128_gcm_sw);
  1368. }
  1369. #if HAVE_AES_NI
  1370. static void test_mac_aesgcm_ni_sw(void)
  1371. {
  1372. test_mac(&ssh2_aesgcm_mac_sw, &ssh_aes128_gcm_ni);
  1373. }
  1374. #endif
  1375. #if HAVE_NEON_CRYPTO
  1376. static void test_mac_aesgcm_neon_sw(void)
  1377. {
  1378. test_mac(&ssh2_aesgcm_mac_sw, &ssh_aes128_gcm_neon);
  1379. }
  1380. #endif
  1381. #if HAVE_CLMUL
  1382. static void test_mac_aesgcm_sw_clmul(void)
  1383. {
  1384. test_mac(&ssh2_aesgcm_mac_clmul, &ssh_aes128_gcm_sw);
  1385. }
  1386. #endif
  1387. #if HAVE_NEON_PMULL
  1388. static void test_mac_aesgcm_sw_neon(void)
  1389. {
  1390. test_mac(&ssh2_aesgcm_mac_neon, &ssh_aes128_gcm_sw);
  1391. }
  1392. #endif
  1393. #if HAVE_AES_NI && HAVE_CLMUL
  1394. static void test_mac_aesgcm_ni_clmul(void)
  1395. {
  1396. test_mac(&ssh2_aesgcm_mac_clmul, &ssh_aes128_gcm_ni);
  1397. }
  1398. #endif
  1399. #if HAVE_NEON_CRYPTO && HAVE_NEON_PMULL
  1400. static void test_mac_aesgcm_neon_neon(void)
  1401. {
  1402. test_mac(&ssh2_aesgcm_mac_neon, &ssh_aes128_gcm_neon);
  1403. }
  1404. #endif
  1405. static void test_hash(const ssh_hashalg *halg)
  1406. {
  1407. ssh_hash *h = ssh_hash_new(halg);
  1408. if (!h) {
  1409. test_skipped = true;
  1410. return;
  1411. }
  1412. ssh_hash_free(h);
  1413. size_t datalen = 256;
  1414. uint8_t *data = snewn(datalen, uint8_t);
  1415. uint8_t *hash = snewn(halg->hlen, uint8_t);
  1416. for (size_t i = 0; i < looplimit(16); i++) {
  1417. random_read(data, datalen);
  1418. log_start();
  1419. h = ssh_hash_new(halg);
  1420. put_data(h, data, datalen);
  1421. ssh_hash_final(h, hash);
  1422. log_end();
  1423. }
  1424. sfree(data);
  1425. sfree(hash);
  1426. }
  1427. #define HASH_TESTFN(Y_unused, hash) \
  1428. static void test_hash_##hash(void) { test_hash(&hash); }
  1429. HASHES(HASH_TESTFN, Y_unused)
  1430. struct test {
  1431. const char *testname;
  1432. void (*testfn)(void);
  1433. };
  1434. static void test_argon2(void)
  1435. {
  1436. /*
  1437. * We can only expect the Argon2i variant to pass this stringent
  1438. * test for no data-dependency, because the other two variants of
  1439. * Argon2 have _deliberate_ data-dependency.
  1440. */
  1441. size_t inlen = 48+16+24+8;
  1442. uint8_t *indata = snewn(inlen, uint8_t);
  1443. ptrlen password = make_ptrlen(indata, 48);
  1444. ptrlen salt = make_ptrlen(indata+48, 16);
  1445. ptrlen secret = make_ptrlen(indata+48+16, 24);
  1446. ptrlen assoc = make_ptrlen(indata+48+16+24, 8);
  1447. strbuf *outdata = strbuf_new();
  1448. strbuf_append(outdata, 256);
  1449. for (size_t i = 0; i < looplimit(16); i++) {
  1450. strbuf_clear(outdata);
  1451. random_read(indata, inlen);
  1452. log_start();
  1453. argon2(Argon2i, 32, 2, 2, 144, password, salt, secret, assoc, outdata);
  1454. log_end();
  1455. }
  1456. sfree(indata);
  1457. strbuf_free(outdata);
  1458. }
  1459. static void test_primegen(const PrimeGenerationPolicy *policy)
  1460. {
  1461. static ProgressReceiver null_progress = { .vt = &null_progress_vt };
  1462. PrimeGenerationContext *pgc = primegen_new_context(policy);
  1463. init_smallprimes();
  1464. mp_int *pcopy = mp_new(128);
  1465. for (size_t i = 0; i < looplimit(2); i++) {
  1466. while (true) {
  1467. random_advance_counter();
  1468. struct random_state st = random_get_state();
  1469. PrimeCandidateSource *pcs = pcs_new(128);
  1470. pcs_set_oneshot(pcs);
  1471. pcs_ready(pcs);
  1472. mp_int *p = primegen_generate(pgc, pcs, &null_progress);
  1473. if (p) {
  1474. mp_copy_into(pcopy, p);
  1475. sfree(p);
  1476. random_set_state(st);
  1477. log_start();
  1478. PrimeCandidateSource *pcs = pcs_new(128);
  1479. pcs_set_oneshot(pcs);
  1480. pcs_ready(pcs);
  1481. mp_int *q = primegen_generate(pgc, pcs, &null_progress);
  1482. log_end();
  1483. assert(q);
  1484. assert(mp_cmp_eq(pcopy, q));
  1485. mp_free(q);
  1486. break;
  1487. }
  1488. }
  1489. }
  1490. mp_free(pcopy);
  1491. primegen_free_context(pgc);
  1492. }
  1493. static void test_primegen_probabilistic(void)
  1494. {
  1495. test_primegen(&primegen_probabilistic);
  1496. }
  1497. static void test_ntru(void)
  1498. {
  1499. unsigned p = 11, q = 59, w = 3;
  1500. uint16_t *pubkey_orig = snewn(p, uint16_t);
  1501. uint16_t *pubkey_check = snewn(p, uint16_t);
  1502. uint16_t *pubkey = snewn(p, uint16_t);
  1503. uint16_t *plaintext = snewn(p, uint16_t);
  1504. uint16_t *ciphertext = snewn(p, uint16_t);
  1505. strbuf *buffer = strbuf_new();
  1506. strbuf_append(buffer, 16384);
  1507. BinarySource src[1];
  1508. for (size_t i = 0; i < looplimit(32); i++) {
  1509. while (true) {
  1510. random_advance_counter();
  1511. struct random_state st = random_get_state();
  1512. NTRUKeyPair *keypair = ntru_keygen_attempt(p, q, w);
  1513. if (keypair) {
  1514. memcpy(pubkey_orig, ntru_pubkey(keypair),
  1515. p*sizeof(*pubkey_orig));
  1516. ntru_keypair_free(keypair);
  1517. random_set_state(st);
  1518. log_start();
  1519. NTRUKeyPair *keypair = ntru_keygen_attempt(p, q, w);
  1520. memcpy(pubkey_check, ntru_pubkey(keypair),
  1521. p*sizeof(*pubkey_check));
  1522. ntru_gen_short(plaintext, p, w);
  1523. ntru_encrypt(ciphertext, plaintext, pubkey, p, w);
  1524. ntru_decrypt(plaintext, ciphertext, keypair);
  1525. strbuf_clear(buffer);
  1526. ntru_encode_pubkey(ntru_pubkey(keypair), p, q,
  1527. BinarySink_UPCAST(buffer));
  1528. BinarySource_BARE_INIT_PL(src, ptrlen_from_strbuf(buffer));
  1529. ntru_decode_pubkey(pubkey, p, q, src);
  1530. strbuf_clear(buffer);
  1531. ntru_encode_ciphertext(ciphertext, p, q,
  1532. BinarySink_UPCAST(buffer));
  1533. BinarySource_BARE_INIT_PL(src, ptrlen_from_strbuf(buffer));
  1534. ntru_decode_ciphertext(ciphertext, keypair, src);
  1535. strbuf_clear(buffer);
  1536. ntru_encode_plaintext(plaintext, p, BinarySink_UPCAST(buffer));
  1537. log_end();
  1538. ntru_keypair_free(keypair);
  1539. break;
  1540. }
  1541. assert(!memcmp(pubkey_orig, pubkey_check,
  1542. p*sizeof(*pubkey_check)));
  1543. }
  1544. }
  1545. sfree(pubkey_orig);
  1546. sfree(pubkey_check);
  1547. sfree(pubkey);
  1548. sfree(plaintext);
  1549. sfree(ciphertext);
  1550. strbuf_free(buffer);
  1551. }
  1552. static void test_rfc6979_setup(void)
  1553. {
  1554. mp_int *q = mp_new(512);
  1555. mp_int *x = mp_new(512);
  1556. strbuf *message = strbuf_new();
  1557. strbuf_append(message, 123);
  1558. RFC6979 *s = rfc6979_new(&ssh_sha256, q, x);
  1559. for (size_t i = 0; i < looplimit(20); i++) {
  1560. random_read(message->u, message->len);
  1561. mp_random_fill(q);
  1562. mp_random_fill(x);
  1563. log_start();
  1564. rfc6979_setup(s, ptrlen_from_strbuf(message));
  1565. log_end();
  1566. }
  1567. rfc6979_free(s);
  1568. mp_free(q);
  1569. mp_free(x);
  1570. strbuf_free(message);
  1571. }
  1572. static void test_rfc6979_attempt(void)
  1573. {
  1574. mp_int *q = mp_new(512);
  1575. mp_int *x = mp_new(512);
  1576. strbuf *message = strbuf_new();
  1577. strbuf_append(message, 123);
  1578. RFC6979 *s = rfc6979_new(&ssh_sha256, q, x);
  1579. for (size_t i = 0; i < looplimit(5); i++) {
  1580. random_read(message->u, message->len);
  1581. mp_random_fill(q);
  1582. mp_random_fill(x);
  1583. rfc6979_setup(s, ptrlen_from_strbuf(message));
  1584. for (size_t j = 0; j < looplimit(10); j++) {
  1585. log_start();
  1586. RFC6979Result result = rfc6979_attempt(s);
  1587. mp_free(result.k);
  1588. log_end();
  1589. }
  1590. }
  1591. rfc6979_free(s);
  1592. mp_free(q);
  1593. mp_free(x);
  1594. strbuf_free(message);
  1595. }
  1596. static const struct test tests[] = {
  1597. #define STRUCT_TEST(X) { #X, test_##X },
  1598. TESTLIST(STRUCT_TEST)
  1599. #undef STRUCT_TEST
  1600. };
  1601. void dputs(const char *buf)
  1602. {
  1603. fputs(buf, stderr);
  1604. }
  1605. int main(int argc, char **argv)
  1606. {
  1607. bool doing_opts = true;
  1608. const char *pname = argv[0];
  1609. uint8_t tests_to_run[lenof(tests)];
  1610. bool keep_outfiles = false;
  1611. bool test_names_given = false;
  1612. memset(tests_to_run, 1, sizeof(tests_to_run));
  1613. random_hash = ssh_hash_new(&ssh_sha256);
  1614. while (--argc > 0) {
  1615. char *p = *++argv;
  1616. if (p[0] == '-' && doing_opts) {
  1617. if (!strcmp(p, "-O")) {
  1618. if (--argc <= 0) {
  1619. fprintf(stderr, "'-O' expects a directory name\n");
  1620. return 1;
  1621. }
  1622. outdir = *++argv;
  1623. } else if (!strcmp(p, "-k") || !strcmp(p, "--keep")) {
  1624. keep_outfiles = true;
  1625. } else if (!strcmp(p, "--")) {
  1626. doing_opts = false;
  1627. } else if (!strcmp(p, "--help")) {
  1628. printf(" usage: drrun -c test/sclog/libsclog.so -- "
  1629. "%s -O <outdir>\n", pname);
  1630. printf("options: -O <outdir> "
  1631. "put log files in the specified directory\n");
  1632. printf(" -k, --keep "
  1633. "do not delete log files for tests that passed\n");
  1634. printf(" also: --help "
  1635. "display this text\n");
  1636. return 0;
  1637. } else {
  1638. fprintf(stderr, "unknown command line option '%s'\n", p);
  1639. return 1;
  1640. }
  1641. } else {
  1642. if (!test_names_given) {
  1643. test_names_given = true;
  1644. memset(tests_to_run, 0, sizeof(tests_to_run));
  1645. }
  1646. bool found_one = false;
  1647. for (size_t i = 0; i < lenof(tests); i++) {
  1648. if (wc_match(p, tests[i].testname)) {
  1649. tests_to_run[i] = 1;
  1650. found_one = true;
  1651. }
  1652. }
  1653. if (!found_one) {
  1654. fprintf(stderr, "no test name matched '%s'\n", p);
  1655. return 1;
  1656. }
  1657. }
  1658. }
  1659. bool is_dry_run = dry_run();
  1660. if (is_dry_run) {
  1661. printf("Dry run (DynamoRIO instrumentation not detected)\n");
  1662. } else {
  1663. /* Print the address of main() in this run. The idea is that
  1664. * if this image is compiled to be position-independent, then
  1665. * PC values in the logs won't match the ones you get if you
  1666. * disassemble the binary, so it'll be harder to match up the
  1667. * log messages to the code. But if you know the address of a
  1668. * fixed (and not inlined) function in both worlds, you can
  1669. * find out the offset between them. */
  1670. printf("Live run, main = %p\n", (void *)main);
  1671. if (!outdir) {
  1672. fprintf(stderr, "expected -O <outdir> option\n");
  1673. return 1;
  1674. }
  1675. printf("Will write log files to %s\n", outdir);
  1676. }
  1677. size_t nrun = 0, npass = 0;
  1678. for (size_t i = 0; i < lenof(tests); i++) {
  1679. bool keep_these_outfiles = true;
  1680. if (!tests_to_run[i])
  1681. continue;
  1682. const struct test *test = &tests[i];
  1683. printf("Running test %s ... ", test->testname);
  1684. fflush(stdout);
  1685. test_skipped = false;
  1686. random_seed(test->testname);
  1687. test_basename = test->testname;
  1688. test_index = 0;
  1689. test->testfn();
  1690. if (test_skipped) {
  1691. /* Used for e.g. tests of hardware-accelerated crypto when
  1692. * the hardware acceleration isn't available */
  1693. printf("skipped\n");
  1694. continue;
  1695. }
  1696. nrun++;
  1697. if (is_dry_run) {
  1698. printf("dry run done\n");
  1699. continue; /* test files won't exist anyway */
  1700. }
  1701. if (test_index < 2) {
  1702. printf("FAIL: test did not generate multiple output files\n");
  1703. goto test_done;
  1704. }
  1705. char *firstfile = log_filename(test_basename, 0);
  1706. FILE *firstfp = fopen(firstfile, "rb");
  1707. if (!firstfp) {
  1708. printf("ERR: %s: open: %s\n", firstfile, strerror(errno));
  1709. goto test_done;
  1710. }
  1711. for (size_t i = 1; i < test_index; i++) {
  1712. char *nextfile = log_filename(test_basename, i);
  1713. FILE *nextfp = fopen(nextfile, "rb");
  1714. if (!nextfp) {
  1715. printf("ERR: %s: open: %s\n", nextfile, strerror(errno));
  1716. goto test_done;
  1717. }
  1718. rewind(firstfp);
  1719. char buf1[4096], bufn[4096];
  1720. bool compare_ok = false;
  1721. while (true) {
  1722. size_t r1 = fread(buf1, 1, sizeof(buf1), firstfp);
  1723. size_t rn = fread(bufn, 1, sizeof(bufn), nextfp);
  1724. if (r1 != rn) {
  1725. printf("FAIL: %s %s: different lengths\n",
  1726. firstfile, nextfile);
  1727. break;
  1728. }
  1729. if (r1 == 0) {
  1730. if (feof(firstfp) && feof(nextfp)) {
  1731. compare_ok = true;
  1732. } else {
  1733. printf("FAIL: %s %s: error at end of file\n",
  1734. firstfile, nextfile);
  1735. }
  1736. break;
  1737. }
  1738. if (memcmp(buf1, bufn, r1) != 0) {
  1739. printf("FAIL: %s %s: different content\n",
  1740. firstfile, nextfile);
  1741. break;
  1742. }
  1743. }
  1744. fclose(nextfp);
  1745. sfree(nextfile);
  1746. if (!compare_ok) {
  1747. goto test_done;
  1748. }
  1749. }
  1750. fclose(firstfp);
  1751. sfree(firstfile);
  1752. printf("pass\n");
  1753. npass++;
  1754. keep_these_outfiles = keep_outfiles;
  1755. test_done:
  1756. if (!keep_these_outfiles) {
  1757. for (size_t i = 0; i < test_index; i++) {
  1758. char *file = log_filename(test_basename, i);
  1759. remove(file);
  1760. sfree(file);
  1761. }
  1762. }
  1763. }
  1764. ssh_hash_free(random_hash);
  1765. if (npass == nrun) {
  1766. printf("All tests passed\n");
  1767. return 0;
  1768. } else {
  1769. printf("%"SIZEu" tests failed\n", nrun - npass);
  1770. return 1;
  1771. }
  1772. }