您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

1661 行
65 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. /* Per C99, various stdint.h and inttypes.h macros (the latter used by bn.h) are
  70. * unavailable in C++ unless some macros are defined. C++11 overruled this
  71. * decision, but older Android NDKs still require it. */
  72. #if !defined(__STDC_CONSTANT_MACROS)
  73. #define __STDC_CONSTANT_MACROS
  74. #endif
  75. #if !defined(__STDC_FORMAT_MACROS)
  76. #define __STDC_FORMAT_MACROS
  77. #endif
  78. #include <assert.h>
  79. #include <errno.h>
  80. #include <limits.h>
  81. #include <stdio.h>
  82. #include <string.h>
  83. #include <utility>
  84. #include <gtest/gtest.h>
  85. #include <openssl/bn.h>
  86. #include <openssl/bytestring.h>
  87. #include <openssl/crypto.h>
  88. #include <openssl/err.h>
  89. #include <openssl/mem.h>
  90. #include "../../internal.h"
  91. #include "../../test/file_test.h"
  92. #include "../../test/test_util.h"
  93. static int HexToBIGNUM(bssl::UniquePtr<BIGNUM> *out, const char *in) {
  94. BIGNUM *raw = NULL;
  95. int ret = BN_hex2bn(&raw, in);
  96. out->reset(raw);
  97. return ret;
  98. }
  99. static bssl::UniquePtr<BIGNUM> GetBIGNUM(FileTest *t, const char *attribute) {
  100. std::string hex;
  101. if (!t->GetAttribute(&hex, attribute)) {
  102. return nullptr;
  103. }
  104. bssl::UniquePtr<BIGNUM> ret;
  105. if (HexToBIGNUM(&ret, hex.c_str()) != static_cast<int>(hex.size())) {
  106. t->PrintLine("Could not decode '%s'.", hex.c_str());
  107. return nullptr;
  108. }
  109. return ret;
  110. }
  111. static bool GetInt(FileTest *t, int *out, const char *attribute) {
  112. bssl::UniquePtr<BIGNUM> ret = GetBIGNUM(t, attribute);
  113. if (!ret) {
  114. return false;
  115. }
  116. BN_ULONG word = BN_get_word(ret.get());
  117. if (word > INT_MAX) {
  118. return false;
  119. }
  120. *out = static_cast<int>(word);
  121. return true;
  122. }
  123. static testing::AssertionResult AssertBIGNUMSEqual(
  124. const char *operation_expr, const char *expected_expr,
  125. const char *actual_expr, const char *operation, const BIGNUM *expected,
  126. const BIGNUM *actual) {
  127. if (BN_cmp(expected, actual) == 0) {
  128. return testing::AssertionSuccess();
  129. }
  130. bssl::UniquePtr<char> expected_str(BN_bn2hex(expected));
  131. bssl::UniquePtr<char> actual_str(BN_bn2hex(actual));
  132. if (!expected_str || !actual_str) {
  133. return testing::AssertionFailure() << "Error converting BIGNUMs to hex";
  134. }
  135. return testing::AssertionFailure()
  136. << "Wrong value for " << operation
  137. << "\nActual: " << actual_str.get() << " (" << actual_expr
  138. << ")\nExpected: " << expected_str.get() << " (" << expected_expr
  139. << ")";
  140. }
  141. #define EXPECT_BIGNUMS_EQUAL(op, a, b) \
  142. EXPECT_PRED_FORMAT3(AssertBIGNUMSEqual, op, a, b)
  143. static void TestSum(FileTest *t, BN_CTX *ctx) {
  144. bssl::UniquePtr<BIGNUM> a = GetBIGNUM(t, "A");
  145. bssl::UniquePtr<BIGNUM> b = GetBIGNUM(t, "B");
  146. bssl::UniquePtr<BIGNUM> sum = GetBIGNUM(t, "Sum");
  147. ASSERT_TRUE(a);
  148. ASSERT_TRUE(b);
  149. ASSERT_TRUE(sum);
  150. bssl::UniquePtr<BIGNUM> ret(BN_new());
  151. ASSERT_TRUE(ret);
  152. ASSERT_TRUE(BN_add(ret.get(), a.get(), b.get()));
  153. EXPECT_BIGNUMS_EQUAL("A + B", sum.get(), ret.get());
  154. ASSERT_TRUE(BN_sub(ret.get(), sum.get(), a.get()));
  155. EXPECT_BIGNUMS_EQUAL("Sum - A", b.get(), ret.get());
  156. ASSERT_TRUE(BN_sub(ret.get(), sum.get(), b.get()));
  157. EXPECT_BIGNUMS_EQUAL("Sum - B", a.get(), ret.get());
  158. // Test that the functions work when |r| and |a| point to the same |BIGNUM|,
  159. // or when |r| and |b| point to the same |BIGNUM|. TODO: Test the case where
  160. // all of |r|, |a|, and |b| point to the same |BIGNUM|.
  161. ASSERT_TRUE(BN_copy(ret.get(), a.get()));
  162. ASSERT_TRUE(BN_add(ret.get(), ret.get(), b.get()));
  163. EXPECT_BIGNUMS_EQUAL("A + B (r is a)", sum.get(), ret.get());
  164. ASSERT_TRUE(BN_copy(ret.get(), b.get()));
  165. ASSERT_TRUE(BN_add(ret.get(), a.get(), ret.get()));
  166. EXPECT_BIGNUMS_EQUAL("A + B (r is b)", sum.get(), ret.get());
  167. ASSERT_TRUE(BN_copy(ret.get(), sum.get()));
  168. ASSERT_TRUE(BN_sub(ret.get(), ret.get(), a.get()));
  169. EXPECT_BIGNUMS_EQUAL("Sum - A (r is a)", b.get(), ret.get());
  170. ASSERT_TRUE(BN_copy(ret.get(), a.get()));
  171. ASSERT_TRUE(BN_sub(ret.get(), sum.get(), ret.get()));
  172. EXPECT_BIGNUMS_EQUAL("Sum - A (r is b)", b.get(), ret.get());
  173. ASSERT_TRUE(BN_copy(ret.get(), sum.get()));
  174. ASSERT_TRUE(BN_sub(ret.get(), ret.get(), b.get()));
  175. EXPECT_BIGNUMS_EQUAL("Sum - B (r is a)", a.get(), ret.get());
  176. ASSERT_TRUE(BN_copy(ret.get(), b.get()));
  177. ASSERT_TRUE(BN_sub(ret.get(), sum.get(), ret.get()));
  178. EXPECT_BIGNUMS_EQUAL("Sum - B (r is b)", a.get(), ret.get());
  179. // Test |BN_uadd| and |BN_usub| with the prerequisites they are documented as
  180. // having. Note that these functions are frequently used when the
  181. // prerequisites don't hold. In those cases, they are supposed to work as if
  182. // the prerequisite hold, but we don't test that yet. TODO: test that.
  183. if (!BN_is_negative(a.get()) &&
  184. !BN_is_negative(b.get()) && BN_cmp(a.get(), b.get()) >= 0) {
  185. ASSERT_TRUE(BN_uadd(ret.get(), a.get(), b.get()));
  186. EXPECT_BIGNUMS_EQUAL("A +u B", sum.get(), ret.get());
  187. ASSERT_TRUE(BN_usub(ret.get(), sum.get(), a.get()));
  188. EXPECT_BIGNUMS_EQUAL("Sum -u A", b.get(), ret.get());
  189. ASSERT_TRUE(BN_usub(ret.get(), sum.get(), b.get()));
  190. EXPECT_BIGNUMS_EQUAL("Sum -u B", a.get(), ret.get());
  191. // Test that the functions work when |r| and |a| point to the same |BIGNUM|,
  192. // or when |r| and |b| point to the same |BIGNUM|. TODO: Test the case where
  193. // all of |r|, |a|, and |b| point to the same |BIGNUM|.
  194. ASSERT_TRUE(BN_copy(ret.get(), a.get()));
  195. ASSERT_TRUE(BN_uadd(ret.get(), ret.get(), b.get()));
  196. EXPECT_BIGNUMS_EQUAL("A +u B (r is a)", sum.get(), ret.get());
  197. ASSERT_TRUE(BN_copy(ret.get(), b.get()));
  198. ASSERT_TRUE(BN_uadd(ret.get(), a.get(), ret.get()));
  199. EXPECT_BIGNUMS_EQUAL("A +u B (r is b)", sum.get(), ret.get());
  200. ASSERT_TRUE(BN_copy(ret.get(), sum.get()));
  201. ASSERT_TRUE(BN_usub(ret.get(), ret.get(), a.get()));
  202. EXPECT_BIGNUMS_EQUAL("Sum -u A (r is a)", b.get(), ret.get());
  203. ASSERT_TRUE(BN_copy(ret.get(), a.get()));
  204. ASSERT_TRUE(BN_usub(ret.get(), sum.get(), ret.get()));
  205. EXPECT_BIGNUMS_EQUAL("Sum -u A (r is b)", b.get(), ret.get());
  206. ASSERT_TRUE(BN_copy(ret.get(), sum.get()));
  207. ASSERT_TRUE(BN_usub(ret.get(), ret.get(), b.get()));
  208. EXPECT_BIGNUMS_EQUAL("Sum -u B (r is a)", a.get(), ret.get());
  209. ASSERT_TRUE(BN_copy(ret.get(), b.get()));
  210. ASSERT_TRUE(BN_usub(ret.get(), sum.get(), ret.get()));
  211. EXPECT_BIGNUMS_EQUAL("Sum -u B (r is b)", a.get(), ret.get());
  212. }
  213. // Test with |BN_add_word| and |BN_sub_word| if |b| is small enough.
  214. BN_ULONG b_word = BN_get_word(b.get());
  215. if (!BN_is_negative(b.get()) && b_word != (BN_ULONG)-1) {
  216. ASSERT_TRUE(BN_copy(ret.get(), a.get()));
  217. ASSERT_TRUE(BN_add_word(ret.get(), b_word));
  218. EXPECT_BIGNUMS_EQUAL("A + B (word)", sum.get(), ret.get());
  219. ASSERT_TRUE(BN_copy(ret.get(), sum.get()));
  220. ASSERT_TRUE(BN_sub_word(ret.get(), b_word));
  221. EXPECT_BIGNUMS_EQUAL("Sum - B (word)", a.get(), ret.get());
  222. }
  223. }
  224. static void TestLShift1(FileTest *t, BN_CTX *ctx) {
  225. bssl::UniquePtr<BIGNUM> a = GetBIGNUM(t, "A");
  226. bssl::UniquePtr<BIGNUM> lshift1 = GetBIGNUM(t, "LShift1");
  227. bssl::UniquePtr<BIGNUM> zero(BN_new());
  228. ASSERT_TRUE(a);
  229. ASSERT_TRUE(lshift1);
  230. ASSERT_TRUE(zero);
  231. BN_zero(zero.get());
  232. bssl::UniquePtr<BIGNUM> ret(BN_new()), two(BN_new()), remainder(BN_new());
  233. ASSERT_TRUE(ret);
  234. ASSERT_TRUE(two);
  235. ASSERT_TRUE(remainder);
  236. ASSERT_TRUE(BN_set_word(two.get(), 2));
  237. ASSERT_TRUE(BN_add(ret.get(), a.get(), a.get()));
  238. EXPECT_BIGNUMS_EQUAL("A + A", lshift1.get(), ret.get());
  239. ASSERT_TRUE(BN_mul(ret.get(), a.get(), two.get(), ctx));
  240. EXPECT_BIGNUMS_EQUAL("A * 2", lshift1.get(), ret.get());
  241. ASSERT_TRUE(
  242. BN_div(ret.get(), remainder.get(), lshift1.get(), two.get(), ctx));
  243. EXPECT_BIGNUMS_EQUAL("LShift1 / 2", a.get(), ret.get());
  244. EXPECT_BIGNUMS_EQUAL("LShift1 % 2", zero.get(), remainder.get());
  245. ASSERT_TRUE(BN_lshift1(ret.get(), a.get()));
  246. EXPECT_BIGNUMS_EQUAL("A << 1", lshift1.get(), ret.get());
  247. ASSERT_TRUE(BN_rshift1(ret.get(), lshift1.get()));
  248. EXPECT_BIGNUMS_EQUAL("LShift >> 1", a.get(), ret.get());
  249. ASSERT_TRUE(BN_rshift1(ret.get(), lshift1.get()));
  250. EXPECT_BIGNUMS_EQUAL("LShift >> 1", a.get(), ret.get());
  251. // Set the LSB to 1 and test rshift1 again.
  252. ASSERT_TRUE(BN_set_bit(lshift1.get(), 0));
  253. ASSERT_TRUE(
  254. BN_div(ret.get(), nullptr /* rem */, lshift1.get(), two.get(), ctx));
  255. EXPECT_BIGNUMS_EQUAL("(LShift1 | 1) / 2", a.get(), ret.get());
  256. ASSERT_TRUE(BN_rshift1(ret.get(), lshift1.get()));
  257. EXPECT_BIGNUMS_EQUAL("(LShift | 1) >> 1", a.get(), ret.get());
  258. }
  259. static void TestLShift(FileTest *t, BN_CTX *ctx) {
  260. bssl::UniquePtr<BIGNUM> a = GetBIGNUM(t, "A");
  261. bssl::UniquePtr<BIGNUM> lshift = GetBIGNUM(t, "LShift");
  262. ASSERT_TRUE(a);
  263. ASSERT_TRUE(lshift);
  264. int n = 0;
  265. ASSERT_TRUE(GetInt(t, &n, "N"));
  266. bssl::UniquePtr<BIGNUM> ret(BN_new());
  267. ASSERT_TRUE(ret);
  268. ASSERT_TRUE(BN_lshift(ret.get(), a.get(), n));
  269. EXPECT_BIGNUMS_EQUAL("A << N", lshift.get(), ret.get());
  270. ASSERT_TRUE(BN_rshift(ret.get(), lshift.get(), n));
  271. EXPECT_BIGNUMS_EQUAL("A >> N", a.get(), ret.get());
  272. }
  273. static void TestRShift(FileTest *t, BN_CTX *ctx) {
  274. bssl::UniquePtr<BIGNUM> a = GetBIGNUM(t, "A");
  275. bssl::UniquePtr<BIGNUM> rshift = GetBIGNUM(t, "RShift");
  276. ASSERT_TRUE(a);
  277. ASSERT_TRUE(rshift);
  278. int n = 0;
  279. ASSERT_TRUE(GetInt(t, &n, "N"));
  280. bssl::UniquePtr<BIGNUM> ret(BN_new());
  281. ASSERT_TRUE(ret);
  282. ASSERT_TRUE(BN_rshift(ret.get(), a.get(), n));
  283. EXPECT_BIGNUMS_EQUAL("A >> N", rshift.get(), ret.get());
  284. }
  285. static void TestSquare(FileTest *t, BN_CTX *ctx) {
  286. bssl::UniquePtr<BIGNUM> a = GetBIGNUM(t, "A");
  287. bssl::UniquePtr<BIGNUM> square = GetBIGNUM(t, "Square");
  288. bssl::UniquePtr<BIGNUM> zero(BN_new());
  289. ASSERT_TRUE(a);
  290. ASSERT_TRUE(square);
  291. ASSERT_TRUE(zero);
  292. BN_zero(zero.get());
  293. bssl::UniquePtr<BIGNUM> ret(BN_new()), remainder(BN_new());
  294. ASSERT_TRUE(ret);
  295. ASSERT_TRUE(remainder);
  296. ASSERT_TRUE(BN_sqr(ret.get(), a.get(), ctx));
  297. EXPECT_BIGNUMS_EQUAL("A^2", square.get(), ret.get());
  298. ASSERT_TRUE(BN_mul(ret.get(), a.get(), a.get(), ctx));
  299. EXPECT_BIGNUMS_EQUAL("A * A", square.get(), ret.get());
  300. ASSERT_TRUE(BN_div(ret.get(), remainder.get(), square.get(), a.get(), ctx));
  301. EXPECT_BIGNUMS_EQUAL("Square / A", a.get(), ret.get());
  302. EXPECT_BIGNUMS_EQUAL("Square % A", zero.get(), remainder.get());
  303. BN_set_negative(a.get(), 0);
  304. ASSERT_TRUE(BN_sqrt(ret.get(), square.get(), ctx));
  305. EXPECT_BIGNUMS_EQUAL("sqrt(Square)", a.get(), ret.get());
  306. // BN_sqrt should fail on non-squares and negative numbers.
  307. if (!BN_is_zero(square.get())) {
  308. bssl::UniquePtr<BIGNUM> tmp(BN_new());
  309. ASSERT_TRUE(tmp);
  310. ASSERT_TRUE(BN_copy(tmp.get(), square.get()));
  311. BN_set_negative(tmp.get(), 1);
  312. EXPECT_FALSE(BN_sqrt(ret.get(), tmp.get(), ctx))
  313. << "BN_sqrt succeeded on a negative number";
  314. ERR_clear_error();
  315. BN_set_negative(tmp.get(), 0);
  316. ASSERT_TRUE(BN_add(tmp.get(), tmp.get(), BN_value_one()));
  317. EXPECT_FALSE(BN_sqrt(ret.get(), tmp.get(), ctx))
  318. << "BN_sqrt succeeded on a non-square";
  319. ERR_clear_error();
  320. }
  321. }
  322. static void TestProduct(FileTest *t, BN_CTX *ctx) {
  323. bssl::UniquePtr<BIGNUM> a = GetBIGNUM(t, "A");
  324. bssl::UniquePtr<BIGNUM> b = GetBIGNUM(t, "B");
  325. bssl::UniquePtr<BIGNUM> product = GetBIGNUM(t, "Product");
  326. bssl::UniquePtr<BIGNUM> zero(BN_new());
  327. ASSERT_TRUE(a);
  328. ASSERT_TRUE(b);
  329. ASSERT_TRUE(product);
  330. ASSERT_TRUE(zero);
  331. BN_zero(zero.get());
  332. bssl::UniquePtr<BIGNUM> ret(BN_new()), remainder(BN_new());
  333. ASSERT_TRUE(ret);
  334. ASSERT_TRUE(remainder);
  335. ASSERT_TRUE(BN_mul(ret.get(), a.get(), b.get(), ctx));
  336. EXPECT_BIGNUMS_EQUAL("A * B", product.get(), ret.get());
  337. ASSERT_TRUE(BN_div(ret.get(), remainder.get(), product.get(), a.get(), ctx));
  338. EXPECT_BIGNUMS_EQUAL("Product / A", b.get(), ret.get());
  339. EXPECT_BIGNUMS_EQUAL("Product % A", zero.get(), remainder.get());
  340. ASSERT_TRUE(BN_div(ret.get(), remainder.get(), product.get(), b.get(), ctx));
  341. EXPECT_BIGNUMS_EQUAL("Product / B", a.get(), ret.get());
  342. EXPECT_BIGNUMS_EQUAL("Product % B", zero.get(), remainder.get());
  343. }
  344. static void TestQuotient(FileTest *t, BN_CTX *ctx) {
  345. bssl::UniquePtr<BIGNUM> a = GetBIGNUM(t, "A");
  346. bssl::UniquePtr<BIGNUM> b = GetBIGNUM(t, "B");
  347. bssl::UniquePtr<BIGNUM> quotient = GetBIGNUM(t, "Quotient");
  348. bssl::UniquePtr<BIGNUM> remainder = GetBIGNUM(t, "Remainder");
  349. ASSERT_TRUE(a);
  350. ASSERT_TRUE(b);
  351. ASSERT_TRUE(quotient);
  352. ASSERT_TRUE(remainder);
  353. bssl::UniquePtr<BIGNUM> ret(BN_new()), ret2(BN_new());
  354. ASSERT_TRUE(ret);
  355. ASSERT_TRUE(ret2);
  356. ASSERT_TRUE(BN_div(ret.get(), ret2.get(), a.get(), b.get(), ctx));
  357. EXPECT_BIGNUMS_EQUAL("A / B", quotient.get(), ret.get());
  358. EXPECT_BIGNUMS_EQUAL("A % B", remainder.get(), ret2.get());
  359. ASSERT_TRUE(BN_mul(ret.get(), quotient.get(), b.get(), ctx));
  360. ASSERT_TRUE(BN_add(ret.get(), ret.get(), remainder.get()));
  361. EXPECT_BIGNUMS_EQUAL("Quotient * B + Remainder", a.get(), ret.get());
  362. // Test with |BN_mod_word| and |BN_div_word| if the divisor is small enough.
  363. BN_ULONG b_word = BN_get_word(b.get());
  364. if (!BN_is_negative(b.get()) && b_word != (BN_ULONG)-1) {
  365. BN_ULONG remainder_word = BN_get_word(remainder.get());
  366. ASSERT_NE(remainder_word, (BN_ULONG)-1);
  367. ASSERT_TRUE(BN_copy(ret.get(), a.get()));
  368. BN_ULONG ret_word = BN_div_word(ret.get(), b_word);
  369. EXPECT_EQ(remainder_word, ret_word);
  370. EXPECT_BIGNUMS_EQUAL("A / B (word)", quotient.get(), ret.get());
  371. ret_word = BN_mod_word(a.get(), b_word);
  372. EXPECT_EQ(remainder_word, ret_word);
  373. }
  374. // Test BN_nnmod.
  375. if (!BN_is_negative(b.get())) {
  376. bssl::UniquePtr<BIGNUM> nnmod(BN_new());
  377. ASSERT_TRUE(nnmod);
  378. ASSERT_TRUE(BN_copy(nnmod.get(), remainder.get()));
  379. if (BN_is_negative(nnmod.get())) {
  380. ASSERT_TRUE(BN_add(nnmod.get(), nnmod.get(), b.get()));
  381. }
  382. ASSERT_TRUE(BN_nnmod(ret.get(), a.get(), b.get(), ctx));
  383. EXPECT_BIGNUMS_EQUAL("A % B (non-negative)", nnmod.get(), ret.get());
  384. }
  385. }
  386. static void TestModMul(FileTest *t, BN_CTX *ctx) {
  387. bssl::UniquePtr<BIGNUM> a = GetBIGNUM(t, "A");
  388. bssl::UniquePtr<BIGNUM> b = GetBIGNUM(t, "B");
  389. bssl::UniquePtr<BIGNUM> m = GetBIGNUM(t, "M");
  390. bssl::UniquePtr<BIGNUM> mod_mul = GetBIGNUM(t, "ModMul");
  391. ASSERT_TRUE(a);
  392. ASSERT_TRUE(b);
  393. ASSERT_TRUE(m);
  394. ASSERT_TRUE(mod_mul);
  395. bssl::UniquePtr<BIGNUM> ret(BN_new());
  396. ASSERT_TRUE(ret);
  397. ASSERT_TRUE(BN_mod_mul(ret.get(), a.get(), b.get(), m.get(), ctx));
  398. EXPECT_BIGNUMS_EQUAL("A * B (mod M)", mod_mul.get(), ret.get());
  399. if (BN_is_odd(m.get())) {
  400. // Reduce |a| and |b| and test the Montgomery version.
  401. bssl::UniquePtr<BN_MONT_CTX> mont(BN_MONT_CTX_new());
  402. bssl::UniquePtr<BIGNUM> a_tmp(BN_new()), b_tmp(BN_new());
  403. ASSERT_TRUE(mont);
  404. ASSERT_TRUE(a_tmp);
  405. ASSERT_TRUE(b_tmp);
  406. ASSERT_TRUE(BN_MONT_CTX_set(mont.get(), m.get(), ctx));
  407. ASSERT_TRUE(BN_nnmod(a_tmp.get(), a.get(), m.get(), ctx));
  408. ASSERT_TRUE(BN_nnmod(b_tmp.get(), b.get(), m.get(), ctx));
  409. ASSERT_TRUE(BN_to_montgomery(a_tmp.get(), a_tmp.get(), mont.get(), ctx));
  410. ASSERT_TRUE(BN_to_montgomery(b_tmp.get(), b_tmp.get(), mont.get(), ctx));
  411. ASSERT_TRUE(BN_mod_mul_montgomery(ret.get(), a_tmp.get(), b_tmp.get(),
  412. mont.get(), ctx));
  413. ASSERT_TRUE(BN_from_montgomery(ret.get(), ret.get(), mont.get(), ctx));
  414. EXPECT_BIGNUMS_EQUAL("A * B (mod M) (Montgomery)", mod_mul.get(),
  415. ret.get());
  416. }
  417. }
  418. static void TestModSquare(FileTest *t, BN_CTX *ctx) {
  419. bssl::UniquePtr<BIGNUM> a = GetBIGNUM(t, "A");
  420. bssl::UniquePtr<BIGNUM> m = GetBIGNUM(t, "M");
  421. bssl::UniquePtr<BIGNUM> mod_square = GetBIGNUM(t, "ModSquare");
  422. ASSERT_TRUE(a);
  423. ASSERT_TRUE(m);
  424. ASSERT_TRUE(mod_square);
  425. bssl::UniquePtr<BIGNUM> a_copy(BN_new());
  426. bssl::UniquePtr<BIGNUM> ret(BN_new());
  427. ASSERT_TRUE(ret);
  428. ASSERT_TRUE(a_copy);
  429. ASSERT_TRUE(BN_mod_mul(ret.get(), a.get(), a.get(), m.get(), ctx));
  430. EXPECT_BIGNUMS_EQUAL("A * A (mod M)", mod_square.get(), ret.get());
  431. // Repeat the operation with |a_copy|.
  432. ASSERT_TRUE(BN_copy(a_copy.get(), a.get()));
  433. ASSERT_TRUE(BN_mod_mul(ret.get(), a.get(), a_copy.get(), m.get(), ctx));
  434. EXPECT_BIGNUMS_EQUAL("A * A_copy (mod M)", mod_square.get(), ret.get());
  435. if (BN_is_odd(m.get())) {
  436. // Reduce |a| and test the Montgomery version.
  437. bssl::UniquePtr<BN_MONT_CTX> mont(BN_MONT_CTX_new());
  438. bssl::UniquePtr<BIGNUM> a_tmp(BN_new());
  439. ASSERT_TRUE(mont);
  440. ASSERT_TRUE(a_tmp);
  441. ASSERT_TRUE(BN_MONT_CTX_set(mont.get(), m.get(), ctx));
  442. ASSERT_TRUE(BN_nnmod(a_tmp.get(), a.get(), m.get(), ctx));
  443. ASSERT_TRUE(BN_to_montgomery(a_tmp.get(), a_tmp.get(), mont.get(), ctx));
  444. ASSERT_TRUE(BN_mod_mul_montgomery(ret.get(), a_tmp.get(), a_tmp.get(),
  445. mont.get(), ctx));
  446. ASSERT_TRUE(BN_from_montgomery(ret.get(), ret.get(), mont.get(), ctx));
  447. EXPECT_BIGNUMS_EQUAL("A * A (mod M) (Montgomery)", mod_square.get(),
  448. ret.get());
  449. // Repeat the operation with |a_copy|.
  450. ASSERT_TRUE(BN_copy(a_copy.get(), a_tmp.get()));
  451. ASSERT_TRUE(BN_mod_mul_montgomery(ret.get(), a_tmp.get(), a_copy.get(),
  452. mont.get(), ctx));
  453. ASSERT_TRUE(BN_from_montgomery(ret.get(), ret.get(), mont.get(), ctx));
  454. EXPECT_BIGNUMS_EQUAL("A * A_copy (mod M) (Montgomery)", mod_square.get(),
  455. ret.get());
  456. }
  457. }
  458. static void TestModExp(FileTest *t, BN_CTX *ctx) {
  459. bssl::UniquePtr<BIGNUM> a = GetBIGNUM(t, "A");
  460. bssl::UniquePtr<BIGNUM> e = GetBIGNUM(t, "E");
  461. bssl::UniquePtr<BIGNUM> m = GetBIGNUM(t, "M");
  462. bssl::UniquePtr<BIGNUM> mod_exp = GetBIGNUM(t, "ModExp");
  463. ASSERT_TRUE(a);
  464. ASSERT_TRUE(e);
  465. ASSERT_TRUE(m);
  466. ASSERT_TRUE(mod_exp);
  467. bssl::UniquePtr<BIGNUM> ret(BN_new());
  468. ASSERT_TRUE(ret);
  469. ASSERT_TRUE(BN_mod_exp(ret.get(), a.get(), e.get(), m.get(), ctx));
  470. EXPECT_BIGNUMS_EQUAL("A ^ E (mod M)", mod_exp.get(), ret.get());
  471. if (BN_is_odd(m.get())) {
  472. ASSERT_TRUE(
  473. BN_mod_exp_mont(ret.get(), a.get(), e.get(), m.get(), ctx, NULL));
  474. EXPECT_BIGNUMS_EQUAL("A ^ E (mod M) (Montgomery)", mod_exp.get(),
  475. ret.get());
  476. ASSERT_TRUE(BN_mod_exp_mont_consttime(ret.get(), a.get(), e.get(), m.get(),
  477. ctx, NULL));
  478. EXPECT_BIGNUMS_EQUAL("A ^ E (mod M) (constant-time)", mod_exp.get(),
  479. ret.get());
  480. }
  481. }
  482. static void TestExp(FileTest *t, BN_CTX *ctx) {
  483. bssl::UniquePtr<BIGNUM> a = GetBIGNUM(t, "A");
  484. bssl::UniquePtr<BIGNUM> e = GetBIGNUM(t, "E");
  485. bssl::UniquePtr<BIGNUM> exp = GetBIGNUM(t, "Exp");
  486. ASSERT_TRUE(a);
  487. ASSERT_TRUE(e);
  488. ASSERT_TRUE(exp);
  489. bssl::UniquePtr<BIGNUM> ret(BN_new());
  490. ASSERT_TRUE(ret);
  491. ASSERT_TRUE(BN_exp(ret.get(), a.get(), e.get(), ctx));
  492. EXPECT_BIGNUMS_EQUAL("A ^ E", exp.get(), ret.get());
  493. }
  494. static void TestModSqrt(FileTest *t, BN_CTX *ctx) {
  495. bssl::UniquePtr<BIGNUM> a = GetBIGNUM(t, "A");
  496. bssl::UniquePtr<BIGNUM> p = GetBIGNUM(t, "P");
  497. bssl::UniquePtr<BIGNUM> mod_sqrt = GetBIGNUM(t, "ModSqrt");
  498. bssl::UniquePtr<BIGNUM> mod_sqrt2(BN_new());
  499. ASSERT_TRUE(a);
  500. ASSERT_TRUE(p);
  501. ASSERT_TRUE(mod_sqrt);
  502. ASSERT_TRUE(mod_sqrt2);
  503. // There are two possible answers.
  504. ASSERT_TRUE(BN_sub(mod_sqrt2.get(), p.get(), mod_sqrt.get()));
  505. // -0 is 0, not P.
  506. if (BN_is_zero(mod_sqrt.get())) {
  507. BN_zero(mod_sqrt2.get());
  508. }
  509. bssl::UniquePtr<BIGNUM> ret(BN_new());
  510. ASSERT_TRUE(ret);
  511. ASSERT_TRUE(BN_mod_sqrt(ret.get(), a.get(), p.get(), ctx));
  512. if (BN_cmp(ret.get(), mod_sqrt2.get()) != 0) {
  513. EXPECT_BIGNUMS_EQUAL("sqrt(A) (mod P)", mod_sqrt.get(), ret.get());
  514. }
  515. }
  516. static void TestNotModSquare(FileTest *t, BN_CTX *ctx) {
  517. bssl::UniquePtr<BIGNUM> not_mod_square = GetBIGNUM(t, "NotModSquare");
  518. bssl::UniquePtr<BIGNUM> p = GetBIGNUM(t, "P");
  519. bssl::UniquePtr<BIGNUM> ret(BN_new());
  520. ASSERT_TRUE(not_mod_square);
  521. ASSERT_TRUE(p);
  522. ASSERT_TRUE(ret);
  523. EXPECT_FALSE(BN_mod_sqrt(ret.get(), not_mod_square.get(), p.get(), ctx))
  524. << "BN_mod_sqrt unexpectedly succeeded.";
  525. uint32_t err = ERR_peek_error();
  526. EXPECT_EQ(ERR_LIB_BN, ERR_GET_LIB(err));
  527. EXPECT_EQ(BN_R_NOT_A_SQUARE, ERR_GET_REASON(err));
  528. ERR_clear_error();
  529. }
  530. static void TestModInv(FileTest *t, BN_CTX *ctx) {
  531. bssl::UniquePtr<BIGNUM> a = GetBIGNUM(t, "A");
  532. bssl::UniquePtr<BIGNUM> m = GetBIGNUM(t, "M");
  533. bssl::UniquePtr<BIGNUM> mod_inv = GetBIGNUM(t, "ModInv");
  534. ASSERT_TRUE(a);
  535. ASSERT_TRUE(m);
  536. ASSERT_TRUE(mod_inv);
  537. bssl::UniquePtr<BIGNUM> ret(BN_new());
  538. ASSERT_TRUE(ret);
  539. ASSERT_TRUE(BN_mod_inverse(ret.get(), a.get(), m.get(), ctx));
  540. EXPECT_BIGNUMS_EQUAL("inv(A) (mod M)", mod_inv.get(), ret.get());
  541. }
  542. class BNTest : public testing::Test {
  543. protected:
  544. void SetUp() override {
  545. ctx_.reset(BN_CTX_new());
  546. ASSERT_TRUE(ctx_);
  547. }
  548. BN_CTX *ctx() { return ctx_.get(); }
  549. private:
  550. bssl::UniquePtr<BN_CTX> ctx_;
  551. };
  552. TEST_F(BNTest, TestVectors) {
  553. static const struct {
  554. const char *name;
  555. void (*func)(FileTest *t, BN_CTX *ctx);
  556. } kTests[] = {
  557. {"Sum", TestSum},
  558. {"LShift1", TestLShift1},
  559. {"LShift", TestLShift},
  560. {"RShift", TestRShift},
  561. {"Square", TestSquare},
  562. {"Product", TestProduct},
  563. {"Quotient", TestQuotient},
  564. {"ModMul", TestModMul},
  565. {"ModSquare", TestModSquare},
  566. {"ModExp", TestModExp},
  567. {"Exp", TestExp},
  568. {"ModSqrt", TestModSqrt},
  569. {"NotModSquare", TestNotModSquare},
  570. {"ModInv", TestModInv},
  571. };
  572. FileTestGTest("crypto/fipsmodule/bn/bn_tests.txt", [&](FileTest *t) {
  573. for (const auto &test : kTests) {
  574. if (t->GetType() == test.name) {
  575. test.func(t, ctx());
  576. return;
  577. }
  578. }
  579. FAIL() << "Unknown test type: " << t->GetType();
  580. });
  581. }
  582. TEST_F(BNTest, BN2BinPadded) {
  583. uint8_t zeros[256], out[256], reference[128];
  584. OPENSSL_memset(zeros, 0, sizeof(zeros));
  585. // Test edge case at 0.
  586. bssl::UniquePtr<BIGNUM> n(BN_new());
  587. ASSERT_TRUE(n);
  588. ASSERT_TRUE(BN_bn2bin_padded(NULL, 0, n.get()));
  589. OPENSSL_memset(out, -1, sizeof(out));
  590. ASSERT_TRUE(BN_bn2bin_padded(out, sizeof(out), n.get()));
  591. EXPECT_EQ(Bytes(zeros), Bytes(out));
  592. // Test a random numbers at various byte lengths.
  593. for (size_t bytes = 128 - 7; bytes <= 128; bytes++) {
  594. ASSERT_TRUE(
  595. BN_rand(n.get(), bytes * 8, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY));
  596. ASSERT_EQ(bytes, BN_num_bytes(n.get()));
  597. ASSERT_EQ(bytes, BN_bn2bin(n.get(), reference));
  598. // Empty buffer should fail.
  599. EXPECT_FALSE(BN_bn2bin_padded(NULL, 0, n.get()));
  600. // One byte short should fail.
  601. EXPECT_FALSE(BN_bn2bin_padded(out, bytes - 1, n.get()));
  602. // Exactly right size should encode.
  603. ASSERT_TRUE(BN_bn2bin_padded(out, bytes, n.get()));
  604. EXPECT_EQ(Bytes(reference, bytes), Bytes(out, bytes));
  605. // Pad up one byte extra.
  606. ASSERT_TRUE(BN_bn2bin_padded(out, bytes + 1, n.get()));
  607. EXPECT_EQ(0u, out[0]);
  608. EXPECT_EQ(Bytes(reference, bytes), Bytes(out + 1, bytes));
  609. // Pad up to 256.
  610. ASSERT_TRUE(BN_bn2bin_padded(out, sizeof(out), n.get()));
  611. EXPECT_EQ(Bytes(zeros, sizeof(out) - bytes),
  612. Bytes(out, sizeof(out) - bytes));
  613. EXPECT_EQ(Bytes(reference, bytes), Bytes(out + sizeof(out) - bytes, bytes));
  614. }
  615. }
  616. TEST_F(BNTest, LittleEndian) {
  617. bssl::UniquePtr<BIGNUM> x(BN_new());
  618. bssl::UniquePtr<BIGNUM> y(BN_new());
  619. ASSERT_TRUE(x);
  620. ASSERT_TRUE(y);
  621. // Test edge case at 0. Fill |out| with garbage to ensure |BN_bn2le_padded|
  622. // wrote the result.
  623. uint8_t out[256], zeros[256];
  624. OPENSSL_memset(out, -1, sizeof(out));
  625. OPENSSL_memset(zeros, 0, sizeof(zeros));
  626. ASSERT_TRUE(BN_bn2le_padded(out, sizeof(out), x.get()));
  627. EXPECT_EQ(Bytes(zeros), Bytes(out));
  628. ASSERT_TRUE(BN_le2bn(out, sizeof(out), y.get()));
  629. EXPECT_BIGNUMS_EQUAL("BN_le2bn round-trip", x.get(), y.get());
  630. // Test random numbers at various byte lengths.
  631. for (size_t bytes = 128 - 7; bytes <= 128; bytes++) {
  632. ASSERT_TRUE(
  633. BN_rand(x.get(), bytes * 8, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY));
  634. // Fill |out| with garbage to ensure |BN_bn2le_padded| wrote the result.
  635. OPENSSL_memset(out, -1, sizeof(out));
  636. ASSERT_TRUE(BN_bn2le_padded(out, sizeof(out), x.get()));
  637. // Compute the expected value by reversing the big-endian output.
  638. uint8_t expected[sizeof(out)];
  639. ASSERT_TRUE(BN_bn2bin_padded(expected, sizeof(expected), x.get()));
  640. for (size_t i = 0; i < sizeof(expected) / 2; i++) {
  641. uint8_t tmp = expected[i];
  642. expected[i] = expected[sizeof(expected) - 1 - i];
  643. expected[sizeof(expected) - 1 - i] = tmp;
  644. }
  645. EXPECT_EQ(Bytes(out), Bytes(expected));
  646. // Make sure the decoding produces the same BIGNUM.
  647. ASSERT_TRUE(BN_le2bn(out, bytes, y.get()));
  648. EXPECT_BIGNUMS_EQUAL("BN_le2bn round-trip", x.get(), y.get());
  649. }
  650. }
  651. static int DecimalToBIGNUM(bssl::UniquePtr<BIGNUM> *out, const char *in) {
  652. BIGNUM *raw = NULL;
  653. int ret = BN_dec2bn(&raw, in);
  654. out->reset(raw);
  655. return ret;
  656. }
  657. TEST_F(BNTest, Dec2BN) {
  658. bssl::UniquePtr<BIGNUM> bn;
  659. int ret = DecimalToBIGNUM(&bn, "0");
  660. EXPECT_EQ(1, ret);
  661. EXPECT_TRUE(BN_is_zero(bn.get()));
  662. EXPECT_FALSE(BN_is_negative(bn.get()));
  663. ret = DecimalToBIGNUM(&bn, "256");
  664. EXPECT_EQ(3, ret);
  665. EXPECT_TRUE(BN_is_word(bn.get(), 256));
  666. EXPECT_FALSE(BN_is_negative(bn.get()));
  667. ret = DecimalToBIGNUM(&bn, "-42");
  668. EXPECT_EQ(3, ret);
  669. EXPECT_TRUE(BN_abs_is_word(bn.get(), 42));
  670. EXPECT_TRUE(BN_is_negative(bn.get()));
  671. ret = DecimalToBIGNUM(&bn, "-0");
  672. EXPECT_EQ(2, ret);
  673. EXPECT_TRUE(BN_is_zero(bn.get()));
  674. EXPECT_FALSE(BN_is_negative(bn.get()));
  675. ret = DecimalToBIGNUM(&bn, "42trailing garbage is ignored");
  676. EXPECT_EQ(2, ret);
  677. EXPECT_TRUE(BN_abs_is_word(bn.get(), 42));
  678. EXPECT_FALSE(BN_is_negative(bn.get()));
  679. }
  680. TEST_F(BNTest, Hex2BN) {
  681. bssl::UniquePtr<BIGNUM> bn;
  682. int ret = HexToBIGNUM(&bn, "0");
  683. EXPECT_EQ(1, ret);
  684. EXPECT_TRUE(BN_is_zero(bn.get()));
  685. EXPECT_FALSE(BN_is_negative(bn.get()));
  686. ret = HexToBIGNUM(&bn, "256");
  687. EXPECT_EQ(3, ret);
  688. EXPECT_TRUE(BN_is_word(bn.get(), 0x256));
  689. EXPECT_FALSE(BN_is_negative(bn.get()));
  690. ret = HexToBIGNUM(&bn, "-42");
  691. EXPECT_EQ(3, ret);
  692. EXPECT_TRUE(BN_abs_is_word(bn.get(), 0x42));
  693. EXPECT_TRUE(BN_is_negative(bn.get()));
  694. ret = HexToBIGNUM(&bn, "-0");
  695. EXPECT_EQ(2, ret);
  696. EXPECT_TRUE(BN_is_zero(bn.get()));
  697. EXPECT_FALSE(BN_is_negative(bn.get()));
  698. ret = HexToBIGNUM(&bn, "abctrailing garbage is ignored");
  699. EXPECT_EQ(3, ret);
  700. EXPECT_TRUE(BN_is_word(bn.get(), 0xabc));
  701. EXPECT_FALSE(BN_is_negative(bn.get()));
  702. }
  703. static bssl::UniquePtr<BIGNUM> ASCIIToBIGNUM(const char *in) {
  704. BIGNUM *raw = NULL;
  705. if (!BN_asc2bn(&raw, in)) {
  706. return nullptr;
  707. }
  708. return bssl::UniquePtr<BIGNUM>(raw);
  709. }
  710. TEST_F(BNTest, ASC2BN) {
  711. bssl::UniquePtr<BIGNUM> bn = ASCIIToBIGNUM("0");
  712. ASSERT_TRUE(bn);
  713. EXPECT_TRUE(BN_is_zero(bn.get()));
  714. EXPECT_FALSE(BN_is_negative(bn.get()));
  715. bn = ASCIIToBIGNUM("256");
  716. ASSERT_TRUE(bn);
  717. EXPECT_TRUE(BN_is_word(bn.get(), 256));
  718. EXPECT_FALSE(BN_is_negative(bn.get()));
  719. bn = ASCIIToBIGNUM("-42");
  720. ASSERT_TRUE(bn);
  721. EXPECT_TRUE(BN_abs_is_word(bn.get(), 42));
  722. EXPECT_TRUE(BN_is_negative(bn.get()));
  723. bn = ASCIIToBIGNUM("0x1234");
  724. ASSERT_TRUE(bn);
  725. EXPECT_TRUE(BN_is_word(bn.get(), 0x1234));
  726. EXPECT_FALSE(BN_is_negative(bn.get()));
  727. bn = ASCIIToBIGNUM("0X1234");
  728. ASSERT_TRUE(bn);
  729. EXPECT_TRUE(BN_is_word(bn.get(), 0x1234));
  730. EXPECT_FALSE(BN_is_negative(bn.get()));
  731. bn = ASCIIToBIGNUM("-0xabcd");
  732. ASSERT_TRUE(bn);
  733. EXPECT_TRUE(BN_abs_is_word(bn.get(), 0xabcd));
  734. EXPECT_FALSE(!BN_is_negative(bn.get()));
  735. bn = ASCIIToBIGNUM("-0");
  736. ASSERT_TRUE(bn);
  737. EXPECT_TRUE(BN_is_zero(bn.get()));
  738. EXPECT_FALSE(BN_is_negative(bn.get()));
  739. bn = ASCIIToBIGNUM("123trailing garbage is ignored");
  740. ASSERT_TRUE(bn);
  741. EXPECT_TRUE(BN_is_word(bn.get(), 123));
  742. EXPECT_FALSE(BN_is_negative(bn.get()));
  743. }
  744. struct MPITest {
  745. const char *base10;
  746. const char *mpi;
  747. size_t mpi_len;
  748. };
  749. static const MPITest kMPITests[] = {
  750. { "0", "\x00\x00\x00\x00", 4 },
  751. { "1", "\x00\x00\x00\x01\x01", 5 },
  752. { "-1", "\x00\x00\x00\x01\x81", 5 },
  753. { "128", "\x00\x00\x00\x02\x00\x80", 6 },
  754. { "256", "\x00\x00\x00\x02\x01\x00", 6 },
  755. { "-256", "\x00\x00\x00\x02\x81\x00", 6 },
  756. };
  757. TEST_F(BNTest, MPI) {
  758. uint8_t scratch[8];
  759. for (const auto &test : kMPITests) {
  760. SCOPED_TRACE(test.base10);
  761. bssl::UniquePtr<BIGNUM> bn(ASCIIToBIGNUM(test.base10));
  762. ASSERT_TRUE(bn);
  763. const size_t mpi_len = BN_bn2mpi(bn.get(), NULL);
  764. ASSERT_LE(mpi_len, sizeof(scratch)) << "MPI size is too large to test";
  765. const size_t mpi_len2 = BN_bn2mpi(bn.get(), scratch);
  766. EXPECT_EQ(mpi_len, mpi_len2);
  767. EXPECT_EQ(Bytes(test.mpi, test.mpi_len), Bytes(scratch, mpi_len));
  768. bssl::UniquePtr<BIGNUM> bn2(BN_mpi2bn(scratch, mpi_len, NULL));
  769. ASSERT_TRUE(bn2) << "failed to parse";
  770. EXPECT_BIGNUMS_EQUAL("BN_mpi2bn", bn.get(), bn2.get());
  771. }
  772. }
  773. TEST_F(BNTest, Rand) {
  774. bssl::UniquePtr<BIGNUM> bn(BN_new());
  775. ASSERT_TRUE(bn);
  776. // Test BN_rand accounts for degenerate cases with |top| and |bottom|
  777. // parameters.
  778. ASSERT_TRUE(BN_rand(bn.get(), 0, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY));
  779. EXPECT_TRUE(BN_is_zero(bn.get()));
  780. ASSERT_TRUE(BN_rand(bn.get(), 0, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ODD));
  781. EXPECT_TRUE(BN_is_zero(bn.get()));
  782. ASSERT_TRUE(BN_rand(bn.get(), 1, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY));
  783. EXPECT_TRUE(BN_is_word(bn.get(), 1));
  784. ASSERT_TRUE(BN_rand(bn.get(), 1, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ANY));
  785. EXPECT_TRUE(BN_is_word(bn.get(), 1));
  786. ASSERT_TRUE(BN_rand(bn.get(), 1, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ODD));
  787. EXPECT_TRUE(BN_is_word(bn.get(), 1));
  788. ASSERT_TRUE(BN_rand(bn.get(), 2, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ANY));
  789. EXPECT_TRUE(BN_is_word(bn.get(), 3));
  790. }
  791. TEST_F(BNTest, RandRange) {
  792. bssl::UniquePtr<BIGNUM> bn(BN_new()), six(BN_new());
  793. ASSERT_TRUE(bn);
  794. ASSERT_TRUE(six);
  795. ASSERT_TRUE(BN_set_word(six.get(), 6));
  796. // Generate 1,000 random numbers and ensure they all stay in range. This check
  797. // may flakily pass when it should have failed but will not flakily fail.
  798. bool seen[6] = {false, false, false, false, false};
  799. for (unsigned i = 0; i < 1000; i++) {
  800. SCOPED_TRACE(i);
  801. ASSERT_TRUE(BN_rand_range_ex(bn.get(), 1, six.get()));
  802. BN_ULONG word = BN_get_word(bn.get());
  803. if (BN_is_negative(bn.get()) ||
  804. word < 1 ||
  805. word >= 6) {
  806. FAIL() << "BN_rand_range_ex generated invalid value: " << word;
  807. }
  808. seen[word] = true;
  809. }
  810. // Test that all numbers were accounted for. Note this test is probabilistic
  811. // and may flakily fail when it should have passed. As an upper-bound on the
  812. // failure probability, we'll never see any one number with probability
  813. // (4/5)^1000, so the probability of failure is at most 5*(4/5)^1000. This is
  814. // around 1 in 2^320.
  815. for (unsigned i = 1; i < 6; i++) {
  816. EXPECT_TRUE(seen[i]) << "BN_rand_range failed to generated " << i;
  817. }
  818. }
  819. struct ASN1Test {
  820. const char *value_ascii;
  821. const char *der;
  822. size_t der_len;
  823. };
  824. static const ASN1Test kASN1Tests[] = {
  825. {"0", "\x02\x01\x00", 3},
  826. {"1", "\x02\x01\x01", 3},
  827. {"127", "\x02\x01\x7f", 3},
  828. {"128", "\x02\x02\x00\x80", 4},
  829. {"0xdeadbeef", "\x02\x05\x00\xde\xad\xbe\xef", 7},
  830. {"0x0102030405060708",
  831. "\x02\x08\x01\x02\x03\x04\x05\x06\x07\x08", 10},
  832. {"0xffffffffffffffff",
  833. "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff", 11},
  834. };
  835. struct ASN1InvalidTest {
  836. const char *der;
  837. size_t der_len;
  838. };
  839. static const ASN1InvalidTest kASN1InvalidTests[] = {
  840. // Bad tag.
  841. {"\x03\x01\x00", 3},
  842. // Empty contents.
  843. {"\x02\x00", 2},
  844. };
  845. // kASN1BuggyTests contains incorrect encodings and the corresponding, expected
  846. // results of |BN_parse_asn1_unsigned_buggy| given that input.
  847. static const ASN1Test kASN1BuggyTests[] = {
  848. // Negative numbers.
  849. {"128", "\x02\x01\x80", 3},
  850. {"255", "\x02\x01\xff", 3},
  851. // Unnecessary leading zeros.
  852. {"1", "\x02\x02\x00\x01", 4},
  853. };
  854. TEST_F(BNTest, ASN1) {
  855. for (const ASN1Test &test : kASN1Tests) {
  856. SCOPED_TRACE(test.value_ascii);
  857. bssl::UniquePtr<BIGNUM> bn = ASCIIToBIGNUM(test.value_ascii);
  858. ASSERT_TRUE(bn);
  859. // Test that the input is correctly parsed.
  860. bssl::UniquePtr<BIGNUM> bn2(BN_new());
  861. ASSERT_TRUE(bn2);
  862. CBS cbs;
  863. CBS_init(&cbs, reinterpret_cast<const uint8_t*>(test.der), test.der_len);
  864. ASSERT_TRUE(BN_parse_asn1_unsigned(&cbs, bn2.get()));
  865. EXPECT_EQ(0u, CBS_len(&cbs));
  866. EXPECT_BIGNUMS_EQUAL("decode ASN.1", bn.get(), bn2.get());
  867. // Test the value serializes correctly.
  868. bssl::ScopedCBB cbb;
  869. uint8_t *der;
  870. size_t der_len;
  871. ASSERT_TRUE(CBB_init(cbb.get(), 0));
  872. ASSERT_TRUE(BN_marshal_asn1(cbb.get(), bn.get()));
  873. ASSERT_TRUE(CBB_finish(cbb.get(), &der, &der_len));
  874. bssl::UniquePtr<uint8_t> delete_der(der);
  875. EXPECT_EQ(Bytes(test.der, test.der_len), Bytes(der, der_len));
  876. // |BN_parse_asn1_unsigned_buggy| parses all valid input.
  877. CBS_init(&cbs, reinterpret_cast<const uint8_t*>(test.der), test.der_len);
  878. ASSERT_TRUE(BN_parse_asn1_unsigned_buggy(&cbs, bn2.get()));
  879. EXPECT_EQ(0u, CBS_len(&cbs));
  880. EXPECT_BIGNUMS_EQUAL("decode ASN.1 buggy", bn.get(), bn2.get());
  881. }
  882. for (const ASN1InvalidTest &test : kASN1InvalidTests) {
  883. SCOPED_TRACE(Bytes(test.der, test.der_len));;
  884. bssl::UniquePtr<BIGNUM> bn(BN_new());
  885. ASSERT_TRUE(bn);
  886. CBS cbs;
  887. CBS_init(&cbs, reinterpret_cast<const uint8_t *>(test.der), test.der_len);
  888. EXPECT_FALSE(BN_parse_asn1_unsigned(&cbs, bn.get()))
  889. << "Parsed invalid input.";
  890. ERR_clear_error();
  891. // All tests in kASN1InvalidTests are also rejected by
  892. // |BN_parse_asn1_unsigned_buggy|.
  893. CBS_init(&cbs, reinterpret_cast<const uint8_t*>(test.der), test.der_len);
  894. EXPECT_FALSE(BN_parse_asn1_unsigned_buggy(&cbs, bn.get()))
  895. << "Parsed invalid input.";
  896. ERR_clear_error();
  897. }
  898. for (const ASN1Test &test : kASN1BuggyTests) {
  899. SCOPED_TRACE(Bytes(test.der, test.der_len));
  900. // These broken encodings are rejected by |BN_parse_asn1_unsigned|.
  901. bssl::UniquePtr<BIGNUM> bn(BN_new());
  902. ASSERT_TRUE(bn);
  903. CBS cbs;
  904. CBS_init(&cbs, reinterpret_cast<const uint8_t*>(test.der), test.der_len);
  905. EXPECT_FALSE(BN_parse_asn1_unsigned(&cbs, bn.get()))
  906. << "Parsed invalid input.";
  907. ERR_clear_error();
  908. // However |BN_parse_asn1_unsigned_buggy| accepts them.
  909. bssl::UniquePtr<BIGNUM> bn2 = ASCIIToBIGNUM(test.value_ascii);
  910. ASSERT_TRUE(bn2);
  911. CBS_init(&cbs, reinterpret_cast<const uint8_t*>(test.der), test.der_len);
  912. ASSERT_TRUE(BN_parse_asn1_unsigned_buggy(&cbs, bn.get()));
  913. EXPECT_EQ(0u, CBS_len(&cbs));
  914. EXPECT_BIGNUMS_EQUAL("decode ASN.1 buggy", bn2.get(), bn.get());
  915. }
  916. // Serializing negative numbers is not supported.
  917. bssl::UniquePtr<BIGNUM> bn = ASCIIToBIGNUM("-1");
  918. ASSERT_TRUE(bn);
  919. bssl::ScopedCBB cbb;
  920. ASSERT_TRUE(CBB_init(cbb.get(), 0));
  921. EXPECT_FALSE(BN_marshal_asn1(cbb.get(), bn.get()))
  922. << "Serialized negative number.";
  923. ERR_clear_error();
  924. }
  925. TEST_F(BNTest, NegativeZero) {
  926. bssl::UniquePtr<BIGNUM> a(BN_new());
  927. bssl::UniquePtr<BIGNUM> b(BN_new());
  928. bssl::UniquePtr<BIGNUM> c(BN_new());
  929. ASSERT_TRUE(a);
  930. ASSERT_TRUE(b);
  931. ASSERT_TRUE(c);
  932. // Test that BN_mul never gives negative zero.
  933. ASSERT_TRUE(BN_set_word(a.get(), 1));
  934. BN_set_negative(a.get(), 1);
  935. BN_zero(b.get());
  936. ASSERT_TRUE(BN_mul(c.get(), a.get(), b.get(), ctx()));
  937. EXPECT_TRUE(BN_is_zero(c.get()));
  938. EXPECT_FALSE(BN_is_negative(c.get()));
  939. bssl::UniquePtr<BIGNUM> numerator(BN_new()), denominator(BN_new());
  940. ASSERT_TRUE(numerator);
  941. ASSERT_TRUE(denominator);
  942. // Test that BN_div never gives negative zero in the quotient.
  943. ASSERT_TRUE(BN_set_word(numerator.get(), 1));
  944. ASSERT_TRUE(BN_set_word(denominator.get(), 2));
  945. BN_set_negative(numerator.get(), 1);
  946. ASSERT_TRUE(
  947. BN_div(a.get(), b.get(), numerator.get(), denominator.get(), ctx()));
  948. EXPECT_TRUE(BN_is_zero(a.get()));
  949. EXPECT_FALSE(BN_is_negative(a.get()));
  950. // Test that BN_div never gives negative zero in the remainder.
  951. ASSERT_TRUE(BN_set_word(denominator.get(), 1));
  952. ASSERT_TRUE(
  953. BN_div(a.get(), b.get(), numerator.get(), denominator.get(), ctx()));
  954. EXPECT_TRUE(BN_is_zero(b.get()));
  955. EXPECT_FALSE(BN_is_negative(b.get()));
  956. // Test that BN_set_negative will not produce a negative zero.
  957. BN_zero(a.get());
  958. BN_set_negative(a.get(), 1);
  959. EXPECT_FALSE(BN_is_negative(a.get()));
  960. // Test that forcibly creating a negative zero does not break |BN_bn2hex| or
  961. // |BN_bn2dec|.
  962. a->neg = 1;
  963. bssl::UniquePtr<char> dec(BN_bn2dec(a.get()));
  964. bssl::UniquePtr<char> hex(BN_bn2hex(a.get()));
  965. ASSERT_TRUE(dec);
  966. ASSERT_TRUE(hex);
  967. EXPECT_STREQ("-0", dec.get());
  968. EXPECT_STREQ("-0", hex.get());
  969. // Test that |BN_rshift| and |BN_rshift1| will not produce a negative zero.
  970. ASSERT_TRUE(BN_set_word(a.get(), 1));
  971. BN_set_negative(a.get(), 1);
  972. ASSERT_TRUE(BN_rshift(b.get(), a.get(), 1));
  973. EXPECT_TRUE(BN_is_zero(b.get()));
  974. EXPECT_FALSE(BN_is_negative(b.get()));
  975. ASSERT_TRUE(BN_rshift1(c.get(), a.get()));
  976. EXPECT_TRUE(BN_is_zero(c.get()));
  977. EXPECT_FALSE(BN_is_negative(c.get()));
  978. // Test that |BN_div_word| will not produce a negative zero.
  979. ASSERT_NE((BN_ULONG)-1, BN_div_word(a.get(), 2));
  980. EXPECT_TRUE(BN_is_zero(a.get()));
  981. EXPECT_FALSE(BN_is_negative(a.get()));
  982. }
  983. TEST_F(BNTest, BadModulus) {
  984. bssl::UniquePtr<BIGNUM> a(BN_new());
  985. bssl::UniquePtr<BIGNUM> b(BN_new());
  986. bssl::UniquePtr<BIGNUM> zero(BN_new());
  987. bssl::UniquePtr<BN_MONT_CTX> mont(BN_MONT_CTX_new());
  988. ASSERT_TRUE(a);
  989. ASSERT_TRUE(b);
  990. ASSERT_TRUE(zero);
  991. ASSERT_TRUE(mont);
  992. BN_zero(zero.get());
  993. EXPECT_FALSE(BN_div(a.get(), b.get(), BN_value_one(), zero.get(), ctx()));
  994. ERR_clear_error();
  995. EXPECT_FALSE(
  996. BN_mod_mul(a.get(), BN_value_one(), BN_value_one(), zero.get(), ctx()));
  997. ERR_clear_error();
  998. EXPECT_FALSE(
  999. BN_mod_exp(a.get(), BN_value_one(), BN_value_one(), zero.get(), ctx()));
  1000. ERR_clear_error();
  1001. EXPECT_FALSE(BN_mod_exp_mont(a.get(), BN_value_one(), BN_value_one(),
  1002. zero.get(), ctx(), NULL));
  1003. ERR_clear_error();
  1004. EXPECT_FALSE(BN_mod_exp_mont_consttime(
  1005. a.get(), BN_value_one(), BN_value_one(), zero.get(), ctx(), nullptr));
  1006. ERR_clear_error();
  1007. EXPECT_FALSE(BN_MONT_CTX_set(mont.get(), zero.get(), ctx()));
  1008. ERR_clear_error();
  1009. // Some operations also may not be used with an even modulus.
  1010. ASSERT_TRUE(BN_set_word(b.get(), 16));
  1011. EXPECT_FALSE(BN_MONT_CTX_set(mont.get(), b.get(), ctx()));
  1012. ERR_clear_error();
  1013. EXPECT_FALSE(BN_mod_exp_mont(a.get(), BN_value_one(), BN_value_one(), b.get(),
  1014. ctx(), NULL));
  1015. ERR_clear_error();
  1016. EXPECT_FALSE(BN_mod_exp_mont_consttime(
  1017. a.get(), BN_value_one(), BN_value_one(), b.get(), ctx(), nullptr));
  1018. ERR_clear_error();
  1019. }
  1020. // Test that 1**0 mod 1 == 0.
  1021. TEST_F(BNTest, ExpModZero) {
  1022. bssl::UniquePtr<BIGNUM> zero(BN_new()), a(BN_new()), r(BN_new());
  1023. ASSERT_TRUE(zero);
  1024. ASSERT_TRUE(a);
  1025. ASSERT_TRUE(r);
  1026. ASSERT_TRUE(BN_rand(a.get(), 1024, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY));
  1027. BN_zero(zero.get());
  1028. ASSERT_TRUE(
  1029. BN_mod_exp(r.get(), a.get(), zero.get(), BN_value_one(), nullptr));
  1030. EXPECT_TRUE(BN_is_zero(r.get()));
  1031. ASSERT_TRUE(BN_mod_exp_mont(r.get(), a.get(), zero.get(), BN_value_one(),
  1032. nullptr, nullptr));
  1033. EXPECT_TRUE(BN_is_zero(r.get()));
  1034. ASSERT_TRUE(BN_mod_exp_mont_consttime(r.get(), a.get(), zero.get(),
  1035. BN_value_one(), nullptr, nullptr));
  1036. EXPECT_TRUE(BN_is_zero(r.get()));
  1037. ASSERT_TRUE(BN_mod_exp_mont_word(r.get(), 42, zero.get(), BN_value_one(),
  1038. nullptr, nullptr));
  1039. EXPECT_TRUE(BN_is_zero(r.get()));
  1040. }
  1041. TEST_F(BNTest, SmallPrime) {
  1042. static const unsigned kBits = 10;
  1043. bssl::UniquePtr<BIGNUM> r(BN_new());
  1044. ASSERT_TRUE(r);
  1045. ASSERT_TRUE(BN_generate_prime_ex(r.get(), static_cast<int>(kBits), 0, NULL,
  1046. NULL, NULL));
  1047. EXPECT_EQ(kBits, BN_num_bits(r.get()));
  1048. }
  1049. TEST_F(BNTest, CmpWord) {
  1050. static const BN_ULONG kMaxWord = (BN_ULONG)-1;
  1051. bssl::UniquePtr<BIGNUM> r(BN_new());
  1052. ASSERT_TRUE(r);
  1053. ASSERT_TRUE(BN_set_word(r.get(), 0));
  1054. EXPECT_EQ(BN_cmp_word(r.get(), 0), 0);
  1055. EXPECT_LT(BN_cmp_word(r.get(), 1), 0);
  1056. EXPECT_LT(BN_cmp_word(r.get(), kMaxWord), 0);
  1057. ASSERT_TRUE(BN_set_word(r.get(), 100));
  1058. EXPECT_GT(BN_cmp_word(r.get(), 0), 0);
  1059. EXPECT_GT(BN_cmp_word(r.get(), 99), 0);
  1060. EXPECT_EQ(BN_cmp_word(r.get(), 100), 0);
  1061. EXPECT_LT(BN_cmp_word(r.get(), 101), 0);
  1062. EXPECT_LT(BN_cmp_word(r.get(), kMaxWord), 0);
  1063. BN_set_negative(r.get(), 1);
  1064. EXPECT_LT(BN_cmp_word(r.get(), 0), 0);
  1065. EXPECT_LT(BN_cmp_word(r.get(), 100), 0);
  1066. EXPECT_LT(BN_cmp_word(r.get(), kMaxWord), 0);
  1067. ASSERT_TRUE(BN_set_word(r.get(), kMaxWord));
  1068. EXPECT_GT(BN_cmp_word(r.get(), 0), 0);
  1069. EXPECT_GT(BN_cmp_word(r.get(), kMaxWord - 1), 0);
  1070. EXPECT_EQ(BN_cmp_word(r.get(), kMaxWord), 0);
  1071. ASSERT_TRUE(BN_add(r.get(), r.get(), BN_value_one()));
  1072. EXPECT_GT(BN_cmp_word(r.get(), 0), 0);
  1073. EXPECT_GT(BN_cmp_word(r.get(), kMaxWord), 0);
  1074. BN_set_negative(r.get(), 1);
  1075. EXPECT_LT(BN_cmp_word(r.get(), 0), 0);
  1076. EXPECT_LT(BN_cmp_word(r.get(), kMaxWord), 0);
  1077. }
  1078. TEST_F(BNTest, BN2Dec) {
  1079. static const char *kBN2DecTests[] = {
  1080. "0",
  1081. "1",
  1082. "-1",
  1083. "100",
  1084. "-100",
  1085. "123456789012345678901234567890",
  1086. "-123456789012345678901234567890",
  1087. "123456789012345678901234567890123456789012345678901234567890",
  1088. "-123456789012345678901234567890123456789012345678901234567890",
  1089. };
  1090. for (const char *test : kBN2DecTests) {
  1091. SCOPED_TRACE(test);
  1092. bssl::UniquePtr<BIGNUM> bn;
  1093. int ret = DecimalToBIGNUM(&bn, test);
  1094. ASSERT_NE(0, ret);
  1095. bssl::UniquePtr<char> dec(BN_bn2dec(bn.get()));
  1096. ASSERT_TRUE(dec);
  1097. EXPECT_STREQ(test, dec.get());
  1098. }
  1099. }
  1100. TEST_F(BNTest, SetGetU64) {
  1101. static const struct {
  1102. const char *hex;
  1103. uint64_t value;
  1104. } kU64Tests[] = {
  1105. {"0", UINT64_C(0x0)},
  1106. {"1", UINT64_C(0x1)},
  1107. {"ffffffff", UINT64_C(0xffffffff)},
  1108. {"100000000", UINT64_C(0x100000000)},
  1109. {"ffffffffffffffff", UINT64_C(0xffffffffffffffff)},
  1110. };
  1111. for (const auto& test : kU64Tests) {
  1112. SCOPED_TRACE(test.hex);
  1113. bssl::UniquePtr<BIGNUM> bn(BN_new()), expected;
  1114. ASSERT_TRUE(bn);
  1115. ASSERT_TRUE(BN_set_u64(bn.get(), test.value));
  1116. ASSERT_TRUE(HexToBIGNUM(&expected, test.hex));
  1117. EXPECT_BIGNUMS_EQUAL("BN_set_u64", expected.get(), bn.get());
  1118. uint64_t tmp;
  1119. ASSERT_TRUE(BN_get_u64(bn.get(), &tmp));
  1120. EXPECT_EQ(test.value, tmp);
  1121. // BN_get_u64 ignores the sign bit.
  1122. BN_set_negative(bn.get(), 1);
  1123. ASSERT_TRUE(BN_get_u64(bn.get(), &tmp));
  1124. EXPECT_EQ(test.value, tmp);
  1125. }
  1126. // Test that BN_get_u64 fails on large numbers.
  1127. bssl::UniquePtr<BIGNUM> bn(BN_new());
  1128. ASSERT_TRUE(bn);
  1129. ASSERT_TRUE(BN_lshift(bn.get(), BN_value_one(), 64));
  1130. uint64_t tmp;
  1131. EXPECT_FALSE(BN_get_u64(bn.get(), &tmp));
  1132. BN_set_negative(bn.get(), 1);
  1133. EXPECT_FALSE(BN_get_u64(bn.get(), &tmp));
  1134. }
  1135. TEST_F(BNTest, Pow2) {
  1136. bssl::UniquePtr<BIGNUM> power_of_two(BN_new()), random(BN_new()),
  1137. expected(BN_new()), actual(BN_new());
  1138. ASSERT_TRUE(power_of_two);
  1139. ASSERT_TRUE(random);
  1140. ASSERT_TRUE(expected);
  1141. ASSERT_TRUE(actual);
  1142. // Choose an exponent.
  1143. for (size_t e = 3; e < 512; e += 11) {
  1144. SCOPED_TRACE(e);
  1145. // Choose a bit length for our randoms.
  1146. for (int len = 3; len < 512; len += 23) {
  1147. SCOPED_TRACE(len);
  1148. // Set power_of_two = 2^e.
  1149. ASSERT_TRUE(BN_lshift(power_of_two.get(), BN_value_one(), (int)e));
  1150. // Test BN_is_pow2 on power_of_two.
  1151. EXPECT_TRUE(BN_is_pow2(power_of_two.get()));
  1152. // Pick a large random value, ensuring it isn't a power of two.
  1153. ASSERT_TRUE(
  1154. BN_rand(random.get(), len, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ANY));
  1155. // Test BN_is_pow2 on |r|.
  1156. EXPECT_FALSE(BN_is_pow2(random.get()));
  1157. // Test BN_mod_pow2 on |r|.
  1158. ASSERT_TRUE(
  1159. BN_mod(expected.get(), random.get(), power_of_two.get(), ctx()));
  1160. ASSERT_TRUE(BN_mod_pow2(actual.get(), random.get(), e));
  1161. EXPECT_BIGNUMS_EQUAL("random (mod power_of_two)", expected.get(),
  1162. actual.get());
  1163. // Test BN_nnmod_pow2 on |r|.
  1164. ASSERT_TRUE(
  1165. BN_nnmod(expected.get(), random.get(), power_of_two.get(), ctx()));
  1166. ASSERT_TRUE(BN_nnmod_pow2(actual.get(), random.get(), e));
  1167. EXPECT_BIGNUMS_EQUAL("random (mod power_of_two), non-negative",
  1168. expected.get(), actual.get());
  1169. // Test BN_nnmod_pow2 on -|r|.
  1170. BN_set_negative(random.get(), 1);
  1171. ASSERT_TRUE(
  1172. BN_nnmod(expected.get(), random.get(), power_of_two.get(), ctx()));
  1173. ASSERT_TRUE(BN_nnmod_pow2(actual.get(), random.get(), e));
  1174. EXPECT_BIGNUMS_EQUAL("-random (mod power_of_two), non-negative",
  1175. expected.get(), actual.get());
  1176. }
  1177. }
  1178. }
  1179. static const int kPrimes[] = {
  1180. 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31,
  1181. 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79,
  1182. 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137,
  1183. 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193,
  1184. 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257,
  1185. 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317,
  1186. 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389,
  1187. 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457,
  1188. 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523,
  1189. 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601,
  1190. 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661,
  1191. 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743,
  1192. 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823,
  1193. 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887,
  1194. 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977,
  1195. 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049,
  1196. 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117,
  1197. 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213,
  1198. 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289,
  1199. 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373,
  1200. 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453,
  1201. 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531,
  1202. 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607,
  1203. 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693,
  1204. 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777,
  1205. 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871,
  1206. 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951,
  1207. 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029,
  1208. 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113,
  1209. 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213,
  1210. 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293,
  1211. 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377,
  1212. 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441, 2447,
  1213. 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551,
  1214. 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659,
  1215. 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713,
  1216. 2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797,
  1217. 2801, 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887,
  1218. 2897, 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971,
  1219. 2999, 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079,
  1220. 3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187,
  1221. 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271,
  1222. 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359,
  1223. 3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461,
  1224. 3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533, 3539,
  1225. 3541, 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607, 3613, 3617,
  1226. 3623, 3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691, 3697, 3701,
  1227. 3709, 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797,
  1228. 3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889,
  1229. 3907, 3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989,
  1230. 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, 4073,
  1231. 4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, 4153, 4157,
  1232. 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243, 4253,
  1233. 4259, 4261, 4271, 4273, 4283, 4289, 4297, 4327, 4337, 4339, 4349,
  1234. 4357, 4363, 4373, 4391, 4397, 4409, 4421, 4423, 4441, 4447, 4451,
  1235. 4457, 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519, 4523, 4547,
  1236. 4549, 4561, 4567, 4583, 4591, 4597, 4603, 4621, 4637, 4639, 4643,
  1237. 4649, 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729,
  1238. 4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817,
  1239. 4831, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937,
  1240. 4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999, 5003, 5009,
  1241. 5011, 5021, 5023, 5039, 5051, 5059, 5077, 5081, 5087, 5099, 5101,
  1242. 5107, 5113, 5119, 5147, 5153, 5167, 5171, 5179, 5189, 5197, 5209,
  1243. 5227, 5231, 5233, 5237, 5261, 5273, 5279, 5281, 5297, 5303, 5309,
  1244. 5323, 5333, 5347, 5351, 5381, 5387, 5393, 5399, 5407, 5413, 5417,
  1245. 5419, 5431, 5437, 5441, 5443, 5449, 5471, 5477, 5479, 5483, 5501,
  1246. 5503, 5507, 5519, 5521, 5527, 5531, 5557, 5563, 5569, 5573, 5581,
  1247. 5591, 5623, 5639, 5641, 5647, 5651, 5653, 5657, 5659, 5669, 5683,
  1248. 5689, 5693, 5701, 5711, 5717, 5737, 5741, 5743, 5749, 5779, 5783,
  1249. 5791, 5801, 5807, 5813, 5821, 5827, 5839, 5843, 5849, 5851, 5857,
  1250. 5861, 5867, 5869, 5879, 5881, 5897, 5903, 5923, 5927, 5939, 5953,
  1251. 5981, 5987, 6007, 6011, 6029, 6037, 6043, 6047, 6053, 6067, 6073,
  1252. 6079, 6089, 6091, 6101, 6113, 6121, 6131, 6133, 6143, 6151, 6163,
  1253. 6173, 6197, 6199, 6203, 6211, 6217, 6221, 6229, 6247, 6257, 6263,
  1254. 6269, 6271, 6277, 6287, 6299, 6301, 6311, 6317, 6323, 6329, 6337,
  1255. 6343, 6353, 6359, 6361, 6367, 6373, 6379, 6389, 6397, 6421, 6427,
  1256. 6449, 6451, 6469, 6473, 6481, 6491, 6521, 6529, 6547, 6551, 6553,
  1257. 6563, 6569, 6571, 6577, 6581, 6599, 6607, 6619, 6637, 6653, 6659,
  1258. 6661, 6673, 6679, 6689, 6691, 6701, 6703, 6709, 6719, 6733, 6737,
  1259. 6761, 6763, 6779, 6781, 6791, 6793, 6803, 6823, 6827, 6829, 6833,
  1260. 6841, 6857, 6863, 6869, 6871, 6883, 6899, 6907, 6911, 6917, 6947,
  1261. 6949, 6959, 6961, 6967, 6971, 6977, 6983, 6991, 6997, 7001, 7013,
  1262. 7019, 7027, 7039, 7043, 7057, 7069, 7079, 7103, 7109, 7121, 7127,
  1263. 7129, 7151, 7159, 7177, 7187, 7193, 7207, 7211, 7213, 7219, 7229,
  1264. 7237, 7243, 7247, 7253, 7283, 7297, 7307, 7309, 7321, 7331, 7333,
  1265. 7349, 7351, 7369, 7393, 7411, 7417, 7433, 7451, 7457, 7459, 7477,
  1266. 7481, 7487, 7489, 7499, 7507, 7517, 7523, 7529, 7537, 7541, 7547,
  1267. 7549, 7559, 7561, 7573, 7577, 7583, 7589, 7591, 7603, 7607, 7621,
  1268. 7639, 7643, 7649, 7669, 7673, 7681, 7687, 7691, 7699, 7703, 7717,
  1269. 7723, 7727, 7741, 7753, 7757, 7759, 7789, 7793, 7817, 7823, 7829,
  1270. 7841, 7853, 7867, 7873, 7877, 7879, 7883, 7901, 7907, 7919, 7927,
  1271. 7933, 7937, 7949, 7951, 7963, 7993, 8009, 8011, 8017, 8039, 8053,
  1272. 8059, 8069, 8081, 8087, 8089, 8093, 8101, 8111, 8117, 8123, 8147,
  1273. 8161, 8167, 8171, 8179, 8191, 8209, 8219, 8221, 8231, 8233, 8237,
  1274. 8243, 8263, 8269, 8273, 8287, 8291, 8293, 8297, 8311, 8317, 8329,
  1275. 8353, 8363, 8369, 8377, 8387, 8389, 8419, 8423, 8429, 8431, 8443,
  1276. 8447, 8461, 8467, 8501, 8513, 8521, 8527, 8537, 8539, 8543, 8563,
  1277. 8573, 8581, 8597, 8599, 8609, 8623, 8627, 8629, 8641, 8647, 8663,
  1278. 8669, 8677, 8681, 8689, 8693, 8699, 8707, 8713, 8719, 8731, 8737,
  1279. 8741, 8747, 8753, 8761, 8779, 8783, 8803, 8807, 8819, 8821, 8831,
  1280. 8837, 8839, 8849, 8861, 8863, 8867, 8887, 8893, 8923, 8929, 8933,
  1281. 8941, 8951, 8963, 8969, 8971, 8999, 9001, 9007, 9011, 9013, 9029,
  1282. 9041, 9043, 9049, 9059, 9067, 9091, 9103, 9109, 9127, 9133, 9137,
  1283. 9151, 9157, 9161, 9173, 9181, 9187, 9199, 9203, 9209, 9221, 9227,
  1284. 9239, 9241, 9257, 9277, 9281, 9283, 9293, 9311, 9319, 9323, 9337,
  1285. 9341, 9343, 9349, 9371, 9377, 9391, 9397, 9403, 9413, 9419, 9421,
  1286. 9431, 9433, 9437, 9439, 9461, 9463, 9467, 9473, 9479, 9491, 9497,
  1287. 9511, 9521, 9533, 9539, 9547, 9551, 9587, 9601, 9613, 9619, 9623,
  1288. 9629, 9631, 9643, 9649, 9661, 9677, 9679, 9689, 9697, 9719, 9721,
  1289. 9733, 9739, 9743, 9749, 9767, 9769, 9781, 9787, 9791, 9803, 9811,
  1290. 9817, 9829, 9833, 9839, 9851, 9857, 9859, 9871, 9883, 9887, 9901,
  1291. 9907, 9923, 9929, 9931, 9941, 9949, 9967, 9973, 10007, 10009, 10037,
  1292. 10039, 10061, 10067, 10069, 10079, 10091, 10093, 10099, 10103, 10111, 10133,
  1293. 10139, 10141, 10151, 10159, 10163, 10169, 10177, 10181, 10193, 10211, 10223,
  1294. 10243, 10247, 10253, 10259, 10267, 10271, 10273, 10289, 10301, 10303, 10313,
  1295. 10321, 10331, 10333, 10337, 10343, 10357, 10369, 10391, 10399, 10427, 10429,
  1296. 10433, 10453, 10457, 10459, 10463, 10477, 10487, 10499, 10501, 10513, 10529,
  1297. 10531, 10559, 10567, 10589, 10597, 10601, 10607, 10613, 10627, 10631, 10639,
  1298. 10651, 10657, 10663, 10667, 10687, 10691, 10709, 10711, 10723, 10729, 10733,
  1299. 10739, 10753, 10771, 10781, 10789, 10799, 10831, 10837, 10847, 10853, 10859,
  1300. 10861, 10867, 10883, 10889, 10891, 10903, 10909, 10937, 10939, 10949, 10957,
  1301. 10973, 10979, 10987, 10993, 11003, 11027, 11047, 11057, 11059, 11069, 11071,
  1302. 11083, 11087, 11093, 11113, 11117, 11119, 11131, 11149, 11159, 11161, 11171,
  1303. 11173, 11177, 11197, 11213, 11239, 11243, 11251, 11257, 11261, 11273, 11279,
  1304. 11287, 11299, 11311, 11317, 11321, 11329, 11351, 11353, 11369, 11383, 11393,
  1305. 11399, 11411, 11423, 11437, 11443, 11447, 11467, 11471, 11483, 11489, 11491,
  1306. 11497, 11503, 11519, 11527, 11549, 11551, 11579, 11587, 11593, 11597, 11617,
  1307. 11621, 11633, 11657, 11677, 11681, 11689, 11699, 11701, 11717, 11719, 11731,
  1308. 11743, 11777, 11779, 11783, 11789, 11801, 11807, 11813, 11821, 11827, 11831,
  1309. 11833, 11839, 11863, 11867, 11887, 11897, 11903, 11909, 11923, 11927, 11933,
  1310. 11939, 11941, 11953, 11959, 11969, 11971, 11981, 11987, 12007, 12011, 12037,
  1311. 12041, 12043, 12049, 12071, 12073, 12097, 12101, 12107, 12109, 12113, 12119,
  1312. 12143, 12149, 12157, 12161, 12163, 12197, 12203, 12211, 12227, 12239, 12241,
  1313. 12251, 12253, 12263, 12269, 12277, 12281, 12289, 12301, 12323, 12329, 12343,
  1314. 12347, 12373, 12377, 12379, 12391, 12401, 12409, 12413, 12421, 12433, 12437,
  1315. 12451, 12457, 12473, 12479, 12487, 12491, 12497, 12503, 12511, 12517, 12527,
  1316. 12539, 12541, 12547, 12553, 12569, 12577, 12583, 12589, 12601, 12611, 12613,
  1317. 12619, 12637, 12641, 12647, 12653, 12659, 12671, 12689, 12697, 12703, 12713,
  1318. 12721, 12739, 12743, 12757, 12763, 12781, 12791, 12799, 12809, 12821, 12823,
  1319. 12829, 12841, 12853, 12889, 12893, 12899, 12907, 12911, 12917, 12919, 12923,
  1320. 12941, 12953, 12959, 12967, 12973, 12979, 12983, 13001, 13003, 13007, 13009,
  1321. 13033, 13037, 13043, 13049, 13063, 13093, 13099, 13103, 13109, 13121, 13127,
  1322. 13147, 13151, 13159, 13163, 13171, 13177, 13183, 13187, 13217, 13219, 13229,
  1323. 13241, 13249, 13259, 13267, 13291, 13297, 13309, 13313, 13327, 13331, 13337,
  1324. 13339, 13367, 13381, 13397, 13399, 13411, 13417, 13421, 13441, 13451, 13457,
  1325. 13463, 13469, 13477, 13487, 13499, 13513, 13523, 13537, 13553, 13567, 13577,
  1326. 13591, 13597, 13613, 13619, 13627, 13633, 13649, 13669, 13679, 13681, 13687,
  1327. 13691, 13693, 13697, 13709, 13711, 13721, 13723, 13729, 13751, 13757, 13759,
  1328. 13763, 13781, 13789, 13799, 13807, 13829, 13831, 13841, 13859, 13873, 13877,
  1329. 13879, 13883, 13901, 13903, 13907, 13913, 13921, 13931, 13933, 13963, 13967,
  1330. 13997, 13999, 14009, 14011, 14029, 14033, 14051, 14057, 14071, 14081, 14083,
  1331. 14087, 14107, 14143, 14149, 14153, 14159, 14173, 14177, 14197, 14207, 14221,
  1332. 14243, 14249, 14251, 14281, 14293, 14303, 14321, 14323, 14327, 14341, 14347,
  1333. 14369, 14387, 14389, 14401, 14407, 14411, 14419, 14423, 14431, 14437, 14447,
  1334. 14449, 14461, 14479, 14489, 14503, 14519, 14533, 14537, 14543, 14549, 14551,
  1335. 14557, 14561, 14563, 14591, 14593, 14621, 14627, 14629, 14633, 14639, 14653,
  1336. 14657, 14669, 14683, 14699, 14713, 14717, 14723, 14731, 14737, 14741, 14747,
  1337. 14753, 14759, 14767, 14771, 14779, 14783, 14797, 14813, 14821, 14827, 14831,
  1338. 14843, 14851, 14867, 14869, 14879, 14887, 14891, 14897, 14923, 14929, 14939,
  1339. 14947, 14951, 14957, 14969, 14983, 15013, 15017, 15031, 15053, 15061, 15073,
  1340. 15077, 15083, 15091, 15101, 15107, 15121, 15131, 15137, 15139, 15149, 15161,
  1341. 15173, 15187, 15193, 15199, 15217, 15227, 15233, 15241, 15259, 15263, 15269,
  1342. 15271, 15277, 15287, 15289, 15299, 15307, 15313, 15319, 15329, 15331, 15349,
  1343. 15359, 15361, 15373, 15377, 15383, 15391, 15401, 15413, 15427, 15439, 15443,
  1344. 15451, 15461, 15467, 15473, 15493, 15497, 15511, 15527, 15541, 15551, 15559,
  1345. 15569, 15581, 15583, 15601, 15607, 15619, 15629, 15641, 15643, 15647, 15649,
  1346. 15661, 15667, 15671, 15679, 15683, 15727, 15731, 15733, 15737, 15739, 15749,
  1347. 15761, 15767, 15773, 15787, 15791, 15797, 15803, 15809, 15817, 15823, 15859,
  1348. 15877, 15881, 15887, 15889, 15901, 15907, 15913, 15919, 15923, 15937, 15959,
  1349. 15971, 15973, 15991, 16001, 16007, 16033, 16057, 16061, 16063, 16067, 16069,
  1350. 16073, 16087, 16091, 16097, 16103, 16111, 16127, 16139, 16141, 16183, 16187,
  1351. 16189, 16193, 16217, 16223, 16229, 16231, 16249, 16253, 16267, 16273, 16301,
  1352. 16319, 16333, 16339, 16349, 16361, 16363, 16369, 16381, 16411, 16417, 16421,
  1353. 16427, 16433, 16447, 16451, 16453, 16477, 16481, 16487, 16493, 16519, 16529,
  1354. 16547, 16553, 16561, 16567, 16573, 16603, 16607, 16619, 16631, 16633, 16649,
  1355. 16651, 16657, 16661, 16673, 16691, 16693, 16699, 16703, 16729, 16741, 16747,
  1356. 16759, 16763, 16787, 16811, 16823, 16829, 16831, 16843, 16871, 16879, 16883,
  1357. 16889, 16901, 16903, 16921, 16927, 16931, 16937, 16943, 16963, 16979, 16981,
  1358. 16987, 16993, 17011, 17021, 17027, 17029, 17033, 17041, 17047, 17053, 17077,
  1359. 17093, 17099, 17107, 17117, 17123, 17137, 17159, 17167, 17183, 17189, 17191,
  1360. 17203, 17207, 17209, 17231, 17239, 17257, 17291, 17293, 17299, 17317, 17321,
  1361. 17327, 17333, 17341, 17351, 17359, 17377, 17383, 17387, 17389, 17393, 17401,
  1362. 17417, 17419, 17431, 17443, 17449, 17467, 17471, 17477, 17483, 17489, 17491,
  1363. 17497, 17509, 17519, 17539, 17551, 17569, 17573, 17579, 17581, 17597, 17599,
  1364. 17609, 17623, 17627, 17657, 17659, 17669, 17681, 17683, 17707, 17713, 17729,
  1365. 17737, 17747, 17749, 17761, 17783, 17789, 17791, 17807, 17827, 17837, 17839,
  1366. 17851, 17863, 17881, 17891, 17903, 17909, 17911, 17921, 17923, 17929, 17939,
  1367. 17957, 17959, 17971, 17977, 17981, 17987, 17989, 18013, 18041, 18043, 18047,
  1368. 18049, 18059, 18061, 18077, 18089, 18097, 18119, 18121, 18127, 18131, 18133,
  1369. 18143, 18149, 18169, 18181, 18191, 18199, 18211, 18217, 18223, 18229, 18233,
  1370. 18251, 18253, 18257, 18269, 18287, 18289, 18301, 18307, 18311, 18313, 18329,
  1371. 18341, 18353, 18367, 18371, 18379, 18397, 18401, 18413, 18427, 18433, 18439,
  1372. 18443, 18451, 18457, 18461, 18481, 18493, 18503, 18517, 18521, 18523, 18539,
  1373. 18541, 18553, 18583, 18587, 18593, 18617, 18637, 18661, 18671, 18679, 18691,
  1374. 18701, 18713, 18719, 18731, 18743, 18749, 18757, 18773, 18787, 18793, 18797,
  1375. 18803, 18839, 18859, 18869, 18899, 18911, 18913, 18917, 18919, 18947, 18959,
  1376. 18973, 18979, 19001, 19009, 19013, 19031, 19037, 19051, 19069, 19073, 19079,
  1377. 19081, 19087, 19121, 19139, 19141, 19157, 19163, 19181, 19183, 19207, 19211,
  1378. 19213, 19219, 19231, 19237, 19249, 19259, 19267, 19273, 19289, 19301, 19309,
  1379. 19319, 19333, 19373, 19379, 19381, 19387, 19391, 19403, 19417, 19421, 19423,
  1380. 19427, 19429, 19433, 19441, 19447, 19457, 19463, 19469, 19471, 19477, 19483,
  1381. 19489, 19501, 19507, 19531, 19541, 19543, 19553, 19559, 19571, 19577, 19583,
  1382. 19597, 19603, 19609, 19661, 19681, 19687, 19697, 19699, 19709, 19717, 19727,
  1383. 19739, 19751, 19753, 19759, 19763, 19777, 19793, 19801, 19813, 19819, 19841,
  1384. 19843, 19853, 19861, 19867, 19889, 19891, 19913, 19919, 19927, 19937, 19949,
  1385. 19961, 19963, 19973, 19979, 19991, 19993, 19997,
  1386. };
  1387. TEST_F(BNTest, PrimeChecking) {
  1388. bssl::UniquePtr<BIGNUM> p(BN_new());
  1389. ASSERT_TRUE(p);
  1390. int is_probably_prime_1 = 0, is_probably_prime_2 = 0;
  1391. const int max_prime = kPrimes[OPENSSL_ARRAY_SIZE(kPrimes)-1];
  1392. size_t next_prime_index = 0;
  1393. for (int i = 0; i <= max_prime; i++) {
  1394. SCOPED_TRACE(i);
  1395. bool is_prime = false;
  1396. if (i == kPrimes[next_prime_index]) {
  1397. is_prime = true;
  1398. next_prime_index++;
  1399. }
  1400. ASSERT_TRUE(BN_set_word(p.get(), i));
  1401. ASSERT_TRUE(BN_primality_test(
  1402. &is_probably_prime_1, p.get(), BN_prime_checks, ctx(),
  1403. false /* do_trial_division */, nullptr /* callback */));
  1404. EXPECT_EQ(is_prime ? 1 : 0, is_probably_prime_1);
  1405. ASSERT_TRUE(BN_primality_test(
  1406. &is_probably_prime_2, p.get(), BN_prime_checks, ctx(),
  1407. true /* do_trial_division */, nullptr /* callback */));
  1408. EXPECT_EQ(is_prime ? 1 : 0, is_probably_prime_2);
  1409. }
  1410. // Negative numbers are not prime.
  1411. ASSERT_TRUE(BN_set_word(p.get(), 7));
  1412. BN_set_negative(p.get(), 1);
  1413. ASSERT_TRUE(BN_primality_test(&is_probably_prime_1, p.get(), BN_prime_checks,
  1414. ctx(), false /* do_trial_division */,
  1415. nullptr /* callback */));
  1416. EXPECT_EQ(0, is_probably_prime_1);
  1417. ASSERT_TRUE(BN_primality_test(&is_probably_prime_2, p.get(), BN_prime_checks,
  1418. ctx(), true /* do_trial_division */,
  1419. nullptr /* callback */));
  1420. EXPECT_EQ(0, is_probably_prime_2);
  1421. }