bntest.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085
  1. /* crypto/bn/bntest.c */
  2. /* Copyright (C) 1995-1998 Eric Young ([email protected])
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young ([email protected]).
  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 ([email protected]).
  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 ([email protected])"
  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 ([email protected])"
  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. for (i = 0; i < num0 + num1; i++) {
  391. if (i < num1) {
  392. BN_bntest_rand(&a, 400, 0, 0);
  393. BN_copy(&b, &a);
  394. BN_lshift(&a, &a, i);
  395. BN_add_word(&a, i);
  396. } else
  397. BN_bntest_rand(&b, 50 + 3 * (i - num1), 0, 0);
  398. a.neg = rand_neg();
  399. b.neg = rand_neg();
  400. BN_div(&d, &c, &a, &b, ctx);
  401. if (bp != NULL) {
  402. if (!results) {
  403. BN_print(bp, &a);
  404. BIO_puts(bp, " / ");
  405. BN_print(bp, &b);
  406. BIO_puts(bp, " - ");
  407. }
  408. BN_print(bp, &d);
  409. BIO_puts(bp, "\n");
  410. if (!results) {
  411. BN_print(bp, &a);
  412. BIO_puts(bp, " % ");
  413. BN_print(bp, &b);
  414. BIO_puts(bp, " - ");
  415. }
  416. BN_print(bp, &c);
  417. BIO_puts(bp, "\n");
  418. }
  419. BN_mul(&e, &d, &b, ctx);
  420. BN_add(&d, &e, &c);
  421. BN_sub(&d, &d, &a);
  422. if (!BN_is_zero(&d)) {
  423. fprintf(stderr, "Division test failed!\n");
  424. return 0;
  425. }
  426. }
  427. BN_free(&a);
  428. BN_free(&b);
  429. BN_free(&c);
  430. BN_free(&d);
  431. BN_free(&e);
  432. return (1);
  433. }
  434. static void print_word(BIO *bp, BN_ULONG w)
  435. {
  436. #ifdef SIXTY_FOUR_BIT
  437. if (sizeof(w) > sizeof(unsigned long)) {
  438. unsigned long h = (unsigned long)(w >> 32), l = (unsigned long)(w);
  439. if (h)
  440. BIO_printf(bp, "%lX%08lX", h, l);
  441. else
  442. BIO_printf(bp, "%lX", l);
  443. return;
  444. }
  445. #endif
  446. BIO_printf(bp, BN_HEX_FMT1, w);
  447. }
  448. int test_div_word(BIO *bp)
  449. {
  450. BIGNUM a, b;
  451. BN_ULONG r, s;
  452. int i;
  453. BN_init(&a);
  454. BN_init(&b);
  455. for (i = 0; i < num0; i++) {
  456. do {
  457. BN_bntest_rand(&a, 512, -1, 0);
  458. BN_bntest_rand(&b, BN_BITS2, -1, 0);
  459. s = b.d[0];
  460. } while (!s);
  461. BN_copy(&b, &a);
  462. r = BN_div_word(&b, s);
  463. if (bp != NULL) {
  464. if (!results) {
  465. BN_print(bp, &a);
  466. BIO_puts(bp, " / ");
  467. print_word(bp, s);
  468. BIO_puts(bp, " - ");
  469. }
  470. BN_print(bp, &b);
  471. BIO_puts(bp, "\n");
  472. if (!results) {
  473. BN_print(bp, &a);
  474. BIO_puts(bp, " % ");
  475. print_word(bp, s);
  476. BIO_puts(bp, " - ");
  477. }
  478. print_word(bp, r);
  479. BIO_puts(bp, "\n");
  480. }
  481. BN_mul_word(&b, s);
  482. BN_add_word(&b, r);
  483. BN_sub(&b, &a, &b);
  484. if (!BN_is_zero(&b)) {
  485. fprintf(stderr, "Division (word) test failed!\n");
  486. return 0;
  487. }
  488. }
  489. BN_free(&a);
  490. BN_free(&b);
  491. return (1);
  492. }
  493. int test_div_recp(BIO *bp, BN_CTX *ctx)
  494. {
  495. BIGNUM a, b, c, d, e;
  496. BN_RECP_CTX recp;
  497. int i;
  498. BN_RECP_CTX_init(&recp);
  499. BN_init(&a);
  500. BN_init(&b);
  501. BN_init(&c);
  502. BN_init(&d);
  503. BN_init(&e);
  504. for (i = 0; i < num0 + num1; i++) {
  505. if (i < num1) {
  506. BN_bntest_rand(&a, 400, 0, 0);
  507. BN_copy(&b, &a);
  508. BN_lshift(&a, &a, i);
  509. BN_add_word(&a, i);
  510. } else
  511. BN_bntest_rand(&b, 50 + 3 * (i - num1), 0, 0);
  512. a.neg = rand_neg();
  513. b.neg = rand_neg();
  514. BN_RECP_CTX_set(&recp, &b, ctx);
  515. BN_div_recp(&d, &c, &a, &recp, ctx);
  516. if (bp != NULL) {
  517. if (!results) {
  518. BN_print(bp, &a);
  519. BIO_puts(bp, " / ");
  520. BN_print(bp, &b);
  521. BIO_puts(bp, " - ");
  522. }
  523. BN_print(bp, &d);
  524. BIO_puts(bp, "\n");
  525. if (!results) {
  526. BN_print(bp, &a);
  527. BIO_puts(bp, " % ");
  528. BN_print(bp, &b);
  529. BIO_puts(bp, " - ");
  530. }
  531. BN_print(bp, &c);
  532. BIO_puts(bp, "\n");
  533. }
  534. BN_mul(&e, &d, &b, ctx);
  535. BN_add(&d, &e, &c);
  536. BN_sub(&d, &d, &a);
  537. if (!BN_is_zero(&d)) {
  538. fprintf(stderr, "Reciprocal division test failed!\n");
  539. fprintf(stderr, "a=");
  540. BN_print_fp(stderr, &a);
  541. fprintf(stderr, "\nb=");
  542. BN_print_fp(stderr, &b);
  543. fprintf(stderr, "\n");
  544. return 0;
  545. }
  546. }
  547. BN_free(&a);
  548. BN_free(&b);
  549. BN_free(&c);
  550. BN_free(&d);
  551. BN_free(&e);
  552. BN_RECP_CTX_free(&recp);
  553. return (1);
  554. }
  555. int test_mul(BIO *bp)
  556. {
  557. BIGNUM a, b, c, d, e;
  558. int i;
  559. BN_CTX *ctx;
  560. ctx = BN_CTX_new();
  561. if (ctx == NULL)
  562. EXIT(1);
  563. BN_init(&a);
  564. BN_init(&b);
  565. BN_init(&c);
  566. BN_init(&d);
  567. BN_init(&e);
  568. for (i = 0; i < num0 + num1; i++) {
  569. if (i <= num1) {
  570. BN_bntest_rand(&a, 100, 0, 0);
  571. BN_bntest_rand(&b, 100, 0, 0);
  572. } else
  573. BN_bntest_rand(&b, i - num1, 0, 0);
  574. a.neg = rand_neg();
  575. b.neg = rand_neg();
  576. BN_mul(&c, &a, &b, ctx);
  577. if (bp != NULL) {
  578. if (!results) {
  579. BN_print(bp, &a);
  580. BIO_puts(bp, " * ");
  581. BN_print(bp, &b);
  582. BIO_puts(bp, " - ");
  583. }
  584. BN_print(bp, &c);
  585. BIO_puts(bp, "\n");
  586. }
  587. BN_div(&d, &e, &c, &a, ctx);
  588. BN_sub(&d, &d, &b);
  589. if (!BN_is_zero(&d) || !BN_is_zero(&e)) {
  590. fprintf(stderr, "Multiplication test failed!\n");
  591. return 0;
  592. }
  593. }
  594. BN_free(&a);
  595. BN_free(&b);
  596. BN_free(&c);
  597. BN_free(&d);
  598. BN_free(&e);
  599. BN_CTX_free(ctx);
  600. return (1);
  601. }
  602. int test_sqr(BIO *bp, BN_CTX *ctx)
  603. {
  604. BIGNUM *a, *c, *d, *e;
  605. int i, ret = 0;
  606. a = BN_new();
  607. c = BN_new();
  608. d = BN_new();
  609. e = BN_new();
  610. if (a == NULL || c == NULL || d == NULL || e == NULL) {
  611. goto err;
  612. }
  613. for (i = 0; i < num0; i++) {
  614. BN_bntest_rand(a, 40 + i * 10, 0, 0);
  615. a->neg = rand_neg();
  616. BN_sqr(c, a, ctx);
  617. if (bp != NULL) {
  618. if (!results) {
  619. BN_print(bp, a);
  620. BIO_puts(bp, " * ");
  621. BN_print(bp, a);
  622. BIO_puts(bp, " - ");
  623. }
  624. BN_print(bp, c);
  625. BIO_puts(bp, "\n");
  626. }
  627. BN_div(d, e, c, a, ctx);
  628. BN_sub(d, d, a);
  629. if (!BN_is_zero(d) || !BN_is_zero(e)) {
  630. fprintf(stderr, "Square test failed!\n");
  631. goto err;
  632. }
  633. }
  634. /* Regression test for a BN_sqr overflow bug. */
  635. BN_hex2bn(&a,
  636. "80000000000000008000000000000001"
  637. "FFFFFFFFFFFFFFFE0000000000000000");
  638. BN_sqr(c, a, ctx);
  639. if (bp != NULL) {
  640. if (!results) {
  641. BN_print(bp, a);
  642. BIO_puts(bp, " * ");
  643. BN_print(bp, a);
  644. BIO_puts(bp, " - ");
  645. }
  646. BN_print(bp, c);
  647. BIO_puts(bp, "\n");
  648. }
  649. BN_mul(d, a, a, ctx);
  650. if (BN_cmp(c, d)) {
  651. fprintf(stderr, "Square test failed: BN_sqr and BN_mul produce "
  652. "different results!\n");
  653. goto err;
  654. }
  655. /* Regression test for a BN_sqr overflow bug. */
  656. BN_hex2bn(&a,
  657. "80000000000000000000000080000001"
  658. "FFFFFFFE000000000000000000000000");
  659. BN_sqr(c, a, ctx);
  660. if (bp != NULL) {
  661. if (!results) {
  662. BN_print(bp, a);
  663. BIO_puts(bp, " * ");
  664. BN_print(bp, a);
  665. BIO_puts(bp, " - ");
  666. }
  667. BN_print(bp, c);
  668. BIO_puts(bp, "\n");
  669. }
  670. BN_mul(d, a, a, ctx);
  671. if (BN_cmp(c, d)) {
  672. fprintf(stderr, "Square test failed: BN_sqr and BN_mul produce "
  673. "different results!\n");
  674. goto err;
  675. }
  676. ret = 1;
  677. err:
  678. if (a != NULL)
  679. BN_free(a);
  680. if (c != NULL)
  681. BN_free(c);
  682. if (d != NULL)
  683. BN_free(d);
  684. if (e != NULL)
  685. BN_free(e);
  686. return ret;
  687. }
  688. int test_mont(BIO *bp, BN_CTX *ctx)
  689. {
  690. BIGNUM a, b, c, d, A, B;
  691. BIGNUM n;
  692. int i;
  693. BN_MONT_CTX *mont;
  694. BN_init(&a);
  695. BN_init(&b);
  696. BN_init(&c);
  697. BN_init(&d);
  698. BN_init(&A);
  699. BN_init(&B);
  700. BN_init(&n);
  701. mont = BN_MONT_CTX_new();
  702. if (mont == NULL)
  703. return 0;
  704. BN_bntest_rand(&a, 100, 0, 0);
  705. BN_bntest_rand(&b, 100, 0, 0);
  706. for (i = 0; i < num2; i++) {
  707. int bits = (200 * (i + 1)) / num2;
  708. if (bits == 0)
  709. continue;
  710. BN_bntest_rand(&n, bits, 0, 1);
  711. BN_MONT_CTX_set(mont, &n, ctx);
  712. BN_nnmod(&a, &a, &n, ctx);
  713. BN_nnmod(&b, &b, &n, ctx);
  714. BN_to_montgomery(&A, &a, mont, ctx);
  715. BN_to_montgomery(&B, &b, mont, ctx);
  716. BN_mod_mul_montgomery(&c, &A, &B, mont, ctx);
  717. BN_from_montgomery(&A, &c, mont, ctx);
  718. if (bp != NULL) {
  719. if (!results) {
  720. #ifdef undef
  721. fprintf(stderr, "%d * %d %% %d\n",
  722. BN_num_bits(&a),
  723. BN_num_bits(&b), BN_num_bits(mont->N));
  724. #endif
  725. BN_print(bp, &a);
  726. BIO_puts(bp, " * ");
  727. BN_print(bp, &b);
  728. BIO_puts(bp, " % ");
  729. BN_print(bp, &(mont->N));
  730. BIO_puts(bp, " - ");
  731. }
  732. BN_print(bp, &A);
  733. BIO_puts(bp, "\n");
  734. }
  735. BN_mod_mul(&d, &a, &b, &n, ctx);
  736. BN_sub(&d, &d, &A);
  737. if (!BN_is_zero(&d)) {
  738. fprintf(stderr, "Montgomery multiplication test failed!\n");
  739. return 0;
  740. }
  741. }
  742. BN_MONT_CTX_free(mont);
  743. BN_free(&a);
  744. BN_free(&b);
  745. BN_free(&c);
  746. BN_free(&d);
  747. BN_free(&A);
  748. BN_free(&B);
  749. BN_free(&n);
  750. return (1);
  751. }
  752. int test_mod(BIO *bp, BN_CTX *ctx)
  753. {
  754. BIGNUM *a, *b, *c, *d, *e;
  755. int i;
  756. a = BN_new();
  757. b = BN_new();
  758. c = BN_new();
  759. d = BN_new();
  760. e = BN_new();
  761. BN_bntest_rand(a, 1024, 0, 0);
  762. for (i = 0; i < num0; i++) {
  763. BN_bntest_rand(b, 450 + i * 10, 0, 0);
  764. a->neg = rand_neg();
  765. b->neg = rand_neg();
  766. BN_mod(c, a, b, ctx);
  767. if (bp != NULL) {
  768. if (!results) {
  769. BN_print(bp, a);
  770. BIO_puts(bp, " % ");
  771. BN_print(bp, b);
  772. BIO_puts(bp, " - ");
  773. }
  774. BN_print(bp, c);
  775. BIO_puts(bp, "\n");
  776. }
  777. BN_div(d, e, a, b, ctx);
  778. BN_sub(e, e, c);
  779. if (!BN_is_zero(e)) {
  780. fprintf(stderr, "Modulo test failed!\n");
  781. return 0;
  782. }
  783. }
  784. BN_free(a);
  785. BN_free(b);
  786. BN_free(c);
  787. BN_free(d);
  788. BN_free(e);
  789. return (1);
  790. }
  791. int test_mod_mul(BIO *bp, BN_CTX *ctx)
  792. {
  793. BIGNUM *a, *b, *c, *d, *e;
  794. int i, j;
  795. a = BN_new();
  796. b = BN_new();
  797. c = BN_new();
  798. d = BN_new();
  799. e = BN_new();
  800. for (j = 0; j < 3; j++) {
  801. BN_bntest_rand(c, 1024, 0, 0);
  802. for (i = 0; i < num0; i++) {
  803. BN_bntest_rand(a, 475 + i * 10, 0, 0);
  804. BN_bntest_rand(b, 425 + i * 11, 0, 0);
  805. a->neg = rand_neg();
  806. b->neg = rand_neg();
  807. if (!BN_mod_mul(e, a, b, c, ctx)) {
  808. unsigned long l;
  809. while ((l = ERR_get_error()))
  810. fprintf(stderr, "ERROR:%s\n", ERR_error_string(l, NULL));
  811. EXIT(1);
  812. }
  813. if (bp != NULL) {
  814. if (!results) {
  815. BN_print(bp, a);
  816. BIO_puts(bp, " * ");
  817. BN_print(bp, b);
  818. BIO_puts(bp, " % ");
  819. BN_print(bp, c);
  820. if ((a->neg ^ b->neg) && !BN_is_zero(e)) {
  821. /*
  822. * If (a*b) % c is negative, c must be added in order
  823. * to obtain the normalized remainder (new with
  824. * OpenSSL 0.9.7, previous versions of BN_mod_mul
  825. * could generate negative results)
  826. */
  827. BIO_puts(bp, " + ");
  828. BN_print(bp, c);
  829. }
  830. BIO_puts(bp, " - ");
  831. }
  832. BN_print(bp, e);
  833. BIO_puts(bp, "\n");
  834. }
  835. BN_mul(d, a, b, ctx);
  836. BN_sub(d, d, e);
  837. BN_div(a, b, d, c, ctx);
  838. if (!BN_is_zero(b)) {
  839. fprintf(stderr, "Modulo multiply test failed!\n");
  840. ERR_print_errors_fp(stderr);
  841. return 0;
  842. }
  843. }
  844. }
  845. BN_free(a);
  846. BN_free(b);
  847. BN_free(c);
  848. BN_free(d);
  849. BN_free(e);
  850. return (1);
  851. }
  852. int test_mod_exp(BIO *bp, BN_CTX *ctx)
  853. {
  854. BIGNUM *a, *b, *c, *d, *e;
  855. int i;
  856. a = BN_new();
  857. b = BN_new();
  858. c = BN_new();
  859. d = BN_new();
  860. e = BN_new();
  861. BN_bntest_rand(c, 30, 0, 1); /* must be odd for montgomery */
  862. for (i = 0; i < num2; i++) {
  863. BN_bntest_rand(a, 20 + i * 5, 0, 0);
  864. BN_bntest_rand(b, 2 + i, 0, 0);
  865. if (!BN_mod_exp(d, a, b, c, ctx))
  866. return (0);
  867. if (bp != NULL) {
  868. if (!results) {
  869. BN_print(bp, a);
  870. BIO_puts(bp, " ^ ");
  871. BN_print(bp, b);
  872. BIO_puts(bp, " % ");
  873. BN_print(bp, c);
  874. BIO_puts(bp, " - ");
  875. }
  876. BN_print(bp, d);
  877. BIO_puts(bp, "\n");
  878. }
  879. BN_exp(e, a, b, ctx);
  880. BN_sub(e, e, d);
  881. BN_div(a, b, e, c, ctx);
  882. if (!BN_is_zero(b)) {
  883. fprintf(stderr, "Modulo exponentiation test failed!\n");
  884. return 0;
  885. }
  886. }
  887. BN_free(a);
  888. BN_free(b);
  889. BN_free(c);
  890. BN_free(d);
  891. BN_free(e);
  892. return (1);
  893. }
  894. int test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx)
  895. {
  896. BIGNUM *a, *b, *c, *d, *e;
  897. int i;
  898. a = BN_new();
  899. b = BN_new();
  900. c = BN_new();
  901. d = BN_new();
  902. e = BN_new();
  903. BN_bntest_rand(c, 30, 0, 1); /* must be odd for montgomery */
  904. for (i = 0; i < num2; i++) {
  905. BN_bntest_rand(a, 20 + i * 5, 0, 0);
  906. BN_bntest_rand(b, 2 + i, 0, 0);
  907. if (!BN_mod_exp_mont_consttime(d, a, b, c, ctx, NULL))
  908. return (00);
  909. if (bp != NULL) {
  910. if (!results) {
  911. BN_print(bp, a);
  912. BIO_puts(bp, " ^ ");
  913. BN_print(bp, b);
  914. BIO_puts(bp, " % ");
  915. BN_print(bp, c);
  916. BIO_puts(bp, " - ");
  917. }
  918. BN_print(bp, d);
  919. BIO_puts(bp, "\n");
  920. }
  921. BN_exp(e, a, b, ctx);
  922. BN_sub(e, e, d);
  923. BN_div(a, b, e, c, ctx);
  924. if (!BN_is_zero(b)) {
  925. fprintf(stderr, "Modulo exponentiation test failed!\n");
  926. return 0;
  927. }
  928. }
  929. BN_free(a);
  930. BN_free(b);
  931. BN_free(c);
  932. BN_free(d);
  933. BN_free(e);
  934. return (1);
  935. }
  936. /*
  937. * Test constant-time modular exponentiation with 1024-bit inputs, which on
  938. * x86_64 cause a different code branch to be taken.
  939. */
  940. int test_mod_exp_mont5(BIO *bp, BN_CTX *ctx)
  941. {
  942. BIGNUM *a, *p, *m, *d, *e;
  943. BN_MONT_CTX *mont;
  944. a = BN_new();
  945. p = BN_new();
  946. m = BN_new();
  947. d = BN_new();
  948. e = BN_new();
  949. mont = BN_MONT_CTX_new();
  950. BN_bntest_rand(m, 1024, 0, 1); /* must be odd for montgomery */
  951. /* Zero exponent */
  952. BN_bntest_rand(a, 1024, 0, 0);
  953. BN_zero(p);
  954. if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
  955. return 0;
  956. if (!BN_is_one(d)) {
  957. fprintf(stderr, "Modular exponentiation test failed!\n");
  958. return 0;
  959. }
  960. /* Zero input */
  961. BN_bntest_rand(p, 1024, 0, 0);
  962. BN_zero(a);
  963. if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
  964. return 0;
  965. if (!BN_is_zero(d)) {
  966. fprintf(stderr, "Modular exponentiation test failed!\n");
  967. return 0;
  968. }
  969. /*
  970. * Craft an input whose Montgomery representation is 1, i.e., shorter
  971. * than the modulus m, in order to test the const time precomputation
  972. * scattering/gathering.
  973. */
  974. BN_one(a);
  975. BN_MONT_CTX_set(mont, m, ctx);
  976. if (!BN_from_montgomery(e, a, mont, ctx))
  977. return 0;
  978. if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
  979. return 0;
  980. if (!BN_mod_exp_simple(a, e, p, m, ctx))
  981. return 0;
  982. if (BN_cmp(a, d) != 0) {
  983. fprintf(stderr, "Modular exponentiation test failed!\n");
  984. return 0;
  985. }
  986. /* Finally, some regular test vectors. */
  987. BN_bntest_rand(e, 1024, 0, 0);
  988. if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
  989. return 0;
  990. if (!BN_mod_exp_simple(a, e, p, m, ctx))
  991. return 0;
  992. if (BN_cmp(a, d) != 0) {
  993. fprintf(stderr, "Modular exponentiation test failed!\n");
  994. return 0;
  995. }
  996. BN_free(a);
  997. BN_free(p);
  998. BN_free(m);
  999. BN_free(d);
  1000. BN_free(e);
  1001. return (1);
  1002. }
  1003. int test_exp(BIO *bp, BN_CTX *ctx)
  1004. {
  1005. BIGNUM *a, *b, *d, *e, *one;
  1006. int i;
  1007. a = BN_new();
  1008. b = BN_new();
  1009. d = BN_new();
  1010. e = BN_new();
  1011. one = BN_new();
  1012. BN_one(one);
  1013. for (i = 0; i < num2; i++) {
  1014. BN_bntest_rand(a, 20 + i * 5, 0, 0);
  1015. BN_bntest_rand(b, 2 + i, 0, 0);
  1016. if (BN_exp(d, a, b, ctx) <= 0)
  1017. return (0);
  1018. if (bp != NULL) {
  1019. if (!results) {
  1020. BN_print(bp, a);
  1021. BIO_puts(bp, " ^ ");
  1022. BN_print(bp, b);
  1023. BIO_puts(bp, " - ");
  1024. }
  1025. BN_print(bp, d);
  1026. BIO_puts(bp, "\n");
  1027. }
  1028. BN_one(e);
  1029. for (; !BN_is_zero(b); BN_sub(b, b, one))
  1030. BN_mul(e, e, a, ctx);
  1031. BN_sub(e, e, d);
  1032. if (!BN_is_zero(e)) {
  1033. fprintf(stderr, "Exponentiation test failed!\n");
  1034. return 0;
  1035. }
  1036. }
  1037. BN_free(a);
  1038. BN_free(b);
  1039. BN_free(d);
  1040. BN_free(e);
  1041. BN_free(one);
  1042. return (1);
  1043. }
  1044. #ifndef OPENSSL_NO_EC2M
  1045. int test_gf2m_add(BIO *bp)
  1046. {
  1047. BIGNUM a, b, c;
  1048. int i, ret = 0;
  1049. BN_init(&a);
  1050. BN_init(&b);
  1051. BN_init(&c);
  1052. for (i = 0; i < num0; i++) {
  1053. BN_rand(&a, 512, 0, 0);
  1054. BN_copy(&b, BN_value_one());
  1055. a.neg = rand_neg();
  1056. b.neg = rand_neg();
  1057. BN_GF2m_add(&c, &a, &b);
  1058. # if 0 /* make test uses ouput in bc but bc can't
  1059. * handle GF(2^m) arithmetic */
  1060. if (bp != NULL) {
  1061. if (!results) {
  1062. BN_print(bp, &a);
  1063. BIO_puts(bp, " ^ ");
  1064. BN_print(bp, &b);
  1065. BIO_puts(bp, " = ");
  1066. }
  1067. BN_print(bp, &c);
  1068. BIO_puts(bp, "\n");
  1069. }
  1070. # endif
  1071. /* Test that two added values have the correct parity. */
  1072. if ((BN_is_odd(&a) && BN_is_odd(&c))
  1073. || (!BN_is_odd(&a) && !BN_is_odd(&c))) {
  1074. fprintf(stderr, "GF(2^m) addition test (a) failed!\n");
  1075. goto err;
  1076. }
  1077. BN_GF2m_add(&c, &c, &c);
  1078. /* Test that c + c = 0. */
  1079. if (!BN_is_zero(&c)) {
  1080. fprintf(stderr, "GF(2^m) addition test (b) failed!\n");
  1081. goto err;
  1082. }
  1083. }
  1084. ret = 1;
  1085. err:
  1086. BN_free(&a);
  1087. BN_free(&b);
  1088. BN_free(&c);
  1089. return ret;
  1090. }
  1091. int test_gf2m_mod(BIO *bp)
  1092. {
  1093. BIGNUM *a, *b[2], *c, *d, *e;
  1094. int i, j, ret = 0;
  1095. int p0[] = { 163, 7, 6, 3, 0, -1 };
  1096. int p1[] = { 193, 15, 0, -1 };
  1097. a = BN_new();
  1098. b[0] = BN_new();
  1099. b[1] = BN_new();
  1100. c = BN_new();
  1101. d = BN_new();
  1102. e = BN_new();
  1103. BN_GF2m_arr2poly(p0, b[0]);
  1104. BN_GF2m_arr2poly(p1, b[1]);
  1105. for (i = 0; i < num0; i++) {
  1106. BN_bntest_rand(a, 1024, 0, 0);
  1107. for (j = 0; j < 2; j++) {
  1108. BN_GF2m_mod(c, a, b[j]);
  1109. # if 0 /* make test uses ouput in bc but bc can't
  1110. * handle GF(2^m) arithmetic */
  1111. if (bp != NULL) {
  1112. if (!results) {
  1113. BN_print(bp, a);
  1114. BIO_puts(bp, " % ");
  1115. BN_print(bp, b[j]);
  1116. BIO_puts(bp, " - ");
  1117. BN_print(bp, c);
  1118. BIO_puts(bp, "\n");
  1119. }
  1120. }
  1121. # endif
  1122. BN_GF2m_add(d, a, c);
  1123. BN_GF2m_mod(e, d, b[j]);
  1124. /* Test that a + (a mod p) mod p == 0. */
  1125. if (!BN_is_zero(e)) {
  1126. fprintf(stderr, "GF(2^m) modulo test failed!\n");
  1127. goto err;
  1128. }
  1129. }
  1130. }
  1131. ret = 1;
  1132. err:
  1133. BN_free(a);
  1134. BN_free(b[0]);
  1135. BN_free(b[1]);
  1136. BN_free(c);
  1137. BN_free(d);
  1138. BN_free(e);
  1139. return ret;
  1140. }
  1141. int test_gf2m_mod_mul(BIO *bp, BN_CTX *ctx)
  1142. {
  1143. BIGNUM *a, *b[2], *c, *d, *e, *f, *g, *h;
  1144. int i, j, ret = 0;
  1145. int p0[] = { 163, 7, 6, 3, 0, -1 };
  1146. int p1[] = { 193, 15, 0, -1 };
  1147. a = BN_new();
  1148. b[0] = BN_new();
  1149. b[1] = BN_new();
  1150. c = BN_new();
  1151. d = BN_new();
  1152. e = BN_new();
  1153. f = BN_new();
  1154. g = BN_new();
  1155. h = BN_new();
  1156. BN_GF2m_arr2poly(p0, b[0]);
  1157. BN_GF2m_arr2poly(p1, b[1]);
  1158. for (i = 0; i < num0; i++) {
  1159. BN_bntest_rand(a, 1024, 0, 0);
  1160. BN_bntest_rand(c, 1024, 0, 0);
  1161. BN_bntest_rand(d, 1024, 0, 0);
  1162. for (j = 0; j < 2; j++) {
  1163. BN_GF2m_mod_mul(e, a, c, b[j], ctx);
  1164. # if 0 /* make test uses ouput in bc but bc can't
  1165. * handle GF(2^m) arithmetic */
  1166. if (bp != NULL) {
  1167. if (!results) {
  1168. BN_print(bp, a);
  1169. BIO_puts(bp, " * ");
  1170. BN_print(bp, c);
  1171. BIO_puts(bp, " % ");
  1172. BN_print(bp, b[j]);
  1173. BIO_puts(bp, " - ");
  1174. BN_print(bp, e);
  1175. BIO_puts(bp, "\n");
  1176. }
  1177. }
  1178. # endif
  1179. BN_GF2m_add(f, a, d);
  1180. BN_GF2m_mod_mul(g, f, c, b[j], ctx);
  1181. BN_GF2m_mod_mul(h, d, c, b[j], ctx);
  1182. BN_GF2m_add(f, e, g);
  1183. BN_GF2m_add(f, f, h);
  1184. /* Test that (a+d)*c = a*c + d*c. */
  1185. if (!BN_is_zero(f)) {
  1186. fprintf(stderr,
  1187. "GF(2^m) modular multiplication test failed!\n");
  1188. goto err;
  1189. }
  1190. }
  1191. }
  1192. ret = 1;
  1193. err:
  1194. BN_free(a);
  1195. BN_free(b[0]);
  1196. BN_free(b[1]);
  1197. BN_free(c);
  1198. BN_free(d);
  1199. BN_free(e);
  1200. BN_free(f);
  1201. BN_free(g);
  1202. BN_free(h);
  1203. return ret;
  1204. }
  1205. int test_gf2m_mod_sqr(BIO *bp, BN_CTX *ctx)
  1206. {
  1207. BIGNUM *a, *b[2], *c, *d;
  1208. int i, j, ret = 0;
  1209. int p0[] = { 163, 7, 6, 3, 0, -1 };
  1210. int p1[] = { 193, 15, 0, -1 };
  1211. a = BN_new();
  1212. b[0] = BN_new();
  1213. b[1] = BN_new();
  1214. c = BN_new();
  1215. d = BN_new();
  1216. BN_GF2m_arr2poly(p0, b[0]);
  1217. BN_GF2m_arr2poly(p1, b[1]);
  1218. for (i = 0; i < num0; i++) {
  1219. BN_bntest_rand(a, 1024, 0, 0);
  1220. for (j = 0; j < 2; j++) {
  1221. BN_GF2m_mod_sqr(c, a, b[j], ctx);
  1222. BN_copy(d, a);
  1223. BN_GF2m_mod_mul(d, a, d, b[j], ctx);
  1224. # if 0 /* make test uses ouput in bc but bc can't
  1225. * handle GF(2^m) arithmetic */
  1226. if (bp != NULL) {
  1227. if (!results) {
  1228. BN_print(bp, a);
  1229. BIO_puts(bp, " ^ 2 % ");
  1230. BN_print(bp, b[j]);
  1231. BIO_puts(bp, " = ");
  1232. BN_print(bp, c);
  1233. BIO_puts(bp, "; a * a = ");
  1234. BN_print(bp, d);
  1235. BIO_puts(bp, "\n");
  1236. }
  1237. }
  1238. # endif
  1239. BN_GF2m_add(d, c, d);
  1240. /* Test that a*a = a^2. */
  1241. if (!BN_is_zero(d)) {
  1242. fprintf(stderr, "GF(2^m) modular squaring test failed!\n");
  1243. goto err;
  1244. }
  1245. }
  1246. }
  1247. ret = 1;
  1248. err:
  1249. BN_free(a);
  1250. BN_free(b[0]);
  1251. BN_free(b[1]);
  1252. BN_free(c);
  1253. BN_free(d);
  1254. return ret;
  1255. }
  1256. int test_gf2m_mod_inv(BIO *bp, BN_CTX *ctx)
  1257. {
  1258. BIGNUM *a, *b[2], *c, *d;
  1259. int i, j, ret = 0;
  1260. int p0[] = { 163, 7, 6, 3, 0, -1 };
  1261. int p1[] = { 193, 15, 0, -1 };
  1262. a = BN_new();
  1263. b[0] = BN_new();
  1264. b[1] = BN_new();
  1265. c = BN_new();
  1266. d = BN_new();
  1267. BN_GF2m_arr2poly(p0, b[0]);
  1268. BN_GF2m_arr2poly(p1, b[1]);
  1269. for (i = 0; i < num0; i++) {
  1270. BN_bntest_rand(a, 512, 0, 0);
  1271. for (j = 0; j < 2; j++) {
  1272. BN_GF2m_mod_inv(c, a, b[j], ctx);
  1273. BN_GF2m_mod_mul(d, a, c, b[j], ctx);
  1274. # if 0 /* make test uses ouput in bc but bc can't
  1275. * handle GF(2^m) arithmetic */
  1276. if (bp != NULL) {
  1277. if (!results) {
  1278. BN_print(bp, a);
  1279. BIO_puts(bp, " * ");
  1280. BN_print(bp, c);
  1281. BIO_puts(bp, " - 1 % ");
  1282. BN_print(bp, b[j]);
  1283. BIO_puts(bp, "\n");
  1284. }
  1285. }
  1286. # endif
  1287. /* Test that ((1/a)*a) = 1. */
  1288. if (!BN_is_one(d)) {
  1289. fprintf(stderr, "GF(2^m) modular inversion test failed!\n");
  1290. goto err;
  1291. }
  1292. }
  1293. }
  1294. ret = 1;
  1295. err:
  1296. BN_free(a);
  1297. BN_free(b[0]);
  1298. BN_free(b[1]);
  1299. BN_free(c);
  1300. BN_free(d);
  1301. return ret;
  1302. }
  1303. int test_gf2m_mod_div(BIO *bp, BN_CTX *ctx)
  1304. {
  1305. BIGNUM *a, *b[2], *c, *d, *e, *f;
  1306. int i, j, ret = 0;
  1307. int p0[] = { 163, 7, 6, 3, 0, -1 };
  1308. int p1[] = { 193, 15, 0, -1 };
  1309. a = BN_new();
  1310. b[0] = BN_new();
  1311. b[1] = BN_new();
  1312. c = BN_new();
  1313. d = BN_new();
  1314. e = BN_new();
  1315. f = BN_new();
  1316. BN_GF2m_arr2poly(p0, b[0]);
  1317. BN_GF2m_arr2poly(p1, b[1]);
  1318. for (i = 0; i < num0; i++) {
  1319. BN_bntest_rand(a, 512, 0, 0);
  1320. BN_bntest_rand(c, 512, 0, 0);
  1321. for (j = 0; j < 2; j++) {
  1322. BN_GF2m_mod_div(d, a, c, b[j], ctx);
  1323. BN_GF2m_mod_mul(e, d, c, b[j], ctx);
  1324. BN_GF2m_mod_div(f, a, e, b[j], ctx);
  1325. # if 0 /* make test uses ouput in bc but bc can't
  1326. * handle GF(2^m) arithmetic */
  1327. if (bp != NULL) {
  1328. if (!results) {
  1329. BN_print(bp, a);
  1330. BIO_puts(bp, " = ");
  1331. BN_print(bp, c);
  1332. BIO_puts(bp, " * ");
  1333. BN_print(bp, d);
  1334. BIO_puts(bp, " % ");
  1335. BN_print(bp, b[j]);
  1336. BIO_puts(bp, "\n");
  1337. }
  1338. }
  1339. # endif
  1340. /* Test that ((a/c)*c)/a = 1. */
  1341. if (!BN_is_one(f)) {
  1342. fprintf(stderr, "GF(2^m) modular division test failed!\n");
  1343. goto err;
  1344. }
  1345. }
  1346. }
  1347. ret = 1;
  1348. err:
  1349. BN_free(a);
  1350. BN_free(b[0]);
  1351. BN_free(b[1]);
  1352. BN_free(c);
  1353. BN_free(d);
  1354. BN_free(e);
  1355. BN_free(f);
  1356. return ret;
  1357. }
  1358. int test_gf2m_mod_exp(BIO *bp, BN_CTX *ctx)
  1359. {
  1360. BIGNUM *a, *b[2], *c, *d, *e, *f;
  1361. int i, j, ret = 0;
  1362. int p0[] = { 163, 7, 6, 3, 0, -1 };
  1363. int p1[] = { 193, 15, 0, -1 };
  1364. a = BN_new();
  1365. b[0] = BN_new();
  1366. b[1] = BN_new();
  1367. c = BN_new();
  1368. d = BN_new();
  1369. e = BN_new();
  1370. f = BN_new();
  1371. BN_GF2m_arr2poly(p0, b[0]);
  1372. BN_GF2m_arr2poly(p1, b[1]);
  1373. for (i = 0; i < num0; i++) {
  1374. BN_bntest_rand(a, 512, 0, 0);
  1375. BN_bntest_rand(c, 512, 0, 0);
  1376. BN_bntest_rand(d, 512, 0, 0);
  1377. for (j = 0; j < 2; j++) {
  1378. BN_GF2m_mod_exp(e, a, c, b[j], ctx);
  1379. BN_GF2m_mod_exp(f, a, d, b[j], ctx);
  1380. BN_GF2m_mod_mul(e, e, f, b[j], ctx);
  1381. BN_add(f, c, d);
  1382. BN_GF2m_mod_exp(f, a, f, b[j], ctx);
  1383. # if 0 /* make test uses ouput in bc but bc can't
  1384. * handle GF(2^m) arithmetic */
  1385. if (bp != NULL) {
  1386. if (!results) {
  1387. BN_print(bp, a);
  1388. BIO_puts(bp, " ^ (");
  1389. BN_print(bp, c);
  1390. BIO_puts(bp, " + ");
  1391. BN_print(bp, d);
  1392. BIO_puts(bp, ") = ");
  1393. BN_print(bp, e);
  1394. BIO_puts(bp, "; - ");
  1395. BN_print(bp, f);
  1396. BIO_puts(bp, " % ");
  1397. BN_print(bp, b[j]);
  1398. BIO_puts(bp, "\n");
  1399. }
  1400. }
  1401. # endif
  1402. BN_GF2m_add(f, e, f);
  1403. /* Test that a^(c+d)=a^c*a^d. */
  1404. if (!BN_is_zero(f)) {
  1405. fprintf(stderr,
  1406. "GF(2^m) modular exponentiation test failed!\n");
  1407. goto err;
  1408. }
  1409. }
  1410. }
  1411. ret = 1;
  1412. err:
  1413. BN_free(a);
  1414. BN_free(b[0]);
  1415. BN_free(b[1]);
  1416. BN_free(c);
  1417. BN_free(d);
  1418. BN_free(e);
  1419. BN_free(f);
  1420. return ret;
  1421. }
  1422. int test_gf2m_mod_sqrt(BIO *bp, BN_CTX *ctx)
  1423. {
  1424. BIGNUM *a, *b[2], *c, *d, *e, *f;
  1425. int i, j, ret = 0;
  1426. int p0[] = { 163, 7, 6, 3, 0, -1 };
  1427. int p1[] = { 193, 15, 0, -1 };
  1428. a = BN_new();
  1429. b[0] = BN_new();
  1430. b[1] = BN_new();
  1431. c = BN_new();
  1432. d = BN_new();
  1433. e = BN_new();
  1434. f = BN_new();
  1435. BN_GF2m_arr2poly(p0, b[0]);
  1436. BN_GF2m_arr2poly(p1, b[1]);
  1437. for (i = 0; i < num0; i++) {
  1438. BN_bntest_rand(a, 512, 0, 0);
  1439. for (j = 0; j < 2; j++) {
  1440. BN_GF2m_mod(c, a, b[j]);
  1441. BN_GF2m_mod_sqrt(d, a, b[j], ctx);
  1442. BN_GF2m_mod_sqr(e, d, b[j], ctx);
  1443. # if 0 /* make test uses ouput in bc but bc can't
  1444. * handle GF(2^m) arithmetic */
  1445. if (bp != NULL) {
  1446. if (!results) {
  1447. BN_print(bp, d);
  1448. BIO_puts(bp, " ^ 2 - ");
  1449. BN_print(bp, a);
  1450. BIO_puts(bp, "\n");
  1451. }
  1452. }
  1453. # endif
  1454. BN_GF2m_add(f, c, e);
  1455. /* Test that d^2 = a, where d = sqrt(a). */
  1456. if (!BN_is_zero(f)) {
  1457. fprintf(stderr, "GF(2^m) modular square root test failed!\n");
  1458. goto err;
  1459. }
  1460. }
  1461. }
  1462. ret = 1;
  1463. err:
  1464. BN_free(a);
  1465. BN_free(b[0]);
  1466. BN_free(b[1]);
  1467. BN_free(c);
  1468. BN_free(d);
  1469. BN_free(e);
  1470. BN_free(f);
  1471. return ret;
  1472. }
  1473. int test_gf2m_mod_solve_quad(BIO *bp, BN_CTX *ctx)
  1474. {
  1475. BIGNUM *a, *b[2], *c, *d, *e;
  1476. int i, j, s = 0, t, ret = 0;
  1477. int p0[] = { 163, 7, 6, 3, 0, -1 };
  1478. int p1[] = { 193, 15, 0, -1 };
  1479. a = BN_new();
  1480. b[0] = BN_new();
  1481. b[1] = BN_new();
  1482. c = BN_new();
  1483. d = BN_new();
  1484. e = BN_new();
  1485. BN_GF2m_arr2poly(p0, b[0]);
  1486. BN_GF2m_arr2poly(p1, b[1]);
  1487. for (i = 0; i < num0; i++) {
  1488. BN_bntest_rand(a, 512, 0, 0);
  1489. for (j = 0; j < 2; j++) {
  1490. t = BN_GF2m_mod_solve_quad(c, a, b[j], ctx);
  1491. if (t) {
  1492. s++;
  1493. BN_GF2m_mod_sqr(d, c, b[j], ctx);
  1494. BN_GF2m_add(d, c, d);
  1495. BN_GF2m_mod(e, a, b[j]);
  1496. # if 0 /* make test uses ouput in bc but bc can't
  1497. * handle GF(2^m) arithmetic */
  1498. if (bp != NULL) {
  1499. if (!results) {
  1500. BN_print(bp, c);
  1501. BIO_puts(bp, " is root of z^2 + z = ");
  1502. BN_print(bp, a);
  1503. BIO_puts(bp, " % ");
  1504. BN_print(bp, b[j]);
  1505. BIO_puts(bp, "\n");
  1506. }
  1507. }
  1508. # endif
  1509. BN_GF2m_add(e, e, d);
  1510. /*
  1511. * Test that solution of quadratic c satisfies c^2 + c = a.
  1512. */
  1513. if (!BN_is_zero(e)) {
  1514. fprintf(stderr,
  1515. "GF(2^m) modular solve quadratic test failed!\n");
  1516. goto err;
  1517. }
  1518. } else {
  1519. # if 0 /* make test uses ouput in bc but bc can't
  1520. * handle GF(2^m) arithmetic */
  1521. if (bp != NULL) {
  1522. if (!results) {
  1523. BIO_puts(bp, "There are no roots of z^2 + z = ");
  1524. BN_print(bp, a);
  1525. BIO_puts(bp, " % ");
  1526. BN_print(bp, b[j]);
  1527. BIO_puts(bp, "\n");
  1528. }
  1529. }
  1530. # endif
  1531. }
  1532. }
  1533. }
  1534. if (s == 0) {
  1535. fprintf(stderr,
  1536. "All %i tests of GF(2^m) modular solve quadratic resulted in no roots;\n",
  1537. num0);
  1538. fprintf(stderr,
  1539. "this is very unlikely and probably indicates an error.\n");
  1540. goto err;
  1541. }
  1542. ret = 1;
  1543. err:
  1544. BN_free(a);
  1545. BN_free(b[0]);
  1546. BN_free(b[1]);
  1547. BN_free(c);
  1548. BN_free(d);
  1549. BN_free(e);
  1550. return ret;
  1551. }
  1552. #endif
  1553. static int genprime_cb(int p, int n, BN_GENCB *arg)
  1554. {
  1555. char c = '*';
  1556. if (p == 0)
  1557. c = '.';
  1558. if (p == 1)
  1559. c = '+';
  1560. if (p == 2)
  1561. c = '*';
  1562. if (p == 3)
  1563. c = '\n';
  1564. putc(c, stderr);
  1565. fflush(stderr);
  1566. return 1;
  1567. }
  1568. int test_kron(BIO *bp, BN_CTX *ctx)
  1569. {
  1570. BN_GENCB cb;
  1571. BIGNUM *a, *b, *r, *t;
  1572. int i;
  1573. int legendre, kronecker;
  1574. int ret = 0;
  1575. a = BN_new();
  1576. b = BN_new();
  1577. r = BN_new();
  1578. t = BN_new();
  1579. if (a == NULL || b == NULL || r == NULL || t == NULL)
  1580. goto err;
  1581. BN_GENCB_set(&cb, genprime_cb, NULL);
  1582. /*
  1583. * We test BN_kronecker(a, b, ctx) just for b odd (Jacobi symbol). In
  1584. * this case we know that if b is prime, then BN_kronecker(a, b, ctx) is
  1585. * congruent to $a^{(b-1)/2}$, modulo $b$ (Legendre symbol). So we
  1586. * generate a random prime b and compare these values for a number of
  1587. * random a's. (That is, we run the Solovay-Strassen primality test to
  1588. * confirm that b is prime, except that we don't want to test whether b
  1589. * is prime but whether BN_kronecker works.)
  1590. */
  1591. if (!BN_generate_prime_ex(b, 512, 0, NULL, NULL, &cb))
  1592. goto err;
  1593. b->neg = rand_neg();
  1594. putc('\n', stderr);
  1595. for (i = 0; i < num0; i++) {
  1596. if (!BN_bntest_rand(a, 512, 0, 0))
  1597. goto err;
  1598. a->neg = rand_neg();
  1599. /* t := (|b|-1)/2 (note that b is odd) */
  1600. if (!BN_copy(t, b))
  1601. goto err;
  1602. t->neg = 0;
  1603. if (!BN_sub_word(t, 1))
  1604. goto err;
  1605. if (!BN_rshift1(t, t))
  1606. goto err;
  1607. /* r := a^t mod b */
  1608. b->neg = 0;
  1609. if (!BN_mod_exp_recp(r, a, t, b, ctx))
  1610. goto err;
  1611. b->neg = 1;
  1612. if (BN_is_word(r, 1))
  1613. legendre = 1;
  1614. else if (BN_is_zero(r))
  1615. legendre = 0;
  1616. else {
  1617. if (!BN_add_word(r, 1))
  1618. goto err;
  1619. if (0 != BN_ucmp(r, b)) {
  1620. fprintf(stderr, "Legendre symbol computation failed\n");
  1621. goto err;
  1622. }
  1623. legendre = -1;
  1624. }
  1625. kronecker = BN_kronecker(a, b, ctx);
  1626. if (kronecker < -1)
  1627. goto err;
  1628. /* we actually need BN_kronecker(a, |b|) */
  1629. if (a->neg && b->neg)
  1630. kronecker = -kronecker;
  1631. if (legendre != kronecker) {
  1632. fprintf(stderr, "legendre != kronecker; a = ");
  1633. BN_print_fp(stderr, a);
  1634. fprintf(stderr, ", b = ");
  1635. BN_print_fp(stderr, b);
  1636. fprintf(stderr, "\n");
  1637. goto err;
  1638. }
  1639. putc('.', stderr);
  1640. fflush(stderr);
  1641. }
  1642. putc('\n', stderr);
  1643. fflush(stderr);
  1644. ret = 1;
  1645. err:
  1646. if (a != NULL)
  1647. BN_free(a);
  1648. if (b != NULL)
  1649. BN_free(b);
  1650. if (r != NULL)
  1651. BN_free(r);
  1652. if (t != NULL)
  1653. BN_free(t);
  1654. return ret;
  1655. }
  1656. int test_sqrt(BIO *bp, BN_CTX *ctx)
  1657. {
  1658. BN_GENCB cb;
  1659. BIGNUM *a, *p, *r;
  1660. int i, j;
  1661. int ret = 0;
  1662. a = BN_new();
  1663. p = BN_new();
  1664. r = BN_new();
  1665. if (a == NULL || p == NULL || r == NULL)
  1666. goto err;
  1667. BN_GENCB_set(&cb, genprime_cb, NULL);
  1668. for (i = 0; i < 16; i++) {
  1669. if (i < 8) {
  1670. unsigned primes[8] = { 2, 3, 5, 7, 11, 13, 17, 19 };
  1671. if (!BN_set_word(p, primes[i]))
  1672. goto err;
  1673. } else {
  1674. if (!BN_set_word(a, 32))
  1675. goto err;
  1676. if (!BN_set_word(r, 2 * i + 1))
  1677. goto err;
  1678. if (!BN_generate_prime_ex(p, 256, 0, a, r, &cb))
  1679. goto err;
  1680. putc('\n', stderr);
  1681. }
  1682. p->neg = rand_neg();
  1683. for (j = 0; j < num2; j++) {
  1684. /*
  1685. * construct 'a' such that it is a square modulo p, but in
  1686. * general not a proper square and not reduced modulo p
  1687. */
  1688. if (!BN_bntest_rand(r, 256, 0, 3))
  1689. goto err;
  1690. if (!BN_nnmod(r, r, p, ctx))
  1691. goto err;
  1692. if (!BN_mod_sqr(r, r, p, ctx))
  1693. goto err;
  1694. if (!BN_bntest_rand(a, 256, 0, 3))
  1695. goto err;
  1696. if (!BN_nnmod(a, a, p, ctx))
  1697. goto err;
  1698. if (!BN_mod_sqr(a, a, p, ctx))
  1699. goto err;
  1700. if (!BN_mul(a, a, r, ctx))
  1701. goto err;
  1702. if (rand_neg())
  1703. if (!BN_sub(a, a, p))
  1704. goto err;
  1705. if (!BN_mod_sqrt(r, a, p, ctx))
  1706. goto err;
  1707. if (!BN_mod_sqr(r, r, p, ctx))
  1708. goto err;
  1709. if (!BN_nnmod(a, a, p, ctx))
  1710. goto err;
  1711. if (BN_cmp(a, r) != 0) {
  1712. fprintf(stderr, "BN_mod_sqrt failed: a = ");
  1713. BN_print_fp(stderr, a);
  1714. fprintf(stderr, ", r = ");
  1715. BN_print_fp(stderr, r);
  1716. fprintf(stderr, ", p = ");
  1717. BN_print_fp(stderr, p);
  1718. fprintf(stderr, "\n");
  1719. goto err;
  1720. }
  1721. putc('.', stderr);
  1722. fflush(stderr);
  1723. }
  1724. putc('\n', stderr);
  1725. fflush(stderr);
  1726. }
  1727. ret = 1;
  1728. err:
  1729. if (a != NULL)
  1730. BN_free(a);
  1731. if (p != NULL)
  1732. BN_free(p);
  1733. if (r != NULL)
  1734. BN_free(r);
  1735. return ret;
  1736. }
  1737. int test_lshift(BIO *bp, BN_CTX *ctx, BIGNUM *a_)
  1738. {
  1739. BIGNUM *a, *b, *c, *d;
  1740. int i;
  1741. b = BN_new();
  1742. c = BN_new();
  1743. d = BN_new();
  1744. BN_one(c);
  1745. if (a_)
  1746. a = a_;
  1747. else {
  1748. a = BN_new();
  1749. BN_bntest_rand(a, 200, 0, 0);
  1750. a->neg = rand_neg();
  1751. }
  1752. for (i = 0; i < num0; i++) {
  1753. BN_lshift(b, a, i + 1);
  1754. BN_add(c, c, c);
  1755. if (bp != NULL) {
  1756. if (!results) {
  1757. BN_print(bp, a);
  1758. BIO_puts(bp, " * ");
  1759. BN_print(bp, c);
  1760. BIO_puts(bp, " - ");
  1761. }
  1762. BN_print(bp, b);
  1763. BIO_puts(bp, "\n");
  1764. }
  1765. BN_mul(d, a, c, ctx);
  1766. BN_sub(d, d, b);
  1767. if (!BN_is_zero(d)) {
  1768. fprintf(stderr, "Left shift test failed!\n");
  1769. fprintf(stderr, "a=");
  1770. BN_print_fp(stderr, a);
  1771. fprintf(stderr, "\nb=");
  1772. BN_print_fp(stderr, b);
  1773. fprintf(stderr, "\nc=");
  1774. BN_print_fp(stderr, c);
  1775. fprintf(stderr, "\nd=");
  1776. BN_print_fp(stderr, d);
  1777. fprintf(stderr, "\n");
  1778. return 0;
  1779. }
  1780. }
  1781. BN_free(a);
  1782. BN_free(b);
  1783. BN_free(c);
  1784. BN_free(d);
  1785. return (1);
  1786. }
  1787. int test_lshift1(BIO *bp)
  1788. {
  1789. BIGNUM *a, *b, *c;
  1790. int i;
  1791. a = BN_new();
  1792. b = BN_new();
  1793. c = BN_new();
  1794. BN_bntest_rand(a, 200, 0, 0);
  1795. a->neg = rand_neg();
  1796. for (i = 0; i < num0; i++) {
  1797. BN_lshift1(b, a);
  1798. if (bp != NULL) {
  1799. if (!results) {
  1800. BN_print(bp, a);
  1801. BIO_puts(bp, " * 2");
  1802. BIO_puts(bp, " - ");
  1803. }
  1804. BN_print(bp, b);
  1805. BIO_puts(bp, "\n");
  1806. }
  1807. BN_add(c, a, a);
  1808. BN_sub(a, b, c);
  1809. if (!BN_is_zero(a)) {
  1810. fprintf(stderr, "Left shift one test failed!\n");
  1811. return 0;
  1812. }
  1813. BN_copy(a, b);
  1814. }
  1815. BN_free(a);
  1816. BN_free(b);
  1817. BN_free(c);
  1818. return (1);
  1819. }
  1820. int test_rshift(BIO *bp, BN_CTX *ctx)
  1821. {
  1822. BIGNUM *a, *b, *c, *d, *e;
  1823. int i;
  1824. a = BN_new();
  1825. b = BN_new();
  1826. c = BN_new();
  1827. d = BN_new();
  1828. e = BN_new();
  1829. BN_one(c);
  1830. BN_bntest_rand(a, 200, 0, 0);
  1831. a->neg = rand_neg();
  1832. for (i = 0; i < num0; i++) {
  1833. BN_rshift(b, a, i + 1);
  1834. BN_add(c, c, c);
  1835. if (bp != NULL) {
  1836. if (!results) {
  1837. BN_print(bp, a);
  1838. BIO_puts(bp, " / ");
  1839. BN_print(bp, c);
  1840. BIO_puts(bp, " - ");
  1841. }
  1842. BN_print(bp, b);
  1843. BIO_puts(bp, "\n");
  1844. }
  1845. BN_div(d, e, a, c, ctx);
  1846. BN_sub(d, d, b);
  1847. if (!BN_is_zero(d)) {
  1848. fprintf(stderr, "Right shift test failed!\n");
  1849. return 0;
  1850. }
  1851. }
  1852. BN_free(a);
  1853. BN_free(b);
  1854. BN_free(c);
  1855. BN_free(d);
  1856. BN_free(e);
  1857. return (1);
  1858. }
  1859. int test_rshift1(BIO *bp)
  1860. {
  1861. BIGNUM *a, *b, *c;
  1862. int i;
  1863. a = BN_new();
  1864. b = BN_new();
  1865. c = BN_new();
  1866. BN_bntest_rand(a, 200, 0, 0);
  1867. a->neg = rand_neg();
  1868. for (i = 0; i < num0; i++) {
  1869. BN_rshift1(b, a);
  1870. if (bp != NULL) {
  1871. if (!results) {
  1872. BN_print(bp, a);
  1873. BIO_puts(bp, " / 2");
  1874. BIO_puts(bp, " - ");
  1875. }
  1876. BN_print(bp, b);
  1877. BIO_puts(bp, "\n");
  1878. }
  1879. BN_sub(c, a, b);
  1880. BN_sub(c, c, b);
  1881. if (!BN_is_zero(c) && !BN_abs_is_word(c, 1)) {
  1882. fprintf(stderr, "Right shift one test failed!\n");
  1883. return 0;
  1884. }
  1885. BN_copy(a, b);
  1886. }
  1887. BN_free(a);
  1888. BN_free(b);
  1889. BN_free(c);
  1890. return (1);
  1891. }
  1892. int rand_neg(void)
  1893. {
  1894. static unsigned int neg = 0;
  1895. static int sign[8] = { 0, 0, 0, 1, 1, 0, 1, 1 };
  1896. return (sign[(neg++) % 8]);
  1897. }