montgomery.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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 <string.h>
  110. #include <openssl/err.h>
  111. #include <openssl/mem.h>
  112. #include <openssl/thread.h>
  113. #include "internal.h"
  114. #include "../internal.h"
  115. #if !defined(OPENSSL_NO_ASM) && \
  116. (defined(OPENSSL_X86) || defined(OPENSSL_X86_64) || \
  117. defined(OPENSSL_ARM) || defined(OPENSSL_AARCH64))
  118. #define OPENSSL_BN_ASM_MONT
  119. #endif
  120. BN_MONT_CTX *BN_MONT_CTX_new(void) {
  121. BN_MONT_CTX *ret = OPENSSL_malloc(sizeof(BN_MONT_CTX));
  122. if (ret == NULL) {
  123. return NULL;
  124. }
  125. memset(ret, 0, sizeof(BN_MONT_CTX));
  126. BN_init(&ret->RR);
  127. BN_init(&ret->N);
  128. return ret;
  129. }
  130. void BN_MONT_CTX_free(BN_MONT_CTX *mont) {
  131. if (mont == NULL) {
  132. return;
  133. }
  134. BN_free(&mont->RR);
  135. BN_free(&mont->N);
  136. OPENSSL_free(mont);
  137. }
  138. BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to, const BN_MONT_CTX *from) {
  139. if (to == from) {
  140. return to;
  141. }
  142. if (!BN_copy(&to->RR, &from->RR) ||
  143. !BN_copy(&to->N, &from->N)) {
  144. return NULL;
  145. }
  146. to->n0[0] = from->n0[0];
  147. to->n0[1] = from->n0[1];
  148. return to;
  149. }
  150. int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx) {
  151. int ret = 0;
  152. BIGNUM *Ri, *R;
  153. BIGNUM tmod;
  154. BN_ULONG buf[2];
  155. if (BN_is_zero(mod)) {
  156. OPENSSL_PUT_ERROR(BN, BN_R_DIV_BY_ZERO);
  157. return 0;
  158. }
  159. BN_CTX_start(ctx);
  160. Ri = BN_CTX_get(ctx);
  161. if (Ri == NULL) {
  162. goto err;
  163. }
  164. R = &mont->RR; /* grab RR as a temp */
  165. if (!BN_copy(&mont->N, mod)) {
  166. goto err; /* Set N */
  167. }
  168. mont->N.neg = 0;
  169. BN_init(&tmod);
  170. tmod.d = buf;
  171. tmod.dmax = 2;
  172. tmod.neg = 0;
  173. #if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2 <= 32)
  174. /* Only certain BN_BITS2<=32 platforms actually make use of
  175. * n0[1], and we could use the #else case (with a shorter R
  176. * value) for the others. However, currently only the assembler
  177. * files do know which is which. */
  178. BN_zero(R);
  179. if (!BN_set_bit(R, 2 * BN_BITS2)) {
  180. goto err;
  181. }
  182. tmod.top = 0;
  183. if ((buf[0] = mod->d[0])) {
  184. tmod.top = 1;
  185. }
  186. if ((buf[1] = mod->top > 1 ? mod->d[1] : 0)) {
  187. tmod.top = 2;
  188. }
  189. if (BN_mod_inverse(Ri, R, &tmod, ctx) == NULL) {
  190. goto err;
  191. }
  192. if (!BN_lshift(Ri, Ri, 2 * BN_BITS2)) {
  193. goto err; /* R*Ri */
  194. }
  195. if (!BN_is_zero(Ri)) {
  196. if (!BN_sub_word(Ri, 1)) {
  197. goto err;
  198. }
  199. } else {
  200. /* if N mod word size == 1 */
  201. if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL) {
  202. goto err;
  203. }
  204. /* Ri-- (mod double word size) */
  205. Ri->neg = 0;
  206. Ri->d[0] = BN_MASK2;
  207. Ri->d[1] = BN_MASK2;
  208. Ri->top = 2;
  209. }
  210. if (!BN_div(Ri, NULL, Ri, &tmod, ctx)) {
  211. goto err;
  212. }
  213. /* Ni = (R*Ri-1)/N,
  214. * keep only couple of least significant words: */
  215. mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;
  216. mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;
  217. #else
  218. BN_zero(R);
  219. if (!BN_set_bit(R, BN_BITS2)) {
  220. goto err; /* R */
  221. }
  222. buf[0] = mod->d[0]; /* tmod = N mod word size */
  223. buf[1] = 0;
  224. tmod.top = buf[0] != 0 ? 1 : 0;
  225. /* Ri = R^-1 mod N*/
  226. if (BN_mod_inverse(Ri, R, &tmod, ctx) == NULL) {
  227. goto err;
  228. }
  229. if (!BN_lshift(Ri, Ri, BN_BITS2)) {
  230. goto err; /* R*Ri */
  231. }
  232. if (!BN_is_zero(Ri)) {
  233. if (!BN_sub_word(Ri, 1)) {
  234. goto err;
  235. }
  236. } else {
  237. /* if N mod word size == 1 */
  238. if (!BN_set_word(Ri, BN_MASK2)) {
  239. goto err; /* Ri-- (mod word size) */
  240. }
  241. }
  242. if (!BN_div(Ri, NULL, Ri, &tmod, ctx)) {
  243. goto err;
  244. }
  245. /* Ni = (R*Ri-1)/N,
  246. * keep only least significant word: */
  247. mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;
  248. mont->n0[1] = 0;
  249. #endif
  250. /* RR = (2^ri)^2 == 2^(ri*2) == 1 << (ri*2), which has its (ri*2)th bit set. */
  251. int ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;
  252. BN_zero(&(mont->RR));
  253. if (!BN_set_bit(&(mont->RR), ri * 2)) {
  254. goto err;
  255. }
  256. if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx)) {
  257. goto err;
  258. }
  259. ret = 1;
  260. err:
  261. BN_CTX_end(ctx);
  262. return ret;
  263. }
  264. BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, CRYPTO_MUTEX *lock,
  265. const BIGNUM *mod, BN_CTX *bn_ctx) {
  266. CRYPTO_MUTEX_lock_read(lock);
  267. BN_MONT_CTX *ctx = *pmont;
  268. CRYPTO_MUTEX_unlock(lock);
  269. if (ctx) {
  270. return ctx;
  271. }
  272. CRYPTO_MUTEX_lock_write(lock);
  273. ctx = *pmont;
  274. if (ctx) {
  275. goto out;
  276. }
  277. ctx = BN_MONT_CTX_new();
  278. if (ctx == NULL) {
  279. goto out;
  280. }
  281. if (!BN_MONT_CTX_set(ctx, mod, bn_ctx)) {
  282. BN_MONT_CTX_free(ctx);
  283. ctx = NULL;
  284. goto out;
  285. }
  286. *pmont = ctx;
  287. out:
  288. CRYPTO_MUTEX_unlock(lock);
  289. return ctx;
  290. }
  291. int BN_to_montgomery(BIGNUM *ret, const BIGNUM *a, const BN_MONT_CTX *mont,
  292. BN_CTX *ctx) {
  293. return BN_mod_mul_montgomery(ret, a, &mont->RR, mont, ctx);
  294. }
  295. #if 0
  296. static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r,
  297. const BN_MONT_CTX *mont) {
  298. const BIGNUM *n;
  299. BN_ULONG *ap, *np, *rp, n0, v, carry;
  300. int nl, max, i;
  301. n = &mont->N;
  302. nl = n->top;
  303. if (nl == 0) {
  304. ret->top = 0;
  305. return 1;
  306. }
  307. max = (2 * nl); /* carry is stored separately */
  308. if (bn_wexpand(r, max) == NULL) {
  309. return 0;
  310. }
  311. r->neg ^= n->neg;
  312. np = n->d;
  313. rp = r->d;
  314. /* clear the top words of T */
  315. if (max > r->top) {
  316. memset(&rp[r->top], 0, (max - r->top) * sizeof(BN_ULONG));
  317. }
  318. r->top = max;
  319. n0 = mont->n0[0];
  320. for (carry = 0, i = 0; i < nl; i++, rp++) {
  321. v = bn_mul_add_words(rp, np, nl, (rp[0] * n0) & BN_MASK2);
  322. v = (v + carry + rp[nl]) & BN_MASK2;
  323. carry |= (v != rp[nl]);
  324. carry &= (v <= rp[nl]);
  325. rp[nl] = v;
  326. }
  327. if (bn_wexpand(ret, nl) == NULL) {
  328. return 0;
  329. }
  330. ret->top = nl;
  331. ret->neg = r->neg;
  332. rp = ret->d;
  333. ap = &(r->d[nl]);
  334. {
  335. BN_ULONG *nrp;
  336. size_t m;
  337. v = bn_sub_words(rp, ap, np, nl) - carry;
  338. /* if subtraction result is real, then trick unconditional memcpy below to
  339. * perform in-place "refresh" instead of actual copy. */
  340. m = (0 - (size_t)v);
  341. nrp = (BN_ULONG *)(((intptr_t)rp & ~m) | ((intptr_t)ap & m));
  342. for (i = 0, nl -= 4; i < nl; i += 4) {
  343. BN_ULONG t1, t2, t3, t4;
  344. t1 = nrp[i + 0];
  345. t2 = nrp[i + 1];
  346. t3 = nrp[i + 2];
  347. ap[i + 0] = 0;
  348. t4 = nrp[i + 3];
  349. ap[i + 1] = 0;
  350. rp[i + 0] = t1;
  351. ap[i + 2] = 0;
  352. rp[i + 1] = t2;
  353. ap[i + 3] = 0;
  354. rp[i + 2] = t3;
  355. rp[i + 3] = t4;
  356. }
  357. for (nl += 4; i < nl; i++) {
  358. rp[i] = nrp[i], ap[i] = 0;
  359. }
  360. }
  361. bn_correct_top(r);
  362. bn_correct_top(ret);
  363. return 1;
  364. }
  365. #endif
  366. #define PTR_SIZE_INT size_t
  367. static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r, const BN_MONT_CTX *mont)
  368. {
  369. BN_ULONG *ap,*np,*rp,n0,v,carry;
  370. int nl,max,i;
  371. const BIGNUM *n = &mont->N;
  372. nl=n->top;
  373. if (nl == 0) { ret->top=0; return(1); }
  374. max=(2*nl); /* carry is stored separately */
  375. if (bn_wexpand(r,max) == NULL) return(0);
  376. r->neg^=n->neg;
  377. np=n->d;
  378. rp=r->d;
  379. /* clear the top words of T */
  380. #if 1
  381. for (i=r->top; i<max; i++) /* memset? XXX */
  382. rp[i]=0;
  383. #else
  384. memset(&(rp[r->top]),0,(max-r->top)*sizeof(BN_ULONG));
  385. #endif
  386. r->top=max;
  387. n0=mont->n0[0];
  388. for (carry=0, i=0; i<nl; i++, rp++)
  389. {
  390. v=bn_mul_add_words(rp,np,nl,(rp[0]*n0)&BN_MASK2);
  391. v = (v+carry+rp[nl])&BN_MASK2;
  392. carry |= (v != rp[nl]);
  393. carry &= (v <= rp[nl]);
  394. rp[nl]=v;
  395. }
  396. if (bn_wexpand(ret,nl) == NULL) return(0);
  397. ret->top=nl;
  398. ret->neg=r->neg;
  399. rp=ret->d;
  400. ap=&(r->d[nl]);
  401. {
  402. BN_ULONG *nrp;
  403. size_t m;
  404. v=bn_sub_words(rp,ap,np,nl)-carry;
  405. /* if subtraction result is real, then
  406. * trick unconditional memcpy below to perform in-place
  407. * "refresh" instead of actual copy. */
  408. m=(0-(size_t)v);
  409. nrp=(BN_ULONG *)(((PTR_SIZE_INT)rp&~m)|((PTR_SIZE_INT)ap&m));
  410. for (i=0,nl-=4; i<nl; i+=4)
  411. {
  412. BN_ULONG t1,t2,t3,t4;
  413. t1=nrp[i+0];
  414. t2=nrp[i+1];
  415. t3=nrp[i+2]; ap[i+0]=0;
  416. t4=nrp[i+3]; ap[i+1]=0;
  417. rp[i+0]=t1; ap[i+2]=0;
  418. rp[i+1]=t2; ap[i+3]=0;
  419. rp[i+2]=t3;
  420. rp[i+3]=t4;
  421. }
  422. for (nl+=4; i<nl; i++)
  423. rp[i]=nrp[i], ap[i]=0;
  424. }
  425. bn_correct_top(r);
  426. bn_correct_top(ret);
  427. return(1);
  428. }
  429. int BN_from_montgomery(BIGNUM *ret, const BIGNUM *a, const BN_MONT_CTX *mont,
  430. BN_CTX *ctx) {
  431. int retn = 0;
  432. BIGNUM *t;
  433. BN_CTX_start(ctx);
  434. t = BN_CTX_get(ctx);
  435. if (t == NULL) {
  436. return 0;
  437. }
  438. if (BN_copy(t, a)) {
  439. retn = BN_from_montgomery_word(ret, t, mont);
  440. }
  441. BN_CTX_end(ctx);
  442. return retn;
  443. }
  444. int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  445. const BN_MONT_CTX *mont, BN_CTX *ctx) {
  446. BIGNUM *tmp;
  447. int ret = 0;
  448. #if defined(OPENSSL_BN_ASM_MONT)
  449. int num = mont->N.top;
  450. if (num > 1 && a->top == num && b->top == num) {
  451. if (bn_wexpand(r, num) == NULL) {
  452. return 0;
  453. }
  454. if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {
  455. r->neg = a->neg ^ b->neg;
  456. r->top = num;
  457. bn_correct_top(r);
  458. return 1;
  459. }
  460. }
  461. #endif
  462. BN_CTX_start(ctx);
  463. tmp = BN_CTX_get(ctx);
  464. if (tmp == NULL) {
  465. goto err;
  466. }
  467. if (a == b) {
  468. if (!BN_sqr(tmp, a, ctx)) {
  469. goto err;
  470. }
  471. } else {
  472. if (!BN_mul(tmp, a, b, ctx)) {
  473. goto err;
  474. }
  475. }
  476. /* reduce from aRR to aR */
  477. if (!BN_from_montgomery_word(r, tmp, mont)) {
  478. goto err;
  479. }
  480. ret = 1;
  481. err:
  482. BN_CTX_end(ctx);
  483. return ret;
  484. }