montgomery.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 "internal.h"
  115. #include "../internal.h"
  116. #if !defined(OPENSSL_NO_ASM) && \
  117. (defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || \
  118. defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64))
  119. #define OPENSSL_BN_ASM_MONT
  120. #endif
  121. BN_MONT_CTX *BN_MONT_CTX_new(void) {
  122. BN_MONT_CTX *ret = OPENSSL_malloc(sizeof(BN_MONT_CTX));
  123. if (ret == NULL) {
  124. return NULL;
  125. }
  126. OPENSSL_memset(ret, 0, sizeof(BN_MONT_CTX));
  127. BN_init(&ret->RR);
  128. BN_init(&ret->N);
  129. return ret;
  130. }
  131. void BN_MONT_CTX_free(BN_MONT_CTX *mont) {
  132. if (mont == NULL) {
  133. return;
  134. }
  135. BN_free(&mont->RR);
  136. BN_free(&mont->N);
  137. OPENSSL_free(mont);
  138. }
  139. BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, const BN_MONT_CTX *from) {
  140. if (to == from) {
  141. return to;
  142. }
  143. if (!BN_copy(&to->RR, &from->RR) ||
  144. !BN_copy(&to->N, &from->N)) {
  145. return NULL;
  146. }
  147. to->n0[0] = from->n0[0];
  148. to->n0[1] = from->n0[1];
  149. return to;
  150. }
  151. OPENSSL_COMPILE_ASSERT(BN_MONT_CTX_N0_LIMBS == 1 || BN_MONT_CTX_N0_LIMBS == 2,
  152. BN_MONT_CTX_N0_LIMBS_VALUE_INVALID);
  153. OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) * BN_MONT_CTX_N0_LIMBS ==
  154. sizeof(uint64_t), BN_MONT_CTX_set_64_bit_mismatch);
  155. int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx) {
  156. if (BN_is_zero(mod)) {
  157. OPENSSL_PUT_ERROR(BN, BN_R_DIV_BY_ZERO);
  158. return 0;
  159. }
  160. if (!BN_is_odd(mod)) {
  161. OPENSSL_PUT_ERROR(BN, BN_R_CALLED_WITH_EVEN_MODULUS);
  162. return 0;
  163. }
  164. if (BN_is_negative(mod)) {
  165. OPENSSL_PUT_ERROR(BN, BN_R_NEGATIVE_NUMBER);
  166. return 0;
  167. }
  168. /* Save the modulus. */
  169. if (!BN_copy(&mont->N, mod)) {
  170. OPENSSL_PUT_ERROR(BN, ERR_R_INTERNAL_ERROR);
  171. return 0;
  172. }
  173. /* Find n0 such that n0 * N == -1 (mod r).
  174. *
  175. * Only certain BN_BITS2<=32 platforms actually make use of n0[1]. For the
  176. * others, we could use a shorter R value and use faster |BN_ULONG|-based
  177. * math instead of |uint64_t|-based math, which would be double-precision.
  178. * However, currently only the assembler files know which is which. */
  179. uint64_t n0 = bn_mont_n0(mod);
  180. mont->n0[0] = (BN_ULONG)n0;
  181. #if BN_MONT_CTX_N0_LIMBS == 2
  182. mont->n0[1] = (BN_ULONG)(n0 >> BN_BITS2);
  183. #else
  184. mont->n0[1] = 0;
  185. #endif
  186. /* Save RR = R**2 (mod N). R is the smallest power of 2**BN_BITS such that R
  187. * > mod. Even though the assembly on some 32-bit platforms works with 64-bit
  188. * values, using |BN_BITS2| here, rather than |BN_MONT_CTX_N0_LIMBS *
  189. * BN_BITS2|, is correct because R**2 will still be a multiple of the latter
  190. * as |BN_MONT_CTX_N0_LIMBS| is either one or two.
  191. *
  192. * XXX: This is not constant time with respect to |mont->N|, but it should
  193. * be. */
  194. unsigned lgBigR = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;
  195. if (!bn_mod_exp_base_2_vartime(&mont->RR, lgBigR * 2, &mont->N)) {
  196. return 0;
  197. }
  198. return 1;
  199. }
  200. int BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_MUTEX *lock,
  201. const BIGNUM *mod, BN_CTX *bn_ctx) {
  202. CRYPTO_MUTEX_lock_read(lock);
  203. BN_MONT_CTX *ctx = *pmont;
  204. CRYPTO_MUTEX_unlock_read(lock);
  205. if (ctx) {
  206. return 1;
  207. }
  208. CRYPTO_MUTEX_lock_write(lock);
  209. ctx = *pmont;
  210. if (ctx) {
  211. goto out;
  212. }
  213. ctx = BN_MONT_CTX_new();
  214. if (ctx == NULL) {
  215. goto out;
  216. }
  217. if (!BN_MONT_CTX_set(ctx, mod, bn_ctx)) {
  218. BN_MONT_CTX_free(ctx);
  219. ctx = NULL;
  220. goto out;
  221. }
  222. *pmont = ctx;
  223. out:
  224. CRYPTO_MUTEX_unlock_write(lock);
  225. return ctx != NULL;
  226. }
  227. int BN_to_montgomery(BIGNUM *ret, const BIGNUM *a, const BN_MONT_CTX *mont,
  228. BN_CTX *ctx) {
  229. return BN_mod_mul_montgomery(ret, a, &mont->RR, mont, ctx);
  230. }
  231. static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r,
  232. const BN_MONT_CTX *mont) {
  233. BN_ULONG *ap, *np, *rp, n0, v, carry;
  234. int nl, max, i;
  235. const BIGNUM *n = &mont->N;
  236. nl = n->top;
  237. if (nl == 0) {
  238. ret->top = 0;
  239. return 1;
  240. }
  241. max = (2 * nl); /* carry is stored separately */
  242. if (bn_wexpand(r, max) == NULL) {
  243. return 0;
  244. }
  245. r->neg ^= n->neg;
  246. np = n->d;
  247. rp = r->d;
  248. /* clear the top words of T */
  249. if (max > r->top) {
  250. OPENSSL_memset(&rp[r->top], 0, (max - r->top) * sizeof(BN_ULONG));
  251. }
  252. r->top = max;
  253. n0 = mont->n0[0];
  254. for (carry = 0, i = 0; i < nl; i++, rp++) {
  255. v = bn_mul_add_words(rp, np, nl, (rp[0] * n0) & BN_MASK2);
  256. v = (v + carry + rp[nl]) & BN_MASK2;
  257. carry |= (v != rp[nl]);
  258. carry &= (v <= rp[nl]);
  259. rp[nl] = v;
  260. }
  261. if (bn_wexpand(ret, nl) == NULL) {
  262. return 0;
  263. }
  264. ret->top = nl;
  265. ret->neg = r->neg;
  266. rp = ret->d;
  267. ap = &(r->d[nl]);
  268. {
  269. BN_ULONG *nrp;
  270. uintptr_t m;
  271. v = bn_sub_words(rp, ap, np, nl) - carry;
  272. /* if subtraction result is real, then trick unconditional memcpy below to
  273. * perform in-place "refresh" instead of actual copy. */
  274. m = (0u - (uintptr_t)v);
  275. nrp = (BN_ULONG *)(((uintptr_t)rp & ~m) | ((uintptr_t)ap & m));
  276. for (i = 0, nl -= 4; i < nl; i += 4) {
  277. BN_ULONG t1, t2, t3, t4;
  278. t1 = nrp[i + 0];
  279. t2 = nrp[i + 1];
  280. t3 = nrp[i + 2];
  281. ap[i + 0] = 0;
  282. t4 = nrp[i + 3];
  283. ap[i + 1] = 0;
  284. rp[i + 0] = t1;
  285. ap[i + 2] = 0;
  286. rp[i + 1] = t2;
  287. ap[i + 3] = 0;
  288. rp[i + 2] = t3;
  289. rp[i + 3] = t4;
  290. }
  291. for (nl += 4; i < nl; i++) {
  292. rp[i] = nrp[i], ap[i] = 0;
  293. }
  294. }
  295. bn_correct_top(r);
  296. bn_correct_top(ret);
  297. return 1;
  298. }
  299. int BN_from_montgomery(BIGNUM *r, const BIGNUM *a, const BN_MONT_CTX *mont,
  300. BN_CTX *ctx) {
  301. int ret = 0;
  302. BIGNUM *t;
  303. BN_CTX_start(ctx);
  304. t = BN_CTX_get(ctx);
  305. if (t == NULL ||
  306. !BN_copy(t, a)) {
  307. goto err;
  308. }
  309. ret = BN_from_montgomery_word(r, t, mont);
  310. err:
  311. BN_CTX_end(ctx);
  312. return ret;
  313. }
  314. int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  315. const BN_MONT_CTX *mont, BN_CTX *ctx) {
  316. BIGNUM *tmp;
  317. int ret = 0;
  318. #if defined(OPENSSL_BN_ASM_MONT)
  319. int num = mont->N.top;
  320. if (num > 1 && a->top == num && b->top == num) {
  321. if (bn_wexpand(r, num) == NULL) {
  322. return 0;
  323. }
  324. if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {
  325. r->neg = a->neg ^ b->neg;
  326. r->top = num;
  327. bn_correct_top(r);
  328. return 1;
  329. }
  330. }
  331. #endif
  332. BN_CTX_start(ctx);
  333. tmp = BN_CTX_get(ctx);
  334. if (tmp == NULL) {
  335. goto err;
  336. }
  337. if (a == b) {
  338. if (!BN_sqr(tmp, a, ctx)) {
  339. goto err;
  340. }
  341. } else {
  342. if (!BN_mul(tmp, a, b, ctx)) {
  343. goto err;
  344. }
  345. }
  346. /* reduce from aRR to aR */
  347. if (!BN_from_montgomery_word(r, tmp, mont)) {
  348. goto err;
  349. }
  350. ret = 1;
  351. err:
  352. BN_CTX_end(ctx);
  353. return ret;
  354. }