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