bn_lib.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  1. /*
  2. * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <assert.h>
  10. #include <limits.h>
  11. #include "internal/cryptlib.h"
  12. #include "bn_lcl.h"
  13. #include <openssl/opensslconf.h>
  14. #include "internal/constant_time_locl.h"
  15. /* This stuff appears to be completely unused, so is deprecated */
  16. #if OPENSSL_API_COMPAT < 0x00908000L
  17. /*-
  18. * For a 32 bit machine
  19. * 2 - 4 == 128
  20. * 3 - 8 == 256
  21. * 4 - 16 == 512
  22. * 5 - 32 == 1024
  23. * 6 - 64 == 2048
  24. * 7 - 128 == 4096
  25. * 8 - 256 == 8192
  26. */
  27. static int bn_limit_bits = 0;
  28. static int bn_limit_num = 8; /* (1<<bn_limit_bits) */
  29. static int bn_limit_bits_low = 0;
  30. static int bn_limit_num_low = 8; /* (1<<bn_limit_bits_low) */
  31. static int bn_limit_bits_high = 0;
  32. static int bn_limit_num_high = 8; /* (1<<bn_limit_bits_high) */
  33. static int bn_limit_bits_mont = 0;
  34. static int bn_limit_num_mont = 8; /* (1<<bn_limit_bits_mont) */
  35. void BN_set_params(int mult, int high, int low, int mont)
  36. {
  37. if (mult >= 0) {
  38. if (mult > (int)(sizeof(int) * 8) - 1)
  39. mult = sizeof(int) * 8 - 1;
  40. bn_limit_bits = mult;
  41. bn_limit_num = 1 << mult;
  42. }
  43. if (high >= 0) {
  44. if (high > (int)(sizeof(int) * 8) - 1)
  45. high = sizeof(int) * 8 - 1;
  46. bn_limit_bits_high = high;
  47. bn_limit_num_high = 1 << high;
  48. }
  49. if (low >= 0) {
  50. if (low > (int)(sizeof(int) * 8) - 1)
  51. low = sizeof(int) * 8 - 1;
  52. bn_limit_bits_low = low;
  53. bn_limit_num_low = 1 << low;
  54. }
  55. if (mont >= 0) {
  56. if (mont > (int)(sizeof(int) * 8) - 1)
  57. mont = sizeof(int) * 8 - 1;
  58. bn_limit_bits_mont = mont;
  59. bn_limit_num_mont = 1 << mont;
  60. }
  61. }
  62. int BN_get_params(int which)
  63. {
  64. if (which == 0)
  65. return bn_limit_bits;
  66. else if (which == 1)
  67. return bn_limit_bits_high;
  68. else if (which == 2)
  69. return bn_limit_bits_low;
  70. else if (which == 3)
  71. return bn_limit_bits_mont;
  72. else
  73. return 0;
  74. }
  75. #endif
  76. const BIGNUM *BN_value_one(void)
  77. {
  78. static const BN_ULONG data_one = 1L;
  79. static const BIGNUM const_one =
  80. { (BN_ULONG *)&data_one, 1, 1, 0, BN_FLG_STATIC_DATA };
  81. return &const_one;
  82. }
  83. int BN_num_bits_word(BN_ULONG l)
  84. {
  85. BN_ULONG x, mask;
  86. int bits = (l != 0);
  87. #if BN_BITS2 > 32
  88. x = l >> 32;
  89. mask = (0 - x) & BN_MASK2;
  90. mask = (0 - (mask >> (BN_BITS2 - 1)));
  91. bits += 32 & mask;
  92. l ^= (x ^ l) & mask;
  93. #endif
  94. x = l >> 16;
  95. mask = (0 - x) & BN_MASK2;
  96. mask = (0 - (mask >> (BN_BITS2 - 1)));
  97. bits += 16 & mask;
  98. l ^= (x ^ l) & mask;
  99. x = l >> 8;
  100. mask = (0 - x) & BN_MASK2;
  101. mask = (0 - (mask >> (BN_BITS2 - 1)));
  102. bits += 8 & mask;
  103. l ^= (x ^ l) & mask;
  104. x = l >> 4;
  105. mask = (0 - x) & BN_MASK2;
  106. mask = (0 - (mask >> (BN_BITS2 - 1)));
  107. bits += 4 & mask;
  108. l ^= (x ^ l) & mask;
  109. x = l >> 2;
  110. mask = (0 - x) & BN_MASK2;
  111. mask = (0 - (mask >> (BN_BITS2 - 1)));
  112. bits += 2 & mask;
  113. l ^= (x ^ l) & mask;
  114. x = l >> 1;
  115. mask = (0 - x) & BN_MASK2;
  116. mask = (0 - (mask >> (BN_BITS2 - 1)));
  117. bits += 1 & mask;
  118. return bits;
  119. }
  120. /*
  121. * This function still leaks `a->dmax`: it's caller's responsibility to
  122. * expand the input `a` in advance to a public length.
  123. */
  124. static ossl_inline
  125. int bn_num_bits_consttime(const BIGNUM *a)
  126. {
  127. int j, ret;
  128. unsigned int mask, past_i;
  129. int i = a->top - 1;
  130. bn_check_top(a);
  131. for (j = 0, past_i = 0, ret = 0; j < a->dmax; j++) {
  132. mask = constant_time_eq_int(i, j); /* 0xff..ff if i==j, 0x0 otherwise */
  133. ret += BN_BITS2 & (~mask & ~past_i);
  134. ret += BN_num_bits_word(a->d[j]) & mask;
  135. past_i |= mask; /* past_i will become 0xff..ff after i==j */
  136. }
  137. /*
  138. * if BN_is_zero(a) => i is -1 and ret contains garbage, so we mask the
  139. * final result.
  140. */
  141. mask = ~(constant_time_eq_int(i, ((int)-1)));
  142. return ret & mask;
  143. }
  144. int BN_num_bits(const BIGNUM *a)
  145. {
  146. int i = a->top - 1;
  147. bn_check_top(a);
  148. if (a->flags & BN_FLG_CONSTTIME) {
  149. /*
  150. * We assume that BIGNUMs flagged as CONSTTIME have also been expanded
  151. * so that a->dmax is not leaking secret information.
  152. *
  153. * In other words, it's the caller's responsibility to ensure `a` has
  154. * been preallocated in advance to a public length if we hit this
  155. * branch.
  156. *
  157. */
  158. return bn_num_bits_consttime(a);
  159. }
  160. if (BN_is_zero(a))
  161. return 0;
  162. return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));
  163. }
  164. static void bn_free_d(BIGNUM *a, int clear)
  165. {
  166. if (BN_get_flags(a, BN_FLG_SECURE))
  167. OPENSSL_secure_clear_free(a->d, a->dmax * sizeof(a->d[0]));
  168. else if (clear != 0)
  169. OPENSSL_clear_free(a->d, a->dmax * sizeof(a->d[0]));
  170. else
  171. OPENSSL_free(a->d);
  172. }
  173. void BN_clear_free(BIGNUM *a)
  174. {
  175. if (a == NULL)
  176. return;
  177. if (a->d != NULL && !BN_get_flags(a, BN_FLG_STATIC_DATA))
  178. bn_free_d(a, 1);
  179. if (BN_get_flags(a, BN_FLG_MALLOCED)) {
  180. OPENSSL_cleanse(a, sizeof(*a));
  181. OPENSSL_free(a);
  182. }
  183. }
  184. void BN_free(BIGNUM *a)
  185. {
  186. if (a == NULL)
  187. return;
  188. if (!BN_get_flags(a, BN_FLG_STATIC_DATA))
  189. bn_free_d(a, 0);
  190. if (a->flags & BN_FLG_MALLOCED)
  191. OPENSSL_free(a);
  192. }
  193. void bn_init(BIGNUM *a)
  194. {
  195. static BIGNUM nilbn;
  196. *a = nilbn;
  197. bn_check_top(a);
  198. }
  199. BIGNUM *BN_new(void)
  200. {
  201. BIGNUM *ret;
  202. if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
  203. BNerr(BN_F_BN_NEW, ERR_R_MALLOC_FAILURE);
  204. return NULL;
  205. }
  206. ret->flags = BN_FLG_MALLOCED;
  207. bn_check_top(ret);
  208. return ret;
  209. }
  210. BIGNUM *BN_secure_new(void)
  211. {
  212. BIGNUM *ret = BN_new();
  213. if (ret != NULL)
  214. ret->flags |= BN_FLG_SECURE;
  215. return ret;
  216. }
  217. /* This is used by bn_expand2() */
  218. /* The caller MUST check that words > b->dmax before calling this */
  219. static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
  220. {
  221. BN_ULONG *a = NULL;
  222. if (words > (INT_MAX / (4 * BN_BITS2))) {
  223. BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
  224. return NULL;
  225. }
  226. if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
  227. BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
  228. return NULL;
  229. }
  230. if (BN_get_flags(b, BN_FLG_SECURE))
  231. a = OPENSSL_secure_zalloc(words * sizeof(*a));
  232. else
  233. a = OPENSSL_zalloc(words * sizeof(*a));
  234. if (a == NULL) {
  235. BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
  236. return NULL;
  237. }
  238. assert(b->top <= words);
  239. if (b->top > 0)
  240. memcpy(a, b->d, sizeof(*a) * b->top);
  241. return a;
  242. }
  243. /*
  244. * This is an internal function that should not be used in applications. It
  245. * ensures that 'b' has enough room for a 'words' word number and initialises
  246. * any unused part of b->d with leading zeros. It is mostly used by the
  247. * various BIGNUM routines. If there is an error, NULL is returned. If not,
  248. * 'b' is returned.
  249. */
  250. BIGNUM *bn_expand2(BIGNUM *b, int words)
  251. {
  252. if (words > b->dmax) {
  253. BN_ULONG *a = bn_expand_internal(b, words);
  254. if (!a)
  255. return NULL;
  256. if (b->d != NULL)
  257. bn_free_d(b, 1);
  258. b->d = a;
  259. b->dmax = words;
  260. }
  261. return b;
  262. }
  263. BIGNUM *BN_dup(const BIGNUM *a)
  264. {
  265. BIGNUM *t;
  266. if (a == NULL)
  267. return NULL;
  268. bn_check_top(a);
  269. t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new();
  270. if (t == NULL)
  271. return NULL;
  272. if (!BN_copy(t, a)) {
  273. BN_free(t);
  274. return NULL;
  275. }
  276. bn_check_top(t);
  277. return t;
  278. }
  279. BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
  280. {
  281. bn_check_top(b);
  282. if (a == b)
  283. return a;
  284. if (bn_wexpand(a, b->top) == NULL)
  285. return NULL;
  286. if (b->top > 0)
  287. memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);
  288. a->neg = b->neg;
  289. a->top = b->top;
  290. a->flags |= b->flags & BN_FLG_FIXED_TOP;
  291. bn_check_top(a);
  292. return a;
  293. }
  294. #define FLAGS_DATA(flags) ((flags) & (BN_FLG_STATIC_DATA \
  295. | BN_FLG_CONSTTIME \
  296. | BN_FLG_SECURE \
  297. | BN_FLG_FIXED_TOP))
  298. #define FLAGS_STRUCT(flags) ((flags) & (BN_FLG_MALLOCED))
  299. void BN_swap(BIGNUM *a, BIGNUM *b)
  300. {
  301. int flags_old_a, flags_old_b;
  302. BN_ULONG *tmp_d;
  303. int tmp_top, tmp_dmax, tmp_neg;
  304. bn_check_top(a);
  305. bn_check_top(b);
  306. flags_old_a = a->flags;
  307. flags_old_b = b->flags;
  308. tmp_d = a->d;
  309. tmp_top = a->top;
  310. tmp_dmax = a->dmax;
  311. tmp_neg = a->neg;
  312. a->d = b->d;
  313. a->top = b->top;
  314. a->dmax = b->dmax;
  315. a->neg = b->neg;
  316. b->d = tmp_d;
  317. b->top = tmp_top;
  318. b->dmax = tmp_dmax;
  319. b->neg = tmp_neg;
  320. a->flags = FLAGS_STRUCT(flags_old_a) | FLAGS_DATA(flags_old_b);
  321. b->flags = FLAGS_STRUCT(flags_old_b) | FLAGS_DATA(flags_old_a);
  322. bn_check_top(a);
  323. bn_check_top(b);
  324. }
  325. void BN_clear(BIGNUM *a)
  326. {
  327. if (a == NULL)
  328. return;
  329. bn_check_top(a);
  330. if (a->d != NULL)
  331. OPENSSL_cleanse(a->d, sizeof(*a->d) * a->dmax);
  332. a->neg = 0;
  333. a->top = 0;
  334. a->flags &= ~BN_FLG_FIXED_TOP;
  335. }
  336. BN_ULONG BN_get_word(const BIGNUM *a)
  337. {
  338. if (a->top > 1)
  339. return BN_MASK2;
  340. else if (a->top == 1)
  341. return a->d[0];
  342. /* a->top == 0 */
  343. return 0;
  344. }
  345. int BN_set_word(BIGNUM *a, BN_ULONG w)
  346. {
  347. bn_check_top(a);
  348. if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
  349. return 0;
  350. a->neg = 0;
  351. a->d[0] = w;
  352. a->top = (w ? 1 : 0);
  353. a->flags &= ~BN_FLG_FIXED_TOP;
  354. bn_check_top(a);
  355. return 1;
  356. }
  357. BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
  358. {
  359. unsigned int i, m;
  360. unsigned int n;
  361. BN_ULONG l;
  362. BIGNUM *bn = NULL;
  363. if (ret == NULL)
  364. ret = bn = BN_new();
  365. if (ret == NULL)
  366. return NULL;
  367. bn_check_top(ret);
  368. /* Skip leading zero's. */
  369. for ( ; len > 0 && *s == 0; s++, len--)
  370. continue;
  371. n = len;
  372. if (n == 0) {
  373. ret->top = 0;
  374. return ret;
  375. }
  376. i = ((n - 1) / BN_BYTES) + 1;
  377. m = ((n - 1) % (BN_BYTES));
  378. if (bn_wexpand(ret, (int)i) == NULL) {
  379. BN_free(bn);
  380. return NULL;
  381. }
  382. ret->top = i;
  383. ret->neg = 0;
  384. l = 0;
  385. while (n--) {
  386. l = (l << 8L) | *(s++);
  387. if (m-- == 0) {
  388. ret->d[--i] = l;
  389. l = 0;
  390. m = BN_BYTES - 1;
  391. }
  392. }
  393. /*
  394. * need to call this due to clear byte at top if avoiding having the top
  395. * bit set (-ve number)
  396. */
  397. bn_correct_top(ret);
  398. return ret;
  399. }
  400. typedef enum {big, little} endianess_t;
  401. /* ignore negative */
  402. static
  403. int bn2binpad(const BIGNUM *a, unsigned char *to, int tolen, endianess_t endianess)
  404. {
  405. int n;
  406. size_t i, lasti, j, atop, mask;
  407. BN_ULONG l;
  408. /*
  409. * In case |a| is fixed-top, BN_num_bytes can return bogus length,
  410. * but it's assumed that fixed-top inputs ought to be "nominated"
  411. * even for padded output, so it works out...
  412. */
  413. n = BN_num_bytes(a);
  414. if (tolen == -1) {
  415. tolen = n;
  416. } else if (tolen < n) { /* uncommon/unlike case */
  417. BIGNUM temp = *a;
  418. bn_correct_top(&temp);
  419. n = BN_num_bytes(&temp);
  420. if (tolen < n)
  421. return -1;
  422. }
  423. /* Swipe through whole available data and don't give away padded zero. */
  424. atop = a->dmax * BN_BYTES;
  425. if (atop == 0) {
  426. OPENSSL_cleanse(to, tolen);
  427. return tolen;
  428. }
  429. lasti = atop - 1;
  430. atop = a->top * BN_BYTES;
  431. if (endianess == big)
  432. to += tolen; /* start from the end of the buffer */
  433. for (i = 0, j = 0; j < (size_t)tolen; j++) {
  434. unsigned char val;
  435. l = a->d[i / BN_BYTES];
  436. mask = 0 - ((j - atop) >> (8 * sizeof(i) - 1));
  437. val = (unsigned char)(l >> (8 * (i % BN_BYTES)) & mask);
  438. if (endianess == big)
  439. *--to = val;
  440. else
  441. *to++ = val;
  442. i += (i - lasti) >> (8 * sizeof(i) - 1); /* stay on last limb */
  443. }
  444. return tolen;
  445. }
  446. int BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen)
  447. {
  448. if (tolen < 0)
  449. return -1;
  450. return bn2binpad(a, to, tolen, big);
  451. }
  452. int BN_bn2bin(const BIGNUM *a, unsigned char *to)
  453. {
  454. return bn2binpad(a, to, -1, big);
  455. }
  456. BIGNUM *BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret)
  457. {
  458. unsigned int i, m;
  459. unsigned int n;
  460. BN_ULONG l;
  461. BIGNUM *bn = NULL;
  462. if (ret == NULL)
  463. ret = bn = BN_new();
  464. if (ret == NULL)
  465. return NULL;
  466. bn_check_top(ret);
  467. s += len;
  468. /* Skip trailing zeroes. */
  469. for ( ; len > 0 && s[-1] == 0; s--, len--)
  470. continue;
  471. n = len;
  472. if (n == 0) {
  473. ret->top = 0;
  474. return ret;
  475. }
  476. i = ((n - 1) / BN_BYTES) + 1;
  477. m = ((n - 1) % (BN_BYTES));
  478. if (bn_wexpand(ret, (int)i) == NULL) {
  479. BN_free(bn);
  480. return NULL;
  481. }
  482. ret->top = i;
  483. ret->neg = 0;
  484. l = 0;
  485. while (n--) {
  486. s--;
  487. l = (l << 8L) | *s;
  488. if (m-- == 0) {
  489. ret->d[--i] = l;
  490. l = 0;
  491. m = BN_BYTES - 1;
  492. }
  493. }
  494. /*
  495. * need to call this due to clear byte at top if avoiding having the top
  496. * bit set (-ve number)
  497. */
  498. bn_correct_top(ret);
  499. return ret;
  500. }
  501. int BN_bn2lebinpad(const BIGNUM *a, unsigned char *to, int tolen)
  502. {
  503. if (tolen < 0)
  504. return -1;
  505. return bn2binpad(a, to, tolen, little);
  506. }
  507. int BN_ucmp(const BIGNUM *a, const BIGNUM *b)
  508. {
  509. int i;
  510. BN_ULONG t1, t2, *ap, *bp;
  511. bn_check_top(a);
  512. bn_check_top(b);
  513. i = a->top - b->top;
  514. if (i != 0)
  515. return i;
  516. ap = a->d;
  517. bp = b->d;
  518. for (i = a->top - 1; i >= 0; i--) {
  519. t1 = ap[i];
  520. t2 = bp[i];
  521. if (t1 != t2)
  522. return ((t1 > t2) ? 1 : -1);
  523. }
  524. return 0;
  525. }
  526. int BN_cmp(const BIGNUM *a, const BIGNUM *b)
  527. {
  528. int i;
  529. int gt, lt;
  530. BN_ULONG t1, t2;
  531. if ((a == NULL) || (b == NULL)) {
  532. if (a != NULL)
  533. return -1;
  534. else if (b != NULL)
  535. return 1;
  536. else
  537. return 0;
  538. }
  539. bn_check_top(a);
  540. bn_check_top(b);
  541. if (a->neg != b->neg) {
  542. if (a->neg)
  543. return -1;
  544. else
  545. return 1;
  546. }
  547. if (a->neg == 0) {
  548. gt = 1;
  549. lt = -1;
  550. } else {
  551. gt = -1;
  552. lt = 1;
  553. }
  554. if (a->top > b->top)
  555. return gt;
  556. if (a->top < b->top)
  557. return lt;
  558. for (i = a->top - 1; i >= 0; i--) {
  559. t1 = a->d[i];
  560. t2 = b->d[i];
  561. if (t1 > t2)
  562. return gt;
  563. if (t1 < t2)
  564. return lt;
  565. }
  566. return 0;
  567. }
  568. int BN_set_bit(BIGNUM *a, int n)
  569. {
  570. int i, j, k;
  571. if (n < 0)
  572. return 0;
  573. i = n / BN_BITS2;
  574. j = n % BN_BITS2;
  575. if (a->top <= i) {
  576. if (bn_wexpand(a, i + 1) == NULL)
  577. return 0;
  578. for (k = a->top; k < i + 1; k++)
  579. a->d[k] = 0;
  580. a->top = i + 1;
  581. a->flags &= ~BN_FLG_FIXED_TOP;
  582. }
  583. a->d[i] |= (((BN_ULONG)1) << j);
  584. bn_check_top(a);
  585. return 1;
  586. }
  587. int BN_clear_bit(BIGNUM *a, int n)
  588. {
  589. int i, j;
  590. bn_check_top(a);
  591. if (n < 0)
  592. return 0;
  593. i = n / BN_BITS2;
  594. j = n % BN_BITS2;
  595. if (a->top <= i)
  596. return 0;
  597. a->d[i] &= (~(((BN_ULONG)1) << j));
  598. bn_correct_top(a);
  599. return 1;
  600. }
  601. int BN_is_bit_set(const BIGNUM *a, int n)
  602. {
  603. int i, j;
  604. bn_check_top(a);
  605. if (n < 0)
  606. return 0;
  607. i = n / BN_BITS2;
  608. j = n % BN_BITS2;
  609. if (a->top <= i)
  610. return 0;
  611. return (int)(((a->d[i]) >> j) & ((BN_ULONG)1));
  612. }
  613. int BN_mask_bits(BIGNUM *a, int n)
  614. {
  615. int b, w;
  616. bn_check_top(a);
  617. if (n < 0)
  618. return 0;
  619. w = n / BN_BITS2;
  620. b = n % BN_BITS2;
  621. if (w >= a->top)
  622. return 0;
  623. if (b == 0)
  624. a->top = w;
  625. else {
  626. a->top = w + 1;
  627. a->d[w] &= ~(BN_MASK2 << b);
  628. }
  629. bn_correct_top(a);
  630. return 1;
  631. }
  632. void BN_set_negative(BIGNUM *a, int b)
  633. {
  634. if (b && !BN_is_zero(a))
  635. a->neg = 1;
  636. else
  637. a->neg = 0;
  638. }
  639. int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
  640. {
  641. int i;
  642. BN_ULONG aa, bb;
  643. if (n == 0)
  644. return 0;
  645. aa = a[n - 1];
  646. bb = b[n - 1];
  647. if (aa != bb)
  648. return ((aa > bb) ? 1 : -1);
  649. for (i = n - 2; i >= 0; i--) {
  650. aa = a[i];
  651. bb = b[i];
  652. if (aa != bb)
  653. return ((aa > bb) ? 1 : -1);
  654. }
  655. return 0;
  656. }
  657. /*
  658. * Here follows a specialised variants of bn_cmp_words(). It has the
  659. * capability of performing the operation on arrays of different sizes. The
  660. * sizes of those arrays is expressed through cl, which is the common length
  661. * ( basically, min(len(a),len(b)) ), and dl, which is the delta between the
  662. * two lengths, calculated as len(a)-len(b). All lengths are the number of
  663. * BN_ULONGs...
  664. */
  665. int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl)
  666. {
  667. int n, i;
  668. n = cl - 1;
  669. if (dl < 0) {
  670. for (i = dl; i < 0; i++) {
  671. if (b[n - i] != 0)
  672. return -1; /* a < b */
  673. }
  674. }
  675. if (dl > 0) {
  676. for (i = dl; i > 0; i--) {
  677. if (a[n + i] != 0)
  678. return 1; /* a > b */
  679. }
  680. }
  681. return bn_cmp_words(a, b, cl);
  682. }
  683. /*-
  684. * Constant-time conditional swap of a and b.
  685. * a and b are swapped if condition is not 0.
  686. * nwords is the number of words to swap.
  687. * Assumes that at least nwords are allocated in both a and b.
  688. * Assumes that no more than nwords are used by either a or b.
  689. */
  690. void BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords)
  691. {
  692. BN_ULONG t;
  693. int i;
  694. if (a == b)
  695. return;
  696. bn_wcheck_size(a, nwords);
  697. bn_wcheck_size(b, nwords);
  698. condition = ((~condition & ((condition - 1))) >> (BN_BITS2 - 1)) - 1;
  699. t = (a->top ^ b->top) & condition;
  700. a->top ^= t;
  701. b->top ^= t;
  702. t = (a->neg ^ b->neg) & condition;
  703. a->neg ^= t;
  704. b->neg ^= t;
  705. /*-
  706. * BN_FLG_STATIC_DATA: indicates that data may not be written to. Intention
  707. * is actually to treat it as it's read-only data, and some (if not most)
  708. * of it does reside in read-only segment. In other words observation of
  709. * BN_FLG_STATIC_DATA in BN_consttime_swap should be treated as fatal
  710. * condition. It would either cause SEGV or effectively cause data
  711. * corruption.
  712. *
  713. * BN_FLG_MALLOCED: refers to BN structure itself, and hence must be
  714. * preserved.
  715. *
  716. * BN_FLG_SECURE: must be preserved, because it determines how x->d was
  717. * allocated and hence how to free it.
  718. *
  719. * BN_FLG_CONSTTIME: sufficient to mask and swap
  720. *
  721. * BN_FLG_FIXED_TOP: indicates that we haven't called bn_correct_top() on
  722. * the data, so the d array may be padded with additional 0 values (i.e.
  723. * top could be greater than the minimal value that it could be). We should
  724. * be swapping it
  725. */
  726. #define BN_CONSTTIME_SWAP_FLAGS (BN_FLG_CONSTTIME | BN_FLG_FIXED_TOP)
  727. t = ((a->flags ^ b->flags) & BN_CONSTTIME_SWAP_FLAGS) & condition;
  728. a->flags ^= t;
  729. b->flags ^= t;
  730. /* conditionally swap the data */
  731. for (i = 0; i < nwords; i++) {
  732. t = (a->d[i] ^ b->d[i]) & condition;
  733. a->d[i] ^= t;
  734. b->d[i] ^= t;
  735. }
  736. }
  737. #undef BN_CONSTTIME_SWAP_FLAGS
  738. /* Bits of security, see SP800-57 */
  739. int BN_security_bits(int L, int N)
  740. {
  741. int secbits, bits;
  742. if (L >= 15360)
  743. secbits = 256;
  744. else if (L >= 7680)
  745. secbits = 192;
  746. else if (L >= 3072)
  747. secbits = 128;
  748. else if (L >= 2048)
  749. secbits = 112;
  750. else if (L >= 1024)
  751. secbits = 80;
  752. else
  753. return 0;
  754. if (N == -1)
  755. return secbits;
  756. bits = N / 2;
  757. if (bits < 80)
  758. return 0;
  759. return bits >= secbits ? secbits : bits;
  760. }
  761. void BN_zero_ex(BIGNUM *a)
  762. {
  763. a->neg = 0;
  764. a->top = 0;
  765. a->flags &= ~BN_FLG_FIXED_TOP;
  766. }
  767. int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w)
  768. {
  769. return ((a->top == 1) && (a->d[0] == w)) || ((w == 0) && (a->top == 0));
  770. }
  771. int BN_is_zero(const BIGNUM *a)
  772. {
  773. return a->top == 0;
  774. }
  775. int BN_is_one(const BIGNUM *a)
  776. {
  777. return BN_abs_is_word(a, 1) && !a->neg;
  778. }
  779. int BN_is_word(const BIGNUM *a, const BN_ULONG w)
  780. {
  781. return BN_abs_is_word(a, w) && (!w || !a->neg);
  782. }
  783. int BN_is_odd(const BIGNUM *a)
  784. {
  785. return (a->top > 0) && (a->d[0] & 1);
  786. }
  787. int BN_is_negative(const BIGNUM *a)
  788. {
  789. return (a->neg != 0);
  790. }
  791. int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,
  792. BN_CTX *ctx)
  793. {
  794. return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);
  795. }
  796. void BN_with_flags(BIGNUM *dest, const BIGNUM *b, int flags)
  797. {
  798. dest->d = b->d;
  799. dest->top = b->top;
  800. dest->dmax = b->dmax;
  801. dest->neg = b->neg;
  802. dest->flags = ((dest->flags & BN_FLG_MALLOCED)
  803. | (b->flags & ~BN_FLG_MALLOCED)
  804. | BN_FLG_STATIC_DATA | flags);
  805. }
  806. BN_GENCB *BN_GENCB_new(void)
  807. {
  808. BN_GENCB *ret;
  809. if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL) {
  810. BNerr(BN_F_BN_GENCB_NEW, ERR_R_MALLOC_FAILURE);
  811. return NULL;
  812. }
  813. return ret;
  814. }
  815. void BN_GENCB_free(BN_GENCB *cb)
  816. {
  817. if (cb == NULL)
  818. return;
  819. OPENSSL_free(cb);
  820. }
  821. void BN_set_flags(BIGNUM *b, int n)
  822. {
  823. b->flags |= n;
  824. }
  825. int BN_get_flags(const BIGNUM *b, int n)
  826. {
  827. return b->flags & n;
  828. }
  829. /* Populate a BN_GENCB structure with an "old"-style callback */
  830. void BN_GENCB_set_old(BN_GENCB *gencb, void (*callback) (int, int, void *),
  831. void *cb_arg)
  832. {
  833. BN_GENCB *tmp_gencb = gencb;
  834. tmp_gencb->ver = 1;
  835. tmp_gencb->arg = cb_arg;
  836. tmp_gencb->cb.cb_1 = callback;
  837. }
  838. /* Populate a BN_GENCB structure with a "new"-style callback */
  839. void BN_GENCB_set(BN_GENCB *gencb, int (*callback) (int, int, BN_GENCB *),
  840. void *cb_arg)
  841. {
  842. BN_GENCB *tmp_gencb = gencb;
  843. tmp_gencb->ver = 2;
  844. tmp_gencb->arg = cb_arg;
  845. tmp_gencb->cb.cb_2 = callback;
  846. }
  847. void *BN_GENCB_get_arg(BN_GENCB *cb)
  848. {
  849. return cb->arg;
  850. }
  851. BIGNUM *bn_wexpand(BIGNUM *a, int words)
  852. {
  853. return (words <= a->dmax) ? a : bn_expand2(a, words);
  854. }
  855. void bn_correct_top(BIGNUM *a)
  856. {
  857. BN_ULONG *ftl;
  858. int tmp_top = a->top;
  859. if (tmp_top > 0) {
  860. for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) {
  861. ftl--;
  862. if (*ftl != 0)
  863. break;
  864. }
  865. a->top = tmp_top;
  866. }
  867. if (a->top == 0)
  868. a->neg = 0;
  869. a->flags &= ~BN_FLG_FIXED_TOP;
  870. bn_pollute(a);
  871. }