2
0

bn_lib.c 22 KB

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