bntest.c 90 KB

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