sshbn.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. /*
  2. * Bignum routines for RSA and DH and stuff.
  3. */
  4. #include <stdio.h>
  5. #include <assert.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include "misc.h"
  9. #if defined __GNUC__ && defined __i386__
  10. typedef unsigned long BignumInt;
  11. typedef unsigned long long BignumDblInt;
  12. #define BIGNUM_INT_MASK 0xFFFFFFFFUL
  13. #define BIGNUM_TOP_BIT 0x80000000UL
  14. #define BIGNUM_INT_BITS 32
  15. #define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2)
  16. #define DIVMOD_WORD(q, r, hi, lo, w) \
  17. __asm__("div %2" : \
  18. "=d" (r), "=a" (q) : \
  19. "r" (w), "d" (hi), "a" (lo))
  20. #else
  21. typedef unsigned short BignumInt;
  22. typedef unsigned long BignumDblInt;
  23. #define BIGNUM_INT_MASK 0xFFFFU
  24. #define BIGNUM_TOP_BIT 0x8000U
  25. #define BIGNUM_INT_BITS 16
  26. #define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2)
  27. #define DIVMOD_WORD(q, r, hi, lo, w) do { \
  28. BignumDblInt n = (((BignumDblInt)hi) << BIGNUM_INT_BITS) | lo; \
  29. q = n / w; \
  30. r = n % w; \
  31. } while (0)
  32. #endif
  33. #define BIGNUM_INT_BYTES (BIGNUM_INT_BITS / 8)
  34. #define BIGNUM_INTERNAL
  35. typedef BignumInt *Bignum;
  36. #include "ssh.h"
  37. BignumInt bnZero[1] = { 0 };
  38. BignumInt bnOne[2] = { 1, 1 };
  39. /*
  40. * The Bignum format is an array of `BignumInt'. The first
  41. * element of the array counts the remaining elements. The
  42. * remaining elements express the actual number, base 2^BIGNUM_INT_BITS, _least_
  43. * significant digit first. (So it's trivial to extract the bit
  44. * with value 2^n for any n.)
  45. *
  46. * All Bignums in this module are positive. Negative numbers must
  47. * be dealt with outside it.
  48. *
  49. * INVARIANT: the most significant word of any Bignum must be
  50. * nonzero.
  51. */
  52. Bignum Zero = bnZero, One = bnOne;
  53. static Bignum newbn(int length)
  54. {
  55. Bignum b = snewn(length + 1, BignumInt);
  56. if (!b)
  57. abort(); /* FIXME */
  58. memset(b, 0, (length + 1) * sizeof(*b));
  59. b[0] = length;
  60. return b;
  61. }
  62. void bn_restore_invariant(Bignum b)
  63. {
  64. while (b[0] > 1 && b[b[0]] == 0)
  65. b[0]--;
  66. }
  67. Bignum copybn(Bignum orig)
  68. {
  69. Bignum b = snewn(orig[0] + 1, BignumInt);
  70. if (!b)
  71. abort(); /* FIXME */
  72. memcpy(b, orig, (orig[0] + 1) * sizeof(*b));
  73. return b;
  74. }
  75. void freebn(Bignum b)
  76. {
  77. /*
  78. * Burn the evidence, just in case.
  79. */
  80. memset(b, 0, sizeof(b[0]) * (b[0] + 1));
  81. sfree(b);
  82. }
  83. Bignum bn_power_2(int n)
  84. {
  85. Bignum ret = newbn(n / BIGNUM_INT_BITS + 1);
  86. bignum_set_bit(ret, n, 1);
  87. return ret;
  88. }
  89. /*
  90. * Compute c = a * b.
  91. * Input is in the first len words of a and b.
  92. * Result is returned in the first 2*len words of c.
  93. */
  94. static void internal_mul(BignumInt *a, BignumInt *b,
  95. BignumInt *c, int len)
  96. {
  97. int i, j;
  98. BignumDblInt t;
  99. for (j = 0; j < 2 * len; j++)
  100. c[j] = 0;
  101. for (i = len - 1; i >= 0; i--) {
  102. t = 0;
  103. for (j = len - 1; j >= 0; j--) {
  104. t += MUL_WORD(a[i], (BignumDblInt) b[j]);
  105. t += (BignumDblInt) c[i + j + 1];
  106. c[i + j + 1] = (BignumInt) t;
  107. t = t >> BIGNUM_INT_BITS;
  108. }
  109. c[i] = (BignumInt) t;
  110. }
  111. }
  112. static void internal_add_shifted(BignumInt *number,
  113. unsigned n, int shift)
  114. {
  115. int word = 1 + (shift / BIGNUM_INT_BITS);
  116. int bshift = shift % BIGNUM_INT_BITS;
  117. BignumDblInt addend;
  118. addend = (BignumDblInt)n << bshift;
  119. while (addend) {
  120. addend += number[word];
  121. number[word] = (BignumInt) addend & BIGNUM_INT_MASK;
  122. addend >>= BIGNUM_INT_BITS;
  123. word++;
  124. }
  125. }
  126. /*
  127. * Compute a = a % m.
  128. * Input in first alen words of a and first mlen words of m.
  129. * Output in first alen words of a
  130. * (of which first alen-mlen words will be zero).
  131. * The MSW of m MUST have its high bit set.
  132. * Quotient is accumulated in the `quotient' array, which is a Bignum
  133. * rather than the internal bigendian format. Quotient parts are shifted
  134. * left by `qshift' before adding into quot.
  135. */
  136. static void internal_mod(BignumInt *a, int alen,
  137. BignumInt *m, int mlen,
  138. BignumInt *quot, int qshift)
  139. {
  140. BignumInt m0, m1;
  141. unsigned int h;
  142. int i, k;
  143. m0 = m[0];
  144. if (mlen > 1)
  145. m1 = m[1];
  146. else
  147. m1 = 0;
  148. for (i = 0; i <= alen - mlen; i++) {
  149. BignumDblInt t;
  150. unsigned int q, r, c, ai1;
  151. if (i == 0) {
  152. h = 0;
  153. } else {
  154. h = a[i - 1];
  155. a[i - 1] = 0;
  156. }
  157. if (i == alen - 1)
  158. ai1 = 0;
  159. else
  160. ai1 = a[i + 1];
  161. /* Find q = h:a[i] / m0 */
  162. DIVMOD_WORD(q, r, h, a[i], m0);
  163. /* Refine our estimate of q by looking at
  164. h:a[i]:a[i+1] / m0:m1 */
  165. t = MUL_WORD(m1, q);
  166. if (t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) {
  167. q--;
  168. t -= m1;
  169. r = (r + m0) & BIGNUM_INT_MASK; /* overflow? */
  170. if (r >= (BignumDblInt) m0 &&
  171. t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) q--;
  172. }
  173. /* Subtract q * m from a[i...] */
  174. c = 0;
  175. for (k = mlen - 1; k >= 0; k--) {
  176. t = MUL_WORD(q, m[k]);
  177. t += c;
  178. c = t >> BIGNUM_INT_BITS;
  179. if ((BignumInt) t > a[i + k])
  180. c++;
  181. a[i + k] -= (BignumInt) t;
  182. }
  183. /* Add back m in case of borrow */
  184. if (c != h) {
  185. t = 0;
  186. for (k = mlen - 1; k >= 0; k--) {
  187. t += m[k];
  188. t += a[i + k];
  189. a[i + k] = (BignumInt) t;
  190. t = t >> BIGNUM_INT_BITS;
  191. }
  192. q--;
  193. }
  194. if (quot)
  195. internal_add_shifted(quot, q, qshift + BIGNUM_INT_BITS * (alen - mlen - i));
  196. }
  197. }
  198. /*
  199. * Compute (base ^ exp) % mod.
  200. */
  201. Bignum modpow(Bignum base_in, Bignum exp, Bignum mod)
  202. {
  203. BignumInt *a, *b, *n, *m;
  204. int mshift;
  205. int mlen, i, j;
  206. Bignum base, result;
  207. /*
  208. * The most significant word of mod needs to be non-zero. It
  209. * should already be, but let's make sure.
  210. */
  211. assert(mod[mod[0]] != 0);
  212. /*
  213. * Make sure the base is smaller than the modulus, by reducing
  214. * it modulo the modulus if not.
  215. */
  216. base = bigmod(base_in, mod);
  217. /* Allocate m of size mlen, copy mod to m */
  218. /* We use big endian internally */
  219. mlen = mod[0];
  220. m = snewn(mlen, BignumInt);
  221. for (j = 0; j < mlen; j++)
  222. m[j] = mod[mod[0] - j];
  223. /* Shift m left to make msb bit set */
  224. for (mshift = 0; mshift < BIGNUM_INT_BITS-1; mshift++)
  225. if ((m[0] << mshift) & BIGNUM_TOP_BIT)
  226. break;
  227. if (mshift) {
  228. for (i = 0; i < mlen - 1; i++)
  229. m[i] = (m[i] << mshift) | (m[i + 1] >> (BIGNUM_INT_BITS - mshift));
  230. m[mlen - 1] = m[mlen - 1] << mshift;
  231. }
  232. /* Allocate n of size mlen, copy base to n */
  233. n = snewn(mlen, BignumInt);
  234. i = mlen - base[0];
  235. for (j = 0; j < i; j++)
  236. n[j] = 0;
  237. for (j = 0; j < base[0]; j++)
  238. n[i + j] = base[base[0] - j];
  239. /* Allocate a and b of size 2*mlen. Set a = 1 */
  240. a = snewn(2 * mlen, BignumInt);
  241. b = snewn(2 * mlen, BignumInt);
  242. for (i = 0; i < 2 * mlen; i++)
  243. a[i] = 0;
  244. a[2 * mlen - 1] = 1;
  245. /* Skip leading zero bits of exp. */
  246. i = 0;
  247. j = BIGNUM_INT_BITS-1;
  248. while (i < exp[0] && (exp[exp[0] - i] & (1 << j)) == 0) {
  249. j--;
  250. if (j < 0) {
  251. i++;
  252. j = BIGNUM_INT_BITS-1;
  253. }
  254. }
  255. /* Main computation */
  256. while (i < exp[0]) {
  257. while (j >= 0) {
  258. internal_mul(a + mlen, a + mlen, b, mlen);
  259. internal_mod(b, mlen * 2, m, mlen, NULL, 0);
  260. if ((exp[exp[0] - i] & (1 << j)) != 0) {
  261. internal_mul(b + mlen, n, a, mlen);
  262. internal_mod(a, mlen * 2, m, mlen, NULL, 0);
  263. } else {
  264. BignumInt *t;
  265. t = a;
  266. a = b;
  267. b = t;
  268. }
  269. j--;
  270. }
  271. i++;
  272. j = BIGNUM_INT_BITS-1;
  273. }
  274. /* Fixup result in case the modulus was shifted */
  275. if (mshift) {
  276. for (i = mlen - 1; i < 2 * mlen - 1; i++)
  277. a[i] = (a[i] << mshift) | (a[i + 1] >> (BIGNUM_INT_BITS - mshift));
  278. a[2 * mlen - 1] = a[2 * mlen - 1] << mshift;
  279. internal_mod(a, mlen * 2, m, mlen, NULL, 0);
  280. for (i = 2 * mlen - 1; i >= mlen; i--)
  281. a[i] = (a[i] >> mshift) | (a[i - 1] << (BIGNUM_INT_BITS - mshift));
  282. }
  283. /* Copy result to buffer */
  284. result = newbn(mod[0]);
  285. for (i = 0; i < mlen; i++)
  286. result[result[0] - i] = a[i + mlen];
  287. while (result[0] > 1 && result[result[0]] == 0)
  288. result[0]--;
  289. /* Free temporary arrays */
  290. for (i = 0; i < 2 * mlen; i++)
  291. a[i] = 0;
  292. sfree(a);
  293. for (i = 0; i < 2 * mlen; i++)
  294. b[i] = 0;
  295. sfree(b);
  296. for (i = 0; i < mlen; i++)
  297. m[i] = 0;
  298. sfree(m);
  299. for (i = 0; i < mlen; i++)
  300. n[i] = 0;
  301. sfree(n);
  302. freebn(base);
  303. return result;
  304. }
  305. /*
  306. * Compute (p * q) % mod.
  307. * The most significant word of mod MUST be non-zero.
  308. * We assume that the result array is the same size as the mod array.
  309. */
  310. Bignum modmul(Bignum p, Bignum q, Bignum mod)
  311. {
  312. BignumInt *a, *n, *m, *o;
  313. int mshift;
  314. int pqlen, mlen, rlen, i, j;
  315. Bignum result;
  316. /* Allocate m of size mlen, copy mod to m */
  317. /* We use big endian internally */
  318. mlen = mod[0];
  319. m = snewn(mlen, BignumInt);
  320. for (j = 0; j < mlen; j++)
  321. m[j] = mod[mod[0] - j];
  322. /* Shift m left to make msb bit set */
  323. for (mshift = 0; mshift < BIGNUM_INT_BITS-1; mshift++)
  324. if ((m[0] << mshift) & BIGNUM_TOP_BIT)
  325. break;
  326. if (mshift) {
  327. for (i = 0; i < mlen - 1; i++)
  328. m[i] = (m[i] << mshift) | (m[i + 1] >> (BIGNUM_INT_BITS - mshift));
  329. m[mlen - 1] = m[mlen - 1] << mshift;
  330. }
  331. pqlen = (p[0] > q[0] ? p[0] : q[0]);
  332. /* Allocate n of size pqlen, copy p to n */
  333. n = snewn(pqlen, BignumInt);
  334. i = pqlen - p[0];
  335. for (j = 0; j < i; j++)
  336. n[j] = 0;
  337. for (j = 0; j < p[0]; j++)
  338. n[i + j] = p[p[0] - j];
  339. /* Allocate o of size pqlen, copy q to o */
  340. o = snewn(pqlen, BignumInt);
  341. i = pqlen - q[0];
  342. for (j = 0; j < i; j++)
  343. o[j] = 0;
  344. for (j = 0; j < q[0]; j++)
  345. o[i + j] = q[q[0] - j];
  346. /* Allocate a of size 2*pqlen for result */
  347. a = snewn(2 * pqlen, BignumInt);
  348. /* Main computation */
  349. internal_mul(n, o, a, pqlen);
  350. internal_mod(a, pqlen * 2, m, mlen, NULL, 0);
  351. /* Fixup result in case the modulus was shifted */
  352. if (mshift) {
  353. for (i = 2 * pqlen - mlen - 1; i < 2 * pqlen - 1; i++)
  354. a[i] = (a[i] << mshift) | (a[i + 1] >> (BIGNUM_INT_BITS - mshift));
  355. a[2 * pqlen - 1] = a[2 * pqlen - 1] << mshift;
  356. internal_mod(a, pqlen * 2, m, mlen, NULL, 0);
  357. for (i = 2 * pqlen - 1; i >= 2 * pqlen - mlen; i--)
  358. a[i] = (a[i] >> mshift) | (a[i - 1] << (BIGNUM_INT_BITS - mshift));
  359. }
  360. /* Copy result to buffer */
  361. rlen = (mlen < pqlen * 2 ? mlen : pqlen * 2);
  362. result = newbn(rlen);
  363. for (i = 0; i < rlen; i++)
  364. result[result[0] - i] = a[i + 2 * pqlen - rlen];
  365. while (result[0] > 1 && result[result[0]] == 0)
  366. result[0]--;
  367. /* Free temporary arrays */
  368. for (i = 0; i < 2 * pqlen; i++)
  369. a[i] = 0;
  370. sfree(a);
  371. for (i = 0; i < mlen; i++)
  372. m[i] = 0;
  373. sfree(m);
  374. for (i = 0; i < pqlen; i++)
  375. n[i] = 0;
  376. sfree(n);
  377. for (i = 0; i < pqlen; i++)
  378. o[i] = 0;
  379. sfree(o);
  380. return result;
  381. }
  382. /*
  383. * Compute p % mod.
  384. * The most significant word of mod MUST be non-zero.
  385. * We assume that the result array is the same size as the mod array.
  386. * We optionally write out a quotient if `quotient' is non-NULL.
  387. * We can avoid writing out the result if `result' is NULL.
  388. */
  389. static void bigdivmod(Bignum p, Bignum mod, Bignum result, Bignum quotient)
  390. {
  391. BignumInt *n, *m;
  392. int mshift;
  393. int plen, mlen, i, j;
  394. /* Allocate m of size mlen, copy mod to m */
  395. /* We use big endian internally */
  396. mlen = mod[0];
  397. m = snewn(mlen, BignumInt);
  398. for (j = 0; j < mlen; j++)
  399. m[j] = mod[mod[0] - j];
  400. /* Shift m left to make msb bit set */
  401. for (mshift = 0; mshift < BIGNUM_INT_BITS-1; mshift++)
  402. if ((m[0] << mshift) & BIGNUM_TOP_BIT)
  403. break;
  404. if (mshift) {
  405. for (i = 0; i < mlen - 1; i++)
  406. m[i] = (m[i] << mshift) | (m[i + 1] >> (BIGNUM_INT_BITS - mshift));
  407. m[mlen - 1] = m[mlen - 1] << mshift;
  408. }
  409. plen = p[0];
  410. /* Ensure plen > mlen */
  411. if (plen <= mlen)
  412. plen = mlen + 1;
  413. /* Allocate n of size plen, copy p to n */
  414. n = snewn(plen, BignumInt);
  415. for (j = 0; j < plen; j++)
  416. n[j] = 0;
  417. for (j = 1; j <= p[0]; j++)
  418. n[plen - j] = p[j];
  419. /* Main computation */
  420. internal_mod(n, plen, m, mlen, quotient, mshift);
  421. /* Fixup result in case the modulus was shifted */
  422. if (mshift) {
  423. for (i = plen - mlen - 1; i < plen - 1; i++)
  424. n[i] = (n[i] << mshift) | (n[i + 1] >> (BIGNUM_INT_BITS - mshift));
  425. n[plen - 1] = n[plen - 1] << mshift;
  426. internal_mod(n, plen, m, mlen, quotient, 0);
  427. for (i = plen - 1; i >= plen - mlen; i--)
  428. n[i] = (n[i] >> mshift) | (n[i - 1] << (BIGNUM_INT_BITS - mshift));
  429. }
  430. /* Copy result to buffer */
  431. if (result) {
  432. for (i = 1; i <= result[0]; i++) {
  433. int j = plen - i;
  434. result[i] = j >= 0 ? n[j] : 0;
  435. }
  436. }
  437. /* Free temporary arrays */
  438. for (i = 0; i < mlen; i++)
  439. m[i] = 0;
  440. sfree(m);
  441. for (i = 0; i < plen; i++)
  442. n[i] = 0;
  443. sfree(n);
  444. }
  445. /*
  446. * Decrement a number.
  447. */
  448. void decbn(Bignum bn)
  449. {
  450. int i = 1;
  451. while (i < bn[0] && bn[i] == 0)
  452. bn[i++] = BIGNUM_INT_MASK;
  453. bn[i]--;
  454. }
  455. Bignum bignum_from_bytes(const unsigned char *data, int nbytes)
  456. {
  457. Bignum result;
  458. int w, i;
  459. w = (nbytes + BIGNUM_INT_BYTES - 1) / BIGNUM_INT_BYTES; /* bytes->words */
  460. result = newbn(w);
  461. for (i = 1; i <= w; i++)
  462. result[i] = 0;
  463. for (i = nbytes; i--;) {
  464. unsigned char byte = *data++;
  465. result[1 + i / BIGNUM_INT_BYTES] |= byte << (8*i % BIGNUM_INT_BITS);
  466. }
  467. while (result[0] > 1 && result[result[0]] == 0)
  468. result[0]--;
  469. return result;
  470. }
  471. /*
  472. * Read an ssh1-format bignum from a data buffer. Return the number
  473. * of bytes consumed, or -1 if there wasn't enough data.
  474. */
  475. int ssh1_read_bignum(const unsigned char *data, int len, Bignum * result)
  476. {
  477. const unsigned char *p = data;
  478. int i;
  479. int w, b;
  480. if (len < 2)
  481. return -1;
  482. w = 0;
  483. for (i = 0; i < 2; i++)
  484. w = (w << 8) + *p++;
  485. b = (w + 7) / 8; /* bits -> bytes */
  486. if (len < b+2)
  487. return -1;
  488. if (!result) /* just return length */
  489. return b + 2;
  490. *result = bignum_from_bytes(p, b);
  491. return p + b - data;
  492. }
  493. /*
  494. * Return the bit count of a bignum, for ssh1 encoding.
  495. */
  496. int bignum_bitcount(Bignum bn)
  497. {
  498. int bitcount = bn[0] * BIGNUM_INT_BITS - 1;
  499. while (bitcount >= 0
  500. && (bn[bitcount / BIGNUM_INT_BITS + 1] >> (bitcount % BIGNUM_INT_BITS)) == 0) bitcount--;
  501. return bitcount + 1;
  502. }
  503. /*
  504. * Return the byte length of a bignum when ssh1 encoded.
  505. */
  506. int ssh1_bignum_length(Bignum bn)
  507. {
  508. return 2 + (bignum_bitcount(bn) + 7) / 8;
  509. }
  510. /*
  511. * Return the byte length of a bignum when ssh2 encoded.
  512. */
  513. int ssh2_bignum_length(Bignum bn)
  514. {
  515. return 4 + (bignum_bitcount(bn) + 8) / 8;
  516. }
  517. /*
  518. * Return a byte from a bignum; 0 is least significant, etc.
  519. */
  520. int bignum_byte(Bignum bn, int i)
  521. {
  522. if (i >= BIGNUM_INT_BYTES * bn[0])
  523. return 0; /* beyond the end */
  524. else
  525. return (bn[i / BIGNUM_INT_BYTES + 1] >>
  526. ((i % BIGNUM_INT_BYTES)*8)) & 0xFF;
  527. }
  528. /*
  529. * Return a bit from a bignum; 0 is least significant, etc.
  530. */
  531. int bignum_bit(Bignum bn, int i)
  532. {
  533. if (i >= BIGNUM_INT_BITS * bn[0])
  534. return 0; /* beyond the end */
  535. else
  536. return (bn[i / BIGNUM_INT_BITS + 1] >> (i % BIGNUM_INT_BITS)) & 1;
  537. }
  538. /*
  539. * Set a bit in a bignum; 0 is least significant, etc.
  540. */
  541. void bignum_set_bit(Bignum bn, int bitnum, int value)
  542. {
  543. if (bitnum >= BIGNUM_INT_BITS * bn[0])
  544. abort(); /* beyond the end */
  545. else {
  546. int v = bitnum / BIGNUM_INT_BITS + 1;
  547. int mask = 1 << (bitnum % BIGNUM_INT_BITS);
  548. if (value)
  549. bn[v] |= mask;
  550. else
  551. bn[v] &= ~mask;
  552. }
  553. }
  554. /*
  555. * Write a ssh1-format bignum into a buffer. It is assumed the
  556. * buffer is big enough. Returns the number of bytes used.
  557. */
  558. int ssh1_write_bignum(void *data, Bignum bn)
  559. {
  560. unsigned char *p = data;
  561. int len = ssh1_bignum_length(bn);
  562. int i;
  563. int bitc = bignum_bitcount(bn);
  564. *p++ = (bitc >> 8) & 0xFF;
  565. *p++ = (bitc) & 0xFF;
  566. for (i = len - 2; i--;)
  567. *p++ = bignum_byte(bn, i);
  568. return len;
  569. }
  570. /*
  571. * Compare two bignums. Returns like strcmp.
  572. */
  573. int bignum_cmp(Bignum a, Bignum b)
  574. {
  575. int amax = a[0], bmax = b[0];
  576. int i = (amax > bmax ? amax : bmax);
  577. while (i) {
  578. BignumInt aval = (i > amax ? 0 : a[i]);
  579. BignumInt bval = (i > bmax ? 0 : b[i]);
  580. if (aval < bval)
  581. return -1;
  582. if (aval > bval)
  583. return +1;
  584. i--;
  585. }
  586. return 0;
  587. }
  588. /*
  589. * Right-shift one bignum to form another.
  590. */
  591. Bignum bignum_rshift(Bignum a, int shift)
  592. {
  593. Bignum ret;
  594. int i, shiftw, shiftb, shiftbb, bits;
  595. BignumInt ai, ai1;
  596. bits = bignum_bitcount(a) - shift;
  597. ret = newbn((bits + BIGNUM_INT_BITS - 1) / BIGNUM_INT_BITS);
  598. if (ret) {
  599. shiftw = shift / BIGNUM_INT_BITS;
  600. shiftb = shift % BIGNUM_INT_BITS;
  601. shiftbb = BIGNUM_INT_BITS - shiftb;
  602. ai1 = a[shiftw + 1];
  603. for (i = 1; i <= ret[0]; i++) {
  604. ai = ai1;
  605. ai1 = (i + shiftw + 1 <= a[0] ? a[i + shiftw + 1] : 0);
  606. ret[i] = ((ai >> shiftb) | (ai1 << shiftbb)) & BIGNUM_INT_MASK;
  607. }
  608. }
  609. return ret;
  610. }
  611. /*
  612. * Non-modular multiplication and addition.
  613. */
  614. Bignum bigmuladd(Bignum a, Bignum b, Bignum addend)
  615. {
  616. int alen = a[0], blen = b[0];
  617. int mlen = (alen > blen ? alen : blen);
  618. int rlen, i, maxspot;
  619. BignumInt *workspace;
  620. Bignum ret;
  621. /* mlen space for a, mlen space for b, 2*mlen for result */
  622. workspace = snewn(mlen * 4, BignumInt);
  623. for (i = 0; i < mlen; i++) {
  624. workspace[0 * mlen + i] = (mlen - i <= a[0] ? a[mlen - i] : 0);
  625. workspace[1 * mlen + i] = (mlen - i <= b[0] ? b[mlen - i] : 0);
  626. }
  627. internal_mul(workspace + 0 * mlen, workspace + 1 * mlen,
  628. workspace + 2 * mlen, mlen);
  629. /* now just copy the result back */
  630. rlen = alen + blen + 1;
  631. if (addend && rlen <= addend[0])
  632. rlen = addend[0] + 1;
  633. ret = newbn(rlen);
  634. maxspot = 0;
  635. for (i = 1; i <= ret[0]; i++) {
  636. ret[i] = (i <= 2 * mlen ? workspace[4 * mlen - i] : 0);
  637. if (ret[i] != 0)
  638. maxspot = i;
  639. }
  640. ret[0] = maxspot;
  641. /* now add in the addend, if any */
  642. if (addend) {
  643. BignumDblInt carry = 0;
  644. for (i = 1; i <= rlen; i++) {
  645. carry += (i <= ret[0] ? ret[i] : 0);
  646. carry += (i <= addend[0] ? addend[i] : 0);
  647. ret[i] = (BignumInt) carry & BIGNUM_INT_MASK;
  648. carry >>= BIGNUM_INT_BITS;
  649. if (ret[i] != 0 && i > maxspot)
  650. maxspot = i;
  651. }
  652. }
  653. ret[0] = maxspot;
  654. sfree(workspace);
  655. return ret;
  656. }
  657. /*
  658. * Non-modular multiplication.
  659. */
  660. Bignum bigmul(Bignum a, Bignum b)
  661. {
  662. return bigmuladd(a, b, NULL);
  663. }
  664. /*
  665. * Create a bignum which is the bitmask covering another one. That
  666. * is, the smallest integer which is >= N and is also one less than
  667. * a power of two.
  668. */
  669. Bignum bignum_bitmask(Bignum n)
  670. {
  671. Bignum ret = copybn(n);
  672. int i;
  673. BignumInt j;
  674. i = ret[0];
  675. while (n[i] == 0 && i > 0)
  676. i--;
  677. if (i <= 0)
  678. return ret; /* input was zero */
  679. j = 1;
  680. while (j < n[i])
  681. j = 2 * j + 1;
  682. ret[i] = j;
  683. while (--i > 0)
  684. ret[i] = BIGNUM_INT_MASK;
  685. return ret;
  686. }
  687. /*
  688. * Convert a (max 32-bit) long into a bignum.
  689. */
  690. Bignum bignum_from_long(unsigned long nn)
  691. {
  692. Bignum ret;
  693. BignumDblInt n = nn;
  694. ret = newbn(3);
  695. ret[1] = (BignumInt)(n & BIGNUM_INT_MASK);
  696. ret[2] = (BignumInt)((n >> BIGNUM_INT_BITS) & BIGNUM_INT_MASK);
  697. ret[3] = 0;
  698. ret[0] = (ret[2] ? 2 : 1);
  699. return ret;
  700. }
  701. /*
  702. * Add a long to a bignum.
  703. */
  704. Bignum bignum_add_long(Bignum number, unsigned long addendx)
  705. {
  706. Bignum ret = newbn(number[0] + 1);
  707. int i, maxspot = 0;
  708. BignumDblInt carry = 0, addend = addendx;
  709. for (i = 1; i <= ret[0]; i++) {
  710. carry += addend & BIGNUM_INT_MASK;
  711. carry += (i <= number[0] ? number[i] : 0);
  712. addend >>= BIGNUM_INT_BITS;
  713. ret[i] = (BignumInt) carry & BIGNUM_INT_MASK;
  714. carry >>= BIGNUM_INT_BITS;
  715. if (ret[i] != 0)
  716. maxspot = i;
  717. }
  718. ret[0] = maxspot;
  719. return ret;
  720. }
  721. /*
  722. * Compute the residue of a bignum, modulo a (max 16-bit) short.
  723. */
  724. unsigned short bignum_mod_short(Bignum number, unsigned short modulus)
  725. {
  726. BignumDblInt mod, r;
  727. int i;
  728. r = 0;
  729. mod = modulus;
  730. for (i = number[0]; i > 0; i--)
  731. r = (r * (BIGNUM_TOP_BIT % mod) * 2 + number[i] % mod) % mod;
  732. return (unsigned short) r;
  733. }
  734. #ifdef DEBUG
  735. void diagbn(char *prefix, Bignum md)
  736. {
  737. int i, nibbles, morenibbles;
  738. static const char hex[] = "0123456789ABCDEF";
  739. debug(("%s0x", prefix ? prefix : ""));
  740. nibbles = (3 + bignum_bitcount(md)) / 4;
  741. if (nibbles < 1)
  742. nibbles = 1;
  743. morenibbles = 4 * md[0] - nibbles;
  744. for (i = 0; i < morenibbles; i++)
  745. debug(("-"));
  746. for (i = nibbles; i--;)
  747. debug(("%c",
  748. hex[(bignum_byte(md, i / 2) >> (4 * (i % 2))) & 0xF]));
  749. if (prefix)
  750. debug(("\n"));
  751. }
  752. #endif
  753. /*
  754. * Simple division.
  755. */
  756. Bignum bigdiv(Bignum a, Bignum b)
  757. {
  758. Bignum q = newbn(a[0]);
  759. bigdivmod(a, b, NULL, q);
  760. return q;
  761. }
  762. /*
  763. * Simple remainder.
  764. */
  765. Bignum bigmod(Bignum a, Bignum b)
  766. {
  767. Bignum r = newbn(b[0]);
  768. bigdivmod(a, b, r, NULL);
  769. return r;
  770. }
  771. /*
  772. * Greatest common divisor.
  773. */
  774. Bignum biggcd(Bignum av, Bignum bv)
  775. {
  776. Bignum a = copybn(av);
  777. Bignum b = copybn(bv);
  778. while (bignum_cmp(b, Zero) != 0) {
  779. Bignum t = newbn(b[0]);
  780. bigdivmod(a, b, t, NULL);
  781. while (t[0] > 1 && t[t[0]] == 0)
  782. t[0]--;
  783. freebn(a);
  784. a = b;
  785. b = t;
  786. }
  787. freebn(b);
  788. return a;
  789. }
  790. /*
  791. * Modular inverse, using Euclid's extended algorithm.
  792. */
  793. Bignum modinv(Bignum number, Bignum modulus)
  794. {
  795. Bignum a = copybn(modulus);
  796. Bignum b = copybn(number);
  797. Bignum xp = copybn(Zero);
  798. Bignum x = copybn(One);
  799. int sign = +1;
  800. while (bignum_cmp(b, One) != 0) {
  801. Bignum t = newbn(b[0]);
  802. Bignum q = newbn(a[0]);
  803. bigdivmod(a, b, t, q);
  804. while (t[0] > 1 && t[t[0]] == 0)
  805. t[0]--;
  806. freebn(a);
  807. a = b;
  808. b = t;
  809. t = xp;
  810. xp = x;
  811. x = bigmuladd(q, xp, t);
  812. sign = -sign;
  813. freebn(t);
  814. freebn(q);
  815. }
  816. freebn(b);
  817. freebn(a);
  818. freebn(xp);
  819. /* now we know that sign * x == 1, and that x < modulus */
  820. if (sign < 0) {
  821. /* set a new x to be modulus - x */
  822. Bignum newx = newbn(modulus[0]);
  823. BignumInt carry = 0;
  824. int maxspot = 1;
  825. int i;
  826. for (i = 1; i <= newx[0]; i++) {
  827. BignumInt aword = (i <= modulus[0] ? modulus[i] : 0);
  828. BignumInt bword = (i <= x[0] ? x[i] : 0);
  829. newx[i] = aword - bword - carry;
  830. bword = ~bword;
  831. carry = carry ? (newx[i] >= bword) : (newx[i] > bword);
  832. if (newx[i] != 0)
  833. maxspot = i;
  834. }
  835. newx[0] = maxspot;
  836. freebn(x);
  837. x = newx;
  838. }
  839. /* and return. */
  840. return x;
  841. }
  842. /*
  843. * Render a bignum into decimal. Return a malloced string holding
  844. * the decimal representation.
  845. */
  846. char *bignum_decimal(Bignum x)
  847. {
  848. int ndigits, ndigit;
  849. int i, iszero;
  850. BignumDblInt carry;
  851. char *ret;
  852. BignumInt *workspace;
  853. /*
  854. * First, estimate the number of digits. Since log(10)/log(2)
  855. * is just greater than 93/28 (the joys of continued fraction
  856. * approximations...) we know that for every 93 bits, we need
  857. * at most 28 digits. This will tell us how much to malloc.
  858. *
  859. * Formally: if x has i bits, that means x is strictly less
  860. * than 2^i. Since 2 is less than 10^(28/93), this is less than
  861. * 10^(28i/93). We need an integer power of ten, so we must
  862. * round up (rounding down might make it less than x again).
  863. * Therefore if we multiply the bit count by 28/93, rounding
  864. * up, we will have enough digits.
  865. */
  866. i = bignum_bitcount(x);
  867. ndigits = (28 * i + 92) / 93; /* multiply by 28/93 and round up */
  868. ndigits++; /* allow for trailing \0 */
  869. ret = snewn(ndigits, char);
  870. /*
  871. * Now allocate some workspace to hold the binary form as we
  872. * repeatedly divide it by ten. Initialise this to the
  873. * big-endian form of the number.
  874. */
  875. workspace = snewn(x[0], BignumInt);
  876. for (i = 0; i < x[0]; i++)
  877. workspace[i] = x[x[0] - i];
  878. /*
  879. * Next, write the decimal number starting with the last digit.
  880. * We use ordinary short division, dividing 10 into the
  881. * workspace.
  882. */
  883. ndigit = ndigits - 1;
  884. ret[ndigit] = '\0';
  885. do {
  886. iszero = 1;
  887. carry = 0;
  888. for (i = 0; i < x[0]; i++) {
  889. carry = (carry << BIGNUM_INT_BITS) + workspace[i];
  890. workspace[i] = (BignumInt) (carry / 10);
  891. if (workspace[i])
  892. iszero = 0;
  893. carry %= 10;
  894. }
  895. ret[--ndigit] = (char) (carry + '0');
  896. } while (!iszero);
  897. /*
  898. * There's a chance we've fallen short of the start of the
  899. * string. Correct if so.
  900. */
  901. if (ndigit > 0)
  902. memmove(ret, ret + ndigit, ndigits - ndigit);
  903. /*
  904. * Done.
  905. */
  906. sfree(workspace);
  907. return ret;
  908. }