1*37860Sbostic static char *sccsid = "@(#)crypt.c 4.4 (Berkeley) 05/11/89";
23981Smckusic
3994Sbill /*
4994Sbill * A one-rotor machine designed along the lines of Enigma
5994Sbill * but considerably trivialized.
6994Sbill */
7994Sbill
8994Sbill #define ECHO 010
9994Sbill #include <stdio.h>
10*37860Sbostic #include "pathnames.h"
11994Sbill #define ROTORSZ 256
12994Sbill #define MASK 0377
13994Sbill char t1[ROTORSZ];
14994Sbill char t2[ROTORSZ];
15994Sbill char t3[ROTORSZ];
163981Smckusic char deck[ROTORSZ];
17994Sbill char *getpass();
183981Smckusic char buf[13];
19994Sbill
setup(pw)20994Sbill setup(pw)
21994Sbill char *pw;
22994Sbill {
23994Sbill int ic, i, k, temp, pf[2];
2417869Sralph int pid, wpid;
25994Sbill unsigned random;
26994Sbill long seed;
27994Sbill
28994Sbill strncpy(buf, pw, 8);
29994Sbill while (*pw)
30994Sbill *pw++ = '\0';
31994Sbill buf[8] = buf[0];
32994Sbill buf[9] = buf[1];
33994Sbill pipe(pf);
3417869Sralph if ((pid=fork())==0) {
35994Sbill close(0);
36994Sbill close(1);
37994Sbill dup(pf[0]);
38994Sbill dup(pf[1]);
39*37860Sbostic execl(_PATH_MAKEKEY, "-", 0);
40994Sbill exit(1);
41994Sbill }
42994Sbill write(pf[1], buf, 10);
4317869Sralph while ((wpid = wait((int *)NULL)) != -1 && wpid != pid)
4417869Sralph ;
45994Sbill if (read(pf[0], buf, 13) != 13) {
46994Sbill fprintf(stderr, "crypt: cannot generate key\n");
47994Sbill exit(1);
48994Sbill }
49994Sbill seed = 123;
50994Sbill for (i=0; i<13; i++)
51994Sbill seed = seed*buf[i] + i;
523981Smckusic for(i=0;i<ROTORSZ;i++) {
53994Sbill t1[i] = i;
543981Smckusic deck[i] = i;
553981Smckusic }
56994Sbill for(i=0;i<ROTORSZ;i++) {
57994Sbill seed = 5*seed + buf[i%13];
58994Sbill random = seed % 65521;
59994Sbill k = ROTORSZ-1 - i;
60994Sbill ic = (random&MASK)%(k+1);
61994Sbill random >>= 8;
62994Sbill temp = t1[k];
63994Sbill t1[k] = t1[ic];
64994Sbill t1[ic] = temp;
65994Sbill if(t3[k]!=0) continue;
66994Sbill ic = (random&MASK) % k;
67994Sbill while(t3[ic]!=0) ic = (ic+1) % k;
68994Sbill t3[k] = ic;
69994Sbill t3[ic] = k;
70994Sbill }
71994Sbill for(i=0;i<ROTORSZ;i++)
72994Sbill t2[t1[i]&MASK] = i;
73994Sbill }
74994Sbill
main(argc,argv)75994Sbill main(argc, argv)
76994Sbill char *argv[];
77994Sbill {
783981Smckusic register i, n1, n2, nr1, nr2;
793981Smckusic int secureflg = 0;
80994Sbill
813981Smckusic if (argc > 1 && argv[1][0] == '-' && argv[1][1] == 's') {
823981Smckusic argc--;
833981Smckusic argv++;
843981Smckusic secureflg = 1;
853981Smckusic }
86994Sbill if (argc != 2){
87994Sbill setup(getpass("Enter key:"));
88994Sbill }
89994Sbill else
90994Sbill setup(argv[1]);
91994Sbill n1 = 0;
92994Sbill n2 = 0;
933981Smckusic nr2 = 0;
94994Sbill
95994Sbill while((i=getchar()) >=0) {
963981Smckusic if (secureflg) {
973981Smckusic nr1 = deck[n1]&MASK;
983981Smckusic nr2 = deck[nr1]&MASK;
993981Smckusic } else {
1003981Smckusic nr1 = n1;
1013981Smckusic }
1023981Smckusic i = t2[(t3[(t1[(i+nr1)&MASK]+nr2)&MASK]-nr2)&MASK]-nr1;
103994Sbill putchar(i);
104994Sbill n1++;
105994Sbill if(n1==ROTORSZ) {
106994Sbill n1 = 0;
107994Sbill n2++;
108994Sbill if(n2==ROTORSZ) n2 = 0;
1093981Smckusic if (secureflg) {
1103981Smckusic shuffle(deck);
1113981Smckusic } else {
1123981Smckusic nr2 = n2;
1133981Smckusic }
114994Sbill }
115994Sbill }
116994Sbill }
1173981Smckusic
shuffle(deck)1183981Smckusic shuffle(deck)
1193981Smckusic char deck[];
1203981Smckusic {
1213981Smckusic int i, ic, k, temp;
1223981Smckusic unsigned random;
1233981Smckusic static long seed = 123;
1243981Smckusic
1253981Smckusic for(i=0;i<ROTORSZ;i++) {
1263981Smckusic seed = 5*seed + buf[i%13];
1273981Smckusic random = seed % 65521;
1283981Smckusic k = ROTORSZ-1 - i;
1293981Smckusic ic = (random&MASK)%(k+1);
1303981Smckusic temp = deck[k];
1313981Smckusic deck[k] = deck[ic];
1323981Smckusic deck[ic] = temp;
1333981Smckusic }
1343981Smckusic }
135