2 * Bignum routines for RSA and DH and stuff.
9 #define BIGNUM_INTERNAL
10 typedef unsigned short *Bignum;
14 unsigned short bnZero[1] = { 0 };
15 unsigned short bnOne[2] = { 1, 1 };
18 * The Bignum format is an array of `unsigned short'. The first
19 * element of the array counts the remaining elements. The
20 * remaining elements express the actual number, base 2^16, _least_
21 * significant digit first. (So it's trivial to extract the bit
22 * with value 2^n for any n.)
24 * All Bignums in this module are positive. Negative numbers must
25 * be dealt with outside it.
27 * INVARIANT: the most significant word of any Bignum must be
31 Bignum Zero = bnZero, One = bnOne;
33 static Bignum newbn(int length) {
34 Bignum b = smalloc((length+1)*sizeof(unsigned short));
37 memset(b, 0, (length+1)*sizeof(*b));
42 void bn_restore_invariant(Bignum b) {
43 while (b[0] > 1 && b[b[0]] == 0) b[0]--;
46 Bignum copybn(Bignum orig) {
47 Bignum b = smalloc((orig[0]+1)*sizeof(unsigned short));
50 memcpy(b, orig, (orig[0]+1)*sizeof(*b));
54 void freebn(Bignum b) {
56 * Burn the evidence, just in case.
58 memset(b, 0, sizeof(b[0]) * (b[0] + 1));
62 Bignum bn_power_2(int n) {
63 Bignum ret = newbn((n+15)/16);
64 bignum_set_bit(ret, n, 1);
70 * Input is in the first len words of a and b.
71 * Result is returned in the first 2*len words of c.
73 static void internal_mul(unsigned short *a, unsigned short *b,
74 unsigned short *c, int len)
79 for (j = 0; j < 2*len; j++)
82 for (i = len - 1; i >= 0; i--) {
85 for (j = len - 1; j >= 0; j--) {
86 t += ai * (unsigned long) b[j];
87 t += (unsigned long) c[i+j+1];
88 c[i+j+1] = (unsigned short)t;
91 c[i] = (unsigned short)t;
95 static void internal_add_shifted(unsigned short *number,
96 unsigned n, int shift) {
97 int word = 1 + (shift / 16);
98 int bshift = shift % 16;
101 addend = n << bshift;
104 addend += number[word];
105 number[word] = (unsigned short) addend & 0xFFFF;
113 * Input in first alen words of a and first mlen words of m.
114 * Output in first alen words of a
115 * (of which first alen-mlen words will be zero).
116 * The MSW of m MUST have its high bit set.
117 * Quotient is accumulated in the `quotient' array, which is a Bignum
118 * rather than the internal bigendian format. Quotient parts are shifted
119 * left by `qshift' before adding into quot.
121 static void internal_mod(unsigned short *a, int alen,
122 unsigned short *m, int mlen,
123 unsigned short *quot, int qshift)
125 unsigned short m0, m1;
135 for (i = 0; i <= alen-mlen; i++) {
137 unsigned int q, r, c, ai1;
151 /* Find q = h:a[i] / m0 */
152 t = ((unsigned long) h << 16) + a[i];
156 /* Refine our estimate of q by looking at
157 h:a[i]:a[i+1] / m0:m1 */
158 t = (long) m1 * (long) q;
159 if (t > ((unsigned long) r << 16) + ai1) {
162 r = (r + m0) & 0xffff; /* overflow? */
163 if (r >= (unsigned long)m0 &&
164 t > ((unsigned long) r << 16) + ai1)
168 /* Subtract q * m from a[i...] */
170 for (k = mlen - 1; k >= 0; k--) {
171 t = (long) q * (long) m[k];
174 if ((unsigned short) t > a[i+k]) c++;
175 a[i+k] -= (unsigned short) t;
178 /* Add back m in case of borrow */
181 for (k = mlen - 1; k >= 0; k--) {
184 a[i+k] = (unsigned short)t;
190 internal_add_shifted(quot, q, qshift + 16 * (alen-mlen-i));
195 * Compute (base ^ exp) % mod.
196 * The base MUST be smaller than the modulus.
197 * The most significant word of mod MUST be non-zero.
198 * We assume that the result array is the same size as the mod array.
200 Bignum modpow(Bignum base, Bignum exp, Bignum mod)
202 unsigned short *a, *b, *n, *m;
207 /* Allocate m of size mlen, copy mod to m */
208 /* We use big endian internally */
210 m = smalloc(mlen * sizeof(unsigned short));
211 for (j = 0; j < mlen; j++) m[j] = mod[mod[0] - j];
213 /* Shift m left to make msb bit set */
214 for (mshift = 0; mshift < 15; mshift++)
215 if ((m[0] << mshift) & 0x8000) break;
217 for (i = 0; i < mlen - 1; i++)
218 m[i] = (m[i] << mshift) | (m[i+1] >> (16-mshift));
219 m[mlen-1] = m[mlen-1] << mshift;
222 /* Allocate n of size mlen, copy base to n */
223 n = smalloc(mlen * sizeof(unsigned short));
225 for (j = 0; j < i; j++) n[j] = 0;
226 for (j = 0; j < base[0]; j++) n[i+j] = base[base[0] - j];
228 /* Allocate a and b of size 2*mlen. Set a = 1 */
229 a = smalloc(2 * mlen * sizeof(unsigned short));
230 b = smalloc(2 * mlen * sizeof(unsigned short));
231 for (i = 0; i < 2*mlen; i++) a[i] = 0;
234 /* Skip leading zero bits of exp. */
236 while (i < exp[0] && (exp[exp[0] - i] & (1 << j)) == 0) {
238 if (j < 0) { i++; j = 15; }
241 /* Main computation */
244 internal_mul(a + mlen, a + mlen, b, mlen);
245 internal_mod(b, mlen*2, m, mlen, NULL, 0);
246 if ((exp[exp[0] - i] & (1 << j)) != 0) {
247 internal_mul(b + mlen, n, a, mlen);
248 internal_mod(a, mlen*2, m, mlen, NULL, 0);
258 /* Fixup result in case the modulus was shifted */
260 for (i = mlen - 1; i < 2*mlen - 1; i++)
261 a[i] = (a[i] << mshift) | (a[i+1] >> (16-mshift));
262 a[2*mlen-1] = a[2*mlen-1] << mshift;
263 internal_mod(a, mlen*2, m, mlen, NULL, 0);
264 for (i = 2*mlen - 1; i >= mlen; i--)
265 a[i] = (a[i] >> mshift) | (a[i-1] << (16-mshift));
268 /* Copy result to buffer */
269 result = newbn(mod[0]);
270 for (i = 0; i < mlen; i++)
271 result[result[0] - i] = a[i+mlen];
272 while (result[0] > 1 && result[result[0]] == 0) result[0]--;
274 /* Free temporary arrays */
275 for (i = 0; i < 2*mlen; i++) a[i] = 0; sfree(a);
276 for (i = 0; i < 2*mlen; i++) b[i] = 0; sfree(b);
277 for (i = 0; i < mlen; i++) m[i] = 0; sfree(m);
278 for (i = 0; i < mlen; i++) n[i] = 0; sfree(n);
284 * Compute (p * q) % mod.
285 * The most significant word of mod MUST be non-zero.
286 * We assume that the result array is the same size as the mod array.
288 Bignum modmul(Bignum p, Bignum q, Bignum mod)
290 unsigned short *a, *n, *m, *o;
292 int pqlen, mlen, i, j;
295 /* Allocate m of size mlen, copy mod to m */
296 /* We use big endian internally */
298 m = smalloc(mlen * sizeof(unsigned short));
299 for (j = 0; j < mlen; j++) m[j] = mod[mod[0] - j];
301 /* Shift m left to make msb bit set */
302 for (mshift = 0; mshift < 15; mshift++)
303 if ((m[0] << mshift) & 0x8000) break;
305 for (i = 0; i < mlen - 1; i++)
306 m[i] = (m[i] << mshift) | (m[i+1] >> (16-mshift));
307 m[mlen-1] = m[mlen-1] << mshift;
310 pqlen = (p[0] > q[0] ? p[0] : q[0]);
312 /* Allocate n of size pqlen, copy p to n */
313 n = smalloc(pqlen * sizeof(unsigned short));
315 for (j = 0; j < i; j++) n[j] = 0;
316 for (j = 0; j < p[0]; j++) n[i+j] = p[p[0] - j];
318 /* Allocate o of size pqlen, copy q to o */
319 o = smalloc(pqlen * sizeof(unsigned short));
321 for (j = 0; j < i; j++) o[j] = 0;
322 for (j = 0; j < q[0]; j++) o[i+j] = q[q[0] - j];
324 /* Allocate a of size 2*pqlen for result */
325 a = smalloc(2 * pqlen * sizeof(unsigned short));
327 /* Main computation */
328 internal_mul(n, o, a, pqlen);
329 internal_mod(a, pqlen*2, m, mlen, NULL, 0);
331 /* Fixup result in case the modulus was shifted */
333 for (i = 2*pqlen - mlen - 1; i < 2*pqlen - 1; i++)
334 a[i] = (a[i] << mshift) | (a[i+1] >> (16-mshift));
335 a[2*pqlen-1] = a[2*pqlen-1] << mshift;
336 internal_mod(a, pqlen*2, m, mlen, NULL, 0);
337 for (i = 2*pqlen - 1; i >= 2*pqlen - mlen; i--)
338 a[i] = (a[i] >> mshift) | (a[i-1] << (16-mshift));
341 /* Copy result to buffer */
342 result = newbn(mod[0]);
343 for (i = 0; i < mlen; i++)
344 result[result[0] - i] = a[i+2*pqlen-mlen];
345 while (result[0] > 1 && result[result[0]] == 0) result[0]--;
347 /* Free temporary arrays */
348 for (i = 0; i < 2*pqlen; i++) a[i] = 0; sfree(a);
349 for (i = 0; i < mlen; i++) m[i] = 0; sfree(m);
350 for (i = 0; i < pqlen; i++) n[i] = 0; sfree(n);
351 for (i = 0; i < pqlen; i++) o[i] = 0; sfree(o);
358 * The most significant word of mod MUST be non-zero.
359 * We assume that the result array is the same size as the mod array.
360 * We optionally write out a quotient.
362 void bigmod(Bignum p, Bignum mod, Bignum result, Bignum quotient)
364 unsigned short *n, *m;
366 int plen, mlen, i, j;
368 /* Allocate m of size mlen, copy mod to m */
369 /* We use big endian internally */
371 m = smalloc(mlen * sizeof(unsigned short));
372 for (j = 0; j < mlen; j++) m[j] = mod[mod[0] - j];
374 /* Shift m left to make msb bit set */
375 for (mshift = 0; mshift < 15; mshift++)
376 if ((m[0] << mshift) & 0x8000) break;
378 for (i = 0; i < mlen - 1; i++)
379 m[i] = (m[i] << mshift) | (m[i+1] >> (16-mshift));
380 m[mlen-1] = m[mlen-1] << mshift;
384 /* Ensure plen > mlen */
385 if (plen <= mlen) plen = mlen+1;
387 /* Allocate n of size plen, copy p to n */
388 n = smalloc(plen * sizeof(unsigned short));
389 for (j = 0; j < plen; j++) n[j] = 0;
390 for (j = 1; j <= p[0]; j++) n[plen-j] = p[j];
392 /* Main computation */
393 internal_mod(n, plen, m, mlen, quotient, mshift);
395 /* Fixup result in case the modulus was shifted */
397 for (i = plen - mlen - 1; i < plen - 1; i++)
398 n[i] = (n[i] << mshift) | (n[i+1] >> (16-mshift));
399 n[plen-1] = n[plen-1] << mshift;
400 internal_mod(n, plen, m, mlen, quotient, 0);
401 for (i = plen - 1; i >= plen - mlen; i--)
402 n[i] = (n[i] >> mshift) | (n[i-1] << (16-mshift));
405 /* Copy result to buffer */
406 for (i = 1; i <= result[0]; i++) {
408 result[i] = j>=0 ? n[j] : 0;
411 /* Free temporary arrays */
412 for (i = 0; i < mlen; i++) m[i] = 0; sfree(m);
413 for (i = 0; i < plen; i++) n[i] = 0; sfree(n);
417 * Decrement a number.
419 void decbn(Bignum bn) {
421 while (i < bn[0] && bn[i] == 0)
426 Bignum bignum_from_bytes(unsigned char *data, int nbytes) {
430 w = (nbytes+1)/2; /* bytes -> words */
435 for (i=nbytes; i-- ;) {
436 unsigned char byte = *data++;
438 result[1+i/2] |= byte<<8;
440 result[1+i/2] |= byte;
443 while (result[0] > 1 && result[result[0]] == 0) result[0]--;
448 * Read an ssh1-format bignum from a data buffer. Return the number
451 int ssh1_read_bignum(unsigned char *data, Bignum *result) {
452 unsigned char *p = data;
459 b = (w+7)/8; /* bits -> bytes */
461 if (!result) /* just return length */
464 *result = bignum_from_bytes(p, b);
470 * Return the bit count of a bignum, for ssh1 encoding.
472 int ssh1_bignum_bitcount(Bignum bn) {
473 int bitcount = bn[0] * 16 - 1;
474 while (bitcount >= 0 && (bn[bitcount/16+1] >> (bitcount % 16)) == 0)
480 * Return the byte length of a bignum when ssh1 encoded.
482 int ssh1_bignum_length(Bignum bn) {
483 return 2 + (ssh1_bignum_bitcount(bn)+7)/8;
487 * Return a byte from a bignum; 0 is least significant, etc.
489 int bignum_byte(Bignum bn, int i) {
491 return 0; /* beyond the end */
493 return (bn[i/2+1] >> 8) & 0xFF;
495 return (bn[i/2+1] ) & 0xFF;
499 * Return a bit from a bignum; 0 is least significant, etc.
501 int bignum_bit(Bignum bn, int i) {
503 return 0; /* beyond the end */
505 return (bn[i/16+1] >> (i%16)) & 1;
509 * Set a bit in a bignum; 0 is least significant, etc.
511 void bignum_set_bit(Bignum bn, int bitnum, int value) {
512 if (bitnum >= 16*bn[0])
513 abort(); /* beyond the end */
516 int mask = 1 << (bitnum%16);
525 * Write a ssh1-format bignum into a buffer. It is assumed the
526 * buffer is big enough. Returns the number of bytes used.
528 int ssh1_write_bignum(void *data, Bignum bn) {
529 unsigned char *p = data;
530 int len = ssh1_bignum_length(bn);
532 int bitc = ssh1_bignum_bitcount(bn);
534 *p++ = (bitc >> 8) & 0xFF;
535 *p++ = (bitc ) & 0xFF;
536 for (i = len-2; i-- ;)
537 *p++ = bignum_byte(bn, i);
542 * Compare two bignums. Returns like strcmp.
544 int bignum_cmp(Bignum a, Bignum b) {
545 int amax = a[0], bmax = b[0];
546 int i = (amax > bmax ? amax : bmax);
548 unsigned short aval = (i > amax ? 0 : a[i]);
549 unsigned short bval = (i > bmax ? 0 : b[i]);
550 if (aval < bval) return -1;
551 if (aval > bval) return +1;
558 * Right-shift one bignum to form another.
560 Bignum bignum_rshift(Bignum a, int shift) {
562 int i, shiftw, shiftb, shiftbb, bits;
563 unsigned short ai, ai1;
565 bits = ssh1_bignum_bitcount(a) - shift;
566 ret = newbn((bits+15)/16);
571 shiftbb = 16 - shiftb;
574 for (i = 1; i <= ret[0]; i++) {
576 ai1 = (i+shiftw+1 <= a[0] ? a[i+shiftw+1] : 0);
577 ret[i] = ((ai >> shiftb) | (ai1 << shiftbb)) & 0xFFFF;
585 * Non-modular multiplication and addition.
587 Bignum bigmuladd(Bignum a, Bignum b, Bignum addend) {
588 int alen = a[0], blen = b[0];
589 int mlen = (alen > blen ? alen : blen);
590 int rlen, i, maxspot;
591 unsigned short *workspace;
594 /* mlen space for a, mlen space for b, 2*mlen for result */
595 workspace = smalloc(mlen * 4 * sizeof(unsigned short));
596 for (i = 0; i < mlen; i++) {
597 workspace[0*mlen + i] = (mlen-i <= a[0] ? a[mlen-i] : 0);
598 workspace[1*mlen + i] = (mlen-i <= b[0] ? b[mlen-i] : 0);
601 internal_mul(workspace+0*mlen, workspace+1*mlen, workspace+2*mlen, mlen);
603 /* now just copy the result back */
604 rlen = alen + blen + 1;
605 if (addend && rlen <= addend[0])
606 rlen = addend[0] + 1;
609 for (i = 1; i <= ret[0]; i++) {
610 ret[i] = (i <= 2*mlen ? workspace[4*mlen - i] : 0);
616 /* now add in the addend, if any */
618 unsigned long carry = 0;
619 for (i = 1; i <= rlen; i++) {
620 carry += (i <= ret[0] ? ret[i] : 0);
621 carry += (i <= addend[0] ? addend[i] : 0);
622 ret[i] = (unsigned short) carry & 0xFFFF;
624 if (ret[i] != 0 && i > maxspot)
634 * Non-modular multiplication.
636 Bignum bigmul(Bignum a, Bignum b) {
637 return bigmuladd(a, b, NULL);
641 * Create a bignum which is the bitmask covering another one. That
642 * is, the smallest integer which is >= N and is also one less than
645 Bignum bignum_bitmask(Bignum n) {
646 Bignum ret = copybn(n);
651 while (n[i] == 0 && i > 0)
654 return ret; /* input was zero */
665 * Convert a (max 16-bit) short into a bignum.
667 Bignum bignum_from_short(unsigned short n) {
672 ret[2] = (n >> 16) & 0xFFFF;
673 ret[0] = (ret[2] ? 2 : 1);
678 * Add a long to a bignum.
680 Bignum bignum_add_long(Bignum number, unsigned long addend) {
681 Bignum ret = newbn(number[0]+1);
683 unsigned long carry = 0;
685 for (i = 1; i <= ret[0]; i++) {
686 carry += addend & 0xFFFF;
687 carry += (i <= number[0] ? number[i] : 0);
689 ret[i] = (unsigned short) carry & 0xFFFF;
699 * Compute the residue of a bignum, modulo a (max 16-bit) short.
701 unsigned short bignum_mod_short(Bignum number, unsigned short modulus) {
702 unsigned long mod, r;
707 for (i = number[0]; i > 0; i--)
708 r = (r * 65536 + number[i]) % mod;
709 return (unsigned short) r;
712 void diagbn(char *prefix, Bignum md) {
713 int i, nibbles, morenibbles;
714 static const char hex[] = "0123456789ABCDEF";
716 printf("%s0x", prefix ? prefix : "");
718 nibbles = (3 + ssh1_bignum_bitcount(md))/4; if (nibbles<1) nibbles=1;
719 morenibbles = 4*md[0] - nibbles;
720 for (i=0; i<morenibbles; i++) putchar('-');
721 for (i=nibbles; i-- ;)
722 putchar(hex[(bignum_byte(md, i/2) >> (4*(i%2))) & 0xF]);
724 if (prefix) putchar('\n');
728 * Greatest common divisor.
730 Bignum biggcd(Bignum av, Bignum bv) {
731 Bignum a = copybn(av);
732 Bignum b = copybn(bv);
736 while (bignum_cmp(b, Zero) != 0) {
737 Bignum t = newbn(b[0]);
738 bigmod(a, b, t, NULL);
740 while (t[0] > 1 && t[t[0]] == 0) t[0]--;
751 * Modular inverse, using Euclid's extended algorithm.
753 Bignum modinv(Bignum number, Bignum modulus) {
754 Bignum a = copybn(modulus);
755 Bignum b = copybn(number);
756 Bignum xp = copybn(Zero);
757 Bignum x = copybn(One);
760 while (bignum_cmp(b, One) != 0) {
761 Bignum t = newbn(b[0]);
762 Bignum q = newbn(a[0]);
764 while (t[0] > 1 && t[t[0]] == 0) t[0]--;
770 x = bigmuladd(q, xp, t);
779 /* now we know that sign * x == 1, and that x < modulus */
781 /* set a new x to be modulus - x */
782 Bignum newx = newbn(modulus[0]);
783 unsigned short carry = 0;
787 for (i = 1; i <= newx[0]; i++) {
788 unsigned short aword = (i <= modulus[0] ? modulus[i] : 0);
789 unsigned short bword = (i <= x[0] ? x[i] : 0);
790 newx[i] = aword - bword - carry;
792 carry = carry ? (newx[i] >= bword) : (newx[i] > bword);
806 * Render a bignum into decimal. Return a malloced string holding
807 * the decimal representation.
809 char *bignum_decimal(Bignum x) {
814 unsigned short *workspace;
817 * First, estimate the number of digits. Since log(10)/log(2)
818 * is just greater than 93/28 (the joys of continued fraction
819 * approximations...) we know that for every 93 bits, we need
820 * at most 28 digits. This will tell us how much to malloc.
822 * Formally: if x has i bits, that means x is strictly less
823 * than 2^i. Since 2 is less than 10^(28/93), this is less than
824 * 10^(28i/93). We need an integer power of ten, so we must
825 * round up (rounding down might make it less than x again).
826 * Therefore if we multiply the bit count by 28/93, rounding
827 * up, we will have enough digits.
829 i = ssh1_bignum_bitcount(x);
830 ndigits = (28*i + 92)/93; /* multiply by 28/93 and round up */
831 ndigits++; /* allow for trailing \0 */
832 ret = smalloc(ndigits);
835 * Now allocate some workspace to hold the binary form as we
836 * repeatedly divide it by ten. Initialise this to the
837 * big-endian form of the number.
839 workspace = smalloc(sizeof(unsigned short) * x[0]);
840 for (i = 0; i < x[0]; i++)
841 workspace[i] = x[x[0] - i];
844 * Next, write the decimal number starting with the last digit.
845 * We use ordinary short division, dividing 10 into the
853 for (i = 0; i < x[0]; i++) {
854 carry = (carry << 16) + workspace[i];
855 workspace[i] = (unsigned short) (carry / 10);
860 ret[--ndigit] = (char)(carry + '0');
864 * There's a chance we've fallen short of the start of the
865 * string. Correct if so.
868 memmove(ret, ret+ndigit, ndigits-ndigit);