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"
28 static char t1
[ROTORSZ
];
29 static char t2
[ROTORSZ
];
30 static char t3
[ROTORSZ
];
31 static char deck
[ROTORSZ
];
34 static void shuffle(char *);
35 static void setup(char *);
46 if (crypt_set_format("des") == 0) {
47 fprintf(stderr
, "crypt_set_format(\"des\") failed.\n");
51 strlcpy(salt
, pw
, sizeof(salt
));
52 cryptpw
= crypt(pw
, salt
);
53 if (cryptpw
== NULL
) {
54 fprintf(stderr
, "crypt(3) failure\n");
57 memcpy(buf
, cryptpw
, sizeof(buf
));
60 seed
= seed
*buf
[i
] + i
;
61 for(i
=0;i
<ROTORSZ
;i
++) {
65 for(i
=0;i
<ROTORSZ
;i
++) {
66 seed
= 5*seed
+ buf
[i
%13];
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 int32_t 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
];