testsc.c 60 KB

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