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 |
0016d70b |
543 | * of bytes consumed, or -1 if there wasn't enough data. |
7cca0d81 |
544 | */ |
0016d70b |
545 | int ssh1_read_bignum(const unsigned char *data, int len, Bignum * result) |
32874aea |
546 | { |
27cd7fc2 |
547 | const unsigned char *p = data; |
7cca0d81 |
548 | int i; |
549 | int w, b; |
550 | |
0016d70b |
551 | if (len < 2) |
552 | return -1; |
553 | |
7cca0d81 |
554 | w = 0; |
32874aea |
555 | for (i = 0; i < 2; i++) |
556 | w = (w << 8) + *p++; |
557 | b = (w + 7) / 8; /* bits -> bytes */ |
7cca0d81 |
558 | |
0016d70b |
559 | if (len < b+2) |
560 | return -1; |
561 | |
32874aea |
562 | if (!result) /* just return length */ |
563 | return b + 2; |
a52f067e |
564 | |
3709bfe9 |
565 | *result = bignum_from_bytes(p, b); |
7cca0d81 |
566 | |
3709bfe9 |
567 | return p + b - data; |
7cca0d81 |
568 | } |
5c58ad2d |
569 | |
570 | /* |
571 | * Return the bit count of a bignum, for ssh1 encoding. |
572 | */ |
32874aea |
573 | int bignum_bitcount(Bignum bn) |
574 | { |
a3412f52 |
575 | int bitcount = bn[0] * BIGNUM_INT_BITS - 1; |
32874aea |
576 | while (bitcount >= 0 |
a3412f52 |
577 | && (bn[bitcount / BIGNUM_INT_BITS + 1] >> (bitcount % BIGNUM_INT_BITS)) == 0) bitcount--; |
5c58ad2d |
578 | return bitcount + 1; |
579 | } |
580 | |
581 | /* |
582 | * Return the byte length of a bignum when ssh1 encoded. |
583 | */ |
32874aea |
584 | int ssh1_bignum_length(Bignum bn) |
585 | { |
586 | return 2 + (bignum_bitcount(bn) + 7) / 8; |
ddecd643 |
587 | } |
588 | |
589 | /* |
590 | * Return the byte length of a bignum when ssh2 encoded. |
591 | */ |
32874aea |
592 | int ssh2_bignum_length(Bignum bn) |
593 | { |
594 | return 4 + (bignum_bitcount(bn) + 8) / 8; |
5c58ad2d |
595 | } |
596 | |
597 | /* |
598 | * Return a byte from a bignum; 0 is least significant, etc. |
599 | */ |
32874aea |
600 | int bignum_byte(Bignum bn, int i) |
601 | { |
a3412f52 |
602 | if (i >= BIGNUM_INT_BYTES * bn[0]) |
32874aea |
603 | return 0; /* beyond the end */ |
5c58ad2d |
604 | else |
a3412f52 |
605 | return (bn[i / BIGNUM_INT_BYTES + 1] >> |
606 | ((i % BIGNUM_INT_BYTES)*8)) & 0xFF; |
5c58ad2d |
607 | } |
608 | |
609 | /* |
9400cf6f |
610 | * Return a bit from a bignum; 0 is least significant, etc. |
611 | */ |
32874aea |
612 | int bignum_bit(Bignum bn, int i) |
613 | { |
a3412f52 |
614 | if (i >= BIGNUM_INT_BITS * bn[0]) |
32874aea |
615 | return 0; /* beyond the end */ |
9400cf6f |
616 | else |
a3412f52 |
617 | return (bn[i / BIGNUM_INT_BITS + 1] >> (i % BIGNUM_INT_BITS)) & 1; |
9400cf6f |
618 | } |
619 | |
620 | /* |
621 | * Set a bit in a bignum; 0 is least significant, etc. |
622 | */ |
32874aea |
623 | void bignum_set_bit(Bignum bn, int bitnum, int value) |
624 | { |
a3412f52 |
625 | if (bitnum >= BIGNUM_INT_BITS * bn[0]) |
32874aea |
626 | abort(); /* beyond the end */ |
9400cf6f |
627 | else { |
a3412f52 |
628 | int v = bitnum / BIGNUM_INT_BITS + 1; |
629 | int mask = 1 << (bitnum % BIGNUM_INT_BITS); |
32874aea |
630 | if (value) |
631 | bn[v] |= mask; |
632 | else |
633 | bn[v] &= ~mask; |
9400cf6f |
634 | } |
635 | } |
636 | |
637 | /* |
5c58ad2d |
638 | * Write a ssh1-format bignum into a buffer. It is assumed the |
639 | * buffer is big enough. Returns the number of bytes used. |
640 | */ |
32874aea |
641 | int ssh1_write_bignum(void *data, Bignum bn) |
642 | { |
5c58ad2d |
643 | unsigned char *p = data; |
644 | int len = ssh1_bignum_length(bn); |
645 | int i; |
ddecd643 |
646 | int bitc = bignum_bitcount(bn); |
5c58ad2d |
647 | |
648 | *p++ = (bitc >> 8) & 0xFF; |
32874aea |
649 | *p++ = (bitc) & 0xFF; |
650 | for (i = len - 2; i--;) |
651 | *p++ = bignum_byte(bn, i); |
5c58ad2d |
652 | return len; |
653 | } |
9400cf6f |
654 | |
655 | /* |
656 | * Compare two bignums. Returns like strcmp. |
657 | */ |
32874aea |
658 | int bignum_cmp(Bignum a, Bignum b) |
659 | { |
9400cf6f |
660 | int amax = a[0], bmax = b[0]; |
661 | int i = (amax > bmax ? amax : bmax); |
662 | while (i) { |
a3412f52 |
663 | BignumInt aval = (i > amax ? 0 : a[i]); |
664 | BignumInt bval = (i > bmax ? 0 : b[i]); |
32874aea |
665 | if (aval < bval) |
666 | return -1; |
667 | if (aval > bval) |
668 | return +1; |
669 | i--; |
9400cf6f |
670 | } |
671 | return 0; |
672 | } |
673 | |
674 | /* |
675 | * Right-shift one bignum to form another. |
676 | */ |
32874aea |
677 | Bignum bignum_rshift(Bignum a, int shift) |
678 | { |
9400cf6f |
679 | Bignum ret; |
680 | int i, shiftw, shiftb, shiftbb, bits; |
a3412f52 |
681 | BignumInt ai, ai1; |
9400cf6f |
682 | |
ddecd643 |
683 | bits = bignum_bitcount(a) - shift; |
a3412f52 |
684 | ret = newbn((bits + BIGNUM_INT_BITS - 1) / BIGNUM_INT_BITS); |
9400cf6f |
685 | |
686 | if (ret) { |
a3412f52 |
687 | shiftw = shift / BIGNUM_INT_BITS; |
688 | shiftb = shift % BIGNUM_INT_BITS; |
689 | shiftbb = BIGNUM_INT_BITS - shiftb; |
32874aea |
690 | |
691 | ai1 = a[shiftw + 1]; |
692 | for (i = 1; i <= ret[0]; i++) { |
693 | ai = ai1; |
694 | ai1 = (i + shiftw + 1 <= a[0] ? a[i + shiftw + 1] : 0); |
a3412f52 |
695 | ret[i] = ((ai >> shiftb) | (ai1 << shiftbb)) & BIGNUM_INT_MASK; |
32874aea |
696 | } |
9400cf6f |
697 | } |
698 | |
699 | return ret; |
700 | } |
701 | |
702 | /* |
703 | * Non-modular multiplication and addition. |
704 | */ |
32874aea |
705 | Bignum bigmuladd(Bignum a, Bignum b, Bignum addend) |
706 | { |
9400cf6f |
707 | int alen = a[0], blen = b[0]; |
708 | int mlen = (alen > blen ? alen : blen); |
709 | int rlen, i, maxspot; |
a3412f52 |
710 | BignumInt *workspace; |
9400cf6f |
711 | Bignum ret; |
712 | |
713 | /* mlen space for a, mlen space for b, 2*mlen for result */ |
a3412f52 |
714 | workspace = snewn(mlen * 4, BignumInt); |
9400cf6f |
715 | for (i = 0; i < mlen; i++) { |
32874aea |
716 | workspace[0 * mlen + i] = (mlen - i <= a[0] ? a[mlen - i] : 0); |
717 | workspace[1 * mlen + i] = (mlen - i <= b[0] ? b[mlen - i] : 0); |
9400cf6f |
718 | } |
719 | |
32874aea |
720 | internal_mul(workspace + 0 * mlen, workspace + 1 * mlen, |
721 | workspace + 2 * mlen, mlen); |
9400cf6f |
722 | |
723 | /* now just copy the result back */ |
724 | rlen = alen + blen + 1; |
725 | if (addend && rlen <= addend[0]) |
32874aea |
726 | rlen = addend[0] + 1; |
9400cf6f |
727 | ret = newbn(rlen); |
728 | maxspot = 0; |
729 | for (i = 1; i <= ret[0]; i++) { |
32874aea |
730 | ret[i] = (i <= 2 * mlen ? workspace[4 * mlen - i] : 0); |
731 | if (ret[i] != 0) |
732 | maxspot = i; |
9400cf6f |
733 | } |
734 | ret[0] = maxspot; |
735 | |
736 | /* now add in the addend, if any */ |
737 | if (addend) { |
a3412f52 |
738 | BignumDblInt carry = 0; |
32874aea |
739 | for (i = 1; i <= rlen; i++) { |
740 | carry += (i <= ret[0] ? ret[i] : 0); |
741 | carry += (i <= addend[0] ? addend[i] : 0); |
a3412f52 |
742 | ret[i] = (BignumInt) carry & BIGNUM_INT_MASK; |
743 | carry >>= BIGNUM_INT_BITS; |
32874aea |
744 | if (ret[i] != 0 && i > maxspot) |
745 | maxspot = i; |
746 | } |
9400cf6f |
747 | } |
748 | ret[0] = maxspot; |
749 | |
c523f55f |
750 | sfree(workspace); |
9400cf6f |
751 | return ret; |
752 | } |
753 | |
754 | /* |
755 | * Non-modular multiplication. |
756 | */ |
32874aea |
757 | Bignum bigmul(Bignum a, Bignum b) |
758 | { |
9400cf6f |
759 | return bigmuladd(a, b, NULL); |
760 | } |
761 | |
762 | /* |
3709bfe9 |
763 | * Create a bignum which is the bitmask covering another one. That |
764 | * is, the smallest integer which is >= N and is also one less than |
765 | * a power of two. |
766 | */ |
32874aea |
767 | Bignum bignum_bitmask(Bignum n) |
768 | { |
3709bfe9 |
769 | Bignum ret = copybn(n); |
770 | int i; |
a3412f52 |
771 | BignumInt j; |
3709bfe9 |
772 | |
773 | i = ret[0]; |
774 | while (n[i] == 0 && i > 0) |
32874aea |
775 | i--; |
3709bfe9 |
776 | if (i <= 0) |
32874aea |
777 | return ret; /* input was zero */ |
3709bfe9 |
778 | j = 1; |
779 | while (j < n[i]) |
32874aea |
780 | j = 2 * j + 1; |
3709bfe9 |
781 | ret[i] = j; |
782 | while (--i > 0) |
a3412f52 |
783 | ret[i] = BIGNUM_INT_MASK; |
3709bfe9 |
784 | return ret; |
785 | } |
786 | |
787 | /* |
5c72ca61 |
788 | * Convert a (max 32-bit) long into a bignum. |
9400cf6f |
789 | */ |
a3412f52 |
790 | Bignum bignum_from_long(unsigned long nn) |
32874aea |
791 | { |
9400cf6f |
792 | Bignum ret; |
a3412f52 |
793 | BignumDblInt n = nn; |
9400cf6f |
794 | |
5c72ca61 |
795 | ret = newbn(3); |
a3412f52 |
796 | ret[1] = (BignumInt)(n & BIGNUM_INT_MASK); |
797 | ret[2] = (BignumInt)((n >> BIGNUM_INT_BITS) & BIGNUM_INT_MASK); |
5c72ca61 |
798 | ret[3] = 0; |
799 | ret[0] = (ret[2] ? 2 : 1); |
32874aea |
800 | return ret; |
9400cf6f |
801 | } |
802 | |
803 | /* |
804 | * Add a long to a bignum. |
805 | */ |
a3412f52 |
806 | Bignum bignum_add_long(Bignum number, unsigned long addendx) |
32874aea |
807 | { |
808 | Bignum ret = newbn(number[0] + 1); |
9400cf6f |
809 | int i, maxspot = 0; |
a3412f52 |
810 | BignumDblInt carry = 0, addend = addendx; |
9400cf6f |
811 | |
812 | for (i = 1; i <= ret[0]; i++) { |
a3412f52 |
813 | carry += addend & BIGNUM_INT_MASK; |
32874aea |
814 | carry += (i <= number[0] ? number[i] : 0); |
a3412f52 |
815 | addend >>= BIGNUM_INT_BITS; |
816 | ret[i] = (BignumInt) carry & BIGNUM_INT_MASK; |
817 | carry >>= BIGNUM_INT_BITS; |
32874aea |
818 | if (ret[i] != 0) |
819 | maxspot = i; |
9400cf6f |
820 | } |
821 | ret[0] = maxspot; |
822 | return ret; |
823 | } |
824 | |
825 | /* |
826 | * Compute the residue of a bignum, modulo a (max 16-bit) short. |
827 | */ |
32874aea |
828 | unsigned short bignum_mod_short(Bignum number, unsigned short modulus) |
829 | { |
a3412f52 |
830 | BignumDblInt mod, r; |
9400cf6f |
831 | int i; |
832 | |
833 | r = 0; |
834 | mod = modulus; |
835 | for (i = number[0]; i > 0; i--) |
736cc6d1 |
836 | r = (r * (BIGNUM_TOP_BIT % mod) * 2 + number[i] % mod) % mod; |
6e522441 |
837 | return (unsigned short) r; |
9400cf6f |
838 | } |
839 | |
a3412f52 |
840 | #ifdef DEBUG |
32874aea |
841 | void diagbn(char *prefix, Bignum md) |
842 | { |
9400cf6f |
843 | int i, nibbles, morenibbles; |
844 | static const char hex[] = "0123456789ABCDEF"; |
845 | |
5c72ca61 |
846 | debug(("%s0x", prefix ? prefix : "")); |
9400cf6f |
847 | |
32874aea |
848 | nibbles = (3 + bignum_bitcount(md)) / 4; |
849 | if (nibbles < 1) |
850 | nibbles = 1; |
851 | morenibbles = 4 * md[0] - nibbles; |
852 | for (i = 0; i < morenibbles; i++) |
5c72ca61 |
853 | debug(("-")); |
32874aea |
854 | for (i = nibbles; i--;) |
5c72ca61 |
855 | debug(("%c", |
856 | hex[(bignum_byte(md, i / 2) >> (4 * (i % 2))) & 0xF])); |
9400cf6f |
857 | |
32874aea |
858 | if (prefix) |
5c72ca61 |
859 | debug(("\n")); |
860 | } |
f28753ab |
861 | #endif |
5c72ca61 |
862 | |
863 | /* |
864 | * Simple division. |
865 | */ |
866 | Bignum bigdiv(Bignum a, Bignum b) |
867 | { |
868 | Bignum q = newbn(a[0]); |
869 | bigdivmod(a, b, NULL, q); |
870 | return q; |
871 | } |
872 | |
873 | /* |
874 | * Simple remainder. |
875 | */ |
876 | Bignum bigmod(Bignum a, Bignum b) |
877 | { |
878 | Bignum r = newbn(b[0]); |
879 | bigdivmod(a, b, r, NULL); |
880 | return r; |
9400cf6f |
881 | } |
882 | |
883 | /* |
884 | * Greatest common divisor. |
885 | */ |
32874aea |
886 | Bignum biggcd(Bignum av, Bignum bv) |
887 | { |
9400cf6f |
888 | Bignum a = copybn(av); |
889 | Bignum b = copybn(bv); |
890 | |
9400cf6f |
891 | while (bignum_cmp(b, Zero) != 0) { |
32874aea |
892 | Bignum t = newbn(b[0]); |
5c72ca61 |
893 | bigdivmod(a, b, t, NULL); |
32874aea |
894 | while (t[0] > 1 && t[t[0]] == 0) |
895 | t[0]--; |
896 | freebn(a); |
897 | a = b; |
898 | b = t; |
9400cf6f |
899 | } |
900 | |
901 | freebn(b); |
902 | return a; |
903 | } |
904 | |
905 | /* |
906 | * Modular inverse, using Euclid's extended algorithm. |
907 | */ |
32874aea |
908 | Bignum modinv(Bignum number, Bignum modulus) |
909 | { |
9400cf6f |
910 | Bignum a = copybn(modulus); |
911 | Bignum b = copybn(number); |
912 | Bignum xp = copybn(Zero); |
913 | Bignum x = copybn(One); |
914 | int sign = +1; |
915 | |
916 | while (bignum_cmp(b, One) != 0) { |
32874aea |
917 | Bignum t = newbn(b[0]); |
918 | Bignum q = newbn(a[0]); |
5c72ca61 |
919 | bigdivmod(a, b, t, q); |
32874aea |
920 | while (t[0] > 1 && t[t[0]] == 0) |
921 | t[0]--; |
922 | freebn(a); |
923 | a = b; |
924 | b = t; |
925 | t = xp; |
926 | xp = x; |
927 | x = bigmuladd(q, xp, t); |
928 | sign = -sign; |
929 | freebn(t); |
75374b2f |
930 | freebn(q); |
9400cf6f |
931 | } |
932 | |
933 | freebn(b); |
934 | freebn(a); |
935 | freebn(xp); |
936 | |
937 | /* now we know that sign * x == 1, and that x < modulus */ |
938 | if (sign < 0) { |
32874aea |
939 | /* set a new x to be modulus - x */ |
940 | Bignum newx = newbn(modulus[0]); |
a3412f52 |
941 | BignumInt carry = 0; |
32874aea |
942 | int maxspot = 1; |
943 | int i; |
944 | |
945 | for (i = 1; i <= newx[0]; i++) { |
a3412f52 |
946 | BignumInt aword = (i <= modulus[0] ? modulus[i] : 0); |
947 | BignumInt bword = (i <= x[0] ? x[i] : 0); |
32874aea |
948 | newx[i] = aword - bword - carry; |
949 | bword = ~bword; |
950 | carry = carry ? (newx[i] >= bword) : (newx[i] > bword); |
951 | if (newx[i] != 0) |
952 | maxspot = i; |
953 | } |
954 | newx[0] = maxspot; |
955 | freebn(x); |
956 | x = newx; |
9400cf6f |
957 | } |
958 | |
959 | /* and return. */ |
960 | return x; |
961 | } |
6e522441 |
962 | |
963 | /* |
964 | * Render a bignum into decimal. Return a malloced string holding |
965 | * the decimal representation. |
966 | */ |
32874aea |
967 | char *bignum_decimal(Bignum x) |
968 | { |
6e522441 |
969 | int ndigits, ndigit; |
970 | int i, iszero; |
a3412f52 |
971 | BignumDblInt carry; |
6e522441 |
972 | char *ret; |
a3412f52 |
973 | BignumInt *workspace; |
6e522441 |
974 | |
975 | /* |
976 | * First, estimate the number of digits. Since log(10)/log(2) |
977 | * is just greater than 93/28 (the joys of continued fraction |
978 | * approximations...) we know that for every 93 bits, we need |
979 | * at most 28 digits. This will tell us how much to malloc. |
980 | * |
981 | * Formally: if x has i bits, that means x is strictly less |
982 | * than 2^i. Since 2 is less than 10^(28/93), this is less than |
983 | * 10^(28i/93). We need an integer power of ten, so we must |
984 | * round up (rounding down might make it less than x again). |
985 | * Therefore if we multiply the bit count by 28/93, rounding |
986 | * up, we will have enough digits. |
987 | */ |
ddecd643 |
988 | i = bignum_bitcount(x); |
32874aea |
989 | ndigits = (28 * i + 92) / 93; /* multiply by 28/93 and round up */ |
990 | ndigits++; /* allow for trailing \0 */ |
3d88e64d |
991 | ret = snewn(ndigits, char); |
6e522441 |
992 | |
993 | /* |
994 | * Now allocate some workspace to hold the binary form as we |
995 | * repeatedly divide it by ten. Initialise this to the |
996 | * big-endian form of the number. |
997 | */ |
a3412f52 |
998 | workspace = snewn(x[0], BignumInt); |
6e522441 |
999 | for (i = 0; i < x[0]; i++) |
32874aea |
1000 | workspace[i] = x[x[0] - i]; |
6e522441 |
1001 | |
1002 | /* |
1003 | * Next, write the decimal number starting with the last digit. |
1004 | * We use ordinary short division, dividing 10 into the |
1005 | * workspace. |
1006 | */ |
32874aea |
1007 | ndigit = ndigits - 1; |
6e522441 |
1008 | ret[ndigit] = '\0'; |
1009 | do { |
32874aea |
1010 | iszero = 1; |
1011 | carry = 0; |
1012 | for (i = 0; i < x[0]; i++) { |
a3412f52 |
1013 | carry = (carry << BIGNUM_INT_BITS) + workspace[i]; |
1014 | workspace[i] = (BignumInt) (carry / 10); |
32874aea |
1015 | if (workspace[i]) |
1016 | iszero = 0; |
1017 | carry %= 10; |
1018 | } |
1019 | ret[--ndigit] = (char) (carry + '0'); |
6e522441 |
1020 | } while (!iszero); |
1021 | |
1022 | /* |
1023 | * There's a chance we've fallen short of the start of the |
1024 | * string. Correct if so. |
1025 | */ |
1026 | if (ndigit > 0) |
32874aea |
1027 | memmove(ret, ret + ndigit, ndigits - ndigit); |
6e522441 |
1028 | |
1029 | /* |
1030 | * Done. |
1031 | */ |
c523f55f |
1032 | sfree(workspace); |
6e522441 |
1033 | return ret; |
1034 | } |