You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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