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