1
0

bn_lib.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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. 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. if (a == NULL)
  294. return;
  295. bn_check_top(a);
  296. if (a->d != NULL)
  297. OPENSSL_cleanse(a->d, sizeof(*a->d) * a->dmax);
  298. a->neg = 0;
  299. a->top = 0;
  300. a->flags &= ~BN_FLG_FIXED_TOP;
  301. }
  302. BN_ULONG BN_get_word(const BIGNUM *a)
  303. {
  304. if (a->top > 1)
  305. return BN_MASK2;
  306. else if (a->top == 1)
  307. return a->d[0];
  308. /* a->top == 0 */
  309. return 0;
  310. }
  311. int BN_set_word(BIGNUM *a, BN_ULONG w)
  312. {
  313. bn_check_top(a);
  314. if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
  315. return 0;
  316. a->neg = 0;
  317. a->d[0] = w;
  318. a->top = (w ? 1 : 0);
  319. a->flags &= ~BN_FLG_FIXED_TOP;
  320. bn_check_top(a);
  321. return 1;
  322. }
  323. BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
  324. {
  325. unsigned int i, m;
  326. unsigned int n;
  327. BN_ULONG l;
  328. BIGNUM *bn = NULL;
  329. if (ret == NULL)
  330. ret = bn = BN_new();
  331. if (ret == NULL)
  332. return NULL;
  333. bn_check_top(ret);
  334. /* Skip leading zero's. */
  335. for ( ; len > 0 && *s == 0; s++, len--)
  336. continue;
  337. n = len;
  338. if (n == 0) {
  339. ret->top = 0;
  340. return ret;
  341. }
  342. i = ((n - 1) / BN_BYTES) + 1;
  343. m = ((n - 1) % (BN_BYTES));
  344. if (bn_wexpand(ret, (int)i) == NULL) {
  345. BN_free(bn);
  346. return NULL;
  347. }
  348. ret->top = i;
  349. ret->neg = 0;
  350. l = 0;
  351. while (n--) {
  352. l = (l << 8L) | *(s++);
  353. if (m-- == 0) {
  354. ret->d[--i] = l;
  355. l = 0;
  356. m = BN_BYTES - 1;
  357. }
  358. }
  359. /*
  360. * need to call this due to clear byte at top if avoiding having the top
  361. * bit set (-ve number)
  362. */
  363. bn_correct_top(ret);
  364. return ret;
  365. }
  366. /* ignore negative */
  367. static int bn2binpad(const BIGNUM *a, unsigned char *to, int tolen)
  368. {
  369. int n;
  370. size_t i, lasti, j, atop, mask;
  371. BN_ULONG l;
  372. /*
  373. * In case |a| is fixed-top, BN_num_bytes can return bogus length,
  374. * but it's assumed that fixed-top inputs ought to be "nominated"
  375. * even for padded output, so it works out...
  376. */
  377. n = BN_num_bytes(a);
  378. if (tolen == -1) {
  379. tolen = n;
  380. } else if (tolen < n) { /* uncommon/unlike case */
  381. BIGNUM temp = *a;
  382. bn_correct_top(&temp);
  383. n = BN_num_bytes(&temp);
  384. if (tolen < n)
  385. return -1;
  386. }
  387. /* Swipe through whole available data and don't give away padded zero. */
  388. atop = a->dmax * BN_BYTES;
  389. if (atop == 0) {
  390. OPENSSL_cleanse(to, tolen);
  391. return tolen;
  392. }
  393. lasti = atop - 1;
  394. atop = a->top * BN_BYTES;
  395. for (i = 0, j = 0, to += tolen; j < (size_t)tolen; j++) {
  396. l = a->d[i / BN_BYTES];
  397. mask = 0 - ((j - atop) >> (8 * sizeof(i) - 1));
  398. *--to = (unsigned char)(l >> (8 * (i % BN_BYTES)) & mask);
  399. i += (i - lasti) >> (8 * sizeof(i) - 1); /* stay on last limb */
  400. }
  401. return tolen;
  402. }
  403. int BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen)
  404. {
  405. if (tolen < 0)
  406. return -1;
  407. return bn2binpad(a, to, tolen);
  408. }
  409. int BN_bn2bin(const BIGNUM *a, unsigned char *to)
  410. {
  411. return bn2binpad(a, to, -1);
  412. }
  413. BIGNUM *BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret)
  414. {
  415. unsigned int i, m;
  416. unsigned int n;
  417. BN_ULONG l;
  418. BIGNUM *bn = NULL;
  419. if (ret == NULL)
  420. ret = bn = BN_new();
  421. if (ret == NULL)
  422. return NULL;
  423. bn_check_top(ret);
  424. s += len;
  425. /* Skip trailing zeroes. */
  426. for ( ; len > 0 && s[-1] == 0; s--, len--)
  427. continue;
  428. n = len;
  429. if (n == 0) {
  430. ret->top = 0;
  431. return ret;
  432. }
  433. i = ((n - 1) / BN_BYTES) + 1;
  434. m = ((n - 1) % (BN_BYTES));
  435. if (bn_wexpand(ret, (int)i) == NULL) {
  436. BN_free(bn);
  437. return NULL;
  438. }
  439. ret->top = i;
  440. ret->neg = 0;
  441. l = 0;
  442. while (n--) {
  443. s--;
  444. l = (l << 8L) | *s;
  445. if (m-- == 0) {
  446. ret->d[--i] = l;
  447. l = 0;
  448. m = BN_BYTES - 1;
  449. }
  450. }
  451. /*
  452. * need to call this due to clear byte at top if avoiding having the top
  453. * bit set (-ve number)
  454. */
  455. bn_correct_top(ret);
  456. return ret;
  457. }
  458. int BN_bn2lebinpad(const BIGNUM *a, unsigned char *to, int tolen)
  459. {
  460. int i;
  461. BN_ULONG l;
  462. bn_check_top(a);
  463. i = BN_num_bytes(a);
  464. if (tolen < i)
  465. return -1;
  466. /* Add trailing zeroes if necessary */
  467. if (tolen > i)
  468. memset(to + i, 0, tolen - i);
  469. to += i;
  470. while (i--) {
  471. l = a->d[i / BN_BYTES];
  472. to--;
  473. *to = (unsigned char)(l >> (8 * (i % BN_BYTES))) & 0xff;
  474. }
  475. return tolen;
  476. }
  477. int BN_ucmp(const BIGNUM *a, const BIGNUM *b)
  478. {
  479. int i;
  480. BN_ULONG t1, t2, *ap, *bp;
  481. bn_check_top(a);
  482. bn_check_top(b);
  483. i = a->top - b->top;
  484. if (i != 0)
  485. return i;
  486. ap = a->d;
  487. bp = b->d;
  488. for (i = a->top - 1; i >= 0; i--) {
  489. t1 = ap[i];
  490. t2 = bp[i];
  491. if (t1 != t2)
  492. return ((t1 > t2) ? 1 : -1);
  493. }
  494. return 0;
  495. }
  496. int BN_cmp(const BIGNUM *a, const BIGNUM *b)
  497. {
  498. int i;
  499. int gt, lt;
  500. BN_ULONG t1, t2;
  501. if ((a == NULL) || (b == NULL)) {
  502. if (a != NULL)
  503. return -1;
  504. else if (b != NULL)
  505. return 1;
  506. else
  507. return 0;
  508. }
  509. bn_check_top(a);
  510. bn_check_top(b);
  511. if (a->neg != b->neg) {
  512. if (a->neg)
  513. return -1;
  514. else
  515. return 1;
  516. }
  517. if (a->neg == 0) {
  518. gt = 1;
  519. lt = -1;
  520. } else {
  521. gt = -1;
  522. lt = 1;
  523. }
  524. if (a->top > b->top)
  525. return gt;
  526. if (a->top < b->top)
  527. return lt;
  528. for (i = a->top - 1; i >= 0; i--) {
  529. t1 = a->d[i];
  530. t2 = b->d[i];
  531. if (t1 > t2)
  532. return gt;
  533. if (t1 < t2)
  534. return lt;
  535. }
  536. return 0;
  537. }
  538. int BN_set_bit(BIGNUM *a, int n)
  539. {
  540. int i, j, k;
  541. if (n < 0)
  542. return 0;
  543. i = n / BN_BITS2;
  544. j = n % BN_BITS2;
  545. if (a->top <= i) {
  546. if (bn_wexpand(a, i + 1) == NULL)
  547. return 0;
  548. for (k = a->top; k < i + 1; k++)
  549. a->d[k] = 0;
  550. a->top = i + 1;
  551. a->flags &= ~BN_FLG_FIXED_TOP;
  552. }
  553. a->d[i] |= (((BN_ULONG)1) << j);
  554. bn_check_top(a);
  555. return 1;
  556. }
  557. int BN_clear_bit(BIGNUM *a, int n)
  558. {
  559. int i, j;
  560. bn_check_top(a);
  561. if (n < 0)
  562. return 0;
  563. i = n / BN_BITS2;
  564. j = n % BN_BITS2;
  565. if (a->top <= i)
  566. return 0;
  567. a->d[i] &= (~(((BN_ULONG)1) << j));
  568. bn_correct_top(a);
  569. return 1;
  570. }
  571. int BN_is_bit_set(const BIGNUM *a, int n)
  572. {
  573. int i, j;
  574. bn_check_top(a);
  575. if (n < 0)
  576. return 0;
  577. i = n / BN_BITS2;
  578. j = n % BN_BITS2;
  579. if (a->top <= i)
  580. return 0;
  581. return (int)(((a->d[i]) >> j) & ((BN_ULONG)1));
  582. }
  583. int BN_mask_bits(BIGNUM *a, int n)
  584. {
  585. int b, w;
  586. bn_check_top(a);
  587. if (n < 0)
  588. return 0;
  589. w = n / BN_BITS2;
  590. b = n % BN_BITS2;
  591. if (w >= a->top)
  592. return 0;
  593. if (b == 0)
  594. a->top = w;
  595. else {
  596. a->top = w + 1;
  597. a->d[w] &= ~(BN_MASK2 << b);
  598. }
  599. bn_correct_top(a);
  600. return 1;
  601. }
  602. void BN_set_negative(BIGNUM *a, int b)
  603. {
  604. if (b && !BN_is_zero(a))
  605. a->neg = 1;
  606. else
  607. a->neg = 0;
  608. }
  609. int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
  610. {
  611. int i;
  612. BN_ULONG aa, bb;
  613. if (n == 0)
  614. return 0;
  615. aa = a[n - 1];
  616. bb = b[n - 1];
  617. if (aa != bb)
  618. return ((aa > bb) ? 1 : -1);
  619. for (i = n - 2; i >= 0; i--) {
  620. aa = a[i];
  621. bb = b[i];
  622. if (aa != bb)
  623. return ((aa > bb) ? 1 : -1);
  624. }
  625. return 0;
  626. }
  627. /*
  628. * Here follows a specialised variants of bn_cmp_words(). It has the
  629. * capability of performing the operation on arrays of different sizes. The
  630. * sizes of those arrays is expressed through cl, which is the common length
  631. * ( basically, min(len(a),len(b)) ), and dl, which is the delta between the
  632. * two lengths, calculated as len(a)-len(b). All lengths are the number of
  633. * BN_ULONGs...
  634. */
  635. int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl)
  636. {
  637. int n, i;
  638. n = cl - 1;
  639. if (dl < 0) {
  640. for (i = dl; i < 0; i++) {
  641. if (b[n - i] != 0)
  642. return -1; /* a < b */
  643. }
  644. }
  645. if (dl > 0) {
  646. for (i = dl; i > 0; i--) {
  647. if (a[n + i] != 0)
  648. return 1; /* a > b */
  649. }
  650. }
  651. return bn_cmp_words(a, b, cl);
  652. }
  653. /*-
  654. * Constant-time conditional swap of a and b.
  655. * a and b are swapped if condition is not 0.
  656. * nwords is the number of words to swap.
  657. * Assumes that at least nwords are allocated in both a and b.
  658. * Assumes that no more than nwords are used by either a or b.
  659. */
  660. void BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords)
  661. {
  662. BN_ULONG t;
  663. int i;
  664. if (a == b)
  665. return;
  666. bn_wcheck_size(a, nwords);
  667. bn_wcheck_size(b, nwords);
  668. condition = ((~condition & ((condition - 1))) >> (BN_BITS2 - 1)) - 1;
  669. t = (a->top ^ b->top) & condition;
  670. a->top ^= t;
  671. b->top ^= t;
  672. t = (a->neg ^ b->neg) & condition;
  673. a->neg ^= t;
  674. b->neg ^= t;
  675. /*-
  676. * BN_FLG_STATIC_DATA: indicates that data may not be written to. Intention
  677. * is actually to treat it as it's read-only data, and some (if not most)
  678. * of it does reside in read-only segment. In other words observation of
  679. * BN_FLG_STATIC_DATA in BN_consttime_swap should be treated as fatal
  680. * condition. It would either cause SEGV or effectively cause data
  681. * corruption.
  682. *
  683. * BN_FLG_MALLOCED: refers to BN structure itself, and hence must be
  684. * preserved.
  685. *
  686. * BN_FLG_SECURE: must be preserved, because it determines how x->d was
  687. * allocated and hence how to free it.
  688. *
  689. * BN_FLG_CONSTTIME: sufficient to mask and swap
  690. *
  691. * BN_FLG_FIXED_TOP: indicates that we haven't called bn_correct_top() on
  692. * the data, so the d array may be padded with additional 0 values (i.e.
  693. * top could be greater than the minimal value that it could be). We should
  694. * be swapping it
  695. */
  696. #define BN_CONSTTIME_SWAP_FLAGS (BN_FLG_CONSTTIME | BN_FLG_FIXED_TOP)
  697. t = ((a->flags ^ b->flags) & BN_CONSTTIME_SWAP_FLAGS) & condition;
  698. a->flags ^= t;
  699. b->flags ^= t;
  700. /* conditionally swap the data */
  701. for (i = 0; i < nwords; i++) {
  702. t = (a->d[i] ^ b->d[i]) & condition;
  703. a->d[i] ^= t;
  704. b->d[i] ^= t;
  705. }
  706. }
  707. #undef BN_CONSTTIME_SWAP_FLAGS
  708. /* Bits of security, see SP800-57 */
  709. int BN_security_bits(int L, int N)
  710. {
  711. int secbits, bits;
  712. if (L >= 15360)
  713. secbits = 256;
  714. else if (L >= 7680)
  715. secbits = 192;
  716. else if (L >= 3072)
  717. secbits = 128;
  718. else if (L >= 2048)
  719. secbits = 112;
  720. else if (L >= 1024)
  721. secbits = 80;
  722. else
  723. return 0;
  724. if (N == -1)
  725. return secbits;
  726. bits = N / 2;
  727. if (bits < 80)
  728. return 0;
  729. return bits >= secbits ? secbits : bits;
  730. }
  731. void BN_zero_ex(BIGNUM *a)
  732. {
  733. a->neg = 0;
  734. a->top = 0;
  735. a->flags &= ~BN_FLG_FIXED_TOP;
  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. a->flags &= ~BN_FLG_FIXED_TOP;
  840. bn_pollute(a);
  841. }