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