bntest.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512
  1. /*
  2. * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <assert.h>
  10. #include <errno.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <ctype.h>
  14. #include <openssl/bn.h>
  15. #include <openssl/crypto.h>
  16. #include <openssl/err.h>
  17. #include <openssl/rand.h>
  18. #include "internal/nelem.h"
  19. #include "internal/numbers.h"
  20. #include "testutil.h"
  21. #ifdef OPENSSL_SYS_WINDOWS
  22. # define strcasecmp _stricmp
  23. #endif
  24. /*
  25. * Things in boring, not in openssl. TODO we should add them.
  26. */
  27. #define HAVE_BN_PADDED 0
  28. #define HAVE_BN_SQRT 0
  29. typedef struct filetest_st {
  30. const char *name;
  31. int (*func)(STANZA *s);
  32. } FILETEST;
  33. typedef struct mpitest_st {
  34. const char *base10;
  35. const char *mpi;
  36. size_t mpi_len;
  37. } MPITEST;
  38. static const int NUM0 = 100; /* number of tests */
  39. static const int NUM1 = 50; /* additional tests for some functions */
  40. static BN_CTX *ctx;
  41. /*
  42. * Polynomial coefficients used in GFM tests.
  43. */
  44. #ifndef OPENSSL_NO_EC2M
  45. static int p0[] = { 163, 7, 6, 3, 0, -1 };
  46. static int p1[] = { 193, 15, 0, -1 };
  47. #endif
  48. /*
  49. * Look for |key| in the stanza and return it or NULL if not found.
  50. */
  51. static const char *findattr(STANZA *s, const char *key)
  52. {
  53. int i = s->numpairs;
  54. PAIR *pp = s->pairs;
  55. for ( ; --i >= 0; pp++)
  56. if (strcasecmp(pp->key, key) == 0)
  57. return pp->value;
  58. return NULL;
  59. }
  60. /*
  61. * Parse BIGNUM from sparse hex-strings, return |BN_hex2bn| result.
  62. */
  63. static int parse_bigBN(BIGNUM **out, const char *bn_strings[])
  64. {
  65. char *bigstring = glue_strings(bn_strings, NULL);
  66. int ret = BN_hex2bn(out, bigstring);
  67. OPENSSL_free(bigstring);
  68. return ret;
  69. }
  70. /*
  71. * Parse BIGNUM, return number of bytes parsed.
  72. */
  73. static int parseBN(BIGNUM **out, const char *in)
  74. {
  75. *out = NULL;
  76. return BN_hex2bn(out, in);
  77. }
  78. static int parsedecBN(BIGNUM **out, const char *in)
  79. {
  80. *out = NULL;
  81. return BN_dec2bn(out, in);
  82. }
  83. static BIGNUM *getBN(STANZA *s, const char *attribute)
  84. {
  85. const char *hex;
  86. BIGNUM *ret = NULL;
  87. if ((hex = findattr(s, attribute)) == NULL) {
  88. TEST_error("%s:%d: Can't find %s", s->test_file, s->start, attribute);
  89. return NULL;
  90. }
  91. if (parseBN(&ret, hex) != (int)strlen(hex)) {
  92. TEST_error("Could not decode '%s'", hex);
  93. return NULL;
  94. }
  95. return ret;
  96. }
  97. static int getint(STANZA *s, int *out, const char *attribute)
  98. {
  99. BIGNUM *ret;
  100. BN_ULONG word;
  101. int st = 0;
  102. if (!TEST_ptr(ret = getBN(s, attribute))
  103. || !TEST_ulong_le(word = BN_get_word(ret), INT_MAX))
  104. goto err;
  105. *out = (int)word;
  106. st = 1;
  107. err:
  108. BN_free(ret);
  109. return st;
  110. }
  111. static int equalBN(const char *op, const BIGNUM *expected, const BIGNUM *actual)
  112. {
  113. if (BN_cmp(expected, actual) == 0)
  114. return 1;
  115. TEST_error("unexpected %s value", op);
  116. TEST_BN_eq(expected, actual);
  117. return 0;
  118. }
  119. /*
  120. * Return a "random" flag for if a BN should be negated.
  121. */
  122. static int rand_neg(void)
  123. {
  124. static unsigned int neg = 0;
  125. static int sign[8] = { 0, 0, 0, 1, 1, 0, 1, 1 };
  126. return sign[(neg++) % 8];
  127. }
  128. static int test_swap(void)
  129. {
  130. BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
  131. int top, cond, st = 0;
  132. if (!TEST_ptr(a = BN_new())
  133. || !TEST_ptr(b = BN_new())
  134. || !TEST_ptr(c = BN_new())
  135. || !TEST_ptr(d = BN_new()))
  136. goto err;
  137. if (!(TEST_true(BN_bntest_rand(a, 1024, 1, 0))
  138. && TEST_true(BN_bntest_rand(b, 1024, 1, 0))
  139. && TEST_ptr(BN_copy(c, a))
  140. && TEST_ptr(BN_copy(d, b))))
  141. goto err;
  142. top = BN_num_bits(a) / BN_BITS2;
  143. /* regular swap */
  144. BN_swap(a, b);
  145. if (!equalBN("swap", a, d)
  146. || !equalBN("swap", b, c))
  147. goto err;
  148. /* conditional swap: true */
  149. cond = 1;
  150. BN_consttime_swap(cond, a, b, top);
  151. if (!equalBN("cswap true", a, c)
  152. || !equalBN("cswap true", b, d))
  153. goto err;
  154. /* conditional swap: false */
  155. cond = 0;
  156. BN_consttime_swap(cond, a, b, top);
  157. if (!equalBN("cswap false", a, c)
  158. || !equalBN("cswap false", b, d))
  159. goto err;
  160. /* same tests but checking flag swap */
  161. BN_set_flags(a, BN_FLG_CONSTTIME);
  162. BN_swap(a, b);
  163. if (!equalBN("swap, flags", a, d)
  164. || !equalBN("swap, flags", b, c)
  165. || !TEST_true(BN_get_flags(b, BN_FLG_CONSTTIME))
  166. || !TEST_false(BN_get_flags(a, BN_FLG_CONSTTIME)))
  167. goto err;
  168. cond = 1;
  169. BN_consttime_swap(cond, a, b, top);
  170. if (!equalBN("cswap true, flags", a, c)
  171. || !equalBN("cswap true, flags", b, d)
  172. || !TEST_true(BN_get_flags(a, BN_FLG_CONSTTIME))
  173. || !TEST_false(BN_get_flags(b, BN_FLG_CONSTTIME)))
  174. goto err;
  175. cond = 0;
  176. BN_consttime_swap(cond, a, b, top);
  177. if (!equalBN("cswap false, flags", a, c)
  178. || !equalBN("cswap false, flags", b, d)
  179. || !TEST_true(BN_get_flags(a, BN_FLG_CONSTTIME))
  180. || !TEST_false(BN_get_flags(b, BN_FLG_CONSTTIME)))
  181. goto err;
  182. st = 1;
  183. err:
  184. BN_free(a);
  185. BN_free(b);
  186. BN_free(c);
  187. BN_free(d);
  188. return st;
  189. }
  190. static int test_sub(void)
  191. {
  192. BIGNUM *a = NULL, *b = NULL, *c = NULL;
  193. int i, st = 0;
  194. if (!TEST_ptr(a = BN_new())
  195. || !TEST_ptr(b = BN_new())
  196. || !TEST_ptr(c = BN_new()))
  197. goto err;
  198. for (i = 0; i < NUM0 + NUM1; i++) {
  199. if (i < NUM1) {
  200. if (!(TEST_true(BN_bntest_rand(a, 512, 0, 0)))
  201. && TEST_ptr(BN_copy(b, a))
  202. && TEST_int_ne(BN_set_bit(a, i), 0)
  203. && TEST_true(BN_add_word(b, i)))
  204. goto err;
  205. } else {
  206. if (!TEST_true(BN_bntest_rand(b, 400 + i - NUM1, 0, 0)))
  207. goto err;
  208. BN_set_negative(a, rand_neg());
  209. BN_set_negative(b, rand_neg());
  210. }
  211. if (!(TEST_true(BN_sub(c, a, b))
  212. && TEST_true(BN_add(c, c, b))
  213. && TEST_true(BN_sub(c, c, a))
  214. && TEST_BN_eq_zero(c)))
  215. goto err;
  216. }
  217. st = 1;
  218. err:
  219. BN_free(a);
  220. BN_free(b);
  221. BN_free(c);
  222. return st;
  223. }
  224. static int test_div_recip(void)
  225. {
  226. BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
  227. BN_RECP_CTX *recp = NULL;
  228. int st = 0, i;
  229. if (!TEST_ptr(a = BN_new())
  230. || !TEST_ptr(b = BN_new())
  231. || !TEST_ptr(c = BN_new())
  232. || !TEST_ptr(d = BN_new())
  233. || !TEST_ptr(e = BN_new())
  234. || !TEST_ptr(recp = BN_RECP_CTX_new()))
  235. goto err;
  236. for (i = 0; i < NUM0 + NUM1; i++) {
  237. if (i < NUM1) {
  238. if (!(TEST_true(BN_bntest_rand(a, 400, 0, 0))
  239. && TEST_ptr(BN_copy(b, a))
  240. && TEST_true(BN_lshift(a, a, i))
  241. && TEST_true(BN_add_word(a, i))))
  242. goto err;
  243. } else {
  244. if (!(TEST_true(BN_bntest_rand(b, 50 + 3 * (i - NUM1), 0, 0))))
  245. goto err;
  246. }
  247. BN_set_negative(a, rand_neg());
  248. BN_set_negative(b, rand_neg());
  249. if (!(TEST_true(BN_RECP_CTX_set(recp, b, ctx))
  250. && TEST_true(BN_div_recp(d, c, a, recp, ctx))
  251. && TEST_true(BN_mul(e, d, b, ctx))
  252. && TEST_true(BN_add(d, e, c))
  253. && TEST_true(BN_sub(d, d, a))
  254. && TEST_BN_eq_zero(d)))
  255. goto err;
  256. }
  257. st = 1;
  258. err:
  259. BN_free(a);
  260. BN_free(b);
  261. BN_free(c);
  262. BN_free(d);
  263. BN_free(e);
  264. BN_RECP_CTX_free(recp);
  265. return st;
  266. }
  267. static int test_mod(void)
  268. {
  269. BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
  270. int st = 0, i;
  271. if (!TEST_ptr(a = BN_new())
  272. || !TEST_ptr(b = BN_new())
  273. || !TEST_ptr(c = BN_new())
  274. || !TEST_ptr(d = BN_new())
  275. || !TEST_ptr(e = BN_new()))
  276. goto err;
  277. if (!(TEST_true(BN_bntest_rand(a, 1024, 0, 0))))
  278. goto err;
  279. for (i = 0; i < NUM0; i++) {
  280. if (!(TEST_true(BN_bntest_rand(b, 450 + i * 10, 0, 0))))
  281. goto err;
  282. BN_set_negative(a, rand_neg());
  283. BN_set_negative(b, rand_neg());
  284. if (!(TEST_true(BN_mod(c, a, b, ctx))
  285. && TEST_true(BN_div(d, e, a, b, ctx))
  286. && TEST_true(BN_sub(e, e, c))
  287. && TEST_BN_eq_zero(e)))
  288. goto err;
  289. }
  290. st = 1;
  291. err:
  292. BN_free(a);
  293. BN_free(b);
  294. BN_free(c);
  295. BN_free(d);
  296. BN_free(e);
  297. return st;
  298. }
  299. static const char *bn1strings[] = {
  300. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  301. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  302. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  303. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  304. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  305. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  306. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  307. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF00",
  308. "0000000000000000000000000000000000000000000000000000000000000000",
  309. "0000000000000000000000000000000000000000000000000000000000000000",
  310. "0000000000000000000000000000000000000000000000000000000000000000",
  311. "0000000000000000000000000000000000000000000000000000000000000000",
  312. "0000000000000000000000000000000000000000000000000000000000000000",
  313. "0000000000000000000000000000000000000000000000000000000000000000",
  314. "0000000000000000000000000000000000000000000000000000000000000000",
  315. "00000000000000000000000000000000000000000000000000FFFFFFFFFFFFFF",
  316. NULL
  317. };
  318. static const char *bn2strings[] = {
  319. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  320. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  321. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  322. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  323. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  324. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  325. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  326. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF0000000000",
  327. "0000000000000000000000000000000000000000000000000000000000000000",
  328. "0000000000000000000000000000000000000000000000000000000000000000",
  329. "0000000000000000000000000000000000000000000000000000000000000000",
  330. "0000000000000000000000000000000000000000000000000000000000000000",
  331. "0000000000000000000000000000000000000000000000000000000000000000",
  332. "0000000000000000000000000000000000000000000000000000000000000000",
  333. "0000000000000000000000000000000000000000000000000000000000000000",
  334. "000000000000000000000000000000000000000000FFFFFFFFFFFFFF00000000",
  335. NULL
  336. };
  337. /*
  338. * Test constant-time modular exponentiation with 1024-bit inputs, which on
  339. * x86_64 cause a different code branch to be taken.
  340. */
  341. static int test_modexp_mont5(void)
  342. {
  343. BIGNUM *a = NULL, *p = NULL, *m = NULL, *d = NULL, *e = NULL;
  344. BIGNUM *b = NULL, *n = NULL, *c = NULL;
  345. BN_MONT_CTX *mont = NULL;
  346. int st = 0;
  347. if (!TEST_ptr(a = BN_new())
  348. || !TEST_ptr(p = BN_new())
  349. || !TEST_ptr(m = BN_new())
  350. || !TEST_ptr(d = BN_new())
  351. || !TEST_ptr(e = BN_new())
  352. || !TEST_ptr(b = BN_new())
  353. || !TEST_ptr(n = BN_new())
  354. || !TEST_ptr(c = BN_new())
  355. || !TEST_ptr(mont = BN_MONT_CTX_new()))
  356. goto err;
  357. /* must be odd for montgomery */
  358. if (!(TEST_true(BN_bntest_rand(m, 1024, 0, 1))
  359. /* Zero exponent */
  360. && TEST_true(BN_bntest_rand(a, 1024, 0, 0))))
  361. goto err;
  362. BN_zero(p);
  363. if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL)))
  364. goto err;
  365. if (!TEST_BN_eq_one(d))
  366. goto err;
  367. /* Regression test for carry bug in mulx4x_mont */
  368. if (!(TEST_true(BN_hex2bn(&a,
  369. "7878787878787878787878787878787878787878787878787878787878787878"
  370. "7878787878787878787878787878787878787878787878787878787878787878"
  371. "7878787878787878787878787878787878787878787878787878787878787878"
  372. "7878787878787878787878787878787878787878787878787878787878787878"))
  373. && TEST_true(BN_hex2bn(&b,
  374. "095D72C08C097BA488C5E439C655A192EAFB6380073D8C2664668EDDB4060744"
  375. "E16E57FB4EDB9AE10A0CEFCDC28A894F689A128379DB279D48A2E20849D68593"
  376. "9B7803BCF46CEBF5C533FB0DD35B080593DE5472E3FE5DB951B8BFF9B4CB8F03"
  377. "9CC638A5EE8CDD703719F8000E6A9F63BEED5F2FCD52FF293EA05A251BB4AB81"))
  378. && TEST_true(BN_hex2bn(&n,
  379. "D78AF684E71DB0C39CFF4E64FB9DB567132CB9C50CC98009FEB820B26F2DED9B"
  380. "91B9B5E2B83AE0AE4EB4E0523CA726BFBE969B89FD754F674CE99118C3F2D1C5"
  381. "D81FDC7C54E02B60262B241D53C040E99E45826ECA37A804668E690E1AFC1CA4"
  382. "2C9A15D84D4954425F0B7642FC0BD9D7B24E2618D2DCC9B729D944BADACFDDAF"))))
  383. goto err;
  384. if (!(TEST_true(BN_MONT_CTX_set(mont, n, ctx))
  385. && TEST_true(BN_mod_mul_montgomery(c, a, b, mont, ctx))
  386. && TEST_true(BN_mod_mul_montgomery(d, b, a, mont, ctx))
  387. && TEST_BN_eq(c, d)))
  388. goto err;
  389. /* Regression test for carry bug in sqr[x]8x_mont */
  390. if (!(TEST_true(parse_bigBN(&n, bn1strings))
  391. && TEST_true(parse_bigBN(&a, bn2strings))))
  392. goto err;
  393. BN_free(b);
  394. if (!(TEST_ptr(b = BN_dup(a))
  395. && TEST_true(BN_MONT_CTX_set(mont, n, ctx))
  396. && TEST_true(BN_mod_mul_montgomery(c, a, a, mont, ctx))
  397. && TEST_true(BN_mod_mul_montgomery(d, a, b, mont, ctx))
  398. && TEST_BN_eq(c, d)))
  399. goto err;
  400. /* Regression test for carry bug in bn_sqrx8x_internal */
  401. {
  402. static const char *ahex[] = {
  403. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  404. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  405. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  406. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  407. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8FFEADBCFC4DAE7FFF908E92820306B",
  408. "9544D954000000006C0000000000000000000000000000000000000000000000",
  409. "00000000000000000000FF030202FFFFF8FFEBDBCFC4DAE7FFF908E92820306B",
  410. "9544D954000000006C000000FF0302030000000000FFFFFFFFFFFFFFFFFFFFFF",
  411. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF01FC00FF02FFFFFFFF",
  412. "00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00FCFD",
  413. "FCFFFFFFFFFF000000000000000000FF0302030000000000FFFFFFFFFFFFFFFF",
  414. "FF00FCFDFDFF030202FF00000000FFFFFFFFFFFFFFFFFF00FCFDFCFFFFFFFFFF",
  415. NULL
  416. };
  417. static const char *nhex[] = {
  418. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  419. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  420. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  421. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  422. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8F8F8F8000000",
  423. "00000010000000006C0000000000000000000000000000000000000000000000",
  424. "00000000000000000000000000000000000000FFFFFFFFFFFFF8F8F8F8000000",
  425. "00000010000000006C000000000000000000000000FFFFFFFFFFFFFFFFFFFFFF",
  426. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  427. "00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  428. "FFFFFFFFFFFF000000000000000000000000000000000000FFFFFFFFFFFFFFFF",
  429. "FFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  430. NULL
  431. };
  432. if (!(TEST_true(parse_bigBN(&a, ahex))
  433. && TEST_true(parse_bigBN(&n, nhex))))
  434. goto err;
  435. }
  436. BN_free(b);
  437. if (!(TEST_ptr(b = BN_dup(a))
  438. && TEST_true(BN_MONT_CTX_set(mont, n, ctx))))
  439. goto err;
  440. if (!TEST_true(BN_mod_mul_montgomery(c, a, a, mont, ctx))
  441. || !TEST_true(BN_mod_mul_montgomery(d, a, b, mont, ctx))
  442. || !TEST_BN_eq(c, d))
  443. goto err;
  444. /* Regression test for bug in BN_from_montgomery_word */
  445. if (!(TEST_true(BN_hex2bn(&a,
  446. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  447. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  448. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"))
  449. && TEST_true(BN_hex2bn(&n,
  450. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  451. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"))
  452. && TEST_true(BN_MONT_CTX_set(mont, n, ctx))
  453. && TEST_false(BN_mod_mul_montgomery(d, a, a, mont, ctx))))
  454. goto err;
  455. /* Regression test for bug in rsaz_1024_mul_avx2 */
  456. if (!(TEST_true(BN_hex2bn(&a,
  457. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  458. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  459. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  460. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020DF"))
  461. && TEST_true(BN_hex2bn(&b,
  462. "2020202020202020202020202020202020202020202020202020202020202020"
  463. "2020202020202020202020202020202020202020202020202020202020202020"
  464. "20202020202020FF202020202020202020202020202020202020202020202020"
  465. "2020202020202020202020202020202020202020202020202020202020202020"))
  466. && TEST_true(BN_hex2bn(&n,
  467. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  468. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  469. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  470. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020FF"))
  471. && TEST_true(BN_MONT_CTX_set(mont, n, ctx))
  472. && TEST_true(BN_mod_exp_mont_consttime(c, a, b, n, ctx, mont))
  473. && TEST_true(BN_mod_exp_mont(d, a, b, n, ctx, mont))
  474. && TEST_BN_eq(c, d)))
  475. goto err;
  476. /*
  477. * rsaz_1024_mul_avx2 expects fully-reduced inputs.
  478. * BN_mod_exp_mont_consttime should reduce the input first.
  479. */
  480. if (!(TEST_true(BN_hex2bn(&a,
  481. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  482. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  483. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  484. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020DF"))
  485. && TEST_true(BN_hex2bn(&b,
  486. "1FA53F26F8811C58BE0357897AA5E165693230BC9DF5F01DFA6A2D59229EC69D"
  487. "9DE6A89C36E3B6957B22D6FAAD5A3C73AE587B710DBE92E83D3A9A3339A085CB"
  488. "B58F508CA4F837924BB52CC1698B7FDC2FD74362456A595A5B58E38E38E38E38"
  489. "E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E"))
  490. && TEST_true(BN_hex2bn(&n,
  491. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  492. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  493. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  494. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020DF"))
  495. && TEST_true(BN_MONT_CTX_set(mont, n, ctx))
  496. && TEST_true(BN_mod_exp_mont_consttime(c, a, b, n, ctx, mont))))
  497. goto err;
  498. BN_zero(d);
  499. if (!TEST_BN_eq(c, d))
  500. goto err;
  501. /* Zero input */
  502. if (!TEST_true(BN_bntest_rand(p, 1024, 0, 0)))
  503. goto err;
  504. BN_zero(a);
  505. if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
  506. || !TEST_BN_eq_zero(d))
  507. goto err;
  508. /*
  509. * Craft an input whose Montgomery representation is 1, i.e., shorter
  510. * than the modulus m, in order to test the const time precomputation
  511. * scattering/gathering.
  512. */
  513. if (!(TEST_true(BN_one(a))
  514. && TEST_true(BN_MONT_CTX_set(mont, m, ctx))))
  515. goto err;
  516. if (!TEST_true(BN_from_montgomery(e, a, mont, ctx))
  517. || !TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
  518. || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
  519. || !TEST_BN_eq(a, d))
  520. goto err;
  521. /* Finally, some regular test vectors. */
  522. if (!(TEST_true(BN_bntest_rand(e, 1024, 0, 0))
  523. && TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
  524. && TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
  525. && TEST_BN_eq(a, d)))
  526. goto err;
  527. st = 1;
  528. err:
  529. BN_MONT_CTX_free(mont);
  530. BN_free(a);
  531. BN_free(p);
  532. BN_free(m);
  533. BN_free(d);
  534. BN_free(e);
  535. BN_free(b);
  536. BN_free(n);
  537. BN_free(c);
  538. return st;
  539. }
  540. #ifndef OPENSSL_NO_EC2M
  541. static int test_gf2m_add(void)
  542. {
  543. BIGNUM *a = NULL, *b = NULL, *c = NULL;
  544. int i, st = 0;
  545. if (!TEST_ptr(a = BN_new())
  546. || !TEST_ptr(b = BN_new())
  547. || !TEST_ptr(c = BN_new()))
  548. goto err;
  549. for (i = 0; i < NUM0; i++) {
  550. if (!(TEST_true(BN_rand(a, 512, 0, 0))
  551. && TEST_ptr(BN_copy(b, BN_value_one()))))
  552. goto err;
  553. BN_set_negative(a, rand_neg());
  554. BN_set_negative(b, rand_neg());
  555. if (!(TEST_true(BN_GF2m_add(c, a, b))
  556. /* Test that two added values have the correct parity. */
  557. && TEST_false((BN_is_odd(a) && BN_is_odd(c))
  558. || (!BN_is_odd(a) && !BN_is_odd(c)))))
  559. goto err;
  560. if (!(TEST_true(BN_GF2m_add(c, c, c))
  561. /* Test that c + c = 0. */
  562. && TEST_BN_eq_zero(c)))
  563. goto err;
  564. }
  565. st = 1;
  566. err:
  567. BN_free(a);
  568. BN_free(b);
  569. BN_free(c);
  570. return st;
  571. }
  572. static int test_gf2m_mod(void)
  573. {
  574. BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL, *e = NULL;
  575. int i, j, st = 0;
  576. if (!TEST_ptr(a = BN_new())
  577. || !TEST_ptr(b[0] = BN_new())
  578. || !TEST_ptr(b[1] = BN_new())
  579. || !TEST_ptr(c = BN_new())
  580. || !TEST_ptr(d = BN_new())
  581. || !TEST_ptr(e = BN_new()))
  582. goto err;
  583. if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
  584. && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
  585. goto err;
  586. for (i = 0; i < NUM0; i++) {
  587. if (!TEST_true(BN_bntest_rand(a, 1024, 0, 0)))
  588. goto err;
  589. for (j = 0; j < 2; j++) {
  590. if (!(TEST_true(BN_GF2m_mod(c, a, b[j]))
  591. && TEST_true(BN_GF2m_add(d, a, c))
  592. && TEST_true(BN_GF2m_mod(e, d, b[j]))
  593. /* Test that a + (a mod p) mod p == 0. */
  594. && TEST_BN_eq_zero(e)))
  595. goto err;
  596. }
  597. }
  598. st = 1;
  599. err:
  600. BN_free(a);
  601. BN_free(b[0]);
  602. BN_free(b[1]);
  603. BN_free(c);
  604. BN_free(d);
  605. BN_free(e);
  606. return st;
  607. }
  608. static int test_gf2m_mul(void)
  609. {
  610. BIGNUM *a, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
  611. BIGNUM *e = NULL, *f = NULL, *g = NULL, *h = NULL;
  612. int i, j, st = 0;
  613. if (!TEST_ptr(a = BN_new())
  614. || !TEST_ptr(b[0] = BN_new())
  615. || !TEST_ptr(b[1] = BN_new())
  616. || !TEST_ptr(c = BN_new())
  617. || !TEST_ptr(d = BN_new())
  618. || !TEST_ptr(e = BN_new())
  619. || !TEST_ptr(f = BN_new())
  620. || !TEST_ptr(g = BN_new())
  621. || !TEST_ptr(h = BN_new()))
  622. goto err;
  623. if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
  624. && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
  625. goto err;
  626. for (i = 0; i < NUM0; i++) {
  627. if (!(TEST_true(BN_bntest_rand(a, 1024, 0, 0))
  628. && TEST_true(BN_bntest_rand(c, 1024, 0, 0))
  629. && TEST_true(BN_bntest_rand(d, 1024, 0, 0))))
  630. goto err;
  631. for (j = 0; j < 2; j++) {
  632. if (!(TEST_true(BN_GF2m_mod_mul(e, a, c, b[j], ctx))
  633. && TEST_true(BN_GF2m_add(f, a, d))
  634. && TEST_true(BN_GF2m_mod_mul(g, f, c, b[j], ctx))
  635. && TEST_true(BN_GF2m_mod_mul(h, d, c, b[j], ctx))
  636. && TEST_true(BN_GF2m_add(f, e, g))
  637. && TEST_true(BN_GF2m_add(f, f, h))
  638. /* Test that (a+d)*c = a*c + d*c. */
  639. && TEST_BN_eq_zero(f)))
  640. goto err;
  641. }
  642. }
  643. st = 1;
  644. err:
  645. BN_free(a);
  646. BN_free(b[0]);
  647. BN_free(b[1]);
  648. BN_free(c);
  649. BN_free(d);
  650. BN_free(e);
  651. BN_free(f);
  652. BN_free(g);
  653. BN_free(h);
  654. return st;
  655. }
  656. static int test_gf2m_sqr(void)
  657. {
  658. BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
  659. int i, j, st = 0;
  660. if (!TEST_ptr(a = BN_new())
  661. || !TEST_ptr(b[0] = BN_new())
  662. || !TEST_ptr(b[1] = BN_new())
  663. || !TEST_ptr(c = BN_new())
  664. || !TEST_ptr(d = BN_new()))
  665. goto err;
  666. if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
  667. && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
  668. goto err;
  669. for (i = 0; i < NUM0; i++) {
  670. if (!TEST_true(BN_bntest_rand(a, 1024, 0, 0)))
  671. goto err;
  672. for (j = 0; j < 2; j++) {
  673. if (!(TEST_true(BN_GF2m_mod_sqr(c, a, b[j], ctx))
  674. && TEST_true(BN_copy(d, a))
  675. && TEST_true(BN_GF2m_mod_mul(d, a, d, b[j], ctx))
  676. && TEST_true(BN_GF2m_add(d, c, d))
  677. /* Test that a*a = a^2. */
  678. && TEST_BN_eq_zero(d)))
  679. goto err;
  680. }
  681. }
  682. st = 1;
  683. err:
  684. BN_free(a);
  685. BN_free(b[0]);
  686. BN_free(b[1]);
  687. BN_free(c);
  688. BN_free(d);
  689. return st;
  690. }
  691. static int test_gf2m_modinv(void)
  692. {
  693. BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
  694. int i, j, st = 0;
  695. if (!TEST_ptr(a = BN_new())
  696. || !TEST_ptr(b[0] = BN_new())
  697. || !TEST_ptr(b[1] = BN_new())
  698. || !TEST_ptr(c = BN_new())
  699. || !TEST_ptr(d = BN_new()))
  700. goto err;
  701. if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
  702. && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
  703. goto err;
  704. for (i = 0; i < NUM0; i++) {
  705. if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
  706. goto err;
  707. for (j = 0; j < 2; j++) {
  708. if (!(TEST_true(BN_GF2m_mod_inv(c, a, b[j], ctx))
  709. && TEST_true(BN_GF2m_mod_mul(d, a, c, b[j], ctx))
  710. /* Test that ((1/a)*a) = 1. */
  711. && TEST_BN_eq_one(d)))
  712. goto err;
  713. }
  714. }
  715. st = 1;
  716. err:
  717. BN_free(a);
  718. BN_free(b[0]);
  719. BN_free(b[1]);
  720. BN_free(c);
  721. BN_free(d);
  722. return st;
  723. }
  724. static int test_gf2m_moddiv(void)
  725. {
  726. BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
  727. BIGNUM *e = NULL, *f = NULL;
  728. int i, j, st = 0;
  729. if (!TEST_ptr(a = BN_new())
  730. || !TEST_ptr(b[0] = BN_new())
  731. || !TEST_ptr(b[1] = BN_new())
  732. || !TEST_ptr(c = BN_new())
  733. || !TEST_ptr(d = BN_new())
  734. || !TEST_ptr(e = BN_new())
  735. || !TEST_ptr(f = BN_new()))
  736. goto err;
  737. if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
  738. && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
  739. goto err;
  740. for (i = 0; i < NUM0; i++) {
  741. if (!(TEST_true(BN_bntest_rand(a, 512, 0, 0))
  742. && TEST_true(BN_bntest_rand(c, 512, 0, 0))))
  743. goto err;
  744. for (j = 0; j < 2; j++) {
  745. if (!(TEST_true(BN_GF2m_mod_div(d, a, c, b[j], ctx))
  746. && TEST_true(BN_GF2m_mod_mul(e, d, c, b[j], ctx))
  747. && TEST_true(BN_GF2m_mod_div(f, a, e, b[j], ctx))
  748. /* Test that ((a/c)*c)/a = 1. */
  749. && TEST_BN_eq_one(f)))
  750. goto err;
  751. }
  752. }
  753. st = 1;
  754. err:
  755. BN_free(a);
  756. BN_free(b[0]);
  757. BN_free(b[1]);
  758. BN_free(c);
  759. BN_free(d);
  760. BN_free(e);
  761. BN_free(f);
  762. return st;
  763. }
  764. static int test_gf2m_modexp(void)
  765. {
  766. BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
  767. BIGNUM *e = NULL, *f = NULL;
  768. int i, j, st = 0;
  769. if (!TEST_ptr(a = BN_new())
  770. || !TEST_ptr(b[0] = BN_new())
  771. || !TEST_ptr(b[1] = BN_new())
  772. || !TEST_ptr(c = BN_new())
  773. || !TEST_ptr(d = BN_new())
  774. || !TEST_ptr(e = BN_new())
  775. || !TEST_ptr(f = BN_new()))
  776. goto err;
  777. if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
  778. && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
  779. goto err;
  780. for (i = 0; i < NUM0; i++) {
  781. if (!(TEST_true(BN_bntest_rand(a, 512, 0, 0))
  782. && TEST_true(BN_bntest_rand(c, 512, 0, 0))
  783. && TEST_true(BN_bntest_rand(d, 512, 0, 0))))
  784. goto err;
  785. for (j = 0; j < 2; j++) {
  786. if (!(TEST_true(BN_GF2m_mod_exp(e, a, c, b[j], ctx))
  787. && TEST_true(BN_GF2m_mod_exp(f, a, d, b[j], ctx))
  788. && TEST_true(BN_GF2m_mod_mul(e, e, f, b[j], ctx))
  789. && TEST_true(BN_add(f, c, d))
  790. && TEST_true(BN_GF2m_mod_exp(f, a, f, b[j], ctx))
  791. && TEST_true(BN_GF2m_add(f, e, f))
  792. /* Test that a^(c+d)=a^c*a^d. */
  793. && TEST_BN_eq_zero(f)))
  794. goto err;
  795. }
  796. }
  797. st = 1;
  798. err:
  799. BN_free(a);
  800. BN_free(b[0]);
  801. BN_free(b[1]);
  802. BN_free(c);
  803. BN_free(d);
  804. BN_free(e);
  805. BN_free(f);
  806. return st;
  807. }
  808. static int test_gf2m_modsqrt(void)
  809. {
  810. BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
  811. BIGNUM *e = NULL, *f = NULL;
  812. int i, j, st = 0;
  813. if (!TEST_ptr(a = BN_new())
  814. || !TEST_ptr(b[0] = BN_new())
  815. || !TEST_ptr(b[1] = BN_new())
  816. || !TEST_ptr(c = BN_new())
  817. || !TEST_ptr(d = BN_new())
  818. || !TEST_ptr(e = BN_new())
  819. || !TEST_ptr(f = BN_new()))
  820. goto err;
  821. if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
  822. && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
  823. goto err;
  824. for (i = 0; i < NUM0; i++) {
  825. if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
  826. goto err;
  827. for (j = 0; j < 2; j++) {
  828. if (!(TEST_true(BN_GF2m_mod(c, a, b[j]))
  829. && TEST_true(BN_GF2m_mod_sqrt(d, a, b[j], ctx))
  830. && TEST_true(BN_GF2m_mod_sqr(e, d, b[j], ctx))
  831. && TEST_true(BN_GF2m_add(f, c, e))
  832. /* Test that d^2 = a, where d = sqrt(a). */
  833. && TEST_BN_eq_zero(f)))
  834. goto err;
  835. }
  836. }
  837. st = 1;
  838. err:
  839. BN_free(a);
  840. BN_free(b[0]);
  841. BN_free(b[1]);
  842. BN_free(c);
  843. BN_free(d);
  844. BN_free(e);
  845. BN_free(f);
  846. return st;
  847. }
  848. static int test_gf2m_modsolvequad(void)
  849. {
  850. BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
  851. BIGNUM *e = NULL;
  852. int i, j, s = 0, t, st = 0;
  853. if (!TEST_ptr(a = BN_new())
  854. || !TEST_ptr(b[0] = BN_new())
  855. || !TEST_ptr(b[1] = BN_new())
  856. || !TEST_ptr(c = BN_new())
  857. || !TEST_ptr(d = BN_new())
  858. || !TEST_ptr(e = BN_new()))
  859. goto err;
  860. if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
  861. && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
  862. goto err;
  863. for (i = 0; i < NUM0; i++) {
  864. if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
  865. goto err;
  866. for (j = 0; j < 2; j++) {
  867. t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx);
  868. if (t) {
  869. s++;
  870. if (!(TEST_true(BN_GF2m_mod_sqr(d, c, b[j], ctx))
  871. && TEST_true(BN_GF2m_add(d, c, d))
  872. && TEST_true(BN_GF2m_mod(e, a, b[j]))
  873. && TEST_true(BN_GF2m_add(e, e, d))
  874. /*
  875. * Test that solution of quadratic c
  876. * satisfies c^2 + c = a.
  877. */
  878. && TEST_BN_eq_zero(e)))
  879. goto err;
  880. }
  881. }
  882. }
  883. if (!TEST_int_ge(s, 0)) {
  884. TEST_info("%d tests found no roots; probably an error", NUM0);
  885. goto err;
  886. }
  887. st = 1;
  888. err:
  889. BN_free(a);
  890. BN_free(b[0]);
  891. BN_free(b[1]);
  892. BN_free(c);
  893. BN_free(d);
  894. BN_free(e);
  895. return st;
  896. }
  897. #endif
  898. static int test_kronecker(void)
  899. {
  900. BIGNUM *a = NULL, *b = NULL, *r = NULL, *t = NULL;
  901. int i, legendre, kronecker, st = 0;
  902. if (!TEST_ptr(a = BN_new())
  903. || !TEST_ptr(b = BN_new())
  904. || !TEST_ptr(r = BN_new())
  905. || !TEST_ptr(t = BN_new()))
  906. goto err;
  907. /*
  908. * We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol). In
  909. * this case we know that if b is prime, then BN_kronecker(a, b, ctx) is
  910. * congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol). So we
  911. * generate a random prime b and compare these values for a number of
  912. * random a's. (That is, we run the Solovay-Strassen primality test to
  913. * confirm that b is prime, except that we don't want to test whether b
  914. * is prime but whether BN_kronecker works.)
  915. */
  916. if (!TEST_true(BN_generate_prime_ex(b, 512, 0, NULL, NULL, NULL)))
  917. goto err;
  918. BN_set_negative(b, rand_neg());
  919. for (i = 0; i < NUM0; i++) {
  920. if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
  921. goto err;
  922. BN_set_negative(a, rand_neg());
  923. /* t := (|b|-1)/2 (note that b is odd) */
  924. if (!TEST_true(BN_copy(t, b)))
  925. goto err;
  926. BN_set_negative(t, 0);
  927. if (!TEST_true(BN_sub_word(t, 1)))
  928. goto err;
  929. if (!TEST_true(BN_rshift1(t, t)))
  930. goto err;
  931. /* r := a^t mod b */
  932. BN_set_negative(b, 0);
  933. if (!TEST_true(BN_mod_exp_recp(r, a, t, b, ctx)))
  934. goto err;
  935. BN_set_negative(b, 1);
  936. if (BN_is_word(r, 1))
  937. legendre = 1;
  938. else if (BN_is_zero(r))
  939. legendre = 0;
  940. else {
  941. if (!TEST_true(BN_add_word(r, 1)))
  942. goto err;
  943. if (!TEST_int_eq(BN_ucmp(r, b), 0)) {
  944. TEST_info("Legendre symbol computation failed");
  945. goto err;
  946. }
  947. legendre = -1;
  948. }
  949. if (!TEST_int_ge(kronecker = BN_kronecker(a, b, ctx), -1))
  950. goto err;
  951. /* we actually need BN_kronecker(a, |b|) */
  952. if (BN_is_negative(a) && BN_is_negative(b))
  953. kronecker = -kronecker;
  954. if (!TEST_int_eq(legendre, kronecker))
  955. goto err;
  956. }
  957. st = 1;
  958. err:
  959. BN_free(a);
  960. BN_free(b);
  961. BN_free(r);
  962. BN_free(t);
  963. return st;
  964. }
  965. static int file_sum(STANZA *s)
  966. {
  967. BIGNUM *a = NULL, *b = NULL, *sum = NULL, *ret = NULL;
  968. BN_ULONG b_word;
  969. int st = 0;
  970. if (!TEST_ptr(a = getBN(s, "A"))
  971. || !TEST_ptr(b = getBN(s, "B"))
  972. || !TEST_ptr(sum = getBN(s, "Sum"))
  973. || !TEST_ptr(ret = BN_new()))
  974. goto err;
  975. if (!TEST_true(BN_add(ret, a, b))
  976. || !equalBN("A + B", sum, ret)
  977. || !TEST_true(BN_sub(ret, sum, a))
  978. || !equalBN("Sum - A", b, ret)
  979. || !TEST_true(BN_sub(ret, sum, b))
  980. || !equalBN("Sum - B", a, ret))
  981. goto err;
  982. /*
  983. * Test that the functions work when |r| and |a| point to the same BIGNUM,
  984. * or when |r| and |b| point to the same BIGNUM.
  985. * TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM.
  986. */
  987. if (!TEST_true(BN_copy(ret, a))
  988. || !TEST_true(BN_add(ret, ret, b))
  989. || !equalBN("A + B (r is a)", sum, ret)
  990. || !TEST_true(BN_copy(ret, b))
  991. || !TEST_true(BN_add(ret, a, ret))
  992. || !equalBN("A + B (r is b)", sum, ret)
  993. || !TEST_true(BN_copy(ret, sum))
  994. || !TEST_true(BN_sub(ret, ret, a))
  995. || !equalBN("Sum - A (r is a)", b, ret)
  996. || !TEST_true(BN_copy(ret, a))
  997. || !TEST_true(BN_sub(ret, sum, ret))
  998. || !equalBN("Sum - A (r is b)", b, ret)
  999. || !TEST_true(BN_copy(ret, sum))
  1000. || !TEST_true(BN_sub(ret, ret, b))
  1001. || !equalBN("Sum - B (r is a)", a, ret)
  1002. || !TEST_true(BN_copy(ret, b))
  1003. || !TEST_true(BN_sub(ret, sum, ret))
  1004. || !equalBN("Sum - B (r is b)", a, ret))
  1005. goto err;
  1006. /*
  1007. * Test BN_uadd() and BN_usub() with the prerequisites they are
  1008. * documented as having. Note that these functions are frequently used
  1009. * when the prerequisites don't hold. In those cases, they are supposed
  1010. * to work as if the prerequisite hold, but we don't test that yet.
  1011. * TODO: test that.
  1012. */
  1013. if (!BN_is_negative(a) && !BN_is_negative(b) && BN_cmp(a, b) >= 0) {
  1014. if (!TEST_true(BN_uadd(ret, a, b))
  1015. || !equalBN("A +u B", sum, ret)
  1016. || !TEST_true(BN_usub(ret, sum, a))
  1017. || !equalBN("Sum -u A", b, ret)
  1018. || !TEST_true(BN_usub(ret, sum, b))
  1019. || !equalBN("Sum -u B", a, ret))
  1020. goto err;
  1021. /*
  1022. * Test that the functions work when |r| and |a| point to the same
  1023. * BIGNUM, or when |r| and |b| point to the same BIGNUM.
  1024. * TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM.
  1025. */
  1026. if (!TEST_true(BN_copy(ret, a))
  1027. || !TEST_true(BN_uadd(ret, ret, b))
  1028. || !equalBN("A +u B (r is a)", sum, ret)
  1029. || !TEST_true(BN_copy(ret, b))
  1030. || !TEST_true(BN_uadd(ret, a, ret))
  1031. || !equalBN("A +u B (r is b)", sum, ret)
  1032. || !TEST_true(BN_copy(ret, sum))
  1033. || !TEST_true(BN_usub(ret, ret, a))
  1034. || !equalBN("Sum -u A (r is a)", b, ret)
  1035. || !TEST_true(BN_copy(ret, a))
  1036. || !TEST_true(BN_usub(ret, sum, ret))
  1037. || !equalBN("Sum -u A (r is b)", b, ret)
  1038. || !TEST_true(BN_copy(ret, sum))
  1039. || !TEST_true(BN_usub(ret, ret, b))
  1040. || !equalBN("Sum -u B (r is a)", a, ret)
  1041. || !TEST_true(BN_copy(ret, b))
  1042. || !TEST_true(BN_usub(ret, sum, ret))
  1043. || !equalBN("Sum -u B (r is b)", a, ret))
  1044. goto err;
  1045. }
  1046. /*
  1047. * Test with BN_add_word() and BN_sub_word() if |b| is small enough.
  1048. */
  1049. b_word = BN_get_word(b);
  1050. if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
  1051. if (!TEST_true(BN_copy(ret, a))
  1052. || !TEST_true(BN_add_word(ret, b_word))
  1053. || !equalBN("A + B (word)", sum, ret)
  1054. || !TEST_true(BN_copy(ret, sum))
  1055. || !TEST_true(BN_sub_word(ret, b_word))
  1056. || !equalBN("Sum - B (word)", a, ret))
  1057. goto err;
  1058. }
  1059. st = 1;
  1060. err:
  1061. BN_free(a);
  1062. BN_free(b);
  1063. BN_free(sum);
  1064. BN_free(ret);
  1065. return st;
  1066. }
  1067. static int file_lshift1(STANZA *s)
  1068. {
  1069. BIGNUM *a = NULL, *lshift1 = NULL, *zero = NULL, *ret = NULL;
  1070. BIGNUM *two = NULL, *remainder = NULL;
  1071. int st = 0;
  1072. if (!TEST_ptr(a = getBN(s, "A"))
  1073. || !TEST_ptr(lshift1 = getBN(s, "LShift1"))
  1074. || !TEST_ptr(zero = BN_new())
  1075. || !TEST_ptr(ret = BN_new())
  1076. || !TEST_ptr(two = BN_new())
  1077. || !TEST_ptr(remainder = BN_new()))
  1078. goto err;
  1079. BN_zero(zero);
  1080. if (!TEST_true(BN_set_word(two, 2))
  1081. || !TEST_true(BN_add(ret, a, a))
  1082. || !equalBN("A + A", lshift1, ret)
  1083. || !TEST_true(BN_mul(ret, a, two, ctx))
  1084. || !equalBN("A * 2", lshift1, ret)
  1085. || !TEST_true(BN_div(ret, remainder, lshift1, two, ctx))
  1086. || !equalBN("LShift1 / 2", a, ret)
  1087. || !equalBN("LShift1 % 2", zero, remainder)
  1088. || !TEST_true(BN_lshift1(ret, a))
  1089. || !equalBN("A << 1", lshift1, ret)
  1090. || !TEST_true(BN_rshift1(ret, lshift1))
  1091. || !equalBN("LShift >> 1", a, ret)
  1092. || !TEST_true(BN_rshift1(ret, lshift1))
  1093. || !equalBN("LShift >> 1", a, ret))
  1094. goto err;
  1095. /* Set the LSB to 1 and test rshift1 again. */
  1096. if (!TEST_true(BN_set_bit(lshift1, 0))
  1097. || !TEST_true(BN_div(ret, NULL /* rem */ , lshift1, two, ctx))
  1098. || !equalBN("(LShift1 | 1) / 2", a, ret)
  1099. || !TEST_true(BN_rshift1(ret, lshift1))
  1100. || !equalBN("(LShift | 1) >> 1", a, ret))
  1101. goto err;
  1102. st = 1;
  1103. err:
  1104. BN_free(a);
  1105. BN_free(lshift1);
  1106. BN_free(zero);
  1107. BN_free(ret);
  1108. BN_free(two);
  1109. BN_free(remainder);
  1110. return st;
  1111. }
  1112. static int file_lshift(STANZA *s)
  1113. {
  1114. BIGNUM *a = NULL, *lshift = NULL, *ret = NULL;
  1115. int n = 0, st = 0;
  1116. if (!TEST_ptr(a = getBN(s, "A"))
  1117. || !TEST_ptr(lshift = getBN(s, "LShift"))
  1118. || !TEST_ptr(ret = BN_new())
  1119. || !getint(s, &n, "N"))
  1120. goto err;
  1121. if (!TEST_true(BN_lshift(ret, a, n))
  1122. || !equalBN("A << N", lshift, ret)
  1123. || !TEST_true(BN_rshift(ret, lshift, n))
  1124. || !equalBN("A >> N", a, ret))
  1125. goto err;
  1126. st = 1;
  1127. err:
  1128. BN_free(a);
  1129. BN_free(lshift);
  1130. BN_free(ret);
  1131. return st;
  1132. }
  1133. static int file_rshift(STANZA *s)
  1134. {
  1135. BIGNUM *a = NULL, *rshift = NULL, *ret = NULL;
  1136. int n = 0, st = 0;
  1137. if (!TEST_ptr(a = getBN(s, "A"))
  1138. || !TEST_ptr(rshift = getBN(s, "RShift"))
  1139. || !TEST_ptr(ret = BN_new())
  1140. || !getint(s, &n, "N"))
  1141. goto err;
  1142. if (!TEST_true(BN_rshift(ret, a, n))
  1143. || !equalBN("A >> N", rshift, ret))
  1144. goto err;
  1145. /* If N == 1, try with rshift1 as well */
  1146. if (n == 1) {
  1147. if (!TEST_true(BN_rshift1(ret, a))
  1148. || !equalBN("A >> 1 (rshift1)", rshift, ret))
  1149. goto err;
  1150. }
  1151. st = 1;
  1152. err:
  1153. BN_free(a);
  1154. BN_free(rshift);
  1155. BN_free(ret);
  1156. return st;
  1157. }
  1158. static int file_square(STANZA *s)
  1159. {
  1160. BIGNUM *a = NULL, *square = NULL, *zero = NULL, *ret = NULL;
  1161. BIGNUM *remainder = NULL, *tmp = NULL;
  1162. int st = 0;
  1163. if (!TEST_ptr(a = getBN(s, "A"))
  1164. || !TEST_ptr(square = getBN(s, "Square"))
  1165. || !TEST_ptr(zero = BN_new())
  1166. || !TEST_ptr(ret = BN_new())
  1167. || !TEST_ptr(remainder = BN_new()))
  1168. goto err;
  1169. BN_zero(zero);
  1170. if (!TEST_true(BN_sqr(ret, a, ctx))
  1171. || !equalBN("A^2", square, ret)
  1172. || !TEST_true(BN_mul(ret, a, a, ctx))
  1173. || !equalBN("A * A", square, ret)
  1174. || !TEST_true(BN_div(ret, remainder, square, a, ctx))
  1175. || !equalBN("Square / A", a, ret)
  1176. || !equalBN("Square % A", zero, remainder))
  1177. goto err;
  1178. #if HAVE_BN_SQRT
  1179. BN_set_negative(a, 0);
  1180. if (!TEST_true(BN_sqrt(ret, square, ctx))
  1181. || !equalBN("sqrt(Square)", a, ret))
  1182. goto err;
  1183. /* BN_sqrt should fail on non-squares and negative numbers. */
  1184. if (!TEST_BN_eq_zero(square)) {
  1185. if (!TEST_ptr(tmp = BN_new())
  1186. || !TEST_true(BN_copy(tmp, square)))
  1187. goto err;
  1188. BN_set_negative(tmp, 1);
  1189. if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx), 0))
  1190. goto err;
  1191. ERR_clear_error();
  1192. BN_set_negative(tmp, 0);
  1193. if (BN_add(tmp, tmp, BN_value_one()))
  1194. goto err;
  1195. if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx)))
  1196. goto err;
  1197. ERR_clear_error();
  1198. }
  1199. #endif
  1200. st = 1;
  1201. err:
  1202. BN_free(a);
  1203. BN_free(square);
  1204. BN_free(zero);
  1205. BN_free(ret);
  1206. BN_free(remainder);
  1207. BN_free(tmp);
  1208. return st;
  1209. }
  1210. static int file_product(STANZA *s)
  1211. {
  1212. BIGNUM *a = NULL, *b = NULL, *product = NULL, *ret = NULL;
  1213. BIGNUM *remainder = NULL, *zero = NULL;
  1214. int st = 0;
  1215. if (!TEST_ptr(a = getBN(s, "A"))
  1216. || !TEST_ptr(b = getBN(s, "B"))
  1217. || !TEST_ptr(product = getBN(s, "Product"))
  1218. || !TEST_ptr(ret = BN_new())
  1219. || !TEST_ptr(remainder = BN_new())
  1220. || !TEST_ptr(zero = BN_new()))
  1221. goto err;
  1222. BN_zero(zero);
  1223. if (!TEST_true(BN_mul(ret, a, b, ctx))
  1224. || !equalBN("A * B", product, ret)
  1225. || !TEST_true(BN_div(ret, remainder, product, a, ctx))
  1226. || !equalBN("Product / A", b, ret)
  1227. || !equalBN("Product % A", zero, remainder)
  1228. || !TEST_true(BN_div(ret, remainder, product, b, ctx))
  1229. || !equalBN("Product / B", a, ret)
  1230. || !equalBN("Product % B", zero, remainder))
  1231. goto err;
  1232. st = 1;
  1233. err:
  1234. BN_free(a);
  1235. BN_free(b);
  1236. BN_free(product);
  1237. BN_free(ret);
  1238. BN_free(remainder);
  1239. BN_free(zero);
  1240. return st;
  1241. }
  1242. static int file_quotient(STANZA *s)
  1243. {
  1244. BIGNUM *a = NULL, *b = NULL, *quotient = NULL, *remainder = NULL;
  1245. BIGNUM *ret = NULL, *ret2 = NULL, *nnmod = NULL;
  1246. BN_ULONG b_word, ret_word;
  1247. int st = 0;
  1248. if (!TEST_ptr(a = getBN(s, "A"))
  1249. || !TEST_ptr(b = getBN(s, "B"))
  1250. || !TEST_ptr(quotient = getBN(s, "Quotient"))
  1251. || !TEST_ptr(remainder = getBN(s, "Remainder"))
  1252. || !TEST_ptr(ret = BN_new())
  1253. || !TEST_ptr(ret2 = BN_new())
  1254. || !TEST_ptr(nnmod = BN_new()))
  1255. goto err;
  1256. if (!TEST_true(BN_div(ret, ret2, a, b, ctx))
  1257. || !equalBN("A / B", quotient, ret)
  1258. || !equalBN("A % B", remainder, ret2)
  1259. || !TEST_true(BN_mul(ret, quotient, b, ctx))
  1260. || !TEST_true(BN_add(ret, ret, remainder))
  1261. || !equalBN("Quotient * B + Remainder", a, ret))
  1262. goto err;
  1263. /*
  1264. * Test with BN_mod_word() and BN_div_word() if the divisor is
  1265. * small enough.
  1266. */
  1267. b_word = BN_get_word(b);
  1268. if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
  1269. BN_ULONG remainder_word = BN_get_word(remainder);
  1270. assert(remainder_word != (BN_ULONG)-1);
  1271. if (!TEST_ptr(BN_copy(ret, a)))
  1272. goto err;
  1273. ret_word = BN_div_word(ret, b_word);
  1274. if (ret_word != remainder_word) {
  1275. #ifdef BN_DEC_FMT1
  1276. TEST_error(
  1277. "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1,
  1278. ret_word, remainder_word);
  1279. #else
  1280. TEST_error("Got A %% B (word) mismatch");
  1281. #endif
  1282. goto err;
  1283. }
  1284. if (!equalBN ("A / B (word)", quotient, ret))
  1285. goto err;
  1286. ret_word = BN_mod_word(a, b_word);
  1287. if (ret_word != remainder_word) {
  1288. #ifdef BN_DEC_FMT1
  1289. TEST_error(
  1290. "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1 "",
  1291. ret_word, remainder_word);
  1292. #else
  1293. TEST_error("Got A %% B (word) mismatch");
  1294. #endif
  1295. goto err;
  1296. }
  1297. }
  1298. /* Test BN_nnmod. */
  1299. if (!BN_is_negative(b)) {
  1300. if (!TEST_true(BN_copy(nnmod, remainder))
  1301. || (BN_is_negative(nnmod)
  1302. && !TEST_true(BN_add(nnmod, nnmod, b)))
  1303. || !TEST_true(BN_nnmod(ret, a, b, ctx))
  1304. || !equalBN("A % B (non-negative)", nnmod, ret))
  1305. goto err;
  1306. }
  1307. st = 1;
  1308. err:
  1309. BN_free(a);
  1310. BN_free(b);
  1311. BN_free(quotient);
  1312. BN_free(remainder);
  1313. BN_free(ret);
  1314. BN_free(ret2);
  1315. BN_free(nnmod);
  1316. return st;
  1317. }
  1318. static int file_modmul(STANZA *s)
  1319. {
  1320. BIGNUM *a = NULL, *b = NULL, *m = NULL, *mod_mul = NULL, *ret = NULL;
  1321. int st = 0;
  1322. if (!TEST_ptr(a = getBN(s, "A"))
  1323. || !TEST_ptr(b = getBN(s, "B"))
  1324. || !TEST_ptr(m = getBN(s, "M"))
  1325. || !TEST_ptr(mod_mul = getBN(s, "ModMul"))
  1326. || !TEST_ptr(ret = BN_new()))
  1327. goto err;
  1328. if (!TEST_true(BN_mod_mul(ret, a, b, m, ctx))
  1329. || !equalBN("A * B (mod M)", mod_mul, ret))
  1330. goto err;
  1331. if (BN_is_odd(m)) {
  1332. /* Reduce |a| and |b| and test the Montgomery version. */
  1333. BN_MONT_CTX *mont = BN_MONT_CTX_new();
  1334. BIGNUM *a_tmp = BN_new();
  1335. BIGNUM *b_tmp = BN_new();
  1336. if (mont == NULL || a_tmp == NULL || b_tmp == NULL
  1337. || !TEST_true(BN_MONT_CTX_set(mont, m, ctx))
  1338. || !TEST_true(BN_nnmod(a_tmp, a, m, ctx))
  1339. || !TEST_true(BN_nnmod(b_tmp, b, m, ctx))
  1340. || !TEST_true(BN_to_montgomery(a_tmp, a_tmp, mont, ctx))
  1341. || !TEST_true(BN_to_montgomery(b_tmp, b_tmp, mont, ctx))
  1342. || !TEST_true(BN_mod_mul_montgomery(ret, a_tmp, b_tmp,
  1343. mont, ctx))
  1344. || !TEST_true(BN_from_montgomery(ret, ret, mont, ctx))
  1345. || !equalBN("A * B (mod M) (mont)", mod_mul, ret))
  1346. st = 0;
  1347. else
  1348. st = 1;
  1349. BN_MONT_CTX_free(mont);
  1350. BN_free(a_tmp);
  1351. BN_free(b_tmp);
  1352. if (st == 0)
  1353. goto err;
  1354. }
  1355. st = 1;
  1356. err:
  1357. BN_free(a);
  1358. BN_free(b);
  1359. BN_free(m);
  1360. BN_free(mod_mul);
  1361. BN_free(ret);
  1362. return st;
  1363. }
  1364. static int file_modexp(STANZA *s)
  1365. {
  1366. BIGNUM *a = NULL, *e = NULL, *m = NULL, *mod_exp = NULL, *ret = NULL;
  1367. BIGNUM *b = NULL, *c = NULL, *d = NULL;
  1368. int st = 0;
  1369. if (!TEST_ptr(a = getBN(s, "A"))
  1370. || !TEST_ptr(e = getBN(s, "E"))
  1371. || !TEST_ptr(m = getBN(s, "M"))
  1372. || !TEST_ptr(mod_exp = getBN(s, "ModExp"))
  1373. || !TEST_ptr(ret = BN_new())
  1374. || !TEST_ptr(d = BN_new()))
  1375. goto err;
  1376. if (!TEST_true(BN_mod_exp(ret, a, e, m, ctx))
  1377. || !equalBN("A ^ E (mod M)", mod_exp, ret))
  1378. goto err;
  1379. if (BN_is_odd(m)) {
  1380. if (!TEST_true(BN_mod_exp_mont(ret, a, e, m, ctx, NULL))
  1381. || !equalBN("A ^ E (mod M) (mont)", mod_exp, ret)
  1382. || !TEST_true(BN_mod_exp_mont_consttime(ret, a, e, m,
  1383. ctx, NULL))
  1384. || !equalBN("A ^ E (mod M) (mont const", mod_exp, ret))
  1385. goto err;
  1386. }
  1387. /* Regression test for carry propagation bug in sqr8x_reduction */
  1388. BN_hex2bn(&a, "050505050505");
  1389. BN_hex2bn(&b, "02");
  1390. BN_hex2bn(&c,
  1391. "4141414141414141414141274141414141414141414141414141414141414141"
  1392. "4141414141414141414141414141414141414141414141414141414141414141"
  1393. "4141414141414141414141800000000000000000000000000000000000000000"
  1394. "0000000000000000000000000000000000000000000000000000000000000000"
  1395. "0000000000000000000000000000000000000000000000000000000000000000"
  1396. "0000000000000000000000000000000000000000000000000000000001");
  1397. if (!TEST_true(BN_mod_exp(d, a, b, c, ctx))
  1398. || !TEST_true(BN_mul(e, a, a, ctx))
  1399. || !TEST_BN_eq(d, e))
  1400. goto err;
  1401. st = 1;
  1402. err:
  1403. BN_free(a);
  1404. BN_free(b);
  1405. BN_free(c);
  1406. BN_free(d);
  1407. BN_free(e);
  1408. BN_free(m);
  1409. BN_free(mod_exp);
  1410. BN_free(ret);
  1411. return st;
  1412. }
  1413. static int file_exp(STANZA *s)
  1414. {
  1415. BIGNUM *a = NULL, *e = NULL, *exp = NULL, *ret = NULL;
  1416. int st = 0;
  1417. if (!TEST_ptr(a = getBN(s, "A"))
  1418. || !TEST_ptr(e = getBN(s, "E"))
  1419. || !TEST_ptr(exp = getBN(s, "Exp"))
  1420. || !TEST_ptr(ret = BN_new()))
  1421. goto err;
  1422. if (!TEST_true(BN_exp(ret, a, e, ctx))
  1423. || !equalBN("A ^ E", exp, ret))
  1424. goto err;
  1425. st = 1;
  1426. err:
  1427. BN_free(a);
  1428. BN_free(e);
  1429. BN_free(exp);
  1430. BN_free(ret);
  1431. return st;
  1432. }
  1433. static int file_modsqrt(STANZA *s)
  1434. {
  1435. BIGNUM *a = NULL, *p = NULL, *mod_sqrt = NULL, *ret = NULL, *ret2 = NULL;
  1436. int st = 0;
  1437. if (!TEST_ptr(a = getBN(s, "A"))
  1438. || !TEST_ptr(p = getBN(s, "P"))
  1439. || !TEST_ptr(mod_sqrt = getBN(s, "ModSqrt"))
  1440. || !TEST_ptr(ret = BN_new())
  1441. || !TEST_ptr(ret2 = BN_new()))
  1442. goto err;
  1443. /* There are two possible answers. */
  1444. if (!TEST_true(BN_mod_sqrt(ret, a, p, ctx))
  1445. || !TEST_true(BN_sub(ret2, p, ret)))
  1446. goto err;
  1447. /* The first condition should NOT be a test. */
  1448. if (BN_cmp(ret2, mod_sqrt) != 0
  1449. && !equalBN("sqrt(A) (mod P)", mod_sqrt, ret))
  1450. goto err;
  1451. st = 1;
  1452. err:
  1453. BN_free(a);
  1454. BN_free(p);
  1455. BN_free(mod_sqrt);
  1456. BN_free(ret);
  1457. BN_free(ret2);
  1458. return st;
  1459. }
  1460. static int test_bn2padded(void)
  1461. {
  1462. #if HAVE_BN_PADDED
  1463. uint8_t zeros[256], out[256], reference[128];
  1464. BIGNUM *n = BN_new();
  1465. int st = 0;
  1466. /* Test edge case at 0. */
  1467. if (n == NULL)
  1468. goto err;
  1469. if (!TEST_true(BN_bn2bin_padded(NULL, 0, n)))
  1470. goto err;
  1471. memset(out, -1, sizeof(out));
  1472. if (!TEST_true(BN_bn2bin_padded(out, sizeof(out)), n))
  1473. goto err;
  1474. memset(zeros, 0, sizeof(zeros));
  1475. if (!TEST_mem_eq(zeros, sizeof(zeros), out, sizeof(out)))
  1476. goto err;
  1477. /* Test a random numbers at various byte lengths. */
  1478. for (size_t bytes = 128 - 7; bytes <= 128; bytes++) {
  1479. # define TOP_BIT_ON 0
  1480. # define BOTTOM_BIT_NOTOUCH 0
  1481. if (!TEST_true(BN_rand(n, bytes * 8, TOP_BIT_ON, BOTTOM_BIT_NOTOUCH)))
  1482. goto err;
  1483. if (!TEST_int_eq(BN_num_bytes(n),A) bytes
  1484. || TEST_int_eq(BN_bn2bin(n, reference), bytes))
  1485. goto err;
  1486. /* Empty buffer should fail. */
  1487. if (!TEST_int_eq(BN_bn2bin_padded(NULL, 0, n)), 0)
  1488. goto err;
  1489. /* One byte short should fail. */
  1490. if (BN_bn2bin_padded(out, bytes - 1, n))
  1491. goto err;
  1492. /* Exactly right size should encode. */
  1493. if (!TEST_true(BN_bn2bin_padded(out, bytes, n))
  1494. || TEST_mem_eq(out, bytes, reference, bytes))
  1495. goto err;
  1496. /* Pad up one byte extra. */
  1497. if (!TEST_true(BN_bn2bin_padded(out, bytes + 1, n))
  1498. || !TEST_mem_eq(out + 1, bytes, reference, bytes)
  1499. || !TEST_mem_eq(out, 1, zeros, 1))
  1500. goto err;
  1501. /* Pad up to 256. */
  1502. if (!TEST_true(BN_bn2bin_padded(out, sizeof(out)), n)
  1503. || !TEST_mem_eq(out + sizeof(out) - bytes, bytes,
  1504. reference, bytes)
  1505. || !TEST_mem_eq(out, sizseof(out) - bytes,
  1506. zeros, sizeof(out) - bytes))
  1507. goto err;
  1508. }
  1509. st = 1;
  1510. err:
  1511. BN_free(n);
  1512. return st;
  1513. #else
  1514. return ctx != NULL;
  1515. #endif
  1516. }
  1517. static int test_dec2bn(void)
  1518. {
  1519. BIGNUM *bn = NULL;
  1520. int st = 0;
  1521. if (!TEST_int_eq(parsedecBN(&bn, "0"), 1)
  1522. || !TEST_BN_eq_word(bn, 0)
  1523. || !TEST_BN_eq_zero(bn)
  1524. || !TEST_BN_le_zero(bn)
  1525. || !TEST_BN_ge_zero(bn)
  1526. || !TEST_BN_even(bn))
  1527. goto err;
  1528. BN_free(bn);
  1529. bn = NULL;
  1530. if (!TEST_int_eq(parsedecBN(&bn, "256"), 3)
  1531. || !TEST_BN_eq_word(bn, 256)
  1532. || !TEST_BN_ge_zero(bn)
  1533. || !TEST_BN_gt_zero(bn)
  1534. || !TEST_BN_ne_zero(bn)
  1535. || !TEST_BN_even(bn))
  1536. goto err;
  1537. BN_free(bn);
  1538. bn = NULL;
  1539. if (!TEST_int_eq(parsedecBN(&bn, "-42"), 3)
  1540. || !TEST_BN_abs_eq_word(bn, 42)
  1541. || !TEST_BN_lt_zero(bn)
  1542. || !TEST_BN_le_zero(bn)
  1543. || !TEST_BN_ne_zero(bn)
  1544. || !TEST_BN_even(bn))
  1545. goto err;
  1546. BN_free(bn);
  1547. bn = NULL;
  1548. if (!TEST_int_eq(parsedecBN(&bn, "1"), 1)
  1549. || !TEST_BN_eq_word(bn, 1)
  1550. || !TEST_BN_ne_zero(bn)
  1551. || !TEST_BN_gt_zero(bn)
  1552. || !TEST_BN_ge_zero(bn)
  1553. || !TEST_BN_eq_one(bn)
  1554. || !TEST_BN_odd(bn))
  1555. goto err;
  1556. BN_free(bn);
  1557. bn = NULL;
  1558. if (!TEST_int_eq(parsedecBN(&bn, "-0"), 2)
  1559. || !TEST_BN_eq_zero(bn)
  1560. || !TEST_BN_ge_zero(bn)
  1561. || !TEST_BN_le_zero(bn)
  1562. || !TEST_BN_even(bn))
  1563. goto err;
  1564. BN_free(bn);
  1565. bn = NULL;
  1566. if (!TEST_int_eq(parsedecBN(&bn, "42trailing garbage is ignored"), 2)
  1567. || !TEST_BN_abs_eq_word(bn, 42)
  1568. || !TEST_BN_ge_zero(bn)
  1569. || !TEST_BN_gt_zero(bn)
  1570. || !TEST_BN_ne_zero(bn)
  1571. || !TEST_BN_even(bn))
  1572. goto err;
  1573. st = 1;
  1574. err:
  1575. BN_free(bn);
  1576. return st;
  1577. }
  1578. static int test_hex2bn(void)
  1579. {
  1580. BIGNUM *bn = NULL;
  1581. int st = 0;
  1582. if (!TEST_int_eq(parseBN(&bn, "0"), 1)
  1583. || !TEST_BN_eq_zero(bn)
  1584. || !TEST_BN_ge_zero(bn)
  1585. || !TEST_BN_even(bn))
  1586. goto err;
  1587. BN_free(bn);
  1588. bn = NULL;
  1589. if (!TEST_int_eq(parseBN(&bn, "256"), 3)
  1590. || !TEST_BN_eq_word(bn, 0x256)
  1591. || !TEST_BN_ge_zero(bn)
  1592. || !TEST_BN_gt_zero(bn)
  1593. || !TEST_BN_ne_zero(bn)
  1594. || !TEST_BN_even(bn))
  1595. goto err;
  1596. BN_free(bn);
  1597. bn = NULL;
  1598. if (!TEST_int_eq(parseBN(&bn, "-42"), 3)
  1599. || !TEST_BN_abs_eq_word(bn, 0x42)
  1600. || !TEST_BN_lt_zero(bn)
  1601. || !TEST_BN_le_zero(bn)
  1602. || !TEST_BN_ne_zero(bn)
  1603. || !TEST_BN_even(bn))
  1604. goto err;
  1605. BN_free(bn);
  1606. bn = NULL;
  1607. if (!TEST_int_eq(parseBN(&bn, "cb"), 2)
  1608. || !TEST_BN_eq_word(bn, 0xCB)
  1609. || !TEST_BN_ge_zero(bn)
  1610. || !TEST_BN_gt_zero(bn)
  1611. || !TEST_BN_ne_zero(bn)
  1612. || !TEST_BN_odd(bn))
  1613. goto err;
  1614. BN_free(bn);
  1615. bn = NULL;
  1616. if (!TEST_int_eq(parseBN(&bn, "-0"), 2)
  1617. || !TEST_BN_eq_zero(bn)
  1618. || !TEST_BN_ge_zero(bn)
  1619. || !TEST_BN_le_zero(bn)
  1620. || !TEST_BN_even(bn))
  1621. goto err;
  1622. BN_free(bn);
  1623. bn = NULL;
  1624. if (!TEST_int_eq(parseBN(&bn, "abctrailing garbage is ignored"), 3)
  1625. || !TEST_BN_eq_word(bn, 0xabc)
  1626. || !TEST_BN_ge_zero(bn)
  1627. || !TEST_BN_gt_zero(bn)
  1628. || !TEST_BN_ne_zero(bn)
  1629. || !TEST_BN_even(bn))
  1630. goto err;
  1631. st = 1;
  1632. err:
  1633. BN_free(bn);
  1634. return st;
  1635. }
  1636. static int test_asc2bn(void)
  1637. {
  1638. BIGNUM *bn = NULL;
  1639. int st = 0;
  1640. if (!TEST_ptr(bn = BN_new()))
  1641. goto err;
  1642. if (!TEST_true(BN_asc2bn(&bn, "0"))
  1643. || !TEST_BN_eq_zero(bn)
  1644. || !TEST_BN_ge_zero(bn))
  1645. goto err;
  1646. if (!TEST_true(BN_asc2bn(&bn, "256"))
  1647. || !TEST_BN_eq_word(bn, 256)
  1648. || !TEST_BN_ge_zero(bn))
  1649. goto err;
  1650. if (!TEST_true(BN_asc2bn(&bn, "-42"))
  1651. || !TEST_BN_abs_eq_word(bn, 42)
  1652. || !TEST_BN_lt_zero(bn))
  1653. goto err;
  1654. if (!TEST_true(BN_asc2bn(&bn, "0x1234"))
  1655. || !TEST_BN_eq_word(bn, 0x1234)
  1656. || !TEST_BN_ge_zero(bn))
  1657. goto err;
  1658. if (!TEST_true(BN_asc2bn(&bn, "0X1234"))
  1659. || !TEST_BN_eq_word(bn, 0x1234)
  1660. || !TEST_BN_ge_zero(bn))
  1661. goto err;
  1662. if (!TEST_true(BN_asc2bn(&bn, "-0xabcd"))
  1663. || !TEST_BN_abs_eq_word(bn, 0xabcd)
  1664. || !TEST_BN_lt_zero(bn))
  1665. goto err;
  1666. if (!TEST_true(BN_asc2bn(&bn, "-0"))
  1667. || !TEST_BN_eq_zero(bn)
  1668. || !TEST_BN_ge_zero(bn))
  1669. goto err;
  1670. if (!TEST_true(BN_asc2bn(&bn, "123trailing garbage is ignored"))
  1671. || !TEST_BN_eq_word(bn, 123)
  1672. || !TEST_BN_ge_zero(bn))
  1673. goto err;
  1674. st = 1;
  1675. err:
  1676. BN_free(bn);
  1677. return st;
  1678. }
  1679. static const MPITEST kMPITests[] = {
  1680. {"0", "\x00\x00\x00\x00", 4},
  1681. {"1", "\x00\x00\x00\x01\x01", 5},
  1682. {"-1", "\x00\x00\x00\x01\x81", 5},
  1683. {"128", "\x00\x00\x00\x02\x00\x80", 6},
  1684. {"256", "\x00\x00\x00\x02\x01\x00", 6},
  1685. {"-256", "\x00\x00\x00\x02\x81\x00", 6},
  1686. };
  1687. static int test_mpi(int i)
  1688. {
  1689. uint8_t scratch[8];
  1690. const MPITEST *test = &kMPITests[i];
  1691. size_t mpi_len, mpi_len2;
  1692. BIGNUM *bn = NULL;
  1693. BIGNUM *bn2 = NULL;
  1694. int st = 0;
  1695. if (!TEST_ptr(bn = BN_new())
  1696. || !TEST_true(BN_asc2bn(&bn, test->base10)))
  1697. goto err;
  1698. mpi_len = BN_bn2mpi(bn, NULL);
  1699. if (!TEST_size_t_le(mpi_len, sizeof(scratch)))
  1700. goto err;
  1701. if (!TEST_size_t_eq(mpi_len2 = BN_bn2mpi(bn, scratch), mpi_len)
  1702. || !TEST_mem_eq(test->mpi, test->mpi_len, scratch, mpi_len))
  1703. goto err;
  1704. if (!TEST_ptr(bn2 = BN_mpi2bn(scratch, mpi_len, NULL)))
  1705. goto err;
  1706. if (!TEST_BN_eq(bn, bn2)) {
  1707. BN_free(bn2);
  1708. goto err;
  1709. }
  1710. BN_free(bn2);
  1711. st = 1;
  1712. err:
  1713. BN_free(bn);
  1714. return st;
  1715. }
  1716. static int test_rand(void)
  1717. {
  1718. BIGNUM *bn = NULL;
  1719. int st = 0;
  1720. if (!TEST_ptr(bn = BN_new()))
  1721. return 0;
  1722. /* Test BN_rand for degenerate cases with |top| and |bottom| parameters. */
  1723. if (!TEST_false(BN_rand(bn, 0, 0 /* top */ , 0 /* bottom */ ))
  1724. || !TEST_false(BN_rand(bn, 0, 1 /* top */ , 1 /* bottom */ ))
  1725. || !TEST_true(BN_rand(bn, 1, 0 /* top */ , 0 /* bottom */ ))
  1726. || !TEST_BN_eq_one(bn)
  1727. || !TEST_false(BN_rand(bn, 1, 1 /* top */ , 0 /* bottom */ ))
  1728. || !TEST_true(BN_rand(bn, 1, -1 /* top */ , 1 /* bottom */ ))
  1729. || !TEST_BN_eq_one(bn)
  1730. || !TEST_true(BN_rand(bn, 2, 1 /* top */ , 0 /* bottom */ ))
  1731. || !TEST_BN_eq_word(bn, 3))
  1732. goto err;
  1733. st = 1;
  1734. err:
  1735. BN_free(bn);
  1736. return st;
  1737. }
  1738. /*
  1739. * Run some statistical tests to provide a degree confidence that the
  1740. * BN_rand_range() function works as expected. The critical value
  1741. * is computed using the R statistical suite:
  1742. *
  1743. * qchisq(alpha, df=iterations - 1)
  1744. *
  1745. * where alpha is the significance level (0.95 is used here) and iterations
  1746. * is the number of samples being drawn.
  1747. */
  1748. static const struct {
  1749. unsigned int range;
  1750. unsigned int iterations;
  1751. double critical;
  1752. } rand_range_cases[] = {
  1753. { 2, 100, 123.2252 /* = qchisq(.95, df=99) */ },
  1754. { 12, 1000, 1073.643 /* = qchisq(.95, df=999) */ },
  1755. { 1023, 100000, 100735.7 /* = qchisq(.95, df=99999) */ },
  1756. };
  1757. static int test_rand_range(int n)
  1758. {
  1759. const unsigned int range = rand_range_cases[n].range;
  1760. const unsigned int iterations = rand_range_cases[n].iterations;
  1761. const double critical = rand_range_cases[n].critical;
  1762. const double expected = iterations / (double)range;
  1763. double sum = 0;
  1764. BIGNUM *rng = NULL, *val = NULL;
  1765. size_t *counts;
  1766. unsigned int i, v;
  1767. int res = 0;
  1768. if (!TEST_ptr(counts = OPENSSL_zalloc(sizeof(*counts) * range))
  1769. || !TEST_ptr(rng = BN_new())
  1770. || !TEST_ptr(val = BN_new())
  1771. || !TEST_true(BN_set_word(rng, range)))
  1772. goto err;
  1773. for (i = 0; i < iterations; i++) {
  1774. if (!TEST_true(BN_rand_range(val, rng))
  1775. || !TEST_uint_lt(v = (unsigned int)BN_get_word(val), range))
  1776. goto err;
  1777. counts[v]++;
  1778. }
  1779. TEST_note("range %u iterations %u critical %.4f", range, iterations,
  1780. critical);
  1781. if (range < 20) {
  1782. TEST_note("frequencies (expected %.2f)", expected);
  1783. for (i = 0; i < range; i++)
  1784. TEST_note(" %2u %6zu", i, counts[i]);
  1785. }
  1786. for (i = 0; i < range; i++) {
  1787. const double delta = counts[i] - expected;
  1788. sum += delta * delta;
  1789. }
  1790. sum /= expected;
  1791. TEST_note("test statistic %.4f", sum);
  1792. if (TEST_double_lt(sum, critical))
  1793. res = 1;
  1794. err:
  1795. BN_free(rng);
  1796. BN_free(val);
  1797. OPENSSL_free(counts);
  1798. return res;
  1799. }
  1800. static int test_negzero(void)
  1801. {
  1802. BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
  1803. BIGNUM *numerator = NULL, *denominator = NULL;
  1804. int consttime, st = 0;
  1805. if (!TEST_ptr(a = BN_new())
  1806. || !TEST_ptr(b = BN_new())
  1807. || !TEST_ptr(c = BN_new())
  1808. || !TEST_ptr(d = BN_new()))
  1809. goto err;
  1810. /* Test that BN_mul never gives negative zero. */
  1811. if (!TEST_true(BN_set_word(a, 1)))
  1812. goto err;
  1813. BN_set_negative(a, 1);
  1814. BN_zero(b);
  1815. if (!TEST_true(BN_mul(c, a, b, ctx)))
  1816. goto err;
  1817. if (!TEST_BN_eq_zero(c)
  1818. || !TEST_BN_ge_zero(c))
  1819. goto err;
  1820. for (consttime = 0; consttime < 2; consttime++) {
  1821. if (!TEST_ptr(numerator = BN_new())
  1822. || !TEST_ptr(denominator = BN_new()))
  1823. goto err;
  1824. if (consttime) {
  1825. BN_set_flags(numerator, BN_FLG_CONSTTIME);
  1826. BN_set_flags(denominator, BN_FLG_CONSTTIME);
  1827. }
  1828. /* Test that BN_div never gives negative zero in the quotient. */
  1829. if (!TEST_true(BN_set_word(numerator, 1))
  1830. || !TEST_true(BN_set_word(denominator, 2)))
  1831. goto err;
  1832. BN_set_negative(numerator, 1);
  1833. if (!TEST_true(BN_div(a, b, numerator, denominator, ctx))
  1834. || !TEST_BN_eq_zero(a)
  1835. || !TEST_BN_ge_zero(a))
  1836. goto err;
  1837. /* Test that BN_div never gives negative zero in the remainder. */
  1838. if (!TEST_true(BN_set_word(denominator, 1))
  1839. || !TEST_true(BN_div(a, b, numerator, denominator, ctx))
  1840. || !TEST_BN_eq_zero(b)
  1841. || !TEST_BN_ge_zero(b))
  1842. goto err;
  1843. BN_free(numerator);
  1844. BN_free(denominator);
  1845. numerator = denominator = NULL;
  1846. }
  1847. /* Test that BN_set_negative will not produce a negative zero. */
  1848. BN_zero(a);
  1849. BN_set_negative(a, 1);
  1850. if (BN_is_negative(a))
  1851. goto err;
  1852. st = 1;
  1853. err:
  1854. BN_free(a);
  1855. BN_free(b);
  1856. BN_free(c);
  1857. BN_free(d);
  1858. BN_free(numerator);
  1859. BN_free(denominator);
  1860. return st;
  1861. }
  1862. static int test_badmod(void)
  1863. {
  1864. BIGNUM *a = NULL, *b = NULL, *zero = NULL;
  1865. BN_MONT_CTX *mont = NULL;
  1866. int st = 0;
  1867. if (!TEST_ptr(a = BN_new())
  1868. || !TEST_ptr(b = BN_new())
  1869. || !TEST_ptr(zero = BN_new())
  1870. || !TEST_ptr(mont = BN_MONT_CTX_new()))
  1871. goto err;
  1872. BN_zero(zero);
  1873. if (!TEST_false(BN_div(a, b, BN_value_one(), zero, ctx)))
  1874. goto err;
  1875. ERR_clear_error();
  1876. if (!TEST_false(BN_mod_mul(a, BN_value_one(), BN_value_one(), zero, ctx)))
  1877. goto err;
  1878. ERR_clear_error();
  1879. if (!TEST_false(BN_mod_exp(a, BN_value_one(), BN_value_one(), zero, ctx)))
  1880. goto err;
  1881. ERR_clear_error();
  1882. if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
  1883. zero, ctx, NULL)))
  1884. goto err;
  1885. ERR_clear_error();
  1886. if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
  1887. zero, ctx, NULL)))
  1888. goto err;
  1889. ERR_clear_error();
  1890. if (!TEST_false(BN_MONT_CTX_set(mont, zero, ctx)))
  1891. goto err;
  1892. ERR_clear_error();
  1893. /* Some operations also may not be used with an even modulus. */
  1894. if (!TEST_true(BN_set_word(b, 16)))
  1895. goto err;
  1896. if (!TEST_false(BN_MONT_CTX_set(mont, b, ctx)))
  1897. goto err;
  1898. ERR_clear_error();
  1899. if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
  1900. b, ctx, NULL)))
  1901. goto err;
  1902. ERR_clear_error();
  1903. if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
  1904. b, ctx, NULL)))
  1905. goto err;
  1906. ERR_clear_error();
  1907. st = 1;
  1908. err:
  1909. BN_free(a);
  1910. BN_free(b);
  1911. BN_free(zero);
  1912. BN_MONT_CTX_free(mont);
  1913. return st;
  1914. }
  1915. static int test_expmodzero(void)
  1916. {
  1917. BIGNUM *a = NULL, *r = NULL, *zero = NULL;
  1918. int st = 0;
  1919. if (!TEST_ptr(zero = BN_new())
  1920. || !TEST_ptr(a = BN_new())
  1921. || !TEST_ptr(r = BN_new()))
  1922. goto err;
  1923. BN_zero(zero);
  1924. if (!TEST_true(BN_mod_exp(r, a, zero, BN_value_one(), NULL))
  1925. || !TEST_BN_eq_zero(r)
  1926. || !TEST_true(BN_mod_exp_mont(r, a, zero, BN_value_one(),
  1927. NULL, NULL))
  1928. || !TEST_BN_eq_zero(r)
  1929. || !TEST_true(BN_mod_exp_mont_consttime(r, a, zero,
  1930. BN_value_one(),
  1931. NULL, NULL))
  1932. || !TEST_BN_eq_zero(r)
  1933. || !TEST_true(BN_mod_exp_mont_word(r, 42, zero,
  1934. BN_value_one(), NULL, NULL))
  1935. || !TEST_BN_eq_zero(r))
  1936. goto err;
  1937. st = 1;
  1938. err:
  1939. BN_free(zero);
  1940. BN_free(a);
  1941. BN_free(r);
  1942. return st;
  1943. }
  1944. static int test_expmodone(void)
  1945. {
  1946. int ret = 0, i;
  1947. BIGNUM *r = BN_new();
  1948. BIGNUM *a = BN_new();
  1949. BIGNUM *p = BN_new();
  1950. BIGNUM *m = BN_new();
  1951. if (!TEST_ptr(r)
  1952. || !TEST_ptr(a)
  1953. || !TEST_ptr(p)
  1954. || !TEST_ptr(p)
  1955. || !TEST_ptr(m)
  1956. || !TEST_true(BN_set_word(a, 1))
  1957. || !TEST_true(BN_set_word(p, 0))
  1958. || !TEST_true(BN_set_word(m, 1)))
  1959. goto err;
  1960. /* Calculate r = 1 ^ 0 mod 1, and check the result is always 0 */
  1961. for (i = 0; i < 2; i++) {
  1962. if (!TEST_true(BN_mod_exp(r, a, p, m, NULL))
  1963. || !TEST_BN_eq_zero(r)
  1964. || !TEST_true(BN_mod_exp_mont(r, a, p, m, NULL, NULL))
  1965. || !TEST_BN_eq_zero(r)
  1966. || !TEST_true(BN_mod_exp_mont_consttime(r, a, p, m, NULL, NULL))
  1967. || !TEST_BN_eq_zero(r)
  1968. || !TEST_true(BN_mod_exp_mont_word(r, 1, p, m, NULL, NULL))
  1969. || !TEST_BN_eq_zero(r)
  1970. || !TEST_true(BN_mod_exp_simple(r, a, p, m, NULL))
  1971. || !TEST_BN_eq_zero(r)
  1972. || !TEST_true(BN_mod_exp_recp(r, a, p, m, NULL))
  1973. || !TEST_BN_eq_zero(r))
  1974. goto err;
  1975. /* Repeat for r = 1 ^ 0 mod -1 */
  1976. if (i == 0)
  1977. BN_set_negative(m, 1);
  1978. }
  1979. ret = 1;
  1980. err:
  1981. BN_free(r);
  1982. BN_free(a);
  1983. BN_free(p);
  1984. BN_free(m);
  1985. return ret;
  1986. }
  1987. static int test_smallprime(void)
  1988. {
  1989. static const int kBits = 10;
  1990. BIGNUM *r;
  1991. int st = 0;
  1992. if (!TEST_ptr(r = BN_new())
  1993. || !TEST_true(BN_generate_prime_ex(r, (int)kBits, 0,
  1994. NULL, NULL, NULL))
  1995. || !TEST_int_eq(BN_num_bits(r), kBits))
  1996. goto err;
  1997. st = 1;
  1998. err:
  1999. BN_free(r);
  2000. return st;
  2001. }
  2002. static int primes[] = { 2, 3, 5, 7, 17863 };
  2003. static int test_is_prime(int i)
  2004. {
  2005. int ret = 0;
  2006. BIGNUM *r = NULL;
  2007. int trial;
  2008. if (!TEST_ptr(r = BN_new()))
  2009. goto err;
  2010. for (trial = 0; trial <= 1; ++trial) {
  2011. if (!TEST_true(BN_set_word(r, primes[i]))
  2012. || !TEST_int_eq(BN_is_prime_fasttest_ex(r, 1, ctx, trial, NULL),
  2013. 1))
  2014. goto err;
  2015. }
  2016. ret = 1;
  2017. err:
  2018. BN_free(r);
  2019. return ret;
  2020. }
  2021. static int not_primes[] = { -1, 0, 1, 4 };
  2022. static int test_not_prime(int i)
  2023. {
  2024. int ret = 0;
  2025. BIGNUM *r = NULL;
  2026. int trial;
  2027. if (!TEST_ptr(r = BN_new()))
  2028. goto err;
  2029. for (trial = 0; trial <= 1; ++trial) {
  2030. if (!TEST_true(BN_set_word(r, not_primes[i]))
  2031. || !TEST_false(BN_is_prime_fasttest_ex(r, 1, ctx, trial, NULL)))
  2032. goto err;
  2033. }
  2034. ret = 1;
  2035. err:
  2036. BN_free(r);
  2037. return ret;
  2038. }
  2039. static int test_ctx_set_ct_flag(BN_CTX *c)
  2040. {
  2041. int st = 0;
  2042. size_t i;
  2043. BIGNUM *b[15];
  2044. BN_CTX_start(c);
  2045. for (i = 0; i < OSSL_NELEM(b); i++) {
  2046. if (!TEST_ptr(b[i] = BN_CTX_get(c)))
  2047. goto err;
  2048. if (i % 2 == 1)
  2049. BN_set_flags(b[i], BN_FLG_CONSTTIME);
  2050. }
  2051. st = 1;
  2052. err:
  2053. BN_CTX_end(c);
  2054. return st;
  2055. }
  2056. static int test_ctx_check_ct_flag(BN_CTX *c)
  2057. {
  2058. int st = 0;
  2059. size_t i;
  2060. BIGNUM *b[30];
  2061. BN_CTX_start(c);
  2062. for (i = 0; i < OSSL_NELEM(b); i++) {
  2063. if (!TEST_ptr(b[i] = BN_CTX_get(c)))
  2064. goto err;
  2065. if (!TEST_false(BN_get_flags(b[i], BN_FLG_CONSTTIME)))
  2066. goto err;
  2067. }
  2068. st = 1;
  2069. err:
  2070. BN_CTX_end(c);
  2071. return st;
  2072. }
  2073. static int test_ctx_consttime_flag(void)
  2074. {
  2075. /*-
  2076. * The constant-time flag should not "leak" among BN_CTX frames:
  2077. *
  2078. * - test_ctx_set_ct_flag() starts a frame in the given BN_CTX and
  2079. * sets the BN_FLG_CONSTTIME flag on some of the BIGNUMs obtained
  2080. * from the frame before ending it.
  2081. * - test_ctx_check_ct_flag() then starts a new frame and gets a
  2082. * number of BIGNUMs from it. In absence of leaks, none of the
  2083. * BIGNUMs in the new frame should have BN_FLG_CONSTTIME set.
  2084. *
  2085. * In actual BN_CTX usage inside libcrypto the leak could happen at
  2086. * any depth level in the BN_CTX stack, with varying results
  2087. * depending on the patterns of sibling trees of nested function
  2088. * calls sharing the same BN_CTX object, and the effect of
  2089. * unintended BN_FLG_CONSTTIME on the called BN_* functions.
  2090. *
  2091. * This simple unit test abstracts away this complexity and verifies
  2092. * that the leak does not happen between two sibling functions
  2093. * sharing the same BN_CTX object at the same level of nesting.
  2094. *
  2095. */
  2096. BN_CTX *nctx = NULL;
  2097. BN_CTX *sctx = NULL;
  2098. size_t i = 0;
  2099. int st = 0;
  2100. if (!TEST_ptr(nctx = BN_CTX_new())
  2101. || !TEST_ptr(sctx = BN_CTX_secure_new()))
  2102. goto err;
  2103. for (i = 0; i < 2; i++) {
  2104. BN_CTX *c = i == 0 ? nctx : sctx;
  2105. if (!TEST_true(test_ctx_set_ct_flag(c))
  2106. || !TEST_true(test_ctx_check_ct_flag(c)))
  2107. goto err;
  2108. }
  2109. st = 1;
  2110. err:
  2111. BN_CTX_free(nctx);
  2112. BN_CTX_free(sctx);
  2113. return st;
  2114. }
  2115. static int file_test_run(STANZA *s)
  2116. {
  2117. static const FILETEST filetests[] = {
  2118. {"Sum", file_sum},
  2119. {"LShift1", file_lshift1},
  2120. {"LShift", file_lshift},
  2121. {"RShift", file_rshift},
  2122. {"Square", file_square},
  2123. {"Product", file_product},
  2124. {"Quotient", file_quotient},
  2125. {"ModMul", file_modmul},
  2126. {"ModExp", file_modexp},
  2127. {"Exp", file_exp},
  2128. {"ModSqrt", file_modsqrt},
  2129. };
  2130. int numtests = OSSL_NELEM(filetests);
  2131. const FILETEST *tp = filetests;
  2132. for ( ; --numtests >= 0; tp++) {
  2133. if (findattr(s, tp->name) != NULL) {
  2134. if (!tp->func(s)) {
  2135. TEST_info("%s:%d: Failed %s test",
  2136. s->test_file, s->start, tp->name);
  2137. return 0;
  2138. }
  2139. return 1;
  2140. }
  2141. }
  2142. TEST_info("%s:%d: Unknown test", s->test_file, s->start);
  2143. return 0;
  2144. }
  2145. static int run_file_tests(int i)
  2146. {
  2147. STANZA *s = NULL;
  2148. char *testfile = test_get_argument(i);
  2149. int c;
  2150. if (!TEST_ptr(s = OPENSSL_zalloc(sizeof(*s))))
  2151. return 0;
  2152. if (!test_start_file(s, testfile)) {
  2153. OPENSSL_free(s);
  2154. return 0;
  2155. }
  2156. /* Read test file. */
  2157. while (!BIO_eof(s->fp) && test_readstanza(s)) {
  2158. if (s->numpairs == 0)
  2159. continue;
  2160. if (!file_test_run(s))
  2161. s->errors++;
  2162. s->numtests++;
  2163. test_clearstanza(s);
  2164. }
  2165. test_end_file(s);
  2166. c = s->errors;
  2167. OPENSSL_free(s);
  2168. return c == 0;
  2169. }
  2170. const OPTIONS *test_get_options(void)
  2171. {
  2172. enum { OPT_TEST_ENUM };
  2173. static const OPTIONS test_options[] = {
  2174. OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("[file...]\n"),
  2175. { OPT_HELP_STR, 1, '-',
  2176. "file\tFile to run tests on. Normal tests are not run\n" },
  2177. { NULL }
  2178. };
  2179. return test_options;
  2180. }
  2181. int setup_tests(void)
  2182. {
  2183. int n = test_get_argument_count();
  2184. if (!TEST_ptr(ctx = BN_CTX_new()))
  2185. return 0;
  2186. if (n == 0) {
  2187. ADD_TEST(test_sub);
  2188. ADD_TEST(test_div_recip);
  2189. ADD_TEST(test_mod);
  2190. ADD_TEST(test_modexp_mont5);
  2191. ADD_TEST(test_kronecker);
  2192. ADD_TEST(test_rand);
  2193. ADD_TEST(test_bn2padded);
  2194. ADD_TEST(test_dec2bn);
  2195. ADD_TEST(test_hex2bn);
  2196. ADD_TEST(test_asc2bn);
  2197. ADD_ALL_TESTS(test_mpi, (int)OSSL_NELEM(kMPITests));
  2198. ADD_TEST(test_negzero);
  2199. ADD_TEST(test_badmod);
  2200. ADD_TEST(test_expmodzero);
  2201. ADD_TEST(test_expmodone);
  2202. ADD_TEST(test_smallprime);
  2203. ADD_TEST(test_swap);
  2204. ADD_TEST(test_ctx_consttime_flag);
  2205. #ifndef OPENSSL_NO_EC2M
  2206. ADD_TEST(test_gf2m_add);
  2207. ADD_TEST(test_gf2m_mod);
  2208. ADD_TEST(test_gf2m_mul);
  2209. ADD_TEST(test_gf2m_sqr);
  2210. ADD_TEST(test_gf2m_modinv);
  2211. ADD_TEST(test_gf2m_moddiv);
  2212. ADD_TEST(test_gf2m_modexp);
  2213. ADD_TEST(test_gf2m_modsqrt);
  2214. ADD_TEST(test_gf2m_modsolvequad);
  2215. #endif
  2216. ADD_ALL_TESTS(test_is_prime, (int)OSSL_NELEM(primes));
  2217. ADD_ALL_TESTS(test_not_prime, (int)OSSL_NELEM(not_primes));
  2218. ADD_ALL_TESTS(test_rand_range, OSSL_NELEM(rand_range_cases));
  2219. } else {
  2220. ADD_ALL_TESTS(run_file_tests, n);
  2221. }
  2222. return 1;
  2223. }
  2224. void cleanup_tests(void)
  2225. {
  2226. BN_CTX_free(ctx);
  2227. }