Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

1409 строки
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. BIO_printf(bp, BN_HEX_FMT1, w);
  623. }
  624. int test_div_word(BIO *bp) {
  625. BIGNUM a, b;
  626. BN_ULONG r, s;
  627. int i;
  628. BN_init(&a);
  629. BN_init(&b);
  630. for (i = 0; i < num0; i++) {
  631. do {
  632. BN_rand(&a, 512, -1, 0);
  633. BN_rand(&b, BN_BITS2, -1, 0);
  634. s = b.d[0];
  635. } while (!s);
  636. BN_copy(&b, &a);
  637. r = BN_div_word(&b, s);
  638. if (bp != NULL) {
  639. if (!results) {
  640. BN_print(bp, &a);
  641. BIO_puts(bp, " / ");
  642. print_word(bp, s);
  643. BIO_puts(bp, " - ");
  644. }
  645. BN_print(bp, &b);
  646. BIO_puts(bp, "\n");
  647. if (!results) {
  648. BN_print(bp, &a);
  649. BIO_puts(bp, " % ");
  650. print_word(bp, s);
  651. BIO_puts(bp, " - ");
  652. }
  653. print_word(bp, r);
  654. BIO_puts(bp, "\n");
  655. }
  656. BN_mul_word(&b, s);
  657. BN_add_word(&b, r);
  658. BN_sub(&b, &a, &b);
  659. if (!BN_is_zero(&b)) {
  660. fprintf(stderr, "Division (word) test failed!\n");
  661. return 0;
  662. }
  663. }
  664. BN_free(&a);
  665. BN_free(&b);
  666. return (1);
  667. }
  668. int test_mont(BIO *bp, BN_CTX *ctx) {
  669. BIGNUM a, b, c, d, A, B;
  670. BIGNUM n;
  671. int i;
  672. BN_MONT_CTX *mont;
  673. BN_init(&a);
  674. BN_init(&b);
  675. BN_init(&c);
  676. BN_init(&d);
  677. BN_init(&A);
  678. BN_init(&B);
  679. BN_init(&n);
  680. mont = BN_MONT_CTX_new();
  681. if (mont == NULL)
  682. return 0;
  683. BN_rand(&a, 100, 0, 0); /**/
  684. BN_rand(&b, 100, 0, 0); /**/
  685. for (i = 0; i < num2; i++) {
  686. int bits = (200 * (i + 1)) / num2;
  687. if (bits == 0)
  688. continue;
  689. BN_rand(&n, bits, 0, 1);
  690. BN_MONT_CTX_set(mont, &n, ctx);
  691. BN_nnmod(&a, &a, &n, ctx);
  692. BN_nnmod(&b, &b, &n, ctx);
  693. BN_to_montgomery(&A, &a, mont, ctx);
  694. BN_to_montgomery(&B, &b, mont, ctx);
  695. BN_mod_mul_montgomery(&c, &A, &B, mont, ctx); /**/
  696. BN_from_montgomery(&A, &c, mont, ctx); /**/
  697. if (bp != NULL) {
  698. if (!results) {
  699. #ifdef undef
  700. fprintf(stderr, "%d * %d %% %d\n", BN_num_bits(&a), BN_num_bits(&b),
  701. BN_num_bits(mont->N));
  702. #endif
  703. BN_print(bp, &a);
  704. BIO_puts(bp, " * ");
  705. BN_print(bp, &b);
  706. BIO_puts(bp, " % ");
  707. BN_print(bp, &(mont->N));
  708. BIO_puts(bp, " - ");
  709. }
  710. BN_print(bp, &A);
  711. BIO_puts(bp, "\n");
  712. }
  713. BN_mod_mul(&d, &a, &b, &n, ctx);
  714. BN_sub(&d, &d, &A);
  715. if (!BN_is_zero(&d)) {
  716. fprintf(stderr, "Montgomery multiplication test failed!\n");
  717. return 0;
  718. }
  719. }
  720. BN_MONT_CTX_free(mont);
  721. BN_free(&a);
  722. BN_free(&b);
  723. BN_free(&c);
  724. BN_free(&d);
  725. BN_free(&A);
  726. BN_free(&B);
  727. BN_free(&n);
  728. return (1);
  729. }
  730. int test_mod(BIO *bp, BN_CTX *ctx) {
  731. BIGNUM *a, *b, *c, *d, *e;
  732. int i;
  733. a = BN_new();
  734. b = BN_new();
  735. c = BN_new();
  736. d = BN_new();
  737. e = BN_new();
  738. BN_rand(a, 1024, 0, 0); /**/
  739. for (i = 0; i < num0; i++) {
  740. BN_rand(b, 450 + i * 10, 0, 0); /**/
  741. a->neg = rand_neg();
  742. b->neg = rand_neg();
  743. BN_mod(c, a, b, ctx); /**/
  744. if (bp != NULL) {
  745. if (!results) {
  746. BN_print(bp, a);
  747. BIO_puts(bp, " % ");
  748. BN_print(bp, b);
  749. BIO_puts(bp, " - ");
  750. }
  751. BN_print(bp, c);
  752. BIO_puts(bp, "\n");
  753. }
  754. BN_div(d, e, a, b, ctx);
  755. BN_sub(e, e, c);
  756. if (!BN_is_zero(e)) {
  757. fprintf(stderr, "Modulo test failed!\n");
  758. return 0;
  759. }
  760. }
  761. BN_free(a);
  762. BN_free(b);
  763. BN_free(c);
  764. BN_free(d);
  765. BN_free(e);
  766. return (1);
  767. }
  768. int test_mod_mul(BIO *bp, BN_CTX *ctx) {
  769. BIGNUM *a, *b, *c, *d, *e;
  770. int i, j;
  771. a = BN_new();
  772. b = BN_new();
  773. c = BN_new();
  774. d = BN_new();
  775. e = BN_new();
  776. for (j = 0; j < 3; j++) {
  777. BN_rand(c, 1024, 0, 0); /**/
  778. for (i = 0; i < num0; i++) {
  779. BN_rand(a, 475 + i * 10, 0, 0); /**/
  780. BN_rand(b, 425 + i * 11, 0, 0); /**/
  781. a->neg = rand_neg();
  782. b->neg = rand_neg();
  783. if (!BN_mod_mul(e, a, b, c, ctx)) {
  784. unsigned long l;
  785. while ((l = ERR_get_error()))
  786. fprintf(stderr, "ERROR:%s\n", ERR_error_string(l, NULL));
  787. abort();
  788. }
  789. if (bp != NULL) {
  790. if (!results) {
  791. BN_print(bp, a);
  792. BIO_puts(bp, " * ");
  793. BN_print(bp, b);
  794. BIO_puts(bp, " % ");
  795. BN_print(bp, c);
  796. if ((a->neg ^ b->neg) && !BN_is_zero(e)) {
  797. /* If (a*b) % c is negative, c must be added
  798. * in order to obtain the normalized remainder
  799. * (new with OpenSSL 0.9.7, previous versions of
  800. * BN_mod_mul could generate negative results)
  801. */
  802. BIO_puts(bp, " + ");
  803. BN_print(bp, c);
  804. }
  805. BIO_puts(bp, " - ");
  806. }
  807. BN_print(bp, e);
  808. BIO_puts(bp, "\n");
  809. }
  810. BN_mul(d, a, b, ctx);
  811. BN_sub(d, d, e);
  812. BN_div(a, b, d, c, ctx);
  813. if (!BN_is_zero(b)) {
  814. fprintf(stderr, "Modulo multiply test failed!\n");
  815. ERR_print_errors_fp(stderr);
  816. return 0;
  817. }
  818. }
  819. }
  820. BN_free(a);
  821. BN_free(b);
  822. BN_free(c);
  823. BN_free(d);
  824. BN_free(e);
  825. return (1);
  826. }
  827. int test_mod_exp(BIO *bp, BN_CTX *ctx) {
  828. BIGNUM *a, *b, *c, *d, *e;
  829. int i;
  830. a = BN_new();
  831. b = BN_new();
  832. c = BN_new();
  833. d = BN_new();
  834. e = BN_new();
  835. BN_rand(c, 30, 0, 1); /* must be odd for montgomery */
  836. for (i = 0; i < num2; i++) {
  837. BN_rand(a, 20 + i * 5, 0, 0); /**/
  838. BN_rand(b, 2 + i, 0, 0); /**/
  839. if (!BN_mod_exp(d, a, b, c, ctx))
  840. return (0);
  841. if (bp != NULL) {
  842. if (!results) {
  843. BN_print(bp, a);
  844. BIO_puts(bp, " ^ ");
  845. BN_print(bp, b);
  846. BIO_puts(bp, " % ");
  847. BN_print(bp, c);
  848. BIO_puts(bp, " - ");
  849. }
  850. BN_print(bp, d);
  851. BIO_puts(bp, "\n");
  852. }
  853. BN_exp(e, a, b, ctx);
  854. BN_sub(e, e, d);
  855. BN_div(a, b, e, c, ctx);
  856. if (!BN_is_zero(b)) {
  857. fprintf(stderr, "Modulo exponentiation test failed!\n");
  858. return 0;
  859. }
  860. }
  861. BN_free(a);
  862. BN_free(b);
  863. BN_free(c);
  864. BN_free(d);
  865. BN_free(e);
  866. return (1);
  867. }
  868. int test_mod_exp_mont_consttime(BIO *bp, BN_CTX *ctx) {
  869. BIGNUM *a, *b, *c, *d, *e;
  870. int i;
  871. a = BN_new();
  872. b = BN_new();
  873. c = BN_new();
  874. d = BN_new();
  875. e = BN_new();
  876. BN_rand(c, 30, 0, 1); /* must be odd for montgomery */
  877. for (i = 0; i < num2; i++) {
  878. BN_rand(a, 20 + i * 5, 0, 0); /**/
  879. BN_rand(b, 2 + i, 0, 0); /**/
  880. if (!BN_mod_exp_mont_consttime(d, a, b, c, ctx, NULL))
  881. return (00);
  882. if (bp != NULL) {
  883. if (!results) {
  884. BN_print(bp, a);
  885. BIO_puts(bp, " ^ ");
  886. BN_print(bp, b);
  887. BIO_puts(bp, " % ");
  888. BN_print(bp, c);
  889. BIO_puts(bp, " - ");
  890. }
  891. BN_print(bp, d);
  892. BIO_puts(bp, "\n");
  893. }
  894. BN_exp(e, a, b, ctx);
  895. BN_sub(e, e, d);
  896. BN_div(a, b, e, c, ctx);
  897. if (!BN_is_zero(b)) {
  898. fprintf(stderr, "Modulo exponentiation test failed!\n");
  899. return 0;
  900. }
  901. }
  902. BN_free(a);
  903. BN_free(b);
  904. BN_free(c);
  905. BN_free(d);
  906. BN_free(e);
  907. return (1);
  908. }
  909. /* Test constant-time modular exponentiation with 1024-bit inputs,
  910. * which on x86_64 cause a different code branch to be taken. */
  911. int test_mod_exp_mont5(BIO *bp, BN_CTX *ctx) {
  912. BIGNUM *a, *p, *m, *d, *e;
  913. BN_MONT_CTX *mont;
  914. a = BN_new();
  915. p = BN_new();
  916. m = BN_new();
  917. d = BN_new();
  918. e = BN_new();
  919. mont = BN_MONT_CTX_new();
  920. BN_rand(m, 1024, 0, 1); /* must be odd for montgomery */
  921. /* Zero exponent */
  922. BN_rand(a, 1024, 0, 0);
  923. BN_zero(p);
  924. if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
  925. return 0;
  926. if (!BN_is_one(d)) {
  927. fprintf(stderr, "Modular exponentiation test failed!\n");
  928. return 0;
  929. }
  930. /* Zero input */
  931. BN_rand(p, 1024, 0, 0);
  932. BN_zero(a);
  933. if (!BN_mod_exp_mont_consttime(d, a, p, m, ctx, NULL))
  934. return 0;
  935. if (!BN_is_zero(d)) {
  936. fprintf(stderr, "Modular exponentiation test failed!\n");
  937. return 0;
  938. }
  939. /* Craft an input whose Montgomery representation is 1,
  940. * i.e., shorter than the modulus m, in order to test
  941. * the const time precomputation scattering/gathering.
  942. */
  943. BN_one(a);
  944. BN_MONT_CTX_set(mont, m, ctx);
  945. if (!BN_from_montgomery(e, a, mont, ctx) ||
  946. !BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL) ||
  947. !BN_mod_exp(a, e, p, m, ctx)) {
  948. return 0;
  949. }
  950. if (BN_cmp(a, d) != 0) {
  951. fprintf(stderr, "Modular exponentiation test failed!\n");
  952. return 0;
  953. }
  954. /* Finally, some regular test vectors. */
  955. BN_rand(e, 1024, 0, 0);
  956. if (!BN_mod_exp_mont_consttime(d, e, p, m, ctx, NULL))
  957. return 0;
  958. if (!BN_mod_exp(a, e, p, m, ctx))
  959. return 0;
  960. if (BN_cmp(a, d) != 0) {
  961. fprintf(stderr, "Modular exponentiation test failed!\n");
  962. return 0;
  963. }
  964. BN_MONT_CTX_free(mont);
  965. BN_free(a);
  966. BN_free(p);
  967. BN_free(m);
  968. BN_free(d);
  969. BN_free(e);
  970. return (1);
  971. }
  972. int test_exp(BIO *bp, BN_CTX *ctx) {
  973. BIGNUM *a, *b, *d, *e, *one;
  974. int i;
  975. a = BN_new();
  976. b = BN_new();
  977. d = BN_new();
  978. e = BN_new();
  979. one = BN_new();
  980. BN_one(one);
  981. for (i = 0; i < num2; i++) {
  982. BN_rand(a, 20 + i * 5, 0, 0); /**/
  983. BN_rand(b, 2 + i, 0, 0); /**/
  984. if (BN_exp(d, a, b, ctx) <= 0)
  985. return (0);
  986. if (bp != NULL) {
  987. if (!results) {
  988. BN_print(bp, a);
  989. BIO_puts(bp, " ^ ");
  990. BN_print(bp, b);
  991. BIO_puts(bp, " - ");
  992. }
  993. BN_print(bp, d);
  994. BIO_puts(bp, "\n");
  995. }
  996. BN_one(e);
  997. for (; !BN_is_zero(b); BN_sub(b, b, one))
  998. BN_mul(e, e, a, ctx);
  999. BN_sub(e, e, d);
  1000. if (!BN_is_zero(e)) {
  1001. fprintf(stderr, "Exponentiation test failed!\n");
  1002. return 0;
  1003. }
  1004. }
  1005. BN_free(a);
  1006. BN_free(b);
  1007. BN_free(d);
  1008. BN_free(e);
  1009. BN_free(one);
  1010. return (1);
  1011. }
  1012. /* test_exp_mod_zero tests that x**0 mod 1 == 0. */
  1013. static int test_exp_mod_zero() {
  1014. BIGNUM a, p, m;
  1015. BIGNUM r;
  1016. BN_CTX *ctx = BN_CTX_new();
  1017. int ret = 0;
  1018. BN_init(&m);
  1019. BN_one(&m);
  1020. BN_init(&a);
  1021. BN_one(&a);
  1022. BN_init(&p);
  1023. BN_zero(&p);
  1024. BN_init(&r);
  1025. BN_mod_exp(&r, &a, &p, &m, ctx);
  1026. BN_CTX_free(ctx);
  1027. if (BN_is_zero(&r)) {
  1028. ret = 1;
  1029. } else {
  1030. printf("1**0 mod 1 = ");
  1031. BN_print_fp(stdout, &r);
  1032. printf(", should be 0\n");
  1033. }
  1034. BN_free(&r);
  1035. BN_free(&a);
  1036. BN_free(&p);
  1037. BN_free(&m);
  1038. return ret;
  1039. }
  1040. static int genprime_cb(int p, int n, BN_GENCB *arg) {
  1041. char c = '*';
  1042. if (p == 0)
  1043. c = '.';
  1044. if (p == 1)
  1045. c = '+';
  1046. if (p == 2)
  1047. c = '*';
  1048. if (p == 3)
  1049. c = '\n';
  1050. putc(c, stdout);
  1051. fflush(stdout);
  1052. return 1;
  1053. }
  1054. int test_mod_sqrt(BIO *bp, BN_CTX *ctx) {
  1055. BN_GENCB cb;
  1056. BIGNUM *a, *p, *r;
  1057. int i, j;
  1058. int ret = 0;
  1059. a = BN_new();
  1060. p = BN_new();
  1061. r = BN_new();
  1062. if (a == NULL || p == NULL || r == NULL)
  1063. goto err;
  1064. BN_GENCB_set(&cb, genprime_cb, NULL);
  1065. for (i = 0; i < 16; i++) {
  1066. if (i < 8) {
  1067. unsigned primes[8] = {2, 3, 5, 7, 11, 13, 17, 19};
  1068. if (!BN_set_word(p, primes[i]))
  1069. goto err;
  1070. } else {
  1071. if (!BN_set_word(a, 32))
  1072. goto err;
  1073. if (!BN_set_word(r, 2 * i + 1))
  1074. goto err;
  1075. if (!BN_generate_prime_ex(p, 256, 0, a, r, &cb))
  1076. goto err;
  1077. putc('\n', stdout);
  1078. }
  1079. p->neg = rand_neg();
  1080. for (j = 0; j < num2; j++) {
  1081. /* construct 'a' such that it is a square modulo p,
  1082. * but in general not a proper square and not reduced modulo p */
  1083. if (!BN_rand(r, 256, 0, 3))
  1084. goto err;
  1085. if (!BN_nnmod(r, r, p, ctx))
  1086. goto err;
  1087. if (!BN_mod_sqr(r, r, p, ctx))
  1088. goto err;
  1089. if (!BN_rand(a, 256, 0, 3))
  1090. goto err;
  1091. if (!BN_nnmod(a, a, p, ctx))
  1092. goto err;
  1093. if (!BN_mod_sqr(a, a, p, ctx))
  1094. goto err;
  1095. if (!BN_mul(a, a, r, ctx))
  1096. goto err;
  1097. if (rand_neg())
  1098. if (!BN_sub(a, a, p))
  1099. goto err;
  1100. if (!BN_mod_sqrt(r, a, p, ctx))
  1101. goto err;
  1102. if (!BN_mod_sqr(r, r, p, ctx))
  1103. goto err;
  1104. if (!BN_nnmod(a, a, p, ctx))
  1105. goto err;
  1106. if (BN_cmp(a, r) != 0) {
  1107. fprintf(stderr, "BN_mod_sqrt failed: a = ");
  1108. BN_print_fp(stderr, a);
  1109. fprintf(stderr, ", r = ");
  1110. BN_print_fp(stderr, r);
  1111. fprintf(stderr, ", p = ");
  1112. BN_print_fp(stderr, p);
  1113. fprintf(stderr, "\n");
  1114. goto err;
  1115. }
  1116. putc('.', stdout);
  1117. fflush(stdout);
  1118. }
  1119. putc('\n', stdout);
  1120. fflush(stderr);
  1121. }
  1122. ret = 1;
  1123. err:
  1124. if (a != NULL)
  1125. BN_free(a);
  1126. if (p != NULL)
  1127. BN_free(p);
  1128. if (r != NULL)
  1129. BN_free(r);
  1130. return ret;
  1131. }
  1132. int test_small_prime(BIO *bp, BN_CTX *ctx) {
  1133. static const int bits = 10;
  1134. int ret = 0;
  1135. BIGNUM r;
  1136. BN_init(&r);
  1137. if (!BN_generate_prime_ex(&r, bits, 0, NULL, NULL, NULL)) {
  1138. goto err;
  1139. }
  1140. if (BN_num_bits(&r) != bits) {
  1141. BIO_printf(bp, "Expected %d bit prime, got %d bit number\n", bits,
  1142. BN_num_bits(&r));
  1143. goto err;
  1144. }
  1145. ret = 1;
  1146. err:
  1147. BN_free(&r);
  1148. return ret;
  1149. }
  1150. int test_sqrt(BIO *bp, BN_CTX *ctx) {
  1151. BIGNUM *n = BN_new(), *nn = BN_new(), *sqrt = BN_new();
  1152. unsigned i;
  1153. /* Test some random squares. */
  1154. for (i = 0; i < 100; i++) {
  1155. if (!BN_rand(n, 1024 /* bit length */, -1 /* no modification of top bits */,
  1156. 0 /* don't modify bottom bit */) ||
  1157. !BN_mul(nn, n, n, ctx) ||
  1158. !BN_sqrt(sqrt, nn, ctx)) {
  1159. BIO_print_errors_fp(stderr);
  1160. return 0;
  1161. }
  1162. if (BN_cmp(n, sqrt) != 0) {
  1163. fprintf(stderr, "Bad result from BN_sqrt.\n");
  1164. return 0;
  1165. }
  1166. }
  1167. /* Test some non-squares */
  1168. for (i = 0; i < 100; i++) {
  1169. if (!BN_rand(n, 1024 /* bit length */, -1 /* no modification of top bits */,
  1170. 0 /* don't modify bottom bit */) ||
  1171. !BN_mul(nn, n, n, ctx) ||
  1172. !BN_add(nn, nn, BN_value_one())) {
  1173. BIO_print_errors_fp(stderr);
  1174. return 0;
  1175. }
  1176. if (BN_sqrt(sqrt, nn, ctx)) {
  1177. char *nn_str = BN_bn2dec(nn);
  1178. fprintf(stderr, "BIO_sqrt didn't fail on a non-square: %s\n", nn_str);
  1179. OPENSSL_free(nn_str);
  1180. }
  1181. }
  1182. BN_free(n);
  1183. BN_free(sqrt);
  1184. BN_free(nn);
  1185. return 1;
  1186. }
  1187. int test_bn2bin_padded(BIO *bp, BN_CTX *ctx) {
  1188. BIGNUM *n = BN_new();
  1189. uint8_t zeros[256], out[256], reference[128];
  1190. size_t bytes;
  1191. memset(zeros, 0, sizeof(zeros));
  1192. /* Test edge case at 0. */
  1193. if (!BN_bn2bin_padded(NULL, 0, n)) {
  1194. fprintf(stderr,
  1195. "BN_bn2bin_padded failed to encode 0 in an empty buffer.\n");
  1196. return 0;
  1197. }
  1198. memset(out, -1, sizeof(out));
  1199. if (!BN_bn2bin_padded(out, sizeof(out), n)) {
  1200. fprintf(stderr,
  1201. "BN_bn2bin_padded failed to encode 0 in a non-empty buffer.\n");
  1202. return 0;
  1203. }
  1204. if (memcmp(zeros, out, sizeof(out))) {
  1205. fprintf(stderr, "BN_bn2bin_padded did not zero buffer.\n");
  1206. return 0;
  1207. }
  1208. /* Test a random numbers at various byte lengths. */
  1209. for (bytes = 128 - 7; bytes <= 128; bytes++) {
  1210. if (!BN_rand(n, bytes * 8, 0 /* make sure top bit is 1 */,
  1211. 0 /* don't modify bottom bit */)) {
  1212. BIO_print_errors_fp(stderr);
  1213. return 0;
  1214. }
  1215. if (BN_num_bytes(n) != bytes || BN_bn2bin(n, reference) != bytes) {
  1216. fprintf(stderr, "Bad result from BN_rand; bytes.\n");
  1217. return 0;
  1218. }
  1219. /* Empty buffer should fail. */
  1220. if (BN_bn2bin_padded(NULL, 0, n)) {
  1221. fprintf(stderr,
  1222. "BN_bn2bin_padded incorrectly succeeded on empty buffer.\n");
  1223. return 0;
  1224. }
  1225. /* One byte short should fail. */
  1226. if (BN_bn2bin_padded(out, bytes - 1, n)) {
  1227. fprintf(stderr, "BN_bn2bin_padded incorrectly succeeded on short.\n");
  1228. return 0;
  1229. }
  1230. /* Exactly right size should encode. */
  1231. if (!BN_bn2bin_padded(out, bytes, n) ||
  1232. memcmp(out, reference, bytes) != 0) {
  1233. fprintf(stderr, "BN_bn2bin_padded gave a bad result.\n");
  1234. return 0;
  1235. }
  1236. /* Pad up one byte extra. */
  1237. if (!BN_bn2bin_padded(out, bytes + 1, n) ||
  1238. memcmp(out + 1, reference, bytes) || memcmp(out, zeros, 1)) {
  1239. fprintf(stderr, "BN_bn2bin_padded gave a bad result.\n");
  1240. return 0;
  1241. }
  1242. /* Pad up to 256. */
  1243. if (!BN_bn2bin_padded(out, sizeof(out), n) ||
  1244. memcmp(out + sizeof(out) - bytes, reference, bytes) ||
  1245. memcmp(out, zeros, sizeof(out) - bytes)) {
  1246. fprintf(stderr, "BN_bn2bin_padded gave a bad result.\n");
  1247. return 0;
  1248. }
  1249. }
  1250. BN_free(n);
  1251. return 1;
  1252. }