2 * Bignum routines for RSA and DH and stuff.
11 unsigned short bnZero[1] = { 0 };
12 unsigned short bnOne[2] = { 1, 1 };
15 * The Bignum format is an array of `unsigned short'. The first
16 * element of the array counts the remaining elements. The
17 * remaining elements express the actual number, base 2^16, _least_
18 * significant digit first. (So it's trivial to extract the bit
19 * with value 2^n for any n.)
21 * All Bignums in this module are positive. Negative numbers must
22 * be dealt with outside it.
24 * INVARIANT: the most significant word of any Bignum must be
28 Bignum Zero = bnZero, One = bnOne;
30 Bignum newbn(int length) {
31 Bignum b = smalloc((length+1)*sizeof(unsigned short));
34 memset(b, 0, (length+1)*sizeof(*b));
39 Bignum copybn(Bignum orig) {
40 Bignum b = smalloc((orig[0]+1)*sizeof(unsigned short));
43 memcpy(b, orig, (orig[0]+1)*sizeof(*b));
47 void freebn(Bignum b) {
49 * Burn the evidence, just in case.
51 memset(b, 0, sizeof(b[0]) * (b[0] + 1));
57 * Input is in the first len words of a and b.
58 * Result is returned in the first 2*len words of c.
60 static void internal_mul(unsigned short *a, unsigned short *b,
61 unsigned short *c, int len)
66 for (j = 0; j < 2*len; j++)
69 for (i = len - 1; i >= 0; i--) {
72 for (j = len - 1; j >= 0; j--) {
73 t += ai * (unsigned long) b[j];
74 t += (unsigned long) c[i+j+1];
75 c[i+j+1] = (unsigned short)t;
78 c[i] = (unsigned short)t;
82 static void internal_add_shifted(unsigned short *number,
83 unsigned n, int shift) {
84 int word = 1 + (shift / 16);
85 int bshift = shift % 16;
91 addend += number[word];
92 number[word] = (unsigned short) addend & 0xFFFF;
100 * Input in first alen words of a and first mlen words of m.
101 * Output in first alen words of a
102 * (of which first alen-mlen words will be zero).
103 * The MSW of m MUST have its high bit set.
104 * Quotient is accumulated in the `quotient' array, which is a Bignum
105 * rather than the internal bigendian format. Quotient parts are shifted
106 * left by `qshift' before adding into quot.
108 static void internal_mod(unsigned short *a, int alen,
109 unsigned short *m, int mlen,
110 unsigned short *quot, int qshift)
112 unsigned short m0, m1;
122 for (i = 0; i <= alen-mlen; i++) {
124 unsigned int q, r, c, ai1;
138 /* Find q = h:a[i] / m0 */
139 t = ((unsigned long) h << 16) + a[i];
143 /* Refine our estimate of q by looking at
144 h:a[i]:a[i+1] / m0:m1 */
145 t = (long) m1 * (long) q;
146 if (t > ((unsigned long) r << 16) + ai1) {
149 r = (r + m0) & 0xffff; /* overflow? */
150 if (r >= (unsigned long)m0 &&
151 t > ((unsigned long) r << 16) + ai1)
155 /* Subtract q * m from a[i...] */
157 for (k = mlen - 1; k >= 0; k--) {
158 t = (long) q * (long) m[k];
161 if ((unsigned short) t > a[i+k]) c++;
162 a[i+k] -= (unsigned short) t;
165 /* Add back m in case of borrow */
168 for (k = mlen - 1; k >= 0; k--) {
171 a[i+k] = (unsigned short)t;
177 internal_add_shifted(quot, q, qshift + 16 * (alen-mlen-i));
182 * Compute (base ^ exp) % mod.
183 * The base MUST be smaller than the modulus.
184 * The most significant word of mod MUST be non-zero.
185 * We assume that the result array is the same size as the mod array.
187 Bignum modpow(Bignum base, Bignum exp, Bignum mod)
189 unsigned short *a, *b, *n, *m;
194 /* Allocate m of size mlen, copy mod to m */
195 /* We use big endian internally */
197 m = smalloc(mlen * sizeof(unsigned short));
198 for (j = 0; j < mlen; j++) m[j] = mod[mod[0] - j];
200 /* Shift m left to make msb bit set */
201 for (mshift = 0; mshift < 15; mshift++)
202 if ((m[0] << mshift) & 0x8000) break;
204 for (i = 0; i < mlen - 1; i++)
205 m[i] = (m[i] << mshift) | (m[i+1] >> (16-mshift));
206 m[mlen-1] = m[mlen-1] << mshift;
209 /* Allocate n of size mlen, copy base to n */
210 n = smalloc(mlen * sizeof(unsigned short));
212 for (j = 0; j < i; j++) n[j] = 0;
213 for (j = 0; j < base[0]; j++) n[i+j] = base[base[0] - j];
215 /* Allocate a and b of size 2*mlen. Set a = 1 */
216 a = smalloc(2 * mlen * sizeof(unsigned short));
217 b = smalloc(2 * mlen * sizeof(unsigned short));
218 for (i = 0; i < 2*mlen; i++) a[i] = 0;
221 /* Skip leading zero bits of exp. */
223 while (i < exp[0] && (exp[exp[0] - i] & (1 << j)) == 0) {
225 if (j < 0) { i++; j = 15; }
228 /* Main computation */
231 internal_mul(a + mlen, a + mlen, b, mlen);
232 internal_mod(b, mlen*2, m, mlen, NULL, 0);
233 if ((exp[exp[0] - i] & (1 << j)) != 0) {
234 internal_mul(b + mlen, n, a, mlen);
235 internal_mod(a, mlen*2, m, mlen, NULL, 0);
245 /* Fixup result in case the modulus was shifted */
247 for (i = mlen - 1; i < 2*mlen - 1; i++)
248 a[i] = (a[i] << mshift) | (a[i+1] >> (16-mshift));
249 a[2*mlen-1] = a[2*mlen-1] << mshift;
250 internal_mod(a, mlen*2, m, mlen, NULL, 0);
251 for (i = 2*mlen - 1; i >= mlen; i--)
252 a[i] = (a[i] >> mshift) | (a[i-1] << (16-mshift));
255 /* Copy result to buffer */
256 result = newbn(mod[0]);
257 for (i = 0; i < mlen; i++)
258 result[result[0] - i] = a[i+mlen];
259 while (result[0] > 1 && result[result[0]] == 0) result[0]--;
261 /* Free temporary arrays */
262 for (i = 0; i < 2*mlen; i++) a[i] = 0; sfree(a);
263 for (i = 0; i < 2*mlen; i++) b[i] = 0; sfree(b);
264 for (i = 0; i < mlen; i++) m[i] = 0; sfree(m);
265 for (i = 0; i < mlen; i++) n[i] = 0; sfree(n);
271 * Compute (p * q) % mod.
272 * The most significant word of mod MUST be non-zero.
273 * We assume that the result array is the same size as the mod array.
275 Bignum modmul(Bignum p, Bignum q, Bignum mod)
277 unsigned short *a, *n, *m, *o;
279 int pqlen, mlen, i, j;
282 /* Allocate m of size mlen, copy mod to m */
283 /* We use big endian internally */
285 m = smalloc(mlen * sizeof(unsigned short));
286 for (j = 0; j < mlen; j++) m[j] = mod[mod[0] - j];
288 /* Shift m left to make msb bit set */
289 for (mshift = 0; mshift < 15; mshift++)
290 if ((m[0] << mshift) & 0x8000) break;
292 for (i = 0; i < mlen - 1; i++)
293 m[i] = (m[i] << mshift) | (m[i+1] >> (16-mshift));
294 m[mlen-1] = m[mlen-1] << mshift;
297 pqlen = (p[0] > q[0] ? p[0] : q[0]);
299 /* Allocate n of size pqlen, copy p to n */
300 n = smalloc(pqlen * sizeof(unsigned short));
302 for (j = 0; j < i; j++) n[j] = 0;
303 for (j = 0; j < p[0]; j++) n[i+j] = p[p[0] - j];
305 /* Allocate o of size pqlen, copy q to o */
306 o = smalloc(pqlen * sizeof(unsigned short));
308 for (j = 0; j < i; j++) o[j] = 0;
309 for (j = 0; j < q[0]; j++) o[i+j] = q[q[0] - j];
311 /* Allocate a of size 2*pqlen for result */
312 a = smalloc(2 * pqlen * sizeof(unsigned short));
314 /* Main computation */
315 internal_mul(n, o, a, pqlen);
316 internal_mod(a, pqlen*2, m, mlen, NULL, 0);
318 /* Fixup result in case the modulus was shifted */
320 for (i = 2*pqlen - mlen - 1; i < 2*pqlen - 1; i++)
321 a[i] = (a[i] << mshift) | (a[i+1] >> (16-mshift));
322 a[2*pqlen-1] = a[2*pqlen-1] << mshift;
323 internal_mod(a, pqlen*2, m, mlen, NULL, 0);
324 for (i = 2*pqlen - 1; i >= 2*pqlen - mlen; i--)
325 a[i] = (a[i] >> mshift) | (a[i-1] << (16-mshift));
328 /* Copy result to buffer */
329 result = newbn(mod[0]);
330 for (i = 0; i < mlen; i++)
331 result[result[0] - i] = a[i+2*pqlen-mlen];
332 while (result[0] > 1 && result[result[0]] == 0) result[0]--;
334 /* Free temporary arrays */
335 for (i = 0; i < 2*pqlen; i++) a[i] = 0; sfree(a);
336 for (i = 0; i < mlen; i++) m[i] = 0; sfree(m);
337 for (i = 0; i < pqlen; i++) n[i] = 0; sfree(n);
338 for (i = 0; i < pqlen; i++) o[i] = 0; sfree(o);
345 * The most significant word of mod MUST be non-zero.
346 * We assume that the result array is the same size as the mod array.
347 * We optionally write out a quotient.
349 void bigmod(Bignum p, Bignum mod, Bignum result, Bignum quotient)
351 unsigned short *n, *m;
353 int plen, mlen, i, j;
355 /* Allocate m of size mlen, copy mod to m */
356 /* We use big endian internally */
358 m = smalloc(mlen * sizeof(unsigned short));
359 for (j = 0; j < mlen; j++) m[j] = mod[mod[0] - j];
361 /* Shift m left to make msb bit set */
362 for (mshift = 0; mshift < 15; mshift++)
363 if ((m[0] << mshift) & 0x8000) break;
365 for (i = 0; i < mlen - 1; i++)
366 m[i] = (m[i] << mshift) | (m[i+1] >> (16-mshift));
367 m[mlen-1] = m[mlen-1] << mshift;
371 /* Ensure plen > mlen */
372 if (plen <= mlen) plen = mlen+1;
374 /* Allocate n of size plen, copy p to n */
375 n = smalloc(plen * sizeof(unsigned short));
376 for (j = 0; j < plen; j++) n[j] = 0;
377 for (j = 1; j <= p[0]; j++) n[plen-j] = p[j];
379 /* Main computation */
380 internal_mod(n, plen, m, mlen, quotient, mshift);
382 /* Fixup result in case the modulus was shifted */
384 for (i = plen - mlen - 1; i < plen - 1; i++)
385 n[i] = (n[i] << mshift) | (n[i+1] >> (16-mshift));
386 n[plen-1] = n[plen-1] << mshift;
387 internal_mod(n, plen, m, mlen, quotient, 0);
388 for (i = plen - 1; i >= plen - mlen; i--)
389 n[i] = (n[i] >> mshift) | (n[i-1] << (16-mshift));
392 /* Copy result to buffer */
393 for (i = 1; i <= result[0]; i++) {
395 result[i] = j>=0 ? n[j] : 0;
398 /* Free temporary arrays */
399 for (i = 0; i < mlen; i++) m[i] = 0; sfree(m);
400 for (i = 0; i < plen; i++) n[i] = 0; sfree(n);
404 * Decrement a number.
406 void decbn(Bignum bn) {
408 while (i < bn[0] && bn[i] == 0)
414 * Read an ssh1-format bignum from a data buffer. Return the number
417 int ssh1_read_bignum(unsigned char *data, Bignum *result) {
418 unsigned char *p = data;
427 b = (w+7)/8; /* bits -> bytes */
428 w = (w+15)/16; /* bits -> words */
430 if (!result) /* just return length */
438 unsigned char byte = *p++;
440 bn[1+i/2] |= byte<<8;
451 * Return the bit count of a bignum, for ssh1 encoding.
453 int ssh1_bignum_bitcount(Bignum bn) {
454 int bitcount = bn[0] * 16 - 1;
456 while (bitcount >= 0 && (bn[bitcount/16+1] >> (bitcount % 16)) == 0)
462 * Return the byte length of a bignum when ssh1 encoded.
464 int ssh1_bignum_length(Bignum bn) {
465 return 2 + (ssh1_bignum_bitcount(bn)+7)/8;
469 * Return a byte from a bignum; 0 is least significant, etc.
471 int bignum_byte(Bignum bn, int i) {
473 return 0; /* beyond the end */
475 return (bn[i/2+1] >> 8) & 0xFF;
477 return (bn[i/2+1] ) & 0xFF;
481 * Return a bit from a bignum; 0 is least significant, etc.
483 int bignum_bit(Bignum bn, int i) {
485 return 0; /* beyond the end */
487 return (bn[i/16+1] >> (i%16)) & 1;
491 * Set a bit in a bignum; 0 is least significant, etc.
493 void bignum_set_bit(Bignum bn, int bitnum, int value) {
494 if (bitnum >= 16*bn[0])
495 abort(); /* beyond the end */
498 int mask = 1 << (bitnum%16);
507 * Write a ssh1-format bignum into a buffer. It is assumed the
508 * buffer is big enough. Returns the number of bytes used.
510 int ssh1_write_bignum(void *data, Bignum bn) {
511 unsigned char *p = data;
512 int len = ssh1_bignum_length(bn);
514 int bitc = ssh1_bignum_bitcount(bn);
516 *p++ = (bitc >> 8) & 0xFF;
517 *p++ = (bitc ) & 0xFF;
518 for (i = len-2; i-- ;)
519 *p++ = bignum_byte(bn, i);
524 * Compare two bignums. Returns like strcmp.
526 int bignum_cmp(Bignum a, Bignum b) {
527 int amax = a[0], bmax = b[0];
528 int i = (amax > bmax ? amax : bmax);
530 unsigned short aval = (i > amax ? 0 : a[i]);
531 unsigned short bval = (i > bmax ? 0 : b[i]);
532 if (aval < bval) return -1;
533 if (aval > bval) return +1;
540 * Right-shift one bignum to form another.
542 Bignum bignum_rshift(Bignum a, int shift) {
544 int i, shiftw, shiftb, shiftbb, bits;
545 unsigned short ai, ai1;
547 bits = ssh1_bignum_bitcount(a) - shift;
548 ret = newbn((bits+15)/16);
553 shiftbb = 16 - shiftb;
556 for (i = 1; i <= ret[0]; i++) {
558 ai1 = (i+shiftw+1 <= a[0] ? a[i+shiftw+1] : 0);
559 ret[i] = ((ai >> shiftb) | (ai1 << shiftbb)) & 0xFFFF;
567 * Non-modular multiplication and addition.
569 Bignum bigmuladd(Bignum a, Bignum b, Bignum addend) {
570 int alen = a[0], blen = b[0];
571 int mlen = (alen > blen ? alen : blen);
572 int rlen, i, maxspot;
573 unsigned short *workspace;
576 /* mlen space for a, mlen space for b, 2*mlen for result */
577 workspace = smalloc(mlen * 4 * sizeof(unsigned short));
578 for (i = 0; i < mlen; i++) {
579 workspace[0*mlen + i] = (mlen-i <= a[0] ? a[mlen-i] : 0);
580 workspace[1*mlen + i] = (mlen-i <= b[0] ? b[mlen-i] : 0);
583 internal_mul(workspace+0*mlen, workspace+1*mlen, workspace+2*mlen, mlen);
585 /* now just copy the result back */
586 rlen = alen + blen + 1;
587 if (addend && rlen <= addend[0])
588 rlen = addend[0] + 1;
591 for (i = 1; i <= ret[0]; i++) {
592 ret[i] = (i <= 2*mlen ? workspace[4*mlen - i] : 0);
598 /* now add in the addend, if any */
600 unsigned long carry = 0;
601 for (i = 1; i <= rlen; i++) {
602 carry += (i <= ret[0] ? ret[i] : 0);
603 carry += (i <= addend[0] ? addend[i] : 0);
604 ret[i] = (unsigned short) carry & 0xFFFF;
606 if (ret[i] != 0 && i > maxspot)
616 * Non-modular multiplication.
618 Bignum bigmul(Bignum a, Bignum b) {
619 return bigmuladd(a, b, NULL);
623 * Convert a (max 16-bit) short into a bignum.
625 Bignum bignum_from_short(unsigned short n) {
630 ret[2] = (n >> 16) & 0xFFFF;
631 ret[0] = (ret[2] ? 2 : 1);
636 * Add a long to a bignum.
638 Bignum bignum_add_long(Bignum number, unsigned long addend) {
639 Bignum ret = newbn(number[0]+1);
641 unsigned long carry = 0;
643 for (i = 1; i <= ret[0]; i++) {
644 carry += addend & 0xFFFF;
645 carry += (i <= number[0] ? number[i] : 0);
647 ret[i] = (unsigned short) carry & 0xFFFF;
657 * Compute the residue of a bignum, modulo a (max 16-bit) short.
659 unsigned short bignum_mod_short(Bignum number, unsigned short modulus) {
660 unsigned long mod, r;
665 for (i = number[0]; i > 0; i--)
666 r = (r * 65536 + number[i]) % mod;
667 return (unsigned short) r;
670 static void diagbn(char *prefix, Bignum md) {
671 int i, nibbles, morenibbles;
672 static const char hex[] = "0123456789ABCDEF";
674 printf("%s0x", prefix ? prefix : "");
676 nibbles = (3 + ssh1_bignum_bitcount(md))/4; if (nibbles<1) nibbles=1;
677 morenibbles = 4*md[0] - nibbles;
678 for (i=0; i<morenibbles; i++) putchar('-');
679 for (i=nibbles; i-- ;)
680 putchar(hex[(bignum_byte(md, i/2) >> (4*(i%2))) & 0xF]);
682 if (prefix) putchar('\n');
686 * Greatest common divisor.
688 Bignum biggcd(Bignum av, Bignum bv) {
689 Bignum a = copybn(av);
690 Bignum b = copybn(bv);
694 while (bignum_cmp(b, Zero) != 0) {
695 Bignum t = newbn(b[0]);
696 bigmod(a, b, t, NULL);
698 while (t[0] > 1 && t[t[0]] == 0) t[0]--;
709 * Modular inverse, using Euclid's extended algorithm.
711 Bignum modinv(Bignum number, Bignum modulus) {
712 Bignum a = copybn(modulus);
713 Bignum b = copybn(number);
714 Bignum xp = copybn(Zero);
715 Bignum x = copybn(One);
718 while (bignum_cmp(b, One) != 0) {
719 Bignum t = newbn(b[0]);
720 Bignum q = newbn(a[0]);
722 while (t[0] > 1 && t[t[0]] == 0) t[0]--;
728 x = bigmuladd(q, xp, t);
737 /* now we know that sign * x == 1, and that x < modulus */
739 /* set a new x to be modulus - x */
740 Bignum newx = newbn(modulus[0]);
741 unsigned short carry = 0;
745 for (i = 1; i <= newx[0]; i++) {
746 unsigned short aword = (i <= modulus[0] ? modulus[i] : 0);
747 unsigned short bword = (i <= x[0] ? x[i] : 0);
748 newx[i] = aword - bword - carry;
750 carry = carry ? (newx[i] >= bword) : (newx[i] > bword);
764 * Render a bignum into decimal. Return a malloced string holding
765 * the decimal representation.
767 char *bignum_decimal(Bignum x) {
772 unsigned short *workspace;
775 * First, estimate the number of digits. Since log(10)/log(2)
776 * is just greater than 93/28 (the joys of continued fraction
777 * approximations...) we know that for every 93 bits, we need
778 * at most 28 digits. This will tell us how much to malloc.
780 * Formally: if x has i bits, that means x is strictly less
781 * than 2^i. Since 2 is less than 10^(28/93), this is less than
782 * 10^(28i/93). We need an integer power of ten, so we must
783 * round up (rounding down might make it less than x again).
784 * Therefore if we multiply the bit count by 28/93, rounding
785 * up, we will have enough digits.
787 i = ssh1_bignum_bitcount(x);
788 ndigits = (28*i + 92)/93; /* multiply by 28/93 and round up */
789 ndigits++; /* allow for trailing \0 */
790 ret = smalloc(ndigits);
793 * Now allocate some workspace to hold the binary form as we
794 * repeatedly divide it by ten. Initialise this to the
795 * big-endian form of the number.
797 workspace = smalloc(sizeof(unsigned short) * x[0]);
798 for (i = 0; i < x[0]; i++)
799 workspace[i] = x[x[0] - i];
802 * Next, write the decimal number starting with the last digit.
803 * We use ordinary short division, dividing 10 into the
811 for (i = 0; i < x[0]; i++) {
812 carry = (carry << 16) + workspace[i];
813 workspace[i] = (unsigned short) (carry / 10);
818 ret[--ndigit] = (char)(carry + '0');
822 * There's a chance we've fallen short of the start of the
823 * string. Correct if so.
826 memmove(ret, ret+ndigit, ndigits-ndigit);