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.4 2003/11/04 16:36:35 drhodus Exp $
16 #include <sys/types.h>
24 #define MINUSKVAR "CrYpTkEy"
35 int main(int, char *[]);
47 strncpy(salt, pw, sizeof(salt));
48 memcpy(buf, crypt(pw, salt), sizeof(buf));
51 seed = seed*buf[i] + i;
52 for(i=0;i<ROTORSZ;i++) {
56 for(i=0;i<ROTORSZ;i++) {
57 seed = 5*seed + buf[i%13];
58 if( sizeof(long) > 4 ) {
59 /* Force seed to stay in 32-bit signed math */
60 if( seed & 0x80000000 )
61 seed = seed | (-1L & ~0xFFFFFFFFL);
67 ic = (rnd&MASK)%(k+1);
72 if(t3[k]!=0) continue;
74 while(t3[ic]!=0) ic = (ic+1) % k;
78 for(i=0;i<ROTORSZ;i++)
83 main(int argc, char **argv)
85 register int i, n1, n2, nr1, nr2;
86 int secureflg = 0, kflag = 0;
89 if (argc > 1 && argv[1][0] == '-') {
90 if (argv[1][1] == 's') {
94 } else if (argv[1][1] == 'k') {
101 if ((cp = getenv(MINUSKVAR)) == NULL) {
102 fprintf(stderr, "%s not set\n", MINUSKVAR);
106 } else if (argc != 2) {
107 setup(getpass("Enter key:"));
115 while((i=getchar()) != -1) {
118 nr2 = deck[nr1]&MASK;
122 i = t2[(t3[(t1[(i+nr1)&MASK]+nr2)&MASK]-nr2)&MASK]-nr1;
128 if(n2==ROTORSZ) n2 = 0;
141 shuffle(char *deckary)
145 static long seed = 123;
147 for(i=0;i<ROTORSZ;i++) {
148 seed = 5*seed + buf[i%13];
151 ic = (rnd&MASK)%(k+1);
153 deckary[k] = deckary[ic];