bntest.c 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255
  1. /*
  2. * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <assert.h>
  10. #include <errno.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #ifdef __TANDEM
  14. # include <strings.h> /* strcasecmp */
  15. #endif
  16. #include <ctype.h>
  17. #include <openssl/bn.h>
  18. #include <openssl/crypto.h>
  19. #include <openssl/err.h>
  20. #include <openssl/rand.h>
  21. #include "internal/nelem.h"
  22. #include "internal/numbers.h"
  23. #include "testutil.h"
  24. /*
  25. * Things in boring, not in openssl.
  26. */
  27. #define HAVE_BN_SQRT 0
  28. typedef struct filetest_st {
  29. const char *name;
  30. int (*func)(STANZA *s);
  31. } FILETEST;
  32. typedef struct mpitest_st {
  33. const char *base10;
  34. const char *mpi;
  35. size_t mpi_len;
  36. } MPITEST;
  37. static const int NUM0 = 100; /* number of tests */
  38. static const int NUM1 = 50; /* additional tests for some functions */
  39. static const int NUM_PRIME_TESTS = 20;
  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 (OPENSSL_strcasecmp(pp->key, key) == 0)
  57. return pp->value;
  58. return NULL;
  59. }
  60. /*
  61. * Parse BIGNUM from sparse hex-strings, return |BN_hex2bn| result.
  62. */
  63. static int parse_bigBN(BIGNUM **out, const char *bn_strings[])
  64. {
  65. char *bigstring = glue_strings(bn_strings, NULL);
  66. int ret = BN_hex2bn(out, bigstring);
  67. OPENSSL_free(bigstring);
  68. return ret;
  69. }
  70. /*
  71. * Parse BIGNUM, return number of bytes parsed.
  72. */
  73. static int parseBN(BIGNUM **out, const char *in)
  74. {
  75. *out = NULL;
  76. return BN_hex2bn(out, in);
  77. }
  78. static int parsedecBN(BIGNUM **out, const char *in)
  79. {
  80. *out = NULL;
  81. return BN_dec2bn(out, in);
  82. }
  83. static BIGNUM *getBN(STANZA *s, const char *attribute)
  84. {
  85. const char *hex;
  86. BIGNUM *ret = NULL;
  87. if ((hex = findattr(s, attribute)) == NULL) {
  88. TEST_error("%s:%d: Can't find %s", s->test_file, s->start, attribute);
  89. return NULL;
  90. }
  91. if (parseBN(&ret, hex) != (int)strlen(hex)) {
  92. TEST_error("Could not decode '%s'", hex);
  93. return NULL;
  94. }
  95. return ret;
  96. }
  97. static int getint(STANZA *s, int *out, const char *attribute)
  98. {
  99. BIGNUM *ret;
  100. BN_ULONG word;
  101. int st = 0;
  102. if (!TEST_ptr(ret = getBN(s, attribute))
  103. || !TEST_ulong_le(word = BN_get_word(ret), INT_MAX))
  104. goto err;
  105. *out = (int)word;
  106. st = 1;
  107. err:
  108. BN_free(ret);
  109. return st;
  110. }
  111. static int equalBN(const char *op, const BIGNUM *expected, const BIGNUM *actual)
  112. {
  113. if (BN_cmp(expected, actual) == 0)
  114. return 1;
  115. TEST_error("unexpected %s value", op);
  116. TEST_BN_eq(expected, actual);
  117. return 0;
  118. }
  119. /*
  120. * Return a "random" flag for if a BN should be negated.
  121. */
  122. static int rand_neg(void)
  123. {
  124. static unsigned int neg = 0;
  125. static int sign[8] = { 0, 0, 0, 1, 1, 0, 1, 1 };
  126. return sign[(neg++) % 8];
  127. }
  128. static int test_swap(void)
  129. {
  130. BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
  131. int top, cond, st = 0;
  132. if (!TEST_ptr(a = BN_new())
  133. || !TEST_ptr(b = BN_new())
  134. || !TEST_ptr(c = BN_new())
  135. || !TEST_ptr(d = BN_new()))
  136. goto err;
  137. if (!(TEST_true(BN_bntest_rand(a, 1024, 1, 0))
  138. && TEST_true(BN_bntest_rand(b, 1024, 1, 0))
  139. && TEST_ptr(BN_copy(c, a))
  140. && TEST_ptr(BN_copy(d, b))))
  141. goto err;
  142. top = BN_num_bits(a) / BN_BITS2;
  143. /* regular swap */
  144. BN_swap(a, b);
  145. if (!equalBN("swap", a, d)
  146. || !equalBN("swap", b, c))
  147. goto err;
  148. /* regular swap: same pointer */
  149. BN_swap(a, a);
  150. if (!equalBN("swap with same pointer", a, d))
  151. goto err;
  152. /* conditional swap: true */
  153. cond = 1;
  154. BN_consttime_swap(cond, a, b, top);
  155. if (!equalBN("cswap true", a, c)
  156. || !equalBN("cswap true", b, d))
  157. goto err;
  158. /* conditional swap: true, same pointer */
  159. BN_consttime_swap(cond, a, a, top);
  160. if (!equalBN("cswap true", a, c))
  161. goto err;
  162. /* conditional swap: false */
  163. cond = 0;
  164. BN_consttime_swap(cond, a, b, top);
  165. if (!equalBN("cswap false", a, c)
  166. || !equalBN("cswap false", b, d))
  167. goto err;
  168. /* conditional swap: false, same pointer */
  169. BN_consttime_swap(cond, a, a, top);
  170. if (!equalBN("cswap false", a, c))
  171. goto err;
  172. /* same tests but checking flag swap */
  173. BN_set_flags(a, BN_FLG_CONSTTIME);
  174. BN_swap(a, b);
  175. if (!equalBN("swap, flags", a, d)
  176. || !equalBN("swap, flags", b, c)
  177. || !TEST_true(BN_get_flags(b, BN_FLG_CONSTTIME))
  178. || !TEST_false(BN_get_flags(a, BN_FLG_CONSTTIME)))
  179. goto err;
  180. cond = 1;
  181. BN_consttime_swap(cond, a, b, top);
  182. if (!equalBN("cswap true, flags", a, c)
  183. || !equalBN("cswap true, flags", b, d)
  184. || !TEST_true(BN_get_flags(a, BN_FLG_CONSTTIME))
  185. || !TEST_false(BN_get_flags(b, BN_FLG_CONSTTIME)))
  186. goto err;
  187. cond = 0;
  188. BN_consttime_swap(cond, a, b, top);
  189. if (!equalBN("cswap false, flags", a, c)
  190. || !equalBN("cswap false, flags", b, d)
  191. || !TEST_true(BN_get_flags(a, BN_FLG_CONSTTIME))
  192. || !TEST_false(BN_get_flags(b, BN_FLG_CONSTTIME)))
  193. goto err;
  194. st = 1;
  195. err:
  196. BN_free(a);
  197. BN_free(b);
  198. BN_free(c);
  199. BN_free(d);
  200. return st;
  201. }
  202. static int test_sub(void)
  203. {
  204. BIGNUM *a = NULL, *b = NULL, *c = NULL;
  205. int i, st = 0;
  206. if (!TEST_ptr(a = BN_new())
  207. || !TEST_ptr(b = BN_new())
  208. || !TEST_ptr(c = BN_new()))
  209. goto err;
  210. for (i = 0; i < NUM0 + NUM1; i++) {
  211. if (i < NUM1) {
  212. if (!(TEST_true(BN_bntest_rand(a, 512, 0, 0)))
  213. && TEST_ptr(BN_copy(b, a))
  214. && TEST_int_ne(BN_set_bit(a, i), 0)
  215. && TEST_true(BN_add_word(b, i)))
  216. goto err;
  217. } else {
  218. if (!TEST_true(BN_bntest_rand(b, 400 + i - NUM1, 0, 0)))
  219. goto err;
  220. BN_set_negative(a, rand_neg());
  221. BN_set_negative(b, rand_neg());
  222. }
  223. if (!(TEST_true(BN_sub(c, a, b))
  224. && TEST_true(BN_add(c, c, b))
  225. && TEST_true(BN_sub(c, c, a))
  226. && TEST_BN_eq_zero(c)))
  227. goto err;
  228. }
  229. st = 1;
  230. err:
  231. BN_free(a);
  232. BN_free(b);
  233. BN_free(c);
  234. return st;
  235. }
  236. static int test_div_recip(void)
  237. {
  238. BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
  239. BN_RECP_CTX *recp = NULL;
  240. int st = 0, i;
  241. if (!TEST_ptr(a = BN_new())
  242. || !TEST_ptr(b = BN_new())
  243. || !TEST_ptr(c = BN_new())
  244. || !TEST_ptr(d = BN_new())
  245. || !TEST_ptr(e = BN_new())
  246. || !TEST_ptr(recp = BN_RECP_CTX_new()))
  247. goto err;
  248. for (i = 0; i < NUM0 + NUM1; i++) {
  249. if (i < NUM1) {
  250. if (!(TEST_true(BN_bntest_rand(a, 400, 0, 0))
  251. && TEST_ptr(BN_copy(b, a))
  252. && TEST_true(BN_lshift(a, a, i))
  253. && TEST_true(BN_add_word(a, i))))
  254. goto err;
  255. } else {
  256. if (!(TEST_true(BN_bntest_rand(b, 50 + 3 * (i - NUM1), 0, 0))))
  257. goto err;
  258. }
  259. BN_set_negative(a, rand_neg());
  260. BN_set_negative(b, rand_neg());
  261. if (!(TEST_true(BN_RECP_CTX_set(recp, b, ctx))
  262. && TEST_true(BN_div_recp(d, c, a, recp, ctx))
  263. && TEST_true(BN_mul(e, d, b, ctx))
  264. && TEST_true(BN_add(d, e, c))
  265. && TEST_true(BN_sub(d, d, a))
  266. && TEST_BN_eq_zero(d)))
  267. goto err;
  268. }
  269. st = 1;
  270. err:
  271. BN_free(a);
  272. BN_free(b);
  273. BN_free(c);
  274. BN_free(d);
  275. BN_free(e);
  276. BN_RECP_CTX_free(recp);
  277. return st;
  278. }
  279. static struct {
  280. int n, divisor, result, remainder;
  281. } signed_mod_tests[] = {
  282. { 10, 3, 3, 1 },
  283. { -10, 3, -3, -1 },
  284. { 10, -3, -3, 1 },
  285. { -10, -3, 3, -1 },
  286. };
  287. static BIGNUM *set_signed_bn(int value)
  288. {
  289. BIGNUM *bn = BN_new();
  290. if (bn == NULL)
  291. return NULL;
  292. if (!BN_set_word(bn, value < 0 ? -value : value)) {
  293. BN_free(bn);
  294. return NULL;
  295. }
  296. BN_set_negative(bn, value < 0);
  297. return bn;
  298. }
  299. static int test_signed_mod_replace_ab(int n)
  300. {
  301. BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
  302. int st = 0;
  303. if (!TEST_ptr(a = set_signed_bn(signed_mod_tests[n].n))
  304. || !TEST_ptr(b = set_signed_bn(signed_mod_tests[n].divisor))
  305. || !TEST_ptr(c = set_signed_bn(signed_mod_tests[n].result))
  306. || !TEST_ptr(d = set_signed_bn(signed_mod_tests[n].remainder)))
  307. goto err;
  308. if (TEST_true(BN_div(a, b, a, b, ctx))
  309. && TEST_BN_eq(a, c)
  310. && TEST_BN_eq(b, d))
  311. st = 1;
  312. err:
  313. BN_free(a);
  314. BN_free(b);
  315. BN_free(c);
  316. BN_free(d);
  317. return st;
  318. }
  319. static int test_signed_mod_replace_ba(int n)
  320. {
  321. BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
  322. int st = 0;
  323. if (!TEST_ptr(a = set_signed_bn(signed_mod_tests[n].n))
  324. || !TEST_ptr(b = set_signed_bn(signed_mod_tests[n].divisor))
  325. || !TEST_ptr(c = set_signed_bn(signed_mod_tests[n].result))
  326. || !TEST_ptr(d = set_signed_bn(signed_mod_tests[n].remainder)))
  327. goto err;
  328. if (TEST_true(BN_div(b, a, a, b, ctx))
  329. && TEST_BN_eq(b, c)
  330. && TEST_BN_eq(a, d))
  331. st = 1;
  332. err:
  333. BN_free(a);
  334. BN_free(b);
  335. BN_free(c);
  336. BN_free(d);
  337. return st;
  338. }
  339. static int test_mod(void)
  340. {
  341. BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL;
  342. int st = 0, i;
  343. if (!TEST_ptr(a = BN_new())
  344. || !TEST_ptr(b = BN_new())
  345. || !TEST_ptr(c = BN_new())
  346. || !TEST_ptr(d = BN_new())
  347. || !TEST_ptr(e = BN_new()))
  348. goto err;
  349. if (!(TEST_true(BN_bntest_rand(a, 1024, 0, 0))))
  350. goto err;
  351. for (i = 0; i < NUM0; i++) {
  352. if (!(TEST_true(BN_bntest_rand(b, 450 + i * 10, 0, 0))))
  353. goto err;
  354. BN_set_negative(a, rand_neg());
  355. BN_set_negative(b, rand_neg());
  356. if (!(TEST_true(BN_mod(c, a, b, ctx))
  357. && TEST_true(BN_div(d, e, a, b, ctx))
  358. && TEST_BN_eq(e, c)
  359. && TEST_true(BN_mul(c, d, b, ctx))
  360. && TEST_true(BN_add(d, c, e))
  361. && TEST_BN_eq(d, a)))
  362. goto err;
  363. }
  364. st = 1;
  365. err:
  366. BN_free(a);
  367. BN_free(b);
  368. BN_free(c);
  369. BN_free(d);
  370. BN_free(e);
  371. return st;
  372. }
  373. static const char *bn1strings[] = {
  374. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  375. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  376. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  377. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  378. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  379. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  380. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  381. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF00",
  382. "0000000000000000000000000000000000000000000000000000000000000000",
  383. "0000000000000000000000000000000000000000000000000000000000000000",
  384. "0000000000000000000000000000000000000000000000000000000000000000",
  385. "0000000000000000000000000000000000000000000000000000000000000000",
  386. "0000000000000000000000000000000000000000000000000000000000000000",
  387. "0000000000000000000000000000000000000000000000000000000000000000",
  388. "0000000000000000000000000000000000000000000000000000000000000000",
  389. "00000000000000000000000000000000000000000000000000FFFFFFFFFFFFFF",
  390. NULL
  391. };
  392. static const char *bn2strings[] = {
  393. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  394. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  395. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  396. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  397. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  398. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  399. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  400. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000000000FFFFFFFF0000000000",
  401. "0000000000000000000000000000000000000000000000000000000000000000",
  402. "0000000000000000000000000000000000000000000000000000000000000000",
  403. "0000000000000000000000000000000000000000000000000000000000000000",
  404. "0000000000000000000000000000000000000000000000000000000000000000",
  405. "0000000000000000000000000000000000000000000000000000000000000000",
  406. "0000000000000000000000000000000000000000000000000000000000000000",
  407. "0000000000000000000000000000000000000000000000000000000000000000",
  408. "000000000000000000000000000000000000000000FFFFFFFFFFFFFF00000000",
  409. NULL
  410. };
  411. /*
  412. * Test constant-time modular exponentiation with 1024-bit inputs, which on
  413. * x86_64 cause a different code branch to be taken.
  414. */
  415. static int test_modexp_mont5(void)
  416. {
  417. BIGNUM *a = NULL, *p = NULL, *m = NULL, *d = NULL, *e = NULL;
  418. BIGNUM *b = NULL, *n = NULL, *c = NULL;
  419. BN_MONT_CTX *mont = NULL;
  420. int st = 0;
  421. if (!TEST_ptr(a = BN_new())
  422. || !TEST_ptr(p = BN_new())
  423. || !TEST_ptr(m = BN_new())
  424. || !TEST_ptr(d = BN_new())
  425. || !TEST_ptr(e = BN_new())
  426. || !TEST_ptr(b = BN_new())
  427. || !TEST_ptr(n = BN_new())
  428. || !TEST_ptr(c = BN_new())
  429. || !TEST_ptr(mont = BN_MONT_CTX_new()))
  430. goto err;
  431. /* must be odd for montgomery */
  432. if (!(TEST_true(BN_bntest_rand(m, 1024, 0, 1))
  433. /* Zero exponent */
  434. && TEST_true(BN_bntest_rand(a, 1024, 0, 0))))
  435. goto err;
  436. BN_zero(p);
  437. if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL)))
  438. goto err;
  439. if (!TEST_BN_eq_one(d))
  440. goto err;
  441. /* Regression test for carry bug in mulx4x_mont */
  442. if (!(TEST_true(BN_hex2bn(&a,
  443. "7878787878787878787878787878787878787878787878787878787878787878"
  444. "7878787878787878787878787878787878787878787878787878787878787878"
  445. "7878787878787878787878787878787878787878787878787878787878787878"
  446. "7878787878787878787878787878787878787878787878787878787878787878"))
  447. && TEST_true(BN_hex2bn(&b,
  448. "095D72C08C097BA488C5E439C655A192EAFB6380073D8C2664668EDDB4060744"
  449. "E16E57FB4EDB9AE10A0CEFCDC28A894F689A128379DB279D48A2E20849D68593"
  450. "9B7803BCF46CEBF5C533FB0DD35B080593DE5472E3FE5DB951B8BFF9B4CB8F03"
  451. "9CC638A5EE8CDD703719F8000E6A9F63BEED5F2FCD52FF293EA05A251BB4AB81"))
  452. && TEST_true(BN_hex2bn(&n,
  453. "D78AF684E71DB0C39CFF4E64FB9DB567132CB9C50CC98009FEB820B26F2DED9B"
  454. "91B9B5E2B83AE0AE4EB4E0523CA726BFBE969B89FD754F674CE99118C3F2D1C5"
  455. "D81FDC7C54E02B60262B241D53C040E99E45826ECA37A804668E690E1AFC1CA4"
  456. "2C9A15D84D4954425F0B7642FC0BD9D7B24E2618D2DCC9B729D944BADACFDDAF"))))
  457. goto err;
  458. if (!(TEST_true(BN_MONT_CTX_set(mont, n, ctx))
  459. && TEST_true(BN_mod_mul_montgomery(c, a, b, mont, ctx))
  460. && TEST_true(BN_mod_mul_montgomery(d, b, a, mont, ctx))
  461. && TEST_BN_eq(c, d)))
  462. goto err;
  463. /* Regression test for carry bug in sqr[x]8x_mont */
  464. if (!(TEST_true(parse_bigBN(&n, bn1strings))
  465. && TEST_true(parse_bigBN(&a, bn2strings))))
  466. goto err;
  467. BN_free(b);
  468. if (!(TEST_ptr(b = BN_dup(a))
  469. && TEST_true(BN_MONT_CTX_set(mont, n, ctx))
  470. && TEST_true(BN_mod_mul_montgomery(c, a, a, mont, ctx))
  471. && TEST_true(BN_mod_mul_montgomery(d, a, b, mont, ctx))
  472. && TEST_BN_eq(c, d)))
  473. goto err;
  474. /* Regression test for carry bug in bn_sqrx8x_internal */
  475. {
  476. static const char *ahex[] = {
  477. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  478. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  479. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  480. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  481. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8FFEADBCFC4DAE7FFF908E92820306B",
  482. "9544D954000000006C0000000000000000000000000000000000000000000000",
  483. "00000000000000000000FF030202FFFFF8FFEBDBCFC4DAE7FFF908E92820306B",
  484. "9544D954000000006C000000FF0302030000000000FFFFFFFFFFFFFFFFFFFFFF",
  485. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF01FC00FF02FFFFFFFF",
  486. "00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00FCFD",
  487. "FCFFFFFFFFFF000000000000000000FF0302030000000000FFFFFFFFFFFFFFFF",
  488. "FF00FCFDFDFF030202FF00000000FFFFFFFFFFFFFFFFFF00FCFDFCFFFFFFFFFF",
  489. NULL
  490. };
  491. static const char *nhex[] = {
  492. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  493. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  494. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  495. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  496. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8F8F8F8000000",
  497. "00000010000000006C0000000000000000000000000000000000000000000000",
  498. "00000000000000000000000000000000000000FFFFFFFFFFFFF8F8F8F8000000",
  499. "00000010000000006C000000000000000000000000FFFFFFFFFFFFFFFFFFFFFF",
  500. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  501. "00FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  502. "FFFFFFFFFFFF000000000000000000000000000000000000FFFFFFFFFFFFFFFF",
  503. "FFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
  504. NULL
  505. };
  506. if (!(TEST_true(parse_bigBN(&a, ahex))
  507. && TEST_true(parse_bigBN(&n, nhex))))
  508. goto err;
  509. }
  510. BN_free(b);
  511. if (!(TEST_ptr(b = BN_dup(a))
  512. && TEST_true(BN_MONT_CTX_set(mont, n, ctx))))
  513. goto err;
  514. if (!TEST_true(BN_mod_mul_montgomery(c, a, a, mont, ctx))
  515. || !TEST_true(BN_mod_mul_montgomery(d, a, b, mont, ctx))
  516. || !TEST_BN_eq(c, d))
  517. goto err;
  518. /* Regression test for bug in BN_from_montgomery_word */
  519. if (!(TEST_true(BN_hex2bn(&a,
  520. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  521. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  522. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"))
  523. && TEST_true(BN_hex2bn(&n,
  524. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  525. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"))
  526. && TEST_true(BN_MONT_CTX_set(mont, n, ctx))
  527. && TEST_false(BN_mod_mul_montgomery(d, a, a, mont, ctx))))
  528. goto err;
  529. /* Regression test for bug in rsaz_1024_mul_avx2 */
  530. if (!(TEST_true(BN_hex2bn(&a,
  531. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  532. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  533. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  534. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020DF"))
  535. && TEST_true(BN_hex2bn(&b,
  536. "2020202020202020202020202020202020202020202020202020202020202020"
  537. "2020202020202020202020202020202020202020202020202020202020202020"
  538. "20202020202020FF202020202020202020202020202020202020202020202020"
  539. "2020202020202020202020202020202020202020202020202020202020202020"))
  540. && TEST_true(BN_hex2bn(&n,
  541. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  542. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  543. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  544. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020FF"))
  545. && TEST_true(BN_MONT_CTX_set(mont, n, ctx))
  546. && TEST_true(BN_mod_exp_mont_consttime(c, a, b, n, ctx, mont))
  547. && TEST_true(BN_mod_exp_mont(d, a, b, n, ctx, mont))
  548. && TEST_BN_eq(c, d)))
  549. goto err;
  550. /*
  551. * rsaz_1024_mul_avx2 expects fully-reduced inputs.
  552. * BN_mod_exp_mont_consttime should reduce the input first.
  553. */
  554. if (!(TEST_true(BN_hex2bn(&a,
  555. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  556. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  557. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  558. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020DF"))
  559. && TEST_true(BN_hex2bn(&b,
  560. "1FA53F26F8811C58BE0357897AA5E165693230BC9DF5F01DFA6A2D59229EC69D"
  561. "9DE6A89C36E3B6957B22D6FAAD5A3C73AE587B710DBE92E83D3A9A3339A085CB"
  562. "B58F508CA4F837924BB52CC1698B7FDC2FD74362456A595A5B58E38E38E38E38"
  563. "E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E38E"))
  564. && TEST_true(BN_hex2bn(&n,
  565. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  566. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  567. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  568. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF2020202020DF"))
  569. && TEST_true(BN_MONT_CTX_set(mont, n, ctx))
  570. && TEST_true(BN_mod_exp_mont_consttime(c, a, b, n, ctx, mont))))
  571. goto err;
  572. BN_zero(d);
  573. if (!TEST_BN_eq(c, d))
  574. goto err;
  575. /*
  576. * Regression test for overflow bug in bn_sqr_comba4/8 for
  577. * mips-linux-gnu and mipsel-linux-gnu 32bit targets.
  578. */
  579. {
  580. static const char *ehex[] = {
  581. "95564994a96c45954227b845a1e99cb939d5a1da99ee91acc962396ae999a9ee",
  582. "38603790448f2f7694c242a875f0cad0aae658eba085f312d2febbbd128dd2b5",
  583. "8f7d1149f03724215d704344d0d62c587ae3c5939cba4b9b5f3dc5e8e911ef9a",
  584. "5ce1a5a749a4989d0d8368f6e1f8cdf3a362a6c97fb02047ff152b480a4ad985",
  585. "2d45efdf0770542992afca6a0590d52930434bba96017afbc9f99e112950a8b1",
  586. "a359473ec376f329bdae6a19f503be6d4be7393c4e43468831234e27e3838680",
  587. "b949390d2e416a3f9759e5349ab4c253f6f29f819a6fe4cbfd27ada34903300e",
  588. "da021f62839f5878a36f1bc3085375b00fd5fa3e68d316c0fdace87a97558465",
  589. NULL};
  590. static const char *phex[] = {
  591. "f95dc0f980fbd22e90caa5a387cc4a369f3f830d50dd321c40db8c09a7e1a241",
  592. "a536e096622d3280c0c1ba849c1f4a79bf490f60006d081e8cf69960189f0d31",
  593. "2cd9e17073a3fba7881b21474a13b334116cb2f5dbf3189a6de3515d0840f053",
  594. "c776d3982d391b6d04d642dda5cc6d1640174c09875addb70595658f89efb439",
  595. "dc6fbd55f903aadd307982d3f659207f265e1ec6271b274521b7a5e28e8fd7a5",
  596. "5df089292820477802a43cf5b6b94e999e8c9944ddebb0d0e95a60f88cb7e813",
  597. "ba110d20e1024774107dd02949031864923b3cb8c3f7250d6d1287b0a40db6a4",
  598. "7bd5a469518eb65aa207ddc47d8c6e5fc8e0c105be8fc1d4b57b2e27540471d5",
  599. NULL};
  600. static const char *mhex[] = {
  601. "fef15d5ce4625f1bccfbba49fc8439c72bf8202af039a2259678941b60bb4a8f",
  602. "2987e965d58fd8cf86a856674d519763d0e1211cc9f8596971050d56d9b35db3",
  603. "785866cfbca17cfdbed6060be3629d894f924a89fdc1efc624f80d41a22f1900",
  604. "9503fcc3824ef62ccb9208430c26f2d8ceb2c63488ec4c07437aa4c96c43dd8b",
  605. "9289ed00a712ff66ee195dc71f5e4ead02172b63c543d69baf495f5fd63ba7bc",
  606. "c633bd309c016e37736da92129d0b053d4ab28d21ad7d8b6fab2a8bbdc8ee647",
  607. "d2fbcf2cf426cf892e6f5639e0252993965dfb73ccd277407014ea784aaa280c",
  608. "b7b03972bc8b0baa72360bdb44b82415b86b2f260f877791cd33ba8f2d65229b",
  609. NULL};
  610. if (!TEST_true(parse_bigBN(&e, ehex))
  611. || !TEST_true(parse_bigBN(&p, phex))
  612. || !TEST_true(parse_bigBN(&m, mhex))
  613. || !TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
  614. || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
  615. || !TEST_BN_eq(a, d))
  616. goto err;
  617. }
  618. /* Zero input */
  619. if (!TEST_true(BN_bntest_rand(p, 1024, 0, 0)))
  620. goto err;
  621. BN_zero(a);
  622. if (!TEST_true(BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
  623. || !TEST_BN_eq_zero(d))
  624. goto err;
  625. /*
  626. * Craft an input whose Montgomery representation is 1, i.e., shorter
  627. * than the modulus m, in order to test the const time precomputation
  628. * scattering/gathering.
  629. */
  630. if (!(TEST_true(BN_one(a))
  631. && TEST_true(BN_MONT_CTX_set(mont, m, ctx))))
  632. goto err;
  633. if (!TEST_true(BN_from_montgomery(e, a, mont, ctx))
  634. || !TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
  635. || !TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
  636. || !TEST_BN_eq(a, d))
  637. goto err;
  638. /* Finally, some regular test vectors. */
  639. if (!(TEST_true(BN_bntest_rand(e, 1024, 0, 0))
  640. && TEST_true(BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
  641. && TEST_true(BN_mod_exp_simple(a, e, p, m, ctx))
  642. && TEST_BN_eq(a, d)))
  643. goto err;
  644. st = 1;
  645. err:
  646. BN_MONT_CTX_free(mont);
  647. BN_free(a);
  648. BN_free(p);
  649. BN_free(m);
  650. BN_free(d);
  651. BN_free(e);
  652. BN_free(b);
  653. BN_free(n);
  654. BN_free(c);
  655. return st;
  656. }
  657. #ifndef OPENSSL_NO_EC2M
  658. static int test_gf2m_add(void)
  659. {
  660. BIGNUM *a = NULL, *b = NULL, *c = NULL;
  661. int i, st = 0;
  662. if (!TEST_ptr(a = BN_new())
  663. || !TEST_ptr(b = BN_new())
  664. || !TEST_ptr(c = BN_new()))
  665. goto err;
  666. for (i = 0; i < NUM0; i++) {
  667. if (!(TEST_true(BN_rand(a, 512, 0, 0))
  668. && TEST_ptr(BN_copy(b, BN_value_one()))))
  669. goto err;
  670. BN_set_negative(a, rand_neg());
  671. BN_set_negative(b, rand_neg());
  672. if (!(TEST_true(BN_GF2m_add(c, a, b))
  673. /* Test that two added values have the correct parity. */
  674. && TEST_false((BN_is_odd(a) && BN_is_odd(c))
  675. || (!BN_is_odd(a) && !BN_is_odd(c)))))
  676. goto err;
  677. if (!(TEST_true(BN_GF2m_add(c, c, c))
  678. /* Test that c + c = 0. */
  679. && TEST_BN_eq_zero(c)))
  680. goto err;
  681. }
  682. st = 1;
  683. err:
  684. BN_free(a);
  685. BN_free(b);
  686. BN_free(c);
  687. return st;
  688. }
  689. static int test_gf2m_mod(void)
  690. {
  691. BIGNUM *a = NULL, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL, *e = NULL;
  692. int i, j, st = 0;
  693. if (!TEST_ptr(a = BN_new())
  694. || !TEST_ptr(b[0] = BN_new())
  695. || !TEST_ptr(b[1] = BN_new())
  696. || !TEST_ptr(c = BN_new())
  697. || !TEST_ptr(d = BN_new())
  698. || !TEST_ptr(e = BN_new()))
  699. goto err;
  700. if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
  701. && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
  702. goto err;
  703. for (i = 0; i < NUM0; i++) {
  704. if (!TEST_true(BN_bntest_rand(a, 1024, 0, 0)))
  705. goto err;
  706. for (j = 0; j < 2; j++) {
  707. if (!(TEST_true(BN_GF2m_mod(c, a, b[j]))
  708. && TEST_true(BN_GF2m_add(d, a, c))
  709. && TEST_true(BN_GF2m_mod(e, d, b[j]))
  710. /* Test that a + (a mod p) mod p == 0. */
  711. && TEST_BN_eq_zero(e)))
  712. goto err;
  713. }
  714. }
  715. st = 1;
  716. err:
  717. BN_free(a);
  718. BN_free(b[0]);
  719. BN_free(b[1]);
  720. BN_free(c);
  721. BN_free(d);
  722. BN_free(e);
  723. return st;
  724. }
  725. static int test_gf2m_mul(void)
  726. {
  727. BIGNUM *a, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
  728. BIGNUM *e = NULL, *f = NULL, *g = NULL, *h = NULL;
  729. int i, j, st = 0;
  730. if (!TEST_ptr(a = BN_new())
  731. || !TEST_ptr(b[0] = BN_new())
  732. || !TEST_ptr(b[1] = BN_new())
  733. || !TEST_ptr(c = BN_new())
  734. || !TEST_ptr(d = BN_new())
  735. || !TEST_ptr(e = BN_new())
  736. || !TEST_ptr(f = BN_new())
  737. || !TEST_ptr(g = BN_new())
  738. || !TEST_ptr(h = BN_new()))
  739. goto err;
  740. if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
  741. && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
  742. goto err;
  743. for (i = 0; i < NUM0; i++) {
  744. if (!(TEST_true(BN_bntest_rand(a, 1024, 0, 0))
  745. && TEST_true(BN_bntest_rand(c, 1024, 0, 0))
  746. && TEST_true(BN_bntest_rand(d, 1024, 0, 0))))
  747. goto err;
  748. for (j = 0; j < 2; j++) {
  749. if (!(TEST_true(BN_GF2m_mod_mul(e, a, c, b[j], ctx))
  750. && TEST_true(BN_GF2m_add(f, a, d))
  751. && TEST_true(BN_GF2m_mod_mul(g, f, c, b[j], ctx))
  752. && TEST_true(BN_GF2m_mod_mul(h, d, c, b[j], ctx))
  753. && TEST_true(BN_GF2m_add(f, e, g))
  754. && TEST_true(BN_GF2m_add(f, f, h))
  755. /* Test that (a+d)*c = a*c + d*c. */
  756. && TEST_BN_eq_zero(f)))
  757. goto err;
  758. }
  759. }
  760. st = 1;
  761. err:
  762. BN_free(a);
  763. BN_free(b[0]);
  764. BN_free(b[1]);
  765. BN_free(c);
  766. BN_free(d);
  767. BN_free(e);
  768. BN_free(f);
  769. BN_free(g);
  770. BN_free(h);
  771. return st;
  772. }
  773. static int test_gf2m_sqr(void)
  774. {
  775. BIGNUM *a = NULL, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
  776. int i, j, st = 0;
  777. if (!TEST_ptr(a = BN_new())
  778. || !TEST_ptr(b[0] = BN_new())
  779. || !TEST_ptr(b[1] = BN_new())
  780. || !TEST_ptr(c = BN_new())
  781. || !TEST_ptr(d = BN_new()))
  782. goto err;
  783. if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
  784. && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
  785. goto err;
  786. for (i = 0; i < NUM0; i++) {
  787. if (!TEST_true(BN_bntest_rand(a, 1024, 0, 0)))
  788. goto err;
  789. for (j = 0; j < 2; j++) {
  790. if (!(TEST_true(BN_GF2m_mod_sqr(c, a, b[j], ctx))
  791. && TEST_true(BN_copy(d, a))
  792. && TEST_true(BN_GF2m_mod_mul(d, a, d, b[j], ctx))
  793. && TEST_true(BN_GF2m_add(d, c, d))
  794. /* Test that a*a = a^2. */
  795. && TEST_BN_eq_zero(d)))
  796. goto err;
  797. }
  798. }
  799. st = 1;
  800. err:
  801. BN_free(a);
  802. BN_free(b[0]);
  803. BN_free(b[1]);
  804. BN_free(c);
  805. BN_free(d);
  806. return st;
  807. }
  808. static int test_gf2m_modinv(void)
  809. {
  810. BIGNUM *a = NULL, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
  811. int i, j, st = 0;
  812. if (!TEST_ptr(a = BN_new())
  813. || !TEST_ptr(b[0] = BN_new())
  814. || !TEST_ptr(b[1] = BN_new())
  815. || !TEST_ptr(c = BN_new())
  816. || !TEST_ptr(d = BN_new()))
  817. goto err;
  818. if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
  819. && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
  820. goto err;
  821. for (i = 0; i < NUM0; i++) {
  822. if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
  823. goto err;
  824. for (j = 0; j < 2; j++) {
  825. if (!(TEST_true(BN_GF2m_mod_inv(c, a, b[j], ctx))
  826. && TEST_true(BN_GF2m_mod_mul(d, a, c, b[j], ctx))
  827. /* Test that ((1/a)*a) = 1. */
  828. && TEST_BN_eq_one(d)))
  829. goto err;
  830. }
  831. }
  832. st = 1;
  833. err:
  834. BN_free(a);
  835. BN_free(b[0]);
  836. BN_free(b[1]);
  837. BN_free(c);
  838. BN_free(d);
  839. return st;
  840. }
  841. static int test_gf2m_moddiv(void)
  842. {
  843. BIGNUM *a = NULL, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
  844. BIGNUM *e = NULL, *f = NULL;
  845. int i, j, st = 0;
  846. if (!TEST_ptr(a = BN_new())
  847. || !TEST_ptr(b[0] = BN_new())
  848. || !TEST_ptr(b[1] = BN_new())
  849. || !TEST_ptr(c = BN_new())
  850. || !TEST_ptr(d = BN_new())
  851. || !TEST_ptr(e = BN_new())
  852. || !TEST_ptr(f = BN_new()))
  853. goto err;
  854. if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
  855. && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
  856. goto err;
  857. for (i = 0; i < NUM0; i++) {
  858. if (!(TEST_true(BN_bntest_rand(a, 512, 0, 0))
  859. && TEST_true(BN_bntest_rand(c, 512, 0, 0))))
  860. goto err;
  861. for (j = 0; j < 2; j++) {
  862. if (!(TEST_true(BN_GF2m_mod_div(d, a, c, b[j], ctx))
  863. && TEST_true(BN_GF2m_mod_mul(e, d, c, b[j], ctx))
  864. && TEST_true(BN_GF2m_mod_div(f, a, e, b[j], ctx))
  865. /* Test that ((a/c)*c)/a = 1. */
  866. && TEST_BN_eq_one(f)))
  867. goto err;
  868. }
  869. }
  870. st = 1;
  871. err:
  872. BN_free(a);
  873. BN_free(b[0]);
  874. BN_free(b[1]);
  875. BN_free(c);
  876. BN_free(d);
  877. BN_free(e);
  878. BN_free(f);
  879. return st;
  880. }
  881. static int test_gf2m_modexp(void)
  882. {
  883. BIGNUM *a = NULL, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
  884. BIGNUM *e = NULL, *f = NULL;
  885. int i, j, st = 0;
  886. if (!TEST_ptr(a = BN_new())
  887. || !TEST_ptr(b[0] = BN_new())
  888. || !TEST_ptr(b[1] = BN_new())
  889. || !TEST_ptr(c = BN_new())
  890. || !TEST_ptr(d = BN_new())
  891. || !TEST_ptr(e = BN_new())
  892. || !TEST_ptr(f = BN_new()))
  893. goto err;
  894. if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
  895. && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
  896. goto err;
  897. for (i = 0; i < NUM0; i++) {
  898. if (!(TEST_true(BN_bntest_rand(a, 512, 0, 0))
  899. && TEST_true(BN_bntest_rand(c, 512, 0, 0))
  900. && TEST_true(BN_bntest_rand(d, 512, 0, 0))))
  901. goto err;
  902. for (j = 0; j < 2; j++) {
  903. if (!(TEST_true(BN_GF2m_mod_exp(e, a, c, b[j], ctx))
  904. && TEST_true(BN_GF2m_mod_exp(f, a, d, b[j], ctx))
  905. && TEST_true(BN_GF2m_mod_mul(e, e, f, b[j], ctx))
  906. && TEST_true(BN_add(f, c, d))
  907. && TEST_true(BN_GF2m_mod_exp(f, a, f, b[j], ctx))
  908. && TEST_true(BN_GF2m_add(f, e, f))
  909. /* Test that a^(c+d)=a^c*a^d. */
  910. && TEST_BN_eq_zero(f)))
  911. goto err;
  912. }
  913. }
  914. st = 1;
  915. err:
  916. BN_free(a);
  917. BN_free(b[0]);
  918. BN_free(b[1]);
  919. BN_free(c);
  920. BN_free(d);
  921. BN_free(e);
  922. BN_free(f);
  923. return st;
  924. }
  925. static int test_gf2m_modsqrt(void)
  926. {
  927. BIGNUM *a = NULL, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
  928. BIGNUM *e = NULL, *f = NULL;
  929. int i, j, st = 0;
  930. if (!TEST_ptr(a = BN_new())
  931. || !TEST_ptr(b[0] = BN_new())
  932. || !TEST_ptr(b[1] = BN_new())
  933. || !TEST_ptr(c = BN_new())
  934. || !TEST_ptr(d = BN_new())
  935. || !TEST_ptr(e = BN_new())
  936. || !TEST_ptr(f = BN_new()))
  937. goto err;
  938. if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
  939. && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
  940. goto err;
  941. for (i = 0; i < NUM0; i++) {
  942. if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
  943. goto err;
  944. for (j = 0; j < 2; j++) {
  945. if (!(TEST_true(BN_GF2m_mod(c, a, b[j]))
  946. && TEST_true(BN_GF2m_mod_sqrt(d, a, b[j], ctx))
  947. && TEST_true(BN_GF2m_mod_sqr(e, d, b[j], ctx))
  948. && TEST_true(BN_GF2m_add(f, c, e))
  949. /* Test that d^2 = a, where d = sqrt(a). */
  950. && TEST_BN_eq_zero(f)))
  951. goto err;
  952. }
  953. }
  954. st = 1;
  955. err:
  956. BN_free(a);
  957. BN_free(b[0]);
  958. BN_free(b[1]);
  959. BN_free(c);
  960. BN_free(d);
  961. BN_free(e);
  962. BN_free(f);
  963. return st;
  964. }
  965. static int test_gf2m_modsolvequad(void)
  966. {
  967. BIGNUM *a = NULL, *b[2] = {NULL, NULL}, *c = NULL, *d = NULL;
  968. BIGNUM *e = NULL;
  969. int i, j, s = 0, t, st = 0;
  970. if (!TEST_ptr(a = BN_new())
  971. || !TEST_ptr(b[0] = BN_new())
  972. || !TEST_ptr(b[1] = BN_new())
  973. || !TEST_ptr(c = BN_new())
  974. || !TEST_ptr(d = BN_new())
  975. || !TEST_ptr(e = BN_new()))
  976. goto err;
  977. if (!(TEST_true(BN_GF2m_arr2poly(p0, b[0]))
  978. && TEST_true(BN_GF2m_arr2poly(p1, b[1]))))
  979. goto err;
  980. for (i = 0; i < NUM0; i++) {
  981. if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
  982. goto err;
  983. for (j = 0; j < 2; j++) {
  984. t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx);
  985. if (t) {
  986. s++;
  987. if (!(TEST_true(BN_GF2m_mod_sqr(d, c, b[j], ctx))
  988. && TEST_true(BN_GF2m_add(d, c, d))
  989. && TEST_true(BN_GF2m_mod(e, a, b[j]))
  990. && TEST_true(BN_GF2m_add(e, e, d))
  991. /*
  992. * Test that solution of quadratic c
  993. * satisfies c^2 + c = a.
  994. */
  995. && TEST_BN_eq_zero(e)))
  996. goto err;
  997. }
  998. }
  999. }
  1000. if (!TEST_int_ge(s, 0)) {
  1001. TEST_info("%d tests found no roots; probably an error", NUM0);
  1002. goto err;
  1003. }
  1004. st = 1;
  1005. err:
  1006. BN_free(a);
  1007. BN_free(b[0]);
  1008. BN_free(b[1]);
  1009. BN_free(c);
  1010. BN_free(d);
  1011. BN_free(e);
  1012. return st;
  1013. }
  1014. #endif
  1015. static int test_kronecker(void)
  1016. {
  1017. BIGNUM *a = NULL, *b = NULL, *r = NULL, *t = NULL;
  1018. int i, legendre, kronecker, st = 0;
  1019. if (!TEST_ptr(a = BN_new())
  1020. || !TEST_ptr(b = BN_new())
  1021. || !TEST_ptr(r = BN_new())
  1022. || !TEST_ptr(t = BN_new()))
  1023. goto err;
  1024. /*
  1025. * We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol). In
  1026. * this case we know that if b is prime, then BN_kronecker(a, b, ctx) is
  1027. * congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol). So we
  1028. * generate a random prime b and compare these values for a number of
  1029. * random a's. (That is, we run the Solovay-Strassen primality test to
  1030. * confirm that b is prime, except that we don't want to test whether b
  1031. * is prime but whether BN_kronecker works.)
  1032. */
  1033. if (!TEST_true(BN_generate_prime_ex(b, 512, 0, NULL, NULL, NULL)))
  1034. goto err;
  1035. BN_set_negative(b, rand_neg());
  1036. for (i = 0; i < NUM0; i++) {
  1037. if (!TEST_true(BN_bntest_rand(a, 512, 0, 0)))
  1038. goto err;
  1039. BN_set_negative(a, rand_neg());
  1040. /* t := (|b|-1)/2 (note that b is odd) */
  1041. if (!TEST_true(BN_copy(t, b)))
  1042. goto err;
  1043. BN_set_negative(t, 0);
  1044. if (!TEST_true(BN_sub_word(t, 1)))
  1045. goto err;
  1046. if (!TEST_true(BN_rshift1(t, t)))
  1047. goto err;
  1048. /* r := a^t mod b */
  1049. BN_set_negative(b, 0);
  1050. if (!TEST_true(BN_mod_exp_recp(r, a, t, b, ctx)))
  1051. goto err;
  1052. BN_set_negative(b, 1);
  1053. if (BN_is_word(r, 1))
  1054. legendre = 1;
  1055. else if (BN_is_zero(r))
  1056. legendre = 0;
  1057. else {
  1058. if (!TEST_true(BN_add_word(r, 1)))
  1059. goto err;
  1060. if (!TEST_int_eq(BN_ucmp(r, b), 0)) {
  1061. TEST_info("Legendre symbol computation failed");
  1062. goto err;
  1063. }
  1064. legendre = -1;
  1065. }
  1066. if (!TEST_int_ge(kronecker = BN_kronecker(a, b, ctx), -1))
  1067. goto err;
  1068. /* we actually need BN_kronecker(a, |b|) */
  1069. if (BN_is_negative(a) && BN_is_negative(b))
  1070. kronecker = -kronecker;
  1071. if (!TEST_int_eq(legendre, kronecker))
  1072. goto err;
  1073. }
  1074. st = 1;
  1075. err:
  1076. BN_free(a);
  1077. BN_free(b);
  1078. BN_free(r);
  1079. BN_free(t);
  1080. return st;
  1081. }
  1082. static int file_sum(STANZA *s)
  1083. {
  1084. BIGNUM *a = NULL, *b = NULL, *sum = NULL, *ret = NULL;
  1085. BN_ULONG b_word;
  1086. int st = 0;
  1087. if (!TEST_ptr(a = getBN(s, "A"))
  1088. || !TEST_ptr(b = getBN(s, "B"))
  1089. || !TEST_ptr(sum = getBN(s, "Sum"))
  1090. || !TEST_ptr(ret = BN_new()))
  1091. goto err;
  1092. if (!TEST_true(BN_add(ret, a, b))
  1093. || !equalBN("A + B", sum, ret)
  1094. || !TEST_true(BN_sub(ret, sum, a))
  1095. || !equalBN("Sum - A", b, ret)
  1096. || !TEST_true(BN_sub(ret, sum, b))
  1097. || !equalBN("Sum - B", a, ret))
  1098. goto err;
  1099. /*
  1100. * Test that the functions work when |r| and |a| point to the same BIGNUM,
  1101. * or when |r| and |b| point to the same BIGNUM.
  1102. * There is no test for all of |r|, |a|, and |b| pointint to the same BIGNUM.
  1103. */
  1104. if (!TEST_true(BN_copy(ret, a))
  1105. || !TEST_true(BN_add(ret, ret, b))
  1106. || !equalBN("A + B (r is a)", sum, ret)
  1107. || !TEST_true(BN_copy(ret, b))
  1108. || !TEST_true(BN_add(ret, a, ret))
  1109. || !equalBN("A + B (r is b)", sum, ret)
  1110. || !TEST_true(BN_copy(ret, sum))
  1111. || !TEST_true(BN_sub(ret, ret, a))
  1112. || !equalBN("Sum - A (r is a)", b, ret)
  1113. || !TEST_true(BN_copy(ret, a))
  1114. || !TEST_true(BN_sub(ret, sum, ret))
  1115. || !equalBN("Sum - A (r is b)", b, ret)
  1116. || !TEST_true(BN_copy(ret, sum))
  1117. || !TEST_true(BN_sub(ret, ret, b))
  1118. || !equalBN("Sum - B (r is a)", a, ret)
  1119. || !TEST_true(BN_copy(ret, b))
  1120. || !TEST_true(BN_sub(ret, sum, ret))
  1121. || !equalBN("Sum - B (r is b)", a, ret))
  1122. goto err;
  1123. /*
  1124. * Test BN_uadd() and BN_usub() with the prerequisites they are
  1125. * documented as having. Note that these functions are frequently used
  1126. * when the prerequisites don't hold. In those cases, they are supposed
  1127. * to work as if the prerequisite hold, but we don't test that yet.
  1128. */
  1129. if (!BN_is_negative(a) && !BN_is_negative(b) && BN_cmp(a, b) >= 0) {
  1130. if (!TEST_true(BN_uadd(ret, a, b))
  1131. || !equalBN("A +u B", sum, ret)
  1132. || !TEST_true(BN_usub(ret, sum, a))
  1133. || !equalBN("Sum -u A", b, ret)
  1134. || !TEST_true(BN_usub(ret, sum, b))
  1135. || !equalBN("Sum -u B", a, ret))
  1136. goto err;
  1137. /*
  1138. * Test that the functions work when |r| and |a| point to the same
  1139. * BIGNUM, or when |r| and |b| point to the same BIGNUM.
  1140. * There is no test for all of |r|, |a|, and |b| pointint to the same
  1141. * BIGNUM.
  1142. */
  1143. if (!TEST_true(BN_copy(ret, a))
  1144. || !TEST_true(BN_uadd(ret, ret, b))
  1145. || !equalBN("A +u B (r is a)", sum, ret)
  1146. || !TEST_true(BN_copy(ret, b))
  1147. || !TEST_true(BN_uadd(ret, a, ret))
  1148. || !equalBN("A +u B (r is b)", sum, ret)
  1149. || !TEST_true(BN_copy(ret, sum))
  1150. || !TEST_true(BN_usub(ret, ret, a))
  1151. || !equalBN("Sum -u A (r is a)", b, ret)
  1152. || !TEST_true(BN_copy(ret, a))
  1153. || !TEST_true(BN_usub(ret, sum, ret))
  1154. || !equalBN("Sum -u A (r is b)", b, ret)
  1155. || !TEST_true(BN_copy(ret, sum))
  1156. || !TEST_true(BN_usub(ret, ret, b))
  1157. || !equalBN("Sum -u B (r is a)", a, ret)
  1158. || !TEST_true(BN_copy(ret, b))
  1159. || !TEST_true(BN_usub(ret, sum, ret))
  1160. || !equalBN("Sum -u B (r is b)", a, ret))
  1161. goto err;
  1162. }
  1163. /*
  1164. * Test with BN_add_word() and BN_sub_word() if |b| is small enough.
  1165. */
  1166. b_word = BN_get_word(b);
  1167. if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
  1168. if (!TEST_true(BN_copy(ret, a))
  1169. || !TEST_true(BN_add_word(ret, b_word))
  1170. || !equalBN("A + B (word)", sum, ret)
  1171. || !TEST_true(BN_copy(ret, sum))
  1172. || !TEST_true(BN_sub_word(ret, b_word))
  1173. || !equalBN("Sum - B (word)", a, ret))
  1174. goto err;
  1175. }
  1176. st = 1;
  1177. err:
  1178. BN_free(a);
  1179. BN_free(b);
  1180. BN_free(sum);
  1181. BN_free(ret);
  1182. return st;
  1183. }
  1184. static int file_lshift1(STANZA *s)
  1185. {
  1186. BIGNUM *a = NULL, *lshift1 = NULL, *zero = NULL, *ret = NULL;
  1187. BIGNUM *two = NULL, *remainder = NULL;
  1188. int st = 0;
  1189. if (!TEST_ptr(a = getBN(s, "A"))
  1190. || !TEST_ptr(lshift1 = getBN(s, "LShift1"))
  1191. || !TEST_ptr(zero = BN_new())
  1192. || !TEST_ptr(ret = BN_new())
  1193. || !TEST_ptr(two = BN_new())
  1194. || !TEST_ptr(remainder = BN_new()))
  1195. goto err;
  1196. BN_zero(zero);
  1197. if (!TEST_true(BN_set_word(two, 2))
  1198. || !TEST_true(BN_add(ret, a, a))
  1199. || !equalBN("A + A", lshift1, ret)
  1200. || !TEST_true(BN_mul(ret, a, two, ctx))
  1201. || !equalBN("A * 2", lshift1, ret)
  1202. || !TEST_true(BN_div(ret, remainder, lshift1, two, ctx))
  1203. || !equalBN("LShift1 / 2", a, ret)
  1204. || !equalBN("LShift1 % 2", zero, remainder)
  1205. || !TEST_true(BN_lshift1(ret, a))
  1206. || !equalBN("A << 1", lshift1, ret)
  1207. || !TEST_true(BN_rshift1(ret, lshift1))
  1208. || !equalBN("LShift >> 1", a, ret)
  1209. || !TEST_true(BN_rshift1(ret, lshift1))
  1210. || !equalBN("LShift >> 1", a, ret))
  1211. goto err;
  1212. /* Set the LSB to 1 and test rshift1 again. */
  1213. if (!TEST_true(BN_set_bit(lshift1, 0))
  1214. || !TEST_true(BN_div(ret, NULL /* rem */ , lshift1, two, ctx))
  1215. || !equalBN("(LShift1 | 1) / 2", a, ret)
  1216. || !TEST_true(BN_rshift1(ret, lshift1))
  1217. || !equalBN("(LShift | 1) >> 1", a, ret))
  1218. goto err;
  1219. st = 1;
  1220. err:
  1221. BN_free(a);
  1222. BN_free(lshift1);
  1223. BN_free(zero);
  1224. BN_free(ret);
  1225. BN_free(two);
  1226. BN_free(remainder);
  1227. return st;
  1228. }
  1229. static int file_lshift(STANZA *s)
  1230. {
  1231. BIGNUM *a = NULL, *lshift = NULL, *ret = NULL;
  1232. int n = 0, st = 0;
  1233. if (!TEST_ptr(a = getBN(s, "A"))
  1234. || !TEST_ptr(lshift = getBN(s, "LShift"))
  1235. || !TEST_ptr(ret = BN_new())
  1236. || !getint(s, &n, "N"))
  1237. goto err;
  1238. if (!TEST_true(BN_lshift(ret, a, n))
  1239. || !equalBN("A << N", lshift, ret)
  1240. || !TEST_true(BN_rshift(ret, lshift, n))
  1241. || !equalBN("A >> N", a, ret))
  1242. goto err;
  1243. st = 1;
  1244. err:
  1245. BN_free(a);
  1246. BN_free(lshift);
  1247. BN_free(ret);
  1248. return st;
  1249. }
  1250. static int file_rshift(STANZA *s)
  1251. {
  1252. BIGNUM *a = NULL, *rshift = NULL, *ret = NULL;
  1253. int n = 0, st = 0;
  1254. if (!TEST_ptr(a = getBN(s, "A"))
  1255. || !TEST_ptr(rshift = getBN(s, "RShift"))
  1256. || !TEST_ptr(ret = BN_new())
  1257. || !getint(s, &n, "N"))
  1258. goto err;
  1259. if (!TEST_true(BN_rshift(ret, a, n))
  1260. || !equalBN("A >> N", rshift, ret))
  1261. goto err;
  1262. /* If N == 1, try with rshift1 as well */
  1263. if (n == 1) {
  1264. if (!TEST_true(BN_rshift1(ret, a))
  1265. || !equalBN("A >> 1 (rshift1)", rshift, ret))
  1266. goto err;
  1267. }
  1268. st = 1;
  1269. err:
  1270. BN_free(a);
  1271. BN_free(rshift);
  1272. BN_free(ret);
  1273. return st;
  1274. }
  1275. static int file_square(STANZA *s)
  1276. {
  1277. BIGNUM *a = NULL, *square = NULL, *zero = NULL, *ret = NULL;
  1278. BIGNUM *remainder = NULL, *tmp = NULL;
  1279. int st = 0;
  1280. if (!TEST_ptr(a = getBN(s, "A"))
  1281. || !TEST_ptr(square = getBN(s, "Square"))
  1282. || !TEST_ptr(zero = BN_new())
  1283. || !TEST_ptr(ret = BN_new())
  1284. || !TEST_ptr(remainder = BN_new()))
  1285. goto err;
  1286. BN_zero(zero);
  1287. if (!TEST_true(BN_sqr(ret, a, ctx))
  1288. || !equalBN("A^2", square, ret)
  1289. || !TEST_true(BN_mul(ret, a, a, ctx))
  1290. || !equalBN("A * A", square, ret)
  1291. || !TEST_true(BN_div(ret, remainder, square, a, ctx))
  1292. || !equalBN("Square / A", a, ret)
  1293. || !equalBN("Square % A", zero, remainder))
  1294. goto err;
  1295. #if HAVE_BN_SQRT
  1296. BN_set_negative(a, 0);
  1297. if (!TEST_true(BN_sqrt(ret, square, ctx))
  1298. || !equalBN("sqrt(Square)", a, ret))
  1299. goto err;
  1300. /* BN_sqrt should fail on non-squares and negative numbers. */
  1301. if (!TEST_BN_eq_zero(square)) {
  1302. if (!TEST_ptr(tmp = BN_new())
  1303. || !TEST_true(BN_copy(tmp, square)))
  1304. goto err;
  1305. BN_set_negative(tmp, 1);
  1306. if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx), 0))
  1307. goto err;
  1308. ERR_clear_error();
  1309. BN_set_negative(tmp, 0);
  1310. if (BN_add(tmp, tmp, BN_value_one()))
  1311. goto err;
  1312. if (!TEST_int_eq(BN_sqrt(ret, tmp, ctx)))
  1313. goto err;
  1314. ERR_clear_error();
  1315. }
  1316. #endif
  1317. st = 1;
  1318. err:
  1319. BN_free(a);
  1320. BN_free(square);
  1321. BN_free(zero);
  1322. BN_free(ret);
  1323. BN_free(remainder);
  1324. BN_free(tmp);
  1325. return st;
  1326. }
  1327. static int file_product(STANZA *s)
  1328. {
  1329. BIGNUM *a = NULL, *b = NULL, *product = NULL, *ret = NULL;
  1330. BIGNUM *remainder = NULL, *zero = NULL;
  1331. int st = 0;
  1332. if (!TEST_ptr(a = getBN(s, "A"))
  1333. || !TEST_ptr(b = getBN(s, "B"))
  1334. || !TEST_ptr(product = getBN(s, "Product"))
  1335. || !TEST_ptr(ret = BN_new())
  1336. || !TEST_ptr(remainder = BN_new())
  1337. || !TEST_ptr(zero = BN_new()))
  1338. goto err;
  1339. BN_zero(zero);
  1340. if (!TEST_true(BN_mul(ret, a, b, ctx))
  1341. || !equalBN("A * B", product, ret)
  1342. || !TEST_true(BN_div(ret, remainder, product, a, ctx))
  1343. || !equalBN("Product / A", b, ret)
  1344. || !equalBN("Product % A", zero, remainder)
  1345. || !TEST_true(BN_div(ret, remainder, product, b, ctx))
  1346. || !equalBN("Product / B", a, ret)
  1347. || !equalBN("Product % B", zero, remainder))
  1348. goto err;
  1349. st = 1;
  1350. err:
  1351. BN_free(a);
  1352. BN_free(b);
  1353. BN_free(product);
  1354. BN_free(ret);
  1355. BN_free(remainder);
  1356. BN_free(zero);
  1357. return st;
  1358. }
  1359. static int file_quotient(STANZA *s)
  1360. {
  1361. BIGNUM *a = NULL, *b = NULL, *quotient = NULL, *remainder = NULL;
  1362. BIGNUM *ret = NULL, *ret2 = NULL, *nnmod = NULL;
  1363. BN_ULONG b_word, ret_word;
  1364. int st = 0;
  1365. if (!TEST_ptr(a = getBN(s, "A"))
  1366. || !TEST_ptr(b = getBN(s, "B"))
  1367. || !TEST_ptr(quotient = getBN(s, "Quotient"))
  1368. || !TEST_ptr(remainder = getBN(s, "Remainder"))
  1369. || !TEST_ptr(ret = BN_new())
  1370. || !TEST_ptr(ret2 = BN_new())
  1371. || !TEST_ptr(nnmod = BN_new()))
  1372. goto err;
  1373. if (!TEST_true(BN_div(ret, ret2, a, b, ctx))
  1374. || !equalBN("A / B", quotient, ret)
  1375. || !equalBN("A % B", remainder, ret2)
  1376. || !TEST_true(BN_mul(ret, quotient, b, ctx))
  1377. || !TEST_true(BN_add(ret, ret, remainder))
  1378. || !equalBN("Quotient * B + Remainder", a, ret))
  1379. goto err;
  1380. /*
  1381. * Test with BN_mod_word() and BN_div_word() if the divisor is
  1382. * small enough.
  1383. */
  1384. b_word = BN_get_word(b);
  1385. if (!BN_is_negative(b) && b_word != (BN_ULONG)-1) {
  1386. BN_ULONG remainder_word = BN_get_word(remainder);
  1387. assert(remainder_word != (BN_ULONG)-1);
  1388. if (!TEST_ptr(BN_copy(ret, a)))
  1389. goto err;
  1390. ret_word = BN_div_word(ret, b_word);
  1391. if (ret_word != remainder_word) {
  1392. #ifdef BN_DEC_FMT1
  1393. TEST_error(
  1394. "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1,
  1395. ret_word, remainder_word);
  1396. #else
  1397. TEST_error("Got A %% B (word) mismatch");
  1398. #endif
  1399. goto err;
  1400. }
  1401. if (!equalBN ("A / B (word)", quotient, ret))
  1402. goto err;
  1403. ret_word = BN_mod_word(a, b_word);
  1404. if (ret_word != remainder_word) {
  1405. #ifdef BN_DEC_FMT1
  1406. TEST_error(
  1407. "Got A %% B (word) = " BN_DEC_FMT1 ", wanted " BN_DEC_FMT1 "",
  1408. ret_word, remainder_word);
  1409. #else
  1410. TEST_error("Got A %% B (word) mismatch");
  1411. #endif
  1412. goto err;
  1413. }
  1414. }
  1415. /* Test BN_nnmod. */
  1416. if (!BN_is_negative(b)) {
  1417. if (!TEST_true(BN_copy(nnmod, remainder))
  1418. || (BN_is_negative(nnmod)
  1419. && !TEST_true(BN_add(nnmod, nnmod, b)))
  1420. || !TEST_true(BN_nnmod(ret, a, b, ctx))
  1421. || !equalBN("A % B (non-negative)", nnmod, ret))
  1422. goto err;
  1423. }
  1424. st = 1;
  1425. err:
  1426. BN_free(a);
  1427. BN_free(b);
  1428. BN_free(quotient);
  1429. BN_free(remainder);
  1430. BN_free(ret);
  1431. BN_free(ret2);
  1432. BN_free(nnmod);
  1433. return st;
  1434. }
  1435. static int file_modmul(STANZA *s)
  1436. {
  1437. BIGNUM *a = NULL, *b = NULL, *m = NULL, *mod_mul = NULL, *ret = NULL;
  1438. int st = 0;
  1439. if (!TEST_ptr(a = getBN(s, "A"))
  1440. || !TEST_ptr(b = getBN(s, "B"))
  1441. || !TEST_ptr(m = getBN(s, "M"))
  1442. || !TEST_ptr(mod_mul = getBN(s, "ModMul"))
  1443. || !TEST_ptr(ret = BN_new()))
  1444. goto err;
  1445. if (!TEST_true(BN_mod_mul(ret, a, b, m, ctx))
  1446. || !equalBN("A * B (mod M)", mod_mul, ret))
  1447. goto err;
  1448. if (BN_is_odd(m)) {
  1449. /* Reduce |a| and |b| and test the Montgomery version. */
  1450. BN_MONT_CTX *mont = BN_MONT_CTX_new();
  1451. BIGNUM *a_tmp = BN_new();
  1452. BIGNUM *b_tmp = BN_new();
  1453. if (mont == NULL || a_tmp == NULL || b_tmp == NULL
  1454. || !TEST_true(BN_MONT_CTX_set(mont, m, ctx))
  1455. || !TEST_true(BN_nnmod(a_tmp, a, m, ctx))
  1456. || !TEST_true(BN_nnmod(b_tmp, b, m, ctx))
  1457. || !TEST_true(BN_to_montgomery(a_tmp, a_tmp, mont, ctx))
  1458. || !TEST_true(BN_to_montgomery(b_tmp, b_tmp, mont, ctx))
  1459. || !TEST_true(BN_mod_mul_montgomery(ret, a_tmp, b_tmp,
  1460. mont, ctx))
  1461. || !TEST_true(BN_from_montgomery(ret, ret, mont, ctx))
  1462. || !equalBN("A * B (mod M) (mont)", mod_mul, ret))
  1463. st = 0;
  1464. else
  1465. st = 1;
  1466. BN_MONT_CTX_free(mont);
  1467. BN_free(a_tmp);
  1468. BN_free(b_tmp);
  1469. if (st == 0)
  1470. goto err;
  1471. }
  1472. st = 1;
  1473. err:
  1474. BN_free(a);
  1475. BN_free(b);
  1476. BN_free(m);
  1477. BN_free(mod_mul);
  1478. BN_free(ret);
  1479. return st;
  1480. }
  1481. static int file_modexp(STANZA *s)
  1482. {
  1483. BIGNUM *a = NULL, *e = NULL, *m = NULL, *mod_exp = NULL, *ret = NULL;
  1484. BIGNUM *b = NULL, *c = NULL, *d = NULL;
  1485. int st = 0;
  1486. if (!TEST_ptr(a = getBN(s, "A"))
  1487. || !TEST_ptr(e = getBN(s, "E"))
  1488. || !TEST_ptr(m = getBN(s, "M"))
  1489. || !TEST_ptr(mod_exp = getBN(s, "ModExp"))
  1490. || !TEST_ptr(ret = BN_new())
  1491. || !TEST_ptr(d = BN_new()))
  1492. goto err;
  1493. if (!TEST_true(BN_mod_exp(ret, a, e, m, ctx))
  1494. || !equalBN("A ^ E (mod M)", mod_exp, ret))
  1495. goto err;
  1496. if (BN_is_odd(m)) {
  1497. if (!TEST_true(BN_mod_exp_mont(ret, a, e, m, ctx, NULL))
  1498. || !equalBN("A ^ E (mod M) (mont)", mod_exp, ret)
  1499. || !TEST_true(BN_mod_exp_mont_consttime(ret, a, e, m,
  1500. ctx, NULL))
  1501. || !equalBN("A ^ E (mod M) (mont const", mod_exp, ret))
  1502. goto err;
  1503. }
  1504. /* Regression test for carry propagation bug in sqr8x_reduction */
  1505. BN_hex2bn(&a, "050505050505");
  1506. BN_hex2bn(&b, "02");
  1507. BN_hex2bn(&c,
  1508. "4141414141414141414141274141414141414141414141414141414141414141"
  1509. "4141414141414141414141414141414141414141414141414141414141414141"
  1510. "4141414141414141414141800000000000000000000000000000000000000000"
  1511. "0000000000000000000000000000000000000000000000000000000000000000"
  1512. "0000000000000000000000000000000000000000000000000000000000000000"
  1513. "0000000000000000000000000000000000000000000000000000000001");
  1514. if (!TEST_true(BN_mod_exp(d, a, b, c, ctx))
  1515. || !TEST_true(BN_mul(e, a, a, ctx))
  1516. || !TEST_BN_eq(d, e))
  1517. goto err;
  1518. st = 1;
  1519. err:
  1520. BN_free(a);
  1521. BN_free(b);
  1522. BN_free(c);
  1523. BN_free(d);
  1524. BN_free(e);
  1525. BN_free(m);
  1526. BN_free(mod_exp);
  1527. BN_free(ret);
  1528. return st;
  1529. }
  1530. static int file_exp(STANZA *s)
  1531. {
  1532. BIGNUM *a = NULL, *e = NULL, *exp = NULL, *ret = NULL;
  1533. int st = 0;
  1534. if (!TEST_ptr(a = getBN(s, "A"))
  1535. || !TEST_ptr(e = getBN(s, "E"))
  1536. || !TEST_ptr(exp = getBN(s, "Exp"))
  1537. || !TEST_ptr(ret = BN_new()))
  1538. goto err;
  1539. if (!TEST_true(BN_exp(ret, a, e, ctx))
  1540. || !equalBN("A ^ E", exp, ret))
  1541. goto err;
  1542. st = 1;
  1543. err:
  1544. BN_free(a);
  1545. BN_free(e);
  1546. BN_free(exp);
  1547. BN_free(ret);
  1548. return st;
  1549. }
  1550. static int file_modsqrt(STANZA *s)
  1551. {
  1552. BIGNUM *a = NULL, *p = NULL, *mod_sqrt = NULL, *ret = NULL, *ret2 = NULL;
  1553. int st = 0;
  1554. if (!TEST_ptr(a = getBN(s, "A"))
  1555. || !TEST_ptr(p = getBN(s, "P"))
  1556. || !TEST_ptr(mod_sqrt = getBN(s, "ModSqrt"))
  1557. || !TEST_ptr(ret = BN_new())
  1558. || !TEST_ptr(ret2 = BN_new()))
  1559. goto err;
  1560. if (BN_is_negative(mod_sqrt)) {
  1561. /* A negative testcase */
  1562. if (!TEST_ptr_null(BN_mod_sqrt(ret, a, p, ctx)))
  1563. goto err;
  1564. st = 1;
  1565. goto err;
  1566. }
  1567. /* There are two possible answers. */
  1568. if (!TEST_ptr(BN_mod_sqrt(ret, a, p, ctx))
  1569. || !TEST_true(BN_sub(ret2, p, ret)))
  1570. goto err;
  1571. /* The first condition should NOT be a test. */
  1572. if (BN_cmp(ret2, mod_sqrt) != 0
  1573. && !equalBN("sqrt(A) (mod P)", mod_sqrt, ret))
  1574. goto err;
  1575. st = 1;
  1576. err:
  1577. BN_free(a);
  1578. BN_free(p);
  1579. BN_free(mod_sqrt);
  1580. BN_free(ret);
  1581. BN_free(ret2);
  1582. return st;
  1583. }
  1584. static int file_gcd(STANZA *s)
  1585. {
  1586. BIGNUM *a = NULL, *b = NULL, *gcd = NULL, *ret = NULL;
  1587. int st = 0;
  1588. if (!TEST_ptr(a = getBN(s, "A"))
  1589. || !TEST_ptr(b = getBN(s, "B"))
  1590. || !TEST_ptr(gcd = getBN(s, "GCD"))
  1591. || !TEST_ptr(ret = BN_new()))
  1592. goto err;
  1593. if (!TEST_true(BN_gcd(ret, a, b, ctx))
  1594. || !equalBN("gcd(A,B)", gcd, ret))
  1595. goto err;
  1596. st = 1;
  1597. err:
  1598. BN_free(a);
  1599. BN_free(b);
  1600. BN_free(gcd);
  1601. BN_free(ret);
  1602. return st;
  1603. }
  1604. static int test_bn2padded(void)
  1605. {
  1606. uint8_t zeros[256], out[256], reference[128];
  1607. size_t bytes;
  1608. BIGNUM *n;
  1609. int st = 0;
  1610. /* Test edge case at 0. */
  1611. if (!TEST_ptr((n = BN_new())))
  1612. goto err;
  1613. if (!TEST_int_eq(BN_bn2binpad(n, NULL, 0), 0))
  1614. goto err;
  1615. memset(out, -1, sizeof(out));
  1616. if (!TEST_int_eq(BN_bn2binpad(n, out, sizeof(out)), sizeof(out)))
  1617. goto err;
  1618. memset(zeros, 0, sizeof(zeros));
  1619. if (!TEST_mem_eq(zeros, sizeof(zeros), out, sizeof(out)))
  1620. goto err;
  1621. /* Test a random numbers at various byte lengths. */
  1622. for (bytes = 128 - 7; bytes <= 128; bytes++) {
  1623. # define TOP_BIT_ON 0
  1624. # define BOTTOM_BIT_NOTOUCH 0
  1625. if (!TEST_true(BN_rand(n, bytes * 8, TOP_BIT_ON, BOTTOM_BIT_NOTOUCH)))
  1626. goto err;
  1627. if (!TEST_int_eq(BN_num_bytes(n), bytes)
  1628. || !TEST_int_eq(BN_bn2bin(n, reference), bytes))
  1629. goto err;
  1630. /* Empty buffer should fail. */
  1631. if (!TEST_int_eq(BN_bn2binpad(n, NULL, 0), -1))
  1632. goto err;
  1633. /* One byte short should fail. */
  1634. if (!TEST_int_eq(BN_bn2binpad(n, out, bytes - 1), -1))
  1635. goto err;
  1636. /* Exactly right size should encode. */
  1637. if (!TEST_int_eq(BN_bn2binpad(n, out, bytes), bytes)
  1638. || !TEST_mem_eq(out, bytes, reference, bytes))
  1639. goto err;
  1640. /* Pad up one byte extra. */
  1641. if (!TEST_int_eq(BN_bn2binpad(n, out, bytes + 1), bytes + 1)
  1642. || !TEST_mem_eq(out + 1, bytes, reference, bytes)
  1643. || !TEST_mem_eq(out, 1, zeros, 1))
  1644. goto err;
  1645. /* Pad up to 256. */
  1646. if (!TEST_int_eq(BN_bn2binpad(n, out, sizeof(out)), sizeof(out))
  1647. || !TEST_mem_eq(out + sizeof(out) - bytes, bytes,
  1648. reference, bytes)
  1649. || !TEST_mem_eq(out, sizeof(out) - bytes,
  1650. zeros, sizeof(out) - bytes))
  1651. goto err;
  1652. }
  1653. st = 1;
  1654. err:
  1655. BN_free(n);
  1656. return st;
  1657. }
  1658. static const MPITEST kSignedTests_BE[] = {
  1659. {"-1", "\xff", 1},
  1660. {"0", "", 0},
  1661. {"1", "\x01", 1},
  1662. /*
  1663. * The above cover the basics, now let's go for possible bignum
  1664. * chunk edges and other word edges (for a broad definition of
  1665. * "word", i.e. 1 byte included).
  1666. */
  1667. /* 1 byte edge */
  1668. {"127", "\x7f", 1},
  1669. {"-127", "\x81", 1},
  1670. {"128", "\x00\x80", 2},
  1671. {"-128", "\x80", 1},
  1672. {"129", "\x00\x81", 2},
  1673. {"-129", "\xff\x7f", 2},
  1674. {"255", "\x00\xff", 2},
  1675. {"-255", "\xff\x01", 2},
  1676. {"256", "\x01\x00", 2},
  1677. {"-256", "\xff\x00", 2},
  1678. /* 2 byte edge */
  1679. {"32767", "\x7f\xff", 2},
  1680. {"-32767", "\x80\x01", 2},
  1681. {"32768", "\x00\x80\x00", 3},
  1682. {"-32768", "\x80\x00", 2},
  1683. {"32769", "\x00\x80\x01", 3},
  1684. {"-32769", "\xff\x7f\xff", 3},
  1685. {"65535", "\x00\xff\xff", 3},
  1686. {"-65535", "\xff\x00\x01", 3},
  1687. {"65536", "\x01\x00\x00", 3},
  1688. {"-65536", "\xff\x00\x00", 3},
  1689. /* 4 byte edge */
  1690. {"2147483647", "\x7f\xff\xff\xff", 4},
  1691. {"-2147483647", "\x80\x00\x00\x01", 4},
  1692. {"2147483648", "\x00\x80\x00\x00\x00", 5},
  1693. {"-2147483648", "\x80\x00\x00\x00", 4},
  1694. {"2147483649", "\x00\x80\x00\x00\x01", 5},
  1695. {"-2147483649", "\xff\x7f\xff\xff\xff", 5},
  1696. {"4294967295", "\x00\xff\xff\xff\xff", 5},
  1697. {"-4294967295", "\xff\x00\x00\x00\x01", 5},
  1698. {"4294967296", "\x01\x00\x00\x00\x00", 5},
  1699. {"-4294967296", "\xff\x00\x00\x00\x00", 5},
  1700. /* 8 byte edge */
  1701. {"9223372036854775807", "\x7f\xff\xff\xff\xff\xff\xff\xff", 8},
  1702. {"-9223372036854775807", "\x80\x00\x00\x00\x00\x00\x00\x01", 8},
  1703. {"9223372036854775808", "\x00\x80\x00\x00\x00\x00\x00\x00\x00", 9},
  1704. {"-9223372036854775808", "\x80\x00\x00\x00\x00\x00\x00\x00", 8},
  1705. {"9223372036854775809", "\x00\x80\x00\x00\x00\x00\x00\x00\x01", 9},
  1706. {"-9223372036854775809", "\xff\x7f\xff\xff\xff\xff\xff\xff\xff", 9},
  1707. {"18446744073709551615", "\x00\xff\xff\xff\xff\xff\xff\xff\xff", 9},
  1708. {"-18446744073709551615", "\xff\x00\x00\x00\x00\x00\x00\x00\x01", 9},
  1709. {"18446744073709551616", "\x01\x00\x00\x00\x00\x00\x00\x00\x00", 9},
  1710. {"-18446744073709551616", "\xff\x00\x00\x00\x00\x00\x00\x00\x00", 9},
  1711. };
  1712. static int copy_reversed(uint8_t *dst, uint8_t *src, size_t len)
  1713. {
  1714. for (dst += len - 1; len > 0; src++, dst--, len--)
  1715. *dst = *src;
  1716. return 1;
  1717. }
  1718. static int test_bn2signed(int i)
  1719. {
  1720. uint8_t scratch[10], reversed[10];
  1721. const MPITEST *test = &kSignedTests_BE[i];
  1722. BIGNUM *bn = NULL, *bn2 = NULL;
  1723. int st = 0;
  1724. if (!TEST_ptr(bn = BN_new())
  1725. || !TEST_true(BN_asc2bn(&bn, test->base10)))
  1726. goto err;
  1727. /*
  1728. * Check BN_signed_bn2bin() / BN_signed_bin2bn()
  1729. * The interesting stuff happens in the last bytes of the buffers,
  1730. * the beginning is just padding (i.e. sign extension).
  1731. */
  1732. i = sizeof(scratch) - test->mpi_len;
  1733. if (!TEST_int_eq(BN_signed_bn2bin(bn, scratch, sizeof(scratch)),
  1734. sizeof(scratch))
  1735. || !TEST_true(copy_reversed(reversed, scratch, sizeof(scratch)))
  1736. || !TEST_mem_eq(test->mpi, test->mpi_len, scratch + i, test->mpi_len))
  1737. goto err;
  1738. if (!TEST_ptr(bn2 = BN_signed_bin2bn(scratch, sizeof(scratch), NULL))
  1739. || !TEST_BN_eq(bn, bn2))
  1740. goto err;
  1741. BN_free(bn2);
  1742. bn2 = NULL;
  1743. /* Check that a parse of the reversed buffer works too */
  1744. if (!TEST_ptr(bn2 = BN_signed_lebin2bn(reversed, sizeof(reversed), NULL))
  1745. || !TEST_BN_eq(bn, bn2))
  1746. goto err;
  1747. BN_free(bn2);
  1748. bn2 = NULL;
  1749. /*
  1750. * Check BN_signed_bn2lebin() / BN_signed_lebin2bn()
  1751. * The interesting stuff happens in the first bytes of the buffers,
  1752. * the end is just padding (i.e. sign extension).
  1753. */
  1754. i = sizeof(reversed) - test->mpi_len;
  1755. if (!TEST_int_eq(BN_signed_bn2lebin(bn, scratch, sizeof(scratch)),
  1756. sizeof(scratch))
  1757. || !TEST_true(copy_reversed(reversed, scratch, sizeof(scratch)))
  1758. || !TEST_mem_eq(test->mpi, test->mpi_len, reversed + i, test->mpi_len))
  1759. goto err;
  1760. if (!TEST_ptr(bn2 = BN_signed_lebin2bn(scratch, sizeof(scratch), NULL))
  1761. || !TEST_BN_eq(bn, bn2))
  1762. goto err;
  1763. BN_free(bn2);
  1764. bn2 = NULL;
  1765. /* Check that a parse of the reversed buffer works too */
  1766. if (!TEST_ptr(bn2 = BN_signed_bin2bn(reversed, sizeof(reversed), NULL))
  1767. || !TEST_BN_eq(bn, bn2))
  1768. goto err;
  1769. st = 1;
  1770. err:
  1771. BN_free(bn2);
  1772. BN_free(bn);
  1773. return st;
  1774. }
  1775. static int test_dec2bn(void)
  1776. {
  1777. BIGNUM *bn = NULL;
  1778. int st = 0;
  1779. if (!TEST_int_eq(parsedecBN(&bn, "0"), 1)
  1780. || !TEST_BN_eq_word(bn, 0)
  1781. || !TEST_BN_eq_zero(bn)
  1782. || !TEST_BN_le_zero(bn)
  1783. || !TEST_BN_ge_zero(bn)
  1784. || !TEST_BN_even(bn))
  1785. goto err;
  1786. BN_free(bn);
  1787. bn = NULL;
  1788. if (!TEST_int_eq(parsedecBN(&bn, "256"), 3)
  1789. || !TEST_BN_eq_word(bn, 256)
  1790. || !TEST_BN_ge_zero(bn)
  1791. || !TEST_BN_gt_zero(bn)
  1792. || !TEST_BN_ne_zero(bn)
  1793. || !TEST_BN_even(bn))
  1794. goto err;
  1795. BN_free(bn);
  1796. bn = NULL;
  1797. if (!TEST_int_eq(parsedecBN(&bn, "-42"), 3)
  1798. || !TEST_BN_abs_eq_word(bn, 42)
  1799. || !TEST_BN_lt_zero(bn)
  1800. || !TEST_BN_le_zero(bn)
  1801. || !TEST_BN_ne_zero(bn)
  1802. || !TEST_BN_even(bn))
  1803. goto err;
  1804. BN_free(bn);
  1805. bn = NULL;
  1806. if (!TEST_int_eq(parsedecBN(&bn, "1"), 1)
  1807. || !TEST_BN_eq_word(bn, 1)
  1808. || !TEST_BN_ne_zero(bn)
  1809. || !TEST_BN_gt_zero(bn)
  1810. || !TEST_BN_ge_zero(bn)
  1811. || !TEST_BN_eq_one(bn)
  1812. || !TEST_BN_odd(bn))
  1813. goto err;
  1814. BN_free(bn);
  1815. bn = NULL;
  1816. if (!TEST_int_eq(parsedecBN(&bn, "-0"), 2)
  1817. || !TEST_BN_eq_zero(bn)
  1818. || !TEST_BN_ge_zero(bn)
  1819. || !TEST_BN_le_zero(bn)
  1820. || !TEST_BN_even(bn))
  1821. goto err;
  1822. BN_free(bn);
  1823. bn = NULL;
  1824. if (!TEST_int_eq(parsedecBN(&bn, "42trailing garbage is ignored"), 2)
  1825. || !TEST_BN_abs_eq_word(bn, 42)
  1826. || !TEST_BN_ge_zero(bn)
  1827. || !TEST_BN_gt_zero(bn)
  1828. || !TEST_BN_ne_zero(bn)
  1829. || !TEST_BN_even(bn))
  1830. goto err;
  1831. st = 1;
  1832. err:
  1833. BN_free(bn);
  1834. return st;
  1835. }
  1836. static int test_hex2bn(void)
  1837. {
  1838. BIGNUM *bn = NULL;
  1839. int st = 0;
  1840. if (!TEST_int_eq(parseBN(&bn, "0"), 1)
  1841. || !TEST_BN_eq_zero(bn)
  1842. || !TEST_BN_ge_zero(bn)
  1843. || !TEST_BN_even(bn))
  1844. goto err;
  1845. BN_free(bn);
  1846. bn = NULL;
  1847. if (!TEST_int_eq(parseBN(&bn, "256"), 3)
  1848. || !TEST_BN_eq_word(bn, 0x256)
  1849. || !TEST_BN_ge_zero(bn)
  1850. || !TEST_BN_gt_zero(bn)
  1851. || !TEST_BN_ne_zero(bn)
  1852. || !TEST_BN_even(bn))
  1853. goto err;
  1854. BN_free(bn);
  1855. bn = NULL;
  1856. if (!TEST_int_eq(parseBN(&bn, "-42"), 3)
  1857. || !TEST_BN_abs_eq_word(bn, 0x42)
  1858. || !TEST_BN_lt_zero(bn)
  1859. || !TEST_BN_le_zero(bn)
  1860. || !TEST_BN_ne_zero(bn)
  1861. || !TEST_BN_even(bn))
  1862. goto err;
  1863. BN_free(bn);
  1864. bn = NULL;
  1865. if (!TEST_int_eq(parseBN(&bn, "cb"), 2)
  1866. || !TEST_BN_eq_word(bn, 0xCB)
  1867. || !TEST_BN_ge_zero(bn)
  1868. || !TEST_BN_gt_zero(bn)
  1869. || !TEST_BN_ne_zero(bn)
  1870. || !TEST_BN_odd(bn))
  1871. goto err;
  1872. BN_free(bn);
  1873. bn = NULL;
  1874. if (!TEST_int_eq(parseBN(&bn, "-0"), 2)
  1875. || !TEST_BN_eq_zero(bn)
  1876. || !TEST_BN_ge_zero(bn)
  1877. || !TEST_BN_le_zero(bn)
  1878. || !TEST_BN_even(bn))
  1879. goto err;
  1880. BN_free(bn);
  1881. bn = NULL;
  1882. if (!TEST_int_eq(parseBN(&bn, "abctrailing garbage is ignored"), 3)
  1883. || !TEST_BN_eq_word(bn, 0xabc)
  1884. || !TEST_BN_ge_zero(bn)
  1885. || !TEST_BN_gt_zero(bn)
  1886. || !TEST_BN_ne_zero(bn)
  1887. || !TEST_BN_even(bn))
  1888. goto err;
  1889. st = 1;
  1890. err:
  1891. BN_free(bn);
  1892. return st;
  1893. }
  1894. static int test_asc2bn(void)
  1895. {
  1896. BIGNUM *bn = NULL;
  1897. int st = 0;
  1898. if (!TEST_ptr(bn = BN_new()))
  1899. goto err;
  1900. if (!TEST_true(BN_asc2bn(&bn, "0"))
  1901. || !TEST_BN_eq_zero(bn)
  1902. || !TEST_BN_ge_zero(bn))
  1903. goto err;
  1904. if (!TEST_true(BN_asc2bn(&bn, "256"))
  1905. || !TEST_BN_eq_word(bn, 256)
  1906. || !TEST_BN_ge_zero(bn))
  1907. goto err;
  1908. if (!TEST_true(BN_asc2bn(&bn, "-42"))
  1909. || !TEST_BN_abs_eq_word(bn, 42)
  1910. || !TEST_BN_lt_zero(bn))
  1911. goto err;
  1912. if (!TEST_true(BN_asc2bn(&bn, "0x1234"))
  1913. || !TEST_BN_eq_word(bn, 0x1234)
  1914. || !TEST_BN_ge_zero(bn))
  1915. goto err;
  1916. if (!TEST_true(BN_asc2bn(&bn, "0X1234"))
  1917. || !TEST_BN_eq_word(bn, 0x1234)
  1918. || !TEST_BN_ge_zero(bn))
  1919. goto err;
  1920. if (!TEST_true(BN_asc2bn(&bn, "-0xabcd"))
  1921. || !TEST_BN_abs_eq_word(bn, 0xabcd)
  1922. || !TEST_BN_lt_zero(bn))
  1923. goto err;
  1924. if (!TEST_true(BN_asc2bn(&bn, "-0"))
  1925. || !TEST_BN_eq_zero(bn)
  1926. || !TEST_BN_ge_zero(bn))
  1927. goto err;
  1928. if (!TEST_true(BN_asc2bn(&bn, "123trailing garbage is ignored"))
  1929. || !TEST_BN_eq_word(bn, 123)
  1930. || !TEST_BN_ge_zero(bn))
  1931. goto err;
  1932. st = 1;
  1933. err:
  1934. BN_free(bn);
  1935. return st;
  1936. }
  1937. static const MPITEST kMPITests[] = {
  1938. {"0", "\x00\x00\x00\x00", 4},
  1939. {"1", "\x00\x00\x00\x01\x01", 5},
  1940. {"-1", "\x00\x00\x00\x01\x81", 5},
  1941. {"128", "\x00\x00\x00\x02\x00\x80", 6},
  1942. {"256", "\x00\x00\x00\x02\x01\x00", 6},
  1943. {"-256", "\x00\x00\x00\x02\x81\x00", 6},
  1944. };
  1945. static int test_mpi(int i)
  1946. {
  1947. uint8_t scratch[8];
  1948. const MPITEST *test = &kMPITests[i];
  1949. size_t mpi_len, mpi_len2;
  1950. BIGNUM *bn = NULL;
  1951. BIGNUM *bn2 = NULL;
  1952. int st = 0;
  1953. if (!TEST_ptr(bn = BN_new())
  1954. || !TEST_true(BN_asc2bn(&bn, test->base10)))
  1955. goto err;
  1956. mpi_len = BN_bn2mpi(bn, NULL);
  1957. if (!TEST_size_t_le(mpi_len, sizeof(scratch)))
  1958. goto err;
  1959. if (!TEST_size_t_eq(mpi_len2 = BN_bn2mpi(bn, scratch), mpi_len)
  1960. || !TEST_mem_eq(test->mpi, test->mpi_len, scratch, mpi_len))
  1961. goto err;
  1962. if (!TEST_ptr(bn2 = BN_mpi2bn(scratch, mpi_len, NULL)))
  1963. goto err;
  1964. if (!TEST_BN_eq(bn, bn2)) {
  1965. BN_free(bn2);
  1966. goto err;
  1967. }
  1968. BN_free(bn2);
  1969. st = 1;
  1970. err:
  1971. BN_free(bn);
  1972. return st;
  1973. }
  1974. static int test_rand(void)
  1975. {
  1976. BIGNUM *bn = NULL;
  1977. int st = 0;
  1978. if (!TEST_ptr(bn = BN_new()))
  1979. return 0;
  1980. /* Test BN_rand for degenerate cases with |top| and |bottom| parameters. */
  1981. if (!TEST_false(BN_rand(bn, 0, 0 /* top */ , 0 /* bottom */ ))
  1982. || !TEST_false(BN_rand(bn, 0, 1 /* top */ , 1 /* bottom */ ))
  1983. || !TEST_true(BN_rand(bn, 1, 0 /* top */ , 0 /* bottom */ ))
  1984. || !TEST_BN_eq_one(bn)
  1985. || !TEST_false(BN_rand(bn, 1, 1 /* top */ , 0 /* bottom */ ))
  1986. || !TEST_true(BN_rand(bn, 1, -1 /* top */ , 1 /* bottom */ ))
  1987. || !TEST_BN_eq_one(bn)
  1988. || !TEST_true(BN_rand(bn, 2, 1 /* top */ , 0 /* bottom */ ))
  1989. || !TEST_BN_eq_word(bn, 3))
  1990. goto err;
  1991. st = 1;
  1992. err:
  1993. BN_free(bn);
  1994. return st;
  1995. }
  1996. /*
  1997. * Run some statistical tests to provide a degree confidence that the
  1998. * BN_rand_range() function works as expected. The test cases and
  1999. * critical values are generated by the bn_rand_range script.
  2000. *
  2001. * Each individual test is a Chi^2 goodness of fit for a specified number
  2002. * of samples and range. The samples are assumed to be independent and
  2003. * that they are from a discrete uniform distribution.
  2004. *
  2005. * Some of these individual tests are expected to fail, the success/failure
  2006. * of each is an independent Bernoulli trial. The number of such successes
  2007. * will form a binomial distribution. The count of the successes is compared
  2008. * against a precomputed critical value to determine the overall outcome.
  2009. */
  2010. struct rand_range_case {
  2011. unsigned int range;
  2012. unsigned int iterations;
  2013. double critical;
  2014. };
  2015. #include "bn_rand_range.h"
  2016. static int test_rand_range_single(size_t n)
  2017. {
  2018. const unsigned int range = rand_range_cases[n].range;
  2019. const unsigned int iterations = rand_range_cases[n].iterations;
  2020. const double critical = rand_range_cases[n].critical;
  2021. const double expected = iterations / (double)range;
  2022. double sum = 0;
  2023. BIGNUM *rng = NULL, *val = NULL;
  2024. size_t *counts;
  2025. unsigned int i, v;
  2026. int res = 0;
  2027. if (!TEST_ptr(counts = OPENSSL_zalloc(sizeof(*counts) * range))
  2028. || !TEST_ptr(rng = BN_new())
  2029. || !TEST_ptr(val = BN_new())
  2030. || !TEST_true(BN_set_word(rng, range)))
  2031. goto err;
  2032. for (i = 0; i < iterations; i++) {
  2033. if (!TEST_true(BN_rand_range(val, rng))
  2034. || !TEST_uint_lt(v = (unsigned int)BN_get_word(val), range))
  2035. goto err;
  2036. counts[v]++;
  2037. }
  2038. for (i = 0; i < range; i++) {
  2039. const double delta = counts[i] - expected;
  2040. sum += delta * delta;
  2041. }
  2042. sum /= expected;
  2043. if (sum > critical) {
  2044. TEST_info("Chi^2 test negative %.4f > %4.f", sum, critical);
  2045. TEST_note("test case %zu range %u iterations %u", n + 1, range,
  2046. iterations);
  2047. goto err;
  2048. }
  2049. res = 1;
  2050. err:
  2051. BN_free(rng);
  2052. BN_free(val);
  2053. OPENSSL_free(counts);
  2054. return res;
  2055. }
  2056. static int test_rand_range(void)
  2057. {
  2058. int n_success = 0;
  2059. size_t i;
  2060. for (i = 0; i < OSSL_NELEM(rand_range_cases); i++)
  2061. n_success += test_rand_range_single(i);
  2062. if (TEST_int_ge(n_success, binomial_critical))
  2063. return 1;
  2064. TEST_note("This test is expected to fail by chance 0.01%% of the time.");
  2065. return 0;
  2066. }
  2067. static int test_negzero(void)
  2068. {
  2069. BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
  2070. BIGNUM *numerator = NULL, *denominator = NULL;
  2071. int consttime, st = 0;
  2072. if (!TEST_ptr(a = BN_new())
  2073. || !TEST_ptr(b = BN_new())
  2074. || !TEST_ptr(c = BN_new())
  2075. || !TEST_ptr(d = BN_new()))
  2076. goto err;
  2077. /* Test that BN_mul never gives negative zero. */
  2078. if (!TEST_true(BN_set_word(a, 1)))
  2079. goto err;
  2080. BN_set_negative(a, 1);
  2081. BN_zero(b);
  2082. if (!TEST_true(BN_mul(c, a, b, ctx)))
  2083. goto err;
  2084. if (!TEST_BN_eq_zero(c)
  2085. || !TEST_BN_ge_zero(c))
  2086. goto err;
  2087. for (consttime = 0; consttime < 2; consttime++) {
  2088. if (!TEST_ptr(numerator = BN_new())
  2089. || !TEST_ptr(denominator = BN_new()))
  2090. goto err;
  2091. if (consttime) {
  2092. BN_set_flags(numerator, BN_FLG_CONSTTIME);
  2093. BN_set_flags(denominator, BN_FLG_CONSTTIME);
  2094. }
  2095. /* Test that BN_div never gives negative zero in the quotient. */
  2096. if (!TEST_true(BN_set_word(numerator, 1))
  2097. || !TEST_true(BN_set_word(denominator, 2)))
  2098. goto err;
  2099. BN_set_negative(numerator, 1);
  2100. if (!TEST_true(BN_div(a, b, numerator, denominator, ctx))
  2101. || !TEST_BN_eq_zero(a)
  2102. || !TEST_BN_ge_zero(a))
  2103. goto err;
  2104. /* Test that BN_div never gives negative zero in the remainder. */
  2105. if (!TEST_true(BN_set_word(denominator, 1))
  2106. || !TEST_true(BN_div(a, b, numerator, denominator, ctx))
  2107. || !TEST_BN_eq_zero(b)
  2108. || !TEST_BN_ge_zero(b))
  2109. goto err;
  2110. BN_free(numerator);
  2111. BN_free(denominator);
  2112. numerator = denominator = NULL;
  2113. }
  2114. /* Test that BN_set_negative will not produce a negative zero. */
  2115. BN_zero(a);
  2116. BN_set_negative(a, 1);
  2117. if (BN_is_negative(a))
  2118. goto err;
  2119. st = 1;
  2120. err:
  2121. BN_free(a);
  2122. BN_free(b);
  2123. BN_free(c);
  2124. BN_free(d);
  2125. BN_free(numerator);
  2126. BN_free(denominator);
  2127. return st;
  2128. }
  2129. static int test_badmod(void)
  2130. {
  2131. BIGNUM *a = NULL, *b = NULL, *zero = NULL;
  2132. BN_MONT_CTX *mont = NULL;
  2133. int st = 0;
  2134. if (!TEST_ptr(a = BN_new())
  2135. || !TEST_ptr(b = BN_new())
  2136. || !TEST_ptr(zero = BN_new())
  2137. || !TEST_ptr(mont = BN_MONT_CTX_new()))
  2138. goto err;
  2139. BN_zero(zero);
  2140. if (!TEST_false(BN_div(a, b, BN_value_one(), zero, ctx)))
  2141. goto err;
  2142. ERR_clear_error();
  2143. if (!TEST_false(BN_mod_mul(a, BN_value_one(), BN_value_one(), zero, ctx)))
  2144. goto err;
  2145. ERR_clear_error();
  2146. if (!TEST_false(BN_mod_exp(a, BN_value_one(), BN_value_one(), zero, ctx)))
  2147. goto err;
  2148. ERR_clear_error();
  2149. if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
  2150. zero, ctx, NULL)))
  2151. goto err;
  2152. ERR_clear_error();
  2153. if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
  2154. zero, ctx, NULL)))
  2155. goto err;
  2156. ERR_clear_error();
  2157. if (!TEST_false(BN_MONT_CTX_set(mont, zero, ctx)))
  2158. goto err;
  2159. ERR_clear_error();
  2160. /* Some operations also may not be used with an even modulus. */
  2161. if (!TEST_true(BN_set_word(b, 16)))
  2162. goto err;
  2163. if (!TEST_false(BN_MONT_CTX_set(mont, b, ctx)))
  2164. goto err;
  2165. ERR_clear_error();
  2166. if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
  2167. b, ctx, NULL)))
  2168. goto err;
  2169. ERR_clear_error();
  2170. if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
  2171. b, ctx, NULL)))
  2172. goto err;
  2173. ERR_clear_error();
  2174. st = 1;
  2175. err:
  2176. BN_free(a);
  2177. BN_free(b);
  2178. BN_free(zero);
  2179. BN_MONT_CTX_free(mont);
  2180. return st;
  2181. }
  2182. static int test_expmodzero(void)
  2183. {
  2184. BIGNUM *a = NULL, *r = NULL, *zero = NULL;
  2185. int st = 0;
  2186. if (!TEST_ptr(zero = BN_new())
  2187. || !TEST_ptr(a = BN_new())
  2188. || !TEST_ptr(r = BN_new()))
  2189. goto err;
  2190. BN_zero(zero);
  2191. if (!TEST_true(BN_mod_exp(r, a, zero, BN_value_one(), NULL))
  2192. || !TEST_BN_eq_zero(r)
  2193. || !TEST_true(BN_mod_exp_mont(r, a, zero, BN_value_one(),
  2194. NULL, NULL))
  2195. || !TEST_BN_eq_zero(r)
  2196. || !TEST_true(BN_mod_exp_mont_consttime(r, a, zero,
  2197. BN_value_one(),
  2198. NULL, NULL))
  2199. || !TEST_BN_eq_zero(r)
  2200. || !TEST_true(BN_mod_exp_mont_word(r, 42, zero,
  2201. BN_value_one(), NULL, NULL))
  2202. || !TEST_BN_eq_zero(r))
  2203. goto err;
  2204. st = 1;
  2205. err:
  2206. BN_free(zero);
  2207. BN_free(a);
  2208. BN_free(r);
  2209. return st;
  2210. }
  2211. static int test_expmodone(void)
  2212. {
  2213. int ret = 0, i;
  2214. BIGNUM *r = BN_new();
  2215. BIGNUM *a = BN_new();
  2216. BIGNUM *p = BN_new();
  2217. BIGNUM *m = BN_new();
  2218. if (!TEST_ptr(r)
  2219. || !TEST_ptr(a)
  2220. || !TEST_ptr(p)
  2221. || !TEST_ptr(p)
  2222. || !TEST_ptr(m)
  2223. || !TEST_true(BN_set_word(a, 1))
  2224. || !TEST_true(BN_set_word(p, 0))
  2225. || !TEST_true(BN_set_word(m, 1)))
  2226. goto err;
  2227. /* Calculate r = 1 ^ 0 mod 1, and check the result is always 0 */
  2228. for (i = 0; i < 2; i++) {
  2229. if (!TEST_true(BN_mod_exp(r, a, p, m, NULL))
  2230. || !TEST_BN_eq_zero(r)
  2231. || !TEST_true(BN_mod_exp_mont(r, a, p, m, NULL, NULL))
  2232. || !TEST_BN_eq_zero(r)
  2233. || !TEST_true(BN_mod_exp_mont_consttime(r, a, p, m, NULL, NULL))
  2234. || !TEST_BN_eq_zero(r)
  2235. || !TEST_true(BN_mod_exp_mont_word(r, 1, p, m, NULL, NULL))
  2236. || !TEST_BN_eq_zero(r)
  2237. || !TEST_true(BN_mod_exp_simple(r, a, p, m, NULL))
  2238. || !TEST_BN_eq_zero(r)
  2239. || !TEST_true(BN_mod_exp_recp(r, a, p, m, NULL))
  2240. || !TEST_BN_eq_zero(r))
  2241. goto err;
  2242. /* Repeat for r = 1 ^ 0 mod -1 */
  2243. if (i == 0)
  2244. BN_set_negative(m, 1);
  2245. }
  2246. ret = 1;
  2247. err:
  2248. BN_free(r);
  2249. BN_free(a);
  2250. BN_free(p);
  2251. BN_free(m);
  2252. return ret;
  2253. }
  2254. static int test_smallprime(int kBits)
  2255. {
  2256. BIGNUM *r;
  2257. int st = 0;
  2258. if (!TEST_ptr(r = BN_new()))
  2259. goto err;
  2260. if (kBits <= 1) {
  2261. if (!TEST_false(BN_generate_prime_ex(r, kBits, 0,
  2262. NULL, NULL, NULL)))
  2263. goto err;
  2264. } else {
  2265. if (!TEST_true(BN_generate_prime_ex(r, kBits, 0,
  2266. NULL, NULL, NULL))
  2267. || !TEST_int_eq(BN_num_bits(r), kBits))
  2268. goto err;
  2269. }
  2270. st = 1;
  2271. err:
  2272. BN_free(r);
  2273. return st;
  2274. }
  2275. static int test_smallsafeprime(int kBits)
  2276. {
  2277. BIGNUM *r;
  2278. int st = 0;
  2279. if (!TEST_ptr(r = BN_new()))
  2280. goto err;
  2281. if (kBits <= 5 && kBits != 3) {
  2282. if (!TEST_false(BN_generate_prime_ex(r, kBits, 1,
  2283. NULL, NULL, NULL)))
  2284. goto err;
  2285. } else {
  2286. if (!TEST_true(BN_generate_prime_ex(r, kBits, 1,
  2287. NULL, NULL, NULL))
  2288. || !TEST_int_eq(BN_num_bits(r), kBits))
  2289. goto err;
  2290. }
  2291. st = 1;
  2292. err:
  2293. BN_free(r);
  2294. return st;
  2295. }
  2296. static int primes[] = { 2, 3, 5, 7, 17863 };
  2297. static int test_is_prime(int i)
  2298. {
  2299. int ret = 0;
  2300. BIGNUM *r = NULL;
  2301. int trial;
  2302. if (!TEST_ptr(r = BN_new()))
  2303. goto err;
  2304. for (trial = 0; trial <= 1; ++trial) {
  2305. if (!TEST_true(BN_set_word(r, primes[i]))
  2306. || !TEST_int_eq(BN_check_prime(r, ctx, NULL),
  2307. 1))
  2308. goto err;
  2309. }
  2310. ret = 1;
  2311. err:
  2312. BN_free(r);
  2313. return ret;
  2314. }
  2315. static int not_primes[] = { -1, 0, 1, 4 };
  2316. static int test_not_prime(int i)
  2317. {
  2318. int ret = 0;
  2319. BIGNUM *r = NULL;
  2320. int trial;
  2321. if (!TEST_ptr(r = BN_new()))
  2322. goto err;
  2323. for (trial = 0; trial <= 1; ++trial) {
  2324. if (!TEST_true(BN_set_word(r, not_primes[i]))
  2325. || !TEST_false(BN_check_prime(r, ctx, NULL)))
  2326. goto err;
  2327. }
  2328. ret = 1;
  2329. err:
  2330. BN_free(r);
  2331. return ret;
  2332. }
  2333. static int test_ctx_set_ct_flag(BN_CTX *c)
  2334. {
  2335. int st = 0;
  2336. size_t i;
  2337. BIGNUM *b[15];
  2338. BN_CTX_start(c);
  2339. for (i = 0; i < OSSL_NELEM(b); i++) {
  2340. if (!TEST_ptr(b[i] = BN_CTX_get(c)))
  2341. goto err;
  2342. if (i % 2 == 1)
  2343. BN_set_flags(b[i], BN_FLG_CONSTTIME);
  2344. }
  2345. st = 1;
  2346. err:
  2347. BN_CTX_end(c);
  2348. return st;
  2349. }
  2350. static int test_ctx_check_ct_flag(BN_CTX *c)
  2351. {
  2352. int st = 0;
  2353. size_t i;
  2354. BIGNUM *b[30];
  2355. BN_CTX_start(c);
  2356. for (i = 0; i < OSSL_NELEM(b); i++) {
  2357. if (!TEST_ptr(b[i] = BN_CTX_get(c)))
  2358. goto err;
  2359. if (!TEST_false(BN_get_flags(b[i], BN_FLG_CONSTTIME)))
  2360. goto err;
  2361. }
  2362. st = 1;
  2363. err:
  2364. BN_CTX_end(c);
  2365. return st;
  2366. }
  2367. static int test_ctx_consttime_flag(void)
  2368. {
  2369. /*-
  2370. * The constant-time flag should not "leak" among BN_CTX frames:
  2371. *
  2372. * - test_ctx_set_ct_flag() starts a frame in the given BN_CTX and
  2373. * sets the BN_FLG_CONSTTIME flag on some of the BIGNUMs obtained
  2374. * from the frame before ending it.
  2375. * - test_ctx_check_ct_flag() then starts a new frame and gets a
  2376. * number of BIGNUMs from it. In absence of leaks, none of the
  2377. * BIGNUMs in the new frame should have BN_FLG_CONSTTIME set.
  2378. *
  2379. * In actual BN_CTX usage inside libcrypto the leak could happen at
  2380. * any depth level in the BN_CTX stack, with varying results
  2381. * depending on the patterns of sibling trees of nested function
  2382. * calls sharing the same BN_CTX object, and the effect of
  2383. * unintended BN_FLG_CONSTTIME on the called BN_* functions.
  2384. *
  2385. * This simple unit test abstracts away this complexity and verifies
  2386. * that the leak does not happen between two sibling functions
  2387. * sharing the same BN_CTX object at the same level of nesting.
  2388. *
  2389. */
  2390. BN_CTX *nctx = NULL;
  2391. BN_CTX *sctx = NULL;
  2392. size_t i = 0;
  2393. int st = 0;
  2394. if (!TEST_ptr(nctx = BN_CTX_new())
  2395. || !TEST_ptr(sctx = BN_CTX_secure_new()))
  2396. goto err;
  2397. for (i = 0; i < 2; i++) {
  2398. BN_CTX *c = i == 0 ? nctx : sctx;
  2399. if (!TEST_true(test_ctx_set_ct_flag(c))
  2400. || !TEST_true(test_ctx_check_ct_flag(c)))
  2401. goto err;
  2402. }
  2403. st = 1;
  2404. err:
  2405. BN_CTX_free(nctx);
  2406. BN_CTX_free(sctx);
  2407. return st;
  2408. }
  2409. static int test_coprime(void)
  2410. {
  2411. BIGNUM *a = NULL, *b = NULL;
  2412. int ret = 0;
  2413. ret = TEST_ptr(a = BN_new())
  2414. && TEST_ptr(b = BN_new())
  2415. && TEST_true(BN_set_word(a, 66))
  2416. && TEST_true(BN_set_word(b, 99))
  2417. && TEST_int_eq(BN_are_coprime(a, b, ctx), 0)
  2418. && TEST_int_eq(BN_are_coprime(b, a, ctx), 0)
  2419. && TEST_true(BN_set_word(a, 67))
  2420. && TEST_int_eq(BN_are_coprime(a, b, ctx), 1)
  2421. && TEST_int_eq(BN_are_coprime(b, a, ctx), 1);
  2422. BN_free(a);
  2423. BN_free(b);
  2424. return ret;
  2425. }
  2426. static int test_gcd_prime(void)
  2427. {
  2428. BIGNUM *a = NULL, *b = NULL, *gcd = NULL;
  2429. int i, st = 0;
  2430. if (!TEST_ptr(a = BN_new())
  2431. || !TEST_ptr(b = BN_new())
  2432. || !TEST_ptr(gcd = BN_new()))
  2433. goto err;
  2434. if (!TEST_true(BN_generate_prime_ex(a, 1024, 0, NULL, NULL, NULL)))
  2435. goto err;
  2436. for (i = 0; i < NUM_PRIME_TESTS; i++) {
  2437. if (!TEST_true(BN_generate_prime_ex(b, 1024, 0,
  2438. NULL, NULL, NULL))
  2439. || !TEST_true(BN_gcd(gcd, a, b, ctx))
  2440. || !TEST_true(BN_is_one(gcd))
  2441. || !TEST_true(BN_are_coprime(a, b, ctx)))
  2442. goto err;
  2443. }
  2444. st = 1;
  2445. err:
  2446. BN_free(a);
  2447. BN_free(b);
  2448. BN_free(gcd);
  2449. return st;
  2450. }
  2451. typedef struct mod_exp_test_st
  2452. {
  2453. const char *base;
  2454. const char *exp;
  2455. const char *mod;
  2456. const char *res;
  2457. } MOD_EXP_TEST;
  2458. static const MOD_EXP_TEST ModExpTests[] = {
  2459. /* original test vectors for rsaz_512_sqr bug, by OSS-Fuzz */
  2460. {
  2461. "1166180238001879113042182292626169621106255558914000595999312084"
  2462. "4627946820899490684928760491249738643524880720584249698100907201"
  2463. "002086675047927600340800371",
  2464. "8000000000000000000000000000000000000000000000000000000000000000"
  2465. "0000000000000000000000000000000000000000000000000000000000000000"
  2466. "00000000",
  2467. "1340780792684523720980737645613191762604395855615117867483316354"
  2468. "3294276330515137663421134775482798690129946803802212663956180562"
  2469. "088664022929883876655300863",
  2470. "8243904058268085430037326628480645845409758077568738532059032482"
  2471. "8294114415890603594730158120426756266457928475330450251339773498"
  2472. "26758407619521544102068438"
  2473. },
  2474. {
  2475. "4974270041410803822078866696159586946995877618987010219312844726"
  2476. "0284386121835740784990869050050504348861513337232530490826340663"
  2477. "197278031692737429054",
  2478. "4974270041410803822078866696159586946995877428188754995041148539"
  2479. "1663243362592271353668158565195557417149981094324650322556843202"
  2480. "946445882670777892608",
  2481. "1340780716511420227215592830971452482815377482627251725537099028"
  2482. "4429769497230131760206012644403029349547320953206103351725462999"
  2483. "947509743623340557059752191",
  2484. "5296244594780707015616522701706118082963369547253192207884519362"
  2485. "1767869984947542695665420219028522815539559194793619684334900442"
  2486. "49304558011362360473525933"
  2487. },
  2488. /* test vectors for rsaz_512_srq bug, with rcx/rbx=1 */
  2489. { /* between first and second iteration */
  2490. "5148719036160389201525610950887605325980251964889646556085286545"
  2491. "3931548809178823413169359635978762036512397113080988070677858033"
  2492. "36463909753993540214027190",
  2493. "6703903964971298549787012499102923063739682910296196688861780721"
  2494. "8608820150367734884009371490834517138450159290932430254268769414"
  2495. "05973284973216824503042158",
  2496. "6703903964971298549787012499102923063739682910296196688861780721"
  2497. "8608820150367734884009371490834517138450159290932430254268769414"
  2498. "05973284973216824503042159",
  2499. "1"
  2500. },
  2501. { /* between second and third iteration */
  2502. "8908340854353752577419678771330460827942371434853054158622636544"
  2503. "8151360109722890949471912566649465436296659601091730745087014189"
  2504. "2672764191218875181826063",
  2505. "6703903964971298549787012499102923063739682910296196688861780721"
  2506. "8608820150367734884009371490834517138450159290932430254268769414"
  2507. "05973284973216824503042158",
  2508. "6703903964971298549787012499102923063739682910296196688861780721"
  2509. "8608820150367734884009371490834517138450159290932430254268769414"
  2510. "05973284973216824503042159",
  2511. "1"
  2512. },
  2513. { /* between third and fourth iteration */
  2514. "3427446396505596330634350984901719674479522569002785244080234738"
  2515. "4288743635435746136297299366444548736533053717416735379073185344"
  2516. "26985272974404612945608761",
  2517. "6703903964971298549787012499102923063739682910296196688861780721"
  2518. "8608820150367734884009371490834517138450159290932430254268769414"
  2519. "05973284973216824503042158",
  2520. "6703903964971298549787012499102923063739682910296196688861780721"
  2521. "8608820150367734884009371490834517138450159290932430254268769414"
  2522. "05973284973216824503042159",
  2523. "1"
  2524. },
  2525. { /* between fourth and fifth iteration */
  2526. "3472743044917564564078857826111874560045331237315597383869652985"
  2527. "6919870028890895988478351133601517365908445058405433832718206902"
  2528. "4088133164805266956353542",
  2529. "6703903964971298549787012499102923063739682910296196688861780721"
  2530. "8608820150367734884009371490834517138450159290932430254268769414"
  2531. "05973284973216824503042158",
  2532. "6703903964971298549787012499102923063739682910296196688861780721"
  2533. "8608820150367734884009371490834517138450159290932430254268769414"
  2534. "05973284973216824503042159",
  2535. "1"
  2536. },
  2537. { /* between fifth and sixth iteration */
  2538. "3608632990153469264412378349742339216742409743898601587274768025"
  2539. "0110772032985643555192767717344946174122842255204082586753499651"
  2540. "14483434992887431333675068",
  2541. "6703903964971298549787012499102923063739682910296196688861780721"
  2542. "8608820150367734884009371490834517138450159290932430254268769414"
  2543. "05973284973216824503042158",
  2544. "6703903964971298549787012499102923063739682910296196688861780721"
  2545. "8608820150367734884009371490834517138450159290932430254268769414"
  2546. "05973284973216824503042159",
  2547. "1"
  2548. },
  2549. { /* between sixth and seventh iteration */
  2550. "8455374370234070242910508226941981520235709767260723212165264877"
  2551. "8689064388017521524568434328264431772644802567028663962962025746"
  2552. "9283458217850119569539086",
  2553. "6703903964971298549787012499102923063739682910296196688861780721"
  2554. "8608820150367734884009371490834517138450159290932430254268769414"
  2555. "05973284973216824503042158",
  2556. "6703903964971298549787012499102923063739682910296196688861780721"
  2557. "8608820150367734884009371490834517138450159290932430254268769414"
  2558. "05973284973216824503042159",
  2559. "1"
  2560. },
  2561. { /* between seventh and eighth iteration */
  2562. "5155371529688532178421209781159131443543419764974688878527112131"
  2563. "7446518205609427412336183157918981038066636807317733319323257603"
  2564. "04416292040754017461076359",
  2565. "1005585594745694782468051874865438459560952436544429503329267108"
  2566. "2791323022555160232601405723625177570767523893639864538140315412"
  2567. "108959927459825236754563832",
  2568. "1005585594745694782468051874865438459560952436544429503329267108"
  2569. "2791323022555160232601405723625177570767523893639864538140315412"
  2570. "108959927459825236754563833",
  2571. "1"
  2572. },
  2573. /* test vectors for rsaz_512_srq bug, with rcx/rbx=2 */
  2574. { /* between first and second iteration */
  2575. "3155666506033786929967309937640790361084670559125912405342594979"
  2576. "4345142818528956285490897841406338022378565972533508820577760065"
  2577. "58494345853302083699912572",
  2578. "6703903964971298549787012499102923063739682910296196688861780721"
  2579. "8608820150367734884009371490834517138450159290932430254268769414"
  2580. "05973284973216824503042158",
  2581. "6703903964971298549787012499102923063739682910296196688861780721"
  2582. "8608820150367734884009371490834517138450159290932430254268769414"
  2583. "05973284973216824503042159",
  2584. "1"
  2585. },
  2586. { /* between second and third iteration */
  2587. "3789819583801342198190405714582958759005991915505282362397087750"
  2588. "4213544724644823098843135685133927198668818185338794377239590049"
  2589. "41019388529192775771488319",
  2590. "6703903964971298549787012499102923063739682910296196688861780721"
  2591. "8608820150367734884009371490834517138450159290932430254268769414"
  2592. "05973284973216824503042158",
  2593. "6703903964971298549787012499102923063739682910296196688861780721"
  2594. "8608820150367734884009371490834517138450159290932430254268769414"
  2595. "05973284973216824503042159",
  2596. "1"
  2597. },
  2598. { /* between third and forth iteration */
  2599. "4695752552040706867080542538786056470322165281761525158189220280"
  2600. "4025547447667484759200742764246905647644662050122968912279199065"
  2601. "48065034299166336940507214",
  2602. "6703903964971298549787012499102923063739682910296196688861780721"
  2603. "8608820150367734884009371490834517138450159290932430254268769414"
  2604. "05973284973216824503042158",
  2605. "6703903964971298549787012499102923063739682910296196688861780721"
  2606. "8608820150367734884009371490834517138450159290932430254268769414"
  2607. "05973284973216824503042159",
  2608. "1"
  2609. },
  2610. { /* between forth and fifth iteration */
  2611. "2159140240970485794188159431017382878636879856244045329971239574"
  2612. "8919691133560661162828034323196457386059819832804593989740268964"
  2613. "74502911811812651475927076",
  2614. "6703903964971298549787012499102923063739682910296196688861780721"
  2615. "8608820150367734884009371490834517138450159290932430254268769414"
  2616. "05973284973216824503042158",
  2617. "6703903964971298549787012499102923063739682910296196688861780721"
  2618. "8608820150367734884009371490834517138450159290932430254268769414"
  2619. "05973284973216824503042159",
  2620. "1"
  2621. },
  2622. { /* between fifth and sixth iteration */
  2623. "5239312332984325668414624633307915097111691815000872662334695514"
  2624. "5436533521392362443557163429336808208137221322444780490437871903"
  2625. "99972784701334569424519255",
  2626. "6703903964971298549787012499102923063739682910296196688861780721"
  2627. "8608820150367734884009371490834517138450159290932430254268769414"
  2628. "05973284973216824503042158",
  2629. "6703903964971298549787012499102923063739682910296196688861780721"
  2630. "8608820150367734884009371490834517138450159290932430254268769414"
  2631. "05973284973216824503042159",
  2632. "1"
  2633. },
  2634. { /* between sixth and seventh iteration */
  2635. "1977953647322612860406858017869125467496941904523063466791308891"
  2636. "1172796739058531929470539758361774569875505293428856181093904091"
  2637. "33788264851714311303725089",
  2638. "6703903964971298549787012499102923063739682910296196688861780721"
  2639. "8608820150367734884009371490834517138450159290932430254268769414"
  2640. "05973284973216824503042158",
  2641. "6703903964971298549787012499102923063739682910296196688861780721"
  2642. "8608820150367734884009371490834517138450159290932430254268769414"
  2643. "05973284973216824503042159",
  2644. "1"
  2645. },
  2646. { /* between seventh and eighth iteration */
  2647. "6456987954117763835533395796948878140715006860263624787492985786"
  2648. "8514630216966738305923915688821526449499763719943997120302368211"
  2649. "04813318117996225041943964",
  2650. "1340780792994259709957402499820584612747936582059239337772356144"
  2651. "3721764030073546976801874298166903427690031858186486050853753882"
  2652. "811946551499689575296532556",
  2653. "1340780792994259709957402499820584612747936582059239337772356144"
  2654. "3721764030073546976801874298166903427690031858186486050853753882"
  2655. "811946551499689575296532557",
  2656. "1"
  2657. }
  2658. };
  2659. static int test_mod_exp(int i)
  2660. {
  2661. const MOD_EXP_TEST *test = &ModExpTests[i];
  2662. int res = 0;
  2663. BIGNUM* result = NULL;
  2664. BIGNUM *base = NULL, *exponent = NULL, *modulo = NULL;
  2665. char *s = NULL;
  2666. if (!TEST_ptr(result = BN_new())
  2667. || !TEST_true(BN_dec2bn(&base, test->base))
  2668. || !TEST_true(BN_dec2bn(&exponent, test->exp))
  2669. || !TEST_true(BN_dec2bn(&modulo, test->mod)))
  2670. goto err;
  2671. if (!TEST_int_eq(BN_mod_exp(result, base, exponent, modulo, ctx), 1))
  2672. goto err;
  2673. if (!TEST_ptr(s = BN_bn2dec(result)))
  2674. goto err;
  2675. if (!TEST_mem_eq(s, strlen(s), test->res, strlen(test->res)))
  2676. goto err;
  2677. res = 1;
  2678. err:
  2679. OPENSSL_free(s);
  2680. BN_free(result);
  2681. BN_free(base);
  2682. BN_free(exponent);
  2683. BN_free(modulo);
  2684. return res;
  2685. }
  2686. static int test_mod_exp_consttime(int i)
  2687. {
  2688. const MOD_EXP_TEST *test = &ModExpTests[i];
  2689. int res = 0;
  2690. BIGNUM* result = NULL;
  2691. BIGNUM *base = NULL, *exponent = NULL, *modulo = NULL;
  2692. char *s = NULL;
  2693. if (!TEST_ptr(result = BN_new())
  2694. || !TEST_true(BN_dec2bn(&base, test->base))
  2695. || !TEST_true(BN_dec2bn(&exponent, test->exp))
  2696. || !TEST_true(BN_dec2bn(&modulo, test->mod)))
  2697. goto err;
  2698. BN_set_flags(base, BN_FLG_CONSTTIME);
  2699. BN_set_flags(exponent, BN_FLG_CONSTTIME);
  2700. BN_set_flags(modulo, BN_FLG_CONSTTIME);
  2701. if (!TEST_int_eq(BN_mod_exp(result, base, exponent, modulo, ctx), 1))
  2702. goto err;
  2703. if (!TEST_ptr(s = BN_bn2dec(result)))
  2704. goto err;
  2705. if (!TEST_mem_eq(s, strlen(s), test->res, strlen(test->res)))
  2706. goto err;
  2707. res = 1;
  2708. err:
  2709. OPENSSL_free(s);
  2710. BN_free(result);
  2711. BN_free(base);
  2712. BN_free(exponent);
  2713. BN_free(modulo);
  2714. return res;
  2715. }
  2716. /*
  2717. * Regression test to ensure BN_mod_exp2_mont fails safely if argument m is
  2718. * zero.
  2719. */
  2720. static int test_mod_exp2_mont(void)
  2721. {
  2722. int res = 0;
  2723. BIGNUM *exp_result = NULL;
  2724. BIGNUM *exp_a1 = NULL, *exp_p1 = NULL, *exp_a2 = NULL, *exp_p2 = NULL,
  2725. *exp_m = NULL;
  2726. if (!TEST_ptr(exp_result = BN_new())
  2727. || !TEST_ptr(exp_a1 = BN_new())
  2728. || !TEST_ptr(exp_p1 = BN_new())
  2729. || !TEST_ptr(exp_a2 = BN_new())
  2730. || !TEST_ptr(exp_p2 = BN_new())
  2731. || !TEST_ptr(exp_m = BN_new()))
  2732. goto err;
  2733. if (!TEST_true(BN_one(exp_a1))
  2734. || !TEST_true(BN_one(exp_p1))
  2735. || !TEST_true(BN_one(exp_a2))
  2736. || !TEST_true(BN_one(exp_p2)))
  2737. goto err;
  2738. BN_zero(exp_m);
  2739. /* input of 0 is even, so must fail */
  2740. if (!TEST_int_eq(BN_mod_exp2_mont(exp_result, exp_a1, exp_p1, exp_a2,
  2741. exp_p2, exp_m, ctx, NULL), 0))
  2742. goto err;
  2743. res = 1;
  2744. err:
  2745. BN_free(exp_result);
  2746. BN_free(exp_a1);
  2747. BN_free(exp_p1);
  2748. BN_free(exp_a2);
  2749. BN_free(exp_p2);
  2750. BN_free(exp_m);
  2751. return res;
  2752. }
  2753. static int file_test_run(STANZA *s)
  2754. {
  2755. static const FILETEST filetests[] = {
  2756. {"Sum", file_sum},
  2757. {"LShift1", file_lshift1},
  2758. {"LShift", file_lshift},
  2759. {"RShift", file_rshift},
  2760. {"Square", file_square},
  2761. {"Product", file_product},
  2762. {"Quotient", file_quotient},
  2763. {"ModMul", file_modmul},
  2764. {"ModExp", file_modexp},
  2765. {"Exp", file_exp},
  2766. {"ModSqrt", file_modsqrt},
  2767. {"GCD", file_gcd},
  2768. };
  2769. int numtests = OSSL_NELEM(filetests);
  2770. const FILETEST *tp = filetests;
  2771. for ( ; --numtests >= 0; tp++) {
  2772. if (findattr(s, tp->name) != NULL) {
  2773. if (!tp->func(s)) {
  2774. TEST_info("%s:%d: Failed %s test",
  2775. s->test_file, s->start, tp->name);
  2776. return 0;
  2777. }
  2778. return 1;
  2779. }
  2780. }
  2781. TEST_info("%s:%d: Unknown test", s->test_file, s->start);
  2782. return 0;
  2783. }
  2784. static int run_file_tests(int i)
  2785. {
  2786. STANZA *s = NULL;
  2787. char *testfile = test_get_argument(i);
  2788. int c;
  2789. if (!TEST_ptr(s = OPENSSL_zalloc(sizeof(*s))))
  2790. return 0;
  2791. if (!test_start_file(s, testfile)) {
  2792. OPENSSL_free(s);
  2793. return 0;
  2794. }
  2795. /* Read test file. */
  2796. while (!BIO_eof(s->fp) && test_readstanza(s)) {
  2797. if (s->numpairs == 0)
  2798. continue;
  2799. if (!file_test_run(s))
  2800. s->errors++;
  2801. s->numtests++;
  2802. test_clearstanza(s);
  2803. }
  2804. test_end_file(s);
  2805. c = s->errors;
  2806. OPENSSL_free(s);
  2807. return c == 0;
  2808. }
  2809. typedef enum OPTION_choice {
  2810. OPT_ERR = -1,
  2811. OPT_EOF = 0,
  2812. OPT_STOCHASTIC_TESTS,
  2813. OPT_TEST_ENUM
  2814. } OPTION_CHOICE;
  2815. const OPTIONS *test_get_options(void)
  2816. {
  2817. static const OPTIONS test_options[] = {
  2818. OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("[file...]\n"),
  2819. { "stochastic", OPT_STOCHASTIC_TESTS, '-', "Run stochastic tests" },
  2820. { OPT_HELP_STR, 1, '-',
  2821. "file\tFile to run tests on. Normal tests are not run\n" },
  2822. { NULL }
  2823. };
  2824. return test_options;
  2825. }
  2826. int setup_tests(void)
  2827. {
  2828. OPTION_CHOICE o;
  2829. int n, stochastic = 0;
  2830. while ((o = opt_next()) != OPT_EOF) {
  2831. switch (o) {
  2832. case OPT_STOCHASTIC_TESTS:
  2833. stochastic = 1;
  2834. break;
  2835. case OPT_TEST_CASES:
  2836. break;
  2837. default:
  2838. case OPT_ERR:
  2839. return 0;
  2840. }
  2841. }
  2842. n = test_get_argument_count();
  2843. if (!TEST_ptr(ctx = BN_CTX_new()))
  2844. return 0;
  2845. if (n == 0) {
  2846. ADD_TEST(test_sub);
  2847. ADD_TEST(test_div_recip);
  2848. ADD_ALL_TESTS(test_signed_mod_replace_ab, OSSL_NELEM(signed_mod_tests));
  2849. ADD_ALL_TESTS(test_signed_mod_replace_ba, OSSL_NELEM(signed_mod_tests));
  2850. ADD_TEST(test_mod);
  2851. ADD_TEST(test_modexp_mont5);
  2852. ADD_TEST(test_kronecker);
  2853. ADD_TEST(test_rand);
  2854. ADD_TEST(test_bn2padded);
  2855. ADD_TEST(test_dec2bn);
  2856. ADD_TEST(test_hex2bn);
  2857. ADD_TEST(test_asc2bn);
  2858. ADD_ALL_TESTS(test_mpi, (int)OSSL_NELEM(kMPITests));
  2859. ADD_ALL_TESTS(test_bn2signed, (int)OSSL_NELEM(kSignedTests_BE));
  2860. ADD_TEST(test_negzero);
  2861. ADD_TEST(test_badmod);
  2862. ADD_TEST(test_expmodzero);
  2863. ADD_TEST(test_expmodone);
  2864. ADD_ALL_TESTS(test_smallprime, 16);
  2865. ADD_ALL_TESTS(test_smallsafeprime, 16);
  2866. ADD_TEST(test_swap);
  2867. ADD_TEST(test_ctx_consttime_flag);
  2868. #ifndef OPENSSL_NO_EC2M
  2869. ADD_TEST(test_gf2m_add);
  2870. ADD_TEST(test_gf2m_mod);
  2871. ADD_TEST(test_gf2m_mul);
  2872. ADD_TEST(test_gf2m_sqr);
  2873. ADD_TEST(test_gf2m_modinv);
  2874. ADD_TEST(test_gf2m_moddiv);
  2875. ADD_TEST(test_gf2m_modexp);
  2876. ADD_TEST(test_gf2m_modsqrt);
  2877. ADD_TEST(test_gf2m_modsolvequad);
  2878. #endif
  2879. ADD_ALL_TESTS(test_is_prime, (int)OSSL_NELEM(primes));
  2880. ADD_ALL_TESTS(test_not_prime, (int)OSSL_NELEM(not_primes));
  2881. ADD_TEST(test_gcd_prime);
  2882. ADD_TEST(test_coprime);
  2883. ADD_ALL_TESTS(test_mod_exp, (int)OSSL_NELEM(ModExpTests));
  2884. ADD_ALL_TESTS(test_mod_exp_consttime, (int)OSSL_NELEM(ModExpTests));
  2885. ADD_TEST(test_mod_exp2_mont);
  2886. if (stochastic)
  2887. ADD_TEST(test_rand_range);
  2888. } else {
  2889. ADD_ALL_TESTS(run_file_tests, n);
  2890. }
  2891. return 1;
  2892. }
  2893. void cleanup_tests(void)
  2894. {
  2895. BN_CTX_free(ctx);
  2896. }