home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / des.zip / DES.C next >
Text File  |  1991-04-09  |  7KB  |  289 lines

  1. /*************************** des.c ******************************************/
  2. /* Functions and tables for DES encryption and decryption                   */
  3.  
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "des.h"
  7.  
  8. /* 48 bit key permutation */
  9.  
  10. struct ks
  11. {
  12.    char ki[6];
  13. };
  14.  
  15. struct LR
  16. {
  17.    long L;
  18.    long R;
  19. };
  20.  
  21. static struct ks keys[16];
  22. static unsigned char oddparity(unsigned char s);
  23. static void rotate(unsigned char *, int );
  24. static int fourbits(struct ks, int );
  25. static int sixbits(struct ks, int );
  26. static void inverse_permute(long *, long *, long *, int );
  27. static void permute(long *op, long *ip, long *tbl, int n);
  28. static long f(long , struct ks );
  29. static struct ks KS(int , char *);
  30. static void swapbyte(long *);
  31.  
  32. /*****************************************************************************/
  33. /* make a character odd parity */
  34. unsigned char oddparity(unsigned char s)
  35. {
  36.    unsigned char c = s | 0x80;
  37.    while(s)
  38.    {
  39.       if(s & 1)
  40.          c ^= 0x80;
  41.       s = (s >> 1) & 0x7f;
  42.    }
  43.    return c;
  44. }
  45. /*****************************************************************************/
  46. /* make a key odd parity */
  47. /* makes sure that the upper bit of each of the key's bytes is odd parity */
  48. void setparity(char *key)
  49. {
  50.    int i;
  51.    for(i = 0; i < 8; i++)
  52.       *(key + i) = oddparity(*(key + i ));
  53. }
  54.  
  55. /*****************************************************************************/
  56. /* Initialize the key */
  57. void initkey(char *key)
  58. {
  59.    int i;
  60.    for(i=0;i<16;i++)
  61.       keys[i] = KS(i,key);
  62. }
  63. /*****************************************************************************/
  64. /* encrypt an 8 byte block */
  65. void encrypt(char *blk)
  66. {
  67.    struct LR ip,op;
  68.    long temp;
  69.    int n;
  70.  
  71.    memcpy(&ip, blk, sizeof(struct LR));
  72.    /* initial permutation */
  73.    permute(&op.L,&ip.L,(long *)IPtbl,64);
  74.    swapbyte(&op.L);
  75.    swapbyte(&op.R);
  76.    /* swap and key iterations */
  77.    for (n=0;n<16;n++)
  78.       {
  79.          temp = op.R;
  80.          op.R = op.L ^ f(op.R,keys[n]);
  81.          op.L = temp;
  82.       }
  83.    ip.R = op.L;
  84.    ip.L = op.R;
  85.    swapbyte(&ip.L);
  86.    swapbyte(&ip.R);
  87.    /* inverse initial permutation */
  88.    inverse_permute(&op.L,&ip.L,(long *)IPtbl,64);
  89.    memcpy(blk,&op,sizeof(struct LR));
  90. }
  91. /*****************************************************************************/
  92. /* decrypt an 8 byte block */
  93. void decrypt(char *blk)
  94. {
  95.    struct LR ip,op;
  96.    long temp;
  97.    int n;
  98.  
  99.    memcpy(&ip, blk, sizeof(struct LR));
  100.    /* initial permutation */
  101.    permute(&op.L,&ip.L,(long *)IPtbl,64);
  102.    swapbyte(&op.L);
  103.    swapbyte(&op.R);
  104.    ip.R = op.L;
  105.    ip.L = op.R;
  106.    /* swap and key iterations */
  107.    for (n=15;n>=0;--n)
  108.       {
  109.          temp = ip.L;
  110.          ip.L = ip.R ^ f(ip.L,keys[n]);
  111.          ip.R = temp;
  112.       }
  113.    swapbyte(&ip.L);
  114.    swapbyte(&ip.R);
  115.    /* inverse initial permutation */
  116.    inverse_permute(&op.L,&ip.L,(long *)IPtbl,64);
  117.    memcpy(blk,&op,sizeof(struct LR));
  118. }
  119. /*****************************************************************************/
  120. /* inverse permute a 64 bit string */
  121. static void inverse_permute(long *op, long *ip, long *tbl, int n)
  122. {
  123.    int i;
  124.    long *pt = (long *)Pmask;
  125.  
  126.    *op = *(op+1) = 0;
  127.    for (i=0;i<n;i++)
  128.       {
  129.          if ((*ip & *pt ) || (*(ip+1) & *(pt+1))) {
  130.             *op |= *tbl;
  131.             *(op+1) |= *(tbl+1);
  132.          }
  133.          tbl +=2;
  134.          pt += 2;
  135.    }
  136. }
  137. /*****************************************************************************/
  138. /* permute a 64 bit string */
  139. static void permute(long *op, long *ip, long *tbl, int n)
  140. {
  141.    int i;
  142.    long *pt = (long *)Pmask;
  143.  
  144.    *op = *(op+1) = 0;
  145.    for (i=0;i<n;i++)
  146.       {
  147.          if ((*ip & *tbl ) || (*(ip+1) & *(tbl+1))) {
  148.             *op |= *pt;
  149.             *(op+1) |= *(pt+1);
  150.          }
  151.          tbl +=2;
  152.          pt += 2;
  153.    }
  154. }
  155. /*****************************************************************************/
  156. /* key dependent computation function f(R,K) */
  157. static long f(long blk, struct ks key)
  158. {
  159.    struct LR ir;
  160.    struct LR or;
  161.    int i;
  162.    union
  163.    {
  164.       struct LR f;
  165.       struct ks kn;
  166.    }tr = {0,0},kr = {0,0};
  167.  
  168.    ir.L = blk;
  169.    ir.R = 0;
  170.  
  171.    kr.kn = key;
  172.  
  173.    swapbyte(&ir.L);
  174.    swapbyte(&ir.R);
  175.  
  176.    permute(&tr.f.L, &ir.L, (long *)Etbl, 48);
  177.  
  178.    tr.f.L ^= kr.f.L;
  179.    tr.f.R ^= kr.f.R;
  180.  
  181.    /* for DES S function : ir.L = S(tr.kn); */
  182.    ir.L =0;
  183.    for(i=0;i<8;i++)
  184.    {
  185.       long four = fourbits(tr.kn,i);
  186.       ir.L |= four << ((7-i) * 4);
  187.    }
  188.    swapbyte(&ir.L);
  189.  
  190.    ir.R = or.R = 0;
  191.    permute(&or.L,&ir.L,(long *)Ptbl, 32);
  192.  
  193.    swapbyte(&or.L);
  194.    swapbyte(&or.R);
  195.  
  196.    return or.L;
  197. }
  198. /*****************************************************************************/
  199. /* extract a 4 bit stream from the block/key */
  200. static int fourbits(struct ks k, int s)
  201. {
  202.    int i = sixbits(k,s);
  203.    int row,col;
  204.    row = (( i>> 4)&2)|(i&1);
  205.    col = (i>> 1) & 0xf;
  206.    return stbl[s][row][col];
  207. }
  208. /*****************************************************************************/
  209. /* extract 6 bit stream fr pos s of the block/key */
  210. static int sixbits(struct ks k, int s)
  211. {
  212.    int op = 0;
  213.    int n = (s);
  214.    int i;
  215.    for(i=0;i<2;i++)
  216.    {
  217.       int off = ex6[n][i][0];
  218.       unsigned char c = k.ki[off];
  219.       c >>= ex6[n][i][1];
  220.       c <<= ex6[n][i][2];
  221.       c &= ex6[n][i][3];
  222.       op |= c;
  223.    }
  224.    return op;
  225. }
  226.  
  227. /*****************************************************************************/
  228. /* DES key schedule (KS) function */
  229. static struct ks KS(int n, char *key)
  230. {
  231.    static unsigned char cd[8];
  232.    static int its[] = {1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};
  233.    union
  234.    {
  235.       struct ks kn;
  236.       struct LR filler;
  237.    } result;
  238.  
  239.    if(n==0)
  240.       permute((long *)cd, (long *)key, (long *)PC1tbl, 64);
  241.  
  242.    rotate(cd,its[n]);
  243.    rotate(cd + 4,its[n]);
  244.  
  245.    permute(&result.filler.L, (long *)cd, (long *)PC2tbl, 48);
  246.    return result.kn;
  247. }
  248. /*****************************************************************************/
  249. /* rotate a 4 byte string n (1 or 2) positions to the left */
  250. static void rotate(unsigned char *c, int n)
  251. {
  252.    int i;
  253.    unsigned j,k;
  254.    k = ((*c) & 255) >> (8-n);
  255.    for (i=3;i >= 0;--i) {
  256.       j = ((*(c+i) << n) + k);
  257.       k = (j >>8) & 255;
  258.       *(c+i) = j & 255;
  259.    }
  260.    if(n==2)
  261.       *(c+3) = (*(c+3) &0xc0) | ((*(c+3) << 4 ) & 0x30);
  262.    else
  263.       *(c+3) = (*(c+3) &0xe0) | ((*(c+3) << 4 ) & 0x10);
  264. }
  265. /*****************************************************************************/
  266. /* swap bytes in long integer */
  267. static void swapbyte(long *l)
  268. {
  269.    char *cp =(char *) l;
  270.    char t = *(cp + 3);
  271.  
  272.    *(cp +3) = *cp;
  273.    *cp = t;
  274.    t = *(cp + 2);
  275.    *(cp + 2) = *(cp + 1);
  276.    *(cp + 1) = t;
  277. }
  278. /*****************************************************************************/
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.