bntest.c 50 KB

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