bn_lib.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. /* crypto/bn/bn_lib.c */
  2. /* Copyright (C) 1995-1998 Eric Young ([email protected])
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young ([email protected]).
  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 ([email protected]).
  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 ([email protected])"
  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 ([email protected])"
  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. #ifndef BN_DEBUG
  59. # undef NDEBUG /* avoid conflicting definitions */
  60. # define NDEBUG
  61. #endif
  62. #include <assert.h>
  63. #include <limits.h>
  64. #include <stdio.h>
  65. #include "cryptlib.h"
  66. #include "bn_lcl.h"
  67. const char BN_version[] = "Big Number" OPENSSL_VERSION_PTEXT;
  68. /* This stuff appears to be completely unused, so is deprecated */
  69. #ifndef OPENSSL_NO_DEPRECATED
  70. /*-
  71. * For a 32 bit machine
  72. * 2 - 4 == 128
  73. * 3 - 8 == 256
  74. * 4 - 16 == 512
  75. * 5 - 32 == 1024
  76. * 6 - 64 == 2048
  77. * 7 - 128 == 4096
  78. * 8 - 256 == 8192
  79. */
  80. static int bn_limit_bits = 0;
  81. static int bn_limit_num = 8; /* (1<<bn_limit_bits) */
  82. static int bn_limit_bits_low = 0;
  83. static int bn_limit_num_low = 8; /* (1<<bn_limit_bits_low) */
  84. static int bn_limit_bits_high = 0;
  85. static int bn_limit_num_high = 8; /* (1<<bn_limit_bits_high) */
  86. static int bn_limit_bits_mont = 0;
  87. static int bn_limit_num_mont = 8; /* (1<<bn_limit_bits_mont) */
  88. void BN_set_params(int mult, int high, int low, int mont)
  89. {
  90. if (mult >= 0) {
  91. if (mult > (int)(sizeof(int) * 8) - 1)
  92. mult = sizeof(int) * 8 - 1;
  93. bn_limit_bits = mult;
  94. bn_limit_num = 1 << mult;
  95. }
  96. if (high >= 0) {
  97. if (high > (int)(sizeof(int) * 8) - 1)
  98. high = sizeof(int) * 8 - 1;
  99. bn_limit_bits_high = high;
  100. bn_limit_num_high = 1 << high;
  101. }
  102. if (low >= 0) {
  103. if (low > (int)(sizeof(int) * 8) - 1)
  104. low = sizeof(int) * 8 - 1;
  105. bn_limit_bits_low = low;
  106. bn_limit_num_low = 1 << low;
  107. }
  108. if (mont >= 0) {
  109. if (mont > (int)(sizeof(int) * 8) - 1)
  110. mont = sizeof(int) * 8 - 1;
  111. bn_limit_bits_mont = mont;
  112. bn_limit_num_mont = 1 << mont;
  113. }
  114. }
  115. int BN_get_params(int which)
  116. {
  117. if (which == 0)
  118. return (bn_limit_bits);
  119. else if (which == 1)
  120. return (bn_limit_bits_high);
  121. else if (which == 2)
  122. return (bn_limit_bits_low);
  123. else if (which == 3)
  124. return (bn_limit_bits_mont);
  125. else
  126. return (0);
  127. }
  128. #endif
  129. const BIGNUM *BN_value_one(void)
  130. {
  131. static const BN_ULONG data_one = 1L;
  132. static const BIGNUM const_one =
  133. { (BN_ULONG *)&data_one, 1, 1, 0, BN_FLG_STATIC_DATA };
  134. return (&const_one);
  135. }
  136. int BN_num_bits_word(BN_ULONG l)
  137. {
  138. BN_ULONG x, mask;
  139. int bits = (l != 0);
  140. #if BN_BITS2 > 32
  141. x = l >> 32;
  142. mask = (0 - x) & BN_MASK2;
  143. mask = (0 - (mask >> (BN_BITS2 - 1)));
  144. bits += 32 & mask;
  145. l ^= (x ^ l) & mask;
  146. #endif
  147. x = l >> 16;
  148. mask = (0 - x) & BN_MASK2;
  149. mask = (0 - (mask >> (BN_BITS2 - 1)));
  150. bits += 16 & mask;
  151. l ^= (x ^ l) & mask;
  152. x = l >> 8;
  153. mask = (0 - x) & BN_MASK2;
  154. mask = (0 - (mask >> (BN_BITS2 - 1)));
  155. bits += 8 & mask;
  156. l ^= (x ^ l) & mask;
  157. x = l >> 4;
  158. mask = (0 - x) & BN_MASK2;
  159. mask = (0 - (mask >> (BN_BITS2 - 1)));
  160. bits += 4 & mask;
  161. l ^= (x ^ l) & mask;
  162. x = l >> 2;
  163. mask = (0 - x) & BN_MASK2;
  164. mask = (0 - (mask >> (BN_BITS2 - 1)));
  165. bits += 2 & mask;
  166. l ^= (x ^ l) & mask;
  167. x = l >> 1;
  168. mask = (0 - x) & BN_MASK2;
  169. mask = (0 - (mask >> (BN_BITS2 - 1)));
  170. bits += 1 & mask;
  171. return bits;
  172. }
  173. int BN_num_bits(const BIGNUM *a)
  174. {
  175. int i = a->top - 1;
  176. bn_check_top(a);
  177. if (BN_is_zero(a))
  178. return 0;
  179. return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));
  180. }
  181. void BN_clear_free(BIGNUM *a)
  182. {
  183. int i;
  184. if (a == NULL)
  185. return;
  186. bn_check_top(a);
  187. if (a->d != NULL) {
  188. OPENSSL_cleanse(a->d, a->dmax * sizeof(a->d[0]));
  189. if (!(BN_get_flags(a, BN_FLG_STATIC_DATA)))
  190. OPENSSL_free(a->d);
  191. }
  192. i = BN_get_flags(a, BN_FLG_MALLOCED);
  193. OPENSSL_cleanse(a, sizeof(BIGNUM));
  194. if (i)
  195. OPENSSL_free(a);
  196. }
  197. void BN_free(BIGNUM *a)
  198. {
  199. if (a == NULL)
  200. return;
  201. bn_check_top(a);
  202. if ((a->d != NULL) && !(BN_get_flags(a, BN_FLG_STATIC_DATA)))
  203. OPENSSL_free(a->d);
  204. if (a->flags & BN_FLG_MALLOCED)
  205. OPENSSL_free(a);
  206. else {
  207. #ifndef OPENSSL_NO_DEPRECATED
  208. a->flags |= BN_FLG_FREE;
  209. #endif
  210. a->d = NULL;
  211. }
  212. }
  213. void BN_init(BIGNUM *a)
  214. {
  215. memset(a, 0, sizeof(BIGNUM));
  216. bn_check_top(a);
  217. }
  218. BIGNUM *BN_new(void)
  219. {
  220. BIGNUM *ret;
  221. if ((ret = (BIGNUM *)OPENSSL_malloc(sizeof(BIGNUM))) == NULL) {
  222. BNerr(BN_F_BN_NEW, ERR_R_MALLOC_FAILURE);
  223. return (NULL);
  224. }
  225. ret->flags = BN_FLG_MALLOCED;
  226. ret->top = 0;
  227. ret->neg = 0;
  228. ret->dmax = 0;
  229. ret->d = NULL;
  230. bn_check_top(ret);
  231. return (ret);
  232. }
  233. /* This is used both by bn_expand2() and bn_dup_expand() */
  234. /* The caller MUST check that words > b->dmax before calling this */
  235. static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
  236. {
  237. BN_ULONG *A, *a = NULL;
  238. const BN_ULONG *B;
  239. int i;
  240. if (words > (INT_MAX / (4 * BN_BITS2))) {
  241. BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
  242. return NULL;
  243. }
  244. if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
  245. BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
  246. return (NULL);
  247. }
  248. a = A = (BN_ULONG *)OPENSSL_malloc(sizeof(BN_ULONG) * words);
  249. if (A == NULL) {
  250. BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
  251. return (NULL);
  252. }
  253. #ifdef PURIFY
  254. /*
  255. * Valgrind complains in BN_consttime_swap because we process the whole
  256. * array even if it's not initialised yet. This doesn't matter in that
  257. * function - what's important is constant time operation (we're not
  258. * actually going to use the data)
  259. */
  260. memset(a, 0, sizeof(BN_ULONG) * words);
  261. #endif
  262. #if 1
  263. B = b->d;
  264. /* Check if the previous number needs to be copied */
  265. if (B != NULL) {
  266. for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {
  267. /*
  268. * The fact that the loop is unrolled
  269. * 4-wise is a tribute to Intel. It's
  270. * the one that doesn't have enough
  271. * registers to accomodate more data.
  272. * I'd unroll it 8-wise otherwise:-)
  273. *
  274. * <[email protected]>
  275. */
  276. BN_ULONG a0, a1, a2, a3;
  277. a0 = B[0];
  278. a1 = B[1];
  279. a2 = B[2];
  280. a3 = B[3];
  281. A[0] = a0;
  282. A[1] = a1;
  283. A[2] = a2;
  284. A[3] = a3;
  285. }
  286. /*
  287. * workaround for ultrix cc: without 'case 0', the optimizer does
  288. * the switch table by doing a=top&3; a--; goto jump_table[a];
  289. * which fails for top== 0
  290. */
  291. switch (b->top & 3) {
  292. case 3:
  293. A[2] = B[2];
  294. case 2:
  295. A[1] = B[1];
  296. case 1:
  297. A[0] = B[0];
  298. case 0:
  299. ;
  300. }
  301. }
  302. #else
  303. memset(A, 0, sizeof(BN_ULONG) * words);
  304. memcpy(A, b->d, sizeof(b->d[0]) * b->top);
  305. #endif
  306. return (a);
  307. }
  308. /*
  309. * This is an internal function that can be used instead of bn_expand2() when
  310. * there is a need to copy BIGNUMs instead of only expanding the data part,
  311. * while still expanding them. Especially useful when needing to expand
  312. * BIGNUMs that are declared 'const' and should therefore not be changed. The
  313. * reason to use this instead of a BN_dup() followed by a bn_expand2() is
  314. * memory allocation overhead. A BN_dup() followed by a bn_expand2() will
  315. * allocate new memory for the BIGNUM data twice, and free it once, while
  316. * bn_dup_expand() makes sure allocation is made only once.
  317. */
  318. #ifndef OPENSSL_NO_DEPRECATED
  319. BIGNUM *bn_dup_expand(const BIGNUM *b, int words)
  320. {
  321. BIGNUM *r = NULL;
  322. bn_check_top(b);
  323. /*
  324. * This function does not work if words <= b->dmax && top < words because
  325. * BN_dup() does not preserve 'dmax'! (But bn_dup_expand() is not used
  326. * anywhere yet.)
  327. */
  328. if (words > b->dmax) {
  329. BN_ULONG *a = bn_expand_internal(b, words);
  330. if (a) {
  331. r = BN_new();
  332. if (r) {
  333. r->top = b->top;
  334. r->dmax = words;
  335. r->neg = b->neg;
  336. r->d = a;
  337. } else {
  338. /* r == NULL, BN_new failure */
  339. OPENSSL_free(a);
  340. }
  341. }
  342. /*
  343. * If a == NULL, there was an error in allocation in
  344. * bn_expand_internal(), and NULL should be returned
  345. */
  346. } else {
  347. r = BN_dup(b);
  348. }
  349. bn_check_top(r);
  350. return r;
  351. }
  352. #endif
  353. /*
  354. * This is an internal function that should not be used in applications. It
  355. * ensures that 'b' has enough room for a 'words' word number and initialises
  356. * any unused part of b->d with leading zeros. It is mostly used by the
  357. * various BIGNUM routines. If there is an error, NULL is returned. If not,
  358. * 'b' is returned.
  359. */
  360. BIGNUM *bn_expand2(BIGNUM *b, int words)
  361. {
  362. if (words > b->dmax) {
  363. BN_ULONG *a = bn_expand_internal(b, words);
  364. if (!a)
  365. return NULL;
  366. if (b->d)
  367. OPENSSL_free(b->d);
  368. b->d = a;
  369. b->dmax = words;
  370. }
  371. /* None of this should be necessary because of what b->top means! */
  372. #if 0
  373. /*
  374. * NB: bn_wexpand() calls this only if the BIGNUM really has to grow
  375. */
  376. if (b->top < b->dmax) {
  377. int i;
  378. BN_ULONG *A = &(b->d[b->top]);
  379. for (i = (b->dmax - b->top) >> 3; i > 0; i--, A += 8) {
  380. A[0] = 0;
  381. A[1] = 0;
  382. A[2] = 0;
  383. A[3] = 0;
  384. A[4] = 0;
  385. A[5] = 0;
  386. A[6] = 0;
  387. A[7] = 0;
  388. }
  389. for (i = (b->dmax - b->top) & 7; i > 0; i--, A++)
  390. A[0] = 0;
  391. assert(A == &(b->d[b->dmax]));
  392. }
  393. #endif
  394. return b;
  395. }
  396. BIGNUM *BN_dup(const BIGNUM *a)
  397. {
  398. BIGNUM *t;
  399. if (a == NULL)
  400. return NULL;
  401. bn_check_top(a);
  402. t = BN_new();
  403. if (t == NULL)
  404. return NULL;
  405. if (!BN_copy(t, a)) {
  406. BN_free(t);
  407. return NULL;
  408. }
  409. bn_check_top(t);
  410. return t;
  411. }
  412. BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
  413. {
  414. int i;
  415. BN_ULONG *A;
  416. const BN_ULONG *B;
  417. bn_check_top(b);
  418. if (a == b)
  419. return (a);
  420. if (bn_wexpand(a, b->top) == NULL)
  421. return (NULL);
  422. #if 1
  423. A = a->d;
  424. B = b->d;
  425. for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {
  426. BN_ULONG a0, a1, a2, a3;
  427. a0 = B[0];
  428. a1 = B[1];
  429. a2 = B[2];
  430. a3 = B[3];
  431. A[0] = a0;
  432. A[1] = a1;
  433. A[2] = a2;
  434. A[3] = a3;
  435. }
  436. /* ultrix cc workaround, see comments in bn_expand_internal */
  437. switch (b->top & 3) {
  438. case 3:
  439. A[2] = B[2];
  440. case 2:
  441. A[1] = B[1];
  442. case 1:
  443. A[0] = B[0];
  444. case 0:;
  445. }
  446. #else
  447. memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);
  448. #endif
  449. a->neg = b->neg;
  450. a->top = b->top;
  451. a->flags |= b->flags & BN_FLG_FIXED_TOP;
  452. bn_check_top(a);
  453. return (a);
  454. }
  455. #define FLAGS_DATA(flags) ((flags) & (BN_FLG_STATIC_DATA \
  456. | BN_FLG_CONSTTIME \
  457. | BN_FLG_FIXED_TOP))
  458. #define FLAGS_STRUCT(flags) ((flags) & (BN_FLG_MALLOCED))
  459. void BN_swap(BIGNUM *a, BIGNUM *b)
  460. {
  461. int flags_old_a, flags_old_b;
  462. BN_ULONG *tmp_d;
  463. int tmp_top, tmp_dmax, tmp_neg;
  464. bn_check_top(a);
  465. bn_check_top(b);
  466. flags_old_a = a->flags;
  467. flags_old_b = b->flags;
  468. tmp_d = a->d;
  469. tmp_top = a->top;
  470. tmp_dmax = a->dmax;
  471. tmp_neg = a->neg;
  472. a->d = b->d;
  473. a->top = b->top;
  474. a->dmax = b->dmax;
  475. a->neg = b->neg;
  476. b->d = tmp_d;
  477. b->top = tmp_top;
  478. b->dmax = tmp_dmax;
  479. b->neg = tmp_neg;
  480. a->flags = FLAGS_STRUCT(flags_old_a) | FLAGS_DATA(flags_old_b);
  481. b->flags = FLAGS_STRUCT(flags_old_b) | FLAGS_DATA(flags_old_a);
  482. bn_check_top(a);
  483. bn_check_top(b);
  484. }
  485. void BN_clear(BIGNUM *a)
  486. {
  487. bn_check_top(a);
  488. if (a->d != NULL)
  489. OPENSSL_cleanse(a->d, a->dmax * sizeof(a->d[0]));
  490. a->top = 0;
  491. a->neg = 0;
  492. a->flags &= ~BN_FLG_FIXED_TOP;
  493. }
  494. BN_ULONG BN_get_word(const BIGNUM *a)
  495. {
  496. if (a->top > 1)
  497. return BN_MASK2;
  498. else if (a->top == 1)
  499. return a->d[0];
  500. /* a->top == 0 */
  501. return 0;
  502. }
  503. int BN_set_word(BIGNUM *a, BN_ULONG w)
  504. {
  505. bn_check_top(a);
  506. if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
  507. return (0);
  508. a->neg = 0;
  509. a->d[0] = w;
  510. a->top = (w ? 1 : 0);
  511. a->flags &= ~BN_FLG_FIXED_TOP;
  512. bn_check_top(a);
  513. return (1);
  514. }
  515. BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
  516. {
  517. unsigned int i, m;
  518. unsigned int n;
  519. BN_ULONG l;
  520. BIGNUM *bn = NULL;
  521. if (ret == NULL)
  522. ret = bn = BN_new();
  523. if (ret == NULL)
  524. return (NULL);
  525. bn_check_top(ret);
  526. l = 0;
  527. n = len;
  528. if (n == 0) {
  529. ret->top = 0;
  530. return (ret);
  531. }
  532. i = ((n - 1) / BN_BYTES) + 1;
  533. m = ((n - 1) % (BN_BYTES));
  534. if (bn_wexpand(ret, (int)i) == NULL) {
  535. if (bn)
  536. BN_free(bn);
  537. return NULL;
  538. }
  539. ret->top = i;
  540. ret->neg = 0;
  541. while (n--) {
  542. l = (l << 8L) | *(s++);
  543. if (m-- == 0) {
  544. ret->d[--i] = l;
  545. l = 0;
  546. m = BN_BYTES - 1;
  547. }
  548. }
  549. /*
  550. * need to call this due to clear byte at top if avoiding having the top
  551. * bit set (-ve number)
  552. */
  553. bn_correct_top(ret);
  554. return (ret);
  555. }
  556. /* ignore negative */
  557. static int bn2binpad(const BIGNUM *a, unsigned char *to, int tolen)
  558. {
  559. int n;
  560. size_t i, lasti, j, atop, mask;
  561. BN_ULONG l;
  562. /*
  563. * In case |a| is fixed-top, BN_num_bytes can return bogus length,
  564. * but it's assumed that fixed-top inputs ought to be "nominated"
  565. * even for padded output, so it works out...
  566. */
  567. n = BN_num_bytes(a);
  568. if (tolen == -1) {
  569. tolen = n;
  570. } else if (tolen < n) { /* uncommon/unlike case */
  571. BIGNUM temp = *a;
  572. bn_correct_top(&temp);
  573. n = BN_num_bytes(&temp);
  574. if (tolen < n)
  575. return -1;
  576. }
  577. /* Swipe through whole available data and don't give away padded zero. */
  578. atop = a->dmax * BN_BYTES;
  579. if (atop == 0) {
  580. OPENSSL_cleanse(to, tolen);
  581. return tolen;
  582. }
  583. lasti = atop - 1;
  584. atop = a->top * BN_BYTES;
  585. for (i = 0, j = 0, to += tolen; j < (size_t)tolen; j++) {
  586. l = a->d[i / BN_BYTES];
  587. mask = 0 - ((j - atop) >> (8 * sizeof(i) - 1));
  588. *--to = (unsigned char)(l >> (8 * (i % BN_BYTES)) & mask);
  589. i += (i - lasti) >> (8 * sizeof(i) - 1); /* stay on last limb */
  590. }
  591. return tolen;
  592. }
  593. int bn_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen)
  594. {
  595. if (tolen < 0)
  596. return -1;
  597. return bn2binpad(a, to, tolen);
  598. }
  599. int BN_bn2bin(const BIGNUM *a, unsigned char *to)
  600. {
  601. int n, i;
  602. BN_ULONG l;
  603. bn_check_top(a);
  604. n = i = BN_num_bytes(a);
  605. while (i--) {
  606. l = a->d[i / BN_BYTES];
  607. *(to++) = (unsigned char)(l >> (8 * (i % BN_BYTES))) & 0xff;
  608. }
  609. return (n);
  610. }
  611. int BN_ucmp(const BIGNUM *a, const BIGNUM *b)
  612. {
  613. int i;
  614. BN_ULONG t1, t2, *ap, *bp;
  615. bn_check_top(a);
  616. bn_check_top(b);
  617. i = a->top - b->top;
  618. if (i != 0)
  619. return (i);
  620. ap = a->d;
  621. bp = b->d;
  622. for (i = a->top - 1; i >= 0; i--) {
  623. t1 = ap[i];
  624. t2 = bp[i];
  625. if (t1 != t2)
  626. return ((t1 > t2) ? 1 : -1);
  627. }
  628. return (0);
  629. }
  630. int BN_cmp(const BIGNUM *a, const BIGNUM *b)
  631. {
  632. int i;
  633. int gt, lt;
  634. BN_ULONG t1, t2;
  635. if ((a == NULL) || (b == NULL)) {
  636. if (a != NULL)
  637. return (-1);
  638. else if (b != NULL)
  639. return (1);
  640. else
  641. return (0);
  642. }
  643. bn_check_top(a);
  644. bn_check_top(b);
  645. if (a->neg != b->neg) {
  646. if (a->neg)
  647. return (-1);
  648. else
  649. return (1);
  650. }
  651. if (a->neg == 0) {
  652. gt = 1;
  653. lt = -1;
  654. } else {
  655. gt = -1;
  656. lt = 1;
  657. }
  658. if (a->top > b->top)
  659. return (gt);
  660. if (a->top < b->top)
  661. return (lt);
  662. for (i = a->top - 1; i >= 0; i--) {
  663. t1 = a->d[i];
  664. t2 = b->d[i];
  665. if (t1 > t2)
  666. return (gt);
  667. if (t1 < t2)
  668. return (lt);
  669. }
  670. return (0);
  671. }
  672. int BN_set_bit(BIGNUM *a, int n)
  673. {
  674. int i, j, k;
  675. if (n < 0)
  676. return 0;
  677. i = n / BN_BITS2;
  678. j = n % BN_BITS2;
  679. if (a->top <= i) {
  680. if (bn_wexpand(a, i + 1) == NULL)
  681. return (0);
  682. for (k = a->top; k < i + 1; k++)
  683. a->d[k] = 0;
  684. a->top = i + 1;
  685. a->flags &= ~BN_FLG_FIXED_TOP;
  686. }
  687. a->d[i] |= (((BN_ULONG)1) << j);
  688. bn_check_top(a);
  689. return (1);
  690. }
  691. int BN_clear_bit(BIGNUM *a, int n)
  692. {
  693. int i, j;
  694. bn_check_top(a);
  695. if (n < 0)
  696. return 0;
  697. i = n / BN_BITS2;
  698. j = n % BN_BITS2;
  699. if (a->top <= i)
  700. return (0);
  701. a->d[i] &= (~(((BN_ULONG)1) << j));
  702. bn_correct_top(a);
  703. return (1);
  704. }
  705. int BN_is_bit_set(const BIGNUM *a, int n)
  706. {
  707. int i, j;
  708. bn_check_top(a);
  709. if (n < 0)
  710. return 0;
  711. i = n / BN_BITS2;
  712. j = n % BN_BITS2;
  713. if (a->top <= i)
  714. return 0;
  715. return (int)(((a->d[i]) >> j) & ((BN_ULONG)1));
  716. }
  717. int BN_mask_bits(BIGNUM *a, int n)
  718. {
  719. int b, w;
  720. bn_check_top(a);
  721. if (n < 0)
  722. return 0;
  723. w = n / BN_BITS2;
  724. b = n % BN_BITS2;
  725. if (w >= a->top)
  726. return 0;
  727. if (b == 0)
  728. a->top = w;
  729. else {
  730. a->top = w + 1;
  731. a->d[w] &= ~(BN_MASK2 << b);
  732. }
  733. bn_correct_top(a);
  734. return (1);
  735. }
  736. void BN_set_negative(BIGNUM *a, int b)
  737. {
  738. if (b && !BN_is_zero(a))
  739. a->neg = 1;
  740. else
  741. a->neg = 0;
  742. }
  743. int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
  744. {
  745. int i;
  746. BN_ULONG aa, bb;
  747. if (n == 0)
  748. return 0;
  749. aa = a[n - 1];
  750. bb = b[n - 1];
  751. if (aa != bb)
  752. return ((aa > bb) ? 1 : -1);
  753. for (i = n - 2; i >= 0; i--) {
  754. aa = a[i];
  755. bb = b[i];
  756. if (aa != bb)
  757. return ((aa > bb) ? 1 : -1);
  758. }
  759. return (0);
  760. }
  761. /*
  762. * Here follows a specialised variants of bn_cmp_words(). It has the
  763. * property of performing the operation on arrays of different sizes. The
  764. * sizes of those arrays is expressed through cl, which is the common length
  765. * ( basicall, min(len(a),len(b)) ), and dl, which is the delta between the
  766. * two lengths, calculated as len(a)-len(b). All lengths are the number of
  767. * BN_ULONGs...
  768. */
  769. int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl)
  770. {
  771. int n, i;
  772. n = cl - 1;
  773. if (dl < 0) {
  774. for (i = dl; i < 0; i++) {
  775. if (b[n - i] != 0)
  776. return -1; /* a < b */
  777. }
  778. }
  779. if (dl > 0) {
  780. for (i = dl; i > 0; i--) {
  781. if (a[n + i] != 0)
  782. return 1; /* a > b */
  783. }
  784. }
  785. return bn_cmp_words(a, b, cl);
  786. }
  787. /*
  788. * Constant-time conditional swap of a and b.
  789. * a and b are swapped if condition is not 0. The code assumes that at most one bit of condition is set.
  790. * nwords is the number of words to swap. The code assumes that at least nwords are allocated in both a and b,
  791. * and that no more than nwords are used by either a or b.
  792. * a and b cannot be the same number
  793. */
  794. void BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords)
  795. {
  796. BN_ULONG t;
  797. int i;
  798. bn_wcheck_size(a, nwords);
  799. bn_wcheck_size(b, nwords);
  800. assert(a != b);
  801. assert((condition & (condition - 1)) == 0);
  802. assert(sizeof(BN_ULONG) >= sizeof(int));
  803. condition = ((condition - 1) >> (BN_BITS2 - 1)) - 1;
  804. t = (a->top ^ b->top) & condition;
  805. a->top ^= t;
  806. b->top ^= t;
  807. t = (a->neg ^ b->neg) & condition;
  808. a->neg ^= t;
  809. b->neg ^= t;
  810. /*-
  811. * BN_FLG_STATIC_DATA: indicates that data may not be written to. Intention
  812. * is actually to treat it as it's read-only data, and some (if not most)
  813. * of it does reside in read-only segment. In other words observation of
  814. * BN_FLG_STATIC_DATA in BN_consttime_swap should be treated as fatal
  815. * condition. It would either cause SEGV or effectively cause data
  816. * corruption.
  817. *
  818. * BN_FLG_MALLOCED: refers to BN structure itself, and hence must be
  819. * preserved.
  820. *
  821. * BN_FLG_SECURE: must be preserved, because it determines how x->d was
  822. * allocated and hence how to free it.
  823. *
  824. * BN_FLG_CONSTTIME: sufficient to mask and swap
  825. *
  826. * BN_FLG_FIXED_TOP: indicates that we haven't called bn_correct_top() on
  827. * the data, so the d array may be padded with additional 0 values (i.e.
  828. * top could be greater than the minimal value that it could be). We should
  829. * be swapping it
  830. */
  831. #define BN_CONSTTIME_SWAP_FLAGS (BN_FLG_CONSTTIME | BN_FLG_FIXED_TOP)
  832. t = ((a->flags ^ b->flags) & BN_CONSTTIME_SWAP_FLAGS) & condition;
  833. a->flags ^= t;
  834. b->flags ^= t;
  835. #define BN_CONSTTIME_SWAP(ind) \
  836. do { \
  837. t = (a->d[ind] ^ b->d[ind]) & condition; \
  838. a->d[ind] ^= t; \
  839. b->d[ind] ^= t; \
  840. } while (0)
  841. switch (nwords) {
  842. default:
  843. for (i = 10; i < nwords; i++)
  844. BN_CONSTTIME_SWAP(i);
  845. /* Fallthrough */
  846. case 10:
  847. BN_CONSTTIME_SWAP(9); /* Fallthrough */
  848. case 9:
  849. BN_CONSTTIME_SWAP(8); /* Fallthrough */
  850. case 8:
  851. BN_CONSTTIME_SWAP(7); /* Fallthrough */
  852. case 7:
  853. BN_CONSTTIME_SWAP(6); /* Fallthrough */
  854. case 6:
  855. BN_CONSTTIME_SWAP(5); /* Fallthrough */
  856. case 5:
  857. BN_CONSTTIME_SWAP(4); /* Fallthrough */
  858. case 4:
  859. BN_CONSTTIME_SWAP(3); /* Fallthrough */
  860. case 3:
  861. BN_CONSTTIME_SWAP(2); /* Fallthrough */
  862. case 2:
  863. BN_CONSTTIME_SWAP(1); /* Fallthrough */
  864. case 1:
  865. BN_CONSTTIME_SWAP(0);
  866. }
  867. #undef BN_CONSTTIME_SWAP
  868. }