2 * Bignum routines for RSA and DH and stuff.
9 #if 0 // use PuTTY main debugging for diagbn()
12 #define debugprint debug
14 #define debugprint(x) printf x
17 #define BIGNUM_INTERNAL
18 typedef unsigned short *Bignum
;
22 unsigned short bnZero
[1] = { 0 };
23 unsigned short bnOne
[2] = { 1, 1 };
26 * The Bignum format is an array of `unsigned short'. The first
27 * element of the array counts the remaining elements. The
28 * remaining elements express the actual number, base 2^16, _least_
29 * significant digit first. (So it's trivial to extract the bit
30 * with value 2^n for any n.)
32 * All Bignums in this module are positive. Negative numbers must
33 * be dealt with outside it.
35 * INVARIANT: the most significant word of any Bignum must be
39 Bignum Zero
= bnZero
, One
= bnOne
;
41 static Bignum
newbn(int length
)
43 Bignum b
= smalloc((length
+ 1) * sizeof(unsigned short));
46 memset(b
, 0, (length
+ 1) * sizeof(*b
));
51 void bn_restore_invariant(Bignum b
)
53 while (b
[0] > 1 && b
[b
[0]] == 0)
57 Bignum
copybn(Bignum orig
)
59 Bignum b
= smalloc((orig
[0] + 1) * sizeof(unsigned short));
62 memcpy(b
, orig
, (orig
[0] + 1) * sizeof(*b
));
69 * Burn the evidence, just in case.
71 memset(b
, 0, sizeof(b
[0]) * (b
[0] + 1));
75 Bignum
bn_power_2(int n
)
77 Bignum ret
= newbn(n
/ 16 + 1);
78 bignum_set_bit(ret
, n
, 1);
84 * Input is in the first len words of a and b.
85 * Result is returned in the first 2*len words of c.
87 static void internal_mul(unsigned short *a
, unsigned short *b
,
88 unsigned short *c
, int len
)
93 for (j
= 0; j
< 2 * len
; j
++)
96 for (i
= len
- 1; i
>= 0; i
--) {
99 for (j
= len
- 1; j
>= 0; j
--) {
100 t
+= ai
* (unsigned long) b
[j
];
101 t
+= (unsigned long) c
[i
+ j
+ 1];
102 c
[i
+ j
+ 1] = (unsigned short) t
;
105 c
[i
] = (unsigned short) t
;
109 static void internal_add_shifted(unsigned short *number
,
110 unsigned n
, int shift
)
112 int word
= 1 + (shift
/ 16);
113 int bshift
= shift
% 16;
114 unsigned long addend
;
116 addend
= n
<< bshift
;
119 addend
+= number
[word
];
120 number
[word
] = (unsigned short) addend
& 0xFFFF;
128 * Input in first alen words of a and first mlen words of m.
129 * Output in first alen words of a
130 * (of which first alen-mlen words will be zero).
131 * The MSW of m MUST have its high bit set.
132 * Quotient is accumulated in the `quotient' array, which is a Bignum
133 * rather than the internal bigendian format. Quotient parts are shifted
134 * left by `qshift' before adding into quot.
136 static void internal_mod(unsigned short *a
, int alen
,
137 unsigned short *m
, int mlen
,
138 unsigned short *quot
, int qshift
)
140 unsigned short m0
, m1
;
150 for (i
= 0; i
<= alen
- mlen
; i
++) {
152 unsigned int q
, r
, c
, ai1
;
166 /* Find q = h:a[i] / m0 */
167 t
= ((unsigned long) h
<< 16) + a
[i
];
171 /* Refine our estimate of q by looking at
172 h:a[i]:a[i+1] / m0:m1 */
173 t
= (long) m1
*(long) q
;
174 if (t
> ((unsigned long) r
<< 16) + ai1
) {
177 r
= (r
+ m0
) & 0xffff; /* overflow? */
178 if (r
>= (unsigned long) m0
&&
179 t
> ((unsigned long) r
<< 16) + ai1
) q
--;
182 /* Subtract q * m from a[i...] */
184 for (k
= mlen
- 1; k
>= 0; k
--) {
185 t
= (long) q
*(long) m
[k
];
188 if ((unsigned short) t
> a
[i
+ k
])
190 a
[i
+ k
] -= (unsigned short) t
;
193 /* Add back m in case of borrow */
196 for (k
= mlen
- 1; k
>= 0; k
--) {
199 a
[i
+ k
] = (unsigned short) t
;
205 internal_add_shifted(quot
, q
, qshift
+ 16 * (alen
- mlen
- i
));
210 * Compute (base ^ exp) % mod.
211 * The base MUST be smaller than the modulus.
212 * The most significant word of mod MUST be non-zero.
213 * We assume that the result array is the same size as the mod array.
215 Bignum
modpow(Bignum base
, Bignum exp
, Bignum mod
)
217 unsigned short *a
, *b
, *n
, *m
;
222 /* Allocate m of size mlen, copy mod to m */
223 /* We use big endian internally */
225 m
= smalloc(mlen
* sizeof(unsigned short));
226 for (j
= 0; j
< mlen
; j
++)
227 m
[j
] = mod
[mod
[0] - j
];
229 /* Shift m left to make msb bit set */
230 for (mshift
= 0; mshift
< 15; mshift
++)
231 if ((m
[0] << mshift
) & 0x8000)
234 for (i
= 0; i
< mlen
- 1; i
++)
235 m
[i
] = (m
[i
] << mshift
) | (m
[i
+ 1] >> (16 - mshift
));
236 m
[mlen
- 1] = m
[mlen
- 1] << mshift
;
239 /* Allocate n of size mlen, copy base to n */
240 n
= smalloc(mlen
* sizeof(unsigned short));
242 for (j
= 0; j
< i
; j
++)
244 for (j
= 0; j
< base
[0]; j
++)
245 n
[i
+ j
] = base
[base
[0] - j
];
247 /* Allocate a and b of size 2*mlen. Set a = 1 */
248 a
= smalloc(2 * mlen
* sizeof(unsigned short));
249 b
= smalloc(2 * mlen
* sizeof(unsigned short));
250 for (i
= 0; i
< 2 * mlen
; i
++)
254 /* Skip leading zero bits of exp. */
257 while (i
< exp
[0] && (exp
[exp
[0] - i
] & (1 << j
)) == 0) {
265 /* Main computation */
268 internal_mul(a
+ mlen
, a
+ mlen
, b
, mlen
);
269 internal_mod(b
, mlen
* 2, m
, mlen
, NULL
, 0);
270 if ((exp
[exp
[0] - i
] & (1 << j
)) != 0) {
271 internal_mul(b
+ mlen
, n
, a
, mlen
);
272 internal_mod(a
, mlen
* 2, m
, mlen
, NULL
, 0);
285 /* Fixup result in case the modulus was shifted */
287 for (i
= mlen
- 1; i
< 2 * mlen
- 1; i
++)
288 a
[i
] = (a
[i
] << mshift
) | (a
[i
+ 1] >> (16 - mshift
));
289 a
[2 * mlen
- 1] = a
[2 * mlen
- 1] << mshift
;
290 internal_mod(a
, mlen
* 2, m
, mlen
, NULL
, 0);
291 for (i
= 2 * mlen
- 1; i
>= mlen
; i
--)
292 a
[i
] = (a
[i
] >> mshift
) | (a
[i
- 1] << (16 - mshift
));
295 /* Copy result to buffer */
296 result
= newbn(mod
[0]);
297 for (i
= 0; i
< mlen
; i
++)
298 result
[result
[0] - i
] = a
[i
+ mlen
];
299 while (result
[0] > 1 && result
[result
[0]] == 0)
302 /* Free temporary arrays */
303 for (i
= 0; i
< 2 * mlen
; i
++)
306 for (i
= 0; i
< 2 * mlen
; i
++)
309 for (i
= 0; i
< mlen
; i
++)
312 for (i
= 0; i
< mlen
; i
++)
320 * Compute (p * q) % mod.
321 * The most significant word of mod MUST be non-zero.
322 * We assume that the result array is the same size as the mod array.
324 Bignum
modmul(Bignum p
, Bignum q
, Bignum mod
)
326 unsigned short *a
, *n
, *m
, *o
;
328 int pqlen
, mlen
, rlen
, i
, j
;
331 /* Allocate m of size mlen, copy mod to m */
332 /* We use big endian internally */
334 m
= smalloc(mlen
* sizeof(unsigned short));
335 for (j
= 0; j
< mlen
; j
++)
336 m
[j
] = mod
[mod
[0] - j
];
338 /* Shift m left to make msb bit set */
339 for (mshift
= 0; mshift
< 15; mshift
++)
340 if ((m
[0] << mshift
) & 0x8000)
343 for (i
= 0; i
< mlen
- 1; i
++)
344 m
[i
] = (m
[i
] << mshift
) | (m
[i
+ 1] >> (16 - mshift
));
345 m
[mlen
- 1] = m
[mlen
- 1] << mshift
;
348 pqlen
= (p
[0] > q
[0] ? p
[0] : q
[0]);
350 /* Allocate n of size pqlen, copy p to n */
351 n
= smalloc(pqlen
* sizeof(unsigned short));
353 for (j
= 0; j
< i
; j
++)
355 for (j
= 0; j
< p
[0]; j
++)
356 n
[i
+ j
] = p
[p
[0] - j
];
358 /* Allocate o of size pqlen, copy q to o */
359 o
= smalloc(pqlen
* sizeof(unsigned short));
361 for (j
= 0; j
< i
; j
++)
363 for (j
= 0; j
< q
[0]; j
++)
364 o
[i
+ j
] = q
[q
[0] - j
];
366 /* Allocate a of size 2*pqlen for result */
367 a
= smalloc(2 * pqlen
* sizeof(unsigned short));
369 /* Main computation */
370 internal_mul(n
, o
, a
, pqlen
);
371 internal_mod(a
, pqlen
* 2, m
, mlen
, NULL
, 0);
373 /* Fixup result in case the modulus was shifted */
375 for (i
= 2 * pqlen
- mlen
- 1; i
< 2 * pqlen
- 1; i
++)
376 a
[i
] = (a
[i
] << mshift
) | (a
[i
+ 1] >> (16 - mshift
));
377 a
[2 * pqlen
- 1] = a
[2 * pqlen
- 1] << mshift
;
378 internal_mod(a
, pqlen
* 2, m
, mlen
, NULL
, 0);
379 for (i
= 2 * pqlen
- 1; i
>= 2 * pqlen
- mlen
; i
--)
380 a
[i
] = (a
[i
] >> mshift
) | (a
[i
- 1] << (16 - mshift
));
383 /* Copy result to buffer */
384 rlen
= (mlen
< pqlen
* 2 ? mlen
: pqlen
* 2);
385 result
= newbn(rlen
);
386 for (i
= 0; i
< rlen
; i
++)
387 result
[result
[0] - i
] = a
[i
+ 2 * pqlen
- rlen
];
388 while (result
[0] > 1 && result
[result
[0]] == 0)
391 /* Free temporary arrays */
392 for (i
= 0; i
< 2 * pqlen
; i
++)
395 for (i
= 0; i
< mlen
; i
++)
398 for (i
= 0; i
< pqlen
; i
++)
401 for (i
= 0; i
< pqlen
; i
++)
410 * The most significant word of mod MUST be non-zero.
411 * We assume that the result array is the same size as the mod array.
412 * We optionally write out a quotient.
414 void bigmod(Bignum p
, Bignum mod
, Bignum result
, Bignum quotient
)
416 unsigned short *n
, *m
;
418 int plen
, mlen
, i
, j
;
420 /* Allocate m of size mlen, copy mod to m */
421 /* We use big endian internally */
423 m
= smalloc(mlen
* sizeof(unsigned short));
424 for (j
= 0; j
< mlen
; j
++)
425 m
[j
] = mod
[mod
[0] - j
];
427 /* Shift m left to make msb bit set */
428 for (mshift
= 0; mshift
< 15; mshift
++)
429 if ((m
[0] << mshift
) & 0x8000)
432 for (i
= 0; i
< mlen
- 1; i
++)
433 m
[i
] = (m
[i
] << mshift
) | (m
[i
+ 1] >> (16 - mshift
));
434 m
[mlen
- 1] = m
[mlen
- 1] << mshift
;
438 /* Ensure plen > mlen */
442 /* Allocate n of size plen, copy p to n */
443 n
= smalloc(plen
* sizeof(unsigned short));
444 for (j
= 0; j
< plen
; j
++)
446 for (j
= 1; j
<= p
[0]; j
++)
449 /* Main computation */
450 internal_mod(n
, plen
, m
, mlen
, quotient
, mshift
);
452 /* Fixup result in case the modulus was shifted */
454 for (i
= plen
- mlen
- 1; i
< plen
- 1; i
++)
455 n
[i
] = (n
[i
] << mshift
) | (n
[i
+ 1] >> (16 - mshift
));
456 n
[plen
- 1] = n
[plen
- 1] << mshift
;
457 internal_mod(n
, plen
, m
, mlen
, quotient
, 0);
458 for (i
= plen
- 1; i
>= plen
- mlen
; i
--)
459 n
[i
] = (n
[i
] >> mshift
) | (n
[i
- 1] << (16 - mshift
));
462 /* Copy result to buffer */
463 for (i
= 1; i
<= result
[0]; i
++) {
465 result
[i
] = j
>= 0 ? n
[j
] : 0;
468 /* Free temporary arrays */
469 for (i
= 0; i
< mlen
; i
++)
472 for (i
= 0; i
< plen
; i
++)
478 * Decrement a number.
480 void decbn(Bignum bn
)
483 while (i
< bn
[0] && bn
[i
] == 0)
488 Bignum
bignum_from_bytes(unsigned char *data
, int nbytes
)
493 w
= (nbytes
+ 1) / 2; /* bytes -> words */
496 for (i
= 1; i
<= w
; i
++)
498 for (i
= nbytes
; i
--;) {
499 unsigned char byte
= *data
++;
501 result
[1 + i
/ 2] |= byte
<< 8;
503 result
[1 + i
/ 2] |= byte
;
506 while (result
[0] > 1 && result
[result
[0]] == 0)
512 * Read an ssh1-format bignum from a data buffer. Return the number
515 int ssh1_read_bignum(unsigned char *data
, Bignum
* result
)
517 unsigned char *p
= data
;
522 for (i
= 0; i
< 2; i
++)
524 b
= (w
+ 7) / 8; /* bits -> bytes */
526 if (!result
) /* just return length */
529 *result
= bignum_from_bytes(p
, b
);
535 * Return the bit count of a bignum, for ssh1 encoding.
537 int bignum_bitcount(Bignum bn
)
539 int bitcount
= bn
[0] * 16 - 1;
541 && (bn
[bitcount
/ 16 + 1] >> (bitcount
% 16)) == 0) bitcount
--;
546 * Return the byte length of a bignum when ssh1 encoded.
548 int ssh1_bignum_length(Bignum bn
)
550 return 2 + (bignum_bitcount(bn
) + 7) / 8;
554 * Return the byte length of a bignum when ssh2 encoded.
556 int ssh2_bignum_length(Bignum bn
)
558 return 4 + (bignum_bitcount(bn
) + 8) / 8;
562 * Return a byte from a bignum; 0 is least significant, etc.
564 int bignum_byte(Bignum bn
, int i
)
567 return 0; /* beyond the end */
569 return (bn
[i
/ 2 + 1] >> 8) & 0xFF;
571 return (bn
[i
/ 2 + 1]) & 0xFF;
575 * Return a bit from a bignum; 0 is least significant, etc.
577 int bignum_bit(Bignum bn
, int i
)
580 return 0; /* beyond the end */
582 return (bn
[i
/ 16 + 1] >> (i
% 16)) & 1;
586 * Set a bit in a bignum; 0 is least significant, etc.
588 void bignum_set_bit(Bignum bn
, int bitnum
, int value
)
590 if (bitnum
>= 16 * bn
[0])
591 abort(); /* beyond the end */
593 int v
= bitnum
/ 16 + 1;
594 int mask
= 1 << (bitnum
% 16);
603 * Write a ssh1-format bignum into a buffer. It is assumed the
604 * buffer is big enough. Returns the number of bytes used.
606 int ssh1_write_bignum(void *data
, Bignum bn
)
608 unsigned char *p
= data
;
609 int len
= ssh1_bignum_length(bn
);
611 int bitc
= bignum_bitcount(bn
);
613 *p
++ = (bitc
>> 8) & 0xFF;
614 *p
++ = (bitc
) & 0xFF;
615 for (i
= len
- 2; i
--;)
616 *p
++ = bignum_byte(bn
, i
);
621 * Compare two bignums. Returns like strcmp.
623 int bignum_cmp(Bignum a
, Bignum b
)
625 int amax
= a
[0], bmax
= b
[0];
626 int i
= (amax
> bmax ? amax
: bmax
);
628 unsigned short aval
= (i
> amax ?
0 : a
[i
]);
629 unsigned short bval
= (i
> bmax ?
0 : b
[i
]);
640 * Right-shift one bignum to form another.
642 Bignum
bignum_rshift(Bignum a
, int shift
)
645 int i
, shiftw
, shiftb
, shiftbb
, bits
;
646 unsigned short ai
, ai1
;
648 bits
= bignum_bitcount(a
) - shift
;
649 ret
= newbn((bits
+ 15) / 16);
654 shiftbb
= 16 - shiftb
;
657 for (i
= 1; i
<= ret
[0]; i
++) {
659 ai1
= (i
+ shiftw
+ 1 <= a
[0] ? a
[i
+ shiftw
+ 1] : 0);
660 ret
[i
] = ((ai
>> shiftb
) | (ai1
<< shiftbb
)) & 0xFFFF;
668 * Non-modular multiplication and addition.
670 Bignum
bigmuladd(Bignum a
, Bignum b
, Bignum addend
)
672 int alen
= a
[0], blen
= b
[0];
673 int mlen
= (alen
> blen ? alen
: blen
);
674 int rlen
, i
, maxspot
;
675 unsigned short *workspace
;
678 /* mlen space for a, mlen space for b, 2*mlen for result */
679 workspace
= smalloc(mlen
* 4 * sizeof(unsigned short));
680 for (i
= 0; i
< mlen
; i
++) {
681 workspace
[0 * mlen
+ i
] = (mlen
- i
<= a
[0] ? a
[mlen
- i
] : 0);
682 workspace
[1 * mlen
+ i
] = (mlen
- i
<= b
[0] ? b
[mlen
- i
] : 0);
685 internal_mul(workspace
+ 0 * mlen
, workspace
+ 1 * mlen
,
686 workspace
+ 2 * mlen
, mlen
);
688 /* now just copy the result back */
689 rlen
= alen
+ blen
+ 1;
690 if (addend
&& rlen
<= addend
[0])
691 rlen
= addend
[0] + 1;
694 for (i
= 1; i
<= ret
[0]; i
++) {
695 ret
[i
] = (i
<= 2 * mlen ? workspace
[4 * mlen
- i
] : 0);
701 /* now add in the addend, if any */
703 unsigned long carry
= 0;
704 for (i
= 1; i
<= rlen
; i
++) {
705 carry
+= (i
<= ret
[0] ? ret
[i
] : 0);
706 carry
+= (i
<= addend
[0] ? addend
[i
] : 0);
707 ret
[i
] = (unsigned short) carry
& 0xFFFF;
709 if (ret
[i
] != 0 && i
> maxspot
)
719 * Non-modular multiplication.
721 Bignum
bigmul(Bignum a
, Bignum b
)
723 return bigmuladd(a
, b
, NULL
);
727 * Create a bignum which is the bitmask covering another one. That
728 * is, the smallest integer which is >= N and is also one less than
731 Bignum
bignum_bitmask(Bignum n
)
733 Bignum ret
= copybn(n
);
738 while (n
[i
] == 0 && i
> 0)
741 return ret
; /* input was zero */
752 * Convert a (max 16-bit) short into a bignum.
754 Bignum
bignum_from_short(unsigned short n
)
760 ret
[2] = (n
>> 16) & 0xFFFF;
761 ret
[0] = (ret
[2] ?
2 : 1);
766 * Add a long to a bignum.
768 Bignum
bignum_add_long(Bignum number
, unsigned long addend
)
770 Bignum ret
= newbn(number
[0] + 1);
772 unsigned long carry
= 0;
774 for (i
= 1; i
<= ret
[0]; i
++) {
775 carry
+= addend
& 0xFFFF;
776 carry
+= (i
<= number
[0] ? number
[i
] : 0);
778 ret
[i
] = (unsigned short) carry
& 0xFFFF;
788 * Compute the residue of a bignum, modulo a (max 16-bit) short.
790 unsigned short bignum_mod_short(Bignum number
, unsigned short modulus
)
792 unsigned long mod
, r
;
797 for (i
= number
[0]; i
> 0; i
--)
798 r
= (r
* 65536 + number
[i
]) % mod
;
799 return (unsigned short) r
;
802 void diagbn(char *prefix
, Bignum md
)
804 int i
, nibbles
, morenibbles
;
805 static const char hex
[] = "0123456789ABCDEF";
807 debugprint(("%s0x", prefix ? prefix
: ""));
809 nibbles
= (3 + bignum_bitcount(md
)) / 4;
812 morenibbles
= 4 * md
[0] - nibbles
;
813 for (i
= 0; i
< morenibbles
; i
++)
815 for (i
= nibbles
; i
--;)
818 hex
[(bignum_byte(md
, i
/ 2) >> (4 * (i
% 2))) & 0xF]));
825 * Greatest common divisor.
827 Bignum
biggcd(Bignum av
, Bignum bv
)
829 Bignum a
= copybn(av
);
830 Bignum b
= copybn(bv
);
834 while (bignum_cmp(b
, Zero
) != 0) {
835 Bignum t
= newbn(b
[0]);
836 bigmod(a
, b
, t
, NULL
);
838 while (t
[0] > 1 && t
[t
[0]] == 0)
850 * Modular inverse, using Euclid's extended algorithm.
852 Bignum
modinv(Bignum number
, Bignum modulus
)
854 Bignum a
= copybn(modulus
);
855 Bignum b
= copybn(number
);
856 Bignum xp
= copybn(Zero
);
857 Bignum x
= copybn(One
);
860 while (bignum_cmp(b
, One
) != 0) {
861 Bignum t
= newbn(b
[0]);
862 Bignum q
= newbn(a
[0]);
864 while (t
[0] > 1 && t
[t
[0]] == 0)
871 x
= bigmuladd(q
, xp
, t
);
880 /* now we know that sign * x == 1, and that x < modulus */
882 /* set a new x to be modulus - x */
883 Bignum newx
= newbn(modulus
[0]);
884 unsigned short carry
= 0;
888 for (i
= 1; i
<= newx
[0]; i
++) {
889 unsigned short aword
= (i
<= modulus
[0] ? modulus
[i
] : 0);
890 unsigned short bword
= (i
<= x
[0] ? x
[i
] : 0);
891 newx
[i
] = aword
- bword
- carry
;
893 carry
= carry ?
(newx
[i
] >= bword
) : (newx
[i
] > bword
);
907 * Render a bignum into decimal. Return a malloced string holding
908 * the decimal representation.
910 char *bignum_decimal(Bignum x
)
916 unsigned short *workspace
;
919 * First, estimate the number of digits. Since log(10)/log(2)
920 * is just greater than 93/28 (the joys of continued fraction
921 * approximations...) we know that for every 93 bits, we need
922 * at most 28 digits. This will tell us how much to malloc.
924 * Formally: if x has i bits, that means x is strictly less
925 * than 2^i. Since 2 is less than 10^(28/93), this is less than
926 * 10^(28i/93). We need an integer power of ten, so we must
927 * round up (rounding down might make it less than x again).
928 * Therefore if we multiply the bit count by 28/93, rounding
929 * up, we will have enough digits.
931 i
= bignum_bitcount(x
);
932 ndigits
= (28 * i
+ 92) / 93; /* multiply by 28/93 and round up */
933 ndigits
++; /* allow for trailing \0 */
934 ret
= smalloc(ndigits
);
937 * Now allocate some workspace to hold the binary form as we
938 * repeatedly divide it by ten. Initialise this to the
939 * big-endian form of the number.
941 workspace
= smalloc(sizeof(unsigned short) * x
[0]);
942 for (i
= 0; i
< x
[0]; i
++)
943 workspace
[i
] = x
[x
[0] - i
];
946 * Next, write the decimal number starting with the last digit.
947 * We use ordinary short division, dividing 10 into the
950 ndigit
= ndigits
- 1;
955 for (i
= 0; i
< x
[0]; i
++) {
956 carry
= (carry
<< 16) + workspace
[i
];
957 workspace
[i
] = (unsigned short) (carry
/ 10);
962 ret
[--ndigit
] = (char) (carry
+ '0');
966 * There's a chance we've fallen short of the start of the
967 * string. Correct if so.
970 memmove(ret
, ret
+ ndigit
, ndigits
- ndigit
);