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