bn_lib.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  1. /*
  2. * Copyright 1995-2022 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 "internal/endian.h"
  13. #include "bn_local.h"
  14. #include <openssl/opensslconf.h>
  15. #include "internal/constant_time.h"
  16. /* This stuff appears to be completely unused, so is deprecated */
  17. #ifndef OPENSSL_NO_DEPRECATED_0_9_8
  18. /*-
  19. * For a 32 bit machine
  20. * 2 - 4 == 128
  21. * 3 - 8 == 256
  22. * 4 - 16 == 512
  23. * 5 - 32 == 1024
  24. * 6 - 64 == 2048
  25. * 7 - 128 == 4096
  26. * 8 - 256 == 8192
  27. */
  28. static int bn_limit_bits = 0;
  29. static int bn_limit_num = 8; /* (1<<bn_limit_bits) */
  30. static int bn_limit_bits_low = 0;
  31. static int bn_limit_num_low = 8; /* (1<<bn_limit_bits_low) */
  32. static int bn_limit_bits_high = 0;
  33. static int bn_limit_num_high = 8; /* (1<<bn_limit_bits_high) */
  34. static int bn_limit_bits_mont = 0;
  35. static int bn_limit_num_mont = 8; /* (1<<bn_limit_bits_mont) */
  36. void BN_set_params(int mult, int high, int low, int mont)
  37. {
  38. if (mult >= 0) {
  39. if (mult > (int)(sizeof(int) * 8) - 1)
  40. mult = sizeof(int) * 8 - 1;
  41. bn_limit_bits = mult;
  42. bn_limit_num = 1 << mult;
  43. }
  44. if (high >= 0) {
  45. if (high > (int)(sizeof(int) * 8) - 1)
  46. high = sizeof(int) * 8 - 1;
  47. bn_limit_bits_high = high;
  48. bn_limit_num_high = 1 << high;
  49. }
  50. if (low >= 0) {
  51. if (low > (int)(sizeof(int) * 8) - 1)
  52. low = sizeof(int) * 8 - 1;
  53. bn_limit_bits_low = low;
  54. bn_limit_num_low = 1 << low;
  55. }
  56. if (mont >= 0) {
  57. if (mont > (int)(sizeof(int) * 8) - 1)
  58. mont = sizeof(int) * 8 - 1;
  59. bn_limit_bits_mont = mont;
  60. bn_limit_num_mont = 1 << mont;
  61. }
  62. }
  63. int BN_get_params(int which)
  64. {
  65. if (which == 0)
  66. return bn_limit_bits;
  67. else if (which == 1)
  68. return bn_limit_bits_high;
  69. else if (which == 2)
  70. return bn_limit_bits_low;
  71. else if (which == 3)
  72. return bn_limit_bits_mont;
  73. else
  74. return 0;
  75. }
  76. #endif
  77. const BIGNUM *BN_value_one(void)
  78. {
  79. static const BN_ULONG data_one = 1L;
  80. static const BIGNUM const_one =
  81. { (BN_ULONG *)&data_one, 1, 1, 0, BN_FLG_STATIC_DATA };
  82. return &const_one;
  83. }
  84. /*
  85. * Old Visual Studio ARM compiler miscompiles BN_num_bits_word()
  86. * https://mta.openssl.org/pipermail/openssl-users/2018-August/008465.html
  87. */
  88. #if defined(_MSC_VER) && defined(_ARM_) && defined(_WIN32_WCE) \
  89. && _MSC_VER>=1400 && _MSC_VER<1501
  90. # define MS_BROKEN_BN_num_bits_word
  91. # pragma optimize("", off)
  92. #endif
  93. int BN_num_bits_word(BN_ULONG l)
  94. {
  95. BN_ULONG x, mask;
  96. int bits = (l != 0);
  97. #if BN_BITS2 > 32
  98. x = l >> 32;
  99. mask = (0 - x) & BN_MASK2;
  100. mask = (0 - (mask >> (BN_BITS2 - 1)));
  101. bits += 32 & mask;
  102. l ^= (x ^ l) & mask;
  103. #endif
  104. x = l >> 16;
  105. mask = (0 - x) & BN_MASK2;
  106. mask = (0 - (mask >> (BN_BITS2 - 1)));
  107. bits += 16 & mask;
  108. l ^= (x ^ l) & mask;
  109. x = l >> 8;
  110. mask = (0 - x) & BN_MASK2;
  111. mask = (0 - (mask >> (BN_BITS2 - 1)));
  112. bits += 8 & mask;
  113. l ^= (x ^ l) & mask;
  114. x = l >> 4;
  115. mask = (0 - x) & BN_MASK2;
  116. mask = (0 - (mask >> (BN_BITS2 - 1)));
  117. bits += 4 & mask;
  118. l ^= (x ^ l) & mask;
  119. x = l >> 2;
  120. mask = (0 - x) & BN_MASK2;
  121. mask = (0 - (mask >> (BN_BITS2 - 1)));
  122. bits += 2 & mask;
  123. l ^= (x ^ l) & mask;
  124. x = l >> 1;
  125. mask = (0 - x) & BN_MASK2;
  126. mask = (0 - (mask >> (BN_BITS2 - 1)));
  127. bits += 1 & mask;
  128. return bits;
  129. }
  130. #ifdef MS_BROKEN_BN_num_bits_word
  131. # pragma optimize("", on)
  132. #endif
  133. /*
  134. * This function still leaks `a->dmax`: it's caller's responsibility to
  135. * expand the input `a` in advance to a public length.
  136. */
  137. static ossl_inline
  138. int bn_num_bits_consttime(const BIGNUM *a)
  139. {
  140. int j, ret;
  141. unsigned int mask, past_i;
  142. int i = a->top - 1;
  143. bn_check_top(a);
  144. for (j = 0, past_i = 0, ret = 0; j < a->dmax; j++) {
  145. mask = constant_time_eq_int(i, j); /* 0xff..ff if i==j, 0x0 otherwise */
  146. ret += BN_BITS2 & (~mask & ~past_i);
  147. ret += BN_num_bits_word(a->d[j]) & mask;
  148. past_i |= mask; /* past_i will become 0xff..ff after i==j */
  149. }
  150. /*
  151. * if BN_is_zero(a) => i is -1 and ret contains garbage, so we mask the
  152. * final result.
  153. */
  154. mask = ~(constant_time_eq_int(i, ((int)-1)));
  155. return ret & mask;
  156. }
  157. int BN_num_bits(const BIGNUM *a)
  158. {
  159. int i = a->top - 1;
  160. bn_check_top(a);
  161. if (a->flags & BN_FLG_CONSTTIME) {
  162. /*
  163. * We assume that BIGNUMs flagged as CONSTTIME have also been expanded
  164. * so that a->dmax is not leaking secret information.
  165. *
  166. * In other words, it's the caller's responsibility to ensure `a` has
  167. * been preallocated in advance to a public length if we hit this
  168. * branch.
  169. *
  170. */
  171. return bn_num_bits_consttime(a);
  172. }
  173. if (BN_is_zero(a))
  174. return 0;
  175. return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));
  176. }
  177. static void bn_free_d(BIGNUM *a, int clear)
  178. {
  179. if (BN_get_flags(a, BN_FLG_SECURE))
  180. OPENSSL_secure_clear_free(a->d, a->dmax * sizeof(a->d[0]));
  181. else if (clear != 0)
  182. OPENSSL_clear_free(a->d, a->dmax * sizeof(a->d[0]));
  183. else
  184. OPENSSL_free(a->d);
  185. }
  186. void BN_clear_free(BIGNUM *a)
  187. {
  188. if (a == NULL)
  189. return;
  190. if (a->d != NULL && !BN_get_flags(a, BN_FLG_STATIC_DATA))
  191. bn_free_d(a, 1);
  192. if (BN_get_flags(a, BN_FLG_MALLOCED)) {
  193. OPENSSL_cleanse(a, sizeof(*a));
  194. OPENSSL_free(a);
  195. }
  196. }
  197. void BN_free(BIGNUM *a)
  198. {
  199. if (a == NULL)
  200. return;
  201. if (!BN_get_flags(a, BN_FLG_STATIC_DATA))
  202. bn_free_d(a, 0);
  203. if (a->flags & BN_FLG_MALLOCED)
  204. OPENSSL_free(a);
  205. }
  206. void bn_init(BIGNUM *a)
  207. {
  208. static BIGNUM nilbn;
  209. *a = nilbn;
  210. bn_check_top(a);
  211. }
  212. BIGNUM *BN_new(void)
  213. {
  214. BIGNUM *ret;
  215. if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
  216. return NULL;
  217. ret->flags = BN_FLG_MALLOCED;
  218. bn_check_top(ret);
  219. return ret;
  220. }
  221. BIGNUM *BN_secure_new(void)
  222. {
  223. BIGNUM *ret = BN_new();
  224. if (ret != NULL)
  225. ret->flags |= BN_FLG_SECURE;
  226. return ret;
  227. }
  228. /* This is used by bn_expand2() */
  229. /* The caller MUST check that words > b->dmax before calling this */
  230. static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
  231. {
  232. BN_ULONG *a = NULL;
  233. if (words > (INT_MAX / (4 * BN_BITS2))) {
  234. ERR_raise(ERR_LIB_BN, BN_R_BIGNUM_TOO_LONG);
  235. return NULL;
  236. }
  237. if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
  238. ERR_raise(ERR_LIB_BN, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
  239. return NULL;
  240. }
  241. if (BN_get_flags(b, BN_FLG_SECURE))
  242. a = OPENSSL_secure_zalloc(words * sizeof(*a));
  243. else
  244. a = OPENSSL_zalloc(words * sizeof(*a));
  245. if (a == NULL)
  246. return NULL;
  247. assert(b->top <= words);
  248. if (b->top > 0)
  249. memcpy(a, b->d, sizeof(*a) * b->top);
  250. return a;
  251. }
  252. /*
  253. * This is an internal function that should not be used in applications. It
  254. * ensures that 'b' has enough room for a 'words' word number and initialises
  255. * any unused part of b->d with leading zeros. It is mostly used by the
  256. * various BIGNUM routines. If there is an error, NULL is returned. If not,
  257. * 'b' is returned.
  258. */
  259. BIGNUM *bn_expand2(BIGNUM *b, int words)
  260. {
  261. if (words > b->dmax) {
  262. BN_ULONG *a = bn_expand_internal(b, words);
  263. if (!a)
  264. return NULL;
  265. if (b->d != NULL)
  266. bn_free_d(b, 1);
  267. b->d = a;
  268. b->dmax = words;
  269. }
  270. return b;
  271. }
  272. BIGNUM *BN_dup(const BIGNUM *a)
  273. {
  274. BIGNUM *t;
  275. if (a == NULL)
  276. return NULL;
  277. bn_check_top(a);
  278. t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new();
  279. if (t == NULL)
  280. return NULL;
  281. if (!BN_copy(t, a)) {
  282. BN_free(t);
  283. return NULL;
  284. }
  285. bn_check_top(t);
  286. return t;
  287. }
  288. BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
  289. {
  290. int bn_words;
  291. bn_check_top(b);
  292. bn_words = BN_get_flags(b, BN_FLG_CONSTTIME) ? b->dmax : b->top;
  293. if (a == b)
  294. return a;
  295. if (bn_wexpand(a, bn_words) == NULL)
  296. return NULL;
  297. if (b->top > 0)
  298. memcpy(a->d, b->d, sizeof(b->d[0]) * bn_words);
  299. a->neg = b->neg;
  300. a->top = b->top;
  301. a->flags |= b->flags & BN_FLG_FIXED_TOP;
  302. bn_check_top(a);
  303. return a;
  304. }
  305. #define FLAGS_DATA(flags) ((flags) & (BN_FLG_STATIC_DATA \
  306. | BN_FLG_CONSTTIME \
  307. | BN_FLG_SECURE \
  308. | BN_FLG_FIXED_TOP))
  309. #define FLAGS_STRUCT(flags) ((flags) & (BN_FLG_MALLOCED))
  310. void BN_swap(BIGNUM *a, BIGNUM *b)
  311. {
  312. int flags_old_a, flags_old_b;
  313. BN_ULONG *tmp_d;
  314. int tmp_top, tmp_dmax, tmp_neg;
  315. bn_check_top(a);
  316. bn_check_top(b);
  317. flags_old_a = a->flags;
  318. flags_old_b = b->flags;
  319. tmp_d = a->d;
  320. tmp_top = a->top;
  321. tmp_dmax = a->dmax;
  322. tmp_neg = a->neg;
  323. a->d = b->d;
  324. a->top = b->top;
  325. a->dmax = b->dmax;
  326. a->neg = b->neg;
  327. b->d = tmp_d;
  328. b->top = tmp_top;
  329. b->dmax = tmp_dmax;
  330. b->neg = tmp_neg;
  331. a->flags = FLAGS_STRUCT(flags_old_a) | FLAGS_DATA(flags_old_b);
  332. b->flags = FLAGS_STRUCT(flags_old_b) | FLAGS_DATA(flags_old_a);
  333. bn_check_top(a);
  334. bn_check_top(b);
  335. }
  336. void BN_clear(BIGNUM *a)
  337. {
  338. if (a == NULL)
  339. return;
  340. bn_check_top(a);
  341. if (a->d != NULL)
  342. OPENSSL_cleanse(a->d, sizeof(*a->d) * a->dmax);
  343. a->neg = 0;
  344. a->top = 0;
  345. a->flags &= ~BN_FLG_FIXED_TOP;
  346. }
  347. BN_ULONG BN_get_word(const BIGNUM *a)
  348. {
  349. if (a->top > 1)
  350. return BN_MASK2;
  351. else if (a->top == 1)
  352. return a->d[0];
  353. /* a->top == 0 */
  354. return 0;
  355. }
  356. int BN_set_word(BIGNUM *a, BN_ULONG w)
  357. {
  358. bn_check_top(a);
  359. if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
  360. return 0;
  361. a->neg = 0;
  362. a->d[0] = w;
  363. a->top = (w ? 1 : 0);
  364. a->flags &= ~BN_FLG_FIXED_TOP;
  365. bn_check_top(a);
  366. return 1;
  367. }
  368. typedef enum {BIG, LITTLE} endianess_t;
  369. typedef enum {SIGNED, UNSIGNED} signedness_t;
  370. static BIGNUM *bin2bn(const unsigned char *s, int len, BIGNUM *ret,
  371. endianess_t endianess, signedness_t signedness)
  372. {
  373. int inc;
  374. const unsigned char *s2;
  375. int inc2;
  376. int neg = 0, xor = 0, carry = 0;
  377. unsigned int i;
  378. unsigned int n;
  379. BIGNUM *bn = NULL;
  380. if (ret == NULL)
  381. ret = bn = BN_new();
  382. if (ret == NULL)
  383. return NULL;
  384. bn_check_top(ret);
  385. /*
  386. * The loop that does the work iterates from least to most
  387. * significant BIGNUM chunk, so we adapt parameters to transfer
  388. * input bytes accordingly.
  389. */
  390. if (endianess == LITTLE) {
  391. s2 = s + len - 1;
  392. inc2 = -1;
  393. inc = 1;
  394. } else {
  395. s2 = s;
  396. inc2 = 1;
  397. inc = -1;
  398. s += len - 1;
  399. }
  400. /* Take note of the signedness of the input bytes*/
  401. if (signedness == SIGNED) {
  402. neg = !!(*s2 & 0x80);
  403. xor = neg ? 0xff : 0x00;
  404. carry = neg;
  405. }
  406. /*
  407. * Skip leading sign extensions (the value of |xor|).
  408. * This is the only spot where |s2| and |inc2| are used.
  409. */
  410. for ( ; len > 0 && *s2 == xor; s2 += inc2, len--)
  411. continue;
  412. /*
  413. * If there was a set of 0xff, we backtrack one byte unless the next
  414. * one has a sign bit, as the last 0xff is then part of the actual
  415. * number, rather then a mere sign extension.
  416. */
  417. if (xor == 0xff) {
  418. if (len == 0 || !(*s2 & 0x80))
  419. len++;
  420. }
  421. /* If it was all zeros, we're done */
  422. if (len == 0) {
  423. ret->top = 0;
  424. return ret;
  425. }
  426. n = ((len - 1) / BN_BYTES) + 1; /* Number of resulting bignum chunks */
  427. if (!ossl_assert(bn_wexpand(ret, (int)n) != NULL)) {
  428. BN_free(bn);
  429. return NULL;
  430. }
  431. ret->top = n;
  432. ret->neg = neg;
  433. for (i = 0; n-- > 0; i++) {
  434. BN_ULONG l = 0; /* Accumulator */
  435. unsigned int m = 0; /* Offset in a bignum chunk, in bits */
  436. for (; len > 0 && m < BN_BYTES * 8; len--, s += inc, m += 8) {
  437. BN_ULONG byte_xored = *s ^ xor;
  438. BN_ULONG byte = (byte_xored + carry) & 0xff;
  439. carry = byte_xored > byte; /* Implicit 1 or 0 */
  440. l |= (byte << m);
  441. }
  442. ret->d[i] = l;
  443. }
  444. /*
  445. * need to call this due to clear byte at top if avoiding having the top
  446. * bit set (-ve number)
  447. */
  448. bn_correct_top(ret);
  449. return ret;
  450. }
  451. BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
  452. {
  453. return bin2bn(s, len, ret, BIG, UNSIGNED);
  454. }
  455. BIGNUM *BN_signed_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
  456. {
  457. return bin2bn(s, len, ret, BIG, SIGNED);
  458. }
  459. static int bn2binpad(const BIGNUM *a, unsigned char *to, int tolen,
  460. endianess_t endianess, signedness_t signedness)
  461. {
  462. int inc;
  463. int n, n8;
  464. int xor = 0, carry = 0, ext = 0;
  465. size_t i, lasti, j, atop, mask;
  466. BN_ULONG l;
  467. /*
  468. * In case |a| is fixed-top, BN_num_bits can return bogus length,
  469. * but it's assumed that fixed-top inputs ought to be "nominated"
  470. * even for padded output, so it works out...
  471. */
  472. n8 = BN_num_bits(a);
  473. n = (n8 + 7) / 8; /* This is what BN_num_bytes() does */
  474. /* Take note of the signedness of the bignum */
  475. if (signedness == SIGNED) {
  476. xor = a->neg ? 0xff : 0x00;
  477. carry = a->neg;
  478. /*
  479. * if |n * 8 == n|, then the MSbit is set, otherwise unset.
  480. * We must compensate with one extra byte if that doesn't
  481. * correspond to the signedness of the bignum with regards
  482. * to 2's complement.
  483. */
  484. ext = (n * 8 == n8)
  485. ? !a->neg /* MSbit set on nonnegative bignum */
  486. : a->neg; /* MSbit unset on negative bignum */
  487. }
  488. if (tolen == -1) {
  489. tolen = n + ext;
  490. } else if (tolen < n + ext) { /* uncommon/unlike case */
  491. BIGNUM temp = *a;
  492. bn_correct_top(&temp);
  493. n8 = BN_num_bits(&temp);
  494. n = (n8 + 7) / 8; /* This is what BN_num_bytes() does */
  495. if (tolen < n + ext)
  496. return -1;
  497. }
  498. /* Swipe through whole available data and don't give away padded zero. */
  499. atop = a->dmax * BN_BYTES;
  500. if (atop == 0) {
  501. if (tolen != 0)
  502. memset(to, '\0', tolen);
  503. return tolen;
  504. }
  505. /*
  506. * The loop that does the work iterates from least significant
  507. * to most significant BIGNUM limb, so we adapt parameters to
  508. * transfer output bytes accordingly.
  509. */
  510. if (endianess == LITTLE) {
  511. inc = 1;
  512. } else {
  513. inc = -1;
  514. to += tolen - 1; /* Move to the last byte, not beyond */
  515. }
  516. lasti = atop - 1;
  517. atop = a->top * BN_BYTES;
  518. for (i = 0, j = 0; j < (size_t)tolen; j++) {
  519. unsigned char byte, byte_xored;
  520. l = a->d[i / BN_BYTES];
  521. mask = 0 - ((j - atop) >> (8 * sizeof(i) - 1));
  522. byte = (unsigned char)(l >> (8 * (i % BN_BYTES)) & mask);
  523. byte_xored = byte ^ xor;
  524. *to = (unsigned char)(byte_xored + carry);
  525. carry = byte_xored > *to; /* Implicit 1 or 0 */
  526. to += inc;
  527. i += (i - lasti) >> (8 * sizeof(i) - 1); /* stay on last limb */
  528. }
  529. return tolen;
  530. }
  531. int BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen)
  532. {
  533. if (tolen < 0)
  534. return -1;
  535. return bn2binpad(a, to, tolen, BIG, UNSIGNED);
  536. }
  537. int BN_signed_bn2bin(const BIGNUM *a, unsigned char *to, int tolen)
  538. {
  539. if (tolen < 0)
  540. return -1;
  541. return bn2binpad(a, to, tolen, BIG, SIGNED);
  542. }
  543. int BN_bn2bin(const BIGNUM *a, unsigned char *to)
  544. {
  545. return bn2binpad(a, to, -1, BIG, UNSIGNED);
  546. }
  547. BIGNUM *BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret)
  548. {
  549. return bin2bn(s, len, ret, LITTLE, UNSIGNED);
  550. }
  551. BIGNUM *BN_signed_lebin2bn(const unsigned char *s, int len, BIGNUM *ret)
  552. {
  553. return bin2bn(s, len, ret, LITTLE, SIGNED);
  554. }
  555. int BN_bn2lebinpad(const BIGNUM *a, unsigned char *to, int tolen)
  556. {
  557. if (tolen < 0)
  558. return -1;
  559. return bn2binpad(a, to, tolen, LITTLE, UNSIGNED);
  560. }
  561. int BN_signed_bn2lebin(const BIGNUM *a, unsigned char *to, int tolen)
  562. {
  563. if (tolen < 0)
  564. return -1;
  565. return bn2binpad(a, to, tolen, LITTLE, SIGNED);
  566. }
  567. BIGNUM *BN_native2bn(const unsigned char *s, int len, BIGNUM *ret)
  568. {
  569. DECLARE_IS_ENDIAN;
  570. if (IS_LITTLE_ENDIAN)
  571. return BN_lebin2bn(s, len, ret);
  572. return BN_bin2bn(s, len, ret);
  573. }
  574. BIGNUM *BN_signed_native2bn(const unsigned char *s, int len, BIGNUM *ret)
  575. {
  576. DECLARE_IS_ENDIAN;
  577. if (IS_LITTLE_ENDIAN)
  578. return BN_signed_lebin2bn(s, len, ret);
  579. return BN_signed_bin2bn(s, len, ret);
  580. }
  581. int BN_bn2nativepad(const BIGNUM *a, unsigned char *to, int tolen)
  582. {
  583. DECLARE_IS_ENDIAN;
  584. if (IS_LITTLE_ENDIAN)
  585. return BN_bn2lebinpad(a, to, tolen);
  586. return BN_bn2binpad(a, to, tolen);
  587. }
  588. int BN_signed_bn2native(const BIGNUM *a, unsigned char *to, int tolen)
  589. {
  590. DECLARE_IS_ENDIAN;
  591. if (IS_LITTLE_ENDIAN)
  592. return BN_signed_bn2lebin(a, to, tolen);
  593. return BN_signed_bn2bin(a, to, tolen);
  594. }
  595. int BN_ucmp(const BIGNUM *a, const BIGNUM *b)
  596. {
  597. int i;
  598. BN_ULONG t1, t2, *ap, *bp;
  599. bn_check_top(a);
  600. bn_check_top(b);
  601. i = a->top - b->top;
  602. if (i != 0)
  603. return i;
  604. ap = a->d;
  605. bp = b->d;
  606. for (i = a->top - 1; i >= 0; i--) {
  607. t1 = ap[i];
  608. t2 = bp[i];
  609. if (t1 != t2)
  610. return ((t1 > t2) ? 1 : -1);
  611. }
  612. return 0;
  613. }
  614. int BN_cmp(const BIGNUM *a, const BIGNUM *b)
  615. {
  616. int i;
  617. int gt, lt;
  618. BN_ULONG t1, t2;
  619. if ((a == NULL) || (b == NULL)) {
  620. if (a != NULL)
  621. return -1;
  622. else if (b != NULL)
  623. return 1;
  624. else
  625. return 0;
  626. }
  627. bn_check_top(a);
  628. bn_check_top(b);
  629. if (a->neg != b->neg) {
  630. if (a->neg)
  631. return -1;
  632. else
  633. return 1;
  634. }
  635. if (a->neg == 0) {
  636. gt = 1;
  637. lt = -1;
  638. } else {
  639. gt = -1;
  640. lt = 1;
  641. }
  642. if (a->top > b->top)
  643. return gt;
  644. if (a->top < b->top)
  645. return lt;
  646. for (i = a->top - 1; i >= 0; i--) {
  647. t1 = a->d[i];
  648. t2 = b->d[i];
  649. if (t1 > t2)
  650. return gt;
  651. if (t1 < t2)
  652. return lt;
  653. }
  654. return 0;
  655. }
  656. int BN_set_bit(BIGNUM *a, int n)
  657. {
  658. int i, j, k;
  659. if (n < 0)
  660. return 0;
  661. i = n / BN_BITS2;
  662. j = n % BN_BITS2;
  663. if (a->top <= i) {
  664. if (bn_wexpand(a, i + 1) == NULL)
  665. return 0;
  666. for (k = a->top; k < i + 1; k++)
  667. a->d[k] = 0;
  668. a->top = i + 1;
  669. a->flags &= ~BN_FLG_FIXED_TOP;
  670. }
  671. a->d[i] |= (((BN_ULONG)1) << j);
  672. bn_check_top(a);
  673. return 1;
  674. }
  675. int BN_clear_bit(BIGNUM *a, int n)
  676. {
  677. int i, j;
  678. bn_check_top(a);
  679. if (n < 0)
  680. return 0;
  681. i = n / BN_BITS2;
  682. j = n % BN_BITS2;
  683. if (a->top <= i)
  684. return 0;
  685. a->d[i] &= (~(((BN_ULONG)1) << j));
  686. bn_correct_top(a);
  687. return 1;
  688. }
  689. int BN_is_bit_set(const BIGNUM *a, int n)
  690. {
  691. int i, j;
  692. bn_check_top(a);
  693. if (n < 0)
  694. return 0;
  695. i = n / BN_BITS2;
  696. j = n % BN_BITS2;
  697. if (a->top <= i)
  698. return 0;
  699. return (int)(((a->d[i]) >> j) & ((BN_ULONG)1));
  700. }
  701. int BN_mask_bits(BIGNUM *a, int n)
  702. {
  703. int b, w;
  704. bn_check_top(a);
  705. if (n < 0)
  706. return 0;
  707. w = n / BN_BITS2;
  708. b = n % BN_BITS2;
  709. if (w >= a->top)
  710. return 0;
  711. if (b == 0)
  712. a->top = w;
  713. else {
  714. a->top = w + 1;
  715. a->d[w] &= ~(BN_MASK2 << b);
  716. }
  717. bn_correct_top(a);
  718. return 1;
  719. }
  720. void BN_set_negative(BIGNUM *a, int b)
  721. {
  722. if (b && !BN_is_zero(a))
  723. a->neg = 1;
  724. else
  725. a->neg = 0;
  726. }
  727. int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
  728. {
  729. int i;
  730. BN_ULONG aa, bb;
  731. if (n == 0)
  732. return 0;
  733. aa = a[n - 1];
  734. bb = b[n - 1];
  735. if (aa != bb)
  736. return ((aa > bb) ? 1 : -1);
  737. for (i = n - 2; i >= 0; i--) {
  738. aa = a[i];
  739. bb = b[i];
  740. if (aa != bb)
  741. return ((aa > bb) ? 1 : -1);
  742. }
  743. return 0;
  744. }
  745. /*
  746. * Here follows a specialised variants of bn_cmp_words(). It has the
  747. * capability of performing the operation on arrays of different sizes. The
  748. * sizes of those arrays is expressed through cl, which is the common length
  749. * ( basically, min(len(a),len(b)) ), and dl, which is the delta between the
  750. * two lengths, calculated as len(a)-len(b). All lengths are the number of
  751. * BN_ULONGs...
  752. */
  753. int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl)
  754. {
  755. int n, i;
  756. n = cl - 1;
  757. if (dl < 0) {
  758. for (i = dl; i < 0; i++) {
  759. if (b[n - i] != 0)
  760. return -1; /* a < b */
  761. }
  762. }
  763. if (dl > 0) {
  764. for (i = dl; i > 0; i--) {
  765. if (a[n + i] != 0)
  766. return 1; /* a > b */
  767. }
  768. }
  769. return bn_cmp_words(a, b, cl);
  770. }
  771. /*-
  772. * Constant-time conditional swap of a and b.
  773. * a and b are swapped if condition is not 0.
  774. * nwords is the number of words to swap.
  775. * Assumes that at least nwords are allocated in both a and b.
  776. * Assumes that no more than nwords are used by either a or b.
  777. */
  778. void BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords)
  779. {
  780. BN_ULONG t;
  781. int i;
  782. bn_wcheck_size(a, nwords);
  783. bn_wcheck_size(b, nwords);
  784. condition = ((~condition & ((condition - 1))) >> (BN_BITS2 - 1)) - 1;
  785. t = (a->top ^ b->top) & condition;
  786. a->top ^= t;
  787. b->top ^= t;
  788. t = (a->neg ^ b->neg) & condition;
  789. a->neg ^= t;
  790. b->neg ^= t;
  791. /*-
  792. * BN_FLG_STATIC_DATA: indicates that data may not be written to. Intention
  793. * is actually to treat it as it's read-only data, and some (if not most)
  794. * of it does reside in read-only segment. In other words observation of
  795. * BN_FLG_STATIC_DATA in BN_consttime_swap should be treated as fatal
  796. * condition. It would either cause SEGV or effectively cause data
  797. * corruption.
  798. *
  799. * BN_FLG_MALLOCED: refers to BN structure itself, and hence must be
  800. * preserved.
  801. *
  802. * BN_FLG_SECURE: must be preserved, because it determines how x->d was
  803. * allocated and hence how to free it.
  804. *
  805. * BN_FLG_CONSTTIME: sufficient to mask and swap
  806. *
  807. * BN_FLG_FIXED_TOP: indicates that we haven't called bn_correct_top() on
  808. * the data, so the d array may be padded with additional 0 values (i.e.
  809. * top could be greater than the minimal value that it could be). We should
  810. * be swapping it
  811. */
  812. #define BN_CONSTTIME_SWAP_FLAGS (BN_FLG_CONSTTIME | BN_FLG_FIXED_TOP)
  813. t = ((a->flags ^ b->flags) & BN_CONSTTIME_SWAP_FLAGS) & condition;
  814. a->flags ^= t;
  815. b->flags ^= t;
  816. /* conditionally swap the data */
  817. for (i = 0; i < nwords; i++) {
  818. t = (a->d[i] ^ b->d[i]) & condition;
  819. a->d[i] ^= t;
  820. b->d[i] ^= t;
  821. }
  822. }
  823. #undef BN_CONSTTIME_SWAP_FLAGS
  824. /* Bits of security, see SP800-57 */
  825. int BN_security_bits(int L, int N)
  826. {
  827. int secbits, bits;
  828. if (L >= 15360)
  829. secbits = 256;
  830. else if (L >= 7680)
  831. secbits = 192;
  832. else if (L >= 3072)
  833. secbits = 128;
  834. else if (L >= 2048)
  835. secbits = 112;
  836. else if (L >= 1024)
  837. secbits = 80;
  838. else
  839. return 0;
  840. if (N == -1)
  841. return secbits;
  842. bits = N / 2;
  843. if (bits < 80)
  844. return 0;
  845. return bits >= secbits ? secbits : bits;
  846. }
  847. void BN_zero_ex(BIGNUM *a)
  848. {
  849. a->neg = 0;
  850. a->top = 0;
  851. a->flags &= ~BN_FLG_FIXED_TOP;
  852. }
  853. int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w)
  854. {
  855. return ((a->top == 1) && (a->d[0] == w)) || ((w == 0) && (a->top == 0));
  856. }
  857. int BN_is_zero(const BIGNUM *a)
  858. {
  859. return a->top == 0;
  860. }
  861. int BN_is_one(const BIGNUM *a)
  862. {
  863. return BN_abs_is_word(a, 1) && !a->neg;
  864. }
  865. int BN_is_word(const BIGNUM *a, const BN_ULONG w)
  866. {
  867. return BN_abs_is_word(a, w) && (!w || !a->neg);
  868. }
  869. int BN_is_odd(const BIGNUM *a)
  870. {
  871. return (a->top > 0) && (a->d[0] & 1);
  872. }
  873. int BN_is_negative(const BIGNUM *a)
  874. {
  875. return (a->neg != 0);
  876. }
  877. int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,
  878. BN_CTX *ctx)
  879. {
  880. return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);
  881. }
  882. void BN_with_flags(BIGNUM *dest, const BIGNUM *b, int flags)
  883. {
  884. dest->d = b->d;
  885. dest->top = b->top;
  886. dest->dmax = b->dmax;
  887. dest->neg = b->neg;
  888. dest->flags = ((dest->flags & BN_FLG_MALLOCED)
  889. | (b->flags & ~BN_FLG_MALLOCED)
  890. | BN_FLG_STATIC_DATA | flags);
  891. }
  892. BN_GENCB *BN_GENCB_new(void)
  893. {
  894. BN_GENCB *ret;
  895. if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL)
  896. return NULL;
  897. return ret;
  898. }
  899. void BN_GENCB_free(BN_GENCB *cb)
  900. {
  901. if (cb == NULL)
  902. return;
  903. OPENSSL_free(cb);
  904. }
  905. void BN_set_flags(BIGNUM *b, int n)
  906. {
  907. b->flags |= n;
  908. }
  909. int BN_get_flags(const BIGNUM *b, int n)
  910. {
  911. return b->flags & n;
  912. }
  913. /* Populate a BN_GENCB structure with an "old"-style callback */
  914. void BN_GENCB_set_old(BN_GENCB *gencb, void (*callback) (int, int, void *),
  915. void *cb_arg)
  916. {
  917. BN_GENCB *tmp_gencb = gencb;
  918. tmp_gencb->ver = 1;
  919. tmp_gencb->arg = cb_arg;
  920. tmp_gencb->cb.cb_1 = callback;
  921. }
  922. /* Populate a BN_GENCB structure with a "new"-style callback */
  923. void BN_GENCB_set(BN_GENCB *gencb, int (*callback) (int, int, BN_GENCB *),
  924. void *cb_arg)
  925. {
  926. BN_GENCB *tmp_gencb = gencb;
  927. tmp_gencb->ver = 2;
  928. tmp_gencb->arg = cb_arg;
  929. tmp_gencb->cb.cb_2 = callback;
  930. }
  931. void *BN_GENCB_get_arg(BN_GENCB *cb)
  932. {
  933. return cb->arg;
  934. }
  935. BIGNUM *bn_wexpand(BIGNUM *a, int words)
  936. {
  937. return (words <= a->dmax) ? a : bn_expand2(a, words);
  938. }
  939. void bn_correct_top(BIGNUM *a)
  940. {
  941. BN_ULONG *ftl;
  942. int tmp_top = a->top;
  943. if (tmp_top > 0) {
  944. for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) {
  945. ftl--;
  946. if (*ftl != 0)
  947. break;
  948. }
  949. a->top = tmp_top;
  950. }
  951. if (a->top == 0)
  952. a->neg = 0;
  953. a->flags &= ~BN_FLG_FIXED_TOP;
  954. bn_pollute(a);
  955. }