-getkey()
-{
- struct sgttyb b;
- int save;
- int (*sig)();
- register char *p;
- register c;
-
- sig = signal(SIGINT, SIG_IGN);
- if (gtty(0, &b) == -1)
- error("Input not tty");
- save = b.sg_flags;
- b.sg_flags &= ~ECHO;
- stty(0, &b);
- puts("Key:");
- p = key;
- while(((c=getchr()) != EOF) && (c!='\n')) {
- if(p < &key[KSIZE])
- *p++ = c;
- }
- *p = 0;
- b.sg_flags = save;
- stty(0, &b);
- signal(SIGINT, sig);
- return(key[0] != 0);
-}
-
-/*
- * Besides initializing the encryption machine, this routine
- * returns 0 if the key is null, and 1 if it is non-null.
- */
-crinit(keyp, permp)
-char *keyp, *permp;
-{
- register char *t1, *t2, *t3;
- register i;
- int ic, k, temp, pf[2];
- unsigned random;
- char buf[13];
- long seed;
-
- t1 = permp;
- t2 = &permp[256];
- t3 = &permp[512];
- if(*keyp == 0)
- return(0);
- strncpy(buf, keyp, 8);
- while (*keyp)
- *keyp++ = '\0';
- buf[8] = buf[0];
- buf[9] = buf[1];
- if (pipe(pf)<0)
- pf[0] = pf[1] = -1;
- if (fork()==0) {
- close(0);
- close(1);
- dup(pf[0]);
- dup(pf[1]);
- execl("/usr/lib/makekey", "-", 0);
- execl("/lib/makekey", "-", 0);
- exit(1);
- }
- write(pf[1], buf, 10);
- if (wait((int *)NULL)==-1 || read(pf[0], buf, 13)!=13)
- error("crypt: cannot generate key");
- close(pf[0]);
- close(pf[1]);
- seed = 123;
- for (i=0; i<13; i++)
- seed = seed*buf[i] + i;
- for(i=0;i<256;i++){
- t1[i] = i;
- t3[i] = 0;
- }
- for(i=0; i<256; i++) {
- seed = 5*seed + buf[i%13];
- random = seed % 65521;
- k = 256-1 - i;
- ic = (random&0377) % (k+1);
- random >>= 8;
- temp = t1[k];
- t1[k] = t1[ic];
- t1[ic] = temp;
- if(t3[k]!=0) continue;
- ic = (random&0377) % k;
- while(t3[ic]!=0) ic = (ic+1) % k;
- t3[k] = ic;
- t3[ic] = k;
- }
- for(i=0; i<256; i++)
- t2[t1[i]&0377] = i;
- return(1);
-}
-
-makekey(a, b)
-char *a, *b;
-{
- register int i;
- long t;
- char temp[KSIZE + 1];
-
- for(i = 0; i < KSIZE; i++)
- temp[i] = *a++;
- time(&t);
- t += getpid();
- for(i = 0; i < 4; i++)
- temp[i] ^= (t>>(8*i))&0377;
- crinit(temp, b);
-}