]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - sshbn.c
Make memory management uniform: _everything_ now goes through the
[PuTTY.git] / sshbn.c
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
9 #include "ssh.h"
10
11 unsigned short bnZero[1] = { 0 };
12 unsigned short bnOne[2] = { 1, 1 };
13
14 /*
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.)
20  *
21  * All Bignums in this module are positive. Negative numbers must
22  * be dealt with outside it.
23  *
24  * INVARIANT: the most significant word of any Bignum must be
25  * nonzero.
26  */
27
28 Bignum Zero = bnZero, One = bnOne;
29
30 Bignum newbn(int length) {
31     Bignum b = smalloc((length+1)*sizeof(unsigned short));
32     if (!b)
33         abort();                       /* FIXME */
34     memset(b, 0, (length+1)*sizeof(*b));
35     b[0] = length;
36     return b;
37 }
38
39 Bignum copybn(Bignum orig) {
40     Bignum b = smalloc((orig[0]+1)*sizeof(unsigned short));
41     if (!b)
42         abort();                       /* FIXME */
43     memcpy(b, orig, (orig[0]+1)*sizeof(*b));
44     return b;
45 }
46
47 void freebn(Bignum b) {
48     /*
49      * Burn the evidence, just in case.
50      */
51     memset(b, 0, sizeof(b[0]) * (b[0] + 1));
52     sfree(b);
53 }
54
55 /*
56  * Compute c = a * b.
57  * Input is in the first len words of a and b.
58  * Result is returned in the first 2*len words of c.
59  */
60 static void internal_mul(unsigned short *a, unsigned short *b,
61                          unsigned short *c, int len)
62 {
63     int i, j;
64     unsigned long ai, t;
65
66     for (j = 0; j < 2*len; j++)
67         c[j] = 0;
68
69     for (i = len - 1; i >= 0; i--) {
70         ai = a[i];
71         t = 0;
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;
76             t = t >> 16;
77         }
78         c[i] = (unsigned short)t;
79     }
80 }
81
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;
86     unsigned long addend;
87
88     addend = n << bshift;
89
90     while (addend) {
91         addend += number[word];
92         number[word] = (unsigned short) addend & 0xFFFF;
93         addend >>= 16;
94         word++;
95     }
96 }
97
98 /*
99  * Compute a = a % m.
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.
107  */
108 static void internal_mod(unsigned short *a, int alen,
109                          unsigned short *m, int mlen,
110                          unsigned short *quot, int qshift)
111 {
112     unsigned short m0, m1;
113     unsigned int h;
114     int i, k;
115
116     m0 = m[0];
117     if (mlen > 1)
118         m1 = m[1];
119     else
120         m1 = 0;
121
122     for (i = 0; i <= alen-mlen; i++) {
123         unsigned long t;
124         unsigned int q, r, c, ai1;
125
126         if (i == 0) {
127             h = 0;
128         } else {
129             h = a[i-1];
130             a[i-1] = 0;
131         }
132
133         if (i == alen-1)
134             ai1 = 0;
135         else
136             ai1 = a[i+1];
137
138         /* Find q = h:a[i] / m0 */
139         t = ((unsigned long) h << 16) + a[i];
140         q = t / m0;
141         r = t % m0;
142
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) {
147             q--;
148             t -= m1;
149             r = (r + m0) & 0xffff; /* overflow? */
150             if (r >= (unsigned long)m0 &&
151                 t > ((unsigned long) r << 16) + ai1)
152                 q--;
153         }
154
155         /* Subtract q * m from a[i...] */
156         c = 0;
157         for (k = mlen - 1; k >= 0; k--) {
158             t = (long) q * (long) m[k];
159             t += c;
160             c = t >> 16;
161             if ((unsigned short) t > a[i+k]) c++;
162             a[i+k] -= (unsigned short) t;
163         }
164
165         /* Add back m in case of borrow */
166         if (c != h) {
167             t = 0;
168             for (k = mlen - 1; k >= 0; k--) {
169                 t += m[k];
170                 t += a[i+k];
171                 a[i+k] = (unsigned short)t;
172                 t = t >> 16;
173             }
174             q--;
175         }
176         if (quot)
177             internal_add_shifted(quot, q, qshift + 16 * (alen-mlen-i));
178     }
179 }
180
181 /*
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.
186  */
187 Bignum modpow(Bignum base, Bignum exp, Bignum mod)
188 {
189     unsigned short *a, *b, *n, *m;
190     int mshift;
191     int mlen, i, j;
192     Bignum result;
193
194     /* Allocate m of size mlen, copy mod to m */
195     /* We use big endian internally */
196     mlen = mod[0];
197     m = smalloc(mlen * sizeof(unsigned short));
198     for (j = 0; j < mlen; j++) m[j] = mod[mod[0] - j];
199
200     /* Shift m left to make msb bit set */
201     for (mshift = 0; mshift < 15; mshift++)
202         if ((m[0] << mshift) & 0x8000) break;
203     if (mshift) {
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;
207     }
208
209     /* Allocate n of size mlen, copy base to n */
210     n = smalloc(mlen * sizeof(unsigned short));
211     i = mlen - base[0];
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];
214
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;
219     a[2*mlen-1] = 1;
220
221     /* Skip leading zero bits of exp. */
222     i = 0; j = 15;
223     while (i < exp[0] && (exp[exp[0] - i] & (1 << j)) == 0) {
224         j--;
225         if (j < 0) { i++; j = 15; }
226     }
227
228     /* Main computation */
229     while (i < exp[0]) {
230         while (j >= 0) {
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);
236             } else {
237                 unsigned short *t;
238                 t = a;  a = b;  b = t;
239             }
240             j--;
241         }
242         i++; j = 15;
243     }
244
245     /* Fixup result in case the modulus was shifted */
246     if (mshift) {
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));
253     }
254
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]--;
260
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);
266
267     return result;
268 }
269
270 /*
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.
274  */
275 Bignum modmul(Bignum p, Bignum q, Bignum mod)
276 {
277     unsigned short *a, *n, *m, *o;
278     int mshift;
279     int pqlen, mlen, i, j;
280     Bignum result;
281
282     /* Allocate m of size mlen, copy mod to m */
283     /* We use big endian internally */
284     mlen = mod[0];
285     m = smalloc(mlen * sizeof(unsigned short));
286     for (j = 0; j < mlen; j++) m[j] = mod[mod[0] - j];
287
288     /* Shift m left to make msb bit set */
289     for (mshift = 0; mshift < 15; mshift++)
290         if ((m[0] << mshift) & 0x8000) break;
291     if (mshift) {
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;
295     }
296
297     pqlen = (p[0] > q[0] ? p[0] : q[0]);
298
299     /* Allocate n of size pqlen, copy p to n */
300     n = smalloc(pqlen * sizeof(unsigned short));
301     i = pqlen - p[0];
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];
304
305     /* Allocate o of size pqlen, copy q to o */
306     o = smalloc(pqlen * sizeof(unsigned short));
307     i = pqlen - q[0];
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];
310
311     /* Allocate a of size 2*pqlen for result */
312     a = smalloc(2 * pqlen * sizeof(unsigned short));
313
314     /* Main computation */
315     internal_mul(n, o, a, pqlen);
316     internal_mod(a, pqlen*2, m, mlen, NULL, 0);
317
318     /* Fixup result in case the modulus was shifted */
319     if (mshift) {
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));
326     }
327
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]--;
333
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);
339
340     return result;
341 }
342
343 /*
344  * Compute p % mod.
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.
348  */
349 void bigmod(Bignum p, Bignum mod, Bignum result, Bignum quotient)
350 {
351     unsigned short *n, *m;
352     int mshift;
353     int plen, mlen, i, j;
354
355     /* Allocate m of size mlen, copy mod to m */
356     /* We use big endian internally */
357     mlen = mod[0];
358     m = smalloc(mlen * sizeof(unsigned short));
359     for (j = 0; j < mlen; j++) m[j] = mod[mod[0] - j];
360
361     /* Shift m left to make msb bit set */
362     for (mshift = 0; mshift < 15; mshift++)
363         if ((m[0] << mshift) & 0x8000) break;
364     if (mshift) {
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;
368     }
369
370     plen = p[0];
371     /* Ensure plen > mlen */
372     if (plen <= mlen) plen = mlen+1;
373
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];
378
379     /* Main computation */
380     internal_mod(n, plen, m, mlen, quotient, mshift);
381
382     /* Fixup result in case the modulus was shifted */
383     if (mshift) {
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));
390     }
391
392     /* Copy result to buffer */
393     for (i = 1; i <= result[0]; i++) {
394         int j = plen-i;
395         result[i] = j>=0 ? n[j] : 0;
396     }
397
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);
401 }
402
403 /*
404  * Decrement a number.
405  */
406 void decbn(Bignum bn) {
407     int i = 1;
408     while (i < bn[0] && bn[i] == 0)
409         bn[i++] = 0xFFFF;
410     bn[i]--;
411 }
412
413 /*
414  * Read an ssh1-format bignum from a data buffer. Return the number
415  * of bytes consumed.
416  */
417 int ssh1_read_bignum(unsigned char *data, Bignum *result) {
418     unsigned char *p = data;
419     Bignum bn;
420     int i;
421     int w, b;
422
423     w = 0;
424     for (i=0; i<2; i++)
425         w = (w << 8) + *p++;
426
427     b = (w+7)/8;                       /* bits -> bytes */
428     w = (w+15)/16;                     /* bits -> words */
429
430     if (!result)                       /* just return length */
431         return b + 2;
432
433     bn = newbn(w);
434
435     for (i=1; i<=w; i++)
436         bn[i] = 0;
437     for (i=b; i-- ;) {
438         unsigned char byte = *p++;
439         if (i & 1)
440             bn[1+i/2] |= byte<<8;
441         else
442             bn[1+i/2] |= byte;
443     }
444
445     *result = bn;
446
447     return p - data;
448 }
449
450 /*
451  * Return the bit count of a bignum, for ssh1 encoding.
452  */
453 int ssh1_bignum_bitcount(Bignum bn) {
454     int bitcount = bn[0] * 16 - 1;
455
456     while (bitcount >= 0 && (bn[bitcount/16+1] >> (bitcount % 16)) == 0)
457         bitcount--;
458     return bitcount + 1;
459 }
460
461 /*
462  * Return the byte length of a bignum when ssh1 encoded.
463  */
464 int ssh1_bignum_length(Bignum bn) {
465     return 2 + (ssh1_bignum_bitcount(bn)+7)/8;
466 }
467
468 /*
469  * Return a byte from a bignum; 0 is least significant, etc.
470  */
471 int bignum_byte(Bignum bn, int i) {
472     if (i >= 2*bn[0])
473         return 0;                      /* beyond the end */
474     else if (i & 1)
475         return (bn[i/2+1] >> 8) & 0xFF;
476     else
477         return (bn[i/2+1]     ) & 0xFF;
478 }
479
480 /*
481  * Return a bit from a bignum; 0 is least significant, etc.
482  */
483 int bignum_bit(Bignum bn, int i) {
484     if (i >= 16*bn[0])
485         return 0;                      /* beyond the end */
486     else
487         return (bn[i/16+1] >> (i%16)) & 1;
488 }
489
490 /*
491  * Set a bit in a bignum; 0 is least significant, etc.
492  */
493 void bignum_set_bit(Bignum bn, int bitnum, int value) {
494     if (bitnum >= 16*bn[0])
495         abort();                       /* beyond the end */
496     else {
497         int v = bitnum/16+1;
498         int mask = 1 << (bitnum%16);
499         if (value)
500             bn[v] |= mask;
501         else
502             bn[v] &= ~mask;
503     }
504 }
505
506 /*
507  * Write a ssh1-format bignum into a buffer. It is assumed the
508  * buffer is big enough. Returns the number of bytes used.
509  */
510 int ssh1_write_bignum(void *data, Bignum bn) {
511     unsigned char *p = data;
512     int len = ssh1_bignum_length(bn);
513     int i;
514     int bitc = ssh1_bignum_bitcount(bn);
515
516     *p++ = (bitc >> 8) & 0xFF;
517     *p++ = (bitc     ) & 0xFF;
518     for (i = len-2; i-- ;)
519         *p++ = bignum_byte(bn, i);
520     return len;
521 }
522
523 /*
524  * Compare two bignums. Returns like strcmp.
525  */
526 int bignum_cmp(Bignum a, Bignum b) {
527     int amax = a[0], bmax = b[0];
528     int i = (amax > bmax ? amax : bmax);
529     while (i) {
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;
534         i--;
535     }
536     return 0;
537 }
538
539 /*
540  * Right-shift one bignum to form another.
541  */
542 Bignum bignum_rshift(Bignum a, int shift) {
543     Bignum ret;
544     int i, shiftw, shiftb, shiftbb, bits;
545     unsigned short ai, ai1;
546
547     bits = ssh1_bignum_bitcount(a) - shift;
548     ret = newbn((bits+15)/16);
549
550     if (ret) {
551         shiftw = shift / 16;
552         shiftb = shift % 16;
553         shiftbb = 16 - shiftb;
554
555         ai1 = a[shiftw+1];
556         for (i = 1; i <= ret[0]; i++) {
557             ai = ai1;
558             ai1 = (i+shiftw+1 <= a[0] ? a[i+shiftw+1] : 0);
559             ret[i] = ((ai >> shiftb) | (ai1 << shiftbb)) & 0xFFFF;
560         }
561     }
562
563     return ret;
564 }
565
566 /*
567  * Non-modular multiplication and addition.
568  */
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;
574     Bignum ret;
575
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);
581     }
582
583     internal_mul(workspace+0*mlen, workspace+1*mlen, workspace+2*mlen, mlen);
584
585     /* now just copy the result back */
586     rlen = alen + blen + 1;
587     if (addend && rlen <= addend[0])
588         rlen = addend[0] + 1;
589     ret = newbn(rlen);
590     maxspot = 0;
591     for (i = 1; i <= ret[0]; i++) {
592         ret[i] = (i <= 2*mlen ? workspace[4*mlen - i] : 0);
593         if (ret[i] != 0)
594             maxspot = i;
595     }
596     ret[0] = maxspot;
597
598     /* now add in the addend, if any */
599     if (addend) {
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;
605             carry >>= 16;
606             if (ret[i] != 0 && i > maxspot)
607                 maxspot = i;
608         }
609     }
610     ret[0] = maxspot;
611
612     return ret;
613 }
614
615 /*
616  * Non-modular multiplication.
617  */
618 Bignum bigmul(Bignum a, Bignum b) {
619     return bigmuladd(a, b, NULL);
620 }
621
622 /*
623  * Convert a (max 16-bit) short into a bignum.
624  */
625 Bignum bignum_from_short(unsigned short n) {
626     Bignum ret;
627
628     ret = newbn(2);
629     ret[1] = n & 0xFFFF;
630     ret[2] = (n >> 16) & 0xFFFF;
631     ret[0] = (ret[2] ? 2 : 1);
632     return ret;        
633 }
634
635 /*
636  * Add a long to a bignum.
637  */
638 Bignum bignum_add_long(Bignum number, unsigned long addend) {
639     Bignum ret = newbn(number[0]+1);
640     int i, maxspot = 0;
641     unsigned long carry = 0;
642
643     for (i = 1; i <= ret[0]; i++) {
644         carry += addend & 0xFFFF;
645         carry += (i <= number[0] ? number[i] : 0);
646         addend >>= 16;
647         ret[i] = (unsigned short) carry & 0xFFFF;
648         carry >>= 16;
649         if (ret[i] != 0)
650             maxspot = i;
651     }
652     ret[0] = maxspot;
653     return ret;
654 }
655
656 /*
657  * Compute the residue of a bignum, modulo a (max 16-bit) short.
658  */
659 unsigned short bignum_mod_short(Bignum number, unsigned short modulus) {
660     unsigned long mod, r;
661     int i;
662
663     r = 0;
664     mod = modulus;
665     for (i = number[0]; i > 0; i--)
666         r = (r * 65536 + number[i]) % mod;
667     return (unsigned short) r;
668 }
669
670 static void diagbn(char *prefix, Bignum md) {
671     int i, nibbles, morenibbles;
672     static const char hex[] = "0123456789ABCDEF";
673
674     printf("%s0x", prefix ? prefix : "");
675
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]);
681
682     if (prefix) putchar('\n');
683 }
684
685 /*
686  * Greatest common divisor.
687  */
688 Bignum biggcd(Bignum av, Bignum bv) {
689     Bignum a = copybn(av);
690     Bignum b = copybn(bv);
691
692     diagbn("a = ", a);
693     diagbn("b = ", b);
694     while (bignum_cmp(b, Zero) != 0) {
695         Bignum t = newbn(b[0]);
696         bigmod(a, b, t, NULL);
697         diagbn("t = ", t);
698         while (t[0] > 1 && t[t[0]] == 0) t[0]--;
699         freebn(a);
700         a = b;
701         b = t;
702     }
703
704     freebn(b);
705     return a;
706 }
707
708 /*
709  * Modular inverse, using Euclid's extended algorithm.
710  */
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);
716     int sign = +1;
717
718     while (bignum_cmp(b, One) != 0) {
719         Bignum t = newbn(b[0]);
720         Bignum q = newbn(a[0]);
721         bigmod(a, b, t, q);
722         while (t[0] > 1 && t[t[0]] == 0) t[0]--;
723         freebn(a);
724         a = b;
725         b = t;
726         t = xp;
727         xp = x;
728         x = bigmuladd(q, xp, t);
729         sign = -sign;
730         freebn(t);
731     }
732
733     freebn(b);
734     freebn(a);
735     freebn(xp);
736
737     /* now we know that sign * x == 1, and that x < modulus */
738     if (sign < 0) {
739         /* set a new x to be modulus - x */
740         Bignum newx = newbn(modulus[0]);
741         unsigned short carry = 0;
742         int maxspot = 1;
743         int i;
744
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;
749             bword = ~bword;
750             carry = carry ? (newx[i] >= bword) : (newx[i] > bword);
751             if (newx[i] != 0)
752                 maxspot = i;
753         }
754         newx[0] = maxspot;
755         freebn(x);
756         x = newx;
757     }
758
759     /* and return. */
760     return x;
761 }
762
763 /*
764  * Render a bignum into decimal. Return a malloced string holding
765  * the decimal representation.
766  */
767 char *bignum_decimal(Bignum x) {
768     int ndigits, ndigit;
769     int i, iszero;
770     unsigned long carry;
771     char *ret;
772     unsigned short *workspace;
773
774     /*
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.
779      *
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.
786      */
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);
791
792     /*
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.
796      */
797     workspace = smalloc(sizeof(unsigned short) * x[0]);
798     for (i = 0; i < x[0]; i++)
799         workspace[i] = x[x[0] - i];
800
801     /*
802      * Next, write the decimal number starting with the last digit.
803      * We use ordinary short division, dividing 10 into the
804      * workspace.
805      */
806     ndigit = ndigits-1;
807     ret[ndigit] = '\0';
808     do {
809         iszero = 1;
810         carry = 0;
811         for (i = 0; i < x[0]; i++) {
812             carry = (carry << 16) + workspace[i];
813             workspace[i] = (unsigned short) (carry / 10);
814             if (workspace[i])
815                 iszero = 0;
816             carry %= 10;
817         }
818         ret[--ndigit] = (char)(carry + '0');
819     } while (!iszero);
820
821     /*
822      * There's a chance we've fallen short of the start of the
823      * string. Correct if so.
824      */
825     if (ndigit > 0)
826         memmove(ret, ret+ndigit, ndigits-ndigit);
827
828     /*
829      * Done.
830      */
831     return ret;
832 }