bntest.c 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429
  1. /*
  2. * Copyright 1995-2023 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_bin2zero(void)
  1975. {
  1976. unsigned char input[] = { 0 };
  1977. BIGNUM *zbn = NULL;
  1978. int ret = 0;
  1979. if (!TEST_ptr(zbn = BN_new()))
  1980. goto err;
  1981. #define zerotest(fn) \
  1982. if (!TEST_ptr(fn(input, 1, zbn)) \
  1983. || !TEST_true(BN_is_zero(zbn)) \
  1984. || !TEST_ptr(fn(input, 0, zbn)) \
  1985. || !TEST_true(BN_is_zero(zbn)) \
  1986. || !TEST_ptr(fn(NULL, 0, zbn)) \
  1987. || !TEST_true(BN_is_zero(zbn))) \
  1988. goto err
  1989. zerotest(BN_bin2bn);
  1990. zerotest(BN_signed_bin2bn);
  1991. zerotest(BN_lebin2bn);
  1992. zerotest(BN_signed_lebin2bn);
  1993. #undef zerotest
  1994. ret = 1;
  1995. err:
  1996. BN_free(zbn);
  1997. return ret;
  1998. }
  1999. static int test_bin2bn_lengths(void)
  2000. {
  2001. unsigned char input[] = { 1, 2 };
  2002. BIGNUM *bn_be = NULL, *bn_expected_be = NULL;
  2003. BIGNUM *bn_le = NULL, *bn_expected_le = NULL;
  2004. int ret = 0;
  2005. if (!TEST_ptr(bn_be = BN_new())
  2006. || !TEST_ptr(bn_expected_be = BN_new())
  2007. || !TEST_true(BN_set_word(bn_expected_be, 0x102))
  2008. || !TEST_ptr(bn_le = BN_new())
  2009. || !TEST_ptr(bn_expected_le = BN_new())
  2010. || !TEST_true(BN_set_word(bn_expected_le, 0x201)))
  2011. goto err;
  2012. #define lengthtest(fn, e) \
  2013. if (!TEST_ptr_null(fn(input, -1, bn_##e)) \
  2014. || !TEST_ptr(fn(input, 0, bn_##e)) \
  2015. || !TEST_true(BN_is_zero(bn_##e)) \
  2016. || !TEST_ptr(fn(input, 2, bn_##e)) \
  2017. || !TEST_int_eq(BN_cmp(bn_##e, bn_expected_##e), 0)) \
  2018. goto err
  2019. lengthtest(BN_bin2bn, be);
  2020. lengthtest(BN_signed_bin2bn, be);
  2021. lengthtest(BN_lebin2bn, le);
  2022. lengthtest(BN_signed_lebin2bn, le);
  2023. #undef lengthtest
  2024. ret = 1;
  2025. err:
  2026. BN_free(bn_be);
  2027. BN_free(bn_expected_be);
  2028. BN_free(bn_le);
  2029. BN_free(bn_expected_le);
  2030. return ret;
  2031. }
  2032. static int test_rand(void)
  2033. {
  2034. BIGNUM *bn = NULL;
  2035. int st = 0;
  2036. if (!TEST_ptr(bn = BN_new()))
  2037. return 0;
  2038. /* Test BN_rand for degenerate cases with |top| and |bottom| parameters. */
  2039. if (!TEST_false(BN_rand(bn, 0, 0 /* top */ , 0 /* bottom */ ))
  2040. || !TEST_false(BN_rand(bn, 0, 1 /* top */ , 1 /* bottom */ ))
  2041. || !TEST_true(BN_rand(bn, 1, 0 /* top */ , 0 /* bottom */ ))
  2042. || !TEST_BN_eq_one(bn)
  2043. || !TEST_false(BN_rand(bn, 1, 1 /* top */ , 0 /* bottom */ ))
  2044. || !TEST_true(BN_rand(bn, 1, -1 /* top */ , 1 /* bottom */ ))
  2045. || !TEST_BN_eq_one(bn)
  2046. || !TEST_true(BN_rand(bn, 2, 1 /* top */ , 0 /* bottom */ ))
  2047. || !TEST_BN_eq_word(bn, 3))
  2048. goto err;
  2049. st = 1;
  2050. err:
  2051. BN_free(bn);
  2052. return st;
  2053. }
  2054. /*
  2055. * Run some statistical tests to provide a degree confidence that the
  2056. * BN_rand_range() function works as expected. The test cases and
  2057. * critical values are generated by the bn_rand_range script.
  2058. *
  2059. * Each individual test is a Chi^2 goodness of fit for a specified number
  2060. * of samples and range. The samples are assumed to be independent and
  2061. * that they are from a discrete uniform distribution.
  2062. *
  2063. * Some of these individual tests are expected to fail, the success/failure
  2064. * of each is an independent Bernoulli trial. The number of such successes
  2065. * will form a binomial distribution. The count of the successes is compared
  2066. * against a precomputed critical value to determine the overall outcome.
  2067. */
  2068. struct rand_range_case {
  2069. unsigned int range;
  2070. unsigned int iterations;
  2071. double critical;
  2072. };
  2073. #include "bn_rand_range.h"
  2074. static int test_rand_range_single(size_t n)
  2075. {
  2076. const unsigned int range = rand_range_cases[n].range;
  2077. const unsigned int iterations = rand_range_cases[n].iterations;
  2078. const double critical = rand_range_cases[n].critical;
  2079. const double expected = iterations / (double)range;
  2080. double sum = 0;
  2081. BIGNUM *rng = NULL, *val = NULL;
  2082. size_t *counts;
  2083. unsigned int i, v;
  2084. int res = 0;
  2085. if (!TEST_ptr(counts = OPENSSL_zalloc(sizeof(*counts) * range))
  2086. || !TEST_ptr(rng = BN_new())
  2087. || !TEST_ptr(val = BN_new())
  2088. || !TEST_true(BN_set_word(rng, range)))
  2089. goto err;
  2090. for (i = 0; i < iterations; i++) {
  2091. if (!TEST_true(BN_rand_range(val, rng))
  2092. || !TEST_uint_lt(v = (unsigned int)BN_get_word(val), range))
  2093. goto err;
  2094. counts[v]++;
  2095. }
  2096. for (i = 0; i < range; i++) {
  2097. const double delta = counts[i] - expected;
  2098. sum += delta * delta;
  2099. }
  2100. sum /= expected;
  2101. if (sum > critical) {
  2102. TEST_info("Chi^2 test negative %.4f > %4.f", sum, critical);
  2103. TEST_note("test case %zu range %u iterations %u", n + 1, range,
  2104. iterations);
  2105. goto err;
  2106. }
  2107. res = 1;
  2108. err:
  2109. BN_free(rng);
  2110. BN_free(val);
  2111. OPENSSL_free(counts);
  2112. return res;
  2113. }
  2114. static int test_rand_range(void)
  2115. {
  2116. int n_success = 0;
  2117. size_t i;
  2118. for (i = 0; i < OSSL_NELEM(rand_range_cases); i++)
  2119. n_success += test_rand_range_single(i);
  2120. if (TEST_int_ge(n_success, binomial_critical))
  2121. return 1;
  2122. TEST_note("This test is expected to fail by chance 0.01%% of the time.");
  2123. return 0;
  2124. }
  2125. static int test_negzero(void)
  2126. {
  2127. BIGNUM *a = NULL, *b = NULL, *c = NULL, *d = NULL;
  2128. BIGNUM *numerator = NULL, *denominator = NULL;
  2129. int consttime, st = 0;
  2130. if (!TEST_ptr(a = BN_new())
  2131. || !TEST_ptr(b = BN_new())
  2132. || !TEST_ptr(c = BN_new())
  2133. || !TEST_ptr(d = BN_new()))
  2134. goto err;
  2135. /* Test that BN_mul never gives negative zero. */
  2136. if (!TEST_true(BN_set_word(a, 1)))
  2137. goto err;
  2138. BN_set_negative(a, 1);
  2139. BN_zero(b);
  2140. if (!TEST_true(BN_mul(c, a, b, ctx)))
  2141. goto err;
  2142. if (!TEST_BN_eq_zero(c)
  2143. || !TEST_BN_ge_zero(c))
  2144. goto err;
  2145. for (consttime = 0; consttime < 2; consttime++) {
  2146. if (!TEST_ptr(numerator = BN_new())
  2147. || !TEST_ptr(denominator = BN_new()))
  2148. goto err;
  2149. if (consttime) {
  2150. BN_set_flags(numerator, BN_FLG_CONSTTIME);
  2151. BN_set_flags(denominator, BN_FLG_CONSTTIME);
  2152. }
  2153. /* Test that BN_div never gives negative zero in the quotient. */
  2154. if (!TEST_true(BN_set_word(numerator, 1))
  2155. || !TEST_true(BN_set_word(denominator, 2)))
  2156. goto err;
  2157. BN_set_negative(numerator, 1);
  2158. if (!TEST_true(BN_div(a, b, numerator, denominator, ctx))
  2159. || !TEST_BN_eq_zero(a)
  2160. || !TEST_BN_ge_zero(a))
  2161. goto err;
  2162. /* Test that BN_div never gives negative zero in the remainder. */
  2163. if (!TEST_true(BN_set_word(denominator, 1))
  2164. || !TEST_true(BN_div(a, b, numerator, denominator, ctx))
  2165. || !TEST_BN_eq_zero(b)
  2166. || !TEST_BN_ge_zero(b))
  2167. goto err;
  2168. BN_free(numerator);
  2169. BN_free(denominator);
  2170. numerator = denominator = NULL;
  2171. }
  2172. /* Test that BN_set_negative will not produce a negative zero. */
  2173. BN_zero(a);
  2174. BN_set_negative(a, 1);
  2175. if (BN_is_negative(a))
  2176. goto err;
  2177. st = 1;
  2178. err:
  2179. BN_free(a);
  2180. BN_free(b);
  2181. BN_free(c);
  2182. BN_free(d);
  2183. BN_free(numerator);
  2184. BN_free(denominator);
  2185. return st;
  2186. }
  2187. static int test_badmod(void)
  2188. {
  2189. BIGNUM *a = NULL, *b = NULL, *zero = NULL;
  2190. BN_MONT_CTX *mont = NULL;
  2191. int st = 0;
  2192. if (!TEST_ptr(a = BN_new())
  2193. || !TEST_ptr(b = BN_new())
  2194. || !TEST_ptr(zero = BN_new())
  2195. || !TEST_ptr(mont = BN_MONT_CTX_new()))
  2196. goto err;
  2197. BN_zero(zero);
  2198. if (!TEST_false(BN_div(a, b, BN_value_one(), zero, ctx)))
  2199. goto err;
  2200. ERR_clear_error();
  2201. if (!TEST_false(BN_mod_mul(a, BN_value_one(), BN_value_one(), zero, ctx)))
  2202. goto err;
  2203. ERR_clear_error();
  2204. if (!TEST_false(BN_mod_exp(a, BN_value_one(), BN_value_one(), zero, ctx)))
  2205. goto err;
  2206. ERR_clear_error();
  2207. if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
  2208. zero, ctx, NULL)))
  2209. goto err;
  2210. ERR_clear_error();
  2211. if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
  2212. zero, ctx, NULL)))
  2213. goto err;
  2214. ERR_clear_error();
  2215. if (!TEST_false(BN_MONT_CTX_set(mont, zero, ctx)))
  2216. goto err;
  2217. ERR_clear_error();
  2218. /* Some operations also may not be used with an even modulus. */
  2219. if (!TEST_true(BN_set_word(b, 16)))
  2220. goto err;
  2221. if (!TEST_false(BN_MONT_CTX_set(mont, b, ctx)))
  2222. goto err;
  2223. ERR_clear_error();
  2224. if (!TEST_false(BN_mod_exp_mont(a, BN_value_one(), BN_value_one(),
  2225. b, ctx, NULL)))
  2226. goto err;
  2227. ERR_clear_error();
  2228. if (!TEST_false(BN_mod_exp_mont_consttime(a, BN_value_one(), BN_value_one(),
  2229. b, ctx, NULL)))
  2230. goto err;
  2231. ERR_clear_error();
  2232. st = 1;
  2233. err:
  2234. BN_free(a);
  2235. BN_free(b);
  2236. BN_free(zero);
  2237. BN_MONT_CTX_free(mont);
  2238. return st;
  2239. }
  2240. static int test_expmodzero(void)
  2241. {
  2242. BIGNUM *a = NULL, *r = NULL, *zero = NULL;
  2243. int st = 0;
  2244. if (!TEST_ptr(zero = BN_new())
  2245. || !TEST_ptr(a = BN_new())
  2246. || !TEST_ptr(r = BN_new()))
  2247. goto err;
  2248. BN_zero(zero);
  2249. if (!TEST_true(BN_mod_exp(r, a, zero, BN_value_one(), NULL))
  2250. || !TEST_BN_eq_zero(r)
  2251. || !TEST_true(BN_mod_exp_mont(r, a, zero, BN_value_one(),
  2252. NULL, NULL))
  2253. || !TEST_BN_eq_zero(r)
  2254. || !TEST_true(BN_mod_exp_mont_consttime(r, a, zero,
  2255. BN_value_one(),
  2256. NULL, NULL))
  2257. || !TEST_BN_eq_zero(r)
  2258. || !TEST_true(BN_mod_exp_mont_word(r, 42, zero,
  2259. BN_value_one(), NULL, NULL))
  2260. || !TEST_BN_eq_zero(r))
  2261. goto err;
  2262. st = 1;
  2263. err:
  2264. BN_free(zero);
  2265. BN_free(a);
  2266. BN_free(r);
  2267. return st;
  2268. }
  2269. static int test_expmodone(void)
  2270. {
  2271. int ret = 0, i;
  2272. BIGNUM *r = BN_new();
  2273. BIGNUM *a = BN_new();
  2274. BIGNUM *p = BN_new();
  2275. BIGNUM *m = BN_new();
  2276. if (!TEST_ptr(r)
  2277. || !TEST_ptr(a)
  2278. || !TEST_ptr(p)
  2279. || !TEST_ptr(p)
  2280. || !TEST_ptr(m)
  2281. || !TEST_true(BN_set_word(a, 1))
  2282. || !TEST_true(BN_set_word(p, 0))
  2283. || !TEST_true(BN_set_word(m, 1)))
  2284. goto err;
  2285. /* Calculate r = 1 ^ 0 mod 1, and check the result is always 0 */
  2286. for (i = 0; i < 2; i++) {
  2287. if (!TEST_true(BN_mod_exp(r, a, p, m, NULL))
  2288. || !TEST_BN_eq_zero(r)
  2289. || !TEST_true(BN_mod_exp_mont(r, a, p, m, NULL, NULL))
  2290. || !TEST_BN_eq_zero(r)
  2291. || !TEST_true(BN_mod_exp_mont_consttime(r, a, p, m, NULL, NULL))
  2292. || !TEST_BN_eq_zero(r)
  2293. || !TEST_true(BN_mod_exp_mont_word(r, 1, p, m, NULL, NULL))
  2294. || !TEST_BN_eq_zero(r)
  2295. || !TEST_true(BN_mod_exp_simple(r, a, p, m, NULL))
  2296. || !TEST_BN_eq_zero(r)
  2297. || !TEST_true(BN_mod_exp_recp(r, a, p, m, NULL))
  2298. || !TEST_BN_eq_zero(r))
  2299. goto err;
  2300. /* Repeat for r = 1 ^ 0 mod -1 */
  2301. if (i == 0)
  2302. BN_set_negative(m, 1);
  2303. }
  2304. ret = 1;
  2305. err:
  2306. BN_free(r);
  2307. BN_free(a);
  2308. BN_free(p);
  2309. BN_free(m);
  2310. return ret;
  2311. }
  2312. static int test_smallprime(int kBits)
  2313. {
  2314. BIGNUM *r;
  2315. int st = 0;
  2316. if (!TEST_ptr(r = BN_new()))
  2317. goto err;
  2318. if (kBits <= 1) {
  2319. if (!TEST_false(BN_generate_prime_ex(r, kBits, 0,
  2320. NULL, NULL, NULL)))
  2321. goto err;
  2322. } else {
  2323. if (!TEST_true(BN_generate_prime_ex(r, kBits, 0,
  2324. NULL, NULL, NULL))
  2325. || !TEST_int_eq(BN_num_bits(r), kBits))
  2326. goto err;
  2327. }
  2328. st = 1;
  2329. err:
  2330. BN_free(r);
  2331. return st;
  2332. }
  2333. static int test_smallsafeprime(int kBits)
  2334. {
  2335. BIGNUM *r;
  2336. int st = 0;
  2337. if (!TEST_ptr(r = BN_new()))
  2338. goto err;
  2339. if (kBits <= 5 && kBits != 3) {
  2340. if (!TEST_false(BN_generate_prime_ex(r, kBits, 1,
  2341. NULL, NULL, NULL)))
  2342. goto err;
  2343. } else {
  2344. if (!TEST_true(BN_generate_prime_ex(r, kBits, 1,
  2345. NULL, NULL, NULL))
  2346. || !TEST_int_eq(BN_num_bits(r), kBits))
  2347. goto err;
  2348. }
  2349. st = 1;
  2350. err:
  2351. BN_free(r);
  2352. return st;
  2353. }
  2354. static int primes[] = { 2, 3, 5, 7, 17863 };
  2355. static int test_is_prime(int i)
  2356. {
  2357. int ret = 0;
  2358. BIGNUM *r = NULL;
  2359. int trial;
  2360. if (!TEST_ptr(r = BN_new()))
  2361. goto err;
  2362. for (trial = 0; trial <= 1; ++trial) {
  2363. if (!TEST_true(BN_set_word(r, primes[i]))
  2364. || !TEST_int_eq(BN_check_prime(r, ctx, NULL),
  2365. 1))
  2366. goto err;
  2367. }
  2368. ret = 1;
  2369. err:
  2370. BN_free(r);
  2371. return ret;
  2372. }
  2373. static int not_primes[] = { -1, 0, 1, 4 };
  2374. static int test_not_prime(int i)
  2375. {
  2376. int ret = 0;
  2377. BIGNUM *r = NULL;
  2378. int trial;
  2379. if (!TEST_ptr(r = BN_new()))
  2380. goto err;
  2381. for (trial = 0; trial <= 1; ++trial) {
  2382. if (!TEST_true(BN_set_word(r, not_primes[i]))
  2383. || !TEST_false(BN_check_prime(r, ctx, NULL)))
  2384. goto err;
  2385. }
  2386. ret = 1;
  2387. err:
  2388. BN_free(r);
  2389. return ret;
  2390. }
  2391. static int test_ctx_set_ct_flag(BN_CTX *c)
  2392. {
  2393. int st = 0;
  2394. size_t i;
  2395. BIGNUM *b[15];
  2396. BN_CTX_start(c);
  2397. for (i = 0; i < OSSL_NELEM(b); i++) {
  2398. if (!TEST_ptr(b[i] = BN_CTX_get(c)))
  2399. goto err;
  2400. if (i % 2 == 1)
  2401. BN_set_flags(b[i], BN_FLG_CONSTTIME);
  2402. }
  2403. st = 1;
  2404. err:
  2405. BN_CTX_end(c);
  2406. return st;
  2407. }
  2408. static int test_ctx_check_ct_flag(BN_CTX *c)
  2409. {
  2410. int st = 0;
  2411. size_t i;
  2412. BIGNUM *b[30];
  2413. BN_CTX_start(c);
  2414. for (i = 0; i < OSSL_NELEM(b); i++) {
  2415. if (!TEST_ptr(b[i] = BN_CTX_get(c)))
  2416. goto err;
  2417. if (!TEST_false(BN_get_flags(b[i], BN_FLG_CONSTTIME)))
  2418. goto err;
  2419. }
  2420. st = 1;
  2421. err:
  2422. BN_CTX_end(c);
  2423. return st;
  2424. }
  2425. static int test_ctx_consttime_flag(void)
  2426. {
  2427. /*-
  2428. * The constant-time flag should not "leak" among BN_CTX frames:
  2429. *
  2430. * - test_ctx_set_ct_flag() starts a frame in the given BN_CTX and
  2431. * sets the BN_FLG_CONSTTIME flag on some of the BIGNUMs obtained
  2432. * from the frame before ending it.
  2433. * - test_ctx_check_ct_flag() then starts a new frame and gets a
  2434. * number of BIGNUMs from it. In absence of leaks, none of the
  2435. * BIGNUMs in the new frame should have BN_FLG_CONSTTIME set.
  2436. *
  2437. * In actual BN_CTX usage inside libcrypto the leak could happen at
  2438. * any depth level in the BN_CTX stack, with varying results
  2439. * depending on the patterns of sibling trees of nested function
  2440. * calls sharing the same BN_CTX object, and the effect of
  2441. * unintended BN_FLG_CONSTTIME on the called BN_* functions.
  2442. *
  2443. * This simple unit test abstracts away this complexity and verifies
  2444. * that the leak does not happen between two sibling functions
  2445. * sharing the same BN_CTX object at the same level of nesting.
  2446. *
  2447. */
  2448. BN_CTX *nctx = NULL;
  2449. BN_CTX *sctx = NULL;
  2450. size_t i = 0;
  2451. int st = 0;
  2452. if (!TEST_ptr(nctx = BN_CTX_new())
  2453. || !TEST_ptr(sctx = BN_CTX_secure_new()))
  2454. goto err;
  2455. for (i = 0; i < 2; i++) {
  2456. BN_CTX *c = i == 0 ? nctx : sctx;
  2457. if (!TEST_true(test_ctx_set_ct_flag(c))
  2458. || !TEST_true(test_ctx_check_ct_flag(c)))
  2459. goto err;
  2460. }
  2461. st = 1;
  2462. err:
  2463. BN_CTX_free(nctx);
  2464. BN_CTX_free(sctx);
  2465. return st;
  2466. }
  2467. static int test_coprime(void)
  2468. {
  2469. BIGNUM *a = NULL, *b = NULL;
  2470. int ret = 0;
  2471. ret = TEST_ptr(a = BN_new())
  2472. && TEST_ptr(b = BN_new())
  2473. && TEST_true(BN_set_word(a, 66))
  2474. && TEST_true(BN_set_word(b, 99))
  2475. && TEST_int_eq(BN_are_coprime(a, b, ctx), 0)
  2476. && TEST_int_eq(BN_are_coprime(b, a, ctx), 0)
  2477. && TEST_true(BN_set_word(a, 67))
  2478. && TEST_int_eq(BN_are_coprime(a, b, ctx), 1)
  2479. && TEST_int_eq(BN_are_coprime(b, a, ctx), 1);
  2480. BN_free(a);
  2481. BN_free(b);
  2482. return ret;
  2483. }
  2484. static int test_gcd_prime(void)
  2485. {
  2486. BIGNUM *a = NULL, *b = NULL, *gcd = NULL;
  2487. int i, st = 0;
  2488. if (!TEST_ptr(a = BN_new())
  2489. || !TEST_ptr(b = BN_new())
  2490. || !TEST_ptr(gcd = BN_new()))
  2491. goto err;
  2492. if (!TEST_true(BN_generate_prime_ex(a, 1024, 0, NULL, NULL, NULL)))
  2493. goto err;
  2494. for (i = 0; i < NUM_PRIME_TESTS; i++) {
  2495. if (!TEST_true(BN_generate_prime_ex(b, 1024, 0,
  2496. NULL, NULL, NULL))
  2497. || !TEST_true(BN_gcd(gcd, a, b, ctx))
  2498. || !TEST_true(BN_is_one(gcd))
  2499. || !TEST_true(BN_are_coprime(a, b, ctx)))
  2500. goto err;
  2501. }
  2502. st = 1;
  2503. err:
  2504. BN_free(a);
  2505. BN_free(b);
  2506. BN_free(gcd);
  2507. return st;
  2508. }
  2509. typedef struct mod_exp_test_st
  2510. {
  2511. const char *base;
  2512. const char *exp;
  2513. const char *mod;
  2514. const char *res;
  2515. } MOD_EXP_TEST;
  2516. static const MOD_EXP_TEST ModExpTests[] = {
  2517. /* original test vectors for rsaz_512_sqr bug, by OSS-Fuzz */
  2518. {
  2519. "1166180238001879113042182292626169621106255558914000595999312084"
  2520. "4627946820899490684928760491249738643524880720584249698100907201"
  2521. "002086675047927600340800371",
  2522. "8000000000000000000000000000000000000000000000000000000000000000"
  2523. "0000000000000000000000000000000000000000000000000000000000000000"
  2524. "00000000",
  2525. "1340780792684523720980737645613191762604395855615117867483316354"
  2526. "3294276330515137663421134775482798690129946803802212663956180562"
  2527. "088664022929883876655300863",
  2528. "8243904058268085430037326628480645845409758077568738532059032482"
  2529. "8294114415890603594730158120426756266457928475330450251339773498"
  2530. "26758407619521544102068438"
  2531. },
  2532. {
  2533. "4974270041410803822078866696159586946995877618987010219312844726"
  2534. "0284386121835740784990869050050504348861513337232530490826340663"
  2535. "197278031692737429054",
  2536. "4974270041410803822078866696159586946995877428188754995041148539"
  2537. "1663243362592271353668158565195557417149981094324650322556843202"
  2538. "946445882670777892608",
  2539. "1340780716511420227215592830971452482815377482627251725537099028"
  2540. "4429769497230131760206012644403029349547320953206103351725462999"
  2541. "947509743623340557059752191",
  2542. "5296244594780707015616522701706118082963369547253192207884519362"
  2543. "1767869984947542695665420219028522815539559194793619684334900442"
  2544. "49304558011362360473525933"
  2545. },
  2546. /* test vectors for rsaz_512_srq bug, with rcx/rbx=1 */
  2547. { /* between first and second iteration */
  2548. "5148719036160389201525610950887605325980251964889646556085286545"
  2549. "3931548809178823413169359635978762036512397113080988070677858033"
  2550. "36463909753993540214027190",
  2551. "6703903964971298549787012499102923063739682910296196688861780721"
  2552. "8608820150367734884009371490834517138450159290932430254268769414"
  2553. "05973284973216824503042158",
  2554. "6703903964971298549787012499102923063739682910296196688861780721"
  2555. "8608820150367734884009371490834517138450159290932430254268769414"
  2556. "05973284973216824503042159",
  2557. "1"
  2558. },
  2559. { /* between second and third iteration */
  2560. "8908340854353752577419678771330460827942371434853054158622636544"
  2561. "8151360109722890949471912566649465436296659601091730745087014189"
  2562. "2672764191218875181826063",
  2563. "6703903964971298549787012499102923063739682910296196688861780721"
  2564. "8608820150367734884009371490834517138450159290932430254268769414"
  2565. "05973284973216824503042158",
  2566. "6703903964971298549787012499102923063739682910296196688861780721"
  2567. "8608820150367734884009371490834517138450159290932430254268769414"
  2568. "05973284973216824503042159",
  2569. "1"
  2570. },
  2571. { /* between third and fourth iteration */
  2572. "3427446396505596330634350984901719674479522569002785244080234738"
  2573. "4288743635435746136297299366444548736533053717416735379073185344"
  2574. "26985272974404612945608761",
  2575. "6703903964971298549787012499102923063739682910296196688861780721"
  2576. "8608820150367734884009371490834517138450159290932430254268769414"
  2577. "05973284973216824503042158",
  2578. "6703903964971298549787012499102923063739682910296196688861780721"
  2579. "8608820150367734884009371490834517138450159290932430254268769414"
  2580. "05973284973216824503042159",
  2581. "1"
  2582. },
  2583. { /* between fourth and fifth iteration */
  2584. "3472743044917564564078857826111874560045331237315597383869652985"
  2585. "6919870028890895988478351133601517365908445058405433832718206902"
  2586. "4088133164805266956353542",
  2587. "6703903964971298549787012499102923063739682910296196688861780721"
  2588. "8608820150367734884009371490834517138450159290932430254268769414"
  2589. "05973284973216824503042158",
  2590. "6703903964971298549787012499102923063739682910296196688861780721"
  2591. "8608820150367734884009371490834517138450159290932430254268769414"
  2592. "05973284973216824503042159",
  2593. "1"
  2594. },
  2595. { /* between fifth and sixth iteration */
  2596. "3608632990153469264412378349742339216742409743898601587274768025"
  2597. "0110772032985643555192767717344946174122842255204082586753499651"
  2598. "14483434992887431333675068",
  2599. "6703903964971298549787012499102923063739682910296196688861780721"
  2600. "8608820150367734884009371490834517138450159290932430254268769414"
  2601. "05973284973216824503042158",
  2602. "6703903964971298549787012499102923063739682910296196688861780721"
  2603. "8608820150367734884009371490834517138450159290932430254268769414"
  2604. "05973284973216824503042159",
  2605. "1"
  2606. },
  2607. { /* between sixth and seventh iteration */
  2608. "8455374370234070242910508226941981520235709767260723212165264877"
  2609. "8689064388017521524568434328264431772644802567028663962962025746"
  2610. "9283458217850119569539086",
  2611. "6703903964971298549787012499102923063739682910296196688861780721"
  2612. "8608820150367734884009371490834517138450159290932430254268769414"
  2613. "05973284973216824503042158",
  2614. "6703903964971298549787012499102923063739682910296196688861780721"
  2615. "8608820150367734884009371490834517138450159290932430254268769414"
  2616. "05973284973216824503042159",
  2617. "1"
  2618. },
  2619. { /* between seventh and eighth iteration */
  2620. "5155371529688532178421209781159131443543419764974688878527112131"
  2621. "7446518205609427412336183157918981038066636807317733319323257603"
  2622. "04416292040754017461076359",
  2623. "1005585594745694782468051874865438459560952436544429503329267108"
  2624. "2791323022555160232601405723625177570767523893639864538140315412"
  2625. "108959927459825236754563832",
  2626. "1005585594745694782468051874865438459560952436544429503329267108"
  2627. "2791323022555160232601405723625177570767523893639864538140315412"
  2628. "108959927459825236754563833",
  2629. "1"
  2630. },
  2631. /* test vectors for rsaz_512_srq bug, with rcx/rbx=2 */
  2632. { /* between first and second iteration */
  2633. "3155666506033786929967309937640790361084670559125912405342594979"
  2634. "4345142818528956285490897841406338022378565972533508820577760065"
  2635. "58494345853302083699912572",
  2636. "6703903964971298549787012499102923063739682910296196688861780721"
  2637. "8608820150367734884009371490834517138450159290932430254268769414"
  2638. "05973284973216824503042158",
  2639. "6703903964971298549787012499102923063739682910296196688861780721"
  2640. "8608820150367734884009371490834517138450159290932430254268769414"
  2641. "05973284973216824503042159",
  2642. "1"
  2643. },
  2644. { /* between second and third iteration */
  2645. "3789819583801342198190405714582958759005991915505282362397087750"
  2646. "4213544724644823098843135685133927198668818185338794377239590049"
  2647. "41019388529192775771488319",
  2648. "6703903964971298549787012499102923063739682910296196688861780721"
  2649. "8608820150367734884009371490834517138450159290932430254268769414"
  2650. "05973284973216824503042158",
  2651. "6703903964971298549787012499102923063739682910296196688861780721"
  2652. "8608820150367734884009371490834517138450159290932430254268769414"
  2653. "05973284973216824503042159",
  2654. "1"
  2655. },
  2656. { /* between third and forth iteration */
  2657. "4695752552040706867080542538786056470322165281761525158189220280"
  2658. "4025547447667484759200742764246905647644662050122968912279199065"
  2659. "48065034299166336940507214",
  2660. "6703903964971298549787012499102923063739682910296196688861780721"
  2661. "8608820150367734884009371490834517138450159290932430254268769414"
  2662. "05973284973216824503042158",
  2663. "6703903964971298549787012499102923063739682910296196688861780721"
  2664. "8608820150367734884009371490834517138450159290932430254268769414"
  2665. "05973284973216824503042159",
  2666. "1"
  2667. },
  2668. { /* between forth and fifth iteration */
  2669. "2159140240970485794188159431017382878636879856244045329971239574"
  2670. "8919691133560661162828034323196457386059819832804593989740268964"
  2671. "74502911811812651475927076",
  2672. "6703903964971298549787012499102923063739682910296196688861780721"
  2673. "8608820150367734884009371490834517138450159290932430254268769414"
  2674. "05973284973216824503042158",
  2675. "6703903964971298549787012499102923063739682910296196688861780721"
  2676. "8608820150367734884009371490834517138450159290932430254268769414"
  2677. "05973284973216824503042159",
  2678. "1"
  2679. },
  2680. { /* between fifth and sixth iteration */
  2681. "5239312332984325668414624633307915097111691815000872662334695514"
  2682. "5436533521392362443557163429336808208137221322444780490437871903"
  2683. "99972784701334569424519255",
  2684. "6703903964971298549787012499102923063739682910296196688861780721"
  2685. "8608820150367734884009371490834517138450159290932430254268769414"
  2686. "05973284973216824503042158",
  2687. "6703903964971298549787012499102923063739682910296196688861780721"
  2688. "8608820150367734884009371490834517138450159290932430254268769414"
  2689. "05973284973216824503042159",
  2690. "1"
  2691. },
  2692. { /* between sixth and seventh iteration */
  2693. "1977953647322612860406858017869125467496941904523063466791308891"
  2694. "1172796739058531929470539758361774569875505293428856181093904091"
  2695. "33788264851714311303725089",
  2696. "6703903964971298549787012499102923063739682910296196688861780721"
  2697. "8608820150367734884009371490834517138450159290932430254268769414"
  2698. "05973284973216824503042158",
  2699. "6703903964971298549787012499102923063739682910296196688861780721"
  2700. "8608820150367734884009371490834517138450159290932430254268769414"
  2701. "05973284973216824503042159",
  2702. "1"
  2703. },
  2704. { /* between seventh and eighth iteration */
  2705. "6456987954117763835533395796948878140715006860263624787492985786"
  2706. "8514630216966738305923915688821526449499763719943997120302368211"
  2707. "04813318117996225041943964",
  2708. "1340780792994259709957402499820584612747936582059239337772356144"
  2709. "3721764030073546976801874298166903427690031858186486050853753882"
  2710. "811946551499689575296532556",
  2711. "1340780792994259709957402499820584612747936582059239337772356144"
  2712. "3721764030073546976801874298166903427690031858186486050853753882"
  2713. "811946551499689575296532557",
  2714. "1"
  2715. }
  2716. };
  2717. static int test_mod_exp(int i)
  2718. {
  2719. const MOD_EXP_TEST *test = &ModExpTests[i];
  2720. int res = 0;
  2721. BIGNUM* result = NULL;
  2722. BIGNUM *base = NULL, *exponent = NULL, *modulo = NULL;
  2723. char *s = NULL;
  2724. if (!TEST_ptr(result = BN_new())
  2725. || !TEST_true(BN_dec2bn(&base, test->base))
  2726. || !TEST_true(BN_dec2bn(&exponent, test->exp))
  2727. || !TEST_true(BN_dec2bn(&modulo, test->mod)))
  2728. goto err;
  2729. if (!TEST_int_eq(BN_mod_exp(result, base, exponent, modulo, ctx), 1))
  2730. goto err;
  2731. if (!TEST_ptr(s = BN_bn2dec(result)))
  2732. goto err;
  2733. if (!TEST_mem_eq(s, strlen(s), test->res, strlen(test->res)))
  2734. goto err;
  2735. res = 1;
  2736. err:
  2737. OPENSSL_free(s);
  2738. BN_free(result);
  2739. BN_free(base);
  2740. BN_free(exponent);
  2741. BN_free(modulo);
  2742. return res;
  2743. }
  2744. static int test_mod_exp_consttime(int i)
  2745. {
  2746. const MOD_EXP_TEST *test = &ModExpTests[i];
  2747. int res = 0;
  2748. BIGNUM* result = NULL;
  2749. BIGNUM *base = NULL, *exponent = NULL, *modulo = NULL;
  2750. char *s = NULL;
  2751. if (!TEST_ptr(result = BN_new())
  2752. || !TEST_true(BN_dec2bn(&base, test->base))
  2753. || !TEST_true(BN_dec2bn(&exponent, test->exp))
  2754. || !TEST_true(BN_dec2bn(&modulo, test->mod)))
  2755. goto err;
  2756. BN_set_flags(base, BN_FLG_CONSTTIME);
  2757. BN_set_flags(exponent, BN_FLG_CONSTTIME);
  2758. BN_set_flags(modulo, BN_FLG_CONSTTIME);
  2759. if (!TEST_int_eq(BN_mod_exp(result, base, exponent, modulo, ctx), 1))
  2760. goto err;
  2761. if (!TEST_ptr(s = BN_bn2dec(result)))
  2762. goto err;
  2763. if (!TEST_mem_eq(s, strlen(s), test->res, strlen(test->res)))
  2764. goto err;
  2765. res = 1;
  2766. err:
  2767. OPENSSL_free(s);
  2768. BN_free(result);
  2769. BN_free(base);
  2770. BN_free(exponent);
  2771. BN_free(modulo);
  2772. return res;
  2773. }
  2774. /*
  2775. * Regression test to ensure BN_mod_exp2_mont fails safely if argument m is
  2776. * zero.
  2777. */
  2778. static int test_mod_exp2_mont(void)
  2779. {
  2780. int res = 0;
  2781. BIGNUM *exp_result = NULL;
  2782. BIGNUM *exp_a1 = NULL, *exp_p1 = NULL, *exp_a2 = NULL, *exp_p2 = NULL,
  2783. *exp_m = NULL;
  2784. if (!TEST_ptr(exp_result = BN_new())
  2785. || !TEST_ptr(exp_a1 = BN_new())
  2786. || !TEST_ptr(exp_p1 = BN_new())
  2787. || !TEST_ptr(exp_a2 = BN_new())
  2788. || !TEST_ptr(exp_p2 = BN_new())
  2789. || !TEST_ptr(exp_m = BN_new()))
  2790. goto err;
  2791. if (!TEST_true(BN_one(exp_a1))
  2792. || !TEST_true(BN_one(exp_p1))
  2793. || !TEST_true(BN_one(exp_a2))
  2794. || !TEST_true(BN_one(exp_p2)))
  2795. goto err;
  2796. BN_zero(exp_m);
  2797. /* input of 0 is even, so must fail */
  2798. if (!TEST_int_eq(BN_mod_exp2_mont(exp_result, exp_a1, exp_p1, exp_a2,
  2799. exp_p2, exp_m, ctx, NULL), 0))
  2800. goto err;
  2801. res = 1;
  2802. err:
  2803. BN_free(exp_result);
  2804. BN_free(exp_a1);
  2805. BN_free(exp_p1);
  2806. BN_free(exp_a2);
  2807. BN_free(exp_p2);
  2808. BN_free(exp_m);
  2809. return res;
  2810. }
  2811. static int test_mod_inverse(void)
  2812. {
  2813. int res = 0;
  2814. char *str = NULL;
  2815. BIGNUM *a = NULL;
  2816. BIGNUM *b = NULL;
  2817. BIGNUM *r = NULL;
  2818. if (!TEST_true(BN_dec2bn(&a, "5193817943")))
  2819. goto err;
  2820. if (!TEST_true(BN_dec2bn(&b, "3259122431")))
  2821. goto err;
  2822. if (!TEST_ptr(r = BN_new()))
  2823. goto err;
  2824. if (!TEST_ptr_eq(BN_mod_inverse(r, a, b, ctx), r))
  2825. goto err;
  2826. if (!TEST_ptr_ne(str = BN_bn2dec(r), NULL))
  2827. goto err;
  2828. if (!TEST_int_eq(strcmp(str, "2609653924"), 0))
  2829. goto err;
  2830. /* Note that this aliases the result with the modulus. */
  2831. if (!TEST_ptr_null(BN_mod_inverse(b, a, b, ctx)))
  2832. goto err;
  2833. res = 1;
  2834. err:
  2835. BN_free(a);
  2836. BN_free(b);
  2837. BN_free(r);
  2838. OPENSSL_free(str);
  2839. return res;
  2840. }
  2841. static int test_mod_exp_alias(int idx)
  2842. {
  2843. int res = 0;
  2844. char *str = NULL;
  2845. BIGNUM *a = NULL;
  2846. BIGNUM *b = NULL;
  2847. BIGNUM *c = NULL;
  2848. BIGNUM *r = NULL;
  2849. if (!TEST_true(BN_dec2bn(&a, "15")))
  2850. goto err;
  2851. if (!TEST_true(BN_dec2bn(&b, "10")))
  2852. goto err;
  2853. if (!TEST_true(BN_dec2bn(&c, "39")))
  2854. goto err;
  2855. if (!TEST_ptr(r = BN_new()))
  2856. goto err;
  2857. if (!TEST_int_eq((idx == 0 ? BN_mod_exp_simple
  2858. : BN_mod_exp_recp)(r, a, b, c, ctx), 1))
  2859. goto err;
  2860. if (!TEST_ptr_ne(str = BN_bn2dec(r), NULL))
  2861. goto err;
  2862. if (!TEST_str_eq(str, "36"))
  2863. goto err;
  2864. OPENSSL_free(str);
  2865. str = NULL;
  2866. BN_copy(r, b);
  2867. /* Aliasing with exponent must work. */
  2868. if (!TEST_int_eq((idx == 0 ? BN_mod_exp_simple
  2869. : BN_mod_exp_recp)(r, a, r, c, ctx), 1))
  2870. goto err;
  2871. if (!TEST_ptr_ne(str = BN_bn2dec(r), NULL))
  2872. goto err;
  2873. if (!TEST_str_eq(str, "36"))
  2874. goto err;
  2875. OPENSSL_free(str);
  2876. str = NULL;
  2877. /* Aliasing with modulus should return failure for the simple call. */
  2878. if (idx == 0) {
  2879. if (!TEST_int_eq(BN_mod_exp_simple(c, a, b, c, ctx), 0))
  2880. goto err;
  2881. } else {
  2882. if (!TEST_int_eq(BN_mod_exp_recp(c, a, b, c, ctx), 1))
  2883. goto err;
  2884. if (!TEST_ptr_ne(str = BN_bn2dec(c), NULL))
  2885. goto err;
  2886. if (!TEST_str_eq(str, "36"))
  2887. goto err;
  2888. }
  2889. res = 1;
  2890. err:
  2891. BN_free(a);
  2892. BN_free(b);
  2893. BN_free(c);
  2894. BN_free(r);
  2895. OPENSSL_free(str);
  2896. return res;
  2897. }
  2898. static int file_test_run(STANZA *s)
  2899. {
  2900. static const FILETEST filetests[] = {
  2901. {"Sum", file_sum},
  2902. {"LShift1", file_lshift1},
  2903. {"LShift", file_lshift},
  2904. {"RShift", file_rshift},
  2905. {"Square", file_square},
  2906. {"Product", file_product},
  2907. {"Quotient", file_quotient},
  2908. {"ModMul", file_modmul},
  2909. {"ModExp", file_modexp},
  2910. {"Exp", file_exp},
  2911. {"ModSqrt", file_modsqrt},
  2912. {"GCD", file_gcd},
  2913. };
  2914. int numtests = OSSL_NELEM(filetests);
  2915. const FILETEST *tp = filetests;
  2916. for ( ; --numtests >= 0; tp++) {
  2917. if (findattr(s, tp->name) != NULL) {
  2918. if (!tp->func(s)) {
  2919. TEST_info("%s:%d: Failed %s test",
  2920. s->test_file, s->start, tp->name);
  2921. return 0;
  2922. }
  2923. return 1;
  2924. }
  2925. }
  2926. TEST_info("%s:%d: Unknown test", s->test_file, s->start);
  2927. return 0;
  2928. }
  2929. static int run_file_tests(int i)
  2930. {
  2931. STANZA *s = NULL;
  2932. char *testfile = test_get_argument(i);
  2933. int c;
  2934. if (!TEST_ptr(s = OPENSSL_zalloc(sizeof(*s))))
  2935. return 0;
  2936. if (!test_start_file(s, testfile)) {
  2937. OPENSSL_free(s);
  2938. return 0;
  2939. }
  2940. /* Read test file. */
  2941. while (!BIO_eof(s->fp) && test_readstanza(s)) {
  2942. if (s->numpairs == 0)
  2943. continue;
  2944. if (!file_test_run(s))
  2945. s->errors++;
  2946. s->numtests++;
  2947. test_clearstanza(s);
  2948. }
  2949. test_end_file(s);
  2950. c = s->errors;
  2951. OPENSSL_free(s);
  2952. return c == 0;
  2953. }
  2954. typedef enum OPTION_choice {
  2955. OPT_ERR = -1,
  2956. OPT_EOF = 0,
  2957. OPT_STOCHASTIC_TESTS,
  2958. OPT_TEST_ENUM
  2959. } OPTION_CHOICE;
  2960. const OPTIONS *test_get_options(void)
  2961. {
  2962. static const OPTIONS test_options[] = {
  2963. OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("[file...]\n"),
  2964. { "stochastic", OPT_STOCHASTIC_TESTS, '-', "Run stochastic tests" },
  2965. { OPT_HELP_STR, 1, '-',
  2966. "file\tFile to run tests on. Normal tests are not run\n" },
  2967. { NULL }
  2968. };
  2969. return test_options;
  2970. }
  2971. int setup_tests(void)
  2972. {
  2973. OPTION_CHOICE o;
  2974. int n, stochastic = 0;
  2975. while ((o = opt_next()) != OPT_EOF) {
  2976. switch (o) {
  2977. case OPT_STOCHASTIC_TESTS:
  2978. stochastic = 1;
  2979. break;
  2980. case OPT_TEST_CASES:
  2981. break;
  2982. default:
  2983. case OPT_ERR:
  2984. return 0;
  2985. }
  2986. }
  2987. n = test_get_argument_count();
  2988. if (!TEST_ptr(ctx = BN_CTX_new()))
  2989. return 0;
  2990. if (n == 0) {
  2991. ADD_TEST(test_sub);
  2992. ADD_TEST(test_div_recip);
  2993. ADD_ALL_TESTS(test_signed_mod_replace_ab, OSSL_NELEM(signed_mod_tests));
  2994. ADD_ALL_TESTS(test_signed_mod_replace_ba, OSSL_NELEM(signed_mod_tests));
  2995. ADD_TEST(test_mod);
  2996. ADD_TEST(test_mod_inverse);
  2997. ADD_ALL_TESTS(test_mod_exp_alias, 2);
  2998. ADD_TEST(test_modexp_mont5);
  2999. ADD_TEST(test_kronecker);
  3000. ADD_TEST(test_rand);
  3001. ADD_TEST(test_bn2padded);
  3002. ADD_TEST(test_dec2bn);
  3003. ADD_TEST(test_hex2bn);
  3004. ADD_TEST(test_asc2bn);
  3005. ADD_TEST(test_bin2zero);
  3006. ADD_TEST(test_bin2bn_lengths);
  3007. ADD_ALL_TESTS(test_mpi, (int)OSSL_NELEM(kMPITests));
  3008. ADD_ALL_TESTS(test_bn2signed, (int)OSSL_NELEM(kSignedTests_BE));
  3009. ADD_TEST(test_negzero);
  3010. ADD_TEST(test_badmod);
  3011. ADD_TEST(test_expmodzero);
  3012. ADD_TEST(test_expmodone);
  3013. ADD_ALL_TESTS(test_smallprime, 16);
  3014. ADD_ALL_TESTS(test_smallsafeprime, 16);
  3015. ADD_TEST(test_swap);
  3016. ADD_TEST(test_ctx_consttime_flag);
  3017. #ifndef OPENSSL_NO_EC2M
  3018. ADD_TEST(test_gf2m_add);
  3019. ADD_TEST(test_gf2m_mod);
  3020. ADD_TEST(test_gf2m_mul);
  3021. ADD_TEST(test_gf2m_sqr);
  3022. ADD_TEST(test_gf2m_modinv);
  3023. ADD_TEST(test_gf2m_moddiv);
  3024. ADD_TEST(test_gf2m_modexp);
  3025. ADD_TEST(test_gf2m_modsqrt);
  3026. ADD_TEST(test_gf2m_modsolvequad);
  3027. #endif
  3028. ADD_ALL_TESTS(test_is_prime, (int)OSSL_NELEM(primes));
  3029. ADD_ALL_TESTS(test_not_prime, (int)OSSL_NELEM(not_primes));
  3030. ADD_TEST(test_gcd_prime);
  3031. ADD_TEST(test_coprime);
  3032. ADD_ALL_TESTS(test_mod_exp, (int)OSSL_NELEM(ModExpTests));
  3033. ADD_ALL_TESTS(test_mod_exp_consttime, (int)OSSL_NELEM(ModExpTests));
  3034. ADD_TEST(test_mod_exp2_mont);
  3035. if (stochastic)
  3036. ADD_TEST(test_rand_range);
  3037. } else {
  3038. ADD_ALL_TESTS(run_file_tests, n);
  3039. }
  3040. return 1;
  3041. }
  3042. void cleanup_tests(void)
  3043. {
  3044. BN_CTX_free(ctx);
  3045. }