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