24af76fb39b4221e0afb6a8d85e503b46e496e38
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
= malloc((length
+1)*sizeof(unsigned short));
34 memset(b
, 0, (length
+1)*sizeof(*b
));
39 Bignum
copybn(Bignum orig
) {
40 Bignum b
= malloc((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 void modpow(Bignum base
, Bignum exp
, Bignum mod
, Bignum result
)
189 unsigned short *a
, *b
, *n
, *m
;
193 /* Allocate m of size mlen, copy mod to m */
194 /* We use big endian internally */
196 m
= malloc(mlen
* sizeof(unsigned short));
197 for (j
= 0; j
< mlen
; j
++) m
[j
] = mod
[mod
[0] - j
];
199 /* Shift m left to make msb bit set */
200 for (mshift
= 0; mshift
< 15; mshift
++)
201 if ((m
[0] << mshift
) & 0x8000) break;
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
;
208 /* Allocate n of size mlen, copy base to n */
209 n
= malloc(mlen
* sizeof(unsigned short));
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
];
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;
220 /* Skip leading zero bits of exp. */
222 while (i
< exp
[0] && (exp
[exp
[0] - i
] & (1 << j
)) == 0) {
224 if (j
< 0) { i
++; j
= 15; }
227 /* Main computation */
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);
244 /* Fixup result in case the modulus was shifted */
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
));
254 /* Copy result to buffer */
255 for (i
= 0; i
< mlen
; i
++)
256 result
[result
[0] - i
] = a
[i
+mlen
];
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
);
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.
270 void modmul(Bignum p
, Bignum q
, Bignum mod
, Bignum result
)
272 unsigned short *a
, *n
, *m
, *o
;
274 int pqlen
, mlen
, i
, j
;
276 /* Allocate m of size mlen, copy mod to m */
277 /* We use big endian internally */
279 m
= malloc(mlen
* sizeof(unsigned short));
280 for (j
= 0; j
< mlen
; j
++) m
[j
] = mod
[mod
[0] - j
];
282 /* Shift m left to make msb bit set */
283 for (mshift
= 0; mshift
< 15; mshift
++)
284 if ((m
[0] << mshift
) & 0x8000) break;
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
;
291 pqlen
= (p
[0] > q
[0] ? p
[0] : q
[0]);
293 /* Allocate n of size pqlen, copy p to n */
294 n
= malloc(pqlen
* sizeof(unsigned short));
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
];
299 /* Allocate o of size pqlen, copy q to o */
300 o
= malloc(pqlen
* sizeof(unsigned short));
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
];
305 /* Allocate a of size 2*pqlen for result */
306 a
= malloc(2 * pqlen
* sizeof(unsigned short));
308 /* Main computation */
309 internal_mul(n
, o
, a
, pqlen
);
310 internal_mod(a
, pqlen
*2, m
, mlen
, NULL
, 0);
312 /* Fixup result in case the modulus was shifted */
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
));
322 /* Copy result to buffer */
323 for (i
= 0; i
< mlen
; i
++)
324 result
[result
[0] - i
] = a
[i
+2*pqlen
-mlen
];
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
);
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.
339 void bigmod(Bignum p
, Bignum mod
, Bignum result
, Bignum quotient
)
341 unsigned short *n
, *m
;
343 int plen
, mlen
, i
, j
;
345 /* Allocate m of size mlen, copy mod to m */
346 /* We use big endian internally */
348 m
= malloc(mlen
* sizeof(unsigned short));
349 for (j
= 0; j
< mlen
; j
++) m
[j
] = mod
[mod
[0] - j
];
351 /* Shift m left to make msb bit set */
352 for (mshift
= 0; mshift
< 15; mshift
++)
353 if ((m
[0] << mshift
) & 0x8000) break;
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
;
361 /* Ensure plen > mlen */
362 if (plen
<= mlen
) plen
= mlen
+1;
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
];
369 /* Main computation */
370 internal_mod(n
, plen
, m
, mlen
, quotient
, mshift
);
372 /* Fixup result in case the modulus was shifted */
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
));
382 /* Copy result to buffer */
383 for (i
= 1; i
<= result
[0]; i
++) {
385 result
[i
] = j
>=0 ? n
[j
] : 0;
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
);
394 * Decrement a number.
396 void decbn(Bignum bn
) {
398 while (i
< bn
[0] && bn
[i
] == 0)
404 * Read an ssh1-format bignum from a data buffer. Return the number
407 int ssh1_read_bignum(unsigned char *data
, Bignum
*result
) {
408 unsigned char *p
= data
;
417 b
= (w
+7)/8; /* bits -> bytes */
418 w
= (w
+15)/16; /* bits -> words */
420 if (!result
) /* just return length */
428 unsigned char byte
= *p
++;
430 bn
[1+i
/2] |= byte
<<8;
441 * Return the bit count of a bignum, for ssh1 encoding.
443 int ssh1_bignum_bitcount(Bignum bn
) {
444 int bitcount
= bn
[0] * 16 - 1;
446 while (bitcount
>= 0 && (bn
[bitcount
/16+1] >> (bitcount
% 16)) == 0)
452 * Return the byte length of a bignum when ssh1 encoded.
454 int ssh1_bignum_length(Bignum bn
) {
455 return 2 + (ssh1_bignum_bitcount(bn
)+7)/8;
459 * Return a byte from a bignum; 0 is least significant, etc.
461 int bignum_byte(Bignum bn
, int i
) {
463 return 0; /* beyond the end */
465 return (bn
[i
/2+1] >> 8) & 0xFF;
467 return (bn
[i
/2+1] ) & 0xFF;
471 * Return a bit from a bignum; 0 is least significant, etc.
473 int bignum_bit(Bignum bn
, int i
) {
475 return 0; /* beyond the end */
477 return (bn
[i
/16+1] >> (i
%16)) & 1;
481 * Set a bit in a bignum; 0 is least significant, etc.
483 void bignum_set_bit(Bignum bn
, int bitnum
, int value
) {
484 if (bitnum
>= 16*bn
[0])
485 abort(); /* beyond the end */
488 int mask
= 1 << (bitnum
%16);
497 * Write a ssh1-format bignum into a buffer. It is assumed the
498 * buffer is big enough. Returns the number of bytes used.
500 int ssh1_write_bignum(void *data
, Bignum bn
) {
501 unsigned char *p
= data
;
502 int len
= ssh1_bignum_length(bn
);
504 int bitc
= ssh1_bignum_bitcount(bn
);
506 *p
++ = (bitc
>> 8) & 0xFF;
507 *p
++ = (bitc
) & 0xFF;
508 for (i
= len
-2; i
-- ;)
509 *p
++ = bignum_byte(bn
, i
);
514 * Compare two bignums. Returns like strcmp.
516 int bignum_cmp(Bignum a
, Bignum b
) {
517 int amax
= a
[0], bmax
= b
[0];
518 int i
= (amax
> bmax ? amax
: bmax
);
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;
530 * Right-shift one bignum to form another.
532 Bignum
bignum_rshift(Bignum a
, int shift
) {
534 int i
, shiftw
, shiftb
, shiftbb
, bits
;
535 unsigned short ai
, ai1
;
537 bits
= ssh1_bignum_bitcount(a
) - shift
;
538 ret
= newbn((bits
+15)/16);
543 shiftbb
= 16 - shiftb
;
546 for (i
= 1; i
<= ret
[0]; i
++) {
548 ai1
= (i
+shiftw
+1 <= a
[0] ? a
[i
+shiftw
+1] : 0);
549 ret
[i
] = ((ai
>> shiftb
) | (ai1
<< shiftbb
)) & 0xFFFF;
557 * Non-modular multiplication and addition.
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
;
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);
573 internal_mul(workspace
+0*mlen
, workspace
+1*mlen
, workspace
+2*mlen
, mlen
);
575 /* now just copy the result back */
576 rlen
= alen
+ blen
+ 1;
577 if (addend
&& rlen
<= addend
[0])
578 rlen
= addend
[0] + 1;
581 for (i
= 1; i
<= ret
[0]; i
++) {
582 ret
[i
] = (i
<= 2*mlen ? workspace
[4*mlen
- i
] : 0);
588 /* now add in the addend, if any */
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;
596 if (ret
[i
] != 0 && i
> maxspot
)
606 * Non-modular multiplication.
608 Bignum
bigmul(Bignum a
, Bignum b
) {
609 return bigmuladd(a
, b
, NULL
);
613 * Convert a (max 16-bit) short into a bignum.
615 Bignum
bignum_from_short(unsigned short n
) {
620 ret
[2] = (n
>> 16) & 0xFFFF;
621 ret
[0] = (ret
[2] ?
2 : 1);
626 * Add a long to a bignum.
628 Bignum
bignum_add_long(Bignum number
, unsigned long addend
) {
629 Bignum ret
= newbn(number
[0]+1);
631 unsigned long carry
= 0;
633 for (i
= 1; i
<= ret
[0]; i
++) {
634 carry
+= addend
& 0xFFFF;
635 carry
+= (i
<= number
[0] ? number
[i
] : 0);
637 ret
[i
] = (unsigned short) carry
& 0xFFFF;
647 * Compute the residue of a bignum, modulo a (max 16-bit) short.
649 unsigned short bignum_mod_short(Bignum number
, unsigned short modulus
) {
650 unsigned long mod
, r
;
655 for (i
= number
[0]; i
> 0; i
--)
656 r
= (r
* 65536 + number
[i
]) % mod
;
657 return (unsigned short) r
;
660 static void diagbn(char *prefix
, Bignum md
) {
661 int i
, nibbles
, morenibbles
;
662 static const char hex
[] = "0123456789ABCDEF";
664 printf("%s0x", prefix ? prefix
: "");
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]);
672 if (prefix
) putchar('\n');
676 * Greatest common divisor.
678 Bignum
biggcd(Bignum av
, Bignum bv
) {
679 Bignum a
= copybn(av
);
680 Bignum b
= copybn(bv
);
684 while (bignum_cmp(b
, Zero
) != 0) {
685 Bignum t
= newbn(b
[0]);
686 bigmod(a
, b
, t
, NULL
);
688 while (t
[0] > 1 && t
[t
[0]] == 0) t
[0]--;
699 * Modular inverse, using Euclid's extended algorithm.
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
);
708 while (bignum_cmp(b
, One
) != 0) {
709 Bignum t
= newbn(b
[0]);
710 Bignum q
= newbn(a
[0]);
712 while (t
[0] > 1 && t
[t
[0]] == 0) t
[0]--;
718 x
= bigmuladd(q
, xp
, t
);
727 /* now we know that sign * x == 1, and that x < modulus */
729 /* set a new x to be modulus - x */
730 Bignum newx
= newbn(modulus
[0]);
731 unsigned short carry
= 0;
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
;
740 carry
= carry ?
(newx
[i
] >= bword
) : (newx
[i
] > bword
);
754 * Render a bignum into decimal. Return a malloced string holding
755 * the decimal representation.
757 char *bignum_decimal(Bignum x
) {
762 unsigned short *workspace
;
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.
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.
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
);
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.
787 workspace
= malloc(sizeof(unsigned short) * x
[0]);
788 for (i
= 0; i
< x
[0]; i
++)
789 workspace
[i
] = x
[x
[0] - i
];
792 * Next, write the decimal number starting with the last digit.
793 * We use ordinary short division, dividing 10 into the
801 for (i
= 0; i
< x
[0]; i
++) {
802 carry
= (carry
<< 16) + workspace
[i
];
803 workspace
[i
] = (unsigned short) (carry
/ 10);
808 ret
[--ndigit
] = (char)(carry
+ '0');
812 * There's a chance we've fallen short of the start of the
813 * string. Correct if so.
816 memmove(ret
, ret
+ndigit
, ndigits
-ndigit
);