gcd.c 19 KB

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