bn_lib.c 23 KB

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