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.
12 * $FreeBSD: src/usr.bin/enigma/enigma.c,v 1.2.6.3 2001/08/01 23:51:34 obrien Exp $
13 * $DragonFly: src/usr.bin/enigma/enigma.c,v 1.6 2005/01/05 18:42:33 liamfoy Exp $
21 #define MINUSKVAR "CrYpTkEy"
32 static void shuffle(char *);
33 static void setup(const char *);
44 strncpy(salt, pw, sizeof(salt));
45 cryptpw = crypt(pw, salt);
46 if (cryptpw == NULL) {
47 fprintf(stderr, "crypt(3) failure\n");
50 memcpy(buf, cryptpw, sizeof(buf));
53 seed = seed*buf[i] + i;
54 for(i=0;i<ROTORSZ;i++) {
58 for(i=0;i<ROTORSZ;i++) {
59 seed = 5*seed + buf[i%13];
60 if( sizeof(long) > 4 ) {
61 /* Force seed to stay in 32-bit signed math */
62 if( seed & 0x80000000 )
63 seed = seed | (-1L & ~0xFFFFFFFFL);
69 ic = (rnd&MASK)%(k+1);
74 if(t3[k]!=0) continue;
76 while(t3[ic]!=0) ic = (ic+1) % k;
80 for(i=0;i<ROTORSZ;i++)
85 main(int argc, char **argv)
87 int i, n1, n2, nr1, nr2;
88 int secureflg = 0, kflag = 0;
91 if (argc > 1 && argv[1][0] == '-') {
92 if (argv[1][1] == 's') {
96 } else if (argv[1][1] == 'k') {
103 if ((cp = getenv(MINUSKVAR)) == NULL) {
104 fprintf(stderr, "%s not set\n", MINUSKVAR);
108 } else if (argc != 2) {
109 setup(getpass("Enter key:"));
117 while((i=getchar()) != -1) {
120 nr2 = deck[nr1]&MASK;
124 i = t2[(t3[(t1[(i+nr1)&MASK]+nr2)&MASK]-nr2)&MASK]-nr1;
130 if(n2==ROTORSZ) n2 = 0;
143 shuffle(char *deckary)
147 static long seed = 123;
149 for(i=0;i<ROTORSZ;i++) {
150 seed = 5*seed + buf[i%13];
153 ic = (rnd&MASK)%(k+1);
155 deckary[k] = deckary[ic];