gcd.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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 <assert.h>
  110. #include <openssl/err.h>
  111. #include "internal.h"
  112. static BIGNUM *euclid(BIGNUM *a, BIGNUM *b) {
  113. BIGNUM *t;
  114. int shifts = 0;
  115. // 0 <= b <= a
  116. while (!BN_is_zero(b)) {
  117. // 0 < b <= a
  118. if (BN_is_odd(a)) {
  119. if (BN_is_odd(b)) {
  120. if (!BN_sub(a, a, b)) {
  121. goto err;
  122. }
  123. if (!BN_rshift1(a, a)) {
  124. goto err;
  125. }
  126. if (BN_cmp(a, b) < 0) {
  127. t = a;
  128. a = b;
  129. b = t;
  130. }
  131. } else {
  132. // a odd - b even
  133. if (!BN_rshift1(b, b)) {
  134. goto err;
  135. }
  136. if (BN_cmp(a, b) < 0) {
  137. t = a;
  138. a = b;
  139. b = t;
  140. }
  141. }
  142. } else {
  143. // a is even
  144. if (BN_is_odd(b)) {
  145. if (!BN_rshift1(a, a)) {
  146. goto err;
  147. }
  148. if (BN_cmp(a, b) < 0) {
  149. t = a;
  150. a = b;
  151. b = t;
  152. }
  153. } else {
  154. // a even - b even
  155. if (!BN_rshift1(a, a)) {
  156. goto err;
  157. }
  158. if (!BN_rshift1(b, b)) {
  159. goto err;
  160. }
  161. shifts++;
  162. }
  163. }
  164. // 0 <= b <= a
  165. }
  166. if (shifts) {
  167. if (!BN_lshift(a, a, shifts)) {
  168. goto err;
  169. }
  170. }
  171. return a;
  172. err:
  173. return NULL;
  174. }
  175. int BN_gcd(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx) {
  176. BIGNUM *a, *b, *t;
  177. int ret = 0;
  178. BN_CTX_start(ctx);
  179. a = BN_CTX_get(ctx);
  180. b = BN_CTX_get(ctx);
  181. if (a == NULL || b == NULL) {
  182. goto err;
  183. }
  184. if (BN_copy(a, in_a) == NULL) {
  185. goto err;
  186. }
  187. if (BN_copy(b, in_b) == NULL) {
  188. goto err;
  189. }
  190. a->neg = 0;
  191. b->neg = 0;
  192. if (BN_cmp(a, b) < 0) {
  193. t = a;
  194. a = b;
  195. b = t;
  196. }
  197. t = euclid(a, b);
  198. if (t == NULL) {
  199. goto err;
  200. }
  201. if (BN_copy(r, t) == NULL) {
  202. goto err;
  203. }
  204. ret = 1;
  205. err:
  206. BN_CTX_end(ctx);
  207. return ret;
  208. }
  209. // solves ax == 1 (mod n)
  210. static int bn_mod_inverse_general(BIGNUM *out, int *out_no_inverse,
  211. const BIGNUM *a, const BIGNUM *n,
  212. BN_CTX *ctx);
  213. int BN_mod_inverse_odd(BIGNUM *out, int *out_no_inverse, const BIGNUM *a,
  214. const BIGNUM *n, BN_CTX *ctx) {
  215. *out_no_inverse = 0;
  216. if (!BN_is_odd(n)) {
  217. OPENSSL_PUT_ERROR(BN, BN_R_CALLED_WITH_EVEN_MODULUS);
  218. return 0;
  219. }
  220. if (BN_is_negative(a) || BN_cmp(a, n) >= 0) {
  221. OPENSSL_PUT_ERROR(BN, BN_R_INPUT_NOT_REDUCED);
  222. return 0;
  223. }
  224. BIGNUM *A, *B, *X, *Y;
  225. int ret = 0;
  226. int sign;
  227. BN_CTX_start(ctx);
  228. A = BN_CTX_get(ctx);
  229. B = BN_CTX_get(ctx);
  230. X = BN_CTX_get(ctx);
  231. Y = BN_CTX_get(ctx);
  232. if (Y == NULL) {
  233. goto err;
  234. }
  235. BIGNUM *R = out;
  236. BN_zero(Y);
  237. if (!BN_one(X) || BN_copy(B, a) == NULL || BN_copy(A, n) == NULL) {
  238. goto err;
  239. }
  240. A->neg = 0;
  241. sign = -1;
  242. // From B = a mod |n|, A = |n| it follows that
  243. //
  244. // 0 <= B < A,
  245. // -sign*X*a == B (mod |n|),
  246. // sign*Y*a == A (mod |n|).
  247. // Binary inversion algorithm; requires odd modulus. This is faster than the
  248. // general algorithm if the modulus is sufficiently small (about 400 .. 500
  249. // bits on 32-bit systems, but much more on 64-bit systems)
  250. int shift;
  251. while (!BN_is_zero(B)) {
  252. // 0 < B < |n|,
  253. // 0 < A <= |n|,
  254. // (1) -sign*X*a == B (mod |n|),
  255. // (2) sign*Y*a == A (mod |n|)
  256. // Now divide B by the maximum possible power of two in the integers,
  257. // and divide X by the same value mod |n|.
  258. // When we're done, (1) still holds.
  259. shift = 0;
  260. while (!BN_is_bit_set(B, shift)) {
  261. // note that 0 < B
  262. shift++;
  263. if (BN_is_odd(X)) {
  264. if (!BN_uadd(X, X, n)) {
  265. goto err;
  266. }
  267. }
  268. // now X is even, so we can easily divide it by two
  269. if (!BN_rshift1(X, X)) {
  270. goto err;
  271. }
  272. }
  273. if (shift > 0) {
  274. if (!BN_rshift(B, B, shift)) {
  275. goto err;
  276. }
  277. }
  278. // Same for A and Y. Afterwards, (2) still holds.
  279. shift = 0;
  280. while (!BN_is_bit_set(A, shift)) {
  281. // note that 0 < A
  282. shift++;
  283. if (BN_is_odd(Y)) {
  284. if (!BN_uadd(Y, Y, n)) {
  285. goto err;
  286. }
  287. }
  288. // now Y is even
  289. if (!BN_rshift1(Y, Y)) {
  290. goto err;
  291. }
  292. }
  293. if (shift > 0) {
  294. if (!BN_rshift(A, A, shift)) {
  295. goto err;
  296. }
  297. }
  298. // We still have (1) and (2).
  299. // Both A and B are odd.
  300. // The following computations ensure that
  301. //
  302. // 0 <= B < |n|,
  303. // 0 < A < |n|,
  304. // (1) -sign*X*a == B (mod |n|),
  305. // (2) sign*Y*a == A (mod |n|),
  306. //
  307. // and that either A or B is even in the next iteration.
  308. if (BN_ucmp(B, A) >= 0) {
  309. // -sign*(X + Y)*a == B - A (mod |n|)
  310. if (!BN_uadd(X, X, Y)) {
  311. goto err;
  312. }
  313. // NB: we could use BN_mod_add_quick(X, X, Y, n), but that
  314. // actually makes the algorithm slower
  315. if (!BN_usub(B, B, A)) {
  316. goto err;
  317. }
  318. } else {
  319. // sign*(X + Y)*a == A - B (mod |n|)
  320. if (!BN_uadd(Y, Y, X)) {
  321. goto err;
  322. }
  323. // as above, BN_mod_add_quick(Y, Y, X, n) would slow things down
  324. if (!BN_usub(A, A, B)) {
  325. goto err;
  326. }
  327. }
  328. }
  329. if (!BN_is_one(A)) {
  330. *out_no_inverse = 1;
  331. OPENSSL_PUT_ERROR(BN, BN_R_NO_INVERSE);
  332. goto err;
  333. }
  334. // The while loop (Euclid's algorithm) ends when
  335. // A == gcd(a,n);
  336. // we have
  337. // sign*Y*a == A (mod |n|),
  338. // where Y is non-negative.
  339. if (sign < 0) {
  340. if (!BN_sub(Y, n, Y)) {
  341. goto err;
  342. }
  343. }
  344. // Now Y*a == A (mod |n|).
  345. // Y*a == 1 (mod |n|)
  346. if (!Y->neg && BN_ucmp(Y, n) < 0) {
  347. if (!BN_copy(R, Y)) {
  348. goto err;
  349. }
  350. } else {
  351. if (!BN_nnmod(R, Y, n, ctx)) {
  352. goto err;
  353. }
  354. }
  355. ret = 1;
  356. err:
  357. BN_CTX_end(ctx);
  358. return ret;
  359. }
  360. BIGNUM *BN_mod_inverse(BIGNUM *out, const BIGNUM *a, const BIGNUM *n,
  361. BN_CTX *ctx) {
  362. BIGNUM *new_out = NULL;
  363. if (out == NULL) {
  364. new_out = BN_new();
  365. if (new_out == NULL) {
  366. OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
  367. return NULL;
  368. }
  369. out = new_out;
  370. }
  371. int ok = 0;
  372. BIGNUM *a_reduced = NULL;
  373. if (a->neg || BN_ucmp(a, n) >= 0) {
  374. a_reduced = BN_dup(a);
  375. if (a_reduced == NULL) {
  376. goto err;
  377. }
  378. if (!BN_nnmod(a_reduced, a_reduced, n, ctx)) {
  379. goto err;
  380. }
  381. a = a_reduced;
  382. }
  383. int no_inverse;
  384. if (!BN_is_odd(n)) {
  385. if (!bn_mod_inverse_general(out, &no_inverse, a, n, ctx)) {
  386. goto err;
  387. }
  388. } else if (!BN_mod_inverse_odd(out, &no_inverse, a, n, ctx)) {
  389. goto err;
  390. }
  391. ok = 1;
  392. err:
  393. if (!ok) {
  394. BN_free(new_out);
  395. out = NULL;
  396. }
  397. BN_free(a_reduced);
  398. return out;
  399. }
  400. int BN_mod_inverse_blinded(BIGNUM *out, int *out_no_inverse, const BIGNUM *a,
  401. const BN_MONT_CTX *mont, BN_CTX *ctx) {
  402. *out_no_inverse = 0;
  403. if (BN_is_negative(a) || BN_cmp(a, &mont->N) >= 0) {
  404. OPENSSL_PUT_ERROR(BN, BN_R_INPUT_NOT_REDUCED);
  405. return 0;
  406. }
  407. int ret = 0;
  408. BIGNUM blinding_factor;
  409. BN_init(&blinding_factor);
  410. if (!BN_rand_range_ex(&blinding_factor, 1, &mont->N) ||
  411. !BN_mod_mul_montgomery(out, &blinding_factor, a, mont, ctx) ||
  412. !BN_mod_inverse_odd(out, out_no_inverse, out, &mont->N, ctx) ||
  413. !BN_mod_mul_montgomery(out, &blinding_factor, out, mont, ctx)) {
  414. OPENSSL_PUT_ERROR(BN, ERR_R_BN_LIB);
  415. goto err;
  416. }
  417. ret = 1;
  418. err:
  419. BN_free(&blinding_factor);
  420. return ret;
  421. }
  422. // bn_mod_inverse_general is the general inversion algorithm that works for
  423. // both even and odd |n|. It was specifically designed to contain fewer
  424. // branches that may leak sensitive information; see "New Branch Prediction
  425. // Vulnerabilities in OpenSSL and Necessary Software Countermeasures" by
  426. // Onur Acıçmez, Shay Gueron, and Jean-Pierre Seifert.
  427. static int bn_mod_inverse_general(BIGNUM *out, int *out_no_inverse,
  428. const BIGNUM *a, const BIGNUM *n,
  429. BN_CTX *ctx) {
  430. BIGNUM *A, *B, *X, *Y, *M, *D, *T;
  431. int ret = 0;
  432. int sign;
  433. *out_no_inverse = 0;
  434. BN_CTX_start(ctx);
  435. A = BN_CTX_get(ctx);
  436. B = BN_CTX_get(ctx);
  437. X = BN_CTX_get(ctx);
  438. D = BN_CTX_get(ctx);
  439. M = BN_CTX_get(ctx);
  440. Y = BN_CTX_get(ctx);
  441. T = BN_CTX_get(ctx);
  442. if (T == NULL) {
  443. goto err;
  444. }
  445. BIGNUM *R = out;
  446. BN_zero(Y);
  447. if (!BN_one(X) || BN_copy(B, a) == NULL || BN_copy(A, n) == NULL) {
  448. goto err;
  449. }
  450. A->neg = 0;
  451. sign = -1;
  452. // From B = a mod |n|, A = |n| it follows that
  453. //
  454. // 0 <= B < A,
  455. // -sign*X*a == B (mod |n|),
  456. // sign*Y*a == A (mod |n|).
  457. while (!BN_is_zero(B)) {
  458. BIGNUM *tmp;
  459. // 0 < B < A,
  460. // (*) -sign*X*a == B (mod |n|),
  461. // sign*Y*a == A (mod |n|)
  462. // (D, M) := (A/B, A%B) ...
  463. if (!BN_div(D, M, A, B, ctx)) {
  464. goto err;
  465. }
  466. // Now
  467. // A = D*B + M;
  468. // thus we have
  469. // (**) sign*Y*a == D*B + M (mod |n|).
  470. tmp = A; // keep the BIGNUM object, the value does not matter
  471. // (A, B) := (B, A mod B) ...
  472. A = B;
  473. B = M;
  474. // ... so we have 0 <= B < A again
  475. // Since the former M is now B and the former B is now A,
  476. // (**) translates into
  477. // sign*Y*a == D*A + B (mod |n|),
  478. // i.e.
  479. // sign*Y*a - D*A == B (mod |n|).
  480. // Similarly, (*) translates into
  481. // -sign*X*a == A (mod |n|).
  482. //
  483. // Thus,
  484. // sign*Y*a + D*sign*X*a == B (mod |n|),
  485. // i.e.
  486. // sign*(Y + D*X)*a == B (mod |n|).
  487. //
  488. // So if we set (X, Y, sign) := (Y + D*X, X, -sign), we arrive back at
  489. // -sign*X*a == B (mod |n|),
  490. // sign*Y*a == A (mod |n|).
  491. // Note that X and Y stay non-negative all the time.
  492. if (!BN_mul(tmp, D, X, ctx)) {
  493. goto err;
  494. }
  495. if (!BN_add(tmp, tmp, Y)) {
  496. goto err;
  497. }
  498. M = Y; // keep the BIGNUM object, the value does not matter
  499. Y = X;
  500. X = tmp;
  501. sign = -sign;
  502. }
  503. if (!BN_is_one(A)) {
  504. *out_no_inverse = 1;
  505. OPENSSL_PUT_ERROR(BN, BN_R_NO_INVERSE);
  506. goto err;
  507. }
  508. // The while loop (Euclid's algorithm) ends when
  509. // A == gcd(a,n);
  510. // we have
  511. // sign*Y*a == A (mod |n|),
  512. // where Y is non-negative.
  513. if (sign < 0) {
  514. if (!BN_sub(Y, n, Y)) {
  515. goto err;
  516. }
  517. }
  518. // Now Y*a == A (mod |n|).
  519. // Y*a == 1 (mod |n|)
  520. if (!Y->neg && BN_ucmp(Y, n) < 0) {
  521. if (!BN_copy(R, Y)) {
  522. goto err;
  523. }
  524. } else {
  525. if (!BN_nnmod(R, Y, n, ctx)) {
  526. goto err;
  527. }
  528. }
  529. ret = 1;
  530. err:
  531. BN_CTX_end(ctx);
  532. return ret;
  533. }
  534. int bn_mod_inverse_prime(BIGNUM *out, const BIGNUM *a, const BIGNUM *p,
  535. BN_CTX *ctx, const BN_MONT_CTX *mont_p) {
  536. BN_CTX_start(ctx);
  537. BIGNUM *p_minus_2 = BN_CTX_get(ctx);
  538. int ok = p_minus_2 != NULL &&
  539. BN_copy(p_minus_2, p) &&
  540. BN_sub_word(p_minus_2, 2) &&
  541. BN_mod_exp_mont(out, a, p_minus_2, p, ctx, mont_p);
  542. BN_CTX_end(ctx);
  543. return ok;
  544. }
  545. int bn_mod_inverse_secret_prime(BIGNUM *out, const BIGNUM *a, const BIGNUM *p,
  546. BN_CTX *ctx, const BN_MONT_CTX *mont_p) {
  547. BN_CTX_start(ctx);
  548. BIGNUM *p_minus_2 = BN_CTX_get(ctx);
  549. int ok = p_minus_2 != NULL &&
  550. BN_copy(p_minus_2, p) &&
  551. BN_sub_word(p_minus_2, 2) &&
  552. BN_mod_exp_mont_consttime(out, a, p_minus_2, p, ctx, mont_p);
  553. BN_CTX_end(ctx);
  554. return ok;
  555. }