bntest.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125
  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 "e_os.h"
  15. #include <internal/numbers.h>
  16. #include <openssl/bn.h>
  17. #include <openssl/crypto.h>
  18. #include <openssl/err.h>
  19. #include <openssl/rand.h>
  20. #include "testutil.h"
  21. /*
  22. * In bn_lcl.h, bn_expand() is defined as a static ossl_inline function.
  23. * This is fine in itself, it will end up as an unused static function in
  24. * the worst case. However, it references bn_expand2(), which is a private
  25. * function in libcrypto and therefore unavailable on some systems. This
  26. * may result in a linker error because of unresolved symbols.
  27. *
  28. * To avoid this, we define a dummy variant of bn_expand2() here, and to
  29. * avoid possible clashes with libcrypto, we rename it first, using a macro.
  30. */
  31. #define bn_expand2 dummy_bn_expand2
  32. BIGNUM *bn_expand2(BIGNUM *b, int words);
  33. BIGNUM *bn_expand2(BIGNUM *b, int words) { return NULL; }
  34. #include "../crypto/bn/bn_lcl.h"
  35. #define MAXPAIRS 20
  36. /*
  37. * Things in boring, not in openssl. TODO we should add them.
  38. */
  39. #define HAVE_BN_PADDED 0
  40. #define HAVE_BN_SQRT 0
  41. typedef struct pair_st {
  42. char *key;
  43. char *value;
  44. } PAIR;
  45. typedef struct stanza_st {
  46. int start;
  47. int numpairs;
  48. PAIR pairs[MAXPAIRS];
  49. } STANZA;
  50. typedef struct filetest_st {
  51. const char *name;
  52. int (*func)(STANZA *s);
  53. } FILETEST;
  54. typedef struct mpitest_st {
  55. const char *base10;
  56. const char *mpi;
  57. size_t mpi_len;
  58. } MPITEST;
  59. static const int NUM0 = 100; /* number of tests */
  60. static const int NUM1 = 50; /* additional tests for some functions */
  61. static FILE *fp;
  62. static BN_CTX *ctx;
  63. /*
  64. * Polynomial coefficients used in GFM tests.
  65. */
  66. static int p0[] = { 163, 7, 6, 3, 0, -1 };
  67. static int p1[] = { 193, 15, 0, -1 };
  68. /*
  69. * Look for |key| in the stanza and return it or NULL if not found.
  70. */
  71. static const char *findattr(STANZA *s, const char *key)
  72. {
  73. int i = s->numpairs;
  74. PAIR *pp = s->pairs;
  75. for ( ; --i >= 0; pp++)
  76. if (strcasecmp(pp->key, key) == 0)
  77. return pp->value;
  78. return NULL;
  79. }
  80. /*
  81. * Parse BIGNUM, return number of bytes parsed.
  82. */
  83. static int parseBN(BIGNUM **out, const char *in)
  84. {
  85. *out = NULL;
  86. return BN_hex2bn(out, in);
  87. }
  88. static int parsedecBN(BIGNUM **out, const char *in)
  89. {
  90. *out = NULL;
  91. return BN_dec2bn(out, in);
  92. }
  93. static BIGNUM *getBN(STANZA *s, const char *attribute)
  94. {
  95. const char *hex;
  96. BIGNUM *ret = NULL;
  97. if ((hex = findattr(s, attribute)) == NULL) {
  98. TEST_error("Can't find %s in test at line %d", attribute, s->start);
  99. return NULL;
  100. }
  101. if (parseBN(&ret, hex) != (int)strlen(hex)) {
  102. TEST_error("Could not decode '%s'", hex);
  103. return NULL;
  104. }
  105. return ret;
  106. }
  107. static int getint(STANZA *s, int *out, const char *attribute)
  108. {
  109. BIGNUM *ret;
  110. BN_ULONG word;
  111. int st = 0;
  112. if (!TEST_ptr(ret = getBN(s, attribute))
  113. || !TEST_ulong_le(word = BN_get_word(ret), INT_MAX))
  114. goto err;
  115. *out = (int)word;
  116. st = 1;
  117. err:
  118. BN_free(ret);
  119. return st;
  120. }
  121. static int equalBN(const char *op, const BIGNUM *expected, const BIGNUM *actual)
  122. {
  123. char *exstr = NULL;
  124. char *actstr = NULL;
  125. if (BN_cmp(expected, actual) == 0)
  126. return 1;
  127. if (BN_is_zero(expected) && BN_is_negative(expected))
  128. exstr = OPENSSL_strdup("-0");
  129. else
  130. exstr = BN_bn2hex(expected);
  131. if (BN_is_zero(actual) && BN_is_negative(actual))
  132. actstr = OPENSSL_strdup("-0");
  133. else
  134. actstr = BN_bn2hex(actual);
  135. if (!TEST_ptr(exstr) || !TEST_ptr(actstr))
  136. goto err;
  137. TEST_error("Got %s =\n\t%s\nwanted:\n\t%s", op, actstr, exstr);
  138. err:
  139. OPENSSL_free(exstr);
  140. OPENSSL_free(actstr);
  141. return 0;
  142. }
  143. /*
  144. * Return a "random" flag for if a BN should be negated.
  145. */
  146. static int rand_neg(void)
  147. {
  148. static unsigned int neg = 0;
  149. static int sign[8] = { 0, 0, 0, 1, 1, 0, 1, 1 };
  150. return sign[(neg++) % 8];
  151. }
  152. static int test_sub()
  153. {
  154. BIGNUM *a = NULL, *b = NULL, *c = NULL;
  155. int i, st = 0;
  156. if (!TEST_ptr(a = BN_new())
  157. || !TEST_ptr(b = BN_new())
  158. || !TEST_ptr(c = BN_new()))
  159. goto err;
  160. for (i = 0; i < NUM0 + NUM1; i++) {
  161. if (i < NUM1) {
  162. BN_bntest_rand(a, 512, 0, 0);
  163. BN_copy(b, a);
  164. if (!TEST_int_ne(BN_set_bit(a, i), 0))
  165. goto err;
  166. BN_add_word(b, i);
  167. } else {
  168. BN_bntest_rand(b, 400 + i - NUM1, 0, 0);
  169. a->neg = rand_neg();
  170. b->neg = rand_neg();
  171. }
  172. BN_sub(c, a, b);
  173. BN_add(c, c, b);
  174. BN_sub(c, c, a);
  175. if (!TEST_true(BN_is_zero(c)))
  176. goto err;
  177. }
  178. st = 1;
  179. err:
  180. BN_free(a);
  181. BN_free(b);
  182. BN_free(c);
  183. return st;
  184. }
  185. static int test_div_recip()
  186. {
  187. BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
  188. BN_RECP_CTX *recp = NULL;
  189. int st = 0, i;
  190. if (!TEST_ptr(a = BN_new())
  191. || !TEST_ptr(b = BN_new())
  192. || !TEST_ptr(c = BN_new())
  193. || !TEST_ptr(d = BN_new())
  194. || !TEST_ptr(e = BN_new())
  195. || !TEST_ptr(recp = BN_RECP_CTX_new()))
  196. goto err;
  197. for (i = 0; i < NUM0 + NUM1; i++) {
  198. if (i < NUM1) {
  199. BN_bntest_rand(a, 400, 0, 0);
  200. BN_copy(b, a);
  201. BN_lshift(a, a, i);
  202. BN_add_word(a, i);
  203. } else
  204. BN_bntest_rand(b, 50 + 3 * (i - NUM1), 0, 0);
  205. a->neg = rand_neg();
  206. b->neg = rand_neg();
  207. BN_RECP_CTX_set(recp, b, ctx);
  208. BN_div_recp(d, c, a, recp, ctx);
  209. BN_mul(e, d, b, ctx);
  210. BN_add(d, e, c);
  211. BN_sub(d, d, a);
  212. if (!TEST_true(BN_is_zero(d)))
  213. goto err;
  214. }
  215. st = 1;
  216. err:
  217. BN_free(a);
  218. BN_free(b);
  219. BN_free(c);
  220. BN_free(d);
  221. BN_free(e);
  222. BN_RECP_CTX_free(recp);
  223. return st;
  224. }
  225. static int test_mod()
  226. {
  227. BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = 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. goto err;
  235. BN_bntest_rand(a, 1024, 0, 0);
  236. for (i = 0; i < NUM0; i++) {
  237. BN_bntest_rand(b, 450 + i * 10, 0, 0);
  238. a->neg = rand_neg();
  239. b->neg = rand_neg();
  240. BN_mod(c, a, b, ctx);
  241. BN_div(d, e, a, b, ctx);
  242. BN_sub(e, e, c);
  243. if (!TEST_true(BN_is_zero(e)))
  244. goto err;
  245. }
  246. st = 1;
  247. err:
  248. BN_free(a);
  249. BN_free(b);
  250. BN_free(c);
  251. BN_free(d);
  252. BN_free(e);
  253. return st;
  254. }
  255. static const char *bn1strings[] = {
  256. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  257. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  258. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  259. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  260. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  261. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  262. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  263. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF00",
  264. "0000000000000000000000000000000000000000000000000000000000000000",
  265. "0000000000000000000000000000000000000000000000000000000000000000",
  266. "0000000000000000000000000000000000000000000000000000000000000000",
  267. "0000000000000000000000000000000000000000000000000000000000000000",
  268. "0000000000000000000000000000000000000000000000000000000000000000",
  269. "0000000000000000000000000000000000000000000000000000000000000000",
  270. "0000000000000000000000000000000000000000000000000000000000000000",
  271. "00000000000000000000000000000000000000000000000000FFFFFFFFFFFFFF",
  272. NULL
  273. };
  274. static const char *bn2strings[] = {
  275. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  276. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  277. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  278. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  279. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  280. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  281. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  282. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF0000000000",
  283. "0000000000000000000000000000000000000000000000000000000000000000",
  284. "0000000000000000000000000000000000000000000000000000000000000000",
  285. "0000000000000000000000000000000000000000000000000000000000000000",
  286. "0000000000000000000000000000000000000000000000000000000000000000",
  287. "0000000000000000000000000000000000000000000000000000000000000000",
  288. "0000000000000000000000000000000000000000000000000000000000000000",
  289. "0000000000000000000000000000000000000000000000000000000000000000",
  290. "000000000000000000000000000000000000000000FFFFFFFFFFFFFF00000000",
  291. NULL
  292. };
  293. static char *glue(const char *list[])
  294. {
  295. size_t len = 0;
  296. char *p, *save;
  297. int i;
  298. for (i = 0; list[i] != NULL; i++)
  299. len += strlen(list[i]);
  300. if (!TEST_ptr(p = save = OPENSSL_malloc(len + 1)))
  301. return NULL;
  302. for (i = 0; list[i] != NULL; i++)
  303. p += strlen(strcpy(p, list[i]));
  304. return save;
  305. }
  306. /*
  307. * Test constant-time modular exponentiation with 1024-bit inputs, which on
  308. * x86_64 cause a different code branch to be taken.
  309. */
  310. static int test_modexp_mont5()
  311. {
  312. BIGNUM *a = NULL, *p = NULL, *m = NULL, *d = NULL, *e = NULL;
  313. BIGNUM *b = NULL, *n = NULL, *c = NULL;
  314. BN_MONT_CTX *mont = NULL;
  315. char *bigstring;
  316. int st = 0;
  317. if (!TEST_ptr(a = BN_new())
  318. || !TEST_ptr(p = BN_new())
  319. || !TEST_ptr(m = BN_new())
  320. || !TEST_ptr(d = BN_new())
  321. || !TEST_ptr(e = BN_new())
  322. || !TEST_ptr(b = BN_new())
  323. || !TEST_ptr(n = BN_new())
  324. || !TEST_ptr(c = BN_new())
  325. || !TEST_ptr(mont = BN_MONT_CTX_new()))
  326. goto err;
  327. BN_bntest_rand(m, 1024, 0, 1); /* must be odd for montgomery */
  328. /* Zero exponent */
  329. BN_bntest_rand(a, 1024, 0, 0);
  330. BN_zero(p);
  331. if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL)))
  332. goto err;
  333. if (!TEST_true(BN_is_one(d)))
  334. goto err;
  335. /* Regression test for carry bug in mulx4x_mont */
  336. BN_hex2bn(&a,
  337. "7878787878787878787878787878787878787878787878787878787878787878"
  338. "7878787878787878787878787878787878787878787878787878787878787878"
  339. "7878787878787878787878787878787878787878787878787878787878787878"
  340. "7878787878787878787878787878787878787878787878787878787878787878");
  341. BN_hex2bn(&b,
  342. "095D72C08C097BA488C5E439C655A192EAFB6380073D8C2664668EDDB4060744"
  343. "E16E57FB4EDB9AE10A0CEFCDC28A894F689A128379DB279D48A2E20849D68593"
  344. "9B7803BCF46CEBF5C533FB0DD35B080593DE5472E3FE5DB951B8BFF9B4CB8F03"
  345. "9CC638A5EE8CDD703719F8000E6A9F63BEED5F2FCD52FF293EA05A251BB4AB81");
  346. BN_hex2bn(&n,
  347. "D78AF684E71DB0C39CFF4E64FB9DB567132CB9C50CC98009FEB820B26F2DED9B"
  348. "91B9B5E2B83AE0AE4EB4E0523CA726BFBE969B89FD754F674CE99118C3F2D1C5"
  349. "D81FDC7C54E02B60262B241D53C040E99E45826ECA37A804668E690E1AFC1CA4"
  350. "2C9A15D84D4954425F0B7642FC0BD9D7B24E2618D2DCC9B729D944BADACFDDAF");
  351. BN_MONT_CTX_set(mont, n, ctx);
  352. BN_mod_mul_montgomery(c, a, b, mont, ctx);
  353. BN_mod_mul_montgomery(d, b, a, mont, ctx);
  354. if (!TEST_int_eq(BN_cmp(c, d), 0))
  355. goto err;
  356. /* Regression test for carry bug in sqr[x]8x_mont */
  357. bigstring = glue(bn1strings);
  358. BN_hex2bn(&n, bigstring);
  359. OPENSSL_free(bigstring);
  360. bigstring = glue(bn2strings);
  361. BN_hex2bn(&a, bigstring);
  362. OPENSSL_free(bigstring);
  363. BN_free(b);
  364. b = BN_dup(a);
  365. BN_MONT_CTX_set(mont, n, ctx);
  366. BN_mod_mul_montgomery(c, a, a, mont, ctx);
  367. BN_mod_mul_montgomery(d, a, b, mont, ctx);
  368. if (!TEST_int_eq(BN_cmp(c, d), 0))
  369. goto err;
  370. /* Zero input */
  371. BN_bntest_rand(p, 1024, 0, 0);
  372. BN_zero(a);
  373. if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
  374. || !TEST_true(BN_is_zero(d)))
  375. goto err;
  376. /*
  377. * Craft an input whose Montgomery representation is 1, i.e., shorter
  378. * than the modulus m, in order to test the const time precomputation
  379. * scattering/gathering.
  380. */
  381. BN_one(a);
  382. BN_MONT_CTX_set(mont, m, ctx);
  383. if (!TEST_true(BN_from_montgomery(e, a, mont, ctx))
  384. || !TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
  385. || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
  386. || !TEST_int_eq(BN_cmp(a, d), 0))
  387. goto err;
  388. /* Finally, some regular test vectors. */
  389. BN_bntest_rand(e, 1024, 0, 0);
  390. if (!TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
  391. || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
  392. || !TEST_int_eq(BN_cmp(a, d), 0))
  393. goto err;
  394. st = 1;
  395. err:
  396. BN_MONT_CTX_free(mont);
  397. BN_free(a);
  398. BN_free(p);
  399. BN_free(m);
  400. BN_free(d);
  401. BN_free(e);
  402. BN_free(b);
  403. BN_free(n);
  404. BN_free(c);
  405. return st;
  406. }
  407. #ifndef OPENSSL_NO_EC2M
  408. static int test_gf2m_add()
  409. {
  410. BIGNUM *a = NULL, *b = NULL, *c = NULL;
  411. int i, st = 0;
  412. if (!TEST_ptr(a = BN_new())
  413. || !TEST_ptr(b = BN_new())
  414. || !TEST_ptr(c = BN_new()))
  415. goto err;
  416. for (i = 0; i < NUM0; i++) {
  417. BN_rand(a, 512, 0, 0);
  418. BN_copy(b, BN_value_one());
  419. a->neg = rand_neg();
  420. b->neg = rand_neg();
  421. BN_GF2m_add(c, a, b);
  422. /* Test that two added values have the correct parity. */
  423. if (!TEST_false((BN_is_odd(a) && BN_is_odd(c))
  424. || (!BN_is_odd(a) && !BN_is_odd(c))))
  425. goto err;
  426. BN_GF2m_add(c, c, c);
  427. /* Test that c + c = 0. */
  428. if (!TEST_true(BN_is_zero(c)))
  429. goto err;
  430. }
  431. st = 1;
  432. err:
  433. BN_free(a);
  434. BN_free(b);
  435. BN_free(c);
  436. return st;
  437. }
  438. static int test_gf2m_mod()
  439. {
  440. BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL, *e = NULL;
  441. int i, j, st = 0;
  442. if (!TEST_ptr(a = BN_new())
  443. || !TEST_ptr(b[0] = BN_new())
  444. || !TEST_ptr(b[1] = BN_new())
  445. || !TEST_ptr(c = BN_new())
  446. || !TEST_ptr(d = BN_new())
  447. || !TEST_ptr(e = BN_new()))
  448. goto err;
  449. BN_GF2m_arr2poly(p0, b[0]);
  450. BN_GF2m_arr2poly(p1, b[1]);
  451. for (i = 0; i < NUM0; i++) {
  452. BN_bntest_rand(a, 1024, 0, 0);
  453. for (j = 0; j < 2; j++) {
  454. BN_GF2m_mod(c, a, b[j]);
  455. BN_GF2m_add(d, a, c);
  456. BN_GF2m_mod(e, d, b[j]);
  457. /* Test that a + (a mod p) mod p == 0. */
  458. if (!TEST_true(BN_is_zero(e)))
  459. goto err;
  460. }
  461. }
  462. st = 1;
  463. err:
  464. BN_free(a);
  465. BN_free(b[0]);
  466. BN_free(b[1]);
  467. BN_free(c);
  468. BN_free(d);
  469. BN_free(e);
  470. return st;
  471. }
  472. static int test_gf2m_mul()
  473. {
  474. BIGNUM *a, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
  475. BIGNUM *e = NULL, *f = NULL, *g = NULL, *h = NULL;
  476. int i, j, st = 0;
  477. if (!TEST_ptr(a = BN_new())
  478. || !TEST_ptr(b[0] = BN_new())
  479. || !TEST_ptr(b[1] = BN_new())
  480. || !TEST_ptr(c = BN_new())
  481. || !TEST_ptr(d = BN_new())
  482. || !TEST_ptr(e = BN_new())
  483. || !TEST_ptr(f = BN_new())
  484. || !TEST_ptr(g = BN_new())
  485. || !TEST_ptr(h = BN_new()))
  486. goto err;
  487. BN_GF2m_arr2poly(p0, b[0]);
  488. BN_GF2m_arr2poly(p1, b[1]);
  489. for (i = 0; i < NUM0; i++) {
  490. BN_bntest_rand(a, 1024, 0, 0);
  491. BN_bntest_rand(c, 1024, 0, 0);
  492. BN_bntest_rand(d, 1024, 0, 0);
  493. for (j = 0; j < 2; j++) {
  494. BN_GF2m_mod_mul(e, a, c, b[j], ctx);
  495. BN_GF2m_add(f, a, d);
  496. BN_GF2m_mod_mul(g, f, c, b[j], ctx);
  497. BN_GF2m_mod_mul(h, d, c, b[j], ctx);
  498. BN_GF2m_add(f, e, g);
  499. BN_GF2m_add(f, f, h);
  500. /* Test that (a+d)*c = a*c + d*c. */
  501. if (!TEST_true(BN_is_zero(f)))
  502. goto err;
  503. }
  504. }
  505. st = 1;
  506. err:
  507. BN_free(a);
  508. BN_free(b[0]);
  509. BN_free(b[1]);
  510. BN_free(c);
  511. BN_free(d);
  512. BN_free(e);
  513. BN_free(f);
  514. BN_free(g);
  515. BN_free(h);
  516. return st;
  517. }
  518. static int test_gf2m_sqr()
  519. {
  520. BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
  521. int i, j, st = 0;
  522. if (!TEST_ptr(a = BN_new())
  523. || !TEST_ptr(b[0] = BN_new())
  524. || !TEST_ptr(b[1] = BN_new())
  525. || !TEST_ptr(c = BN_new())
  526. || !TEST_ptr(d = BN_new()))
  527. goto err;
  528. BN_GF2m_arr2poly(p0, b[0]);
  529. BN_GF2m_arr2poly(p1, b[1]);
  530. for (i = 0; i < NUM0; i++) {
  531. BN_bntest_rand(a, 1024, 0, 0);
  532. for (j = 0; j < 2; j++) {
  533. BN_GF2m_mod_sqr(c, a, b[j], ctx);
  534. BN_copy(d, a);
  535. BN_GF2m_mod_mul(d, a, d, b[j], ctx);
  536. BN_GF2m_add(d, c, d);
  537. /* Test that a*a = a^2. */
  538. if (!TEST_true(BN_is_zero(d)))
  539. goto err;
  540. }
  541. }
  542. st = 1;
  543. err:
  544. BN_free(a);
  545. BN_free(b[0]);
  546. BN_free(b[1]);
  547. BN_free(c);
  548. BN_free(d);
  549. return st;
  550. }
  551. static int test_gf2m_modinv()
  552. {
  553. BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
  554. int i, j, st = 0;
  555. if (!TEST_ptr(a = BN_new())
  556. || !TEST_ptr(b[0] = BN_new())
  557. || !TEST_ptr(b[1] = BN_new())
  558. || !TEST_ptr(c = BN_new())
  559. || !TEST_ptr(d = BN_new()))
  560. goto err;
  561. BN_GF2m_arr2poly(p0, b[0]);
  562. BN_GF2m_arr2poly(p1, b[1]);
  563. for (i = 0; i < NUM0; i++) {
  564. BN_bntest_rand(a, 512, 0, 0);
  565. for (j = 0; j < 2; j++) {
  566. BN_GF2m_mod_inv(c, a, b[j], ctx);
  567. BN_GF2m_mod_mul(d, a, c, b[j], ctx);
  568. /* Test that ((1/a)*a) = 1. */
  569. if (!TEST_true(BN_is_one(d)))
  570. goto err;
  571. }
  572. }
  573. st = 1;
  574. err:
  575. BN_free(a);
  576. BN_free(b[0]);
  577. BN_free(b[1]);
  578. BN_free(c);
  579. BN_free(d);
  580. return st;
  581. }
  582. static int test_gf2m_moddiv()
  583. {
  584. BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
  585. BIGNUM *e = NULL, *f = NULL;
  586. int i, j, st = 0;
  587. if (!TEST_ptr(a = BN_new())
  588. || !TEST_ptr(b[0] = BN_new())
  589. || !TEST_ptr(b[1] = BN_new())
  590. || !TEST_ptr(c = BN_new())
  591. || !TEST_ptr(d = BN_new())
  592. || !TEST_ptr(e = BN_new())
  593. || !TEST_ptr(f = BN_new()))
  594. goto err;
  595. BN_GF2m_arr2poly(p0, b[0]);
  596. BN_GF2m_arr2poly(p1, b[1]);
  597. for (i = 0; i < NUM0; i++) {
  598. BN_bntest_rand(a, 512, 0, 0);
  599. BN_bntest_rand(c, 512, 0, 0);
  600. for (j = 0; j < 2; j++) {
  601. BN_GF2m_mod_div(d, a, c, b[j], ctx);
  602. BN_GF2m_mod_mul(e, d, c, b[j], ctx);
  603. BN_GF2m_mod_div(f, a, e, b[j], ctx);
  604. /* Test that ((a/c)*c)/a = 1. */
  605. if (!TEST_true(BN_is_one(f)))
  606. goto err;
  607. }
  608. }
  609. st = 1;
  610. err:
  611. BN_free(a);
  612. BN_free(b[0]);
  613. BN_free(b[1]);
  614. BN_free(c);
  615. BN_free(d);
  616. BN_free(e);
  617. BN_free(f);
  618. return st;
  619. }
  620. static int test_gf2m_modexp()
  621. {
  622. BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
  623. BIGNUM *e = NULL, *f = NULL;
  624. int i, j, st = 0;
  625. if (!TEST_ptr(a = BN_new())
  626. || !TEST_ptr(b[0] = BN_new())
  627. || !TEST_ptr(b[1] = BN_new())
  628. || !TEST_ptr(c = BN_new())
  629. || !TEST_ptr(d = BN_new())
  630. || !TEST_ptr(e = BN_new())
  631. || !TEST_ptr(f = BN_new()))
  632. goto err;
  633. BN_GF2m_arr2poly(p0, b[0]);
  634. BN_GF2m_arr2poly(p1, b[1]);
  635. for (i = 0; i < NUM0; i++) {
  636. BN_bntest_rand(a, 512, 0, 0);
  637. BN_bntest_rand(c, 512, 0, 0);
  638. BN_bntest_rand(d, 512, 0, 0);
  639. for (j = 0; j < 2; j++) {
  640. BN_GF2m_mod_exp(e, a, c, b[j], ctx);
  641. BN_GF2m_mod_exp(f, a, d, b[j], ctx);
  642. BN_GF2m_mod_mul(e, e, f, b[j], ctx);
  643. BN_add(f, c, d);
  644. BN_GF2m_mod_exp(f, a, f, b[j], ctx);
  645. BN_GF2m_add(f, e, f);
  646. /* Test that a^(c+d)=a^c*a^d. */
  647. if (!TEST_true(BN_is_zero(f)))
  648. goto err;
  649. }
  650. }
  651. st = 1;
  652. err:
  653. BN_free(a);
  654. BN_free(b[0]);
  655. BN_free(b[1]);
  656. BN_free(c);
  657. BN_free(d);
  658. BN_free(e);
  659. BN_free(f);
  660. return st;
  661. }
  662. static int test_gf2m_modsqrt()
  663. {
  664. BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
  665. BIGNUM *e = NULL, *f = NULL;
  666. int i, j, st = 0;
  667. if (!TEST_ptr(a = BN_new())
  668. || !TEST_ptr(b[0] = BN_new())
  669. || !TEST_ptr(b[1] = BN_new())
  670. || !TEST_ptr(c = BN_new())
  671. || !TEST_ptr(d = BN_new())
  672. || !TEST_ptr(e = BN_new())
  673. || !TEST_ptr(f = BN_new()))
  674. goto err;
  675. BN_GF2m_arr2poly(p0, b[0]);
  676. BN_GF2m_arr2poly(p1, b[1]);
  677. for (i = 0; i < NUM0; i++) {
  678. BN_bntest_rand(a, 512, 0, 0);
  679. for (j = 0; j < 2; j++) {
  680. BN_GF2m_mod(c, a, b[j]);
  681. BN_GF2m_mod_sqrt(d, a, b[j], ctx);
  682. BN_GF2m_mod_sqr(e, d, b[j], ctx);
  683. BN_GF2m_add(f, c, e);
  684. /* Test that d^2 = a, where d = sqrt(a). */
  685. if (!TEST_true(BN_is_zero(f)))
  686. goto err;
  687. }
  688. }
  689. st = 1;
  690. err:
  691. BN_free(a);
  692. BN_free(b[0]);
  693. BN_free(b[1]);
  694. BN_free(c);
  695. BN_free(d);
  696. BN_free(e);
  697. BN_free(f);
  698. return st;
  699. }
  700. static int test_gf2m_modsolvequad()
  701. {
  702. BIGNUM *a = NULL, *b[2] = {NULL,NULL}, *c = NULL, *d = NULL;
  703. BIGNUM *e = NULL;
  704. int i, j, s = 0, t, st = 0;
  705. if (!TEST_ptr(a = BN_new())
  706. || !TEST_ptr(b[0] = BN_new())
  707. || !TEST_ptr(b[1] = BN_new())
  708. || !TEST_ptr(c = BN_new())
  709. || !TEST_ptr(d = BN_new())
  710. || !TEST_ptr(e = BN_new()))
  711. goto err;
  712. BN_GF2m_arr2poly(p0, b[0]);
  713. BN_GF2m_arr2poly(p1, b[1]);
  714. for (i = 0; i < NUM0; i++) {
  715. BN_bntest_rand(a, 512, 0, 0);
  716. for (j = 0; j < 2; j++) {
  717. t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx);
  718. if (t) {
  719. s++;
  720. BN_GF2m_mod_sqr(d, c, b[j], ctx);
  721. BN_GF2m_add(d, c, d);
  722. BN_GF2m_mod(e, a, b[j]);
  723. BN_GF2m_add(e, e, d);
  724. /*
  725. * Test that solution of quadratic c satisfies c^2 + c = a.
  726. */
  727. if (!TEST_true(BN_is_zero(e)))
  728. goto err;
  729. }
  730. }
  731. }
  732. if (!TEST_int_ge(s, 0)) {
  733. TEST_info("%d tests found no roots; probably an error", NUM0);
  734. goto err;
  735. }
  736. st = 1;
  737. err:
  738. BN_free(a);
  739. BN_free(b[0]);
  740. BN_free(b[1]);
  741. BN_free(c);
  742. BN_free(d);
  743. BN_free(e);
  744. return st;
  745. }
  746. #endif
  747. static int test_kronecker()
  748. {
  749. BIGNUM *a = NULL, *b = NULL, *r = NULL, *t = NULL;
  750. int i, legendre, kronecker, st = 0;
  751. if (!TEST_ptr(a = BN_new())
  752. || !TEST_ptr(b = BN_new())
  753. || !TEST_ptr(r = BN_new())
  754. || !TEST_ptr(t = BN_new()))
  755. goto err;
  756. /*
  757. * We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol). In
  758. * this case we know that if b is prime, then BN_kronecker(a, b, ctx) is
  759. * congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol). So we
  760. * generate a random prime b and compare these values for a number of
  761. * random a's. (That is, we run the Solovay-Strassen primality test to
  762. * confirm that b is prime, except that we don't want to test whether b
  763. * is prime but whether BN_kronecker works.)
  764. */
  765. if (!TEST_true(BN_generate_prime_ex(b, 512, 0, NULL, NULL, NULL)))
  766. goto err;
  767. b->neg = rand_neg();
  768. for (i = 0; i < NUM0; i++) {
  769. if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
  770. goto err;
  771. a->neg = rand_neg();
  772. /* t := (|b|-1)/2 (note that b is odd) */
  773. if (!TEST_true(BN_copy(t, b)))
  774. goto err;
  775. t->neg = 0;
  776. if (!TEST_true(BN_sub_word(t, 1)))
  777. goto err;
  778. if (!TEST_true(BN_rshift1(t, t)))
  779. goto err;
  780. /* r := a^t mod b */
  781. b->neg = 0;
  782. if (!TEST_true(BN_mod_exp_recp(r, a, t, b, ctx)))
  783. goto err;
  784. b->neg = 1;
  785. if (BN_is_word(r, 1))
  786. legendre = 1;
  787. else if (BN_is_zero(r))
  788. legendre = 0;
  789. else {
  790. if (!TEST_true(BN_add_word(r, 1)))
  791. goto err;
  792. if (!TEST_int_eq(BN_ucmp(r, b), 0)) {
  793. TEST_info("Legendre symbol computation failed");
  794. goto err;
  795. }
  796. legendre = -1;
  797. }
  798. if (!TEST_int_ge(kronecker = BN_kronecker(a, b, ctx), -1))
  799. goto err;
  800. /* we actually need BN_kronecker(a, |b|) */
  801. if (a->neg && b->neg)
  802. kronecker = -kronecker;
  803. if (!TEST_int_eq(legendre, kronecker))
  804. goto err;
  805. }
  806. st = 1;
  807. err:
  808. BN_free(a);
  809. BN_free(b);
  810. BN_free(r);
  811. BN_free(t);
  812. return st;
  813. }
  814. static int file_sum(STANZA *s)
  815. {
  816. BIGNUM *a = NULL, *b = NULL, *sum = NULL, *ret = NULL;
  817. BN_ULONG b_word;
  818. int st = 0;
  819. if (!TEST_ptr(a = getBN(s, "A"))
  820. || !TEST_ptr(b = getBN(s, "B"))
  821. || !TEST_ptr(sum = getBN(s, "Sum"))
  822. || !TEST_ptr(ret = BN_new()))
  823. goto err;
  824. if (!TEST_true(BN_add(ret, a, b))
  825. || !equalBN("A + B", sum, ret)
  826. || !TEST_true(BN_sub(ret, sum, a))
  827. || !equalBN("Sum - A", b, ret)
  828. || !TEST_true(BN_sub(ret, sum, b))
  829. || !equalBN("Sum - B", a, ret))
  830. goto err;
  831. /*
  832. * Test that the functions work when |r| and |a| point to the same BIGNUM,
  833. * or when |r| and |b| point to the same BIGNUM.
  834. * TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM.
  835. */
  836. if (!TEST_true(BN_copy(ret, a))
  837. || !TEST_true(BN_add(ret, ret, b))
  838. || !equalBN("A + B (r is a)", sum, ret)
  839. || !TEST_true(BN_copy(ret, b))
  840. || !TEST_true(BN_add(ret, a, ret))
  841. || !equalBN("A + B (r is b)", sum, ret)
  842. || !TEST_true(BN_copy(ret, sum))
  843. || !TEST_true(BN_sub(ret, ret, a))
  844. || !equalBN("Sum - A (r is a)", b, ret)
  845. || !TEST_true(BN_copy(ret, a))
  846. || !TEST_true(BN_sub(ret, sum, ret))
  847. || !equalBN("Sum - A (r is b)", b, ret)
  848. || !TEST_true(BN_copy(ret, sum))
  849. || !TEST_true(BN_sub(ret, ret, b))
  850. || !equalBN("Sum - B (r is a)", a, ret)
  851. || !TEST_true(BN_copy(ret, b))
  852. || !TEST_true(BN_sub(ret, sum, ret))
  853. || !equalBN("Sum - B (r is b)", a, ret))
  854. goto err;
  855. /*
  856. * Test BN_uadd() and BN_usub() with the prerequisites they are
  857. * documented as having. Note that these functions are frequently used
  858. * when the prerequisites don't hold. In those cases, they are supposed
  859. * to work as if the prerequisite hold, but we don't test that yet.
  860. * TODO: test that.
  861. */
  862. if (!BN_is_negative(a) && !BN_is_negative(b) && BN_cmp(a, b) >= 0) {
  863. if (!TEST_true(BN_uadd(ret, a, b))
  864. || !equalBN("A +u B", sum, ret)
  865. || !TEST_true(BN_usub(ret, sum, a))
  866. || !equalBN("Sum -u A", b, ret)
  867. || !TEST_true(BN_usub(ret, sum, b))
  868. || !equalBN("Sum -u B", a, ret))
  869. goto err;
  870. /*
  871. * Test that the functions work when |r| and |a| point to the same
  872. * BIGNUM, or when |r| and |b| point to the same BIGNUM.
  873. * TODO: Test where all of |r|, |a|, and |b| point to the same BIGNUM.
  874. */
  875. if (!TEST_true(BN_copy(ret, a))
  876. || !TEST_true(BN_uadd(ret, ret, b))
  877. || !equalBN("A +u B (r is a)", sum, ret)
  878. || !TEST_true(BN_copy(ret, b))
  879. || !TEST_true(BN_uadd(ret, a, ret))
  880. || !equalBN("A +u B (r is b)", sum, ret)
  881. || !TEST_true(BN_copy(ret, sum))
  882. || !TEST_true(BN_usub(ret, ret, a))
  883. || !equalBN("Sum -u A (r is a)", b, ret)
  884. || !TEST_true(BN_copy(ret, a))
  885. || !TEST_true(BN_usub(ret, sum, ret))
  886. || !equalBN("Sum -u A (r is b)", b, ret)
  887. || !TEST_true(BN_copy(ret, sum))
  888. || !TEST_true(BN_usub(ret, ret, b))
  889. || !equalBN("Sum -u B (r is a)", a, ret)
  890. || !TEST_true(BN_copy(ret, b))
  891. || !TEST_true(BN_usub(ret, sum, ret))
  892. || !equalBN("Sum -u B (r is b)", a, ret))
  893. goto err;
  894. }
  895. /*
  896. * Test with BN_add_word() and BN_sub_word() if |b| is small enough.
  897. */
  898. b_word = BN_get_word(b);
  899. if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
  900. if (!TEST_true(BN_copy(ret, a))
  901. || !TEST_true(BN_add_word(ret, b_word))
  902. || !equalBN("A + B (word)", sum, ret)
  903. || !TEST_true(BN_copy(ret, sum))
  904. || !TEST_true(BN_sub_word(ret, b_word))
  905. || !equalBN("Sum - B (word)", a, ret))
  906. goto err;
  907. }
  908. st = 1;
  909. err:
  910. BN_free(a);
  911. BN_free(b);
  912. BN_free(sum);
  913. BN_free(ret);
  914. return st;
  915. }
  916. static int file_lshift1(STANZA *s)
  917. {
  918. BIGNUM *a = NULL, *lshift1 = NULL, *zero = NULL, *ret = NULL;
  919. BIGNUM *two = NULL, *remainder = NULL;
  920. int st = 0;
  921. if (!TEST_ptr(a = getBN(s, "A"))
  922. || !TEST_ptr(lshift1 = getBN(s, "LShift1"))
  923. || !TEST_ptr(zero = BN_new())
  924. || !TEST_ptr(ret = BN_new())
  925. || !TEST_ptr(two = BN_new())
  926. || !TEST_ptr(remainder = BN_new()))
  927. goto err;
  928. BN_zero(zero);
  929. if (!TEST_true(BN_set_word(two, 2))
  930. || !TEST_true(BN_add(ret, a, a))
  931. || !equalBN("A + A", lshift1, ret)
  932. || !TEST_true(BN_mul(ret, a, two, ctx))
  933. || !equalBN("A * 2", lshift1, ret)
  934. || !TEST_true(BN_div(ret, remainder, lshift1, two, ctx))
  935. || !equalBN("LShift1 / 2", a, ret)
  936. || !equalBN("LShift1 % 2", zero, remainder)
  937. || !TEST_true(BN_lshift1(ret, a))
  938. || !equalBN("A << 1", lshift1, ret)
  939. || !TEST_true(BN_rshift1(ret, lshift1))
  940. || !equalBN("LShift >> 1", a, ret)
  941. || !TEST_true(BN_rshift1(ret, lshift1))
  942. || !equalBN("LShift >> 1", a, ret))
  943. goto err;
  944. /* Set the LSB to 1 and test rshift1 again. */
  945. if (!TEST_true(BN_set_bit(lshift1, 0))
  946. || !TEST_true(BN_div(ret, NULL /* rem */ , lshift1, two, ctx))
  947. || !equalBN("(LShift1 | 1) / 2", a, ret)
  948. || !TEST_true(BN_rshift1(ret, lshift1))
  949. || !equalBN("(LShift | 1) >> 1", a, ret))
  950. goto err;
  951. st = 1;
  952. err:
  953. BN_free(a);
  954. BN_free(lshift1);
  955. BN_free(zero);
  956. BN_free(ret);
  957. BN_free(two);
  958. BN_free(remainder);
  959. return st;
  960. }
  961. static int file_lshift(STANZA *s)
  962. {
  963. BIGNUM *a = NULL, *lshift = NULL, *ret = NULL;
  964. int n = 0, st = 0;
  965. if (!TEST_ptr(a = getBN(s, "A"))
  966. || !TEST_ptr(lshift = getBN(s, "LShift"))
  967. || !TEST_ptr(ret = BN_new()))
  968. if (!TEST_true(BN_lshift(ret, a, n))
  969. || !equalBN("A << N", lshift, ret)
  970. || !TEST_true(BN_rshift(ret, lshift, n))
  971. || !equalBN("A >> N", a, ret))
  972. goto err;
  973. st = 1;
  974. err:
  975. BN_free(a);
  976. BN_free(lshift);
  977. BN_free(ret);
  978. return st;
  979. }
  980. static int file_rshift(STANZA *s)
  981. {
  982. BIGNUM *a = NULL, *rshift = NULL, *ret = NULL;
  983. int n = 0, st = 0;
  984. if (!TEST_ptr(a = getBN(s, "A"))
  985. || !TEST_ptr(rshift = getBN(s, "RShift"))
  986. || !TEST_ptr(ret = BN_new())
  987. || !getint(s, &n, "N"))
  988. goto err;
  989. if (!TEST_true(BN_rshift(ret, a, n))
  990. || !equalBN("A >> N", rshift, ret))
  991. goto err;
  992. /* If N == 1, try with rshift1 as well */
  993. if (n == 1) {
  994. if (!TEST_true(BN_rshift1(ret, a))
  995. || !equalBN("A >> 1 (rshift1)", rshift, ret))
  996. goto err;
  997. }
  998. st = 1;
  999. err:
  1000. BN_free(a);
  1001. BN_free(rshift);
  1002. BN_free(ret);
  1003. return st;
  1004. }
  1005. static int file_square(STANZA *s)
  1006. {
  1007. BIGNUM *a = NULL, *square = NULL, *zero = NULL, *ret = NULL;
  1008. BIGNUM *remainder = NULL, *tmp = NULL;
  1009. int st = 0;
  1010. if (!TEST_ptr(a = getBN(s, "A"))
  1011. || !TEST_ptr(square = getBN(s, "Square"))
  1012. || !TEST_ptr(zero = BN_new())
  1013. || !TEST_ptr(ret = BN_new())
  1014. || !TEST_ptr(remainder = BN_new()))
  1015. goto err;
  1016. BN_zero(zero);
  1017. if (!TEST_true(BN_sqr(ret, a, ctx))
  1018. || !equalBN("A^2", square, ret)
  1019. || !TEST_true(BN_mul(ret, a, a, ctx))
  1020. || !equalBN("A * A", square, ret)
  1021. || !TEST_true(BN_div(ret, remainder, square, a, ctx))
  1022. || !equalBN("Square / A", a, ret)
  1023. || !equalBN("Square % A", zero, remainder))
  1024. goto err;
  1025. #if HAVE_BN_SQRT
  1026. BN_set_negative(a, 0);
  1027. if (!TEST_true(BN_sqrt(ret, square, ctx))
  1028. || !equalBN("sqrt(Square)", a, ret))
  1029. goto err;
  1030. /* BN_sqrt should fail on non-squares and negative numbers. */
  1031. if (!TEST_true(BN_is_zero(square))) {
  1032. if (!TEST_ptr(tmp = BN_new()) || !TEST_true(BN_copy(tmp, square)))
  1033. goto err;
  1034. BN_set_negative(tmp, 1);
  1035. if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx), 0))
  1036. goto err;
  1037. ERR_clear_error();
  1038. BN_set_negative(tmp, 0);
  1039. if (BN_add(tmp, tmp, BN_value_one()))
  1040. goto err;
  1041. if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx)))
  1042. goto err;
  1043. ERR_clear_error();
  1044. }
  1045. #endif
  1046. st = 1;
  1047. err:
  1048. BN_free(a);
  1049. BN_free(square);
  1050. BN_free(zero);
  1051. BN_free(ret);
  1052. BN_free(remainder);
  1053. BN_free(tmp);
  1054. return st;
  1055. }
  1056. static int file_product(STANZA *s)
  1057. {
  1058. BIGNUM *a = NULL, *b = NULL, *product = NULL, *ret = NULL;
  1059. BIGNUM *remainder = NULL, *zero = NULL;
  1060. int st = 0;
  1061. if (!TEST_ptr(a = getBN(s, "A"))
  1062. || !TEST_ptr(b = getBN(s, "B"))
  1063. || !TEST_ptr(product = getBN(s, "Product"))
  1064. || !TEST_ptr(ret = BN_new())
  1065. || !TEST_ptr(remainder = BN_new())
  1066. || !TEST_ptr(zero = BN_new()))
  1067. goto err;
  1068. BN_zero(zero);
  1069. if (!TEST_true(BN_mul(ret, a, b, ctx))
  1070. || !equalBN("A * B", product, ret)
  1071. || !TEST_true(BN_div(ret, remainder, product, a, ctx))
  1072. || !equalBN("Product / A", b, ret)
  1073. || !equalBN("Product % A", zero, remainder)
  1074. || !TEST_true(BN_div(ret, remainder, product, b, ctx))
  1075. || !equalBN("Product / B", a, ret)
  1076. || !equalBN("Product % B", zero, remainder))
  1077. goto err;
  1078. st = 1;
  1079. err:
  1080. BN_free(a);
  1081. BN_free(b);
  1082. BN_free(product);
  1083. BN_free(ret);
  1084. BN_free(remainder);
  1085. BN_free(zero);
  1086. return st;
  1087. }
  1088. static int file_quotient(STANZA *s)
  1089. {
  1090. BIGNUM *a = NULL, *b = NULL, *quotient = NULL, *remainder = NULL;
  1091. BIGNUM *ret = NULL, *ret2 = NULL, *nnmod = NULL;
  1092. BN_ULONG b_word, ret_word;
  1093. int st = 0;
  1094. if (!TEST_ptr(a = getBN(s, "A"))
  1095. || !TEST_ptr(b = getBN(s, "B"))
  1096. || !TEST_ptr(quotient = getBN(s, "Quotient"))
  1097. || !TEST_ptr(remainder = getBN(s, "Remainder"))
  1098. || !TEST_ptr(ret = BN_new())
  1099. || !TEST_ptr(ret2 = BN_new())
  1100. || !TEST_ptr(nnmod = BN_new()))
  1101. goto err;
  1102. if (!TEST_true(BN_div(ret, ret2, a, b, ctx))
  1103. || !equalBN("A / B", quotient, ret)
  1104. || !equalBN("A % B", remainder, ret2)
  1105. || !TEST_true(BN_mul(ret, quotient, b, ctx))
  1106. || !TEST_true(BN_add(ret, ret, remainder))
  1107. || !equalBN("Quotient * B + Remainder", a, ret))
  1108. goto err;
  1109. /*
  1110. * Test with BN_mod_word() and BN_div_word() if the divisor is
  1111. * small enough.
  1112. */
  1113. b_word = BN_get_word(b);
  1114. if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
  1115. BN_ULONG remainder_word = BN_get_word(remainder);
  1116. assert(remainder_word != (BN_ULONG)-1);
  1117. if (!TEST_ptr(BN_copy(ret, a)))
  1118. goto err;
  1119. ret_word = BN_div_word(ret, b_word);
  1120. if (ret_word != remainder_word) {
  1121. #ifdef BN_DEC_FMT1
  1122. TEST_error(
  1123. "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1,
  1124. ret_word, remainder_word);
  1125. #else
  1126. TEST_error("Got A %% B (word) mismatch");
  1127. #endif
  1128. goto err;
  1129. }
  1130. if (!equalBN ("A / B (word)", quotient, ret))
  1131. goto err;
  1132. ret_word = BN_mod_word(a, b_word);
  1133. if (ret_word != remainder_word) {
  1134. #ifdef BN_DEC_FMT1
  1135. TEST_error(
  1136. "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1 "",
  1137. ret_word, remainder_word);
  1138. #else
  1139. TEST_error("Got A %% B (word) mismatch");
  1140. #endif
  1141. goto err;
  1142. }
  1143. }
  1144. /* Test BN_nnmod. */
  1145. if (!BN_is_negative(b)) {
  1146. if (!TEST_true(BN_copy(nnmod, remainder))
  1147. || (BN_is_negative(nnmod)
  1148. && !TEST_true(BN_add(nnmod, nnmod, b)))
  1149. || !TEST_true(BN_nnmod(ret, a, b, ctx))
  1150. || !equalBN("A % B (non-negative)", nnmod, ret))
  1151. goto err;
  1152. }
  1153. st = 1;
  1154. err:
  1155. BN_free(a);
  1156. BN_free(b);
  1157. BN_free(quotient);
  1158. BN_free(remainder);
  1159. BN_free(ret);
  1160. BN_free(ret2);
  1161. BN_free(nnmod);
  1162. return st;
  1163. }
  1164. static int file_modmul(STANZA *s)
  1165. {
  1166. BIGNUM *a = NULL, *b = NULL, *m = NULL, *mod_mul = NULL, *ret = NULL;
  1167. int st = 0;
  1168. if (!TEST_ptr(a = getBN(s, "A"))
  1169. || !TEST_ptr(b = getBN(s, "B"))
  1170. || !TEST_ptr(m = getBN(s, "M"))
  1171. || !TEST_ptr(mod_mul = getBN(s, "ModMul"))
  1172. || !TEST_ptr(ret = BN_new()))
  1173. goto err;
  1174. if (!TEST_true(BN_mod_mul(ret, a, b, m, ctx))
  1175. || !equalBN("A * B (mod M)", mod_mul, ret))
  1176. goto err;
  1177. if (BN_is_odd(m)) {
  1178. /* Reduce |a| and |b| and test the Montgomery version. */
  1179. BN_MONT_CTX *mont = BN_MONT_CTX_new();
  1180. BIGNUM *a_tmp = BN_new();
  1181. BIGNUM *b_tmp = BN_new();
  1182. if (mont == NULL || a_tmp == NULL || b_tmp == NULL
  1183. || !TEST_true(BN_MONT_CTX_set(mont, m, ctx))
  1184. || !TEST_true(BN_nnmod(a_tmp, a, m, ctx))
  1185. || !TEST_true(BN_nnmod(b_tmp, b, m, ctx))
  1186. || !TEST_true(BN_to_montgomery(a_tmp, a_tmp, mont, ctx))
  1187. || !TEST_true(BN_to_montgomery(b_tmp, b_tmp, mont, ctx))
  1188. || !TEST_true(BN_mod_mul_montgomery(ret, a_tmp, b_tmp,
  1189. mont, ctx))
  1190. || !TEST_true(BN_from_montgomery(ret, ret, mont, ctx))
  1191. || !equalBN("A * B (mod M) (mont)", mod_mul, ret))
  1192. st = 0;
  1193. else
  1194. st = 1;
  1195. BN_MONT_CTX_free(mont);
  1196. BN_free(a_tmp);
  1197. BN_free(b_tmp);
  1198. if (st == 0)
  1199. goto err;
  1200. }
  1201. st = 1;
  1202. err:
  1203. BN_free(a);
  1204. BN_free(b);
  1205. BN_free(m);
  1206. BN_free(mod_mul);
  1207. BN_free(ret);
  1208. return st;
  1209. }
  1210. static int file_modexp(STANZA *s)
  1211. {
  1212. BIGNUM *a = NULL, *e = NULL, *m = NULL, *mod_exp = NULL, *ret = NULL;
  1213. BIGNUM *b = NULL, *c = NULL, *d = NULL;
  1214. int st = 0;
  1215. if (!TEST_ptr(a = getBN(s, "A"))
  1216. || !TEST_ptr(e = getBN(s, "E"))
  1217. || !TEST_ptr(m = getBN(s, "M"))
  1218. || !TEST_ptr(mod_exp = getBN(s, "ModExp"))
  1219. || !TEST_ptr(ret = BN_new())
  1220. || !TEST_ptr(d = BN_new()))
  1221. goto err;
  1222. if (!TEST_true(BN_mod_exp(ret, a, e, m, ctx))
  1223. || !equalBN("A ^ E (mod M)", mod_exp, ret))
  1224. goto err;
  1225. if (BN_is_odd(m)) {
  1226. if (!TEST_true(BN_mod_exp_mont(ret, a, e, m, ctx, NULL))
  1227. || !equalBN("A ^ E (mod M) (mont)", mod_exp, ret)
  1228. || !TEST_true(BN_mod_exp_mont_consttime(ret, a, e, m,
  1229. ctx, NULL))
  1230. || !equalBN("A ^ E (mod M) (mont const", mod_exp, ret))
  1231. goto err;
  1232. }
  1233. /* Regression test for carry propagation bug in sqr8x_reduction */
  1234. BN_hex2bn(&a, "050505050505");
  1235. BN_hex2bn(&b, "02");
  1236. BN_hex2bn(&c,
  1237. "4141414141414141414141274141414141414141414141414141414141414141"
  1238. "4141414141414141414141414141414141414141414141414141414141414141"
  1239. "4141414141414141414141800000000000000000000000000000000000000000"
  1240. "0000000000000000000000000000000000000000000000000000000000000000"
  1241. "0000000000000000000000000000000000000000000000000000000000000000"
  1242. "0000000000000000000000000000000000000000000000000000000001");
  1243. BN_mod_exp(d, a, b, c, ctx);
  1244. BN_mul(e, a, a, ctx);
  1245. if (!TEST_int_eq(BN_cmp(d, e), 0))
  1246. goto err;
  1247. st = 1;
  1248. err:
  1249. BN_free(a);
  1250. BN_free(b);
  1251. BN_free(c);
  1252. BN_free(d);
  1253. BN_free(e);
  1254. BN_free(m);
  1255. BN_free(mod_exp);
  1256. BN_free(ret);
  1257. return st;
  1258. }
  1259. static int file_exp(STANZA *s)
  1260. {
  1261. BIGNUM *a = NULL, *e = NULL, *exp = NULL, *ret = NULL;
  1262. int st = 0;
  1263. if (!TEST_ptr(a = getBN(s, "A"))
  1264. || !TEST_ptr(e = getBN(s, "E"))
  1265. || !TEST_ptr(exp = getBN(s, "Exp"))
  1266. || !TEST_ptr(ret = BN_new()))
  1267. goto err;
  1268. if (!TEST_true(BN_exp(ret, a, e, ctx))
  1269. || !equalBN("A ^ E", exp, ret))
  1270. goto err;
  1271. st = 1;
  1272. err:
  1273. BN_free(a);
  1274. BN_free(e);
  1275. BN_free(exp);
  1276. BN_free(ret);
  1277. return st;
  1278. }
  1279. static int file_modsqrt(STANZA *s)
  1280. {
  1281. BIGNUM *a = NULL, *p = NULL, *mod_sqrt = NULL, *ret = NULL, *ret2 = NULL;
  1282. int st = 0;
  1283. if (!TEST_ptr(a = getBN(s, "A"))
  1284. || !TEST_ptr(p = getBN(s, "P"))
  1285. || !TEST_ptr(mod_sqrt = getBN(s, "ModSqrt"))
  1286. || !TEST_ptr(ret = BN_new())
  1287. || !TEST_ptr(ret2 = BN_new()))
  1288. goto err;
  1289. /* There are two possible answers. */
  1290. if (!TEST_true(BN_mod_sqrt(ret, a, p, ctx))
  1291. || !TEST_true(BN_sub(ret2, p, ret)))
  1292. goto err;
  1293. /* The first condition should NOT be a test. */
  1294. if (BN_cmp(ret2, mod_sqrt) != 0
  1295. && !equalBN("sqrt(A) (mod P)", mod_sqrt, ret))
  1296. goto err;
  1297. st = 1;
  1298. err:
  1299. BN_free(a);
  1300. BN_free(p);
  1301. BN_free(mod_sqrt);
  1302. BN_free(ret);
  1303. BN_free(ret2);
  1304. return st;
  1305. }
  1306. static int test_bn2padded()
  1307. {
  1308. #if HAVE_BN_PADDED
  1309. uint8_t zeros[256], out[256], reference[128];
  1310. BIGNUM *n = BN_new();
  1311. int st = 0;
  1312. /* Test edge case at 0. */
  1313. if (n == NULL)
  1314. goto err;
  1315. if (!TEST_true(BN_bn2bin_padded(NULL, 0, n)))
  1316. goto err;
  1317. memset(out, -1, sizeof(out));
  1318. if (!TEST_true(BN_bn2bin_padded(out, sizeof(out)), n))
  1319. goto err;
  1320. memset(zeros, 0, sizeof(zeros));
  1321. if (!TEST_mem_eq(zeros, sizeof(zeros), out, sizeof(out)))
  1322. goto err;
  1323. /* Test a random numbers at various byte lengths. */
  1324. for (size_t bytes = 128 - 7; bytes <= 128; bytes++) {
  1325. #define TOP_BIT_ON 0
  1326. #define BOTTOM_BIT_NOTOUCH 0
  1327. if (!TEST_true(BN_rand(n, bytes * 8, TOP_BIT_ON, BOTTOM_BIT_NOTOUCH)))
  1328. goto err;
  1329. if (!TEST_int_eq(BN_num_bytes(n),A) bytes
  1330. || TEST_int_eq(BN_bn2bin(n, reference), bytes))
  1331. goto err;
  1332. /* Empty buffer should fail. */
  1333. if (!TEST_int_eq(BN_bn2bin_padded(NULL, 0, n)), 0)
  1334. goto err;
  1335. /* One byte short should fail. */
  1336. if (BN_bn2bin_padded(out, bytes - 1, n))
  1337. goto err;
  1338. /* Exactly right size should encode. */
  1339. if (!TEST_true(BN_bn2bin_padded(out, bytes, n))
  1340. || TEST_mem_eq(out, bytes, reference, bytes))
  1341. goto err;
  1342. /* Pad up one byte extra. */
  1343. if (!TEST_true(BN_bn2bin_padded(out, bytes + 1, n))
  1344. || !TEST_mem_eq(out + 1, bytes, reference, bytes)
  1345. || !TEST_mem_eq(out, 1, zeros, 1))
  1346. goto err;
  1347. /* Pad up to 256. */
  1348. if (!TEST_true(BN_bn2bin_padded(out, sizeof(out)), n)
  1349. || !TEST_mem_eq(out + sizeof(out) - bytes, bytes,
  1350. reference, bytes)
  1351. || !TEST_mem_eq(out, sizseof(out) - bytes,
  1352. zeros, sizeof(out) - bytes))
  1353. goto err;
  1354. }
  1355. st = 1;
  1356. err:
  1357. BN_free(n);
  1358. return st;
  1359. #else
  1360. return ctx != NULL;
  1361. #endif
  1362. }
  1363. static int test_dec2bn()
  1364. {
  1365. BIGNUM *bn = NULL;
  1366. int st = 0;
  1367. if (!TEST_int_eq(parsedecBN(&bn, "0"), 1)
  1368. || !TEST_true(BN_is_zero(bn))
  1369. || !TEST_false(BN_is_negative(bn)))
  1370. goto err;
  1371. BN_free(bn);
  1372. if (!TEST_int_eq(parsedecBN(&bn, "256"), 3)
  1373. || !TEST_true(BN_is_word(bn, 256))
  1374. || !TEST_false(BN_is_negative(bn)))
  1375. goto err;
  1376. BN_free(bn);
  1377. if (!TEST_int_eq(parsedecBN(&bn, "-42"), 3)
  1378. || !TEST_true(BN_abs_is_word(bn, 42))
  1379. || !TEST_true(BN_is_negative(bn)))
  1380. goto err;
  1381. BN_free(bn);
  1382. if (!TEST_int_eq(parsedecBN(&bn, "-0"), 2)
  1383. || !TEST_true(BN_is_zero(bn))
  1384. || !TEST_false(BN_is_negative(bn)))
  1385. goto err;
  1386. BN_free(bn);
  1387. if (!TEST_int_eq(parsedecBN(&bn, "42trailing garbage is ignored"), 2)
  1388. || !TEST_true(BN_abs_is_word(bn, 42))
  1389. || !TEST_false(BN_is_negative(bn)))
  1390. goto err;
  1391. st = 1;
  1392. err:
  1393. BN_free(bn);
  1394. return st;
  1395. }
  1396. static int test_hex2bn()
  1397. {
  1398. BIGNUM *bn = NULL;
  1399. int st = 0;
  1400. if (!TEST_int_eq(parseBN(&bn, "0"), 1)
  1401. || !TEST_true(BN_is_zero(bn))
  1402. || !TEST_false(BN_is_negative(bn)))
  1403. goto err;
  1404. BN_free(bn);
  1405. if (!TEST_int_eq(parseBN(&bn, "256"), 3)
  1406. || !TEST_true(BN_is_word(bn, 0x256))
  1407. || !TEST_false(BN_is_negative(bn)))
  1408. goto err;
  1409. BN_free(bn);
  1410. if (!TEST_int_eq(parseBN(&bn, "-42"), 3)
  1411. || !TEST_true(BN_abs_is_word(bn, 0x42))
  1412. || !TEST_true(BN_is_negative(bn)))
  1413. goto err;
  1414. BN_free(bn);
  1415. if (!TEST_int_eq(parseBN(&bn, "-0"), 2)
  1416. || !TEST_true(BN_is_zero(bn))
  1417. || !TEST_false(BN_is_negative(bn)))
  1418. goto err;
  1419. BN_free(bn);
  1420. if (!TEST_int_eq(parseBN(&bn, "abctrailing garbage is ignored"), 3)
  1421. || !TEST_true(BN_is_word(bn, 0xabc))
  1422. || !TEST_false(BN_is_negative(bn)))
  1423. goto err;
  1424. st = 1;
  1425. err:
  1426. BN_free(bn);
  1427. return st;
  1428. }
  1429. static int test_asc2bn()
  1430. {
  1431. BIGNUM *bn = NULL;
  1432. int st = 0;
  1433. if (!TEST_ptr(bn = BN_new()))
  1434. goto err;
  1435. if (!TEST_true(BN_asc2bn(&bn, "0"))
  1436. || !TEST_true(BN_is_zero(bn))
  1437. || !TEST_false(BN_is_negative(bn)))
  1438. goto err;
  1439. if (!TEST_true(BN_asc2bn(&bn, "256"))
  1440. || !TEST_true(BN_is_word(bn, 256))
  1441. || !TEST_false(BN_is_negative(bn)))
  1442. goto err;
  1443. if (!TEST_true(BN_asc2bn(&bn, "-42"))
  1444. || !TEST_true(BN_abs_is_word(bn, 42))
  1445. || !TEST_true(BN_is_negative(bn)))
  1446. goto err;
  1447. if (!TEST_true(BN_asc2bn(&bn, "0x1234"))
  1448. || !TEST_true(BN_is_word(bn, 0x1234))
  1449. || !TEST_false(BN_is_negative(bn)))
  1450. goto err;
  1451. if (!TEST_true(BN_asc2bn(&bn, "0X1234"))
  1452. || !TEST_true(BN_is_word(bn, 0x1234))
  1453. || !TEST_false(BN_is_negative(bn)))
  1454. goto err;
  1455. if (!TEST_true(BN_asc2bn(&bn, "-0xabcd"))
  1456. || !TEST_true(BN_abs_is_word(bn, 0xabcd))
  1457. || !TEST_true(BN_is_negative(bn)))
  1458. goto err;
  1459. if (!TEST_true(BN_asc2bn(&bn, "-0"))
  1460. || !TEST_true(BN_is_zero(bn))
  1461. || !TEST_false(BN_is_negative(bn)))
  1462. goto err;
  1463. if (!TEST_true(BN_asc2bn(&bn, "123trailing garbage is ignored"))
  1464. || !TEST_true(BN_is_word(bn, 123))
  1465. || !TEST_false(BN_is_negative(bn)))
  1466. goto err;
  1467. st = 1;
  1468. err:
  1469. BN_free(bn);
  1470. return st;
  1471. }
  1472. static const MPITEST kMPITests[] = {
  1473. {"0", "\x00\x00\x00\x00", 4},
  1474. {"1", "\x00\x00\x00\x01\x01", 5},
  1475. {"-1", "\x00\x00\x00\x01\x81", 5},
  1476. {"128", "\x00\x00\x00\x02\x00\x80", 6},
  1477. {"256", "\x00\x00\x00\x02\x01\x00", 6},
  1478. {"-256", "\x00\x00\x00\x02\x81\x00", 6},
  1479. };
  1480. static int test_mpi(int i)
  1481. {
  1482. uint8_t scratch[8];
  1483. const MPITEST *test = &kMPITests[i];
  1484. size_t mpi_len, mpi_len2;
  1485. BIGNUM *bn = NULL;
  1486. BIGNUM *bn2 = NULL;
  1487. int st = 0;
  1488. if (!TEST_ptr(bn = BN_new())
  1489. || !TEST_true(BN_asc2bn(&bn, test->base10)))
  1490. goto err;
  1491. mpi_len = BN_bn2mpi(bn, NULL);
  1492. if (!TEST_size_t_le(mpi_len, sizeof(scratch)))
  1493. goto err;
  1494. if (!TEST_size_t_eq(mpi_len2 = BN_bn2mpi(bn, scratch), mpi_len)
  1495. || !TEST_mem_eq(test->mpi, test->mpi_len, scratch, mpi_len))
  1496. goto err;
  1497. if (!TEST_ptr(bn2 = BN_mpi2bn(scratch, mpi_len, NULL)))
  1498. goto err;
  1499. if (!TEST_int_eq(BN_cmp(bn, bn2), 0)) {
  1500. BN_free(bn2);
  1501. goto err;
  1502. }
  1503. BN_free(bn2);
  1504. st = 1;
  1505. err:
  1506. BN_free(bn);
  1507. return st;
  1508. }
  1509. static int test_rand()
  1510. {
  1511. BIGNUM *bn = NULL;
  1512. int st = 0;
  1513. if (!TEST_ptr(bn = BN_new()))
  1514. return 0;
  1515. /* Test BN_rand for degenerate cases with |top| and |bottom| parameters. */
  1516. if (!TEST_false(BN_rand(bn, 0, 0 /* top */ , 0 /* bottom */ ))
  1517. || !TEST_false(BN_rand(bn, 0, 1 /* top */ , 1 /* bottom */ ))
  1518. || !TEST_true(BN_rand(bn, 1, 0 /* top */ , 0 /* bottom */ ))
  1519. || !TEST_true(BN_is_word(bn, 1))
  1520. || !TEST_false(BN_rand(bn, 1, 1 /* top */ , 0 /* bottom */ ))
  1521. || !TEST_true(BN_rand(bn, 1, -1 /* top */ , 1 /* bottom */ ))
  1522. || !TEST_true(BN_is_word(bn, 1))
  1523. || !TEST_true(BN_rand(bn, 2, 1 /* top */ , 0 /* bottom */ ))
  1524. || !TEST_true(BN_is_word(bn, 3)))
  1525. goto err;
  1526. st = 1;
  1527. err:
  1528. BN_free(bn);
  1529. return st;
  1530. }
  1531. static int test_negzero()
  1532. {
  1533. BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
  1534. BIGNUM *numerator = NULL, *denominator = NULL;
  1535. int consttime, st = 0;
  1536. if (!TEST_ptr(a = BN_new())
  1537. || !TEST_ptr(b = BN_new())
  1538. || !TEST_ptr(c = BN_new())
  1539. || !TEST_ptr(d = BN_new()))
  1540. goto err;
  1541. /* Test that BN_mul never gives negative zero. */
  1542. if (!TEST_true(BN_set_word(a, 1)))
  1543. goto err;
  1544. BN_set_negative(a, 1);
  1545. BN_zero(b);
  1546. if (!TEST_true(BN_mul(c, a, b, ctx)))
  1547. goto err;
  1548. if (!TEST_true(BN_is_zero(c))
  1549. || !TEST_false(BN_is_negative(c)))
  1550. goto err;
  1551. for (consttime = 0; consttime < 2; consttime++) {
  1552. if (!TEST_ptr(numerator = BN_new())
  1553. || !TEST_ptr(denominator = BN_new()))
  1554. goto err;
  1555. if (consttime) {
  1556. BN_set_flags(numerator, BN_FLG_CONSTTIME);
  1557. BN_set_flags(denominator, BN_FLG_CONSTTIME);
  1558. }
  1559. /* Test that BN_div never gives negative zero in the quotient. */
  1560. if (!TEST_true(BN_set_word(numerator, 1))
  1561. || !TEST_true(BN_set_word(denominator, 2)))
  1562. goto err;
  1563. BN_set_negative(numerator, 1);
  1564. if (!TEST_true(BN_div(a, b, numerator, denominator, ctx))
  1565. || !TEST_true(BN_is_zero(a))
  1566. || !TEST_false(BN_is_negative(a)))
  1567. goto err;
  1568. /* Test that BN_div never gives negative zero in the remainder. */
  1569. if (!TEST_true(BN_set_word(denominator, 1))
  1570. || !TEST_true(BN_div(a, b, numerator, denominator, ctx))
  1571. || !TEST_true(BN_is_zero(b))
  1572. || !TEST_false(BN_is_negative(b)))
  1573. goto err;
  1574. BN_free(numerator);
  1575. BN_free(denominator);
  1576. numerator = denominator = NULL;
  1577. }
  1578. /* Test that BN_set_negative will not produce a negative zero. */
  1579. BN_zero(a);
  1580. BN_set_negative(a, 1);
  1581. if (BN_is_negative(a))
  1582. goto err;
  1583. st = 1;
  1584. err:
  1585. BN_free(a);
  1586. BN_free(b);
  1587. BN_free(c);
  1588. BN_free(d);
  1589. BN_free(numerator);
  1590. BN_free(denominator);
  1591. return st;
  1592. }
  1593. static int test_badmod()
  1594. {
  1595. BIGNUM *a = NULL, *b = NULL, *zero = NULL;
  1596. BN_MONT_CTX *mont = NULL;
  1597. int st = 0;
  1598. if (!TEST_ptr(a = BN_new())
  1599. || !TEST_ptr(b = BN_new())
  1600. || !TEST_ptr(zero = BN_new())
  1601. || !TEST_ptr(mont = BN_MONT_CTX_new()))
  1602. goto err;
  1603. BN_zero(zero);
  1604. if (!TEST_false(BN_div(a, b, BN_value_one(), zero, ctx)))
  1605. goto err;
  1606. ERR_clear_error();
  1607. if (!TEST_false(BN_mod_mul(a, BN_value_one(), BN_value_one(), zero, ctx)))
  1608. goto err;
  1609. ERR_clear_error();
  1610. if (!TEST_false(BN_mod_exp(a, BN_value_one(), BN_value_one(), zero, ctx)))
  1611. goto err;
  1612. ERR_clear_error();
  1613. if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
  1614. zero, ctx, NULL)))
  1615. goto err;
  1616. ERR_clear_error();
  1617. if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
  1618. zero, ctx, NULL)))
  1619. goto err;
  1620. ERR_clear_error();
  1621. if (!TEST_false(BN_MONT_CTX_set(mont, zero, ctx)))
  1622. goto err;
  1623. ERR_clear_error();
  1624. /* Some operations also may not be used with an even modulus. */
  1625. if (!TEST_true(BN_set_word(b, 16)))
  1626. goto err;
  1627. if (!TEST_false(BN_MONT_CTX_set(mont, b, ctx)))
  1628. goto err;
  1629. ERR_clear_error();
  1630. if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
  1631. b, ctx, NULL)))
  1632. goto err;
  1633. ERR_clear_error();
  1634. if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
  1635. b, ctx, NULL)))
  1636. goto err;
  1637. ERR_clear_error();
  1638. st = 1;
  1639. err:
  1640. BN_free(a);
  1641. BN_free(b);
  1642. BN_free(zero);
  1643. BN_MONT_CTX_free(mont);
  1644. return st;
  1645. }
  1646. static int test_expmodzero()
  1647. {
  1648. BIGNUM *a = NULL, *r = NULL, *zero = NULL;
  1649. int st = 0;
  1650. if (!TEST_ptr(zero = BN_new())
  1651. || !TEST_ptr(a = BN_new())
  1652. || !TEST_ptr(r = BN_new()))
  1653. goto err;
  1654. BN_zero(zero);
  1655. if (!TEST_true(BN_mod_exp(r, a, zero, BN_value_one(), NULL))
  1656. || !TEST_true(BN_is_zero(r))
  1657. || !TEST_true(BN_mod_exp_mont(r, a, zero, BN_value_one(),
  1658. NULL, NULL))
  1659. || !TEST_true(BN_is_zero(r))
  1660. || !TEST_true(BN_mod_exp_mont_consttime(r, a, zero,
  1661. BN_value_one(),
  1662. NULL, NULL))
  1663. || !TEST_true(BN_is_zero(r))
  1664. || !TEST_true(BN_mod_exp_mont_word(r, 42, zero,
  1665. BN_value_one(), NULL, NULL))
  1666. || !TEST_true(BN_is_zero(r)))
  1667. goto err;
  1668. st = 1;
  1669. err:
  1670. BN_free(zero);
  1671. BN_free(a);
  1672. BN_free(r);
  1673. return st;
  1674. }
  1675. static int test_smallprime()
  1676. {
  1677. static const int kBits = 10;
  1678. BIGNUM *r;
  1679. int st = 0;
  1680. if (!TEST_ptr(r = BN_new())
  1681. || !TEST_true(BN_generate_prime_ex(r, (int)kBits, 0,
  1682. NULL, NULL, NULL))
  1683. || !TEST_int_eq(BN_num_bits(r), kBits))
  1684. goto err;
  1685. st = 1;
  1686. err:
  1687. BN_free(r);
  1688. return st;
  1689. }
  1690. static int test_3_is_prime()
  1691. {
  1692. int ret = 0;
  1693. BIGNUM *r = NULL;
  1694. /*
  1695. * For a long time, small primes were not considered prime when
  1696. * do_trial_division was set.
  1697. */
  1698. if (!TEST_ptr(r = BN_new())
  1699. || !TEST_true(BN_set_word(r, 3))
  1700. || !TEST_int_eq(BN_is_prime_fasttest_ex(r, 3 /* nchecks */, ctx,
  1701. 0 /* do_trial_division */, NULL), 1)
  1702. || !TEST_int_eq(BN_is_prime_fasttest_ex(r, 3 /* nchecks */, ctx,
  1703. 1 /* do_trial_division */, NULL), 1))
  1704. goto err;
  1705. ret = 1;
  1706. err:
  1707. BN_free(r);
  1708. return ret;
  1709. }
  1710. /* Delete leading and trailing spaces from a string */
  1711. static char *strip_spaces(char *p)
  1712. {
  1713. char *q;
  1714. /* Skip over leading spaces */
  1715. while (*p && isspace(*p))
  1716. p++;
  1717. if (!*p)
  1718. return NULL;
  1719. for (q = p + strlen(p) - 1; q != p && isspace(*q); )
  1720. *q-- = '\0';
  1721. return *p ? p : NULL;
  1722. }
  1723. /*
  1724. * Read next test stanza; return 1 if found, 0 on EOF or error.
  1725. */
  1726. static int readstanza(STANZA *s, int *linesread)
  1727. {
  1728. PAIR *pp = s->pairs;
  1729. char *p, *equals, *key, *value;
  1730. char buff[1024];
  1731. while (fgets(buff, sizeof(buff), fp) != NULL) {
  1732. (*linesread)++;
  1733. if (!TEST_ptr(p = strchr(buff, '\n'))) {
  1734. TEST_info("Line %d too long", s->start);
  1735. return 0;
  1736. }
  1737. *p = '\0';
  1738. /* Blank line marks end of tests. */
  1739. if (buff[0] == '\0')
  1740. break;
  1741. /* Lines starting with a pound sign are ignored. */
  1742. if (buff[0] == '#')
  1743. continue;
  1744. if (!TEST_ptr(equals = strchr(buff, '=')))
  1745. return 0;
  1746. *equals++ = '\0';
  1747. if (!TEST_ptr(key = strip_spaces(buff))
  1748. || !TEST_ptr(value = strip_spaces(equals))
  1749. || !TEST_int_lt(s->numpairs++, MAXPAIRS)
  1750. || !TEST_ptr(pp->key = OPENSSL_strdup(key))
  1751. || !TEST_ptr(pp->value = OPENSSL_strdup(value)))
  1752. return 0;
  1753. pp++;
  1754. }
  1755. /* If we read anything, return ok. */
  1756. return 1;
  1757. }
  1758. static void clearstanza(STANZA *s)
  1759. {
  1760. PAIR *pp = s->pairs;
  1761. int i = s->numpairs;
  1762. int start = s->start;
  1763. for ( ; --i >= 0; pp++) {
  1764. OPENSSL_free(pp->key);
  1765. OPENSSL_free(pp->value);
  1766. }
  1767. memset(s, 0, sizeof(*s));
  1768. s->start = start;
  1769. }
  1770. static int file_test_run(STANZA *s)
  1771. {
  1772. static const FILETEST filetests[] = {
  1773. {"Sum", file_sum},
  1774. {"LShift1", file_lshift1},
  1775. {"LShift", file_lshift},
  1776. {"RShift", file_rshift},
  1777. {"Square", file_square},
  1778. {"Product", file_product},
  1779. {"Quotient", file_quotient},
  1780. {"ModMul", file_modmul},
  1781. {"ModExp", file_modexp},
  1782. {"Exp", file_exp},
  1783. {"ModSqrt", file_modsqrt},
  1784. };
  1785. int numtests = OSSL_NELEM(filetests);
  1786. const FILETEST *tp = filetests;
  1787. for ( ; --numtests >= 0; tp++) {
  1788. if (findattr(s, tp->name) != NULL) {
  1789. if (!tp->func(s)) {
  1790. TEST_info("Failed %s test at %d", tp->name, s->start);
  1791. return 0;
  1792. }
  1793. return 1;
  1794. }
  1795. }
  1796. TEST_info("Unknown test at %d", s->start);
  1797. return 0;
  1798. }
  1799. static int file_tests()
  1800. {
  1801. STANZA s;
  1802. int linesread = 0, errcnt = 0;
  1803. /* Read test file. */
  1804. memset(&s, 0, sizeof(s));
  1805. while (!feof(fp) && readstanza(&s, &linesread)) {
  1806. if (s.numpairs == 0)
  1807. continue;
  1808. if (!file_test_run(&s)) {
  1809. errcnt++;
  1810. }
  1811. clearstanza(&s);
  1812. s.start = linesread;
  1813. }
  1814. return errcnt == 0;
  1815. }
  1816. int test_main(int argc, char *argv[])
  1817. {
  1818. static const char rnd_seed[] =
  1819. "If not seeded, BN_generate_prime might fail";
  1820. int result = 0;
  1821. if (argc != 2) {
  1822. TEST_error("%s TEST_FILE", argv[0]);
  1823. return 0;
  1824. }
  1825. ADD_TEST(test_sub);
  1826. ADD_TEST(test_div_recip);
  1827. ADD_TEST(test_mod);
  1828. ADD_TEST(test_modexp_mont5);
  1829. ADD_TEST(test_kronecker);
  1830. ADD_TEST(test_rand);
  1831. ADD_TEST(test_bn2padded);
  1832. ADD_TEST(test_dec2bn);
  1833. ADD_TEST(test_hex2bn);
  1834. ADD_TEST(test_asc2bn);
  1835. ADD_ALL_TESTS(test_mpi, (int)OSSL_NELEM(kMPITests));
  1836. ADD_TEST(test_negzero);
  1837. ADD_TEST(test_badmod);
  1838. ADD_TEST(test_expmodzero);
  1839. ADD_TEST(test_smallprime);
  1840. #ifndef OPENSSL_NO_EC2M
  1841. ADD_TEST(test_gf2m_add);
  1842. ADD_TEST(test_gf2m_mod);
  1843. ADD_TEST(test_gf2m_mul);
  1844. ADD_TEST(test_gf2m_sqr);
  1845. ADD_TEST(test_gf2m_modinv);
  1846. ADD_TEST(test_gf2m_moddiv);
  1847. ADD_TEST(test_gf2m_modexp);
  1848. ADD_TEST(test_gf2m_modsqrt);
  1849. ADD_TEST(test_gf2m_modsolvequad);
  1850. #endif
  1851. ADD_TEST(test_3_is_prime);
  1852. ADD_TEST(file_tests);
  1853. RAND_seed(rnd_seed, sizeof rnd_seed);
  1854. ctx = BN_CTX_new();
  1855. TEST_check(ctx != NULL);
  1856. if (!TEST_ptr(fp = fopen(argv[1], "r")))
  1857. goto end;
  1858. result = run_tests(argv[0]);
  1859. fclose(fp);
  1860. end:
  1861. BN_CTX_free(ctx);
  1862. return result;
  1863. }