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