div.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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. #include <openssl/bn.h>
  57. #include <assert.h>
  58. #include <limits.h>
  59. #include <openssl/err.h>
  60. #include "internal.h"
  61. #if !defined(BN_ULLONG)
  62. // bn_div_words divides a double-width |h|,|l| by |d| and returns the result,
  63. // which must fit in a |BN_ULONG|.
  64. static BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d) {
  65. BN_ULONG dh, dl, q, ret = 0, th, tl, t;
  66. int i, count = 2;
  67. if (d == 0) {
  68. return BN_MASK2;
  69. }
  70. i = BN_num_bits_word(d);
  71. assert((i == BN_BITS2) || (h <= (BN_ULONG)1 << i));
  72. i = BN_BITS2 - i;
  73. if (h >= d) {
  74. h -= d;
  75. }
  76. if (i) {
  77. d <<= i;
  78. h = (h << i) | (l >> (BN_BITS2 - i));
  79. l <<= i;
  80. }
  81. dh = (d & BN_MASK2h) >> BN_BITS4;
  82. dl = (d & BN_MASK2l);
  83. for (;;) {
  84. if ((h >> BN_BITS4) == dh) {
  85. q = BN_MASK2l;
  86. } else {
  87. q = h / dh;
  88. }
  89. th = q * dh;
  90. tl = dl * q;
  91. for (;;) {
  92. t = h - th;
  93. if ((t & BN_MASK2h) ||
  94. ((tl) <= ((t << BN_BITS4) | ((l & BN_MASK2h) >> BN_BITS4)))) {
  95. break;
  96. }
  97. q--;
  98. th -= dh;
  99. tl -= dl;
  100. }
  101. t = (tl >> BN_BITS4);
  102. tl = (tl << BN_BITS4) & BN_MASK2h;
  103. th += t;
  104. if (l < tl) {
  105. th++;
  106. }
  107. l -= tl;
  108. if (h < th) {
  109. h += d;
  110. q--;
  111. }
  112. h -= th;
  113. if (--count == 0) {
  114. break;
  115. }
  116. ret = q << BN_BITS4;
  117. h = (h << BN_BITS4) | (l >> BN_BITS4);
  118. l = (l & BN_MASK2l) << BN_BITS4;
  119. }
  120. ret |= q;
  121. return ret;
  122. }
  123. #endif // !defined(BN_ULLONG)
  124. static inline void bn_div_rem_words(BN_ULONG *quotient_out, BN_ULONG *rem_out,
  125. BN_ULONG n0, BN_ULONG n1, BN_ULONG d0) {
  126. // GCC and Clang generate function calls to |__udivdi3| and |__umoddi3| when
  127. // the |BN_ULLONG|-based C code is used.
  128. //
  129. // GCC bugs:
  130. // * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=14224
  131. // * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=43721
  132. // * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54183
  133. // * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58897
  134. // * https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65668
  135. //
  136. // Clang bugs:
  137. // * https://llvm.org/bugs/show_bug.cgi?id=6397
  138. // * https://llvm.org/bugs/show_bug.cgi?id=12418
  139. //
  140. // These issues aren't specific to x86 and x86_64, so it might be worthwhile
  141. // to add more assembly language implementations.
  142. #if !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86) && defined(__GNUC__)
  143. __asm__ volatile (
  144. "divl %4"
  145. : "=a"(*quotient_out), "=d"(*rem_out)
  146. : "a"(n1), "d"(n0), "rm"(d0)
  147. : "cc" );
  148. #elif !defined(OPENSSL_NO_ASM) && defined(OPENSSL_X86_64) && defined(__GNUC__)
  149. __asm__ volatile (
  150. "divq %4"
  151. : "=a"(*quotient_out), "=d"(*rem_out)
  152. : "a"(n1), "d"(n0), "rm"(d0)
  153. : "cc" );
  154. #else
  155. #if defined(BN_ULLONG)
  156. BN_ULLONG n = (((BN_ULLONG)n0) << BN_BITS2) | n1;
  157. *quotient_out = (BN_ULONG)(n / d0);
  158. #else
  159. *quotient_out = bn_div_words(n0, n1, d0);
  160. #endif
  161. *rem_out = n1 - (*quotient_out * d0);
  162. #endif
  163. }
  164. // BN_div computes "quotient := numerator / divisor", rounding towards zero,
  165. // and sets up |rem| such that "quotient * divisor + rem = numerator" holds.
  166. //
  167. // Thus:
  168. //
  169. // quotient->neg == numerator->neg ^ divisor->neg
  170. // (unless the result is zero)
  171. // rem->neg == numerator->neg
  172. // (unless the remainder is zero)
  173. //
  174. // If |quotient| or |rem| is NULL, the respective value is not returned.
  175. //
  176. // This was specifically designed to contain fewer branches that may leak
  177. // sensitive information; see "New Branch Prediction Vulnerabilities in OpenSSL
  178. // and Necessary Software Countermeasures" by Onur Acıçmez, Shay Gueron, and
  179. // Jean-Pierre Seifert.
  180. int BN_div(BIGNUM *quotient, BIGNUM *rem, const BIGNUM *numerator,
  181. const BIGNUM *divisor, BN_CTX *ctx) {
  182. int norm_shift, loop;
  183. BIGNUM wnum;
  184. BN_ULONG *resp, *wnump;
  185. BN_ULONG d0, d1;
  186. int num_n, div_n;
  187. // Invalid zero-padding would have particularly bad consequences
  188. // so don't just rely on bn_check_top() here
  189. if ((numerator->top > 0 && numerator->d[numerator->top - 1] == 0) ||
  190. (divisor->top > 0 && divisor->d[divisor->top - 1] == 0)) {
  191. OPENSSL_PUT_ERROR(BN, BN_R_NOT_INITIALIZED);
  192. return 0;
  193. }
  194. if (BN_is_zero(divisor)) {
  195. OPENSSL_PUT_ERROR(BN, BN_R_DIV_BY_ZERO);
  196. return 0;
  197. }
  198. BN_CTX_start(ctx);
  199. BIGNUM *tmp = BN_CTX_get(ctx);
  200. BIGNUM *snum = BN_CTX_get(ctx);
  201. BIGNUM *sdiv = BN_CTX_get(ctx);
  202. BIGNUM *res = NULL;
  203. if (quotient == NULL) {
  204. res = BN_CTX_get(ctx);
  205. } else {
  206. res = quotient;
  207. }
  208. if (sdiv == NULL || res == NULL) {
  209. goto err;
  210. }
  211. // First we normalise the numbers
  212. norm_shift = BN_BITS2 - (BN_num_bits(divisor) % BN_BITS2);
  213. if (!BN_lshift(sdiv, divisor, norm_shift)) {
  214. goto err;
  215. }
  216. sdiv->neg = 0;
  217. norm_shift += BN_BITS2;
  218. if (!BN_lshift(snum, numerator, norm_shift)) {
  219. goto err;
  220. }
  221. snum->neg = 0;
  222. // Since we don't want to have special-case logic for the case where snum is
  223. // larger than sdiv, we pad snum with enough zeroes without changing its
  224. // value.
  225. if (snum->top <= sdiv->top + 1) {
  226. if (!bn_wexpand(snum, sdiv->top + 2)) {
  227. goto err;
  228. }
  229. for (int i = snum->top; i < sdiv->top + 2; i++) {
  230. snum->d[i] = 0;
  231. }
  232. snum->top = sdiv->top + 2;
  233. } else {
  234. if (!bn_wexpand(snum, snum->top + 1)) {
  235. goto err;
  236. }
  237. snum->d[snum->top] = 0;
  238. snum->top++;
  239. }
  240. div_n = sdiv->top;
  241. num_n = snum->top;
  242. loop = num_n - div_n;
  243. // Lets setup a 'window' into snum
  244. // This is the part that corresponds to the current
  245. // 'area' being divided
  246. wnum.neg = 0;
  247. wnum.d = &(snum->d[loop]);
  248. wnum.top = div_n;
  249. // only needed when BN_ucmp messes up the values between top and max
  250. wnum.dmax = snum->dmax - loop; // so we don't step out of bounds
  251. // Get the top 2 words of sdiv
  252. // div_n=sdiv->top;
  253. d0 = sdiv->d[div_n - 1];
  254. d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];
  255. // pointer to the 'top' of snum
  256. wnump = &(snum->d[num_n - 1]);
  257. // Setup to 'res'
  258. res->neg = (numerator->neg ^ divisor->neg);
  259. if (!bn_wexpand(res, loop + 1)) {
  260. goto err;
  261. }
  262. res->top = loop - 1;
  263. resp = &(res->d[loop - 1]);
  264. // space for temp
  265. if (!bn_wexpand(tmp, div_n + 1)) {
  266. goto err;
  267. }
  268. // if res->top == 0 then clear the neg value otherwise decrease
  269. // the resp pointer
  270. if (res->top == 0) {
  271. res->neg = 0;
  272. } else {
  273. resp--;
  274. }
  275. for (int i = 0; i < loop - 1; i++, wnump--, resp--) {
  276. BN_ULONG q, l0;
  277. // the first part of the loop uses the top two words of snum and sdiv to
  278. // calculate a BN_ULONG q such that | wnum - sdiv * q | < sdiv
  279. BN_ULONG n0, n1, rm = 0;
  280. n0 = wnump[0];
  281. n1 = wnump[-1];
  282. if (n0 == d0) {
  283. q = BN_MASK2;
  284. } else {
  285. // n0 < d0
  286. bn_div_rem_words(&q, &rm, n0, n1, d0);
  287. #ifdef BN_ULLONG
  288. BN_ULLONG t2 = (BN_ULLONG)d1 * q;
  289. for (;;) {
  290. if (t2 <= ((((BN_ULLONG)rm) << BN_BITS2) | wnump[-2])) {
  291. break;
  292. }
  293. q--;
  294. rm += d0;
  295. if (rm < d0) {
  296. break; // don't let rm overflow
  297. }
  298. t2 -= d1;
  299. }
  300. #else // !BN_ULLONG
  301. BN_ULONG t2l, t2h;
  302. BN_UMULT_LOHI(t2l, t2h, d1, q);
  303. for (;;) {
  304. if (t2h < rm ||
  305. (t2h == rm && t2l <= wnump[-2])) {
  306. break;
  307. }
  308. q--;
  309. rm += d0;
  310. if (rm < d0) {
  311. break; // don't let rm overflow
  312. }
  313. if (t2l < d1) {
  314. t2h--;
  315. }
  316. t2l -= d1;
  317. }
  318. #endif // !BN_ULLONG
  319. }
  320. l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);
  321. tmp->d[div_n] = l0;
  322. wnum.d--;
  323. // ingore top values of the bignums just sub the two
  324. // BN_ULONG arrays with bn_sub_words
  325. if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {
  326. // Note: As we have considered only the leading
  327. // two BN_ULONGs in the calculation of q, sdiv * q
  328. // might be greater than wnum (but then (q-1) * sdiv
  329. // is less or equal than wnum)
  330. q--;
  331. if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n)) {
  332. // we can't have an overflow here (assuming
  333. // that q != 0, but if q == 0 then tmp is
  334. // zero anyway)
  335. (*wnump)++;
  336. }
  337. }
  338. // store part of the result
  339. *resp = q;
  340. }
  341. bn_correct_top(snum);
  342. if (rem != NULL) {
  343. // Keep a copy of the neg flag in numerator because if |rem| == |numerator|
  344. // |BN_rshift| will overwrite it.
  345. int neg = numerator->neg;
  346. if (!BN_rshift(rem, snum, norm_shift)) {
  347. goto err;
  348. }
  349. if (!BN_is_zero(rem)) {
  350. rem->neg = neg;
  351. }
  352. }
  353. bn_correct_top(res);
  354. BN_CTX_end(ctx);
  355. return 1;
  356. err:
  357. BN_CTX_end(ctx);
  358. return 0;
  359. }
  360. int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx) {
  361. if (!(BN_mod(r, m, d, ctx))) {
  362. return 0;
  363. }
  364. if (!r->neg) {
  365. return 1;
  366. }
  367. // now -|d| < r < 0, so we have to set r := r + |d|.
  368. return (d->neg ? BN_sub : BN_add)(r, r, d);
  369. }
  370. int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
  371. BN_CTX *ctx) {
  372. if (!BN_add(r, a, b)) {
  373. return 0;
  374. }
  375. return BN_nnmod(r, r, m, ctx);
  376. }
  377. int BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  378. const BIGNUM *m) {
  379. if (!BN_uadd(r, a, b)) {
  380. return 0;
  381. }
  382. if (BN_ucmp(r, m) >= 0) {
  383. return BN_usub(r, r, m);
  384. }
  385. return 1;
  386. }
  387. int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
  388. BN_CTX *ctx) {
  389. if (!BN_sub(r, a, b)) {
  390. return 0;
  391. }
  392. return BN_nnmod(r, r, m, ctx);
  393. }
  394. // BN_mod_sub variant that may be used if both a and b are non-negative
  395. // and less than m
  396. int BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  397. const BIGNUM *m) {
  398. if (!BN_sub(r, a, b)) {
  399. return 0;
  400. }
  401. if (r->neg) {
  402. return BN_add(r, r, m);
  403. }
  404. return 1;
  405. }
  406. int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
  407. BN_CTX *ctx) {
  408. BIGNUM *t;
  409. int ret = 0;
  410. BN_CTX_start(ctx);
  411. t = BN_CTX_get(ctx);
  412. if (t == NULL) {
  413. goto err;
  414. }
  415. if (a == b) {
  416. if (!BN_sqr(t, a, ctx)) {
  417. goto err;
  418. }
  419. } else {
  420. if (!BN_mul(t, a, b, ctx)) {
  421. goto err;
  422. }
  423. }
  424. if (!BN_nnmod(r, t, m, ctx)) {
  425. goto err;
  426. }
  427. ret = 1;
  428. err:
  429. BN_CTX_end(ctx);
  430. return ret;
  431. }
  432. int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx) {
  433. if (!BN_sqr(r, a, ctx)) {
  434. return 0;
  435. }
  436. // r->neg == 0, thus we don't need BN_nnmod
  437. return BN_mod(r, r, m, ctx);
  438. }
  439. int BN_mod_lshift(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m,
  440. BN_CTX *ctx) {
  441. BIGNUM *abs_m = NULL;
  442. int ret;
  443. if (!BN_nnmod(r, a, m, ctx)) {
  444. return 0;
  445. }
  446. if (m->neg) {
  447. abs_m = BN_dup(m);
  448. if (abs_m == NULL) {
  449. return 0;
  450. }
  451. abs_m->neg = 0;
  452. }
  453. ret = BN_mod_lshift_quick(r, r, n, (abs_m ? abs_m : m));
  454. BN_free(abs_m);
  455. return ret;
  456. }
  457. int BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m) {
  458. if (r != a) {
  459. if (BN_copy(r, a) == NULL) {
  460. return 0;
  461. }
  462. }
  463. while (n > 0) {
  464. int max_shift;
  465. // 0 < r < m
  466. max_shift = BN_num_bits(m) - BN_num_bits(r);
  467. // max_shift >= 0
  468. if (max_shift < 0) {
  469. OPENSSL_PUT_ERROR(BN, BN_R_INPUT_NOT_REDUCED);
  470. return 0;
  471. }
  472. if (max_shift > n) {
  473. max_shift = n;
  474. }
  475. if (max_shift) {
  476. if (!BN_lshift(r, r, max_shift)) {
  477. return 0;
  478. }
  479. n -= max_shift;
  480. } else {
  481. if (!BN_lshift1(r, r)) {
  482. return 0;
  483. }
  484. --n;
  485. }
  486. // BN_num_bits(r) <= BN_num_bits(m)
  487. if (BN_cmp(r, m) >= 0) {
  488. if (!BN_sub(r, r, m)) {
  489. return 0;
  490. }
  491. }
  492. }
  493. return 1;
  494. }
  495. int BN_mod_lshift1(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx) {
  496. if (!BN_lshift1(r, a)) {
  497. return 0;
  498. }
  499. return BN_nnmod(r, r, m, ctx);
  500. }
  501. int BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *m) {
  502. if (!BN_lshift1(r, a)) {
  503. return 0;
  504. }
  505. if (BN_cmp(r, m) >= 0) {
  506. return BN_sub(r, r, m);
  507. }
  508. return 1;
  509. }
  510. BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w) {
  511. BN_ULONG ret = 0;
  512. int i, j;
  513. if (!w) {
  514. // actually this an error (division by zero)
  515. return (BN_ULONG) - 1;
  516. }
  517. if (a->top == 0) {
  518. return 0;
  519. }
  520. // normalize input for |bn_div_rem_words|.
  521. j = BN_BITS2 - BN_num_bits_word(w);
  522. w <<= j;
  523. if (!BN_lshift(a, a, j)) {
  524. return (BN_ULONG) - 1;
  525. }
  526. for (i = a->top - 1; i >= 0; i--) {
  527. BN_ULONG l = a->d[i];
  528. BN_ULONG d;
  529. BN_ULONG unused_rem;
  530. bn_div_rem_words(&d, &unused_rem, ret, l, w);
  531. ret = l - (d * w);
  532. a->d[i] = d;
  533. }
  534. if ((a->top > 0) && (a->d[a->top - 1] == 0)) {
  535. a->top--;
  536. }
  537. if (a->top == 0) {
  538. a->neg = 0;
  539. }
  540. ret >>= j;
  541. return ret;
  542. }
  543. BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w) {
  544. #ifndef BN_ULLONG
  545. BN_ULONG ret = 0;
  546. #else
  547. BN_ULLONG ret = 0;
  548. #endif
  549. int i;
  550. if (w == 0) {
  551. return (BN_ULONG) -1;
  552. }
  553. #ifndef BN_ULLONG
  554. // If |w| is too long and we don't have |BN_ULLONG| then we need to fall back
  555. // to using |BN_div_word|.
  556. if (w > ((BN_ULONG)1 << BN_BITS4)) {
  557. BIGNUM *tmp = BN_dup(a);
  558. if (tmp == NULL) {
  559. return (BN_ULONG)-1;
  560. }
  561. ret = BN_div_word(tmp, w);
  562. BN_free(tmp);
  563. return ret;
  564. }
  565. #endif
  566. for (i = a->top - 1; i >= 0; i--) {
  567. #ifndef BN_ULLONG
  568. ret = ((ret << BN_BITS4) | ((a->d[i] >> BN_BITS4) & BN_MASK2l)) % w;
  569. ret = ((ret << BN_BITS4) | (a->d[i] & BN_MASK2l)) % w;
  570. #else
  571. ret = (BN_ULLONG)(((ret << (BN_ULLONG)BN_BITS2) | a->d[i]) % (BN_ULLONG)w);
  572. #endif
  573. }
  574. return (BN_ULONG)ret;
  575. }
  576. int BN_mod_pow2(BIGNUM *r, const BIGNUM *a, size_t e) {
  577. if (e == 0 || a->top == 0) {
  578. BN_zero(r);
  579. return 1;
  580. }
  581. size_t num_words = 1 + ((e - 1) / BN_BITS2);
  582. // If |a| definitely has less than |e| bits, just BN_copy.
  583. if ((size_t) a->top < num_words) {
  584. return BN_copy(r, a) != NULL;
  585. }
  586. // Otherwise, first make sure we have enough space in |r|.
  587. // Note that this will fail if num_words > INT_MAX.
  588. if (!bn_wexpand(r, num_words)) {
  589. return 0;
  590. }
  591. // Copy the content of |a| into |r|.
  592. OPENSSL_memcpy(r->d, a->d, num_words * sizeof(BN_ULONG));
  593. // If |e| isn't word-aligned, we have to mask off some of our bits.
  594. size_t top_word_exponent = e % (sizeof(BN_ULONG) * 8);
  595. if (top_word_exponent != 0) {
  596. r->d[num_words - 1] &= (((BN_ULONG) 1) << top_word_exponent) - 1;
  597. }
  598. // Fill in the remaining fields of |r|.
  599. r->neg = a->neg;
  600. r->top = (int) num_words;
  601. bn_correct_top(r);
  602. return 1;
  603. }
  604. int BN_nnmod_pow2(BIGNUM *r, const BIGNUM *a, size_t e) {
  605. if (!BN_mod_pow2(r, a, e)) {
  606. return 0;
  607. }
  608. // If the returned value was non-negative, we're done.
  609. if (BN_is_zero(r) || !r->neg) {
  610. return 1;
  611. }
  612. size_t num_words = 1 + (e - 1) / BN_BITS2;
  613. // Expand |r| to the size of our modulus.
  614. if (!bn_wexpand(r, num_words)) {
  615. return 0;
  616. }
  617. // Clear the upper words of |r|.
  618. OPENSSL_memset(&r->d[r->top], 0, (num_words - r->top) * BN_BYTES);
  619. // Set parameters of |r|.
  620. r->neg = 0;
  621. r->top = (int) num_words;
  622. // Now, invert every word. The idea here is that we want to compute 2^e-|x|,
  623. // which is actually equivalent to the twos-complement representation of |x|
  624. // in |e| bits, which is -x = ~x + 1.
  625. for (int i = 0; i < r->top; i++) {
  626. r->d[i] = ~r->d[i];
  627. }
  628. // If our exponent doesn't span the top word, we have to mask the rest.
  629. size_t top_word_exponent = e % BN_BITS2;
  630. if (top_word_exponent != 0) {
  631. r->d[r->top - 1] &= (((BN_ULONG) 1) << top_word_exponent) - 1;
  632. }
  633. // Keep the correct_top invariant for BN_add.
  634. bn_correct_top(r);
  635. // Finally, add one, for the reason described above.
  636. return BN_add(r, r, BN_value_one());
  637. }