25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

705 satır
19 KiB

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