bn_lib.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. /* crypto/bn/bn_lib.c */
  2. /* Copyright (C) 1995-1998 Eric Young ([email protected])
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young ([email protected]).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson ([email protected]).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young ([email protected])"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson ([email protected])"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #ifndef BN_DEBUG
  59. # undef NDEBUG /* avoid conflicting definitions */
  60. # define NDEBUG
  61. #endif
  62. #include <assert.h>
  63. #include <limits.h>
  64. #include <stdio.h>
  65. #include "cryptlib.h"
  66. #include "bn_lcl.h"
  67. #include "constant_time_locl.h"
  68. const char BN_version[] = "Big Number" OPENSSL_VERSION_PTEXT;
  69. /* This stuff appears to be completely unused, so is deprecated */
  70. #ifndef OPENSSL_NO_DEPRECATED
  71. /*-
  72. * For a 32 bit machine
  73. * 2 - 4 == 128
  74. * 3 - 8 == 256
  75. * 4 - 16 == 512
  76. * 5 - 32 == 1024
  77. * 6 - 64 == 2048
  78. * 7 - 128 == 4096
  79. * 8 - 256 == 8192
  80. */
  81. static int bn_limit_bits = 0;
  82. static int bn_limit_num = 8; /* (1<<bn_limit_bits) */
  83. static int bn_limit_bits_low = 0;
  84. static int bn_limit_num_low = 8; /* (1<<bn_limit_bits_low) */
  85. static int bn_limit_bits_high = 0;
  86. static int bn_limit_num_high = 8; /* (1<<bn_limit_bits_high) */
  87. static int bn_limit_bits_mont = 0;
  88. static int bn_limit_num_mont = 8; /* (1<<bn_limit_bits_mont) */
  89. void BN_set_params(int mult, int high, int low, int mont)
  90. {
  91. if (mult >= 0) {
  92. if (mult > (int)(sizeof(int) * 8) - 1)
  93. mult = sizeof(int) * 8 - 1;
  94. bn_limit_bits = mult;
  95. bn_limit_num = 1 << mult;
  96. }
  97. if (high >= 0) {
  98. if (high > (int)(sizeof(int) * 8) - 1)
  99. high = sizeof(int) * 8 - 1;
  100. bn_limit_bits_high = high;
  101. bn_limit_num_high = 1 << high;
  102. }
  103. if (low >= 0) {
  104. if (low > (int)(sizeof(int) * 8) - 1)
  105. low = sizeof(int) * 8 - 1;
  106. bn_limit_bits_low = low;
  107. bn_limit_num_low = 1 << low;
  108. }
  109. if (mont >= 0) {
  110. if (mont > (int)(sizeof(int) * 8) - 1)
  111. mont = sizeof(int) * 8 - 1;
  112. bn_limit_bits_mont = mont;
  113. bn_limit_num_mont = 1 << mont;
  114. }
  115. }
  116. int BN_get_params(int which)
  117. {
  118. if (which == 0)
  119. return (bn_limit_bits);
  120. else if (which == 1)
  121. return (bn_limit_bits_high);
  122. else if (which == 2)
  123. return (bn_limit_bits_low);
  124. else if (which == 3)
  125. return (bn_limit_bits_mont);
  126. else
  127. return (0);
  128. }
  129. #endif
  130. const BIGNUM *BN_value_one(void)
  131. {
  132. static const BN_ULONG data_one = 1L;
  133. static const BIGNUM const_one =
  134. { (BN_ULONG *)&data_one, 1, 1, 0, BN_FLG_STATIC_DATA };
  135. return (&const_one);
  136. }
  137. int BN_num_bits_word(BN_ULONG l)
  138. {
  139. BN_ULONG x, mask;
  140. int bits = (l != 0);
  141. #if BN_BITS2 > 32
  142. x = l >> 32;
  143. mask = (0 - x) & BN_MASK2;
  144. mask = (0 - (mask >> (BN_BITS2 - 1)));
  145. bits += 32 & mask;
  146. l ^= (x ^ l) & mask;
  147. #endif
  148. x = l >> 16;
  149. mask = (0 - x) & BN_MASK2;
  150. mask = (0 - (mask >> (BN_BITS2 - 1)));
  151. bits += 16 & mask;
  152. l ^= (x ^ l) & mask;
  153. x = l >> 8;
  154. mask = (0 - x) & BN_MASK2;
  155. mask = (0 - (mask >> (BN_BITS2 - 1)));
  156. bits += 8 & mask;
  157. l ^= (x ^ l) & mask;
  158. x = l >> 4;
  159. mask = (0 - x) & BN_MASK2;
  160. mask = (0 - (mask >> (BN_BITS2 - 1)));
  161. bits += 4 & mask;
  162. l ^= (x ^ l) & mask;
  163. x = l >> 2;
  164. mask = (0 - x) & BN_MASK2;
  165. mask = (0 - (mask >> (BN_BITS2 - 1)));
  166. bits += 2 & mask;
  167. l ^= (x ^ l) & mask;
  168. x = l >> 1;
  169. mask = (0 - x) & BN_MASK2;
  170. mask = (0 - (mask >> (BN_BITS2 - 1)));
  171. bits += 1 & mask;
  172. return bits;
  173. }
  174. /*
  175. * This function still leaks `a->dmax`: it's caller's responsibility to
  176. * expand the input `a` in advance to a public length.
  177. */
  178. static inline
  179. int bn_num_bits_consttime(const BIGNUM *a)
  180. {
  181. int j, ret;
  182. unsigned int mask, past_i;
  183. int i = a->top - 1;
  184. bn_check_top(a);
  185. for (j = 0, past_i = 0, ret = 0; j < a->dmax; j++) {
  186. mask = constant_time_eq_int(i, j); /* 0xff..ff if i==j, 0x0 otherwise */
  187. ret += BN_BITS2 & (~mask & ~past_i);
  188. ret += BN_num_bits_word(a->d[j]) & mask;
  189. past_i |= mask; /* past_i will become 0xff..ff after i==j */
  190. }
  191. /*
  192. * if BN_is_zero(a) => i is -1 and ret contains garbage, so we mask the
  193. * final result.
  194. */
  195. mask = ~(constant_time_eq_int(i, ((int)-1)));
  196. return ret & mask;
  197. }
  198. int BN_num_bits(const BIGNUM *a)
  199. {
  200. int i = a->top - 1;
  201. bn_check_top(a);
  202. if (a->flags & BN_FLG_CONSTTIME) {
  203. /*
  204. * We assume that BIGNUMs flagged as CONSTTIME have also been expanded
  205. * so that a->dmax is not leaking secret information.
  206. *
  207. * In other words, it's the caller's responsibility to ensure `a` has
  208. * been preallocated in advance to a public length if we hit this
  209. * branch.
  210. *
  211. */
  212. return bn_num_bits_consttime(a);
  213. }
  214. if (BN_is_zero(a))
  215. return 0;
  216. return ((i * BN_BITS2) + BN_num_bits_word(a->d[i]));
  217. }
  218. void BN_clear_free(BIGNUM *a)
  219. {
  220. int i;
  221. if (a == NULL)
  222. return;
  223. bn_check_top(a);
  224. if (a->d != NULL) {
  225. OPENSSL_cleanse(a->d, a->dmax * sizeof(a->d[0]));
  226. if (!(BN_get_flags(a, BN_FLG_STATIC_DATA)))
  227. OPENSSL_free(a->d);
  228. }
  229. i = BN_get_flags(a, BN_FLG_MALLOCED);
  230. OPENSSL_cleanse(a, sizeof(BIGNUM));
  231. if (i)
  232. OPENSSL_free(a);
  233. }
  234. void BN_free(BIGNUM *a)
  235. {
  236. if (a == NULL)
  237. return;
  238. bn_check_top(a);
  239. if ((a->d != NULL) && !(BN_get_flags(a, BN_FLG_STATIC_DATA)))
  240. OPENSSL_free(a->d);
  241. if (a->flags & BN_FLG_MALLOCED)
  242. OPENSSL_free(a);
  243. else {
  244. #ifndef OPENSSL_NO_DEPRECATED
  245. a->flags |= BN_FLG_FREE;
  246. #endif
  247. a->d = NULL;
  248. }
  249. }
  250. void BN_init(BIGNUM *a)
  251. {
  252. memset(a, 0, sizeof(BIGNUM));
  253. bn_check_top(a);
  254. }
  255. BIGNUM *BN_new(void)
  256. {
  257. BIGNUM *ret;
  258. if ((ret = (BIGNUM *)OPENSSL_malloc(sizeof(BIGNUM))) == NULL) {
  259. BNerr(BN_F_BN_NEW, ERR_R_MALLOC_FAILURE);
  260. return (NULL);
  261. }
  262. ret->flags = BN_FLG_MALLOCED;
  263. ret->top = 0;
  264. ret->neg = 0;
  265. ret->dmax = 0;
  266. ret->d = NULL;
  267. bn_check_top(ret);
  268. return (ret);
  269. }
  270. /* This is used both by bn_expand2() and bn_dup_expand() */
  271. /* The caller MUST check that words > b->dmax before calling this */
  272. static BN_ULONG *bn_expand_internal(const BIGNUM *b, int words)
  273. {
  274. BN_ULONG *A, *a = NULL;
  275. const BN_ULONG *B;
  276. int i;
  277. if (words > (INT_MAX / (4 * BN_BITS2))) {
  278. BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_BIGNUM_TOO_LONG);
  279. return NULL;
  280. }
  281. if (BN_get_flags(b, BN_FLG_STATIC_DATA)) {
  282. BNerr(BN_F_BN_EXPAND_INTERNAL, BN_R_EXPAND_ON_STATIC_BIGNUM_DATA);
  283. return (NULL);
  284. }
  285. a = A = (BN_ULONG *)OPENSSL_malloc(sizeof(BN_ULONG) * words);
  286. if (A == NULL) {
  287. BNerr(BN_F_BN_EXPAND_INTERNAL, ERR_R_MALLOC_FAILURE);
  288. return (NULL);
  289. }
  290. #ifdef PURIFY
  291. /*
  292. * Valgrind complains in BN_consttime_swap because we process the whole
  293. * array even if it's not initialised yet. This doesn't matter in that
  294. * function - what's important is constant time operation (we're not
  295. * actually going to use the data)
  296. */
  297. memset(a, 0, sizeof(BN_ULONG) * words);
  298. #endif
  299. #if 1
  300. B = b->d;
  301. /* Check if the previous number needs to be copied */
  302. if (B != NULL) {
  303. for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {
  304. /*
  305. * The fact that the loop is unrolled
  306. * 4-wise is a tribute to Intel. It's
  307. * the one that doesn't have enough
  308. * registers to accomodate more data.
  309. * I'd unroll it 8-wise otherwise:-)
  310. *
  311. * <[email protected]>
  312. */
  313. BN_ULONG a0, a1, a2, a3;
  314. a0 = B[0];
  315. a1 = B[1];
  316. a2 = B[2];
  317. a3 = B[3];
  318. A[0] = a0;
  319. A[1] = a1;
  320. A[2] = a2;
  321. A[3] = a3;
  322. }
  323. /*
  324. * workaround for ultrix cc: without 'case 0', the optimizer does
  325. * the switch table by doing a=top&3; a--; goto jump_table[a];
  326. * which fails for top== 0
  327. */
  328. switch (b->top & 3) {
  329. case 3:
  330. A[2] = B[2];
  331. case 2:
  332. A[1] = B[1];
  333. case 1:
  334. A[0] = B[0];
  335. case 0:
  336. ;
  337. }
  338. }
  339. #else
  340. memset(A, 0, sizeof(BN_ULONG) * words);
  341. memcpy(A, b->d, sizeof(b->d[0]) * b->top);
  342. #endif
  343. return (a);
  344. }
  345. /*
  346. * This is an internal function that can be used instead of bn_expand2() when
  347. * there is a need to copy BIGNUMs instead of only expanding the data part,
  348. * while still expanding them. Especially useful when needing to expand
  349. * BIGNUMs that are declared 'const' and should therefore not be changed. The
  350. * reason to use this instead of a BN_dup() followed by a bn_expand2() is
  351. * memory allocation overhead. A BN_dup() followed by a bn_expand2() will
  352. * allocate new memory for the BIGNUM data twice, and free it once, while
  353. * bn_dup_expand() makes sure allocation is made only once.
  354. */
  355. #ifndef OPENSSL_NO_DEPRECATED
  356. BIGNUM *bn_dup_expand(const BIGNUM *b, int words)
  357. {
  358. BIGNUM *r = NULL;
  359. bn_check_top(b);
  360. /*
  361. * This function does not work if words <= b->dmax && top < words because
  362. * BN_dup() does not preserve 'dmax'! (But bn_dup_expand() is not used
  363. * anywhere yet.)
  364. */
  365. if (words > b->dmax) {
  366. BN_ULONG *a = bn_expand_internal(b, words);
  367. if (a) {
  368. r = BN_new();
  369. if (r) {
  370. r->top = b->top;
  371. r->dmax = words;
  372. r->neg = b->neg;
  373. r->d = a;
  374. } else {
  375. /* r == NULL, BN_new failure */
  376. OPENSSL_free(a);
  377. }
  378. }
  379. /*
  380. * If a == NULL, there was an error in allocation in
  381. * bn_expand_internal(), and NULL should be returned
  382. */
  383. } else {
  384. r = BN_dup(b);
  385. }
  386. bn_check_top(r);
  387. return r;
  388. }
  389. #endif
  390. /*
  391. * This is an internal function that should not be used in applications. It
  392. * ensures that 'b' has enough room for a 'words' word number and initialises
  393. * any unused part of b->d with leading zeros. It is mostly used by the
  394. * various BIGNUM routines. If there is an error, NULL is returned. If not,
  395. * 'b' is returned.
  396. */
  397. BIGNUM *bn_expand2(BIGNUM *b, int words)
  398. {
  399. if (words > b->dmax) {
  400. BN_ULONG *a = bn_expand_internal(b, words);
  401. if (!a)
  402. return NULL;
  403. if (b->d)
  404. OPENSSL_free(b->d);
  405. b->d = a;
  406. b->dmax = words;
  407. }
  408. /* None of this should be necessary because of what b->top means! */
  409. #if 0
  410. /*
  411. * NB: bn_wexpand() calls this only if the BIGNUM really has to grow
  412. */
  413. if (b->top < b->dmax) {
  414. int i;
  415. BN_ULONG *A = &(b->d[b->top]);
  416. for (i = (b->dmax - b->top) >> 3; i > 0; i--, A += 8) {
  417. A[0] = 0;
  418. A[1] = 0;
  419. A[2] = 0;
  420. A[3] = 0;
  421. A[4] = 0;
  422. A[5] = 0;
  423. A[6] = 0;
  424. A[7] = 0;
  425. }
  426. for (i = (b->dmax - b->top) & 7; i > 0; i--, A++)
  427. A[0] = 0;
  428. assert(A == &(b->d[b->dmax]));
  429. }
  430. #endif
  431. return b;
  432. }
  433. BIGNUM *BN_dup(const BIGNUM *a)
  434. {
  435. BIGNUM *t;
  436. if (a == NULL)
  437. return NULL;
  438. bn_check_top(a);
  439. t = BN_new();
  440. if (t == NULL)
  441. return NULL;
  442. if (!BN_copy(t, a)) {
  443. BN_free(t);
  444. return NULL;
  445. }
  446. bn_check_top(t);
  447. return t;
  448. }
  449. BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
  450. {
  451. int i;
  452. BN_ULONG *A;
  453. const BN_ULONG *B;
  454. bn_check_top(b);
  455. if (a == b)
  456. return (a);
  457. if (bn_wexpand(a, b->top) == NULL)
  458. return (NULL);
  459. #if 1
  460. A = a->d;
  461. B = b->d;
  462. for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {
  463. BN_ULONG a0, a1, a2, a3;
  464. a0 = B[0];
  465. a1 = B[1];
  466. a2 = B[2];
  467. a3 = B[3];
  468. A[0] = a0;
  469. A[1] = a1;
  470. A[2] = a2;
  471. A[3] = a3;
  472. }
  473. /* ultrix cc workaround, see comments in bn_expand_internal */
  474. switch (b->top & 3) {
  475. case 3:
  476. A[2] = B[2];
  477. case 2:
  478. A[1] = B[1];
  479. case 1:
  480. A[0] = B[0];
  481. case 0:;
  482. }
  483. #else
  484. memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);
  485. #endif
  486. a->neg = b->neg;
  487. a->top = b->top;
  488. a->flags |= b->flags & BN_FLG_FIXED_TOP;
  489. bn_check_top(a);
  490. return (a);
  491. }
  492. #define FLAGS_DATA(flags) ((flags) & (BN_FLG_STATIC_DATA \
  493. | BN_FLG_CONSTTIME \
  494. | BN_FLG_FIXED_TOP))
  495. #define FLAGS_STRUCT(flags) ((flags) & (BN_FLG_MALLOCED))
  496. void BN_swap(BIGNUM *a, BIGNUM *b)
  497. {
  498. int flags_old_a, flags_old_b;
  499. BN_ULONG *tmp_d;
  500. int tmp_top, tmp_dmax, tmp_neg;
  501. bn_check_top(a);
  502. bn_check_top(b);
  503. flags_old_a = a->flags;
  504. flags_old_b = b->flags;
  505. tmp_d = a->d;
  506. tmp_top = a->top;
  507. tmp_dmax = a->dmax;
  508. tmp_neg = a->neg;
  509. a->d = b->d;
  510. a->top = b->top;
  511. a->dmax = b->dmax;
  512. a->neg = b->neg;
  513. b->d = tmp_d;
  514. b->top = tmp_top;
  515. b->dmax = tmp_dmax;
  516. b->neg = tmp_neg;
  517. a->flags = FLAGS_STRUCT(flags_old_a) | FLAGS_DATA(flags_old_b);
  518. b->flags = FLAGS_STRUCT(flags_old_b) | FLAGS_DATA(flags_old_a);
  519. bn_check_top(a);
  520. bn_check_top(b);
  521. }
  522. void BN_clear(BIGNUM *a)
  523. {
  524. bn_check_top(a);
  525. if (a->d != NULL)
  526. OPENSSL_cleanse(a->d, a->dmax * sizeof(a->d[0]));
  527. a->top = 0;
  528. a->neg = 0;
  529. a->flags &= ~BN_FLG_FIXED_TOP;
  530. }
  531. BN_ULONG BN_get_word(const BIGNUM *a)
  532. {
  533. if (a->top > 1)
  534. return BN_MASK2;
  535. else if (a->top == 1)
  536. return a->d[0];
  537. /* a->top == 0 */
  538. return 0;
  539. }
  540. int BN_set_word(BIGNUM *a, BN_ULONG w)
  541. {
  542. bn_check_top(a);
  543. if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
  544. return (0);
  545. a->neg = 0;
  546. a->d[0] = w;
  547. a->top = (w ? 1 : 0);
  548. a->flags &= ~BN_FLG_FIXED_TOP;
  549. bn_check_top(a);
  550. return (1);
  551. }
  552. BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
  553. {
  554. unsigned int i, m;
  555. unsigned int n;
  556. BN_ULONG l;
  557. BIGNUM *bn = NULL;
  558. if (ret == NULL)
  559. ret = bn = BN_new();
  560. if (ret == NULL)
  561. return (NULL);
  562. bn_check_top(ret);
  563. l = 0;
  564. n = len;
  565. if (n == 0) {
  566. ret->top = 0;
  567. return (ret);
  568. }
  569. i = ((n - 1) / BN_BYTES) + 1;
  570. m = ((n - 1) % (BN_BYTES));
  571. if (bn_wexpand(ret, (int)i) == NULL) {
  572. if (bn)
  573. BN_free(bn);
  574. return NULL;
  575. }
  576. ret->top = i;
  577. ret->neg = 0;
  578. while (n--) {
  579. l = (l << 8L) | *(s++);
  580. if (m-- == 0) {
  581. ret->d[--i] = l;
  582. l = 0;
  583. m = BN_BYTES - 1;
  584. }
  585. }
  586. /*
  587. * need to call this due to clear byte at top if avoiding having the top
  588. * bit set (-ve number)
  589. */
  590. bn_correct_top(ret);
  591. return (ret);
  592. }
  593. typedef enum {big, little} endianess_t;
  594. /* ignore negative */
  595. static
  596. int bn2binpad(const BIGNUM *a, unsigned char *to, int tolen, endianess_t endianess)
  597. {
  598. int n;
  599. size_t i, lasti, j, atop, mask;
  600. BN_ULONG l;
  601. /*
  602. * In case |a| is fixed-top, BN_num_bytes can return bogus length,
  603. * but it's assumed that fixed-top inputs ought to be "nominated"
  604. * even for padded output, so it works out...
  605. */
  606. n = BN_num_bytes(a);
  607. if (tolen == -1) {
  608. tolen = n;
  609. } else if (tolen < n) { /* uncommon/unlike case */
  610. BIGNUM temp = *a;
  611. bn_correct_top(&temp);
  612. n = BN_num_bytes(&temp);
  613. if (tolen < n)
  614. return -1;
  615. }
  616. /* Swipe through whole available data and don't give away padded zero. */
  617. atop = a->dmax * BN_BYTES;
  618. if (atop == 0) {
  619. OPENSSL_cleanse(to, tolen);
  620. return tolen;
  621. }
  622. lasti = atop - 1;
  623. atop = a->top * BN_BYTES;
  624. if (endianess == big)
  625. to += tolen; /* start from the end of the buffer */
  626. for (i = 0, j = 0; j < (size_t)tolen; j++) {
  627. unsigned char val;
  628. l = a->d[i / BN_BYTES];
  629. mask = 0 - ((j - atop) >> (8 * sizeof(i) - 1));
  630. val = (unsigned char)(l >> (8 * (i % BN_BYTES)) & mask);
  631. if (endianess == big)
  632. *--to = val;
  633. else
  634. *to++ = val;
  635. i += (i - lasti) >> (8 * sizeof(i) - 1); /* stay on last limb */
  636. }
  637. return tolen;
  638. }
  639. int bn_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen)
  640. {
  641. if (tolen < 0)
  642. return -1;
  643. return bn2binpad(a, to, tolen, big);
  644. }
  645. int BN_bn2bin(const BIGNUM *a, unsigned char *to)
  646. {
  647. return bn2binpad(a, to, -1, big);
  648. }
  649. BIGNUM *bn_lebin2bn(const unsigned char *s, int len, BIGNUM *ret)
  650. {
  651. unsigned int i, m;
  652. unsigned int n;
  653. BN_ULONG l;
  654. BIGNUM *bn = NULL;
  655. if (ret == NULL)
  656. ret = bn = BN_new();
  657. if (ret == NULL)
  658. return NULL;
  659. bn_check_top(ret);
  660. s += len;
  661. /* Skip trailing zeroes. */
  662. for ( ; len > 0 && s[-1] == 0; s--, len--)
  663. continue;
  664. n = len;
  665. if (n == 0) {
  666. ret->top = 0;
  667. return ret;
  668. }
  669. i = ((n - 1) / BN_BYTES) + 1;
  670. m = ((n - 1) % (BN_BYTES));
  671. if (bn_wexpand(ret, (int)i) == NULL) {
  672. BN_free(bn);
  673. return NULL;
  674. }
  675. ret->top = i;
  676. ret->neg = 0;
  677. l = 0;
  678. while (n--) {
  679. s--;
  680. l = (l << 8L) | *s;
  681. if (m-- == 0) {
  682. ret->d[--i] = l;
  683. l = 0;
  684. m = BN_BYTES - 1;
  685. }
  686. }
  687. /*
  688. * need to call this due to clear byte at top if avoiding having the top
  689. * bit set (-ve number)
  690. */
  691. bn_correct_top(ret);
  692. return ret;
  693. }
  694. int bn_bn2lebinpad(const BIGNUM *a, unsigned char *to, int tolen)
  695. {
  696. if (tolen < 0)
  697. return -1;
  698. return bn2binpad(a, to, tolen, little);
  699. }
  700. int BN_ucmp(const BIGNUM *a, const BIGNUM *b)
  701. {
  702. int i;
  703. BN_ULONG t1, t2, *ap, *bp;
  704. bn_check_top(a);
  705. bn_check_top(b);
  706. i = a->top - b->top;
  707. if (i != 0)
  708. return (i);
  709. ap = a->d;
  710. bp = b->d;
  711. for (i = a->top - 1; i >= 0; i--) {
  712. t1 = ap[i];
  713. t2 = bp[i];
  714. if (t1 != t2)
  715. return ((t1 > t2) ? 1 : -1);
  716. }
  717. return (0);
  718. }
  719. int BN_cmp(const BIGNUM *a, const BIGNUM *b)
  720. {
  721. int i;
  722. int gt, lt;
  723. BN_ULONG t1, t2;
  724. if ((a == NULL) || (b == NULL)) {
  725. if (a != NULL)
  726. return (-1);
  727. else if (b != NULL)
  728. return (1);
  729. else
  730. return (0);
  731. }
  732. bn_check_top(a);
  733. bn_check_top(b);
  734. if (a->neg != b->neg) {
  735. if (a->neg)
  736. return (-1);
  737. else
  738. return (1);
  739. }
  740. if (a->neg == 0) {
  741. gt = 1;
  742. lt = -1;
  743. } else {
  744. gt = -1;
  745. lt = 1;
  746. }
  747. if (a->top > b->top)
  748. return (gt);
  749. if (a->top < b->top)
  750. return (lt);
  751. for (i = a->top - 1; i >= 0; i--) {
  752. t1 = a->d[i];
  753. t2 = b->d[i];
  754. if (t1 > t2)
  755. return (gt);
  756. if (t1 < t2)
  757. return (lt);
  758. }
  759. return (0);
  760. }
  761. int BN_set_bit(BIGNUM *a, int n)
  762. {
  763. int i, j, k;
  764. if (n < 0)
  765. return 0;
  766. i = n / BN_BITS2;
  767. j = n % BN_BITS2;
  768. if (a->top <= i) {
  769. if (bn_wexpand(a, i + 1) == NULL)
  770. return (0);
  771. for (k = a->top; k < i + 1; k++)
  772. a->d[k] = 0;
  773. a->top = i + 1;
  774. a->flags &= ~BN_FLG_FIXED_TOP;
  775. }
  776. a->d[i] |= (((BN_ULONG)1) << j);
  777. bn_check_top(a);
  778. return (1);
  779. }
  780. int BN_clear_bit(BIGNUM *a, int n)
  781. {
  782. int i, j;
  783. bn_check_top(a);
  784. if (n < 0)
  785. return 0;
  786. i = n / BN_BITS2;
  787. j = n % BN_BITS2;
  788. if (a->top <= i)
  789. return (0);
  790. a->d[i] &= (~(((BN_ULONG)1) << j));
  791. bn_correct_top(a);
  792. return (1);
  793. }
  794. int BN_is_bit_set(const BIGNUM *a, int n)
  795. {
  796. int i, j;
  797. bn_check_top(a);
  798. if (n < 0)
  799. return 0;
  800. i = n / BN_BITS2;
  801. j = n % BN_BITS2;
  802. if (a->top <= i)
  803. return 0;
  804. return (int)(((a->d[i]) >> j) & ((BN_ULONG)1));
  805. }
  806. int BN_mask_bits(BIGNUM *a, int n)
  807. {
  808. int b, w;
  809. bn_check_top(a);
  810. if (n < 0)
  811. return 0;
  812. w = n / BN_BITS2;
  813. b = n % BN_BITS2;
  814. if (w >= a->top)
  815. return 0;
  816. if (b == 0)
  817. a->top = w;
  818. else {
  819. a->top = w + 1;
  820. a->d[w] &= ~(BN_MASK2 << b);
  821. }
  822. bn_correct_top(a);
  823. return (1);
  824. }
  825. void BN_set_negative(BIGNUM *a, int b)
  826. {
  827. if (b && !BN_is_zero(a))
  828. a->neg = 1;
  829. else
  830. a->neg = 0;
  831. }
  832. int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
  833. {
  834. int i;
  835. BN_ULONG aa, bb;
  836. if (n == 0)
  837. return 0;
  838. aa = a[n - 1];
  839. bb = b[n - 1];
  840. if (aa != bb)
  841. return ((aa > bb) ? 1 : -1);
  842. for (i = n - 2; i >= 0; i--) {
  843. aa = a[i];
  844. bb = b[i];
  845. if (aa != bb)
  846. return ((aa > bb) ? 1 : -1);
  847. }
  848. return (0);
  849. }
  850. /*
  851. * Here follows a specialised variants of bn_cmp_words(). It has the
  852. * property of performing the operation on arrays of different sizes. The
  853. * sizes of those arrays is expressed through cl, which is the common length
  854. * ( basicall, min(len(a),len(b)) ), and dl, which is the delta between the
  855. * two lengths, calculated as len(a)-len(b). All lengths are the number of
  856. * BN_ULONGs...
  857. */
  858. int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl)
  859. {
  860. int n, i;
  861. n = cl - 1;
  862. if (dl < 0) {
  863. for (i = dl; i < 0; i++) {
  864. if (b[n - i] != 0)
  865. return -1; /* a < b */
  866. }
  867. }
  868. if (dl > 0) {
  869. for (i = dl; i > 0; i--) {
  870. if (a[n + i] != 0)
  871. return 1; /* a > b */
  872. }
  873. }
  874. return bn_cmp_words(a, b, cl);
  875. }
  876. /*
  877. * Constant-time conditional swap of a and b.
  878. * a and b are swapped if condition is not 0. The code assumes that at most one bit of condition is set.
  879. * nwords is the number of words to swap. The code assumes that at least nwords are allocated in both a and b,
  880. * and that no more than nwords are used by either a or b.
  881. * a and b cannot be the same number
  882. */
  883. void BN_consttime_swap(BN_ULONG condition, BIGNUM *a, BIGNUM *b, int nwords)
  884. {
  885. BN_ULONG t;
  886. int i;
  887. bn_wcheck_size(a, nwords);
  888. bn_wcheck_size(b, nwords);
  889. assert(a != b);
  890. assert((condition & (condition - 1)) == 0);
  891. assert(sizeof(BN_ULONG) >= sizeof(int));
  892. condition = ((condition - 1) >> (BN_BITS2 - 1)) - 1;
  893. t = (a->top ^ b->top) & condition;
  894. a->top ^= t;
  895. b->top ^= t;
  896. t = (a->neg ^ b->neg) & condition;
  897. a->neg ^= t;
  898. b->neg ^= t;
  899. /*-
  900. * BN_FLG_STATIC_DATA: indicates that data may not be written to. Intention
  901. * is actually to treat it as it's read-only data, and some (if not most)
  902. * of it does reside in read-only segment. In other words observation of
  903. * BN_FLG_STATIC_DATA in BN_consttime_swap should be treated as fatal
  904. * condition. It would either cause SEGV or effectively cause data
  905. * corruption.
  906. *
  907. * BN_FLG_MALLOCED: refers to BN structure itself, and hence must be
  908. * preserved.
  909. *
  910. * BN_FLG_SECURE: must be preserved, because it determines how x->d was
  911. * allocated and hence how to free it.
  912. *
  913. * BN_FLG_CONSTTIME: sufficient to mask and swap
  914. *
  915. * BN_FLG_FIXED_TOP: indicates that we haven't called bn_correct_top() on
  916. * the data, so the d array may be padded with additional 0 values (i.e.
  917. * top could be greater than the minimal value that it could be). We should
  918. * be swapping it
  919. */
  920. #define BN_CONSTTIME_SWAP_FLAGS (BN_FLG_CONSTTIME | BN_FLG_FIXED_TOP)
  921. t = ((a->flags ^ b->flags) & BN_CONSTTIME_SWAP_FLAGS) & condition;
  922. a->flags ^= t;
  923. b->flags ^= t;
  924. #define BN_CONSTTIME_SWAP(ind) \
  925. do { \
  926. t = (a->d[ind] ^ b->d[ind]) & condition; \
  927. a->d[ind] ^= t; \
  928. b->d[ind] ^= t; \
  929. } while (0)
  930. switch (nwords) {
  931. default:
  932. for (i = 10; i < nwords; i++)
  933. BN_CONSTTIME_SWAP(i);
  934. /* Fallthrough */
  935. case 10:
  936. BN_CONSTTIME_SWAP(9); /* Fallthrough */
  937. case 9:
  938. BN_CONSTTIME_SWAP(8); /* Fallthrough */
  939. case 8:
  940. BN_CONSTTIME_SWAP(7); /* Fallthrough */
  941. case 7:
  942. BN_CONSTTIME_SWAP(6); /* Fallthrough */
  943. case 6:
  944. BN_CONSTTIME_SWAP(5); /* Fallthrough */
  945. case 5:
  946. BN_CONSTTIME_SWAP(4); /* Fallthrough */
  947. case 4:
  948. BN_CONSTTIME_SWAP(3); /* Fallthrough */
  949. case 3:
  950. BN_CONSTTIME_SWAP(2); /* Fallthrough */
  951. case 2:
  952. BN_CONSTTIME_SWAP(1); /* Fallthrough */
  953. case 1:
  954. BN_CONSTTIME_SWAP(0);
  955. }
  956. #undef BN_CONSTTIME_SWAP
  957. }