1998-10-30 18:20:54 +00:00
|
|
|
/*
|
|
|
|
* "enigma.c" is in file cbw.tar from
|
|
|
|
* anonymous FTP host watmsg.waterloo.edu: pub/crypt/cbw.tar.Z
|
|
|
|
*
|
|
|
|
* A one-rotor machine designed along the lines of Enigma
|
|
|
|
* but considerably trivialized.
|
|
|
|
*
|
|
|
|
* A public-domain replacement for the UNIX "crypt" command.
|
|
|
|
*
|
|
|
|
* Upgraded to function properly on 64-bit machines.
|
|
|
|
*/
|
|
|
|
|
2001-06-15 23:25:22 +00:00
|
|
|
#ifndef lint
|
|
|
|
static const char rcsid[] =
|
|
|
|
"$FreeBSD$";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
1998-10-30 18:24:54 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
1998-10-30 18:20:54 +00:00
|
|
|
#include <stdio.h>
|
1998-10-30 18:24:54 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#define MINUSKVAR "CrYpTkEy"
|
|
|
|
|
|
|
|
#define ECHO 010
|
1998-10-30 18:20:54 +00:00
|
|
|
#define ROTORSZ 256
|
|
|
|
#define MASK 0377
|
|
|
|
char t1[ROTORSZ];
|
|
|
|
char t2[ROTORSZ];
|
|
|
|
char t3[ROTORSZ];
|
|
|
|
char deck[ROTORSZ];
|
|
|
|
char buf[13];
|
|
|
|
|
1998-10-30 18:24:54 +00:00
|
|
|
void shuffle(char *);
|
2001-06-25 21:25:27 +00:00
|
|
|
void setup(char *);
|
1998-10-30 18:20:54 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
setup(pw)
|
1998-10-30 18:24:54 +00:00
|
|
|
char *pw;
|
1998-10-30 18:20:54 +00:00
|
|
|
{
|
|
|
|
int ic, i, k, temp, pf[2], pid;
|
2001-06-25 21:25:27 +00:00
|
|
|
unsigned rnd;
|
1998-10-30 18:20:54 +00:00
|
|
|
long seed;
|
|
|
|
|
|
|
|
strncpy(buf, pw, 8);
|
|
|
|
while (*pw)
|
|
|
|
*pw++ = '\0';
|
|
|
|
buf[8] = buf[0];
|
|
|
|
buf[9] = buf[1];
|
|
|
|
pipe(pf);
|
|
|
|
if ((pid=fork())==0) {
|
|
|
|
close(0);
|
|
|
|
close(1);
|
|
|
|
dup(pf[0]);
|
|
|
|
dup(pf[1]);
|
2001-07-09 09:24:06 +00:00
|
|
|
execlp("makekey", "-", (char *)0);
|
|
|
|
execl("/usr/libexec/makekey", "-", (char *)0); /* BSDI */
|
|
|
|
execl("/usr/lib/makekey", "-", (char *)0);
|
|
|
|
execl("/usr/bin/makekey", "-", (char *)0); /* IBM */
|
|
|
|
execl("/lib/makekey", "-", (char *)0);
|
1998-10-30 18:20:54 +00:00
|
|
|
perror("makekey");
|
|
|
|
fprintf(stderr, "enigma: cannot execute 'makekey', aborting\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
write(pf[1], buf, 10);
|
|
|
|
close(pf[1]);
|
|
|
|
i=wait((int *)NULL);
|
|
|
|
if (i<0) perror("enigma: wait");
|
|
|
|
if (i!=pid) {
|
|
|
|
fprintf(stderr, "enigma: expected pid %d, got pid %d\n", pid, i);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if ((i=read(pf[0], buf, 13)) != 13) {
|
|
|
|
fprintf(stderr, "enigma: cannot generate key, read %d\n",i);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
seed = 123;
|
|
|
|
for (i=0; i<13; i++)
|
|
|
|
seed = seed*buf[i] + i;
|
|
|
|
for(i=0;i<ROTORSZ;i++) {
|
|
|
|
t1[i] = i;
|
|
|
|
deck[i] = i;
|
|
|
|
}
|
|
|
|
for(i=0;i<ROTORSZ;i++) {
|
|
|
|
seed = 5*seed + buf[i%13];
|
|
|
|
if( sizeof(long) > 4 ) {
|
|
|
|
/* Force seed to stay in 32-bit signed math */
|
|
|
|
if( seed & 0x80000000 )
|
|
|
|
seed = seed | (-1L & ~0xFFFFFFFFL);
|
|
|
|
else
|
|
|
|
seed &= 0x7FFFFFFF;
|
|
|
|
}
|
2001-06-25 21:25:27 +00:00
|
|
|
rnd = seed % 65521;
|
1998-10-30 18:20:54 +00:00
|
|
|
k = ROTORSZ-1 - i;
|
2001-06-25 21:25:27 +00:00
|
|
|
ic = (rnd&MASK)%(k+1);
|
|
|
|
rnd >>= 8;
|
1998-10-30 18:20:54 +00:00
|
|
|
temp = t1[k];
|
|
|
|
t1[k] = t1[ic];
|
|
|
|
t1[ic] = temp;
|
|
|
|
if(t3[k]!=0) continue;
|
2001-06-25 21:25:27 +00:00
|
|
|
ic = (rnd&MASK) % k;
|
1998-10-30 18:20:54 +00:00
|
|
|
while(t3[ic]!=0) ic = (ic+1) % k;
|
|
|
|
t3[k] = ic;
|
|
|
|
t3[ic] = k;
|
|
|
|
}
|
|
|
|
for(i=0;i<ROTORSZ;i++)
|
|
|
|
t2[t1[i]&MASK] = i;
|
|
|
|
}
|
|
|
|
|
1998-10-30 18:24:54 +00:00
|
|
|
int
|
1998-10-30 18:20:54 +00:00
|
|
|
main(argc, argv)
|
2001-06-25 21:25:27 +00:00
|
|
|
int argc;
|
1998-10-30 18:24:54 +00:00
|
|
|
char *argv[];
|
1998-10-30 18:20:54 +00:00
|
|
|
{
|
|
|
|
register int i, n1, n2, nr1, nr2;
|
1998-10-30 18:24:54 +00:00
|
|
|
int secureflg = 0, kflag = 0;
|
|
|
|
char *cp;
|
1998-10-30 18:20:54 +00:00
|
|
|
|
1998-10-30 18:24:54 +00:00
|
|
|
if (argc > 1 && argv[1][0] == '-') {
|
|
|
|
if (argv[1][1] == 's') {
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
secureflg = 1;
|
|
|
|
} else if (argv[1][1] == 'k') {
|
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
kflag = 1;
|
|
|
|
}
|
1998-10-30 18:20:54 +00:00
|
|
|
}
|
1998-10-30 18:24:54 +00:00
|
|
|
if (kflag) {
|
|
|
|
if ((cp = getenv(MINUSKVAR)) == NULL) {
|
|
|
|
fprintf(stderr, "%s not set\n", MINUSKVAR);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
setup(cp);
|
|
|
|
} else if (argc != 2) {
|
1998-10-30 18:20:54 +00:00
|
|
|
setup(getpass("Enter key:"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
setup(argv[1]);
|
|
|
|
n1 = 0;
|
|
|
|
n2 = 0;
|
|
|
|
nr2 = 0;
|
|
|
|
|
1998-10-30 18:24:54 +00:00
|
|
|
while((i=getchar()) != -1) {
|
1998-10-30 18:20:54 +00:00
|
|
|
if (secureflg) {
|
|
|
|
nr1 = deck[n1]&MASK;
|
|
|
|
nr2 = deck[nr1]&MASK;
|
|
|
|
} else {
|
|
|
|
nr1 = n1;
|
|
|
|
}
|
|
|
|
i = t2[(t3[(t1[(i+nr1)&MASK]+nr2)&MASK]-nr2)&MASK]-nr1;
|
|
|
|
putchar(i);
|
|
|
|
n1++;
|
|
|
|
if(n1==ROTORSZ) {
|
|
|
|
n1 = 0;
|
|
|
|
n2++;
|
|
|
|
if(n2==ROTORSZ) n2 = 0;
|
|
|
|
if (secureflg) {
|
|
|
|
shuffle(deck);
|
|
|
|
} else {
|
|
|
|
nr2 = n2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1998-10-30 18:24:54 +00:00
|
|
|
|
|
|
|
return 0;
|
1998-10-30 18:20:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-06-25 21:25:27 +00:00
|
|
|
shuffle(deckary)
|
|
|
|
char deckary[];
|
1998-10-30 18:20:54 +00:00
|
|
|
{
|
|
|
|
int i, ic, k, temp;
|
2001-06-25 21:25:27 +00:00
|
|
|
unsigned rnd;
|
1998-10-30 18:20:54 +00:00
|
|
|
static long seed = 123;
|
|
|
|
|
|
|
|
for(i=0;i<ROTORSZ;i++) {
|
|
|
|
seed = 5*seed + buf[i%13];
|
2001-06-25 21:25:27 +00:00
|
|
|
rnd = seed % 65521;
|
1998-10-30 18:20:54 +00:00
|
|
|
k = ROTORSZ-1 - i;
|
2001-06-25 21:25:27 +00:00
|
|
|
ic = (rnd&MASK)%(k+1);
|
|
|
|
temp = deckary[k];
|
|
|
|
deckary[k] = deckary[ic];
|
|
|
|
deckary[ic] = temp;
|
1998-10-30 18:20:54 +00:00
|
|
|
}
|
|
|
|
}
|