bn_gcd.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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. #define OPENSSL_FIPSAPI
  112. #include "cryptlib.h"
  113. #include "bn_lcl.h"
  114. static BIGNUM *euclid(BIGNUM *a, BIGNUM *b);
  115. int BN_gcd(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx)
  116. {
  117. BIGNUM *a,*b,*t;
  118. int ret=0;
  119. bn_check_top(in_a);
  120. bn_check_top(in_b);
  121. BN_CTX_start(ctx);
  122. a = BN_CTX_get(ctx);
  123. b = BN_CTX_get(ctx);
  124. if (a == NULL || b == NULL) goto err;
  125. if (BN_copy(a,in_a) == NULL) goto err;
  126. if (BN_copy(b,in_b) == NULL) goto err;
  127. a->neg = 0;
  128. b->neg = 0;
  129. if (BN_cmp(a,b) < 0) { t=a; a=b; b=t; }
  130. t=euclid(a,b);
  131. if (t == NULL) goto err;
  132. if (BN_copy(r,t) == NULL) goto err;
  133. ret=1;
  134. err:
  135. BN_CTX_end(ctx);
  136. bn_check_top(r);
  137. return(ret);
  138. }
  139. static BIGNUM *euclid(BIGNUM *a, BIGNUM *b)
  140. {
  141. BIGNUM *t;
  142. int shifts=0;
  143. bn_check_top(a);
  144. bn_check_top(b);
  145. /* 0 <= b <= a */
  146. while (!BN_is_zero(b))
  147. {
  148. /* 0 < b <= a */
  149. if (BN_is_odd(a))
  150. {
  151. if (BN_is_odd(b))
  152. {
  153. if (!BN_sub(a,a,b)) goto err;
  154. if (!BN_rshift1(a,a)) goto err;
  155. if (BN_cmp(a,b) < 0)
  156. { t=a; a=b; b=t; }
  157. }
  158. else /* a odd - b even */
  159. {
  160. if (!BN_rshift1(b,b)) goto err;
  161. if (BN_cmp(a,b) < 0)
  162. { t=a; a=b; b=t; }
  163. }
  164. }
  165. else /* a is even */
  166. {
  167. if (BN_is_odd(b))
  168. {
  169. if (!BN_rshift1(a,a)) goto err;
  170. if (BN_cmp(a,b) < 0)
  171. { t=a; a=b; b=t; }
  172. }
  173. else /* a even - b even */
  174. {
  175. if (!BN_rshift1(a,a)) goto err;
  176. if (!BN_rshift1(b,b)) goto err;
  177. shifts++;
  178. }
  179. }
  180. /* 0 <= b <= a */
  181. }
  182. if (shifts)
  183. {
  184. if (!BN_lshift(a,a,shifts)) goto err;
  185. }
  186. bn_check_top(a);
  187. return(a);
  188. err:
  189. return(NULL);
  190. }
  191. /* solves ax == 1 (mod n) */
  192. static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,
  193. const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx);
  194. BIGNUM *BN_mod_inverse(BIGNUM *in,
  195. const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)
  196. {
  197. BIGNUM *rv;
  198. int noinv;
  199. rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);
  200. if (noinv)
  201. BNerr(BN_F_BN_MOD_INVERSE,BN_R_NO_INVERSE);
  202. return rv;
  203. }
  204. BIGNUM *int_bn_mod_inverse(BIGNUM *in,
  205. const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx, int *pnoinv)
  206. {
  207. BIGNUM *A,*B,*X,*Y,*M,*D,*T,*R=NULL;
  208. BIGNUM *ret=NULL;
  209. int sign;
  210. if (pnoinv)
  211. *pnoinv = 0;
  212. if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0))
  213. {
  214. return BN_mod_inverse_no_branch(in, a, n, ctx);
  215. }
  216. bn_check_top(a);
  217. bn_check_top(n);
  218. BN_CTX_start(ctx);
  219. A = BN_CTX_get(ctx);
  220. B = BN_CTX_get(ctx);
  221. X = BN_CTX_get(ctx);
  222. D = BN_CTX_get(ctx);
  223. M = BN_CTX_get(ctx);
  224. Y = BN_CTX_get(ctx);
  225. T = BN_CTX_get(ctx);
  226. if (T == NULL) goto err;
  227. if (in == NULL)
  228. R=BN_new();
  229. else
  230. R=in;
  231. if (R == NULL) goto err;
  232. BN_one(X);
  233. BN_zero(Y);
  234. if (BN_copy(B,a) == NULL) goto err;
  235. if (BN_copy(A,n) == NULL) goto err;
  236. A->neg = 0;
  237. if (B->neg || (BN_ucmp(B, A) >= 0))
  238. {
  239. if (!BN_nnmod(B, B, A, ctx)) goto err;
  240. }
  241. sign = -1;
  242. /* From B = a mod |n|, A = |n| it follows that
  243. *
  244. * 0 <= B < A,
  245. * -sign*X*a == B (mod |n|),
  246. * sign*Y*a == A (mod |n|).
  247. */
  248. if (BN_is_odd(n) && (BN_num_bits(n) <= (BN_BITS <= 32 ? 450 : 2048)))
  249. {
  250. /* Binary inversion algorithm; requires odd modulus.
  251. * This is faster than the general algorithm if the modulus
  252. * is sufficiently small (about 400 .. 500 bits on 32-bit
  253. * sytems, but much more on 64-bit systems) */
  254. int shift;
  255. while (!BN_is_zero(B))
  256. {
  257. /*
  258. * 0 < B < |n|,
  259. * 0 < A <= |n|,
  260. * (1) -sign*X*a == B (mod |n|),
  261. * (2) sign*Y*a == A (mod |n|)
  262. */
  263. /* Now divide B by the maximum possible power of two in the integers,
  264. * and divide X by the same value mod |n|.
  265. * When we're done, (1) still holds. */
  266. shift = 0;
  267. while (!BN_is_bit_set(B, shift)) /* note that 0 < B */
  268. {
  269. shift++;
  270. if (BN_is_odd(X))
  271. {
  272. if (!BN_uadd(X, X, n)) goto err;
  273. }
  274. /* now X is even, so we can easily divide it by two */
  275. if (!BN_rshift1(X, X)) goto err;
  276. }
  277. if (shift > 0)
  278. {
  279. if (!BN_rshift(B, B, shift)) goto err;
  280. }
  281. /* Same for A and Y. Afterwards, (2) still holds. */
  282. shift = 0;
  283. while (!BN_is_bit_set(A, shift)) /* note that 0 < A */
  284. {
  285. shift++;
  286. if (BN_is_odd(Y))
  287. {
  288. if (!BN_uadd(Y, Y, n)) goto err;
  289. }
  290. /* now Y is even */
  291. if (!BN_rshift1(Y, Y)) goto err;
  292. }
  293. if (shift > 0)
  294. {
  295. if (!BN_rshift(A, A, shift)) goto err;
  296. }
  297. /* We still have (1) and (2).
  298. * Both A and B are odd.
  299. * The following computations ensure that
  300. *
  301. * 0 <= B < |n|,
  302. * 0 < A < |n|,
  303. * (1) -sign*X*a == B (mod |n|),
  304. * (2) sign*Y*a == A (mod |n|),
  305. *
  306. * and that either A or B is even in the next iteration.
  307. */
  308. if (BN_ucmp(B, A) >= 0)
  309. {
  310. /* -sign*(X + Y)*a == B - A (mod |n|) */
  311. if (!BN_uadd(X, X, Y)) goto err;
  312. /* NB: we could use BN_mod_add_quick(X, X, Y, n), but that
  313. * actually makes the algorithm slower */
  314. if (!BN_usub(B, B, A)) goto err;
  315. }
  316. else
  317. {
  318. /* sign*(X + Y)*a == A - B (mod |n|) */
  319. if (!BN_uadd(Y, Y, X)) goto err;
  320. /* as above, BN_mod_add_quick(Y, Y, X, n) would slow things down */
  321. if (!BN_usub(A, A, B)) goto err;
  322. }
  323. }
  324. }
  325. else
  326. {
  327. /* general inversion algorithm */
  328. while (!BN_is_zero(B))
  329. {
  330. BIGNUM *tmp;
  331. /*
  332. * 0 < B < A,
  333. * (*) -sign*X*a == B (mod |n|),
  334. * sign*Y*a == A (mod |n|)
  335. */
  336. /* (D, M) := (A/B, A%B) ... */
  337. if (BN_num_bits(A) == BN_num_bits(B))
  338. {
  339. if (!BN_one(D)) goto err;
  340. if (!BN_sub(M,A,B)) goto err;
  341. }
  342. else if (BN_num_bits(A) == BN_num_bits(B) + 1)
  343. {
  344. /* A/B is 1, 2, or 3 */
  345. if (!BN_lshift1(T,B)) goto err;
  346. if (BN_ucmp(A,T) < 0)
  347. {
  348. /* A < 2*B, so D=1 */
  349. if (!BN_one(D)) goto err;
  350. if (!BN_sub(M,A,B)) goto err;
  351. }
  352. else
  353. {
  354. /* A >= 2*B, so D=2 or D=3 */
  355. if (!BN_sub(M,A,T)) goto err;
  356. if (!BN_add(D,T,B)) goto err; /* use D (:= 3*B) as temp */
  357. if (BN_ucmp(A,D) < 0)
  358. {
  359. /* A < 3*B, so D=2 */
  360. if (!BN_set_word(D,2)) goto err;
  361. /* M (= A - 2*B) already has the correct value */
  362. }
  363. else
  364. {
  365. /* only D=3 remains */
  366. if (!BN_set_word(D,3)) goto err;
  367. /* currently M = A - 2*B, but we need M = A - 3*B */
  368. if (!BN_sub(M,M,B)) goto err;
  369. }
  370. }
  371. }
  372. else
  373. {
  374. if (!BN_div(D,M,A,B,ctx)) goto err;
  375. }
  376. /* Now
  377. * A = D*B + M;
  378. * thus we have
  379. * (**) sign*Y*a == D*B + M (mod |n|).
  380. */
  381. tmp=A; /* keep the BIGNUM object, the value does not matter */
  382. /* (A, B) := (B, A mod B) ... */
  383. A=B;
  384. B=M;
  385. /* ... so we have 0 <= B < A again */
  386. /* Since the former M is now B and the former B is now A,
  387. * (**) translates into
  388. * sign*Y*a == D*A + B (mod |n|),
  389. * i.e.
  390. * sign*Y*a - D*A == B (mod |n|).
  391. * Similarly, (*) translates into
  392. * -sign*X*a == A (mod |n|).
  393. *
  394. * Thus,
  395. * sign*Y*a + D*sign*X*a == B (mod |n|),
  396. * i.e.
  397. * sign*(Y + D*X)*a == B (mod |n|).
  398. *
  399. * So if we set (X, Y, sign) := (Y + D*X, X, -sign), we arrive back at
  400. * -sign*X*a == B (mod |n|),
  401. * sign*Y*a == A (mod |n|).
  402. * Note that X and Y stay non-negative all the time.
  403. */
  404. /* most of the time D is very small, so we can optimize tmp := D*X+Y */
  405. if (BN_is_one(D))
  406. {
  407. if (!BN_add(tmp,X,Y)) goto err;
  408. }
  409. else
  410. {
  411. if (BN_is_word(D,2))
  412. {
  413. if (!BN_lshift1(tmp,X)) goto err;
  414. }
  415. else if (BN_is_word(D,4))
  416. {
  417. if (!BN_lshift(tmp,X,2)) goto err;
  418. }
  419. else if (D->top == 1)
  420. {
  421. if (!BN_copy(tmp,X)) goto err;
  422. if (!BN_mul_word(tmp,D->d[0])) goto err;
  423. }
  424. else
  425. {
  426. if (!BN_mul(tmp,D,X,ctx)) goto err;
  427. }
  428. if (!BN_add(tmp,tmp,Y)) goto err;
  429. }
  430. M=Y; /* keep the BIGNUM object, the value does not matter */
  431. Y=X;
  432. X=tmp;
  433. sign = -sign;
  434. }
  435. }
  436. /*
  437. * The while loop (Euclid's algorithm) ends when
  438. * A == gcd(a,n);
  439. * we have
  440. * sign*Y*a == A (mod |n|),
  441. * where Y is non-negative.
  442. */
  443. if (sign < 0)
  444. {
  445. if (!BN_sub(Y,n,Y)) goto err;
  446. }
  447. /* Now Y*a == A (mod |n|). */
  448. if (BN_is_one(A))
  449. {
  450. /* Y*a == 1 (mod |n|) */
  451. if (!Y->neg && BN_ucmp(Y,n) < 0)
  452. {
  453. if (!BN_copy(R,Y)) goto err;
  454. }
  455. else
  456. {
  457. if (!BN_nnmod(R,Y,n,ctx)) goto err;
  458. }
  459. }
  460. else
  461. {
  462. if (pnoinv)
  463. *pnoinv = 1;
  464. goto err;
  465. }
  466. ret=R;
  467. err:
  468. if ((ret == NULL) && (in == NULL)) BN_free(R);
  469. BN_CTX_end(ctx);
  470. bn_check_top(ret);
  471. return(ret);
  472. }
  473. /* BN_mod_inverse_no_branch is a special version of BN_mod_inverse.
  474. * It does not contain branches that may leak sensitive information.
  475. */
  476. static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,
  477. const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)
  478. {
  479. BIGNUM *A,*B,*X,*Y,*M,*D,*T,*R=NULL;
  480. BIGNUM local_A, local_B;
  481. BIGNUM *pA, *pB;
  482. BIGNUM *ret=NULL;
  483. int sign;
  484. bn_check_top(a);
  485. bn_check_top(n);
  486. BN_CTX_start(ctx);
  487. A = BN_CTX_get(ctx);
  488. B = BN_CTX_get(ctx);
  489. X = BN_CTX_get(ctx);
  490. D = BN_CTX_get(ctx);
  491. M = BN_CTX_get(ctx);
  492. Y = BN_CTX_get(ctx);
  493. T = BN_CTX_get(ctx);
  494. if (T == NULL) goto err;
  495. if (in == NULL)
  496. R=BN_new();
  497. else
  498. R=in;
  499. if (R == NULL) goto err;
  500. BN_one(X);
  501. BN_zero(Y);
  502. if (BN_copy(B,a) == NULL) goto err;
  503. if (BN_copy(A,n) == NULL) goto err;
  504. A->neg = 0;
  505. if (B->neg || (BN_ucmp(B, A) >= 0))
  506. {
  507. /* Turn BN_FLG_CONSTTIME flag on, so that when BN_div is invoked,
  508. * BN_div_no_branch will be called eventually.
  509. */
  510. pB = &local_B;
  511. BN_with_flags(pB, B, BN_FLG_CONSTTIME);
  512. if (!BN_nnmod(B, pB, A, ctx)) goto err;
  513. }
  514. sign = -1;
  515. /* From B = a mod |n|, A = |n| it follows that
  516. *
  517. * 0 <= B < A,
  518. * -sign*X*a == B (mod |n|),
  519. * sign*Y*a == A (mod |n|).
  520. */
  521. while (!BN_is_zero(B))
  522. {
  523. BIGNUM *tmp;
  524. /*
  525. * 0 < B < A,
  526. * (*) -sign*X*a == B (mod |n|),
  527. * sign*Y*a == A (mod |n|)
  528. */
  529. /* Turn BN_FLG_CONSTTIME flag on, so that when BN_div is invoked,
  530. * BN_div_no_branch will be called eventually.
  531. */
  532. pA = &local_A;
  533. BN_with_flags(pA, A, BN_FLG_CONSTTIME);
  534. /* (D, M) := (A/B, A%B) ... */
  535. if (!BN_div(D,M,pA,B,ctx)) goto err;
  536. /* Now
  537. * A = D*B + M;
  538. * thus we have
  539. * (**) sign*Y*a == D*B + M (mod |n|).
  540. */
  541. tmp=A; /* keep the BIGNUM object, the value does not matter */
  542. /* (A, B) := (B, A mod B) ... */
  543. A=B;
  544. B=M;
  545. /* ... so we have 0 <= B < A again */
  546. /* Since the former M is now B and the former B is now A,
  547. * (**) translates into
  548. * sign*Y*a == D*A + B (mod |n|),
  549. * i.e.
  550. * sign*Y*a - D*A == B (mod |n|).
  551. * Similarly, (*) translates into
  552. * -sign*X*a == A (mod |n|).
  553. *
  554. * Thus,
  555. * sign*Y*a + D*sign*X*a == B (mod |n|),
  556. * i.e.
  557. * sign*(Y + D*X)*a == B (mod |n|).
  558. *
  559. * So if we set (X, Y, sign) := (Y + D*X, X, -sign), we arrive back at
  560. * -sign*X*a == B (mod |n|),
  561. * sign*Y*a == A (mod |n|).
  562. * Note that X and Y stay non-negative all the time.
  563. */
  564. if (!BN_mul(tmp,D,X,ctx)) goto err;
  565. if (!BN_add(tmp,tmp,Y)) goto err;
  566. M=Y; /* keep the BIGNUM object, the value does not matter */
  567. Y=X;
  568. X=tmp;
  569. sign = -sign;
  570. }
  571. /*
  572. * The while loop (Euclid's algorithm) ends when
  573. * A == gcd(a,n);
  574. * we have
  575. * sign*Y*a == A (mod |n|),
  576. * where Y is non-negative.
  577. */
  578. if (sign < 0)
  579. {
  580. if (!BN_sub(Y,n,Y)) goto err;
  581. }
  582. /* Now Y*a == A (mod |n|). */
  583. if (BN_is_one(A))
  584. {
  585. /* Y*a == 1 (mod |n|) */
  586. if (!Y->neg && BN_ucmp(Y,n) < 0)
  587. {
  588. if (!BN_copy(R,Y)) goto err;
  589. }
  590. else
  591. {
  592. if (!BN_nnmod(R,Y,n,ctx)) goto err;
  593. }
  594. }
  595. else
  596. {
  597. BNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH,BN_R_NO_INVERSE);
  598. goto err;
  599. }
  600. ret=R;
  601. err:
  602. if ((ret == NULL) && (in == NULL)) BN_free(R);
  603. BN_CTX_end(ctx);
  604. bn_check_top(ret);
  605. return(ret);
  606. }