bn_lib.c 23 KB

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