1 /* Bignum routines adapted from PUTTY sources. PuTTY copyright notice follows.
3 * PuTTY is copyright 1997-2007 Simon Tatham.
5 * Portions copyright Robert de Bath, Joris van Rantwijk, Delian
6 * Delchev, Andreas Schultz, Jeroen Massar, Wez Furlong, Nicolas Barry,
7 * Justin Bradford, Ben Harris, Malcolm Smith, Ahmad Khalifa, Markus
8 * Kuhn, and CORE SDI S.A.
10 * Permission is hereby granted, free of charge, to any person
11 * obtaining a copy of this software and associated documentation files
12 * (the "Software"), to deal in the Software without restriction,
13 * including without limitation the rights to use, copy, modify, merge,
14 * publish, distribute, sublicense, and/or sell copies of the Software,
15 * and to permit persons to whom the Software is furnished to do so,
16 * subject to the following conditions:
18 * The above copyright notice and this permission notice shall be
19 * included in all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 * NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE
25 * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
26 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 #ifndef CONFIG_MACH_IPMATE
34 #include "dwc_modpow.h"
36 #define BIGNUM_INT_MASK 0xFFFFFFFFUL
37 #define BIGNUM_TOP_BIT 0x80000000UL
38 #define BIGNUM_INT_BITS 32
40 static void *snmalloc(void *mem_ctx, size_t n, size_t size)
46 p = dwc_alloc(mem_ctx, size);
50 #define snewn(ctx, n, type) ((type *)snmalloc((ctx), (n), sizeof(type)))
51 #define sfree dwc_free
55 * * Do not call the DIVMOD_WORD macro with expressions such as array
56 * subscripts, as some implementations object to this (see below).
57 * * Note that none of the division methods below will cope if the
58 * quotient won't fit into BIGNUM_INT_BITS. Callers should be careful
60 * If this condition occurs, in the case of the x86 DIV instruction,
61 * an overflow exception will occur, which (according to a correspondent)
62 * will manifest on Windows as something like
63 * 0xC0000095: Integer overflow
64 * The C variant won't give the right answer, either.
67 #define MUL_WORD(w1, w2) ((BignumDblInt)w1 * w2)
69 #if defined __GNUC__ && defined __i386__
70 #define DIVMOD_WORD(q, r, hi, lo, w) \
72 "=d" (r), "=a" (q) : \
73 "r" (w), "d" (hi), "a" (lo))
75 #define DIVMOD_WORD(q, r, hi, lo, w) do { \
76 BignumDblInt n = (((BignumDblInt)hi) << BIGNUM_INT_BITS) | lo; \
82 #define BIGNUM_INT_BYTES (BIGNUM_INT_BITS / 8)
84 #define BIGNUM_INTERNAL
86 static Bignum newbn(void *mem_ctx, int length)
88 Bignum b = snewn(mem_ctx, length + 1, BignumInt);
90 /* abort(); */ /* FIXME */
91 DWC_MEMSET(b, 0, (length + 1) * sizeof(*b));
96 void freebn(void *mem_ctx, Bignum b)
99 * Burn the evidence, just in case.
101 DWC_MEMSET(b, 0, sizeof(b[0]) * (b[0] + 1));
107 * Input is in the first len words of a and b.
108 * Result is returned in the first 2*len words of c.
110 static void internal_mul(BignumInt *a, BignumInt *b, BignumInt *c, int len)
115 for (j = 0; j < 2 * len; j++)
118 for (i = len - 1; i >= 0; i--) {
120 for (j = len - 1; j >= 0; j--) {
121 t += MUL_WORD(a[i], (BignumDblInt) b[j]);
122 t += (BignumDblInt) c[i + j + 1];
123 c[i + j + 1] = (BignumInt) t;
124 t = t >> BIGNUM_INT_BITS;
126 c[i] = (BignumInt) t;
130 static void internal_add_shifted(BignumInt *number, unsigned n, int shift)
132 int word = 1 + (shift / BIGNUM_INT_BITS);
133 int bshift = shift % BIGNUM_INT_BITS;
136 addend = (BignumDblInt) n << bshift;
139 addend += number[word];
140 number[word] = (BignumInt) addend & BIGNUM_INT_MASK;
141 addend >>= BIGNUM_INT_BITS;
148 * Input in first alen words of a and first mlen words of m.
149 * Output in first alen words of a
150 * (of which first alen-mlen words will be zero).
151 * The MSW of m MUST have its high bit set.
152 * Quotient is accumulated in the `quotient' array, which is a Bignum
153 * rather than the internal bigendian format. Quotient parts are shifted
154 * left by `qshift' before adding into quot.
156 static void internal_mod(BignumInt *a, int alen,
157 BignumInt *m, int mlen, BignumInt *quot, int qshift)
169 for (i = 0; i <= alen - mlen; i++) {
171 unsigned int q, r, c, ai1;
185 /* Find q = h:a[i] / m0 */
190 * To illustrate it, suppose a BignumInt is 8 bits, and
191 * we are dividing (say) A1:23:45:67 by A1:B2:C3. Then
192 * our initial division will be 0xA123 / 0xA1, which
193 * will give a quotient of 0x100 and a divide overflow.
194 * However, the invariants in this division algorithm
195 * are not violated, since the full number A1:23:... is
196 * _less_ than the quotient prefix A1:B2:... and so the
197 * following correction loop would have sorted it out.
199 * In this situation we set q to be the largest
200 * quotient we _can_ stomach (0xFF, of course).
204 /* Macro doesn't want an array subscript expression passed
205 * into it (see definition), so use a temporary. */
206 BignumInt tmplo = a[i];
207 DIVMOD_WORD(q, r, h, tmplo, m0);
209 /* Refine our estimate of q by looking at
210 h:a[i]:a[i+1] / m0:m1 */
212 if (t > ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1) {
215 r = (r + m0) & BIGNUM_INT_MASK; /* overflow? */
216 if (r >= (BignumDblInt) m0 &&
218 ((BignumDblInt) r << BIGNUM_INT_BITS) + ai1)
223 /* Subtract q * m from a[i...] */
225 for (k = mlen - 1; k >= 0; k--) {
226 t = MUL_WORD(q, m[k]);
228 c = (unsigned)(t >> BIGNUM_INT_BITS);
229 if ((BignumInt) t > a[i + k])
231 a[i + k] -= (BignumInt) t;
234 /* Add back m in case of borrow */
237 for (k = mlen - 1; k >= 0; k--) {
240 a[i + k] = (BignumInt) t;
241 t = t >> BIGNUM_INT_BITS;
246 internal_add_shifted(quot, q,
247 qshift + BIGNUM_INT_BITS * (alen -
255 * The most significant word of mod MUST be non-zero.
256 * We assume that the result array is the same size as the mod array.
257 * We optionally write out a quotient if `quotient' is non-NULL.
258 * We can avoid writing out the result if `result' is NULL.
260 void bigdivmod(void *mem_ctx, Bignum p, Bignum mod, Bignum result,
265 int plen, mlen, i, j;
267 /* Allocate m of size mlen, copy mod to m */
268 /* We use big endian internally */
270 m = snewn(mem_ctx, mlen, BignumInt);
272 /* abort(); */ /* FIXME */
273 for (j = 0; j < mlen; j++)
274 m[j] = mod[mod[0] - j];
276 /* Shift m left to make msb bit set */
277 for (mshift = 0; mshift < BIGNUM_INT_BITS - 1; mshift++)
278 if ((m[0] << mshift) & BIGNUM_TOP_BIT)
281 for (i = 0; i < mlen - 1; i++)
283 (m[i] << mshift) | (m[i + 1] >>
284 (BIGNUM_INT_BITS - mshift));
285 m[mlen - 1] = m[mlen - 1] << mshift;
289 /* Ensure plen > mlen */
293 /* Allocate n of size plen, copy p to n */
294 n = snewn(mem_ctx, plen, BignumInt);
296 /* abort(); */ /* FIXME */
297 for (j = 0; j < plen; j++)
299 for (j = 1; j <= (int)p[0]; j++)
302 /* Main computation */
303 internal_mod(n, plen, m, mlen, quotient, mshift);
305 /* Fixup result in case the modulus was shifted */
307 for (i = plen - mlen - 1; i < plen - 1; i++)
309 (n[i] << mshift) | (n[i + 1] >>
310 (BIGNUM_INT_BITS - mshift));
311 n[plen - 1] = n[plen - 1] << mshift;
312 internal_mod(n, plen, m, mlen, quotient, 0);
313 for (i = plen - 1; i >= plen - mlen; i--)
315 (n[i] >> mshift) | (n[i - 1] <<
316 (BIGNUM_INT_BITS - mshift));
319 /* Copy result to buffer */
321 for (i = 1; i <= (int)result[0]; i++) {
323 result[i] = j >= 0 ? n[j] : 0;
327 /* Free temporary arrays */
328 for (i = 0; i < mlen; i++)
331 for (i = 0; i < plen; i++)
339 Bignum bigmod(void *mem_ctx, Bignum a, Bignum b)
341 Bignum r = newbn(mem_ctx, b[0]);
342 bigdivmod(mem_ctx, a, b, r, NULL);
347 * Compute (base ^ exp) % mod.
349 Bignum dwc_modpow(void *mem_ctx, Bignum base_in, Bignum exp, Bignum mod)
351 BignumInt *a, *b, *n, *m;
357 * The most significant word of mod needs to be non-zero. It
358 * should already be, but let's make sure.
360 /* assert(mod[mod[0]] != 0); */
363 * Make sure the base is smaller than the modulus, by reducing
364 * it modulo the modulus if not.
366 base = bigmod(mem_ctx, base_in, mod);
368 /* Allocate m of size mlen, copy mod to m */
369 /* We use big endian internally */
371 m = snewn(mem_ctx, mlen, BignumInt);
373 /* abort(); */ /* FIXME */
374 for (j = 0; j < mlen; j++)
375 m[j] = mod[mod[0] - j];
377 /* Shift m left to make msb bit set */
378 for (mshift = 0; mshift < BIGNUM_INT_BITS - 1; mshift++)
379 if ((m[0] << mshift) & BIGNUM_TOP_BIT)
382 for (i = 0; i < mlen - 1; i++)
384 (m[i] << mshift) | (m[i + 1] >>
385 (BIGNUM_INT_BITS - mshift));
386 m[mlen - 1] = m[mlen - 1] << mshift;
389 /* Allocate n of size mlen, copy base to n */
390 n = snewn(mem_ctx, mlen, BignumInt);
392 /* abort(); */ /* FIXME */
394 for (j = 0; j < i; j++)
396 for (j = 0; j < base[0]; j++)
397 n[i + j] = base[base[0] - j];
399 /* Allocate a and b of size 2*mlen. Set a = 1 */
400 a = snewn(mem_ctx, 2 * mlen, BignumInt);
402 /* abort(); */ /* FIXME */
403 b = snewn(mem_ctx, 2 * mlen, BignumInt);
405 /* abort(); */ /* FIXME */
406 for (i = 0; i < 2 * mlen; i++)
410 /* Skip leading zero bits of exp. */
412 j = BIGNUM_INT_BITS - 1;
413 while (i < exp[0] && (exp[exp[0] - i] & (1 << j)) == 0) {
417 j = BIGNUM_INT_BITS - 1;
421 /* Main computation */
424 internal_mul(a + mlen, a + mlen, b, mlen);
425 internal_mod(b, mlen * 2, m, mlen, NULL, 0);
426 if ((exp[exp[0] - i] & (1 << j)) != 0) {
427 internal_mul(b + mlen, n, a, mlen);
428 internal_mod(a, mlen * 2, m, mlen, NULL, 0);
438 j = BIGNUM_INT_BITS - 1;
441 /* Fixup result in case the modulus was shifted */
443 for (i = mlen - 1; i < 2 * mlen - 1; i++)
445 (a[i] << mshift) | (a[i + 1] >>
446 (BIGNUM_INT_BITS - mshift));
447 a[2 * mlen - 1] = a[2 * mlen - 1] << mshift;
448 internal_mod(a, mlen * 2, m, mlen, NULL, 0);
449 for (i = 2 * mlen - 1; i >= mlen; i--)
451 (a[i] >> mshift) | (a[i - 1] <<
452 (BIGNUM_INT_BITS - mshift));
455 /* Copy result to buffer */
456 result = newbn(mem_ctx, mod[0]);
457 for (i = 0; i < mlen; i++)
458 result[result[0] - i] = a[i + mlen];
459 while (result[0] > 1 && result[result[0]] == 0)
462 /* Free temporary arrays */
463 for (i = 0; i < 2 * mlen; i++)
466 for (i = 0; i < 2 * mlen; i++)
469 for (i = 0; i < mlen; i++)
472 for (i = 0; i < mlen; i++)
476 freebn(mem_ctx, base);
483 static __u32 dh_p[] = {
583 static __u32 dh_a[] = {
595 static __u32 dh_b[] = {
607 static __u32 dh_g[] = {
616 k = dwc_modpow(NULL, dh_g, dh_a, dh_p);
619 for (i = 0; i < k[0]; i++) {
620 __u32 word32 = k[k[0] - i];
621 __u16 l = word32 & 0xffff;
622 __u16 m = (word32 & 0xffff0000) >> 16;
623 printf("%04x %04x ", m, l);
629 if ((k[0] == 0x60) && (k[1] == 0x28e490e5) && (k[0x60] == 0x5a0d3d4e)) {
637 #endif /* UNITTEST */
639 #endif /* CONFIG_MACH_IPMATE */
641 #endif /*DWC_CRYPTOLIB */