bntest.c 100 KB

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