bn_gcd.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "internal/cryptlib.h"
  10. #include "bn_local.h"
  11. /* solves ax == 1 (mod n) */
  12. static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,
  13. const BIGNUM *a, const BIGNUM *n,
  14. BN_CTX *ctx);
  15. BIGNUM *BN_mod_inverse(BIGNUM *in,
  16. const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)
  17. {
  18. BIGNUM *rv;
  19. int noinv;
  20. rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);
  21. if (noinv)
  22. BNerr(BN_F_BN_MOD_INVERSE, BN_R_NO_INVERSE);
  23. return rv;
  24. }
  25. BIGNUM *int_bn_mod_inverse(BIGNUM *in,
  26. const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,
  27. int *pnoinv)
  28. {
  29. BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;
  30. BIGNUM *ret = NULL;
  31. int sign;
  32. /* This is invalid input so we don't worry about constant time here */
  33. if (BN_abs_is_word(n, 1) || BN_is_zero(n)) {
  34. if (pnoinv != NULL)
  35. *pnoinv = 1;
  36. return NULL;
  37. }
  38. if (pnoinv != NULL)
  39. *pnoinv = 0;
  40. if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)
  41. || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {
  42. return BN_mod_inverse_no_branch(in, a, n, ctx);
  43. }
  44. bn_check_top(a);
  45. bn_check_top(n);
  46. BN_CTX_start(ctx);
  47. A = BN_CTX_get(ctx);
  48. B = BN_CTX_get(ctx);
  49. X = BN_CTX_get(ctx);
  50. D = BN_CTX_get(ctx);
  51. M = BN_CTX_get(ctx);
  52. Y = BN_CTX_get(ctx);
  53. T = BN_CTX_get(ctx);
  54. if (T == NULL)
  55. goto err;
  56. if (in == NULL)
  57. R = BN_new();
  58. else
  59. R = in;
  60. if (R == NULL)
  61. goto err;
  62. BN_one(X);
  63. BN_zero(Y);
  64. if (BN_copy(B, a) == NULL)
  65. goto err;
  66. if (BN_copy(A, n) == NULL)
  67. goto err;
  68. A->neg = 0;
  69. if (B->neg || (BN_ucmp(B, A) >= 0)) {
  70. if (!BN_nnmod(B, B, A, ctx))
  71. goto err;
  72. }
  73. sign = -1;
  74. /*-
  75. * From B = a mod |n|, A = |n| it follows that
  76. *
  77. * 0 <= B < A,
  78. * -sign*X*a == B (mod |n|),
  79. * sign*Y*a == A (mod |n|).
  80. */
  81. if (BN_is_odd(n) && (BN_num_bits(n) <= 2048)) {
  82. /*
  83. * Binary inversion algorithm; requires odd modulus. This is faster
  84. * than the general algorithm if the modulus is sufficiently small
  85. * (about 400 .. 500 bits on 32-bit systems, but much more on 64-bit
  86. * systems)
  87. */
  88. int shift;
  89. while (!BN_is_zero(B)) {
  90. /*-
  91. * 0 < B < |n|,
  92. * 0 < A <= |n|,
  93. * (1) -sign*X*a == B (mod |n|),
  94. * (2) sign*Y*a == A (mod |n|)
  95. */
  96. /*
  97. * Now divide B by the maximum possible power of two in the
  98. * integers, and divide X by the same value mod |n|. When we're
  99. * done, (1) still holds.
  100. */
  101. shift = 0;
  102. while (!BN_is_bit_set(B, shift)) { /* note that 0 < B */
  103. shift++;
  104. if (BN_is_odd(X)) {
  105. if (!BN_uadd(X, X, n))
  106. goto err;
  107. }
  108. /*
  109. * now X is even, so we can easily divide it by two
  110. */
  111. if (!BN_rshift1(X, X))
  112. goto err;
  113. }
  114. if (shift > 0) {
  115. if (!BN_rshift(B, B, shift))
  116. goto err;
  117. }
  118. /*
  119. * Same for A and Y. Afterwards, (2) still holds.
  120. */
  121. shift = 0;
  122. while (!BN_is_bit_set(A, shift)) { /* note that 0 < A */
  123. shift++;
  124. if (BN_is_odd(Y)) {
  125. if (!BN_uadd(Y, Y, n))
  126. goto err;
  127. }
  128. /* now Y is even */
  129. if (!BN_rshift1(Y, Y))
  130. goto err;
  131. }
  132. if (shift > 0) {
  133. if (!BN_rshift(A, A, shift))
  134. goto err;
  135. }
  136. /*-
  137. * We still have (1) and (2).
  138. * Both A and B are odd.
  139. * The following computations ensure that
  140. *
  141. * 0 <= B < |n|,
  142. * 0 < A < |n|,
  143. * (1) -sign*X*a == B (mod |n|),
  144. * (2) sign*Y*a == A (mod |n|),
  145. *
  146. * and that either A or B is even in the next iteration.
  147. */
  148. if (BN_ucmp(B, A) >= 0) {
  149. /* -sign*(X + Y)*a == B - A (mod |n|) */
  150. if (!BN_uadd(X, X, Y))
  151. goto err;
  152. /*
  153. * NB: we could use BN_mod_add_quick(X, X, Y, n), but that
  154. * actually makes the algorithm slower
  155. */
  156. if (!BN_usub(B, B, A))
  157. goto err;
  158. } else {
  159. /* sign*(X + Y)*a == A - B (mod |n|) */
  160. if (!BN_uadd(Y, Y, X))
  161. goto err;
  162. /*
  163. * as above, BN_mod_add_quick(Y, Y, X, n) would slow things down
  164. */
  165. if (!BN_usub(A, A, B))
  166. goto err;
  167. }
  168. }
  169. } else {
  170. /* general inversion algorithm */
  171. while (!BN_is_zero(B)) {
  172. BIGNUM *tmp;
  173. /*-
  174. * 0 < B < A,
  175. * (*) -sign*X*a == B (mod |n|),
  176. * sign*Y*a == A (mod |n|)
  177. */
  178. /* (D, M) := (A/B, A%B) ... */
  179. if (BN_num_bits(A) == BN_num_bits(B)) {
  180. if (!BN_one(D))
  181. goto err;
  182. if (!BN_sub(M, A, B))
  183. goto err;
  184. } else if (BN_num_bits(A) == BN_num_bits(B) + 1) {
  185. /* A/B is 1, 2, or 3 */
  186. if (!BN_lshift1(T, B))
  187. goto err;
  188. if (BN_ucmp(A, T) < 0) {
  189. /* A < 2*B, so D=1 */
  190. if (!BN_one(D))
  191. goto err;
  192. if (!BN_sub(M, A, B))
  193. goto err;
  194. } else {
  195. /* A >= 2*B, so D=2 or D=3 */
  196. if (!BN_sub(M, A, T))
  197. goto err;
  198. if (!BN_add(D, T, B))
  199. goto err; /* use D (:= 3*B) as temp */
  200. if (BN_ucmp(A, D) < 0) {
  201. /* A < 3*B, so D=2 */
  202. if (!BN_set_word(D, 2))
  203. goto err;
  204. /*
  205. * M (= A - 2*B) already has the correct value
  206. */
  207. } else {
  208. /* only D=3 remains */
  209. if (!BN_set_word(D, 3))
  210. goto err;
  211. /*
  212. * currently M = A - 2*B, but we need M = A - 3*B
  213. */
  214. if (!BN_sub(M, M, B))
  215. goto err;
  216. }
  217. }
  218. } else {
  219. if (!BN_div(D, M, A, B, ctx))
  220. goto err;
  221. }
  222. /*-
  223. * Now
  224. * A = D*B + M;
  225. * thus we have
  226. * (**) sign*Y*a == D*B + M (mod |n|).
  227. */
  228. tmp = A; /* keep the BIGNUM object, the value does not matter */
  229. /* (A, B) := (B, A mod B) ... */
  230. A = B;
  231. B = M;
  232. /* ... so we have 0 <= B < A again */
  233. /*-
  234. * Since the former M is now B and the former B is now A,
  235. * (**) translates into
  236. * sign*Y*a == D*A + B (mod |n|),
  237. * i.e.
  238. * sign*Y*a - D*A == B (mod |n|).
  239. * Similarly, (*) translates into
  240. * -sign*X*a == A (mod |n|).
  241. *
  242. * Thus,
  243. * sign*Y*a + D*sign*X*a == B (mod |n|),
  244. * i.e.
  245. * sign*(Y + D*X)*a == B (mod |n|).
  246. *
  247. * So if we set (X, Y, sign) := (Y + D*X, X, -sign), we arrive back at
  248. * -sign*X*a == B (mod |n|),
  249. * sign*Y*a == A (mod |n|).
  250. * Note that X and Y stay non-negative all the time.
  251. */
  252. /*
  253. * most of the time D is very small, so we can optimize tmp := D*X+Y
  254. */
  255. if (BN_is_one(D)) {
  256. if (!BN_add(tmp, X, Y))
  257. goto err;
  258. } else {
  259. if (BN_is_word(D, 2)) {
  260. if (!BN_lshift1(tmp, X))
  261. goto err;
  262. } else if (BN_is_word(D, 4)) {
  263. if (!BN_lshift(tmp, X, 2))
  264. goto err;
  265. } else if (D->top == 1) {
  266. if (!BN_copy(tmp, X))
  267. goto err;
  268. if (!BN_mul_word(tmp, D->d[0]))
  269. goto err;
  270. } else {
  271. if (!BN_mul(tmp, D, X, ctx))
  272. goto err;
  273. }
  274. if (!BN_add(tmp, tmp, Y))
  275. goto err;
  276. }
  277. M = Y; /* keep the BIGNUM object, the value does not matter */
  278. Y = X;
  279. X = tmp;
  280. sign = -sign;
  281. }
  282. }
  283. /*-
  284. * The while loop (Euclid's algorithm) ends when
  285. * A == gcd(a,n);
  286. * we have
  287. * sign*Y*a == A (mod |n|),
  288. * where Y is non-negative.
  289. */
  290. if (sign < 0) {
  291. if (!BN_sub(Y, n, Y))
  292. goto err;
  293. }
  294. /* Now Y*a == A (mod |n|). */
  295. if (BN_is_one(A)) {
  296. /* Y*a == 1 (mod |n|) */
  297. if (!Y->neg && BN_ucmp(Y, n) < 0) {
  298. if (!BN_copy(R, Y))
  299. goto err;
  300. } else {
  301. if (!BN_nnmod(R, Y, n, ctx))
  302. goto err;
  303. }
  304. } else {
  305. if (pnoinv)
  306. *pnoinv = 1;
  307. goto err;
  308. }
  309. ret = R;
  310. err:
  311. if ((ret == NULL) && (in == NULL))
  312. BN_free(R);
  313. BN_CTX_end(ctx);
  314. bn_check_top(ret);
  315. return ret;
  316. }
  317. /*
  318. * BN_mod_inverse_no_branch is a special version of BN_mod_inverse. It does
  319. * not contain branches that may leak sensitive information.
  320. */
  321. static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,
  322. const BIGNUM *a, const BIGNUM *n,
  323. BN_CTX *ctx)
  324. {
  325. BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;
  326. BIGNUM *ret = NULL;
  327. int sign;
  328. bn_check_top(a);
  329. bn_check_top(n);
  330. BN_CTX_start(ctx);
  331. A = BN_CTX_get(ctx);
  332. B = BN_CTX_get(ctx);
  333. X = BN_CTX_get(ctx);
  334. D = BN_CTX_get(ctx);
  335. M = BN_CTX_get(ctx);
  336. Y = BN_CTX_get(ctx);
  337. T = BN_CTX_get(ctx);
  338. if (T == NULL)
  339. goto err;
  340. if (in == NULL)
  341. R = BN_new();
  342. else
  343. R = in;
  344. if (R == NULL)
  345. goto err;
  346. BN_one(X);
  347. BN_zero(Y);
  348. if (BN_copy(B, a) == NULL)
  349. goto err;
  350. if (BN_copy(A, n) == NULL)
  351. goto err;
  352. A->neg = 0;
  353. if (B->neg || (BN_ucmp(B, A) >= 0)) {
  354. /*
  355. * Turn BN_FLG_CONSTTIME flag on, so that when BN_div is invoked,
  356. * BN_div_no_branch will be called eventually.
  357. */
  358. {
  359. BIGNUM local_B;
  360. bn_init(&local_B);
  361. BN_with_flags(&local_B, B, BN_FLG_CONSTTIME);
  362. if (!BN_nnmod(B, &local_B, A, ctx))
  363. goto err;
  364. /* Ensure local_B goes out of scope before any further use of B */
  365. }
  366. }
  367. sign = -1;
  368. /*-
  369. * From B = a mod |n|, A = |n| it follows that
  370. *
  371. * 0 <= B < A,
  372. * -sign*X*a == B (mod |n|),
  373. * sign*Y*a == A (mod |n|).
  374. */
  375. while (!BN_is_zero(B)) {
  376. BIGNUM *tmp;
  377. /*-
  378. * 0 < B < A,
  379. * (*) -sign*X*a == B (mod |n|),
  380. * sign*Y*a == A (mod |n|)
  381. */
  382. /*
  383. * Turn BN_FLG_CONSTTIME flag on, so that when BN_div is invoked,
  384. * BN_div_no_branch will be called eventually.
  385. */
  386. {
  387. BIGNUM local_A;
  388. bn_init(&local_A);
  389. BN_with_flags(&local_A, A, BN_FLG_CONSTTIME);
  390. /* (D, M) := (A/B, A%B) ... */
  391. if (!BN_div(D, M, &local_A, B, ctx))
  392. goto err;
  393. /* Ensure local_A goes out of scope before any further use of A */
  394. }
  395. /*-
  396. * Now
  397. * A = D*B + M;
  398. * thus we have
  399. * (**) sign*Y*a == D*B + M (mod |n|).
  400. */
  401. tmp = A; /* keep the BIGNUM object, the value does not
  402. * matter */
  403. /* (A, B) := (B, A mod B) ... */
  404. A = B;
  405. B = M;
  406. /* ... so we have 0 <= B < A again */
  407. /*-
  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. */
  426. if (!BN_mul(tmp, D, X, ctx))
  427. goto err;
  428. if (!BN_add(tmp, tmp, Y))
  429. goto err;
  430. M = Y; /* keep the BIGNUM object, the value does not
  431. * matter */
  432. Y = X;
  433. X = tmp;
  434. sign = -sign;
  435. }
  436. /*-
  437. * The while loop (Euclid's algorithm) ends when
  438. * A == gcd(a,n);
  439. * we have
  440. * sign*Y*a == A (mod |n|),
  441. * where Y is non-negative.
  442. */
  443. if (sign < 0) {
  444. if (!BN_sub(Y, n, Y))
  445. goto err;
  446. }
  447. /* Now Y*a == A (mod |n|). */
  448. if (BN_is_one(A)) {
  449. /* Y*a == 1 (mod |n|) */
  450. if (!Y->neg && BN_ucmp(Y, n) < 0) {
  451. if (!BN_copy(R, Y))
  452. goto err;
  453. } else {
  454. if (!BN_nnmod(R, Y, n, ctx))
  455. goto err;
  456. }
  457. } else {
  458. BNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH, BN_R_NO_INVERSE);
  459. goto err;
  460. }
  461. ret = R;
  462. err:
  463. if ((ret == NULL) && (in == NULL))
  464. BN_free(R);
  465. BN_CTX_end(ctx);
  466. bn_check_top(ret);
  467. return ret;
  468. }
  469. /*-
  470. * This function is based on the constant-time GCD work by Bernstein and Yang:
  471. * https://eprint.iacr.org/2019/266
  472. * Generalized fast GCD function to allow even inputs.
  473. * The algorithm first finds the shared powers of 2 between
  474. * the inputs, and removes them, reducing at least one of the
  475. * inputs to an odd value. Then it proceeds to calculate the GCD.
  476. * Before returning the resulting GCD, we take care of adding
  477. * back the powers of two removed at the beginning.
  478. * Note 1: we assume the bit length of both inputs is public information,
  479. * since access to top potentially leaks this information.
  480. */
  481. int BN_gcd(BIGNUM *r, const BIGNUM *in_a, const BIGNUM *in_b, BN_CTX *ctx)
  482. {
  483. BIGNUM *g, *temp = NULL;
  484. BN_ULONG mask = 0;
  485. int i, j, top, rlen, glen, m, bit = 1, delta = 1, cond = 0, shifts = 0, ret = 0;
  486. /* Note 2: zero input corner cases are not constant-time since they are
  487. * handled immediately. An attacker can run an attack under this
  488. * assumption without the need of side-channel information. */
  489. if (BN_is_zero(in_b)) {
  490. ret = BN_copy(r, in_a) != NULL;
  491. r->neg = 0;
  492. return ret;
  493. }
  494. if (BN_is_zero(in_a)) {
  495. ret = BN_copy(r, in_b) != NULL;
  496. r->neg = 0;
  497. return ret;
  498. }
  499. bn_check_top(in_a);
  500. bn_check_top(in_b);
  501. BN_CTX_start(ctx);
  502. temp = BN_CTX_get(ctx);
  503. g = BN_CTX_get(ctx);
  504. /* make r != 0, g != 0 even, so BN_rshift is not a potential nop */
  505. if (g == NULL
  506. || !BN_lshift1(g, in_b)
  507. || !BN_lshift1(r, in_a))
  508. goto err;
  509. /* find shared powers of two, i.e. "shifts" >= 1 */
  510. for (i = 0; i < r->dmax && i < g->dmax; i++) {
  511. mask = ~(r->d[i] | g->d[i]);
  512. for (j = 0; j < BN_BITS2; j++) {
  513. bit &= mask;
  514. shifts += bit;
  515. mask >>= 1;
  516. }
  517. }
  518. /* subtract shared powers of two; shifts >= 1 */
  519. if (!BN_rshift(r, r, shifts)
  520. || !BN_rshift(g, g, shifts))
  521. goto err;
  522. /* expand to biggest nword, with room for a possible extra word */
  523. top = 1 + ((r->top >= g->top) ? r->top : g->top);
  524. if (bn_wexpand(r, top) == NULL
  525. || bn_wexpand(g, top) == NULL
  526. || bn_wexpand(temp, top) == NULL)
  527. goto err;
  528. /* re arrange inputs s.t. r is odd */
  529. BN_consttime_swap((~r->d[0]) & 1, r, g, top);
  530. /* compute the number of iterations */
  531. rlen = BN_num_bits(r);
  532. glen = BN_num_bits(g);
  533. m = 4 + 3 * ((rlen >= glen) ? rlen : glen);
  534. for (i = 0; i < m; i++) {
  535. /* conditionally flip signs if delta is positive and g is odd */
  536. cond = (-delta >> (8 * sizeof(delta) - 1)) & g->d[0] & 1
  537. /* make sure g->top > 0 (i.e. if top == 0 then g == 0 always) */
  538. & (~((g->top - 1) >> (sizeof(g->top) * 8 - 1)));
  539. delta = (-cond & -delta) | ((cond - 1) & delta);
  540. r->neg ^= cond;
  541. /* swap */
  542. BN_consttime_swap(cond, r, g, top);
  543. /* elimination step */
  544. delta++;
  545. if (!BN_add(temp, g, r))
  546. goto err;
  547. BN_consttime_swap(g->d[0] & 1 /* g is odd */
  548. /* make sure g->top > 0 (i.e. if top == 0 then g == 0 always) */
  549. & (~((g->top - 1) >> (sizeof(g->top) * 8 - 1))),
  550. g, temp, top);
  551. if (!BN_rshift1(g, g))
  552. goto err;
  553. }
  554. /* remove possible negative sign */
  555. r->neg = 0;
  556. /* add powers of 2 removed, then correct the artificial shift */
  557. if (!BN_lshift(r, r, shifts)
  558. || !BN_rshift1(r, r))
  559. goto err;
  560. ret = 1;
  561. err:
  562. BN_CTX_end(ctx);
  563. bn_check_top(r);
  564. return ret;
  565. }