bn_lib.c 21 KB

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