home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.sources.unix
- From: ram@eiffel.com (Raphael Manfredi)
- Subject: v25i051: kit - the ultimate mailing kit, Part03/04
- Sender: sources-moderator@pa.dec.com
- Approved: vixie@pa.dec.com
-
- Submitted-By: ram@eiffel.com (Raphael Manfredi)
- Posting-Number: Volume 25, Issue 51
- Archive-Name: kit/part03
-
- #! /bin/sh
- # This is a shell archive. Remove anything before this line, then unpack
- # it by saving it into a file and typing "sh file". To overwrite existing
- # files, type "sh file -c". You can also feed this as standard input via
- # unshar, or by typing "sh <file", e.g.. If this archive is complete, you
- # will see the following message at the end:
- # "End of archive 3 (of 4)."
- # Contents: des/des.c
- # Wrapped by vixie@cognition.pa.dec.com on Thu Dec 19 21:18:30 1991
- PATH=/bin:/usr/bin:/usr/ucb ; export PATH
- if test -f 'des/des.c' -a "${1}" != "-c" ; then
- echo shar: Will not clobber existing file \"'des/des.c'\"
- else
- echo shar: Extracting \"'des/des.c'\" \(13388 characters\)
- sed "s/^X//" >'des/des.c' <<'END_OF_FILE'
- X/* Sofware DES functions
- X * written 12 Dec 1986 by Phil Karn, KA9Q; large sections adapted from
- X * the 1977 public-domain program by Jim Gillogly
- X */
- X
- X/*
- X * $Id: des.c,v 2.0.1.1 91/04/01 15:39:35 ram Exp $
- X *
- X * $Log: des.c,v $
- X * Revision 2.0.1.1 91/04/01 15:39:35 ram
- X * patch1: created
- X *
- X */
- X
- X#define NULL 0
- X
- X#include "../config.h"
- X
- X#if BYTEORDER == 0x1234
- X#define LITTLE_ENDIAN
- X#endif
- X
- X#ifdef LITTLE_ENDIAN
- unsigned long byteswap();
- X#endif
- X
- X/* Tables defined in the Data Encryption Standard documents */
- X
- X/* initial permutation IP */
- static char ip[] = {
- 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
- X};
- X
- X/* final permutation IP^-1 */
- static char fp[] = {
- 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};
- X
- X/* expansion operation matrix
- X * This is for reference only; it is unused in the code
- X * as the f() function performs it implicitly for speed
- X */
- X#ifdef notdef
- static char ei[] = {
- X 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
- X};
- X#endif
- X
- X/* permuted choice table (key) */
- static char pc1[] = {
- 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
- 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
- X};
- X
- X/* number left rotations of pc1 */
- static char totrot[] = {
- X 1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28
- X};
- X
- X/* permuted choice key (table) */
- static char pc2[] = {
- 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
- X};
- X
- X/* The (in)famous S-boxes */
- static char si[8][64] = {
- X /* S1 */
- 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
- X /* S2 */
- 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
- X /* S3 */
- 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
- X /* S4 */
- 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
- X /* S5 */
- 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
- X /* S6 */
- 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
- X /* S7 */
- 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
- X /* S8 */
- 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
- X};
- X
- X/* 32-bit permutation function P used on the output of the S-boxes */
- static char p32i[] = {
- 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
- X};
- X/* End of DES-defined tables */
- X
- X/* Lookup tables initialized once only at startup by desinit() */
- static long (*sp)[64]; /* Combined S and P boxes */
- X
- static char (*iperm)[16][8]; /* Initial and final permutations */
- static char (*fperm)[16][8];
- X
- X/* 8 6-bit subkeys for each of 16 rounds, initialized by setkey() */
- static unsigned char (*kn)[8];
- X
- X/* bit 0 is left-most in byte */
- static int bytebit[] = {
- X 0200,0100,040,020,010,04,02,01
- X};
- X
- static int nibblebit[] = {
- X 010,04,02,01
- X};
- static int desmode;
- X
- X/* Allocate space and initialize DES lookup arrays
- X * mode == 0: standard Data Encryption Algorithm
- X * mode == 1: DEA without initial and final permutations for speed
- X * mode == 2: DEA without permutations and with 128-byte key (completely
- X * independent subkeys for each round)
- X */
- desinit(mode)
- int mode;
- X{
- X char *malloc();
- X
- X if(sp != NULL){
- X /* Already initialized */
- X return 0;
- X }
- X desmode = mode;
- X
- X if((sp = (long (*)[64])malloc(sizeof(long) * 8 * 64)) == NULL){
- X return -1;
- X }
- X spinit();
- X kn = (unsigned char (*)[8])malloc(sizeof(char) * 8 * 16);
- X if(kn == NULL){
- X free((char *)sp);
- X return -1;
- X }
- X if(mode == 1 || mode == 2) /* No permutations */
- X return 0;
- X
- X iperm = (char (*)[16][8])malloc(sizeof(char) * 16 * 16 * 8);
- X if(iperm == NULL){
- X free((char *)sp);
- X free((char *)kn);
- X return -1;
- X }
- X perminit(iperm,ip);
- X
- X fperm = (char (*)[16][8])malloc(sizeof(char) * 16 * 16 * 8);
- X if(fperm == NULL){
- X free((char *)sp);
- X free((char *)kn);
- X free((char *)iperm);
- X return -1;
- X }
- X perminit(fperm,fp);
- X
- X return 0;
- X}
- X/* Free up storage used by DES */
- desdone()
- X{
- X if(sp == NULL)
- X return; /* Already done */
- X
- X free((char *)sp);
- X free((char *)kn);
- X if(iperm != NULL)
- X free((char *)iperm);
- X if(fperm != NULL)
- X free((char *)fperm);
- X
- X sp = NULL;
- X iperm = NULL;
- X fperm = NULL;
- X kn = NULL;
- X}
- X/* Set key (initialize key schedule array) */
- setkey(key)
- char *key; /* 64 bits (will use only 56) */
- X{
- X char pc1m[56]; /* place to modify pc1 into */
- X char pcr[56]; /* place to rotate pc1 into */
- X register int i,j,l;
- X int m;
- X
- X /* In mode 2, the 128 bytes of subkey are set directly from the
- X * user's key, allowing him to use completely independent
- X * subkeys for each round. Note that the user MUST specify a
- X * full 128 bytes.
- X *
- X * I would like to think that this technique gives the NSA a real
- X * headache, but I'm not THAT naive.
- X */
- X if(desmode == 2){
- X for(i=0;i<16;i++)
- X for(j=0;j<8;j++)
- X kn[i][j] = *key++;
- X return;
- X }
- X /* Clear key schedule */
- X for (i=0; i<16; i++)
- X for (j=0; j<8; j++)
- X kn[i][j]=0;
- X
- 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++) { /* 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 /* check bit that goes to kn[j] */
- X if (pcr[pc2[j]-1]){
- X /* mask it in if it's there */
- X l= j % 6;
- X kn[i][j/6] |= bytebit[l] >> 2;
- X }
- X }
- X }
- X}
- X/* In-place encryption of 64-bit block */
- endes(block)
- char *block;
- X{
- X register int i;
- X unsigned long work[2]; /* Working data storage */
- X long tmp;
- X
- X permute(block,iperm,(char *)work); /* Initial Permutation */
- X#ifdef LITTLE_ENDIAN
- X work[0] = byteswap(work[0]);
- X work[1] = byteswap(work[1]);
- X#endif
- X
- X /* Do the 16 rounds */
- X for (i=0; i<16; i++)
- X round(i,work);
- X
- X /* Left/right half swap */
- X tmp = work[0];
- X work[0] = work[1];
- X work[1] = tmp;
- X
- X#ifdef LITTLE_ENDIAN
- X work[0] = byteswap(work[0]);
- X work[1] = byteswap(work[1]);
- X#endif
- X permute((char *)work,fperm,block); /* Inverse initial permutation */
- X}
- X/* In-place decryption of 64-bit block */
- dedes(block)
- char *block;
- X{
- X register int i;
- X unsigned long work[2]; /* Working data storage */
- X long tmp;
- X
- X permute(block,iperm,(char *)work); /* Initial permutation */
- X
- X#ifdef LITTLE_ENDIAN
- X work[0] = byteswap(work[0]);
- X work[1] = byteswap(work[1]);
- X#endif
- X
- X /* Left/right half swap */
- X tmp = work[0];
- X work[0] = work[1];
- X work[1] = tmp;
- X
- X /* Do the 16 rounds in reverse order */
- X for (i=15; i >= 0; i--)
- X round(i,work);
- X
- X#ifdef LITTLE_ENDIAN
- X work[0] = byteswap(work[0]);
- X work[1] = byteswap(work[1]);
- X#endif
- X
- X permute((char *)work,fperm,block); /* Inverse initial permutation */
- X}
- X
- X/* Permute inblock with perm */
- static
- permute(inblock,perm,outblock)
- char *inblock, *outblock; /* result into outblock,64 bits */
- char perm[16][16][8]; /* 2K bytes defining perm. */
- X{
- X register int i,j;
- X register char *ib, *ob; /* ptr to input or output block */
- X register char *p, *q;
- X
- X if(perm == NULL){
- X /* No permutation, just copy */
- X for(i=8; i!=0; i--)
- X *outblock++ = *inblock++;
- X return;
- X }
- X /* Clear output block */
- X for (i=8, ob = outblock; i != 0; i--)
- X *ob++ = 0;
- X
- 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 = 8; i != 0; i--){ /* and each output byte */
- X *ob++ |= *p++ | *q++; /* OR the masks together*/
- X }
- X }
- X}
- X
- X/* Do one DES cipher round */
- static
- round(num,block)
- int num; /* i.e. the num-th one */
- unsigned long *block;
- X{
- X long f();
- X
- X /* The rounds are numbered from 0 to 15. On even rounds
- X * the right half is fed to f() and the result exclusive-ORs
- X * the left half; on odd rounds the reverse is done.
- X */
- X if(num & 1){
- X block[1] ^= f(block[0],kn[num]);
- X } else {
- X block[0] ^= f(block[1],kn[num]);
- X }
- X}
- X/* The nonlinear function f(r,k), the heart of DES */
- static
- long
- f(r,subkey)
- unsigned long r; /* 32 bits */
- unsigned char subkey[8]; /* 48-bit key for this round */
- X{
- X register unsigned long rval,rt;
- X#ifdef TRACE
- X unsigned char *cp;
- X int i;
- X
- X printf("f(%08lx, %02x %02x %02x %02x %02x %02x %02x %02x) = ",
- X r,
- X subkey[0], subkey[1], subkey[2],
- X subkey[3], subkey[4], subkey[5],
- X subkey[6], subkey[7]);
- X#endif
- X /* Run E(R) ^ K through the combined S & P boxes
- X * This code takes advantage of a convenient regularity in
- X * E, namely that each group of 6 bits in E(R) feeding
- X * a single S-box is a contiguous segment of R.
- X */
- X rt = (r >> 1) | ((r & 1) ? 0x80000000 : 0);
- X rval = 0;
- X rval |= sp[0][((rt >> 26) ^ *subkey++) & 0x3f];
- X rval |= sp[1][((rt >> 22) ^ *subkey++) & 0x3f];
- X rval |= sp[2][((rt >> 18) ^ *subkey++) & 0x3f];
- X rval |= sp[3][((rt >> 14) ^ *subkey++) & 0x3f];
- X rval |= sp[4][((rt >> 10) ^ *subkey++) & 0x3f];
- X rval |= sp[5][((rt >> 6) ^ *subkey++) & 0x3f];
- X rval |= sp[6][((rt >> 2) ^ *subkey++) & 0x3f];
- X rt = (r << 1) | ((r & 0x80000000) ? 1 : 0);
- X rval |= sp[7][(rt ^ *subkey) & 0x3f];
- X#ifdef TRACE
- X printf(" %08lx\n",rval);
- X#endif
- X return rval;
- X}
- X/* initialize a perm array */
- static
- perminit(perm,p)
- char perm[16][16][8]; /* 64-bit, either init or final */
- char p[64];
- X{
- X register int l, j, k;
- X int i,m;
- X
- X /* Clear the permutation array */
- X for (i=0; i<16; i++)
- X for (j=0; j<16; j++)
- X for (k=0; k<8; k++)
- X perm[i][j][k]=0;
- X
- 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}
- X
- X/* Initialize the lookup table for the combined S and P boxes */
- static int
- spinit()
- X{
- X char pbox[32];
- X int p,i,s,j,rowcol;
- X long val;
- X
- X /* Compute pbox, the inverse of p32i.
- X * This is easier to work with
- X */
- X for(p=0;p<32;p++){
- X for(i=0;i<32;i++){
- X if(p32i[i]-1 == p){
- X pbox[p] = i;
- X break;
- X }
- X }
- X }
- X for(s = 0; s < 8; s++){ /* For each S-box */
- X for(i=0; i<64; i++){ /* For each possible input */
- X val = 0;
- X /* The row number is formed from the first and last
- X * bits; the column number is from the middle 4
- X */
- X rowcol = (i & 32) | ((i & 1) ? 16 : 0) | ((i >> 1) & 0xf);
- X for(j=0;j<4;j++){ /* For each output bit */
- X if(si[s][rowcol] & (8 >> j)){
- X val |= 1L << (31 - pbox[4*s + j]);
- X }
- X }
- X sp[s][i] = val;
- X
- X#ifdef DEBUG
- X printf("sp[%d][%2d] = %08lx\n",s,i,sp[s][i]);
- X#endif
- X }
- X }
- X}
- X#ifdef LITTLE_ENDIAN
- X/* Byte swap a long */
- static
- unsigned long
- byteswap(x)
- unsigned long x;
- X{
- X register char *cp,tmp;
- X
- X cp = (char *)&x;
- X tmp = cp[3];
- X cp[3] = cp[0];
- X cp[0] = tmp;
- X
- X tmp = cp[2];
- X cp[2] = cp[1];
- X cp[1] = tmp;
- X
- X return x;
- X}
- X#endif
- END_OF_FILE
- if test 13388 -ne `wc -c <'des/des.c'`; then
- echo shar: \"'des/des.c'\" unpacked with wrong size!
- fi
- # end of 'des/des.c'
- fi
- echo shar: End of archive 3 \(of 4\).
- cp /dev/null ark3isdone
- MISSING=""
- for I in 1 2 3 4 ; do
- if test ! -f ark${I}isdone ; then
- MISSING="${MISSING} ${I}"
- fi
- done
- if test "${MISSING}" = "" ; then
- echo You have unpacked all 4 archives.
- rm -f ark[1-9]isdone
- else
- echo You still need to unpack the following archives:
- echo " " ${MISSING}
- fi
- ## End of shell archive.
- exit 0
-