1*11786Ssam #ifndef lint 2*11786Ssam static char sccsid[] = "@(#)lib.c 4.1 03/30/83"; 3*11786Ssam #endif 4*11786Ssam 5*11786Ssam #include "xmail.h" 6*11786Ssam MINT *x, *b, *one, *c64, *t45, *z, *q, *r, *two, *t15; 7*11786Ssam MINT *a[42]; 8*11786Ssam setup(s) char *s; 9*11786Ssam { int pf[2]; 10*11786Ssam strncpy(buf, s, 10); 11*11786Ssam while(*s) *s++ = 0; 12*11786Ssam pipe(pf); 13*11786Ssam if(fork()==0) 14*11786Ssam { 15*11786Ssam close(0); 16*11786Ssam close(1); 17*11786Ssam dup(pf[0]); 18*11786Ssam dup(pf[1]); 19*11786Ssam execl("/usr/lib/makekey", "-", 0); 20*11786Ssam execl("/lib/makekey", "-", 0); 21*11786Ssam exit(1); 22*11786Ssam } 23*11786Ssam write(pf[1], buf, 10); 24*11786Ssam wait((int *)NULL); 25*11786Ssam if(read(pf[0], buf, 13) != 13) 26*11786Ssam { fprintf(stderr, "enroll: cannot generate key\n"); 27*11786Ssam exit(1); 28*11786Ssam } 29*11786Ssam } 30*11786Ssam mkx() 31*11786Ssam { int i, j; 32*11786Ssam for(i=0; i<4; i++) 33*11786Ssam { mult(x, t15, x); 34*11786Ssam *z->val = (short)(rand() ^ buf[i]); 35*11786Ssam madd(x, z, x); 36*11786Ssam } 37*11786Ssam mdiv(x, b, q, x); 38*11786Ssam for(;;) 39*11786Ssam { gcd(b, x, q); 40*11786Ssam if(q->len == 1 && q->val[0] == 1) 41*11786Ssam return; 42*11786Ssam madd(x, one, x); 43*11786Ssam } 44*11786Ssam } 45*11786Ssam mka() 46*11786Ssam { int i, j; 47*11786Ssam for(i=0; i<42; i++) 48*11786Ssam a[i] = itom(1); 49*11786Ssam for(i=j=0; i<42; i++, j++) 50*11786Ssam { *z->val = (short)rand()&00; 51*11786Ssam mult(a[i], z, a[i]); 52*11786Ssam mult(a[i], t45, a[i]); 53*11786Ssam rpow(two, j, q); 54*11786Ssam if( i%14 == 6) j++; 55*11786Ssam madd(a[i], q, a[i]); 56*11786Ssam mult(a[i], t15, a[i]); 57*11786Ssam *z->val = (short)rand()&0777; 58*11786Ssam madd(a[i], z, a[i]); 59*11786Ssam mdiv(a[i], b, q, a[i]); 60*11786Ssam } 61*11786Ssam for(i=0; i<42; i++) 62*11786Ssam mult(a[i], x, a[i]); 63*11786Ssam for(i=0; i<42; i++) 64*11786Ssam { mdiv(a[i], b, q, a[i]); 65*11786Ssam } 66*11786Ssam } 67*11786Ssam mkb() 68*11786Ssam { int i, c; 69*11786Ssam unsigned seed; 70*11786Ssam seed = 123; 71*11786Ssam for(i=0; i<13; i++) 72*11786Ssam seed = seed*buf[i] + i; 73*11786Ssam srand(seed); 74*11786Ssam *b->val = 04 + (rand()&03); 75*11786Ssam for(i=0; i<11; i++) 76*11786Ssam { *z->val = (buf[i+2] + rand()) & 077; 77*11786Ssam mult(b, c64, b); 78*11786Ssam madd(b, z, b); 79*11786Ssam } 80*11786Ssam } 81*11786Ssam comminit() 82*11786Ssam { int i; 83*11786Ssam x = itom(0); 84*11786Ssam b = itom(1); 85*11786Ssam one = itom(1); 86*11786Ssam two = itom(2); 87*11786Ssam c64 = itom(64); 88*11786Ssam t45 = itom(1); 89*11786Ssam t15 = itom(1); 90*11786Ssam rpow(two, 45, t45); 91*11786Ssam rpow(two, 15, t15); 92*11786Ssam z = itom(1); 93*11786Ssam q = itom(1); 94*11786Ssam } 95*11786Ssam #ifndef debug 96*11786Ssam nout(a, fd) MINT *a; FILE *fd; 97*11786Ssam { 98*11786Ssam fwrite(&a->len, sizeof(int), 1, fd); 99*11786Ssam fwrite(a->val, sizeof(short), a->len, fd); 100*11786Ssam } 101*11786Ssam nin(a, fd) MINT *a; FILE *fd; 102*11786Ssam { 103*11786Ssam xfree(a); 104*11786Ssam fread(&a->len, sizeof(int), 1, fd); 105*11786Ssam a->val = xalloc(a->len, "nin"); 106*11786Ssam fread(a->val, sizeof(short), a->len, fd); 107*11786Ssam } 108*11786Ssam #endif 109*11786Ssam xfatal(s) char *s; 110*11786Ssam { 111*11786Ssam fprintf(stderr, "%s\n", s); 112*11786Ssam exit(1); 113*11786Ssam } 114