2
0

bn_gcd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /* crypto/bn/bn_gcd.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* ====================================================================
  59. * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * openssl-core@openssl.org.
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * (eay@cryptsoft.com). This product includes software written by Tim
  108. * Hudson (tjh@cryptsoft.com).
  109. *
  110. */
  111. #include "cryptlib.h"
  112. #include "bn_lcl.h"
  113. static BIGNUM *euclid(BIGNUM *a, BIGNUM *b);
  114. int BN_gcd(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx)
  115. {
  116. BIGNUM *a,*b,*t;
  117. int ret=0;
  118. bn_check_top(in_a);
  119. bn_check_top(in_b);
  120. BN_CTX_start(ctx);
  121. a = BN_CTX_get(ctx);
  122. b = BN_CTX_get(ctx);
  123. if (a == NULL || b == NULL) goto err;
  124. if (BN_copy(a,in_a) == NULL) goto err;
  125. if (BN_copy(b,in_b) == NULL) goto err;
  126. a->neg = 0;
  127. b->neg = 0;
  128. if (BN_cmp(a,b) < 0) { t=a; a=b; b=t; }
  129. t=euclid(a,b);
  130. if (t == NULL) goto err;
  131. if (BN_copy(r,t) == NULL) goto err;
  132. ret=1;
  133. err:
  134. BN_CTX_end(ctx);
  135. bn_check_top(r);
  136. return(ret);
  137. }
  138. static BIGNUM *euclid(BIGNUM *a, BIGNUM *b)
  139. {
  140. BIGNUM *t;
  141. int shifts=0;
  142. bn_check_top(a);
  143. bn_check_top(b);
  144. /* 0 <= b <= a */
  145. while (!BN_is_zero(b))
  146. {
  147. /* 0 < b <= a */
  148. if (BN_is_odd(a))
  149. {
  150. if (BN_is_odd(b))
  151. {
  152. if (!BN_sub(a,a,b)) goto err;
  153. if (!BN_rshift1(a,a)) goto err;
  154. if (BN_cmp(a,b) < 0)
  155. { t=a; a=b; b=t; }
  156. }
  157. else /* a odd - b even */
  158. {
  159. if (!BN_rshift1(b,b)) goto err;
  160. if (BN_cmp(a,b) < 0)
  161. { t=a; a=b; b=t; }
  162. }
  163. }
  164. else /* a is even */
  165. {
  166. if (BN_is_odd(b))
  167. {
  168. if (!BN_rshift1(a,a)) goto err;
  169. if (BN_cmp(a,b) < 0)
  170. { t=a; a=b; b=t; }
  171. }
  172. else /* a even - b even */
  173. {
  174. if (!BN_rshift1(a,a)) goto err;
  175. if (!BN_rshift1(b,b)) goto err;
  176. shifts++;
  177. }
  178. }
  179. /* 0 <= b <= a */
  180. }
  181. if (shifts)
  182. {
  183. if (!BN_lshift(a,a,shifts)) goto err;
  184. }
  185. bn_check_top(a);
  186. return(a);
  187. err:
  188. return(NULL);
  189. }
  190. /* solves ax == 1 (mod n) */
  191. BIGNUM *BN_mod_inverse(BIGNUM *in,
  192. const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)
  193. {
  194. BIGNUM *A,*B,*X,*Y,*M,*D,*T,*R=NULL;
  195. BIGNUM *ret=NULL;
  196. int sign;
  197. bn_check_top(a);
  198. bn_check_top(n);
  199. BN_CTX_start(ctx);
  200. A = BN_CTX_get(ctx);
  201. B = BN_CTX_get(ctx);
  202. X = BN_CTX_get(ctx);
  203. D = BN_CTX_get(ctx);
  204. M = BN_CTX_get(ctx);
  205. Y = BN_CTX_get(ctx);
  206. T = BN_CTX_get(ctx);
  207. if (T == NULL) goto err;
  208. if (in == NULL)
  209. R=BN_new();
  210. else
  211. R=in;
  212. if (R == NULL) goto err;
  213. BN_one(X);
  214. BN_zero(Y);
  215. if (BN_copy(B,a) == NULL) goto err;
  216. if (BN_copy(A,n) == NULL) goto err;
  217. A->neg = 0;
  218. if (B->neg || (BN_ucmp(B, A) >= 0))
  219. {
  220. if (!BN_nnmod(B, B, A, ctx)) goto err;
  221. }
  222. sign = -1;
  223. /* From B = a mod |n|, A = |n| it follows that
  224. *
  225. * 0 <= B < A,
  226. * -sign*X*a == B (mod |n|),
  227. * sign*Y*a == A (mod |n|).
  228. */
  229. if (BN_is_odd(n) && (BN_num_bits(n) <= (BN_BITS <= 32 ? 450 : 2048)))
  230. {
  231. /* Binary inversion algorithm; requires odd modulus.
  232. * This is faster than the general algorithm if the modulus
  233. * is sufficiently small (about 400 .. 500 bits on 32-bit
  234. * sytems, but much more on 64-bit systems) */
  235. int shift;
  236. while (!BN_is_zero(B))
  237. {
  238. /*
  239. * 0 < B < |n|,
  240. * 0 < A <= |n|,
  241. * (1) -sign*X*a == B (mod |n|),
  242. * (2) sign*Y*a == A (mod |n|)
  243. */
  244. /* Now divide B by the maximum possible power of two in the integers,
  245. * and divide X by the same value mod |n|.
  246. * When we're done, (1) still holds. */
  247. shift = 0;
  248. while (!BN_is_bit_set(B, shift)) /* note that 0 < B */
  249. {
  250. shift++;
  251. if (BN_is_odd(X))
  252. {
  253. if (!BN_uadd(X, X, n)) goto err;
  254. }
  255. /* now X is even, so we can easily divide it by two */
  256. if (!BN_rshift1(X, X)) goto err;
  257. }
  258. if (shift > 0)
  259. {
  260. if (!BN_rshift(B, B, shift)) goto err;
  261. }
  262. /* Same for A and Y. Afterwards, (2) still holds. */
  263. shift = 0;
  264. while (!BN_is_bit_set(A, shift)) /* note that 0 < A */
  265. {
  266. shift++;
  267. if (BN_is_odd(Y))
  268. {
  269. if (!BN_uadd(Y, Y, n)) goto err;
  270. }
  271. /* now Y is even */
  272. if (!BN_rshift1(Y, Y)) goto err;
  273. }
  274. if (shift > 0)
  275. {
  276. if (!BN_rshift(A, A, shift)) goto err;
  277. }
  278. /* We still have (1) and (2).
  279. * Both A and B are odd.
  280. * The following computations ensure that
  281. *
  282. * 0 <= B < |n|,
  283. * 0 < A < |n|,
  284. * (1) -sign*X*a == B (mod |n|),
  285. * (2) sign*Y*a == A (mod |n|),
  286. *
  287. * and that either A or B is even in the next iteration.
  288. */
  289. if (BN_ucmp(B, A) >= 0)
  290. {
  291. /* -sign*(X + Y)*a == B - A (mod |n|) */
  292. if (!BN_uadd(X, X, Y)) goto err;
  293. /* NB: we could use BN_mod_add_quick(X, X, Y, n), but that
  294. * actually makes the algorithm slower */
  295. if (!BN_usub(B, B, A)) goto err;
  296. }
  297. else
  298. {
  299. /* sign*(X + Y)*a == A - B (mod |n|) */
  300. if (!BN_uadd(Y, Y, X)) goto err;
  301. /* as above, BN_mod_add_quick(Y, Y, X, n) would slow things down */
  302. if (!BN_usub(A, A, B)) goto err;
  303. }
  304. }
  305. }
  306. else
  307. {
  308. /* general inversion algorithm */
  309. while (!BN_is_zero(B))
  310. {
  311. BIGNUM *tmp;
  312. /*
  313. * 0 < B < A,
  314. * (*) -sign*X*a == B (mod |n|),
  315. * sign*Y*a == A (mod |n|)
  316. */
  317. /* (D, M) := (A/B, A%B) ... */
  318. if (BN_num_bits(A) == BN_num_bits(B))
  319. {
  320. if (!BN_one(D)) goto err;
  321. if (!BN_sub(M,A,B)) goto err;
  322. }
  323. else if (BN_num_bits(A) == BN_num_bits(B) + 1)
  324. {
  325. /* A/B is 1, 2, or 3 */
  326. if (!BN_lshift1(T,B)) goto err;
  327. if (BN_ucmp(A,T) < 0)
  328. {
  329. /* A < 2*B, so D=1 */
  330. if (!BN_one(D)) goto err;
  331. if (!BN_sub(M,A,B)) goto err;
  332. }
  333. else
  334. {
  335. /* A >= 2*B, so D=2 or D=3 */
  336. if (!BN_sub(M,A,T)) goto err;
  337. if (!BN_add(D,T,B)) goto err; /* use D (:= 3*B) as temp */
  338. if (BN_ucmp(A,D) < 0)
  339. {
  340. /* A < 3*B, so D=2 */
  341. if (!BN_set_word(D,2)) goto err;
  342. /* M (= A - 2*B) already has the correct value */
  343. }
  344. else
  345. {
  346. /* only D=3 remains */
  347. if (!BN_set_word(D,3)) goto err;
  348. /* currently M = A - 2*B, but we need M = A - 3*B */
  349. if (!BN_sub(M,M,B)) goto err;
  350. }
  351. }
  352. }
  353. else
  354. {
  355. if (!BN_div(D,M,A,B,ctx)) goto err;
  356. }
  357. /* Now
  358. * A = D*B + M;
  359. * thus we have
  360. * (**) sign*Y*a == D*B + M (mod |n|).
  361. */
  362. tmp=A; /* keep the BIGNUM object, the value does not matter */
  363. /* (A, B) := (B, A mod B) ... */
  364. A=B;
  365. B=M;
  366. /* ... so we have 0 <= B < A again */
  367. /* Since the former M is now B and the former B is now A,
  368. * (**) translates into
  369. * sign*Y*a == D*A + B (mod |n|),
  370. * i.e.
  371. * sign*Y*a - D*A == B (mod |n|).
  372. * Similarly, (*) translates into
  373. * -sign*X*a == A (mod |n|).
  374. *
  375. * Thus,
  376. * sign*Y*a + D*sign*X*a == B (mod |n|),
  377. * i.e.
  378. * sign*(Y + D*X)*a == B (mod |n|).
  379. *
  380. * So if we set (X, Y, sign) := (Y + D*X, X, -sign), we arrive back at
  381. * -sign*X*a == B (mod |n|),
  382. * sign*Y*a == A (mod |n|).
  383. * Note that X and Y stay non-negative all the time.
  384. */
  385. /* most of the time D is very small, so we can optimize tmp := D*X+Y */
  386. if (BN_is_one(D))
  387. {
  388. if (!BN_add(tmp,X,Y)) goto err;
  389. }
  390. else
  391. {
  392. if (BN_is_word(D,2))
  393. {
  394. if (!BN_lshift1(tmp,X)) goto err;
  395. }
  396. else if (BN_is_word(D,4))
  397. {
  398. if (!BN_lshift(tmp,X,2)) goto err;
  399. }
  400. else if (D->top == 1)
  401. {
  402. if (!BN_copy(tmp,X)) goto err;
  403. if (!BN_mul_word(tmp,D->d[0])) goto err;
  404. }
  405. else
  406. {
  407. if (!BN_mul(tmp,D,X,ctx)) goto err;
  408. }
  409. if (!BN_add(tmp,tmp,Y)) goto err;
  410. }
  411. M=Y; /* keep the BIGNUM object, the value does not matter */
  412. Y=X;
  413. X=tmp;
  414. sign = -sign;
  415. }
  416. }
  417. /*
  418. * The while loop (Euclid's algorithm) ends when
  419. * A == gcd(a,n);
  420. * we have
  421. * sign*Y*a == A (mod |n|),
  422. * where Y is non-negative.
  423. */
  424. if (sign < 0)
  425. {
  426. if (!BN_sub(Y,n,Y)) goto err;
  427. }
  428. /* Now Y*a == A (mod |n|). */
  429. if (BN_is_one(A))
  430. {
  431. /* Y*a == 1 (mod |n|) */
  432. if (!Y->neg && BN_ucmp(Y,n) < 0)
  433. {
  434. if (!BN_copy(R,Y)) goto err;
  435. }
  436. else
  437. {
  438. if (!BN_nnmod(R,Y,n,ctx)) goto err;
  439. }
  440. }
  441. else
  442. {
  443. BNerr(BN_F_BN_MOD_INVERSE,BN_R_NO_INVERSE);
  444. goto err;
  445. }
  446. ret=R;
  447. err:
  448. if ((ret == NULL) && (in == NULL)) BN_free(R);
  449. BN_CTX_end(ctx);
  450. bn_check_top(ret);
  451. return(ret);
  452. }