bn_gcd.c 22 KB

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