bn_lib.c 21 KB

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