2 * "enigma.c" is in file cbw.tar from
3 * anonymous FTP host watmsg.waterloo.edu: pub/crypt/cbw.tar.Z
5 * A one-rotor machine designed along the lines of Enigma
6 * but considerably trivialized.
8 * A public-domain replacement for the UNIX "crypt" command.
10 * Upgraded to function properly on 64-bit machines.
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 $";
18 #include <sys/types.h>
26 #define MINUSKVAR "CrYpTkEy"
37 int main(int, char *[]);
45 int ic, i, k, temp, pf[2], pid;
55 if ((pid=fork())==0) {
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);
66 fprintf(stderr, "enigma: cannot execute 'makekey', aborting\n");
69 write(pf[1], buf, 10);
72 if (i<0) perror("enigma: wait");
74 fprintf(stderr, "enigma: expected pid %d, got pid %d\n", pid, i);
77 if ((i=read(pf[0], buf, 13)) != 13) {
78 fprintf(stderr, "enigma: cannot generate key, read %d\n",i);
83 seed = seed*buf[i] + i;
84 for(i=0;i<ROTORSZ;i++) {
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);
99 ic = (rnd&MASK)%(k+1);
104 if(t3[k]!=0) continue;
106 while(t3[ic]!=0) ic = (ic+1) % k;
110 for(i=0;i<ROTORSZ;i++)
119 register int i, n1, n2, nr1, nr2;
120 int secureflg = 0, kflag = 0;
123 if (argc > 1 && argv[1][0] == '-') {
124 if (argv[1][1] == 's') {
128 } else if (argv[1][1] == 'k') {
135 if ((cp = getenv(MINUSKVAR)) == NULL) {
136 fprintf(stderr, "%s not set\n", MINUSKVAR);
140 } else if (argc != 2) {
141 setup(getpass("Enter key:"));
149 while((i=getchar()) != -1) {
152 nr2 = deck[nr1]&MASK;
156 i = t2[(t3[(t1[(i+nr1)&MASK]+nr2)&MASK]-nr2)&MASK]-nr1;
162 if(n2==ROTORSZ) n2 = 0;
180 static long seed = 123;
182 for(i=0;i<ROTORSZ;i++) {
183 seed = 5*seed + buf[i%13];
186 ic = (rnd&MASK)%(k+1);
188 deckary[k] = deckary[ic];