bntest.c 92 KB

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