111786Ssam #ifndef lint
2*37916Sbostic static char sccsid[] = "@(#)lib.c	4.2 05/11/89";
311786Ssam #endif
411786Ssam 
511786Ssam #include "xmail.h"
6*37916Sbostic #include "pathnames.h"
7*37916Sbostic 
811786Ssam MINT *x, *b, *one, *c64, *t45, *z, *q, *r, *two, *t15;
911786Ssam MINT *a[42];
1011786Ssam setup(s) char *s;
1111786Ssam {	int pf[2];
1211786Ssam 	strncpy(buf, s, 10);
1311786Ssam 	while(*s) *s++ = 0;
1411786Ssam 	pipe(pf);
1511786Ssam 	if(fork()==0)
1611786Ssam 	{
1711786Ssam 		close(0);
1811786Ssam 		close(1);
1911786Ssam 		dup(pf[0]);
2011786Ssam 		dup(pf[1]);
21*37916Sbostic 		execl(_PATH_MAKEKEY, "-", 0);
2211786Ssam 		exit(1);
2311786Ssam 	}
2411786Ssam 	write(pf[1], buf, 10);
2511786Ssam 	wait((int *)NULL);
2611786Ssam 	if(read(pf[0], buf, 13) != 13)
2711786Ssam 	{	fprintf(stderr, "enroll: cannot generate key\n");
2811786Ssam 		exit(1);
2911786Ssam 	}
3011786Ssam }
3111786Ssam mkx()
3211786Ssam {	int i, j;
3311786Ssam 	for(i=0; i<4; i++)
3411786Ssam 	{	mult(x, t15, x);
3511786Ssam 		*z->val = (short)(rand() ^ buf[i]);
3611786Ssam 		madd(x, z, x);
3711786Ssam 	}
3811786Ssam 	mdiv(x, b, q, x);
3911786Ssam 	for(;;)
4011786Ssam 	{	gcd(b, x, q);
4111786Ssam 		if(q->len == 1 && q->val[0] == 1)
4211786Ssam 			return;
4311786Ssam 		madd(x, one, x);
4411786Ssam 	}
4511786Ssam }
4611786Ssam mka()
4711786Ssam {	int i, j;
4811786Ssam 	for(i=0; i<42; i++)
4911786Ssam 		a[i] = itom(1);
5011786Ssam 	for(i=j=0; i<42; i++, j++)
5111786Ssam 	{	*z->val = (short)rand()&00;
5211786Ssam 		mult(a[i], z, a[i]);
5311786Ssam 		mult(a[i], t45, a[i]);
5411786Ssam 		rpow(two, j, q);
5511786Ssam 		if( i%14 == 6) j++;
5611786Ssam 		madd(a[i], q, a[i]);
5711786Ssam 		mult(a[i], t15, a[i]);
5811786Ssam 		*z->val = (short)rand()&0777;
5911786Ssam 		madd(a[i], z, a[i]);
6011786Ssam 		mdiv(a[i], b, q, a[i]);
6111786Ssam 	}
6211786Ssam 	for(i=0; i<42; i++)
6311786Ssam 		mult(a[i], x, a[i]);
6411786Ssam 	for(i=0; i<42; i++)
6511786Ssam 	{	mdiv(a[i], b, q, a[i]);
6611786Ssam 	}
6711786Ssam }
6811786Ssam mkb()
6911786Ssam {	int i, c;
7011786Ssam 	unsigned seed;
7111786Ssam 	seed = 123;
7211786Ssam 	for(i=0; i<13; i++)
7311786Ssam 		seed = seed*buf[i] + i;
7411786Ssam 	srand(seed);
7511786Ssam 	*b->val = 04 + (rand()&03);
7611786Ssam 	for(i=0; i<11; i++)
7711786Ssam 	{	*z->val = (buf[i+2] + rand()) & 077;
7811786Ssam 		mult(b, c64, b);
7911786Ssam 		madd(b, z, b);
8011786Ssam 	}
8111786Ssam }
8211786Ssam comminit()
8311786Ssam {	int i;
8411786Ssam 	x = itom(0);
8511786Ssam 	b = itom(1);
8611786Ssam 	one = itom(1);
8711786Ssam 	two = itom(2);
8811786Ssam 	c64 = itom(64);
8911786Ssam 	t45 = itom(1);
9011786Ssam 	t15 = itom(1);
9111786Ssam 	rpow(two, 45, t45);
9211786Ssam 	rpow(two, 15, t15);
9311786Ssam 	z = itom(1);
9411786Ssam 	q = itom(1);
9511786Ssam }
9611786Ssam #ifndef debug
9711786Ssam nout(a, fd) MINT *a; FILE *fd;
9811786Ssam {
9911786Ssam 	fwrite(&a->len, sizeof(int), 1, fd);
10011786Ssam 	fwrite(a->val, sizeof(short), a->len, fd);
10111786Ssam }
10211786Ssam nin(a, fd) MINT *a; FILE *fd;
10311786Ssam {
10411786Ssam 	xfree(a);
10511786Ssam 	fread(&a->len, sizeof(int), 1, fd);
10611786Ssam 	a->val = xalloc(a->len, "nin");
10711786Ssam 	fread(a->val, sizeof(short), a->len, fd);
10811786Ssam }
10911786Ssam #endif
11011786Ssam xfatal(s) char *s;
11111786Ssam {
11211786Ssam 	fprintf(stderr, "%s\n", s);
11311786Ssam 	exit(1);
11411786Ssam }
115