bn_lib.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  1. /*
  2. * Copyright 1995-2023 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} endianness_t;
  369. typedef enum {SIGNED, UNSIGNED} signedness_t;
  370. static BIGNUM *bin2bn(const unsigned char *s, int len, BIGNUM *ret,
  371. endianness_t endianness, 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. /* Negative length is not acceptable */
  381. if (len < 0)
  382. return NULL;
  383. if (ret == NULL)
  384. ret = bn = BN_new();
  385. if (ret == NULL)
  386. return NULL;
  387. bn_check_top(ret);
  388. /*
  389. * If the input has no bits, the number is considered zero.
  390. * This makes calls with s==NULL and len==0 safe.
  391. */
  392. if (len == 0) {
  393. BN_clear(ret);
  394. return ret;
  395. }
  396. /*
  397. * The loop that does the work iterates from least to most
  398. * significant BIGNUM chunk, so we adapt parameters to transfer
  399. * input bytes accordingly.
  400. */
  401. if (endianness == LITTLE) {
  402. s2 = s + len - 1;
  403. inc2 = -1;
  404. inc = 1;
  405. } else {
  406. s2 = s;
  407. inc2 = 1;
  408. inc = -1;
  409. s += len - 1;
  410. }
  411. /* Take note of the signedness of the input bytes*/
  412. if (signedness == SIGNED) {
  413. neg = !!(*s2 & 0x80);
  414. xor = neg ? 0xff : 0x00;
  415. carry = neg;
  416. }
  417. /*
  418. * Skip leading sign extensions (the value of |xor|).
  419. * This is the only spot where |s2| and |inc2| are used.
  420. */
  421. for ( ; len > 0 && *s2 == xor; s2 += inc2, len--)
  422. continue;
  423. /*
  424. * If there was a set of 0xff, we backtrack one byte unless the next
  425. * one has a sign bit, as the last 0xff is then part of the actual
  426. * number, rather then a mere sign extension.
  427. */
  428. if (xor == 0xff) {
  429. if (len == 0 || !(*s2 & 0x80))
  430. len++;
  431. }
  432. /* If it was all zeros, we're done */
  433. if (len == 0) {
  434. ret->top = 0;
  435. return ret;
  436. }
  437. n = ((len - 1) / BN_BYTES) + 1; /* Number of resulting bignum chunks */
  438. if (bn_wexpand(ret, (int)n) == NULL) {
  439. BN_free(bn);
  440. return NULL;
  441. }
  442. ret->top = n;
  443. ret->neg = neg;
  444. for (i = 0; n-- > 0; i++) {
  445. BN_ULONG l = 0; /* Accumulator */
  446. unsigned int m = 0; /* Offset in a bignum chunk, in bits */
  447. for (; len > 0 && m < BN_BYTES * 8; len--, s += inc, m += 8) {
  448. BN_ULONG byte_xored = *s ^ xor;
  449. BN_ULONG byte = (byte_xored + carry) & 0xff;
  450. carry = byte_xored > byte; /* Implicit 1 or 0 */
  451. l |= (byte << m);
  452. }
  453. ret->d[i] = l;
  454. }
  455. /*
  456. * need to call this due to clear byte at top if avoiding having the top
  457. * bit set (-ve number)
  458. */
  459. bn_correct_top(ret);
  460. return ret;
  461. }
  462. BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
  463. {
  464. return bin2bn(s, len, ret, BIG, UNSIGNED);
  465. }
  466. BIGNUM *BN_signed_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
  467. {
  468. return bin2bn(s, len, ret, BIG, SIGNED);
  469. }
  470. static int bn2binpad(const BIGNUM *a, unsigned char *to, int tolen,
  471. endianness_t endianness, signedness_t signedness)
  472. {
  473. int inc;
  474. int n, n8;
  475. int xor = 0, carry = 0, ext = 0;
  476. size_t i, lasti, j, atop, mask;
  477. BN_ULONG l;
  478. /*
  479. * In case |a| is fixed-top, BN_num_bits can return bogus length,
  480. * but it's assumed that fixed-top inputs ought to be "nominated"
  481. * even for padded output, so it works out...
  482. */
  483. n8 = BN_num_bits(a);
  484. n = (n8 + 7) / 8; /* This is what BN_num_bytes() does */
  485. /* Take note of the signedness of the bignum */
  486. if (signedness == SIGNED) {
  487. xor = a->neg ? 0xff : 0x00;
  488. carry = a->neg;
  489. /*
  490. * if |n * 8 == n|, then the MSbit is set, otherwise unset.
  491. * We must compensate with one extra byte if that doesn't
  492. * correspond to the signedness of the bignum with regards
  493. * to 2's complement.
  494. */
  495. ext = (n * 8 == n8)
  496. ? !a->neg /* MSbit set on nonnegative bignum */
  497. : a->neg; /* MSbit unset on negative bignum */
  498. }
  499. if (tolen == -1) {
  500. tolen = n + ext;
  501. } else if (tolen < n + ext) { /* uncommon/unlike case */
  502. BIGNUM temp = *a;
  503. bn_correct_top(&temp);
  504. n8 = BN_num_bits(&temp);
  505. n = (n8 + 7) / 8; /* This is what BN_num_bytes() does */
  506. if (tolen < n + ext)
  507. return -1;
  508. }
  509. /* Swipe through whole available data and don't give away padded zero. */
  510. atop = a->dmax * BN_BYTES;
  511. if (atop == 0) {
  512. if (tolen != 0)
  513. memset(to, '\0', tolen);
  514. return tolen;
  515. }
  516. /*
  517. * The loop that does the work iterates from least significant
  518. * to most significant BIGNUM limb, so we adapt parameters to
  519. * transfer output bytes accordingly.
  520. */
  521. if (endianness == LITTLE) {
  522. inc = 1;
  523. } else {
  524. inc = -1;
  525. to += tolen - 1; /* Move to the last byte, not beyond */
  526. }
  527. lasti = atop - 1;
  528. atop = a->top * BN_BYTES;
  529. for (i = 0, j = 0; j < (size_t)tolen; j++) {
  530. unsigned char byte, byte_xored;
  531. l = a->d[i / BN_BYTES];
  532. mask = 0 - ((j - atop) >> (8 * sizeof(i) - 1));
  533. byte = (unsigned char)(l >> (8 * (i % BN_BYTES)) & mask);
  534. byte_xored = byte ^ xor;
  535. *to = (unsigned char)(byte_xored + carry);
  536. carry = byte_xored > *to; /* Implicit 1 or 0 */
  537. to += inc;
  538. i += (i - lasti) >> (8 * sizeof(i) - 1); /* stay on last limb */
  539. }
  540. return tolen;
  541. }
  542. int BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen)
  543. {
  544. if (tolen < 0)
  545. return -1;
  546. return bn2binpad(a, to, tolen, BIG, UNSIGNED);
  547. }
  548. int BN_signed_bn2bin(const BIGNUM *a, unsigned char *to, int tolen)
  549. {
  550. if (tolen < 0)
  551. return -1;
  552. return bn2binpad(a, to, tolen, BIG, SIGNED);
  553. }
  554. int BN_bn2bin(const BIGNUM *a, unsigned char *to)
  555. {
  556. return bn2binpad(a, to, -1, BIG, UNSIGNED);
  557. }
  558. BIGNUM *BN_lebin2bn(const unsigned char *s, int len, BIGNUM *ret)
  559. {
  560. return bin2bn(s, len, ret, LITTLE, UNSIGNED);
  561. }
  562. BIGNUM *BN_signed_lebin2bn(const unsigned char *s, int len, BIGNUM *ret)
  563. {
  564. return bin2bn(s, len, ret, LITTLE, SIGNED);
  565. }
  566. int BN_bn2lebinpad(const BIGNUM *a, unsigned char *to, int tolen)
  567. {
  568. if (tolen < 0)
  569. return -1;
  570. return bn2binpad(a, to, tolen, LITTLE, UNSIGNED);
  571. }
  572. int BN_signed_bn2lebin(const BIGNUM *a, unsigned char *to, int tolen)
  573. {
  574. if (tolen < 0)
  575. return -1;
  576. return bn2binpad(a, to, tolen, LITTLE, SIGNED);
  577. }
  578. BIGNUM *BN_native2bn(const unsigned char *s, int len, BIGNUM *ret)
  579. {
  580. DECLARE_IS_ENDIAN;
  581. if (IS_LITTLE_ENDIAN)
  582. return BN_lebin2bn(s, len, ret);
  583. return BN_bin2bn(s, len, ret);
  584. }
  585. BIGNUM *BN_signed_native2bn(const unsigned char *s, int len, BIGNUM *ret)
  586. {
  587. DECLARE_IS_ENDIAN;
  588. if (IS_LITTLE_ENDIAN)
  589. return BN_signed_lebin2bn(s, len, ret);
  590. return BN_signed_bin2bn(s, len, ret);
  591. }
  592. int BN_bn2nativepad(const BIGNUM *a, unsigned char *to, int tolen)
  593. {
  594. DECLARE_IS_ENDIAN;
  595. if (IS_LITTLE_ENDIAN)
  596. return BN_bn2lebinpad(a, to, tolen);
  597. return BN_bn2binpad(a, to, tolen);
  598. }
  599. int BN_signed_bn2native(const BIGNUM *a, unsigned char *to, int tolen)
  600. {
  601. DECLARE_IS_ENDIAN;
  602. if (IS_LITTLE_ENDIAN)
  603. return BN_signed_bn2lebin(a, to, tolen);
  604. return BN_signed_bn2bin(a, to, tolen);
  605. }
  606. int BN_ucmp(const BIGNUM *a, const BIGNUM *b)
  607. {
  608. int i;
  609. BN_ULONG t1, t2, *ap, *bp;
  610. ap = a->d;
  611. bp = b->d;
  612. if (BN_get_flags(a, BN_FLG_CONSTTIME)
  613. && a->top == b->top) {
  614. int res = 0;
  615. for (i = 0; i < b->top; i++) {
  616. res = constant_time_select_int(constant_time_lt_bn(ap[i], bp[i]),
  617. -1, res);
  618. res = constant_time_select_int(constant_time_lt_bn(bp[i], ap[i]),
  619. 1, res);
  620. }
  621. return res;
  622. }
  623. bn_check_top(a);
  624. bn_check_top(b);
  625. i = a->top - b->top;
  626. if (i != 0)
  627. return i;
  628. for (i = a->top - 1; i >= 0; i--) {
  629. t1 = ap[i];
  630. t2 = bp[i];
  631. if (t1 != t2)
  632. return ((t1 > t2) ? 1 : -1);
  633. }
  634. return 0;
  635. }
  636. int BN_cmp(const BIGNUM *a, const BIGNUM *b)
  637. {
  638. int i;
  639. int gt, lt;
  640. BN_ULONG t1, t2;
  641. if ((a == NULL) || (b == NULL)) {
  642. if (a != NULL)
  643. return -1;
  644. else if (b != NULL)
  645. return 1;
  646. else
  647. return 0;
  648. }
  649. bn_check_top(a);
  650. bn_check_top(b);
  651. if (a->neg != b->neg) {
  652. if (a->neg)
  653. return -1;
  654. else
  655. return 1;
  656. }
  657. if (a->neg == 0) {
  658. gt = 1;
  659. lt = -1;
  660. } else {
  661. gt = -1;
  662. lt = 1;
  663. }
  664. if (a->top > b->top)
  665. return gt;
  666. if (a->top < b->top)
  667. return lt;
  668. for (i = a->top - 1; i >= 0; i--) {
  669. t1 = a->d[i];
  670. t2 = b->d[i];
  671. if (t1 > t2)
  672. return gt;
  673. if (t1 < t2)
  674. return lt;
  675. }
  676. return 0;
  677. }
  678. int BN_set_bit(BIGNUM *a, int n)
  679. {
  680. int i, j, k;
  681. if (n < 0)
  682. return 0;
  683. i = n / BN_BITS2;
  684. j = n % BN_BITS2;
  685. if (a->top <= i) {
  686. if (bn_wexpand(a, i + 1) == NULL)
  687. return 0;
  688. for (k = a->top; k < i + 1; k++)
  689. a->d[k] = 0;
  690. a->top = i + 1;
  691. a->flags &= ~BN_FLG_FIXED_TOP;
  692. }
  693. a->d[i] |= (((BN_ULONG)1) << j);
  694. bn_check_top(a);
  695. return 1;
  696. }
  697. int BN_clear_bit(BIGNUM *a, int n)
  698. {
  699. int i, j;
  700. bn_check_top(a);
  701. if (n < 0)
  702. return 0;
  703. i = n / BN_BITS2;
  704. j = n % BN_BITS2;
  705. if (a->top <= i)
  706. return 0;
  707. a->d[i] &= (~(((BN_ULONG)1) << j));
  708. bn_correct_top(a);
  709. return 1;
  710. }
  711. int BN_is_bit_set(const BIGNUM *a, int n)
  712. {
  713. int i, j;
  714. bn_check_top(a);
  715. if (n < 0)
  716. return 0;
  717. i = n / BN_BITS2;
  718. j = n % BN_BITS2;
  719. if (a->top <= i)
  720. return 0;
  721. return (int)(((a->d[i]) >> j) & ((BN_ULONG)1));
  722. }
  723. int ossl_bn_mask_bits_fixed_top(BIGNUM *a, int n)
  724. {
  725. int b, w;
  726. if (n < 0)
  727. return 0;
  728. w = n / BN_BITS2;
  729. b = n % BN_BITS2;
  730. if (w >= a->top)
  731. return 0;
  732. if (b == 0)
  733. a->top = w;
  734. else {
  735. a->top = w + 1;
  736. a->d[w] &= ~(BN_MASK2 << b);
  737. }
  738. a->flags |= BN_FLG_FIXED_TOP;
  739. return 1;
  740. }
  741. int BN_mask_bits(BIGNUM *a, int n)
  742. {
  743. int ret;
  744. bn_check_top(a);
  745. ret = ossl_bn_mask_bits_fixed_top(a, n);
  746. if (ret)
  747. bn_correct_top(a);
  748. return ret;
  749. }
  750. void BN_set_negative(BIGNUM *a, int b)
  751. {
  752. if (b && !BN_is_zero(a))
  753. a->neg = 1;
  754. else
  755. a->neg = 0;
  756. }
  757. int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
  758. {
  759. int i;
  760. BN_ULONG aa, bb;
  761. if (n == 0)
  762. return 0;
  763. aa = a[n - 1];
  764. bb = b[n - 1];
  765. if (aa != bb)
  766. return ((aa > bb) ? 1 : -1);
  767. for (i = n - 2; i >= 0; i--) {
  768. aa = a[i];
  769. bb = b[i];
  770. if (aa != bb)
  771. return ((aa > bb) ? 1 : -1);
  772. }
  773. return 0;
  774. }
  775. /*
  776. * Here follows a specialised variants of bn_cmp_words(). It has the
  777. * capability of performing the operation on arrays of different sizes. The
  778. * sizes of those arrays is expressed through cl, which is the common length
  779. * ( basically, min(len(a),len(b)) ), and dl, which is the delta between the
  780. * two lengths, calculated as len(a)-len(b). All lengths are the number of
  781. * BN_ULONGs...
  782. */
  783. int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl)
  784. {
  785. int n, i;
  786. n = cl - 1;
  787. if (dl < 0) {
  788. for (i = dl; i < 0; i++) {
  789. if (b[n - i] != 0)
  790. return -1; /* a < b */
  791. }
  792. }
  793. if (dl > 0) {
  794. for (i = dl; i > 0; i--) {
  795. if (a[n + i] != 0)
  796. return 1; /* a > b */
  797. }
  798. }
  799. return bn_cmp_words(a, b, cl);
  800. }
  801. /*-
  802. * Constant-time conditional swap of a and b.
  803. * a and b are swapped if condition is not 0.
  804. * nwords is the number of words to swap.
  805. * Assumes that at least nwords are allocated in both a and b.
  806. * Assumes that no more than nwords are used by either a or b.
  807. */
  808. void BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords)
  809. {
  810. BN_ULONG t;
  811. int i;
  812. bn_wcheck_size(a, nwords);
  813. bn_wcheck_size(b, nwords);
  814. condition = ((~condition & ((condition - 1))) >> (BN_BITS2 - 1)) - 1;
  815. t = (a->top ^ b->top) & condition;
  816. a->top ^= t;
  817. b->top ^= t;
  818. t = (a->neg ^ b->neg) & condition;
  819. a->neg ^= t;
  820. b->neg ^= t;
  821. /*-
  822. * BN_FLG_STATIC_DATA: indicates that data may not be written to. Intention
  823. * is actually to treat it as it's read-only data, and some (if not most)
  824. * of it does reside in read-only segment. In other words observation of
  825. * BN_FLG_STATIC_DATA in BN_consttime_swap should be treated as fatal
  826. * condition. It would either cause SEGV or effectively cause data
  827. * corruption.
  828. *
  829. * BN_FLG_MALLOCED: refers to BN structure itself, and hence must be
  830. * preserved.
  831. *
  832. * BN_FLG_SECURE: must be preserved, because it determines how x->d was
  833. * allocated and hence how to free it.
  834. *
  835. * BN_FLG_CONSTTIME: sufficient to mask and swap
  836. *
  837. * BN_FLG_FIXED_TOP: indicates that we haven't called bn_correct_top() on
  838. * the data, so the d array may be padded with additional 0 values (i.e.
  839. * top could be greater than the minimal value that it could be). We should
  840. * be swapping it
  841. */
  842. #define BN_CONSTTIME_SWAP_FLAGS (BN_FLG_CONSTTIME | BN_FLG_FIXED_TOP)
  843. t = ((a->flags ^ b->flags) & BN_CONSTTIME_SWAP_FLAGS) & condition;
  844. a->flags ^= t;
  845. b->flags ^= t;
  846. /* conditionally swap the data */
  847. for (i = 0; i < nwords; i++) {
  848. t = (a->d[i] ^ b->d[i]) & condition;
  849. a->d[i] ^= t;
  850. b->d[i] ^= t;
  851. }
  852. }
  853. #undef BN_CONSTTIME_SWAP_FLAGS
  854. /* Bits of security, see SP800-57 */
  855. int BN_security_bits(int L, int N)
  856. {
  857. int secbits, bits;
  858. if (L >= 15360)
  859. secbits = 256;
  860. else if (L >= 7680)
  861. secbits = 192;
  862. else if (L >= 3072)
  863. secbits = 128;
  864. else if (L >= 2048)
  865. secbits = 112;
  866. else if (L >= 1024)
  867. secbits = 80;
  868. else
  869. return 0;
  870. if (N == -1)
  871. return secbits;
  872. bits = N / 2;
  873. if (bits < 80)
  874. return 0;
  875. return bits >= secbits ? secbits : bits;
  876. }
  877. void BN_zero_ex(BIGNUM *a)
  878. {
  879. a->neg = 0;
  880. a->top = 0;
  881. a->flags &= ~BN_FLG_FIXED_TOP;
  882. }
  883. int BN_abs_is_word(const BIGNUM *a, const BN_ULONG w)
  884. {
  885. return ((a->top == 1) && (a->d[0] == w)) || ((w == 0) && (a->top == 0));
  886. }
  887. int BN_is_zero(const BIGNUM *a)
  888. {
  889. return a->top == 0;
  890. }
  891. int BN_is_one(const BIGNUM *a)
  892. {
  893. return BN_abs_is_word(a, 1) && !a->neg;
  894. }
  895. int BN_is_word(const BIGNUM *a, const BN_ULONG w)
  896. {
  897. return BN_abs_is_word(a, w) && (!w || !a->neg);
  898. }
  899. int ossl_bn_is_word_fixed_top(const BIGNUM *a, const BN_ULONG w)
  900. {
  901. int res, i;
  902. const BN_ULONG *ap = a->d;
  903. if (a->neg || a->top == 0)
  904. return 0;
  905. res = constant_time_select_int(constant_time_eq_bn(ap[0], w), 1, 0);
  906. for (i = 1; i < a->top; i++)
  907. res = constant_time_select_int(constant_time_is_zero_bn(ap[i]),
  908. res, 0);
  909. return res;
  910. }
  911. int BN_is_odd(const BIGNUM *a)
  912. {
  913. return (a->top > 0) && (a->d[0] & 1);
  914. }
  915. int BN_is_negative(const BIGNUM *a)
  916. {
  917. return (a->neg != 0);
  918. }
  919. int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,
  920. BN_CTX *ctx)
  921. {
  922. return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);
  923. }
  924. void BN_with_flags(BIGNUM *dest, const BIGNUM *b, int flags)
  925. {
  926. dest->d = b->d;
  927. dest->top = b->top;
  928. dest->dmax = b->dmax;
  929. dest->neg = b->neg;
  930. dest->flags = ((dest->flags & BN_FLG_MALLOCED)
  931. | (b->flags & ~BN_FLG_MALLOCED)
  932. | BN_FLG_STATIC_DATA | flags);
  933. }
  934. BN_GENCB *BN_GENCB_new(void)
  935. {
  936. BN_GENCB *ret;
  937. if ((ret = OPENSSL_malloc(sizeof(*ret))) == NULL)
  938. return NULL;
  939. return ret;
  940. }
  941. void BN_GENCB_free(BN_GENCB *cb)
  942. {
  943. if (cb == NULL)
  944. return;
  945. OPENSSL_free(cb);
  946. }
  947. void BN_set_flags(BIGNUM *b, int n)
  948. {
  949. b->flags |= n;
  950. }
  951. int BN_get_flags(const BIGNUM *b, int n)
  952. {
  953. return b->flags & n;
  954. }
  955. /* Populate a BN_GENCB structure with an "old"-style callback */
  956. void BN_GENCB_set_old(BN_GENCB *gencb, void (*callback) (int, int, void *),
  957. void *cb_arg)
  958. {
  959. BN_GENCB *tmp_gencb = gencb;
  960. tmp_gencb->ver = 1;
  961. tmp_gencb->arg = cb_arg;
  962. tmp_gencb->cb.cb_1 = callback;
  963. }
  964. /* Populate a BN_GENCB structure with a "new"-style callback */
  965. void BN_GENCB_set(BN_GENCB *gencb, int (*callback) (int, int, BN_GENCB *),
  966. void *cb_arg)
  967. {
  968. BN_GENCB *tmp_gencb = gencb;
  969. tmp_gencb->ver = 2;
  970. tmp_gencb->arg = cb_arg;
  971. tmp_gencb->cb.cb_2 = callback;
  972. }
  973. void *BN_GENCB_get_arg(BN_GENCB *cb)
  974. {
  975. return cb->arg;
  976. }
  977. BIGNUM *bn_wexpand(BIGNUM *a, int words)
  978. {
  979. return (words <= a->dmax) ? a : bn_expand2(a, words);
  980. }
  981. void bn_correct_top_consttime(BIGNUM *a)
  982. {
  983. int j, atop;
  984. BN_ULONG limb;
  985. unsigned int mask;
  986. for (j = 0, atop = 0; j < a->dmax; j++) {
  987. limb = a->d[j];
  988. limb |= 0 - limb;
  989. limb >>= BN_BITS2 - 1;
  990. limb = 0 - limb;
  991. mask = (unsigned int)limb;
  992. mask &= constant_time_msb(j - a->top);
  993. atop = constant_time_select_int(mask, j + 1, atop);
  994. }
  995. mask = constant_time_eq_int(atop, 0);
  996. a->top = atop;
  997. a->neg = constant_time_select_int(mask, 0, a->neg);
  998. a->flags &= ~BN_FLG_FIXED_TOP;
  999. }
  1000. void bn_correct_top(BIGNUM *a)
  1001. {
  1002. BN_ULONG *ftl;
  1003. int tmp_top = a->top;
  1004. if (tmp_top > 0) {
  1005. for (ftl = &(a->d[tmp_top]); tmp_top > 0; tmp_top--) {
  1006. ftl--;
  1007. if (*ftl != 0)
  1008. break;
  1009. }
  1010. a->top = tmp_top;
  1011. }
  1012. if (a->top == 0)
  1013. a->neg = 0;
  1014. a->flags &= ~BN_FLG_FIXED_TOP;
  1015. bn_pollute(a);
  1016. }