bn_lib.c 23 KB

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