home *** CD-ROM | disk | FTP | other *** search
/ For Beginners & Professional Hackers / cd.iso / software / internet / mirc452t.exe / des / src / des.c < prev   
Encoding:
C/C++ Source or Header  |  1990-01-10  |  8.9 KB  |  341 lines

  1. /* Sofware DES functions
  2.  * written 12 Dec 1986 by Phil Karn, KA9Q; large sections adapted from
  3.  * the 1977 public-domain program by Jim Gillogly
  4.  */
  5. #define    NULL    0
  6.  
  7. #ifdef    LITTLE_ENDIAN
  8. unsigned long byteswap();
  9. #endif
  10.  
  11. /* Tables defined in the Data Encryption Standard documents */
  12.  
  13. /* initial permutation IP */
  14. static char ip[] = {
  15.     58, 50, 42, 34, 26, 18, 10,  2,
  16.     60, 52, 44, 36, 28, 20, 12,  4,
  17.     62, 54, 46, 38, 30, 22, 14,  6,
  18.     64, 56, 48, 40, 32, 24, 16,  8,
  19.     57, 49, 41, 33, 25, 17,  9,  1,
  20.     59, 51, 43, 35, 27, 19, 11,  3,
  21.     61, 53, 45, 37, 29, 21, 13,  5,
  22.     63, 55, 47, 39, 31, 23, 15,  7
  23. };
  24.  
  25. /* final permutation IP^-1 */
  26. static char fp[] = {
  27.     40,  8, 48, 16, 56, 24, 64, 32,
  28.     39,  7, 47, 15, 55, 23, 63, 31,
  29.     38,  6, 46, 14, 54, 22, 62, 30,
  30.     37,  5, 45, 13, 53, 21, 61, 29,
  31.     36,  4, 44, 12, 52, 20, 60, 28,
  32.     35,  3, 43, 11, 51, 19, 59, 27,
  33.     34,  2, 42, 10, 50, 18, 58, 26,
  34.     33,  1, 41,  9, 49, 17, 57, 25
  35. };
  36.  
  37. /* expansion operation matrix
  38.  * This is for reference only; it is unused in the code
  39.  * as the f() function performs it implicitly for speed
  40.  */
  41. #ifdef notdef
  42. static char ei[] = {
  43.     32,  1,  2,  3,  4,  5,
  44.      4,  5,  6,  7,  8,  9,
  45.      8,  9, 10, 11, 12, 13,
  46.     12, 13, 14, 15, 16, 17,
  47.     16, 17, 18, 19, 20, 21,
  48.     20, 21, 22, 23, 24, 25,
  49.     24, 25, 26, 27, 28, 29,
  50.     28, 29, 30, 31, 32,  1 
  51. };
  52. #endif
  53.  
  54. /* permuted choice table (key) */
  55. static char pc1[] = {
  56.     57, 49, 41, 33, 25, 17,  9,
  57.      1, 58, 50, 42, 34, 26, 18,
  58.     10,  2, 59, 51, 43, 35, 27,
  59.     19, 11,  3, 60, 52, 44, 36,
  60.  
  61.     63, 55, 47, 39, 31, 23, 15,
  62.      7, 62, 54, 46, 38, 30, 22,
  63.     14,  6, 61, 53, 45, 37, 29,
  64.     21, 13,  5, 28, 20, 12,  4
  65. };
  66.  
  67. /* number left rotations of pc1 */
  68. static char totrot[] = {
  69.     1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28
  70. };
  71.  
  72. /* permuted choice key (table) */
  73. static char pc2[] = {
  74.     14, 17, 11, 24,  1,  5,
  75.      3, 28, 15,  6, 21, 10,
  76.     23, 19, 12,  4, 26,  8,
  77.     16,  7, 27, 20, 13,  2,
  78.     41, 52, 31, 37, 47, 55,
  79.     30, 40, 51, 45, 33, 48,
  80.     44, 49, 39, 56, 34, 53,
  81.     46, 42, 50, 36, 29, 32
  82. };
  83.  
  84. /* The (in)famous S-boxes */
  85. static char si[8][64] = {
  86.     /* S1 */
  87.     14,  4, 13,  1,  2, 15, 11,  8,  3, 10,  6, 12,  5,  9,  0,  7,
  88.      0, 15,  7,  4, 14,  2, 13,  1, 10,  6, 12, 11,  9,  5,  3,  8,
  89.      4,  1, 14,  8, 13,  6,  2, 11, 15, 12,  9,  7,  3, 10,  5,  0,
  90.     15, 12,  8,  2,  4,  9,  1,  7,  5, 11,  3, 14, 10,  0,  6, 13,
  91.  
  92.     /* S2 */
  93.     15,  1,  8, 14,  6, 11,  3,  4,  9,  7,  2, 13, 12,  0,  5, 10,
  94.      3, 13,  4,  7, 15,  2,  8, 14, 12,  0,  1, 10,  6,  9, 11,  5,
  95.      0, 14,  7, 11, 10,  4, 13,  1,  5,  8, 12,  6,  9,  3,  2, 15,
  96.     13,  8, 10,  1,  3, 15,  4,  2, 11,  6,  7, 12,  0,  5, 14,  9,
  97.  
  98.     /* S3 */
  99.     10,  0,  9, 14,  6,  3, 15,  5,  1, 13, 12,  7, 11,  4,  2,  8,
  100.     13,  7,  0,  9,  3,  4,  6, 10,  2,  8,  5, 14, 12, 11, 15,  1,
  101.     13,  6,  4,  9,  8, 15,  3,  0, 11,  1,  2, 12,  5, 10, 14,  7,
  102.      1, 10, 13,  0,  6,  9,  8,  7,  4, 15, 14,  3, 11,  5,  2, 12,
  103.  
  104.     /* S4 */
  105.      7, 13, 14,  3,  0,  6,  9, 10,  1,  2,  8,  5, 11, 12,  4, 15,
  106.     13,  8, 11,  5,  6, 15,  0,  3,  4,  7,  2, 12,  1, 10, 14,  9,
  107.     10,  6,  9,  0, 12, 11,  7, 13, 15,  1,  3, 14,  5,  2,  8,  4,
  108.      3, 15,  0,  6, 10,  1, 13,  8,  9,  4,  5, 11, 12,  7,  2, 14,
  109.  
  110.     /* S5 */
  111.      2, 12,  4,  1,  7, 10, 11,  6,  8,  5,  3, 15, 13,  0, 14,  9,
  112.     14, 11,  2, 12,  4,  7, 13,  1,  5,  0, 15, 10,  3,  9,  8,  6,
  113.      4,  2,  1, 11, 10, 13,  7,  8, 15,  9, 12,  5,  6,  3,  0, 14,
  114.     11,  8, 12,  7,  1, 14,  2, 13,  6, 15,  0,  9, 10,  4,  5,  3,
  115.  
  116.     /* S6 */
  117.     12,  1, 10, 15,  9,  2,  6,  8,  0, 13,  3,  4, 14,  7,  5, 11,
  118.     10, 15,  4,  2,  7, 12,  9,  5,  6,  1, 13, 14,  0, 11,  3,  8,
  119.      9, 14, 15,  5,  2,  8, 12,  3,  7,  0,  4, 10,  1, 13, 11,  6,
  120.      4,  3,  2, 12,  9,  5, 15, 10, 11, 14,  1,  7,  6,  0,  8, 13,
  121.  
  122.     /* S7 */
  123.      4, 11,  2, 14, 15,  0,  8, 13,  3, 12,  9,  7,  5, 10,  6,  1,
  124.     13,  0, 11,  7,  4,  9,  1, 10, 14,  3,  5, 12,  2, 15,  8,  6,
  125.      1,  4, 11, 13, 12,  3,  7, 14, 10, 15,  6,  8,  0,  5,  9,  2,
  126.      6, 11, 13,  8,  1,  4, 10,  7,  9,  5,  0, 15, 14,  2,  3, 12,
  127.  
  128.     /* S8 */
  129.     13,  2,  8,  4,  6, 15, 11,  1, 10,  9,  3, 14,  5,  0, 12,  7,
  130.      1, 15, 13,  8, 10,  3,  7,  4, 12,  5,  6, 11,  0, 14,  9,  2,
  131.      7, 11,  4,  1,  9, 12, 14,  2,  0,  6, 10, 13, 15,  3,  5,  8,
  132.      2,  1, 14,  7,  4, 10,  8, 13, 15, 12,  9,  0,  3,  5,  6, 11
  133. };
  134.  
  135. /* 32-bit permutation function P used on the output of the S-boxes */
  136. static char p32i[] = {    
  137.     16,  7, 20, 21,
  138.     29, 12, 28, 17,
  139.      1, 15, 23, 26,
  140.      5, 18, 31, 10,
  141.      2,  8, 24, 14,
  142.     32, 27,  3,  9,
  143.     19, 13, 30,  6,
  144.     22, 11,  4, 25
  145. };
  146. /* End of DES-defined tables */
  147.  
  148. /* Lookup tables initialized once only at startup by desinit() */
  149. static long (*sp)[64];        /* Combined S and P boxes */
  150.  
  151. static char (*iperm)[16][8];    /* Initial and final permutations */
  152. static char (*fperm)[16][8];
  153.  
  154. /* 8 6-bit subkeys for each of 16 rounds, initialized by setkey() */
  155. static unsigned char (*kn)[8];
  156.  
  157. /* bit 0 is left-most in byte */
  158. static int bytebit[] = {
  159.     0200,0100,040,020,010,04,02,01
  160. };
  161.  
  162. static int nibblebit[] = {
  163.      010,04,02,01
  164. };
  165. static int desmode;
  166.  
  167. /* Allocate space and initialize DES lookup arrays
  168.  * mode == 0: standard Data Encryption Algorithm
  169.  * mode == 1: DEA without initial and final permutations for speed
  170.  * mode == 2: DEA without permutations and with 128-byte key (completely
  171.  *            independent subkeys for each round)
  172.  */
  173. desinit(mode)
  174. int mode;
  175. {
  176.     char *malloc();
  177.  
  178.     if(sp != NULL){
  179.         /* Already initialized */
  180.         return 0;
  181.     }
  182.     desmode = mode;
  183.     
  184.     if((sp = (long (*)[64])malloc(sizeof(long) * 8 * 64)) == NULL){
  185.         return -1;
  186.     }
  187.     spinit();
  188.     kn = (unsigned char (*)[8])malloc(sizeof(char) * 8 * 16);
  189.     if(kn == NULL){
  190.         free((char *)sp);
  191.         return -1;
  192.     }
  193.     if(mode == 1 || mode == 2)    /* No permutations */
  194.         return 0;
  195.  
  196.     iperm = (char (*)[16][8])malloc(sizeof(char) * 16 * 16 * 8);
  197.     if(iperm == NULL){
  198.         free((char *)sp);
  199.         free((char *)kn);
  200.         return -1;
  201.     }
  202.     perminit(iperm,ip);
  203.  
  204.     fperm = (char (*)[16][8])malloc(sizeof(char) * 16 * 16 * 8);
  205.     if(fperm == NULL){
  206.         free((char *)sp);
  207.         free((char *)kn);
  208.         free((char *)iperm);
  209.         return -1;
  210.     }
  211.     perminit(fperm,fp);
  212.     
  213.     return 0;
  214. }
  215. /* Free up storage used by DES */
  216. desdone()
  217. {
  218.     if(sp == NULL)
  219.         return;    /* Already done */
  220.  
  221.     free((char *)sp);
  222.     free((char *)kn);
  223.     if(iperm != NULL)
  224.         free((char *)iperm);
  225.     if(fperm != NULL)
  226.         free((char *)fperm);
  227.  
  228.     sp = NULL;
  229.     iperm = NULL;
  230.     fperm = NULL;
  231.     kn = NULL;
  232. }
  233. /* Set key (initialize key schedule array) */
  234. setkey(key)
  235. char *key;            /* 64 bits (will use only 56) */
  236. {
  237.     char pc1m[56];        /* place to modify pc1 into */
  238.     char pcr[56];        /* place to rotate pc1 into */
  239.     register int i,j,l;
  240.     int m;
  241.  
  242.     /* In mode 2, the 128 bytes of subkey are set directly from the
  243.      * user's key, allowing him to use completely independent
  244.      * subkeys for each round. Note that the user MUST specify a
  245.      * full 128 bytes.
  246.      *
  247.      * I would like to think that this technique gives the NSA a real
  248.      * headache, but I'm not THAT naive.
  249.      */
  250.     if(desmode == 2){
  251.         for(i=0;i<16;i++)
  252.             for(j=0;j<8;j++)
  253.                 kn[i][j] = *key++;
  254.         return;
  255.     }
  256.     /* Clear key schedule */
  257.     for (i=0; i<16; i++)
  258.         for (j=0; j<8; j++)
  259.             kn[i][j]=0;
  260.  
  261.     for (j=0; j<56; j++) {        /* convert pc1 to bits of key */
  262.         l=pc1[j]-1;        /* integer bit location     */
  263.         m = l & 07;        /* find bit         */
  264.         pc1m[j]=(key[l>>3] &    /* find which key byte l is in */
  265.             bytebit[m])    /* and which bit of that byte */
  266.             ? 1 : 0;    /* and store 1-bit result */
  267.     }
  268.     for (i=0; i<16; i++) {        /* key chunk for each iteration */
  269.         for (j=0; j<56; j++)    /* rotate pc1 the right amount */
  270.             pcr[j] = pc1m[(l=j+totrot[i])<(j<28? 28 : 56) ? l: l-28];
  271.             /* rotate left and right halves independently */
  272.         for (j=0; j<48; j++){    /* select bits individually */
  273.             /* check bit that goes to kn[j] */
  274.             if (pcr[pc2[j]-1]){
  275.                 /* mask it in if it's there */
  276.                 l= j % 6;
  277.                 kn[i][j/6] |= bytebit[l] >> 2;
  278.             }
  279.         }
  280.     }
  281. }
  282. /* In-place encryption of 64-bit block */
  283. endes(block)
  284. char *block;
  285. {
  286.     register int i;
  287.     unsigned long work[2];         /* Working data storage */
  288.     long tmp;
  289.  
  290.     permute(block,iperm,(char *)work);    /* Initial Permutation */
  291. #ifdef LITTLE_ENDIAN
  292.     work[0] = byteswap(work[0]);
  293.     work[1] = byteswap(work[1]);
  294. #endif
  295.  
  296.  Pers s1b,unqj
  297.     workrf(iperm  Endaor (j=t block */
  298. 6e)8 byiTrkrf(i(iperm  Endaor (j=T(        for  iterr *bloc,L0o 1: DEA withou,!ned lo^rage */
  299. eof(cj6e)8 /%1r 8s^
  300.     
  301.      (j=T(data storteswap
  302.     lona9 dependent5it[m])    /* /* air  9,
  303. f        for (dat3&* No permiIAN
  304. 3f2        }
  305.         }
  306.     }
  307. }
  308. /* In2];         /* Workin48re set 9lr        /* Wo'
  309.     }
  310. /* Wo,    for  iterr/|56) ? l: l j,)d6-28];
  311. t  ute(bloc
  312.     l,i    }
  313.     }
  314. }
  315. /92he14,    ire set 9lr        /*[
  316. #eS, 12,e63 bytes.
  317. Let keyor n 84,  NULLnqj
  318.     j++){    /* select ,  5,  16);
  319.     if(kn ){    /18];
  320.  
  321. /* 8 6-bit subkeys for each of 16 rounds, initialized by setkey(y
  322.  
  323. /* 8 28];
  324. t  ,  5, n);
  325.  #e(!nedis    inarmiI%)
  326.             kn[i][j]=2]; 
  327. Lefoedis    inarmiI%)
  328. ] }
  329. /s.
  330.  
  331.         nt 9i {    /* s
  332.     }
  333. /*,
  334.      2){    /48; j++){     }
  335. /s.iperm}
  336.     perminit(iperm,ip);
  337.  
  338.     f6erm    }
  339. l */
  340.     ]; 
  341. l */
  342.     ];miperm}
  343.         inarmnk<56Gyrm,i)
  344.         frohariowP }
  345.