bntest.c 55 KB

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