Remove the NO_CRYPT build option.
[dragonfly.git] / usr.bin / enigma / enigma.c
blobc3ea26698168127d065bd95e2a565099d5dcbc77
1 /*
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 $
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
21 #define MINUSKVAR "CrYpTkEy"
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];
32 static void shuffle(char *);
33 static void setup(const char *);
35 static void
36 setup(const char *pw)
38 int ic, i, k, temp;
39 char salt[3];
40 unsigned rnd;
41 long seed;
42 char *cryptpw;
44 strncpy(salt, pw, sizeof(salt));
45 cryptpw = crypt(pw, salt);
46 if (cryptpw == NULL) {
47 fprintf(stderr, "crypt(3) failure\n");
48 exit(1);
50 memcpy(buf, cryptpw, sizeof(buf));
51 seed = 123;
52 for (i=0; i<13; i++)
53 seed = seed*buf[i] + i;
54 for(i=0;i<ROTORSZ;i++) {
55 t1[i] = i;
56 deck[i] = 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);
64 else
65 seed &= 0x7FFFFFFF;
67 rnd = seed % 65521;
68 k = ROTORSZ-1 - i;
69 ic = (rnd&MASK)%(k+1);
70 rnd >>= 8;
71 temp = t1[k];
72 t1[k] = t1[ic];
73 t1[ic] = temp;
74 if(t3[k]!=0) continue;
75 ic = (rnd&MASK) % k;
76 while(t3[ic]!=0) ic = (ic+1) % k;
77 t3[k] = ic;
78 t3[ic] = k;
80 for(i=0;i<ROTORSZ;i++)
81 t2[t1[i]&MASK] = i;
84 int
85 main(int argc, char **argv)
87 int i, n1, n2, nr1, nr2;
88 int secureflg = 0, kflag = 0;
89 char *cp;
91 if (argc > 1 && argv[1][0] == '-') {
92 if (argv[1][1] == 's') {
93 argc--;
94 argv++;
95 secureflg = 1;
96 } else if (argv[1][1] == 'k') {
97 argc--;
98 argv++;
99 kflag = 1;
102 if (kflag) {
103 if ((cp = getenv(MINUSKVAR)) == NULL) {
104 fprintf(stderr, "%s not set\n", MINUSKVAR);
105 exit(1);
107 setup(cp);
108 } else if (argc != 2) {
109 setup(getpass("Enter key:"));
111 else
112 setup(argv[1]);
113 n1 = 0;
114 n2 = 0;
115 nr2 = 0;
117 while((i=getchar()) != -1) {
118 if (secureflg) {
119 nr1 = deck[n1]&MASK;
120 nr2 = deck[nr1]&MASK;
121 } else {
122 nr1 = n1;
124 i = t2[(t3[(t1[(i+nr1)&MASK]+nr2)&MASK]-nr2)&MASK]-nr1;
125 putchar(i);
126 n1++;
127 if(n1==ROTORSZ) {
128 n1 = 0;
129 n2++;
130 if(n2==ROTORSZ) n2 = 0;
131 if (secureflg) {
132 shuffle(deck);
133 } else {
134 nr2 = n2;
139 return 0;
142 static void
143 shuffle(char *deckary)
145 int i, ic, k, temp;
146 unsigned rnd;
147 static long seed = 123;
149 for(i=0;i<ROTORSZ;i++) {
150 seed = 5*seed + buf[i%13];
151 rnd = seed % 65521;
152 k = ROTORSZ-1 - i;
153 ic = (rnd&MASK)%(k+1);
154 temp = deckary[k];
155 deckary[k] = deckary[ic];
156 deckary[ic] = temp;