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$"; 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 void shuffle(char *); 38 void setup(char *); 39 40 void 41 setup(pw) 42 char *pw; 43 { 44 int ic, i, k, temp, pf[2], pid; 45 unsigned rnd; 46 long seed; 47 48 strncpy(buf, pw, 8); 49 while (*pw) 50 *pw++ = '\0'; 51 buf[8] = buf[0]; 52 buf[9] = buf[1]; 53 pipe(pf); 54 if ((pid=fork())==0) { 55 close(0); 56 close(1); 57 dup(pf[0]); 58 dup(pf[1]); 59 execlp("makekey", "-", (char *)0); 60 execl("/usr/libexec/makekey", "-", (char *)0); /* BSDI */ 61 execl("/usr/lib/makekey", "-", (char *)0); 62 execl("/usr/bin/makekey", "-", (char *)0); /* IBM */ 63 execl("/lib/makekey", "-", (char *)0); 64 perror("makekey"); 65 fprintf(stderr, "enigma: cannot execute 'makekey', aborting\n"); 66 exit(1); 67 } 68 write(pf[1], buf, 10); 69 close(pf[1]); 70 i=wait((int *)NULL); 71 if (i<0) perror("enigma: wait"); 72 if (i!=pid) { 73 fprintf(stderr, "enigma: expected pid %d, got pid %d\n", pid, i); 74 exit(1); 75 } 76 if ((i=read(pf[0], buf, 13)) != 13) { 77 fprintf(stderr, "enigma: cannot generate key, read %d\n",i); 78 exit(1); 79 } 80 seed = 123; 81 for (i=0; i<13; i++) 82 seed = seed*buf[i] + i; 83 for(i=0;i<ROTORSZ;i++) { 84 t1[i] = i; 85 deck[i] = i; 86 } 87 for(i=0;i<ROTORSZ;i++) { 88 seed = 5*seed + buf[i%13]; 89 if( sizeof(long) > 4 ) { 90 /* Force seed to stay in 32-bit signed math */ 91 if( seed & 0x80000000 ) 92 seed = seed | (-1L & ~0xFFFFFFFFL); 93 else 94 seed &= 0x7FFFFFFF; 95 } 96 rnd = seed % 65521; 97 k = ROTORSZ-1 - i; 98 ic = (rnd&MASK)%(k+1); 99 rnd >>= 8; 100 temp = t1[k]; 101 t1[k] = t1[ic]; 102 t1[ic] = temp; 103 if(t3[k]!=0) continue; 104 ic = (rnd&MASK) % k; 105 while(t3[ic]!=0) ic = (ic+1) % k; 106 t3[k] = ic; 107 t3[ic] = k; 108 } 109 for(i=0;i<ROTORSZ;i++) 110 t2[t1[i]&MASK] = i; 111 } 112 113 int 114 main(argc, argv) 115 int argc; 116 char *argv[]; 117 { 118 register int i, n1, n2, nr1, nr2; 119 int secureflg = 0, kflag = 0; 120 char *cp; 121 122 if (argc > 1 && argv[1][0] == '-') { 123 if (argv[1][1] == 's') { 124 argc--; 125 argv++; 126 secureflg = 1; 127 } else if (argv[1][1] == 'k') { 128 argc--; 129 argv++; 130 kflag = 1; 131 } 132 } 133 if (kflag) { 134 if ((cp = getenv(MINUSKVAR)) == NULL) { 135 fprintf(stderr, "%s not set\n", MINUSKVAR); 136 exit(1); 137 } 138 setup(cp); 139 } else if (argc != 2) { 140 setup(getpass("Enter key:")); 141 } 142 else 143 setup(argv[1]); 144 n1 = 0; 145 n2 = 0; 146 nr2 = 0; 147 148 while((i=getchar()) != -1) { 149 if (secureflg) { 150 nr1 = deck[n1]&MASK; 151 nr2 = deck[nr1]&MASK; 152 } else { 153 nr1 = n1; 154 } 155 i = t2[(t3[(t1[(i+nr1)&MASK]+nr2)&MASK]-nr2)&MASK]-nr1; 156 putchar(i); 157 n1++; 158 if(n1==ROTORSZ) { 159 n1 = 0; 160 n2++; 161 if(n2==ROTORSZ) n2 = 0; 162 if (secureflg) { 163 shuffle(deck); 164 } else { 165 nr2 = n2; 166 } 167 } 168 } 169 170 return 0; 171 } 172 173 void 174 shuffle(deckary) 175 char deckary[]; 176 { 177 int i, ic, k, temp; 178 unsigned rnd; 179 static long seed = 123; 180 181 for(i=0;i<ROTORSZ;i++) { 182 seed = 5*seed + buf[i%13]; 183 rnd = seed % 65521; 184 k = ROTORSZ-1 - i; 185 ic = (rnd&MASK)%(k+1); 186 temp = deckary[k]; 187 deckary[k] = deckary[ic]; 188 deckary[ic] = temp; 189 } 190 } 191