2
0

bntest.c 105 KB

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