Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

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