Improved entropy gathering.
[u/mdw/putty] / 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 = malloc((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 = malloc((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 free(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 void modpow(Bignum base, Bignum exp, Bignum mod, Bignum result)
188 {
189 unsigned short *a, *b, *n, *m;
190 int mshift;
191 int mlen, i, j;
192
193 /* Allocate m of size mlen, copy mod to m */
194 /* We use big endian internally */
195 mlen = mod[0];
196 m = malloc(mlen * sizeof(unsigned short));
197 for (j = 0; j < mlen; j++) m[j] = mod[mod[0] - j];
198
199 /* Shift m left to make msb bit set */
200 for (mshift = 0; mshift < 15; mshift++)
201 if ((m[0] << mshift) & 0x8000) break;
202 if (mshift) {
203 for (i = 0; i < mlen - 1; i++)
204 m[i] = (m[i] << mshift) | (m[i+1] >> (16-mshift));
205 m[mlen-1] = m[mlen-1] << mshift;
206 }
207
208 /* Allocate n of size mlen, copy base to n */
209 n = malloc(mlen * sizeof(unsigned short));
210 i = mlen - base[0];
211 for (j = 0; j < i; j++) n[j] = 0;
212 for (j = 0; j < base[0]; j++) n[i+j] = base[base[0] - j];
213
214 /* Allocate a and b of size 2*mlen. Set a = 1 */
215 a = malloc(2 * mlen * sizeof(unsigned short));
216 b = malloc(2 * mlen * sizeof(unsigned short));
217 for (i = 0; i < 2*mlen; i++) a[i] = 0;
218 a[2*mlen-1] = 1;
219
220 /* Skip leading zero bits of exp. */
221 i = 0; j = 15;
222 while (i < exp[0] && (exp[exp[0] - i] & (1 << j)) == 0) {
223 j--;
224 if (j < 0) { i++; j = 15; }
225 }
226
227 /* Main computation */
228 while (i < exp[0]) {
229 while (j >= 0) {
230 internal_mul(a + mlen, a + mlen, b, mlen);
231 internal_mod(b, mlen*2, m, mlen, NULL, 0);
232 if ((exp[exp[0] - i] & (1 << j)) != 0) {
233 internal_mul(b + mlen, n, a, mlen);
234 internal_mod(a, mlen*2, m, mlen, NULL, 0);
235 } else {
236 unsigned short *t;
237 t = a; a = b; b = t;
238 }
239 j--;
240 }
241 i++; j = 15;
242 }
243
244 /* Fixup result in case the modulus was shifted */
245 if (mshift) {
246 for (i = mlen - 1; i < 2*mlen - 1; i++)
247 a[i] = (a[i] << mshift) | (a[i+1] >> (16-mshift));
248 a[2*mlen-1] = a[2*mlen-1] << mshift;
249 internal_mod(a, mlen*2, m, mlen, NULL, 0);
250 for (i = 2*mlen - 1; i >= mlen; i--)
251 a[i] = (a[i] >> mshift) | (a[i-1] << (16-mshift));
252 }
253
254 /* Copy result to buffer */
255 for (i = 0; i < mlen; i++)
256 result[result[0] - i] = a[i+mlen];
257
258 /* Free temporary arrays */
259 for (i = 0; i < 2*mlen; i++) a[i] = 0; free(a);
260 for (i = 0; i < 2*mlen; i++) b[i] = 0; free(b);
261 for (i = 0; i < mlen; i++) m[i] = 0; free(m);
262 for (i = 0; i < mlen; i++) n[i] = 0; free(n);
263 }
264
265 /*
266 * Compute (p * q) % mod.
267 * The most significant word of mod MUST be non-zero.
268 * We assume that the result array is the same size as the mod array.
269 */
270 void modmul(Bignum p, Bignum q, Bignum mod, Bignum result)
271 {
272 unsigned short *a, *n, *m, *o;
273 int mshift;
274 int pqlen, mlen, i, j;
275
276 /* Allocate m of size mlen, copy mod to m */
277 /* We use big endian internally */
278 mlen = mod[0];
279 m = malloc(mlen * sizeof(unsigned short));
280 for (j = 0; j < mlen; j++) m[j] = mod[mod[0] - j];
281
282 /* Shift m left to make msb bit set */
283 for (mshift = 0; mshift < 15; mshift++)
284 if ((m[0] << mshift) & 0x8000) break;
285 if (mshift) {
286 for (i = 0; i < mlen - 1; i++)
287 m[i] = (m[i] << mshift) | (m[i+1] >> (16-mshift));
288 m[mlen-1] = m[mlen-1] << mshift;
289 }
290
291 pqlen = (p[0] > q[0] ? p[0] : q[0]);
292
293 /* Allocate n of size pqlen, copy p to n */
294 n = malloc(pqlen * sizeof(unsigned short));
295 i = pqlen - p[0];
296 for (j = 0; j < i; j++) n[j] = 0;
297 for (j = 0; j < p[0]; j++) n[i+j] = p[p[0] - j];
298
299 /* Allocate o of size pqlen, copy q to o */
300 o = malloc(pqlen * sizeof(unsigned short));
301 i = pqlen - q[0];
302 for (j = 0; j < i; j++) o[j] = 0;
303 for (j = 0; j < q[0]; j++) o[i+j] = q[q[0] - j];
304
305 /* Allocate a of size 2*pqlen for result */
306 a = malloc(2 * pqlen * sizeof(unsigned short));
307
308 /* Main computation */
309 internal_mul(n, o, a, pqlen);
310 internal_mod(a, pqlen*2, m, mlen, NULL, 0);
311
312 /* Fixup result in case the modulus was shifted */
313 if (mshift) {
314 for (i = 2*pqlen - mlen - 1; i < 2*pqlen - 1; i++)
315 a[i] = (a[i] << mshift) | (a[i+1] >> (16-mshift));
316 a[2*pqlen-1] = a[2*pqlen-1] << mshift;
317 internal_mod(a, pqlen*2, m, mlen, NULL, 0);
318 for (i = 2*pqlen - 1; i >= 2*pqlen - mlen; i--)
319 a[i] = (a[i] >> mshift) | (a[i-1] << (16-mshift));
320 }
321
322 /* Copy result to buffer */
323 for (i = 0; i < mlen; i++)
324 result[result[0] - i] = a[i+2*pqlen-mlen];
325
326 /* Free temporary arrays */
327 for (i = 0; i < 2*pqlen; i++) a[i] = 0; free(a);
328 for (i = 0; i < mlen; i++) m[i] = 0; free(m);
329 for (i = 0; i < pqlen; i++) n[i] = 0; free(n);
330 for (i = 0; i < pqlen; i++) o[i] = 0; free(o);
331 }
332
333 /*
334 * Compute p % mod.
335 * The most significant word of mod MUST be non-zero.
336 * We assume that the result array is the same size as the mod array.
337 * We optionally write out a quotient.
338 */
339 void bigmod(Bignum p, Bignum mod, Bignum result, Bignum quotient)
340 {
341 unsigned short *n, *m;
342 int mshift;
343 int plen, mlen, i, j;
344
345 /* Allocate m of size mlen, copy mod to m */
346 /* We use big endian internally */
347 mlen = mod[0];
348 m = malloc(mlen * sizeof(unsigned short));
349 for (j = 0; j < mlen; j++) m[j] = mod[mod[0] - j];
350
351 /* Shift m left to make msb bit set */
352 for (mshift = 0; mshift < 15; mshift++)
353 if ((m[0] << mshift) & 0x8000) break;
354 if (mshift) {
355 for (i = 0; i < mlen - 1; i++)
356 m[i] = (m[i] << mshift) | (m[i+1] >> (16-mshift));
357 m[mlen-1] = m[mlen-1] << mshift;
358 }
359
360 plen = p[0];
361 /* Ensure plen > mlen */
362 if (plen <= mlen) plen = mlen+1;
363
364 /* Allocate n of size plen, copy p to n */
365 n = malloc(plen * sizeof(unsigned short));
366 for (j = 0; j < plen; j++) n[j] = 0;
367 for (j = 1; j <= p[0]; j++) n[plen-j] = p[j];
368
369 /* Main computation */
370 internal_mod(n, plen, m, mlen, quotient, mshift);
371
372 /* Fixup result in case the modulus was shifted */
373 if (mshift) {
374 for (i = plen - mlen - 1; i < plen - 1; i++)
375 n[i] = (n[i] << mshift) | (n[i+1] >> (16-mshift));
376 n[plen-1] = n[plen-1] << mshift;
377 internal_mod(n, plen, m, mlen, quotient, 0);
378 for (i = plen - 1; i >= plen - mlen; i--)
379 n[i] = (n[i] >> mshift) | (n[i-1] << (16-mshift));
380 }
381
382 /* Copy result to buffer */
383 for (i = 1; i <= result[0]; i++) {
384 int j = plen-i;
385 result[i] = j>=0 ? n[j] : 0;
386 }
387
388 /* Free temporary arrays */
389 for (i = 0; i < mlen; i++) m[i] = 0; free(m);
390 for (i = 0; i < plen; i++) n[i] = 0; free(n);
391 }
392
393 /*
394 * Decrement a number.
395 */
396 void decbn(Bignum bn) {
397 int i = 1;
398 while (i < bn[0] && bn[i] == 0)
399 bn[i++] = 0xFFFF;
400 bn[i]--;
401 }
402
403 /*
404 * Read an ssh1-format bignum from a data buffer. Return the number
405 * of bytes consumed.
406 */
407 int ssh1_read_bignum(unsigned char *data, Bignum *result) {
408 unsigned char *p = data;
409 Bignum bn;
410 int i;
411 int w, b;
412
413 w = 0;
414 for (i=0; i<2; i++)
415 w = (w << 8) + *p++;
416
417 b = (w+7)/8; /* bits -> bytes */
418 w = (w+15)/16; /* bits -> words */
419
420 if (!result) /* just return length */
421 return b + 2;
422
423 bn = newbn(w);
424
425 for (i=1; i<=w; i++)
426 bn[i] = 0;
427 for (i=b; i-- ;) {
428 unsigned char byte = *p++;
429 if (i & 1)
430 bn[1+i/2] |= byte<<8;
431 else
432 bn[1+i/2] |= byte;
433 }
434
435 *result = bn;
436
437 return p - data;
438 }
439
440 /*
441 * Return the bit count of a bignum, for ssh1 encoding.
442 */
443 int ssh1_bignum_bitcount(Bignum bn) {
444 int bitcount = bn[0] * 16 - 1;
445
446 while (bitcount >= 0 && (bn[bitcount/16+1] >> (bitcount % 16)) == 0)
447 bitcount--;
448 return bitcount + 1;
449 }
450
451 /*
452 * Return the byte length of a bignum when ssh1 encoded.
453 */
454 int ssh1_bignum_length(Bignum bn) {
455 return 2 + (ssh1_bignum_bitcount(bn)+7)/8;
456 }
457
458 /*
459 * Return a byte from a bignum; 0 is least significant, etc.
460 */
461 int bignum_byte(Bignum bn, int i) {
462 if (i >= 2*bn[0])
463 return 0; /* beyond the end */
464 else if (i & 1)
465 return (bn[i/2+1] >> 8) & 0xFF;
466 else
467 return (bn[i/2+1] ) & 0xFF;
468 }
469
470 /*
471 * Return a bit from a bignum; 0 is least significant, etc.
472 */
473 int bignum_bit(Bignum bn, int i) {
474 if (i >= 16*bn[0])
475 return 0; /* beyond the end */
476 else
477 return (bn[i/16+1] >> (i%16)) & 1;
478 }
479
480 /*
481 * Set a bit in a bignum; 0 is least significant, etc.
482 */
483 void bignum_set_bit(Bignum bn, int bitnum, int value) {
484 if (bitnum >= 16*bn[0])
485 abort(); /* beyond the end */
486 else {
487 int v = bitnum/16+1;
488 int mask = 1 << (bitnum%16);
489 if (value)
490 bn[v] |= mask;
491 else
492 bn[v] &= ~mask;
493 }
494 }
495
496 /*
497 * Write a ssh1-format bignum into a buffer. It is assumed the
498 * buffer is big enough. Returns the number of bytes used.
499 */
500 int ssh1_write_bignum(void *data, Bignum bn) {
501 unsigned char *p = data;
502 int len = ssh1_bignum_length(bn);
503 int i;
504 int bitc = ssh1_bignum_bitcount(bn);
505
506 *p++ = (bitc >> 8) & 0xFF;
507 *p++ = (bitc ) & 0xFF;
508 for (i = len-2; i-- ;)
509 *p++ = bignum_byte(bn, i);
510 return len;
511 }
512
513 /*
514 * Compare two bignums. Returns like strcmp.
515 */
516 int bignum_cmp(Bignum a, Bignum b) {
517 int amax = a[0], bmax = b[0];
518 int i = (amax > bmax ? amax : bmax);
519 while (i) {
520 unsigned short aval = (i > amax ? 0 : a[i]);
521 unsigned short bval = (i > bmax ? 0 : b[i]);
522 if (aval < bval) return -1;
523 if (aval > bval) return +1;
524 i--;
525 }
526 return 0;
527 }
528
529 /*
530 * Right-shift one bignum to form another.
531 */
532 Bignum bignum_rshift(Bignum a, int shift) {
533 Bignum ret;
534 int i, shiftw, shiftb, shiftbb, bits;
535 unsigned short ai, ai1;
536
537 bits = ssh1_bignum_bitcount(a) - shift;
538 ret = newbn((bits+15)/16);
539
540 if (ret) {
541 shiftw = shift / 16;
542 shiftb = shift % 16;
543 shiftbb = 16 - shiftb;
544
545 ai1 = a[shiftw+1];
546 for (i = 1; i <= ret[0]; i++) {
547 ai = ai1;
548 ai1 = (i+shiftw+1 <= a[0] ? a[i+shiftw+1] : 0);
549 ret[i] = ((ai >> shiftb) | (ai1 << shiftbb)) & 0xFFFF;
550 }
551 }
552
553 return ret;
554 }
555
556 /*
557 * Non-modular multiplication and addition.
558 */
559 Bignum bigmuladd(Bignum a, Bignum b, Bignum addend) {
560 int alen = a[0], blen = b[0];
561 int mlen = (alen > blen ? alen : blen);
562 int rlen, i, maxspot;
563 unsigned short *workspace;
564 Bignum ret;
565
566 /* mlen space for a, mlen space for b, 2*mlen for result */
567 workspace = malloc(mlen * 4 * sizeof(unsigned short));
568 for (i = 0; i < mlen; i++) {
569 workspace[0*mlen + i] = (mlen-i <= a[0] ? a[mlen-i] : 0);
570 workspace[1*mlen + i] = (mlen-i <= b[0] ? b[mlen-i] : 0);
571 }
572
573 internal_mul(workspace+0*mlen, workspace+1*mlen, workspace+2*mlen, mlen);
574
575 /* now just copy the result back */
576 rlen = alen + blen + 1;
577 if (addend && rlen <= addend[0])
578 rlen = addend[0] + 1;
579 ret = newbn(rlen);
580 maxspot = 0;
581 for (i = 1; i <= ret[0]; i++) {
582 ret[i] = (i <= 2*mlen ? workspace[4*mlen - i] : 0);
583 if (ret[i] != 0)
584 maxspot = i;
585 }
586 ret[0] = maxspot;
587
588 /* now add in the addend, if any */
589 if (addend) {
590 unsigned long carry = 0;
591 for (i = 1; i <= rlen; i++) {
592 carry += (i <= ret[0] ? ret[i] : 0);
593 carry += (i <= addend[0] ? addend[i] : 0);
594 ret[i] = (unsigned short) carry & 0xFFFF;
595 carry >>= 16;
596 if (ret[i] != 0 && i > maxspot)
597 maxspot = i;
598 }
599 }
600 ret[0] = maxspot;
601
602 return ret;
603 }
604
605 /*
606 * Non-modular multiplication.
607 */
608 Bignum bigmul(Bignum a, Bignum b) {
609 return bigmuladd(a, b, NULL);
610 }
611
612 /*
613 * Convert a (max 16-bit) short into a bignum.
614 */
615 Bignum bignum_from_short(unsigned short n) {
616 Bignum ret;
617
618 ret = newbn(2);
619 ret[1] = n & 0xFFFF;
620 ret[2] = (n >> 16) & 0xFFFF;
621 ret[0] = (ret[2] ? 2 : 1);
622 return ret;
623 }
624
625 /*
626 * Add a long to a bignum.
627 */
628 Bignum bignum_add_long(Bignum number, unsigned long addend) {
629 Bignum ret = newbn(number[0]+1);
630 int i, maxspot = 0;
631 unsigned long carry = 0;
632
633 for (i = 1; i <= ret[0]; i++) {
634 carry += addend & 0xFFFF;
635 carry += (i <= number[0] ? number[i] : 0);
636 addend >>= 16;
637 ret[i] = (unsigned short) carry & 0xFFFF;
638 carry >>= 16;
639 if (ret[i] != 0)
640 maxspot = i;
641 }
642 ret[0] = maxspot;
643 return ret;
644 }
645
646 /*
647 * Compute the residue of a bignum, modulo a (max 16-bit) short.
648 */
649 unsigned short bignum_mod_short(Bignum number, unsigned short modulus) {
650 unsigned long mod, r;
651 int i;
652
653 r = 0;
654 mod = modulus;
655 for (i = number[0]; i > 0; i--)
656 r = (r * 65536 + number[i]) % mod;
657 return (unsigned short) r;
658 }
659
660 static void diagbn(char *prefix, Bignum md) {
661 int i, nibbles, morenibbles;
662 static const char hex[] = "0123456789ABCDEF";
663
664 printf("%s0x", prefix ? prefix : "");
665
666 nibbles = (3 + ssh1_bignum_bitcount(md))/4; if (nibbles<1) nibbles=1;
667 morenibbles = 4*md[0] - nibbles;
668 for (i=0; i<morenibbles; i++) putchar('-');
669 for (i=nibbles; i-- ;)
670 putchar(hex[(bignum_byte(md, i/2) >> (4*(i%2))) & 0xF]);
671
672 if (prefix) putchar('\n');
673 }
674
675 /*
676 * Greatest common divisor.
677 */
678 Bignum biggcd(Bignum av, Bignum bv) {
679 Bignum a = copybn(av);
680 Bignum b = copybn(bv);
681
682 diagbn("a = ", a);
683 diagbn("b = ", b);
684 while (bignum_cmp(b, Zero) != 0) {
685 Bignum t = newbn(b[0]);
686 bigmod(a, b, t, NULL);
687 diagbn("t = ", t);
688 while (t[0] > 1 && t[t[0]] == 0) t[0]--;
689 freebn(a);
690 a = b;
691 b = t;
692 }
693
694 freebn(b);
695 return a;
696 }
697
698 /*
699 * Modular inverse, using Euclid's extended algorithm.
700 */
701 Bignum modinv(Bignum number, Bignum modulus) {
702 Bignum a = copybn(modulus);
703 Bignum b = copybn(number);
704 Bignum xp = copybn(Zero);
705 Bignum x = copybn(One);
706 int sign = +1;
707
708 while (bignum_cmp(b, One) != 0) {
709 Bignum t = newbn(b[0]);
710 Bignum q = newbn(a[0]);
711 bigmod(a, b, t, q);
712 while (t[0] > 1 && t[t[0]] == 0) t[0]--;
713 freebn(a);
714 a = b;
715 b = t;
716 t = xp;
717 xp = x;
718 x = bigmuladd(q, xp, t);
719 sign = -sign;
720 freebn(t);
721 }
722
723 freebn(b);
724 freebn(a);
725 freebn(xp);
726
727 /* now we know that sign * x == 1, and that x < modulus */
728 if (sign < 0) {
729 /* set a new x to be modulus - x */
730 Bignum newx = newbn(modulus[0]);
731 unsigned short carry = 0;
732 int maxspot = 1;
733 int i;
734
735 for (i = 1; i <= newx[0]; i++) {
736 unsigned short aword = (i <= modulus[0] ? modulus[i] : 0);
737 unsigned short bword = (i <= x[0] ? x[i] : 0);
738 newx[i] = aword - bword - carry;
739 bword = ~bword;
740 carry = carry ? (newx[i] >= bword) : (newx[i] > bword);
741 if (newx[i] != 0)
742 maxspot = i;
743 }
744 newx[0] = maxspot;
745 freebn(x);
746 x = newx;
747 }
748
749 /* and return. */
750 return x;
751 }
752
753 /*
754 * Render a bignum into decimal. Return a malloced string holding
755 * the decimal representation.
756 */
757 char *bignum_decimal(Bignum x) {
758 int ndigits, ndigit;
759 int i, iszero;
760 unsigned long carry;
761 char *ret;
762 unsigned short *workspace;
763
764 /*
765 * First, estimate the number of digits. Since log(10)/log(2)
766 * is just greater than 93/28 (the joys of continued fraction
767 * approximations...) we know that for every 93 bits, we need
768 * at most 28 digits. This will tell us how much to malloc.
769 *
770 * Formally: if x has i bits, that means x is strictly less
771 * than 2^i. Since 2 is less than 10^(28/93), this is less than
772 * 10^(28i/93). We need an integer power of ten, so we must
773 * round up (rounding down might make it less than x again).
774 * Therefore if we multiply the bit count by 28/93, rounding
775 * up, we will have enough digits.
776 */
777 i = ssh1_bignum_bitcount(x);
778 ndigits = (28*i + 92)/93; /* multiply by 28/93 and round up */
779 ndigits++; /* allow for trailing \0 */
780 ret = malloc(ndigits);
781
782 /*
783 * Now allocate some workspace to hold the binary form as we
784 * repeatedly divide it by ten. Initialise this to the
785 * big-endian form of the number.
786 */
787 workspace = malloc(sizeof(unsigned short) * x[0]);
788 for (i = 0; i < x[0]; i++)
789 workspace[i] = x[x[0] - i];
790
791 /*
792 * Next, write the decimal number starting with the last digit.
793 * We use ordinary short division, dividing 10 into the
794 * workspace.
795 */
796 ndigit = ndigits-1;
797 ret[ndigit] = '\0';
798 do {
799 iszero = 1;
800 carry = 0;
801 for (i = 0; i < x[0]; i++) {
802 carry = (carry << 16) + workspace[i];
803 workspace[i] = (unsigned short) (carry / 10);
804 if (workspace[i])
805 iszero = 0;
806 carry %= 10;
807 }
808 ret[--ndigit] = (char)(carry + '0');
809 } while (!iszero);
810
811 /*
812 * There's a chance we've fallen short of the start of the
813 * string. Correct if so.
814 */
815 if (ndigit > 0)
816 memmove(ret, ret+ndigit, ndigits-ndigit);
817
818 /*
819 * Done.
820 */
821 return ret;
822 }