示例#1
0
static int
testdiv(void)
{
	short ro;
	MINT *t2;

	mp_mdiv(c42, c5, t0, t1);
	testmcmp(t0, c8, "mdiv0");
	testmcmp(t1, c2, "mdiv1");

	mp_mdiv(c10, c8, t0, t1);
	testmcmp(t0, c1, "mdiv2");
	testmcmp(t1, c2, "mdiv3");

	mp_sdiv(c42, 5, t0, &ro);
	testmcmp(t0, c8, "sdiv0");
	t2 = mp_itom(ro); // Simpler to use common testmcmp()
	testmcmp(t2, c2, "sdiv1");
	mp_mfree(t2);

	mp_sdiv(c10, 8, t0, &ro);
	testmcmp(t0, c1, "sdiv2");
	t2 = mp_itom(ro); // Simpler to use common testmcmp()
	testmcmp(t2, c2, "sdiv3");
	mp_mfree(t2);
}
示例#2
0
void
mp_invert(MINT *x1, MINT *x0, MINT *c)
{
	MINT u2, u3;
	MINT v2, v3;
	MINT zero;
	MINT q, r;
	MINT t;
	MINT x0_prime;
	static MINT *one = NULL;

	/*
	 * Minimize calls to allocators.  Don't use pointers for local
	 * variables, for the one "initialized" multiple precision
	 * variable, do it just once.
	 */
	if (one == NULL)
		one = mp_itom(1);

	zero.len = q.len = r.len = t.len = 0;

	x0_prime.len = u2.len = u3.len = 0;
	_mp_move(x0, &u3);
	_mp_move(x0, &x0_prime);

	v2.len = v3.len = 0;
	_mp_move(one, &v2);
	_mp_move(x1, &v3);

	while (mp_mcmp(&v3, &zero) != 0) {
		/* invariant: x0*u1 + x1*u2 = u3 */
		/* invariant: x0*v1 + x2*v2 = v3 */
		/* invariant: x(n+1) = x(n-1) % x(n) */
		mp_mdiv(&u3, &v3, &q, &r);
		_mp_move(&v3, &u3);
		_mp_move(&r, &v3);

		mp_mult(&q, &v2, &t);
		mp_msub(&u2, &t, &t);
		_mp_move(&v2, &u2);
		_mp_move(&t, &v2);
	}
	/* now x0*u1 + x1*u2 == 1, therefore,  (u2*x1) % x0  == 1 */
	_mp_move(&u2, c);
	if (mp_mcmp(c, &zero) < 0) {
		mp_madd(&x0_prime, c, c);
	}
	_mp_xfree(&zero);
	_mp_xfree(&v2);
	_mp_xfree(&v3);
	_mp_xfree(&u2);
	_mp_xfree(&u3);
	_mp_xfree(&q);
	_mp_xfree(&r);
	_mp_xfree(&t);
}
示例#3
0
/*
 * Choose middle 64 bits of the common key to use as our des key, possibly
 * overwriting the lower order bits by setting parity. 
 */
static void
extractdeskey(MINT *ck, DesData *deskey)
{
        MINT *a;
        MINT *z;
        short r;
        int i;
        short base = (1 << 8);
        char *k;

        z = mp_itom(0);
        a = mp_itom(0);
        mp_madd(ck, z, a);
        for (i = 0; i < ((KEYSIZE - 64) / 2) / 8; i++) {
                mp_sdiv(a, base, a, &r);
        }
        k = (char *)deskey;
        for (i = 0; i < 8; i++) {
                mp_sdiv(a, base, a, &r);
                *k++ = r;
        }
	mp_mfree(z);
        mp_mfree(a);
}
示例#4
0
/*
 * Choose top 128 bits of the common key to use as our idea key.
 */
static void
extractideakey(MINT *ck, IdeaData *ideakey)
{
        MINT *a;
        MINT *z;
        short r;
        int i;
        short base = (1 << 8);
        char *k;

        z = mp_itom(0);
        a = mp_itom(0);
        mp_madd(ck, z, a);
        for (i = 0; i < ((KEYSIZE - 128) / 8); i++) {
                mp_sdiv(a, base, a, &r);
        }
        k = (char *)ideakey;
        for (i = 0; i < 16; i++) {
                mp_sdiv(a, base, a, &r);
                *k++ = r;
        }
	mp_mfree(z);
        mp_mfree(a);
}
示例#5
0
amp *
mp_lcm(amp *result, amp *a, amp *b)

{
  amp	*g0;
  amp	*g1;
  amp	*g2;

  g0 = mp_mul(a,b);
  g1 = mp_gcd((amp*)0,a,b);
  g2 = mp_itom(0);
  mp_div_to(g0,g0,g1,g2);
  mp_free(g1);
  mp_free(g2);
  if (result) {
    mp_copy_to(result,g0);
    mp_free(g0);
    return result;
  } else {
    return g0;
  }
}
示例#6
0
文件: old_mp.c 项目: andreiw/polaris
MINT *itom(short n) { return (mp_itom(n)); }
示例#7
0
/*
 * This program performs some very basic tests of libmp(3).  It is by
 * no means expected to perform a complete test of the library for
 * correctness, but is meant to test the API to make sure libmp (or
 * libcrypto) updates don't totally break the library.
 */
int
main(int argc, char *argv[])
{

	printf("1..25\n");

	/*
	 * Init "constants" variables - done in this somewhat
	 * cumbersome way to in theory be able to check for memory
	 * leaks.
	 */
	c0 = mp_itom(0);
	c1 = mp_itom(1);
	c2 = mp_itom(2);
	c3 = mp_itom(3);
	c5 = mp_itom(5);
	c6 = mp_itom(6);
	c8 = mp_itom(8);
	c10 = mp_itom(10);
	c14 = mp_itom(14);
	c15 = mp_itom(15);
	c25 = mp_itom(25);
	c42 = mp_itom(42);
	c43 = mp_itom(43);
	c44 = mp_itom(44);
	c45 = mp_itom(45);

	// Init temp variables
	t0 = mp_itom(0);
	t1 = mp_itom(0);

	// Run tests
	testsimpel();
	testgcd();
	testdiv();
	testmult();
	testpow();
	testmsqrt();

	// Cleanup
	mp_mfree(c0);
	mp_mfree(c1);
	mp_mfree(c2);
	mp_mfree(c3);
	mp_mfree(c5);
	mp_mfree(c6);
	mp_mfree(c8);
	mp_mfree(c10);
	mp_mfree(c14);
	mp_mfree(c15);
	mp_mfree(c25);
	mp_mfree(c42);
	mp_mfree(c43);
	mp_mfree(c44);
	mp_mfree(c45);
	mp_mfree(t0);
	mp_mfree(t1);

	return (EX_OK);
}
示例#8
0
文件: generic.c 项目: coyizumi/cs111
		seed[i] = (arc4random() & 0xff) ^ pass[i % 8];
	}
}

/*
 * Generate a random public/secret key pair
 */
void
genkeys(char *public, char *secret, char *pass)
{
	unsigned int i;

#   define BASEBITS (8*sizeof (short) - 1)
#	define BASE		(1 << BASEBITS)

	MINT *pk = mp_itom(0);
	MINT *sk = mp_itom(0);
	MINT *tmp;
	MINT *base = mp_itom(BASE);
	MINT *root = mp_itom(PROOT);
	MINT *modulus = mp_xtom(HEXMODULUS);
	short r;
	unsigned short seed[KEYSIZE/BASEBITS + 1];
	char *xkey;

	getseed((char *)seed, sizeof (seed), (u_char *)pass);
	for (i = 0; i < KEYSIZE/BASEBITS + 1; i++) {
		r = seed[i] % BASE;
		tmp = mp_itom(r);
		mp_mult(sk, base, sk);
		mp_madd(sk, tmp, sk);