No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

2488 líneas
96 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  59. *
  60. * Portions of the attached software ("Contribution") are developed by
  61. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  62. *
  63. * The Contribution is licensed pursuant to the Eric Young open source
  64. * license provided above.
  65. *
  66. * The binary polynomial arithmetic software is originally written by
  67. * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems
  68. * Laboratories. */
  69. #include <assert.h>
  70. #include <errno.h>
  71. #include <limits.h>
  72. #include <stdio.h>
  73. #include <string.h>
  74. #include <utility>
  75. #include <gtest/gtest.h>
  76. #include <openssl/bio.h>
  77. #include <openssl/bn.h>
  78. #include <openssl/bytestring.h>
  79. #include <openssl/crypto.h>
  80. #include <openssl/err.h>
  81. #include <openssl/mem.h>
  82. #include <openssl/rand.h>
  83. #include "./internal.h"
  84. #include "./rsaz_exp.h"
  85. #include "../../internal.h"
  86. #include "../../test/abi_test.h"
  87. #include "../../test/file_test.h"
  88. #include "../../test/test_util.h"
  89. static int HexToBIGNUM(bssl::UniquePtr<BIGNUM> *out, const char *in) {
  90. BIGNUM *raw = NULL;
  91. int ret = BN_hex2bn(&raw, in);
  92. out->reset(raw);
  93. return ret;
  94. }
  95. // A BIGNUMFileTest wraps a FileTest to give |BIGNUM| values and also allows
  96. // injecting oversized |BIGNUM|s.
  97. class BIGNUMFileTest {
  98. public:
  99. BIGNUMFileTest(FileTest *t, unsigned large_mask)
  100. : t_(t), large_mask_(large_mask), num_bignums_(0) {}
  101. unsigned num_bignums() const { return num_bignums_; }
  102. bssl::UniquePtr<BIGNUM> GetBIGNUM(const char *attribute) {
  103. return GetBIGNUMImpl(attribute, true /* resize */);
  104. }
  105. bool GetInt(int *out, const char *attribute) {
  106. bssl::UniquePtr<BIGNUM> ret =
  107. GetBIGNUMImpl(attribute, false /* don't resize */);
  108. if (!ret) {
  109. return false;
  110. }
  111. BN_ULONG word = BN_get_word(ret.get());
  112. if (word > INT_MAX) {
  113. return false;
  114. }
  115. *out = static_cast<int>(word);
  116. return true;
  117. }
  118. private:
  119. bssl::UniquePtr<BIGNUM> GetBIGNUMImpl(const char *attribute, bool resize) {
  120. std::string hex;
  121. if (!t_->GetAttribute(&hex, attribute)) {
  122. return nullptr;
  123. }
  124. bssl::UniquePtr<BIGNUM> ret;
  125. if (HexToBIGNUM(&ret, hex.c_str()) != static_cast<int>(hex.size())) {
  126. t_->PrintLine("Could not decode '%s'.", hex.c_str());
  127. return nullptr;
  128. }
  129. if (resize) {
  130. // Test with an oversized |BIGNUM| if necessary.
  131. if ((large_mask_ & (1 << num_bignums_)) &&
  132. !bn_resize_words(ret.get(), ret->width * 2 + 1)) {
  133. return nullptr;
  134. }
  135. num_bignums_++;
  136. }
  137. return ret;
  138. }
  139. FileTest *t_;
  140. unsigned large_mask_;
  141. unsigned num_bignums_;
  142. };
  143. static testing::AssertionResult AssertBIGNUMSEqual(
  144. const char *operation_expr, const char *expected_expr,
  145. const char *actual_expr, const char *operation, const BIGNUM *expected,
  146. const BIGNUM *actual) {
  147. if (BN_cmp(expected, actual) == 0) {
  148. return testing::AssertionSuccess();
  149. }
  150. bssl::UniquePtr<char> expected_str(BN_bn2hex(expected));
  151. bssl::UniquePtr<char> actual_str(BN_bn2hex(actual));
  152. if (!expected_str || !actual_str) {
  153. return testing::AssertionFailure() << "Error converting BIGNUMs to hex";
  154. }
  155. return testing::AssertionFailure()
  156. << "Wrong value for " << operation
  157. << "\nActual: " << actual_str.get() << " (" << actual_expr
  158. << ")\nExpected: " << expected_str.get() << " (" << expected_expr
  159. << ")";
  160. }
  161. #define EXPECT_BIGNUMS_EQUAL(op, a, b) \
  162. EXPECT_PRED_FORMAT3(AssertBIGNUMSEqual, op, a, b)
  163. static void TestSum(BIGNUMFileTest *t, BN_CTX *ctx) {
  164. bssl::UniquePtr<BIGNUM> a = t->GetBIGNUM("A");
  165. bssl::UniquePtr<BIGNUM> b = t->GetBIGNUM("B");
  166. bssl::UniquePtr<BIGNUM> sum = t->GetBIGNUM("Sum");
  167. ASSERT_TRUE(a);
  168. ASSERT_TRUE(b);
  169. ASSERT_TRUE(sum);
  170. bssl::UniquePtr<BIGNUM> ret(BN_new());
  171. ASSERT_TRUE(ret);
  172. ASSERT_TRUE(BN_add(ret.get(), a.get(), b.get()));
  173. EXPECT_BIGNUMS_EQUAL("A + B", sum.get(), ret.get());
  174. ASSERT_TRUE(BN_sub(ret.get(), sum.get(), a.get()));
  175. EXPECT_BIGNUMS_EQUAL("Sum - A", b.get(), ret.get());
  176. ASSERT_TRUE(BN_sub(ret.get(), sum.get(), b.get()));
  177. EXPECT_BIGNUMS_EQUAL("Sum - B", a.get(), ret.get());
  178. // Test that the functions work when |r| and |a| point to the same |BIGNUM|,
  179. // or when |r| and |b| point to the same |BIGNUM|. TODO: Test the case where
  180. // all of |r|, |a|, and |b| point to the same |BIGNUM|.
  181. ASSERT_TRUE(BN_copy(ret.get(), a.get()));
  182. ASSERT_TRUE(BN_add(ret.get(), ret.get(), b.get()));
  183. EXPECT_BIGNUMS_EQUAL("A + B (r is a)", sum.get(), ret.get());
  184. ASSERT_TRUE(BN_copy(ret.get(), b.get()));
  185. ASSERT_TRUE(BN_add(ret.get(), a.get(), ret.get()));
  186. EXPECT_BIGNUMS_EQUAL("A + B (r is b)", sum.get(), ret.get());
  187. ASSERT_TRUE(BN_copy(ret.get(), sum.get()));
  188. ASSERT_TRUE(BN_sub(ret.get(), ret.get(), a.get()));
  189. EXPECT_BIGNUMS_EQUAL("Sum - A (r is a)", b.get(), ret.get());
  190. ASSERT_TRUE(BN_copy(ret.get(), a.get()));
  191. ASSERT_TRUE(BN_sub(ret.get(), sum.get(), ret.get()));
  192. EXPECT_BIGNUMS_EQUAL("Sum - A (r is b)", b.get(), ret.get());
  193. ASSERT_TRUE(BN_copy(ret.get(), sum.get()));
  194. ASSERT_TRUE(BN_sub(ret.get(), ret.get(), b.get()));
  195. EXPECT_BIGNUMS_EQUAL("Sum - B (r is a)", a.get(), ret.get());
  196. ASSERT_TRUE(BN_copy(ret.get(), b.get()));
  197. ASSERT_TRUE(BN_sub(ret.get(), sum.get(), ret.get()));
  198. EXPECT_BIGNUMS_EQUAL("Sum - B (r is b)", a.get(), ret.get());
  199. // Test |BN_uadd| and |BN_usub| with the prerequisites they are documented as
  200. // having. Note that these functions are frequently used when the
  201. // prerequisites don't hold. In those cases, they are supposed to work as if
  202. // the prerequisite hold, but we don't test that yet. TODO: test that.
  203. if (!BN_is_negative(a.get()) && !BN_is_negative(b.get())) {
  204. ASSERT_TRUE(BN_uadd(ret.get(), a.get(), b.get()));
  205. EXPECT_BIGNUMS_EQUAL("A +u B", sum.get(), ret.get());
  206. ASSERT_TRUE(BN_usub(ret.get(), sum.get(), a.get()));
  207. EXPECT_BIGNUMS_EQUAL("Sum -u A", b.get(), ret.get());
  208. ASSERT_TRUE(BN_usub(ret.get(), sum.get(), b.get()));
  209. EXPECT_BIGNUMS_EQUAL("Sum -u B", a.get(), ret.get());
  210. // Test that the functions work when |r| and |a| point to the same |BIGNUM|,
  211. // or when |r| and |b| point to the same |BIGNUM|. TODO: Test the case where
  212. // all of |r|, |a|, and |b| point to the same |BIGNUM|.
  213. ASSERT_TRUE(BN_copy(ret.get(), a.get()));
  214. ASSERT_TRUE(BN_uadd(ret.get(), ret.get(), b.get()));
  215. EXPECT_BIGNUMS_EQUAL("A +u B (r is a)", sum.get(), ret.get());
  216. ASSERT_TRUE(BN_copy(ret.get(), b.get()));
  217. ASSERT_TRUE(BN_uadd(ret.get(), a.get(), ret.get()));
  218. EXPECT_BIGNUMS_EQUAL("A +u B (r is b)", sum.get(), ret.get());
  219. ASSERT_TRUE(BN_copy(ret.get(), sum.get()));
  220. ASSERT_TRUE(BN_usub(ret.get(), ret.get(), a.get()));
  221. EXPECT_BIGNUMS_EQUAL("Sum -u A (r is a)", b.get(), ret.get());
  222. ASSERT_TRUE(BN_copy(ret.get(), a.get()));
  223. ASSERT_TRUE(BN_usub(ret.get(), sum.get(), ret.get()));
  224. EXPECT_BIGNUMS_EQUAL("Sum -u A (r is b)", b.get(), ret.get());
  225. ASSERT_TRUE(BN_copy(ret.get(), sum.get()));
  226. ASSERT_TRUE(BN_usub(ret.get(), ret.get(), b.get()));
  227. EXPECT_BIGNUMS_EQUAL("Sum -u B (r is a)", a.get(), ret.get());
  228. ASSERT_TRUE(BN_copy(ret.get(), b.get()));
  229. ASSERT_TRUE(BN_usub(ret.get(), sum.get(), ret.get()));
  230. EXPECT_BIGNUMS_EQUAL("Sum -u B (r is b)", a.get(), ret.get());
  231. ASSERT_TRUE(bn_abs_sub_consttime(ret.get(), sum.get(), a.get(), ctx));
  232. EXPECT_BIGNUMS_EQUAL("|Sum - A|", b.get(), ret.get());
  233. ASSERT_TRUE(bn_abs_sub_consttime(ret.get(), a.get(), sum.get(), ctx));
  234. EXPECT_BIGNUMS_EQUAL("|A - Sum|", b.get(), ret.get());
  235. ASSERT_TRUE(bn_abs_sub_consttime(ret.get(), sum.get(), b.get(), ctx));
  236. EXPECT_BIGNUMS_EQUAL("|Sum - B|", a.get(), ret.get());
  237. ASSERT_TRUE(bn_abs_sub_consttime(ret.get(), b.get(), sum.get(), ctx));
  238. EXPECT_BIGNUMS_EQUAL("|B - Sum|", a.get(), ret.get());
  239. }
  240. // Test with |BN_add_word| and |BN_sub_word| if |b| is small enough.
  241. BN_ULONG b_word = BN_get_word(b.get());
  242. if (!BN_is_negative(b.get()) && b_word != (BN_ULONG)-1) {
  243. ASSERT_TRUE(BN_copy(ret.get(), a.get()));
  244. ASSERT_TRUE(BN_add_word(ret.get(), b_word));
  245. EXPECT_BIGNUMS_EQUAL("A + B (word)", sum.get(), ret.get());
  246. ASSERT_TRUE(BN_copy(ret.get(), sum.get()));
  247. ASSERT_TRUE(BN_sub_word(ret.get(), b_word));
  248. EXPECT_BIGNUMS_EQUAL("Sum - B (word)", a.get(), ret.get());
  249. }
  250. }
  251. static void TestLShift1(BIGNUMFileTest *t, BN_CTX *ctx) {
  252. bssl::UniquePtr<BIGNUM> a = t->GetBIGNUM("A");
  253. bssl::UniquePtr<BIGNUM> lshift1 = t->GetBIGNUM("LShift1");
  254. bssl::UniquePtr<BIGNUM> zero(BN_new());
  255. ASSERT_TRUE(a);
  256. ASSERT_TRUE(lshift1);
  257. ASSERT_TRUE(zero);
  258. BN_zero(zero.get());
  259. bssl::UniquePtr<BIGNUM> ret(BN_new()), two(BN_new()), remainder(BN_new());
  260. ASSERT_TRUE(ret);
  261. ASSERT_TRUE(two);
  262. ASSERT_TRUE(remainder);
  263. ASSERT_TRUE(BN_set_word(two.get(), 2));
  264. ASSERT_TRUE(BN_add(ret.get(), a.get(), a.get()));
  265. EXPECT_BIGNUMS_EQUAL("A + A", lshift1.get(), ret.get());
  266. ASSERT_TRUE(BN_mul(ret.get(), a.get(), two.get(), ctx));
  267. EXPECT_BIGNUMS_EQUAL("A * 2", lshift1.get(), ret.get());
  268. ASSERT_TRUE(
  269. BN_div(ret.get(), remainder.get(), lshift1.get(), two.get(), ctx));
  270. EXPECT_BIGNUMS_EQUAL("LShift1 / 2", a.get(), ret.get());
  271. EXPECT_BIGNUMS_EQUAL("LShift1 % 2", zero.get(), remainder.get());
  272. ASSERT_TRUE(BN_lshift1(ret.get(), a.get()));
  273. EXPECT_BIGNUMS_EQUAL("A << 1", lshift1.get(), ret.get());
  274. ASSERT_TRUE(BN_lshift(ret.get(), a.get(), 1));
  275. EXPECT_BIGNUMS_EQUAL("A << 1 (variable shift)", lshift1.get(), ret.get());
  276. ASSERT_TRUE(BN_rshift1(ret.get(), lshift1.get()));
  277. EXPECT_BIGNUMS_EQUAL("LShift >> 1", a.get(), ret.get());
  278. ASSERT_TRUE(BN_rshift(ret.get(), lshift1.get(), 1));
  279. EXPECT_BIGNUMS_EQUAL("LShift >> 1 (variable shift)", a.get(), ret.get());
  280. ASSERT_TRUE(bn_rshift_secret_shift(ret.get(), lshift1.get(), 1, ctx));
  281. EXPECT_BIGNUMS_EQUAL("LShift >> 1 (secret shift)", a.get(), ret.get());
  282. // Set the LSB to 1 and test rshift1 again.
  283. ASSERT_TRUE(BN_set_bit(lshift1.get(), 0));
  284. ASSERT_TRUE(
  285. BN_div(ret.get(), nullptr /* rem */, lshift1.get(), two.get(), ctx));
  286. EXPECT_BIGNUMS_EQUAL("(LShift1 | 1) / 2", a.get(), ret.get());
  287. ASSERT_TRUE(BN_rshift1(ret.get(), lshift1.get()));
  288. EXPECT_BIGNUMS_EQUAL("(LShift | 1) >> 1", a.get(), ret.get());
  289. ASSERT_TRUE(BN_rshift(ret.get(), lshift1.get(), 1));
  290. EXPECT_BIGNUMS_EQUAL("(LShift | 1) >> 1 (variable shift)", a.get(),
  291. ret.get());
  292. ASSERT_TRUE(bn_rshift_secret_shift(ret.get(), lshift1.get(), 1, ctx));
  293. EXPECT_BIGNUMS_EQUAL("(LShift | 1) >> 1 (secret shift)", a.get(), ret.get());
  294. }
  295. static void TestLShift(BIGNUMFileTest *t, BN_CTX *ctx) {
  296. bssl::UniquePtr<BIGNUM> a = t->GetBIGNUM("A");
  297. bssl::UniquePtr<BIGNUM> lshift = t->GetBIGNUM("LShift");
  298. ASSERT_TRUE(a);
  299. ASSERT_TRUE(lshift);
  300. int n = 0;
  301. ASSERT_TRUE(t->GetInt(&n, "N"));
  302. bssl::UniquePtr<BIGNUM> ret(BN_new());
  303. ASSERT_TRUE(ret);
  304. ASSERT_TRUE(BN_lshift(ret.get(), a.get(), n));
  305. EXPECT_BIGNUMS_EQUAL("A << N", lshift.get(), ret.get());
  306. ASSERT_TRUE(BN_copy(ret.get(), a.get()));
  307. ASSERT_TRUE(BN_lshift(ret.get(), ret.get(), n));
  308. EXPECT_BIGNUMS_EQUAL("A << N (in-place)", lshift.get(), ret.get());
  309. ASSERT_TRUE(BN_rshift(ret.get(), lshift.get(), n));
  310. EXPECT_BIGNUMS_EQUAL("A >> N", a.get(), ret.get());
  311. ASSERT_TRUE(bn_rshift_secret_shift(ret.get(), lshift.get(), n, ctx));
  312. EXPECT_BIGNUMS_EQUAL("A >> N (secret shift)", a.get(), ret.get());
  313. }
  314. static void TestRShift(BIGNUMFileTest *t, BN_CTX *ctx) {
  315. bssl::UniquePtr<BIGNUM> a = t->GetBIGNUM("A");
  316. bssl::UniquePtr<BIGNUM> rshift = t->GetBIGNUM("RShift");
  317. ASSERT_TRUE(a);
  318. ASSERT_TRUE(rshift);
  319. int n = 0;
  320. ASSERT_TRUE(t->GetInt(&n, "N"));
  321. bssl::UniquePtr<BIGNUM> ret(BN_new());
  322. ASSERT_TRUE(ret);
  323. ASSERT_TRUE(BN_rshift(ret.get(), a.get(), n));
  324. EXPECT_BIGNUMS_EQUAL("A >> N", rshift.get(), ret.get());
  325. ASSERT_TRUE(BN_copy(ret.get(), a.get()));
  326. ASSERT_TRUE(BN_rshift(ret.get(), ret.get(), n));
  327. EXPECT_BIGNUMS_EQUAL("A >> N (in-place)", rshift.get(), ret.get());
  328. ASSERT_TRUE(bn_rshift_secret_shift(ret.get(), a.get(), n, ctx));
  329. EXPECT_BIGNUMS_EQUAL("A >> N (secret shift)", rshift.get(), ret.get());
  330. ASSERT_TRUE(BN_copy(ret.get(), a.get()));
  331. ASSERT_TRUE(bn_rshift_secret_shift(ret.get(), ret.get(), n, ctx));
  332. EXPECT_BIGNUMS_EQUAL("A >> N (in-place secret shift)", rshift.get(),
  333. ret.get());
  334. }
  335. static void TestSquare(BIGNUMFileTest *t, BN_CTX *ctx) {
  336. bssl::UniquePtr<BIGNUM> a = t->GetBIGNUM("A");
  337. bssl::UniquePtr<BIGNUM> square = t->GetBIGNUM("Square");
  338. bssl::UniquePtr<BIGNUM> zero(BN_new());
  339. ASSERT_TRUE(a);
  340. ASSERT_TRUE(square);
  341. ASSERT_TRUE(zero);
  342. BN_zero(zero.get());
  343. bssl::UniquePtr<BIGNUM> ret(BN_new()), remainder(BN_new());
  344. ASSERT_TRUE(ret);
  345. ASSERT_TRUE(remainder);
  346. ASSERT_TRUE(BN_sqr(ret.get(), a.get(), ctx));
  347. EXPECT_BIGNUMS_EQUAL("A^2", square.get(), ret.get());
  348. ASSERT_TRUE(BN_mul(ret.get(), a.get(), a.get(), ctx));
  349. EXPECT_BIGNUMS_EQUAL("A * A", square.get(), ret.get());
  350. if (!BN_is_zero(a.get())) {
  351. ASSERT_TRUE(BN_div(ret.get(), remainder.get(), square.get(), a.get(), ctx));
  352. EXPECT_BIGNUMS_EQUAL("Square / A", a.get(), ret.get());
  353. EXPECT_BIGNUMS_EQUAL("Square % A", zero.get(), remainder.get());
  354. }
  355. BN_set_negative(a.get(), 0);
  356. ASSERT_TRUE(BN_sqrt(ret.get(), square.get(), ctx));
  357. EXPECT_BIGNUMS_EQUAL("sqrt(Square)", a.get(), ret.get());
  358. // BN_sqrt should fail on non-squares and negative numbers.
  359. if (!BN_is_zero(square.get())) {
  360. bssl::UniquePtr<BIGNUM> tmp(BN_new());
  361. ASSERT_TRUE(tmp);
  362. ASSERT_TRUE(BN_copy(tmp.get(), square.get()));
  363. BN_set_negative(tmp.get(), 1);
  364. EXPECT_FALSE(BN_sqrt(ret.get(), tmp.get(), ctx))
  365. << "BN_sqrt succeeded on a negative number";
  366. ERR_clear_error();
  367. BN_set_negative(tmp.get(), 0);
  368. ASSERT_TRUE(BN_add(tmp.get(), tmp.get(), BN_value_one()));
  369. EXPECT_FALSE(BN_sqrt(ret.get(), tmp.get(), ctx))
  370. << "BN_sqrt succeeded on a non-square";
  371. ERR_clear_error();
  372. }
  373. #if !defined(BORINGSSL_SHARED_LIBRARY)
  374. int a_width = bn_minimal_width(a.get());
  375. if (a_width <= BN_SMALL_MAX_WORDS) {
  376. for (size_t num_a = a_width; num_a <= BN_SMALL_MAX_WORDS; num_a++) {
  377. SCOPED_TRACE(num_a);
  378. size_t num_r = 2 * num_a;
  379. // Use newly-allocated buffers so ASan will catch out-of-bounds writes.
  380. std::unique_ptr<BN_ULONG[]> a_words(new BN_ULONG[num_a]),
  381. r_words(new BN_ULONG[num_r]);
  382. ASSERT_TRUE(bn_copy_words(a_words.get(), num_a, a.get()));
  383. bn_mul_small(r_words.get(), num_r, a_words.get(), num_a, a_words.get(),
  384. num_a);
  385. ASSERT_TRUE(bn_set_words(ret.get(), r_words.get(), num_r));
  386. EXPECT_BIGNUMS_EQUAL("A * A (words)", square.get(), ret.get());
  387. OPENSSL_memset(r_words.get(), 'A', num_r * sizeof(BN_ULONG));
  388. bn_sqr_small(r_words.get(), num_r, a_words.get(), num_a);
  389. ASSERT_TRUE(bn_set_words(ret.get(), r_words.get(), num_r));
  390. EXPECT_BIGNUMS_EQUAL("A^2 (words)", square.get(), ret.get());
  391. }
  392. }
  393. #endif
  394. }
  395. static void TestProduct(BIGNUMFileTest *t, BN_CTX *ctx) {
  396. bssl::UniquePtr<BIGNUM> a = t->GetBIGNUM("A");
  397. bssl::UniquePtr<BIGNUM> b = t->GetBIGNUM("B");
  398. bssl::UniquePtr<BIGNUM> product = t->GetBIGNUM("Product");
  399. bssl::UniquePtr<BIGNUM> zero(BN_new());
  400. ASSERT_TRUE(a);
  401. ASSERT_TRUE(b);
  402. ASSERT_TRUE(product);
  403. ASSERT_TRUE(zero);
  404. BN_zero(zero.get());
  405. bssl::UniquePtr<BIGNUM> ret(BN_new()), remainder(BN_new());
  406. ASSERT_TRUE(ret);
  407. ASSERT_TRUE(remainder);
  408. ASSERT_TRUE(BN_mul(ret.get(), a.get(), b.get(), ctx));
  409. EXPECT_BIGNUMS_EQUAL("A * B", product.get(), ret.get());
  410. if (!BN_is_zero(a.get())) {
  411. ASSERT_TRUE(
  412. BN_div(ret.get(), remainder.get(), product.get(), a.get(), ctx));
  413. EXPECT_BIGNUMS_EQUAL("Product / A", b.get(), ret.get());
  414. EXPECT_BIGNUMS_EQUAL("Product % A", zero.get(), remainder.get());
  415. }
  416. if (!BN_is_zero(b.get())) {
  417. ASSERT_TRUE(
  418. BN_div(ret.get(), remainder.get(), product.get(), b.get(), ctx));
  419. EXPECT_BIGNUMS_EQUAL("Product / B", a.get(), ret.get());
  420. EXPECT_BIGNUMS_EQUAL("Product % B", zero.get(), remainder.get());
  421. }
  422. #if !defined(BORINGSSL_SHARED_LIBRARY)
  423. BN_set_negative(a.get(), 0);
  424. BN_set_negative(b.get(), 0);
  425. BN_set_negative(product.get(), 0);
  426. int a_width = bn_minimal_width(a.get());
  427. int b_width = bn_minimal_width(b.get());
  428. if (a_width <= BN_SMALL_MAX_WORDS && b_width <= BN_SMALL_MAX_WORDS) {
  429. for (size_t num_a = static_cast<size_t>(a_width);
  430. num_a <= BN_SMALL_MAX_WORDS; num_a++) {
  431. SCOPED_TRACE(num_a);
  432. for (size_t num_b = static_cast<size_t>(b_width);
  433. num_b <= BN_SMALL_MAX_WORDS; num_b++) {
  434. SCOPED_TRACE(num_b);
  435. size_t num_r = num_a + num_b;
  436. // Use newly-allocated buffers so ASan will catch out-of-bounds writes.
  437. std::unique_ptr<BN_ULONG[]> a_words(new BN_ULONG[num_a]),
  438. b_words(new BN_ULONG[num_b]), r_words(new BN_ULONG[num_r]);
  439. ASSERT_TRUE(bn_copy_words(a_words.get(), num_a, a.get()));
  440. ASSERT_TRUE(bn_copy_words(b_words.get(), num_b, b.get()));
  441. bn_mul_small(r_words.get(), num_r, a_words.get(), num_a, b_words.get(),
  442. num_b);
  443. ASSERT_TRUE(bn_set_words(ret.get(), r_words.get(), num_r));
  444. EXPECT_BIGNUMS_EQUAL("A * B (words)", product.get(), ret.get());
  445. }
  446. }
  447. }
  448. #endif
  449. }
  450. static void TestQuotient(BIGNUMFileTest *t, BN_CTX *ctx) {
  451. bssl::UniquePtr<BIGNUM> a = t->GetBIGNUM("A");
  452. bssl::UniquePtr<BIGNUM> b = t->GetBIGNUM("B");
  453. bssl::UniquePtr<BIGNUM> quotient = t->GetBIGNUM("Quotient");
  454. bssl::UniquePtr<BIGNUM> remainder = t->GetBIGNUM("Remainder");
  455. ASSERT_TRUE(a);
  456. ASSERT_TRUE(b);
  457. ASSERT_TRUE(quotient);
  458. ASSERT_TRUE(remainder);
  459. bssl::UniquePtr<BIGNUM> ret(BN_new()), ret2(BN_new());
  460. ASSERT_TRUE(ret);
  461. ASSERT_TRUE(ret2);
  462. ASSERT_TRUE(BN_div(ret.get(), ret2.get(), a.get(), b.get(), ctx));
  463. EXPECT_BIGNUMS_EQUAL("A / B", quotient.get(), ret.get());
  464. EXPECT_BIGNUMS_EQUAL("A % B", remainder.get(), ret2.get());
  465. ASSERT_TRUE(BN_mul(ret.get(), quotient.get(), b.get(), ctx));
  466. ASSERT_TRUE(BN_add(ret.get(), ret.get(), remainder.get()));
  467. EXPECT_BIGNUMS_EQUAL("Quotient * B + Remainder", a.get(), ret.get());
  468. // The remaining division variants only handle a positive quotient.
  469. if (BN_is_negative(b.get())) {
  470. BN_set_negative(b.get(), 0);
  471. BN_set_negative(quotient.get(), !BN_is_negative(quotient.get()));
  472. }
  473. bssl::UniquePtr<BIGNUM> nnmod(BN_new());
  474. ASSERT_TRUE(nnmod);
  475. ASSERT_TRUE(BN_copy(nnmod.get(), remainder.get()));
  476. if (BN_is_negative(nnmod.get())) {
  477. ASSERT_TRUE(BN_add(nnmod.get(), nnmod.get(), b.get()));
  478. }
  479. ASSERT_TRUE(BN_nnmod(ret.get(), a.get(), b.get(), ctx));
  480. EXPECT_BIGNUMS_EQUAL("A % B (non-negative)", nnmod.get(), ret.get());
  481. // The remaining division variants only handle a positive numerator.
  482. if (BN_is_negative(a.get())) {
  483. BN_set_negative(a.get(), 0);
  484. BN_set_negative(quotient.get(), 0);
  485. BN_set_negative(remainder.get(), 0);
  486. }
  487. // Test with |BN_mod_word| and |BN_div_word| if the divisor is small enough.
  488. BN_ULONG b_word = BN_get_word(b.get());
  489. if (b_word != (BN_ULONG)-1) {
  490. BN_ULONG remainder_word = BN_get_word(remainder.get());
  491. ASSERT_NE(remainder_word, (BN_ULONG)-1);
  492. ASSERT_TRUE(BN_copy(ret.get(), a.get()));
  493. BN_ULONG ret_word = BN_div_word(ret.get(), b_word);
  494. EXPECT_EQ(remainder_word, ret_word);
  495. EXPECT_BIGNUMS_EQUAL("A / B (word)", quotient.get(), ret.get());
  496. ret_word = BN_mod_word(a.get(), b_word);
  497. EXPECT_EQ(remainder_word, ret_word);
  498. if (b_word <= 0xffff) {
  499. EXPECT_EQ(remainder_word, bn_mod_u16_consttime(a.get(), b_word));
  500. }
  501. }
  502. ASSERT_TRUE(bn_div_consttime(ret.get(), ret2.get(), a.get(), b.get(), ctx));
  503. EXPECT_BIGNUMS_EQUAL("A / B (constant-time)", quotient.get(), ret.get());
  504. EXPECT_BIGNUMS_EQUAL("A % B (constant-time)", remainder.get(), ret2.get());
  505. }
  506. static void TestModMul(BIGNUMFileTest *t, BN_CTX *ctx) {
  507. bssl::UniquePtr<BIGNUM> a = t->GetBIGNUM("A");
  508. bssl::UniquePtr<BIGNUM> b = t->GetBIGNUM("B");
  509. bssl::UniquePtr<BIGNUM> m = t->GetBIGNUM("M");
  510. bssl::UniquePtr<BIGNUM> mod_mul = t->GetBIGNUM("ModMul");
  511. ASSERT_TRUE(a);
  512. ASSERT_TRUE(b);
  513. ASSERT_TRUE(m);
  514. ASSERT_TRUE(mod_mul);
  515. bssl::UniquePtr<BIGNUM> ret(BN_new());
  516. ASSERT_TRUE(ret);
  517. ASSERT_TRUE(BN_mod_mul(ret.get(), a.get(), b.get(), m.get(), ctx));
  518. EXPECT_BIGNUMS_EQUAL("A * B (mod M)", mod_mul.get(), ret.get());
  519. if (BN_is_odd(m.get())) {
  520. // Reduce |a| and |b| and test the Montgomery version.
  521. bssl::UniquePtr<BN_MONT_CTX> mont(
  522. BN_MONT_CTX_new_for_modulus(m.get(), ctx));
  523. ASSERT_TRUE(mont);
  524. // Sanity-check that the constant-time version computes the same n0 and RR.
  525. bssl::UniquePtr<BN_MONT_CTX> mont2(
  526. BN_MONT_CTX_new_consttime(m.get(), ctx));
  527. ASSERT_TRUE(mont2);
  528. EXPECT_BIGNUMS_EQUAL("RR (mod M) (constant-time)", &mont->RR, &mont2->RR);
  529. EXPECT_EQ(mont->n0[0], mont2->n0[0]);
  530. EXPECT_EQ(mont->n0[1], mont2->n0[1]);
  531. bssl::UniquePtr<BIGNUM> a_tmp(BN_new()), b_tmp(BN_new());
  532. ASSERT_TRUE(a_tmp);
  533. ASSERT_TRUE(b_tmp);
  534. ASSERT_TRUE(BN_nnmod(a.get(), a.get(), m.get(), ctx));
  535. ASSERT_TRUE(BN_nnmod(b.get(), b.get(), m.get(), ctx));
  536. ASSERT_TRUE(BN_to_montgomery(a_tmp.get(), a.get(), mont.get(), ctx));
  537. ASSERT_TRUE(BN_to_montgomery(b_tmp.get(), b.get(), mont.get(), ctx));
  538. ASSERT_TRUE(BN_mod_mul_montgomery(ret.get(), a_tmp.get(), b_tmp.get(),
  539. mont.get(), ctx));
  540. ASSERT_TRUE(BN_from_montgomery(ret.get(), ret.get(), mont.get(), ctx));
  541. EXPECT_BIGNUMS_EQUAL("A * B (mod M) (Montgomery)", mod_mul.get(),
  542. ret.get());
  543. #if !defined(BORINGSSL_SHARED_LIBRARY)
  544. size_t m_width = static_cast<size_t>(bn_minimal_width(m.get()));
  545. if (m_width <= BN_SMALL_MAX_WORDS) {
  546. std::unique_ptr<BN_ULONG[]> a_words(new BN_ULONG[m_width]),
  547. b_words(new BN_ULONG[m_width]), r_words(new BN_ULONG[m_width]);
  548. ASSERT_TRUE(bn_copy_words(a_words.get(), m_width, a.get()));
  549. ASSERT_TRUE(bn_copy_words(b_words.get(), m_width, b.get()));
  550. bn_to_montgomery_small(a_words.get(), a_words.get(), m_width, mont.get());
  551. bn_to_montgomery_small(b_words.get(), b_words.get(), m_width, mont.get());
  552. bn_mod_mul_montgomery_small(r_words.get(), a_words.get(), b_words.get(),
  553. m_width, mont.get());
  554. // Use the second half of |tmp| so ASan will catch out-of-bounds writes.
  555. bn_from_montgomery_small(r_words.get(), r_words.get(), m_width,
  556. mont.get());
  557. ASSERT_TRUE(bn_set_words(ret.get(), r_words.get(), m_width));
  558. EXPECT_BIGNUMS_EQUAL("A * B (mod M) (Montgomery, words)", mod_mul.get(),
  559. ret.get());
  560. }
  561. #endif
  562. }
  563. }
  564. static void TestModSquare(BIGNUMFileTest *t, BN_CTX *ctx) {
  565. bssl::UniquePtr<BIGNUM> a = t->GetBIGNUM("A");
  566. bssl::UniquePtr<BIGNUM> m = t->GetBIGNUM("M");
  567. bssl::UniquePtr<BIGNUM> mod_square = t->GetBIGNUM("ModSquare");
  568. ASSERT_TRUE(a);
  569. ASSERT_TRUE(m);
  570. ASSERT_TRUE(mod_square);
  571. bssl::UniquePtr<BIGNUM> a_copy(BN_new());
  572. bssl::UniquePtr<BIGNUM> ret(BN_new());
  573. ASSERT_TRUE(ret);
  574. ASSERT_TRUE(a_copy);
  575. ASSERT_TRUE(BN_mod_mul(ret.get(), a.get(), a.get(), m.get(), ctx));
  576. EXPECT_BIGNUMS_EQUAL("A * A (mod M)", mod_square.get(), ret.get());
  577. // Repeat the operation with |a_copy|.
  578. ASSERT_TRUE(BN_copy(a_copy.get(), a.get()));
  579. ASSERT_TRUE(BN_mod_mul(ret.get(), a.get(), a_copy.get(), m.get(), ctx));
  580. EXPECT_BIGNUMS_EQUAL("A * A_copy (mod M)", mod_square.get(), ret.get());
  581. if (BN_is_odd(m.get())) {
  582. // Reduce |a| and test the Montgomery version.
  583. bssl::UniquePtr<BN_MONT_CTX> mont(
  584. BN_MONT_CTX_new_for_modulus(m.get(), ctx));
  585. bssl::UniquePtr<BIGNUM> a_tmp(BN_new());
  586. ASSERT_TRUE(mont);
  587. ASSERT_TRUE(a_tmp);
  588. ASSERT_TRUE(BN_nnmod(a.get(), a.get(), m.get(), ctx));
  589. ASSERT_TRUE(BN_to_montgomery(a_tmp.get(), a.get(), mont.get(), ctx));
  590. ASSERT_TRUE(BN_mod_mul_montgomery(ret.get(), a_tmp.get(), a_tmp.get(),
  591. mont.get(), ctx));
  592. ASSERT_TRUE(BN_from_montgomery(ret.get(), ret.get(), mont.get(), ctx));
  593. EXPECT_BIGNUMS_EQUAL("A * A (mod M) (Montgomery)", mod_square.get(),
  594. ret.get());
  595. // Repeat the operation with |a_copy|.
  596. ASSERT_TRUE(BN_copy(a_copy.get(), a_tmp.get()));
  597. ASSERT_TRUE(BN_mod_mul_montgomery(ret.get(), a_tmp.get(), a_copy.get(),
  598. mont.get(), ctx));
  599. ASSERT_TRUE(BN_from_montgomery(ret.get(), ret.get(), mont.get(), ctx));
  600. EXPECT_BIGNUMS_EQUAL("A * A_copy (mod M) (Montgomery)", mod_square.get(),
  601. ret.get());
  602. #if !defined(BORINGSSL_SHARED_LIBRARY)
  603. size_t m_width = static_cast<size_t>(bn_minimal_width(m.get()));
  604. if (m_width <= BN_SMALL_MAX_WORDS) {
  605. std::unique_ptr<BN_ULONG[]> a_words(new BN_ULONG[m_width]),
  606. a_copy_words(new BN_ULONG[m_width]), r_words(new BN_ULONG[m_width]);
  607. ASSERT_TRUE(bn_copy_words(a_words.get(), m_width, a.get()));
  608. bn_to_montgomery_small(a_words.get(), a_words.get(), m_width, mont.get());
  609. bn_mod_mul_montgomery_small(r_words.get(), a_words.get(), a_words.get(),
  610. m_width, mont.get());
  611. bn_from_montgomery_small(r_words.get(), r_words.get(), m_width, mont.get());
  612. ASSERT_TRUE(bn_set_words(ret.get(), r_words.get(), m_width));
  613. EXPECT_BIGNUMS_EQUAL("A * A (mod M) (Montgomery, words)",
  614. mod_square.get(), ret.get());
  615. // Repeat the operation with |a_copy_words|.
  616. OPENSSL_memcpy(a_copy_words.get(), a_words.get(),
  617. m_width * sizeof(BN_ULONG));
  618. bn_mod_mul_montgomery_small(r_words.get(), a_words.get(),
  619. a_copy_words.get(), m_width, mont.get());
  620. // Use the second half of |tmp| so ASan will catch out-of-bounds writes.
  621. bn_from_montgomery_small(r_words.get(), r_words.get(), m_width,
  622. mont.get());
  623. ASSERT_TRUE(bn_set_words(ret.get(), r_words.get(), m_width));
  624. EXPECT_BIGNUMS_EQUAL("A * A_copy (mod M) (Montgomery, words)",
  625. mod_square.get(), ret.get());
  626. }
  627. #endif
  628. }
  629. }
  630. static void TestModExp(BIGNUMFileTest *t, BN_CTX *ctx) {
  631. bssl::UniquePtr<BIGNUM> a = t->GetBIGNUM("A");
  632. bssl::UniquePtr<BIGNUM> e = t->GetBIGNUM("E");
  633. bssl::UniquePtr<BIGNUM> m = t->GetBIGNUM("M");
  634. bssl::UniquePtr<BIGNUM> mod_exp = t->GetBIGNUM("ModExp");
  635. ASSERT_TRUE(a);
  636. ASSERT_TRUE(e);
  637. ASSERT_TRUE(m);
  638. ASSERT_TRUE(mod_exp);
  639. bssl::UniquePtr<BIGNUM> ret(BN_new());
  640. ASSERT_TRUE(ret);
  641. ASSERT_TRUE(BN_mod_exp(ret.get(), a.get(), e.get(), m.get(), ctx));
  642. EXPECT_BIGNUMS_EQUAL("A ^ E (mod M)", mod_exp.get(), ret.get());
  643. // The other implementations require reduced inputs.
  644. ASSERT_TRUE(BN_nnmod(a.get(), a.get(), m.get(), ctx));
  645. if (BN_is_odd(m.get())) {
  646. ASSERT_TRUE(
  647. BN_mod_exp_mont(ret.get(), a.get(), e.get(), m.get(), ctx, NULL));
  648. EXPECT_BIGNUMS_EQUAL("A ^ E (mod M) (Montgomery)", mod_exp.get(),
  649. ret.get());
  650. ASSERT_TRUE(BN_mod_exp_mont_consttime(ret.get(), a.get(), e.get(), m.get(),
  651. ctx, NULL));
  652. EXPECT_BIGNUMS_EQUAL("A ^ E (mod M) (constant-time)", mod_exp.get(),
  653. ret.get());
  654. #if !defined(BORINGSSL_SHARED_LIBRARY)
  655. size_t m_width = static_cast<size_t>(bn_minimal_width(m.get()));
  656. if (m_width <= BN_SMALL_MAX_WORDS) {
  657. bssl::UniquePtr<BN_MONT_CTX> mont(
  658. BN_MONT_CTX_new_for_modulus(m.get(), ctx));
  659. ASSERT_TRUE(mont.get());
  660. std::unique_ptr<BN_ULONG[]> r_words(new BN_ULONG[m_width]),
  661. a_words(new BN_ULONG[m_width]);
  662. ASSERT_TRUE(bn_copy_words(a_words.get(), m_width, a.get()));
  663. bn_to_montgomery_small(a_words.get(), a_words.get(), m_width, mont.get());
  664. bn_mod_exp_mont_small(r_words.get(), a_words.get(), m_width, e->d,
  665. e->width, mont.get());
  666. bn_from_montgomery_small(r_words.get(), r_words.get(), m_width,
  667. mont.get());
  668. ASSERT_TRUE(bn_set_words(ret.get(), r_words.get(), m_width));
  669. EXPECT_BIGNUMS_EQUAL("A ^ E (mod M) (Montgomery, words)", mod_exp.get(),
  670. ret.get());
  671. }
  672. #endif
  673. }
  674. }
  675. static void TestExp(BIGNUMFileTest *t, BN_CTX *ctx) {
  676. bssl::UniquePtr<BIGNUM> a = t->GetBIGNUM("A");
  677. bssl::UniquePtr<BIGNUM> e = t->GetBIGNUM("E");
  678. bssl::UniquePtr<BIGNUM> exp = t->GetBIGNUM("Exp");
  679. ASSERT_TRUE(a);
  680. ASSERT_TRUE(e);
  681. ASSERT_TRUE(exp);
  682. bssl::UniquePtr<BIGNUM> ret(BN_new());
  683. ASSERT_TRUE(ret);
  684. ASSERT_TRUE(BN_exp(ret.get(), a.get(), e.get(), ctx));
  685. EXPECT_BIGNUMS_EQUAL("A ^ E", exp.get(), ret.get());
  686. }
  687. static void TestModSqrt(BIGNUMFileTest *t, BN_CTX *ctx) {
  688. bssl::UniquePtr<BIGNUM> a = t->GetBIGNUM("A");
  689. bssl::UniquePtr<BIGNUM> p = t->GetBIGNUM("P");
  690. bssl::UniquePtr<BIGNUM> mod_sqrt = t->GetBIGNUM("ModSqrt");
  691. bssl::UniquePtr<BIGNUM> mod_sqrt2(BN_new());
  692. ASSERT_TRUE(a);
  693. ASSERT_TRUE(p);
  694. ASSERT_TRUE(mod_sqrt);
  695. ASSERT_TRUE(mod_sqrt2);
  696. // There are two possible answers.
  697. ASSERT_TRUE(BN_sub(mod_sqrt2.get(), p.get(), mod_sqrt.get()));
  698. // -0 is 0, not P.
  699. if (BN_is_zero(mod_sqrt.get())) {
  700. BN_zero(mod_sqrt2.get());
  701. }
  702. bssl::UniquePtr<BIGNUM> ret(BN_new());
  703. ASSERT_TRUE(ret);
  704. ASSERT_TRUE(BN_mod_sqrt(ret.get(), a.get(), p.get(), ctx));
  705. if (BN_cmp(ret.get(), mod_sqrt2.get()) != 0) {
  706. EXPECT_BIGNUMS_EQUAL("sqrt(A) (mod P)", mod_sqrt.get(), ret.get());
  707. }
  708. }
  709. static void TestNotModSquare(BIGNUMFileTest *t, BN_CTX *ctx) {
  710. bssl::UniquePtr<BIGNUM> not_mod_square = t->GetBIGNUM("NotModSquare");
  711. bssl::UniquePtr<BIGNUM> p = t->GetBIGNUM("P");
  712. bssl::UniquePtr<BIGNUM> ret(BN_new());
  713. ASSERT_TRUE(not_mod_square);
  714. ASSERT_TRUE(p);
  715. ASSERT_TRUE(ret);
  716. EXPECT_FALSE(BN_mod_sqrt(ret.get(), not_mod_square.get(), p.get(), ctx))
  717. << "BN_mod_sqrt unexpectedly succeeded.";
  718. uint32_t err = ERR_peek_error();
  719. EXPECT_EQ(ERR_LIB_BN, ERR_GET_LIB(err));
  720. EXPECT_EQ(BN_R_NOT_A_SQUARE, ERR_GET_REASON(err));
  721. ERR_clear_error();
  722. }
  723. static void TestModInv(BIGNUMFileTest *t, BN_CTX *ctx) {
  724. bssl::UniquePtr<BIGNUM> a = t->GetBIGNUM("A");
  725. bssl::UniquePtr<BIGNUM> m = t->GetBIGNUM("M");
  726. bssl::UniquePtr<BIGNUM> mod_inv = t->GetBIGNUM("ModInv");
  727. ASSERT_TRUE(a);
  728. ASSERT_TRUE(m);
  729. ASSERT_TRUE(mod_inv);
  730. bssl::UniquePtr<BIGNUM> ret(BN_new());
  731. ASSERT_TRUE(ret);
  732. ASSERT_TRUE(BN_mod_inverse(ret.get(), a.get(), m.get(), ctx));
  733. EXPECT_BIGNUMS_EQUAL("inv(A) (mod M)", mod_inv.get(), ret.get());
  734. ASSERT_TRUE(BN_gcd(ret.get(), a.get(), m.get(), ctx));
  735. EXPECT_BIGNUMS_EQUAL("GCD(A, M)", BN_value_one(), ret.get());
  736. ASSERT_TRUE(BN_nnmod(a.get(), a.get(), m.get(), ctx));
  737. int no_inverse;
  738. ASSERT_TRUE(
  739. bn_mod_inverse_consttime(ret.get(), &no_inverse, a.get(), m.get(), ctx));
  740. EXPECT_BIGNUMS_EQUAL("inv(A) (mod M) (constant-time)", mod_inv.get(),
  741. ret.get());
  742. }
  743. static void TestGCD(BIGNUMFileTest *t, BN_CTX *ctx) {
  744. bssl::UniquePtr<BIGNUM> a = t->GetBIGNUM("A");
  745. bssl::UniquePtr<BIGNUM> b = t->GetBIGNUM("B");
  746. bssl::UniquePtr<BIGNUM> gcd = t->GetBIGNUM("GCD");
  747. bssl::UniquePtr<BIGNUM> lcm = t->GetBIGNUM("LCM");
  748. ASSERT_TRUE(a);
  749. ASSERT_TRUE(b);
  750. ASSERT_TRUE(gcd);
  751. ASSERT_TRUE(lcm);
  752. bssl::UniquePtr<BIGNUM> ret(BN_new());
  753. ASSERT_TRUE(ret);
  754. ASSERT_TRUE(BN_gcd(ret.get(), a.get(), b.get(), ctx));
  755. EXPECT_BIGNUMS_EQUAL("GCD(A, B)", gcd.get(), ret.get());
  756. if (!BN_is_one(gcd.get())) {
  757. EXPECT_FALSE(BN_mod_inverse(ret.get(), a.get(), b.get(), ctx))
  758. << "A^-1 (mod B) computed, but it does not exist";
  759. EXPECT_FALSE(BN_mod_inverse(ret.get(), b.get(), a.get(), ctx))
  760. << "B^-1 (mod A) computed, but it does not exist";
  761. if (!BN_is_zero(b.get())) {
  762. bssl::UniquePtr<BIGNUM> a_reduced(BN_new());
  763. ASSERT_TRUE(a_reduced);
  764. ASSERT_TRUE(BN_nnmod(a_reduced.get(), a.get(), b.get(), ctx));
  765. int no_inverse;
  766. EXPECT_FALSE(bn_mod_inverse_consttime(ret.get(), &no_inverse,
  767. a_reduced.get(), b.get(), ctx))
  768. << "A^-1 (mod B) computed, but it does not exist";
  769. EXPECT_TRUE(no_inverse);
  770. }
  771. if (!BN_is_zero(a.get())) {
  772. bssl::UniquePtr<BIGNUM> b_reduced(BN_new());
  773. ASSERT_TRUE(b_reduced);
  774. ASSERT_TRUE(BN_nnmod(b_reduced.get(), b.get(), a.get(), ctx));
  775. int no_inverse;
  776. EXPECT_FALSE(bn_mod_inverse_consttime(ret.get(), &no_inverse,
  777. b_reduced.get(), a.get(), ctx))
  778. << "B^-1 (mod A) computed, but it does not exist";
  779. EXPECT_TRUE(no_inverse);
  780. }
  781. }
  782. int is_relative_prime;
  783. ASSERT_TRUE(
  784. bn_is_relatively_prime(&is_relative_prime, a.get(), b.get(), ctx));
  785. EXPECT_EQ(is_relative_prime, BN_is_one(gcd.get()));
  786. if (!BN_is_zero(gcd.get())) {
  787. ASSERT_TRUE(bn_lcm_consttime(ret.get(), a.get(), b.get(), ctx));
  788. EXPECT_BIGNUMS_EQUAL("LCM(A, B)", lcm.get(), ret.get());
  789. }
  790. }
  791. class BNTest : public testing::Test {
  792. protected:
  793. void SetUp() override {
  794. ctx_.reset(BN_CTX_new());
  795. ASSERT_TRUE(ctx_);
  796. }
  797. BN_CTX *ctx() { return ctx_.get(); }
  798. private:
  799. bssl::UniquePtr<BN_CTX> ctx_;
  800. };
  801. TEST_F(BNTest, TestVectors) {
  802. static const struct {
  803. const char *name;
  804. void (*func)(BIGNUMFileTest *t, BN_CTX *ctx);
  805. } kTests[] = {
  806. {"Sum", TestSum},
  807. {"LShift1", TestLShift1},
  808. {"LShift", TestLShift},
  809. {"RShift", TestRShift},
  810. {"Square", TestSquare},
  811. {"Product", TestProduct},
  812. {"Quotient", TestQuotient},
  813. {"ModMul", TestModMul},
  814. {"ModSquare", TestModSquare},
  815. {"ModExp", TestModExp},
  816. {"Exp", TestExp},
  817. {"ModSqrt", TestModSqrt},
  818. {"NotModSquare", TestNotModSquare},
  819. {"ModInv", TestModInv},
  820. {"GCD", TestGCD},
  821. };
  822. FileTestGTest("crypto/fipsmodule/bn/bn_tests.txt", [&](FileTest *t) {
  823. void (*func)(BIGNUMFileTest *t, BN_CTX *ctx) = nullptr;
  824. for (const auto &test : kTests) {
  825. if (t->GetType() == test.name) {
  826. func = test.func;
  827. break;
  828. }
  829. }
  830. if (!func) {
  831. FAIL() << "Unknown test type: " << t->GetType();
  832. return;
  833. }
  834. // Run the test with normalize-sized |BIGNUM|s.
  835. BIGNUMFileTest bn_test(t, 0);
  836. BN_CTX_start(ctx());
  837. func(&bn_test, ctx());
  838. BN_CTX_end(ctx());
  839. unsigned num_bignums = bn_test.num_bignums();
  840. // Repeat the test with all combinations of large and small |BIGNUM|s.
  841. for (unsigned large_mask = 1; large_mask < (1u << num_bignums);
  842. large_mask++) {
  843. SCOPED_TRACE(large_mask);
  844. BIGNUMFileTest bn_test2(t, large_mask);
  845. BN_CTX_start(ctx());
  846. func(&bn_test2, ctx());
  847. BN_CTX_end(ctx());
  848. }
  849. });
  850. }
  851. TEST_F(BNTest, BN2BinPadded) {
  852. uint8_t zeros[256], out[256], reference[128];
  853. OPENSSL_memset(zeros, 0, sizeof(zeros));
  854. // Test edge case at 0.
  855. bssl::UniquePtr<BIGNUM> n(BN_new());
  856. ASSERT_TRUE(n);
  857. ASSERT_TRUE(BN_bn2bin_padded(NULL, 0, n.get()));
  858. OPENSSL_memset(out, -1, sizeof(out));
  859. ASSERT_TRUE(BN_bn2bin_padded(out, sizeof(out), n.get()));
  860. EXPECT_EQ(Bytes(zeros), Bytes(out));
  861. // Test a random numbers at various byte lengths.
  862. for (size_t bytes = 128 - 7; bytes <= 128; bytes++) {
  863. ASSERT_TRUE(
  864. BN_rand(n.get(), bytes * 8, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY));
  865. ASSERT_EQ(bytes, BN_num_bytes(n.get()));
  866. ASSERT_EQ(bytes, BN_bn2bin(n.get(), reference));
  867. // Empty buffer should fail.
  868. EXPECT_FALSE(BN_bn2bin_padded(NULL, 0, n.get()));
  869. // One byte short should fail.
  870. EXPECT_FALSE(BN_bn2bin_padded(out, bytes - 1, n.get()));
  871. // Exactly right size should encode.
  872. ASSERT_TRUE(BN_bn2bin_padded(out, bytes, n.get()));
  873. EXPECT_EQ(Bytes(reference, bytes), Bytes(out, bytes));
  874. // Pad up one byte extra.
  875. ASSERT_TRUE(BN_bn2bin_padded(out, bytes + 1, n.get()));
  876. EXPECT_EQ(0u, out[0]);
  877. EXPECT_EQ(Bytes(reference, bytes), Bytes(out + 1, bytes));
  878. // Pad up to 256.
  879. ASSERT_TRUE(BN_bn2bin_padded(out, sizeof(out), n.get()));
  880. EXPECT_EQ(Bytes(zeros, sizeof(out) - bytes),
  881. Bytes(out, sizeof(out) - bytes));
  882. EXPECT_EQ(Bytes(reference, bytes), Bytes(out + sizeof(out) - bytes, bytes));
  883. // Repeat some tests with a non-minimal |BIGNUM|.
  884. EXPECT_TRUE(bn_resize_words(n.get(), 32));
  885. EXPECT_FALSE(BN_bn2bin_padded(out, bytes - 1, n.get()));
  886. ASSERT_TRUE(BN_bn2bin_padded(out, bytes + 1, n.get()));
  887. EXPECT_EQ(0u, out[0]);
  888. EXPECT_EQ(Bytes(reference, bytes), Bytes(out + 1, bytes));
  889. }
  890. }
  891. TEST_F(BNTest, LittleEndian) {
  892. bssl::UniquePtr<BIGNUM> x(BN_new());
  893. bssl::UniquePtr<BIGNUM> y(BN_new());
  894. ASSERT_TRUE(x);
  895. ASSERT_TRUE(y);
  896. // Test edge case at 0. Fill |out| with garbage to ensure |BN_bn2le_padded|
  897. // wrote the result.
  898. uint8_t out[256], zeros[256];
  899. OPENSSL_memset(out, -1, sizeof(out));
  900. OPENSSL_memset(zeros, 0, sizeof(zeros));
  901. ASSERT_TRUE(BN_bn2le_padded(out, sizeof(out), x.get()));
  902. EXPECT_EQ(Bytes(zeros), Bytes(out));
  903. ASSERT_TRUE(BN_le2bn(out, sizeof(out), y.get()));
  904. EXPECT_BIGNUMS_EQUAL("BN_le2bn round-trip", x.get(), y.get());
  905. // Test random numbers at various byte lengths.
  906. for (size_t bytes = 128 - 7; bytes <= 128; bytes++) {
  907. ASSERT_TRUE(
  908. BN_rand(x.get(), bytes * 8, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY));
  909. // Fill |out| with garbage to ensure |BN_bn2le_padded| wrote the result.
  910. OPENSSL_memset(out, -1, sizeof(out));
  911. ASSERT_TRUE(BN_bn2le_padded(out, sizeof(out), x.get()));
  912. // Compute the expected value by reversing the big-endian output.
  913. uint8_t expected[sizeof(out)];
  914. ASSERT_TRUE(BN_bn2bin_padded(expected, sizeof(expected), x.get()));
  915. for (size_t i = 0; i < sizeof(expected) / 2; i++) {
  916. uint8_t tmp = expected[i];
  917. expected[i] = expected[sizeof(expected) - 1 - i];
  918. expected[sizeof(expected) - 1 - i] = tmp;
  919. }
  920. EXPECT_EQ(Bytes(out), Bytes(expected));
  921. // Make sure the decoding produces the same BIGNUM.
  922. ASSERT_TRUE(BN_le2bn(out, bytes, y.get()));
  923. EXPECT_BIGNUMS_EQUAL("BN_le2bn round-trip", x.get(), y.get());
  924. }
  925. }
  926. static int DecimalToBIGNUM(bssl::UniquePtr<BIGNUM> *out, const char *in) {
  927. BIGNUM *raw = NULL;
  928. int ret = BN_dec2bn(&raw, in);
  929. out->reset(raw);
  930. return ret;
  931. }
  932. TEST_F(BNTest, Dec2BN) {
  933. bssl::UniquePtr<BIGNUM> bn;
  934. int ret = DecimalToBIGNUM(&bn, "0");
  935. ASSERT_EQ(1, ret);
  936. EXPECT_TRUE(BN_is_zero(bn.get()));
  937. EXPECT_FALSE(BN_is_negative(bn.get()));
  938. ret = DecimalToBIGNUM(&bn, "256");
  939. ASSERT_EQ(3, ret);
  940. EXPECT_TRUE(BN_is_word(bn.get(), 256));
  941. EXPECT_FALSE(BN_is_negative(bn.get()));
  942. ret = DecimalToBIGNUM(&bn, "-42");
  943. ASSERT_EQ(3, ret);
  944. EXPECT_TRUE(BN_abs_is_word(bn.get(), 42));
  945. EXPECT_TRUE(BN_is_negative(bn.get()));
  946. ret = DecimalToBIGNUM(&bn, "-0");
  947. ASSERT_EQ(2, ret);
  948. EXPECT_TRUE(BN_is_zero(bn.get()));
  949. EXPECT_FALSE(BN_is_negative(bn.get()));
  950. ret = DecimalToBIGNUM(&bn, "42trailing garbage is ignored");
  951. ASSERT_EQ(2, ret);
  952. EXPECT_TRUE(BN_abs_is_word(bn.get(), 42));
  953. EXPECT_FALSE(BN_is_negative(bn.get()));
  954. }
  955. TEST_F(BNTest, Hex2BN) {
  956. bssl::UniquePtr<BIGNUM> bn;
  957. int ret = HexToBIGNUM(&bn, "0");
  958. ASSERT_EQ(1, ret);
  959. EXPECT_TRUE(BN_is_zero(bn.get()));
  960. EXPECT_FALSE(BN_is_negative(bn.get()));
  961. ret = HexToBIGNUM(&bn, "256");
  962. ASSERT_EQ(3, ret);
  963. EXPECT_TRUE(BN_is_word(bn.get(), 0x256));
  964. EXPECT_FALSE(BN_is_negative(bn.get()));
  965. ret = HexToBIGNUM(&bn, "-42");
  966. ASSERT_EQ(3, ret);
  967. EXPECT_TRUE(BN_abs_is_word(bn.get(), 0x42));
  968. EXPECT_TRUE(BN_is_negative(bn.get()));
  969. ret = HexToBIGNUM(&bn, "-0");
  970. ASSERT_EQ(2, ret);
  971. EXPECT_TRUE(BN_is_zero(bn.get()));
  972. EXPECT_FALSE(BN_is_negative(bn.get()));
  973. ret = HexToBIGNUM(&bn, "abctrailing garbage is ignored");
  974. ASSERT_EQ(3, ret);
  975. EXPECT_TRUE(BN_is_word(bn.get(), 0xabc));
  976. EXPECT_FALSE(BN_is_negative(bn.get()));
  977. }
  978. static bssl::UniquePtr<BIGNUM> ASCIIToBIGNUM(const char *in) {
  979. BIGNUM *raw = NULL;
  980. if (!BN_asc2bn(&raw, in)) {
  981. return nullptr;
  982. }
  983. return bssl::UniquePtr<BIGNUM>(raw);
  984. }
  985. TEST_F(BNTest, ASC2BN) {
  986. bssl::UniquePtr<BIGNUM> bn = ASCIIToBIGNUM("0");
  987. ASSERT_TRUE(bn);
  988. EXPECT_TRUE(BN_is_zero(bn.get()));
  989. EXPECT_FALSE(BN_is_negative(bn.get()));
  990. bn = ASCIIToBIGNUM("256");
  991. ASSERT_TRUE(bn);
  992. EXPECT_TRUE(BN_is_word(bn.get(), 256));
  993. EXPECT_FALSE(BN_is_negative(bn.get()));
  994. bn = ASCIIToBIGNUM("-42");
  995. ASSERT_TRUE(bn);
  996. EXPECT_TRUE(BN_abs_is_word(bn.get(), 42));
  997. EXPECT_TRUE(BN_is_negative(bn.get()));
  998. bn = ASCIIToBIGNUM("0x1234");
  999. ASSERT_TRUE(bn);
  1000. EXPECT_TRUE(BN_is_word(bn.get(), 0x1234));
  1001. EXPECT_FALSE(BN_is_negative(bn.get()));
  1002. bn = ASCIIToBIGNUM("0X1234");
  1003. ASSERT_TRUE(bn);
  1004. EXPECT_TRUE(BN_is_word(bn.get(), 0x1234));
  1005. EXPECT_FALSE(BN_is_negative(bn.get()));
  1006. bn = ASCIIToBIGNUM("-0xabcd");
  1007. ASSERT_TRUE(bn);
  1008. EXPECT_TRUE(BN_abs_is_word(bn.get(), 0xabcd));
  1009. EXPECT_FALSE(!BN_is_negative(bn.get()));
  1010. bn = ASCIIToBIGNUM("-0");
  1011. ASSERT_TRUE(bn);
  1012. EXPECT_TRUE(BN_is_zero(bn.get()));
  1013. EXPECT_FALSE(BN_is_negative(bn.get()));
  1014. bn = ASCIIToBIGNUM("123trailing garbage is ignored");
  1015. ASSERT_TRUE(bn);
  1016. EXPECT_TRUE(BN_is_word(bn.get(), 123));
  1017. EXPECT_FALSE(BN_is_negative(bn.get()));
  1018. }
  1019. struct MPITest {
  1020. const char *base10;
  1021. const char *mpi;
  1022. size_t mpi_len;
  1023. };
  1024. static const MPITest kMPITests[] = {
  1025. { "0", "\x00\x00\x00\x00", 4 },
  1026. { "1", "\x00\x00\x00\x01\x01", 5 },
  1027. { "-1", "\x00\x00\x00\x01\x81", 5 },
  1028. { "128", "\x00\x00\x00\x02\x00\x80", 6 },
  1029. { "256", "\x00\x00\x00\x02\x01\x00", 6 },
  1030. { "-256", "\x00\x00\x00\x02\x81\x00", 6 },
  1031. };
  1032. TEST_F(BNTest, MPI) {
  1033. uint8_t scratch[8];
  1034. for (const auto &test : kMPITests) {
  1035. SCOPED_TRACE(test.base10);
  1036. bssl::UniquePtr<BIGNUM> bn(ASCIIToBIGNUM(test.base10));
  1037. ASSERT_TRUE(bn);
  1038. const size_t mpi_len = BN_bn2mpi(bn.get(), NULL);
  1039. ASSERT_LE(mpi_len, sizeof(scratch)) << "MPI size is too large to test";
  1040. const size_t mpi_len2 = BN_bn2mpi(bn.get(), scratch);
  1041. EXPECT_EQ(mpi_len, mpi_len2);
  1042. EXPECT_EQ(Bytes(test.mpi, test.mpi_len), Bytes(scratch, mpi_len));
  1043. bssl::UniquePtr<BIGNUM> bn2(BN_mpi2bn(scratch, mpi_len, NULL));
  1044. ASSERT_TRUE(bn2) << "failed to parse";
  1045. EXPECT_BIGNUMS_EQUAL("BN_mpi2bn", bn.get(), bn2.get());
  1046. }
  1047. }
  1048. TEST_F(BNTest, Rand) {
  1049. bssl::UniquePtr<BIGNUM> bn(BN_new());
  1050. ASSERT_TRUE(bn);
  1051. static const int kTop[] = {BN_RAND_TOP_ANY, BN_RAND_TOP_ONE, BN_RAND_TOP_TWO};
  1052. static const int kBottom[] = {BN_RAND_BOTTOM_ANY, BN_RAND_BOTTOM_ODD};
  1053. for (unsigned bits = 0; bits < 256; bits++) {
  1054. SCOPED_TRACE(bits);
  1055. for (int top : kTop) {
  1056. SCOPED_TRACE(top);
  1057. for (int bottom : kBottom) {
  1058. SCOPED_TRACE(bottom);
  1059. // Generate 100 numbers and ensure that they have the expected bit
  1060. // patterns. The probability of any one bit not covering both its values
  1061. // is 2^-100.
  1062. bool seen_n_1_clear = false, seen_n_1_set = false;
  1063. bool seen_n_2_clear = false, seen_n_2_set = false;
  1064. bool seen_0_clear = false, seen_0_set = false;
  1065. for (int i = 0; i < 100; i++) {
  1066. ASSERT_TRUE(BN_rand(bn.get(), bits, top, bottom));
  1067. EXPECT_LE(BN_num_bits(bn.get()), bits);
  1068. if (BN_is_bit_set(bn.get(), bits - 1)) {
  1069. seen_n_1_set = true;
  1070. } else {
  1071. seen_n_1_clear = true;
  1072. }
  1073. if (BN_is_bit_set(bn.get(), bits - 2)) {
  1074. seen_n_2_set = true;
  1075. } else {
  1076. seen_n_2_clear = true;
  1077. }
  1078. if (BN_is_bit_set(bn.get(), 0)) {
  1079. seen_0_set = true;
  1080. } else {
  1081. seen_0_clear = true;
  1082. }
  1083. }
  1084. if (bits > 0) {
  1085. EXPECT_TRUE(seen_0_set);
  1086. EXPECT_TRUE(seen_n_1_set);
  1087. if (bits > 1) {
  1088. EXPECT_TRUE(seen_n_2_set);
  1089. }
  1090. }
  1091. if (bits == 0) {
  1092. // Nothing additional to check. The |BN_num_bits| check ensures we
  1093. // always got zero.
  1094. } else if (bits == 1) {
  1095. // Bit zero is bit n-1.
  1096. EXPECT_EQ(bottom == BN_RAND_BOTTOM_ANY && top == BN_RAND_TOP_ANY,
  1097. seen_0_clear);
  1098. } else if (bits == 2) {
  1099. // Bit zero is bit n-2.
  1100. EXPECT_EQ(bottom == BN_RAND_BOTTOM_ANY && top != BN_RAND_TOP_TWO,
  1101. seen_0_clear);
  1102. EXPECT_EQ(top == BN_RAND_TOP_ANY, seen_n_1_clear);
  1103. } else {
  1104. EXPECT_EQ(bottom == BN_RAND_BOTTOM_ANY, seen_0_clear);
  1105. EXPECT_EQ(top != BN_RAND_TOP_TWO, seen_n_2_clear);
  1106. EXPECT_EQ(top == BN_RAND_TOP_ANY, seen_n_1_clear);
  1107. }
  1108. }
  1109. }
  1110. }
  1111. }
  1112. TEST_F(BNTest, RandRange) {
  1113. bssl::UniquePtr<BIGNUM> bn(BN_new()), six(BN_new());
  1114. ASSERT_TRUE(bn);
  1115. ASSERT_TRUE(six);
  1116. ASSERT_TRUE(BN_set_word(six.get(), 6));
  1117. // Generate 1,000 random numbers and ensure they all stay in range. This check
  1118. // may flakily pass when it should have failed but will not flakily fail.
  1119. bool seen[6] = {false, false, false, false, false};
  1120. for (unsigned i = 0; i < 1000; i++) {
  1121. SCOPED_TRACE(i);
  1122. ASSERT_TRUE(BN_rand_range_ex(bn.get(), 1, six.get()));
  1123. BN_ULONG word = BN_get_word(bn.get());
  1124. if (BN_is_negative(bn.get()) ||
  1125. word < 1 ||
  1126. word >= 6) {
  1127. FAIL() << "BN_rand_range_ex generated invalid value: " << word;
  1128. }
  1129. seen[word] = true;
  1130. }
  1131. // Test that all numbers were accounted for. Note this test is probabilistic
  1132. // and may flakily fail when it should have passed. As an upper-bound on the
  1133. // failure probability, we'll never see any one number with probability
  1134. // (4/5)^1000, so the probability of failure is at most 5*(4/5)^1000. This is
  1135. // around 1 in 2^320.
  1136. for (unsigned i = 1; i < 6; i++) {
  1137. EXPECT_TRUE(seen[i]) << "BN_rand_range failed to generated " << i;
  1138. }
  1139. }
  1140. struct ASN1Test {
  1141. const char *value_ascii;
  1142. const char *der;
  1143. size_t der_len;
  1144. };
  1145. static const ASN1Test kASN1Tests[] = {
  1146. {"0", "\x02\x01\x00", 3},
  1147. {"1", "\x02\x01\x01", 3},
  1148. {"127", "\x02\x01\x7f", 3},
  1149. {"128", "\x02\x02\x00\x80", 4},
  1150. {"0xdeadbeef", "\x02\x05\x00\xde\xad\xbe\xef", 7},
  1151. {"0x0102030405060708",
  1152. "\x02\x08\x01\x02\x03\x04\x05\x06\x07\x08", 10},
  1153. {"0xffffffffffffffff",
  1154. "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff", 11},
  1155. };
  1156. struct ASN1InvalidTest {
  1157. const char *der;
  1158. size_t der_len;
  1159. };
  1160. static const ASN1InvalidTest kASN1InvalidTests[] = {
  1161. // Bad tag.
  1162. {"\x03\x01\x00", 3},
  1163. // Empty contents.
  1164. {"\x02\x00", 2},
  1165. // Negative numbers.
  1166. {"\x02\x01\x80", 3},
  1167. {"\x02\x01\xff", 3},
  1168. // Unnecessary leading zeros.
  1169. {"\x02\x02\x00\x01", 4},
  1170. };
  1171. TEST_F(BNTest, ASN1) {
  1172. for (const ASN1Test &test : kASN1Tests) {
  1173. SCOPED_TRACE(test.value_ascii);
  1174. bssl::UniquePtr<BIGNUM> bn = ASCIIToBIGNUM(test.value_ascii);
  1175. ASSERT_TRUE(bn);
  1176. // Test that the input is correctly parsed.
  1177. bssl::UniquePtr<BIGNUM> bn2(BN_new());
  1178. ASSERT_TRUE(bn2);
  1179. CBS cbs;
  1180. CBS_init(&cbs, reinterpret_cast<const uint8_t*>(test.der), test.der_len);
  1181. ASSERT_TRUE(BN_parse_asn1_unsigned(&cbs, bn2.get()));
  1182. EXPECT_EQ(0u, CBS_len(&cbs));
  1183. EXPECT_BIGNUMS_EQUAL("decode ASN.1", bn.get(), bn2.get());
  1184. // Test the value serializes correctly.
  1185. bssl::ScopedCBB cbb;
  1186. uint8_t *der;
  1187. size_t der_len;
  1188. ASSERT_TRUE(CBB_init(cbb.get(), 0));
  1189. ASSERT_TRUE(BN_marshal_asn1(cbb.get(), bn.get()));
  1190. ASSERT_TRUE(CBB_finish(cbb.get(), &der, &der_len));
  1191. bssl::UniquePtr<uint8_t> delete_der(der);
  1192. EXPECT_EQ(Bytes(test.der, test.der_len), Bytes(der, der_len));
  1193. }
  1194. for (const ASN1InvalidTest &test : kASN1InvalidTests) {
  1195. SCOPED_TRACE(Bytes(test.der, test.der_len));;
  1196. bssl::UniquePtr<BIGNUM> bn(BN_new());
  1197. ASSERT_TRUE(bn);
  1198. CBS cbs;
  1199. CBS_init(&cbs, reinterpret_cast<const uint8_t *>(test.der), test.der_len);
  1200. EXPECT_FALSE(BN_parse_asn1_unsigned(&cbs, bn.get()))
  1201. << "Parsed invalid input.";
  1202. ERR_clear_error();
  1203. }
  1204. // Serializing negative numbers is not supported.
  1205. bssl::UniquePtr<BIGNUM> bn = ASCIIToBIGNUM("-1");
  1206. ASSERT_TRUE(bn);
  1207. bssl::ScopedCBB cbb;
  1208. ASSERT_TRUE(CBB_init(cbb.get(), 0));
  1209. EXPECT_FALSE(BN_marshal_asn1(cbb.get(), bn.get()))
  1210. << "Serialized negative number.";
  1211. ERR_clear_error();
  1212. }
  1213. TEST_F(BNTest, NegativeZero) {
  1214. bssl::UniquePtr<BIGNUM> a(BN_new());
  1215. bssl::UniquePtr<BIGNUM> b(BN_new());
  1216. bssl::UniquePtr<BIGNUM> c(BN_new());
  1217. ASSERT_TRUE(a);
  1218. ASSERT_TRUE(b);
  1219. ASSERT_TRUE(c);
  1220. // Test that BN_mul never gives negative zero.
  1221. ASSERT_TRUE(BN_set_word(a.get(), 1));
  1222. BN_set_negative(a.get(), 1);
  1223. BN_zero(b.get());
  1224. ASSERT_TRUE(BN_mul(c.get(), a.get(), b.get(), ctx()));
  1225. EXPECT_TRUE(BN_is_zero(c.get()));
  1226. EXPECT_FALSE(BN_is_negative(c.get()));
  1227. bssl::UniquePtr<BIGNUM> numerator(BN_new()), denominator(BN_new());
  1228. ASSERT_TRUE(numerator);
  1229. ASSERT_TRUE(denominator);
  1230. // Test that BN_div never gives negative zero in the quotient.
  1231. ASSERT_TRUE(BN_set_word(numerator.get(), 1));
  1232. ASSERT_TRUE(BN_set_word(denominator.get(), 2));
  1233. BN_set_negative(numerator.get(), 1);
  1234. ASSERT_TRUE(
  1235. BN_div(a.get(), b.get(), numerator.get(), denominator.get(), ctx()));
  1236. EXPECT_TRUE(BN_is_zero(a.get()));
  1237. EXPECT_FALSE(BN_is_negative(a.get()));
  1238. // Test that BN_div never gives negative zero in the remainder.
  1239. ASSERT_TRUE(BN_set_word(denominator.get(), 1));
  1240. ASSERT_TRUE(
  1241. BN_div(a.get(), b.get(), numerator.get(), denominator.get(), ctx()));
  1242. EXPECT_TRUE(BN_is_zero(b.get()));
  1243. EXPECT_FALSE(BN_is_negative(b.get()));
  1244. // Test that BN_set_negative will not produce a negative zero.
  1245. BN_zero(a.get());
  1246. BN_set_negative(a.get(), 1);
  1247. EXPECT_FALSE(BN_is_negative(a.get()));
  1248. // Test that forcibly creating a negative zero does not break |BN_bn2hex| or
  1249. // |BN_bn2dec|.
  1250. a->neg = 1;
  1251. bssl::UniquePtr<char> dec(BN_bn2dec(a.get()));
  1252. bssl::UniquePtr<char> hex(BN_bn2hex(a.get()));
  1253. ASSERT_TRUE(dec);
  1254. ASSERT_TRUE(hex);
  1255. EXPECT_STREQ("-0", dec.get());
  1256. EXPECT_STREQ("-0", hex.get());
  1257. // Test that |BN_rshift| and |BN_rshift1| will not produce a negative zero.
  1258. ASSERT_TRUE(BN_set_word(a.get(), 1));
  1259. BN_set_negative(a.get(), 1);
  1260. ASSERT_TRUE(BN_rshift(b.get(), a.get(), 1));
  1261. EXPECT_TRUE(BN_is_zero(b.get()));
  1262. EXPECT_FALSE(BN_is_negative(b.get()));
  1263. ASSERT_TRUE(BN_rshift1(c.get(), a.get()));
  1264. EXPECT_TRUE(BN_is_zero(c.get()));
  1265. EXPECT_FALSE(BN_is_negative(c.get()));
  1266. // Test that |BN_div_word| will not produce a negative zero.
  1267. ASSERT_NE((BN_ULONG)-1, BN_div_word(a.get(), 2));
  1268. EXPECT_TRUE(BN_is_zero(a.get()));
  1269. EXPECT_FALSE(BN_is_negative(a.get()));
  1270. }
  1271. TEST_F(BNTest, BadModulus) {
  1272. bssl::UniquePtr<BIGNUM> a(BN_new());
  1273. bssl::UniquePtr<BIGNUM> b(BN_new());
  1274. bssl::UniquePtr<BIGNUM> zero(BN_new());
  1275. ASSERT_TRUE(a);
  1276. ASSERT_TRUE(b);
  1277. ASSERT_TRUE(zero);
  1278. BN_zero(zero.get());
  1279. EXPECT_FALSE(BN_div(a.get(), b.get(), BN_value_one(), zero.get(), ctx()));
  1280. ERR_clear_error();
  1281. EXPECT_FALSE(
  1282. BN_mod_mul(a.get(), BN_value_one(), BN_value_one(), zero.get(), ctx()));
  1283. ERR_clear_error();
  1284. EXPECT_FALSE(
  1285. BN_mod_exp(a.get(), BN_value_one(), BN_value_one(), zero.get(), ctx()));
  1286. ERR_clear_error();
  1287. EXPECT_FALSE(BN_mod_exp_mont(a.get(), BN_value_one(), BN_value_one(),
  1288. zero.get(), ctx(), NULL));
  1289. ERR_clear_error();
  1290. EXPECT_FALSE(BN_mod_exp_mont_consttime(
  1291. a.get(), BN_value_one(), BN_value_one(), zero.get(), ctx(), nullptr));
  1292. ERR_clear_error();
  1293. bssl::UniquePtr<BN_MONT_CTX> mont(
  1294. BN_MONT_CTX_new_for_modulus(zero.get(), ctx()));
  1295. EXPECT_FALSE(mont);
  1296. ERR_clear_error();
  1297. mont.reset(BN_MONT_CTX_new_consttime(b.get(), ctx()));
  1298. EXPECT_FALSE(mont);
  1299. ERR_clear_error();
  1300. // Some operations also may not be used with an even modulus.
  1301. ASSERT_TRUE(BN_set_word(b.get(), 16));
  1302. mont.reset(BN_MONT_CTX_new_for_modulus(b.get(), ctx()));
  1303. EXPECT_FALSE(mont);
  1304. ERR_clear_error();
  1305. mont.reset(BN_MONT_CTX_new_consttime(b.get(), ctx()));
  1306. EXPECT_FALSE(mont);
  1307. ERR_clear_error();
  1308. EXPECT_FALSE(BN_mod_exp_mont(a.get(), BN_value_one(), BN_value_one(), b.get(),
  1309. ctx(), NULL));
  1310. ERR_clear_error();
  1311. EXPECT_FALSE(BN_mod_exp_mont_consttime(
  1312. a.get(), BN_value_one(), BN_value_one(), b.get(), ctx(), nullptr));
  1313. ERR_clear_error();
  1314. }
  1315. // Test that a**0 mod 1 == 0.
  1316. TEST_F(BNTest, ExpZeroModOne) {
  1317. bssl::UniquePtr<BIGNUM> zero(BN_new()), a(BN_new()), r(BN_new()),
  1318. minus_one(BN_new());
  1319. ASSERT_TRUE(zero);
  1320. ASSERT_TRUE(a);
  1321. ASSERT_TRUE(r);
  1322. ASSERT_TRUE(minus_one);
  1323. ASSERT_TRUE(BN_set_word(minus_one.get(), 1));
  1324. BN_set_negative(minus_one.get(), 1);
  1325. ASSERT_TRUE(BN_rand(a.get(), 1024, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY));
  1326. BN_zero(zero.get());
  1327. ASSERT_TRUE(BN_mod_exp(r.get(), a.get(), zero.get(), BN_value_one(), ctx()));
  1328. EXPECT_TRUE(BN_is_zero(r.get()));
  1329. ASSERT_TRUE(
  1330. BN_mod_exp(r.get(), zero.get(), zero.get(), BN_value_one(), ctx()));
  1331. EXPECT_TRUE(BN_is_zero(r.get()));
  1332. ASSERT_TRUE(BN_mod_exp_mont_word(r.get(), 42, zero.get(), BN_value_one(),
  1333. ctx(), nullptr));
  1334. EXPECT_TRUE(BN_is_zero(r.get()));
  1335. ASSERT_TRUE(BN_mod_exp_mont_word(r.get(), 0, zero.get(), BN_value_one(),
  1336. ctx(), nullptr));
  1337. EXPECT_TRUE(BN_is_zero(r.get()));
  1338. // |BN_mod_exp_mont| and |BN_mod_exp_mont_consttime| require fully-reduced
  1339. // inputs, so a**0 mod 1 is not a valid call. 0**0 mod 1 is valid, however.
  1340. ASSERT_TRUE(BN_mod_exp_mont(r.get(), zero.get(), zero.get(), BN_value_one(),
  1341. ctx(), nullptr));
  1342. EXPECT_TRUE(BN_is_zero(r.get()));
  1343. ASSERT_TRUE(BN_mod_exp_mont_consttime(r.get(), zero.get(), zero.get(),
  1344. BN_value_one(), ctx(), nullptr));
  1345. EXPECT_TRUE(BN_is_zero(r.get()));
  1346. }
  1347. TEST_F(BNTest, SmallPrime) {
  1348. static const unsigned kBits = 10;
  1349. bssl::UniquePtr<BIGNUM> r(BN_new());
  1350. ASSERT_TRUE(r);
  1351. ASSERT_TRUE(BN_generate_prime_ex(r.get(), static_cast<int>(kBits), 0, NULL,
  1352. NULL, NULL));
  1353. EXPECT_EQ(kBits, BN_num_bits(r.get()));
  1354. }
  1355. TEST_F(BNTest, CmpWord) {
  1356. static const BN_ULONG kMaxWord = (BN_ULONG)-1;
  1357. bssl::UniquePtr<BIGNUM> r(BN_new());
  1358. ASSERT_TRUE(r);
  1359. ASSERT_TRUE(BN_set_word(r.get(), 0));
  1360. EXPECT_EQ(BN_cmp_word(r.get(), 0), 0);
  1361. EXPECT_LT(BN_cmp_word(r.get(), 1), 0);
  1362. EXPECT_LT(BN_cmp_word(r.get(), kMaxWord), 0);
  1363. ASSERT_TRUE(BN_set_word(r.get(), 100));
  1364. EXPECT_GT(BN_cmp_word(r.get(), 0), 0);
  1365. EXPECT_GT(BN_cmp_word(r.get(), 99), 0);
  1366. EXPECT_EQ(BN_cmp_word(r.get(), 100), 0);
  1367. EXPECT_LT(BN_cmp_word(r.get(), 101), 0);
  1368. EXPECT_LT(BN_cmp_word(r.get(), kMaxWord), 0);
  1369. BN_set_negative(r.get(), 1);
  1370. EXPECT_LT(BN_cmp_word(r.get(), 0), 0);
  1371. EXPECT_LT(BN_cmp_word(r.get(), 100), 0);
  1372. EXPECT_LT(BN_cmp_word(r.get(), kMaxWord), 0);
  1373. ASSERT_TRUE(BN_set_word(r.get(), kMaxWord));
  1374. EXPECT_GT(BN_cmp_word(r.get(), 0), 0);
  1375. EXPECT_GT(BN_cmp_word(r.get(), kMaxWord - 1), 0);
  1376. EXPECT_EQ(BN_cmp_word(r.get(), kMaxWord), 0);
  1377. ASSERT_TRUE(BN_add(r.get(), r.get(), BN_value_one()));
  1378. EXPECT_GT(BN_cmp_word(r.get(), 0), 0);
  1379. EXPECT_GT(BN_cmp_word(r.get(), kMaxWord), 0);
  1380. BN_set_negative(r.get(), 1);
  1381. EXPECT_LT(BN_cmp_word(r.get(), 0), 0);
  1382. EXPECT_LT(BN_cmp_word(r.get(), kMaxWord), 0);
  1383. }
  1384. TEST_F(BNTest, BN2Dec) {
  1385. static const char *kBN2DecTests[] = {
  1386. "0",
  1387. "1",
  1388. "-1",
  1389. "100",
  1390. "-100",
  1391. "123456789012345678901234567890",
  1392. "-123456789012345678901234567890",
  1393. "123456789012345678901234567890123456789012345678901234567890",
  1394. "-123456789012345678901234567890123456789012345678901234567890",
  1395. };
  1396. for (const char *test : kBN2DecTests) {
  1397. SCOPED_TRACE(test);
  1398. bssl::UniquePtr<BIGNUM> bn;
  1399. int ret = DecimalToBIGNUM(&bn, test);
  1400. ASSERT_NE(0, ret);
  1401. bssl::UniquePtr<char> dec(BN_bn2dec(bn.get()));
  1402. ASSERT_TRUE(dec);
  1403. EXPECT_STREQ(test, dec.get());
  1404. }
  1405. }
  1406. TEST_F(BNTest, SetGetU64) {
  1407. static const struct {
  1408. const char *hex;
  1409. uint64_t value;
  1410. } kU64Tests[] = {
  1411. {"0", UINT64_C(0x0)},
  1412. {"1", UINT64_C(0x1)},
  1413. {"ffffffff", UINT64_C(0xffffffff)},
  1414. {"100000000", UINT64_C(0x100000000)},
  1415. {"ffffffffffffffff", UINT64_C(0xffffffffffffffff)},
  1416. };
  1417. for (const auto& test : kU64Tests) {
  1418. SCOPED_TRACE(test.hex);
  1419. bssl::UniquePtr<BIGNUM> bn(BN_new()), expected;
  1420. ASSERT_TRUE(bn);
  1421. ASSERT_TRUE(BN_set_u64(bn.get(), test.value));
  1422. ASSERT_TRUE(HexToBIGNUM(&expected, test.hex));
  1423. EXPECT_BIGNUMS_EQUAL("BN_set_u64", expected.get(), bn.get());
  1424. uint64_t tmp;
  1425. ASSERT_TRUE(BN_get_u64(bn.get(), &tmp));
  1426. EXPECT_EQ(test.value, tmp);
  1427. // BN_get_u64 ignores the sign bit.
  1428. BN_set_negative(bn.get(), 1);
  1429. ASSERT_TRUE(BN_get_u64(bn.get(), &tmp));
  1430. EXPECT_EQ(test.value, tmp);
  1431. }
  1432. // Test that BN_get_u64 fails on large numbers.
  1433. bssl::UniquePtr<BIGNUM> bn(BN_new());
  1434. ASSERT_TRUE(bn);
  1435. ASSERT_TRUE(BN_lshift(bn.get(), BN_value_one(), 64));
  1436. uint64_t tmp;
  1437. EXPECT_FALSE(BN_get_u64(bn.get(), &tmp));
  1438. BN_set_negative(bn.get(), 1);
  1439. EXPECT_FALSE(BN_get_u64(bn.get(), &tmp));
  1440. }
  1441. TEST_F(BNTest, Pow2) {
  1442. bssl::UniquePtr<BIGNUM> power_of_two(BN_new()), random(BN_new()),
  1443. expected(BN_new()), actual(BN_new());
  1444. ASSERT_TRUE(power_of_two);
  1445. ASSERT_TRUE(random);
  1446. ASSERT_TRUE(expected);
  1447. ASSERT_TRUE(actual);
  1448. // Choose an exponent.
  1449. for (size_t e = 3; e < 512; e += 11) {
  1450. SCOPED_TRACE(e);
  1451. // Choose a bit length for our randoms.
  1452. for (int len = 3; len < 512; len += 23) {
  1453. SCOPED_TRACE(len);
  1454. // Set power_of_two = 2^e.
  1455. ASSERT_TRUE(BN_lshift(power_of_two.get(), BN_value_one(), (int)e));
  1456. // Test BN_is_pow2 on power_of_two.
  1457. EXPECT_TRUE(BN_is_pow2(power_of_two.get()));
  1458. // Pick a large random value, ensuring it isn't a power of two.
  1459. ASSERT_TRUE(
  1460. BN_rand(random.get(), len, BN_RAND_TOP_TWO, BN_RAND_BOTTOM_ANY));
  1461. // Test BN_is_pow2 on |r|.
  1462. EXPECT_FALSE(BN_is_pow2(random.get()));
  1463. // Test BN_mod_pow2 on |r|.
  1464. ASSERT_TRUE(
  1465. BN_mod(expected.get(), random.get(), power_of_two.get(), ctx()));
  1466. ASSERT_TRUE(BN_mod_pow2(actual.get(), random.get(), e));
  1467. EXPECT_BIGNUMS_EQUAL("random (mod power_of_two)", expected.get(),
  1468. actual.get());
  1469. // Test BN_nnmod_pow2 on |r|.
  1470. ASSERT_TRUE(
  1471. BN_nnmod(expected.get(), random.get(), power_of_two.get(), ctx()));
  1472. ASSERT_TRUE(BN_nnmod_pow2(actual.get(), random.get(), e));
  1473. EXPECT_BIGNUMS_EQUAL("random (mod power_of_two), non-negative",
  1474. expected.get(), actual.get());
  1475. // Test BN_nnmod_pow2 on -|r|.
  1476. BN_set_negative(random.get(), 1);
  1477. ASSERT_TRUE(
  1478. BN_nnmod(expected.get(), random.get(), power_of_two.get(), ctx()));
  1479. ASSERT_TRUE(BN_nnmod_pow2(actual.get(), random.get(), e));
  1480. EXPECT_BIGNUMS_EQUAL("-random (mod power_of_two), non-negative",
  1481. expected.get(), actual.get());
  1482. }
  1483. }
  1484. }
  1485. static const int kPrimes[] = {
  1486. 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31,
  1487. 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79,
  1488. 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137,
  1489. 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193,
  1490. 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257,
  1491. 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317,
  1492. 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389,
  1493. 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457,
  1494. 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523,
  1495. 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601,
  1496. 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661,
  1497. 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743,
  1498. 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823,
  1499. 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887,
  1500. 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977,
  1501. 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049,
  1502. 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117,
  1503. 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213,
  1504. 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289,
  1505. 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373,
  1506. 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453,
  1507. 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531,
  1508. 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607,
  1509. 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693,
  1510. 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777,
  1511. 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871,
  1512. 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951,
  1513. 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029,
  1514. 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113,
  1515. 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213,
  1516. 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293,
  1517. 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377,
  1518. 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441, 2447,
  1519. 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551,
  1520. 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659,
  1521. 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713,
  1522. 2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797,
  1523. 2801, 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887,
  1524. 2897, 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971,
  1525. 2999, 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079,
  1526. 3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187,
  1527. 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271,
  1528. 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359,
  1529. 3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461,
  1530. 3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533, 3539,
  1531. 3541, 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607, 3613, 3617,
  1532. 3623, 3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691, 3697, 3701,
  1533. 3709, 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797,
  1534. 3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889,
  1535. 3907, 3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989,
  1536. 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, 4073,
  1537. 4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, 4153, 4157,
  1538. 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243, 4253,
  1539. 4259, 4261, 4271, 4273, 4283, 4289, 4297, 4327, 4337, 4339, 4349,
  1540. 4357, 4363, 4373, 4391, 4397, 4409, 4421, 4423, 4441, 4447, 4451,
  1541. 4457, 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519, 4523, 4547,
  1542. 4549, 4561, 4567, 4583, 4591, 4597, 4603, 4621, 4637, 4639, 4643,
  1543. 4649, 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729,
  1544. 4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817,
  1545. 4831, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937,
  1546. 4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999, 5003, 5009,
  1547. 5011, 5021, 5023, 5039, 5051, 5059, 5077, 5081, 5087, 5099, 5101,
  1548. 5107, 5113, 5119, 5147, 5153, 5167, 5171, 5179, 5189, 5197, 5209,
  1549. 5227, 5231, 5233, 5237, 5261, 5273, 5279, 5281, 5297, 5303, 5309,
  1550. 5323, 5333, 5347, 5351, 5381, 5387, 5393, 5399, 5407, 5413, 5417,
  1551. 5419, 5431, 5437, 5441, 5443, 5449, 5471, 5477, 5479, 5483, 5501,
  1552. 5503, 5507, 5519, 5521, 5527, 5531, 5557, 5563, 5569, 5573, 5581,
  1553. 5591, 5623, 5639, 5641, 5647, 5651, 5653, 5657, 5659, 5669, 5683,
  1554. 5689, 5693, 5701, 5711, 5717, 5737, 5741, 5743, 5749, 5779, 5783,
  1555. 5791, 5801, 5807, 5813, 5821, 5827, 5839, 5843, 5849, 5851, 5857,
  1556. 5861, 5867, 5869, 5879, 5881, 5897, 5903, 5923, 5927, 5939, 5953,
  1557. 5981, 5987, 6007, 6011, 6029, 6037, 6043, 6047, 6053, 6067, 6073,
  1558. 6079, 6089, 6091, 6101, 6113, 6121, 6131, 6133, 6143, 6151, 6163,
  1559. 6173, 6197, 6199, 6203, 6211, 6217, 6221, 6229, 6247, 6257, 6263,
  1560. 6269, 6271, 6277, 6287, 6299, 6301, 6311, 6317, 6323, 6329, 6337,
  1561. 6343, 6353, 6359, 6361, 6367, 6373, 6379, 6389, 6397, 6421, 6427,
  1562. 6449, 6451, 6469, 6473, 6481, 6491, 6521, 6529, 6547, 6551, 6553,
  1563. 6563, 6569, 6571, 6577, 6581, 6599, 6607, 6619, 6637, 6653, 6659,
  1564. 6661, 6673, 6679, 6689, 6691, 6701, 6703, 6709, 6719, 6733, 6737,
  1565. 6761, 6763, 6779, 6781, 6791, 6793, 6803, 6823, 6827, 6829, 6833,
  1566. 6841, 6857, 6863, 6869, 6871, 6883, 6899, 6907, 6911, 6917, 6947,
  1567. 6949, 6959, 6961, 6967, 6971, 6977, 6983, 6991, 6997, 7001, 7013,
  1568. 7019, 7027, 7039, 7043, 7057, 7069, 7079, 7103, 7109, 7121, 7127,
  1569. 7129, 7151, 7159, 7177, 7187, 7193, 7207, 7211, 7213, 7219, 7229,
  1570. 7237, 7243, 7247, 7253, 7283, 7297, 7307, 7309, 7321, 7331, 7333,
  1571. 7349, 7351, 7369, 7393, 7411, 7417, 7433, 7451, 7457, 7459, 7477,
  1572. 7481, 7487, 7489, 7499, 7507, 7517, 7523, 7529, 7537, 7541, 7547,
  1573. 7549, 7559, 7561, 7573, 7577, 7583, 7589, 7591, 7603, 7607, 7621,
  1574. 7639, 7643, 7649, 7669, 7673, 7681, 7687, 7691, 7699, 7703, 7717,
  1575. 7723, 7727, 7741, 7753, 7757, 7759, 7789, 7793, 7817, 7823, 7829,
  1576. 7841, 7853, 7867, 7873, 7877, 7879, 7883, 7901, 7907, 7919, 7927,
  1577. 7933, 7937, 7949, 7951, 7963, 7993, 8009, 8011, 8017, 8039, 8053,
  1578. 8059, 8069, 8081, 8087, 8089, 8093, 8101, 8111, 8117, 8123, 8147,
  1579. 8161, 8167, 8171, 8179, 8191, 8209, 8219, 8221, 8231, 8233, 8237,
  1580. 8243, 8263, 8269, 8273, 8287, 8291, 8293, 8297, 8311, 8317, 8329,
  1581. 8353, 8363, 8369, 8377, 8387, 8389, 8419, 8423, 8429, 8431, 8443,
  1582. 8447, 8461, 8467, 8501, 8513, 8521, 8527, 8537, 8539, 8543, 8563,
  1583. 8573, 8581, 8597, 8599, 8609, 8623, 8627, 8629, 8641, 8647, 8663,
  1584. 8669, 8677, 8681, 8689, 8693, 8699, 8707, 8713, 8719, 8731, 8737,
  1585. 8741, 8747, 8753, 8761, 8779, 8783, 8803, 8807, 8819, 8821, 8831,
  1586. 8837, 8839, 8849, 8861, 8863, 8867, 8887, 8893, 8923, 8929, 8933,
  1587. 8941, 8951, 8963, 8969, 8971, 8999, 9001, 9007, 9011, 9013, 9029,
  1588. 9041, 9043, 9049, 9059, 9067, 9091, 9103, 9109, 9127, 9133, 9137,
  1589. 9151, 9157, 9161, 9173, 9181, 9187, 9199, 9203, 9209, 9221, 9227,
  1590. 9239, 9241, 9257, 9277, 9281, 9283, 9293, 9311, 9319, 9323, 9337,
  1591. 9341, 9343, 9349, 9371, 9377, 9391, 9397, 9403, 9413, 9419, 9421,
  1592. 9431, 9433, 9437, 9439, 9461, 9463, 9467, 9473, 9479, 9491, 9497,
  1593. 9511, 9521, 9533, 9539, 9547, 9551, 9587, 9601, 9613, 9619, 9623,
  1594. 9629, 9631, 9643, 9649, 9661, 9677, 9679, 9689, 9697, 9719, 9721,
  1595. 9733, 9739, 9743, 9749, 9767, 9769, 9781, 9787, 9791, 9803, 9811,
  1596. 9817, 9829, 9833, 9839, 9851, 9857, 9859, 9871, 9883, 9887, 9901,
  1597. 9907, 9923, 9929, 9931, 9941, 9949, 9967, 9973, 10007, 10009, 10037,
  1598. 10039, 10061, 10067, 10069, 10079, 10091, 10093, 10099, 10103, 10111, 10133,
  1599. 10139, 10141, 10151, 10159, 10163, 10169, 10177, 10181, 10193, 10211, 10223,
  1600. 10243, 10247, 10253, 10259, 10267, 10271, 10273, 10289, 10301, 10303, 10313,
  1601. 10321, 10331, 10333, 10337, 10343, 10357, 10369, 10391, 10399, 10427, 10429,
  1602. 10433, 10453, 10457, 10459, 10463, 10477, 10487, 10499, 10501, 10513, 10529,
  1603. 10531, 10559, 10567, 10589, 10597, 10601, 10607, 10613, 10627, 10631, 10639,
  1604. 10651, 10657, 10663, 10667, 10687, 10691, 10709, 10711, 10723, 10729, 10733,
  1605. 10739, 10753, 10771, 10781, 10789, 10799, 10831, 10837, 10847, 10853, 10859,
  1606. 10861, 10867, 10883, 10889, 10891, 10903, 10909, 10937, 10939, 10949, 10957,
  1607. 10973, 10979, 10987, 10993, 11003, 11027, 11047, 11057, 11059, 11069, 11071,
  1608. 11083, 11087, 11093, 11113, 11117, 11119, 11131, 11149, 11159, 11161, 11171,
  1609. 11173, 11177, 11197, 11213, 11239, 11243, 11251, 11257, 11261, 11273, 11279,
  1610. 11287, 11299, 11311, 11317, 11321, 11329, 11351, 11353, 11369, 11383, 11393,
  1611. 11399, 11411, 11423, 11437, 11443, 11447, 11467, 11471, 11483, 11489, 11491,
  1612. 11497, 11503, 11519, 11527, 11549, 11551, 11579, 11587, 11593, 11597, 11617,
  1613. 11621, 11633, 11657, 11677, 11681, 11689, 11699, 11701, 11717, 11719, 11731,
  1614. 11743, 11777, 11779, 11783, 11789, 11801, 11807, 11813, 11821, 11827, 11831,
  1615. 11833, 11839, 11863, 11867, 11887, 11897, 11903, 11909, 11923, 11927, 11933,
  1616. 11939, 11941, 11953, 11959, 11969, 11971, 11981, 11987, 12007, 12011, 12037,
  1617. 12041, 12043, 12049, 12071, 12073, 12097, 12101, 12107, 12109, 12113, 12119,
  1618. 12143, 12149, 12157, 12161, 12163, 12197, 12203, 12211, 12227, 12239, 12241,
  1619. 12251, 12253, 12263, 12269, 12277, 12281, 12289, 12301, 12323, 12329, 12343,
  1620. 12347, 12373, 12377, 12379, 12391, 12401, 12409, 12413, 12421, 12433, 12437,
  1621. 12451, 12457, 12473, 12479, 12487, 12491, 12497, 12503, 12511, 12517, 12527,
  1622. 12539, 12541, 12547, 12553, 12569, 12577, 12583, 12589, 12601, 12611, 12613,
  1623. 12619, 12637, 12641, 12647, 12653, 12659, 12671, 12689, 12697, 12703, 12713,
  1624. 12721, 12739, 12743, 12757, 12763, 12781, 12791, 12799, 12809, 12821, 12823,
  1625. 12829, 12841, 12853, 12889, 12893, 12899, 12907, 12911, 12917, 12919, 12923,
  1626. 12941, 12953, 12959, 12967, 12973, 12979, 12983, 13001, 13003, 13007, 13009,
  1627. 13033, 13037, 13043, 13049, 13063, 13093, 13099, 13103, 13109, 13121, 13127,
  1628. 13147, 13151, 13159, 13163, 13171, 13177, 13183, 13187, 13217, 13219, 13229,
  1629. 13241, 13249, 13259, 13267, 13291, 13297, 13309, 13313, 13327, 13331, 13337,
  1630. 13339, 13367, 13381, 13397, 13399, 13411, 13417, 13421, 13441, 13451, 13457,
  1631. 13463, 13469, 13477, 13487, 13499, 13513, 13523, 13537, 13553, 13567, 13577,
  1632. 13591, 13597, 13613, 13619, 13627, 13633, 13649, 13669, 13679, 13681, 13687,
  1633. 13691, 13693, 13697, 13709, 13711, 13721, 13723, 13729, 13751, 13757, 13759,
  1634. 13763, 13781, 13789, 13799, 13807, 13829, 13831, 13841, 13859, 13873, 13877,
  1635. 13879, 13883, 13901, 13903, 13907, 13913, 13921, 13931, 13933, 13963, 13967,
  1636. 13997, 13999, 14009, 14011, 14029, 14033, 14051, 14057, 14071, 14081, 14083,
  1637. 14087, 14107, 14143, 14149, 14153, 14159, 14173, 14177, 14197, 14207, 14221,
  1638. 14243, 14249, 14251, 14281, 14293, 14303, 14321, 14323, 14327, 14341, 14347,
  1639. 14369, 14387, 14389, 14401, 14407, 14411, 14419, 14423, 14431, 14437, 14447,
  1640. 14449, 14461, 14479, 14489, 14503, 14519, 14533, 14537, 14543, 14549, 14551,
  1641. 14557, 14561, 14563, 14591, 14593, 14621, 14627, 14629, 14633, 14639, 14653,
  1642. 14657, 14669, 14683, 14699, 14713, 14717, 14723, 14731, 14737, 14741, 14747,
  1643. 14753, 14759, 14767, 14771, 14779, 14783, 14797, 14813, 14821, 14827, 14831,
  1644. 14843, 14851, 14867, 14869, 14879, 14887, 14891, 14897, 14923, 14929, 14939,
  1645. 14947, 14951, 14957, 14969, 14983, 15013, 15017, 15031, 15053, 15061, 15073,
  1646. 15077, 15083, 15091, 15101, 15107, 15121, 15131, 15137, 15139, 15149, 15161,
  1647. 15173, 15187, 15193, 15199, 15217, 15227, 15233, 15241, 15259, 15263, 15269,
  1648. 15271, 15277, 15287, 15289, 15299, 15307, 15313, 15319, 15329, 15331, 15349,
  1649. 15359, 15361, 15373, 15377, 15383, 15391, 15401, 15413, 15427, 15439, 15443,
  1650. 15451, 15461, 15467, 15473, 15493, 15497, 15511, 15527, 15541, 15551, 15559,
  1651. 15569, 15581, 15583, 15601, 15607, 15619, 15629, 15641, 15643, 15647, 15649,
  1652. 15661, 15667, 15671, 15679, 15683, 15727, 15731, 15733, 15737, 15739, 15749,
  1653. 15761, 15767, 15773, 15787, 15791, 15797, 15803, 15809, 15817, 15823, 15859,
  1654. 15877, 15881, 15887, 15889, 15901, 15907, 15913, 15919, 15923, 15937, 15959,
  1655. 15971, 15973, 15991, 16001, 16007, 16033, 16057, 16061, 16063, 16067, 16069,
  1656. 16073, 16087, 16091, 16097, 16103, 16111, 16127, 16139, 16141, 16183, 16187,
  1657. 16189, 16193, 16217, 16223, 16229, 16231, 16249, 16253, 16267, 16273, 16301,
  1658. 16319, 16333, 16339, 16349, 16361, 16363, 16369, 16381, 16411, 16417, 16421,
  1659. 16427, 16433, 16447, 16451, 16453, 16477, 16481, 16487, 16493, 16519, 16529,
  1660. 16547, 16553, 16561, 16567, 16573, 16603, 16607, 16619, 16631, 16633, 16649,
  1661. 16651, 16657, 16661, 16673, 16691, 16693, 16699, 16703, 16729, 16741, 16747,
  1662. 16759, 16763, 16787, 16811, 16823, 16829, 16831, 16843, 16871, 16879, 16883,
  1663. 16889, 16901, 16903, 16921, 16927, 16931, 16937, 16943, 16963, 16979, 16981,
  1664. 16987, 16993, 17011, 17021, 17027, 17029, 17033, 17041, 17047, 17053, 17077,
  1665. 17093, 17099, 17107, 17117, 17123, 17137, 17159, 17167, 17183, 17189, 17191,
  1666. 17203, 17207, 17209, 17231, 17239, 17257, 17291, 17293, 17299, 17317, 17321,
  1667. 17327, 17333, 17341, 17351, 17359, 17377, 17383, 17387, 17389, 17393, 17401,
  1668. 17417, 17419, 17431, 17443, 17449, 17467, 17471, 17477, 17483, 17489, 17491,
  1669. 17497, 17509, 17519, 17539, 17551, 17569, 17573, 17579, 17581, 17597, 17599,
  1670. 17609, 17623, 17627, 17657, 17659, 17669, 17681, 17683, 17707, 17713, 17729,
  1671. 17737, 17747, 17749, 17761, 17783, 17789, 17791, 17807, 17827, 17837, 17839,
  1672. 17851, 17863, 17881, 17891, 17903, 17909, 17911, 17921, 17923, 17929, 17939,
  1673. 17957, 17959, 17971, 17977, 17981, 17987, 17989, 18013, 18041, 18043, 18047,
  1674. 18049, 18059, 18061, 18077, 18089, 18097, 18119, 18121, 18127, 18131, 18133,
  1675. 18143, 18149, 18169, 18181, 18191, 18199, 18211, 18217, 18223, 18229, 18233,
  1676. 18251, 18253, 18257, 18269, 18287, 18289, 18301, 18307, 18311, 18313, 18329,
  1677. 18341, 18353, 18367, 18371, 18379, 18397, 18401, 18413, 18427, 18433, 18439,
  1678. 18443, 18451, 18457, 18461, 18481, 18493, 18503, 18517, 18521, 18523, 18539,
  1679. 18541, 18553, 18583, 18587, 18593, 18617, 18637, 18661, 18671, 18679, 18691,
  1680. 18701, 18713, 18719, 18731, 18743, 18749, 18757, 18773, 18787, 18793, 18797,
  1681. 18803, 18839, 18859, 18869, 18899, 18911, 18913, 18917, 18919, 18947, 18959,
  1682. 18973, 18979, 19001, 19009, 19013, 19031, 19037, 19051, 19069, 19073, 19079,
  1683. 19081, 19087, 19121, 19139, 19141, 19157, 19163, 19181, 19183, 19207, 19211,
  1684. 19213, 19219, 19231, 19237, 19249, 19259, 19267, 19273, 19289, 19301, 19309,
  1685. 19319, 19333, 19373, 19379, 19381, 19387, 19391, 19403, 19417, 19421, 19423,
  1686. 19427, 19429, 19433, 19441, 19447, 19457, 19463, 19469, 19471, 19477, 19483,
  1687. 19489, 19501, 19507, 19531, 19541, 19543, 19553, 19559, 19571, 19577, 19583,
  1688. 19597, 19603, 19609, 19661, 19681, 19687, 19697, 19699, 19709, 19717, 19727,
  1689. 19739, 19751, 19753, 19759, 19763, 19777, 19793, 19801, 19813, 19819, 19841,
  1690. 19843, 19853, 19861, 19867, 19889, 19891, 19913, 19919, 19927, 19937, 19949,
  1691. 19961, 19963, 19973, 19979, 19991, 19993, 19997,
  1692. };
  1693. TEST_F(BNTest, PrimeChecking) {
  1694. bssl::UniquePtr<BIGNUM> p(BN_new());
  1695. ASSERT_TRUE(p);
  1696. int is_probably_prime_1 = 0, is_probably_prime_2 = 0;
  1697. enum bn_primality_result_t result_3;
  1698. const int max_prime = kPrimes[OPENSSL_ARRAY_SIZE(kPrimes)-1];
  1699. size_t next_prime_index = 0;
  1700. for (int i = 0; i <= max_prime; i++) {
  1701. SCOPED_TRACE(i);
  1702. bool is_prime = false;
  1703. if (i == kPrimes[next_prime_index]) {
  1704. is_prime = true;
  1705. next_prime_index++;
  1706. }
  1707. ASSERT_TRUE(BN_set_word(p.get(), i));
  1708. ASSERT_TRUE(BN_primality_test(
  1709. &is_probably_prime_1, p.get(), BN_prime_checks, ctx(),
  1710. false /* do_trial_division */, nullptr /* callback */));
  1711. EXPECT_EQ(is_prime ? 1 : 0, is_probably_prime_1);
  1712. ASSERT_TRUE(BN_primality_test(
  1713. &is_probably_prime_2, p.get(), BN_prime_checks, ctx(),
  1714. true /* do_trial_division */, nullptr /* callback */));
  1715. EXPECT_EQ(is_prime ? 1 : 0, is_probably_prime_2);
  1716. if (i > 3 && i % 2 == 1) {
  1717. ASSERT_TRUE(BN_enhanced_miller_rabin_primality_test(
  1718. &result_3, p.get(), BN_prime_checks, ctx(), nullptr /* callback */));
  1719. EXPECT_EQ(is_prime, result_3 == bn_probably_prime);
  1720. }
  1721. }
  1722. // Negative numbers are not prime.
  1723. ASSERT_TRUE(BN_set_word(p.get(), 7));
  1724. BN_set_negative(p.get(), 1);
  1725. ASSERT_TRUE(BN_primality_test(&is_probably_prime_1, p.get(), BN_prime_checks,
  1726. ctx(), false /* do_trial_division */,
  1727. nullptr /* callback */));
  1728. EXPECT_EQ(0, is_probably_prime_1);
  1729. ASSERT_TRUE(BN_primality_test(&is_probably_prime_2, p.get(), BN_prime_checks,
  1730. ctx(), true /* do_trial_division */,
  1731. nullptr /* callback */));
  1732. EXPECT_EQ(0, is_probably_prime_2);
  1733. // The following composite numbers come from http://oeis.org/A014233 and are
  1734. // such that the first several primes are not a Rabin-Miller composite
  1735. // witness.
  1736. static const char *kA014233[] = {
  1737. "2047",
  1738. "1373653",
  1739. "25326001",
  1740. "3215031751",
  1741. "2152302898747",
  1742. "3474749660383",
  1743. "341550071728321",
  1744. "3825123056546413051",
  1745. "318665857834031151167461",
  1746. "3317044064679887385961981",
  1747. };
  1748. for (const char *str : kA014233) {
  1749. SCOPED_TRACE(str);
  1750. EXPECT_NE(0, DecimalToBIGNUM(&p, str));
  1751. ASSERT_TRUE(BN_primality_test(
  1752. &is_probably_prime_1, p.get(), BN_prime_checks, ctx(),
  1753. false /* do_trial_division */, nullptr /* callback */));
  1754. EXPECT_EQ(0, is_probably_prime_1);
  1755. ASSERT_TRUE(BN_primality_test(
  1756. &is_probably_prime_2, p.get(), BN_prime_checks, ctx(),
  1757. true /* do_trial_division */, nullptr /* callback */));
  1758. EXPECT_EQ(0, is_probably_prime_2);
  1759. ASSERT_TRUE(BN_enhanced_miller_rabin_primality_test(
  1760. &result_3, p.get(), BN_prime_checks, ctx(), nullptr /* callback */));
  1761. EXPECT_EQ(bn_composite, result_3);
  1762. }
  1763. // BN_primality_test works with null |BN_CTX|.
  1764. ASSERT_TRUE(BN_set_word(p.get(), 5));
  1765. ASSERT_TRUE(BN_primality_test(
  1766. &is_probably_prime_1, p.get(), BN_prime_checks, nullptr /* ctx */,
  1767. false /* do_trial_division */, nullptr /* callback */));
  1768. EXPECT_EQ(1, is_probably_prime_1);
  1769. }
  1770. TEST_F(BNTest, NumBitsWord) {
  1771. constexpr BN_ULONG kOne = 1;
  1772. // 2^(N-1) takes N bits.
  1773. for (unsigned i = 1; i < BN_BITS2; i++) {
  1774. EXPECT_EQ(i, BN_num_bits_word(kOne << (i - 1))) << i;
  1775. }
  1776. // 2^N - 1 takes N bits.
  1777. for (unsigned i = 0; i < BN_BITS2; i++) {
  1778. EXPECT_EQ(i, BN_num_bits_word((kOne << i) - 1)) << i;
  1779. }
  1780. for (unsigned i = 1; i < 100; i++) {
  1781. // Generate a random value of a random length.
  1782. uint8_t buf[1 + sizeof(BN_ULONG)];
  1783. RAND_bytes(buf, sizeof(buf));
  1784. BN_ULONG w;
  1785. memcpy(&w, &buf[1], sizeof(w));
  1786. const unsigned num_bits = buf[0] % (BN_BITS2 + 1);
  1787. if (num_bits == BN_BITS2) {
  1788. w |= kOne << (BN_BITS2 - 1);
  1789. } else if (num_bits == 0) {
  1790. w = 0;
  1791. } else {
  1792. w &= (kOne << num_bits) - 1;
  1793. w |= kOne << (num_bits - 1);
  1794. }
  1795. EXPECT_EQ(num_bits, BN_num_bits_word(w)) << w;
  1796. }
  1797. }
  1798. #if !defined(BORINGSSL_SHARED_LIBRARY)
  1799. TEST_F(BNTest, LessThanWords) {
  1800. // kTestVectors is an array of 256-bit values in sorted order.
  1801. static const BN_ULONG kTestVectors[][256 / BN_BITS2] = {
  1802. {TOBN(0x00000000, 0x00000000), TOBN(0x00000000, 0x00000000),
  1803. TOBN(0x00000000, 0x00000000), TOBN(0x00000000, 0x00000000)},
  1804. {TOBN(0x00000000, 0x00000001), TOBN(0x00000000, 0x00000000),
  1805. TOBN(0x00000000, 0x00000000), TOBN(0x00000000, 0x00000000)},
  1806. {TOBN(0x00000000, 0x00000002), TOBN(0x00000000, 0x00000000),
  1807. TOBN(0x00000000, 0x00000000), TOBN(0x00000000, 0x00000000)},
  1808. {TOBN(0x00000000, 0x0000ffff), TOBN(0x00000000, 0x00000000),
  1809. TOBN(0x00000000, 0x00000000), TOBN(0x00000000, 0x00000000)},
  1810. {TOBN(0x00000000, 0x83339914), TOBN(0x00000000, 0x00000000),
  1811. TOBN(0x00000000, 0x00000000), TOBN(0x00000000, 0x00000000)},
  1812. {TOBN(0x00000000, 0xfffffffe), TOBN(0x00000000, 0x00000000),
  1813. TOBN(0x00000000, 0x00000000), TOBN(0x00000000, 0x00000000)},
  1814. {TOBN(0x00000000, 0xffffffff), TOBN(0x00000000, 0x00000000),
  1815. TOBN(0x00000000, 0x00000000), TOBN(0x00000000, 0x00000000)},
  1816. {TOBN(0xed17ac85, 0x83339914), TOBN(0x00000000, 0x00000000),
  1817. TOBN(0x00000000, 0x00000000), TOBN(0x00000000, 0x00000000)},
  1818. {TOBN(0xffffffff, 0xffffffff), TOBN(0x00000000, 0x00000000),
  1819. TOBN(0x00000000, 0x00000000), TOBN(0x00000000, 0x00000000)},
  1820. {TOBN(0x00000000, 0x83339914), TOBN(0x00000000, 0x00000001),
  1821. TOBN(0x00000000, 0x00000000), TOBN(0x00000000, 0x00000000)},
  1822. {TOBN(0xffffffff, 0xffffffff), TOBN(0xffffffff, 0xffffffff),
  1823. TOBN(0x00000000, 0x00000000), TOBN(0x00000000, 0x00000000)},
  1824. {TOBN(0xffffffff, 0xffffffff), TOBN(0xffffffff, 0xffffffff),
  1825. TOBN(0xffffffff, 0xffffffff), TOBN(0x00000000, 0x00000000)},
  1826. {TOBN(0x00000000, 0x00000000), TOBN(0x1d6f60ba, 0x893ba84c),
  1827. TOBN(0x597d89b3, 0x754abe9f), TOBN(0xb504f333, 0xf9de6484)},
  1828. {TOBN(0x00000000, 0x83339915), TOBN(0x1d6f60ba, 0x893ba84c),
  1829. TOBN(0x597d89b3, 0x754abe9f), TOBN(0xb504f333, 0xf9de6484)},
  1830. {TOBN(0xed17ac85, 0x00000000), TOBN(0x1d6f60ba, 0x893ba84c),
  1831. TOBN(0x597d89b3, 0x754abe9f), TOBN(0xb504f333, 0xf9de6484)},
  1832. {TOBN(0xed17ac85, 0x83339915), TOBN(0x1d6f60ba, 0x893ba84c),
  1833. TOBN(0x597d89b3, 0x754abe9f), TOBN(0xb504f333, 0xf9de6484)},
  1834. {TOBN(0xed17ac85, 0xffffffff), TOBN(0x1d6f60ba, 0x893ba84c),
  1835. TOBN(0x597d89b3, 0x754abe9f), TOBN(0xb504f333, 0xf9de6484)},
  1836. {TOBN(0xffffffff, 0x83339915), TOBN(0x1d6f60ba, 0x893ba84c),
  1837. TOBN(0x597d89b3, 0x754abe9f), TOBN(0xb504f333, 0xf9de6484)},
  1838. {TOBN(0xffffffff, 0xffffffff), TOBN(0x1d6f60ba, 0x893ba84c),
  1839. TOBN(0x597d89b3, 0x754abe9f), TOBN(0xb504f333, 0xf9de6484)},
  1840. {TOBN(0x00000000, 0x00000000), TOBN(0x00000000, 0x00000000),
  1841. TOBN(0x00000000, 0x00000000), TOBN(0xffffffff, 0xffffffff)},
  1842. {TOBN(0x00000000, 0x00000000), TOBN(0x00000000, 0x00000000),
  1843. TOBN(0xffffffff, 0xffffffff), TOBN(0xffffffff, 0xffffffff)},
  1844. {TOBN(0x00000000, 0x00000001), TOBN(0x00000000, 0x00000000),
  1845. TOBN(0xffffffff, 0xffffffff), TOBN(0xffffffff, 0xffffffff)},
  1846. {TOBN(0x00000000, 0x00000000), TOBN(0xffffffff, 0xffffffff),
  1847. TOBN(0xffffffff, 0xffffffff), TOBN(0xffffffff, 0xffffffff)},
  1848. {TOBN(0xffffffff, 0xffffffff), TOBN(0xffffffff, 0xffffffff),
  1849. TOBN(0xffffffff, 0xffffffff), TOBN(0xffffffff, 0xffffffff)},
  1850. };
  1851. // Determine where the single-word values stop.
  1852. size_t one_word;
  1853. for (one_word = 0; one_word < OPENSSL_ARRAY_SIZE(kTestVectors); one_word++) {
  1854. int is_word = 1;
  1855. for (size_t i = 1; i < OPENSSL_ARRAY_SIZE(kTestVectors[one_word]); i++) {
  1856. if (kTestVectors[one_word][i] != 0) {
  1857. is_word = 0;
  1858. break;
  1859. }
  1860. }
  1861. if (!is_word) {
  1862. break;
  1863. }
  1864. }
  1865. for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kTestVectors); i++) {
  1866. SCOPED_TRACE(i);
  1867. for (size_t j = 0; j < OPENSSL_ARRAY_SIZE(kTestVectors); j++) {
  1868. SCOPED_TRACE(j);
  1869. EXPECT_EQ(i < j ? 1 : 0,
  1870. bn_less_than_words(kTestVectors[i], kTestVectors[j],
  1871. OPENSSL_ARRAY_SIZE(kTestVectors[i])));
  1872. for (size_t k = 0; k < one_word; k++) {
  1873. SCOPED_TRACE(k);
  1874. EXPECT_EQ(k <= i && i < j ? 1 : 0,
  1875. bn_in_range_words(kTestVectors[i], kTestVectors[k][0],
  1876. kTestVectors[j],
  1877. OPENSSL_ARRAY_SIZE(kTestVectors[i])));
  1878. }
  1879. }
  1880. }
  1881. EXPECT_EQ(0, bn_less_than_words(NULL, NULL, 0));
  1882. EXPECT_EQ(0, bn_in_range_words(NULL, 0, NULL, 0));
  1883. }
  1884. #endif // !BORINGSSL_SHARED_LIBRARY
  1885. TEST_F(BNTest, NonMinimal) {
  1886. bssl::UniquePtr<BIGNUM> ten(BN_new());
  1887. ASSERT_TRUE(ten);
  1888. ASSERT_TRUE(BN_set_word(ten.get(), 10));
  1889. bssl::UniquePtr<BIGNUM> ten_copy(BN_dup(ten.get()));
  1890. ASSERT_TRUE(ten_copy);
  1891. bssl::UniquePtr<BIGNUM> eight(BN_new());
  1892. ASSERT_TRUE(eight);
  1893. ASSERT_TRUE(BN_set_word(eight.get(), 8));
  1894. bssl::UniquePtr<BIGNUM> forty_two(BN_new());
  1895. ASSERT_TRUE(forty_two);
  1896. ASSERT_TRUE(BN_set_word(forty_two.get(), 42));
  1897. bssl::UniquePtr<BIGNUM> two_exp_256(BN_new());
  1898. ASSERT_TRUE(two_exp_256);
  1899. ASSERT_TRUE(BN_lshift(two_exp_256.get(), BN_value_one(), 256));
  1900. bssl::UniquePtr<BIGNUM> zero(BN_new());
  1901. ASSERT_TRUE(zero);
  1902. BN_zero(zero.get());
  1903. for (size_t width = 1; width < 10; width++) {
  1904. SCOPED_TRACE(width);
  1905. // Make |ten| and |zero| wider.
  1906. EXPECT_TRUE(bn_resize_words(ten.get(), width));
  1907. EXPECT_EQ(static_cast<int>(width), ten->width);
  1908. EXPECT_TRUE(bn_resize_words(zero.get(), width));
  1909. EXPECT_EQ(static_cast<int>(width), zero->width);
  1910. EXPECT_TRUE(BN_abs_is_word(ten.get(), 10));
  1911. EXPECT_TRUE(BN_is_word(ten.get(), 10));
  1912. EXPECT_EQ(10u, BN_get_word(ten.get()));
  1913. uint64_t v;
  1914. ASSERT_TRUE(BN_get_u64(ten.get(), &v));
  1915. EXPECT_EQ(10u, v);
  1916. EXPECT_TRUE(BN_equal_consttime(ten.get(), ten_copy.get()));
  1917. EXPECT_TRUE(BN_equal_consttime(ten_copy.get(), ten.get()));
  1918. EXPECT_EQ(BN_cmp(ten.get(), ten_copy.get()), 0);
  1919. EXPECT_EQ(BN_cmp(ten_copy.get(), ten.get()), 0);
  1920. EXPECT_FALSE(BN_equal_consttime(ten.get(), eight.get()));
  1921. EXPECT_LT(BN_cmp(eight.get(), ten.get()), 0);
  1922. EXPECT_GT(BN_cmp(ten.get(), eight.get()), 0);
  1923. EXPECT_FALSE(BN_equal_consttime(ten.get(), forty_two.get()));
  1924. EXPECT_GT(BN_cmp(forty_two.get(), ten.get()), 0);
  1925. EXPECT_LT(BN_cmp(ten.get(), forty_two.get()), 0);
  1926. EXPECT_FALSE(BN_equal_consttime(ten.get(), two_exp_256.get()));
  1927. EXPECT_GT(BN_cmp(two_exp_256.get(), ten.get()), 0);
  1928. EXPECT_LT(BN_cmp(ten.get(), two_exp_256.get()), 0);
  1929. EXPECT_EQ(4u, BN_num_bits(ten.get()));
  1930. EXPECT_EQ(1u, BN_num_bytes(ten.get()));
  1931. EXPECT_FALSE(BN_is_pow2(ten.get()));
  1932. bssl::UniquePtr<char> hex(BN_bn2hex(ten.get()));
  1933. EXPECT_STREQ("0a", hex.get());
  1934. hex.reset(BN_bn2hex(zero.get()));
  1935. EXPECT_STREQ("0", hex.get());
  1936. bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
  1937. ASSERT_TRUE(bio);
  1938. ASSERT_TRUE(BN_print(bio.get(), ten.get()));
  1939. const uint8_t *ptr;
  1940. size_t len;
  1941. ASSERT_TRUE(BIO_mem_contents(bio.get(), &ptr, &len));
  1942. // TODO(davidben): |BN_print| removes leading zeros within a byte, while
  1943. // |BN_bn2hex| rounds up to a byte, except for zero which it prints as
  1944. // "0". Fix this discrepancy?
  1945. EXPECT_EQ(Bytes("a"), Bytes(ptr, len));
  1946. bio.reset(BIO_new(BIO_s_mem()));
  1947. ASSERT_TRUE(bio);
  1948. ASSERT_TRUE(BN_print(bio.get(), zero.get()));
  1949. ASSERT_TRUE(BIO_mem_contents(bio.get(), &ptr, &len));
  1950. EXPECT_EQ(Bytes("0"), Bytes(ptr, len));
  1951. }
  1952. // |ten| may be resized back down to one word.
  1953. EXPECT_TRUE(bn_resize_words(ten.get(), 1));
  1954. EXPECT_EQ(1, ten->width);
  1955. // But not to zero words, which it does not fit.
  1956. EXPECT_FALSE(bn_resize_words(ten.get(), 0));
  1957. EXPECT_TRUE(BN_is_pow2(eight.get()));
  1958. EXPECT_TRUE(bn_resize_words(eight.get(), 4));
  1959. EXPECT_EQ(4, eight->width);
  1960. EXPECT_TRUE(BN_is_pow2(eight.get()));
  1961. // |BN_MONT_CTX| is always stored minimally and uses the same R independent of
  1962. // input width. Additionally, mont->RR is always the same width as mont->N,
  1963. // even if it fits in a smaller value.
  1964. static const uint8_t kP[] = {
  1965. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1966. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1967. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01,
  1968. };
  1969. bssl::UniquePtr<BIGNUM> p(BN_bin2bn(kP, sizeof(kP), nullptr));
  1970. ASSERT_TRUE(p);
  1971. // Test both the constant-time and variable-time functions at both minimal and
  1972. // non-minimal |p|.
  1973. bssl::UniquePtr<BN_MONT_CTX> mont(
  1974. BN_MONT_CTX_new_for_modulus(p.get(), ctx()));
  1975. ASSERT_TRUE(mont);
  1976. bssl::UniquePtr<BN_MONT_CTX> mont2(
  1977. BN_MONT_CTX_new_consttime(p.get(), ctx()));
  1978. ASSERT_TRUE(mont2);
  1979. ASSERT_TRUE(bn_resize_words(p.get(), 32));
  1980. bssl::UniquePtr<BN_MONT_CTX> mont3(
  1981. BN_MONT_CTX_new_for_modulus(p.get(), ctx()));
  1982. ASSERT_TRUE(mont3);
  1983. bssl::UniquePtr<BN_MONT_CTX> mont4(
  1984. BN_MONT_CTX_new_consttime(p.get(), ctx()));
  1985. ASSERT_TRUE(mont4);
  1986. EXPECT_EQ(mont->N.width, mont2->N.width);
  1987. EXPECT_EQ(mont->N.width, mont3->N.width);
  1988. EXPECT_EQ(mont->N.width, mont4->N.width);
  1989. EXPECT_EQ(0, BN_cmp(&mont->RR, &mont2->RR));
  1990. EXPECT_EQ(0, BN_cmp(&mont->RR, &mont3->RR));
  1991. EXPECT_EQ(0, BN_cmp(&mont->RR, &mont4->RR));
  1992. EXPECT_EQ(mont->N.width, mont->RR.width);
  1993. EXPECT_EQ(mont->N.width, mont2->RR.width);
  1994. EXPECT_EQ(mont->N.width, mont3->RR.width);
  1995. EXPECT_EQ(mont->N.width, mont4->RR.width);
  1996. }
  1997. TEST_F(BNTest, CountLowZeroBits) {
  1998. bssl::UniquePtr<BIGNUM> bn(BN_new());
  1999. ASSERT_TRUE(bn);
  2000. for (int i = 0; i < BN_BITS2; i++) {
  2001. SCOPED_TRACE(i);
  2002. for (int set_high_bits = 0; set_high_bits < 2; set_high_bits++) {
  2003. BN_ULONG word = ((BN_ULONG)1) << i;
  2004. if (set_high_bits) {
  2005. BN_ULONG junk;
  2006. RAND_bytes(reinterpret_cast<uint8_t *>(&junk), sizeof(junk));
  2007. word |= junk & ~(word - 1);
  2008. }
  2009. SCOPED_TRACE(word);
  2010. ASSERT_TRUE(BN_set_word(bn.get(), word));
  2011. EXPECT_EQ(i, BN_count_low_zero_bits(bn.get()));
  2012. ASSERT_TRUE(bn_resize_words(bn.get(), 16));
  2013. EXPECT_EQ(i, BN_count_low_zero_bits(bn.get()));
  2014. ASSERT_TRUE(BN_set_word(bn.get(), word));
  2015. ASSERT_TRUE(BN_lshift(bn.get(), bn.get(), BN_BITS2 * 5));
  2016. EXPECT_EQ(i + BN_BITS2 * 5, BN_count_low_zero_bits(bn.get()));
  2017. ASSERT_TRUE(bn_resize_words(bn.get(), 16));
  2018. EXPECT_EQ(i + BN_BITS2 * 5, BN_count_low_zero_bits(bn.get()));
  2019. ASSERT_TRUE(BN_set_word(bn.get(), word));
  2020. ASSERT_TRUE(BN_set_bit(bn.get(), BN_BITS2 * 5));
  2021. EXPECT_EQ(i, BN_count_low_zero_bits(bn.get()));
  2022. ASSERT_TRUE(bn_resize_words(bn.get(), 16));
  2023. EXPECT_EQ(i, BN_count_low_zero_bits(bn.get()));
  2024. }
  2025. }
  2026. BN_zero(bn.get());
  2027. EXPECT_EQ(0, BN_count_low_zero_bits(bn.get()));
  2028. ASSERT_TRUE(bn_resize_words(bn.get(), 16));
  2029. EXPECT_EQ(0, BN_count_low_zero_bits(bn.get()));
  2030. }
  2031. TEST_F(BNTest, WriteIntoNegative) {
  2032. bssl::UniquePtr<BIGNUM> r(BN_new());
  2033. ASSERT_TRUE(r);
  2034. bssl::UniquePtr<BIGNUM> two(BN_new());
  2035. ASSERT_TRUE(two);
  2036. ASSERT_TRUE(BN_set_word(two.get(), 2));
  2037. bssl::UniquePtr<BIGNUM> three(BN_new());
  2038. ASSERT_TRUE(three);
  2039. ASSERT_TRUE(BN_set_word(three.get(), 3));
  2040. bssl::UniquePtr<BIGNUM> seven(BN_new());
  2041. ASSERT_TRUE(seven);
  2042. ASSERT_TRUE(BN_set_word(seven.get(), 7));
  2043. ASSERT_TRUE(BN_set_word(r.get(), 1));
  2044. BN_set_negative(r.get(), 1);
  2045. ASSERT_TRUE(BN_mod_add_quick(r.get(), two.get(), three.get(), seven.get()));
  2046. EXPECT_TRUE(BN_is_word(r.get(), 5));
  2047. EXPECT_FALSE(BN_is_negative(r.get()));
  2048. BN_set_negative(r.get(), 1);
  2049. ASSERT_TRUE(BN_mod_sub_quick(r.get(), two.get(), three.get(), seven.get()));
  2050. EXPECT_TRUE(BN_is_word(r.get(), 6));
  2051. EXPECT_FALSE(BN_is_negative(r.get()));
  2052. }
  2053. #if defined(OPENSSL_BN_ASM_MONT) && defined(SUPPORTS_ABI_TEST)
  2054. TEST_F(BNTest, BNMulMontABI) {
  2055. for (size_t words : {4, 5, 6, 7, 8, 16, 32}) {
  2056. SCOPED_TRACE(words);
  2057. bssl::UniquePtr<BIGNUM> m(BN_new());
  2058. ASSERT_TRUE(m);
  2059. ASSERT_TRUE(BN_set_bit(m.get(), 0));
  2060. ASSERT_TRUE(BN_set_bit(m.get(), words * BN_BITS2 - 1));
  2061. bssl::UniquePtr<BN_MONT_CTX> mont(
  2062. BN_MONT_CTX_new_for_modulus(m.get(), ctx()));
  2063. ASSERT_TRUE(mont);
  2064. std::vector<BN_ULONG> r(words), a(words), b(words);
  2065. a[0] = 1;
  2066. b[0] = 42;
  2067. CHECK_ABI(bn_mul_mont, r.data(), a.data(), b.data(), mont->N.d, mont->n0,
  2068. words);
  2069. CHECK_ABI(bn_mul_mont, r.data(), a.data(), a.data(), mont->N.d, mont->n0,
  2070. words);
  2071. }
  2072. }
  2073. #endif // OPENSSL_BN_ASM_MONT && SUPPORTS_ABI_TEST
  2074. #if defined(OPENSSL_BN_ASM_MONT5) && defined(SUPPORTS_ABI_TEST)
  2075. TEST_F(BNTest, BNMulMont5ABI) {
  2076. for (size_t words : {4, 5, 6, 7, 8, 16, 32}) {
  2077. SCOPED_TRACE(words);
  2078. bssl::UniquePtr<BIGNUM> m(BN_new());
  2079. ASSERT_TRUE(m);
  2080. ASSERT_TRUE(BN_set_bit(m.get(), 0));
  2081. ASSERT_TRUE(BN_set_bit(m.get(), words * BN_BITS2 - 1));
  2082. bssl::UniquePtr<BN_MONT_CTX> mont(
  2083. BN_MONT_CTX_new_for_modulus(m.get(), ctx()));
  2084. ASSERT_TRUE(mont);
  2085. std::vector<BN_ULONG> r(words), a(words), b(words), table(words * 32);
  2086. a[0] = 1;
  2087. b[0] = 42;
  2088. bn_mul_mont(r.data(), a.data(), b.data(), mont->N.d, mont->n0, words);
  2089. CHECK_ABI(bn_scatter5, r.data(), words, table.data(), 13);
  2090. for (size_t i = 0; i < 32; i++) {
  2091. bn_mul_mont(r.data(), a.data(), b.data(), mont->N.d, mont->n0, words);
  2092. bn_scatter5(r.data(), words, table.data(), i);
  2093. }
  2094. CHECK_ABI(bn_gather5, r.data(), words, table.data(), 13);
  2095. CHECK_ABI(bn_mul_mont_gather5, r.data(), r.data(), table.data(), m->d,
  2096. mont->n0, words, 13);
  2097. CHECK_ABI(bn_mul_mont_gather5, r.data(), a.data(), table.data(), m->d,
  2098. mont->n0, words, 13);
  2099. if (words % 8 == 0) {
  2100. CHECK_ABI(bn_power5, r.data(), r.data(), table.data(), m->d, mont->n0,
  2101. words, 13);
  2102. CHECK_ABI(bn_power5, r.data(), a.data(), table.data(), m->d, mont->n0,
  2103. words, 13);
  2104. EXPECT_EQ(1, CHECK_ABI(bn_from_montgomery, r.data(), r.data(), nullptr,
  2105. m->d, mont->n0, words));
  2106. EXPECT_EQ(1, CHECK_ABI(bn_from_montgomery, r.data(), a.data(), nullptr,
  2107. m->d, mont->n0, words));
  2108. } else {
  2109. EXPECT_EQ(0, CHECK_ABI(bn_from_montgomery, r.data(), r.data(), nullptr,
  2110. m->d, mont->n0, words));
  2111. EXPECT_EQ(0, CHECK_ABI(bn_from_montgomery, r.data(), a.data(), nullptr,
  2112. m->d, mont->n0, words));
  2113. }
  2114. }
  2115. }
  2116. #endif // OPENSSL_BN_ASM_MONT5 && SUPPORTS_ABI_TEST
  2117. #if defined(RSAZ_ENABLED) && defined(SUPPORTS_ABI_TEST)
  2118. TEST_F(BNTest, RSAZABI) {
  2119. if (!rsaz_avx2_capable()) {
  2120. return;
  2121. }
  2122. alignas(64) BN_ULONG table[32 * 18] = {0};
  2123. alignas(64) BN_ULONG rsaz1[40], rsaz2[40], rsaz3[40], n_rsaz[40];
  2124. BN_ULONG norm[16], n_norm[16];
  2125. OPENSSL_memset(norm, 0x42, sizeof(norm));
  2126. OPENSSL_memset(n_norm, 0x99, sizeof(n_norm));
  2127. bssl::UniquePtr<BIGNUM> n(BN_new());
  2128. ASSERT_TRUE(n);
  2129. ASSERT_TRUE(bn_set_words(n.get(), n_norm, 16));
  2130. bssl::UniquePtr<BN_MONT_CTX> mont(
  2131. BN_MONT_CTX_new_for_modulus(n.get(), nullptr));
  2132. ASSERT_TRUE(mont);
  2133. const BN_ULONG k = mont->n0[0];
  2134. CHECK_ABI(rsaz_1024_norm2red_avx2, rsaz1, norm);
  2135. CHECK_ABI(rsaz_1024_norm2red_avx2, n_rsaz, n_norm);
  2136. CHECK_ABI(rsaz_1024_sqr_avx2, rsaz2, rsaz1, n_rsaz, k, 1);
  2137. CHECK_ABI(rsaz_1024_sqr_avx2, rsaz3, rsaz2, n_rsaz, k, 4);
  2138. CHECK_ABI(rsaz_1024_mul_avx2, rsaz3, rsaz1, rsaz2, n_rsaz, k);
  2139. CHECK_ABI(rsaz_1024_scatter5_avx2, table, rsaz3, 7);
  2140. CHECK_ABI(rsaz_1024_gather5_avx2, rsaz1, table, 7);
  2141. CHECK_ABI(rsaz_1024_red2norm_avx2, norm, rsaz1);
  2142. }
  2143. #endif // RSAZ_ENABLED && SUPPORTS_ABI_TEST