選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

1915 行
52 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. /* For BIGNUM format macros. */
  70. #if !defined(__STDC_FORMAT_MACROS)
  71. #define __STDC_FORMAT_MACROS
  72. #endif
  73. #include <errno.h>
  74. #include <stdio.h>
  75. #include <string.h>
  76. #include <openssl/bn.h>
  77. #include <openssl/crypto.h>
  78. #include <openssl/err.h>
  79. #include <openssl/mem.h>
  80. #include "../crypto/test/scoped_types.h"
  81. #include "../crypto/test/test_util.h"
  82. // This program tests the BIGNUM implementation. It takes an optional -bc
  83. // argument to write a transcript compatible with the UNIX bc utility.
  84. //
  85. // TODO(davidben): Rather than generate random inputs and depend on bc to check
  86. // the results, most of these tests should use known answers.
  87. static const int num0 = 100; // number of tests
  88. static const int num1 = 50; // additional tests for some functions
  89. static const int num2 = 5; // number of tests for slow functions
  90. static bool test_add(FILE *fp);
  91. static bool test_sub(FILE *fp);
  92. static bool test_lshift1(FILE *fp);
  93. static bool test_lshift(FILE *fp, BN_CTX *ctx, ScopedBIGNUM a);
  94. static bool test_rshift1(FILE *fp);
  95. static bool test_rshift(FILE *fp, BN_CTX *ctx);
  96. static bool test_sqr(FILE *fp, BN_CTX *ctx);
  97. static bool test_mul(FILE *fp);
  98. static bool test_div(FILE *fp, BN_CTX *ctx);
  99. static int rand_neg();
  100. static bool test_div_word(FILE *fp);
  101. static bool test_mont(FILE *fp, BN_CTX *ctx);
  102. static bool test_mod(FILE *fp, BN_CTX *ctx);
  103. static bool test_mod_mul(FILE *fp, BN_CTX *ctx);
  104. static bool test_mod_exp(FILE *fp, BN_CTX *ctx);
  105. static bool test_mod_exp_mont_consttime(FILE *fp, BN_CTX *ctx);
  106. static bool test_exp(FILE *fp, BN_CTX *ctx);
  107. static bool test_mod_sqrt(FILE *fp, BN_CTX *ctx);
  108. static bool test_exp_mod_zero(void);
  109. static bool test_small_prime(FILE *fp, BN_CTX *ctx);
  110. static bool test_mod_exp_mont5(FILE *fp, BN_CTX *ctx);
  111. static bool test_sqrt(FILE *fp, BN_CTX *ctx);
  112. static bool test_bn2bin_padded(BN_CTX *ctx);
  113. static bool test_dec2bn(BN_CTX *ctx);
  114. static bool test_hex2bn(BN_CTX *ctx);
  115. static bool test_asc2bn(BN_CTX *ctx);
  116. static bool test_mpi();
  117. static bool test_rand();
  118. static bool test_asn1();
  119. static const uint8_t kSample[] =
  120. "\xC6\x4F\x43\x04\x2A\xEA\xCA\x6E\x58\x36\x80\x5B\xE8\xC9"
  121. "\x9B\x04\x5D\x48\x36\xC2\xFD\x16\xC9\x64\xF0";
  122. // A wrapper around puts that takes its arguments in the same order as our *_fp
  123. // functions.
  124. static void puts_fp(FILE *out, const char *m) {
  125. if (out != nullptr) {
  126. fputs(m, out);
  127. }
  128. }
  129. static void flush_fp(FILE *out) {
  130. if (out != nullptr) {
  131. fflush(out);
  132. }
  133. }
  134. static void message(FILE *out, const char *m) {
  135. puts_fp(out, "print \"test ");
  136. puts_fp(out, m);
  137. puts_fp(out, "\\n\"\n");
  138. }
  139. int main(int argc, char *argv[]) {
  140. CRYPTO_library_init();
  141. ScopedFILE bc_file;
  142. argc--;
  143. argv++;
  144. while (argc >= 1) {
  145. if (strcmp(*argv, "-bc") == 0) {
  146. if (argc < 2) {
  147. fprintf(stderr, "Missing parameter to -bc\n");
  148. return 1;
  149. }
  150. bc_file.reset(fopen(argv[1], "w+"));
  151. if (!bc_file) {
  152. fprintf(stderr, "Failed to open %s: %s\n", argv[1], strerror(errno));
  153. }
  154. argc--;
  155. argv++;
  156. } else {
  157. fprintf(stderr, "Unknown option: %s\n", argv[0]);
  158. return 1;
  159. }
  160. argc--;
  161. argv++;
  162. }
  163. ScopedBN_CTX ctx(BN_CTX_new());
  164. if (!ctx) {
  165. return 1;
  166. }
  167. puts_fp(bc_file.get(), "/* This script, when run through the UNIX bc utility, "
  168. "should produce a sequence of zeros. */\n");
  169. puts_fp(bc_file.get(), "/* tr a-f A-F < bn_test.out | sed s/BAsE/base/ | bc "
  170. "| grep -v 0 */\n");
  171. puts_fp(bc_file.get(), "obase=16\nibase=16\n");
  172. message(bc_file.get(), "BN_add");
  173. if (!test_add(bc_file.get())) {
  174. return 1;
  175. }
  176. flush_fp(bc_file.get());
  177. message(bc_file.get(), "BN_sub");
  178. if (!test_sub(bc_file.get())) {
  179. return 1;
  180. }
  181. flush_fp(bc_file.get());
  182. message(bc_file.get(), "BN_lshift1");
  183. if (!test_lshift1(bc_file.get())) {
  184. return 1;
  185. }
  186. flush_fp(bc_file.get());
  187. message(bc_file.get(), "BN_lshift (fixed)");
  188. ScopedBIGNUM sample(BN_bin2bn(kSample, sizeof(kSample) - 1, NULL));
  189. if (!sample) {
  190. return 1;
  191. }
  192. if (!test_lshift(bc_file.get(), ctx.get(), bssl::move(sample))) {
  193. return 1;
  194. }
  195. flush_fp(bc_file.get());
  196. message(bc_file.get(), "BN_lshift");
  197. if (!test_lshift(bc_file.get(), ctx.get(), nullptr)) {
  198. return 1;
  199. }
  200. flush_fp(bc_file.get());
  201. message(bc_file.get(), "BN_rshift1");
  202. if (!test_rshift1(bc_file.get())) {
  203. return 1;
  204. }
  205. flush_fp(bc_file.get());
  206. message(bc_file.get(), "BN_rshift");
  207. if (!test_rshift(bc_file.get(), ctx.get())) {
  208. return 1;
  209. }
  210. flush_fp(bc_file.get());
  211. message(bc_file.get(), "BN_sqr");
  212. if (!test_sqr(bc_file.get(), ctx.get())) {
  213. return 1;
  214. }
  215. flush_fp(bc_file.get());
  216. message(bc_file.get(), "BN_mul");
  217. if (!test_mul(bc_file.get())) {
  218. return 1;
  219. }
  220. flush_fp(bc_file.get());
  221. message(bc_file.get(), "BN_div");
  222. if (!test_div(bc_file.get(), ctx.get())) {
  223. return 1;
  224. }
  225. flush_fp(bc_file.get());
  226. message(bc_file.get(), "BN_div_word");
  227. if (!test_div_word(bc_file.get())) {
  228. return 1;
  229. }
  230. flush_fp(bc_file.get());
  231. message(bc_file.get(), "BN_mod");
  232. if (!test_mod(bc_file.get(), ctx.get())) {
  233. return 1;
  234. }
  235. flush_fp(bc_file.get());
  236. message(bc_file.get(), "BN_mod_mul");
  237. if (!test_mod_mul(bc_file.get(), ctx.get())) {
  238. return 1;
  239. }
  240. flush_fp(bc_file.get());
  241. message(bc_file.get(), "BN_mont");
  242. if (!test_mont(bc_file.get(), ctx.get())) {
  243. return 1;
  244. }
  245. flush_fp(bc_file.get());
  246. message(bc_file.get(), "BN_mod_exp");
  247. if (!test_mod_exp(bc_file.get(), ctx.get())) {
  248. return 1;
  249. }
  250. flush_fp(bc_file.get());
  251. message(bc_file.get(), "BN_mod_exp_mont_consttime");
  252. if (!test_mod_exp_mont_consttime(bc_file.get(), ctx.get()) ||
  253. !test_mod_exp_mont5(bc_file.get(), ctx.get())) {
  254. return 1;
  255. }
  256. flush_fp(bc_file.get());
  257. message(bc_file.get(), "BN_exp");
  258. if (!test_exp(bc_file.get(), ctx.get()) ||
  259. !test_exp_mod_zero()) {
  260. return 1;
  261. }
  262. flush_fp(bc_file.get());
  263. message(bc_file.get(), "BN_mod_sqrt");
  264. if (!test_mod_sqrt(bc_file.get(), ctx.get())) {
  265. return 1;
  266. }
  267. flush_fp(bc_file.get());
  268. message(bc_file.get(), "Small prime generation");
  269. if (!test_small_prime(bc_file.get(), ctx.get())) {
  270. return 1;
  271. }
  272. flush_fp(bc_file.get());
  273. message(bc_file.get(), "BN_sqrt");
  274. if (!test_sqrt(bc_file.get(), ctx.get())) {
  275. return 1;
  276. }
  277. flush_fp(bc_file.get());
  278. if (!test_bn2bin_padded(ctx.get()) ||
  279. !test_dec2bn(ctx.get()) ||
  280. !test_hex2bn(ctx.get()) ||
  281. !test_asc2bn(ctx.get()) ||
  282. !test_mpi() ||
  283. !test_rand() ||
  284. !test_asn1()) {
  285. return 1;
  286. }
  287. printf("PASS\n");
  288. return 0;
  289. }
  290. static bool test_add(FILE *fp) {
  291. ScopedBIGNUM a(BN_new());
  292. ScopedBIGNUM b(BN_new());
  293. ScopedBIGNUM c(BN_new());
  294. if (!a || !b || !c || !BN_rand(a.get(), 512, 0, 0)) {
  295. return false;
  296. }
  297. for (int i = 0; i < num0; i++) {
  298. if (!BN_rand(b.get(), 450 + i, 0, 0)) {
  299. return false;
  300. }
  301. a->neg = rand_neg();
  302. b->neg = rand_neg();
  303. if (!BN_add(c.get(), a.get(), b.get())) {
  304. return false;
  305. }
  306. if (fp != NULL) {
  307. BN_print_fp(fp, a.get());
  308. puts_fp(fp, " + ");
  309. BN_print_fp(fp, b.get());
  310. puts_fp(fp, " - ");
  311. BN_print_fp(fp, c.get());
  312. puts_fp(fp, "\n");
  313. }
  314. a->neg = !a->neg;
  315. b->neg = !b->neg;
  316. if (!BN_add(c.get(), c.get(), b.get()) ||
  317. !BN_add(c.get(), c.get(), a.get())) {
  318. return false;
  319. }
  320. if (!BN_is_zero(c.get())) {
  321. fprintf(stderr, "Add test failed!\n");
  322. return false;
  323. }
  324. }
  325. return true;
  326. }
  327. static bool test_sub(FILE *fp) {
  328. ScopedBIGNUM a(BN_new());
  329. ScopedBIGNUM b(BN_new());
  330. ScopedBIGNUM c(BN_new());
  331. if (!a || !b || !c) {
  332. return false;
  333. }
  334. for (int i = 0; i < num0 + num1; i++) {
  335. if (i < num1) {
  336. if (!BN_rand(a.get(), 512, 0, 0) ||
  337. !BN_copy(b.get(), a.get()) ||
  338. !BN_set_bit(a.get(), i) ||
  339. !BN_add_word(b.get(), i)) {
  340. return false;
  341. }
  342. } else {
  343. if (!BN_rand(b.get(), 400 + i - num1, 0, 0)) {
  344. return false;
  345. }
  346. a->neg = rand_neg();
  347. b->neg = rand_neg();
  348. }
  349. if (!BN_sub(c.get(), a.get(), b.get())) {
  350. return false;
  351. }
  352. if (fp != NULL) {
  353. BN_print_fp(fp, a.get());
  354. puts_fp(fp, " - ");
  355. BN_print_fp(fp, b.get());
  356. puts_fp(fp, " - ");
  357. BN_print_fp(fp, c.get());
  358. puts_fp(fp, "\n");
  359. }
  360. if (!BN_add(c.get(), c.get(), b.get()) ||
  361. !BN_sub(c.get(), c.get(), a.get())) {
  362. return false;
  363. }
  364. if (!BN_is_zero(c.get())) {
  365. fprintf(stderr, "Subtract test failed!\n");
  366. return false;
  367. }
  368. }
  369. return true;
  370. }
  371. static bool test_div(FILE *fp, BN_CTX *ctx) {
  372. ScopedBIGNUM a(BN_new());
  373. ScopedBIGNUM b(BN_new());
  374. ScopedBIGNUM c(BN_new());
  375. ScopedBIGNUM d(BN_new());
  376. ScopedBIGNUM e(BN_new());
  377. if (!a || !b || !c || !d || !e) {
  378. return false;
  379. }
  380. if (!BN_one(a.get())) {
  381. return false;
  382. }
  383. BN_zero(b.get());
  384. if (BN_div(d.get(), c.get(), a.get(), b.get(), ctx)) {
  385. fprintf(stderr, "Division by zero succeeded!\n");
  386. return false;
  387. }
  388. ERR_clear_error();
  389. for (int i = 0; i < num0 + num1; i++) {
  390. if (i < num1) {
  391. if (!BN_rand(a.get(), 400, 0, 0) ||
  392. !BN_copy(b.get(), a.get()) ||
  393. !BN_lshift(a.get(), a.get(), i) ||
  394. !BN_add_word(a.get(), i)) {
  395. return false;
  396. }
  397. } else if (!BN_rand(b.get(), 50 + 3 * (i - num1), 0, 0)) {
  398. return false;
  399. }
  400. a->neg = rand_neg();
  401. b->neg = rand_neg();
  402. if (!BN_div(d.get(), c.get(), a.get(), b.get(), ctx)) {
  403. return false;
  404. }
  405. if (fp != NULL) {
  406. BN_print_fp(fp, a.get());
  407. puts_fp(fp, " / ");
  408. BN_print_fp(fp, b.get());
  409. puts_fp(fp, " - ");
  410. BN_print_fp(fp, d.get());
  411. puts_fp(fp, "\n");
  412. BN_print_fp(fp, a.get());
  413. puts_fp(fp, " % ");
  414. BN_print_fp(fp, b.get());
  415. puts_fp(fp, " - ");
  416. BN_print_fp(fp, c.get());
  417. puts_fp(fp, "\n");
  418. }
  419. if (!BN_mul(e.get(), d.get(), b.get(), ctx) ||
  420. !BN_add(d.get(), e.get(), c.get()) ||
  421. !BN_sub(d.get(), d.get(), a.get())) {
  422. return false;
  423. }
  424. if (!BN_is_zero(d.get())) {
  425. fprintf(stderr, "Division test failed!\n");
  426. return false;
  427. }
  428. }
  429. // Test that BN_div never gives negative zero in the quotient.
  430. if (!BN_set_word(a.get(), 1) ||
  431. !BN_set_word(b.get(), 2)) {
  432. return false;
  433. }
  434. BN_set_negative(a.get(), 1);
  435. if (!BN_div(d.get(), c.get(), a.get(), b.get(), ctx)) {
  436. return false;
  437. }
  438. if (!BN_is_zero(d.get()) || BN_is_negative(d.get())) {
  439. fprintf(stderr, "Division test failed!\n");
  440. return false;
  441. }
  442. // Test that BN_div never gives negative zero in the remainder.
  443. if (!BN_set_word(b.get(), 1)) {
  444. return false;
  445. }
  446. if (!BN_div(d.get(), c.get(), a.get(), b.get(), ctx)) {
  447. return false;
  448. }
  449. if (!BN_is_zero(c.get()) || BN_is_negative(c.get())) {
  450. fprintf(stderr, "Division test failed!\n");
  451. return false;
  452. }
  453. return true;
  454. }
  455. static bool test_lshift1(FILE *fp) {
  456. ScopedBIGNUM a(BN_new());
  457. ScopedBIGNUM b(BN_new());
  458. ScopedBIGNUM c(BN_new());
  459. if (!a || !b || !c || !BN_rand(a.get(), 200, 0, 0)) {
  460. return false;
  461. }
  462. a->neg = rand_neg();
  463. for (int i = 0; i < num0; i++) {
  464. if (!BN_lshift1(b.get(), a.get())) {
  465. return false;
  466. }
  467. if (fp != NULL) {
  468. BN_print_fp(fp, a.get());
  469. puts_fp(fp, " * 2");
  470. puts_fp(fp, " - ");
  471. BN_print_fp(fp, b.get());
  472. puts_fp(fp, "\n");
  473. }
  474. if (!BN_add(c.get(), a.get(), a.get()) ||
  475. !BN_sub(a.get(), b.get(), c.get())) {
  476. return false;
  477. }
  478. if (!BN_is_zero(a.get())) {
  479. fprintf(stderr, "Left shift one test failed!\n");
  480. return false;
  481. }
  482. if (!BN_copy(a.get(), b.get())) {
  483. return false;
  484. }
  485. }
  486. return true;
  487. }
  488. static bool test_rshift(FILE *fp, BN_CTX *ctx) {
  489. ScopedBIGNUM a(BN_new());
  490. ScopedBIGNUM b(BN_new());
  491. ScopedBIGNUM c(BN_new());
  492. ScopedBIGNUM d(BN_new());
  493. ScopedBIGNUM e(BN_new());
  494. if (!a || !b || !c || !d || !e || !BN_one(c.get()) ||
  495. !BN_rand(a.get(), 200, 0, 0)) {
  496. return false;
  497. }
  498. a->neg = rand_neg();
  499. for (int i = 0; i < num0; i++) {
  500. if (!BN_rshift(b.get(), a.get(), i + 1) ||
  501. !BN_add(c.get(), c.get(), c.get())) {
  502. return false;
  503. }
  504. if (fp != NULL) {
  505. BN_print_fp(fp, a.get());
  506. puts_fp(fp, " / ");
  507. BN_print_fp(fp, c.get());
  508. puts_fp(fp, " - ");
  509. BN_print_fp(fp, b.get());
  510. puts_fp(fp, "\n");
  511. }
  512. if (!BN_div(d.get(), e.get(), a.get(), c.get(), ctx) ||
  513. !BN_sub(d.get(), d.get(), b.get())) {
  514. return false;
  515. }
  516. if (!BN_is_zero(d.get())) {
  517. fprintf(stderr, "Right shift test failed!\n");
  518. return false;
  519. }
  520. }
  521. return true;
  522. }
  523. static bool test_rshift1(FILE *fp) {
  524. ScopedBIGNUM a(BN_new());
  525. ScopedBIGNUM b(BN_new());
  526. ScopedBIGNUM c(BN_new());
  527. if (!a || !b || !c || !BN_rand(a.get(), 200, 0, 0)) {
  528. return false;
  529. }
  530. a->neg = rand_neg();
  531. for (int i = 0; i < num0; i++) {
  532. if (!BN_rshift1(b.get(), a.get())) {
  533. return false;
  534. }
  535. if (fp != NULL) {
  536. BN_print_fp(fp, a.get());
  537. puts_fp(fp, " / 2");
  538. puts_fp(fp, " - ");
  539. BN_print_fp(fp, b.get());
  540. puts_fp(fp, "\n");
  541. }
  542. if (!BN_sub(c.get(), a.get(), b.get()) ||
  543. !BN_sub(c.get(), c.get(), b.get())) {
  544. return false;
  545. }
  546. if (!BN_is_zero(c.get()) && !BN_abs_is_word(c.get(), 1)) {
  547. fprintf(stderr, "Right shift one test failed!\n");
  548. return false;
  549. }
  550. if (!BN_copy(a.get(), b.get())) {
  551. return false;
  552. }
  553. }
  554. return true;
  555. }
  556. static bool test_lshift(FILE *fp, BN_CTX *ctx, ScopedBIGNUM a) {
  557. if (!a) {
  558. a.reset(BN_new());
  559. if (!a || !BN_rand(a.get(), 200, 0, 0)) {
  560. return false;
  561. }
  562. a->neg = rand_neg();
  563. }
  564. ScopedBIGNUM b(BN_new());
  565. ScopedBIGNUM c(BN_new());
  566. ScopedBIGNUM d(BN_new());
  567. if (!b || !c || !d || !BN_one(c.get())) {
  568. return false;
  569. }
  570. for (int i = 0; i < num0; i++) {
  571. if (!BN_lshift(b.get(), a.get(), i + 1) ||
  572. !BN_add(c.get(), c.get(), c.get())) {
  573. return false;
  574. }
  575. if (fp != NULL) {
  576. BN_print_fp(fp, a.get());
  577. puts_fp(fp, " * ");
  578. BN_print_fp(fp, c.get());
  579. puts_fp(fp, " - ");
  580. BN_print_fp(fp, b.get());
  581. puts_fp(fp, "\n");
  582. }
  583. if (!BN_mul(d.get(), a.get(), c.get(), ctx) ||
  584. !BN_sub(d.get(), d.get(), b.get())) {
  585. return false;
  586. }
  587. if (!BN_is_zero(d.get())) {
  588. fprintf(stderr, "Left shift test failed!\n");
  589. fprintf(stderr, "a=");
  590. BN_print_fp(stderr, a.get());
  591. fprintf(stderr, "\nb=");
  592. BN_print_fp(stderr, b.get());
  593. fprintf(stderr, "\nc=");
  594. BN_print_fp(stderr, c.get());
  595. fprintf(stderr, "\nd=");
  596. BN_print_fp(stderr, d.get());
  597. fprintf(stderr, "\n");
  598. return false;
  599. }
  600. }
  601. return true;
  602. }
  603. static bool test_mul(FILE *fp) {
  604. ScopedBN_CTX ctx(BN_CTX_new());
  605. ScopedBIGNUM a(BN_new());
  606. ScopedBIGNUM b(BN_new());
  607. ScopedBIGNUM c(BN_new());
  608. ScopedBIGNUM d(BN_new());
  609. ScopedBIGNUM e(BN_new());
  610. if (!ctx || !a || !b || !c || !d || !e) {
  611. return false;
  612. }
  613. for (int i = 0; i < num0 + num1; i++) {
  614. if (i <= num1) {
  615. if (!BN_rand(a.get(), 100, 0, 0) ||
  616. !BN_rand(b.get(), 100, 0, 0)) {
  617. return false;
  618. }
  619. } else if (!BN_rand(b.get(), i - num1, 0, 0)) {
  620. return false;
  621. }
  622. a->neg = rand_neg();
  623. b->neg = rand_neg();
  624. if (!BN_mul(c.get(), a.get(), b.get(), ctx.get())) {
  625. return false;
  626. }
  627. if (fp != NULL) {
  628. BN_print_fp(fp, a.get());
  629. puts_fp(fp, " * ");
  630. BN_print_fp(fp, b.get());
  631. puts_fp(fp, " - ");
  632. BN_print_fp(fp, c.get());
  633. puts_fp(fp, "\n");
  634. }
  635. if (!BN_div(d.get(), e.get(), c.get(), a.get(), ctx.get()) ||
  636. !BN_sub(d.get(), d.get(), b.get())) {
  637. return false;
  638. }
  639. if (!BN_is_zero(d.get()) || !BN_is_zero(e.get())) {
  640. fprintf(stderr, "Multiplication test failed!\n");
  641. return false;
  642. }
  643. }
  644. // Test that BN_mul never gives negative zero.
  645. if (!BN_set_word(a.get(), 1)) {
  646. return false;
  647. }
  648. BN_set_negative(a.get(), 1);
  649. BN_zero(b.get());
  650. if (!BN_mul(c.get(), a.get(), b.get(), ctx.get())) {
  651. return false;
  652. }
  653. if (!BN_is_zero(c.get()) || BN_is_negative(c.get())) {
  654. fprintf(stderr, "Multiplication test failed!\n");
  655. return false;
  656. }
  657. return true;
  658. }
  659. static bool test_sqr(FILE *fp, BN_CTX *ctx) {
  660. ScopedBIGNUM a(BN_new());
  661. ScopedBIGNUM c(BN_new());
  662. ScopedBIGNUM d(BN_new());
  663. ScopedBIGNUM e(BN_new());
  664. if (!a || !c || !d || !e) {
  665. return false;
  666. }
  667. for (int i = 0; i < num0; i++) {
  668. if (!BN_rand(a.get(), 40 + i * 10, 0, 0)) {
  669. return false;
  670. }
  671. a->neg = rand_neg();
  672. if (!BN_sqr(c.get(), a.get(), ctx)) {
  673. return false;
  674. }
  675. if (fp != NULL) {
  676. BN_print_fp(fp, a.get());
  677. puts_fp(fp, " * ");
  678. BN_print_fp(fp, a.get());
  679. puts_fp(fp, " - ");
  680. BN_print_fp(fp, c.get());
  681. puts_fp(fp, "\n");
  682. }
  683. if (!BN_div(d.get(), e.get(), c.get(), a.get(), ctx) ||
  684. !BN_sub(d.get(), d.get(), a.get())) {
  685. return false;
  686. }
  687. if (!BN_is_zero(d.get()) || !BN_is_zero(e.get())) {
  688. fprintf(stderr, "Square test failed!\n");
  689. return false;
  690. }
  691. }
  692. // Regression test for a BN_sqr overflow bug.
  693. BIGNUM *a_raw = a.get();
  694. if (!BN_hex2bn(
  695. &a_raw,
  696. "80000000000000008000000000000001FFFFFFFFFFFFFFFE0000000000000000") ||
  697. !BN_sqr(c.get(), a.get(), ctx)) {
  698. return false;
  699. }
  700. if (fp != NULL) {
  701. BN_print_fp(fp, a.get());
  702. puts_fp(fp, " * ");
  703. BN_print_fp(fp, a.get());
  704. puts_fp(fp, " - ");
  705. BN_print_fp(fp, c.get());
  706. puts_fp(fp, "\n");
  707. }
  708. if (!BN_mul(d.get(), a.get(), a.get(), ctx)) {
  709. return false;
  710. }
  711. if (BN_cmp(c.get(), d.get())) {
  712. fprintf(stderr,
  713. "Square test failed: BN_sqr and BN_mul produce "
  714. "different results!\n");
  715. return false;
  716. }
  717. // Regression test for a BN_sqr overflow bug.
  718. a_raw = a.get();
  719. if (!BN_hex2bn(
  720. &a_raw,
  721. "80000000000000000000000080000001FFFFFFFE000000000000000000000000") ||
  722. !BN_sqr(c.get(), a.get(), ctx)) {
  723. return false;
  724. }
  725. if (fp != NULL) {
  726. BN_print_fp(fp, a.get());
  727. puts_fp(fp, " * ");
  728. BN_print_fp(fp, a.get());
  729. puts_fp(fp, " - ");
  730. BN_print_fp(fp, c.get());
  731. puts_fp(fp, "\n");
  732. }
  733. if (!BN_mul(d.get(), a.get(), a.get(), ctx)) {
  734. return false;
  735. }
  736. if (BN_cmp(c.get(), d.get())) {
  737. fprintf(stderr,
  738. "Square test failed: BN_sqr and BN_mul produce "
  739. "different results!\n");
  740. return false;
  741. }
  742. return true;
  743. }
  744. static int rand_neg() {
  745. static unsigned int neg = 0;
  746. static const int sign[8] = {0, 0, 0, 1, 1, 0, 1, 1};
  747. return sign[(neg++) % 8];
  748. }
  749. static void print_word(FILE *fp, BN_ULONG w) {
  750. fprintf(fp, BN_HEX_FMT1, w);
  751. }
  752. static bool test_div_word(FILE *fp) {
  753. ScopedBIGNUM a(BN_new());
  754. ScopedBIGNUM b(BN_new());
  755. if (!a || !b) {
  756. return false;
  757. }
  758. for (int i = 0; i < num0; i++) {
  759. do {
  760. if (!BN_rand(a.get(), 512, -1, 0) ||
  761. !BN_rand(b.get(), BN_BITS2, -1, 0)) {
  762. return false;
  763. }
  764. } while (BN_is_zero(b.get()));
  765. if (!BN_copy(b.get(), a.get())) {
  766. return false;
  767. }
  768. BN_ULONG s = b->d[0];
  769. BN_ULONG r = BN_div_word(b.get(), s);
  770. if (r == (BN_ULONG)-1) {
  771. return false;
  772. }
  773. if (fp != NULL) {
  774. BN_print_fp(fp, a.get());
  775. puts_fp(fp, " / ");
  776. print_word(fp, s);
  777. puts_fp(fp, " - ");
  778. BN_print_fp(fp, b.get());
  779. puts_fp(fp, "\n");
  780. BN_print_fp(fp, a.get());
  781. puts_fp(fp, " % ");
  782. print_word(fp, s);
  783. puts_fp(fp, " - ");
  784. print_word(fp, r);
  785. puts_fp(fp, "\n");
  786. }
  787. if (!BN_mul_word(b.get(), s) ||
  788. !BN_add_word(b.get(), r) ||
  789. !BN_sub(b.get(), a.get(), b.get())) {
  790. return false;
  791. }
  792. if (!BN_is_zero(b.get())) {
  793. fprintf(stderr, "Division (word) test failed!\n");
  794. return false;
  795. }
  796. }
  797. return true;
  798. }
  799. static bool test_mont(FILE *fp, BN_CTX *ctx) {
  800. ScopedBIGNUM a(BN_new());
  801. ScopedBIGNUM b(BN_new());
  802. ScopedBIGNUM c(BN_new());
  803. ScopedBIGNUM d(BN_new());
  804. ScopedBIGNUM A(BN_new());
  805. ScopedBIGNUM B(BN_new());
  806. ScopedBIGNUM n(BN_new());
  807. ScopedBN_MONT_CTX mont(BN_MONT_CTX_new());
  808. if (!a || !b || !c || !d || !A || !B || !n || !mont) {
  809. return false;
  810. }
  811. BN_zero(n.get());
  812. if (BN_MONT_CTX_set(mont.get(), n.get(), ctx)) {
  813. fprintf(stderr, "BN_MONT_CTX_set succeeded for zero modulus!\n");
  814. return false;
  815. }
  816. ERR_clear_error();
  817. if (!BN_set_word(n.get(), 16)) {
  818. return false;
  819. }
  820. if (BN_MONT_CTX_set(mont.get(), n.get(), ctx)) {
  821. fprintf(stderr, "BN_MONT_CTX_set succeeded for even modulus!\n");
  822. return false;
  823. }
  824. ERR_clear_error();
  825. if (!BN_rand(a.get(), 100, 0, 0) ||
  826. !BN_rand(b.get(), 100, 0, 0)) {
  827. return false;
  828. }
  829. for (int i = 0; i < num2; i++) {
  830. int bits = (200 * (i + 1)) / num2;
  831. if (bits == 0) {
  832. continue;
  833. }
  834. if (!BN_rand(n.get(), bits, 0, 1) ||
  835. !BN_MONT_CTX_set(mont.get(), n.get(), ctx) ||
  836. !BN_nnmod(a.get(), a.get(), n.get(), ctx) ||
  837. !BN_nnmod(b.get(), b.get(), n.get(), ctx) ||
  838. !BN_to_montgomery(A.get(), a.get(), mont.get(), ctx) ||
  839. !BN_to_montgomery(B.get(), b.get(), mont.get(), ctx) ||
  840. !BN_mod_mul_montgomery(c.get(), A.get(), B.get(), mont.get(), ctx) ||
  841. !BN_from_montgomery(A.get(), c.get(), mont.get(), ctx)) {
  842. return false;
  843. }
  844. if (fp != NULL) {
  845. BN_print_fp(fp, a.get());
  846. puts_fp(fp, " * ");
  847. BN_print_fp(fp, b.get());
  848. puts_fp(fp, " % ");
  849. BN_print_fp(fp, &mont->N);
  850. puts_fp(fp, " - ");
  851. BN_print_fp(fp, A.get());
  852. puts_fp(fp, "\n");
  853. }
  854. if (!BN_mod_mul(d.get(), a.get(), b.get(), n.get(), ctx) ||
  855. !BN_sub(d.get(), d.get(), A.get())) {
  856. return false;
  857. }
  858. if (!BN_is_zero(d.get())) {
  859. fprintf(stderr, "Montgomery multiplication test failed!\n");
  860. return false;
  861. }
  862. }
  863. return true;
  864. }
  865. static bool test_mod(FILE *fp, BN_CTX *ctx) {
  866. ScopedBIGNUM a(BN_new());
  867. ScopedBIGNUM b(BN_new());
  868. ScopedBIGNUM c(BN_new());
  869. ScopedBIGNUM d(BN_new());
  870. ScopedBIGNUM e(BN_new());
  871. if (!a || !b || !c || !d || !e ||
  872. !BN_rand(a.get(), 1024, 0, 0)) {
  873. return false;
  874. }
  875. for (int i = 0; i < num0; i++) {
  876. if (!BN_rand(b.get(), 450 + i * 10, 0, 0)) {
  877. return false;
  878. }
  879. a->neg = rand_neg();
  880. b->neg = rand_neg();
  881. if (!BN_mod(c.get(), a.get(), b.get(), ctx)) {
  882. return false;
  883. }
  884. if (fp != NULL) {
  885. BN_print_fp(fp, a.get());
  886. puts_fp(fp, " % ");
  887. BN_print_fp(fp, b.get());
  888. puts_fp(fp, " - ");
  889. BN_print_fp(fp, c.get());
  890. puts_fp(fp, "\n");
  891. }
  892. if (!BN_div(d.get(), e.get(), a.get(), b.get(), ctx) ||
  893. !BN_sub(e.get(), e.get(), c.get())) {
  894. return false;
  895. }
  896. if (!BN_is_zero(e.get())) {
  897. fprintf(stderr, "Modulo test failed!\n");
  898. return false;
  899. }
  900. }
  901. return true;
  902. }
  903. static bool test_mod_mul(FILE *fp, BN_CTX *ctx) {
  904. ScopedBIGNUM a(BN_new());
  905. ScopedBIGNUM b(BN_new());
  906. ScopedBIGNUM c(BN_new());
  907. ScopedBIGNUM d(BN_new());
  908. ScopedBIGNUM e(BN_new());
  909. if (!a || !b || !c || !d || !e) {
  910. return false;
  911. }
  912. if (!BN_one(a.get()) || !BN_one(b.get())) {
  913. return false;
  914. }
  915. BN_zero(c.get());
  916. if (BN_mod_mul(e.get(), a.get(), b.get(), c.get(), ctx)) {
  917. fprintf(stderr, "BN_mod_mul with zero modulus succeeded!\n");
  918. return false;
  919. }
  920. ERR_clear_error();
  921. for (int j = 0; j < 3; j++) {
  922. if (!BN_rand(c.get(), 1024, 0, 0)) {
  923. return false;
  924. }
  925. for (int i = 0; i < num0; i++) {
  926. if (!BN_rand(a.get(), 475 + i * 10, 0, 0) ||
  927. !BN_rand(b.get(), 425 + i * 11, 0, 0)) {
  928. return false;
  929. }
  930. a->neg = rand_neg();
  931. b->neg = rand_neg();
  932. if (!BN_mod_mul(e.get(), a.get(), b.get(), c.get(), ctx)) {
  933. ERR_print_errors_fp(stderr);
  934. return false;
  935. }
  936. if (fp != NULL) {
  937. BN_print_fp(fp, a.get());
  938. puts_fp(fp, " * ");
  939. BN_print_fp(fp, b.get());
  940. puts_fp(fp, " % ");
  941. BN_print_fp(fp, c.get());
  942. if (a->neg != b->neg && !BN_is_zero(e.get())) {
  943. // If (a*b) % c is negative, c must be added
  944. // in order to obtain the normalized remainder
  945. // (new with OpenSSL 0.9.7, previous versions of
  946. // BN_mod_mul could generate negative results)
  947. puts_fp(fp, " + ");
  948. BN_print_fp(fp, c.get());
  949. }
  950. puts_fp(fp, " - ");
  951. BN_print_fp(fp, e.get());
  952. puts_fp(fp, "\n");
  953. }
  954. if (!BN_mul(d.get(), a.get(), b.get(), ctx) ||
  955. !BN_sub(d.get(), d.get(), e.get()) ||
  956. !BN_div(a.get(), b.get(), d.get(), c.get(), ctx)) {
  957. return false;
  958. }
  959. if (!BN_is_zero(b.get())) {
  960. fprintf(stderr, "Modulo multiply test failed!\n");
  961. ERR_print_errors_fp(stderr);
  962. return false;
  963. }
  964. }
  965. }
  966. return true;
  967. }
  968. static bool test_mod_exp(FILE *fp, BN_CTX *ctx) {
  969. ScopedBIGNUM a(BN_new());
  970. ScopedBIGNUM b(BN_new());
  971. ScopedBIGNUM c(BN_new());
  972. ScopedBIGNUM d(BN_new());
  973. ScopedBIGNUM e(BN_new());
  974. if (!a || !b || !c || !d || !e) {
  975. return false;
  976. }
  977. if (!BN_one(a.get()) || !BN_one(b.get())) {
  978. return false;
  979. }
  980. BN_zero(c.get());
  981. if (BN_mod_exp(d.get(), a.get(), b.get(), c.get(), ctx)) {
  982. fprintf(stderr, "BN_mod_exp with zero modulus succeeded!\n");
  983. return 0;
  984. }
  985. ERR_clear_error();
  986. if (!BN_rand(c.get(), 30, 0, 1)) { // must be odd for montgomery
  987. return false;
  988. }
  989. for (int i = 0; i < num2; i++) {
  990. if (!BN_rand(a.get(), 20 + i * 5, 0, 0) ||
  991. !BN_rand(b.get(), 2 + i, 0, 0) ||
  992. !BN_mod_exp(d.get(), a.get(), b.get(), c.get(), ctx)) {
  993. return false;
  994. }
  995. if (fp != NULL) {
  996. BN_print_fp(fp, a.get());
  997. puts_fp(fp, " ^ ");
  998. BN_print_fp(fp, b.get());
  999. puts_fp(fp, " % ");
  1000. BN_print_fp(fp, c.get());
  1001. puts_fp(fp, " - ");
  1002. BN_print_fp(fp, d.get());
  1003. puts_fp(fp, "\n");
  1004. }
  1005. if (!BN_exp(e.get(), a.get(), b.get(), ctx) ||
  1006. !BN_sub(e.get(), e.get(), d.get()) ||
  1007. !BN_div(a.get(), b.get(), e.get(), c.get(), ctx)) {
  1008. return false;
  1009. }
  1010. if (!BN_is_zero(b.get())) {
  1011. fprintf(stderr, "Modulo exponentiation test failed!\n");
  1012. return false;
  1013. }
  1014. }
  1015. return true;
  1016. }
  1017. static bool test_mod_exp_mont_consttime(FILE *fp, BN_CTX *ctx) {
  1018. ScopedBIGNUM a(BN_new());
  1019. ScopedBIGNUM b(BN_new());
  1020. ScopedBIGNUM c(BN_new());
  1021. ScopedBIGNUM d(BN_new());
  1022. ScopedBIGNUM e(BN_new());
  1023. if (!a || !b || !c || !d || !e) {
  1024. return false;
  1025. }
  1026. if (!BN_one(a.get()) || !BN_one(b.get())) {
  1027. return false;
  1028. }
  1029. BN_zero(c.get());
  1030. if (BN_mod_exp_mont_consttime(d.get(), a.get(), b.get(), c.get(), ctx,
  1031. nullptr)) {
  1032. fprintf(stderr, "BN_mod_exp_mont_consttime with zero modulus succeeded!\n");
  1033. return 0;
  1034. }
  1035. ERR_clear_error();
  1036. if (!BN_set_word(c.get(), 16)) {
  1037. return false;
  1038. }
  1039. if (BN_mod_exp_mont_consttime(d.get(), a.get(), b.get(), c.get(), ctx,
  1040. nullptr)) {
  1041. fprintf(stderr, "BN_mod_exp_mont_consttime with even modulus succeeded!\n");
  1042. return 0;
  1043. }
  1044. ERR_clear_error();
  1045. if (!BN_rand(c.get(), 30, 0, 1)) { // must be odd for montgomery
  1046. return false;
  1047. }
  1048. for (int i = 0; i < num2; i++) {
  1049. if (!BN_rand(a.get(), 20 + i * 5, 0, 0) ||
  1050. !BN_rand(b.get(), 2 + i, 0, 0) ||
  1051. !BN_mod_exp_mont_consttime(d.get(), a.get(), b.get(), c.get(), ctx,
  1052. NULL)) {
  1053. return false;
  1054. }
  1055. if (fp != NULL) {
  1056. BN_print_fp(fp, a.get());
  1057. puts_fp(fp, " ^ ");
  1058. BN_print_fp(fp, b.get());
  1059. puts_fp(fp, " % ");
  1060. BN_print_fp(fp, c.get());
  1061. puts_fp(fp, " - ");
  1062. BN_print_fp(fp, d.get());
  1063. puts_fp(fp, "\n");
  1064. }
  1065. if (!BN_exp(e.get(), a.get(), b.get(), ctx) ||
  1066. !BN_sub(e.get(), e.get(), d.get()) ||
  1067. !BN_div(a.get(), b.get(), e.get(), c.get(), ctx)) {
  1068. return false;
  1069. }
  1070. if (!BN_is_zero(b.get())) {
  1071. fprintf(stderr, "Modulo exponentiation test failed!\n");
  1072. return false;
  1073. }
  1074. }
  1075. return true;
  1076. }
  1077. // Test constant-time modular exponentiation with 1024-bit inputs,
  1078. // which on x86_64 cause a different code branch to be taken.
  1079. static bool test_mod_exp_mont5(FILE *fp, BN_CTX *ctx) {
  1080. ScopedBIGNUM a(BN_new());
  1081. ScopedBIGNUM p(BN_new());
  1082. ScopedBIGNUM m(BN_new());
  1083. ScopedBIGNUM d(BN_new());
  1084. ScopedBIGNUM e(BN_new());
  1085. if (!a || !p || !m || !d || !e ||
  1086. !BN_rand(m.get(), 1024, 0, 1) || // must be odd for montgomery
  1087. !BN_rand(a.get(), 1024, 0, 0)) {
  1088. return false;
  1089. }
  1090. // Zero exponent.
  1091. BN_zero(p.get());
  1092. if (!BN_mod_exp_mont_consttime(d.get(), a.get(), p.get(), m.get(), ctx,
  1093. NULL)) {
  1094. return false;
  1095. }
  1096. if (!BN_is_one(d.get())) {
  1097. fprintf(stderr, "Modular exponentiation test failed!\n");
  1098. return false;
  1099. }
  1100. if (!BN_rand(p.get(), 1024, 0, 0)) {
  1101. return false;
  1102. }
  1103. // Zero input.
  1104. BN_zero(a.get());
  1105. if (!BN_mod_exp_mont_consttime(d.get(), a.get(), p.get(), m.get(), ctx,
  1106. NULL)) {
  1107. return false;
  1108. }
  1109. if (!BN_is_zero(d.get())) {
  1110. fprintf(stderr, "Modular exponentiation test failed!\n");
  1111. return false;
  1112. }
  1113. // Craft an input whose Montgomery representation is 1, i.e., shorter than the
  1114. // modulus m, in order to test the const time precomputation
  1115. // scattering/gathering.
  1116. ScopedBN_MONT_CTX mont(BN_MONT_CTX_new());
  1117. if (!mont || !BN_one(a.get()) ||
  1118. !BN_MONT_CTX_set(mont.get(), m.get(), ctx) ||
  1119. !BN_from_montgomery(e.get(), a.get(), mont.get(), ctx) ||
  1120. !BN_mod_exp_mont_consttime(d.get(), e.get(), p.get(), m.get(), ctx,
  1121. NULL) ||
  1122. !BN_mod_exp(a.get(), e.get(), p.get(), m.get(), ctx)) {
  1123. return false;
  1124. }
  1125. if (BN_cmp(a.get(), d.get()) != 0) {
  1126. fprintf(stderr, "Modular exponentiation test failed!\n");
  1127. return false;
  1128. }
  1129. // Finally, some regular test vectors.
  1130. if (!BN_rand(e.get(), 1024, 0, 0) ||
  1131. !BN_mod_exp_mont_consttime(d.get(), e.get(), p.get(), m.get(), ctx,
  1132. NULL) ||
  1133. !BN_mod_exp(a.get(), e.get(), p.get(), m.get(), ctx)) {
  1134. return false;
  1135. }
  1136. if (BN_cmp(a.get(), d.get()) != 0) {
  1137. fprintf(stderr, "Modular exponentiation test failed!\n");
  1138. return false;
  1139. }
  1140. return true;
  1141. }
  1142. static bool test_exp(FILE *fp, BN_CTX *ctx) {
  1143. ScopedBIGNUM a(BN_new());
  1144. ScopedBIGNUM b(BN_new());
  1145. ScopedBIGNUM d(BN_new());
  1146. ScopedBIGNUM e(BN_new());
  1147. if (!a || !b || !d || !e) {
  1148. return false;
  1149. }
  1150. for (int i = 0; i < num2; i++) {
  1151. if (!BN_rand(a.get(), 20 + i * 5, 0, 0) ||
  1152. !BN_rand(b.get(), 2 + i, 0, 0) ||
  1153. !BN_exp(d.get(), a.get(), b.get(), ctx)) {
  1154. return false;
  1155. }
  1156. if (fp != NULL) {
  1157. BN_print_fp(fp, a.get());
  1158. puts_fp(fp, " ^ ");
  1159. BN_print_fp(fp, b.get());
  1160. puts_fp(fp, " - ");
  1161. BN_print_fp(fp, d.get());
  1162. puts_fp(fp, "\n");
  1163. }
  1164. if (!BN_one(e.get())) {
  1165. return false;
  1166. }
  1167. while (!BN_is_zero(b.get())) {
  1168. if (!BN_mul(e.get(), e.get(), a.get(), ctx) ||
  1169. !BN_sub(b.get(), b.get(), BN_value_one())) {
  1170. return false;
  1171. }
  1172. }
  1173. if (!BN_sub(e.get(), e.get(), d.get())) {
  1174. return false;
  1175. }
  1176. if (!BN_is_zero(e.get())) {
  1177. fprintf(stderr, "Exponentiation test failed!\n");
  1178. return false;
  1179. }
  1180. }
  1181. return true;
  1182. }
  1183. // test_exp_mod_zero tests that 1**0 mod 1 == 0.
  1184. static bool test_exp_mod_zero(void) {
  1185. ScopedBIGNUM zero(BN_new());
  1186. if (!zero) {
  1187. return false;
  1188. }
  1189. BN_zero(zero.get());
  1190. ScopedBN_CTX ctx(BN_CTX_new());
  1191. ScopedBIGNUM r(BN_new());
  1192. if (!ctx || !r ||
  1193. !BN_mod_exp(r.get(), BN_value_one(), zero.get(), BN_value_one(), ctx.get())) {
  1194. return false;
  1195. }
  1196. if (!BN_is_zero(r.get())) {
  1197. fprintf(stderr, "1**0 mod 1 = ");
  1198. BN_print_fp(stderr, r.get());
  1199. fprintf(stderr, ", should be 0\n");
  1200. return false;
  1201. }
  1202. return true;
  1203. }
  1204. static bool test_mod_sqrt(FILE *fp, BN_CTX *ctx) {
  1205. ScopedBIGNUM a(BN_new());
  1206. ScopedBIGNUM p(BN_new());
  1207. ScopedBIGNUM r(BN_new());
  1208. if (!a || !p || !r) {
  1209. return false;
  1210. }
  1211. for (int i = 0; i < 16; i++) {
  1212. if (i < 8) {
  1213. const unsigned kPrimes[8] = {2, 3, 5, 7, 11, 13, 17, 19};
  1214. if (!BN_set_word(p.get(), kPrimes[i])) {
  1215. return false;
  1216. }
  1217. } else {
  1218. if (!BN_set_word(a.get(), 32) ||
  1219. !BN_set_word(r.get(), 2 * i + 1) ||
  1220. !BN_generate_prime_ex(p.get(), 256, 0, a.get(), r.get(), nullptr)) {
  1221. return false;
  1222. }
  1223. }
  1224. p->neg = rand_neg();
  1225. for (int j = 0; j < num2; j++) {
  1226. // construct 'a' such that it is a square modulo p, but in general not a
  1227. // proper square and not reduced modulo p
  1228. if (!BN_rand(r.get(), 256, 0, 3) ||
  1229. !BN_nnmod(r.get(), r.get(), p.get(), ctx) ||
  1230. !BN_mod_sqr(r.get(), r.get(), p.get(), ctx) ||
  1231. !BN_rand(a.get(), 256, 0, 3) ||
  1232. !BN_nnmod(a.get(), a.get(), p.get(), ctx) ||
  1233. !BN_mod_sqr(a.get(), a.get(), p.get(), ctx) ||
  1234. !BN_mul(a.get(), a.get(), r.get(), ctx)) {
  1235. return false;
  1236. }
  1237. if (rand_neg() && !BN_sub(a.get(), a.get(), p.get())) {
  1238. return false;
  1239. }
  1240. if (!BN_mod_sqrt(r.get(), a.get(), p.get(), ctx) ||
  1241. !BN_mod_sqr(r.get(), r.get(), p.get(), ctx) ||
  1242. !BN_nnmod(a.get(), a.get(), p.get(), ctx)) {
  1243. return false;
  1244. }
  1245. if (BN_cmp(a.get(), r.get()) != 0) {
  1246. fprintf(stderr, "BN_mod_sqrt failed: a = ");
  1247. BN_print_fp(stderr, a.get());
  1248. fprintf(stderr, ", r = ");
  1249. BN_print_fp(stderr, r.get());
  1250. fprintf(stderr, ", p = ");
  1251. BN_print_fp(stderr, p.get());
  1252. fprintf(stderr, "\n");
  1253. return false;
  1254. }
  1255. }
  1256. }
  1257. return true;
  1258. }
  1259. static bool test_small_prime(FILE *fp, BN_CTX *ctx) {
  1260. static const unsigned kBits = 10;
  1261. ScopedBIGNUM r(BN_new());
  1262. if (!r || !BN_generate_prime_ex(r.get(), static_cast<int>(kBits), 0, NULL,
  1263. NULL, NULL)) {
  1264. return false;
  1265. }
  1266. if (BN_num_bits(r.get()) != kBits) {
  1267. fprintf(fp, "Expected %u bit prime, got %u bit number\n", kBits,
  1268. BN_num_bits(r.get()));
  1269. return false;
  1270. }
  1271. return true;
  1272. }
  1273. static bool test_sqrt(FILE *fp, BN_CTX *ctx) {
  1274. ScopedBIGNUM n(BN_new());
  1275. ScopedBIGNUM nn(BN_new());
  1276. ScopedBIGNUM sqrt(BN_new());
  1277. if (!n || !nn || !sqrt) {
  1278. return false;
  1279. }
  1280. // Test some random squares.
  1281. for (int i = 0; i < 100; i++) {
  1282. if (!BN_rand(n.get(), 1024 /* bit length */,
  1283. -1 /* no modification of top bits */,
  1284. 0 /* don't modify bottom bit */) ||
  1285. !BN_mul(nn.get(), n.get(), n.get(), ctx) ||
  1286. !BN_sqrt(sqrt.get(), nn.get(), ctx)) {
  1287. ERR_print_errors_fp(stderr);
  1288. return false;
  1289. }
  1290. if (BN_cmp(n.get(), sqrt.get()) != 0) {
  1291. fprintf(stderr, "Bad result from BN_sqrt.\n");
  1292. return false;
  1293. }
  1294. }
  1295. // Test some non-squares.
  1296. for (int i = 0; i < 100; i++) {
  1297. if (!BN_rand(n.get(), 1024 /* bit length */,
  1298. -1 /* no modification of top bits */,
  1299. 0 /* don't modify bottom bit */) ||
  1300. !BN_mul(nn.get(), n.get(), n.get(), ctx) ||
  1301. !BN_add(nn.get(), nn.get(), BN_value_one())) {
  1302. ERR_print_errors_fp(stderr);
  1303. return false;
  1304. }
  1305. if (BN_sqrt(sqrt.get(), nn.get(), ctx)) {
  1306. char *nn_str = BN_bn2dec(nn.get());
  1307. fprintf(stderr, "BIO_sqrt didn't fail on a non-square: %s\n", nn_str);
  1308. OPENSSL_free(nn_str);
  1309. }
  1310. }
  1311. return true;
  1312. }
  1313. static bool test_bn2bin_padded(BN_CTX *ctx) {
  1314. uint8_t zeros[256], out[256], reference[128];
  1315. memset(zeros, 0, sizeof(zeros));
  1316. // Test edge case at 0.
  1317. ScopedBIGNUM n(BN_new());
  1318. if (!n || !BN_bn2bin_padded(NULL, 0, n.get())) {
  1319. fprintf(stderr,
  1320. "BN_bn2bin_padded failed to encode 0 in an empty buffer.\n");
  1321. return false;
  1322. }
  1323. memset(out, -1, sizeof(out));
  1324. if (!BN_bn2bin_padded(out, sizeof(out), n.get())) {
  1325. fprintf(stderr,
  1326. "BN_bn2bin_padded failed to encode 0 in a non-empty buffer.\n");
  1327. return false;
  1328. }
  1329. if (memcmp(zeros, out, sizeof(out))) {
  1330. fprintf(stderr, "BN_bn2bin_padded did not zero buffer.\n");
  1331. return false;
  1332. }
  1333. // Test a random numbers at various byte lengths.
  1334. for (size_t bytes = 128 - 7; bytes <= 128; bytes++) {
  1335. if (!BN_rand(n.get(), bytes * 8, 0 /* make sure top bit is 1 */,
  1336. 0 /* don't modify bottom bit */)) {
  1337. ERR_print_errors_fp(stderr);
  1338. return false;
  1339. }
  1340. if (BN_num_bytes(n.get()) != bytes ||
  1341. BN_bn2bin(n.get(), reference) != bytes) {
  1342. fprintf(stderr, "Bad result from BN_rand; bytes.\n");
  1343. return false;
  1344. }
  1345. // Empty buffer should fail.
  1346. if (BN_bn2bin_padded(NULL, 0, n.get())) {
  1347. fprintf(stderr,
  1348. "BN_bn2bin_padded incorrectly succeeded on empty buffer.\n");
  1349. return false;
  1350. }
  1351. // One byte short should fail.
  1352. if (BN_bn2bin_padded(out, bytes - 1, n.get())) {
  1353. fprintf(stderr, "BN_bn2bin_padded incorrectly succeeded on short.\n");
  1354. return false;
  1355. }
  1356. // Exactly right size should encode.
  1357. if (!BN_bn2bin_padded(out, bytes, n.get()) ||
  1358. memcmp(out, reference, bytes) != 0) {
  1359. fprintf(stderr, "BN_bn2bin_padded gave a bad result.\n");
  1360. return false;
  1361. }
  1362. // Pad up one byte extra.
  1363. if (!BN_bn2bin_padded(out, bytes + 1, n.get()) ||
  1364. memcmp(out + 1, reference, bytes) || memcmp(out, zeros, 1)) {
  1365. fprintf(stderr, "BN_bn2bin_padded gave a bad result.\n");
  1366. return false;
  1367. }
  1368. // Pad up to 256.
  1369. if (!BN_bn2bin_padded(out, sizeof(out), n.get()) ||
  1370. memcmp(out + sizeof(out) - bytes, reference, bytes) ||
  1371. memcmp(out, zeros, sizeof(out) - bytes)) {
  1372. fprintf(stderr, "BN_bn2bin_padded gave a bad result.\n");
  1373. return false;
  1374. }
  1375. }
  1376. return true;
  1377. }
  1378. static int DecimalToBIGNUM(ScopedBIGNUM *out, const char *in) {
  1379. BIGNUM *raw = NULL;
  1380. int ret = BN_dec2bn(&raw, in);
  1381. out->reset(raw);
  1382. return ret;
  1383. }
  1384. static bool test_dec2bn(BN_CTX *ctx) {
  1385. ScopedBIGNUM bn;
  1386. int ret = DecimalToBIGNUM(&bn, "0");
  1387. if (ret != 1 || !BN_is_zero(bn.get()) || BN_is_negative(bn.get())) {
  1388. fprintf(stderr, "BN_dec2bn gave a bad result.\n");
  1389. return false;
  1390. }
  1391. ret = DecimalToBIGNUM(&bn, "256");
  1392. if (ret != 3 || !BN_is_word(bn.get(), 256) || BN_is_negative(bn.get())) {
  1393. fprintf(stderr, "BN_dec2bn gave a bad result.\n");
  1394. return false;
  1395. }
  1396. ret = DecimalToBIGNUM(&bn, "-42");
  1397. if (ret != 3 || !BN_abs_is_word(bn.get(), 42) || !BN_is_negative(bn.get())) {
  1398. fprintf(stderr, "BN_dec2bn gave a bad result.\n");
  1399. return false;
  1400. }
  1401. ret = DecimalToBIGNUM(&bn, "-0");
  1402. if (ret != 2 || !BN_is_zero(bn.get()) || BN_is_negative(bn.get())) {
  1403. fprintf(stderr, "BN_dec2bn gave a bad result.\n");
  1404. return false;
  1405. }
  1406. ret = DecimalToBIGNUM(&bn, "42trailing garbage is ignored");
  1407. if (ret != 2 || !BN_abs_is_word(bn.get(), 42) || BN_is_negative(bn.get())) {
  1408. fprintf(stderr, "BN_dec2bn gave a bad result.\n");
  1409. return false;
  1410. }
  1411. return true;
  1412. }
  1413. static int HexToBIGNUM(ScopedBIGNUM *out, const char *in) {
  1414. BIGNUM *raw = NULL;
  1415. int ret = BN_hex2bn(&raw, in);
  1416. out->reset(raw);
  1417. return ret;
  1418. }
  1419. static bool test_hex2bn(BN_CTX *ctx) {
  1420. ScopedBIGNUM bn;
  1421. int ret = HexToBIGNUM(&bn, "0");
  1422. if (ret != 1 || !BN_is_zero(bn.get()) || BN_is_negative(bn.get())) {
  1423. fprintf(stderr, "BN_hex2bn gave a bad result.\n");
  1424. return false;
  1425. }
  1426. ret = HexToBIGNUM(&bn, "256");
  1427. if (ret != 3 || !BN_is_word(bn.get(), 0x256) || BN_is_negative(bn.get())) {
  1428. fprintf(stderr, "BN_hex2bn gave a bad result.\n");
  1429. return false;
  1430. }
  1431. ret = HexToBIGNUM(&bn, "-42");
  1432. if (ret != 3 || !BN_abs_is_word(bn.get(), 0x42) || !BN_is_negative(bn.get())) {
  1433. fprintf(stderr, "BN_hex2bn gave a bad result.\n");
  1434. return false;
  1435. }
  1436. ret = HexToBIGNUM(&bn, "-0");
  1437. if (ret != 2 || !BN_is_zero(bn.get()) || BN_is_negative(bn.get())) {
  1438. fprintf(stderr, "BN_hex2bn gave a bad result.\n");
  1439. return false;
  1440. }
  1441. ret = HexToBIGNUM(&bn, "abctrailing garbage is ignored");
  1442. if (ret != 3 || !BN_is_word(bn.get(), 0xabc) || BN_is_negative(bn.get())) {
  1443. fprintf(stderr, "BN_hex2bn gave a bad result.\n");
  1444. return false;
  1445. }
  1446. return true;
  1447. }
  1448. static ScopedBIGNUM ASCIIToBIGNUM(const char *in) {
  1449. BIGNUM *raw = NULL;
  1450. if (!BN_asc2bn(&raw, in)) {
  1451. return nullptr;
  1452. }
  1453. return ScopedBIGNUM(raw);
  1454. }
  1455. static bool test_asc2bn(BN_CTX *ctx) {
  1456. ScopedBIGNUM bn = ASCIIToBIGNUM("0");
  1457. if (!bn || !BN_is_zero(bn.get()) || BN_is_negative(bn.get())) {
  1458. fprintf(stderr, "BN_asc2bn gave a bad result.\n");
  1459. return false;
  1460. }
  1461. bn = ASCIIToBIGNUM("256");
  1462. if (!bn || !BN_is_word(bn.get(), 256) || BN_is_negative(bn.get())) {
  1463. fprintf(stderr, "BN_asc2bn gave a bad result.\n");
  1464. return false;
  1465. }
  1466. bn = ASCIIToBIGNUM("-42");
  1467. if (!bn || !BN_abs_is_word(bn.get(), 42) || !BN_is_negative(bn.get())) {
  1468. fprintf(stderr, "BN_asc2bn gave a bad result.\n");
  1469. return false;
  1470. }
  1471. bn = ASCIIToBIGNUM("0x1234");
  1472. if (!bn || !BN_is_word(bn.get(), 0x1234) || BN_is_negative(bn.get())) {
  1473. fprintf(stderr, "BN_asc2bn gave a bad result.\n");
  1474. return false;
  1475. }
  1476. bn = ASCIIToBIGNUM("0X1234");
  1477. if (!bn || !BN_is_word(bn.get(), 0x1234) || BN_is_negative(bn.get())) {
  1478. fprintf(stderr, "BN_asc2bn gave a bad result.\n");
  1479. return false;
  1480. }
  1481. bn = ASCIIToBIGNUM("-0xabcd");
  1482. if (!bn || !BN_abs_is_word(bn.get(), 0xabcd) || !BN_is_negative(bn.get())) {
  1483. fprintf(stderr, "BN_asc2bn gave a bad result.\n");
  1484. return false;
  1485. }
  1486. bn = ASCIIToBIGNUM("-0");
  1487. if (!bn || !BN_is_zero(bn.get()) || BN_is_negative(bn.get())) {
  1488. fprintf(stderr, "BN_asc2bn gave a bad result.\n");
  1489. return false;
  1490. }
  1491. bn = ASCIIToBIGNUM("123trailing garbage is ignored");
  1492. if (!bn || !BN_is_word(bn.get(), 123) || BN_is_negative(bn.get())) {
  1493. fprintf(stderr, "BN_asc2bn gave a bad result.\n");
  1494. return false;
  1495. }
  1496. return true;
  1497. }
  1498. struct MPITest {
  1499. const char *base10;
  1500. const char *mpi;
  1501. size_t mpi_len;
  1502. };
  1503. static const MPITest kMPITests[] = {
  1504. { "0", "\x00\x00\x00\x00", 4 },
  1505. { "1", "\x00\x00\x00\x01\x01", 5 },
  1506. { "-1", "\x00\x00\x00\x01\x81", 5 },
  1507. { "128", "\x00\x00\x00\x02\x00\x80", 6 },
  1508. { "256", "\x00\x00\x00\x02\x01\x00", 6 },
  1509. { "-256", "\x00\x00\x00\x02\x81\x00", 6 },
  1510. };
  1511. static bool test_mpi() {
  1512. uint8_t scratch[8];
  1513. for (size_t i = 0; i < sizeof(kMPITests) / sizeof(kMPITests[0]); i++) {
  1514. const MPITest &test = kMPITests[i];
  1515. ScopedBIGNUM bn(ASCIIToBIGNUM(test.base10));
  1516. const size_t mpi_len = BN_bn2mpi(bn.get(), NULL);
  1517. if (mpi_len > sizeof(scratch)) {
  1518. fprintf(stderr, "MPI test #%u: MPI size is too large to test.\n",
  1519. (unsigned)i);
  1520. return false;
  1521. }
  1522. const size_t mpi_len2 = BN_bn2mpi(bn.get(), scratch);
  1523. if (mpi_len != mpi_len2) {
  1524. fprintf(stderr, "MPI test #%u: length changes.\n", (unsigned)i);
  1525. return false;
  1526. }
  1527. if (mpi_len != test.mpi_len ||
  1528. memcmp(test.mpi, scratch, mpi_len) != 0) {
  1529. fprintf(stderr, "MPI test #%u failed:\n", (unsigned)i);
  1530. hexdump(stderr, "Expected: ", test.mpi, test.mpi_len);
  1531. hexdump(stderr, "Got: ", scratch, mpi_len);
  1532. return false;
  1533. }
  1534. ScopedBIGNUM bn2(BN_mpi2bn(scratch, mpi_len, NULL));
  1535. if (bn2.get() == nullptr) {
  1536. fprintf(stderr, "MPI test #%u: failed to parse\n", (unsigned)i);
  1537. return false;
  1538. }
  1539. if (BN_cmp(bn.get(), bn2.get()) != 0) {
  1540. fprintf(stderr, "MPI test #%u: wrong result\n", (unsigned)i);
  1541. return false;
  1542. }
  1543. }
  1544. return true;
  1545. }
  1546. static bool test_rand() {
  1547. ScopedBIGNUM bn(BN_new());
  1548. if (!bn) {
  1549. return false;
  1550. }
  1551. // Test BN_rand accounts for degenerate cases with |top| and |bottom|
  1552. // parameters.
  1553. if (!BN_rand(bn.get(), 0, 0 /* top */, 0 /* bottom */) ||
  1554. !BN_is_zero(bn.get())) {
  1555. fprintf(stderr, "BN_rand gave a bad result.\n");
  1556. return false;
  1557. }
  1558. if (!BN_rand(bn.get(), 0, 1 /* top */, 1 /* bottom */) ||
  1559. !BN_is_zero(bn.get())) {
  1560. fprintf(stderr, "BN_rand gave a bad result.\n");
  1561. return false;
  1562. }
  1563. if (!BN_rand(bn.get(), 1, 0 /* top */, 0 /* bottom */) ||
  1564. !BN_is_word(bn.get(), 1)) {
  1565. fprintf(stderr, "BN_rand gave a bad result.\n");
  1566. return false;
  1567. }
  1568. if (!BN_rand(bn.get(), 1, 1 /* top */, 0 /* bottom */) ||
  1569. !BN_is_word(bn.get(), 1)) {
  1570. fprintf(stderr, "BN_rand gave a bad result.\n");
  1571. return false;
  1572. }
  1573. if (!BN_rand(bn.get(), 1, -1 /* top */, 1 /* bottom */) ||
  1574. !BN_is_word(bn.get(), 1)) {
  1575. fprintf(stderr, "BN_rand gave a bad result.\n");
  1576. return false;
  1577. }
  1578. if (!BN_rand(bn.get(), 2, 1 /* top */, 0 /* bottom */) ||
  1579. !BN_is_word(bn.get(), 3)) {
  1580. fprintf(stderr, "BN_rand gave a bad result.\n");
  1581. return false;
  1582. }
  1583. return true;
  1584. }
  1585. struct ASN1Test {
  1586. const char *value_ascii;
  1587. const char *der;
  1588. size_t der_len;
  1589. };
  1590. static const ASN1Test kASN1Tests[] = {
  1591. {"0", "\x02\x01\x00", 3},
  1592. {"1", "\x02\x01\x01", 3},
  1593. {"127", "\x02\x01\x7f", 3},
  1594. {"128", "\x02\x02\x00\x80", 4},
  1595. {"0xdeadbeef", "\x02\x05\x00\xde\xad\xbe\xef", 7},
  1596. {"0x0102030405060708",
  1597. "\x02\x08\x01\x02\x03\x04\x05\x06\x07\x08", 10},
  1598. {"0xffffffffffffffff",
  1599. "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff", 11},
  1600. };
  1601. struct ASN1InvalidTest {
  1602. const char *der;
  1603. size_t der_len;
  1604. };
  1605. static const ASN1InvalidTest kASN1InvalidTests[] = {
  1606. // Bad tag.
  1607. {"\x03\x01\x00", 3},
  1608. // Empty contents.
  1609. {"\x02\x00", 2},
  1610. };
  1611. // kASN1BuggyTests are incorrect encodings and how |BN_cbs2unsigned_buggy|
  1612. // should interpret them.
  1613. static const ASN1Test kASN1BuggyTests[] = {
  1614. // Negative numbers.
  1615. {"128", "\x02\x01\x80", 3},
  1616. {"255", "\x02\x01\xff", 3},
  1617. // Unnecessary leading zeros.
  1618. {"1", "\x02\x02\x00\x01", 4},
  1619. };
  1620. static bool test_asn1() {
  1621. for (const ASN1Test &test : kASN1Tests) {
  1622. ScopedBIGNUM bn = ASCIIToBIGNUM(test.value_ascii);
  1623. if (!bn) {
  1624. return false;
  1625. }
  1626. // Test that the input is correctly parsed.
  1627. ScopedBIGNUM bn2(BN_new());
  1628. if (!bn2) {
  1629. return false;
  1630. }
  1631. CBS cbs;
  1632. CBS_init(&cbs, reinterpret_cast<const uint8_t*>(test.der), test.der_len);
  1633. if (!BN_cbs2unsigned(&cbs, bn2.get()) || CBS_len(&cbs) != 0) {
  1634. fprintf(stderr, "Parsing ASN.1 INTEGER failed.\n");
  1635. return false;
  1636. }
  1637. if (BN_cmp(bn.get(), bn2.get()) != 0) {
  1638. fprintf(stderr, "Bad parse.\n");
  1639. return false;
  1640. }
  1641. // Test the value serializes correctly.
  1642. CBB cbb;
  1643. uint8_t *der;
  1644. size_t der_len;
  1645. CBB_zero(&cbb);
  1646. if (!CBB_init(&cbb, 0) ||
  1647. !BN_bn2cbb(&cbb, bn.get()) ||
  1648. !CBB_finish(&cbb, &der, &der_len)) {
  1649. CBB_cleanup(&cbb);
  1650. return false;
  1651. }
  1652. ScopedOpenSSLBytes delete_der(der);
  1653. if (der_len != test.der_len ||
  1654. memcmp(der, reinterpret_cast<const uint8_t*>(test.der), der_len) != 0) {
  1655. fprintf(stderr, "Bad serialization.\n");
  1656. return false;
  1657. }
  1658. // |BN_cbs2unsigned_buggy| parses all valid input.
  1659. CBS_init(&cbs, reinterpret_cast<const uint8_t*>(test.der), test.der_len);
  1660. if (!BN_cbs2unsigned_buggy(&cbs, bn2.get()) || CBS_len(&cbs) != 0) {
  1661. fprintf(stderr, "Parsing ASN.1 INTEGER failed.\n");
  1662. return false;
  1663. }
  1664. if (BN_cmp(bn.get(), bn2.get()) != 0) {
  1665. fprintf(stderr, "Bad parse.\n");
  1666. return false;
  1667. }
  1668. }
  1669. for (const ASN1InvalidTest &test : kASN1InvalidTests) {
  1670. ScopedBIGNUM bn(BN_new());
  1671. if (!bn) {
  1672. return false;
  1673. }
  1674. CBS cbs;
  1675. CBS_init(&cbs, reinterpret_cast<const uint8_t*>(test.der), test.der_len);
  1676. if (BN_cbs2unsigned(&cbs, bn.get())) {
  1677. fprintf(stderr, "Parsed invalid input.\n");
  1678. return false;
  1679. }
  1680. ERR_clear_error();
  1681. // All tests in kASN1InvalidTests are also rejected by
  1682. // |BN_cbs2unsigned_buggy|.
  1683. CBS_init(&cbs, reinterpret_cast<const uint8_t*>(test.der), test.der_len);
  1684. if (BN_cbs2unsigned_buggy(&cbs, bn.get())) {
  1685. fprintf(stderr, "Parsed invalid input.\n");
  1686. return false;
  1687. }
  1688. ERR_clear_error();
  1689. }
  1690. for (const ASN1Test &test : kASN1BuggyTests) {
  1691. // These broken encodings are rejected by |BN_cbs2unsigned|.
  1692. ScopedBIGNUM bn(BN_new());
  1693. if (!bn) {
  1694. return false;
  1695. }
  1696. CBS cbs;
  1697. CBS_init(&cbs, reinterpret_cast<const uint8_t*>(test.der), test.der_len);
  1698. if (BN_cbs2unsigned(&cbs, bn.get())) {
  1699. fprintf(stderr, "Parsed invalid input.\n");
  1700. return false;
  1701. }
  1702. ERR_clear_error();
  1703. // However |BN_cbs2unsigned_buggy| accepts them.
  1704. ScopedBIGNUM bn2 = ASCIIToBIGNUM(test.value_ascii);
  1705. if (!bn2) {
  1706. return false;
  1707. }
  1708. CBS_init(&cbs, reinterpret_cast<const uint8_t*>(test.der), test.der_len);
  1709. if (!BN_cbs2unsigned_buggy(&cbs, bn.get()) || CBS_len(&cbs) != 0) {
  1710. fprintf(stderr, "Parsing (invalid) ASN.1 INTEGER failed.\n");
  1711. return false;
  1712. }
  1713. if (BN_cmp(bn.get(), bn2.get()) != 0) {
  1714. fprintf(stderr, "\"Bad\" parse.\n");
  1715. return false;
  1716. }
  1717. }
  1718. // Serializing negative numbers is not supported.
  1719. ScopedBIGNUM bn = ASCIIToBIGNUM("-1");
  1720. if (!bn) {
  1721. return false;
  1722. }
  1723. CBB cbb;
  1724. CBB_zero(&cbb);
  1725. if (!CBB_init(&cbb, 0) ||
  1726. BN_bn2cbb(&cbb, bn.get())) {
  1727. fprintf(stderr, "Serialized negative number.\n");
  1728. CBB_cleanup(&cbb);
  1729. return false;
  1730. }
  1731. CBB_cleanup(&cbb);
  1732. return true;
  1733. }