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.

gcd.c 19 KiB

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