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