2
0

bn_exp.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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-2005 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. /* maximum precomputation table size for *variable* sliding windows */
  114. #define TABLE_SIZE 32
  115. /* this one works - simple but works */
  116. int BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
  117. {
  118. int i, bits, ret = 0;
  119. BIGNUM *v, *rr;
  120. if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0) {
  121. /* BN_FLG_CONSTTIME only supported by BN_mod_exp_mont() */
  122. BNerr(BN_F_BN_EXP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  123. return -1;
  124. }
  125. BN_CTX_start(ctx);
  126. if ((r == a) || (r == p))
  127. rr = BN_CTX_get(ctx);
  128. else
  129. rr = r;
  130. v = BN_CTX_get(ctx);
  131. if (rr == NULL || v == NULL)
  132. goto err;
  133. if (BN_copy(v, a) == NULL)
  134. goto err;
  135. bits = BN_num_bits(p);
  136. if (BN_is_odd(p)) {
  137. if (BN_copy(rr, a) == NULL)
  138. goto err;
  139. } else {
  140. if (!BN_one(rr))
  141. goto err;
  142. }
  143. for (i = 1; i < bits; i++) {
  144. if (!BN_sqr(v, v, ctx))
  145. goto err;
  146. if (BN_is_bit_set(p, i)) {
  147. if (!BN_mul(rr, rr, v, ctx))
  148. goto err;
  149. }
  150. }
  151. ret = 1;
  152. err:
  153. if (r != rr)
  154. BN_copy(r, rr);
  155. BN_CTX_end(ctx);
  156. bn_check_top(r);
  157. return (ret);
  158. }
  159. int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m,
  160. BN_CTX *ctx)
  161. {
  162. int ret;
  163. bn_check_top(a);
  164. bn_check_top(p);
  165. bn_check_top(m);
  166. /*-
  167. * For even modulus m = 2^k*m_odd, it might make sense to compute
  168. * a^p mod m_odd and a^p mod 2^k separately (with Montgomery
  169. * exponentiation for the odd part), using appropriate exponent
  170. * reductions, and combine the results using the CRT.
  171. *
  172. * For now, we use Montgomery only if the modulus is odd; otherwise,
  173. * exponentiation using the reciprocal-based quick remaindering
  174. * algorithm is used.
  175. *
  176. * (Timing obtained with expspeed.c [computations a^p mod m
  177. * where a, p, m are of the same length: 256, 512, 1024, 2048,
  178. * 4096, 8192 bits], compared to the running time of the
  179. * standard algorithm:
  180. *
  181. * BN_mod_exp_mont 33 .. 40 % [AMD K6-2, Linux, debug configuration]
  182. * 55 .. 77 % [UltraSparc processor, but
  183. * debug-solaris-sparcv8-gcc conf.]
  184. *
  185. * BN_mod_exp_recp 50 .. 70 % [AMD K6-2, Linux, debug configuration]
  186. * 62 .. 118 % [UltraSparc, debug-solaris-sparcv8-gcc]
  187. *
  188. * On the Sparc, BN_mod_exp_recp was faster than BN_mod_exp_mont
  189. * at 2048 and more bits, but at 512 and 1024 bits, it was
  190. * slower even than the standard algorithm!
  191. *
  192. * "Real" timings [linux-elf, solaris-sparcv9-gcc configurations]
  193. * should be obtained when the new Montgomery reduction code
  194. * has been integrated into OpenSSL.)
  195. */
  196. #define MONT_MUL_MOD
  197. #define MONT_EXP_WORD
  198. #define RECP_MUL_MOD
  199. #ifdef MONT_MUL_MOD
  200. /*
  201. * I have finally been able to take out this pre-condition of the top bit
  202. * being set. It was caused by an error in BN_div with negatives. There
  203. * was also another problem when for a^b%m a >= m. eay 07-May-97
  204. */
  205. /* if ((m->d[m->top-1]&BN_TBIT) && BN_is_odd(m)) */
  206. if (BN_is_odd(m)) {
  207. # ifdef MONT_EXP_WORD
  208. if (a->top == 1 && !a->neg
  209. && (BN_get_flags(p, BN_FLG_CONSTTIME) == 0)) {
  210. BN_ULONG A = a->d[0];
  211. ret = BN_mod_exp_mont_word(r, A, p, m, ctx, NULL);
  212. } else
  213. # endif
  214. ret = BN_mod_exp_mont(r, a, p, m, ctx, NULL);
  215. } else
  216. #endif
  217. #ifdef RECP_MUL_MOD
  218. {
  219. ret = BN_mod_exp_recp(r, a, p, m, ctx);
  220. }
  221. #else
  222. {
  223. ret = BN_mod_exp_simple(r, a, p, m, ctx);
  224. }
  225. #endif
  226. bn_check_top(r);
  227. return (ret);
  228. }
  229. int BN_mod_exp_recp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  230. const BIGNUM *m, BN_CTX *ctx)
  231. {
  232. int i, j, bits, ret = 0, wstart, wend, window, wvalue;
  233. int start = 1;
  234. BIGNUM *aa;
  235. /* Table of variables obtained from 'ctx' */
  236. BIGNUM *val[TABLE_SIZE];
  237. BN_RECP_CTX recp;
  238. if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0) {
  239. /* BN_FLG_CONSTTIME only supported by BN_mod_exp_mont() */
  240. BNerr(BN_F_BN_MOD_EXP_RECP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  241. return -1;
  242. }
  243. bits = BN_num_bits(p);
  244. if (bits == 0) {
  245. ret = BN_one(r);
  246. return ret;
  247. }
  248. BN_CTX_start(ctx);
  249. aa = BN_CTX_get(ctx);
  250. val[0] = BN_CTX_get(ctx);
  251. if (!aa || !val[0])
  252. goto err;
  253. BN_RECP_CTX_init(&recp);
  254. if (m->neg) {
  255. /* ignore sign of 'm' */
  256. if (!BN_copy(aa, m))
  257. goto err;
  258. aa->neg = 0;
  259. if (BN_RECP_CTX_set(&recp, aa, ctx) <= 0)
  260. goto err;
  261. } else {
  262. if (BN_RECP_CTX_set(&recp, m, ctx) <= 0)
  263. goto err;
  264. }
  265. if (!BN_nnmod(val[0], a, m, ctx))
  266. goto err; /* 1 */
  267. if (BN_is_zero(val[0])) {
  268. BN_zero(r);
  269. ret = 1;
  270. goto err;
  271. }
  272. window = BN_window_bits_for_exponent_size(bits);
  273. if (window > 1) {
  274. if (!BN_mod_mul_reciprocal(aa, val[0], val[0], &recp, ctx))
  275. goto err; /* 2 */
  276. j = 1 << (window - 1);
  277. for (i = 1; i < j; i++) {
  278. if (((val[i] = BN_CTX_get(ctx)) == NULL) ||
  279. !BN_mod_mul_reciprocal(val[i], val[i - 1], aa, &recp, ctx))
  280. goto err;
  281. }
  282. }
  283. start = 1; /* This is used to avoid multiplication etc
  284. * when there is only the value '1' in the
  285. * buffer. */
  286. wvalue = 0; /* The 'value' of the window */
  287. wstart = bits - 1; /* The top bit of the window */
  288. wend = 0; /* The bottom bit of the window */
  289. if (!BN_one(r))
  290. goto err;
  291. for (;;) {
  292. if (BN_is_bit_set(p, wstart) == 0) {
  293. if (!start)
  294. if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))
  295. goto err;
  296. if (wstart == 0)
  297. break;
  298. wstart--;
  299. continue;
  300. }
  301. /*
  302. * We now have wstart on a 'set' bit, we now need to work out how bit
  303. * a window to do. To do this we need to scan forward until the last
  304. * set bit before the end of the window
  305. */
  306. j = wstart;
  307. wvalue = 1;
  308. wend = 0;
  309. for (i = 1; i < window; i++) {
  310. if (wstart - i < 0)
  311. break;
  312. if (BN_is_bit_set(p, wstart - i)) {
  313. wvalue <<= (i - wend);
  314. wvalue |= 1;
  315. wend = i;
  316. }
  317. }
  318. /* wend is the size of the current window */
  319. j = wend + 1;
  320. /* add the 'bytes above' */
  321. if (!start)
  322. for (i = 0; i < j; i++) {
  323. if (!BN_mod_mul_reciprocal(r, r, r, &recp, ctx))
  324. goto err;
  325. }
  326. /* wvalue will be an odd number < 2^window */
  327. if (!BN_mod_mul_reciprocal(r, r, val[wvalue >> 1], &recp, ctx))
  328. goto err;
  329. /* move the 'window' down further */
  330. wstart -= wend + 1;
  331. wvalue = 0;
  332. start = 0;
  333. if (wstart < 0)
  334. break;
  335. }
  336. ret = 1;
  337. err:
  338. BN_CTX_end(ctx);
  339. BN_RECP_CTX_free(&recp);
  340. bn_check_top(r);
  341. return (ret);
  342. }
  343. int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
  344. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
  345. {
  346. int i, j, bits, ret = 0, wstart, wend, window, wvalue;
  347. int start = 1;
  348. BIGNUM *d, *r;
  349. const BIGNUM *aa;
  350. /* Table of variables obtained from 'ctx' */
  351. BIGNUM *val[TABLE_SIZE];
  352. BN_MONT_CTX *mont = NULL;
  353. if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0) {
  354. return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);
  355. }
  356. bn_check_top(a);
  357. bn_check_top(p);
  358. bn_check_top(m);
  359. if (!BN_is_odd(m)) {
  360. BNerr(BN_F_BN_MOD_EXP_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);
  361. return (0);
  362. }
  363. bits = BN_num_bits(p);
  364. if (bits == 0) {
  365. ret = BN_one(rr);
  366. return ret;
  367. }
  368. BN_CTX_start(ctx);
  369. d = BN_CTX_get(ctx);
  370. r = BN_CTX_get(ctx);
  371. val[0] = BN_CTX_get(ctx);
  372. if (!d || !r || !val[0])
  373. goto err;
  374. /*
  375. * If this is not done, things will break in the montgomery part
  376. */
  377. if (in_mont != NULL)
  378. mont = in_mont;
  379. else {
  380. if ((mont = BN_MONT_CTX_new()) == NULL)
  381. goto err;
  382. if (!BN_MONT_CTX_set(mont, m, ctx))
  383. goto err;
  384. }
  385. if (a->neg || BN_ucmp(a, m) >= 0) {
  386. if (!BN_nnmod(val[0], a, m, ctx))
  387. goto err;
  388. aa = val[0];
  389. } else
  390. aa = a;
  391. if (BN_is_zero(aa)) {
  392. BN_zero(rr);
  393. ret = 1;
  394. goto err;
  395. }
  396. if (!BN_to_montgomery(val[0], aa, mont, ctx))
  397. goto err; /* 1 */
  398. window = BN_window_bits_for_exponent_size(bits);
  399. if (window > 1) {
  400. if (!BN_mod_mul_montgomery(d, val[0], val[0], mont, ctx))
  401. goto err; /* 2 */
  402. j = 1 << (window - 1);
  403. for (i = 1; i < j; i++) {
  404. if (((val[i] = BN_CTX_get(ctx)) == NULL) ||
  405. !BN_mod_mul_montgomery(val[i], val[i - 1], d, mont, ctx))
  406. goto err;
  407. }
  408. }
  409. start = 1; /* This is used to avoid multiplication etc
  410. * when there is only the value '1' in the
  411. * buffer. */
  412. wvalue = 0; /* The 'value' of the window */
  413. wstart = bits - 1; /* The top bit of the window */
  414. wend = 0; /* The bottom bit of the window */
  415. if (!BN_to_montgomery(r, BN_value_one(), mont, ctx))
  416. goto err;
  417. for (;;) {
  418. if (BN_is_bit_set(p, wstart) == 0) {
  419. if (!start) {
  420. if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))
  421. goto err;
  422. }
  423. if (wstart == 0)
  424. break;
  425. wstart--;
  426. continue;
  427. }
  428. /*
  429. * We now have wstart on a 'set' bit, we now need to work out how bit
  430. * a window to do. To do this we need to scan forward until the last
  431. * set bit before the end of the window
  432. */
  433. j = wstart;
  434. wvalue = 1;
  435. wend = 0;
  436. for (i = 1; i < window; i++) {
  437. if (wstart - i < 0)
  438. break;
  439. if (BN_is_bit_set(p, wstart - i)) {
  440. wvalue <<= (i - wend);
  441. wvalue |= 1;
  442. wend = i;
  443. }
  444. }
  445. /* wend is the size of the current window */
  446. j = wend + 1;
  447. /* add the 'bytes above' */
  448. if (!start)
  449. for (i = 0; i < j; i++) {
  450. if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))
  451. goto err;
  452. }
  453. /* wvalue will be an odd number < 2^window */
  454. if (!BN_mod_mul_montgomery(r, r, val[wvalue >> 1], mont, ctx))
  455. goto err;
  456. /* move the 'window' down further */
  457. wstart -= wend + 1;
  458. wvalue = 0;
  459. start = 0;
  460. if (wstart < 0)
  461. break;
  462. }
  463. if (!BN_from_montgomery(rr, r, mont, ctx))
  464. goto err;
  465. ret = 1;
  466. err:
  467. if ((in_mont == NULL) && (mont != NULL))
  468. BN_MONT_CTX_free(mont);
  469. BN_CTX_end(ctx);
  470. bn_check_top(rr);
  471. return (ret);
  472. }
  473. /*
  474. * BN_mod_exp_mont_consttime() stores the precomputed powers in a specific
  475. * layout so that accessing any of these table values shows the same access
  476. * pattern as far as cache lines are concerned. The following functions are
  477. * used to transfer a BIGNUM from/to that table.
  478. */
  479. static int MOD_EXP_CTIME_COPY_TO_PREBUF(BIGNUM *b, int top,
  480. unsigned char *buf, int idx,
  481. int width)
  482. {
  483. size_t i, j;
  484. if (bn_wexpand(b, top) == NULL)
  485. return 0;
  486. while (b->top < top) {
  487. b->d[b->top++] = 0;
  488. }
  489. for (i = 0, j = idx; i < top * sizeof b->d[0]; i++, j += width) {
  490. buf[j] = ((unsigned char *)b->d)[i];
  491. }
  492. bn_correct_top(b);
  493. return 1;
  494. }
  495. static int MOD_EXP_CTIME_COPY_FROM_PREBUF(BIGNUM *b, int top,
  496. unsigned char *buf, int idx,
  497. int width)
  498. {
  499. size_t i, j;
  500. if (bn_wexpand(b, top) == NULL)
  501. return 0;
  502. for (i = 0, j = idx; i < top * sizeof b->d[0]; i++, j += width) {
  503. ((unsigned char *)b->d)[i] = buf[j];
  504. }
  505. b->top = top;
  506. bn_correct_top(b);
  507. return 1;
  508. }
  509. /*
  510. * Given a pointer value, compute the next address that is a cache line
  511. * multiple.
  512. */
  513. #define MOD_EXP_CTIME_ALIGN(x_) \
  514. ((unsigned char*)(x_) + (MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH - (((BN_ULONG)(x_)) & (MOD_EXP_CTIME_MIN_CACHE_LINE_MASK))))
  515. /*
  516. * This variant of BN_mod_exp_mont() uses fixed windows and the special
  517. * precomputation memory layout to limit data-dependency to a minimum to
  518. * protect secret exponents (cf. the hyper-threading timing attacks pointed
  519. * out by Colin Percival,
  520. * http://www.daemong-consideredperthreading-considered-harmful/)
  521. */
  522. int BN_mod_exp_mont_consttime(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
  523. const BIGNUM *m, BN_CTX *ctx,
  524. BN_MONT_CTX *in_mont)
  525. {
  526. int i, bits, ret = 0, idx, window, wvalue;
  527. int top;
  528. BIGNUM *r;
  529. const BIGNUM *aa;
  530. BN_MONT_CTX *mont = NULL;
  531. int numPowers;
  532. unsigned char *powerbufFree = NULL;
  533. int powerbufLen = 0;
  534. unsigned char *powerbuf = NULL;
  535. BIGNUM *computeTemp = NULL, *am = NULL;
  536. bn_check_top(a);
  537. bn_check_top(p);
  538. bn_check_top(m);
  539. top = m->top;
  540. if (!(m->d[0] & 1)) {
  541. BNerr(BN_F_BN_MOD_EXP_MONT_CONSTTIME, BN_R_CALLED_WITH_EVEN_MODULUS);
  542. return (0);
  543. }
  544. bits = BN_num_bits(p);
  545. if (bits == 0) {
  546. ret = BN_one(rr);
  547. return ret;
  548. }
  549. /* Initialize BIGNUM context and allocate intermediate result */
  550. BN_CTX_start(ctx);
  551. r = BN_CTX_get(ctx);
  552. if (r == NULL)
  553. goto err;
  554. /*
  555. * Allocate a montgomery context if it was not supplied by the caller. If
  556. * this is not done, things will break in the montgomery part.
  557. */
  558. if (in_mont != NULL)
  559. mont = in_mont;
  560. else {
  561. if ((mont = BN_MONT_CTX_new()) == NULL)
  562. goto err;
  563. if (!BN_MONT_CTX_set(mont, m, ctx))
  564. goto err;
  565. }
  566. /* Get the window size to use with size of p. */
  567. window = BN_window_bits_for_ctime_exponent_size(bits);
  568. /*
  569. * Allocate a buffer large enough to hold all of the pre-computed powers
  570. * of a.
  571. */
  572. numPowers = 1 << window;
  573. powerbufLen = sizeof(m->d[0]) * top * numPowers;
  574. if ((powerbufFree =
  575. (unsigned char *)OPENSSL_malloc(powerbufLen +
  576. MOD_EXP_CTIME_MIN_CACHE_LINE_WIDTH))
  577. == NULL)
  578. goto err;
  579. powerbuf = MOD_EXP_CTIME_ALIGN(powerbufFree);
  580. memset(powerbuf, 0, powerbufLen);
  581. /*
  582. * Initialize the intermediate result. Do this early to save double
  583. * conversion, once each for a^0 and intermediate result.
  584. */
  585. if (!BN_to_montgomery(r, BN_value_one(), mont, ctx))
  586. goto err;
  587. if (!MOD_EXP_CTIME_COPY_TO_PREBUF(r, top, powerbuf, 0, numPowers))
  588. goto err;
  589. /* Initialize computeTemp as a^1 with montgomery precalcs */
  590. computeTemp = BN_CTX_get(ctx);
  591. am = BN_CTX_get(ctx);
  592. if (computeTemp == NULL || am == NULL)
  593. goto err;
  594. if (a->neg || BN_ucmp(a, m) >= 0) {
  595. if (!BN_mod(am, a, m, ctx))
  596. goto err;
  597. aa = am;
  598. } else
  599. aa = a;
  600. if (!BN_to_montgomery(am, aa, mont, ctx))
  601. goto err;
  602. if (!BN_copy(computeTemp, am))
  603. goto err;
  604. if (!MOD_EXP_CTIME_COPY_TO_PREBUF(am, top, powerbuf, 1, numPowers))
  605. goto err;
  606. /*
  607. * If the window size is greater than 1, then calculate
  608. * val[i=2..2^winsize-1]. Powers are computed as a*a^(i-1) (even powers
  609. * could instead be computed as (a^(i/2))^2 to use the slight performance
  610. * advantage of sqr over mul).
  611. */
  612. if (window > 1) {
  613. for (i = 2; i < numPowers; i++) {
  614. /* Calculate a^i = a^(i-1) * a */
  615. if (!BN_mod_mul_montgomery
  616. (computeTemp, am, computeTemp, mont, ctx))
  617. goto err;
  618. if (!MOD_EXP_CTIME_COPY_TO_PREBUF
  619. (computeTemp, top, powerbuf, i, numPowers))
  620. goto err;
  621. }
  622. }
  623. /*
  624. * Adjust the number of bits up to a multiple of the window size. If the
  625. * exponent length is not a multiple of the window size, then this pads
  626. * the most significant bits with zeros to normalize the scanning loop to
  627. * there's no special cases. * NOTE: Making the window size a power of
  628. * two less than the native * word size ensures that the padded bits
  629. * won't go past the last * word in the internal BIGNUM structure. Going
  630. * past the end will * still produce the correct result, but causes a
  631. * different branch * to be taken in the BN_is_bit_set function.
  632. */
  633. bits = ((bits + window - 1) / window) * window;
  634. idx = bits - 1; /* The top bit of the window */
  635. /*
  636. * Scan the exponent one window at a time starting from the most
  637. * significant bits.
  638. */
  639. while (idx >= 0) {
  640. wvalue = 0; /* The 'value' of the window */
  641. /* Scan the window, squaring the result as we go */
  642. for (i = 0; i < window; i++, idx--) {
  643. if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))
  644. goto err;
  645. wvalue = (wvalue << 1) + BN_is_bit_set(p, idx);
  646. }
  647. /*
  648. * Fetch the appropriate pre-computed value from the pre-buf
  649. */
  650. if (!MOD_EXP_CTIME_COPY_FROM_PREBUF
  651. (computeTemp, top, powerbuf, wvalue, numPowers))
  652. goto err;
  653. /* Multiply the result into the intermediate result */
  654. if (!BN_mod_mul_montgomery(r, r, computeTemp, mont, ctx))
  655. goto err;
  656. }
  657. /* Convert the final result from montgomery to standard format */
  658. if (!BN_from_montgomery(rr, r, mont, ctx))
  659. goto err;
  660. ret = 1;
  661. err:
  662. if ((in_mont == NULL) && (mont != NULL))
  663. BN_MONT_CTX_free(mont);
  664. if (powerbuf != NULL) {
  665. OPENSSL_cleanse(powerbuf, powerbufLen);
  666. OPENSSL_free(powerbufFree);
  667. }
  668. if (am != NULL)
  669. BN_clear(am);
  670. if (computeTemp != NULL)
  671. BN_clear(computeTemp);
  672. BN_CTX_end(ctx);
  673. return (ret);
  674. }
  675. int BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p,
  676. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
  677. {
  678. BN_MONT_CTX *mont = NULL;
  679. int b, bits, ret = 0;
  680. int r_is_one;
  681. BN_ULONG w, next_w;
  682. BIGNUM *d, *r, *t;
  683. BIGNUM *swap_tmp;
  684. #define BN_MOD_MUL_WORD(r, w, m) \
  685. (BN_mul_word(r, (w)) && \
  686. (/* BN_ucmp(r, (m)) < 0 ? 1 :*/ \
  687. (BN_mod(t, r, m, ctx) && (swap_tmp = r, r = t, t = swap_tmp, 1))))
  688. /*
  689. * BN_MOD_MUL_WORD is only used with 'w' large, so the BN_ucmp test is
  690. * probably more overhead than always using BN_mod (which uses BN_copy if
  691. * a similar test returns true).
  692. */
  693. /*
  694. * We can use BN_mod and do not need BN_nnmod because our accumulator is
  695. * never negative (the result of BN_mod does not depend on the sign of
  696. * the modulus).
  697. */
  698. #define BN_TO_MONTGOMERY_WORD(r, w, mont) \
  699. (BN_set_word(r, (w)) && BN_to_montgomery(r, r, (mont), ctx))
  700. if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0) {
  701. /* BN_FLG_CONSTTIME only supported by BN_mod_exp_mont() */
  702. BNerr(BN_F_BN_MOD_EXP_MONT_WORD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  703. return -1;
  704. }
  705. bn_check_top(p);
  706. bn_check_top(m);
  707. if (!BN_is_odd(m)) {
  708. BNerr(BN_F_BN_MOD_EXP_MONT_WORD, BN_R_CALLED_WITH_EVEN_MODULUS);
  709. return (0);
  710. }
  711. if (m->top == 1)
  712. a %= m->d[0]; /* make sure that 'a' is reduced */
  713. bits = BN_num_bits(p);
  714. if (bits == 0) {
  715. /* x**0 mod 1 is still zero. */
  716. if (BN_is_one(m)) {
  717. ret = 1;
  718. BN_zero(rr);
  719. } else
  720. ret = BN_one(rr);
  721. return ret;
  722. }
  723. if (a == 0) {
  724. BN_zero(rr);
  725. ret = 1;
  726. return ret;
  727. }
  728. BN_CTX_start(ctx);
  729. d = BN_CTX_get(ctx);
  730. r = BN_CTX_get(ctx);
  731. t = BN_CTX_get(ctx);
  732. if (d == NULL || r == NULL || t == NULL)
  733. goto err;
  734. if (in_mont != NULL)
  735. mont = in_mont;
  736. else {
  737. if ((mont = BN_MONT_CTX_new()) == NULL)
  738. goto err;
  739. if (!BN_MONT_CTX_set(mont, m, ctx))
  740. goto err;
  741. }
  742. r_is_one = 1; /* except for Montgomery factor */
  743. /* bits-1 >= 0 */
  744. /* The result is accumulated in the product r*w. */
  745. w = a; /* bit 'bits-1' of 'p' is always set */
  746. for (b = bits - 2; b >= 0; b--) {
  747. /* First, square r*w. */
  748. next_w = w * w;
  749. if ((next_w / w) != w) { /* overflow */
  750. if (r_is_one) {
  751. if (!BN_TO_MONTGOMERY_WORD(r, w, mont))
  752. goto err;
  753. r_is_one = 0;
  754. } else {
  755. if (!BN_MOD_MUL_WORD(r, w, m))
  756. goto err;
  757. }
  758. next_w = 1;
  759. }
  760. w = next_w;
  761. if (!r_is_one) {
  762. if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))
  763. goto err;
  764. }
  765. /* Second, multiply r*w by 'a' if exponent bit is set. */
  766. if (BN_is_bit_set(p, b)) {
  767. next_w = w * a;
  768. if ((next_w / a) != w) { /* overflow */
  769. if (r_is_one) {
  770. if (!BN_TO_MONTGOMERY_WORD(r, w, mont))
  771. goto err;
  772. r_is_one = 0;
  773. } else {
  774. if (!BN_MOD_MUL_WORD(r, w, m))
  775. goto err;
  776. }
  777. next_w = a;
  778. }
  779. w = next_w;
  780. }
  781. }
  782. /* Finally, set r:=r*w. */
  783. if (w != 1) {
  784. if (r_is_one) {
  785. if (!BN_TO_MONTGOMERY_WORD(r, w, mont))
  786. goto err;
  787. r_is_one = 0;
  788. } else {
  789. if (!BN_MOD_MUL_WORD(r, w, m))
  790. goto err;
  791. }
  792. }
  793. if (r_is_one) { /* can happen only if a == 1 */
  794. if (!BN_one(rr))
  795. goto err;
  796. } else {
  797. if (!BN_from_montgomery(rr, r, mont, ctx))
  798. goto err;
  799. }
  800. ret = 1;
  801. err:
  802. if ((in_mont == NULL) && (mont != NULL))
  803. BN_MONT_CTX_free(mont);
  804. BN_CTX_end(ctx);
  805. bn_check_top(rr);
  806. return (ret);
  807. }
  808. /* The old fallback, simple version :-) */
  809. int BN_mod_exp_simple(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  810. const BIGNUM *m, BN_CTX *ctx)
  811. {
  812. int i, j, bits, ret = 0, wstart, wend, window, wvalue;
  813. int start = 1;
  814. BIGNUM *d;
  815. /* Table of variables obtained from 'ctx' */
  816. BIGNUM *val[TABLE_SIZE];
  817. if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0) {
  818. /* BN_FLG_CONSTTIME only supported by BN_mod_exp_mont() */
  819. BNerr(BN_F_BN_MOD_EXP_SIMPLE, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  820. return -1;
  821. }
  822. bits = BN_num_bits(p);
  823. if (bits == 0) {
  824. ret = BN_one(r);
  825. return ret;
  826. }
  827. BN_CTX_start(ctx);
  828. d = BN_CTX_get(ctx);
  829. val[0] = BN_CTX_get(ctx);
  830. if (!d || !val[0])
  831. goto err;
  832. if (!BN_nnmod(val[0], a, m, ctx))
  833. goto err; /* 1 */
  834. if (BN_is_zero(val[0])) {
  835. BN_zero(r);
  836. ret = 1;
  837. goto err;
  838. }
  839. window = BN_window_bits_for_exponent_size(bits);
  840. if (window > 1) {
  841. if (!BN_mod_mul(d, val[0], val[0], m, ctx))
  842. goto err; /* 2 */
  843. j = 1 << (window - 1);
  844. for (i = 1; i < j; i++) {
  845. if (((val[i] = BN_CTX_get(ctx)) == NULL) ||
  846. !BN_mod_mul(val[i], val[i - 1], d, m, ctx))
  847. goto err;
  848. }
  849. }
  850. start = 1; /* This is used to avoid multiplication etc
  851. * when there is only the value '1' in the
  852. * buffer. */
  853. wvalue = 0; /* The 'value' of the window */
  854. wstart = bits - 1; /* The top bit of the window */
  855. wend = 0; /* The bottom bit of the window */
  856. if (!BN_one(r))
  857. goto err;
  858. for (;;) {
  859. if (BN_is_bit_set(p, wstart) == 0) {
  860. if (!start)
  861. if (!BN_mod_mul(r, r, r, m, ctx))
  862. goto err;
  863. if (wstart == 0)
  864. break;
  865. wstart--;
  866. continue;
  867. }
  868. /*
  869. * We now have wstart on a 'set' bit, we now need to work out how bit
  870. * a window to do. To do this we need to scan forward until the last
  871. * set bit before the end of the window
  872. */
  873. j = wstart;
  874. wvalue = 1;
  875. wend = 0;
  876. for (i = 1; i < window; i++) {
  877. if (wstart - i < 0)
  878. break;
  879. if (BN_is_bit_set(p, wstart - i)) {
  880. wvalue <<= (i - wend);
  881. wvalue |= 1;
  882. wend = i;
  883. }
  884. }
  885. /* wend is the size of the current window */
  886. j = wend + 1;
  887. /* add the 'bytes above' */
  888. if (!start)
  889. for (i = 0; i < j; i++) {
  890. if (!BN_mod_mul(r, r, r, m, ctx))
  891. goto err;
  892. }
  893. /* wvalue will be an odd number < 2^window */
  894. if (!BN_mod_mul(r, r, val[wvalue >> 1], m, ctx))
  895. goto err;
  896. /* move the 'window' down further */
  897. wstart -= wend + 1;
  898. wvalue = 0;
  899. start = 0;
  900. if (wstart < 0)
  901. break;
  902. }
  903. ret = 1;
  904. err:
  905. BN_CTX_end(ctx);
  906. bn_check_top(r);
  907. return (ret);
  908. }