xref: /dragonfly/usr.bin/enigma/enigma.c (revision 984263bc)
1 /*
2  *	"enigma.c" is in file cbw.tar from
3  *	anonymous FTP host watmsg.waterloo.edu: pub/crypt/cbw.tar.Z
4  *
5  *	A one-rotor machine designed along the lines of Enigma
6  *	but considerably trivialized.
7  *
8  *	A public-domain replacement for the UNIX "crypt" command.
9  *
10  *	Upgraded to function properly on 64-bit machines.
11  */
12 
13 #ifndef lint
14 static const char rcsid[] =
15   "$FreeBSD: src/usr.bin/enigma/enigma.c,v 1.2.6.3 2001/08/01 23:51:34 obrien Exp $";
16 #endif /* not lint */
17 
18 #include <sys/types.h>
19 #include <sys/wait.h>
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 
26 #define MINUSKVAR "CrYpTkEy"
27 
28 #define ECHO 010
29 #define ROTORSZ 256
30 #define MASK 0377
31 char	t1[ROTORSZ];
32 char	t2[ROTORSZ];
33 char	t3[ROTORSZ];
34 char	deck[ROTORSZ];
35 char	buf[13];
36 
37 int	main(int, char *[]);
38 void	shuffle(char *);
39 void	setup(char *);
40 
41 void
42 setup(pw)
43 	char *pw;
44 {
45 	int ic, i, k, temp, pf[2], pid;
46 	unsigned rnd;
47 	long seed;
48 
49 	strncpy(buf, pw, 8);
50 	while (*pw)
51 		*pw++ = '\0';
52 	buf[8] = buf[0];
53 	buf[9] = buf[1];
54 	pipe(pf);
55 	if ((pid=fork())==0) {
56 		close(0);
57 		close(1);
58 		dup(pf[0]);
59 		dup(pf[1]);
60 		execlp("makekey", "-", (char *)0);
61 		execl("/usr/libexec/makekey", "-", (char *)0);	/* BSDI */
62 		execl("/usr/lib/makekey", "-", (char *)0);
63 		execl("/usr/bin/makekey", "-", (char *)0);	/* IBM */
64 		execl("/lib/makekey", "-", (char *)0);
65 		perror("makekey");
66 		fprintf(stderr, "enigma: cannot execute 'makekey', aborting\n");
67 		exit(1);
68 	}
69 	write(pf[1], buf, 10);
70 	close(pf[1]);
71 	i=wait((int *)NULL);
72 	if (i<0) perror("enigma: wait");
73 	if (i!=pid) {
74 		fprintf(stderr, "enigma: expected pid %d, got pid %d\n", pid, i);
75 		exit(1);
76 	}
77 	if ((i=read(pf[0], buf, 13)) != 13) {
78 		fprintf(stderr, "enigma: cannot generate key, read %d\n",i);
79 		exit(1);
80 	}
81 	seed = 123;
82 	for (i=0; i<13; i++)
83 		seed = seed*buf[i] + i;
84 	for(i=0;i<ROTORSZ;i++) {
85 		t1[i] = i;
86 		deck[i] = i;
87 	}
88 	for(i=0;i<ROTORSZ;i++) {
89 		seed = 5*seed + buf[i%13];
90 		if( sizeof(long) > 4 )  {
91 			/* Force seed to stay in 32-bit signed math */
92 			if( seed & 0x80000000 )
93 				seed = seed | (-1L & ~0xFFFFFFFFL);
94 			else
95 				seed &= 0x7FFFFFFF;
96 		}
97 		rnd = seed % 65521;
98 		k = ROTORSZ-1 - i;
99 		ic = (rnd&MASK)%(k+1);
100 		rnd >>= 8;
101 		temp = t1[k];
102 		t1[k] = t1[ic];
103 		t1[ic] = temp;
104 		if(t3[k]!=0) continue;
105 		ic = (rnd&MASK) % k;
106 		while(t3[ic]!=0) ic = (ic+1) % k;
107 		t3[k] = ic;
108 		t3[ic] = k;
109 	}
110 	for(i=0;i<ROTORSZ;i++)
111 		t2[t1[i]&MASK] = i;
112 }
113 
114 int
115 main(argc, argv)
116 	int argc;
117 	char *argv[];
118 {
119 	register int i, n1, n2, nr1, nr2;
120 	int secureflg = 0, kflag = 0;
121 	char *cp;
122 
123 	if (argc > 1 && argv[1][0] == '-') {
124 		if (argv[1][1] == 's') {
125 			argc--;
126 			argv++;
127 			secureflg = 1;
128 		} else if (argv[1][1] == 'k') {
129 			argc--;
130 			argv++;
131 			kflag = 1;
132 		}
133 	}
134 	if (kflag) {
135 		if ((cp = getenv(MINUSKVAR)) == NULL) {
136 			fprintf(stderr, "%s not set\n", MINUSKVAR);
137 			exit(1);
138 		}
139 		setup(cp);
140 	} else if (argc != 2) {
141 		setup(getpass("Enter key:"));
142 	}
143 	else
144 		setup(argv[1]);
145 	n1 = 0;
146 	n2 = 0;
147 	nr2 = 0;
148 
149 	while((i=getchar()) != -1) {
150 		if (secureflg) {
151 			nr1 = deck[n1]&MASK;
152 			nr2 = deck[nr1]&MASK;
153 		} else {
154 			nr1 = n1;
155 		}
156 		i = t2[(t3[(t1[(i+nr1)&MASK]+nr2)&MASK]-nr2)&MASK]-nr1;
157 		putchar(i);
158 		n1++;
159 		if(n1==ROTORSZ) {
160 			n1 = 0;
161 			n2++;
162 			if(n2==ROTORSZ) n2 = 0;
163 			if (secureflg) {
164 				shuffle(deck);
165 			} else {
166 				nr2 = n2;
167 			}
168 		}
169 	}
170 
171 	return 0;
172 }
173 
174 void
175 shuffle(deckary)
176 	char deckary[];
177 {
178 	int i, ic, k, temp;
179 	unsigned rnd;
180 	static long seed = 123;
181 
182 	for(i=0;i<ROTORSZ;i++) {
183 		seed = 5*seed + buf[i%13];
184 		rnd = seed % 65521;
185 		k = ROTORSZ-1 - i;
186 		ic = (rnd&MASK)%(k+1);
187 		temp = deckary[k];
188 		deckary[k] = deckary[ic];
189 		deckary[ic] = temp;
190 	}
191 }
192