bntest.c 96 KB

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