e5574168 |
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 | |
5c72ca61 |
9 | #include "misc.h" |
98ba26b9 |
10 | |
a3412f52 |
11 | #if defined __GNUC__ && defined __i386__ |
12 | typedef unsigned long BignumInt; |
13 | typedef unsigned long long BignumDblInt; |
14 | #define BIGNUM_INT_MASK 0xFFFFFFFFUL |
15 | #define BIGNUM_TOP_BIT 0x80000000UL |
16 | #define BIGNUM_INT_BITS 32 |
17 | #define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2) |
a47e8bba |
18 | #define DIVMOD_WORD(q, r, hi, lo, w) \ |
19 | __asm__("div %2" : \ |
20 | "=d" (r), "=a" (q) : \ |
21 | "r" (w), "d" (hi), "a" (lo)) |
a3412f52 |
22 | #else |
23 | typedef unsigned short BignumInt; |
24 | typedef unsigned long BignumDblInt; |
25 | #define BIGNUM_INT_MASK 0xFFFFU |
26 | #define BIGNUM_TOP_BIT 0x8000U |
27 | #define BIGNUM_INT_BITS 16 |
28 | #define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2) |
a47e8bba |
29 | #define DIVMOD_WORD(q, r, hi, lo, w) do { \ |
30 | BignumDblInt n = (((BignumDblInt)hi) << BIGNUM_INT_BITS) | lo; \ |
31 | q = n / w; \ |
32 | r = n % w; \ |
33 | } while (0) |
a3412f52 |
34 | #endif |
35 | |
36 | #define BIGNUM_INT_BYTES (BIGNUM_INT_BITS / 8) |
37 | |
3709bfe9 |
38 | #define BIGNUM_INTERNAL |
a3412f52 |
39 | typedef BignumInt *Bignum; |
3709bfe9 |
40 | |
e5574168 |
41 | #include "ssh.h" |
42 | |
a3412f52 |
43 | BignumInt bnZero[1] = { 0 }; |
44 | BignumInt bnOne[2] = { 1, 1 }; |
e5574168 |
45 | |
7d6ee6ff |
46 | /* |
a3412f52 |
47 | * The Bignum format is an array of `BignumInt'. The first |
7d6ee6ff |
48 | * element of the array counts the remaining elements. The |
a3412f52 |
49 | * remaining elements express the actual number, base 2^BIGNUM_INT_BITS, _least_ |
7d6ee6ff |
50 | * significant digit first. (So it's trivial to extract the bit |
51 | * with value 2^n for any n.) |
52 | * |
53 | * All Bignums in this module are positive. Negative numbers must |
54 | * be dealt with outside it. |
55 | * |
56 | * INVARIANT: the most significant word of any Bignum must be |
57 | * nonzero. |
58 | */ |
59 | |
7cca0d81 |
60 | Bignum Zero = bnZero, One = bnOne; |
e5574168 |
61 | |
32874aea |
62 | static Bignum newbn(int length) |
63 | { |
a3412f52 |
64 | Bignum b = snewn(length + 1, BignumInt); |
e5574168 |
65 | if (!b) |
66 | abort(); /* FIXME */ |
32874aea |
67 | memset(b, 0, (length + 1) * sizeof(*b)); |
e5574168 |
68 | b[0] = length; |
69 | return b; |
70 | } |
71 | |
32874aea |
72 | void bn_restore_invariant(Bignum b) |
73 | { |
74 | while (b[0] > 1 && b[b[0]] == 0) |
75 | b[0]--; |
3709bfe9 |
76 | } |
77 | |
32874aea |
78 | Bignum copybn(Bignum orig) |
79 | { |
a3412f52 |
80 | Bignum b = snewn(orig[0] + 1, BignumInt); |
7cca0d81 |
81 | if (!b) |
82 | abort(); /* FIXME */ |
32874aea |
83 | memcpy(b, orig, (orig[0] + 1) * sizeof(*b)); |
7cca0d81 |
84 | return b; |
85 | } |
86 | |
32874aea |
87 | void freebn(Bignum b) |
88 | { |
e5574168 |
89 | /* |
90 | * Burn the evidence, just in case. |
91 | */ |
92 | memset(b, 0, sizeof(b[0]) * (b[0] + 1)); |
dcbde236 |
93 | sfree(b); |
e5574168 |
94 | } |
95 | |
32874aea |
96 | Bignum bn_power_2(int n) |
97 | { |
a3412f52 |
98 | Bignum ret = newbn(n / BIGNUM_INT_BITS + 1); |
3709bfe9 |
99 | bignum_set_bit(ret, n, 1); |
100 | return ret; |
101 | } |
102 | |
e5574168 |
103 | /* |
104 | * Compute c = a * b. |
105 | * Input is in the first len words of a and b. |
106 | * Result is returned in the first 2*len words of c. |
107 | */ |
a3412f52 |
108 | static void internal_mul(BignumInt *a, BignumInt *b, |
109 | BignumInt *c, int len) |
e5574168 |
110 | { |
111 | int i, j; |
a3412f52 |
112 | BignumDblInt t; |
e5574168 |
113 | |
32874aea |
114 | for (j = 0; j < 2 * len; j++) |
9400cf6f |
115 | c[j] = 0; |
e5574168 |
116 | |
117 | for (i = len - 1; i >= 0; i--) { |
e5574168 |
118 | t = 0; |
119 | for (j = len - 1; j >= 0; j--) { |
a3412f52 |
120 | t += MUL_WORD(a[i], (BignumDblInt) b[j]); |
121 | t += (BignumDblInt) c[i + j + 1]; |
122 | c[i + j + 1] = (BignumInt) t; |
123 | t = t >> BIGNUM_INT_BITS; |
e5574168 |
124 | } |
a3412f52 |
125 | c[i] = (BignumInt) t; |
e5574168 |
126 | } |
127 | } |
128 | |
a3412f52 |
129 | static void internal_add_shifted(BignumInt *number, |
32874aea |
130 | unsigned n, int shift) |
131 | { |
a3412f52 |
132 | int word = 1 + (shift / BIGNUM_INT_BITS); |
133 | int bshift = shift % BIGNUM_INT_BITS; |
134 | BignumDblInt addend; |
9400cf6f |
135 | |
136 | addend = n << bshift; |
137 | |
138 | while (addend) { |
32874aea |
139 | addend += number[word]; |
a3412f52 |
140 | number[word] = (BignumInt) addend & BIGNUM_INT_MASK; |
141 | addend >>= BIGNUM_INT_BITS; |
32874aea |
142 | word++; |
9400cf6f |
143 | } |
144 | } |
145 | |
e5574168 |
146 | /* |
147 | * Compute a = a % m. |
9400cf6f |
148 | * Input in first alen words of a and first mlen words of m. |
149 | * Output in first alen words of a |
150 | * (of which first alen-mlen words will be zero). |
e5574168 |
151 | * The MSW of m MUST have its high bit set. |
9400cf6f |
152 | * Quotient is accumulated in the `quotient' array, which is a Bignum |
153 | * rather than the internal bigendian format. Quotient parts are shifted |
154 | * left by `qshift' before adding into quot. |
e5574168 |
155 | */ |
a3412f52 |
156 | static void internal_mod(BignumInt *a, int alen, |
157 | BignumInt *m, int mlen, |
158 | BignumInt *quot, int qshift) |
e5574168 |
159 | { |
a3412f52 |
160 | BignumInt m0, m1; |
e5574168 |
161 | unsigned int h; |
162 | int i, k; |
163 | |
e5574168 |
164 | m0 = m[0]; |
9400cf6f |
165 | if (mlen > 1) |
32874aea |
166 | m1 = m[1]; |
9400cf6f |
167 | else |
32874aea |
168 | m1 = 0; |
e5574168 |
169 | |
32874aea |
170 | for (i = 0; i <= alen - mlen; i++) { |
a3412f52 |
171 | BignumDblInt t; |
9400cf6f |
172 | unsigned int q, r, c, ai1; |
e5574168 |
173 | |
174 | if (i == 0) { |
175 | h = 0; |
176 | } else { |
32874aea |
177 | h = a[i - 1]; |
178 | a[i - 1] = 0; |
e5574168 |
179 | } |
180 | |
32874aea |
181 | if (i == alen - 1) |
182 | ai1 = 0; |
183 | else |
184 | ai1 = a[i + 1]; |
9400cf6f |
185 | |
e5574168 |
186 | /* Find q = h:a[i] / m0 */ |
a47e8bba |
187 | DIVMOD_WORD(q, r, h, a[i], m0); |
e5574168 |
188 | |
189 | /* Refine our estimate of q by looking at |
32874aea |
190 | h:a[i]:a[i+1] / m0:m1 */ |
a47e8bba |
191 | t = MUL_WORD(m1, q); |
a3412f52 |
192 | if (t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) { |
e5574168 |
193 | q--; |
194 | t -= m1; |
a3412f52 |
195 | r = (r + m0) & BIGNUM_INT_MASK; /* overflow? */ |
196 | if (r >= (BignumDblInt) m0 && |
197 | t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) q--; |
e5574168 |
198 | } |
199 | |
9400cf6f |
200 | /* Subtract q * m from a[i...] */ |
e5574168 |
201 | c = 0; |
9400cf6f |
202 | for (k = mlen - 1; k >= 0; k--) { |
a47e8bba |
203 | t = MUL_WORD(q, m[k]); |
e5574168 |
204 | t += c; |
a3412f52 |
205 | c = t >> BIGNUM_INT_BITS; |
206 | if ((BignumInt) t > a[i + k]) |
32874aea |
207 | c++; |
a3412f52 |
208 | a[i + k] -= (BignumInt) t; |
e5574168 |
209 | } |
210 | |
211 | /* Add back m in case of borrow */ |
212 | if (c != h) { |
213 | t = 0; |
9400cf6f |
214 | for (k = mlen - 1; k >= 0; k--) { |
e5574168 |
215 | t += m[k]; |
32874aea |
216 | t += a[i + k]; |
a3412f52 |
217 | a[i + k] = (BignumInt) t; |
218 | t = t >> BIGNUM_INT_BITS; |
e5574168 |
219 | } |
32874aea |
220 | q--; |
e5574168 |
221 | } |
32874aea |
222 | if (quot) |
a3412f52 |
223 | internal_add_shifted(quot, q, qshift + BIGNUM_INT_BITS * (alen - mlen - i)); |
e5574168 |
224 | } |
225 | } |
226 | |
227 | /* |
228 | * Compute (base ^ exp) % mod. |
229 | * The base MUST be smaller than the modulus. |
230 | * The most significant word of mod MUST be non-zero. |
231 | * We assume that the result array is the same size as the mod array. |
232 | */ |
59600f67 |
233 | Bignum modpow(Bignum base, Bignum exp, Bignum mod) |
e5574168 |
234 | { |
a3412f52 |
235 | BignumInt *a, *b, *n, *m; |
e5574168 |
236 | int mshift; |
237 | int mlen, i, j; |
59600f67 |
238 | Bignum result; |
e5574168 |
239 | |
240 | /* Allocate m of size mlen, copy mod to m */ |
241 | /* We use big endian internally */ |
242 | mlen = mod[0]; |
a3412f52 |
243 | m = snewn(mlen, BignumInt); |
32874aea |
244 | for (j = 0; j < mlen; j++) |
245 | m[j] = mod[mod[0] - j]; |
e5574168 |
246 | |
247 | /* Shift m left to make msb bit set */ |
a3412f52 |
248 | for (mshift = 0; mshift < BIGNUM_INT_BITS-1; mshift++) |
249 | if ((m[0] << mshift) & BIGNUM_TOP_BIT) |
32874aea |
250 | break; |
e5574168 |
251 | if (mshift) { |
252 | for (i = 0; i < mlen - 1; i++) |
a3412f52 |
253 | m[i] = (m[i] << mshift) | (m[i + 1] >> (BIGNUM_INT_BITS - mshift)); |
32874aea |
254 | m[mlen - 1] = m[mlen - 1] << mshift; |
e5574168 |
255 | } |
256 | |
257 | /* Allocate n of size mlen, copy base to n */ |
a3412f52 |
258 | n = snewn(mlen, BignumInt); |
e5574168 |
259 | i = mlen - base[0]; |
32874aea |
260 | for (j = 0; j < i; j++) |
261 | n[j] = 0; |
262 | for (j = 0; j < base[0]; j++) |
263 | n[i + j] = base[base[0] - j]; |
e5574168 |
264 | |
265 | /* Allocate a and b of size 2*mlen. Set a = 1 */ |
a3412f52 |
266 | a = snewn(2 * mlen, BignumInt); |
267 | b = snewn(2 * mlen, BignumInt); |
32874aea |
268 | for (i = 0; i < 2 * mlen; i++) |
269 | a[i] = 0; |
270 | a[2 * mlen - 1] = 1; |
e5574168 |
271 | |
272 | /* Skip leading zero bits of exp. */ |
32874aea |
273 | i = 0; |
a3412f52 |
274 | j = BIGNUM_INT_BITS-1; |
e5574168 |
275 | while (i < exp[0] && (exp[exp[0] - i] & (1 << j)) == 0) { |
276 | j--; |
32874aea |
277 | if (j < 0) { |
278 | i++; |
a3412f52 |
279 | j = BIGNUM_INT_BITS-1; |
32874aea |
280 | } |
e5574168 |
281 | } |
282 | |
283 | /* Main computation */ |
284 | while (i < exp[0]) { |
285 | while (j >= 0) { |
9400cf6f |
286 | internal_mul(a + mlen, a + mlen, b, mlen); |
32874aea |
287 | internal_mod(b, mlen * 2, m, mlen, NULL, 0); |
e5574168 |
288 | if ((exp[exp[0] - i] & (1 << j)) != 0) { |
9400cf6f |
289 | internal_mul(b + mlen, n, a, mlen); |
32874aea |
290 | internal_mod(a, mlen * 2, m, mlen, NULL, 0); |
e5574168 |
291 | } else { |
a3412f52 |
292 | BignumInt *t; |
32874aea |
293 | t = a; |
294 | a = b; |
295 | b = t; |
e5574168 |
296 | } |
297 | j--; |
298 | } |
32874aea |
299 | i++; |
a3412f52 |
300 | j = BIGNUM_INT_BITS-1; |
e5574168 |
301 | } |
302 | |
303 | /* Fixup result in case the modulus was shifted */ |
304 | if (mshift) { |
32874aea |
305 | for (i = mlen - 1; i < 2 * mlen - 1; i++) |
a3412f52 |
306 | a[i] = (a[i] << mshift) | (a[i + 1] >> (BIGNUM_INT_BITS - mshift)); |
32874aea |
307 | a[2 * mlen - 1] = a[2 * mlen - 1] << mshift; |
308 | internal_mod(a, mlen * 2, m, mlen, NULL, 0); |
309 | for (i = 2 * mlen - 1; i >= mlen; i--) |
a3412f52 |
310 | a[i] = (a[i] >> mshift) | (a[i - 1] << (BIGNUM_INT_BITS - mshift)); |
e5574168 |
311 | } |
312 | |
313 | /* Copy result to buffer */ |
59600f67 |
314 | result = newbn(mod[0]); |
e5574168 |
315 | for (i = 0; i < mlen; i++) |
32874aea |
316 | result[result[0] - i] = a[i + mlen]; |
317 | while (result[0] > 1 && result[result[0]] == 0) |
318 | result[0]--; |
e5574168 |
319 | |
320 | /* Free temporary arrays */ |
32874aea |
321 | for (i = 0; i < 2 * mlen; i++) |
322 | a[i] = 0; |
323 | sfree(a); |
324 | for (i = 0; i < 2 * mlen; i++) |
325 | b[i] = 0; |
326 | sfree(b); |
327 | for (i = 0; i < mlen; i++) |
328 | m[i] = 0; |
329 | sfree(m); |
330 | for (i = 0; i < mlen; i++) |
331 | n[i] = 0; |
332 | sfree(n); |
59600f67 |
333 | |
334 | return result; |
e5574168 |
335 | } |
7cca0d81 |
336 | |
337 | /* |
338 | * Compute (p * q) % mod. |
339 | * The most significant word of mod MUST be non-zero. |
340 | * We assume that the result array is the same size as the mod array. |
341 | */ |
59600f67 |
342 | Bignum modmul(Bignum p, Bignum q, Bignum mod) |
7cca0d81 |
343 | { |
a3412f52 |
344 | BignumInt *a, *n, *m, *o; |
7cca0d81 |
345 | int mshift; |
80b10571 |
346 | int pqlen, mlen, rlen, i, j; |
59600f67 |
347 | Bignum result; |
7cca0d81 |
348 | |
349 | /* Allocate m of size mlen, copy mod to m */ |
350 | /* We use big endian internally */ |
351 | mlen = mod[0]; |
a3412f52 |
352 | m = snewn(mlen, BignumInt); |
32874aea |
353 | for (j = 0; j < mlen; j++) |
354 | m[j] = mod[mod[0] - j]; |
7cca0d81 |
355 | |
356 | /* Shift m left to make msb bit set */ |
a3412f52 |
357 | for (mshift = 0; mshift < BIGNUM_INT_BITS-1; mshift++) |
358 | if ((m[0] << mshift) & BIGNUM_TOP_BIT) |
32874aea |
359 | break; |
7cca0d81 |
360 | if (mshift) { |
361 | for (i = 0; i < mlen - 1; i++) |
a3412f52 |
362 | m[i] = (m[i] << mshift) | (m[i + 1] >> (BIGNUM_INT_BITS - mshift)); |
32874aea |
363 | m[mlen - 1] = m[mlen - 1] << mshift; |
7cca0d81 |
364 | } |
365 | |
366 | pqlen = (p[0] > q[0] ? p[0] : q[0]); |
367 | |
368 | /* Allocate n of size pqlen, copy p to n */ |
a3412f52 |
369 | n = snewn(pqlen, BignumInt); |
7cca0d81 |
370 | i = pqlen - p[0]; |
32874aea |
371 | for (j = 0; j < i; j++) |
372 | n[j] = 0; |
373 | for (j = 0; j < p[0]; j++) |
374 | n[i + j] = p[p[0] - j]; |
7cca0d81 |
375 | |
376 | /* Allocate o of size pqlen, copy q to o */ |
a3412f52 |
377 | o = snewn(pqlen, BignumInt); |
7cca0d81 |
378 | i = pqlen - q[0]; |
32874aea |
379 | for (j = 0; j < i; j++) |
380 | o[j] = 0; |
381 | for (j = 0; j < q[0]; j++) |
382 | o[i + j] = q[q[0] - j]; |
7cca0d81 |
383 | |
384 | /* Allocate a of size 2*pqlen for result */ |
a3412f52 |
385 | a = snewn(2 * pqlen, BignumInt); |
7cca0d81 |
386 | |
387 | /* Main computation */ |
9400cf6f |
388 | internal_mul(n, o, a, pqlen); |
32874aea |
389 | internal_mod(a, pqlen * 2, m, mlen, NULL, 0); |
7cca0d81 |
390 | |
391 | /* Fixup result in case the modulus was shifted */ |
392 | if (mshift) { |
32874aea |
393 | for (i = 2 * pqlen - mlen - 1; i < 2 * pqlen - 1; i++) |
a3412f52 |
394 | a[i] = (a[i] << mshift) | (a[i + 1] >> (BIGNUM_INT_BITS - mshift)); |
32874aea |
395 | a[2 * pqlen - 1] = a[2 * pqlen - 1] << mshift; |
396 | internal_mod(a, pqlen * 2, m, mlen, NULL, 0); |
397 | for (i = 2 * pqlen - 1; i >= 2 * pqlen - mlen; i--) |
a3412f52 |
398 | a[i] = (a[i] >> mshift) | (a[i - 1] << (BIGNUM_INT_BITS - mshift)); |
7cca0d81 |
399 | } |
400 | |
401 | /* Copy result to buffer */ |
32874aea |
402 | rlen = (mlen < pqlen * 2 ? mlen : pqlen * 2); |
80b10571 |
403 | result = newbn(rlen); |
404 | for (i = 0; i < rlen; i++) |
32874aea |
405 | result[result[0] - i] = a[i + 2 * pqlen - rlen]; |
406 | while (result[0] > 1 && result[result[0]] == 0) |
407 | result[0]--; |
7cca0d81 |
408 | |
409 | /* Free temporary arrays */ |
32874aea |
410 | for (i = 0; i < 2 * pqlen; i++) |
411 | a[i] = 0; |
412 | sfree(a); |
413 | for (i = 0; i < mlen; i++) |
414 | m[i] = 0; |
415 | sfree(m); |
416 | for (i = 0; i < pqlen; i++) |
417 | n[i] = 0; |
418 | sfree(n); |
419 | for (i = 0; i < pqlen; i++) |
420 | o[i] = 0; |
421 | sfree(o); |
59600f67 |
422 | |
423 | return result; |
7cca0d81 |
424 | } |
425 | |
426 | /* |
9400cf6f |
427 | * Compute p % mod. |
428 | * The most significant word of mod MUST be non-zero. |
429 | * We assume that the result array is the same size as the mod array. |
5c72ca61 |
430 | * We optionally write out a quotient if `quotient' is non-NULL. |
431 | * We can avoid writing out the result if `result' is NULL. |
9400cf6f |
432 | */ |
f28753ab |
433 | static void bigdivmod(Bignum p, Bignum mod, Bignum result, Bignum quotient) |
9400cf6f |
434 | { |
a3412f52 |
435 | BignumInt *n, *m; |
9400cf6f |
436 | int mshift; |
437 | int plen, mlen, i, j; |
438 | |
439 | /* Allocate m of size mlen, copy mod to m */ |
440 | /* We use big endian internally */ |
441 | mlen = mod[0]; |
a3412f52 |
442 | m = snewn(mlen, BignumInt); |
32874aea |
443 | for (j = 0; j < mlen; j++) |
444 | m[j] = mod[mod[0] - j]; |
9400cf6f |
445 | |
446 | /* Shift m left to make msb bit set */ |
a3412f52 |
447 | for (mshift = 0; mshift < BIGNUM_INT_BITS-1; mshift++) |
448 | if ((m[0] << mshift) & BIGNUM_TOP_BIT) |
32874aea |
449 | break; |
9400cf6f |
450 | if (mshift) { |
451 | for (i = 0; i < mlen - 1; i++) |
a3412f52 |
452 | m[i] = (m[i] << mshift) | (m[i + 1] >> (BIGNUM_INT_BITS - mshift)); |
32874aea |
453 | m[mlen - 1] = m[mlen - 1] << mshift; |
9400cf6f |
454 | } |
455 | |
456 | plen = p[0]; |
457 | /* Ensure plen > mlen */ |
32874aea |
458 | if (plen <= mlen) |
459 | plen = mlen + 1; |
9400cf6f |
460 | |
461 | /* Allocate n of size plen, copy p to n */ |
a3412f52 |
462 | n = snewn(plen, BignumInt); |
32874aea |
463 | for (j = 0; j < plen; j++) |
464 | n[j] = 0; |
465 | for (j = 1; j <= p[0]; j++) |
466 | n[plen - j] = p[j]; |
9400cf6f |
467 | |
468 | /* Main computation */ |
469 | internal_mod(n, plen, m, mlen, quotient, mshift); |
470 | |
471 | /* Fixup result in case the modulus was shifted */ |
472 | if (mshift) { |
473 | for (i = plen - mlen - 1; i < plen - 1; i++) |
a3412f52 |
474 | n[i] = (n[i] << mshift) | (n[i + 1] >> (BIGNUM_INT_BITS - mshift)); |
32874aea |
475 | n[plen - 1] = n[plen - 1] << mshift; |
9400cf6f |
476 | internal_mod(n, plen, m, mlen, quotient, 0); |
477 | for (i = plen - 1; i >= plen - mlen; i--) |
a3412f52 |
478 | n[i] = (n[i] >> mshift) | (n[i - 1] << (BIGNUM_INT_BITS - mshift)); |
9400cf6f |
479 | } |
480 | |
481 | /* Copy result to buffer */ |
5c72ca61 |
482 | if (result) { |
483 | for (i = 1; i <= result[0]; i++) { |
484 | int j = plen - i; |
485 | result[i] = j >= 0 ? n[j] : 0; |
486 | } |
9400cf6f |
487 | } |
488 | |
489 | /* Free temporary arrays */ |
32874aea |
490 | for (i = 0; i < mlen; i++) |
491 | m[i] = 0; |
492 | sfree(m); |
493 | for (i = 0; i < plen; i++) |
494 | n[i] = 0; |
495 | sfree(n); |
9400cf6f |
496 | } |
497 | |
498 | /* |
7cca0d81 |
499 | * Decrement a number. |
500 | */ |
32874aea |
501 | void decbn(Bignum bn) |
502 | { |
7cca0d81 |
503 | int i = 1; |
504 | while (i < bn[0] && bn[i] == 0) |
a3412f52 |
505 | bn[i++] = BIGNUM_INT_MASK; |
7cca0d81 |
506 | bn[i]--; |
507 | } |
508 | |
27cd7fc2 |
509 | Bignum bignum_from_bytes(const unsigned char *data, int nbytes) |
32874aea |
510 | { |
3709bfe9 |
511 | Bignum result; |
512 | int w, i; |
513 | |
a3412f52 |
514 | w = (nbytes + BIGNUM_INT_BYTES - 1) / BIGNUM_INT_BYTES; /* bytes->words */ |
3709bfe9 |
515 | |
516 | result = newbn(w); |
32874aea |
517 | for (i = 1; i <= w; i++) |
518 | result[i] = 0; |
519 | for (i = nbytes; i--;) { |
520 | unsigned char byte = *data++; |
a3412f52 |
521 | result[1 + i / BIGNUM_INT_BYTES] |= byte << (8*i % BIGNUM_INT_BITS); |
3709bfe9 |
522 | } |
523 | |
32874aea |
524 | while (result[0] > 1 && result[result[0]] == 0) |
525 | result[0]--; |
3709bfe9 |
526 | return result; |
527 | } |
528 | |
7cca0d81 |
529 | /* |
530 | * Read an ssh1-format bignum from a data buffer. Return the number |
531 | * of bytes consumed. |
532 | */ |
27cd7fc2 |
533 | int ssh1_read_bignum(const unsigned char *data, Bignum * result) |
32874aea |
534 | { |
27cd7fc2 |
535 | const unsigned char *p = data; |
7cca0d81 |
536 | int i; |
537 | int w, b; |
538 | |
539 | w = 0; |
32874aea |
540 | for (i = 0; i < 2; i++) |
541 | w = (w << 8) + *p++; |
542 | b = (w + 7) / 8; /* bits -> bytes */ |
7cca0d81 |
543 | |
32874aea |
544 | if (!result) /* just return length */ |
545 | return b + 2; |
a52f067e |
546 | |
3709bfe9 |
547 | *result = bignum_from_bytes(p, b); |
7cca0d81 |
548 | |
3709bfe9 |
549 | return p + b - data; |
7cca0d81 |
550 | } |
5c58ad2d |
551 | |
552 | /* |
553 | * Return the bit count of a bignum, for ssh1 encoding. |
554 | */ |
32874aea |
555 | int bignum_bitcount(Bignum bn) |
556 | { |
a3412f52 |
557 | int bitcount = bn[0] * BIGNUM_INT_BITS - 1; |
32874aea |
558 | while (bitcount >= 0 |
a3412f52 |
559 | && (bn[bitcount / BIGNUM_INT_BITS + 1] >> (bitcount % BIGNUM_INT_BITS)) == 0) bitcount--; |
5c58ad2d |
560 | return bitcount + 1; |
561 | } |
562 | |
563 | /* |
564 | * Return the byte length of a bignum when ssh1 encoded. |
565 | */ |
32874aea |
566 | int ssh1_bignum_length(Bignum bn) |
567 | { |
568 | return 2 + (bignum_bitcount(bn) + 7) / 8; |
ddecd643 |
569 | } |
570 | |
571 | /* |
572 | * Return the byte length of a bignum when ssh2 encoded. |
573 | */ |
32874aea |
574 | int ssh2_bignum_length(Bignum bn) |
575 | { |
576 | return 4 + (bignum_bitcount(bn) + 8) / 8; |
5c58ad2d |
577 | } |
578 | |
579 | /* |
580 | * Return a byte from a bignum; 0 is least significant, etc. |
581 | */ |
32874aea |
582 | int bignum_byte(Bignum bn, int i) |
583 | { |
a3412f52 |
584 | if (i >= BIGNUM_INT_BYTES * bn[0]) |
32874aea |
585 | return 0; /* beyond the end */ |
5c58ad2d |
586 | else |
a3412f52 |
587 | return (bn[i / BIGNUM_INT_BYTES + 1] >> |
588 | ((i % BIGNUM_INT_BYTES)*8)) & 0xFF; |
5c58ad2d |
589 | } |
590 | |
591 | /* |
9400cf6f |
592 | * Return a bit from a bignum; 0 is least significant, etc. |
593 | */ |
32874aea |
594 | int bignum_bit(Bignum bn, int i) |
595 | { |
a3412f52 |
596 | if (i >= BIGNUM_INT_BITS * bn[0]) |
32874aea |
597 | return 0; /* beyond the end */ |
9400cf6f |
598 | else |
a3412f52 |
599 | return (bn[i / BIGNUM_INT_BITS + 1] >> (i % BIGNUM_INT_BITS)) & 1; |
9400cf6f |
600 | } |
601 | |
602 | /* |
603 | * Set a bit in a bignum; 0 is least significant, etc. |
604 | */ |
32874aea |
605 | void bignum_set_bit(Bignum bn, int bitnum, int value) |
606 | { |
a3412f52 |
607 | if (bitnum >= BIGNUM_INT_BITS * bn[0]) |
32874aea |
608 | abort(); /* beyond the end */ |
9400cf6f |
609 | else { |
a3412f52 |
610 | int v = bitnum / BIGNUM_INT_BITS + 1; |
611 | int mask = 1 << (bitnum % BIGNUM_INT_BITS); |
32874aea |
612 | if (value) |
613 | bn[v] |= mask; |
614 | else |
615 | bn[v] &= ~mask; |
9400cf6f |
616 | } |
617 | } |
618 | |
619 | /* |
5c58ad2d |
620 | * Write a ssh1-format bignum into a buffer. It is assumed the |
621 | * buffer is big enough. Returns the number of bytes used. |
622 | */ |
32874aea |
623 | int ssh1_write_bignum(void *data, Bignum bn) |
624 | { |
5c58ad2d |
625 | unsigned char *p = data; |
626 | int len = ssh1_bignum_length(bn); |
627 | int i; |
ddecd643 |
628 | int bitc = bignum_bitcount(bn); |
5c58ad2d |
629 | |
630 | *p++ = (bitc >> 8) & 0xFF; |
32874aea |
631 | *p++ = (bitc) & 0xFF; |
632 | for (i = len - 2; i--;) |
633 | *p++ = bignum_byte(bn, i); |
5c58ad2d |
634 | return len; |
635 | } |
9400cf6f |
636 | |
637 | /* |
638 | * Compare two bignums. Returns like strcmp. |
639 | */ |
32874aea |
640 | int bignum_cmp(Bignum a, Bignum b) |
641 | { |
9400cf6f |
642 | int amax = a[0], bmax = b[0]; |
643 | int i = (amax > bmax ? amax : bmax); |
644 | while (i) { |
a3412f52 |
645 | BignumInt aval = (i > amax ? 0 : a[i]); |
646 | BignumInt bval = (i > bmax ? 0 : b[i]); |
32874aea |
647 | if (aval < bval) |
648 | return -1; |
649 | if (aval > bval) |
650 | return +1; |
651 | i--; |
9400cf6f |
652 | } |
653 | return 0; |
654 | } |
655 | |
656 | /* |
657 | * Right-shift one bignum to form another. |
658 | */ |
32874aea |
659 | Bignum bignum_rshift(Bignum a, int shift) |
660 | { |
9400cf6f |
661 | Bignum ret; |
662 | int i, shiftw, shiftb, shiftbb, bits; |
a3412f52 |
663 | BignumInt ai, ai1; |
9400cf6f |
664 | |
ddecd643 |
665 | bits = bignum_bitcount(a) - shift; |
a3412f52 |
666 | ret = newbn((bits + BIGNUM_INT_BITS - 1) / BIGNUM_INT_BITS); |
9400cf6f |
667 | |
668 | if (ret) { |
a3412f52 |
669 | shiftw = shift / BIGNUM_INT_BITS; |
670 | shiftb = shift % BIGNUM_INT_BITS; |
671 | shiftbb = BIGNUM_INT_BITS - shiftb; |
32874aea |
672 | |
673 | ai1 = a[shiftw + 1]; |
674 | for (i = 1; i <= ret[0]; i++) { |
675 | ai = ai1; |
676 | ai1 = (i + shiftw + 1 <= a[0] ? a[i + shiftw + 1] : 0); |
a3412f52 |
677 | ret[i] = ((ai >> shiftb) | (ai1 << shiftbb)) & BIGNUM_INT_MASK; |
32874aea |
678 | } |
9400cf6f |
679 | } |
680 | |
681 | return ret; |
682 | } |
683 | |
684 | /* |
685 | * Non-modular multiplication and addition. |
686 | */ |
32874aea |
687 | Bignum bigmuladd(Bignum a, Bignum b, Bignum addend) |
688 | { |
9400cf6f |
689 | int alen = a[0], blen = b[0]; |
690 | int mlen = (alen > blen ? alen : blen); |
691 | int rlen, i, maxspot; |
a3412f52 |
692 | BignumInt *workspace; |
9400cf6f |
693 | Bignum ret; |
694 | |
695 | /* mlen space for a, mlen space for b, 2*mlen for result */ |
a3412f52 |
696 | workspace = snewn(mlen * 4, BignumInt); |
9400cf6f |
697 | for (i = 0; i < mlen; i++) { |
32874aea |
698 | workspace[0 * mlen + i] = (mlen - i <= a[0] ? a[mlen - i] : 0); |
699 | workspace[1 * mlen + i] = (mlen - i <= b[0] ? b[mlen - i] : 0); |
9400cf6f |
700 | } |
701 | |
32874aea |
702 | internal_mul(workspace + 0 * mlen, workspace + 1 * mlen, |
703 | workspace + 2 * mlen, mlen); |
9400cf6f |
704 | |
705 | /* now just copy the result back */ |
706 | rlen = alen + blen + 1; |
707 | if (addend && rlen <= addend[0]) |
32874aea |
708 | rlen = addend[0] + 1; |
9400cf6f |
709 | ret = newbn(rlen); |
710 | maxspot = 0; |
711 | for (i = 1; i <= ret[0]; i++) { |
32874aea |
712 | ret[i] = (i <= 2 * mlen ? workspace[4 * mlen - i] : 0); |
713 | if (ret[i] != 0) |
714 | maxspot = i; |
9400cf6f |
715 | } |
716 | ret[0] = maxspot; |
717 | |
718 | /* now add in the addend, if any */ |
719 | if (addend) { |
a3412f52 |
720 | BignumDblInt carry = 0; |
32874aea |
721 | for (i = 1; i <= rlen; i++) { |
722 | carry += (i <= ret[0] ? ret[i] : 0); |
723 | carry += (i <= addend[0] ? addend[i] : 0); |
a3412f52 |
724 | ret[i] = (BignumInt) carry & BIGNUM_INT_MASK; |
725 | carry >>= BIGNUM_INT_BITS; |
32874aea |
726 | if (ret[i] != 0 && i > maxspot) |
727 | maxspot = i; |
728 | } |
9400cf6f |
729 | } |
730 | ret[0] = maxspot; |
731 | |
c523f55f |
732 | sfree(workspace); |
9400cf6f |
733 | return ret; |
734 | } |
735 | |
736 | /* |
737 | * Non-modular multiplication. |
738 | */ |
32874aea |
739 | Bignum bigmul(Bignum a, Bignum b) |
740 | { |
9400cf6f |
741 | return bigmuladd(a, b, NULL); |
742 | } |
743 | |
744 | /* |
3709bfe9 |
745 | * Create a bignum which is the bitmask covering another one. That |
746 | * is, the smallest integer which is >= N and is also one less than |
747 | * a power of two. |
748 | */ |
32874aea |
749 | Bignum bignum_bitmask(Bignum n) |
750 | { |
3709bfe9 |
751 | Bignum ret = copybn(n); |
752 | int i; |
a3412f52 |
753 | BignumInt j; |
3709bfe9 |
754 | |
755 | i = ret[0]; |
756 | while (n[i] == 0 && i > 0) |
32874aea |
757 | i--; |
3709bfe9 |
758 | if (i <= 0) |
32874aea |
759 | return ret; /* input was zero */ |
3709bfe9 |
760 | j = 1; |
761 | while (j < n[i]) |
32874aea |
762 | j = 2 * j + 1; |
3709bfe9 |
763 | ret[i] = j; |
764 | while (--i > 0) |
a3412f52 |
765 | ret[i] = BIGNUM_INT_MASK; |
3709bfe9 |
766 | return ret; |
767 | } |
768 | |
769 | /* |
5c72ca61 |
770 | * Convert a (max 32-bit) long into a bignum. |
9400cf6f |
771 | */ |
a3412f52 |
772 | Bignum bignum_from_long(unsigned long nn) |
32874aea |
773 | { |
9400cf6f |
774 | Bignum ret; |
a3412f52 |
775 | BignumDblInt n = nn; |
9400cf6f |
776 | |
5c72ca61 |
777 | ret = newbn(3); |
a3412f52 |
778 | ret[1] = (BignumInt)(n & BIGNUM_INT_MASK); |
779 | ret[2] = (BignumInt)((n >> BIGNUM_INT_BITS) & BIGNUM_INT_MASK); |
5c72ca61 |
780 | ret[3] = 0; |
781 | ret[0] = (ret[2] ? 2 : 1); |
32874aea |
782 | return ret; |
9400cf6f |
783 | } |
784 | |
785 | /* |
786 | * Add a long to a bignum. |
787 | */ |
a3412f52 |
788 | Bignum bignum_add_long(Bignum number, unsigned long addendx) |
32874aea |
789 | { |
790 | Bignum ret = newbn(number[0] + 1); |
9400cf6f |
791 | int i, maxspot = 0; |
a3412f52 |
792 | BignumDblInt carry = 0, addend = addendx; |
9400cf6f |
793 | |
794 | for (i = 1; i <= ret[0]; i++) { |
a3412f52 |
795 | carry += addend & BIGNUM_INT_MASK; |
32874aea |
796 | carry += (i <= number[0] ? number[i] : 0); |
a3412f52 |
797 | addend >>= BIGNUM_INT_BITS; |
798 | ret[i] = (BignumInt) carry & BIGNUM_INT_MASK; |
799 | carry >>= BIGNUM_INT_BITS; |
32874aea |
800 | if (ret[i] != 0) |
801 | maxspot = i; |
9400cf6f |
802 | } |
803 | ret[0] = maxspot; |
804 | return ret; |
805 | } |
806 | |
807 | /* |
808 | * Compute the residue of a bignum, modulo a (max 16-bit) short. |
809 | */ |
32874aea |
810 | unsigned short bignum_mod_short(Bignum number, unsigned short modulus) |
811 | { |
a3412f52 |
812 | BignumDblInt mod, r; |
9400cf6f |
813 | int i; |
814 | |
815 | r = 0; |
816 | mod = modulus; |
817 | for (i = number[0]; i > 0; i--) |
736cc6d1 |
818 | r = (r * (BIGNUM_TOP_BIT % mod) * 2 + number[i] % mod) % mod; |
6e522441 |
819 | return (unsigned short) r; |
9400cf6f |
820 | } |
821 | |
a3412f52 |
822 | #ifdef DEBUG |
32874aea |
823 | void diagbn(char *prefix, Bignum md) |
824 | { |
9400cf6f |
825 | int i, nibbles, morenibbles; |
826 | static const char hex[] = "0123456789ABCDEF"; |
827 | |
5c72ca61 |
828 | debug(("%s0x", prefix ? prefix : "")); |
9400cf6f |
829 | |
32874aea |
830 | nibbles = (3 + bignum_bitcount(md)) / 4; |
831 | if (nibbles < 1) |
832 | nibbles = 1; |
833 | morenibbles = 4 * md[0] - nibbles; |
834 | for (i = 0; i < morenibbles; i++) |
5c72ca61 |
835 | debug(("-")); |
32874aea |
836 | for (i = nibbles; i--;) |
5c72ca61 |
837 | debug(("%c", |
838 | hex[(bignum_byte(md, i / 2) >> (4 * (i % 2))) & 0xF])); |
9400cf6f |
839 | |
32874aea |
840 | if (prefix) |
5c72ca61 |
841 | debug(("\n")); |
842 | } |
f28753ab |
843 | #endif |
5c72ca61 |
844 | |
845 | /* |
846 | * Simple division. |
847 | */ |
848 | Bignum bigdiv(Bignum a, Bignum b) |
849 | { |
850 | Bignum q = newbn(a[0]); |
851 | bigdivmod(a, b, NULL, q); |
852 | return q; |
853 | } |
854 | |
855 | /* |
856 | * Simple remainder. |
857 | */ |
858 | Bignum bigmod(Bignum a, Bignum b) |
859 | { |
860 | Bignum r = newbn(b[0]); |
861 | bigdivmod(a, b, r, NULL); |
862 | return r; |
9400cf6f |
863 | } |
864 | |
865 | /* |
866 | * Greatest common divisor. |
867 | */ |
32874aea |
868 | Bignum biggcd(Bignum av, Bignum bv) |
869 | { |
9400cf6f |
870 | Bignum a = copybn(av); |
871 | Bignum b = copybn(bv); |
872 | |
9400cf6f |
873 | while (bignum_cmp(b, Zero) != 0) { |
32874aea |
874 | Bignum t = newbn(b[0]); |
5c72ca61 |
875 | bigdivmod(a, b, t, NULL); |
32874aea |
876 | while (t[0] > 1 && t[t[0]] == 0) |
877 | t[0]--; |
878 | freebn(a); |
879 | a = b; |
880 | b = t; |
9400cf6f |
881 | } |
882 | |
883 | freebn(b); |
884 | return a; |
885 | } |
886 | |
887 | /* |
888 | * Modular inverse, using Euclid's extended algorithm. |
889 | */ |
32874aea |
890 | Bignum modinv(Bignum number, Bignum modulus) |
891 | { |
9400cf6f |
892 | Bignum a = copybn(modulus); |
893 | Bignum b = copybn(number); |
894 | Bignum xp = copybn(Zero); |
895 | Bignum x = copybn(One); |
896 | int sign = +1; |
897 | |
898 | while (bignum_cmp(b, One) != 0) { |
32874aea |
899 | Bignum t = newbn(b[0]); |
900 | Bignum q = newbn(a[0]); |
5c72ca61 |
901 | bigdivmod(a, b, t, q); |
32874aea |
902 | while (t[0] > 1 && t[t[0]] == 0) |
903 | t[0]--; |
904 | freebn(a); |
905 | a = b; |
906 | b = t; |
907 | t = xp; |
908 | xp = x; |
909 | x = bigmuladd(q, xp, t); |
910 | sign = -sign; |
911 | freebn(t); |
9400cf6f |
912 | } |
913 | |
914 | freebn(b); |
915 | freebn(a); |
916 | freebn(xp); |
917 | |
918 | /* now we know that sign * x == 1, and that x < modulus */ |
919 | if (sign < 0) { |
32874aea |
920 | /* set a new x to be modulus - x */ |
921 | Bignum newx = newbn(modulus[0]); |
a3412f52 |
922 | BignumInt carry = 0; |
32874aea |
923 | int maxspot = 1; |
924 | int i; |
925 | |
926 | for (i = 1; i <= newx[0]; i++) { |
a3412f52 |
927 | BignumInt aword = (i <= modulus[0] ? modulus[i] : 0); |
928 | BignumInt bword = (i <= x[0] ? x[i] : 0); |
32874aea |
929 | newx[i] = aword - bword - carry; |
930 | bword = ~bword; |
931 | carry = carry ? (newx[i] >= bword) : (newx[i] > bword); |
932 | if (newx[i] != 0) |
933 | maxspot = i; |
934 | } |
935 | newx[0] = maxspot; |
936 | freebn(x); |
937 | x = newx; |
9400cf6f |
938 | } |
939 | |
940 | /* and return. */ |
941 | return x; |
942 | } |
6e522441 |
943 | |
944 | /* |
945 | * Render a bignum into decimal. Return a malloced string holding |
946 | * the decimal representation. |
947 | */ |
32874aea |
948 | char *bignum_decimal(Bignum x) |
949 | { |
6e522441 |
950 | int ndigits, ndigit; |
951 | int i, iszero; |
a3412f52 |
952 | BignumDblInt carry; |
6e522441 |
953 | char *ret; |
a3412f52 |
954 | BignumInt *workspace; |
6e522441 |
955 | |
956 | /* |
957 | * First, estimate the number of digits. Since log(10)/log(2) |
958 | * is just greater than 93/28 (the joys of continued fraction |
959 | * approximations...) we know that for every 93 bits, we need |
960 | * at most 28 digits. This will tell us how much to malloc. |
961 | * |
962 | * Formally: if x has i bits, that means x is strictly less |
963 | * than 2^i. Since 2 is less than 10^(28/93), this is less than |
964 | * 10^(28i/93). We need an integer power of ten, so we must |
965 | * round up (rounding down might make it less than x again). |
966 | * Therefore if we multiply the bit count by 28/93, rounding |
967 | * up, we will have enough digits. |
968 | */ |
ddecd643 |
969 | i = bignum_bitcount(x); |
32874aea |
970 | ndigits = (28 * i + 92) / 93; /* multiply by 28/93 and round up */ |
971 | ndigits++; /* allow for trailing \0 */ |
3d88e64d |
972 | ret = snewn(ndigits, char); |
6e522441 |
973 | |
974 | /* |
975 | * Now allocate some workspace to hold the binary form as we |
976 | * repeatedly divide it by ten. Initialise this to the |
977 | * big-endian form of the number. |
978 | */ |
a3412f52 |
979 | workspace = snewn(x[0], BignumInt); |
6e522441 |
980 | for (i = 0; i < x[0]; i++) |
32874aea |
981 | workspace[i] = x[x[0] - i]; |
6e522441 |
982 | |
983 | /* |
984 | * Next, write the decimal number starting with the last digit. |
985 | * We use ordinary short division, dividing 10 into the |
986 | * workspace. |
987 | */ |
32874aea |
988 | ndigit = ndigits - 1; |
6e522441 |
989 | ret[ndigit] = '\0'; |
990 | do { |
32874aea |
991 | iszero = 1; |
992 | carry = 0; |
993 | for (i = 0; i < x[0]; i++) { |
a3412f52 |
994 | carry = (carry << BIGNUM_INT_BITS) + workspace[i]; |
995 | workspace[i] = (BignumInt) (carry / 10); |
32874aea |
996 | if (workspace[i]) |
997 | iszero = 0; |
998 | carry %= 10; |
999 | } |
1000 | ret[--ndigit] = (char) (carry + '0'); |
6e522441 |
1001 | } while (!iszero); |
1002 | |
1003 | /* |
1004 | * There's a chance we've fallen short of the start of the |
1005 | * string. Correct if so. |
1006 | */ |
1007 | if (ndigit > 0) |
32874aea |
1008 | memmove(ret, ret + ndigit, ndigits - ndigit); |
6e522441 |
1009 | |
1010 | /* |
1011 | * Done. |
1012 | */ |
c523f55f |
1013 | sfree(workspace); |
6e522441 |
1014 | return ret; |
1015 | } |