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/cdefs.h>
14 __FBSDID("$FreeBSD$");
16 #include <sys/types.h>
23 #define MINUSKVAR "CrYpTkEy"
27 static char t1[ROTORSZ];
28 static char t2[ROTORSZ];
29 static char t3[ROTORSZ];
30 static char deck[ROTORSZ];
33 static void shuffle(char *);
34 static void setup(char *);
45 if (crypt_set_format("des") == 0) {
46 fprintf(stderr, "crypt_set_format(\"des\") failed.\n");
50 strlcpy(salt, pw, sizeof(salt));
51 cryptpw = crypt(pw, salt);
52 if (cryptpw == NULL) {
53 fprintf(stderr, "crypt(3) failure\n");
56 memcpy(buf, cryptpw, sizeof(buf));
59 seed = seed*buf[i] + i;
60 for(i=0;i<ROTORSZ;i++) {
64 for(i=0;i<ROTORSZ;i++) {
65 seed = 5*seed + buf[i%13];
68 ic = (rnd&MASK)%(k+1);
73 if(t3[k]!=0) continue;
75 while(t3[ic]!=0) ic = (ic+1) % k;
79 for(i=0;i<ROTORSZ;i++)
84 main(int argc, char *argv[])
86 int i, n1, n2, nr1, nr2;
87 int secureflg = 0, kflag = 0;
90 if (argc > 1 && argv[1][0] == '-') {
91 if (argv[1][1] == 's') {
95 } else if (argv[1][1] == 'k') {
102 if ((cp = getenv(MINUSKVAR)) == NULL) {
103 fprintf(stderr, "%s not set\n", MINUSKVAR);
107 } else if (argc != 2) {
108 setup(getpass("Enter key:"));
116 while((i=getchar()) != -1) {
119 nr2 = deck[nr1]&MASK;
123 i = t2[(t3[(t1[(i+nr1)&MASK]+nr2)&MASK]-nr2)&MASK]-nr1;
129 if(n2==ROTORSZ) n2 = 0;
142 shuffle(char deckary[])
146 static int32_t seed = 123;
148 for(i=0;i<ROTORSZ;i++) {
149 seed = 5*seed + buf[i%13];
152 ic = (rnd&MASK)%(k+1);
154 deckary[k] = deckary[ic];