bn_lib.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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_0_9_8
  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. BIGNUM *BN_native2bn(const unsigned char *s, int len, BIGNUM *ret)
  478. {
  479. #ifdef B_ENDIAN
  480. return BN_bin2bn(s, len, ret);
  481. #else
  482. return BN_lebin2bn(s, len, ret);
  483. #endif
  484. }
  485. int BN_bn2nativepad(const BIGNUM *a, unsigned char *to, int tolen)
  486. {
  487. #ifdef B_ENDIAN
  488. return BN_bn2binpad(a, to, tolen);
  489. #else
  490. return BN_bn2lebinpad(a, to, tolen);
  491. #endif
  492. }
  493. int BN_ucmp(const BIGNUM *a, const BIGNUM *b)
  494. {
  495. int i;
  496. BN_ULONG t1, t2, *ap, *bp;
  497. bn_check_top(a);
  498. bn_check_top(b);
  499. i = a->top - b->top;
  500. if (i != 0)
  501. return i;
  502. ap = a->d;
  503. bp = b->d;
  504. for (i = a->top - 1; i >= 0; i--) {
  505. t1 = ap[i];
  506. t2 = bp[i];
  507. if (t1 != t2)
  508. return ((t1 > t2) ? 1 : -1);
  509. }
  510. return 0;
  511. }
  512. int BN_cmp(const BIGNUM *a, const BIGNUM *b)
  513. {
  514. int i;
  515. int gt, lt;
  516. BN_ULONG t1, t2;
  517. if ((a == NULL) || (b == NULL)) {
  518. if (a != NULL)
  519. return -1;
  520. else if (b != NULL)
  521. return 1;
  522. else
  523. return 0;
  524. }
  525. bn_check_top(a);
  526. bn_check_top(b);
  527. if (a->neg != b->neg) {
  528. if (a->neg)
  529. return -1;
  530. else
  531. return 1;
  532. }
  533. if (a->neg == 0) {
  534. gt = 1;
  535. lt = -1;
  536. } else {
  537. gt = -1;
  538. lt = 1;
  539. }
  540. if (a->top > b->top)
  541. return gt;
  542. if (a->top < b->top)
  543. return lt;
  544. for (i = a->top - 1; i >= 0; i--) {
  545. t1 = a->d[i];
  546. t2 = b->d[i];
  547. if (t1 > t2)
  548. return gt;
  549. if (t1 < t2)
  550. return lt;
  551. }
  552. return 0;
  553. }
  554. int BN_set_bit(BIGNUM *a, int n)
  555. {
  556. int i, j, k;
  557. if (n < 0)
  558. return 0;
  559. i = n / BN_BITS2;
  560. j = n % BN_BITS2;
  561. if (a->top <= i) {
  562. if (bn_wexpand(a, i + 1) == NULL)
  563. return 0;
  564. for (k = a->top; k < i + 1; k++)
  565. a->d[k] = 0;
  566. a->top = i + 1;
  567. a->flags &= ~BN_FLG_FIXED_TOP;
  568. }
  569. a->d[i] |= (((BN_ULONG)1) << j);
  570. bn_check_top(a);
  571. return 1;
  572. }
  573. int BN_clear_bit(BIGNUM *a, int n)
  574. {
  575. int i, j;
  576. bn_check_top(a);
  577. if (n < 0)
  578. return 0;
  579. i = n / BN_BITS2;
  580. j = n % BN_BITS2;
  581. if (a->top <= i)
  582. return 0;
  583. a->d[i] &= (~(((BN_ULONG)1) << j));
  584. bn_correct_top(a);
  585. return 1;
  586. }
  587. int BN_is_bit_set(const 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. return (int)(((a->d[i]) >> j) & ((BN_ULONG)1));
  598. }
  599. int BN_mask_bits(BIGNUM *a, int n)
  600. {
  601. int b, w;
  602. bn_check_top(a);
  603. if (n < 0)
  604. return 0;
  605. w = n / BN_BITS2;
  606. b = n % BN_BITS2;
  607. if (w >= a->top)
  608. return 0;
  609. if (b == 0)
  610. a->top = w;
  611. else {
  612. a->top = w + 1;
  613. a->d[w] &= ~(BN_MASK2 << b);
  614. }
  615. bn_correct_top(a);
  616. return 1;
  617. }
  618. void BN_set_negative(BIGNUM *a, int b)
  619. {
  620. if (b && !BN_is_zero(a))
  621. a->neg = 1;
  622. else
  623. a->neg = 0;
  624. }
  625. int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
  626. {
  627. int i;
  628. BN_ULONG aa, bb;
  629. if (n == 0)
  630. return 0;
  631. aa = a[n - 1];
  632. bb = b[n - 1];
  633. if (aa != bb)
  634. return ((aa > bb) ? 1 : -1);
  635. for (i = n - 2; i >= 0; i--) {
  636. aa = a[i];
  637. bb = b[i];
  638. if (aa != bb)
  639. return ((aa > bb) ? 1 : -1);
  640. }
  641. return 0;
  642. }
  643. /*
  644. * Here follows a specialised variants of bn_cmp_words(). It has the
  645. * capability of performing the operation on arrays of different sizes. The
  646. * sizes of those arrays is expressed through cl, which is the common length
  647. * ( basically, min(len(a),len(b)) ), and dl, which is the delta between the
  648. * two lengths, calculated as len(a)-len(b). All lengths are the number of
  649. * BN_ULONGs...
  650. */
  651. int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl)
  652. {
  653. int n, i;
  654. n = cl - 1;
  655. if (dl < 0) {
  656. for (i = dl; i < 0; i++) {
  657. if (b[n - i] != 0)
  658. return -1; /* a < b */
  659. }
  660. }
  661. if (dl > 0) {
  662. for (i = dl; i > 0; i--) {
  663. if (a[n + i] != 0)
  664. return 1; /* a > b */
  665. }
  666. }
  667. return bn_cmp_words(a, b, cl);
  668. }
  669. /*-
  670. * Constant-time conditional swap of a and b.
  671. * a and b are swapped if condition is not 0.
  672. * nwords is the number of words to swap.
  673. * Assumes that at least nwords are allocated in both a and b.
  674. * Assumes that no more than nwords are used by either a or b.
  675. */
  676. void BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords)
  677. {
  678. BN_ULONG t;
  679. int i;
  680. if (a == b)
  681. return;
  682. bn_wcheck_size(a, nwords);
  683. bn_wcheck_size(b, nwords);
  684. condition = ((~condition & ((condition - 1))) >> (BN_BITS2 - 1)) - 1;
  685. t = (a->top ^ b->top) & condition;
  686. a->top ^= t;
  687. b->top ^= t;
  688. t = (a->neg ^ b->neg) & condition;
  689. a->neg ^= t;
  690. b->neg ^= t;
  691. /*-
  692. * BN_FLG_STATIC_DATA: indicates that data may not be written to. Intention
  693. * is actually to treat it as it's read-only data, and some (if not most)
  694. * of it does reside in read-only segment. In other words observation of
  695. * BN_FLG_STATIC_DATA in BN_consttime_swap should be treated as fatal
  696. * condition. It would either cause SEGV or effectively cause data
  697. * corruption.
  698. *
  699. * BN_FLG_MALLOCED: refers to BN structure itself, and hence must be
  700. * preserved.
  701. *
  702. * BN_FLG_SECURE: must be preserved, because it determines how x->d was
  703. * allocated and hence how to free it.
  704. *
  705. * BN_FLG_CONSTTIME: sufficient to mask and swap
  706. *
  707. * BN_FLG_FIXED_TOP: indicates that we haven't called bn_correct_top() on
  708. * the data, so the d array may be padded with additional 0 values (i.e.
  709. * top could be greater than the minimal value that it could be). We should
  710. * be swapping it
  711. */
  712. #define BN_CONSTTIME_SWAP_FLAGS (BN_FLG_CONSTTIME | BN_FLG_FIXED_TOP)
  713. t = ((a->flags ^ b->flags) & BN_CONSTTIME_SWAP_FLAGS) & condition;
  714. a->flags ^= t;
  715. b->flags ^= t;
  716. /* conditionally swap the data */
  717. for (i = 0; i < nwords; i++) {
  718. t = (a->d[i] ^ b->d[i]) & condition;
  719. a->d[i] ^= t;
  720. b->d[i] ^= t;
  721. }
  722. }
  723. #undef BN_CONSTTIME_SWAP_FLAGS
  724. /* Bits of security, see SP800-57 */
  725. int BN_security_bits(int L, int N)
  726. {
  727. int secbits, bits;
  728. if (L >= 15360)
  729. secbits = 256;
  730. else if (L >= 7680)
  731. secbits = 192;
  732. else if (L >= 3072)
  733. secbits = 128;
  734. else if (L >= 2048)
  735. secbits = 112;
  736. else if (L >= 1024)
  737. secbits = 80;
  738. else
  739. return 0;
  740. if (N == -1)
  741. return secbits;
  742. bits = N / 2;
  743. if (bits < 80)
  744. return 0;
  745. return bits >= secbits ? secbits : bits;
  746. }
  747. void BN_zero_ex(BIGNUM *a)
  748. {
  749. a->neg = 0;
  750. a->top = 0;
  751. a->flags &= ~BN_FLG_FIXED_TOP;
  752. }
  753. int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w)
  754. {
  755. return ((a->top == 1) && (a->d[0] == w)) || ((w == 0) && (a->top == 0));
  756. }
  757. int BN_is_zero(const BIGNUM *a)
  758. {
  759. return a->top == 0;
  760. }
  761. int BN_is_one(const BIGNUM *a)
  762. {
  763. return BN_abs_is_word(a, 1) && !a->neg;
  764. }
  765. int BN_is_word(const BIGNUM *a, const BN_ULONG w)
  766. {
  767. return BN_abs_is_word(a, w) && (!w || !a->neg);
  768. }
  769. int BN_is_odd(const BIGNUM *a)
  770. {
  771. return (a->top > 0) && (a->d[0] & 1);
  772. }
  773. int BN_is_negative(const BIGNUM *a)
  774. {
  775. return (a->neg != 0);
  776. }
  777. int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,
  778. BN_CTX *ctx)
  779. {
  780. return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);
  781. }
  782. void BN_with_flags(BIGNUM *dest, const BIGNUM *b, int flags)
  783. {
  784. dest->d = b->d;
  785. dest->top = b->top;
  786. dest->dmax = b->dmax;
  787. dest->neg = b->neg;
  788. dest->flags = ((dest->flags & BN_FLG_MALLOCED)
  789. | (b->flags & ~BN_FLG_MALLOCED)
  790. | BN_FLG_STATIC_DATA | flags);
  791. }
  792. BN_GENCB *BN_GENCB_new(void)
  793. {
  794. BN_GENCB *ret;
  795. if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL) {
  796. BNerr(BN_F_BN_GENCB_NEW, ERR_R_MALLOC_FAILURE);
  797. return NULL;
  798. }
  799. return ret;
  800. }
  801. void BN_GENCB_free(BN_GENCB *cb)
  802. {
  803. if (cb == NULL)
  804. return;
  805. OPENSSL_free(cb);
  806. }
  807. void BN_set_flags(BIGNUM *b, int n)
  808. {
  809. b->flags |= n;
  810. }
  811. int BN_get_flags(const BIGNUM *b, int n)
  812. {
  813. return b->flags & n;
  814. }
  815. /* Populate a BN_GENCB structure with an "old"-style callback */
  816. void BN_GENCB_set_old(BN_GENCB *gencb, void (*callback) (int, int, void *),
  817. void *cb_arg)
  818. {
  819. BN_GENCB *tmp_gencb = gencb;
  820. tmp_gencb->ver = 1;
  821. tmp_gencb->arg = cb_arg;
  822. tmp_gencb->cb.cb_1 = callback;
  823. }
  824. /* Populate a BN_GENCB structure with a "new"-style callback */
  825. void BN_GENCB_set(BN_GENCB *gencb, int (*callback) (int, int, BN_GENCB *),
  826. void *cb_arg)
  827. {
  828. BN_GENCB *tmp_gencb = gencb;
  829. tmp_gencb->ver = 2;
  830. tmp_gencb->arg = cb_arg;
  831. tmp_gencb->cb.cb_2 = callback;
  832. }
  833. void *BN_GENCB_get_arg(BN_GENCB *cb)
  834. {
  835. return cb->arg;
  836. }
  837. BIGNUM *bn_wexpand(BIGNUM *a, int words)
  838. {
  839. return (words <= a->dmax) ? a : bn_expand2(a, words);
  840. }
  841. void bn_correct_top(BIGNUM *a)
  842. {
  843. BN_ULONG *ftl;
  844. int tmp_top = a->top;
  845. if (tmp_top > 0) {
  846. for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) {
  847. ftl--;
  848. if (*ftl != 0)
  849. break;
  850. }
  851. a->top = tmp_top;
  852. }
  853. if (a->top == 0)
  854. a->neg = 0;
  855. a->flags &= ~BN_FLG_FIXED_TOP;
  856. bn_pollute(a);
  857. }