/*
   Copyright R.M.A.Lucock 1991
*/

void setup_keydata(char*,int);

long KSB[16][2];
static char KS[16][48];

void setup_key(char *key, int edflag)
{
char bits[64] , *ptr = bits , *p;

int i , j , indx;

   for(i = 0; i < 8; i++)
      {
      p = ptr;
      for(j = 7; j >= 0; j--)
         {
         *ptr++ = (key[i] >> j) & 1;
         }

      if( ((p[7] + p[6] + p[5] + p[4] + p[3] + p[2] + p[1]) & 1) == 0)
         p[0] = 1;
      else
         p[0] = 0;
      }

   setup_keydata(bits,edflag);

/* Now put the 16 groups of 48 bits into 16 long words */

   for(i=0; i < 16; ++i)
      {
      KSB[i][0] = KSB[i][1] = 0;
      indx = 0;
      for(j=0; j < 48; ++j)
         {
         if(j == 32)
            indx = 1;
         if(KS[i][j] != 0)
            KSB[i][indx] |= 1 << (j - indx*32);
         }
      }
}


/*
 * Permuted-choice 1 from the key bits
 * to yield C and D.
 * Note that bits 8,16... are left out:
 * They are intended for a parity check.
 */
static  char    PC1_C[] = {
        57,49,41,33,25,17, 9,
         1,58,50,42,34,26,18,
        10, 2,59,51,43,35,27,
        19,11, 3,60,52,44,36,
};

static  char    PC1_D[] = {
        63,55,47,39,31,23,15,
         7,62,54,46,38,30,22,
        14, 6,61,53,45,37,29,
        21,13, 5,28,20,12, 4,
};

/*
 * Sequence of shifts used for the key schedule.
*/
static  char    shifts[] = {
        1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1,
};

/*
 * Permuted-choice 2, to pick out the bits from
 * the CD array that generate the key schedule.
 */
static  char    PC2_C[] = {
        14,17,11,24, 1, 5,
         3,28,15, 6,21,10,
        23,19,12, 4,26, 8,
        16, 7,27,20,13, 2,
};

static  char    PC2_D[] = {
        41,52,31,37,47,55,
        30,40,51,45,33,48,
        44,49,39,56,34,53,
        46,42,50,36,29,32,
};

/*
 * The C and D arrays used to calculate the key schedule.
 */

static char     C[28];
static  char    D[28];

/*
 * Set up the key schedule from the key.
 */

void setup_keydata(key,edflag)
char *key;
int edflag;
{
        register i, j, k , ii;
        int t;

        /*
         * First, generate C and D by permuting
         * the key.  The low order bit of each
         * 8-bit char is not used, so C and D are only 28
         * bits apiece.
         */
        for (i=0; i<28; i++) {
                C[i] = key[PC1_C[i]-1];
                D[i] = key[PC1_D[i]-1];
        }
        /*
         * To generate Ki, rotate C and D according
         * to schedule and pick up a permutation
         * using PC2.
         */
        for (i=0; i<16; i++) {

                if(!edflag)
                  ii = i;
                else
                  ii = 15-i;


                /*
                 * rotate.
                 */
                for (k=0; k<shifts[i]; k++) {
                        t = C[0];
                        for (j=0; j<28-1; j++)
                                C[j] = C[j+1];
                        C[27] = t;
                        t = D[0];
                        for (j=0; j<28-1; j++)
                                D[j] = D[j+1];
                        D[27] = t;
                }
                /*
                 * get Ki. Note C and D are concatenated.
                 */
                for (j=0; j<24; j++) {
                        KS[ii][j] = C[PC2_C[j]-1];
                        KS[ii][j+24] = D[PC2_D[j]-28-1];
                }
        }

}
