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/types.h> 14 #include <sys/wait.h> 15 16 #include <stdio.h> 17 #include <stdlib.h> 18 #include <string.h> 19 #include <unistd.h> 20 21 #define MINUSKVAR "CrYpTkEy" 22 23 #define ECHO 010 24 #define ROTORSZ 256 25 #define MASK 0377 26 char t1[ROTORSZ]; 27 char t2[ROTORSZ]; 28 char t3[ROTORSZ]; 29 char deck[ROTORSZ]; 30 char buf[13]; 31 32 void shuffle(char *); 33 34 void 35 setup(pw) 36 char *pw; 37 { 38 int ic, i, k, temp, pf[2], pid; 39 unsigned random; 40 long seed; 41 42 strncpy(buf, pw, 8); 43 while (*pw) 44 *pw++ = '\0'; 45 buf[8] = buf[0]; 46 buf[9] = buf[1]; 47 pipe(pf); 48 if ((pid=fork())==0) { 49 close(0); 50 close(1); 51 dup(pf[0]); 52 dup(pf[1]); 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); 58 perror("makekey"); 59 fprintf(stderr, "enigma: cannot execute 'makekey', aborting\n"); 60 exit(1); 61 } 62 write(pf[1], buf, 10); 63 close(pf[1]); 64 i=wait((int *)NULL); 65 if (i<0) perror("enigma: wait"); 66 if (i!=pid) { 67 fprintf(stderr, "enigma: expected pid %d, got pid %d\n", pid, i); 68 exit(1); 69 } 70 if ((i=read(pf[0], buf, 13)) != 13) { 71 fprintf(stderr, "enigma: cannot generate key, read %d\n",i); 72 exit(1); 73 } 74 seed = 123; 75 for (i=0; i<13; i++) 76 seed = seed*buf[i] + i; 77 for(i=0;i<ROTORSZ;i++) { 78 t1[i] = i; 79 deck[i] = i; 80 } 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); 87 else 88 seed &= 0x7FFFFFFF; 89 } 90 random = seed % 65521; 91 k = ROTORSZ-1 - i; 92 ic = (random&MASK)%(k+1); 93 random >>= 8; 94 temp = t1[k]; 95 t1[k] = t1[ic]; 96 t1[ic] = temp; 97 if(t3[k]!=0) continue; 98 ic = (random&MASK) % k; 99 while(t3[ic]!=0) ic = (ic+1) % k; 100 t3[k] = ic; 101 t3[ic] = k; 102 } 103 for(i=0;i<ROTORSZ;i++) 104 t2[t1[i]&MASK] = i; 105 } 106 107 int 108 main(argc, argv) 109 char *argv[]; 110 { 111 register int i, n1, n2, nr1, nr2; 112 int secureflg = 0, kflag = 0; 113 char *cp; 114 115 if (argc > 1 && argv[1][0] == '-') { 116 if (argv[1][1] == 's') { 117 argc--; 118 argv++; 119 secureflg = 1; 120 } else if (argv[1][1] == 'k') { 121 argc--; 122 argv++; 123 kflag = 1; 124 } 125 } 126 if (kflag) { 127 if ((cp = getenv(MINUSKVAR)) == NULL) { 128 fprintf(stderr, "%s not set\n", MINUSKVAR); 129 exit(1); 130 } 131 setup(cp); 132 } else if (argc != 2) { 133 setup(getpass("Enter key:")); 134 } 135 else 136 setup(argv[1]); 137 n1 = 0; 138 n2 = 0; 139 nr2 = 0; 140 141 while((i=getchar()) != -1) { 142 if (secureflg) { 143 nr1 = deck[n1]&MASK; 144 nr2 = deck[nr1]&MASK; 145 } else { 146 nr1 = n1; 147 } 148 i = t2[(t3[(t1[(i+nr1)&MASK]+nr2)&MASK]-nr2)&MASK]-nr1; 149 putchar(i); 150 n1++; 151 if(n1==ROTORSZ) { 152 n1 = 0; 153 n2++; 154 if(n2==ROTORSZ) n2 = 0; 155 if (secureflg) { 156 shuffle(deck); 157 } else { 158 nr2 = n2; 159 } 160 } 161 } 162 163 return 0; 164 } 165 166 void 167 shuffle(deck) 168 char deck[]; 169 { 170 int i, ic, k, temp; 171 unsigned random; 172 static long seed = 123; 173 174 for(i=0;i<ROTORSZ;i++) { 175 seed = 5*seed + buf[i%13]; 176 random = seed % 65521; 177 k = ROTORSZ-1 - i; 178 ic = (random&MASK)%(k+1); 179 temp = deck[k]; 180 deck[k] = deck[ic]; 181 deck[ic] = temp; 182 } 183 } 184