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