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 | |
819a22b3 |
12 | /* |
13 | * Usage notes: |
14 | * * Do not call the DIVMOD_WORD macro with expressions such as array |
15 | * subscripts, as some implementations object to this (see below). |
16 | * * Note that none of the division methods below will cope if the |
17 | * quotient won't fit into BIGNUM_INT_BITS. Callers should be careful |
18 | * to avoid this case. |
19 | * If this condition occurs, in the case of the x86 DIV instruction, |
20 | * an overflow exception will occur, which (according to a correspondent) |
21 | * will manifest on Windows as something like |
22 | * 0xC0000095: Integer overflow |
23 | * The C variant won't give the right answer, either. |
24 | */ |
25 | |
a3412f52 |
26 | #if defined __GNUC__ && defined __i386__ |
27 | typedef unsigned long BignumInt; |
28 | typedef unsigned long long BignumDblInt; |
29 | #define BIGNUM_INT_MASK 0xFFFFFFFFUL |
30 | #define BIGNUM_TOP_BIT 0x80000000UL |
31 | #define BIGNUM_INT_BITS 32 |
32 | #define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2) |
a47e8bba |
33 | #define DIVMOD_WORD(q, r, hi, lo, w) \ |
34 | __asm__("div %2" : \ |
35 | "=d" (r), "=a" (q) : \ |
36 | "r" (w), "d" (hi), "a" (lo)) |
036eddfb |
37 | #elif defined _MSC_VER && defined _M_IX86 |
38 | typedef unsigned __int32 BignumInt; |
39 | typedef unsigned __int64 BignumDblInt; |
40 | #define BIGNUM_INT_MASK 0xFFFFFFFFUL |
41 | #define BIGNUM_TOP_BIT 0x80000000UL |
42 | #define BIGNUM_INT_BITS 32 |
43 | #define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2) |
819a22b3 |
44 | /* Note: MASM interprets array subscripts in the macro arguments as |
45 | * assembler syntax, which gives the wrong answer. Don't supply them. |
46 | * <http://msdn2.microsoft.com/en-us/library/bf1dw62z.aspx> */ |
036eddfb |
47 | #define DIVMOD_WORD(q, r, hi, lo, w) do { \ |
819a22b3 |
48 | __asm mov edx, hi \ |
49 | __asm mov eax, lo \ |
50 | __asm div w \ |
51 | __asm mov r, edx \ |
52 | __asm mov q, eax \ |
53 | } while(0) |
32e51f76 |
54 | #elif defined _LP64 |
55 | /* 64-bit architectures can do 32x32->64 chunks at a time */ |
56 | typedef unsigned int BignumInt; |
57 | typedef unsigned long BignumDblInt; |
58 | #define BIGNUM_INT_MASK 0xFFFFFFFFU |
59 | #define BIGNUM_TOP_BIT 0x80000000U |
60 | #define BIGNUM_INT_BITS 32 |
61 | #define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2) |
62 | #define DIVMOD_WORD(q, r, hi, lo, w) do { \ |
63 | BignumDblInt n = (((BignumDblInt)hi) << BIGNUM_INT_BITS) | lo; \ |
64 | q = n / w; \ |
65 | r = n % w; \ |
66 | } while (0) |
67 | #elif defined _LLP64 |
68 | /* 64-bit architectures in which unsigned long is 32 bits, not 64 */ |
69 | typedef unsigned long BignumInt; |
70 | typedef unsigned long long BignumDblInt; |
71 | #define BIGNUM_INT_MASK 0xFFFFFFFFUL |
72 | #define BIGNUM_TOP_BIT 0x80000000UL |
73 | #define BIGNUM_INT_BITS 32 |
74 | #define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2) |
75 | #define DIVMOD_WORD(q, r, hi, lo, w) do { \ |
76 | BignumDblInt n = (((BignumDblInt)hi) << BIGNUM_INT_BITS) | lo; \ |
77 | q = n / w; \ |
78 | r = n % w; \ |
79 | } while (0) |
a3412f52 |
80 | #else |
32e51f76 |
81 | /* Fallback for all other cases */ |
a3412f52 |
82 | typedef unsigned short BignumInt; |
83 | typedef unsigned long BignumDblInt; |
84 | #define BIGNUM_INT_MASK 0xFFFFU |
85 | #define BIGNUM_TOP_BIT 0x8000U |
86 | #define BIGNUM_INT_BITS 16 |
87 | #define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2) |
a47e8bba |
88 | #define DIVMOD_WORD(q, r, hi, lo, w) do { \ |
89 | BignumDblInt n = (((BignumDblInt)hi) << BIGNUM_INT_BITS) | lo; \ |
90 | q = n / w; \ |
91 | r = n % w; \ |
92 | } while (0) |
a3412f52 |
93 | #endif |
94 | |
95 | #define BIGNUM_INT_BYTES (BIGNUM_INT_BITS / 8) |
96 | |
3709bfe9 |
97 | #define BIGNUM_INTERNAL |
a3412f52 |
98 | typedef BignumInt *Bignum; |
3709bfe9 |
99 | |
e5574168 |
100 | #include "ssh.h" |
101 | |
a3412f52 |
102 | BignumInt bnZero[1] = { 0 }; |
103 | BignumInt bnOne[2] = { 1, 1 }; |
e5574168 |
104 | |
7d6ee6ff |
105 | /* |
a3412f52 |
106 | * The Bignum format is an array of `BignumInt'. The first |
7d6ee6ff |
107 | * element of the array counts the remaining elements. The |
a3412f52 |
108 | * remaining elements express the actual number, base 2^BIGNUM_INT_BITS, _least_ |
7d6ee6ff |
109 | * significant digit first. (So it's trivial to extract the bit |
110 | * with value 2^n for any n.) |
111 | * |
112 | * All Bignums in this module are positive. Negative numbers must |
113 | * be dealt with outside it. |
114 | * |
115 | * INVARIANT: the most significant word of any Bignum must be |
116 | * nonzero. |
117 | */ |
118 | |
7cca0d81 |
119 | Bignum Zero = bnZero, One = bnOne; |
e5574168 |
120 | |
32874aea |
121 | static Bignum newbn(int length) |
122 | { |
a3412f52 |
123 | Bignum b = snewn(length + 1, BignumInt); |
e5574168 |
124 | if (!b) |
125 | abort(); /* FIXME */ |
32874aea |
126 | memset(b, 0, (length + 1) * sizeof(*b)); |
e5574168 |
127 | b[0] = length; |
128 | return b; |
129 | } |
130 | |
32874aea |
131 | void bn_restore_invariant(Bignum b) |
132 | { |
133 | while (b[0] > 1 && b[b[0]] == 0) |
134 | b[0]--; |
3709bfe9 |
135 | } |
136 | |
32874aea |
137 | Bignum copybn(Bignum orig) |
138 | { |
a3412f52 |
139 | Bignum b = snewn(orig[0] + 1, BignumInt); |
7cca0d81 |
140 | if (!b) |
141 | abort(); /* FIXME */ |
32874aea |
142 | memcpy(b, orig, (orig[0] + 1) * sizeof(*b)); |
7cca0d81 |
143 | return b; |
144 | } |
145 | |
32874aea |
146 | void freebn(Bignum b) |
147 | { |
e5574168 |
148 | /* |
149 | * Burn the evidence, just in case. |
150 | */ |
151 | memset(b, 0, sizeof(b[0]) * (b[0] + 1)); |
dcbde236 |
152 | sfree(b); |
e5574168 |
153 | } |
154 | |
32874aea |
155 | Bignum bn_power_2(int n) |
156 | { |
a3412f52 |
157 | Bignum ret = newbn(n / BIGNUM_INT_BITS + 1); |
3709bfe9 |
158 | bignum_set_bit(ret, n, 1); |
159 | return ret; |
160 | } |
161 | |
e5574168 |
162 | /* |
0c431b2f |
163 | * Internal addition. Sets c = a - b, where 'a', 'b' and 'c' are all |
164 | * big-endian arrays of 'len' BignumInts. Returns a BignumInt carried |
165 | * off the top. |
166 | */ |
167 | static BignumInt internal_add(const BignumInt *a, const BignumInt *b, |
168 | BignumInt *c, int len) |
169 | { |
170 | int i; |
171 | BignumDblInt carry = 0; |
172 | |
173 | for (i = len-1; i >= 0; i--) { |
174 | carry += (BignumDblInt)a[i] + b[i]; |
175 | c[i] = (BignumInt)carry; |
176 | carry >>= BIGNUM_INT_BITS; |
177 | } |
178 | |
179 | return (BignumInt)carry; |
180 | } |
181 | |
182 | /* |
183 | * Internal subtraction. Sets c = a - b, where 'a', 'b' and 'c' are |
184 | * all big-endian arrays of 'len' BignumInts. Any borrow from the top |
185 | * is ignored. |
186 | */ |
187 | static void internal_sub(const BignumInt *a, const BignumInt *b, |
188 | BignumInt *c, int len) |
189 | { |
190 | int i; |
191 | BignumDblInt carry = 1; |
192 | |
193 | for (i = len-1; i >= 0; i--) { |
194 | carry += (BignumDblInt)a[i] + (b[i] ^ BIGNUM_INT_MASK); |
195 | c[i] = (BignumInt)carry; |
196 | carry >>= BIGNUM_INT_BITS; |
197 | } |
198 | } |
199 | |
200 | /* |
e5574168 |
201 | * Compute c = a * b. |
202 | * Input is in the first len words of a and b. |
203 | * Result is returned in the first 2*len words of c. |
204 | */ |
0c431b2f |
205 | #define KARATSUBA_THRESHOLD 50 |
132c534f |
206 | static void internal_mul(const BignumInt *a, const BignumInt *b, |
a3412f52 |
207 | BignumInt *c, int len) |
e5574168 |
208 | { |
209 | int i, j; |
a3412f52 |
210 | BignumDblInt t; |
e5574168 |
211 | |
0c431b2f |
212 | if (len > KARATSUBA_THRESHOLD) { |
213 | |
214 | /* |
215 | * Karatsuba divide-and-conquer algorithm. Cut each input in |
216 | * half, so that it's expressed as two big 'digits' in a giant |
217 | * base D: |
218 | * |
219 | * a = a_1 D + a_0 |
220 | * b = b_1 D + b_0 |
221 | * |
222 | * Then the product is of course |
223 | * |
224 | * ab = a_1 b_1 D^2 + (a_1 b_0 + a_0 b_1) D + a_0 b_0 |
225 | * |
226 | * and we compute the three coefficients by recursively |
227 | * calling ourself to do half-length multiplications. |
228 | * |
229 | * The clever bit that makes this worth doing is that we only |
230 | * need _one_ half-length multiplication for the central |
231 | * coefficient rather than the two that it obviouly looks |
232 | * like, because we can use a single multiplication to compute |
233 | * |
234 | * (a_1 + a_0) (b_1 + b_0) = a_1 b_1 + a_1 b_0 + a_0 b_1 + a_0 b_0 |
235 | * |
236 | * and then we subtract the other two coefficients (a_1 b_1 |
237 | * and a_0 b_0) which we were computing anyway. |
238 | * |
239 | * Hence we get to multiply two numbers of length N in about |
240 | * three times as much work as it takes to multiply numbers of |
241 | * length N/2, which is obviously better than the four times |
242 | * as much work it would take if we just did a long |
243 | * conventional multiply. |
244 | */ |
245 | |
246 | int toplen = len/2, botlen = len - toplen; /* botlen is the bigger */ |
247 | int midlen = botlen + 1; |
248 | BignumInt *scratch; |
249 | BignumDblInt carry; |
f3c29e34 |
250 | #ifdef KARA_DEBUG |
251 | int i; |
252 | #endif |
0c431b2f |
253 | |
254 | /* |
255 | * The coefficients a_1 b_1 and a_0 b_0 just avoid overlapping |
256 | * in the output array, so we can compute them immediately in |
257 | * place. |
258 | */ |
259 | |
f3c29e34 |
260 | #ifdef KARA_DEBUG |
261 | printf("a1,a0 = 0x"); |
262 | for (i = 0; i < len; i++) { |
263 | if (i == toplen) printf(", 0x"); |
264 | printf("%0*x", BIGNUM_INT_BITS/4, a[i]); |
265 | } |
266 | printf("\n"); |
267 | printf("b1,b0 = 0x"); |
268 | for (i = 0; i < len; i++) { |
269 | if (i == toplen) printf(", 0x"); |
270 | printf("%0*x", BIGNUM_INT_BITS/4, b[i]); |
271 | } |
272 | printf("\n"); |
273 | #endif |
274 | |
0c431b2f |
275 | /* a_1 b_1 */ |
276 | internal_mul(a, b, c, toplen); |
f3c29e34 |
277 | #ifdef KARA_DEBUG |
278 | printf("a1b1 = 0x"); |
279 | for (i = 0; i < 2*toplen; i++) { |
280 | printf("%0*x", BIGNUM_INT_BITS/4, c[i]); |
281 | } |
282 | printf("\n"); |
283 | #endif |
0c431b2f |
284 | |
285 | /* a_0 b_0 */ |
286 | internal_mul(a + toplen, b + toplen, c + 2*toplen, botlen); |
f3c29e34 |
287 | #ifdef KARA_DEBUG |
288 | printf("a0b0 = 0x"); |
289 | for (i = 0; i < 2*botlen; i++) { |
290 | printf("%0*x", BIGNUM_INT_BITS/4, c[2*toplen+i]); |
291 | } |
292 | printf("\n"); |
293 | #endif |
0c431b2f |
294 | |
295 | /* |
296 | * We must allocate scratch space for the central coefficient, |
297 | * and also for the two input values that we multiply when |
298 | * computing it. Since either or both may carry into the |
299 | * (botlen+1)th word, we must use a slightly longer length |
300 | * 'midlen'. |
301 | */ |
302 | scratch = snewn(4 * midlen, BignumInt); |
303 | |
304 | /* Zero padding. midlen exceeds toplen by at most 2, so just |
305 | * zero the first two words of each input and the rest will be |
306 | * copied over. */ |
307 | scratch[0] = scratch[1] = scratch[midlen] = scratch[midlen+1] = 0; |
308 | |
309 | for (j = 0; j < toplen; j++) { |
310 | scratch[midlen - toplen + j] = a[j]; /* a_1 */ |
311 | scratch[2*midlen - toplen + j] = b[j]; /* b_1 */ |
312 | } |
313 | |
314 | /* compute a_1 + a_0 */ |
315 | scratch[0] = internal_add(scratch+1, a+toplen, scratch+1, botlen); |
f3c29e34 |
316 | #ifdef KARA_DEBUG |
317 | printf("a1plusa0 = 0x"); |
318 | for (i = 0; i < midlen; i++) { |
319 | printf("%0*x", BIGNUM_INT_BITS/4, scratch[i]); |
320 | } |
321 | printf("\n"); |
322 | #endif |
0c431b2f |
323 | /* compute b_1 + b_0 */ |
324 | scratch[midlen] = internal_add(scratch+midlen+1, b+toplen, |
325 | scratch+midlen+1, botlen); |
f3c29e34 |
326 | #ifdef KARA_DEBUG |
327 | printf("b1plusb0 = 0x"); |
328 | for (i = 0; i < midlen; i++) { |
329 | printf("%0*x", BIGNUM_INT_BITS/4, scratch[midlen+i]); |
330 | } |
331 | printf("\n"); |
332 | #endif |
0c431b2f |
333 | |
334 | /* |
335 | * Now we can do the third multiplication. |
336 | */ |
337 | internal_mul(scratch, scratch + midlen, scratch + 2*midlen, midlen); |
f3c29e34 |
338 | #ifdef KARA_DEBUG |
339 | printf("a1plusa0timesb1plusb0 = 0x"); |
340 | for (i = 0; i < 2*midlen; i++) { |
341 | printf("%0*x", BIGNUM_INT_BITS/4, scratch[2*midlen+i]); |
342 | } |
343 | printf("\n"); |
344 | #endif |
0c431b2f |
345 | |
346 | /* |
347 | * Now we can reuse the first half of 'scratch' to compute the |
348 | * sum of the outer two coefficients, to subtract from that |
349 | * product to obtain the middle one. |
350 | */ |
351 | scratch[0] = scratch[1] = scratch[2] = scratch[3] = 0; |
352 | for (j = 0; j < 2*toplen; j++) |
353 | scratch[2*midlen - 2*toplen + j] = c[j]; |
354 | scratch[1] = internal_add(scratch+2, c + 2*toplen, |
355 | scratch+2, 2*botlen); |
f3c29e34 |
356 | #ifdef KARA_DEBUG |
357 | printf("a1b1plusa0b0 = 0x"); |
358 | for (i = 0; i < 2*midlen; i++) { |
359 | printf("%0*x", BIGNUM_INT_BITS/4, scratch[i]); |
360 | } |
361 | printf("\n"); |
362 | #endif |
0c431b2f |
363 | |
364 | internal_sub(scratch + 2*midlen, scratch, |
365 | scratch + 2*midlen, 2*midlen); |
f3c29e34 |
366 | #ifdef KARA_DEBUG |
367 | printf("a1b0plusa0b1 = 0x"); |
368 | for (i = 0; i < 2*midlen; i++) { |
369 | printf("%0*x", BIGNUM_INT_BITS/4, scratch[2*midlen+i]); |
370 | } |
371 | printf("\n"); |
372 | #endif |
0c431b2f |
373 | |
374 | /* |
375 | * And now all we need to do is to add that middle coefficient |
376 | * back into the output. We may have to propagate a carry |
377 | * further up the output, but we can be sure it won't |
378 | * propagate right the way off the top. |
379 | */ |
380 | carry = internal_add(c + 2*len - botlen - 2*midlen, |
381 | scratch + 2*midlen, |
382 | c + 2*len - botlen - 2*midlen, 2*midlen); |
383 | j = 2*len - botlen - 2*midlen - 1; |
384 | while (carry) { |
385 | assert(j >= 0); |
386 | carry += c[j]; |
387 | c[j] = (BignumInt)carry; |
388 | carry >>= BIGNUM_INT_BITS; |
389 | } |
f3c29e34 |
390 | #ifdef KARA_DEBUG |
391 | printf("ab = 0x"); |
392 | for (i = 0; i < 2*len; i++) { |
393 | printf("%0*x", BIGNUM_INT_BITS/4, c[i]); |
394 | } |
395 | printf("\n"); |
396 | #endif |
0c431b2f |
397 | |
398 | /* Free scratch. */ |
399 | for (j = 0; j < 4 * midlen; j++) |
400 | scratch[j] = 0; |
401 | sfree(scratch); |
402 | |
403 | } else { |
404 | |
405 | /* |
406 | * Multiply in the ordinary O(N^2) way. |
407 | */ |
408 | |
409 | for (j = 0; j < 2 * len; j++) |
410 | c[j] = 0; |
411 | |
412 | for (i = len - 1; i >= 0; i--) { |
413 | t = 0; |
414 | for (j = len - 1; j >= 0; j--) { |
415 | t += MUL_WORD(a[i], (BignumDblInt) b[j]); |
416 | t += (BignumDblInt) c[i + j + 1]; |
417 | c[i + j + 1] = (BignumInt) t; |
418 | t = t >> BIGNUM_INT_BITS; |
419 | } |
420 | c[i] = (BignumInt) t; |
421 | } |
e5574168 |
422 | } |
423 | } |
424 | |
132c534f |
425 | /* |
426 | * Variant form of internal_mul used for the initial step of |
427 | * Montgomery reduction. Only bothers outputting 'len' words |
428 | * (everything above that is thrown away). |
429 | */ |
430 | static void internal_mul_low(const BignumInt *a, const BignumInt *b, |
431 | BignumInt *c, int len) |
432 | { |
433 | int i, j; |
434 | BignumDblInt t; |
435 | |
436 | if (len > KARATSUBA_THRESHOLD) { |
437 | |
438 | /* |
439 | * Karatsuba-aware version of internal_mul_low. As before, we |
440 | * express each input value as a shifted combination of two |
441 | * halves: |
442 | * |
443 | * a = a_1 D + a_0 |
444 | * b = b_1 D + b_0 |
445 | * |
446 | * Then the full product is, as before, |
447 | * |
448 | * ab = a_1 b_1 D^2 + (a_1 b_0 + a_0 b_1) D + a_0 b_0 |
449 | * |
450 | * Provided we choose D on the large side (so that a_0 and b_0 |
451 | * are _at least_ as long as a_1 and b_1), we don't need the |
452 | * topmost term at all, and we only need half of the middle |
453 | * term. So there's no point in doing the proper Karatsuba |
454 | * optimisation which computes the middle term using the top |
455 | * one, because we'd take as long computing the top one as |
456 | * just computing the middle one directly. |
457 | * |
458 | * So instead, we do a much more obvious thing: we call the |
459 | * fully optimised internal_mul to compute a_0 b_0, and we |
460 | * recursively call ourself to compute the _bottom halves_ of |
461 | * a_1 b_0 and a_0 b_1, each of which we add into the result |
462 | * in the obvious way. |
463 | * |
464 | * In other words, there's no actual Karatsuba _optimisation_ |
465 | * in this function; the only benefit in doing it this way is |
466 | * that we call internal_mul proper for a large part of the |
467 | * work, and _that_ can optimise its operation. |
468 | */ |
469 | |
470 | int toplen = len/2, botlen = len - toplen; /* botlen is the bigger */ |
471 | BignumInt *scratch; |
472 | |
473 | /* |
474 | * Allocate scratch space for the various bits and pieces |
475 | * we're going to be adding together. We need botlen*2 words |
476 | * for a_0 b_0 (though we may end up throwing away its topmost |
477 | * word), and toplen words for each of a_1 b_0 and a_0 b_1. |
478 | * That adds up to exactly 2*len. |
479 | */ |
480 | scratch = snewn(len*2, BignumInt); |
481 | |
482 | /* a_0 b_0 */ |
483 | internal_mul(a + toplen, b + toplen, scratch + 2*toplen, botlen); |
484 | |
485 | /* a_1 b_0 */ |
486 | internal_mul_low(a, b + len - toplen, scratch + toplen, toplen); |
487 | |
488 | /* a_0 b_1 */ |
489 | internal_mul_low(a + len - toplen, b, scratch, toplen); |
490 | |
491 | /* Copy the bottom half of the big coefficient into place */ |
492 | for (j = 0; j < botlen; j++) |
493 | c[toplen + j] = scratch[2*toplen + botlen + j]; |
494 | |
495 | /* Add the two small coefficients, throwing away the returned carry */ |
496 | internal_add(scratch, scratch + toplen, scratch, toplen); |
497 | |
498 | /* And add that to the large coefficient, leaving the result in c. */ |
499 | internal_add(scratch, scratch + 2*toplen + botlen - toplen, |
500 | c, toplen); |
501 | |
502 | /* Free scratch. */ |
503 | for (j = 0; j < len*2; j++) |
504 | scratch[j] = 0; |
505 | sfree(scratch); |
506 | |
507 | } else { |
508 | |
509 | for (j = 0; j < len; j++) |
510 | c[j] = 0; |
511 | |
512 | for (i = len - 1; i >= 0; i--) { |
513 | t = 0; |
514 | for (j = len - 1; j >= len - i - 1; j--) { |
515 | t += MUL_WORD(a[i], (BignumDblInt) b[j]); |
516 | t += (BignumDblInt) c[i + j + 1 - len]; |
517 | c[i + j + 1 - len] = (BignumInt) t; |
518 | t = t >> BIGNUM_INT_BITS; |
519 | } |
520 | } |
521 | |
522 | } |
523 | } |
524 | |
525 | /* |
526 | * Montgomery reduction. Expects x to be a big-endian array of 2*len |
527 | * BignumInts whose value satisfies 0 <= x < rn (where r = 2^(len * |
528 | * BIGNUM_INT_BITS) is the Montgomery base). Returns in the same array |
529 | * a value x' which is congruent to xr^{-1} mod n, and satisfies 0 <= |
530 | * x' < n. |
531 | * |
532 | * 'n' and 'mninv' should be big-endian arrays of 'len' BignumInts |
533 | * each, containing respectively n and the multiplicative inverse of |
534 | * -n mod r. |
535 | * |
536 | * 'tmp' is an array of at least '3*len' BignumInts used as scratch |
537 | * space. |
538 | */ |
539 | static void monty_reduce(BignumInt *x, const BignumInt *n, |
540 | const BignumInt *mninv, BignumInt *tmp, int len) |
541 | { |
542 | int i; |
543 | BignumInt carry; |
544 | |
545 | /* |
546 | * Multiply x by (-n)^{-1} mod r. This gives us a value m such |
547 | * that mn is congruent to -x mod r. Hence, mn+x is an exact |
548 | * multiple of r, and is also (obviously) congruent to x mod n. |
549 | */ |
550 | internal_mul_low(x + len, mninv, tmp, len); |
551 | |
552 | /* |
553 | * Compute t = (mn+x)/r in ordinary, non-modular, integer |
554 | * arithmetic. By construction this is exact, and is congruent mod |
555 | * n to x * r^{-1}, i.e. the answer we want. |
556 | * |
557 | * The following multiply leaves that answer in the _most_ |
558 | * significant half of the 'x' array, so then we must shift it |
559 | * down. |
560 | */ |
561 | internal_mul(tmp, n, tmp+len, len); |
562 | carry = internal_add(x, tmp+len, x, 2*len); |
563 | for (i = 0; i < len; i++) |
564 | x[len + i] = x[i], x[i] = 0; |
565 | |
566 | /* |
567 | * Reduce t mod n. This doesn't require a full-on division by n, |
568 | * but merely a test and single optional subtraction, since we can |
569 | * show that 0 <= t < 2n. |
570 | * |
571 | * Proof: |
572 | * + we computed m mod r, so 0 <= m < r. |
573 | * + so 0 <= mn < rn, obviously |
574 | * + hence we only need 0 <= x < rn to guarantee that 0 <= mn+x < 2rn |
575 | * + yielding 0 <= (mn+x)/r < 2n as required. |
576 | */ |
577 | if (!carry) { |
578 | for (i = 0; i < len; i++) |
579 | if (x[len + i] != n[i]) |
580 | break; |
581 | } |
582 | if (carry || i >= len || x[len + i] > n[i]) |
583 | internal_sub(x+len, n, x+len, len); |
584 | } |
585 | |
a3412f52 |
586 | static void internal_add_shifted(BignumInt *number, |
32874aea |
587 | unsigned n, int shift) |
588 | { |
a3412f52 |
589 | int word = 1 + (shift / BIGNUM_INT_BITS); |
590 | int bshift = shift % BIGNUM_INT_BITS; |
591 | BignumDblInt addend; |
9400cf6f |
592 | |
3014da2b |
593 | addend = (BignumDblInt)n << bshift; |
9400cf6f |
594 | |
595 | while (addend) { |
32874aea |
596 | addend += number[word]; |
a3412f52 |
597 | number[word] = (BignumInt) addend & BIGNUM_INT_MASK; |
598 | addend >>= BIGNUM_INT_BITS; |
32874aea |
599 | word++; |
9400cf6f |
600 | } |
601 | } |
602 | |
e5574168 |
603 | /* |
604 | * Compute a = a % m. |
9400cf6f |
605 | * Input in first alen words of a and first mlen words of m. |
606 | * Output in first alen words of a |
607 | * (of which first alen-mlen words will be zero). |
e5574168 |
608 | * The MSW of m MUST have its high bit set. |
9400cf6f |
609 | * Quotient is accumulated in the `quotient' array, which is a Bignum |
610 | * rather than the internal bigendian format. Quotient parts are shifted |
611 | * left by `qshift' before adding into quot. |
e5574168 |
612 | */ |
a3412f52 |
613 | static void internal_mod(BignumInt *a, int alen, |
614 | BignumInt *m, int mlen, |
615 | BignumInt *quot, int qshift) |
e5574168 |
616 | { |
a3412f52 |
617 | BignumInt m0, m1; |
e5574168 |
618 | unsigned int h; |
619 | int i, k; |
620 | |
e5574168 |
621 | m0 = m[0]; |
9400cf6f |
622 | if (mlen > 1) |
32874aea |
623 | m1 = m[1]; |
9400cf6f |
624 | else |
32874aea |
625 | m1 = 0; |
e5574168 |
626 | |
32874aea |
627 | for (i = 0; i <= alen - mlen; i++) { |
a3412f52 |
628 | BignumDblInt t; |
9400cf6f |
629 | unsigned int q, r, c, ai1; |
e5574168 |
630 | |
631 | if (i == 0) { |
632 | h = 0; |
633 | } else { |
32874aea |
634 | h = a[i - 1]; |
635 | a[i - 1] = 0; |
e5574168 |
636 | } |
637 | |
32874aea |
638 | if (i == alen - 1) |
639 | ai1 = 0; |
640 | else |
641 | ai1 = a[i + 1]; |
9400cf6f |
642 | |
e5574168 |
643 | /* Find q = h:a[i] / m0 */ |
62ef3d44 |
644 | if (h >= m0) { |
645 | /* |
646 | * Special case. |
647 | * |
648 | * To illustrate it, suppose a BignumInt is 8 bits, and |
649 | * we are dividing (say) A1:23:45:67 by A1:B2:C3. Then |
650 | * our initial division will be 0xA123 / 0xA1, which |
651 | * will give a quotient of 0x100 and a divide overflow. |
652 | * However, the invariants in this division algorithm |
653 | * are not violated, since the full number A1:23:... is |
654 | * _less_ than the quotient prefix A1:B2:... and so the |
655 | * following correction loop would have sorted it out. |
656 | * |
657 | * In this situation we set q to be the largest |
658 | * quotient we _can_ stomach (0xFF, of course). |
659 | */ |
660 | q = BIGNUM_INT_MASK; |
661 | } else { |
819a22b3 |
662 | /* Macro doesn't want an array subscript expression passed |
663 | * into it (see definition), so use a temporary. */ |
664 | BignumInt tmplo = a[i]; |
665 | DIVMOD_WORD(q, r, h, tmplo, m0); |
62ef3d44 |
666 | |
667 | /* Refine our estimate of q by looking at |
668 | h:a[i]:a[i+1] / m0:m1 */ |
669 | t = MUL_WORD(m1, q); |
670 | if (t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) { |
671 | q--; |
672 | t -= m1; |
673 | r = (r + m0) & BIGNUM_INT_MASK; /* overflow? */ |
674 | if (r >= (BignumDblInt) m0 && |
675 | t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) q--; |
676 | } |
e5574168 |
677 | } |
678 | |
9400cf6f |
679 | /* Subtract q * m from a[i...] */ |
e5574168 |
680 | c = 0; |
9400cf6f |
681 | for (k = mlen - 1; k >= 0; k--) { |
a47e8bba |
682 | t = MUL_WORD(q, m[k]); |
e5574168 |
683 | t += c; |
62ddb51e |
684 | c = (unsigned)(t >> BIGNUM_INT_BITS); |
a3412f52 |
685 | if ((BignumInt) t > a[i + k]) |
32874aea |
686 | c++; |
a3412f52 |
687 | a[i + k] -= (BignumInt) t; |
e5574168 |
688 | } |
689 | |
690 | /* Add back m in case of borrow */ |
691 | if (c != h) { |
692 | t = 0; |
9400cf6f |
693 | for (k = mlen - 1; k >= 0; k--) { |
e5574168 |
694 | t += m[k]; |
32874aea |
695 | t += a[i + k]; |
a3412f52 |
696 | a[i + k] = (BignumInt) t; |
697 | t = t >> BIGNUM_INT_BITS; |
e5574168 |
698 | } |
32874aea |
699 | q--; |
e5574168 |
700 | } |
32874aea |
701 | if (quot) |
a3412f52 |
702 | internal_add_shifted(quot, q, qshift + BIGNUM_INT_BITS * (alen - mlen - i)); |
e5574168 |
703 | } |
704 | } |
705 | |
706 | /* |
132c534f |
707 | * Compute (base ^ exp) % mod. Uses the Montgomery multiplication |
708 | * technique. |
e5574168 |
709 | */ |
ed953b91 |
710 | Bignum modpow(Bignum base_in, Bignum exp, Bignum mod) |
e5574168 |
711 | { |
132c534f |
712 | BignumInt *a, *b, *x, *n, *mninv, *tmp; |
713 | int len, i, j; |
714 | Bignum base, base2, r, rn, inv, result; |
ed953b91 |
715 | |
716 | /* |
717 | * The most significant word of mod needs to be non-zero. It |
718 | * should already be, but let's make sure. |
719 | */ |
720 | assert(mod[mod[0]] != 0); |
721 | |
722 | /* |
723 | * Make sure the base is smaller than the modulus, by reducing |
724 | * it modulo the modulus if not. |
725 | */ |
726 | base = bigmod(base_in, mod); |
e5574168 |
727 | |
132c534f |
728 | /* |
729 | * mod had better be odd, or we can't do Montgomery multiplication |
730 | * using a power of two at all. |
731 | */ |
732 | assert(mod[1] & 1); |
e5574168 |
733 | |
132c534f |
734 | /* |
735 | * Compute the inverse of n mod r, for monty_reduce. (In fact we |
736 | * want the inverse of _minus_ n mod r, but we'll sort that out |
737 | * below.) |
738 | */ |
739 | len = mod[0]; |
740 | r = bn_power_2(BIGNUM_INT_BITS * len); |
741 | inv = modinv(mod, r); |
e5574168 |
742 | |
132c534f |
743 | /* |
744 | * Multiply the base by r mod n, to get it into Montgomery |
745 | * representation. |
746 | */ |
747 | base2 = modmul(base, r, mod); |
748 | freebn(base); |
749 | base = base2; |
750 | |
751 | rn = bigmod(r, mod); /* r mod n, i.e. Montgomerified 1 */ |
752 | |
753 | freebn(r); /* won't need this any more */ |
754 | |
755 | /* |
756 | * Set up internal arrays of the right lengths, in big-endian |
757 | * format, containing the base, the modulus, and the modulus's |
758 | * inverse. |
759 | */ |
760 | n = snewn(len, BignumInt); |
761 | for (j = 0; j < len; j++) |
762 | n[len - 1 - j] = mod[j + 1]; |
763 | |
764 | mninv = snewn(len, BignumInt); |
765 | for (j = 0; j < len; j++) |
766 | mninv[len - 1 - j] = (j < inv[0] ? inv[j + 1] : 0); |
767 | freebn(inv); /* we don't need this copy of it any more */ |
768 | /* Now negate mninv mod r, so it's the inverse of -n rather than +n. */ |
769 | x = snewn(len, BignumInt); |
770 | for (j = 0; j < len; j++) |
771 | x[j] = 0; |
772 | internal_sub(x, mninv, mninv, len); |
773 | |
774 | /* x = snewn(len, BignumInt); */ /* already done above */ |
775 | for (j = 0; j < len; j++) |
776 | x[len - 1 - j] = (j < base[0] ? base[j + 1] : 0); |
777 | freebn(base); /* we don't need this copy of it any more */ |
778 | |
779 | a = snewn(2*len, BignumInt); |
780 | b = snewn(2*len, BignumInt); |
781 | for (j = 0; j < len; j++) |
782 | a[2*len - 1 - j] = (j < rn[0] ? rn[j + 1] : 0); |
783 | freebn(rn); |
784 | |
785 | tmp = snewn(3*len, BignumInt); |
e5574168 |
786 | |
787 | /* Skip leading zero bits of exp. */ |
32874aea |
788 | i = 0; |
a3412f52 |
789 | j = BIGNUM_INT_BITS-1; |
62ddb51e |
790 | while (i < (int)exp[0] && (exp[exp[0] - i] & (1 << j)) == 0) { |
e5574168 |
791 | j--; |
32874aea |
792 | if (j < 0) { |
793 | i++; |
a3412f52 |
794 | j = BIGNUM_INT_BITS-1; |
32874aea |
795 | } |
e5574168 |
796 | } |
797 | |
798 | /* Main computation */ |
62ddb51e |
799 | while (i < (int)exp[0]) { |
e5574168 |
800 | while (j >= 0) { |
132c534f |
801 | internal_mul(a + len, a + len, b, len); |
802 | monty_reduce(b, n, mninv, tmp, len); |
e5574168 |
803 | if ((exp[exp[0] - i] & (1 << j)) != 0) { |
132c534f |
804 | internal_mul(b + len, x, a, len); |
805 | monty_reduce(a, n, mninv, tmp, len); |
e5574168 |
806 | } else { |
a3412f52 |
807 | BignumInt *t; |
32874aea |
808 | t = a; |
809 | a = b; |
810 | b = t; |
e5574168 |
811 | } |
812 | j--; |
813 | } |
32874aea |
814 | i++; |
a3412f52 |
815 | j = BIGNUM_INT_BITS-1; |
e5574168 |
816 | } |
817 | |
132c534f |
818 | /* |
819 | * Final monty_reduce to get back from the adjusted Montgomery |
820 | * representation. |
821 | */ |
822 | monty_reduce(a, n, mninv, tmp, len); |
e5574168 |
823 | |
824 | /* Copy result to buffer */ |
59600f67 |
825 | result = newbn(mod[0]); |
132c534f |
826 | for (i = 0; i < len; i++) |
827 | result[result[0] - i] = a[i + len]; |
32874aea |
828 | while (result[0] > 1 && result[result[0]] == 0) |
829 | result[0]--; |
e5574168 |
830 | |
831 | /* Free temporary arrays */ |
132c534f |
832 | for (i = 0; i < 3 * len; i++) |
833 | tmp[i] = 0; |
834 | sfree(tmp); |
835 | for (i = 0; i < 2 * len; i++) |
32874aea |
836 | a[i] = 0; |
837 | sfree(a); |
132c534f |
838 | for (i = 0; i < 2 * len; i++) |
32874aea |
839 | b[i] = 0; |
840 | sfree(b); |
132c534f |
841 | for (i = 0; i < len; i++) |
842 | mninv[i] = 0; |
843 | sfree(mninv); |
844 | for (i = 0; i < len; i++) |
32874aea |
845 | n[i] = 0; |
846 | sfree(n); |
132c534f |
847 | for (i = 0; i < len; i++) |
848 | x[i] = 0; |
849 | sfree(x); |
ed953b91 |
850 | |
59600f67 |
851 | return result; |
e5574168 |
852 | } |
7cca0d81 |
853 | |
854 | /* |
855 | * Compute (p * q) % mod. |
856 | * The most significant word of mod MUST be non-zero. |
857 | * We assume that the result array is the same size as the mod array. |
858 | */ |
59600f67 |
859 | Bignum modmul(Bignum p, Bignum q, Bignum mod) |
7cca0d81 |
860 | { |
a3412f52 |
861 | BignumInt *a, *n, *m, *o; |
7cca0d81 |
862 | int mshift; |
80b10571 |
863 | int pqlen, mlen, rlen, i, j; |
59600f67 |
864 | Bignum result; |
7cca0d81 |
865 | |
866 | /* Allocate m of size mlen, copy mod to m */ |
867 | /* We use big endian internally */ |
868 | mlen = mod[0]; |
a3412f52 |
869 | m = snewn(mlen, BignumInt); |
32874aea |
870 | for (j = 0; j < mlen; j++) |
871 | m[j] = mod[mod[0] - j]; |
7cca0d81 |
872 | |
873 | /* Shift m left to make msb bit set */ |
a3412f52 |
874 | for (mshift = 0; mshift < BIGNUM_INT_BITS-1; mshift++) |
875 | if ((m[0] << mshift) & BIGNUM_TOP_BIT) |
32874aea |
876 | break; |
7cca0d81 |
877 | if (mshift) { |
878 | for (i = 0; i < mlen - 1; i++) |
a3412f52 |
879 | m[i] = (m[i] << mshift) | (m[i + 1] >> (BIGNUM_INT_BITS - mshift)); |
32874aea |
880 | m[mlen - 1] = m[mlen - 1] << mshift; |
7cca0d81 |
881 | } |
882 | |
883 | pqlen = (p[0] > q[0] ? p[0] : q[0]); |
884 | |
885 | /* Allocate n of size pqlen, copy p to n */ |
a3412f52 |
886 | n = snewn(pqlen, BignumInt); |
7cca0d81 |
887 | i = pqlen - p[0]; |
32874aea |
888 | for (j = 0; j < i; j++) |
889 | n[j] = 0; |
62ddb51e |
890 | for (j = 0; j < (int)p[0]; j++) |
32874aea |
891 | n[i + j] = p[p[0] - j]; |
7cca0d81 |
892 | |
893 | /* Allocate o of size pqlen, copy q to o */ |
a3412f52 |
894 | o = snewn(pqlen, BignumInt); |
7cca0d81 |
895 | i = pqlen - q[0]; |
32874aea |
896 | for (j = 0; j < i; j++) |
897 | o[j] = 0; |
62ddb51e |
898 | for (j = 0; j < (int)q[0]; j++) |
32874aea |
899 | o[i + j] = q[q[0] - j]; |
7cca0d81 |
900 | |
901 | /* Allocate a of size 2*pqlen for result */ |
a3412f52 |
902 | a = snewn(2 * pqlen, BignumInt); |
7cca0d81 |
903 | |
904 | /* Main computation */ |
9400cf6f |
905 | internal_mul(n, o, a, pqlen); |
32874aea |
906 | internal_mod(a, pqlen * 2, m, mlen, NULL, 0); |
7cca0d81 |
907 | |
908 | /* Fixup result in case the modulus was shifted */ |
909 | if (mshift) { |
32874aea |
910 | for (i = 2 * pqlen - mlen - 1; i < 2 * pqlen - 1; i++) |
a3412f52 |
911 | a[i] = (a[i] << mshift) | (a[i + 1] >> (BIGNUM_INT_BITS - mshift)); |
32874aea |
912 | a[2 * pqlen - 1] = a[2 * pqlen - 1] << mshift; |
913 | internal_mod(a, pqlen * 2, m, mlen, NULL, 0); |
914 | for (i = 2 * pqlen - 1; i >= 2 * pqlen - mlen; i--) |
a3412f52 |
915 | a[i] = (a[i] >> mshift) | (a[i - 1] << (BIGNUM_INT_BITS - mshift)); |
7cca0d81 |
916 | } |
917 | |
918 | /* Copy result to buffer */ |
32874aea |
919 | rlen = (mlen < pqlen * 2 ? mlen : pqlen * 2); |
80b10571 |
920 | result = newbn(rlen); |
921 | for (i = 0; i < rlen; i++) |
32874aea |
922 | result[result[0] - i] = a[i + 2 * pqlen - rlen]; |
923 | while (result[0] > 1 && result[result[0]] == 0) |
924 | result[0]--; |
7cca0d81 |
925 | |
926 | /* Free temporary arrays */ |
32874aea |
927 | for (i = 0; i < 2 * pqlen; i++) |
928 | a[i] = 0; |
929 | sfree(a); |
930 | for (i = 0; i < mlen; i++) |
931 | m[i] = 0; |
932 | sfree(m); |
933 | for (i = 0; i < pqlen; i++) |
934 | n[i] = 0; |
935 | sfree(n); |
936 | for (i = 0; i < pqlen; i++) |
937 | o[i] = 0; |
938 | sfree(o); |
59600f67 |
939 | |
940 | return result; |
7cca0d81 |
941 | } |
942 | |
943 | /* |
9400cf6f |
944 | * Compute p % mod. |
945 | * The most significant word of mod MUST be non-zero. |
946 | * We assume that the result array is the same size as the mod array. |
5c72ca61 |
947 | * We optionally write out a quotient if `quotient' is non-NULL. |
948 | * We can avoid writing out the result if `result' is NULL. |
9400cf6f |
949 | */ |
f28753ab |
950 | static void bigdivmod(Bignum p, Bignum mod, Bignum result, Bignum quotient) |
9400cf6f |
951 | { |
a3412f52 |
952 | BignumInt *n, *m; |
9400cf6f |
953 | int mshift; |
954 | int plen, mlen, i, j; |
955 | |
956 | /* Allocate m of size mlen, copy mod to m */ |
957 | /* We use big endian internally */ |
958 | mlen = mod[0]; |
a3412f52 |
959 | m = snewn(mlen, BignumInt); |
32874aea |
960 | for (j = 0; j < mlen; j++) |
961 | m[j] = mod[mod[0] - j]; |
9400cf6f |
962 | |
963 | /* Shift m left to make msb bit set */ |
a3412f52 |
964 | for (mshift = 0; mshift < BIGNUM_INT_BITS-1; mshift++) |
965 | if ((m[0] << mshift) & BIGNUM_TOP_BIT) |
32874aea |
966 | break; |
9400cf6f |
967 | if (mshift) { |
968 | for (i = 0; i < mlen - 1; i++) |
a3412f52 |
969 | m[i] = (m[i] << mshift) | (m[i + 1] >> (BIGNUM_INT_BITS - mshift)); |
32874aea |
970 | m[mlen - 1] = m[mlen - 1] << mshift; |
9400cf6f |
971 | } |
972 | |
973 | plen = p[0]; |
974 | /* Ensure plen > mlen */ |
32874aea |
975 | if (plen <= mlen) |
976 | plen = mlen + 1; |
9400cf6f |
977 | |
978 | /* Allocate n of size plen, copy p to n */ |
a3412f52 |
979 | n = snewn(plen, BignumInt); |
32874aea |
980 | for (j = 0; j < plen; j++) |
981 | n[j] = 0; |
62ddb51e |
982 | for (j = 1; j <= (int)p[0]; j++) |
32874aea |
983 | n[plen - j] = p[j]; |
9400cf6f |
984 | |
985 | /* Main computation */ |
986 | internal_mod(n, plen, m, mlen, quotient, mshift); |
987 | |
988 | /* Fixup result in case the modulus was shifted */ |
989 | if (mshift) { |
990 | for (i = plen - mlen - 1; i < plen - 1; i++) |
a3412f52 |
991 | n[i] = (n[i] << mshift) | (n[i + 1] >> (BIGNUM_INT_BITS - mshift)); |
32874aea |
992 | n[plen - 1] = n[plen - 1] << mshift; |
9400cf6f |
993 | internal_mod(n, plen, m, mlen, quotient, 0); |
994 | for (i = plen - 1; i >= plen - mlen; i--) |
a3412f52 |
995 | n[i] = (n[i] >> mshift) | (n[i - 1] << (BIGNUM_INT_BITS - mshift)); |
9400cf6f |
996 | } |
997 | |
998 | /* Copy result to buffer */ |
5c72ca61 |
999 | if (result) { |
62ddb51e |
1000 | for (i = 1; i <= (int)result[0]; i++) { |
5c72ca61 |
1001 | int j = plen - i; |
1002 | result[i] = j >= 0 ? n[j] : 0; |
1003 | } |
9400cf6f |
1004 | } |
1005 | |
1006 | /* Free temporary arrays */ |
32874aea |
1007 | for (i = 0; i < mlen; i++) |
1008 | m[i] = 0; |
1009 | sfree(m); |
1010 | for (i = 0; i < plen; i++) |
1011 | n[i] = 0; |
1012 | sfree(n); |
9400cf6f |
1013 | } |
1014 | |
1015 | /* |
7cca0d81 |
1016 | * Decrement a number. |
1017 | */ |
32874aea |
1018 | void decbn(Bignum bn) |
1019 | { |
7cca0d81 |
1020 | int i = 1; |
62ddb51e |
1021 | while (i < (int)bn[0] && bn[i] == 0) |
a3412f52 |
1022 | bn[i++] = BIGNUM_INT_MASK; |
7cca0d81 |
1023 | bn[i]--; |
1024 | } |
1025 | |
27cd7fc2 |
1026 | Bignum bignum_from_bytes(const unsigned char *data, int nbytes) |
32874aea |
1027 | { |
3709bfe9 |
1028 | Bignum result; |
1029 | int w, i; |
1030 | |
a3412f52 |
1031 | w = (nbytes + BIGNUM_INT_BYTES - 1) / BIGNUM_INT_BYTES; /* bytes->words */ |
3709bfe9 |
1032 | |
1033 | result = newbn(w); |
32874aea |
1034 | for (i = 1; i <= w; i++) |
1035 | result[i] = 0; |
1036 | for (i = nbytes; i--;) { |
1037 | unsigned char byte = *data++; |
a3412f52 |
1038 | result[1 + i / BIGNUM_INT_BYTES] |= byte << (8*i % BIGNUM_INT_BITS); |
3709bfe9 |
1039 | } |
1040 | |
32874aea |
1041 | while (result[0] > 1 && result[result[0]] == 0) |
1042 | result[0]--; |
3709bfe9 |
1043 | return result; |
1044 | } |
1045 | |
7cca0d81 |
1046 | /* |
2e85c969 |
1047 | * Read an SSH-1-format bignum from a data buffer. Return the number |
0016d70b |
1048 | * of bytes consumed, or -1 if there wasn't enough data. |
7cca0d81 |
1049 | */ |
0016d70b |
1050 | int ssh1_read_bignum(const unsigned char *data, int len, Bignum * result) |
32874aea |
1051 | { |
27cd7fc2 |
1052 | const unsigned char *p = data; |
7cca0d81 |
1053 | int i; |
1054 | int w, b; |
1055 | |
0016d70b |
1056 | if (len < 2) |
1057 | return -1; |
1058 | |
7cca0d81 |
1059 | w = 0; |
32874aea |
1060 | for (i = 0; i < 2; i++) |
1061 | w = (w << 8) + *p++; |
1062 | b = (w + 7) / 8; /* bits -> bytes */ |
7cca0d81 |
1063 | |
0016d70b |
1064 | if (len < b+2) |
1065 | return -1; |
1066 | |
32874aea |
1067 | if (!result) /* just return length */ |
1068 | return b + 2; |
a52f067e |
1069 | |
3709bfe9 |
1070 | *result = bignum_from_bytes(p, b); |
7cca0d81 |
1071 | |
3709bfe9 |
1072 | return p + b - data; |
7cca0d81 |
1073 | } |
5c58ad2d |
1074 | |
1075 | /* |
2e85c969 |
1076 | * Return the bit count of a bignum, for SSH-1 encoding. |
5c58ad2d |
1077 | */ |
32874aea |
1078 | int bignum_bitcount(Bignum bn) |
1079 | { |
a3412f52 |
1080 | int bitcount = bn[0] * BIGNUM_INT_BITS - 1; |
32874aea |
1081 | while (bitcount >= 0 |
a3412f52 |
1082 | && (bn[bitcount / BIGNUM_INT_BITS + 1] >> (bitcount % BIGNUM_INT_BITS)) == 0) bitcount--; |
5c58ad2d |
1083 | return bitcount + 1; |
1084 | } |
1085 | |
1086 | /* |
2e85c969 |
1087 | * Return the byte length of a bignum when SSH-1 encoded. |
5c58ad2d |
1088 | */ |
32874aea |
1089 | int ssh1_bignum_length(Bignum bn) |
1090 | { |
1091 | return 2 + (bignum_bitcount(bn) + 7) / 8; |
ddecd643 |
1092 | } |
1093 | |
1094 | /* |
2e85c969 |
1095 | * Return the byte length of a bignum when SSH-2 encoded. |
ddecd643 |
1096 | */ |
32874aea |
1097 | int ssh2_bignum_length(Bignum bn) |
1098 | { |
1099 | return 4 + (bignum_bitcount(bn) + 8) / 8; |
5c58ad2d |
1100 | } |
1101 | |
1102 | /* |
1103 | * Return a byte from a bignum; 0 is least significant, etc. |
1104 | */ |
32874aea |
1105 | int bignum_byte(Bignum bn, int i) |
1106 | { |
62ddb51e |
1107 | if (i >= (int)(BIGNUM_INT_BYTES * bn[0])) |
32874aea |
1108 | return 0; /* beyond the end */ |
5c58ad2d |
1109 | else |
a3412f52 |
1110 | return (bn[i / BIGNUM_INT_BYTES + 1] >> |
1111 | ((i % BIGNUM_INT_BYTES)*8)) & 0xFF; |
5c58ad2d |
1112 | } |
1113 | |
1114 | /* |
9400cf6f |
1115 | * Return a bit from a bignum; 0 is least significant, etc. |
1116 | */ |
32874aea |
1117 | int bignum_bit(Bignum bn, int i) |
1118 | { |
62ddb51e |
1119 | if (i >= (int)(BIGNUM_INT_BITS * bn[0])) |
32874aea |
1120 | return 0; /* beyond the end */ |
9400cf6f |
1121 | else |
a3412f52 |
1122 | return (bn[i / BIGNUM_INT_BITS + 1] >> (i % BIGNUM_INT_BITS)) & 1; |
9400cf6f |
1123 | } |
1124 | |
1125 | /* |
1126 | * Set a bit in a bignum; 0 is least significant, etc. |
1127 | */ |
32874aea |
1128 | void bignum_set_bit(Bignum bn, int bitnum, int value) |
1129 | { |
62ddb51e |
1130 | if (bitnum >= (int)(BIGNUM_INT_BITS * bn[0])) |
32874aea |
1131 | abort(); /* beyond the end */ |
9400cf6f |
1132 | else { |
a3412f52 |
1133 | int v = bitnum / BIGNUM_INT_BITS + 1; |
1134 | int mask = 1 << (bitnum % BIGNUM_INT_BITS); |
32874aea |
1135 | if (value) |
1136 | bn[v] |= mask; |
1137 | else |
1138 | bn[v] &= ~mask; |
9400cf6f |
1139 | } |
1140 | } |
1141 | |
1142 | /* |
2e85c969 |
1143 | * Write a SSH-1-format bignum into a buffer. It is assumed the |
5c58ad2d |
1144 | * buffer is big enough. Returns the number of bytes used. |
1145 | */ |
32874aea |
1146 | int ssh1_write_bignum(void *data, Bignum bn) |
1147 | { |
5c58ad2d |
1148 | unsigned char *p = data; |
1149 | int len = ssh1_bignum_length(bn); |
1150 | int i; |
ddecd643 |
1151 | int bitc = bignum_bitcount(bn); |
5c58ad2d |
1152 | |
1153 | *p++ = (bitc >> 8) & 0xFF; |
32874aea |
1154 | *p++ = (bitc) & 0xFF; |
1155 | for (i = len - 2; i--;) |
1156 | *p++ = bignum_byte(bn, i); |
5c58ad2d |
1157 | return len; |
1158 | } |
9400cf6f |
1159 | |
1160 | /* |
1161 | * Compare two bignums. Returns like strcmp. |
1162 | */ |
32874aea |
1163 | int bignum_cmp(Bignum a, Bignum b) |
1164 | { |
9400cf6f |
1165 | int amax = a[0], bmax = b[0]; |
1166 | int i = (amax > bmax ? amax : bmax); |
1167 | while (i) { |
a3412f52 |
1168 | BignumInt aval = (i > amax ? 0 : a[i]); |
1169 | BignumInt bval = (i > bmax ? 0 : b[i]); |
32874aea |
1170 | if (aval < bval) |
1171 | return -1; |
1172 | if (aval > bval) |
1173 | return +1; |
1174 | i--; |
9400cf6f |
1175 | } |
1176 | return 0; |
1177 | } |
1178 | |
1179 | /* |
1180 | * Right-shift one bignum to form another. |
1181 | */ |
32874aea |
1182 | Bignum bignum_rshift(Bignum a, int shift) |
1183 | { |
9400cf6f |
1184 | Bignum ret; |
1185 | int i, shiftw, shiftb, shiftbb, bits; |
a3412f52 |
1186 | BignumInt ai, ai1; |
9400cf6f |
1187 | |
ddecd643 |
1188 | bits = bignum_bitcount(a) - shift; |
a3412f52 |
1189 | ret = newbn((bits + BIGNUM_INT_BITS - 1) / BIGNUM_INT_BITS); |
9400cf6f |
1190 | |
1191 | if (ret) { |
a3412f52 |
1192 | shiftw = shift / BIGNUM_INT_BITS; |
1193 | shiftb = shift % BIGNUM_INT_BITS; |
1194 | shiftbb = BIGNUM_INT_BITS - shiftb; |
32874aea |
1195 | |
1196 | ai1 = a[shiftw + 1]; |
62ddb51e |
1197 | for (i = 1; i <= (int)ret[0]; i++) { |
32874aea |
1198 | ai = ai1; |
62ddb51e |
1199 | ai1 = (i + shiftw + 1 <= (int)a[0] ? a[i + shiftw + 1] : 0); |
a3412f52 |
1200 | ret[i] = ((ai >> shiftb) | (ai1 << shiftbb)) & BIGNUM_INT_MASK; |
32874aea |
1201 | } |
9400cf6f |
1202 | } |
1203 | |
1204 | return ret; |
1205 | } |
1206 | |
1207 | /* |
1208 | * Non-modular multiplication and addition. |
1209 | */ |
32874aea |
1210 | Bignum bigmuladd(Bignum a, Bignum b, Bignum addend) |
1211 | { |
9400cf6f |
1212 | int alen = a[0], blen = b[0]; |
1213 | int mlen = (alen > blen ? alen : blen); |
1214 | int rlen, i, maxspot; |
a3412f52 |
1215 | BignumInt *workspace; |
9400cf6f |
1216 | Bignum ret; |
1217 | |
1218 | /* mlen space for a, mlen space for b, 2*mlen for result */ |
a3412f52 |
1219 | workspace = snewn(mlen * 4, BignumInt); |
9400cf6f |
1220 | for (i = 0; i < mlen; i++) { |
62ddb51e |
1221 | workspace[0 * mlen + i] = (mlen - i <= (int)a[0] ? a[mlen - i] : 0); |
1222 | workspace[1 * mlen + i] = (mlen - i <= (int)b[0] ? b[mlen - i] : 0); |
9400cf6f |
1223 | } |
1224 | |
32874aea |
1225 | internal_mul(workspace + 0 * mlen, workspace + 1 * mlen, |
1226 | workspace + 2 * mlen, mlen); |
9400cf6f |
1227 | |
1228 | /* now just copy the result back */ |
1229 | rlen = alen + blen + 1; |
62ddb51e |
1230 | if (addend && rlen <= (int)addend[0]) |
32874aea |
1231 | rlen = addend[0] + 1; |
9400cf6f |
1232 | ret = newbn(rlen); |
1233 | maxspot = 0; |
62ddb51e |
1234 | for (i = 1; i <= (int)ret[0]; i++) { |
32874aea |
1235 | ret[i] = (i <= 2 * mlen ? workspace[4 * mlen - i] : 0); |
1236 | if (ret[i] != 0) |
1237 | maxspot = i; |
9400cf6f |
1238 | } |
1239 | ret[0] = maxspot; |
1240 | |
1241 | /* now add in the addend, if any */ |
1242 | if (addend) { |
a3412f52 |
1243 | BignumDblInt carry = 0; |
32874aea |
1244 | for (i = 1; i <= rlen; i++) { |
62ddb51e |
1245 | carry += (i <= (int)ret[0] ? ret[i] : 0); |
1246 | carry += (i <= (int)addend[0] ? addend[i] : 0); |
a3412f52 |
1247 | ret[i] = (BignumInt) carry & BIGNUM_INT_MASK; |
1248 | carry >>= BIGNUM_INT_BITS; |
32874aea |
1249 | if (ret[i] != 0 && i > maxspot) |
1250 | maxspot = i; |
1251 | } |
9400cf6f |
1252 | } |
1253 | ret[0] = maxspot; |
1254 | |
c523f55f |
1255 | sfree(workspace); |
9400cf6f |
1256 | return ret; |
1257 | } |
1258 | |
1259 | /* |
1260 | * Non-modular multiplication. |
1261 | */ |
32874aea |
1262 | Bignum bigmul(Bignum a, Bignum b) |
1263 | { |
9400cf6f |
1264 | return bigmuladd(a, b, NULL); |
1265 | } |
1266 | |
1267 | /* |
d737853b |
1268 | * Simple addition. |
1269 | */ |
1270 | Bignum bigadd(Bignum a, Bignum b) |
1271 | { |
1272 | int alen = a[0], blen = b[0]; |
1273 | int rlen = (alen > blen ? alen : blen) + 1; |
1274 | int i, maxspot; |
1275 | Bignum ret; |
1276 | BignumDblInt carry; |
1277 | |
1278 | ret = newbn(rlen); |
1279 | |
1280 | carry = 0; |
1281 | maxspot = 0; |
1282 | for (i = 1; i <= rlen; i++) { |
1283 | carry += (i <= (int)a[0] ? a[i] : 0); |
1284 | carry += (i <= (int)b[0] ? b[i] : 0); |
1285 | ret[i] = (BignumInt) carry & BIGNUM_INT_MASK; |
1286 | carry >>= BIGNUM_INT_BITS; |
1287 | if (ret[i] != 0 && i > maxspot) |
1288 | maxspot = i; |
1289 | } |
1290 | ret[0] = maxspot; |
1291 | |
1292 | return ret; |
1293 | } |
1294 | |
1295 | /* |
1296 | * Subtraction. Returns a-b, or NULL if the result would come out |
1297 | * negative (recall that this entire bignum module only handles |
1298 | * positive numbers). |
1299 | */ |
1300 | Bignum bigsub(Bignum a, Bignum b) |
1301 | { |
1302 | int alen = a[0], blen = b[0]; |
1303 | int rlen = (alen > blen ? alen : blen); |
1304 | int i, maxspot; |
1305 | Bignum ret; |
1306 | BignumDblInt carry; |
1307 | |
1308 | ret = newbn(rlen); |
1309 | |
1310 | carry = 1; |
1311 | maxspot = 0; |
1312 | for (i = 1; i <= rlen; i++) { |
1313 | carry += (i <= (int)a[0] ? a[i] : 0); |
1314 | carry += (i <= (int)b[0] ? b[i] ^ BIGNUM_INT_MASK : BIGNUM_INT_MASK); |
1315 | ret[i] = (BignumInt) carry & BIGNUM_INT_MASK; |
1316 | carry >>= BIGNUM_INT_BITS; |
1317 | if (ret[i] != 0 && i > maxspot) |
1318 | maxspot = i; |
1319 | } |
1320 | ret[0] = maxspot; |
1321 | |
1322 | if (!carry) { |
1323 | freebn(ret); |
1324 | return NULL; |
1325 | } |
1326 | |
1327 | return ret; |
1328 | } |
1329 | |
1330 | /* |
3709bfe9 |
1331 | * Create a bignum which is the bitmask covering another one. That |
1332 | * is, the smallest integer which is >= N and is also one less than |
1333 | * a power of two. |
1334 | */ |
32874aea |
1335 | Bignum bignum_bitmask(Bignum n) |
1336 | { |
3709bfe9 |
1337 | Bignum ret = copybn(n); |
1338 | int i; |
a3412f52 |
1339 | BignumInt j; |
3709bfe9 |
1340 | |
1341 | i = ret[0]; |
1342 | while (n[i] == 0 && i > 0) |
32874aea |
1343 | i--; |
3709bfe9 |
1344 | if (i <= 0) |
32874aea |
1345 | return ret; /* input was zero */ |
3709bfe9 |
1346 | j = 1; |
1347 | while (j < n[i]) |
32874aea |
1348 | j = 2 * j + 1; |
3709bfe9 |
1349 | ret[i] = j; |
1350 | while (--i > 0) |
a3412f52 |
1351 | ret[i] = BIGNUM_INT_MASK; |
3709bfe9 |
1352 | return ret; |
1353 | } |
1354 | |
1355 | /* |
5c72ca61 |
1356 | * Convert a (max 32-bit) long into a bignum. |
9400cf6f |
1357 | */ |
a3412f52 |
1358 | Bignum bignum_from_long(unsigned long nn) |
32874aea |
1359 | { |
9400cf6f |
1360 | Bignum ret; |
a3412f52 |
1361 | BignumDblInt n = nn; |
9400cf6f |
1362 | |
5c72ca61 |
1363 | ret = newbn(3); |
a3412f52 |
1364 | ret[1] = (BignumInt)(n & BIGNUM_INT_MASK); |
1365 | ret[2] = (BignumInt)((n >> BIGNUM_INT_BITS) & BIGNUM_INT_MASK); |
5c72ca61 |
1366 | ret[3] = 0; |
1367 | ret[0] = (ret[2] ? 2 : 1); |
32874aea |
1368 | return ret; |
9400cf6f |
1369 | } |
1370 | |
1371 | /* |
1372 | * Add a long to a bignum. |
1373 | */ |
a3412f52 |
1374 | Bignum bignum_add_long(Bignum number, unsigned long addendx) |
32874aea |
1375 | { |
1376 | Bignum ret = newbn(number[0] + 1); |
9400cf6f |
1377 | int i, maxspot = 0; |
a3412f52 |
1378 | BignumDblInt carry = 0, addend = addendx; |
9400cf6f |
1379 | |
62ddb51e |
1380 | for (i = 1; i <= (int)ret[0]; i++) { |
a3412f52 |
1381 | carry += addend & BIGNUM_INT_MASK; |
62ddb51e |
1382 | carry += (i <= (int)number[0] ? number[i] : 0); |
a3412f52 |
1383 | addend >>= BIGNUM_INT_BITS; |
1384 | ret[i] = (BignumInt) carry & BIGNUM_INT_MASK; |
1385 | carry >>= BIGNUM_INT_BITS; |
32874aea |
1386 | if (ret[i] != 0) |
1387 | maxspot = i; |
9400cf6f |
1388 | } |
1389 | ret[0] = maxspot; |
1390 | return ret; |
1391 | } |
1392 | |
1393 | /* |
1394 | * Compute the residue of a bignum, modulo a (max 16-bit) short. |
1395 | */ |
32874aea |
1396 | unsigned short bignum_mod_short(Bignum number, unsigned short modulus) |
1397 | { |
a3412f52 |
1398 | BignumDblInt mod, r; |
9400cf6f |
1399 | int i; |
1400 | |
1401 | r = 0; |
1402 | mod = modulus; |
1403 | for (i = number[0]; i > 0; i--) |
736cc6d1 |
1404 | r = (r * (BIGNUM_TOP_BIT % mod) * 2 + number[i] % mod) % mod; |
6e522441 |
1405 | return (unsigned short) r; |
9400cf6f |
1406 | } |
1407 | |
a3412f52 |
1408 | #ifdef DEBUG |
32874aea |
1409 | void diagbn(char *prefix, Bignum md) |
1410 | { |
9400cf6f |
1411 | int i, nibbles, morenibbles; |
1412 | static const char hex[] = "0123456789ABCDEF"; |
1413 | |
5c72ca61 |
1414 | debug(("%s0x", prefix ? prefix : "")); |
9400cf6f |
1415 | |
32874aea |
1416 | nibbles = (3 + bignum_bitcount(md)) / 4; |
1417 | if (nibbles < 1) |
1418 | nibbles = 1; |
1419 | morenibbles = 4 * md[0] - nibbles; |
1420 | for (i = 0; i < morenibbles; i++) |
5c72ca61 |
1421 | debug(("-")); |
32874aea |
1422 | for (i = nibbles; i--;) |
5c72ca61 |
1423 | debug(("%c", |
1424 | hex[(bignum_byte(md, i / 2) >> (4 * (i % 2))) & 0xF])); |
9400cf6f |
1425 | |
32874aea |
1426 | if (prefix) |
5c72ca61 |
1427 | debug(("\n")); |
1428 | } |
f28753ab |
1429 | #endif |
5c72ca61 |
1430 | |
1431 | /* |
1432 | * Simple division. |
1433 | */ |
1434 | Bignum bigdiv(Bignum a, Bignum b) |
1435 | { |
1436 | Bignum q = newbn(a[0]); |
1437 | bigdivmod(a, b, NULL, q); |
1438 | return q; |
1439 | } |
1440 | |
1441 | /* |
1442 | * Simple remainder. |
1443 | */ |
1444 | Bignum bigmod(Bignum a, Bignum b) |
1445 | { |
1446 | Bignum r = newbn(b[0]); |
1447 | bigdivmod(a, b, r, NULL); |
1448 | return r; |
9400cf6f |
1449 | } |
1450 | |
1451 | /* |
1452 | * Greatest common divisor. |
1453 | */ |
32874aea |
1454 | Bignum biggcd(Bignum av, Bignum bv) |
1455 | { |
9400cf6f |
1456 | Bignum a = copybn(av); |
1457 | Bignum b = copybn(bv); |
1458 | |
9400cf6f |
1459 | while (bignum_cmp(b, Zero) != 0) { |
32874aea |
1460 | Bignum t = newbn(b[0]); |
5c72ca61 |
1461 | bigdivmod(a, b, t, NULL); |
32874aea |
1462 | while (t[0] > 1 && t[t[0]] == 0) |
1463 | t[0]--; |
1464 | freebn(a); |
1465 | a = b; |
1466 | b = t; |
9400cf6f |
1467 | } |
1468 | |
1469 | freebn(b); |
1470 | return a; |
1471 | } |
1472 | |
1473 | /* |
1474 | * Modular inverse, using Euclid's extended algorithm. |
1475 | */ |
32874aea |
1476 | Bignum modinv(Bignum number, Bignum modulus) |
1477 | { |
9400cf6f |
1478 | Bignum a = copybn(modulus); |
1479 | Bignum b = copybn(number); |
1480 | Bignum xp = copybn(Zero); |
1481 | Bignum x = copybn(One); |
1482 | int sign = +1; |
1483 | |
1484 | while (bignum_cmp(b, One) != 0) { |
32874aea |
1485 | Bignum t = newbn(b[0]); |
1486 | Bignum q = newbn(a[0]); |
5c72ca61 |
1487 | bigdivmod(a, b, t, q); |
32874aea |
1488 | while (t[0] > 1 && t[t[0]] == 0) |
1489 | t[0]--; |
1490 | freebn(a); |
1491 | a = b; |
1492 | b = t; |
1493 | t = xp; |
1494 | xp = x; |
1495 | x = bigmuladd(q, xp, t); |
1496 | sign = -sign; |
1497 | freebn(t); |
75374b2f |
1498 | freebn(q); |
9400cf6f |
1499 | } |
1500 | |
1501 | freebn(b); |
1502 | freebn(a); |
1503 | freebn(xp); |
1504 | |
1505 | /* now we know that sign * x == 1, and that x < modulus */ |
1506 | if (sign < 0) { |
32874aea |
1507 | /* set a new x to be modulus - x */ |
1508 | Bignum newx = newbn(modulus[0]); |
a3412f52 |
1509 | BignumInt carry = 0; |
32874aea |
1510 | int maxspot = 1; |
1511 | int i; |
1512 | |
62ddb51e |
1513 | for (i = 1; i <= (int)newx[0]; i++) { |
1514 | BignumInt aword = (i <= (int)modulus[0] ? modulus[i] : 0); |
1515 | BignumInt bword = (i <= (int)x[0] ? x[i] : 0); |
32874aea |
1516 | newx[i] = aword - bword - carry; |
1517 | bword = ~bword; |
1518 | carry = carry ? (newx[i] >= bword) : (newx[i] > bword); |
1519 | if (newx[i] != 0) |
1520 | maxspot = i; |
1521 | } |
1522 | newx[0] = maxspot; |
1523 | freebn(x); |
1524 | x = newx; |
9400cf6f |
1525 | } |
1526 | |
1527 | /* and return. */ |
1528 | return x; |
1529 | } |
6e522441 |
1530 | |
1531 | /* |
1532 | * Render a bignum into decimal. Return a malloced string holding |
1533 | * the decimal representation. |
1534 | */ |
32874aea |
1535 | char *bignum_decimal(Bignum x) |
1536 | { |
6e522441 |
1537 | int ndigits, ndigit; |
1538 | int i, iszero; |
a3412f52 |
1539 | BignumDblInt carry; |
6e522441 |
1540 | char *ret; |
a3412f52 |
1541 | BignumInt *workspace; |
6e522441 |
1542 | |
1543 | /* |
1544 | * First, estimate the number of digits. Since log(10)/log(2) |
1545 | * is just greater than 93/28 (the joys of continued fraction |
1546 | * approximations...) we know that for every 93 bits, we need |
1547 | * at most 28 digits. This will tell us how much to malloc. |
1548 | * |
1549 | * Formally: if x has i bits, that means x is strictly less |
1550 | * than 2^i. Since 2 is less than 10^(28/93), this is less than |
1551 | * 10^(28i/93). We need an integer power of ten, so we must |
1552 | * round up (rounding down might make it less than x again). |
1553 | * Therefore if we multiply the bit count by 28/93, rounding |
1554 | * up, we will have enough digits. |
74c79ce8 |
1555 | * |
1556 | * i=0 (i.e., x=0) is an irritating special case. |
6e522441 |
1557 | */ |
ddecd643 |
1558 | i = bignum_bitcount(x); |
74c79ce8 |
1559 | if (!i) |
1560 | ndigits = 1; /* x = 0 */ |
1561 | else |
1562 | ndigits = (28 * i + 92) / 93; /* multiply by 28/93 and round up */ |
32874aea |
1563 | ndigits++; /* allow for trailing \0 */ |
3d88e64d |
1564 | ret = snewn(ndigits, char); |
6e522441 |
1565 | |
1566 | /* |
1567 | * Now allocate some workspace to hold the binary form as we |
1568 | * repeatedly divide it by ten. Initialise this to the |
1569 | * big-endian form of the number. |
1570 | */ |
a3412f52 |
1571 | workspace = snewn(x[0], BignumInt); |
62ddb51e |
1572 | for (i = 0; i < (int)x[0]; i++) |
32874aea |
1573 | workspace[i] = x[x[0] - i]; |
6e522441 |
1574 | |
1575 | /* |
1576 | * Next, write the decimal number starting with the last digit. |
1577 | * We use ordinary short division, dividing 10 into the |
1578 | * workspace. |
1579 | */ |
32874aea |
1580 | ndigit = ndigits - 1; |
6e522441 |
1581 | ret[ndigit] = '\0'; |
1582 | do { |
32874aea |
1583 | iszero = 1; |
1584 | carry = 0; |
62ddb51e |
1585 | for (i = 0; i < (int)x[0]; i++) { |
a3412f52 |
1586 | carry = (carry << BIGNUM_INT_BITS) + workspace[i]; |
1587 | workspace[i] = (BignumInt) (carry / 10); |
32874aea |
1588 | if (workspace[i]) |
1589 | iszero = 0; |
1590 | carry %= 10; |
1591 | } |
1592 | ret[--ndigit] = (char) (carry + '0'); |
6e522441 |
1593 | } while (!iszero); |
1594 | |
1595 | /* |
1596 | * There's a chance we've fallen short of the start of the |
1597 | * string. Correct if so. |
1598 | */ |
1599 | if (ndigit > 0) |
32874aea |
1600 | memmove(ret, ret + ndigit, ndigits - ndigit); |
6e522441 |
1601 | |
1602 | /* |
1603 | * Done. |
1604 | */ |
c523f55f |
1605 | sfree(workspace); |
6e522441 |
1606 | return ret; |
1607 | } |
f3c29e34 |
1608 | |
1609 | #ifdef TESTBN |
1610 | |
1611 | #include <stdio.h> |
1612 | #include <stdlib.h> |
1613 | #include <ctype.h> |
1614 | |
1615 | /* |
1616 | * gcc -g -O0 -DTESTBN -o testbn sshbn.c misc.c -I unix -I charset |
1617 | */ |
1618 | |
1619 | void modalfatalbox(char *p, ...) |
1620 | { |
1621 | va_list ap; |
1622 | fprintf(stderr, "FATAL ERROR: "); |
1623 | va_start(ap, p); |
1624 | vfprintf(stderr, p, ap); |
1625 | va_end(ap); |
1626 | fputc('\n', stderr); |
1627 | exit(1); |
1628 | } |
1629 | |
1630 | #define fromxdigit(c) ( (c)>'9' ? ((c)&0xDF) - 'A' + 10 : (c) - '0' ) |
1631 | |
1632 | int main(int argc, char **argv) |
1633 | { |
1634 | char *buf; |
1635 | int line = 0; |
1636 | int passes = 0, fails = 0; |
1637 | |
1638 | while ((buf = fgetline(stdin)) != NULL) { |
1639 | int maxlen = strlen(buf); |
1640 | unsigned char *data = snewn(maxlen, unsigned char); |
1641 | unsigned char *ptrs[4], *q; |
1642 | int ptrnum; |
1643 | char *bufp = buf; |
1644 | |
1645 | line++; |
1646 | |
1647 | q = data; |
1648 | ptrnum = 0; |
1649 | |
1650 | while (*bufp) { |
1651 | char *start, *end; |
1652 | int i; |
1653 | |
1654 | while (*bufp && !isxdigit((unsigned char)*bufp)) |
1655 | bufp++; |
1656 | start = bufp; |
1657 | |
1658 | if (!*bufp) |
1659 | break; |
1660 | |
1661 | while (*bufp && isxdigit((unsigned char)*bufp)) |
1662 | bufp++; |
1663 | end = bufp; |
1664 | |
1665 | if (ptrnum >= lenof(ptrs)) |
1666 | break; |
1667 | ptrs[ptrnum++] = q; |
1668 | |
1669 | for (i = -((end - start) & 1); i < end-start; i += 2) { |
1670 | unsigned char val = (i < 0 ? 0 : fromxdigit(start[i])); |
1671 | val = val * 16 + fromxdigit(start[i+1]); |
1672 | *q++ = val; |
1673 | } |
1674 | |
1675 | ptrs[ptrnum] = q; |
1676 | } |
1677 | |
1678 | if (ptrnum == 3) { |
1679 | Bignum a = bignum_from_bytes(ptrs[0], ptrs[1]-ptrs[0]); |
1680 | Bignum b = bignum_from_bytes(ptrs[1], ptrs[2]-ptrs[1]); |
1681 | Bignum c = bignum_from_bytes(ptrs[2], ptrs[3]-ptrs[2]); |
1682 | Bignum p = bigmul(a, b); |
1683 | |
1684 | if (bignum_cmp(c, p) == 0) { |
1685 | passes++; |
1686 | } else { |
1687 | char *as = bignum_decimal(a); |
1688 | char *bs = bignum_decimal(b); |
1689 | char *cs = bignum_decimal(c); |
1690 | char *ps = bignum_decimal(p); |
1691 | |
1692 | printf("%d: fail: %s * %s gave %s expected %s\n", |
1693 | line, as, bs, ps, cs); |
1694 | fails++; |
1695 | |
1696 | sfree(as); |
1697 | sfree(bs); |
1698 | sfree(cs); |
1699 | sfree(ps); |
1700 | } |
1701 | freebn(a); |
1702 | freebn(b); |
1703 | freebn(c); |
1704 | freebn(p); |
1705 | } |
1706 | sfree(buf); |
1707 | sfree(data); |
1708 | } |
1709 | |
1710 | printf("passed %d failed %d total %d\n", passes, fails, passes+fails); |
1711 | return fails != 0; |
1712 | } |
1713 | |
1714 | #endif |