bn_div.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. /* crypto/bn/bn_div.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include <stdio.h>
  59. #include <openssl/bn.h>
  60. #include "cryptlib.h"
  61. #include "bn_lcl.h"
  62. /* The old slow way */
  63. #if 0
  64. int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d,
  65. BN_CTX *ctx)
  66. {
  67. int i, nm, nd;
  68. int ret = 0;
  69. BIGNUM *D;
  70. bn_check_top(m);
  71. bn_check_top(d);
  72. if (BN_is_zero(d)) {
  73. BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);
  74. return (0);
  75. }
  76. if (BN_ucmp(m, d) < 0) {
  77. if (rem != NULL) {
  78. if (BN_copy(rem, m) == NULL)
  79. return (0);
  80. }
  81. if (dv != NULL)
  82. BN_zero(dv);
  83. return (1);
  84. }
  85. BN_CTX_start(ctx);
  86. D = BN_CTX_get(ctx);
  87. if (dv == NULL)
  88. dv = BN_CTX_get(ctx);
  89. if (rem == NULL)
  90. rem = BN_CTX_get(ctx);
  91. if (D == NULL || dv == NULL || rem == NULL)
  92. goto end;
  93. nd = BN_num_bits(d);
  94. nm = BN_num_bits(m);
  95. if (BN_copy(D, d) == NULL)
  96. goto end;
  97. if (BN_copy(rem, m) == NULL)
  98. goto end;
  99. /*
  100. * The next 2 are needed so we can do a dv->d[0]|=1 later since
  101. * BN_lshift1 will only work once there is a value :-)
  102. */
  103. BN_zero(dv);
  104. if (bn_wexpand(dv, 1) == NULL)
  105. goto end;
  106. dv->top = 1;
  107. if (!BN_lshift(D, D, nm - nd))
  108. goto end;
  109. for (i = nm - nd; i >= 0; i--) {
  110. if (!BN_lshift1(dv, dv))
  111. goto end;
  112. if (BN_ucmp(rem, D) >= 0) {
  113. dv->d[0] |= 1;
  114. if (!BN_usub(rem, rem, D))
  115. goto end;
  116. }
  117. /* CAN IMPROVE (and have now :=) */
  118. if (!BN_rshift1(D, D))
  119. goto end;
  120. }
  121. rem->neg = BN_is_zero(rem) ? 0 : m->neg;
  122. dv->neg = m->neg ^ d->neg;
  123. ret = 1;
  124. end:
  125. BN_CTX_end(ctx);
  126. return (ret);
  127. }
  128. #else
  129. # if !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) \
  130. && !defined(PEDANTIC) && !defined(BN_DIV3W)
  131. # if defined(__GNUC__) && __GNUC__>=2
  132. # if defined(__i386) || defined (__i386__)
  133. /*-
  134. * There were two reasons for implementing this template:
  135. * - GNU C generates a call to a function (__udivdi3 to be exact)
  136. * in reply to ((((BN_ULLONG)n0)<<BN_BITS2)|n1)/d0 (I fail to
  137. * understand why...);
  138. * - divl doesn't only calculate quotient, but also leaves
  139. * remainder in %edx which we can definitely use here:-)
  140. *
  141. * <appro@fy.chalmers.se>
  142. */
  143. # define bn_div_words(n0,n1,d0) \
  144. ({ asm volatile ( \
  145. "divl %4" \
  146. : "=a"(q), "=d"(rem) \
  147. : "a"(n1), "d"(n0), "g"(d0) \
  148. : "cc"); \
  149. q; \
  150. })
  151. # define REMAINDER_IS_ALREADY_CALCULATED
  152. # elif defined(__x86_64) && defined(SIXTY_FOUR_BIT_LONG)
  153. /*
  154. * Same story here, but it's 128-bit by 64-bit division. Wow!
  155. * <appro@fy.chalmers.se>
  156. */
  157. # define bn_div_words(n0,n1,d0) \
  158. ({ asm volatile ( \
  159. "divq %4" \
  160. : "=a"(q), "=d"(rem) \
  161. : "a"(n1), "d"(n0), "g"(d0) \
  162. : "cc"); \
  163. q; \
  164. })
  165. # define REMAINDER_IS_ALREADY_CALCULATED
  166. # endif /* __<cpu> */
  167. # endif /* __GNUC__ */
  168. # endif /* OPENSSL_NO_ASM */
  169. /*-
  170. * BN_div[_no_branch] computes dv := num / divisor, rounding towards
  171. * zero, and sets up rm such that dv*divisor + rm = num holds.
  172. * Thus:
  173. * dv->neg == num->neg ^ divisor->neg (unless the result is zero)
  174. * rm->neg == num->neg (unless the remainder is zero)
  175. * If 'dv' or 'rm' is NULL, the respective value is not returned.
  176. */
  177. static int BN_div_no_branch(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,
  178. const BIGNUM *divisor, BN_CTX *ctx);
  179. int BN_div(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num, const BIGNUM *divisor,
  180. BN_CTX *ctx)
  181. {
  182. int norm_shift, i, loop;
  183. BIGNUM *tmp, wnum, *snum, *sdiv, *res;
  184. BN_ULONG *resp, *wnump;
  185. BN_ULONG d0, d1;
  186. int num_n, div_n;
  187. /*
  188. * Invalid zero-padding would have particularly bad consequences in the
  189. * case of 'num', so don't just rely on bn_check_top() for this one
  190. * (bn_check_top() works only for BN_DEBUG builds)
  191. */
  192. if (num->top > 0 && num->d[num->top - 1] == 0) {
  193. BNerr(BN_F_BN_DIV, BN_R_NOT_INITIALIZED);
  194. return 0;
  195. }
  196. bn_check_top(num);
  197. if ((BN_get_flags(num, BN_FLG_CONSTTIME) != 0)
  198. || (BN_get_flags(divisor, BN_FLG_CONSTTIME) != 0)) {
  199. return BN_div_no_branch(dv, rm, num, divisor, ctx);
  200. }
  201. bn_check_top(dv);
  202. bn_check_top(rm);
  203. /*- bn_check_top(num); *//*
  204. * 'num' has been checked already
  205. */
  206. bn_check_top(divisor);
  207. if (BN_is_zero(divisor)) {
  208. BNerr(BN_F_BN_DIV, BN_R_DIV_BY_ZERO);
  209. return (0);
  210. }
  211. if (BN_ucmp(num, divisor) < 0) {
  212. if (rm != NULL) {
  213. if (BN_copy(rm, num) == NULL)
  214. return (0);
  215. }
  216. if (dv != NULL)
  217. BN_zero(dv);
  218. return (1);
  219. }
  220. BN_CTX_start(ctx);
  221. tmp = BN_CTX_get(ctx);
  222. snum = BN_CTX_get(ctx);
  223. sdiv = BN_CTX_get(ctx);
  224. if (dv == NULL)
  225. res = BN_CTX_get(ctx);
  226. else
  227. res = dv;
  228. if (sdiv == NULL || res == NULL || tmp == NULL || snum == NULL)
  229. goto err;
  230. /* First we normalise the numbers */
  231. norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);
  232. if (!(BN_lshift(sdiv, divisor, norm_shift)))
  233. goto err;
  234. sdiv->neg = 0;
  235. norm_shift += BN_BITS2;
  236. if (!(BN_lshift(snum, num, norm_shift)))
  237. goto err;
  238. snum->neg = 0;
  239. div_n = sdiv->top;
  240. num_n = snum->top;
  241. loop = num_n - div_n;
  242. /*
  243. * Lets setup a 'window' into snum This is the part that corresponds to
  244. * the current 'area' being divided
  245. */
  246. wnum.neg = 0;
  247. wnum.d = &(snum->d[loop]);
  248. wnum.top = div_n;
  249. /*
  250. * only needed when BN_ucmp messes up the values between top and max
  251. */
  252. wnum.dmax = snum->dmax - loop; /* so we don't step out of bounds */
  253. /* Get the top 2 words of sdiv */
  254. /* div_n=sdiv->top; */
  255. d0 = sdiv->d[div_n - 1];
  256. d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];
  257. /* pointer to the 'top' of snum */
  258. wnump = &(snum->d[num_n - 1]);
  259. /* Setup to 'res' */
  260. res->neg = (num->neg ^ divisor->neg);
  261. if (!bn_wexpand(res, (loop + 1)))
  262. goto err;
  263. res->top = loop;
  264. resp = &(res->d[loop - 1]);
  265. /* space for temp */
  266. if (!bn_wexpand(tmp, (div_n + 1)))
  267. goto err;
  268. if (BN_ucmp(&wnum, sdiv) >= 0) {
  269. /*
  270. * If BN_DEBUG_RAND is defined BN_ucmp changes (via bn_pollute) the
  271. * const bignum arguments => clean the values between top and max
  272. * again
  273. */
  274. bn_clear_top2max(&wnum);
  275. bn_sub_words(wnum.d, wnum.d, sdiv->d, div_n);
  276. *resp = 1;
  277. } else
  278. res->top--;
  279. /*
  280. * if res->top == 0 then clear the neg value otherwise decrease the resp
  281. * pointer
  282. */
  283. if (res->top == 0)
  284. res->neg = 0;
  285. else
  286. resp--;
  287. for (i = 0; i < loop - 1; i++, wnump--, resp--) {
  288. BN_ULONG q, l0;
  289. /*
  290. * the first part of the loop uses the top two words of snum and sdiv
  291. * to calculate a BN_ULONG q such that | wnum - sdiv * q | < sdiv
  292. */
  293. # if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)
  294. BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);
  295. q = bn_div_3_words(wnump, d1, d0);
  296. # else
  297. BN_ULONG n0, n1, rem = 0;
  298. n0 = wnump[0];
  299. n1 = wnump[-1];
  300. if (n0 == d0)
  301. q = BN_MASK2;
  302. else { /* n0 < d0 */
  303. # ifdef BN_LLONG
  304. BN_ULLONG t2;
  305. # if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)
  306. q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);
  307. # else
  308. q = bn_div_words(n0, n1, d0);
  309. # ifdef BN_DEBUG_LEVITTE
  310. fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\
  311. X) -> 0x%08X\n", n0, n1, d0, q);
  312. # endif
  313. # endif
  314. # ifndef REMAINDER_IS_ALREADY_CALCULATED
  315. /*
  316. * rem doesn't have to be BN_ULLONG. The least we
  317. * know it's less that d0, isn't it?
  318. */
  319. rem = (n1 - q * d0) & BN_MASK2;
  320. # endif
  321. t2 = (BN_ULLONG) d1 *q;
  322. for (;;) {
  323. if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))
  324. break;
  325. q--;
  326. rem += d0;
  327. if (rem < d0)
  328. break; /* don't let rem overflow */
  329. t2 -= d1;
  330. }
  331. # else /* !BN_LLONG */
  332. BN_ULONG t2l, t2h;
  333. # if !defined(BN_UMULT_LOHI) && !defined(BN_UMULT_HIGH)
  334. BN_ULONG ql, qh;
  335. # endif
  336. q = bn_div_words(n0, n1, d0);
  337. # ifdef BN_DEBUG_LEVITTE
  338. fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\
  339. X) -> 0x%08X\n", n0, n1, d0, q);
  340. # endif
  341. # ifndef REMAINDER_IS_ALREADY_CALCULATED
  342. rem = (n1 - q * d0) & BN_MASK2;
  343. # endif
  344. # if defined(BN_UMULT_LOHI)
  345. BN_UMULT_LOHI(t2l, t2h, d1, q);
  346. # elif defined(BN_UMULT_HIGH)
  347. t2l = d1 * q;
  348. t2h = BN_UMULT_HIGH(d1, q);
  349. # else
  350. t2l = LBITS(d1);
  351. t2h = HBITS(d1);
  352. ql = LBITS(q);
  353. qh = HBITS(q);
  354. mul64(t2l, t2h, ql, qh); /* t2=(BN_ULLONG)d1*q; */
  355. # endif
  356. for (;;) {
  357. if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))
  358. break;
  359. q--;
  360. rem += d0;
  361. if (rem < d0)
  362. break; /* don't let rem overflow */
  363. if (t2l < d1)
  364. t2h--;
  365. t2l -= d1;
  366. }
  367. # endif /* !BN_LLONG */
  368. }
  369. # endif /* !BN_DIV3W */
  370. l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);
  371. tmp->d[div_n] = l0;
  372. wnum.d--;
  373. /*
  374. * ingore top values of the bignums just sub the two BN_ULONG arrays
  375. * with bn_sub_words
  376. */
  377. if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {
  378. /*
  379. * Note: As we have considered only the leading two BN_ULONGs in
  380. * the calculation of q, sdiv * q might be greater than wnum (but
  381. * then (q-1) * sdiv is less or equal than wnum)
  382. */
  383. q--;
  384. if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))
  385. /*
  386. * we can't have an overflow here (assuming that q != 0, but
  387. * if q == 0 then tmp is zero anyway)
  388. */
  389. (*wnump)++;
  390. }
  391. /* store part of the result */
  392. *resp = q;
  393. }
  394. bn_correct_top(snum);
  395. if (rm != NULL) {
  396. /*
  397. * Keep a copy of the neg flag in num because if rm==num BN_rshift()
  398. * will overwrite it.
  399. */
  400. int neg = num->neg;
  401. BN_rshift(rm, snum, norm_shift);
  402. if (!BN_is_zero(rm))
  403. rm->neg = neg;
  404. bn_check_top(rm);
  405. }
  406. BN_CTX_end(ctx);
  407. return (1);
  408. err:
  409. bn_check_top(rm);
  410. BN_CTX_end(ctx);
  411. return (0);
  412. }
  413. /*
  414. * BN_div_no_branch is a special version of BN_div. It does not contain
  415. * branches that may leak sensitive information.
  416. */
  417. static int BN_div_no_branch(BIGNUM *dv, BIGNUM *rm, const BIGNUM *num,
  418. const BIGNUM *divisor, BN_CTX *ctx)
  419. {
  420. int norm_shift, i, loop;
  421. BIGNUM *tmp, wnum, *snum, *sdiv, *res;
  422. BN_ULONG *resp, *wnump;
  423. BN_ULONG d0, d1;
  424. int num_n, div_n;
  425. bn_check_top(dv);
  426. bn_check_top(rm);
  427. /*- bn_check_top(num); *//*
  428. * 'num' has been checked in BN_div()
  429. */
  430. bn_check_top(divisor);
  431. if (BN_is_zero(divisor)) {
  432. BNerr(BN_F_BN_DIV_NO_BRANCH, BN_R_DIV_BY_ZERO);
  433. return (0);
  434. }
  435. BN_CTX_start(ctx);
  436. tmp = BN_CTX_get(ctx);
  437. snum = BN_CTX_get(ctx);
  438. sdiv = BN_CTX_get(ctx);
  439. if (dv == NULL)
  440. res = BN_CTX_get(ctx);
  441. else
  442. res = dv;
  443. if (sdiv == NULL || res == NULL)
  444. goto err;
  445. /* First we normalise the numbers */
  446. norm_shift = BN_BITS2 - ((BN_num_bits(divisor)) % BN_BITS2);
  447. if (!(BN_lshift(sdiv, divisor, norm_shift)))
  448. goto err;
  449. sdiv->neg = 0;
  450. norm_shift += BN_BITS2;
  451. if (!(BN_lshift(snum, num, norm_shift)))
  452. goto err;
  453. snum->neg = 0;
  454. /*
  455. * Since we don't know whether snum is larger than sdiv, we pad snum with
  456. * enough zeroes without changing its value.
  457. */
  458. if (snum->top <= sdiv->top + 1) {
  459. if (bn_wexpand(snum, sdiv->top + 2) == NULL)
  460. goto err;
  461. for (i = snum->top; i < sdiv->top + 2; i++)
  462. snum->d[i] = 0;
  463. snum->top = sdiv->top + 2;
  464. } else {
  465. if (bn_wexpand(snum, snum->top + 1) == NULL)
  466. goto err;
  467. snum->d[snum->top] = 0;
  468. snum->top++;
  469. }
  470. div_n = sdiv->top;
  471. num_n = snum->top;
  472. loop = num_n - div_n;
  473. /*
  474. * Lets setup a 'window' into snum This is the part that corresponds to
  475. * the current 'area' being divided
  476. */
  477. wnum.neg = 0;
  478. wnum.d = &(snum->d[loop]);
  479. wnum.top = div_n;
  480. /*
  481. * only needed when BN_ucmp messes up the values between top and max
  482. */
  483. wnum.dmax = snum->dmax - loop; /* so we don't step out of bounds */
  484. /* Get the top 2 words of sdiv */
  485. /* div_n=sdiv->top; */
  486. d0 = sdiv->d[div_n - 1];
  487. d1 = (div_n == 1) ? 0 : sdiv->d[div_n - 2];
  488. /* pointer to the 'top' of snum */
  489. wnump = &(snum->d[num_n - 1]);
  490. /* Setup to 'res' */
  491. res->neg = (num->neg ^ divisor->neg);
  492. if (!bn_wexpand(res, (loop + 1)))
  493. goto err;
  494. res->top = loop - 1;
  495. resp = &(res->d[loop - 1]);
  496. /* space for temp */
  497. if (!bn_wexpand(tmp, (div_n + 1)))
  498. goto err;
  499. /*
  500. * if res->top == 0 then clear the neg value otherwise decrease the resp
  501. * pointer
  502. */
  503. if (res->top == 0)
  504. res->neg = 0;
  505. else
  506. resp--;
  507. for (i = 0; i < loop - 1; i++, wnump--, resp--) {
  508. BN_ULONG q, l0;
  509. /*
  510. * the first part of the loop uses the top two words of snum and sdiv
  511. * to calculate a BN_ULONG q such that | wnum - sdiv * q | < sdiv
  512. */
  513. # if defined(BN_DIV3W) && !defined(OPENSSL_NO_ASM)
  514. BN_ULONG bn_div_3_words(BN_ULONG *, BN_ULONG, BN_ULONG);
  515. q = bn_div_3_words(wnump, d1, d0);
  516. # else
  517. BN_ULONG n0, n1, rem = 0;
  518. n0 = wnump[0];
  519. n1 = wnump[-1];
  520. if (n0 == d0)
  521. q = BN_MASK2;
  522. else { /* n0 < d0 */
  523. # ifdef BN_LLONG
  524. BN_ULLONG t2;
  525. # if defined(BN_LLONG) && defined(BN_DIV2W) && !defined(bn_div_words)
  526. q = (BN_ULONG)(((((BN_ULLONG) n0) << BN_BITS2) | n1) / d0);
  527. # else
  528. q = bn_div_words(n0, n1, d0);
  529. # ifdef BN_DEBUG_LEVITTE
  530. fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\
  531. X) -> 0x%08X\n", n0, n1, d0, q);
  532. # endif
  533. # endif
  534. # ifndef REMAINDER_IS_ALREADY_CALCULATED
  535. /*
  536. * rem doesn't have to be BN_ULLONG. The least we
  537. * know it's less that d0, isn't it?
  538. */
  539. rem = (n1 - q * d0) & BN_MASK2;
  540. # endif
  541. t2 = (BN_ULLONG) d1 *q;
  542. for (;;) {
  543. if (t2 <= ((((BN_ULLONG) rem) << BN_BITS2) | wnump[-2]))
  544. break;
  545. q--;
  546. rem += d0;
  547. if (rem < d0)
  548. break; /* don't let rem overflow */
  549. t2 -= d1;
  550. }
  551. # else /* !BN_LLONG */
  552. BN_ULONG t2l, t2h;
  553. # if !defined(BN_UMULT_LOHI) && !defined(BN_UMULT_HIGH)
  554. BN_ULONG ql, qh;
  555. # endif
  556. q = bn_div_words(n0, n1, d0);
  557. # ifdef BN_DEBUG_LEVITTE
  558. fprintf(stderr, "DEBUG: bn_div_words(0x%08X,0x%08X,0x%08\
  559. X) -> 0x%08X\n", n0, n1, d0, q);
  560. # endif
  561. # ifndef REMAINDER_IS_ALREADY_CALCULATED
  562. rem = (n1 - q * d0) & BN_MASK2;
  563. # endif
  564. # if defined(BN_UMULT_LOHI)
  565. BN_UMULT_LOHI(t2l, t2h, d1, q);
  566. # elif defined(BN_UMULT_HIGH)
  567. t2l = d1 * q;
  568. t2h = BN_UMULT_HIGH(d1, q);
  569. # else
  570. t2l = LBITS(d1);
  571. t2h = HBITS(d1);
  572. ql = LBITS(q);
  573. qh = HBITS(q);
  574. mul64(t2l, t2h, ql, qh); /* t2=(BN_ULLONG)d1*q; */
  575. # endif
  576. for (;;) {
  577. if ((t2h < rem) || ((t2h == rem) && (t2l <= wnump[-2])))
  578. break;
  579. q--;
  580. rem += d0;
  581. if (rem < d0)
  582. break; /* don't let rem overflow */
  583. if (t2l < d1)
  584. t2h--;
  585. t2l -= d1;
  586. }
  587. # endif /* !BN_LLONG */
  588. }
  589. # endif /* !BN_DIV3W */
  590. l0 = bn_mul_words(tmp->d, sdiv->d, div_n, q);
  591. tmp->d[div_n] = l0;
  592. wnum.d--;
  593. /*
  594. * ingore top values of the bignums just sub the two BN_ULONG arrays
  595. * with bn_sub_words
  596. */
  597. if (bn_sub_words(wnum.d, wnum.d, tmp->d, div_n + 1)) {
  598. /*
  599. * Note: As we have considered only the leading two BN_ULONGs in
  600. * the calculation of q, sdiv * q might be greater than wnum (but
  601. * then (q-1) * sdiv is less or equal than wnum)
  602. */
  603. q--;
  604. if (bn_add_words(wnum.d, wnum.d, sdiv->d, div_n))
  605. /*
  606. * we can't have an overflow here (assuming that q != 0, but
  607. * if q == 0 then tmp is zero anyway)
  608. */
  609. (*wnump)++;
  610. }
  611. /* store part of the result */
  612. *resp = q;
  613. }
  614. bn_correct_top(snum);
  615. if (rm != NULL) {
  616. /*
  617. * Keep a copy of the neg flag in num because if rm==num BN_rshift()
  618. * will overwrite it.
  619. */
  620. int neg = num->neg;
  621. BN_rshift(rm, snum, norm_shift);
  622. if (!BN_is_zero(rm))
  623. rm->neg = neg;
  624. bn_check_top(rm);
  625. }
  626. bn_correct_top(res);
  627. BN_CTX_end(ctx);
  628. return (1);
  629. err:
  630. bn_check_top(rm);
  631. BN_CTX_end(ctx);
  632. return (0);
  633. }
  634. #endif