bntest.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160
  1. /* crypto/bn/bntest.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* ====================================================================
  59. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  60. *
  61. * Portions of the attached software ("Contribution") are developed by
  62. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  63. *
  64. * The Contribution is licensed pursuant to the Eric Young open source
  65. * license provided above.
  66. *
  67. * The binary polynomial arithmetic software is originally written by
  68. * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
  69. *
  70. */
  71. /*
  72. * Until the key-gen callbacks are modified to use newer prototypes, we allow
  73. * deprecated functions for openssl-internal code
  74. */
  75. #ifdef OPENSSL_NO_DEPRECATED
  76. # undef OPENSSL_NO_DEPRECATED
  77. #endif
  78. #include <stdio.h>
  79. #include <stdlib.h>
  80. #include <string.h>
  81. #include "e_os.h"
  82. #include <openssl/bio.h>
  83. #include <openssl/bn.h>
  84. #include <openssl/rand.h>
  85. #include <openssl/x509.h>
  86. #include <openssl/err.h>
  87. const int num0 = 100; /* number of tests */
  88. const int num1 = 50; /* additional tests for some functions */
  89. const int num2 = 5; /* number of tests for slow functions */
  90. int test_add(BIO *bp);
  91. int test_sub(BIO *bp);
  92. int test_lshift1(BIO *bp);
  93. int test_lshift(BIO *bp, BN_CTX *ctx, BIGNUM *a_);
  94. int test_rshift1(BIO *bp);
  95. int test_rshift(BIO *bp, BN_CTX *ctx);
  96. int test_div(BIO *bp, BN_CTX *ctx);
  97. int test_div_word(BIO *bp);
  98. int test_div_recp(BIO *bp, BN_CTX *ctx);
  99. int test_mul(BIO *bp);
  100. int test_sqr(BIO *bp, BN_CTX *ctx);
  101. int test_mont(BIO *bp, BN_CTX *ctx);
  102. int test_mod(BIO *bp, BN_CTX *ctx);
  103. int test_mod_mul(BIO *bp, BN_CTX *ctx);
  104. int test_mod_exp(BIO *bp, BN_CTX *ctx);
  105. int test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx);
  106. int test_mod_exp_mont5(BIO *bp, BN_CTX *ctx);
  107. int test_exp(BIO *bp, BN_CTX *ctx);
  108. int test_gf2m_add(BIO *bp);
  109. int test_gf2m_mod(BIO *bp);
  110. int test_gf2m_mod_mul(BIO *bp, BN_CTX *ctx);
  111. int test_gf2m_mod_sqr(BIO *bp, BN_CTX *ctx);
  112. int test_gf2m_mod_inv(BIO *bp, BN_CTX *ctx);
  113. int test_gf2m_mod_div(BIO *bp, BN_CTX *ctx);
  114. int test_gf2m_mod_exp(BIO *bp, BN_CTX *ctx);
  115. int test_gf2m_mod_sqrt(BIO *bp, BN_CTX *ctx);
  116. int test_gf2m_mod_solve_quad(BIO *bp, BN_CTX *ctx);
  117. int test_kron(BIO *bp, BN_CTX *ctx);
  118. int test_sqrt(BIO *bp, BN_CTX *ctx);
  119. int rand_neg(void);
  120. static int results = 0;
  121. static unsigned char lst[] =
  122. "\xC6\x4F\x43\x04\x2A\xEA\xCA\x6E\x58\x36\x80\x5B\xE8\xC9"
  123. "\x9B\x04\x5D\x48\x36\xC2\xFD\x16\xC9\x64\xF0";
  124. static const char rnd_seed[] =
  125. "string to make the random number generator think it has entropy";
  126. static void message(BIO *out, char *m)
  127. {
  128. fprintf(stderr, "test %s\n", m);
  129. BIO_puts(out, "print \"test ");
  130. BIO_puts(out, m);
  131. BIO_puts(out, "\\n\"\n");
  132. }
  133. int main(int argc, char *argv[])
  134. {
  135. BN_CTX *ctx;
  136. BIO *out;
  137. char *outfile = NULL;
  138. results = 0;
  139. RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime may fail */
  140. argc--;
  141. argv++;
  142. while (argc >= 1) {
  143. if (strcmp(*argv, "-results") == 0)
  144. results = 1;
  145. else if (strcmp(*argv, "-out") == 0) {
  146. if (--argc < 1)
  147. break;
  148. outfile = *(++argv);
  149. }
  150. argc--;
  151. argv++;
  152. }
  153. ctx = BN_CTX_new();
  154. if (ctx == NULL)
  155. EXIT(1);
  156. out = BIO_new(BIO_s_file());
  157. if (out == NULL)
  158. EXIT(1);
  159. if (outfile == NULL) {
  160. BIO_set_fp(out, stdout, BIO_NOCLOSE);
  161. } else {
  162. if (!BIO_write_filename(out, outfile)) {
  163. perror(outfile);
  164. EXIT(1);
  165. }
  166. }
  167. if (!results)
  168. BIO_puts(out, "obase=16\nibase=16\n");
  169. message(out, "BN_add");
  170. if (!test_add(out))
  171. goto err;
  172. (void)BIO_flush(out);
  173. message(out, "BN_sub");
  174. if (!test_sub(out))
  175. goto err;
  176. (void)BIO_flush(out);
  177. message(out, "BN_lshift1");
  178. if (!test_lshift1(out))
  179. goto err;
  180. (void)BIO_flush(out);
  181. message(out, "BN_lshift (fixed)");
  182. if (!test_lshift(out, ctx, BN_bin2bn(lst, sizeof(lst) - 1, NULL)))
  183. goto err;
  184. (void)BIO_flush(out);
  185. message(out, "BN_lshift");
  186. if (!test_lshift(out, ctx, NULL))
  187. goto err;
  188. (void)BIO_flush(out);
  189. message(out, "BN_rshift1");
  190. if (!test_rshift1(out))
  191. goto err;
  192. (void)BIO_flush(out);
  193. message(out, "BN_rshift");
  194. if (!test_rshift(out, ctx))
  195. goto err;
  196. (void)BIO_flush(out);
  197. message(out, "BN_sqr");
  198. if (!test_sqr(out, ctx))
  199. goto err;
  200. (void)BIO_flush(out);
  201. message(out, "BN_mul");
  202. if (!test_mul(out))
  203. goto err;
  204. (void)BIO_flush(out);
  205. message(out, "BN_div");
  206. if (!test_div(out, ctx))
  207. goto err;
  208. (void)BIO_flush(out);
  209. message(out, "BN_div_word");
  210. if (!test_div_word(out))
  211. goto err;
  212. (void)BIO_flush(out);
  213. message(out, "BN_div_recp");
  214. if (!test_div_recp(out, ctx))
  215. goto err;
  216. (void)BIO_flush(out);
  217. message(out, "BN_mod");
  218. if (!test_mod(out, ctx))
  219. goto err;
  220. (void)BIO_flush(out);
  221. message(out, "BN_mod_mul");
  222. if (!test_mod_mul(out, ctx))
  223. goto err;
  224. (void)BIO_flush(out);
  225. message(out, "BN_mont");
  226. if (!test_mont(out, ctx))
  227. goto err;
  228. (void)BIO_flush(out);
  229. message(out, "BN_mod_exp");
  230. if (!test_mod_exp(out, ctx))
  231. goto err;
  232. (void)BIO_flush(out);
  233. message(out, "BN_mod_exp_mont_consttime");
  234. if (!test_mod_exp_mont_consttime(out, ctx))
  235. goto err;
  236. if (!test_mod_exp_mont5(out, ctx))
  237. goto err;
  238. (void)BIO_flush(out);
  239. message(out, "BN_exp");
  240. if (!test_exp(out, ctx))
  241. goto err;
  242. (void)BIO_flush(out);
  243. message(out, "BN_kronecker");
  244. if (!test_kron(out, ctx))
  245. goto err;
  246. (void)BIO_flush(out);
  247. message(out, "BN_mod_sqrt");
  248. if (!test_sqrt(out, ctx))
  249. goto err;
  250. (void)BIO_flush(out);
  251. #ifndef OPENSSL_NO_EC2M
  252. message(out, "BN_GF2m_add");
  253. if (!test_gf2m_add(out))
  254. goto err;
  255. (void)BIO_flush(out);
  256. message(out, "BN_GF2m_mod");
  257. if (!test_gf2m_mod(out))
  258. goto err;
  259. (void)BIO_flush(out);
  260. message(out, "BN_GF2m_mod_mul");
  261. if (!test_gf2m_mod_mul(out, ctx))
  262. goto err;
  263. (void)BIO_flush(out);
  264. message(out, "BN_GF2m_mod_sqr");
  265. if (!test_gf2m_mod_sqr(out, ctx))
  266. goto err;
  267. (void)BIO_flush(out);
  268. message(out, "BN_GF2m_mod_inv");
  269. if (!test_gf2m_mod_inv(out, ctx))
  270. goto err;
  271. (void)BIO_flush(out);
  272. message(out, "BN_GF2m_mod_div");
  273. if (!test_gf2m_mod_div(out, ctx))
  274. goto err;
  275. (void)BIO_flush(out);
  276. message(out, "BN_GF2m_mod_exp");
  277. if (!test_gf2m_mod_exp(out, ctx))
  278. goto err;
  279. (void)BIO_flush(out);
  280. message(out, "BN_GF2m_mod_sqrt");
  281. if (!test_gf2m_mod_sqrt(out, ctx))
  282. goto err;
  283. (void)BIO_flush(out);
  284. message(out, "BN_GF2m_mod_solve_quad");
  285. if (!test_gf2m_mod_solve_quad(out, ctx))
  286. goto err;
  287. (void)BIO_flush(out);
  288. #endif
  289. BN_CTX_free(ctx);
  290. BIO_free(out);
  291. EXIT(0);
  292. err:
  293. BIO_puts(out, "1\n"); /* make sure the Perl script fed by bc
  294. * notices the failure, see test_bn in
  295. * test/Makefile.ssl */
  296. (void)BIO_flush(out);
  297. ERR_load_crypto_strings();
  298. ERR_print_errors_fp(stderr);
  299. EXIT(1);
  300. return (1);
  301. }
  302. int test_add(BIO *bp)
  303. {
  304. BIGNUM a, b, c;
  305. int i;
  306. BN_init(&a);
  307. BN_init(&b);
  308. BN_init(&c);
  309. BN_bntest_rand(&a, 512, 0, 0);
  310. for (i = 0; i < num0; i++) {
  311. BN_bntest_rand(&b, 450 + i, 0, 0);
  312. a.neg = rand_neg();
  313. b.neg = rand_neg();
  314. BN_add(&c, &a, &b);
  315. if (bp != NULL) {
  316. if (!results) {
  317. BN_print(bp, &a);
  318. BIO_puts(bp, " + ");
  319. BN_print(bp, &b);
  320. BIO_puts(bp, " - ");
  321. }
  322. BN_print(bp, &c);
  323. BIO_puts(bp, "\n");
  324. }
  325. a.neg = !a.neg;
  326. b.neg = !b.neg;
  327. BN_add(&c, &c, &b);
  328. BN_add(&c, &c, &a);
  329. if (!BN_is_zero(&c)) {
  330. fprintf(stderr, "Add test failed!\n");
  331. return 0;
  332. }
  333. }
  334. BN_free(&a);
  335. BN_free(&b);
  336. BN_free(&c);
  337. return (1);
  338. }
  339. int test_sub(BIO *bp)
  340. {
  341. BIGNUM a, b, c;
  342. int i;
  343. BN_init(&a);
  344. BN_init(&b);
  345. BN_init(&c);
  346. for (i = 0; i < num0 + num1; i++) {
  347. if (i < num1) {
  348. BN_bntest_rand(&a, 512, 0, 0);
  349. BN_copy(&b, &a);
  350. if (BN_set_bit(&a, i) == 0)
  351. return (0);
  352. BN_add_word(&b, i);
  353. } else {
  354. BN_bntest_rand(&b, 400 + i - num1, 0, 0);
  355. a.neg = rand_neg();
  356. b.neg = rand_neg();
  357. }
  358. BN_sub(&c, &a, &b);
  359. if (bp != NULL) {
  360. if (!results) {
  361. BN_print(bp, &a);
  362. BIO_puts(bp, " - ");
  363. BN_print(bp, &b);
  364. BIO_puts(bp, " - ");
  365. }
  366. BN_print(bp, &c);
  367. BIO_puts(bp, "\n");
  368. }
  369. BN_add(&c, &c, &b);
  370. BN_sub(&c, &c, &a);
  371. if (!BN_is_zero(&c)) {
  372. fprintf(stderr, "Subtract test failed!\n");
  373. return 0;
  374. }
  375. }
  376. BN_free(&a);
  377. BN_free(&b);
  378. BN_free(&c);
  379. return (1);
  380. }
  381. int test_div(BIO *bp, BN_CTX *ctx)
  382. {
  383. BIGNUM a, b, c, d, e;
  384. int i;
  385. BN_init(&a);
  386. BN_init(&b);
  387. BN_init(&c);
  388. BN_init(&d);
  389. BN_init(&e);
  390. BN_one(&a);
  391. BN_zero(&b);
  392. if (BN_div(&d, &c, &a, &b, ctx)) {
  393. fprintf(stderr, "Division by zero succeeded!\n");
  394. return 0;
  395. }
  396. for (i = 0; i < num0 + num1; i++) {
  397. if (i < num1) {
  398. BN_bntest_rand(&a, 400, 0, 0);
  399. BN_copy(&b, &a);
  400. BN_lshift(&a, &a, i);
  401. BN_add_word(&a, i);
  402. } else
  403. BN_bntest_rand(&b, 50 + 3 * (i - num1), 0, 0);
  404. a.neg = rand_neg();
  405. b.neg = rand_neg();
  406. BN_div(&d, &c, &a, &b, ctx);
  407. if (bp != NULL) {
  408. if (!results) {
  409. BN_print(bp, &a);
  410. BIO_puts(bp, " / ");
  411. BN_print(bp, &b);
  412. BIO_puts(bp, " - ");
  413. }
  414. BN_print(bp, &d);
  415. BIO_puts(bp, "\n");
  416. if (!results) {
  417. BN_print(bp, &a);
  418. BIO_puts(bp, " % ");
  419. BN_print(bp, &b);
  420. BIO_puts(bp, " - ");
  421. }
  422. BN_print(bp, &c);
  423. BIO_puts(bp, "\n");
  424. }
  425. BN_mul(&e, &d, &b, ctx);
  426. BN_add(&d, &e, &c);
  427. BN_sub(&d, &d, &a);
  428. if (!BN_is_zero(&d)) {
  429. fprintf(stderr, "Division test failed!\n");
  430. return 0;
  431. }
  432. }
  433. BN_free(&a);
  434. BN_free(&b);
  435. BN_free(&c);
  436. BN_free(&d);
  437. BN_free(&e);
  438. return (1);
  439. }
  440. static void print_word(BIO *bp, BN_ULONG w)
  441. {
  442. #ifdef SIXTY_FOUR_BIT
  443. if (sizeof(w) > sizeof(unsigned long)) {
  444. unsigned long h = (unsigned long)(w >> 32), l = (unsigned long)(w);
  445. if (h)
  446. BIO_printf(bp, "%lX%08lX", h, l);
  447. else
  448. BIO_printf(bp, "%lX", l);
  449. return;
  450. }
  451. #endif
  452. BIO_printf(bp, BN_HEX_FMT1, w);
  453. }
  454. int test_div_word(BIO *bp)
  455. {
  456. BIGNUM a, b;
  457. BN_ULONG r, rmod, s;
  458. int i;
  459. BN_init(&a);
  460. BN_init(&b);
  461. for (i = 0; i < num0; i++) {
  462. do {
  463. BN_bntest_rand(&a, 512, -1, 0);
  464. BN_bntest_rand(&b, BN_BITS2, -1, 0);
  465. } while (BN_is_zero(&b));
  466. s = b.d[0];
  467. BN_copy(&b, &a);
  468. rmod = BN_mod_word(&b, s);
  469. r = BN_div_word(&b, s);
  470. if (rmod != r) {
  471. fprintf(stderr, "Mod (word) test failed!\n");
  472. return 0;
  473. }
  474. if (bp != NULL) {
  475. if (!results) {
  476. BN_print(bp, &a);
  477. BIO_puts(bp, " / ");
  478. print_word(bp, s);
  479. BIO_puts(bp, " - ");
  480. }
  481. BN_print(bp, &b);
  482. BIO_puts(bp, "\n");
  483. if (!results) {
  484. BN_print(bp, &a);
  485. BIO_puts(bp, " % ");
  486. print_word(bp, s);
  487. BIO_puts(bp, " - ");
  488. }
  489. print_word(bp, r);
  490. BIO_puts(bp, "\n");
  491. }
  492. BN_mul_word(&b, s);
  493. BN_add_word(&b, r);
  494. BN_sub(&b, &a, &b);
  495. if (!BN_is_zero(&b)) {
  496. fprintf(stderr, "Division (word) test failed!\n");
  497. return 0;
  498. }
  499. }
  500. BN_free(&a);
  501. BN_free(&b);
  502. return (1);
  503. }
  504. int test_div_recp(BIO *bp, BN_CTX *ctx)
  505. {
  506. BIGNUM a, b, c, d, e;
  507. BN_RECP_CTX recp;
  508. int i;
  509. BN_RECP_CTX_init(&recp);
  510. BN_init(&a);
  511. BN_init(&b);
  512. BN_init(&c);
  513. BN_init(&d);
  514. BN_init(&e);
  515. for (i = 0; i < num0 + num1; i++) {
  516. if (i < num1) {
  517. BN_bntest_rand(&a, 400, 0, 0);
  518. BN_copy(&b, &a);
  519. BN_lshift(&a, &a, i);
  520. BN_add_word(&a, i);
  521. } else
  522. BN_bntest_rand(&b, 50 + 3 * (i - num1), 0, 0);
  523. a.neg = rand_neg();
  524. b.neg = rand_neg();
  525. BN_RECP_CTX_set(&recp, &b, ctx);
  526. BN_div_recp(&d, &c, &a, &recp, ctx);
  527. if (bp != NULL) {
  528. if (!results) {
  529. BN_print(bp, &a);
  530. BIO_puts(bp, " / ");
  531. BN_print(bp, &b);
  532. BIO_puts(bp, " - ");
  533. }
  534. BN_print(bp, &d);
  535. BIO_puts(bp, "\n");
  536. if (!results) {
  537. BN_print(bp, &a);
  538. BIO_puts(bp, " % ");
  539. BN_print(bp, &b);
  540. BIO_puts(bp, " - ");
  541. }
  542. BN_print(bp, &c);
  543. BIO_puts(bp, "\n");
  544. }
  545. BN_mul(&e, &d, &b, ctx);
  546. BN_add(&d, &e, &c);
  547. BN_sub(&d, &d, &a);
  548. if (!BN_is_zero(&d)) {
  549. fprintf(stderr, "Reciprocal division test failed!\n");
  550. fprintf(stderr, "a=");
  551. BN_print_fp(stderr, &a);
  552. fprintf(stderr, "\nb=");
  553. BN_print_fp(stderr, &b);
  554. fprintf(stderr, "\n");
  555. return 0;
  556. }
  557. }
  558. BN_free(&a);
  559. BN_free(&b);
  560. BN_free(&c);
  561. BN_free(&d);
  562. BN_free(&e);
  563. BN_RECP_CTX_free(&recp);
  564. return (1);
  565. }
  566. int test_mul(BIO *bp)
  567. {
  568. BIGNUM a, b, c, d, e;
  569. int i;
  570. BN_CTX *ctx;
  571. ctx = BN_CTX_new();
  572. if (ctx == NULL)
  573. EXIT(1);
  574. BN_init(&a);
  575. BN_init(&b);
  576. BN_init(&c);
  577. BN_init(&d);
  578. BN_init(&e);
  579. for (i = 0; i < num0 + num1; i++) {
  580. if (i <= num1) {
  581. BN_bntest_rand(&a, 100, 0, 0);
  582. BN_bntest_rand(&b, 100, 0, 0);
  583. } else
  584. BN_bntest_rand(&b, i - num1, 0, 0);
  585. a.neg = rand_neg();
  586. b.neg = rand_neg();
  587. BN_mul(&c, &a, &b, ctx);
  588. if (bp != NULL) {
  589. if (!results) {
  590. BN_print(bp, &a);
  591. BIO_puts(bp, " * ");
  592. BN_print(bp, &b);
  593. BIO_puts(bp, " - ");
  594. }
  595. BN_print(bp, &c);
  596. BIO_puts(bp, "\n");
  597. }
  598. BN_div(&d, &e, &c, &a, ctx);
  599. BN_sub(&d, &d, &b);
  600. if (!BN_is_zero(&d) || !BN_is_zero(&e)) {
  601. fprintf(stderr, "Multiplication test failed!\n");
  602. return 0;
  603. }
  604. }
  605. BN_free(&a);
  606. BN_free(&b);
  607. BN_free(&c);
  608. BN_free(&d);
  609. BN_free(&e);
  610. BN_CTX_free(ctx);
  611. return (1);
  612. }
  613. int test_sqr(BIO *bp, BN_CTX *ctx)
  614. {
  615. BIGNUM *a, *c, *d, *e;
  616. int i, ret = 0;
  617. a = BN_new();
  618. c = BN_new();
  619. d = BN_new();
  620. e = BN_new();
  621. if (a == NULL || c == NULL || d == NULL || e == NULL) {
  622. goto err;
  623. }
  624. for (i = 0; i < num0; i++) {
  625. BN_bntest_rand(a, 40 + i * 10, 0, 0);
  626. a->neg = rand_neg();
  627. BN_sqr(c, a, ctx);
  628. if (bp != NULL) {
  629. if (!results) {
  630. BN_print(bp, a);
  631. BIO_puts(bp, " * ");
  632. BN_print(bp, a);
  633. BIO_puts(bp, " - ");
  634. }
  635. BN_print(bp, c);
  636. BIO_puts(bp, "\n");
  637. }
  638. BN_div(d, e, c, a, ctx);
  639. BN_sub(d, d, a);
  640. if (!BN_is_zero(d) || !BN_is_zero(e)) {
  641. fprintf(stderr, "Square test failed!\n");
  642. goto err;
  643. }
  644. }
  645. /* Regression test for a BN_sqr overflow bug. */
  646. BN_hex2bn(&a,
  647. "80000000000000008000000000000001"
  648. "FFFFFFFFFFFFFFFE0000000000000000");
  649. BN_sqr(c, a, ctx);
  650. if (bp != NULL) {
  651. if (!results) {
  652. BN_print(bp, a);
  653. BIO_puts(bp, " * ");
  654. BN_print(bp, a);
  655. BIO_puts(bp, " - ");
  656. }
  657. BN_print(bp, c);
  658. BIO_puts(bp, "\n");
  659. }
  660. BN_mul(d, a, a, ctx);
  661. if (BN_cmp(c, d)) {
  662. fprintf(stderr, "Square test failed: BN_sqr and BN_mul produce "
  663. "different results!\n");
  664. goto err;
  665. }
  666. /* Regression test for a BN_sqr overflow bug. */
  667. BN_hex2bn(&a,
  668. "80000000000000000000000080000001"
  669. "FFFFFFFE000000000000000000000000");
  670. BN_sqr(c, a, ctx);
  671. if (bp != NULL) {
  672. if (!results) {
  673. BN_print(bp, a);
  674. BIO_puts(bp, " * ");
  675. BN_print(bp, a);
  676. BIO_puts(bp, " - ");
  677. }
  678. BN_print(bp, c);
  679. BIO_puts(bp, "\n");
  680. }
  681. BN_mul(d, a, a, ctx);
  682. if (BN_cmp(c, d)) {
  683. fprintf(stderr, "Square test failed: BN_sqr and BN_mul produce "
  684. "different results!\n");
  685. goto err;
  686. }
  687. ret = 1;
  688. err:
  689. if (a != NULL)
  690. BN_free(a);
  691. if (c != NULL)
  692. BN_free(c);
  693. if (d != NULL)
  694. BN_free(d);
  695. if (e != NULL)
  696. BN_free(e);
  697. return ret;
  698. }
  699. int test_mont(BIO *bp, BN_CTX *ctx)
  700. {
  701. BIGNUM a, b, c, d, A, B;
  702. BIGNUM n;
  703. int i;
  704. BN_MONT_CTX *mont;
  705. BN_init(&a);
  706. BN_init(&b);
  707. BN_init(&c);
  708. BN_init(&d);
  709. BN_init(&A);
  710. BN_init(&B);
  711. BN_init(&n);
  712. mont = BN_MONT_CTX_new();
  713. if (mont == NULL)
  714. return 0;
  715. BN_zero(&n);
  716. if (BN_MONT_CTX_set(mont, &n, ctx)) {
  717. fprintf(stderr, "BN_MONT_CTX_set succeeded for zero modulus!\n");
  718. return 0;
  719. }
  720. BN_set_word(&n, 16);
  721. if (BN_MONT_CTX_set(mont, &n, ctx)) {
  722. fprintf(stderr, "BN_MONT_CTX_set succeeded for even modulus!\n");
  723. return 0;
  724. }
  725. BN_bntest_rand(&a, 100, 0, 0);
  726. BN_bntest_rand(&b, 100, 0, 0);
  727. for (i = 0; i < num2; i++) {
  728. int bits = (200 * (i + 1)) / num2;
  729. if (bits == 0)
  730. continue;
  731. BN_bntest_rand(&n, bits, 0, 1);
  732. BN_MONT_CTX_set(mont, &n, ctx);
  733. BN_nnmod(&a, &a, &n, ctx);
  734. BN_nnmod(&b, &b, &n, ctx);
  735. BN_to_montgomery(&A, &a, mont, ctx);
  736. BN_to_montgomery(&B, &b, mont, ctx);
  737. BN_mod_mul_montgomery(&c, &A, &B, mont, ctx);
  738. BN_from_montgomery(&A, &c, mont, ctx);
  739. if (bp != NULL) {
  740. if (!results) {
  741. #ifdef undef
  742. fprintf(stderr, "%d * %d %% %d\n",
  743. BN_num_bits(&a),
  744. BN_num_bits(&b), BN_num_bits(mont->N));
  745. #endif
  746. BN_print(bp, &a);
  747. BIO_puts(bp, " * ");
  748. BN_print(bp, &b);
  749. BIO_puts(bp, " % ");
  750. BN_print(bp, &(mont->N));
  751. BIO_puts(bp, " - ");
  752. }
  753. BN_print(bp, &A);
  754. BIO_puts(bp, "\n");
  755. }
  756. BN_mod_mul(&d, &a, &b, &n, ctx);
  757. BN_sub(&d, &d, &A);
  758. if (!BN_is_zero(&d)) {
  759. fprintf(stderr, "Montgomery multiplication test failed!\n");
  760. return 0;
  761. }
  762. }
  763. BN_MONT_CTX_free(mont);
  764. BN_free(&a);
  765. BN_free(&b);
  766. BN_free(&c);
  767. BN_free(&d);
  768. BN_free(&A);
  769. BN_free(&B);
  770. BN_free(&n);
  771. return (1);
  772. }
  773. int test_mod(BIO *bp, BN_CTX *ctx)
  774. {
  775. BIGNUM *a, *b, *c, *d, *e;
  776. int i;
  777. a = BN_new();
  778. b = BN_new();
  779. c = BN_new();
  780. d = BN_new();
  781. e = BN_new();
  782. BN_bntest_rand(a, 1024, 0, 0);
  783. for (i = 0; i < num0; i++) {
  784. BN_bntest_rand(b, 450 + i * 10, 0, 0);
  785. a->neg = rand_neg();
  786. b->neg = rand_neg();
  787. BN_mod(c, a, b, ctx);
  788. if (bp != NULL) {
  789. if (!results) {
  790. BN_print(bp, a);
  791. BIO_puts(bp, " % ");
  792. BN_print(bp, b);
  793. BIO_puts(bp, " - ");
  794. }
  795. BN_print(bp, c);
  796. BIO_puts(bp, "\n");
  797. }
  798. BN_div(d, e, a, b, ctx);
  799. BN_sub(e, e, c);
  800. if (!BN_is_zero(e)) {
  801. fprintf(stderr, "Modulo test failed!\n");
  802. return 0;
  803. }
  804. }
  805. BN_free(a);
  806. BN_free(b);
  807. BN_free(c);
  808. BN_free(d);
  809. BN_free(e);
  810. return (1);
  811. }
  812. int test_mod_mul(BIO *bp, BN_CTX *ctx)
  813. {
  814. BIGNUM *a, *b, *c, *d, *e;
  815. int i, j;
  816. a = BN_new();
  817. b = BN_new();
  818. c = BN_new();
  819. d = BN_new();
  820. e = BN_new();
  821. BN_one(a);
  822. BN_one(b);
  823. BN_zero(c);
  824. if (BN_mod_mul(e, a, b, c, ctx)) {
  825. fprintf(stderr, "BN_mod_mul with zero modulus succeeded!\n");
  826. return 0;
  827. }
  828. for (j = 0; j < 3; j++) {
  829. BN_bntest_rand(c, 1024, 0, 0);
  830. for (i = 0; i < num0; i++) {
  831. BN_bntest_rand(a, 475 + i * 10, 0, 0);
  832. BN_bntest_rand(b, 425 + i * 11, 0, 0);
  833. a->neg = rand_neg();
  834. b->neg = rand_neg();
  835. if (!BN_mod_mul(e, a, b, c, ctx)) {
  836. unsigned long l;
  837. while ((l = ERR_get_error()))
  838. fprintf(stderr, "ERROR:%s\n", ERR_error_string(l, NULL));
  839. EXIT(1);
  840. }
  841. if (bp != NULL) {
  842. if (!results) {
  843. BN_print(bp, a);
  844. BIO_puts(bp, " * ");
  845. BN_print(bp, b);
  846. BIO_puts(bp, " % ");
  847. BN_print(bp, c);
  848. if ((a->neg ^ b->neg) && !BN_is_zero(e)) {
  849. /*
  850. * If (a*b) % c is negative, c must be added in order
  851. * to obtain the normalized remainder (new with
  852. * OpenSSL 0.9.7, previous versions of BN_mod_mul
  853. * could generate negative results)
  854. */
  855. BIO_puts(bp, " + ");
  856. BN_print(bp, c);
  857. }
  858. BIO_puts(bp, " - ");
  859. }
  860. BN_print(bp, e);
  861. BIO_puts(bp, "\n");
  862. }
  863. BN_mul(d, a, b, ctx);
  864. BN_sub(d, d, e);
  865. BN_div(a, b, d, c, ctx);
  866. if (!BN_is_zero(b)) {
  867. fprintf(stderr, "Modulo multiply test failed!\n");
  868. ERR_print_errors_fp(stderr);
  869. return 0;
  870. }
  871. }
  872. }
  873. BN_free(a);
  874. BN_free(b);
  875. BN_free(c);
  876. BN_free(d);
  877. BN_free(e);
  878. return (1);
  879. }
  880. int test_mod_exp(BIO *bp, BN_CTX *ctx)
  881. {
  882. BIGNUM *a, *b, *c, *d, *e;
  883. int i;
  884. a = BN_new();
  885. b = BN_new();
  886. c = BN_new();
  887. d = BN_new();
  888. e = BN_new();
  889. BN_one(a);
  890. BN_one(b);
  891. BN_zero(c);
  892. if (BN_mod_exp(d, a, b, c, ctx)) {
  893. fprintf(stderr, "BN_mod_exp with zero modulus succeeded!\n");
  894. return 0;
  895. }
  896. BN_bntest_rand(c, 30, 0, 1); /* must be odd for montgomery */
  897. for (i = 0; i < num2; i++) {
  898. BN_bntest_rand(a, 20 + i * 5, 0, 0);
  899. BN_bntest_rand(b, 2 + i, 0, 0);
  900. if (!BN_mod_exp(d, a, b, c, ctx))
  901. return (0);
  902. if (bp != NULL) {
  903. if (!results) {
  904. BN_print(bp, a);
  905. BIO_puts(bp, " ^ ");
  906. BN_print(bp, b);
  907. BIO_puts(bp, " % ");
  908. BN_print(bp, c);
  909. BIO_puts(bp, " - ");
  910. }
  911. BN_print(bp, d);
  912. BIO_puts(bp, "\n");
  913. }
  914. BN_exp(e, a, b, ctx);
  915. BN_sub(e, e, d);
  916. BN_div(a, b, e, c, ctx);
  917. if (!BN_is_zero(b)) {
  918. fprintf(stderr, "Modulo exponentiation test failed!\n");
  919. return 0;
  920. }
  921. }
  922. /* Regression test for carry propagation bug in sqr8x_reduction */
  923. BN_hex2bn(&a, "050505050505");
  924. BN_hex2bn(&b, "02");
  925. BN_hex2bn(&c,
  926. "4141414141414141414141274141414141414141414141414141414141414141"
  927. "4141414141414141414141414141414141414141414141414141414141414141"
  928. "4141414141414141414141800000000000000000000000000000000000000000"
  929. "0000000000000000000000000000000000000000000000000000000000000000"
  930. "0000000000000000000000000000000000000000000000000000000000000000"
  931. "0000000000000000000000000000000000000000000000000000000001");
  932. BN_mod_exp(d, a, b, c, ctx);
  933. BN_mul(e, a, a, ctx);
  934. if (BN_cmp(d, e)) {
  935. fprintf(stderr, "BN_mod_exp and BN_mul produce different results!\n");
  936. return 0;
  937. }
  938. BN_free(a);
  939. BN_free(b);
  940. BN_free(c);
  941. BN_free(d);
  942. BN_free(e);
  943. return (1);
  944. }
  945. int test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx)
  946. {
  947. BIGNUM *a, *b, *c, *d, *e;
  948. int i;
  949. a = BN_new();
  950. b = BN_new();
  951. c = BN_new();
  952. d = BN_new();
  953. e = BN_new();
  954. BN_one(a);
  955. BN_one(b);
  956. BN_zero(c);
  957. if (BN_mod_exp_mont_consttime(d, a, b, c, ctx, NULL)) {
  958. fprintf(stderr, "BN_mod_exp_mont_consttime with zero modulus "
  959. "succeeded\n");
  960. return 0;
  961. }
  962. BN_set_word(c, 16);
  963. if (BN_mod_exp_mont_consttime(d, a, b, c, ctx, NULL)) {
  964. fprintf(stderr, "BN_mod_exp_mont_consttime with even modulus "
  965. "succeeded\n");
  966. return 0;
  967. }
  968. BN_bntest_rand(c, 30, 0, 1); /* must be odd for montgomery */
  969. for (i = 0; i < num2; i++) {
  970. BN_bntest_rand(a, 20 + i * 5, 0, 0);
  971. BN_bntest_rand(b, 2 + i, 0, 0);
  972. if (!BN_mod_exp_mont_consttime(d, a, b, c, ctx, NULL))
  973. return (00);
  974. if (bp != NULL) {
  975. if (!results) {
  976. BN_print(bp, a);
  977. BIO_puts(bp, " ^ ");
  978. BN_print(bp, b);
  979. BIO_puts(bp, " % ");
  980. BN_print(bp, c);
  981. BIO_puts(bp, " - ");
  982. }
  983. BN_print(bp, d);
  984. BIO_puts(bp, "\n");
  985. }
  986. BN_exp(e, a, b, ctx);
  987. BN_sub(e, e, d);
  988. BN_div(a, b, e, c, ctx);
  989. if (!BN_is_zero(b)) {
  990. fprintf(stderr, "Modulo exponentiation test failed!\n");
  991. return 0;
  992. }
  993. }
  994. BN_free(a);
  995. BN_free(b);
  996. BN_free(c);
  997. BN_free(d);
  998. BN_free(e);
  999. return (1);
  1000. }
  1001. /*
  1002. * Test constant-time modular exponentiation with 1024-bit inputs, which on
  1003. * x86_64 cause a different code branch to be taken.
  1004. */
  1005. int test_mod_exp_mont5(BIO *bp, BN_CTX *ctx)
  1006. {
  1007. BIGNUM *a, *p, *m, *d, *e;
  1008. BN_MONT_CTX *mont;
  1009. a = BN_new();
  1010. p = BN_new();
  1011. m = BN_new();
  1012. d = BN_new();
  1013. e = BN_new();
  1014. mont = BN_MONT_CTX_new();
  1015. BN_bntest_rand(m, 1024, 0, 1); /* must be odd for montgomery */
  1016. /* Zero exponent */
  1017. BN_bntest_rand(a, 1024, 0, 0);
  1018. BN_zero(p);
  1019. if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
  1020. return 0;
  1021. if (!BN_is_one(d)) {
  1022. fprintf(stderr, "Modular exponentiation test failed!\n");
  1023. return 0;
  1024. }
  1025. /* Zero input */
  1026. BN_bntest_rand(p, 1024, 0, 0);
  1027. BN_zero(a);
  1028. if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
  1029. return 0;
  1030. if (!BN_is_zero(d)) {
  1031. fprintf(stderr, "Modular exponentiation test failed!\n");
  1032. return 0;
  1033. }
  1034. /*
  1035. * Craft an input whose Montgomery representation is 1, i.e., shorter
  1036. * than the modulus m, in order to test the const time precomputation
  1037. * scattering/gathering.
  1038. */
  1039. BN_one(a);
  1040. BN_MONT_CTX_set(mont, m, ctx);
  1041. if (!BN_from_montgomery(e, a, mont, ctx))
  1042. return 0;
  1043. if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
  1044. return 0;
  1045. if (!BN_mod_exp_simple(a, e, p, m, ctx))
  1046. return 0;
  1047. if (BN_cmp(a, d) != 0) {
  1048. fprintf(stderr, "Modular exponentiation test failed!\n");
  1049. return 0;
  1050. }
  1051. /* Finally, some regular test vectors. */
  1052. BN_bntest_rand(e, 1024, 0, 0);
  1053. if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
  1054. return 0;
  1055. if (!BN_mod_exp_simple(a, e, p, m, ctx))
  1056. return 0;
  1057. if (BN_cmp(a, d) != 0) {
  1058. fprintf(stderr, "Modular exponentiation test failed!\n");
  1059. return 0;
  1060. }
  1061. BN_MONT_CTX_free(mont);
  1062. BN_free(a);
  1063. BN_free(p);
  1064. BN_free(m);
  1065. BN_free(d);
  1066. BN_free(e);
  1067. return (1);
  1068. }
  1069. int test_exp(BIO *bp, BN_CTX *ctx)
  1070. {
  1071. BIGNUM *a, *b, *d, *e, *one;
  1072. int i;
  1073. a = BN_new();
  1074. b = BN_new();
  1075. d = BN_new();
  1076. e = BN_new();
  1077. one = BN_new();
  1078. BN_one(one);
  1079. for (i = 0; i < num2; i++) {
  1080. BN_bntest_rand(a, 20 + i * 5, 0, 0);
  1081. BN_bntest_rand(b, 2 + i, 0, 0);
  1082. if (BN_exp(d, a, b, ctx) <= 0)
  1083. return (0);
  1084. if (bp != NULL) {
  1085. if (!results) {
  1086. BN_print(bp, a);
  1087. BIO_puts(bp, " ^ ");
  1088. BN_print(bp, b);
  1089. BIO_puts(bp, " - ");
  1090. }
  1091. BN_print(bp, d);
  1092. BIO_puts(bp, "\n");
  1093. }
  1094. BN_one(e);
  1095. for (; !BN_is_zero(b); BN_sub(b, b, one))
  1096. BN_mul(e, e, a, ctx);
  1097. BN_sub(e, e, d);
  1098. if (!BN_is_zero(e)) {
  1099. fprintf(stderr, "Exponentiation test failed!\n");
  1100. return 0;
  1101. }
  1102. }
  1103. BN_free(a);
  1104. BN_free(b);
  1105. BN_free(d);
  1106. BN_free(e);
  1107. BN_free(one);
  1108. return (1);
  1109. }
  1110. #ifndef OPENSSL_NO_EC2M
  1111. int test_gf2m_add(BIO *bp)
  1112. {
  1113. BIGNUM a, b, c;
  1114. int i, ret = 0;
  1115. BN_init(&a);
  1116. BN_init(&b);
  1117. BN_init(&c);
  1118. for (i = 0; i < num0; i++) {
  1119. BN_rand(&a, 512, 0, 0);
  1120. BN_copy(&b, BN_value_one());
  1121. a.neg = rand_neg();
  1122. b.neg = rand_neg();
  1123. BN_GF2m_add(&c, &a, &b);
  1124. # if 0 /* make test uses ouput in bc but bc can't
  1125. * handle GF(2^m) arithmetic */
  1126. if (bp != NULL) {
  1127. if (!results) {
  1128. BN_print(bp, &a);
  1129. BIO_puts(bp, " ^ ");
  1130. BN_print(bp, &b);
  1131. BIO_puts(bp, " = ");
  1132. }
  1133. BN_print(bp, &c);
  1134. BIO_puts(bp, "\n");
  1135. }
  1136. # endif
  1137. /* Test that two added values have the correct parity. */
  1138. if ((BN_is_odd(&a) && BN_is_odd(&c))
  1139. || (!BN_is_odd(&a) && !BN_is_odd(&c))) {
  1140. fprintf(stderr, "GF(2^m) addition test (a) failed!\n");
  1141. goto err;
  1142. }
  1143. BN_GF2m_add(&c, &c, &c);
  1144. /* Test that c + c = 0. */
  1145. if (!BN_is_zero(&c)) {
  1146. fprintf(stderr, "GF(2^m) addition test (b) failed!\n");
  1147. goto err;
  1148. }
  1149. }
  1150. ret = 1;
  1151. err:
  1152. BN_free(&a);
  1153. BN_free(&b);
  1154. BN_free(&c);
  1155. return ret;
  1156. }
  1157. int test_gf2m_mod(BIO *bp)
  1158. {
  1159. BIGNUM *a, *b[2], *c, *d, *e;
  1160. int i, j, ret = 0;
  1161. int p0[] = { 163, 7, 6, 3, 0, -1 };
  1162. int p1[] = { 193, 15, 0, -1 };
  1163. a = BN_new();
  1164. b[0] = BN_new();
  1165. b[1] = BN_new();
  1166. c = BN_new();
  1167. d = BN_new();
  1168. e = BN_new();
  1169. BN_GF2m_arr2poly(p0, b[0]);
  1170. BN_GF2m_arr2poly(p1, b[1]);
  1171. for (i = 0; i < num0; i++) {
  1172. BN_bntest_rand(a, 1024, 0, 0);
  1173. for (j = 0; j < 2; j++) {
  1174. BN_GF2m_mod(c, a, b[j]);
  1175. # if 0 /* make test uses ouput in bc but bc can't
  1176. * handle GF(2^m) arithmetic */
  1177. if (bp != NULL) {
  1178. if (!results) {
  1179. BN_print(bp, a);
  1180. BIO_puts(bp, " % ");
  1181. BN_print(bp, b[j]);
  1182. BIO_puts(bp, " - ");
  1183. BN_print(bp, c);
  1184. BIO_puts(bp, "\n");
  1185. }
  1186. }
  1187. # endif
  1188. BN_GF2m_add(d, a, c);
  1189. BN_GF2m_mod(e, d, b[j]);
  1190. /* Test that a + (a mod p) mod p == 0. */
  1191. if (!BN_is_zero(e)) {
  1192. fprintf(stderr, "GF(2^m) modulo test failed!\n");
  1193. goto err;
  1194. }
  1195. }
  1196. }
  1197. ret = 1;
  1198. err:
  1199. BN_free(a);
  1200. BN_free(b[0]);
  1201. BN_free(b[1]);
  1202. BN_free(c);
  1203. BN_free(d);
  1204. BN_free(e);
  1205. return ret;
  1206. }
  1207. int test_gf2m_mod_mul(BIO *bp, BN_CTX *ctx)
  1208. {
  1209. BIGNUM *a, *b[2], *c, *d, *e, *f, *g, *h;
  1210. int i, j, ret = 0;
  1211. int p0[] = { 163, 7, 6, 3, 0, -1 };
  1212. int p1[] = { 193, 15, 0, -1 };
  1213. a = BN_new();
  1214. b[0] = BN_new();
  1215. b[1] = BN_new();
  1216. c = BN_new();
  1217. d = BN_new();
  1218. e = BN_new();
  1219. f = BN_new();
  1220. g = BN_new();
  1221. h = BN_new();
  1222. BN_GF2m_arr2poly(p0, b[0]);
  1223. BN_GF2m_arr2poly(p1, b[1]);
  1224. for (i = 0; i < num0; i++) {
  1225. BN_bntest_rand(a, 1024, 0, 0);
  1226. BN_bntest_rand(c, 1024, 0, 0);
  1227. BN_bntest_rand(d, 1024, 0, 0);
  1228. for (j = 0; j < 2; j++) {
  1229. BN_GF2m_mod_mul(e, a, c, b[j], ctx);
  1230. # if 0 /* make test uses ouput in bc but bc can't
  1231. * handle GF(2^m) arithmetic */
  1232. if (bp != NULL) {
  1233. if (!results) {
  1234. BN_print(bp, a);
  1235. BIO_puts(bp, " * ");
  1236. BN_print(bp, c);
  1237. BIO_puts(bp, " % ");
  1238. BN_print(bp, b[j]);
  1239. BIO_puts(bp, " - ");
  1240. BN_print(bp, e);
  1241. BIO_puts(bp, "\n");
  1242. }
  1243. }
  1244. # endif
  1245. BN_GF2m_add(f, a, d);
  1246. BN_GF2m_mod_mul(g, f, c, b[j], ctx);
  1247. BN_GF2m_mod_mul(h, d, c, b[j], ctx);
  1248. BN_GF2m_add(f, e, g);
  1249. BN_GF2m_add(f, f, h);
  1250. /* Test that (a+d)*c = a*c + d*c. */
  1251. if (!BN_is_zero(f)) {
  1252. fprintf(stderr,
  1253. "GF(2^m) modular multiplication test failed!\n");
  1254. goto err;
  1255. }
  1256. }
  1257. }
  1258. ret = 1;
  1259. err:
  1260. BN_free(a);
  1261. BN_free(b[0]);
  1262. BN_free(b[1]);
  1263. BN_free(c);
  1264. BN_free(d);
  1265. BN_free(e);
  1266. BN_free(f);
  1267. BN_free(g);
  1268. BN_free(h);
  1269. return ret;
  1270. }
  1271. int test_gf2m_mod_sqr(BIO *bp, BN_CTX *ctx)
  1272. {
  1273. BIGNUM *a, *b[2], *c, *d;
  1274. int i, j, ret = 0;
  1275. int p0[] = { 163, 7, 6, 3, 0, -1 };
  1276. int p1[] = { 193, 15, 0, -1 };
  1277. a = BN_new();
  1278. b[0] = BN_new();
  1279. b[1] = BN_new();
  1280. c = BN_new();
  1281. d = BN_new();
  1282. BN_GF2m_arr2poly(p0, b[0]);
  1283. BN_GF2m_arr2poly(p1, b[1]);
  1284. for (i = 0; i < num0; i++) {
  1285. BN_bntest_rand(a, 1024, 0, 0);
  1286. for (j = 0; j < 2; j++) {
  1287. BN_GF2m_mod_sqr(c, a, b[j], ctx);
  1288. BN_copy(d, a);
  1289. BN_GF2m_mod_mul(d, a, d, b[j], ctx);
  1290. # if 0 /* make test uses ouput in bc but bc can't
  1291. * handle GF(2^m) arithmetic */
  1292. if (bp != NULL) {
  1293. if (!results) {
  1294. BN_print(bp, a);
  1295. BIO_puts(bp, " ^ 2 % ");
  1296. BN_print(bp, b[j]);
  1297. BIO_puts(bp, " = ");
  1298. BN_print(bp, c);
  1299. BIO_puts(bp, "; a * a = ");
  1300. BN_print(bp, d);
  1301. BIO_puts(bp, "\n");
  1302. }
  1303. }
  1304. # endif
  1305. BN_GF2m_add(d, c, d);
  1306. /* Test that a*a = a^2. */
  1307. if (!BN_is_zero(d)) {
  1308. fprintf(stderr, "GF(2^m) modular squaring test failed!\n");
  1309. goto err;
  1310. }
  1311. }
  1312. }
  1313. ret = 1;
  1314. err:
  1315. BN_free(a);
  1316. BN_free(b[0]);
  1317. BN_free(b[1]);
  1318. BN_free(c);
  1319. BN_free(d);
  1320. return ret;
  1321. }
  1322. int test_gf2m_mod_inv(BIO *bp, BN_CTX *ctx)
  1323. {
  1324. BIGNUM *a, *b[2], *c, *d;
  1325. int i, j, ret = 0;
  1326. int p0[] = { 163, 7, 6, 3, 0, -1 };
  1327. int p1[] = { 193, 15, 0, -1 };
  1328. a = BN_new();
  1329. b[0] = BN_new();
  1330. b[1] = BN_new();
  1331. c = BN_new();
  1332. d = BN_new();
  1333. BN_GF2m_arr2poly(p0, b[0]);
  1334. BN_GF2m_arr2poly(p1, b[1]);
  1335. for (i = 0; i < num0; i++) {
  1336. BN_bntest_rand(a, 512, 0, 0);
  1337. for (j = 0; j < 2; j++) {
  1338. BN_GF2m_mod_inv(c, a, b[j], ctx);
  1339. BN_GF2m_mod_mul(d, a, c, b[j], ctx);
  1340. # if 0 /* make test uses ouput in bc but bc can't
  1341. * handle GF(2^m) arithmetic */
  1342. if (bp != NULL) {
  1343. if (!results) {
  1344. BN_print(bp, a);
  1345. BIO_puts(bp, " * ");
  1346. BN_print(bp, c);
  1347. BIO_puts(bp, " - 1 % ");
  1348. BN_print(bp, b[j]);
  1349. BIO_puts(bp, "\n");
  1350. }
  1351. }
  1352. # endif
  1353. /* Test that ((1/a)*a) = 1. */
  1354. if (!BN_is_one(d)) {
  1355. fprintf(stderr, "GF(2^m) modular inversion test failed!\n");
  1356. goto err;
  1357. }
  1358. }
  1359. }
  1360. ret = 1;
  1361. err:
  1362. BN_free(a);
  1363. BN_free(b[0]);
  1364. BN_free(b[1]);
  1365. BN_free(c);
  1366. BN_free(d);
  1367. return ret;
  1368. }
  1369. int test_gf2m_mod_div(BIO *bp, BN_CTX *ctx)
  1370. {
  1371. BIGNUM *a, *b[2], *c, *d, *e, *f;
  1372. int i, j, ret = 0;
  1373. int p0[] = { 163, 7, 6, 3, 0, -1 };
  1374. int p1[] = { 193, 15, 0, -1 };
  1375. a = BN_new();
  1376. b[0] = BN_new();
  1377. b[1] = BN_new();
  1378. c = BN_new();
  1379. d = BN_new();
  1380. e = BN_new();
  1381. f = BN_new();
  1382. BN_GF2m_arr2poly(p0, b[0]);
  1383. BN_GF2m_arr2poly(p1, b[1]);
  1384. for (i = 0; i < num0; i++) {
  1385. BN_bntest_rand(a, 512, 0, 0);
  1386. BN_bntest_rand(c, 512, 0, 0);
  1387. for (j = 0; j < 2; j++) {
  1388. BN_GF2m_mod_div(d, a, c, b[j], ctx);
  1389. BN_GF2m_mod_mul(e, d, c, b[j], ctx);
  1390. BN_GF2m_mod_div(f, a, e, b[j], ctx);
  1391. # if 0 /* make test uses ouput in bc but bc can't
  1392. * handle GF(2^m) arithmetic */
  1393. if (bp != NULL) {
  1394. if (!results) {
  1395. BN_print(bp, a);
  1396. BIO_puts(bp, " = ");
  1397. BN_print(bp, c);
  1398. BIO_puts(bp, " * ");
  1399. BN_print(bp, d);
  1400. BIO_puts(bp, " % ");
  1401. BN_print(bp, b[j]);
  1402. BIO_puts(bp, "\n");
  1403. }
  1404. }
  1405. # endif
  1406. /* Test that ((a/c)*c)/a = 1. */
  1407. if (!BN_is_one(f)) {
  1408. fprintf(stderr, "GF(2^m) modular division test failed!\n");
  1409. goto err;
  1410. }
  1411. }
  1412. }
  1413. ret = 1;
  1414. err:
  1415. BN_free(a);
  1416. BN_free(b[0]);
  1417. BN_free(b[1]);
  1418. BN_free(c);
  1419. BN_free(d);
  1420. BN_free(e);
  1421. BN_free(f);
  1422. return ret;
  1423. }
  1424. int test_gf2m_mod_exp(BIO *bp, BN_CTX *ctx)
  1425. {
  1426. BIGNUM *a, *b[2], *c, *d, *e, *f;
  1427. int i, j, ret = 0;
  1428. int p0[] = { 163, 7, 6, 3, 0, -1 };
  1429. int p1[] = { 193, 15, 0, -1 };
  1430. a = BN_new();
  1431. b[0] = BN_new();
  1432. b[1] = BN_new();
  1433. c = BN_new();
  1434. d = BN_new();
  1435. e = BN_new();
  1436. f = BN_new();
  1437. BN_GF2m_arr2poly(p0, b[0]);
  1438. BN_GF2m_arr2poly(p1, b[1]);
  1439. for (i = 0; i < num0; i++) {
  1440. BN_bntest_rand(a, 512, 0, 0);
  1441. BN_bntest_rand(c, 512, 0, 0);
  1442. BN_bntest_rand(d, 512, 0, 0);
  1443. for (j = 0; j < 2; j++) {
  1444. BN_GF2m_mod_exp(e, a, c, b[j], ctx);
  1445. BN_GF2m_mod_exp(f, a, d, b[j], ctx);
  1446. BN_GF2m_mod_mul(e, e, f, b[j], ctx);
  1447. BN_add(f, c, d);
  1448. BN_GF2m_mod_exp(f, a, f, b[j], ctx);
  1449. # if 0 /* make test uses ouput in bc but bc can't
  1450. * handle GF(2^m) arithmetic */
  1451. if (bp != NULL) {
  1452. if (!results) {
  1453. BN_print(bp, a);
  1454. BIO_puts(bp, " ^ (");
  1455. BN_print(bp, c);
  1456. BIO_puts(bp, " + ");
  1457. BN_print(bp, d);
  1458. BIO_puts(bp, ") = ");
  1459. BN_print(bp, e);
  1460. BIO_puts(bp, "; - ");
  1461. BN_print(bp, f);
  1462. BIO_puts(bp, " % ");
  1463. BN_print(bp, b[j]);
  1464. BIO_puts(bp, "\n");
  1465. }
  1466. }
  1467. # endif
  1468. BN_GF2m_add(f, e, f);
  1469. /* Test that a^(c+d)=a^c*a^d. */
  1470. if (!BN_is_zero(f)) {
  1471. fprintf(stderr,
  1472. "GF(2^m) modular exponentiation test failed!\n");
  1473. goto err;
  1474. }
  1475. }
  1476. }
  1477. ret = 1;
  1478. err:
  1479. BN_free(a);
  1480. BN_free(b[0]);
  1481. BN_free(b[1]);
  1482. BN_free(c);
  1483. BN_free(d);
  1484. BN_free(e);
  1485. BN_free(f);
  1486. return ret;
  1487. }
  1488. int test_gf2m_mod_sqrt(BIO *bp, BN_CTX *ctx)
  1489. {
  1490. BIGNUM *a, *b[2], *c, *d, *e, *f;
  1491. int i, j, ret = 0;
  1492. int p0[] = { 163, 7, 6, 3, 0, -1 };
  1493. int p1[] = { 193, 15, 0, -1 };
  1494. a = BN_new();
  1495. b[0] = BN_new();
  1496. b[1] = BN_new();
  1497. c = BN_new();
  1498. d = BN_new();
  1499. e = BN_new();
  1500. f = BN_new();
  1501. BN_GF2m_arr2poly(p0, b[0]);
  1502. BN_GF2m_arr2poly(p1, b[1]);
  1503. for (i = 0; i < num0; i++) {
  1504. BN_bntest_rand(a, 512, 0, 0);
  1505. for (j = 0; j < 2; j++) {
  1506. BN_GF2m_mod(c, a, b[j]);
  1507. BN_GF2m_mod_sqrt(d, a, b[j], ctx);
  1508. BN_GF2m_mod_sqr(e, d, b[j], ctx);
  1509. # if 0 /* make test uses ouput in bc but bc can't
  1510. * handle GF(2^m) arithmetic */
  1511. if (bp != NULL) {
  1512. if (!results) {
  1513. BN_print(bp, d);
  1514. BIO_puts(bp, " ^ 2 - ");
  1515. BN_print(bp, a);
  1516. BIO_puts(bp, "\n");
  1517. }
  1518. }
  1519. # endif
  1520. BN_GF2m_add(f, c, e);
  1521. /* Test that d^2 = a, where d = sqrt(a). */
  1522. if (!BN_is_zero(f)) {
  1523. fprintf(stderr, "GF(2^m) modular square root test failed!\n");
  1524. goto err;
  1525. }
  1526. }
  1527. }
  1528. ret = 1;
  1529. err:
  1530. BN_free(a);
  1531. BN_free(b[0]);
  1532. BN_free(b[1]);
  1533. BN_free(c);
  1534. BN_free(d);
  1535. BN_free(e);
  1536. BN_free(f);
  1537. return ret;
  1538. }
  1539. int test_gf2m_mod_solve_quad(BIO *bp, BN_CTX *ctx)
  1540. {
  1541. BIGNUM *a, *b[2], *c, *d, *e;
  1542. int i, j, s = 0, t, ret = 0;
  1543. int p0[] = { 163, 7, 6, 3, 0, -1 };
  1544. int p1[] = { 193, 15, 0, -1 };
  1545. a = BN_new();
  1546. b[0] = BN_new();
  1547. b[1] = BN_new();
  1548. c = BN_new();
  1549. d = BN_new();
  1550. e = BN_new();
  1551. BN_GF2m_arr2poly(p0, b[0]);
  1552. BN_GF2m_arr2poly(p1, b[1]);
  1553. for (i = 0; i < num0; i++) {
  1554. BN_bntest_rand(a, 512, 0, 0);
  1555. for (j = 0; j < 2; j++) {
  1556. t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx);
  1557. if (t) {
  1558. s++;
  1559. BN_GF2m_mod_sqr(d, c, b[j], ctx);
  1560. BN_GF2m_add(d, c, d);
  1561. BN_GF2m_mod(e, a, b[j]);
  1562. # if 0 /* make test uses ouput in bc but bc can't
  1563. * handle GF(2^m) arithmetic */
  1564. if (bp != NULL) {
  1565. if (!results) {
  1566. BN_print(bp, c);
  1567. BIO_puts(bp, " is root of z^2 + z = ");
  1568. BN_print(bp, a);
  1569. BIO_puts(bp, " % ");
  1570. BN_print(bp, b[j]);
  1571. BIO_puts(bp, "\n");
  1572. }
  1573. }
  1574. # endif
  1575. BN_GF2m_add(e, e, d);
  1576. /*
  1577. * Test that solution of quadratic c satisfies c^2 + c = a.
  1578. */
  1579. if (!BN_is_zero(e)) {
  1580. fprintf(stderr,
  1581. "GF(2^m) modular solve quadratic test failed!\n");
  1582. goto err;
  1583. }
  1584. } else {
  1585. # if 0 /* make test uses ouput in bc but bc can't
  1586. * handle GF(2^m) arithmetic */
  1587. if (bp != NULL) {
  1588. if (!results) {
  1589. BIO_puts(bp, "There are no roots of z^2 + z = ");
  1590. BN_print(bp, a);
  1591. BIO_puts(bp, " % ");
  1592. BN_print(bp, b[j]);
  1593. BIO_puts(bp, "\n");
  1594. }
  1595. }
  1596. # endif
  1597. }
  1598. }
  1599. }
  1600. if (s == 0) {
  1601. fprintf(stderr,
  1602. "All %i tests of GF(2^m) modular solve quadratic resulted in no roots;\n",
  1603. num0);
  1604. fprintf(stderr,
  1605. "this is very unlikely and probably indicates an error.\n");
  1606. goto err;
  1607. }
  1608. ret = 1;
  1609. err:
  1610. BN_free(a);
  1611. BN_free(b[0]);
  1612. BN_free(b[1]);
  1613. BN_free(c);
  1614. BN_free(d);
  1615. BN_free(e);
  1616. return ret;
  1617. }
  1618. #endif
  1619. static int genprime_cb(int p, int n, BN_GENCB *arg)
  1620. {
  1621. char c = '*';
  1622. if (p == 0)
  1623. c = '.';
  1624. if (p == 1)
  1625. c = '+';
  1626. if (p == 2)
  1627. c = '*';
  1628. if (p == 3)
  1629. c = '\n';
  1630. putc(c, stderr);
  1631. fflush(stderr);
  1632. return 1;
  1633. }
  1634. int test_kron(BIO *bp, BN_CTX *ctx)
  1635. {
  1636. BN_GENCB cb;
  1637. BIGNUM *a, *b, *r, *t;
  1638. int i;
  1639. int legendre, kronecker;
  1640. int ret = 0;
  1641. a = BN_new();
  1642. b = BN_new();
  1643. r = BN_new();
  1644. t = BN_new();
  1645. if (a == NULL || b == NULL || r == NULL || t == NULL)
  1646. goto err;
  1647. BN_GENCB_set(&cb, genprime_cb, NULL);
  1648. /*
  1649. * We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol). In
  1650. * this case we know that if b is prime, then BN_kronecker(a, b, ctx) is
  1651. * congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol). So we
  1652. * generate a random prime b and compare these values for a number of
  1653. * random a's. (That is, we run the Solovay-Strassen primality test to
  1654. * confirm that b is prime, except that we don't want to test whether b
  1655. * is prime but whether BN_kronecker works.)
  1656. */
  1657. if (!BN_generate_prime_ex(b, 512, 0, NULL, NULL, &cb))
  1658. goto err;
  1659. b->neg = rand_neg();
  1660. putc('\n', stderr);
  1661. for (i = 0; i < num0; i++) {
  1662. if (!BN_bntest_rand(a, 512, 0, 0))
  1663. goto err;
  1664. a->neg = rand_neg();
  1665. /* t := (|b|-1)/2 (note that b is odd) */
  1666. if (!BN_copy(t, b))
  1667. goto err;
  1668. t->neg = 0;
  1669. if (!BN_sub_word(t, 1))
  1670. goto err;
  1671. if (!BN_rshift1(t, t))
  1672. goto err;
  1673. /* r := a^t mod b */
  1674. b->neg = 0;
  1675. if (!BN_mod_exp_recp(r, a, t, b, ctx))
  1676. goto err;
  1677. b->neg = 1;
  1678. if (BN_is_word(r, 1))
  1679. legendre = 1;
  1680. else if (BN_is_zero(r))
  1681. legendre = 0;
  1682. else {
  1683. if (!BN_add_word(r, 1))
  1684. goto err;
  1685. if (0 != BN_ucmp(r, b)) {
  1686. fprintf(stderr, "Legendre symbol computation failed\n");
  1687. goto err;
  1688. }
  1689. legendre = -1;
  1690. }
  1691. kronecker = BN_kronecker(a, b, ctx);
  1692. if (kronecker < -1)
  1693. goto err;
  1694. /* we actually need BN_kronecker(a, |b|) */
  1695. if (a->neg && b->neg)
  1696. kronecker = -kronecker;
  1697. if (legendre != kronecker) {
  1698. fprintf(stderr, "legendre != kronecker; a = ");
  1699. BN_print_fp(stderr, a);
  1700. fprintf(stderr, ", b = ");
  1701. BN_print_fp(stderr, b);
  1702. fprintf(stderr, "\n");
  1703. goto err;
  1704. }
  1705. putc('.', stderr);
  1706. fflush(stderr);
  1707. }
  1708. putc('\n', stderr);
  1709. fflush(stderr);
  1710. ret = 1;
  1711. err:
  1712. if (a != NULL)
  1713. BN_free(a);
  1714. if (b != NULL)
  1715. BN_free(b);
  1716. if (r != NULL)
  1717. BN_free(r);
  1718. if (t != NULL)
  1719. BN_free(t);
  1720. return ret;
  1721. }
  1722. int test_sqrt(BIO *bp, BN_CTX *ctx)
  1723. {
  1724. BN_GENCB cb;
  1725. BIGNUM *a, *p, *r;
  1726. int i, j;
  1727. int ret = 0;
  1728. a = BN_new();
  1729. p = BN_new();
  1730. r = BN_new();
  1731. if (a == NULL || p == NULL || r == NULL)
  1732. goto err;
  1733. BN_GENCB_set(&cb, genprime_cb, NULL);
  1734. for (i = 0; i < 16; i++) {
  1735. if (i < 8) {
  1736. unsigned primes[8] = { 2, 3, 5, 7, 11, 13, 17, 19 };
  1737. if (!BN_set_word(p, primes[i]))
  1738. goto err;
  1739. } else {
  1740. if (!BN_set_word(a, 32))
  1741. goto err;
  1742. if (!BN_set_word(r, 2 * i + 1))
  1743. goto err;
  1744. if (!BN_generate_prime_ex(p, 256, 0, a, r, &cb))
  1745. goto err;
  1746. putc('\n', stderr);
  1747. }
  1748. p->neg = rand_neg();
  1749. for (j = 0; j < num2; j++) {
  1750. /*
  1751. * construct 'a' such that it is a square modulo p, but in
  1752. * general not a proper square and not reduced modulo p
  1753. */
  1754. if (!BN_bntest_rand(r, 256, 0, 3))
  1755. goto err;
  1756. if (!BN_nnmod(r, r, p, ctx))
  1757. goto err;
  1758. if (!BN_mod_sqr(r, r, p, ctx))
  1759. goto err;
  1760. if (!BN_bntest_rand(a, 256, 0, 3))
  1761. goto err;
  1762. if (!BN_nnmod(a, a, p, ctx))
  1763. goto err;
  1764. if (!BN_mod_sqr(a, a, p, ctx))
  1765. goto err;
  1766. if (!BN_mul(a, a, r, ctx))
  1767. goto err;
  1768. if (rand_neg())
  1769. if (!BN_sub(a, a, p))
  1770. goto err;
  1771. if (!BN_mod_sqrt(r, a, p, ctx))
  1772. goto err;
  1773. if (!BN_mod_sqr(r, r, p, ctx))
  1774. goto err;
  1775. if (!BN_nnmod(a, a, p, ctx))
  1776. goto err;
  1777. if (BN_cmp(a, r) != 0) {
  1778. fprintf(stderr, "BN_mod_sqrt failed: a = ");
  1779. BN_print_fp(stderr, a);
  1780. fprintf(stderr, ", r = ");
  1781. BN_print_fp(stderr, r);
  1782. fprintf(stderr, ", p = ");
  1783. BN_print_fp(stderr, p);
  1784. fprintf(stderr, "\n");
  1785. goto err;
  1786. }
  1787. putc('.', stderr);
  1788. fflush(stderr);
  1789. }
  1790. putc('\n', stderr);
  1791. fflush(stderr);
  1792. }
  1793. ret = 1;
  1794. err:
  1795. if (a != NULL)
  1796. BN_free(a);
  1797. if (p != NULL)
  1798. BN_free(p);
  1799. if (r != NULL)
  1800. BN_free(r);
  1801. return ret;
  1802. }
  1803. int test_lshift(BIO *bp, BN_CTX *ctx, BIGNUM *a_)
  1804. {
  1805. BIGNUM *a, *b, *c, *d;
  1806. int i;
  1807. b = BN_new();
  1808. c = BN_new();
  1809. d = BN_new();
  1810. BN_one(c);
  1811. if (a_)
  1812. a = a_;
  1813. else {
  1814. a = BN_new();
  1815. BN_bntest_rand(a, 200, 0, 0);
  1816. a->neg = rand_neg();
  1817. }
  1818. for (i = 0; i < num0; i++) {
  1819. BN_lshift(b, a, i + 1);
  1820. BN_add(c, c, c);
  1821. if (bp != NULL) {
  1822. if (!results) {
  1823. BN_print(bp, a);
  1824. BIO_puts(bp, " * ");
  1825. BN_print(bp, c);
  1826. BIO_puts(bp, " - ");
  1827. }
  1828. BN_print(bp, b);
  1829. BIO_puts(bp, "\n");
  1830. }
  1831. BN_mul(d, a, c, ctx);
  1832. BN_sub(d, d, b);
  1833. if (!BN_is_zero(d)) {
  1834. fprintf(stderr, "Left shift test failed!\n");
  1835. fprintf(stderr, "a=");
  1836. BN_print_fp(stderr, a);
  1837. fprintf(stderr, "\nb=");
  1838. BN_print_fp(stderr, b);
  1839. fprintf(stderr, "\nc=");
  1840. BN_print_fp(stderr, c);
  1841. fprintf(stderr, "\nd=");
  1842. BN_print_fp(stderr, d);
  1843. fprintf(stderr, "\n");
  1844. return 0;
  1845. }
  1846. }
  1847. BN_free(a);
  1848. BN_free(b);
  1849. BN_free(c);
  1850. BN_free(d);
  1851. return (1);
  1852. }
  1853. int test_lshift1(BIO *bp)
  1854. {
  1855. BIGNUM *a, *b, *c;
  1856. int i;
  1857. a = BN_new();
  1858. b = BN_new();
  1859. c = BN_new();
  1860. BN_bntest_rand(a, 200, 0, 0);
  1861. a->neg = rand_neg();
  1862. for (i = 0; i < num0; i++) {
  1863. BN_lshift1(b, a);
  1864. if (bp != NULL) {
  1865. if (!results) {
  1866. BN_print(bp, a);
  1867. BIO_puts(bp, " * 2");
  1868. BIO_puts(bp, " - ");
  1869. }
  1870. BN_print(bp, b);
  1871. BIO_puts(bp, "\n");
  1872. }
  1873. BN_add(c, a, a);
  1874. BN_sub(a, b, c);
  1875. if (!BN_is_zero(a)) {
  1876. fprintf(stderr, "Left shift one test failed!\n");
  1877. return 0;
  1878. }
  1879. BN_copy(a, b);
  1880. }
  1881. BN_free(a);
  1882. BN_free(b);
  1883. BN_free(c);
  1884. return (1);
  1885. }
  1886. int test_rshift(BIO *bp, BN_CTX *ctx)
  1887. {
  1888. BIGNUM *a, *b, *c, *d, *e;
  1889. int i;
  1890. a = BN_new();
  1891. b = BN_new();
  1892. c = BN_new();
  1893. d = BN_new();
  1894. e = BN_new();
  1895. BN_one(c);
  1896. BN_bntest_rand(a, 200, 0, 0);
  1897. a->neg = rand_neg();
  1898. for (i = 0; i < num0; i++) {
  1899. BN_rshift(b, a, i + 1);
  1900. BN_add(c, c, c);
  1901. if (bp != NULL) {
  1902. if (!results) {
  1903. BN_print(bp, a);
  1904. BIO_puts(bp, " / ");
  1905. BN_print(bp, c);
  1906. BIO_puts(bp, " - ");
  1907. }
  1908. BN_print(bp, b);
  1909. BIO_puts(bp, "\n");
  1910. }
  1911. BN_div(d, e, a, c, ctx);
  1912. BN_sub(d, d, b);
  1913. if (!BN_is_zero(d)) {
  1914. fprintf(stderr, "Right shift test failed!\n");
  1915. return 0;
  1916. }
  1917. }
  1918. BN_free(a);
  1919. BN_free(b);
  1920. BN_free(c);
  1921. BN_free(d);
  1922. BN_free(e);
  1923. return (1);
  1924. }
  1925. int test_rshift1(BIO *bp)
  1926. {
  1927. BIGNUM *a, *b, *c;
  1928. int i;
  1929. a = BN_new();
  1930. b = BN_new();
  1931. c = BN_new();
  1932. BN_bntest_rand(a, 200, 0, 0);
  1933. a->neg = rand_neg();
  1934. for (i = 0; i < num0; i++) {
  1935. BN_rshift1(b, a);
  1936. if (bp != NULL) {
  1937. if (!results) {
  1938. BN_print(bp, a);
  1939. BIO_puts(bp, " / 2");
  1940. BIO_puts(bp, " - ");
  1941. }
  1942. BN_print(bp, b);
  1943. BIO_puts(bp, "\n");
  1944. }
  1945. BN_sub(c, a, b);
  1946. BN_sub(c, c, b);
  1947. if (!BN_is_zero(c) && !BN_abs_is_word(c, 1)) {
  1948. fprintf(stderr, "Right shift one test failed!\n");
  1949. return 0;
  1950. }
  1951. BN_copy(a, b);
  1952. }
  1953. BN_free(a);
  1954. BN_free(b);
  1955. BN_free(c);
  1956. return (1);
  1957. }
  1958. int rand_neg(void)
  1959. {
  1960. static unsigned int neg = 0;
  1961. static int sign[8] = { 0, 0, 0, 1, 1, 0, 1, 1 };
  1962. return (sign[(neg++) % 8]);
  1963. }