bn_lib.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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. 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. * <[email protected]>
  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. if (BN_get_flags(b, BN_FLG_CONSTTIME) != 0)
  486. BN_set_flags(a, BN_FLG_CONSTTIME);
  487. a->top = b->top;
  488. a->neg = b->neg;
  489. bn_check_top(a);
  490. return (a);
  491. }
  492. void BN_swap(BIGNUM *a, BIGNUM *b)
  493. {
  494. int flags_old_a, flags_old_b;
  495. BN_ULONG *tmp_d;
  496. int tmp_top, tmp_dmax, tmp_neg;
  497. bn_check_top(a);
  498. bn_check_top(b);
  499. flags_old_a = a->flags;
  500. flags_old_b = b->flags;
  501. tmp_d = a->d;
  502. tmp_top = a->top;
  503. tmp_dmax = a->dmax;
  504. tmp_neg = a->neg;
  505. a->d = b->d;
  506. a->top = b->top;
  507. a->dmax = b->dmax;
  508. a->neg = b->neg;
  509. b->d = tmp_d;
  510. b->top = tmp_top;
  511. b->dmax = tmp_dmax;
  512. b->neg = tmp_neg;
  513. a->flags =
  514. (flags_old_a & BN_FLG_MALLOCED) | (flags_old_b & BN_FLG_STATIC_DATA);
  515. b->flags =
  516. (flags_old_b & BN_FLG_MALLOCED) | (flags_old_a & BN_FLG_STATIC_DATA);
  517. bn_check_top(a);
  518. bn_check_top(b);
  519. }
  520. void BN_clear(BIGNUM *a)
  521. {
  522. bn_check_top(a);
  523. if (a->d != NULL)
  524. OPENSSL_cleanse(a->d, a->dmax * sizeof(a->d[0]));
  525. a->top = 0;
  526. a->neg = 0;
  527. }
  528. BN_ULONG BN_get_word(const BIGNUM *a)
  529. {
  530. if (a->top > 1)
  531. return BN_MASK2;
  532. else if (a->top == 1)
  533. return a->d[0];
  534. /* a->top == 0 */
  535. return 0;
  536. }
  537. int BN_set_word(BIGNUM *a, BN_ULONG w)
  538. {
  539. bn_check_top(a);
  540. if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
  541. return (0);
  542. a->neg = 0;
  543. a->d[0] = w;
  544. a->top = (w ? 1 : 0);
  545. bn_check_top(a);
  546. return (1);
  547. }
  548. BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
  549. {
  550. unsigned int i, m;
  551. unsigned int n;
  552. BN_ULONG l;
  553. BIGNUM *bn = NULL;
  554. if (ret == NULL)
  555. ret = bn = BN_new();
  556. if (ret == NULL)
  557. return (NULL);
  558. bn_check_top(ret);
  559. l = 0;
  560. n = len;
  561. if (n == 0) {
  562. ret->top = 0;
  563. return (ret);
  564. }
  565. i = ((n - 1) / BN_BYTES) + 1;
  566. m = ((n - 1) % (BN_BYTES));
  567. if (bn_wexpand(ret, (int)i) == NULL) {
  568. if (bn)
  569. BN_free(bn);
  570. return NULL;
  571. }
  572. ret->top = i;
  573. ret->neg = 0;
  574. while (n--) {
  575. l = (l << 8L) | *(s++);
  576. if (m-- == 0) {
  577. ret->d[--i] = l;
  578. l = 0;
  579. m = BN_BYTES - 1;
  580. }
  581. }
  582. /*
  583. * need to call this due to clear byte at top if avoiding having the top
  584. * bit set (-ve number)
  585. */
  586. bn_correct_top(ret);
  587. return (ret);
  588. }
  589. /* ignore negative */
  590. int BN_bn2bin(const BIGNUM *a, unsigned char *to)
  591. {
  592. int n, i;
  593. BN_ULONG l;
  594. bn_check_top(a);
  595. n = i = BN_num_bytes(a);
  596. while (i--) {
  597. l = a->d[i / BN_BYTES];
  598. *(to++) = (unsigned char)(l >> (8 * (i % BN_BYTES))) & 0xff;
  599. }
  600. return (n);
  601. }
  602. int BN_ucmp(const BIGNUM *a, const BIGNUM *b)
  603. {
  604. int i;
  605. BN_ULONG t1, t2, *ap, *bp;
  606. bn_check_top(a);
  607. bn_check_top(b);
  608. i = a->top - b->top;
  609. if (i != 0)
  610. return (i);
  611. ap = a->d;
  612. bp = b->d;
  613. for (i = a->top - 1; i >= 0; i--) {
  614. t1 = ap[i];
  615. t2 = bp[i];
  616. if (t1 != t2)
  617. return ((t1 > t2) ? 1 : -1);
  618. }
  619. return (0);
  620. }
  621. int BN_cmp(const BIGNUM *a, const BIGNUM *b)
  622. {
  623. int i;
  624. int gt, lt;
  625. BN_ULONG t1, t2;
  626. if ((a == NULL) || (b == NULL)) {
  627. if (a != NULL)
  628. return (-1);
  629. else if (b != NULL)
  630. return (1);
  631. else
  632. return (0);
  633. }
  634. bn_check_top(a);
  635. bn_check_top(b);
  636. if (a->neg != b->neg) {
  637. if (a->neg)
  638. return (-1);
  639. else
  640. return (1);
  641. }
  642. if (a->neg == 0) {
  643. gt = 1;
  644. lt = -1;
  645. } else {
  646. gt = -1;
  647. lt = 1;
  648. }
  649. if (a->top > b->top)
  650. return (gt);
  651. if (a->top < b->top)
  652. return (lt);
  653. for (i = a->top - 1; i >= 0; i--) {
  654. t1 = a->d[i];
  655. t2 = b->d[i];
  656. if (t1 > t2)
  657. return (gt);
  658. if (t1 < t2)
  659. return (lt);
  660. }
  661. return (0);
  662. }
  663. int BN_set_bit(BIGNUM *a, int n)
  664. {
  665. int i, j, k;
  666. if (n < 0)
  667. return 0;
  668. i = n / BN_BITS2;
  669. j = n % BN_BITS2;
  670. if (a->top <= i) {
  671. if (bn_wexpand(a, i + 1) == NULL)
  672. return (0);
  673. for (k = a->top; k < i + 1; k++)
  674. a->d[k] = 0;
  675. a->top = i + 1;
  676. }
  677. a->d[i] |= (((BN_ULONG)1) << j);
  678. bn_check_top(a);
  679. return (1);
  680. }
  681. int BN_clear_bit(BIGNUM *a, int n)
  682. {
  683. int i, j;
  684. bn_check_top(a);
  685. if (n < 0)
  686. return 0;
  687. i = n / BN_BITS2;
  688. j = n % BN_BITS2;
  689. if (a->top <= i)
  690. return (0);
  691. a->d[i] &= (~(((BN_ULONG)1) << j));
  692. bn_correct_top(a);
  693. return (1);
  694. }
  695. int BN_is_bit_set(const BIGNUM *a, int n)
  696. {
  697. int i, j;
  698. bn_check_top(a);
  699. if (n < 0)
  700. return 0;
  701. i = n / BN_BITS2;
  702. j = n % BN_BITS2;
  703. if (a->top <= i)
  704. return 0;
  705. return (int)(((a->d[i]) >> j) & ((BN_ULONG)1));
  706. }
  707. int BN_mask_bits(BIGNUM *a, int n)
  708. {
  709. int b, w;
  710. bn_check_top(a);
  711. if (n < 0)
  712. return 0;
  713. w = n / BN_BITS2;
  714. b = n % BN_BITS2;
  715. if (w >= a->top)
  716. return 0;
  717. if (b == 0)
  718. a->top = w;
  719. else {
  720. a->top = w + 1;
  721. a->d[w] &= ~(BN_MASK2 << b);
  722. }
  723. bn_correct_top(a);
  724. return (1);
  725. }
  726. void BN_set_negative(BIGNUM *a, int b)
  727. {
  728. if (b && !BN_is_zero(a))
  729. a->neg = 1;
  730. else
  731. a->neg = 0;
  732. }
  733. int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
  734. {
  735. int i;
  736. BN_ULONG aa, bb;
  737. aa = a[n - 1];
  738. bb = b[n - 1];
  739. if (aa != bb)
  740. return ((aa > bb) ? 1 : -1);
  741. for (i = n - 2; i >= 0; i--) {
  742. aa = a[i];
  743. bb = b[i];
  744. if (aa != bb)
  745. return ((aa > bb) ? 1 : -1);
  746. }
  747. return (0);
  748. }
  749. /*
  750. * Here follows a specialised variants of bn_cmp_words(). It has the
  751. * property of performing the operation on arrays of different sizes. The
  752. * sizes of those arrays is expressed through cl, which is the common length
  753. * ( basicall, min(len(a),len(b)) ), and dl, which is the delta between the
  754. * two lengths, calculated as len(a)-len(b). All lengths are the number of
  755. * BN_ULONGs...
  756. */
  757. int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl)
  758. {
  759. int n, i;
  760. n = cl - 1;
  761. if (dl < 0) {
  762. for (i = dl; i < 0; i++) {
  763. if (b[n - i] != 0)
  764. return -1; /* a < b */
  765. }
  766. }
  767. if (dl > 0) {
  768. for (i = dl; i > 0; i--) {
  769. if (a[n + i] != 0)
  770. return 1; /* a > b */
  771. }
  772. }
  773. return bn_cmp_words(a, b, cl);
  774. }
  775. /*
  776. * Constant-time conditional swap of a and b.
  777. * a and b are swapped if condition is not 0. The code assumes that at most one bit of condition is set.
  778. * nwords is the number of words to swap. The code assumes that at least nwords are allocated in both a and b,
  779. * and that no more than nwords are used by either a or b.
  780. * a and b cannot be the same number
  781. */
  782. void BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords)
  783. {
  784. BN_ULONG t;
  785. int i;
  786. bn_wcheck_size(a, nwords);
  787. bn_wcheck_size(b, nwords);
  788. assert(a != b);
  789. assert((condition & (condition - 1)) == 0);
  790. assert(sizeof(BN_ULONG) >= sizeof(int));
  791. condition = ((condition - 1) >> (BN_BITS2 - 1)) - 1;
  792. t = (a->top ^ b->top) & condition;
  793. a->top ^= t;
  794. b->top ^= t;
  795. #define BN_CONSTTIME_SWAP(ind) \
  796. do { \
  797. t = (a->d[ind] ^ b->d[ind]) & condition; \
  798. a->d[ind] ^= t; \
  799. b->d[ind] ^= t; \
  800. } while (0)
  801. switch (nwords) {
  802. default:
  803. for (i = 10; i < nwords; i++)
  804. BN_CONSTTIME_SWAP(i);
  805. /* Fallthrough */
  806. case 10:
  807. BN_CONSTTIME_SWAP(9); /* Fallthrough */
  808. case 9:
  809. BN_CONSTTIME_SWAP(8); /* Fallthrough */
  810. case 8:
  811. BN_CONSTTIME_SWAP(7); /* Fallthrough */
  812. case 7:
  813. BN_CONSTTIME_SWAP(6); /* Fallthrough */
  814. case 6:
  815. BN_CONSTTIME_SWAP(5); /* Fallthrough */
  816. case 5:
  817. BN_CONSTTIME_SWAP(4); /* Fallthrough */
  818. case 4:
  819. BN_CONSTTIME_SWAP(3); /* Fallthrough */
  820. case 3:
  821. BN_CONSTTIME_SWAP(2); /* Fallthrough */
  822. case 2:
  823. BN_CONSTTIME_SWAP(1); /* Fallthrough */
  824. case 1:
  825. BN_CONSTTIME_SWAP(0);
  826. }
  827. #undef BN_CONSTTIME_SWAP
  828. }