montgomery.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. static int bn_mod_mul_montgomery_fallback(BIGNUM *r, const BIGNUM *a,
  123. const BIGNUM *b,
  124. const BN_MONT_CTX *mont, BN_CTX *ctx);
  125. BN_MONT_CTX *BN_MONT_CTX_new(void) {
  126. BN_MONT_CTX *ret = OPENSSL_malloc(sizeof(BN_MONT_CTX));
  127. if (ret == NULL) {
  128. return NULL;
  129. }
  130. OPENSSL_memset(ret, 0, sizeof(BN_MONT_CTX));
  131. BN_init(&ret->RR);
  132. BN_init(&ret->N);
  133. return ret;
  134. }
  135. void BN_MONT_CTX_free(BN_MONT_CTX *mont) {
  136. if (mont == NULL) {
  137. return;
  138. }
  139. BN_free(&mont->RR);
  140. BN_free(&mont->N);
  141. OPENSSL_free(mont);
  142. }
  143. BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, const BN_MONT_CTX *from) {
  144. if (to == from) {
  145. return to;
  146. }
  147. if (!BN_copy(&to->RR, &from->RR) ||
  148. !BN_copy(&to->N, &from->N)) {
  149. return NULL;
  150. }
  151. to->n0[0] = from->n0[0];
  152. to->n0[1] = from->n0[1];
  153. return to;
  154. }
  155. OPENSSL_COMPILE_ASSERT(BN_MONT_CTX_N0_LIMBS == 1 || BN_MONT_CTX_N0_LIMBS == 2,
  156. BN_MONT_CTX_N0_LIMBS_VALUE_INVALID);
  157. OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) * BN_MONT_CTX_N0_LIMBS ==
  158. sizeof(uint64_t), BN_MONT_CTX_set_64_bit_mismatch);
  159. int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx) {
  160. if (BN_is_zero(mod)) {
  161. OPENSSL_PUT_ERROR(BN, BN_R_DIV_BY_ZERO);
  162. return 0;
  163. }
  164. if (!BN_is_odd(mod)) {
  165. OPENSSL_PUT_ERROR(BN, BN_R_CALLED_WITH_EVEN_MODULUS);
  166. return 0;
  167. }
  168. if (BN_is_negative(mod)) {
  169. OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
  170. return 0;
  171. }
  172. // Save the modulus.
  173. if (!BN_copy(&mont->N, mod)) {
  174. OPENSSL_PUT_ERROR(BN, ERR_R_INTERNAL_ERROR);
  175. return 0;
  176. }
  177. // Find n0 such that n0 * N == -1 (mod r).
  178. //
  179. // Only certain BN_BITS2<=32 platforms actually make use of n0[1]. For the
  180. // others, we could use a shorter R value and use faster |BN_ULONG|-based
  181. // math instead of |uint64_t|-based math, which would be double-precision.
  182. // However, currently only the assembler files know which is which.
  183. uint64_t n0 = bn_mont_n0(mod);
  184. mont->n0[0] = (BN_ULONG)n0;
  185. #if BN_MONT_CTX_N0_LIMBS == 2
  186. mont->n0[1] = (BN_ULONG)(n0 >> BN_BITS2);
  187. #else
  188. mont->n0[1] = 0;
  189. #endif
  190. // Save RR = R**2 (mod N). R is the smallest power of 2**BN_BITS2 such that R
  191. // > mod. Even though the assembly on some 32-bit platforms works with 64-bit
  192. // values, using |BN_BITS2| here, rather than |BN_MONT_CTX_N0_LIMBS *
  193. // BN_BITS2|, is correct because R**2 will still be a multiple of the latter
  194. // as |BN_MONT_CTX_N0_LIMBS| is either one or two.
  195. //
  196. // XXX: This is not constant time with respect to |mont->N|, but it should be.
  197. unsigned lgBigR = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;
  198. if (!bn_mod_exp_base_2_vartime(&mont->RR, lgBigR * 2, &mont->N)) {
  199. return 0;
  200. }
  201. return 1;
  202. }
  203. int BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_MUTEX *lock,
  204. const BIGNUM *mod, BN_CTX *bn_ctx) {
  205. CRYPTO_MUTEX_lock_read(lock);
  206. BN_MONT_CTX *ctx = *pmont;
  207. CRYPTO_MUTEX_unlock_read(lock);
  208. if (ctx) {
  209. return 1;
  210. }
  211. CRYPTO_MUTEX_lock_write(lock);
  212. ctx = *pmont;
  213. if (ctx) {
  214. goto out;
  215. }
  216. ctx = BN_MONT_CTX_new();
  217. if (ctx == NULL) {
  218. goto out;
  219. }
  220. if (!BN_MONT_CTX_set(ctx, mod, bn_ctx)) {
  221. BN_MONT_CTX_free(ctx);
  222. ctx = NULL;
  223. goto out;
  224. }
  225. *pmont = ctx;
  226. out:
  227. CRYPTO_MUTEX_unlock_write(lock);
  228. return ctx != NULL;
  229. }
  230. int BN_to_montgomery(BIGNUM *ret, const BIGNUM *a, const BN_MONT_CTX *mont,
  231. BN_CTX *ctx) {
  232. return BN_mod_mul_montgomery(ret, a, &mont->RR, mont, ctx);
  233. }
  234. static int bn_from_montgomery_in_place(BN_ULONG *r, size_t num_r, BN_ULONG *a,
  235. size_t num_a, const BN_MONT_CTX *mont) {
  236. const BN_ULONG *n = mont->N.d;
  237. size_t num_n = mont->N.top;
  238. if (num_r != num_n || num_a != 2 * num_n) {
  239. OPENSSL_PUT_ERROR(BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  240. return 0;
  241. }
  242. // Add multiples of |n| to |r| until R = 2^(nl * BN_BITS2) divides it. On
  243. // input, we had |r| < |n| * R, so now |r| < 2 * |n| * R. Note that |r|
  244. // includes |carry| which is stored separately.
  245. BN_ULONG n0 = mont->n0[0];
  246. BN_ULONG carry = 0;
  247. for (size_t i = 0; i < num_n; i++) {
  248. BN_ULONG v = bn_mul_add_words(a + i, n, num_n, a[i] * n0);
  249. v += carry + a[i + num_n];
  250. carry |= (v != a[i + num_n]);
  251. carry &= (v <= a[i + num_n]);
  252. a[i + num_n] = v;
  253. }
  254. // Shift |num_n| words to divide by R. We have |a| < 2 * |n|. Note that |a|
  255. // includes |carry| which is stored separately.
  256. a += num_n;
  257. // |a| thus requires at most one additional subtraction |n| to be reduced.
  258. // Subtract |n| and select the answer in constant time.
  259. OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
  260. crypto_word_t_too_small);
  261. BN_ULONG v = bn_sub_words(r, a, n, num_n) - carry;
  262. // |v| is one if |a| - |n| underflowed or zero if it did not. Note |v| cannot
  263. // be -1. That would imply the subtraction did not fit in |num_n| words, and
  264. // we know at most one subtraction is needed.
  265. v = 0u - v;
  266. for (size_t i = 0; i < num_n; i++) {
  267. r[i] = constant_time_select_w(v, a[i], r[i]);
  268. a[i] = 0;
  269. }
  270. return 1;
  271. }
  272. static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r,
  273. const BN_MONT_CTX *mont) {
  274. const BIGNUM *n = &mont->N;
  275. if (n->top == 0) {
  276. ret->top = 0;
  277. return 1;
  278. }
  279. int max = (2 * n->top); // carry is stored separately
  280. if (!bn_wexpand(r, max) ||
  281. !bn_wexpand(ret, n->top)) {
  282. return 0;
  283. }
  284. // Clear the top words of |r|.
  285. if (max > r->top) {
  286. OPENSSL_memset(r->d + r->top, 0, (max - r->top) * sizeof(BN_ULONG));
  287. }
  288. r->top = max;
  289. ret->top = n->top;
  290. if (!bn_from_montgomery_in_place(ret->d, ret->top, r->d, r->top, mont)) {
  291. return 0;
  292. }
  293. ret->neg = r->neg;
  294. bn_correct_top(r);
  295. bn_correct_top(ret);
  296. return 1;
  297. }
  298. int BN_from_montgomery(BIGNUM *r, const BIGNUM *a, const BN_MONT_CTX *mont,
  299. BN_CTX *ctx) {
  300. int ret = 0;
  301. BIGNUM *t;
  302. BN_CTX_start(ctx);
  303. t = BN_CTX_get(ctx);
  304. if (t == NULL ||
  305. !BN_copy(t, a)) {
  306. goto err;
  307. }
  308. ret = BN_from_montgomery_word(r, t, mont);
  309. err:
  310. BN_CTX_end(ctx);
  311. return ret;
  312. }
  313. int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  314. const BN_MONT_CTX *mont, BN_CTX *ctx) {
  315. #if !defined(OPENSSL_BN_ASM_MONT)
  316. return bn_mod_mul_montgomery_fallback(r, a, b, mont, ctx);
  317. #else
  318. int num = mont->N.top;
  319. // |bn_mul_mont| requires at least 128 bits of limbs, at least for x86.
  320. if (num < (128 / BN_BITS2) ||
  321. a->top != num ||
  322. b->top != num) {
  323. return bn_mod_mul_montgomery_fallback(r, a, b, mont, ctx);
  324. }
  325. if (!bn_wexpand(r, num)) {
  326. return 0;
  327. }
  328. if (!bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {
  329. // The check above ensures this won't happen.
  330. assert(0);
  331. OPENSSL_PUT_ERROR(BN, ERR_R_INTERNAL_ERROR);
  332. return 0;
  333. }
  334. r->neg = a->neg ^ b->neg;
  335. r->top = num;
  336. bn_correct_top(r);
  337. return 1;
  338. #endif
  339. }
  340. static int bn_mod_mul_montgomery_fallback(BIGNUM *r, const BIGNUM *a,
  341. const BIGNUM *b,
  342. const BN_MONT_CTX *mont,
  343. BN_CTX *ctx) {
  344. int ret = 0;
  345. BN_CTX_start(ctx);
  346. BIGNUM *tmp = BN_CTX_get(ctx);
  347. if (tmp == NULL) {
  348. goto err;
  349. }
  350. if (a == b) {
  351. if (!BN_sqr(tmp, a, ctx)) {
  352. goto err;
  353. }
  354. } else {
  355. if (!BN_mul(tmp, a, b, ctx)) {
  356. goto err;
  357. }
  358. }
  359. // reduce from aRR to aR
  360. if (!BN_from_montgomery_word(r, tmp, mont)) {
  361. goto err;
  362. }
  363. ret = 1;
  364. err:
  365. BN_CTX_end(ctx);
  366. return ret;
  367. }
  368. int bn_to_montgomery_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a,
  369. size_t num_a, const BN_MONT_CTX *mont) {
  370. return bn_mod_mul_montgomery_small(r, num_r, a, num_a, mont->RR.d,
  371. mont->RR.top, mont);
  372. }
  373. int bn_from_montgomery_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a,
  374. size_t num_a, const BN_MONT_CTX *mont) {
  375. size_t num_n = mont->N.top;
  376. if (num_a > 2 * num_n || num_r != num_n || num_n > BN_SMALL_MAX_WORDS) {
  377. OPENSSL_PUT_ERROR(BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  378. return 0;
  379. }
  380. BN_ULONG tmp[BN_SMALL_MAX_WORDS * 2];
  381. size_t num_tmp = 2 * num_n;
  382. OPENSSL_memcpy(tmp, a, num_a * sizeof(BN_ULONG));
  383. OPENSSL_memset(tmp + num_a, 0, (num_tmp - num_a) * sizeof(BN_ULONG));
  384. int ret = bn_from_montgomery_in_place(r, num_r, tmp, num_tmp, mont);
  385. OPENSSL_cleanse(tmp, num_tmp * sizeof(BN_ULONG));
  386. return ret;
  387. }
  388. int bn_mod_mul_montgomery_small(BN_ULONG *r, size_t num_r, const BN_ULONG *a,
  389. size_t num_a, const BN_ULONG *b, size_t num_b,
  390. const BN_MONT_CTX *mont) {
  391. size_t num_n = mont->N.top;
  392. if (num_r != num_n || num_a + num_b > 2 * num_n ||
  393. num_n > BN_SMALL_MAX_WORDS) {
  394. OPENSSL_PUT_ERROR(BN, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  395. return 0;
  396. }
  397. #if defined(OPENSSL_BN_ASM_MONT)
  398. // |bn_mul_mont| requires at least 128 bits of limbs, at least for x86.
  399. if (num_n >= (128 / BN_BITS2) &&
  400. num_a == num_n &&
  401. num_b == num_n) {
  402. if (!bn_mul_mont(r, a, b, mont->N.d, mont->n0, num_n)) {
  403. assert(0); // The check above ensures this won't happen.
  404. OPENSSL_PUT_ERROR(BN, ERR_R_INTERNAL_ERROR);
  405. return 0;
  406. }
  407. return 1;
  408. }
  409. #endif
  410. // Compute the product.
  411. BN_ULONG tmp[2 * BN_SMALL_MAX_WORDS];
  412. size_t num_tmp = 2 * num_n;
  413. size_t num_ab = num_a + num_b;
  414. if (a == b && num_a == num_b) {
  415. if (!bn_sqr_small(tmp, num_ab, a, num_a)) {
  416. return 0;
  417. }
  418. } else if (!bn_mul_small(tmp, num_ab, a, num_a, b, num_b)) {
  419. return 0;
  420. }
  421. // Zero-extend to full width and reduce.
  422. OPENSSL_memset(tmp + num_ab, 0, (num_tmp - num_ab) * sizeof(BN_ULONG));
  423. int ret = bn_from_montgomery_in_place(r, num_r, tmp, num_tmp, mont);
  424. OPENSSL_cleanse(tmp, num_tmp * sizeof(BN_ULONG));
  425. return ret;
  426. }