bn_exp.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. /* crypto/bn/bn_exp.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. /* ====================================================================
  59. * Copyright (c) 1998-2000 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * openssl-core@openssl.org.
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * (eay@cryptsoft.com). This product includes software written by Tim
  108. * Hudson (tjh@cryptsoft.com).
  109. *
  110. */
  111. #include "cryptlib.h"
  112. #include "bn_lcl.h"
  113. #define TABLE_SIZE 32
  114. /* this one works - simple but works */
  115. int BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
  116. {
  117. int i,bits,ret=0;
  118. BIGNUM *v,*rr;
  119. BN_CTX_start(ctx);
  120. if ((r == a) || (r == p))
  121. rr = BN_CTX_get(ctx);
  122. else
  123. rr = r;
  124. if ((v = BN_CTX_get(ctx)) == NULL) goto err;
  125. if (BN_copy(v,a) == NULL) goto err;
  126. bits=BN_num_bits(p);
  127. if (BN_is_odd(p))
  128. { if (BN_copy(rr,a) == NULL) goto err; }
  129. else { if (!BN_one(rr)) goto err; }
  130. for (i=1; i<bits; i++)
  131. {
  132. if (!BN_sqr(v,v,ctx)) goto err;
  133. if (BN_is_bit_set(p,i))
  134. {
  135. if (!BN_mul(rr,rr,v,ctx)) goto err;
  136. }
  137. }
  138. ret=1;
  139. err:
  140. if (r != rr) BN_copy(r,rr);
  141. BN_CTX_end(ctx);
  142. return(ret);
  143. }
  144. int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,
  145. BN_CTX *ctx)
  146. {
  147. int ret;
  148. bn_check_top(a);
  149. bn_check_top(p);
  150. bn_check_top(m);
  151. /* For even modulus m = 2^k*m_odd, it might make sense to compute
  152. * a^p mod m_odd and a^p mod 2^k separately (with Montgomery
  153. * exponentiation for the odd part), using appropriate exponent
  154. * reductions, and combine the results using the CRT.
  155. *
  156. * For now, we use Montgomery only if the modulus is odd; otherwise,
  157. * exponentiation using the reciprocal-based quick remaindering
  158. * algorithm is used.
  159. *
  160. * (Timing obtained with expspeed.c [computations a^p mod m
  161. * where a, p, m are of the same length: 256, 512, 1024, 2048,
  162. * 4096, 8192 bits], compared to the running time of the
  163. * standard algorithm:
  164. *
  165. * BN_mod_exp_mont 33 .. 40 % [AMD K6-2, Linux, debug configuration]
  166. * 55 .. 77 % [UltraSparc processor, but
  167. * debug-solaris-sparcv8-gcc conf.]
  168. *
  169. * BN_mod_exp_recp 50 .. 70 % [AMD K6-2, Linux, debug configuration]
  170. * 62 .. 118 % [UltraSparc, debug-solaris-sparcv8-gcc]
  171. *
  172. * On the Sparc, BN_mod_exp_recp was faster than BN_mod_exp_mont
  173. * at 2048 and more bits, but at 512 and 1024 bits, it was
  174. * slower even than the standard algorithm!
  175. *
  176. * "Real" timings [linux-elf, solaris-sparcv9-gcc configurations]
  177. * should be obtained when the new Montgomery reduction code
  178. * has been integrated into OpenSSL.)
  179. */
  180. #define MONT_MUL_MOD
  181. #define MONT_EXP_WORD
  182. #define RECP_MUL_MOD
  183. #ifdef MONT_MUL_MOD
  184. /* I have finally been able to take out this pre-condition of
  185. * the top bit being set. It was caused by an error in BN_div
  186. * with negatives. There was also another problem when for a^b%m
  187. * a >= m. eay 07-May-97 */
  188. /* if ((m->d[m->top-1]&BN_TBIT) && BN_is_odd(m)) */
  189. if (BN_is_odd(m))
  190. {
  191. # ifdef MONT_EXP_WORD
  192. if (a->top == 1 && !a->neg)
  193. {
  194. BN_ULONG A = a->d[0];
  195. ret=BN_mod_exp_mont_word(r,A,p,m,ctx,NULL);
  196. }
  197. else
  198. # endif
  199. ret=BN_mod_exp_mont(r,a,p,m,ctx,NULL);
  200. }
  201. else
  202. #endif
  203. #ifdef RECP_MUL_MOD
  204. { ret=BN_mod_exp_recp(r,a,p,m,ctx); }
  205. #else
  206. { ret=BN_mod_exp_simple(r,a,p,m,ctx); }
  207. #endif
  208. return(ret);
  209. }
  210. int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  211. const BIGNUM *m, BN_CTX *ctx)
  212. {
  213. int i,j,bits,ret=0,wstart,wend,window,wvalue;
  214. int start=1,ts=0;
  215. BIGNUM *aa;
  216. BIGNUM val[TABLE_SIZE];
  217. BN_RECP_CTX recp;
  218. bits=BN_num_bits(p);
  219. if (bits == 0)
  220. {
  221. ret = BN_one(r);
  222. return ret;
  223. }
  224. BN_CTX_start(ctx);
  225. if ((aa = BN_CTX_get(ctx)) == NULL) goto err;
  226. BN_RECP_CTX_init(&recp);
  227. if (m->neg)
  228. {
  229. /* ignore sign of 'm' */
  230. if (!BN_copy(aa, m)) goto err;
  231. aa->neg = 0;
  232. if (BN_RECP_CTX_set(&recp,aa,ctx) <= 0) goto err;
  233. }
  234. else
  235. {
  236. if (BN_RECP_CTX_set(&recp,m,ctx) <= 0) goto err;
  237. }
  238. BN_init(&(val[0]));
  239. ts=1;
  240. if (!BN_nnmod(&(val[0]),a,m,ctx)) goto err; /* 1 */
  241. if (BN_is_zero(&(val[0])))
  242. {
  243. ret = BN_zero(r);
  244. goto err;
  245. }
  246. window = BN_window_bits_for_exponent_size(bits);
  247. if (window > 1)
  248. {
  249. if (!BN_mod_mul_reciprocal(aa,&(val[0]),&(val[0]),&recp,ctx))
  250. goto err; /* 2 */
  251. j=1<<(window-1);
  252. for (i=1; i<j; i++)
  253. {
  254. BN_init(&val[i]);
  255. if (!BN_mod_mul_reciprocal(&(val[i]),&(val[i-1]),aa,&recp,ctx))
  256. goto err;
  257. }
  258. ts=i;
  259. }
  260. start=1; /* This is used to avoid multiplication etc
  261. * when there is only the value '1' in the
  262. * buffer. */
  263. wvalue=0; /* The 'value' of the window */
  264. wstart=bits-1; /* The top bit of the window */
  265. wend=0; /* The bottom bit of the window */
  266. if (!BN_one(r)) goto err;
  267. for (;;)
  268. {
  269. if (BN_is_bit_set(p,wstart) == 0)
  270. {
  271. if (!start)
  272. if (!BN_mod_mul_reciprocal(r,r,r,&recp,ctx))
  273. goto err;
  274. if (wstart == 0) break;
  275. wstart--;
  276. continue;
  277. }
  278. /* We now have wstart on a 'set' bit, we now need to work out
  279. * how bit a window to do. To do this we need to scan
  280. * forward until the last set bit before the end of the
  281. * window */
  282. j=wstart;
  283. wvalue=1;
  284. wend=0;
  285. for (i=1; i<window; i++)
  286. {
  287. if (wstart-i < 0) break;
  288. if (BN_is_bit_set(p,wstart-i))
  289. {
  290. wvalue<<=(i-wend);
  291. wvalue|=1;
  292. wend=i;
  293. }
  294. }
  295. /* wend is the size of the current window */
  296. j=wend+1;
  297. /* add the 'bytes above' */
  298. if (!start)
  299. for (i=0; i<j; i++)
  300. {
  301. if (!BN_mod_mul_reciprocal(r,r,r,&recp,ctx))
  302. goto err;
  303. }
  304. /* wvalue will be an odd number < 2^window */
  305. if (!BN_mod_mul_reciprocal(r,r,&(val[wvalue>>1]),&recp,ctx))
  306. goto err;
  307. /* move the 'window' down further */
  308. wstart-=wend+1;
  309. wvalue=0;
  310. start=0;
  311. if (wstart < 0) break;
  312. }
  313. ret=1;
  314. err:
  315. BN_CTX_end(ctx);
  316. for (i=0; i<ts; i++)
  317. BN_clear_free(&(val[i]));
  318. BN_RECP_CTX_free(&recp);
  319. return(ret);
  320. }
  321. int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
  322. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
  323. {
  324. int i,j,bits,ret=0,wstart,wend,window,wvalue;
  325. int start=1,ts=0;
  326. BIGNUM *d,*r;
  327. const BIGNUM *aa;
  328. BIGNUM val[TABLE_SIZE];
  329. BN_MONT_CTX *mont=NULL;
  330. bn_check_top(a);
  331. bn_check_top(p);
  332. bn_check_top(m);
  333. if (!(m->d[0] & 1))
  334. {
  335. BNerr(BN_F_BN_MOD_EXP_MONT,BN_R_CALLED_WITH_EVEN_MODULUS);
  336. return(0);
  337. }
  338. bits=BN_num_bits(p);
  339. if (bits == 0)
  340. {
  341. ret = BN_one(rr);
  342. return ret;
  343. }
  344. BN_CTX_start(ctx);
  345. d = BN_CTX_get(ctx);
  346. r = BN_CTX_get(ctx);
  347. if (d == NULL || r == NULL) goto err;
  348. /* If this is not done, things will break in the montgomery
  349. * part */
  350. if (in_mont != NULL)
  351. mont=in_mont;
  352. else
  353. {
  354. if ((mont=BN_MONT_CTX_new()) == NULL) goto err;
  355. if (!BN_MONT_CTX_set(mont,m,ctx)) goto err;
  356. }
  357. BN_init(&val[0]);
  358. ts=1;
  359. if (a->neg || BN_ucmp(a,m) >= 0)
  360. {
  361. if (!BN_nnmod(&(val[0]),a,m,ctx))
  362. goto err;
  363. aa= &(val[0]);
  364. }
  365. else
  366. aa=a;
  367. if (BN_is_zero(aa))
  368. {
  369. ret = BN_zero(rr);
  370. goto err;
  371. }
  372. if (!BN_to_montgomery(&(val[0]),aa,mont,ctx)) goto err; /* 1 */
  373. window = BN_window_bits_for_exponent_size(bits);
  374. if (window > 1)
  375. {
  376. if (!BN_mod_mul_montgomery(d,&(val[0]),&(val[0]),mont,ctx)) goto err; /* 2 */
  377. j=1<<(window-1);
  378. for (i=1; i<j; i++)
  379. {
  380. BN_init(&(val[i]));
  381. if (!BN_mod_mul_montgomery(&(val[i]),&(val[i-1]),d,mont,ctx))
  382. goto err;
  383. }
  384. ts=i;
  385. }
  386. start=1; /* This is used to avoid multiplication etc
  387. * when there is only the value '1' in the
  388. * buffer. */
  389. wvalue=0; /* The 'value' of the window */
  390. wstart=bits-1; /* The top bit of the window */
  391. wend=0; /* The bottom bit of the window */
  392. if (!BN_to_montgomery(r,BN_value_one(),mont,ctx)) goto err;
  393. for (;;)
  394. {
  395. if (BN_is_bit_set(p,wstart) == 0)
  396. {
  397. if (!start)
  398. {
  399. if (!BN_mod_mul_montgomery(r,r,r,mont,ctx))
  400. goto err;
  401. }
  402. if (wstart == 0) break;
  403. wstart--;
  404. continue;
  405. }
  406. /* We now have wstart on a 'set' bit, we now need to work out
  407. * how bit a window to do. To do this we need to scan
  408. * forward until the last set bit before the end of the
  409. * window */
  410. j=wstart;
  411. wvalue=1;
  412. wend=0;
  413. for (i=1; i<window; i++)
  414. {
  415. if (wstart-i < 0) break;
  416. if (BN_is_bit_set(p,wstart-i))
  417. {
  418. wvalue<<=(i-wend);
  419. wvalue|=1;
  420. wend=i;
  421. }
  422. }
  423. /* wend is the size of the current window */
  424. j=wend+1;
  425. /* add the 'bytes above' */
  426. if (!start)
  427. for (i=0; i<j; i++)
  428. {
  429. if (!BN_mod_mul_montgomery(r,r,r,mont,ctx))
  430. goto err;
  431. }
  432. /* wvalue will be an odd number < 2^window */
  433. if (!BN_mod_mul_montgomery(r,r,&(val[wvalue>>1]),mont,ctx))
  434. goto err;
  435. /* move the 'window' down further */
  436. wstart-=wend+1;
  437. wvalue=0;
  438. start=0;
  439. if (wstart < 0) break;
  440. }
  441. if (!BN_from_montgomery(rr,r,mont,ctx)) goto err;
  442. ret=1;
  443. err:
  444. if ((in_mont == NULL) && (mont != NULL)) BN_MONT_CTX_free(mont);
  445. BN_CTX_end(ctx);
  446. for (i=0; i<ts; i++)
  447. BN_clear_free(&(val[i]));
  448. return(ret);
  449. }
  450. int BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p,
  451. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
  452. {
  453. BN_MONT_CTX *mont = NULL;
  454. int b, bits, ret=0;
  455. int r_is_one;
  456. BN_ULONG w, next_w;
  457. BIGNUM *d, *r, *t;
  458. BIGNUM *swap_tmp;
  459. #define BN_MOD_MUL_WORD(r, w, m) \
  460. (BN_mul_word(r, (w)) && \
  461. (/* BN_ucmp(r, (m)) < 0 ? 1 :*/ \
  462. (BN_mod(t, r, m, ctx) && (swap_tmp = r, r = t, t = swap_tmp, 1))))
  463. /* BN_MOD_MUL_WORD is only used with 'w' large,
  464. * so the BN_ucmp test is probably more overhead
  465. * than always using BN_mod (which uses BN_copy if
  466. * a similar test returns true). */
  467. /* We can use BN_mod and do not need BN_nnmod because our
  468. * accumulator is never negative (the result of BN_mod does
  469. * not depend on the sign of the modulus).
  470. */
  471. #define BN_TO_MONTGOMERY_WORD(r, w, mont) \
  472. (BN_set_word(r, (w)) && BN_to_montgomery(r, r, (mont), ctx))
  473. bn_check_top(p);
  474. bn_check_top(m);
  475. if (m->top == 0 || !(m->d[0] & 1))
  476. {
  477. BNerr(BN_F_BN_MOD_EXP_MONT_WORD,BN_R_CALLED_WITH_EVEN_MODULUS);
  478. return(0);
  479. }
  480. if (m->top == 1)
  481. a %= m->d[0]; /* make sure that 'a' is reduced */
  482. bits = BN_num_bits(p);
  483. if (bits == 0)
  484. {
  485. ret = BN_one(rr);
  486. return ret;
  487. }
  488. if (a == 0)
  489. {
  490. ret = BN_zero(rr);
  491. return ret;
  492. }
  493. BN_CTX_start(ctx);
  494. d = BN_CTX_get(ctx);
  495. r = BN_CTX_get(ctx);
  496. t = BN_CTX_get(ctx);
  497. if (d == NULL || r == NULL || t == NULL) goto err;
  498. if (in_mont != NULL)
  499. mont=in_mont;
  500. else
  501. {
  502. if ((mont = BN_MONT_CTX_new()) == NULL) goto err;
  503. if (!BN_MONT_CTX_set(mont, m, ctx)) goto err;
  504. }
  505. r_is_one = 1; /* except for Montgomery factor */
  506. /* bits-1 >= 0 */
  507. /* The result is accumulated in the product r*w. */
  508. w = a; /* bit 'bits-1' of 'p' is always set */
  509. for (b = bits-2; b >= 0; b--)
  510. {
  511. /* First, square r*w. */
  512. next_w = w*w;
  513. if ((next_w/w) != w) /* overflow */
  514. {
  515. if (r_is_one)
  516. {
  517. if (!BN_TO_MONTGOMERY_WORD(r, w, mont)) goto err;
  518. r_is_one = 0;
  519. }
  520. else
  521. {
  522. if (!BN_MOD_MUL_WORD(r, w, m)) goto err;
  523. }
  524. next_w = 1;
  525. }
  526. w = next_w;
  527. if (!r_is_one)
  528. {
  529. if (!BN_mod_mul_montgomery(r, r, r, mont, ctx)) goto err;
  530. }
  531. /* Second, multiply r*w by 'a' if exponent bit is set. */
  532. if (BN_is_bit_set(p, b))
  533. {
  534. next_w = w*a;
  535. if ((next_w/a) != w) /* overflow */
  536. {
  537. if (r_is_one)
  538. {
  539. if (!BN_TO_MONTGOMERY_WORD(r, w, mont)) goto err;
  540. r_is_one = 0;
  541. }
  542. else
  543. {
  544. if (!BN_MOD_MUL_WORD(r, w, m)) goto err;
  545. }
  546. next_w = a;
  547. }
  548. w = next_w;
  549. }
  550. }
  551. /* Finally, set r:=r*w. */
  552. if (w != 1)
  553. {
  554. if (r_is_one)
  555. {
  556. if (!BN_TO_MONTGOMERY_WORD(r, w, mont)) goto err;
  557. r_is_one = 0;
  558. }
  559. else
  560. {
  561. if (!BN_MOD_MUL_WORD(r, w, m)) goto err;
  562. }
  563. }
  564. if (r_is_one) /* can happen only if a == 1*/
  565. {
  566. if (!BN_one(rr)) goto err;
  567. }
  568. else
  569. {
  570. if (!BN_from_montgomery(rr, r, mont, ctx)) goto err;
  571. }
  572. ret = 1;
  573. err:
  574. if ((in_mont == NULL) && (mont != NULL)) BN_MONT_CTX_free(mont);
  575. BN_CTX_end(ctx);
  576. return(ret);
  577. }
  578. /* The old fallback, simple version :-) */
  579. int BN_mod_exp_simple(BIGNUM *r,
  580. const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,
  581. BN_CTX *ctx)
  582. {
  583. int i,j,bits,ret=0,wstart,wend,window,wvalue,ts=0;
  584. int start=1;
  585. BIGNUM *d;
  586. BIGNUM val[TABLE_SIZE];
  587. bits=BN_num_bits(p);
  588. if (bits == 0)
  589. {
  590. ret = BN_one(r);
  591. return ret;
  592. }
  593. BN_CTX_start(ctx);
  594. if ((d = BN_CTX_get(ctx)) == NULL) goto err;
  595. BN_init(&(val[0]));
  596. ts=1;
  597. if (!BN_nnmod(&(val[0]),a,m,ctx)) goto err; /* 1 */
  598. if (BN_is_zero(&(val[0])))
  599. {
  600. ret = BN_zero(r);
  601. goto err;
  602. }
  603. window = BN_window_bits_for_exponent_size(bits);
  604. if (window > 1)
  605. {
  606. if (!BN_mod_mul(d,&(val[0]),&(val[0]),m,ctx))
  607. goto err; /* 2 */
  608. j=1<<(window-1);
  609. for (i=1; i<j; i++)
  610. {
  611. BN_init(&(val[i]));
  612. if (!BN_mod_mul(&(val[i]),&(val[i-1]),d,m,ctx))
  613. goto err;
  614. }
  615. ts=i;
  616. }
  617. start=1; /* This is used to avoid multiplication etc
  618. * when there is only the value '1' in the
  619. * buffer. */
  620. wvalue=0; /* The 'value' of the window */
  621. wstart=bits-1; /* The top bit of the window */
  622. wend=0; /* The bottom bit of the window */
  623. if (!BN_one(r)) goto err;
  624. for (;;)
  625. {
  626. if (BN_is_bit_set(p,wstart) == 0)
  627. {
  628. if (!start)
  629. if (!BN_mod_mul(r,r,r,m,ctx))
  630. goto err;
  631. if (wstart == 0) break;
  632. wstart--;
  633. continue;
  634. }
  635. /* We now have wstart on a 'set' bit, we now need to work out
  636. * how bit a window to do. To do this we need to scan
  637. * forward until the last set bit before the end of the
  638. * window */
  639. j=wstart;
  640. wvalue=1;
  641. wend=0;
  642. for (i=1; i<window; i++)
  643. {
  644. if (wstart-i < 0) break;
  645. if (BN_is_bit_set(p,wstart-i))
  646. {
  647. wvalue<<=(i-wend);
  648. wvalue|=1;
  649. wend=i;
  650. }
  651. }
  652. /* wend is the size of the current window */
  653. j=wend+1;
  654. /* add the 'bytes above' */
  655. if (!start)
  656. for (i=0; i<j; i++)
  657. {
  658. if (!BN_mod_mul(r,r,r,m,ctx))
  659. goto err;
  660. }
  661. /* wvalue will be an odd number < 2^window */
  662. if (!BN_mod_mul(r,r,&(val[wvalue>>1]),m,ctx))
  663. goto err;
  664. /* move the 'window' down further */
  665. wstart-=wend+1;
  666. wvalue=0;
  667. start=0;
  668. if (wstart < 0) break;
  669. }
  670. ret=1;
  671. err:
  672. BN_CTX_end(ctx);
  673. for (i=0; i<ts; i++)
  674. BN_clear_free(&(val[i]));
  675. return(ret);
  676. }