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.
13 #include <sys/types.h>
21 #define MINUSKVAR "CrYpTkEy"
38 int ic, i, k, temp, pf[2], pid;
48 if ((pid=fork())==0) {
53 execlp("makekey", "-", 0);
54 execl("/usr/libexec/makekey", "-", 0); /* BSDI */
55 execl("/usr/lib/makekey", "-", 0);
56 execl("/usr/bin/makekey", "-", 0); /* IBM */
57 execl("/lib/makekey", "-", 0);
59 fprintf(stderr, "enigma: cannot execute 'makekey', aborting\n");
62 write(pf[1], buf, 10);
65 if (i<0) perror("enigma: wait");
67 fprintf(stderr, "enigma: expected pid %d, got pid %d\n", pid, i);
70 if ((i=read(pf[0], buf, 13)) != 13) {
71 fprintf(stderr, "enigma: cannot generate key, read %d\n",i);
76 seed = seed*buf[i] + i;
77 for(i=0;i<ROTORSZ;i++) {
81 for(i=0;i<ROTORSZ;i++) {
82 seed = 5*seed + buf[i%13];
83 if( sizeof(long) > 4 ) {
84 /* Force seed to stay in 32-bit signed math */
85 if( seed & 0x80000000 )
86 seed = seed | (-1L & ~0xFFFFFFFFL);
90 random = seed % 65521;
92 ic = (random&MASK)%(k+1);
97 if(t3[k]!=0) continue;
98 ic = (random&MASK) % k;
99 while(t3[ic]!=0) ic = (ic+1) % k;
103 for(i=0;i<ROTORSZ;i++)
111 register int i, n1, n2, nr1, nr2;
112 int secureflg = 0, kflag = 0;
115 if (argc > 1 && argv[1][0] == '-') {
116 if (argv[1][1] == 's') {
120 } else if (argv[1][1] == 'k') {
127 if ((cp = getenv(MINUSKVAR)) == NULL) {
128 fprintf(stderr, "%s not set\n", MINUSKVAR);
132 } else if (argc != 2) {
133 setup(getpass("Enter key:"));
141 while((i=getchar()) != -1) {
144 nr2 = deck[nr1]&MASK;
148 i = t2[(t3[(t1[(i+nr1)&MASK]+nr2)&MASK]-nr2)&MASK]-nr1;
154 if(n2==ROTORSZ) n2 = 0;
172 static long seed = 123;
174 for(i=0;i<ROTORSZ;i++) {
175 seed = 5*seed + buf[i%13];
176 random = seed % 65521;
178 ic = (random&MASK)%(k+1);