bn_gcd.c 22 KB

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