v07i027: Purported DES program in C
sources-request at mirror.UUCP
sources-request at mirror.UUCP
Mon Sep 15 14:17:06 AEST 1986
Submitted by: cca!caip!lll-crg!hoptoad!gnu (John Gilmore)
Mod.sources: Volume 7, Issue 27
Archive-name: des
[ I wrote the Makefile and manpage. Before compiling this program, you
will want to take a look at the non-standard fopen calls. I believe
the "rb" and "wb" parameters are instructions to open the file in
'binary' (8bit, no newline mapping) mode for some MS-DOS compilers.
--r$ ]
#!/bin/sh
# This is a shell archive. Remove anything before this line,
# then unpack it by saving it in a file and typing "sh file".
# If all goes well, you will see the message "No problems found."
# Wrapped by mirror!rs on Mon Sep 15 00:14:18 EDT 1986
# Exit status; set to 1 on "wc" errors or if would overwrite.
STATUS=0
# Contents: README Makefile des.1 des.c
echo x - README
if test -f README ; then
echo README exists, putting output in $$README
OUT=$$README
STATUS=1
else
OUT=README
fi
sed 's/^X//' > $OUT <<'@//E*O*F README//'
XThis program came from a cryptography related BBS near Washington, DC.
XI have been unable to verify whether it works. It does compile and run
Xon 4.2BSD (Sun Unix). However, the other DES implementation I have
Xavailable (Sun's) uses a different format for keys. I wish both would
Xallow you to type the 56-bit key as 14 digits of hex -- but neither one
Xdoes, they do different transformations of 8 bytes of ASCII.
XIf anyone has the DES validation samples [from the standard or its
Xaccompanying publications], please post them.
XNote that I do not recommend the use of DES if you want to keep your
Xdata secure. All that can be said for it is that fewer people know
Xhow to break it than crypt(1).
@//E*O*F README//
chmod u=rw,g=rw,o=rw $OUT
echo x - Makefile
if test -f Makefile ; then
echo Makefile exists, putting output in $$Makefile
OUT=$$Makefile
STATUS=1
else
OUT=Makefile
fi
sed 's/^X//' > $OUT <<'@//E*O*F Makefile//'
Xdes: des.c
X cc -O -o des des.c
X# these targets are for my BSD system; your mileage will vary.
Xinstall: des
X cp des /usr/bin/des
X cp des.1 /usr/man/man1/des.1
@//E*O*F Makefile//
chmod u=rw,g=rw,o=rw $OUT
echo x - des.1
if test -f des.1 ; then
echo des.1 exists, putting output in $$des.1
OUT=$$des.1
STATUS=1
else
OUT=des.1
fi
sed 's/^X//' > $OUT <<'@//E*O*F des.1//'
X.TH DES 1 LOCAL
X.SH NAME
Xdes \- perform DES encryption on a file
X.SH SYNOPSIS
X.B des
Xfilename
X.SH DESCRIPTION
XThis program takes a single argument, a file to encrypt or decrypt.
XWhen started, it asks for a password.
XIf the file is named like ``foo.n'' the file is decrypted with the given key,
Xand output is put into the file ``foo.''
XOtherwise, if the file is named like ``foo,'' the file is encrypted and
Xthe output is put into ``foo.n''.
XIn both cases, the original input file is removed.
X.SH "SEE ALSO"
Xcrypt(1)
X.SH BUGS
XMay not be true DES.
@//E*O*F des.1//
chmod u=rw,g=rw,o=rw $OUT
echo x - des.c
if test -f des.c ; then
echo des.c exists, putting output in $$des.c
OUT=$$des.c
STATUS=1
else
OUT=des.c
fi
sed 's/^X//' > $OUT <<'@//E*O*F des.c//'
X/* des: duplicate the NBS Data Encryption Standard in software.
X * usage: des <file>
X * prompts for the password
X * If the filename ends in ".n" it will be decrypted with the key;
X * otherwise it will be encrypted.
X *
X * Permutation algorithm:
X * The permutation is defined by its effect on each of the 16 nibbles
X * of the 64-bit input. For each nibble we give an 8-byte bit array
X * that has the bits in the input nibble distributed correctly. The
X * complete permutation involves ORing the 16 sets of 8 bytes designated
X * by the 16 input nibbles. Uses 16*16*8 = 2K bytes of storage for
X * each 64-bit permutation. 32-bit permutations (P) and expansion (E)
X * are done similarly, but using bytes instead of nibbles.
X * Should be able to use long ints, adding the masks, at a
X * later pass. Tradeoff: can speed 64-bit perms up at cost of slowing
X * down expansion or contraction operations by using 8K tables here and
X * decreasing the size of the other tables.
X * The compressions are pre-computed in 12-bit chunks, combining 2 of the
X * 6->4 bit compressions.
X * The key schedule is also precomputed.
X * Compile with VALIDATE defined to run the NBS validation suite.
X *
X * Jim Gillogly, May 1977
X * Modified 8/84 by Jim Gillogly and Lauren Weinstein to compile with
X * post-1977 C compilers and systems
X *
X * This program is now officially in the public domain, and is available for
X * any non-profit use as long as the authorship line is retained.
X */
X/*#define VALIDATE */ /* define to check the NBS validation suite */
X/*#define DEBUG */
X/*#define LATTICE */ /* define for Lattice C on IBM PC */
X#include <stdio.h>
X#ifndef LATTICE
X#include <sgtty.h>
X#include <signal.h>
X#include <sys/types.h> /* for local timer */
X#include <sys/timeb.h> /* ditto */
Xstruct sgttyb ttybuf; /* for gtty/stty */
Xint bye(); /* for caught interrupts */
X#endif
Xchar iperm[16][16][8],fperm[16][16][8]; /* inital and final permutations*/
Xchar s[4][4096]; /* S1 thru S8 precomputed */
Xchar p32[4][256][4]; /* for permuting 32-bit f output*/
Xchar kn[16][6]; /* key selections */
Xendes(inblock,outblock) /* encrypt 64-bit inblock */
Xchar *inblock, *outblock;
X{ char iters[17][8]; /* workspace for each iteration */
X char swap[8]; /* place to interchange L and R */
X register int i;
X register char *s, *t;
X permute(inblock,iperm,iters[0]);/* apply initial permutation */
X for (i=0; i<16; i++) /* 16 churning operations */
X iter(i,iters[i],iters[i+1]);
X /* don't re-copy to save space */
X s = swap; t = &iters[16][4]; /* interchange left */
X *s++ = *t++; *s++ = *t++; *s++ = *t++; *s++ = *t++;
X t = &iters[16][0]; /* and right */
X *s++ = *t++; *s++ = *t++; *s++ = *t++; *s++ = *t++;
X permute(swap,fperm,outblock); /* apply final permutation */
X}
Xdedes(inblock,outblock) /* decrypt 64-bit inblock */
Xchar *inblock,*outblock;
X{ char iters[17][8]; /* workspace for each iteration */
X char swap[8]; /* place to interchange L and R */
X register int i;
X register char *s, *t;
X permute(inblock,iperm,iters[0]);/* apply initial permutation */
X for (i=0; i<16; i++) /* 16 churning operations */
X iter(15-i,iters[i],iters[i+1]);
X /* reverse order from encrypting*/
X s = swap; t = &iters[16][4]; /* interchange left */
X *s++ = *t++; *s++ = *t++; *s++ = *t++; *s++ = *t++;
X t = &iters[16][0]; /* and right */
X *s++ = *t++; *s++ = *t++; *s++ = *t++; *s++ = *t++;
X permute(swap,fperm,outblock); /* apply final permutation */
X}
Xpermute(inblock,perm,outblock) /* permute inblock with perm */
Xchar *inblock, *outblock; /* result into outblock,64 bits */
Xchar perm[16][16][8]; /* 2K bytes defining perm. */
X{ register int i,j;
X register char *ib, *ob; /* ptr to input or output block */
X register char *p, *q;
X for (i=0, ob = outblock; i<8; i++)
X *ob++ = 0; /* clear output block */
X ib = inblock;
X for (j = 0; j < 16; j += 2, ib++) /* for each input nibble */
X { ob = outblock;
X p = perm[j][(*ib >> 4) & 017];
X q = perm[j + 1][*ib & 017];
X for (i = 0; i < 8; i++) /* and each output byte */
X *ob++ |= *p++ | *q++; /* OR the masks together*/
X }
X}
Xchar ip[] /* initial permutation P */
X= { 58, 50, 42, 34, 26, 18, 10, 2,
X 60, 52, 44, 36, 28, 20, 12, 4,
X 62, 54, 46, 38, 30, 22, 14, 6,
X 64, 56, 48, 40, 32, 24, 16, 8,
X 57, 49, 41, 33, 25, 17, 9, 1,
X 59, 51, 43, 35, 27, 19, 11, 3,
X 61, 53, 45, 37, 29, 21, 13, 5,
X 63, 55, 47, 39, 31, 23, 15, 7 };
Xchar fp[] /* final permutation F */
X= { 40, 8, 48, 16, 56, 24, 64, 32,
X 39, 7, 47, 15, 55, 23, 63, 31,
X 38, 6, 46, 14, 54, 22, 62, 30,
X 37, 5, 45, 13, 53, 21, 61, 29,
X 36, 4, 44, 12, 52, 20, 60, 28,
X 35, 3, 43, 11, 51, 19, 59, 27,
X 34, 2, 42, 10, 50, 18, 58, 26,
X 33, 1, 41, 9, 49, 17, 57, 25 };
X/* expansion operation matrix */ /* rwo: unused */
X/* char ei[] = { 32, 1, 2, 3, 4, 5,
X 4, 5, 6, 7, 8, 9,
X 8, 9, 10, 11, 12, 13,
X 12, 13, 14, 15, 16, 17,
X 16, 17, 18, 19, 20, 21,
X 20, 21, 22, 23, 24, 25,
X 24, 25, 26, 27, 28, 29,
X 28, 29, 30, 31, 32, 1 }; */
Xchar pc1[] /* permuted choice table (key) */
X= { 57, 49, 41, 33, 25, 17, 9,
X 1, 58, 50, 42, 34, 26, 18,
X 10, 2, 59, 51, 43, 35, 27,
X 19, 11, 3, 60, 52, 44, 36,
X 63, 55, 47, 39, 31, 23, 15,
X 7, 62, 54, 46, 38, 30, 22,
X 14, 6, 61, 53, 45, 37, 29,
X 21, 13, 5, 28, 20, 12, 4 };
Xchar totrot[] /* number left rotations of pc1 */
X= { 1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28 };
Xchar pc1m[56]; /* place to modify pc1 into */
Xchar pcr[56]; /* place to rotate pc1 into */
Xchar pc2[] /* permuted choice key (table) */
X= { 14, 17, 11, 24, 1, 5,
X 3, 28, 15, 6, 21, 10,
X 23, 19, 12, 4, 26, 8,
X 16, 7, 27, 20, 13, 2,
X 41, 52, 31, 37, 47, 55,
X 30, 40, 51, 45, 33, 48,
X 44, 49, 39, 56, 34, 53,
X 46, 42, 50, 36, 29, 32 };
Xchar si[8][64] /* 48->32 bit compression tables*/
X= { /* S[1] */
X 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
X 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
X 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
X 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13,
X /* S[2] */
X 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
X 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
X 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
X 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9,
X /* S[3] */
X 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
X 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
X 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
X 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12,
X /* S[4] */
X 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
X 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
X 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
X 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14,
X /* S[5] */
X 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
X 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
X 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
X 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3,
X /* S[6] */
X 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
X 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
X 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
X 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13,
X /* S[7] */
X 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
X 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
X 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
X 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12,
X /* S[8] */
X 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
X 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
X 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
X 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 };
Xchar p32i[] /* 32-bit permutation function */
X= { 16, 7, 20, 21,
X 29, 12, 28, 17,
X 1, 15, 23, 26,
X 5, 18, 31, 10,
X 2, 8, 24, 14,
X 32, 27, 3, 9,
X 19, 13, 30, 6,
X 22, 11, 4, 25 };
Xdesinit(key) /* initialize all des arrays */
Xchar *key;
X{
X#ifdef DEBUG
X/*deb*/ printf("Initial perm init.\n");
X#endif
X perminit(iperm,ip); /* initial permutation */
X#ifdef DEBUG
X/*deb*/ printf("Final perm init.\n");
X#endif
X perminit(fperm,fp); /* final permutation */
X#ifdef DEBUG
X/*deb*/ printf("Key sched init.\n");
X#endif
X kinit(key); /* key schedule */
X#ifdef DEBUG
X/*deb*/ printf("Compression init.\n");
X#endif
X sinit(); /* compression functions */
X#ifdef DEBUG
X/*deb*/ printf("32-bit perm init.\n");
X#endif
X p32init(); /* 32-bit permutation in f */
X#ifdef DEBUG
X/*deb*/ printf("End init.\n");
X#endif
X}
Xint bytebit[] /* bit 0 is left-most in byte */
X = { 0200,0100,040,020,010,04,02,01 };
Xint nibblebit[] = { 010,04,02,01 };
Xsinit() /* initialize s1-s8 arrays */
X{ register int i,j;
X for (i=0; i<4; i++) /* each 12-bit position */
X for (j=0; j<4096; j++) /* each possible 12-bit value */
X s[i][j]=(getcomp(i*2,j>>6)<<4) |
X (017&getcomp(i*2+1,j&077));
X /* store 2 compressions per char*/
X}
Xgetcomp(k,v) /* 1 compression value for sinit*/
Xint k,v;
X{ register int i,j; /* correspond to i and j in FIPS*/
X i=((v&040)>>4)|(v&1); /* first and last bits make row */
X j=(v&037)>>1; /* middle 4 bits are column */
X return (int) si[k][(i<<4)+j]; /* result is ith row, jth col */
X}
Xkinit(key) /* initialize key schedule array*/
Xchar *key; /* 64 bits (will use only 56) */
X{ register int i,j,l;
X int m;
X for (j=0; j<56; j++) /* convert pc1 to bits of key */
X { l=pc1[j]-1; /* integer bit location */
X m = l & 07; /* find bit */
X pc1m[j]=(key[l>>3] & /* find which key byte l is in */
X bytebit[m]) /* and which bit of that byte */
X ? 1 : 0; /* and store 1-bit result */
X }
X for (i=0; i<16; i++) /* for each key sched section */
X for (j=0; j<6; j++) /* and each byte of the kn */
X kn[i][j]=0; /* clear it for accumulation */
X for (i=0; i<16; i++) /* key chunk for each iteration */
X { for (j=0; j<56; j++) /* rotate pc1 the right amount */
X pcr[j] = pc1m[(l=j+totrot[i])<(j<28? 28 : 56) ? l: l-28];
X /* rotate left and right halves independently */
X for (j=0; j<48; j++) /* select bits individually */
X if (pcr[pc2[j]-1]) /* check bit that goes to kn[j] */
X { l= j & 07;
X kn[i][j>>3] |= bytebit[l];
X } /* mask it in if it's there */
X }
X}
Xp32init() /* initialize 32-bit permutation*/
X{ register int l, j, k;
X int i,m;
X for (i=0; i<4; i++) /* each input byte position */
X for (j=0; j<256; j++) /* all possible input bytes */
X for (k=0; k<4; k++) /* each byte of the mask */
X p32[i][j][k]=0; /* clear permutation array */
X for (i=0; i<4; i++) /* each input byte position */
X for (j=0; j<256; j++) /* each possible input byte */
X for (k=0; k<32; k++) /* each output bit position */
X { l=p32i[k]-1; /* invert this bit (0-31) */
X if ((l>>3)!=i) /* does it come from input posn?*/
X continue; /* if not, bit k is 0 */
X if (!(j&bytebit[l&07]))
X continue; /* any such bit in input? */
X m = k & 07; /* which bit is it? */
X p32[i][j][k>>3] |= bytebit[m];
X }
X}
Xperminit(perm,p) /* initialize a perm array */
Xchar perm[16][16][8]; /* 64-bit, either init or final */
Xchar p[64];
X{ register int l, j, k;
X int i,m;
X for (i=0; i<16; i++) /* each input nibble position */
X for (j=0; j<16; j++) /* all possible input nibbles */
X for (k=0; k<8; k++) /* each byte of the mask */
X perm[i][j][k]=0;/* clear permutation array */
X for (i=0; i<16; i++) /* each input nibble position */
X for (j = 0; j < 16; j++)/* each possible input nibble */
X for (k = 0; k < 64; k++)/* each output bit position */
X { l = p[k] - 1; /* where does this bit come from*/
X if ((l >> 2) != i) /* does it come from input posn?*/
X continue; /* if not, bit k is 0 */
X if (!(j & nibblebit[l & 3]))
X continue; /* any such bit in input? */
X m = k & 07; /* which bit is this in the byte*/
X perm[i][j][k>>3] |= bytebit[m];
X }
X}
Xiter(num,inblock,outblock) /* 1 churning operation */
Xint num; /* i.e. the num-th one */
Xchar *inblock, *outblock; /* 64 bits each */
X{ char fret[4]; /* return from f(R[i-1],key) */
X register char *ib, *ob, *fb;
X/* register int i; */ /* rwo: unused */
X ob = outblock; ib = &inblock[4];
X f(ib, num, fret); /* the primary transformation */
X *ob++ = *ib++; /* L[i] = R[i-1] */
X *ob++ = *ib++;
X *ob++ = *ib++;
X *ob++ = *ib++;
X ib = inblock; fb = fret; /* R[i]=L[i] XOR f(R[i-1],key) */
X *ob++ = *ib++ ^ *fb++;
X *ob++ = *ib++ ^ *fb++;
X *ob++ = *ib++ ^ *fb++;
X *ob++ = *ib++ ^ *fb++;
X}
Xf(right,num,fret) /* critical cryptographic trans */
Xchar *right, *fret; /* 32 bits each */
Xint num; /* index number of this iter */
X{ register char *kb, *rb, *bb; /* ptr to key selection &c */
X char bigright[6]; /* right expanded to 48 bits */
X char result[6]; /* expand(R) XOR keyselect[num] */
X char preout[4]; /* result of 32-bit permutation */
X kb = kn[num]; /* fast version of iteration */
X bb = bigright;
X rb = result;
X expand(right,bb); /* expand to 48 bits */
X *rb++ = *bb++ ^ *kb++; /* expanded R XOR chunk of key */
X *rb++ = *bb++ ^ *kb++;
X *rb++ = *bb++ ^ *kb++;
X *rb++ = *bb++ ^ *kb++;
X *rb++ = *bb++ ^ *kb++;
X *rb++ = *bb++ ^ *kb++;
X contract(result,preout); /* use S fns to get 32 bits */
X perm32(preout,fret); /* and do final 32-bit perm */
X}
Xperm32(inblock,outblock) /* 32-bit permutation at end */
Xchar *inblock,*outblock; /* of the f crypto function */
X{ register int j;
X/* register int i; */ /* rwo: unused */
X register char *ib, *ob;
X register char *q;
X ob = outblock; /* clear output block */
X *ob++ = 0; *ob++ = 0; *ob++ = 0; *ob++ = 0;
X ib=inblock; /* ptr to 1st byte of input */
X for (j=0; j<4; j++, ib++) /* for each input byte */
X { q = p32[j][*ib & 0377];
X ob = outblock; /* and each output byte */
X *ob++ |= *q++; /* OR the 16 masks together */
X *ob++ |= *q++;
X *ob++ |= *q++;
X *ob++ |= *q++;
X }
X}
Xexpand(right,bigright) /* 32 to 48 bits with E oper */
Xchar *right,*bigright; /* right is 32, bigright 48 */
X{
X register char *bb, *r, r0, r1, r2, r3;
X bb = bigright;
X r = right; r0 = *r++; r1 = *r++; r2 = *r++; r3 = *r++;
X *bb++ = ((r3 & 0001) << 7) | /* 32 */
X ((r0 & 0370) >> 1) | /* 1 2 3 4 5 */
X ((r0 & 0030) >> 3); /* 4 5 */
X *bb++ = ((r0 & 0007) << 5) | /* 6 7 8 */
X ((r1 & 0200) >> 3) | /* 9 */
X ((r0 & 0001) << 3) | /* 8 */
X ((r1 & 0340) >> 5); /* 9 10 11 */
X *bb++ = ((r1 & 0030) << 3) | /* 12 13 */
X ((r1 & 0037) << 1) | /* 12 13 14 15 16 */
X ((r2 & 0200) >> 7); /* 17 */
X *bb++ = ((r1 & 0001) << 7) | /* 16 */
X ((r2 & 0370) >> 1) | /* 17 18 19 20 21 */
X ((r2 & 0030) >> 3); /* 20 21 */
X *bb++ = ((r2 & 0007) << 5) | /* 22 23 24 */
X ((r3 & 0200) >> 3) | /* 25 */
X ((r2 & 0001) << 3) | /* 24 */
X ((r3 & 0340) >> 5); /* 25 26 27 */
X *bb++ = ((r3 & 0030) << 3) | /* 28 29 */
X ((r3 & 0037) << 1) | /* 28 29 30 31 32 */
X ((r0 & 0200) >> 7); /* 1 */
X}
Xcontract(in48,out32) /* contract f from 48 to 32 bits*/
Xchar *in48,*out32; /* using 12-bit pieces into bytes */
X{ register char *c;
X register char *i;
X register int i0, i1, i2, i3, i4, i5;
X i = in48;
X i0 = *i++; i1 = *i++; i2 = *i++; i3 = *i++; i4 = *i++; i5 = *i++;
X c = out32; /* do output a byte at a time */
X *c++ = s[0][07777 & ((i0 << 4) | ((i1 >> 4) & 017 ))];
X *c++ = s[1][07777 & ((i1 << 8) | ( i2 & 0377 ))];
X *c++ = s[2][07777 & ((i3 << 4) | ((i4 >> 4) & 017 ))];
X *c++ = s[3][07777 & ((i4 << 8) | ( i5 & 0377 ))];
X}
X/* End of DES algorithm (except for calling desinit below) */
X#ifndef VALIDATE
Xchar *inname, *outname;
XFILE *infile, *outfile;
Xint encrypting;
Xchar buf[512];
Xchar keyx[9], keyy[9];
Xchar *malloc(), *strcpy(), *strcat();
Xmain(argc, argv)
Xint argc; char *argv[];
X{ register char *u;
X char *filename;
X if (argc < 2) /* filenames given? */
X { fprintf(stderr, "Usage: des file ...\n");
X exit(1);
X }
X for (++argv; --argc; ++argv)
X { inname = *argv;
X outname = filename = malloc((unsigned) strlen(inname) + 3);
X strcpy(filename, inname);
X u = &filename[strlen(filename) - 2]; /* check last 2 chars */
X encrypting = (strcmp(".n", u) != 0);
X if (!encrypting) *u = 0; /* strip .n from output filename */
X else strcat(filename, ".n"); /* or add .n to output file */
X if ((infile = fopen(inname, "rb")) == NULL)
X { fprintf(stderr,"Can't read %s.\n", inname);
X exit(1);
X }
X if ((outfile = fopen(outname, "rb")) != NULL)
X { fprintf(stderr, "%s would be overwritten.\n",outname);
X exit(1);
X }
X if ((outfile = fopen(outname, "wb")) == NULL)
X { fprintf(stderr,"Can't write %s.\n", outname);
X exit(1);
X }
X key_get("Type password for ");
X for (;;)
X { strcpy(keyx, keyy);
X key_get("Verify password for ");
X if (strcmp(keyx, keyy) == 0) break;
X }
X desinit(keyx); /* set up tables for DES */
X if (pfile() == 0) unlink(inname);
X else fprintf(stderr,
X "%s: I/O Error -- File unchanged\n", inname);
X fclose(outfile);
X fclose(infile);
X }
X exit(0);
X}
Xkey_get(mes) /* get file key */
Xchar *mes;
X{ register int i, j;
X char linebuf[256];
X int count;
X for (i=0; i<14; i++) keyy[i]=0;
X#ifdef LATTICE
X#else
X gtty(0, &ttybuf);
X ttybuf.sg_flags &= ~ECHO; /* turn off echoing */
X signal(SIGINT, bye); /* catch ints */
X stty(0, &ttybuf);
X#endif
X printf("%s%s: ", mes, inname);
X fflush(stdout);
X count = read(0, linebuf, 256); /* read input line */
X printf("\n");
X#ifndef LATTICE
X ttybuf.sg_flags |= ECHO; /* restore echo */
X stty(0, &ttybuf);
X#endif
X linebuf[count] = 0; /* null terminate */
X if (linebuf[count-1] == '\n') /* ignore any terminating newline */
X { linebuf[count-1] = 0;
X count--;
X }
X if (count > 8) count = 8; /* only use 8 chars */
X for (i = j = 0; count--;)
X keyy[i++] = linebuf[j++];
X}
Xpfile() /* process the file */
X{ register int m, nsave;
X register char *b;
X int j;
X while (m = fread(buf, 1, 512, infile))
X {
X if ((nsave = m) < 0) /* read error */
X return(-1);
X for (b=buf; m>0; /* encrypt/decrypt 1 buffer-full*/
X m -= 8, b += 8) /* 8-byte blocks */
X { if (encrypting)
X { if (m<8) /* don't have a full 64 bits */
X { for (j=0; j<8-m; j++)
X b[m+j]=garbage(); /* fill block with trash */
X nsave += 8-m; /* complete the block */
X }
X else j=0; /* number of nulls in last block*/
X endes(b,b); /* don't need diff input, output*/
X }
X else /* decrypting */
X { if (m < 8) deout(b, 1); /* last byte in file: count */
X else
X { dedes(b, b); /* decrypt and output block */
X deout(b, 0);
X }
X }
X }
X if (encrypting) if (fwrite(buf, 1, nsave, outfile) != nsave)
X return(-1);
X }
X /* have now encrypted/decrypted the whole file;
X * need to append the byte count for the last block if encrypting.
X */
X if (encrypting) fputc(8 - j, outfile); /* how many good bytes? */
X return(0);
X}
Xint outcount = 0; /* see when caught up with delay*/
Xdeout(block,flag) /* 1-block delay on output */
Xchar *block,flag; /* 64-bit block, last block flag*/
X{ static char last[8]; /* previous input block */
X register int i;
X/* register char *c,*j; */ /* rwo: unused */
X if (flag) /* output the last few bytes */
X {
X fwrite(last, 1, block[0] & 0377, outfile);
X return;
X }
X if (outcount++) /* seen any blocks before? */
X fwrite(last, 1, 8, outfile);
X for (i = 0; i < 8; i++) last[i] = block[i]; /* copy the block */
X}
Xgarbage() /* generate garbage for filling */
X/* This garbage should be as random as possible. We're using subsequent calls
X * on the timer, but ideally each byte should be uncorrelated. Preferable
X * would be to call the timer once and use it to initialize a dumb random
X * number generator.
X */
X{
X#ifdef LATTICE
X long timer(), ltime;
X ltime = timer();
X return (int) ltime & 0377;
X#else
X struct timeb tp;
X ftime(&tp); /* get current time */
X return tp.millitm; /* return time in milliseconds */
X#endif
X}
X#ifndef LATTICE
X/* restore echo to tty and exit */
Xbye()
X{
X ttybuf.sg_flags |= ECHO; /* restore echoing */
X stty(0, &ttybuf);
X exit(2);
X}
X#endif
X#else /* validation */
X#define VALFILE "valid.triples"
XFILE *fd;
Xchar key[8], plain[8], cipher[8], processed[8];
Xmain() /* read key/plain/cipher triples until exhausted */
X{ int count, i;
X if ((fd = fopen(VALFILE, "r")) == NULL)
X { fprintf(stderr, "Can't read %s.\n", VALFILE);
X exit(1);
X }
X count = 0;
X desinit(key); /* initialize most of the arrays */
X while (readvals())
X { kinit(key); /* initialize key stuff */
X printf("Key: "); writehex(key);
X printf(" Plain: "); writehex(plain);
X printf(" Cipher: "); writehex(cipher);
X printf("\n");
X endes(plain, processed); /* encipher the plaintext */
X printf("Encry: "); writehex(processed);
X printf("\n");
X for (i = 0; i < 8; i++)
X if (processed[i] != cipher[i])
X printf("Encryption failed.\n");
X dedes(cipher, processed); /* decipher the ciphertext */
X printf("Decry: "); writehex(processed);
X printf("\n");
X for (i = 0; i < 8; i++)
X if (processed[i] != plain[i])
X printf("Decryption failed.\n");
X count++;
X }
X printf("Processed %d tests.\n", count);
X}
Xreadvals() /* get the next legit triple */
X{ int r;
X r = readhex(key);
X readhex(plain);
X readhex(cipher);
X return r;
X}
Xwritehex(str) /* write the 64-bit hex string */
Xchar *str;
X{ int i;
X for (i = 0; i < 8; i++)
X printf("%02x", str[i] & 0377);
X}
Xhex(n) /* convert hex nibble into integer */
Xint n;
X{
X if (n >= 'A' && n <= 'F') return n - 'A' + 10;
X return n - '0';
X}
Xreadhex(str) /* read 64 bits of hex code */
Xchar *str;
X{ int i, c;
X for (i = 0; i < 8; i++)
X { c = hex(getc(fd)) << 4;
X str[i] = c | hex(getc(fd));
X }
X while ((c = getc(fd)) == ' ' || c == '\t' || c == '\n');
X ungetc(c, fd); /* skip to next field */
X return c != EOF;
X}
X#endif
X/************ end scrydes ************/
@//E*O*F des.c//
chmod u=rw,g=rw,o=rw $OUT
echo Inspecting for damage in transit...
temp=/tmp/sharin$$; dtemp=/tmp/sharout$$
trap "rm -f $temp $dtemp; exit" 0 1 2 3 15
cat > $temp <<\!!!
13 127 699 README
7 27 159 Makefile
18 102 547 des.1
716 4037 21942 des.c
754 4293 23347 total
!!!
wc README Makefile des.1 des.c | sed 's=[^ ]*/==' | diff -b $temp - >$dtemp
if test -s $dtemp ; then
echo "Ouch [diff of wc output]:"
cat $dtemp
STATUS=1
elif test $STATUS = 0 ; then
echo "No problems found."
else
echo "WARNING -- PROBLEMS WERE FOUND..."
fi
exit $STATUS
More information about the Mod.sources
mailing list