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