bn_lib.c 23 KB

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