bn_lib.c 23 KB

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