xref: /freebsd/usr.bin/enigma/enigma.c (revision 86c9d9918f1db7cdd968b60f8902466887bcd9e9)
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 #include <sys/cdefs.h>
14 __FBSDID("$FreeBSD$");
15 
16 #include <sys/types.h>
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 
23 #define MINUSKVAR "CrYpTkEy"
24 
25 #define ECHO 010
26 #define ROTORSZ 256
27 #define MASK 0377
28 static char	t1[ROTORSZ];
29 static char	t2[ROTORSZ];
30 static char	t3[ROTORSZ];
31 static char	deck[ROTORSZ];
32 static char	buf[13];
33 
34 static void	shuffle(char *);
35 static void	setup(char *);
36 
37 static void
38 setup(char *pw)
39 {
40 	int ic, i, k, temp;
41 	char salt[3];
42 	unsigned rnd;
43 	int32_t seed;
44 	char *cryptpw;
45 
46 	if (crypt_set_format("des") == 0) {
47 		fprintf(stderr, "crypt_set_format(\"des\") failed.\n");
48 		exit(1);
49 	}
50 
51 	strlcpy(salt, pw, sizeof(salt));
52 	cryptpw = crypt(pw, salt);
53 	if (cryptpw == NULL) {
54 		fprintf(stderr, "crypt(3) failure\n");
55 		exit(1);
56 	}
57 	memcpy(buf, cryptpw, sizeof(buf));
58 	seed = 123;
59 	for (i=0; i<13; i++)
60 		seed = seed*buf[i] + i;
61 	for(i=0;i<ROTORSZ;i++) {
62 		t1[i] = i;
63 		deck[i] = i;
64 	}
65 	for(i=0;i<ROTORSZ;i++) {
66 		seed = 5*seed + buf[i%13];
67 		rnd = seed % 65521;
68 		k = ROTORSZ-1 - i;
69 		ic = (rnd&MASK)%(k+1);
70 		rnd >>= 8;
71 		temp = t1[k];
72 		t1[k] = t1[ic];
73 		t1[ic] = temp;
74 		if(t3[k]!=0) continue;
75 		ic = (rnd&MASK) % k;
76 		while(t3[ic]!=0) ic = (ic+1) % k;
77 		t3[k] = ic;
78 		t3[ic] = k;
79 	}
80 	for(i=0;i<ROTORSZ;i++)
81 		t2[t1[i]&MASK] = i;
82 }
83 
84 int
85 main(int argc, char *argv[])
86 {
87 	int i, n1, n2, nr1, nr2;
88 	int secureflg = 0, kflag = 0;
89 	char *cp;
90 
91 	if (argc > 1 && argv[1][0] == '-') {
92 		if (argv[1][1] == 's') {
93 			argc--;
94 			argv++;
95 			secureflg = 1;
96 		} else if (argv[1][1] == 'k') {
97 			argc--;
98 			argv++;
99 			kflag = 1;
100 		}
101 	}
102 	if (kflag) {
103 		if ((cp = getenv(MINUSKVAR)) == NULL) {
104 			fprintf(stderr, "%s not set\n", MINUSKVAR);
105 			exit(1);
106 		}
107 		setup(cp);
108 	} else if (argc != 2) {
109 		setup(getpass("Enter key:"));
110 	}
111 	else
112 		setup(argv[1]);
113 	n1 = 0;
114 	n2 = 0;
115 	nr2 = 0;
116 
117 	while((i=getchar()) != -1) {
118 		if (secureflg) {
119 			nr1 = deck[n1]&MASK;
120 			nr2 = deck[nr1]&MASK;
121 		} else {
122 			nr1 = n1;
123 		}
124 		i = t2[(t3[(t1[(i+nr1)&MASK]+nr2)&MASK]-nr2)&MASK]-nr1;
125 		putchar(i);
126 		n1++;
127 		if(n1==ROTORSZ) {
128 			n1 = 0;
129 			n2++;
130 			if(n2==ROTORSZ) n2 = 0;
131 			if (secureflg) {
132 				shuffle(deck);
133 			} else {
134 				nr2 = n2;
135 			}
136 		}
137 	}
138 
139 	return 0;
140 }
141 
142 static void
143 shuffle(char deckary[])
144 {
145 	int i, ic, k, temp;
146 	unsigned rnd;
147 	static int32_t seed = 123;
148 
149 	for(i=0;i<ROTORSZ;i++) {
150 		seed = 5*seed + buf[i%13];
151 		rnd = seed % 65521;
152 		k = ROTORSZ-1 - i;
153 		ic = (rnd&MASK)%(k+1);
154 		temp = deckary[k];
155 		deckary[k] = deckary[ic];
156 		deckary[ic] = temp;
157 	}
158 }
159