Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

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