Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

1420 linhas
32 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  59. *
  60. * Portions of the attached software ("Contribution") are developed by
  61. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  62. *
  63. * The Contribution is licensed pursuant to the Eric Young open source
  64. * license provided above.
  65. *
  66. * The binary polynomial arithmetic software is originally written by
  67. * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems
  68. * Laboratories. */
  69. #include <stdio.h>
  70. #include <openssl/bio.h>
  71. #include <openssl/bn.h>
  72. #include <openssl/err.h>
  73. #include <openssl/mem.h>
  74. #include "internal.h"
  75. static const int num0 = 100; /* number of tests */
  76. static const int num1 = 50; /* additional tests for some functions */
  77. static const int num2 = 5; /* number of tests for slow functions */
  78. int test_add(BIO *bp);
  79. int test_sub(BIO *bp);
  80. int test_lshift1(BIO *bp);
  81. int test_lshift(BIO *bp, BN_CTX *ctx, BIGNUM *a_);
  82. int test_rshift1(BIO *bp);
  83. int test_rshift(BIO *bp, BN_CTX *ctx);
  84. int test_sqr(BIO *bp, BN_CTX *ctx);
  85. int test_mul(BIO *bp);
  86. int test_div(BIO *bp, BN_CTX *ctx);
  87. int rand_neg(void);
  88. int test_div_word(BIO *bp);
  89. int test_mont(BIO *bp, BN_CTX *ctx);
  90. int test_mod(BIO *bp, BN_CTX *ctx);
  91. int test_mod_mul(BIO *bp, BN_CTX *ctx);
  92. int test_mod_exp(BIO *bp, BN_CTX *ctx);
  93. int test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx);
  94. int test_exp(BIO *bp, BN_CTX *ctx);
  95. int test_mod_sqrt(BIO *bp, BN_CTX *ctx);
  96. static int test_exp_mod_zero();
  97. int test_small_prime(BIO *bp,BN_CTX *ctx);
  98. int test_mod_exp_mont5(BIO *bp, BN_CTX *ctx);
  99. int test_sqrt(BIO *bp, BN_CTX *ctx);
  100. int test_bn2bin_padded(BIO *bp, BN_CTX *ctx);
  101. #if 0
  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. #endif
  112. static int results = 0;
  113. static unsigned char lst[] =
  114. "\xC6\x4F\x43\x04\x2A\xEA\xCA\x6E\x58\x36\x80\x5B\xE8\xC9"
  115. "\x9B\x04\x5D\x48\x36\xC2\xFD\x16\xC9\x64\xF0";
  116. static void ERR_print_errors_fp(FILE *out) {
  117. }
  118. static void message(BIO *out, char *m) {
  119. BIO_puts(out, "print \"test ");
  120. BIO_puts(out, m);
  121. BIO_puts(out, "\\n\"\n");
  122. }
  123. int main(int argc, char *argv[]) {
  124. BN_CTX *ctx;
  125. BIO *out = NULL;
  126. char *outfile = NULL;
  127. results = 0;
  128. argc--;
  129. argv++;
  130. while (argc >= 1) {
  131. if (strcmp(*argv, "-results") == 0)
  132. results = 1;
  133. else if (strcmp(*argv, "-out") == 0) {
  134. if (--argc < 1)
  135. break;
  136. outfile = *(++argv);
  137. }
  138. argc--;
  139. argv++;
  140. }
  141. ctx = BN_CTX_new();
  142. if (ctx == NULL)
  143. return 1;
  144. out = BIO_new(BIO_s_file());
  145. if (out == NULL) {
  146. return 1;
  147. }
  148. if (outfile == NULL) {
  149. BIO_set_fp(out, stdout, BIO_NOCLOSE);
  150. } else {
  151. if (!BIO_write_filename(out, outfile)) {
  152. perror(outfile);
  153. return 1;
  154. }
  155. }
  156. if (!results)
  157. BIO_puts(out, "obase=16\nibase=16\n");
  158. message(out, "BN_add");
  159. if (!test_add(out))
  160. goto err;
  161. (void)BIO_flush(out);
  162. message(out, "BN_sub");
  163. if (!test_sub(out))
  164. goto err;
  165. (void)BIO_flush(out);
  166. message(out, "BN_lshift1");
  167. if (!test_lshift1(out))
  168. goto err;
  169. (void)BIO_flush(out);
  170. message(out, "BN_lshift (fixed)");
  171. if (!test_lshift(out, ctx, BN_bin2bn(lst, sizeof(lst) - 1, NULL)))
  172. goto err;
  173. (void)BIO_flush(out);
  174. message(out, "BN_lshift");
  175. if (!test_lshift(out, ctx, NULL))
  176. goto err;
  177. (void)BIO_flush(out);
  178. message(out, "BN_rshift1");
  179. if (!test_rshift1(out))
  180. goto err;
  181. (void)BIO_flush(out);
  182. message(out, "BN_rshift");
  183. if (!test_rshift(out, ctx))
  184. goto err;
  185. (void)BIO_flush(out);
  186. message(out, "BN_sqr");
  187. if (!test_sqr(out, ctx))
  188. goto err;
  189. (void)BIO_flush(out);
  190. message(out, "BN_mul");
  191. if (!test_mul(out))
  192. goto err;
  193. (void)BIO_flush(out);
  194. message(out, "BN_div");
  195. if (!test_div(out, ctx))
  196. goto err;
  197. (void)BIO_flush(out);
  198. message(out, "BN_div_word");
  199. if (!test_div_word(out))
  200. goto err;
  201. (void)BIO_flush(out);
  202. message(out, "BN_mod");
  203. if (!test_mod(out, ctx))
  204. goto err;
  205. (void)BIO_flush(out);
  206. message(out, "BN_mod_mul");
  207. if (!test_mod_mul(out, ctx))
  208. goto err;
  209. (void)BIO_flush(out);
  210. message(out, "BN_mont");
  211. if (!test_mont(out, ctx))
  212. goto err;
  213. (void)BIO_flush(out);
  214. message(out, "BN_mod_exp");
  215. if (!test_mod_exp(out, ctx))
  216. goto err;
  217. (void)BIO_flush(out);
  218. message(out, "BN_mod_exp_mont_consttime");
  219. if (!test_mod_exp_mont_consttime(out, ctx) ||
  220. !test_mod_exp_mont5(out, ctx)) {
  221. goto err;
  222. }
  223. (void)BIO_flush(out);
  224. message(out, "BN_exp");
  225. if (!test_exp(out, ctx) ||
  226. !test_exp_mod_zero()) {
  227. goto err;
  228. }
  229. (void)BIO_flush(out);
  230. message(out, "BN_mod_sqrt");
  231. if (!test_mod_sqrt(out, ctx))
  232. goto err;
  233. (void)BIO_flush(out);
  234. message(out, "Small prime generation");
  235. if (!test_small_prime(out, ctx))
  236. goto err;
  237. (void)BIO_flush(out);
  238. message(out, "BN_sqrt");
  239. if (!test_sqrt(out, ctx))
  240. goto err;
  241. (void)BIO_flush(out);
  242. message(out, "BN_bn2bin_padded");
  243. if (!test_bn2bin_padded(out, ctx))
  244. goto err;
  245. (void)BIO_flush(out);
  246. BN_CTX_free(ctx);
  247. BIO_free(out);
  248. printf("PASS\n");
  249. return 0;
  250. err:
  251. BIO_puts(out, "1\n"); /* make sure the Perl script fed by bc notices
  252. * the failure, see test_bn in test/Makefile.ssl*/
  253. (void)BIO_flush(out);
  254. return 1;
  255. }
  256. int test_add(BIO *bp) {
  257. BIGNUM a, b, c;
  258. int i;
  259. BN_init(&a);
  260. BN_init(&b);
  261. BN_init(&c);
  262. BN_rand(&a, 512, 0, 0);
  263. for (i = 0; i < num0; i++) {
  264. BN_rand(&b, 450 + i, 0, 0);
  265. a.neg = rand_neg();
  266. b.neg = rand_neg();
  267. BN_add(&c, &a, &b);
  268. if (bp != NULL) {
  269. if (!results) {
  270. BN_print(bp, &a);
  271. BIO_puts(bp, " + ");
  272. BN_print(bp, &b);
  273. BIO_puts(bp, " - ");
  274. }
  275. BN_print(bp, &c);
  276. BIO_puts(bp, "\n");
  277. }
  278. a.neg = !a.neg;
  279. b.neg = !b.neg;
  280. BN_add(&c, &c, &b);
  281. BN_add(&c, &c, &a);
  282. if (!BN_is_zero(&c)) {
  283. fprintf(stderr, "Add test failed!\n");
  284. return 0;
  285. }
  286. }
  287. BN_free(&a);
  288. BN_free(&b);
  289. BN_free(&c);
  290. return (1);
  291. }
  292. int test_sub(BIO *bp) {
  293. BIGNUM a, b, c;
  294. int i;
  295. BN_init(&a);
  296. BN_init(&b);
  297. BN_init(&c);
  298. for (i = 0; i < num0 + num1; i++) {
  299. if (i < num1) {
  300. BN_rand(&a, 512, 0, 0);
  301. BN_copy(&b, &a);
  302. if (BN_set_bit(&a, i) == 0)
  303. return (0);
  304. BN_add_word(&b, i);
  305. } else {
  306. BN_rand(&b, 400 + i - num1, 0, 0);
  307. a.neg = rand_neg();
  308. b.neg = rand_neg();
  309. }
  310. BN_sub(&c, &a, &b);
  311. if (bp != NULL) {
  312. if (!results) {
  313. BN_print(bp, &a);
  314. BIO_puts(bp, " - ");
  315. BN_print(bp, &b);
  316. BIO_puts(bp, " - ");
  317. }
  318. BN_print(bp, &c);
  319. BIO_puts(bp, "\n");
  320. }
  321. BN_add(&c, &c, &b);
  322. BN_sub(&c, &c, &a);
  323. if (!BN_is_zero(&c)) {
  324. fprintf(stderr, "Subtract test failed!\n");
  325. return 0;
  326. }
  327. }
  328. BN_free(&a);
  329. BN_free(&b);
  330. BN_free(&c);
  331. return (1);
  332. }
  333. int test_div(BIO *bp, BN_CTX *ctx) {
  334. BIGNUM a, b, c, d, e;
  335. int i;
  336. BN_init(&a);
  337. BN_init(&b);
  338. BN_init(&c);
  339. BN_init(&d);
  340. BN_init(&e);
  341. for (i = 0; i < num0 + num1; i++) {
  342. if (i < num1) {
  343. BN_rand(&a, 400, 0, 0);
  344. BN_copy(&b, &a);
  345. BN_lshift(&a, &a, i);
  346. BN_add_word(&a, i);
  347. } else
  348. BN_rand(&b, 50 + 3 * (i - num1), 0, 0);
  349. a.neg = rand_neg();
  350. b.neg = rand_neg();
  351. BN_div(&d, &c, &a, &b, ctx);
  352. if (bp != NULL) {
  353. if (!results) {
  354. BN_print(bp, &a);
  355. BIO_puts(bp, " / ");
  356. BN_print(bp, &b);
  357. BIO_puts(bp, " - ");
  358. }
  359. BN_print(bp, &d);
  360. BIO_puts(bp, "\n");
  361. if (!results) {
  362. BN_print(bp, &a);
  363. BIO_puts(bp, " % ");
  364. BN_print(bp, &b);
  365. BIO_puts(bp, " - ");
  366. }
  367. BN_print(bp, &c);
  368. BIO_puts(bp, "\n");
  369. }
  370. BN_mul(&e, &d, &b, ctx);
  371. BN_add(&d, &e, &c);
  372. BN_sub(&d, &d, &a);
  373. if (!BN_is_zero(&d)) {
  374. fprintf(stderr, "Division test failed!\n");
  375. return 0;
  376. }
  377. }
  378. BN_free(&a);
  379. BN_free(&b);
  380. BN_free(&c);
  381. BN_free(&d);
  382. BN_free(&e);
  383. return (1);
  384. }
  385. int test_lshift1(BIO *bp) {
  386. BIGNUM *a, *b, *c;
  387. int i;
  388. a = BN_new();
  389. b = BN_new();
  390. c = BN_new();
  391. BN_rand(a, 200, 0, 0); /**/
  392. a->neg = rand_neg();
  393. for (i = 0; i < num0; i++) {
  394. BN_lshift1(b, a);
  395. if (bp != NULL) {
  396. if (!results) {
  397. BN_print(bp, a);
  398. BIO_puts(bp, " * 2");
  399. BIO_puts(bp, " - ");
  400. }
  401. BN_print(bp, b);
  402. BIO_puts(bp, "\n");
  403. }
  404. BN_add(c, a, a);
  405. BN_sub(a, b, c);
  406. if (!BN_is_zero(a)) {
  407. fprintf(stderr, "Left shift one test failed!\n");
  408. return 0;
  409. }
  410. BN_copy(a, b);
  411. }
  412. BN_free(a);
  413. BN_free(b);
  414. BN_free(c);
  415. return (1);
  416. }
  417. int test_rshift(BIO *bp, BN_CTX *ctx) {
  418. BIGNUM *a, *b, *c, *d, *e;
  419. int i;
  420. a = BN_new();
  421. b = BN_new();
  422. c = BN_new();
  423. d = BN_new();
  424. e = BN_new();
  425. BN_one(c);
  426. BN_rand(a, 200, 0, 0); /**/
  427. a->neg = rand_neg();
  428. for (i = 0; i < num0; i++) {
  429. BN_rshift(b, a, i + 1);
  430. BN_add(c, c, c);
  431. if (bp != NULL) {
  432. if (!results) {
  433. BN_print(bp, a);
  434. BIO_puts(bp, " / ");
  435. BN_print(bp, c);
  436. BIO_puts(bp, " - ");
  437. }
  438. BN_print(bp, b);
  439. BIO_puts(bp, "\n");
  440. }
  441. BN_div(d, e, a, c, ctx);
  442. BN_sub(d, d, b);
  443. if (!BN_is_zero(d)) {
  444. fprintf(stderr, "Right shift test failed!\n");
  445. return 0;
  446. }
  447. }
  448. BN_free(a);
  449. BN_free(b);
  450. BN_free(c);
  451. BN_free(d);
  452. BN_free(e);
  453. return (1);
  454. }
  455. int test_rshift1(BIO *bp) {
  456. BIGNUM *a, *b, *c;
  457. int i;
  458. a = BN_new();
  459. b = BN_new();
  460. c = BN_new();
  461. BN_rand(a, 200, 0, 0); /**/
  462. a->neg = rand_neg();
  463. for (i = 0; i < num0; i++) {
  464. BN_rshift1(b, a);
  465. if (bp != NULL) {
  466. if (!results) {
  467. BN_print(bp, a);
  468. BIO_puts(bp, " / 2");
  469. BIO_puts(bp, " - ");
  470. }
  471. BN_print(bp, b);
  472. BIO_puts(bp, "\n");
  473. }
  474. BN_sub(c, a, b);
  475. BN_sub(c, c, b);
  476. if (!BN_is_zero(c) && !BN_abs_is_word(c, 1)) {
  477. fprintf(stderr, "Right shift one test failed!\n");
  478. return 0;
  479. }
  480. BN_copy(a, b);
  481. }
  482. BN_free(a);
  483. BN_free(b);
  484. BN_free(c);
  485. return (1);
  486. }
  487. int test_lshift(BIO *bp, BN_CTX *ctx, BIGNUM *a_) {
  488. BIGNUM *a, *b, *c, *d;
  489. int i;
  490. b = BN_new();
  491. c = BN_new();
  492. d = BN_new();
  493. BN_one(c);
  494. if (a_)
  495. a = a_;
  496. else {
  497. a = BN_new();
  498. BN_rand(a, 200, 0, 0); /**/
  499. a->neg = rand_neg();
  500. }
  501. for (i = 0; i < num0; i++) {
  502. BN_lshift(b, a, i + 1);
  503. BN_add(c, c, c);
  504. if (bp != NULL) {
  505. if (!results) {
  506. BN_print(bp, a);
  507. BIO_puts(bp, " * ");
  508. BN_print(bp, c);
  509. BIO_puts(bp, " - ");
  510. }
  511. BN_print(bp, b);
  512. BIO_puts(bp, "\n");
  513. }
  514. BN_mul(d, a, c, ctx);
  515. BN_sub(d, d, b);
  516. if (!BN_is_zero(d)) {
  517. fprintf(stderr, "Left shift test failed!\n");
  518. fprintf(stderr, "a=");
  519. BN_print_fp(stderr, a);
  520. fprintf(stderr, "\nb=");
  521. BN_print_fp(stderr, b);
  522. fprintf(stderr, "\nc=");
  523. BN_print_fp(stderr, c);
  524. fprintf(stderr, "\nd=");
  525. BN_print_fp(stderr, d);
  526. fprintf(stderr, "\n");
  527. return 0;
  528. }
  529. }
  530. BN_free(a);
  531. BN_free(b);
  532. BN_free(c);
  533. BN_free(d);
  534. return (1);
  535. }
  536. int test_mul(BIO *bp) {
  537. BIGNUM a, b, c, d, e;
  538. int i;
  539. BN_CTX *ctx;
  540. ctx = BN_CTX_new();
  541. if (ctx == NULL)
  542. abort();
  543. BN_init(&a);
  544. BN_init(&b);
  545. BN_init(&c);
  546. BN_init(&d);
  547. BN_init(&e);
  548. for (i = 0; i < num0 + num1; i++) {
  549. if (i <= num1) {
  550. BN_rand(&a, 100, 0, 0);
  551. BN_rand(&b, 100, 0, 0);
  552. } else
  553. BN_rand(&b, i - num1, 0, 0);
  554. a.neg = rand_neg();
  555. b.neg = rand_neg();
  556. BN_mul(&c, &a, &b, ctx);
  557. if (bp != NULL) {
  558. if (!results) {
  559. BN_print(bp, &a);
  560. BIO_puts(bp, " * ");
  561. BN_print(bp, &b);
  562. BIO_puts(bp, " - ");
  563. }
  564. BN_print(bp, &c);
  565. BIO_puts(bp, "\n");
  566. }
  567. BN_div(&d, &e, &c, &a, ctx);
  568. BN_sub(&d, &d, &b);
  569. if (!BN_is_zero(&d) || !BN_is_zero(&e)) {
  570. fprintf(stderr, "Multiplication test failed!\n");
  571. return 0;
  572. }
  573. }
  574. BN_free(&a);
  575. BN_free(&b);
  576. BN_free(&c);
  577. BN_free(&d);
  578. BN_free(&e);
  579. BN_CTX_free(ctx);
  580. return (1);
  581. }
  582. int test_sqr(BIO *bp, BN_CTX *ctx) {
  583. BIGNUM a, c, d, e;
  584. int i;
  585. BN_init(&a);
  586. BN_init(&c);
  587. BN_init(&d);
  588. BN_init(&e);
  589. for (i = 0; i < num0; i++) {
  590. BN_rand(&a, 40 + i * 10, 0, 0);
  591. a.neg = rand_neg();
  592. BN_sqr(&c, &a, ctx);
  593. if (bp != NULL) {
  594. if (!results) {
  595. BN_print(bp, &a);
  596. BIO_puts(bp, " * ");
  597. BN_print(bp, &a);
  598. BIO_puts(bp, " - ");
  599. }
  600. BN_print(bp, &c);
  601. BIO_puts(bp, "\n");
  602. }
  603. BN_div(&d, &e, &c, &a, ctx);
  604. BN_sub(&d, &d, &a);
  605. if (!BN_is_zero(&d) || !BN_is_zero(&e)) {
  606. fprintf(stderr, "Square test failed!\n");
  607. return 0;
  608. }
  609. }
  610. BN_free(&a);
  611. BN_free(&c);
  612. BN_free(&d);
  613. BN_free(&e);
  614. return (1);
  615. }
  616. int rand_neg(void) {
  617. static unsigned int neg = 0;
  618. static int sign[8] = {0, 0, 0, 1, 1, 0, 1, 1};
  619. return (sign[(neg++) % 8]);
  620. }
  621. static void print_word(BIO *bp, BN_ULONG w) {
  622. #ifdef OPENSSL_64_BIT
  623. if (sizeof(w) > sizeof(unsigned long)) {
  624. unsigned long h = (unsigned long)(w >> 32), l = (unsigned long)(w);
  625. if (h)
  626. BIO_printf(bp, "%lX%08lX", h, l);
  627. else
  628. BIO_printf(bp, "%lX", l);
  629. return;
  630. }
  631. #endif
  632. BIO_printf(bp, BN_HEX_FMT1, w);
  633. }
  634. int test_div_word(BIO *bp) {
  635. BIGNUM a, b;
  636. BN_ULONG r, s;
  637. int i;
  638. BN_init(&a);
  639. BN_init(&b);
  640. for (i = 0; i < num0; i++) {
  641. do {
  642. BN_rand(&a, 512, -1, 0);
  643. BN_rand(&b, BN_BITS2, -1, 0);
  644. s = b.d[0];
  645. } while (!s);
  646. BN_copy(&b, &a);
  647. r = BN_div_word(&b, s);
  648. if (bp != NULL) {
  649. if (!results) {
  650. BN_print(bp, &a);
  651. BIO_puts(bp, " / ");
  652. print_word(bp, s);
  653. BIO_puts(bp, " - ");
  654. }
  655. BN_print(bp, &b);
  656. BIO_puts(bp, "\n");
  657. if (!results) {
  658. BN_print(bp, &a);
  659. BIO_puts(bp, " % ");
  660. print_word(bp, s);
  661. BIO_puts(bp, " - ");
  662. }
  663. print_word(bp, r);
  664. BIO_puts(bp, "\n");
  665. }
  666. BN_mul_word(&b, s);
  667. BN_add_word(&b, r);
  668. BN_sub(&b, &a, &b);
  669. if (!BN_is_zero(&b)) {
  670. fprintf(stderr, "Division (word) test failed!\n");
  671. return 0;
  672. }
  673. }
  674. BN_free(&a);
  675. BN_free(&b);
  676. return (1);
  677. }
  678. int test_mont(BIO *bp, BN_CTX *ctx) {
  679. BIGNUM a, b, c, d, A, B;
  680. BIGNUM n;
  681. int i;
  682. BN_MONT_CTX *mont;
  683. BN_init(&a);
  684. BN_init(&b);
  685. BN_init(&c);
  686. BN_init(&d);
  687. BN_init(&A);
  688. BN_init(&B);
  689. BN_init(&n);
  690. mont = BN_MONT_CTX_new();
  691. if (mont == NULL)
  692. return 0;
  693. BN_rand(&a, 100, 0, 0); /**/
  694. BN_rand(&b, 100, 0, 0); /**/
  695. for (i = 0; i < num2; i++) {
  696. int bits = (200 * (i + 1)) / num2;
  697. if (bits == 0)
  698. continue;
  699. BN_rand(&n, bits, 0, 1);
  700. BN_MONT_CTX_set(mont, &n, ctx);
  701. BN_nnmod(&a, &a, &n, ctx);
  702. BN_nnmod(&b, &b, &n, ctx);
  703. BN_to_montgomery(&A, &a, mont, ctx);
  704. BN_to_montgomery(&B, &b, mont, ctx);
  705. BN_mod_mul_montgomery(&c, &A, &B, mont, ctx); /**/
  706. BN_from_montgomery(&A, &c, mont, ctx); /**/
  707. if (bp != NULL) {
  708. if (!results) {
  709. #ifdef undef
  710. fprintf(stderr, "%d * %d %% %d\n", BN_num_bits(&a), BN_num_bits(&b),
  711. BN_num_bits(mont->N));
  712. #endif
  713. BN_print(bp, &a);
  714. BIO_puts(bp, " * ");
  715. BN_print(bp, &b);
  716. BIO_puts(bp, " % ");
  717. BN_print(bp, &(mont->N));
  718. BIO_puts(bp, " - ");
  719. }
  720. BN_print(bp, &A);
  721. BIO_puts(bp, "\n");
  722. }
  723. BN_mod_mul(&d, &a, &b, &n, ctx);
  724. BN_sub(&d, &d, &A);
  725. if (!BN_is_zero(&d)) {
  726. fprintf(stderr, "Montgomery multiplication test failed!\n");
  727. return 0;
  728. }
  729. }
  730. BN_MONT_CTX_free(mont);
  731. BN_free(&a);
  732. BN_free(&b);
  733. BN_free(&c);
  734. BN_free(&d);
  735. BN_free(&A);
  736. BN_free(&B);
  737. BN_free(&n);
  738. return (1);
  739. }
  740. int test_mod(BIO *bp, BN_CTX *ctx) {
  741. BIGNUM *a, *b, *c, *d, *e;
  742. int i;
  743. a = BN_new();
  744. b = BN_new();
  745. c = BN_new();
  746. d = BN_new();
  747. e = BN_new();
  748. BN_rand(a, 1024, 0, 0); /**/
  749. for (i = 0; i < num0; i++) {
  750. BN_rand(b, 450 + i * 10, 0, 0); /**/
  751. a->neg = rand_neg();
  752. b->neg = rand_neg();
  753. BN_mod(c, a, b, ctx); /**/
  754. if (bp != NULL) {
  755. if (!results) {
  756. BN_print(bp, a);
  757. BIO_puts(bp, " % ");
  758. BN_print(bp, b);
  759. BIO_puts(bp, " - ");
  760. }
  761. BN_print(bp, c);
  762. BIO_puts(bp, "\n");
  763. }
  764. BN_div(d, e, a, b, ctx);
  765. BN_sub(e, e, c);
  766. if (!BN_is_zero(e)) {
  767. fprintf(stderr, "Modulo test failed!\n");
  768. return 0;
  769. }
  770. }
  771. BN_free(a);
  772. BN_free(b);
  773. BN_free(c);
  774. BN_free(d);
  775. BN_free(e);
  776. return (1);
  777. }
  778. int test_mod_mul(BIO *bp, BN_CTX *ctx) {
  779. BIGNUM *a, *b, *c, *d, *e;
  780. int i, j;
  781. a = BN_new();
  782. b = BN_new();
  783. c = BN_new();
  784. d = BN_new();
  785. e = BN_new();
  786. for (j = 0; j < 3; j++) {
  787. BN_rand(c, 1024, 0, 0); /**/
  788. for (i = 0; i < num0; i++) {
  789. BN_rand(a, 475 + i * 10, 0, 0); /**/
  790. BN_rand(b, 425 + i * 11, 0, 0); /**/
  791. a->neg = rand_neg();
  792. b->neg = rand_neg();
  793. if (!BN_mod_mul(e, a, b, c, ctx)) {
  794. unsigned long l;
  795. while ((l = ERR_get_error()))
  796. fprintf(stderr, "ERROR:%s\n", ERR_error_string(l, NULL));
  797. abort();
  798. }
  799. if (bp != NULL) {
  800. if (!results) {
  801. BN_print(bp, a);
  802. BIO_puts(bp, " * ");
  803. BN_print(bp, b);
  804. BIO_puts(bp, " % ");
  805. BN_print(bp, c);
  806. if ((a->neg ^ b->neg) && !BN_is_zero(e)) {
  807. /* If (a*b) % c is negative, c must be added
  808. * in order to obtain the normalized remainder
  809. * (new with OpenSSL 0.9.7, previous versions of
  810. * BN_mod_mul could generate negative results)
  811. */
  812. BIO_puts(bp, " + ");
  813. BN_print(bp, c);
  814. }
  815. BIO_puts(bp, " - ");
  816. }
  817. BN_print(bp, e);
  818. BIO_puts(bp, "\n");
  819. }
  820. BN_mul(d, a, b, ctx);
  821. BN_sub(d, d, e);
  822. BN_div(a, b, d, c, ctx);
  823. if (!BN_is_zero(b)) {
  824. fprintf(stderr, "Modulo multiply test failed!\n");
  825. ERR_print_errors_fp(stderr);
  826. return 0;
  827. }
  828. }
  829. }
  830. BN_free(a);
  831. BN_free(b);
  832. BN_free(c);
  833. BN_free(d);
  834. BN_free(e);
  835. return (1);
  836. }
  837. int test_mod_exp(BIO *bp, BN_CTX *ctx) {
  838. BIGNUM *a, *b, *c, *d, *e;
  839. int i;
  840. a = BN_new();
  841. b = BN_new();
  842. c = BN_new();
  843. d = BN_new();
  844. e = BN_new();
  845. BN_rand(c, 30, 0, 1); /* must be odd for montgomery */
  846. for (i = 0; i < num2; i++) {
  847. BN_rand(a, 20 + i * 5, 0, 0); /**/
  848. BN_rand(b, 2 + i, 0, 0); /**/
  849. if (!BN_mod_exp(d, a, b, c, ctx))
  850. return (0);
  851. if (bp != NULL) {
  852. if (!results) {
  853. BN_print(bp, a);
  854. BIO_puts(bp, " ^ ");
  855. BN_print(bp, b);
  856. BIO_puts(bp, " % ");
  857. BN_print(bp, c);
  858. BIO_puts(bp, " - ");
  859. }
  860. BN_print(bp, d);
  861. BIO_puts(bp, "\n");
  862. }
  863. BN_exp(e, a, b, ctx);
  864. BN_sub(e, e, d);
  865. BN_div(a, b, e, c, ctx);
  866. if (!BN_is_zero(b)) {
  867. fprintf(stderr, "Modulo exponentiation test failed!\n");
  868. return 0;
  869. }
  870. }
  871. BN_free(a);
  872. BN_free(b);
  873. BN_free(c);
  874. BN_free(d);
  875. BN_free(e);
  876. return (1);
  877. }
  878. int test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx) {
  879. BIGNUM *a, *b, *c, *d, *e;
  880. int i;
  881. a = BN_new();
  882. b = BN_new();
  883. c = BN_new();
  884. d = BN_new();
  885. e = BN_new();
  886. BN_rand(c, 30, 0, 1); /* must be odd for montgomery */
  887. for (i = 0; i < num2; i++) {
  888. BN_rand(a, 20 + i * 5, 0, 0); /**/
  889. BN_rand(b, 2 + i, 0, 0); /**/
  890. if (!BN_mod_exp_mont_consttime(d, a, b, c, ctx, NULL))
  891. return (00);
  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. BN_free(a);
  913. BN_free(b);
  914. BN_free(c);
  915. BN_free(d);
  916. BN_free(e);
  917. return (1);
  918. }
  919. /* Test constant-time modular exponentiation with 1024-bit inputs,
  920. * which on x86_64 cause a different code branch to be taken. */
  921. int test_mod_exp_mont5(BIO *bp, BN_CTX *ctx) {
  922. BIGNUM *a, *p, *m, *d, *e;
  923. BN_MONT_CTX *mont;
  924. a = BN_new();
  925. p = BN_new();
  926. m = BN_new();
  927. d = BN_new();
  928. e = BN_new();
  929. mont = BN_MONT_CTX_new();
  930. BN_rand(m, 1024, 0, 1); /* must be odd for montgomery */
  931. /* Zero exponent */
  932. BN_rand(a, 1024, 0, 0);
  933. BN_zero(p);
  934. if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
  935. return 0;
  936. if (!BN_is_one(d)) {
  937. fprintf(stderr, "Modular exponentiation test failed!\n");
  938. return 0;
  939. }
  940. /* Zero input */
  941. BN_rand(p, 1024, 0, 0);
  942. BN_zero(a);
  943. if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
  944. return 0;
  945. if (!BN_is_zero(d)) {
  946. fprintf(stderr, "Modular exponentiation test failed!\n");
  947. return 0;
  948. }
  949. /* Craft an input whose Montgomery representation is 1,
  950. * i.e., shorter than the modulus m, in order to test
  951. * the const time precomputation scattering/gathering.
  952. */
  953. BN_one(a);
  954. BN_MONT_CTX_set(mont, m, ctx);
  955. if (!BN_from_montgomery(e, a, mont, ctx) ||
  956. !BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL) ||
  957. !BN_mod_exp(a, e, p, m, ctx)) {
  958. return 0;
  959. }
  960. if (BN_cmp(a, d) != 0) {
  961. fprintf(stderr, "Modular exponentiation test failed!\n");
  962. return 0;
  963. }
  964. /* Finally, some regular test vectors. */
  965. BN_rand(e, 1024, 0, 0);
  966. if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
  967. return 0;
  968. if (!BN_mod_exp(a, e, p, m, ctx))
  969. return 0;
  970. if (BN_cmp(a, d) != 0) {
  971. fprintf(stderr, "Modular exponentiation test failed!\n");
  972. return 0;
  973. }
  974. BN_MONT_CTX_free(mont);
  975. BN_free(a);
  976. BN_free(p);
  977. BN_free(m);
  978. BN_free(d);
  979. BN_free(e);
  980. return (1);
  981. }
  982. int test_exp(BIO *bp, BN_CTX *ctx) {
  983. BIGNUM *a, *b, *d, *e, *one;
  984. int i;
  985. a = BN_new();
  986. b = BN_new();
  987. d = BN_new();
  988. e = BN_new();
  989. one = BN_new();
  990. BN_one(one);
  991. for (i = 0; i < num2; i++) {
  992. BN_rand(a, 20 + i * 5, 0, 0); /**/
  993. BN_rand(b, 2 + i, 0, 0); /**/
  994. if (BN_exp(d, a, b, ctx) <= 0)
  995. return (0);
  996. if (bp != NULL) {
  997. if (!results) {
  998. BN_print(bp, a);
  999. BIO_puts(bp, " ^ ");
  1000. BN_print(bp, b);
  1001. BIO_puts(bp, " - ");
  1002. }
  1003. BN_print(bp, d);
  1004. BIO_puts(bp, "\n");
  1005. }
  1006. BN_one(e);
  1007. for (; !BN_is_zero(b); BN_sub(b, b, one))
  1008. BN_mul(e, e, a, ctx);
  1009. BN_sub(e, e, d);
  1010. if (!BN_is_zero(e)) {
  1011. fprintf(stderr, "Exponentiation test failed!\n");
  1012. return 0;
  1013. }
  1014. }
  1015. BN_free(a);
  1016. BN_free(b);
  1017. BN_free(d);
  1018. BN_free(e);
  1019. BN_free(one);
  1020. return (1);
  1021. }
  1022. /* test_exp_mod_zero tests that x**0 mod 1 == 0. */
  1023. static int test_exp_mod_zero() {
  1024. BIGNUM a, p, m;
  1025. BIGNUM r;
  1026. BN_CTX *ctx = BN_CTX_new();
  1027. int ret = 0;
  1028. BN_init(&m);
  1029. BN_one(&m);
  1030. BN_init(&a);
  1031. BN_one(&a);
  1032. BN_init(&p);
  1033. BN_zero(&p);
  1034. BN_init(&r);
  1035. BN_mod_exp(&r, &a, &p, &m, ctx);
  1036. BN_CTX_free(ctx);
  1037. if (BN_is_zero(&r)) {
  1038. ret = 1;
  1039. } else {
  1040. printf("1**0 mod 1 = ");
  1041. BN_print_fp(stdout, &r);
  1042. printf(", should be 0\n");
  1043. }
  1044. BN_free(&r);
  1045. BN_free(&a);
  1046. BN_free(&p);
  1047. BN_free(&m);
  1048. return ret;
  1049. }
  1050. static int genprime_cb(int p, int n, BN_GENCB *arg) {
  1051. char c = '*';
  1052. if (p == 0)
  1053. c = '.';
  1054. if (p == 1)
  1055. c = '+';
  1056. if (p == 2)
  1057. c = '*';
  1058. if (p == 3)
  1059. c = '\n';
  1060. putc(c, stdout);
  1061. fflush(stdout);
  1062. return 1;
  1063. }
  1064. int test_mod_sqrt(BIO *bp, BN_CTX *ctx) {
  1065. BN_GENCB cb;
  1066. BIGNUM *a, *p, *r;
  1067. int i, j;
  1068. int ret = 0;
  1069. a = BN_new();
  1070. p = BN_new();
  1071. r = BN_new();
  1072. if (a == NULL || p == NULL || r == NULL)
  1073. goto err;
  1074. BN_GENCB_set(&cb, genprime_cb, NULL);
  1075. for (i = 0; i < 16; i++) {
  1076. if (i < 8) {
  1077. unsigned primes[8] = {2, 3, 5, 7, 11, 13, 17, 19};
  1078. if (!BN_set_word(p, primes[i]))
  1079. goto err;
  1080. } else {
  1081. if (!BN_set_word(a, 32))
  1082. goto err;
  1083. if (!BN_set_word(r, 2 * i + 1))
  1084. goto err;
  1085. if (!BN_generate_prime_ex(p, 256, 0, a, r, &cb))
  1086. goto err;
  1087. putc('\n', stdout);
  1088. }
  1089. p->neg = rand_neg();
  1090. for (j = 0; j < num2; j++) {
  1091. /* construct 'a' such that it is a square modulo p,
  1092. * but in general not a proper square and not reduced modulo p */
  1093. if (!BN_rand(r, 256, 0, 3))
  1094. goto err;
  1095. if (!BN_nnmod(r, r, p, ctx))
  1096. goto err;
  1097. if (!BN_mod_sqr(r, r, p, ctx))
  1098. goto err;
  1099. if (!BN_rand(a, 256, 0, 3))
  1100. goto err;
  1101. if (!BN_nnmod(a, a, p, ctx))
  1102. goto err;
  1103. if (!BN_mod_sqr(a, a, p, ctx))
  1104. goto err;
  1105. if (!BN_mul(a, a, r, ctx))
  1106. goto err;
  1107. if (rand_neg())
  1108. if (!BN_sub(a, a, p))
  1109. goto err;
  1110. if (!BN_mod_sqrt(r, a, p, ctx))
  1111. goto err;
  1112. if (!BN_mod_sqr(r, r, p, ctx))
  1113. goto err;
  1114. if (!BN_nnmod(a, a, p, ctx))
  1115. goto err;
  1116. if (BN_cmp(a, r) != 0) {
  1117. fprintf(stderr, "BN_mod_sqrt failed: a = ");
  1118. BN_print_fp(stderr, a);
  1119. fprintf(stderr, ", r = ");
  1120. BN_print_fp(stderr, r);
  1121. fprintf(stderr, ", p = ");
  1122. BN_print_fp(stderr, p);
  1123. fprintf(stderr, "\n");
  1124. goto err;
  1125. }
  1126. putc('.', stdout);
  1127. fflush(stdout);
  1128. }
  1129. putc('\n', stdout);
  1130. fflush(stderr);
  1131. }
  1132. ret = 1;
  1133. err:
  1134. if (a != NULL)
  1135. BN_free(a);
  1136. if (p != NULL)
  1137. BN_free(p);
  1138. if (r != NULL)
  1139. BN_free(r);
  1140. return ret;
  1141. }
  1142. int test_small_prime(BIO *bp, BN_CTX *ctx) {
  1143. static const int bits = 10;
  1144. int ret = 0;
  1145. BIGNUM r;
  1146. BN_init(&r);
  1147. if (!BN_generate_prime_ex(&r, bits, 0, NULL, NULL, NULL)) {
  1148. goto err;
  1149. }
  1150. if (BN_num_bits(&r) != bits) {
  1151. BIO_printf(bp, "Expected %d bit prime, got %d bit number\n", bits,
  1152. BN_num_bits(&r));
  1153. goto err;
  1154. }
  1155. ret = 1;
  1156. err:
  1157. BN_free(&r);
  1158. return ret;
  1159. }
  1160. int test_sqrt(BIO *bp, BN_CTX *ctx) {
  1161. BIGNUM *n = BN_new(), *nn = BN_new(), *sqrt = BN_new();
  1162. unsigned i;
  1163. /* Test some random squares. */
  1164. for (i = 0; i < 100; i++) {
  1165. if (!BN_rand(n, 1024 /* bit length */, -1 /* no modification of top bits */,
  1166. 0 /* don't modify bottom bit */) ||
  1167. !BN_mul(nn, n, n, ctx) ||
  1168. !BN_sqrt(sqrt, nn, ctx)) {
  1169. BIO_print_errors_fp(stderr);
  1170. return 0;
  1171. }
  1172. if (BN_cmp(n, sqrt) != 0) {
  1173. fprintf(stderr, "Bad result from BN_sqrt.\n");
  1174. return 0;
  1175. }
  1176. }
  1177. /* Test some non-squares */
  1178. for (i = 0; i < 100; i++) {
  1179. if (!BN_rand(n, 1024 /* bit length */, -1 /* no modification of top bits */,
  1180. 0 /* don't modify bottom bit */) ||
  1181. !BN_mul(nn, n, n, ctx) ||
  1182. !BN_add(nn, nn, BN_value_one())) {
  1183. BIO_print_errors_fp(stderr);
  1184. return 0;
  1185. }
  1186. if (BN_sqrt(sqrt, nn, ctx)) {
  1187. char *nn_str = BN_bn2dec(nn);
  1188. fprintf(stderr, "BIO_sqrt didn't fail on a non-square: %s\n", nn_str);
  1189. OPENSSL_free(nn_str);
  1190. }
  1191. }
  1192. BN_free(n);
  1193. BN_free(sqrt);
  1194. BN_free(nn);
  1195. return 1;
  1196. }
  1197. int test_bn2bin_padded(BIO *bp, BN_CTX *ctx) {
  1198. BIGNUM *n = BN_new();
  1199. uint8_t zeros[256], out[256], reference[128];
  1200. size_t bytes;
  1201. memset(zeros, 0, sizeof(zeros));
  1202. /* Test edge case at 0. */
  1203. if (!BN_bn2bin_padded(NULL, 0, n)) {
  1204. fprintf(stderr,
  1205. "BN_bn2bin_padded failed to encode 0 in an empty buffer.\n");
  1206. return 0;
  1207. }
  1208. memset(out, -1, sizeof(out));
  1209. if (!BN_bn2bin_padded(out, sizeof(out), n)) {
  1210. fprintf(stderr,
  1211. "BN_bn2bin_padded failed to encode 0 in a non-empty buffer.\n");
  1212. return 0;
  1213. }
  1214. if (memcmp(zeros, out, sizeof(out))) {
  1215. fprintf(stderr, "BN_bn2bin_padded did not zero buffer.\n");
  1216. return 0;
  1217. }
  1218. /* Test a random numbers at various byte lengths. */
  1219. for (bytes = 128 - 7; bytes <= 128; bytes++) {
  1220. if (!BN_rand(n, bytes * 8, 0 /* make sure top bit is 1 */,
  1221. 0 /* don't modify bottom bit */)) {
  1222. BIO_print_errors_fp(stderr);
  1223. return 0;
  1224. }
  1225. if (BN_num_bytes(n) != bytes || BN_bn2bin(n, reference) != bytes) {
  1226. fprintf(stderr, "Bad result from BN_rand; bytes.\n");
  1227. return 0;
  1228. }
  1229. /* Empty buffer should fail. */
  1230. if (BN_bn2bin_padded(NULL, 0, n)) {
  1231. fprintf(stderr,
  1232. "BN_bn2bin_padded incorrectly succeeded on empty buffer.\n");
  1233. return 0;
  1234. }
  1235. /* One byte short should fail. */
  1236. if (BN_bn2bin_padded(out, bytes - 1, n)) {
  1237. fprintf(stderr, "BN_bn2bin_padded incorrectly succeeded on short.\n");
  1238. return 0;
  1239. }
  1240. /* Exactly right size should encode. */
  1241. if (!BN_bn2bin_padded(out, bytes, n) ||
  1242. memcmp(out, reference, bytes) != 0) {
  1243. fprintf(stderr, "BN_bn2bin_padded gave a bad result.\n");
  1244. return 0;
  1245. }
  1246. /* Pad up one byte extra. */
  1247. if (!BN_bn2bin_padded(out, bytes + 1, n) ||
  1248. memcmp(out + 1, reference, bytes) || memcmp(out, zeros, 1)) {
  1249. fprintf(stderr, "BN_bn2bin_padded gave a bad result.\n");
  1250. return 0;
  1251. }
  1252. /* Pad up to 256. */
  1253. if (!BN_bn2bin_padded(out, sizeof(out), n) ||
  1254. memcmp(out + sizeof(out) - bytes, reference, bytes) ||
  1255. memcmp(out, zeros, sizeof(out) - bytes)) {
  1256. fprintf(stderr, "BN_bn2bin_padded gave a bad result.\n");
  1257. return 0;
  1258. }
  1259. }
  1260. BN_free(n);
  1261. return 1;
  1262. }