gcd.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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. */
  248. /* Binary inversion algorithm; requires odd modulus. This is faster than the
  249. * general algorithm if the modulus is sufficiently small (about 400 .. 500
  250. * bits on 32-bit systems, but much more on 64-bit systems) */
  251. int shift;
  252. while (!BN_is_zero(B)) {
  253. /* 0 < B < |n|,
  254. * 0 < A <= |n|,
  255. * (1) -sign*X*a == B (mod |n|),
  256. * (2) sign*Y*a == A (mod |n|) */
  257. /* Now divide B by the maximum possible power of two in the integers,
  258. * and divide X by the same value mod |n|.
  259. * When we're done, (1) still holds. */
  260. shift = 0;
  261. while (!BN_is_bit_set(B, shift)) {
  262. /* note that 0 < B */
  263. shift++;
  264. if (BN_is_odd(X)) {
  265. if (!BN_uadd(X, X, n)) {
  266. goto err;
  267. }
  268. }
  269. /* now X is even, so we can easily divide it by two */
  270. if (!BN_rshift1(X, X)) {
  271. goto err;
  272. }
  273. }
  274. if (shift > 0) {
  275. if (!BN_rshift(B, B, shift)) {
  276. goto err;
  277. }
  278. }
  279. /* Same for A and Y. Afterwards, (2) still holds. */
  280. shift = 0;
  281. while (!BN_is_bit_set(A, shift)) {
  282. /* note that 0 < A */
  283. shift++;
  284. if (BN_is_odd(Y)) {
  285. if (!BN_uadd(Y, Y, n)) {
  286. goto err;
  287. }
  288. }
  289. /* now Y is even */
  290. if (!BN_rshift1(Y, Y)) {
  291. goto err;
  292. }
  293. }
  294. if (shift > 0) {
  295. if (!BN_rshift(A, A, shift)) {
  296. goto err;
  297. }
  298. }
  299. /* We still have (1) and (2).
  300. * Both A and B are odd.
  301. * The following computations ensure that
  302. *
  303. * 0 <= B < |n|,
  304. * 0 < A < |n|,
  305. * (1) -sign*X*a == B (mod |n|),
  306. * (2) sign*Y*a == A (mod |n|),
  307. *
  308. * and that either A or B is even in the next iteration. */
  309. if (BN_ucmp(B, A) >= 0) {
  310. /* -sign*(X + Y)*a == B - A (mod |n|) */
  311. if (!BN_uadd(X, X, Y)) {
  312. goto err;
  313. }
  314. /* NB: we could use BN_mod_add_quick(X, X, Y, n), but that
  315. * actually makes the algorithm slower */
  316. if (!BN_usub(B, B, A)) {
  317. goto err;
  318. }
  319. } else {
  320. /* sign*(X + Y)*a == A - B (mod |n|) */
  321. if (!BN_uadd(Y, Y, X)) {
  322. goto err;
  323. }
  324. /* as above, BN_mod_add_quick(Y, Y, X, n) would slow things down */
  325. if (!BN_usub(A, A, B)) {
  326. goto err;
  327. }
  328. }
  329. }
  330. if (!BN_is_one(A)) {
  331. *out_no_inverse = 1;
  332. OPENSSL_PUT_ERROR(BN, BN_R_NO_INVERSE);
  333. goto err;
  334. }
  335. /* The while loop (Euclid's algorithm) ends when
  336. * A == gcd(a,n);
  337. * we have
  338. * sign*Y*a == A (mod |n|),
  339. * where Y is non-negative. */
  340. if (sign < 0) {
  341. if (!BN_sub(Y, n, Y)) {
  342. goto err;
  343. }
  344. }
  345. /* Now Y*a == A (mod |n|). */
  346. /* Y*a == 1 (mod |n|) */
  347. if (!Y->neg && BN_ucmp(Y, n) < 0) {
  348. if (!BN_copy(R, Y)) {
  349. goto err;
  350. }
  351. } else {
  352. if (!BN_nnmod(R, Y, n, ctx)) {
  353. goto err;
  354. }
  355. }
  356. ret = 1;
  357. err:
  358. BN_CTX_end(ctx);
  359. return ret;
  360. }
  361. BIGNUM *BN_mod_inverse(BIGNUM *out, const BIGNUM *a, const BIGNUM *n,
  362. BN_CTX *ctx) {
  363. BIGNUM *new_out = NULL;
  364. if (out == NULL) {
  365. new_out = BN_new();
  366. if (new_out == NULL) {
  367. OPENSSL_PUT_ERROR(BN, ERR_R_MALLOC_FAILURE);
  368. return NULL;
  369. }
  370. out = new_out;
  371. }
  372. int ok = 0;
  373. BIGNUM *a_reduced = NULL;
  374. if (a->neg || BN_ucmp(a, n) >= 0) {
  375. a_reduced = BN_dup(a);
  376. if (a_reduced == NULL) {
  377. goto err;
  378. }
  379. if (!BN_nnmod(a_reduced, a_reduced, n, ctx)) {
  380. goto err;
  381. }
  382. a = a_reduced;
  383. }
  384. int no_inverse;
  385. if (!BN_is_odd(n)) {
  386. if (!bn_mod_inverse_general(out, &no_inverse, a, n, ctx)) {
  387. goto err;
  388. }
  389. } else if (!BN_mod_inverse_odd(out, &no_inverse, a, n, ctx)) {
  390. goto err;
  391. }
  392. ok = 1;
  393. err:
  394. if (!ok) {
  395. BN_free(new_out);
  396. out = NULL;
  397. }
  398. BN_free(a_reduced);
  399. return out;
  400. }
  401. int BN_mod_inverse_blinded(BIGNUM *out, int *out_no_inverse, const BIGNUM *a,
  402. const BN_MONT_CTX *mont, BN_CTX *ctx) {
  403. *out_no_inverse = 0;
  404. if (BN_is_negative(a) || BN_cmp(a, &mont->N) >= 0) {
  405. OPENSSL_PUT_ERROR(BN, BN_R_INPUT_NOT_REDUCED);
  406. return 0;
  407. }
  408. int ret = 0;
  409. BIGNUM blinding_factor;
  410. BN_init(&blinding_factor);
  411. if (!BN_rand_range_ex(&blinding_factor, 1, &mont->N) ||
  412. !BN_mod_mul_montgomery(out, &blinding_factor, a, mont, ctx) ||
  413. !BN_mod_inverse_odd(out, out_no_inverse, out, &mont->N, ctx) ||
  414. !BN_mod_mul_montgomery(out, &blinding_factor, out, mont, ctx)) {
  415. OPENSSL_PUT_ERROR(BN, ERR_R_BN_LIB);
  416. goto err;
  417. }
  418. ret = 1;
  419. err:
  420. BN_free(&blinding_factor);
  421. return ret;
  422. }
  423. /* bn_mod_inverse_general is the general inversion algorithm that works for
  424. * both even and odd |n|. It was specifically designed to contain fewer
  425. * branches that may leak sensitive information; see "New Branch Prediction
  426. * Vulnerabilities in OpenSSL and Necessary Software Countermeasures" by
  427. * Onur Acıçmez, Shay Gueron, and Jean-Pierre Seifert. */
  428. static int bn_mod_inverse_general(BIGNUM *out, int *out_no_inverse,
  429. const BIGNUM *a, const BIGNUM *n,
  430. BN_CTX *ctx) {
  431. BIGNUM *A, *B, *X, *Y, *M, *D, *T;
  432. int ret = 0;
  433. int sign;
  434. *out_no_inverse = 0;
  435. BN_CTX_start(ctx);
  436. A = BN_CTX_get(ctx);
  437. B = BN_CTX_get(ctx);
  438. X = BN_CTX_get(ctx);
  439. D = BN_CTX_get(ctx);
  440. M = BN_CTX_get(ctx);
  441. Y = BN_CTX_get(ctx);
  442. T = BN_CTX_get(ctx);
  443. if (T == NULL) {
  444. goto err;
  445. }
  446. BIGNUM *R = out;
  447. BN_zero(Y);
  448. if (!BN_one(X) || BN_copy(B, a) == NULL || BN_copy(A, n) == NULL) {
  449. goto err;
  450. }
  451. A->neg = 0;
  452. sign = -1;
  453. /* From B = a mod |n|, A = |n| it follows that
  454. *
  455. * 0 <= B < A,
  456. * -sign*X*a == B (mod |n|),
  457. * sign*Y*a == A (mod |n|).
  458. */
  459. while (!BN_is_zero(B)) {
  460. BIGNUM *tmp;
  461. /*
  462. * 0 < B < A,
  463. * (*) -sign*X*a == B (mod |n|),
  464. * sign*Y*a == A (mod |n|)
  465. */
  466. /* (D, M) := (A/B, A%B) ... */
  467. if (!BN_div(D, M, A, B, ctx)) {
  468. goto err;
  469. }
  470. /* Now
  471. * A = D*B + M;
  472. * thus we have
  473. * (**) sign*Y*a == D*B + M (mod |n|).
  474. */
  475. tmp = A; /* keep the BIGNUM object, the value does not matter */
  476. /* (A, B) := (B, A mod B) ... */
  477. A = B;
  478. B = M;
  479. /* ... so we have 0 <= B < A again */
  480. /* Since the former M is now B and the former B is now A,
  481. * (**) translates into
  482. * sign*Y*a == D*A + B (mod |n|),
  483. * i.e.
  484. * sign*Y*a - D*A == B (mod |n|).
  485. * Similarly, (*) translates into
  486. * -sign*X*a == A (mod |n|).
  487. *
  488. * Thus,
  489. * sign*Y*a + D*sign*X*a == B (mod |n|),
  490. * i.e.
  491. * sign*(Y + D*X)*a == B (mod |n|).
  492. *
  493. * So if we set (X, Y, sign) := (Y + D*X, X, -sign), we arrive back at
  494. * -sign*X*a == B (mod |n|),
  495. * sign*Y*a == A (mod |n|).
  496. * Note that X and Y stay non-negative all the time.
  497. */
  498. if (!BN_mul(tmp, D, X, ctx)) {
  499. goto err;
  500. }
  501. if (!BN_add(tmp, tmp, Y)) {
  502. goto err;
  503. }
  504. M = Y; /* keep the BIGNUM object, the value does not matter */
  505. Y = X;
  506. X = tmp;
  507. sign = -sign;
  508. }
  509. if (!BN_is_one(A)) {
  510. *out_no_inverse = 1;
  511. OPENSSL_PUT_ERROR(BN, BN_R_NO_INVERSE);
  512. goto err;
  513. }
  514. /*
  515. * The while loop (Euclid's algorithm) ends when
  516. * A == gcd(a,n);
  517. * we have
  518. * sign*Y*a == A (mod |n|),
  519. * where Y is non-negative.
  520. */
  521. if (sign < 0) {
  522. if (!BN_sub(Y, n, Y)) {
  523. goto err;
  524. }
  525. }
  526. /* Now Y*a == A (mod |n|). */
  527. /* Y*a == 1 (mod |n|) */
  528. if (!Y->neg && BN_ucmp(Y, n) < 0) {
  529. if (!BN_copy(R, Y)) {
  530. goto err;
  531. }
  532. } else {
  533. if (!BN_nnmod(R, Y, n, ctx)) {
  534. goto err;
  535. }
  536. }
  537. ret = 1;
  538. err:
  539. BN_CTX_end(ctx);
  540. return ret;
  541. }
  542. int bn_mod_inverse_prime(BIGNUM *out, const BIGNUM *a, const BIGNUM *p,
  543. BN_CTX *ctx, const BN_MONT_CTX *mont_p) {
  544. BN_CTX_start(ctx);
  545. BIGNUM *p_minus_2 = BN_CTX_get(ctx);
  546. int ok = p_minus_2 != NULL &&
  547. BN_copy(p_minus_2, p) &&
  548. BN_sub_word(p_minus_2, 2) &&
  549. BN_mod_exp_mont(out, a, p_minus_2, p, ctx, mont_p);
  550. BN_CTX_end(ctx);
  551. return ok;
  552. }
  553. int bn_mod_inverse_secret_prime(BIGNUM *out, const BIGNUM *a, const BIGNUM *p,
  554. BN_CTX *ctx, const BN_MONT_CTX *mont_p) {
  555. BN_CTX_start(ctx);
  556. BIGNUM *p_minus_2 = BN_CTX_get(ctx);
  557. int ok = p_minus_2 != NULL &&
  558. BN_copy(p_minus_2, p) &&
  559. BN_sub_word(p_minus_2, 2) &&
  560. BN_mod_exp_mont_consttime(out, a, p_minus_2, p, ctx, mont_p);
  561. BN_CTX_end(ctx);
  562. return ok;
  563. }