bntest.c 88 KB

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