montgomery.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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 (c) 1998-2006 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com). */
  108. #include <openssl/bn.h>
  109. #include <assert.h>
  110. #include <string.h>
  111. #include <openssl/err.h>
  112. #include <openssl/mem.h>
  113. #include <openssl/thread.h>
  114. #include <openssl/type_check.h>
  115. #include "internal.h"
  116. #include "../../internal.h"
  117. #if !defined(OPENSSL_NO_ASM) && \
  118. (defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || \
  119. defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64))
  120. #define OPENSSL_BN_ASM_MONT
  121. #endif
  122. BN_MONT_CTX *BN_MONT_CTX_new(void) {
  123. BN_MONT_CTX *ret = OPENSSL_malloc(sizeof(BN_MONT_CTX));
  124. if (ret == NULL) {
  125. return NULL;
  126. }
  127. OPENSSL_memset(ret, 0, sizeof(BN_MONT_CTX));
  128. BN_init(&ret->RR);
  129. BN_init(&ret->N);
  130. return ret;
  131. }
  132. void BN_MONT_CTX_free(BN_MONT_CTX *mont) {
  133. if (mont == NULL) {
  134. return;
  135. }
  136. BN_free(&mont->RR);
  137. BN_free(&mont->N);
  138. OPENSSL_free(mont);
  139. }
  140. BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, const BN_MONT_CTX *from) {
  141. if (to == from) {
  142. return to;
  143. }
  144. if (!BN_copy(&to->RR, &from->RR) ||
  145. !BN_copy(&to->N, &from->N)) {
  146. return NULL;
  147. }
  148. to->n0[0] = from->n0[0];
  149. to->n0[1] = from->n0[1];
  150. return to;
  151. }
  152. OPENSSL_COMPILE_ASSERT(BN_MONT_CTX_N0_LIMBS == 1 || BN_MONT_CTX_N0_LIMBS == 2,
  153. BN_MONT_CTX_N0_LIMBS_VALUE_INVALID);
  154. OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) * BN_MONT_CTX_N0_LIMBS ==
  155. sizeof(uint64_t), BN_MONT_CTX_set_64_bit_mismatch);
  156. int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx) {
  157. if (BN_is_zero(mod)) {
  158. OPENSSL_PUT_ERROR(BN, BN_R_DIV_BY_ZERO);
  159. return 0;
  160. }
  161. if (!BN_is_odd(mod)) {
  162. OPENSSL_PUT_ERROR(BN, BN_R_CALLED_WITH_EVEN_MODULUS);
  163. return 0;
  164. }
  165. if (BN_is_negative(mod)) {
  166. OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
  167. return 0;
  168. }
  169. // Save the modulus.
  170. if (!BN_copy(&mont->N, mod)) {
  171. OPENSSL_PUT_ERROR(BN, ERR_R_INTERNAL_ERROR);
  172. return 0;
  173. }
  174. // |mont->N| is always stored minimally. Computing RR efficiently leaks the
  175. // size of the modulus. While the modulus may be private in RSA (one of the
  176. // primes), their sizes are public, so this is fine.
  177. bn_set_minimal_width(&mont->N);
  178. // Find n0 such that n0 * N == -1 (mod r).
  179. //
  180. // Only certain BN_BITS2<=32 platforms actually make use of n0[1]. For the
  181. // others, we could use a shorter R value and use faster |BN_ULONG|-based
  182. // math instead of |uint64_t|-based math, which would be double-precision.
  183. // However, currently only the assembler files know which is which.
  184. uint64_t n0 = bn_mont_n0(&mont->N);
  185. mont->n0[0] = (BN_ULONG)n0;
  186. #if BN_MONT_CTX_N0_LIMBS == 2
  187. mont->n0[1] = (BN_ULONG)(n0 >> BN_BITS2);
  188. #else
  189. mont->n0[1] = 0;
  190. #endif
  191. BN_CTX *new_ctx = NULL;
  192. if (ctx == NULL) {
  193. new_ctx = BN_CTX_new();
  194. if (new_ctx == NULL) {
  195. return 0;
  196. }
  197. ctx = new_ctx;
  198. }
  199. // Save RR = R**2 (mod N). R is the smallest power of 2**BN_BITS2 such that R
  200. // > mod. Even though the assembly on some 32-bit platforms works with 64-bit
  201. // values, using |BN_BITS2| here, rather than |BN_MONT_CTX_N0_LIMBS *
  202. // BN_BITS2|, is correct because R**2 will still be a multiple of the latter
  203. // as |BN_MONT_CTX_N0_LIMBS| is either one or two.
  204. unsigned lgBigR = mont->N.width * BN_BITS2;
  205. int ok = bn_mod_exp_base_2_consttime(&mont->RR, lgBigR * 2, &mont->N, ctx);
  206. BN_CTX_free(new_ctx);
  207. return ok;
  208. }
  209. BN_MONT_CTX *BN_MONT_CTX_new_for_modulus(const BIGNUM *mod, BN_CTX *ctx) {
  210. BN_MONT_CTX *mont = BN_MONT_CTX_new();
  211. if (mont == NULL ||
  212. !BN_MONT_CTX_set(mont, mod, ctx)) {
  213. BN_MONT_CTX_free(mont);
  214. return NULL;
  215. }
  216. return mont;
  217. }
  218. int BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_MUTEX *lock,
  219. const BIGNUM *mod, BN_CTX *bn_ctx) {
  220. CRYPTO_MUTEX_lock_read(lock);
  221. BN_MONT_CTX *ctx = *pmont;
  222. CRYPTO_MUTEX_unlock_read(lock);
  223. if (ctx) {
  224. return 1;
  225. }
  226. CRYPTO_MUTEX_lock_write(lock);
  227. if (*pmont == NULL) {
  228. *pmont = BN_MONT_CTX_new_for_modulus(mod, bn_ctx);
  229. }
  230. const int ok = *pmont != NULL;
  231. CRYPTO_MUTEX_unlock_write(lock);
  232. return ok;
  233. }
  234. int BN_to_montgomery(BIGNUM *ret, const BIGNUM *a, const BN_MONT_CTX *mont,
  235. BN_CTX *ctx) {
  236. return BN_mod_mul_montgomery(ret, a, &mont->RR, mont, ctx);
  237. }
  238. static int bn_from_montgomery_in_place(BN_ULONG *r, size_t num_r, BN_ULONG *a,
  239. size_t num_a, const BN_MONT_CTX *mont) {
  240. const BN_ULONG *n = mont->N.d;
  241. size_t num_n = mont->N.width;
  242. if (num_r != num_n || num_a != 2 * num_n) {
  243. OPENSSL_PUT_ERROR(BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  244. return 0;
  245. }
  246. // Add multiples of |n| to |r| until R = 2^(nl * BN_BITS2) divides it. On
  247. // input, we had |r| < |n| * R, so now |r| < 2 * |n| * R. Note that |r|
  248. // includes |carry| which is stored separately.
  249. BN_ULONG n0 = mont->n0[0];
  250. BN_ULONG carry = 0;
  251. for (size_t i = 0; i < num_n; i++) {
  252. BN_ULONG v = bn_mul_add_words(a + i, n, num_n, a[i] * n0);
  253. v += carry + a[i + num_n];
  254. carry |= (v != a[i + num_n]);
  255. carry &= (v <= a[i + num_n]);
  256. a[i + num_n] = v;
  257. }
  258. // Shift |num_n| words to divide by R. We have |a| < 2 * |n|. Note that |a|
  259. // includes |carry| which is stored separately.
  260. a += num_n;
  261. // |a| thus requires at most one additional subtraction |n| to be reduced.
  262. // Subtract |n| and select the answer in constant time.
  263. OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
  264. crypto_word_t_too_small);
  265. BN_ULONG v = bn_sub_words(r, a, n, num_n) - carry;
  266. // |v| is one if |a| - |n| underflowed or zero if it did not. Note |v| cannot
  267. // be -1. That would imply the subtraction did not fit in |num_n| words, and
  268. // we know at most one subtraction is needed.
  269. v = 0u - v;
  270. for (size_t i = 0; i < num_n; i++) {
  271. r[i] = constant_time_select_w(v, a[i], r[i]);
  272. a[i] = 0;
  273. }
  274. return 1;
  275. }
  276. static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r,
  277. const BN_MONT_CTX *mont) {
  278. if (r->neg) {
  279. OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
  280. return 0;
  281. }
  282. const BIGNUM *n = &mont->N;
  283. if (n->width == 0) {
  284. ret->width = 0;
  285. return 1;
  286. }
  287. int max = 2 * n->width; // carry is stored separately
  288. if (!bn_resize_words(r, max) ||
  289. !bn_wexpand(ret, n->width)) {
  290. return 0;
  291. }
  292. ret->width = n->width;
  293. ret->neg = 0;
  294. return bn_from_montgomery_in_place(ret->d, ret->width, r->d, r->width, mont);
  295. }
  296. int BN_from_montgomery(BIGNUM *r, const BIGNUM *a, const BN_MONT_CTX *mont,
  297. BN_CTX *ctx) {
  298. int ret = 0;
  299. BIGNUM *t;
  300. BN_CTX_start(ctx);
  301. t = BN_CTX_get(ctx);
  302. if (t == NULL ||
  303. !BN_copy(t, a)) {
  304. goto err;
  305. }
  306. ret = BN_from_montgomery_word(r, t, mont);
  307. err:
  308. BN_CTX_end(ctx);
  309. return ret;
  310. }
  311. int bn_one_to_montgomery(BIGNUM *r, const BN_MONT_CTX *mont, BN_CTX *ctx) {
  312. // If the high bit of |n| is set, R = 2^(width*BN_BITS2) < 2 * |n|, so we
  313. // compute R - |n| rather than perform Montgomery reduction.
  314. const BIGNUM *n = &mont->N;
  315. if (n->width > 0 && (n->d[n->width - 1] >> (BN_BITS2 - 1)) != 0) {
  316. if (!bn_wexpand(r, n->width)) {
  317. return 0;
  318. }
  319. r->d[0] = 0 - n->d[0];
  320. for (int i = 1; i < n->width; i++) {
  321. r->d[i] = ~n->d[i];
  322. }
  323. r->width = n->width;
  324. r->neg = 0;
  325. return 1;
  326. }
  327. return BN_from_montgomery(r, &mont->RR, mont, ctx);
  328. }
  329. static int bn_mod_mul_montgomery_fallback(BIGNUM *r, const BIGNUM *a,
  330. const BIGNUM *b,
  331. const BN_MONT_CTX *mont,
  332. BN_CTX *ctx) {
  333. int ret = 0;
  334. BN_CTX_start(ctx);
  335. BIGNUM *tmp = BN_CTX_get(ctx);
  336. if (tmp == NULL) {
  337. goto err;
  338. }
  339. if (a == b) {
  340. if (!bn_sqr_consttime(tmp, a, ctx)) {
  341. goto err;
  342. }
  343. } else {
  344. if (!bn_mul_consttime(tmp, a, b, ctx)) {
  345. goto err;
  346. }
  347. }
  348. // reduce from aRR to aR
  349. if (!BN_from_montgomery_word(r, tmp, mont)) {
  350. goto err;
  351. }
  352. ret = 1;
  353. err:
  354. BN_CTX_end(ctx);
  355. return ret;
  356. }
  357. int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  358. const BN_MONT_CTX *mont, BN_CTX *ctx) {
  359. if (a->neg || b->neg) {
  360. OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
  361. return 0;
  362. }
  363. #if defined(OPENSSL_BN_ASM_MONT)
  364. // |bn_mul_mont| requires at least 128 bits of limbs, at least for x86.
  365. int num = mont->N.width;
  366. if (num >= (128 / BN_BITS2) &&
  367. a->width == num &&
  368. b->width == num) {
  369. if (!bn_wexpand(r, num)) {
  370. return 0;
  371. }
  372. if (!bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {
  373. // The check above ensures this won't happen.
  374. assert(0);
  375. OPENSSL_PUT_ERROR(BN, ERR_R_INTERNAL_ERROR);
  376. return 0;
  377. }
  378. r->neg = 0;
  379. r->width = num;
  380. return 1;
  381. }
  382. #endif
  383. return bn_mod_mul_montgomery_fallback(r, a, b, mont, ctx);
  384. }
  385. int bn_less_than_montgomery_R(const BIGNUM *bn, const BN_MONT_CTX *mont) {
  386. return !BN_is_negative(bn) &&
  387. bn_fits_in_words(bn, mont->N.width);
  388. }
  389. int bn_to_montgomery_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a,
  390. size_t num_a, const BN_MONT_CTX *mont) {
  391. return bn_mod_mul_montgomery_small(r, num_r, a, num_a, mont->RR.d,
  392. mont->RR.width, mont);
  393. }
  394. int bn_from_montgomery_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a,
  395. size_t num_a, const BN_MONT_CTX *mont) {
  396. size_t num_n = mont->N.width;
  397. if (num_a > 2 * num_n || num_r != num_n || num_n > BN_SMALL_MAX_WORDS) {
  398. OPENSSL_PUT_ERROR(BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  399. return 0;
  400. }
  401. BN_ULONG tmp[BN_SMALL_MAX_WORDS * 2];
  402. size_t num_tmp = 2 * num_n;
  403. OPENSSL_memcpy(tmp, a, num_a * sizeof(BN_ULONG));
  404. OPENSSL_memset(tmp + num_a, 0, (num_tmp - num_a) * sizeof(BN_ULONG));
  405. int ret = bn_from_montgomery_in_place(r, num_r, tmp, num_tmp, mont);
  406. OPENSSL_cleanse(tmp, num_tmp * sizeof(BN_ULONG));
  407. return ret;
  408. }
  409. int bn_one_to_montgomery_small(BN_ULONG *r, size_t num_r,
  410. const BN_MONT_CTX *mont) {
  411. const BN_ULONG *n = mont->N.d;
  412. size_t num_n = mont->N.width;
  413. if (num_n == 0 || num_r != num_n) {
  414. OPENSSL_PUT_ERROR(BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  415. return 0;
  416. }
  417. // If the high bit of |n| is set, R = 2^(num_n*BN_BITS2) < 2 * |n|, so we
  418. // compute R - |n| rather than perform Montgomery reduction.
  419. if (num_n > 0 && (n[num_n - 1] >> (BN_BITS2 - 1)) != 0) {
  420. r[0] = 0 - n[0];
  421. for (size_t i = 1; i < num_n; i++) {
  422. r[i] = ~n[i];
  423. }
  424. return 1;
  425. }
  426. return bn_from_montgomery_small(r, num_r, mont->RR.d, mont->RR.width, mont);
  427. }
  428. int bn_mod_mul_montgomery_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a,
  429. size_t num_a, const BN_ULONG *b, size_t num_b,
  430. const BN_MONT_CTX *mont) {
  431. size_t num_n = mont->N.width;
  432. if (num_r != num_n || num_a + num_b > 2 * num_n ||
  433. num_n > BN_SMALL_MAX_WORDS) {
  434. OPENSSL_PUT_ERROR(BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  435. return 0;
  436. }
  437. #if defined(OPENSSL_BN_ASM_MONT)
  438. // |bn_mul_mont| requires at least 128 bits of limbs, at least for x86.
  439. if (num_n >= (128 / BN_BITS2) &&
  440. num_a == num_n &&
  441. num_b == num_n) {
  442. if (!bn_mul_mont(r, a, b, mont->N.d, mont->n0, num_n)) {
  443. assert(0); // The check above ensures this won't happen.
  444. OPENSSL_PUT_ERROR(BN, ERR_R_INTERNAL_ERROR);
  445. return 0;
  446. }
  447. return 1;
  448. }
  449. #endif
  450. // Compute the product.
  451. BN_ULONG tmp[2 * BN_SMALL_MAX_WORDS];
  452. size_t num_tmp = 2 * num_n;
  453. size_t num_ab = num_a + num_b;
  454. if (a == b && num_a == num_b) {
  455. if (!bn_sqr_small(tmp, num_ab, a, num_a)) {
  456. return 0;
  457. }
  458. } else if (!bn_mul_small(tmp, num_ab, a, num_a, b, num_b)) {
  459. return 0;
  460. }
  461. // Zero-extend to full width and reduce.
  462. OPENSSL_memset(tmp + num_ab, 0, (num_tmp - num_ab) * sizeof(BN_ULONG));
  463. int ret = bn_from_montgomery_in_place(r, num_r, tmp, num_tmp, mont);
  464. OPENSSL_cleanse(tmp, num_tmp * sizeof(BN_ULONG));
  465. return ret;
  466. }