bntest.c 62 KB

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