Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

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