bn_lib.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  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. bn_check_top(b);
  241. if (words > (INT_MAX / (4 * BN_BITS2))) {
  242. BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
  243. return NULL;
  244. }
  245. if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
  246. BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
  247. return (NULL);
  248. }
  249. a = A = (BN_ULONG *)OPENSSL_malloc(sizeof(BN_ULONG) * words);
  250. if (A == NULL) {
  251. BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
  252. return (NULL);
  253. }
  254. #ifdef PURIFY
  255. /*
  256. * Valgrind complains in BN_consttime_swap because we process the whole
  257. * array even if it's not initialised yet. This doesn't matter in that
  258. * function - what's important is constant time operation (we're not
  259. * actually going to use the data)
  260. */
  261. memset(a, 0, sizeof(BN_ULONG) * words);
  262. #endif
  263. #if 1
  264. B = b->d;
  265. /* Check if the previous number needs to be copied */
  266. if (B != NULL) {
  267. for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {
  268. /*
  269. * The fact that the loop is unrolled
  270. * 4-wise is a tribute to Intel. It's
  271. * the one that doesn't have enough
  272. * registers to accomodate more data.
  273. * I'd unroll it 8-wise otherwise:-)
  274. *
  275. * <[email protected]>
  276. */
  277. BN_ULONG a0, a1, a2, a3;
  278. a0 = B[0];
  279. a1 = B[1];
  280. a2 = B[2];
  281. a3 = B[3];
  282. A[0] = a0;
  283. A[1] = a1;
  284. A[2] = a2;
  285. A[3] = a3;
  286. }
  287. /*
  288. * workaround for ultrix cc: without 'case 0', the optimizer does
  289. * the switch table by doing a=top&3; a--; goto jump_table[a];
  290. * which fails for top== 0
  291. */
  292. switch (b->top & 3) {
  293. case 3:
  294. A[2] = B[2];
  295. case 2:
  296. A[1] = B[1];
  297. case 1:
  298. A[0] = B[0];
  299. case 0:
  300. ;
  301. }
  302. }
  303. #else
  304. memset(A, 0, sizeof(BN_ULONG) * words);
  305. memcpy(A, b->d, sizeof(b->d[0]) * b->top);
  306. #endif
  307. return (a);
  308. }
  309. /*
  310. * This is an internal function that can be used instead of bn_expand2() when
  311. * there is a need to copy BIGNUMs instead of only expanding the data part,
  312. * while still expanding them. Especially useful when needing to expand
  313. * BIGNUMs that are declared 'const' and should therefore not be changed. The
  314. * reason to use this instead of a BN_dup() followed by a bn_expand2() is
  315. * memory allocation overhead. A BN_dup() followed by a bn_expand2() will
  316. * allocate new memory for the BIGNUM data twice, and free it once, while
  317. * bn_dup_expand() makes sure allocation is made only once.
  318. */
  319. #ifndef OPENSSL_NO_DEPRECATED
  320. BIGNUM *bn_dup_expand(const BIGNUM *b, int words)
  321. {
  322. BIGNUM *r = NULL;
  323. bn_check_top(b);
  324. /*
  325. * This function does not work if words <= b->dmax && top < words because
  326. * BN_dup() does not preserve 'dmax'! (But bn_dup_expand() is not used
  327. * anywhere yet.)
  328. */
  329. if (words > b->dmax) {
  330. BN_ULONG *a = bn_expand_internal(b, words);
  331. if (a) {
  332. r = BN_new();
  333. if (r) {
  334. r->top = b->top;
  335. r->dmax = words;
  336. r->neg = b->neg;
  337. r->d = a;
  338. } else {
  339. /* r == NULL, BN_new failure */
  340. OPENSSL_free(a);
  341. }
  342. }
  343. /*
  344. * If a == NULL, there was an error in allocation in
  345. * bn_expand_internal(), and NULL should be returned
  346. */
  347. } else {
  348. r = BN_dup(b);
  349. }
  350. bn_check_top(r);
  351. return r;
  352. }
  353. #endif
  354. /*
  355. * This is an internal function that should not be used in applications. It
  356. * ensures that 'b' has enough room for a 'words' word number and initialises
  357. * any unused part of b->d with leading zeros. It is mostly used by the
  358. * various BIGNUM routines. If there is an error, NULL is returned. If not,
  359. * 'b' is returned.
  360. */
  361. BIGNUM *bn_expand2(BIGNUM *b, int words)
  362. {
  363. bn_check_top(b);
  364. if (words > b->dmax) {
  365. BN_ULONG *a = bn_expand_internal(b, words);
  366. if (!a)
  367. return NULL;
  368. if (b->d)
  369. OPENSSL_free(b->d);
  370. b->d = a;
  371. b->dmax = words;
  372. }
  373. /* None of this should be necessary because of what b->top means! */
  374. #if 0
  375. /*
  376. * NB: bn_wexpand() calls this only if the BIGNUM really has to grow
  377. */
  378. if (b->top < b->dmax) {
  379. int i;
  380. BN_ULONG *A = &(b->d[b->top]);
  381. for (i = (b->dmax - b->top) >> 3; i > 0; i--, A += 8) {
  382. A[0] = 0;
  383. A[1] = 0;
  384. A[2] = 0;
  385. A[3] = 0;
  386. A[4] = 0;
  387. A[5] = 0;
  388. A[6] = 0;
  389. A[7] = 0;
  390. }
  391. for (i = (b->dmax - b->top) & 7; i > 0; i--, A++)
  392. A[0] = 0;
  393. assert(A == &(b->d[b->dmax]));
  394. }
  395. #endif
  396. bn_check_top(b);
  397. return b;
  398. }
  399. BIGNUM *BN_dup(const BIGNUM *a)
  400. {
  401. BIGNUM *t;
  402. if (a == NULL)
  403. return NULL;
  404. bn_check_top(a);
  405. t = BN_new();
  406. if (t == NULL)
  407. return NULL;
  408. if (!BN_copy(t, a)) {
  409. BN_free(t);
  410. return NULL;
  411. }
  412. bn_check_top(t);
  413. return t;
  414. }
  415. BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
  416. {
  417. int i;
  418. BN_ULONG *A;
  419. const BN_ULONG *B;
  420. bn_check_top(b);
  421. if (a == b)
  422. return (a);
  423. if (bn_wexpand(a, b->top) == NULL)
  424. return (NULL);
  425. #if 1
  426. A = a->d;
  427. B = b->d;
  428. for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {
  429. BN_ULONG a0, a1, a2, a3;
  430. a0 = B[0];
  431. a1 = B[1];
  432. a2 = B[2];
  433. a3 = B[3];
  434. A[0] = a0;
  435. A[1] = a1;
  436. A[2] = a2;
  437. A[3] = a3;
  438. }
  439. /* ultrix cc workaround, see comments in bn_expand_internal */
  440. switch (b->top & 3) {
  441. case 3:
  442. A[2] = B[2];
  443. case 2:
  444. A[1] = B[1];
  445. case 1:
  446. A[0] = B[0];
  447. case 0:;
  448. }
  449. #else
  450. memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);
  451. #endif
  452. a->top = b->top;
  453. a->neg = b->neg;
  454. bn_check_top(a);
  455. return (a);
  456. }
  457. void BN_swap(BIGNUM *a, BIGNUM *b)
  458. {
  459. int flags_old_a, flags_old_b;
  460. BN_ULONG *tmp_d;
  461. int tmp_top, tmp_dmax, tmp_neg;
  462. bn_check_top(a);
  463. bn_check_top(b);
  464. flags_old_a = a->flags;
  465. flags_old_b = b->flags;
  466. tmp_d = a->d;
  467. tmp_top = a->top;
  468. tmp_dmax = a->dmax;
  469. tmp_neg = a->neg;
  470. a->d = b->d;
  471. a->top = b->top;
  472. a->dmax = b->dmax;
  473. a->neg = b->neg;
  474. b->d = tmp_d;
  475. b->top = tmp_top;
  476. b->dmax = tmp_dmax;
  477. b->neg = tmp_neg;
  478. a->flags =
  479. (flags_old_a & BN_FLG_MALLOCED) | (flags_old_b & BN_FLG_STATIC_DATA);
  480. b->flags =
  481. (flags_old_b & BN_FLG_MALLOCED) | (flags_old_a & BN_FLG_STATIC_DATA);
  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. }
  493. BN_ULONG BN_get_word(const BIGNUM *a)
  494. {
  495. if (a->top > 1)
  496. return BN_MASK2;
  497. else if (a->top == 1)
  498. return a->d[0];
  499. /* a->top == 0 */
  500. return 0;
  501. }
  502. int BN_set_word(BIGNUM *a, BN_ULONG w)
  503. {
  504. bn_check_top(a);
  505. if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
  506. return (0);
  507. a->neg = 0;
  508. a->d[0] = w;
  509. a->top = (w ? 1 : 0);
  510. bn_check_top(a);
  511. return (1);
  512. }
  513. BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
  514. {
  515. unsigned int i, m;
  516. unsigned int n;
  517. BN_ULONG l;
  518. BIGNUM *bn = NULL;
  519. if (ret == NULL)
  520. ret = bn = BN_new();
  521. if (ret == NULL)
  522. return (NULL);
  523. bn_check_top(ret);
  524. l = 0;
  525. n = len;
  526. if (n == 0) {
  527. ret->top = 0;
  528. return (ret);
  529. }
  530. i = ((n - 1) / BN_BYTES) + 1;
  531. m = ((n - 1) % (BN_BYTES));
  532. if (bn_wexpand(ret, (int)i) == NULL) {
  533. if (bn)
  534. BN_free(bn);
  535. return NULL;
  536. }
  537. ret->top = i;
  538. ret->neg = 0;
  539. while (n--) {
  540. l = (l << 8L) | *(s++);
  541. if (m-- == 0) {
  542. ret->d[--i] = l;
  543. l = 0;
  544. m = BN_BYTES - 1;
  545. }
  546. }
  547. /*
  548. * need to call this due to clear byte at top if avoiding having the top
  549. * bit set (-ve number)
  550. */
  551. bn_correct_top(ret);
  552. return (ret);
  553. }
  554. /* ignore negative */
  555. int BN_bn2bin(const BIGNUM *a, unsigned char *to)
  556. {
  557. int n, i;
  558. BN_ULONG l;
  559. bn_check_top(a);
  560. n = i = BN_num_bytes(a);
  561. while (i--) {
  562. l = a->d[i / BN_BYTES];
  563. *(to++) = (unsigned char)(l >> (8 * (i % BN_BYTES))) & 0xff;
  564. }
  565. return (n);
  566. }
  567. int BN_ucmp(const BIGNUM *a, const BIGNUM *b)
  568. {
  569. int i;
  570. BN_ULONG t1, t2, *ap, *bp;
  571. bn_check_top(a);
  572. bn_check_top(b);
  573. i = a->top - b->top;
  574. if (i != 0)
  575. return (i);
  576. ap = a->d;
  577. bp = b->d;
  578. for (i = a->top - 1; i >= 0; i--) {
  579. t1 = ap[i];
  580. t2 = bp[i];
  581. if (t1 != t2)
  582. return ((t1 > t2) ? 1 : -1);
  583. }
  584. return (0);
  585. }
  586. int BN_cmp(const BIGNUM *a, const BIGNUM *b)
  587. {
  588. int i;
  589. int gt, lt;
  590. BN_ULONG t1, t2;
  591. if ((a == NULL) || (b == NULL)) {
  592. if (a != NULL)
  593. return (-1);
  594. else if (b != NULL)
  595. return (1);
  596. else
  597. return (0);
  598. }
  599. bn_check_top(a);
  600. bn_check_top(b);
  601. if (a->neg != b->neg) {
  602. if (a->neg)
  603. return (-1);
  604. else
  605. return (1);
  606. }
  607. if (a->neg == 0) {
  608. gt = 1;
  609. lt = -1;
  610. } else {
  611. gt = -1;
  612. lt = 1;
  613. }
  614. if (a->top > b->top)
  615. return (gt);
  616. if (a->top < b->top)
  617. return (lt);
  618. for (i = a->top - 1; i >= 0; i--) {
  619. t1 = a->d[i];
  620. t2 = b->d[i];
  621. if (t1 > t2)
  622. return (gt);
  623. if (t1 < t2)
  624. return (lt);
  625. }
  626. return (0);
  627. }
  628. int BN_set_bit(BIGNUM *a, int n)
  629. {
  630. int i, j, k;
  631. if (n < 0)
  632. return 0;
  633. i = n / BN_BITS2;
  634. j = n % BN_BITS2;
  635. if (a->top <= i) {
  636. if (bn_wexpand(a, i + 1) == NULL)
  637. return (0);
  638. for (k = a->top; k < i + 1; k++)
  639. a->d[k] = 0;
  640. a->top = i + 1;
  641. }
  642. a->d[i] |= (((BN_ULONG)1) << j);
  643. bn_check_top(a);
  644. return (1);
  645. }
  646. int BN_clear_bit(BIGNUM *a, int n)
  647. {
  648. int i, j;
  649. bn_check_top(a);
  650. if (n < 0)
  651. return 0;
  652. i = n / BN_BITS2;
  653. j = n % BN_BITS2;
  654. if (a->top <= i)
  655. return (0);
  656. a->d[i] &= (~(((BN_ULONG)1) << j));
  657. bn_correct_top(a);
  658. return (1);
  659. }
  660. int BN_is_bit_set(const BIGNUM *a, int n)
  661. {
  662. int i, j;
  663. bn_check_top(a);
  664. if (n < 0)
  665. return 0;
  666. i = n / BN_BITS2;
  667. j = n % BN_BITS2;
  668. if (a->top <= i)
  669. return 0;
  670. return (int)(((a->d[i]) >> j) & ((BN_ULONG)1));
  671. }
  672. int BN_mask_bits(BIGNUM *a, int n)
  673. {
  674. int b, w;
  675. bn_check_top(a);
  676. if (n < 0)
  677. return 0;
  678. w = n / BN_BITS2;
  679. b = n % BN_BITS2;
  680. if (w >= a->top)
  681. return 0;
  682. if (b == 0)
  683. a->top = w;
  684. else {
  685. a->top = w + 1;
  686. a->d[w] &= ~(BN_MASK2 << b);
  687. }
  688. bn_correct_top(a);
  689. return (1);
  690. }
  691. void BN_set_negative(BIGNUM *a, int b)
  692. {
  693. if (b && !BN_is_zero(a))
  694. a->neg = 1;
  695. else
  696. a->neg = 0;
  697. }
  698. int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
  699. {
  700. int i;
  701. BN_ULONG aa, bb;
  702. aa = a[n - 1];
  703. bb = b[n - 1];
  704. if (aa != bb)
  705. return ((aa > bb) ? 1 : -1);
  706. for (i = n - 2; i >= 0; i--) {
  707. aa = a[i];
  708. bb = b[i];
  709. if (aa != bb)
  710. return ((aa > bb) ? 1 : -1);
  711. }
  712. return (0);
  713. }
  714. /*
  715. * Here follows a specialised variants of bn_cmp_words(). It has the
  716. * property of performing the operation on arrays of different sizes. The
  717. * sizes of those arrays is expressed through cl, which is the common length
  718. * ( basicall, min(len(a),len(b)) ), and dl, which is the delta between the
  719. * two lengths, calculated as len(a)-len(b). All lengths are the number of
  720. * BN_ULONGs...
  721. */
  722. int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl)
  723. {
  724. int n, i;
  725. n = cl - 1;
  726. if (dl < 0) {
  727. for (i = dl; i < 0; i++) {
  728. if (b[n - i] != 0)
  729. return -1; /* a < b */
  730. }
  731. }
  732. if (dl > 0) {
  733. for (i = dl; i > 0; i--) {
  734. if (a[n + i] != 0)
  735. return 1; /* a > b */
  736. }
  737. }
  738. return bn_cmp_words(a, b, cl);
  739. }
  740. /*
  741. * Constant-time conditional swap of a and b.
  742. * a and b are swapped if condition is not 0. The code assumes that at most one bit of condition is set.
  743. * nwords is the number of words to swap. The code assumes that at least nwords are allocated in both a and b,
  744. * and that no more than nwords are used by either a or b.
  745. * a and b cannot be the same number
  746. */
  747. void BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords)
  748. {
  749. BN_ULONG t;
  750. int i;
  751. bn_wcheck_size(a, nwords);
  752. bn_wcheck_size(b, nwords);
  753. assert(a != b);
  754. assert((condition & (condition - 1)) == 0);
  755. assert(sizeof(BN_ULONG) >= sizeof(int));
  756. condition = ((condition - 1) >> (BN_BITS2 - 1)) - 1;
  757. t = (a->top ^ b->top) & condition;
  758. a->top ^= t;
  759. b->top ^= t;
  760. #define BN_CONSTTIME_SWAP(ind) \
  761. do { \
  762. t = (a->d[ind] ^ b->d[ind]) & condition; \
  763. a->d[ind] ^= t; \
  764. b->d[ind] ^= t; \
  765. } while (0)
  766. switch (nwords) {
  767. default:
  768. for (i = 10; i < nwords; i++)
  769. BN_CONSTTIME_SWAP(i);
  770. /* Fallthrough */
  771. case 10:
  772. BN_CONSTTIME_SWAP(9); /* Fallthrough */
  773. case 9:
  774. BN_CONSTTIME_SWAP(8); /* Fallthrough */
  775. case 8:
  776. BN_CONSTTIME_SWAP(7); /* Fallthrough */
  777. case 7:
  778. BN_CONSTTIME_SWAP(6); /* Fallthrough */
  779. case 6:
  780. BN_CONSTTIME_SWAP(5); /* Fallthrough */
  781. case 5:
  782. BN_CONSTTIME_SWAP(4); /* Fallthrough */
  783. case 4:
  784. BN_CONSTTIME_SWAP(3); /* Fallthrough */
  785. case 3:
  786. BN_CONSTTIME_SWAP(2); /* Fallthrough */
  787. case 2:
  788. BN_CONSTTIME_SWAP(1); /* Fallthrough */
  789. case 1:
  790. BN_CONSTTIME_SWAP(0);
  791. }
  792. #undef BN_CONSTTIME_SWAP
  793. }