home *** CD-ROM | disk | FTP | other *** search
/ The Unsorted BBS Collection / thegreatunsorted.tar / thegreatunsorted / hacking / internet / circ01.sh / Circ / d3des / d3des.c
Encoding:
C/C++ Source or Header  |  1993-08-11  |  19.7 KB  |  684 lines

  1. /* D3DES (V5.09) -
  2.  *
  3.  * A portable, public domain, version of the Data Encryption Standard.
  4.  *
  5.  * Written with Symantec's THINK (Lightspeed) C by Richard Outerbridge.
  6.  * Thanks to: Dan Hoey for his excellent Initial and Inverse permutation
  7.  * code;  Jim Gillogly & Phil Karn for the DES key schedule code; Dennis
  8.  * Ferguson, Eric Young and Dana How for comparing notes; and Ray Lau,
  9.  * for humouring me on.
  10.  *
  11.  * Copyright (c) 1988,1989,1990,1991,1992 by Richard Outerbridge.
  12.  * (GEnie : OUTER; CIS : [71755,204]) Graven Imagery, 1992.
  13.  */
  14.  
  15. #include "d3des.h"
  16.  
  17. static void scrunch(/* unsigned char *, unsigned long * */);
  18. static void unscrun(/* unsigned long *, unsigned char * */);
  19. static void desfunc(/* unsigned long *, unsigned long * */);
  20. static void cookey(/* unsigned long * */);
  21.  
  22. static unsigned long KnL[32] = { 0L };
  23. static unsigned long KnR[32] = { 0L };
  24. static unsigned long Kn3[32] = { 0L };
  25. static unsigned char Df_Key[24] = {
  26.     0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  27.     0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10,
  28.     0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67 };
  29.  
  30. static unsigned short bytebit[8]    = {
  31.     0200, 0100, 040, 020, 010, 04, 02, 01 };
  32.  
  33. static unsigned long bigbyte[24] = {
  34.     0x800000L,    0x400000L,    0x200000L,    0x100000L,
  35.     0x80000L,    0x40000L,    0x20000L,    0x10000L,
  36.     0x8000L,    0x4000L,    0x2000L,    0x1000L,
  37.     0x800L,     0x400L,     0x200L,     0x100L,
  38.     0x80L,        0x40L,        0x20L,        0x10L,
  39.     0x8L,        0x4L,        0x2L,        0x1L    };
  40.  
  41. /* Use the key schedule specified in the Standard (ANSI X3.92-1981). */
  42.  
  43. static unsigned char pc1[56] = {
  44.     56, 48, 40, 32, 24, 16,  8,     0, 57, 49, 41, 33, 25, 17,
  45.      9,  1, 58, 50, 42, 34, 26,    18, 10,  2, 59, 51, 43, 35,
  46.     62, 54, 46, 38, 30, 22, 14,     6, 61, 53, 45, 37, 29, 21,
  47.     13,  5, 60, 52, 44, 36, 28,    20, 12,  4, 27, 19, 11,  3 };
  48.  
  49. static unsigned char totrot[16] = {
  50.     1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28 };
  51.  
  52. static unsigned char pc2[48] = {
  53.     13, 16, 10, 23,  0,  4,  2, 27, 14,  5, 20,  9,
  54.     22, 18, 11,  3, 25,  7, 15,  6, 26, 19, 12,  1,
  55.     40, 51, 30, 36, 46, 54, 29, 39, 50, 44, 32, 47,
  56.     43, 48, 38, 55, 33, 52, 45, 41, 49, 35, 28, 31 };
  57.  
  58. void deskey(key, edf)    /* Thanks to James Gillogly & Phil Karn! */
  59. unsigned char *key;
  60. short edf;
  61. {
  62.     register int i, j, l, m, n;
  63.     unsigned char pc1m[56], pcr[56];
  64.     unsigned long kn[32];
  65.  
  66.     for ( j = 0; j < 56; j++ ) {
  67.         l = pc1[j];
  68.         m = l & 07;
  69.         pc1m[j] = (key[l >> 3] & bytebit[m]) ? 1 : 0;
  70.         }
  71.     for( i = 0; i < 16; i++ ) {
  72.         if( edf == DE1 ) m = (15 - i) << 1;
  73.         else m = i << 1;
  74.         n = m + 1;
  75.         kn[m] = kn[n] = 0L;
  76.         for( j = 0; j < 28; j++ ) {
  77.             l = j + totrot[i];
  78.             if( l < 28 ) pcr[j] = pc1m[l];
  79.             else pcr[j] = pc1m[l - 28];
  80.             }
  81.         for( j = 28; j < 56; j++ ) {
  82.             l = j + totrot[i];
  83.             if( l < 56 ) pcr[j] = pc1m[l];
  84.             else pcr[j] = pc1m[l - 28];
  85.             }
  86.         for( j = 0; j < 24; j++ ) {
  87.             if( pcr[pc2[j]] ) kn[m] |= bigbyte[j];
  88.             if( pcr[pc2[j+24]] ) kn[n] |= bigbyte[j];
  89.             }
  90.         }
  91.     cookey(kn);
  92.     return;
  93.     }
  94.  
  95. static void cookey(raw1)
  96. register unsigned long *raw1;
  97. {
  98.     register unsigned long *cook, *raw0;
  99.     unsigned long dough[32];
  100.     register int i;
  101.  
  102.     cook = dough;
  103.     for( i = 0; i < 16; i++, raw1++ ) {
  104.         raw0 = raw1++;
  105.         *cook     = (*raw0 & 0x00fc0000L) << 6;
  106.         *cook    |= (*raw0 & 0x00000fc0L) << 10;
  107.         *cook    |= (*raw1 & 0x00fc0000L) >> 10;
  108.         *cook++ |= (*raw1 & 0x00000fc0L) >> 6;
  109.         *cook     = (*raw0 & 0x0003f000L) << 12;
  110.         *cook    |= (*raw0 & 0x0000003fL) << 16;
  111.         *cook    |= (*raw1 & 0x0003f000L) >> 4;
  112.         *cook++ |= (*raw1 & 0x0000003fL);
  113.         }
  114.     usekey(dough);
  115.     return;
  116.     }
  117.  
  118. void cpkey(into)
  119. register unsigned long *into;
  120. {
  121.     register unsigned long *from, *endp;
  122.  
  123.     from = KnL, endp = &KnL[32];
  124.     while( from < endp ) *into++ = *from++;
  125.     return;
  126.     }
  127.  
  128. void usekey(from)
  129. register unsigned long *from;
  130. {
  131.     register unsigned long *to, *endp;
  132.  
  133.     to = KnL, endp = &KnL[32];
  134.     while( to < endp ) *to++ = *from++;
  135.     return;
  136.     }
  137.  
  138. void des(inblock, outblock)
  139. unsigned char *inblock, *outblock;
  140. {
  141.     unsigned long work[2];
  142.  
  143.     scrunch(inblock, work);
  144.     desfunc(work, KnL);
  145.     unscrun(work, outblock);
  146.     return;
  147.     }
  148.  
  149. static void scrunch(outof, into)
  150. register unsigned char *outof;
  151. register unsigned long *into;
  152. {
  153.     *into     = (*outof++ & 0xffL) << 24;
  154.     *into    |= (*outof++ & 0xffL) << 16;
  155.     *into    |= (*outof++ & 0xffL) << 8;
  156.     *into++ |= (*outof++ & 0xffL);
  157.     *into     = (*outof++ & 0xffL) << 24;
  158.     *into    |= (*outof++ & 0xffL) << 16;
  159.     *into    |= (*outof++ & 0xffL) << 8;
  160.     *into    |= (*outof   & 0xffL);
  161.     return;
  162.     }
  163.  
  164. static void unscrun(outof, into)
  165. register unsigned long *outof;
  166. register unsigned char *into;
  167. {
  168.     *into++ = (*outof >> 24) & 0xffL;
  169.     *into++ = (*outof >> 16) & 0xffL;
  170.     *into++ = (*outof >>  8) & 0xffL;
  171.     *into++ =  *outof++     & 0xffL;
  172.     *into++ = (*outof >> 24) & 0xffL;
  173.     *into++ = (*outof >> 16) & 0xffL;
  174.     *into++ = (*outof >>  8) & 0xffL;
  175.     *into    =  *outof     & 0xffL;
  176.     return;
  177.     }
  178.  
  179. static unsigned long SP1[64] = {
  180.     0x01010400L, 0x00000000L, 0x00010000L, 0x01010404L,
  181.     0x01010004L, 0x00010404L, 0x00000004L, 0x00010000L,
  182.     0x00000400L, 0x01010400L, 0x01010404L, 0x00000400L,
  183.     0x01000404L, 0x01010004L, 0x01000000L, 0x00000004L,
  184.     0x00000404L, 0x01000400L, 0x01000400L, 0x00010400L,
  185.     0x00010400L, 0x01010000L, 0x01010000L, 0x01000404L,
  186.     0x00010004L, 0x01000004L, 0x01000004L, 0x00010004L,
  187.     0x00000000L, 0x00000404L, 0x00010404L, 0x01000000L,
  188.     0x00010000L, 0x01010404L, 0x00000004L, 0x01010000L,
  189.     0x01010400L, 0x01000000L, 0x01000000L, 0x00000400L,
  190.     0x01010004L, 0x00010000L, 0x00010400L, 0x01000004L,
  191.     0x00000400L, 0x00000004L, 0x01000404L, 0x00010404L,
  192.     0x01010404L, 0x00010004L, 0x01010000L, 0x01000404L,
  193.     0x01000004L, 0x00000404L, 0x00010404L, 0x01010400L,
  194.     0x00000404L, 0x01000400L, 0x01000400L, 0x00000000L,
  195.     0x00010004L, 0x00010400L, 0x00000000L, 0x01010004L };
  196.  
  197. static unsigned long SP2[64] = {
  198.     0x80108020L, 0x80008000L, 0x00008000L, 0x00108020L,
  199.     0x00100000L, 0x00000020L, 0x80100020L, 0x80008020L,
  200.     0x80000020L, 0x80108020L, 0x80108000L, 0x80000000L,
  201.     0x80008000L, 0x00100000L, 0x00000020L, 0x80100020L,
  202.     0x00108000L, 0x00100020L, 0x80008020L, 0x00000000L,
  203.     0x80000000L, 0x00008000L, 0x00108020L, 0x80100000L,
  204.     0x00100020L, 0x80000020L, 0x00000000L, 0x00108000L,
  205.     0x00008020L, 0x80108000L, 0x80100000L, 0x00008020L,
  206.     0x00000000L, 0x00108020L, 0x80100020L, 0x00100000L,
  207.     0x80008020L, 0x80100000L, 0x80108000L, 0x00008000L,
  208.     0x80100000L, 0x80008000L, 0x00000020L, 0x80108020L,
  209.     0x00108020L, 0x00000020L, 0x00008000L, 0x80000000L,
  210.     0x00008020L, 0x80108000L, 0x00100000L, 0x80000020L,
  211.     0x00100020L, 0x80008020L, 0x80000020L, 0x00100020L,
  212.     0x00108000L, 0x00000000L, 0x80008000L, 0x00008020L,
  213.     0x80000000L, 0x80100020L, 0x80108020L, 0x00108000L };
  214.  
  215. static unsigned long SP3[64] = {
  216.     0x00000208L, 0x08020200L, 0x00000000L, 0x08020008L,
  217.     0x08000200L, 0x00000000L, 0x00020208L, 0x08000200L,
  218.     0x00020008L, 0x08000008L, 0x08000008L, 0x00020000L,
  219.     0x08020208L, 0x00020008L, 0x08020000L, 0x00000208L,
  220.     0x08000000L, 0x00000008L, 0x08020200L, 0x00000200L,
  221.     0x00020200L, 0x08020000L, 0x08020008L, 0x00020208L,
  222.     0x08000208L, 0x00020200L, 0x00020000L, 0x08000208L,
  223.     0x00000008L, 0x08020208L, 0x00000200L, 0x08000000L,
  224.     0x08020200L, 0x08000000L, 0x00020008L, 0x00000208L,
  225.     0x00020000L, 0x08020200L, 0x08000200L, 0x00000000L,
  226.     0x00000200L, 0x00020008L, 0x08020208L, 0x08000200L,
  227.     0x08000008L, 0x00000200L, 0x00000000L, 0x08020008L,
  228.     0x08000208L, 0x00020000L, 0x08000000L, 0x08020208L,
  229.     0x00000008L, 0x00020208L, 0x00020200L, 0x08000008L,
  230.     0x08020000L, 0x08000208L, 0x00000208L, 0x08020000L,
  231.     0x00020208L, 0x00000008L, 0x08020008L, 0x00020200L };
  232.  
  233. static unsigned long SP4[64] = {
  234.     0x00802001L, 0x00002081L, 0x00002081L, 0x00000080L,
  235.     0x00802080L, 0x00800081L, 0x00800001L, 0x00002001L,
  236.     0x00000000L, 0x00802000L, 0x00802000L, 0x00802081L,
  237.     0x00000081L, 0x00000000L, 0x00800080L, 0x00800001L,
  238.     0x00000001L, 0x00002000L, 0x00800000L, 0x00802001L,
  239.     0x00000080L, 0x00800000L, 0x00002001L, 0x00002080L,
  240.     0x00800081L, 0x00000001L, 0x00002080L, 0x00800080L,
  241.     0x00002000L, 0x00802080L, 0x00802081L, 0x00000081L,
  242.     0x00800080L, 0x00800001L, 0x00802000L, 0x00802081L,
  243.     0x00000081L, 0x00000000L, 0x00000000L, 0x00802000L,
  244.     0x00002080L, 0x00800080L, 0x00800081L, 0x00000001L,
  245.     0x00802001L, 0x00002081L, 0x00002081L, 0x00000080L,
  246.     0x00802081L, 0x00000081L, 0x00000001L, 0x00002000L,
  247.     0x00800001L, 0x00002001L, 0x00802080L, 0x00800081L,
  248.     0x00002001L, 0x00002080L, 0x00800000L, 0x00802001L,
  249.     0x00000080L, 0x00800000L, 0x00002000L, 0x00802080L };
  250.  
  251. static unsigned long SP5[64] = {
  252.     0x00000100L, 0x02080100L, 0x02080000L, 0x42000100L,
  253.     0x00080000L, 0x00000100L, 0x40000000L, 0x02080000L,
  254.     0x40080100L, 0x00080000L, 0x02000100L, 0x40080100L,
  255.     0x42000100L, 0x42080000L, 0x00080100L, 0x40000000L,
  256.     0x02000000L, 0x40080000L, 0x40080000L, 0x00000000L,
  257.     0x40000100L, 0x42080100L, 0x42080100L, 0x02000100L,
  258.     0x42080000L, 0x40000100L, 0x00000000L, 0x42000000L,
  259.     0x02080100L, 0x02000000L, 0x42000000L, 0x00080100L,
  260.     0x00080000L, 0x42000100L, 0x00000100L, 0x02000000L,
  261.     0x40000000L, 0x02080000L, 0x42000100L, 0x40080100L,
  262.     0x02000100L, 0x40000000L, 0x42080000L, 0x02080100L,
  263.     0x40080100L, 0x00000100L, 0x02000000L, 0x42080000L,
  264.     0x42080100L, 0x00080100L, 0x42000000L, 0x42080100L,
  265.     0x02080000L, 0x00000000L, 0x40080000L, 0x42000000L,
  266.     0x00080100L, 0x02000100L, 0x40000100L, 0x00080000L,
  267.     0x00000000L, 0x40080000L, 0x02080100L, 0x40000100L };
  268.  
  269. static unsigned long SP6[64] = {
  270.     0x20000010L, 0x20400000L, 0x00004000L, 0x20404010L,
  271.     0x20400000L, 0x00000010L, 0x20404010L, 0x00400000L,
  272.     0x20004000L, 0x00404010L, 0x00400000L, 0x20000010L,
  273.     0x00400010L, 0x20004000L, 0x20000000L, 0x00004010L,
  274.     0x00000000L, 0x00400010L, 0x20004010L, 0x00004000L,
  275.     0x00404000L, 0x20004010L, 0x00000010L, 0x20400010L,
  276.     0x20400010L, 0x00000000L, 0x00404010L, 0x20404000L,
  277.     0x00004010L, 0x00404000L, 0x20404000L, 0x20000000L,
  278.     0x20004000L, 0x00000010L, 0x20400010L, 0x00404000L,
  279.     0x20404010L, 0x00400000L, 0x00004010L, 0x20000010L,
  280.     0x00400000L, 0x20004000L, 0x20000000L, 0x00004010L,
  281.     0x20000010L, 0x20404010L, 0x00404000L, 0x20400000L,
  282.     0x00404010L, 0x20404000L, 0x00000000L, 0x20400010L,
  283.     0x00000010L, 0x00004000L, 0x20400000L, 0x00404010L,
  284.     0x00004000L, 0x00400010L, 0x20004010L, 0x00000000L,
  285.     0x20404000L, 0x20000000L, 0x00400010L, 0x20004010L };
  286.  
  287. static unsigned long SP7[64] = {
  288.     0x00200000L, 0x04200002L, 0x04000802L, 0x00000000L,
  289.     0x00000800L, 0x04000802L, 0x00200802L, 0x04200800L,
  290.     0x04200802L, 0x00200000L, 0x00000000L, 0x04000002L,
  291.     0x00000002L, 0x04000000L, 0x04200002L, 0x00000802L,
  292.     0x04000800L, 0x00200802L, 0x00200002L, 0x04000800L,
  293.     0x04000002L, 0x04200000L, 0x04200800L, 0x00200002L,
  294.     0x04200000L, 0x00000800L, 0x00000802L, 0x04200802L,
  295.     0x00200800L, 0x00000002L, 0x04000000L, 0x00200800L,
  296.     0x04000000L, 0x00200800L, 0x00200000L, 0x04000802L,
  297.     0x04000802L, 0x04200002L, 0x04200002L, 0x00000002L,
  298.     0x00200002L, 0x04000000L, 0x04000800L, 0x00200000L,
  299.     0x04200800L, 0x00000802L, 0x00200802L, 0x04200800L,
  300.     0x00000802L, 0x04000002L, 0x04200802L, 0x04200000L,
  301.     0x00200800L, 0x00000000L, 0x00000002L, 0x04200802L,
  302.     0x00000000L, 0x00200802L, 0x04200000L, 0x00000800L,
  303.     0x04000002L, 0x04000800L, 0x00000800L, 0x00200002L };
  304.  
  305. static unsigned long SP8[64] = {
  306.     0x10001040L, 0x00001000L, 0x00040000L, 0x10041040L,
  307.     0x10000000L, 0x10001040L, 0x00000040L, 0x10000000L,
  308.     0x00040040L, 0x10040000L, 0x10041040L, 0x00041000L,
  309.     0x10041000L, 0x00041040L, 0x00001000L, 0x00000040L,
  310.     0x10040000L, 0x10000040L, 0x10001000L, 0x00001040L,
  311.     0x00041000L, 0x00040040L, 0x10040040L, 0x10041000L,
  312.     0x00001040L, 0x00000000L, 0x00000000L, 0x10040040L,
  313.     0x10000040L, 0x10001000L, 0x00041040L, 0x00040000L,
  314.     0x00041040L, 0x00040000L, 0x10041000L, 0x00001000L,
  315.     0x00000040L, 0x10040040L, 0x00001000L, 0x00041040L,
  316.     0x10001000L, 0x00000040L, 0x10000040L, 0x10040000L,
  317.     0x10040040L, 0x10000000L, 0x00040000L, 0x10001040L,
  318.     0x00000000L, 0x10041040L, 0x00040040L, 0x10000040L,
  319.     0x10040000L, 0x10001000L, 0x10001040L, 0x00000000L,
  320.     0x10041040L, 0x00041000L, 0x00041000L, 0x00001040L,
  321.     0x00001040L, 0x00040040L, 0x10000000L, 0x10041000L };
  322.  
  323. static void desfunc(block, keys)
  324. register unsigned long *block, *keys;
  325. {
  326.     register unsigned long fval, work, right, leftt;
  327.     register int round;
  328.  
  329.     leftt = block[0];
  330.     right = block[1];
  331.     work = ((leftt >> 4) ^ right) & 0x0f0f0f0fL;
  332.     right ^= work;
  333.     leftt ^= (work << 4);
  334.     work = ((leftt >> 16) ^ right) & 0x0000ffffL;
  335.     right ^= work;
  336.     leftt ^= (work << 16);
  337.     work = ((right >> 2) ^ leftt) & 0x33333333L;
  338.     leftt ^= work;
  339.     right ^= (work << 2);
  340.     work = ((right >> 8) ^ leftt) & 0x00ff00ffL;
  341.     leftt ^= work;
  342.     right ^= (work << 8);
  343.     right = ((right << 1) | ((right >> 31) & 1L)) & 0xffffffffL;
  344.     work = (leftt ^ right) & 0xaaaaaaaaL;
  345.     leftt ^= work;
  346.     right ^= work;
  347.     leftt = ((leftt << 1) | ((leftt >> 31) & 1L)) & 0xffffffffL;
  348.  
  349.     for( round = 0; round < 8; round++ ) {
  350.         work  = (right << 28) | (right >> 4);
  351.         work ^= *keys++;
  352.         fval  = SP7[ work         & 0x3fL];
  353.         fval |= SP5[(work >>  8) & 0x3fL];
  354.         fval |= SP3[(work >> 16) & 0x3fL];
  355.         fval |= SP1[(work >> 24) & 0x3fL];
  356.         work  = right ^ *keys++;
  357.         fval |= SP8[ work         & 0x3fL];
  358.         fval |= SP6[(work >>  8) & 0x3fL];
  359.         fval |= SP4[(work >> 16) & 0x3fL];
  360.         fval |= SP2[(work >> 24) & 0x3fL];
  361.         leftt ^= fval;
  362.         work  = (leftt << 28) | (leftt >> 4);
  363.         work ^= *keys++;
  364.         fval  = SP7[ work         & 0x3fL];
  365.         fval |= SP5[(work >>  8) & 0x3fL];
  366.         fval |= SP3[(work >> 16) & 0x3fL];
  367.         fval |= SP1[(work >> 24) & 0x3fL];
  368.         work  = leftt ^ *keys++;
  369.         fval |= SP8[ work         & 0x3fL];
  370.         fval |= SP6[(work >>  8) & 0x3fL];
  371.         fval |= SP4[(work >> 16) & 0x3fL];
  372.         fval |= SP2[(work >> 24) & 0x3fL];
  373.         right ^= fval;
  374.         }
  375.  
  376.     right = (right << 31) | (right >> 1);
  377.     work = (leftt ^ right) & 0xaaaaaaaaL;
  378.     leftt ^= work;
  379.     right ^= work;
  380.     leftt = (leftt << 31) | (leftt >> 1);
  381.     work = ((leftt >> 8) ^ right) & 0x00ff00ffL;
  382.     right ^= work;
  383.     leftt ^= (work << 8);
  384.     work = ((leftt >> 2) ^ right) & 0x33333333L;
  385.     right ^= work;
  386.     leftt ^= (work << 2);
  387.     work = ((right >> 16) ^ leftt) & 0x0000ffffL;
  388.     leftt ^= work;
  389.     right ^= (work << 16);
  390.     work = ((right >> 4) ^ leftt) & 0x0f0f0f0fL;
  391.     leftt ^= work;
  392.     right ^= (work << 4);
  393.     *block++ = right;
  394.     *block = leftt;
  395.     return;
  396.     }
  397.  
  398. #ifdef D2_DES
  399.  
  400. void des2key(hexkey, mode)        /* stomps on Kn3 too */
  401. unsigned char *hexkey;            /* unsigned char[16] */
  402. short mode;
  403. {
  404.     short revmod;
  405.  
  406.     revmod = (mode == EN0) ? DE1 : EN0;
  407.     deskey(&hexkey[8], revmod);
  408.     cpkey(KnR);
  409.     deskey(hexkey, mode);
  410.     cpkey(Kn3);                    /* Kn3 = KnL */
  411.     return;
  412.     }
  413.  
  414. void Ddes(from, into)
  415. unsigned char *from, *into;        /* unsigned char[8] */
  416. {
  417.     unsigned long work[2];
  418.  
  419.     scrunch(from, work);
  420.     desfunc(work, KnL);
  421.     desfunc(work, KnR);
  422.     desfunc(work, Kn3);
  423.     unscrun(work, into);
  424.     return;
  425.     }
  426.  
  427. void D2des(from, into)
  428. unsigned char *from;            /* unsigned char[16] */
  429. unsigned char *into;            /* unsigned char[16] */
  430. {
  431.     unsigned long *right, *l1, swap;
  432.     unsigned long leftt[2], bufR[2];
  433.  
  434.     right = bufR;
  435.     l1 = &leftt[1];
  436.     scrunch(from, leftt);
  437.     scrunch(&from[8], right);
  438.     desfunc(leftt, KnL);
  439.     desfunc(right, KnL);
  440.     swap = *l1;
  441.     *l1 = *right;
  442.     *right = swap;
  443.     desfunc(leftt, KnR);
  444.     desfunc(right, KnR);
  445.     swap = *l1;
  446.     *l1 = *right;
  447.     *right = swap;
  448.     desfunc(leftt, Kn3);
  449.     desfunc(right, Kn3);
  450.     unscrun(leftt, into);
  451.     unscrun(right, &into[8]);
  452.     return;
  453.     }
  454.  
  455. void makekey(aptr, kptr)
  456. register char *aptr;                /* NULL-terminated  */
  457. register unsigned char *kptr;        /* unsigned char[8] */
  458. {
  459.     register unsigned char *store;
  460.     register int first, i;
  461.     unsigned long savek[96];
  462.  
  463.     cpDkey(savek);
  464.     des2key(Df_Key, EN0);
  465.     for( i = 0; i < 8; i++ ) kptr[i] = Df_Key[i];
  466.     first = 1;
  467.     while( (*aptr != '\0') || first ) {
  468.         store = kptr;
  469.         for( i = 0; i < 8 && (*aptr != '\0'); i++ ) {
  470.             *store++ ^= *aptr & 0x7f;
  471.             *aptr++ = '\0';
  472.             }
  473.         Ddes(kptr, kptr);
  474.         first = 0;
  475.         }
  476.     useDkey(savek);
  477.     return;
  478.     }
  479.  
  480. void make2key(aptr, kptr)
  481. register char *aptr;                /* NULL-terminated   */
  482. register unsigned char *kptr;        /* unsigned char[16] */
  483. {
  484.     register unsigned char *store;
  485.     register int first, i;
  486.     unsigned long savek[96];
  487.  
  488.     cpDkey(savek);
  489.     des2key(Df_Key, EN0);
  490.     for( i = 0; i < 16; i++ ) kptr[i] = Df_Key[i];
  491.     first = 1;
  492.     while( (*aptr != '\0') || first ) {
  493.         store = kptr;
  494.         for( i = 0; i < 16 && (*aptr != '\0'); i++ ) {
  495.             *store++ ^= *aptr & 0x7f;
  496.             *aptr++ = '\0';
  497.             }
  498.         D2des(kptr, kptr);
  499.         first = 0;
  500.         }
  501.     useDkey(savek);
  502.     return;
  503.     }
  504.  
  505. #ifndef D3_DES    /* D2_DES only */
  506. #ifdef    D2_DES    /* iff D2_DES! */
  507.  
  508. void cp2key(into)
  509. register unsigned long *into;    /* unsigned long[64] */
  510. {
  511.     register unsigned long *from, *endp;
  512.  
  513.     cpkey(into);
  514.     into = &into[32];
  515.     from = KnR, endp = &KnR[32];
  516.     while( from < endp ) *into++ = *from++;
  517.     return;
  518.     }
  519.  
  520. void use2key(from)                /* stomps on Kn3 too */
  521. register unsigned long *from;    /* unsigned long[64] */
  522. {
  523.     register unsigned long *to, *endp;
  524.  
  525.     usekey(from);
  526.     from = &from[32];
  527.     to = KnR, endp = &KnR[32];
  528.     while( to < endp ) *to++ = *from++;
  529.     cpkey(Kn3);                    /* Kn3 = KnL */
  530.     return;
  531.     }
  532.  
  533. #endif    /* iff D2_DES */
  534. #else    /* D3_DES too */
  535.  
  536. static void D3des(/* unsigned char *, unsigned char * */);
  537.  
  538. void des3key(hexkey, mode)
  539. unsigned char *hexkey;            /* unsigned char[24] */
  540. short mode;
  541. {
  542.     unsigned char *first, *third;
  543.     short revmod;
  544.  
  545.     if( mode == EN0 ) {
  546.         revmod = DE1;
  547.         first = hexkey;
  548.         third = &hexkey[16];
  549.         }
  550.     else {
  551.         revmod = EN0;
  552.         first = &hexkey[16];
  553.         third = hexkey;
  554.         }
  555.     deskey(&hexkey[8], revmod);
  556.     cpkey(KnR);
  557.     deskey(third, mode);
  558.     cpkey(Kn3);
  559.     deskey(first, mode);
  560.     return;
  561.     }
  562.  
  563. void cp3key(into)
  564. register unsigned long *into;    /* unsigned long[96] */
  565. {
  566.     register unsigned long *from, *endp;
  567.  
  568.     cpkey(into);
  569.     into = &into[32];
  570.     from = KnR, endp = &KnR[32];
  571.     while( from < endp ) *into++ = *from++;
  572.     from = Kn3, endp = &Kn3[32];
  573.     while( from < endp ) *into++ = *from++;
  574.     return;
  575.     }
  576.  
  577. void use3key(from)
  578. register unsigned long *from;    /* unsigned long[96] */
  579. {
  580.     register unsigned long *to, *endp;
  581.  
  582.     usekey(from);
  583.     from = &from[32];
  584.     to = KnR, endp = &KnR[32];
  585.     while( to < endp ) *to++ = *from++;
  586.     to = Kn3, endp = &Kn3[32];
  587.     while( to < endp ) *to++ = *from++;
  588.     return;
  589.     }
  590.  
  591. static void D3des(from, into)    /* amateur theatrics */
  592. unsigned char *from;            /* unsigned char[24] */
  593. unsigned char *into;            /* unsigned char[24] */
  594. {
  595.     unsigned long swap, leftt[2], middl[2], right[2];
  596.  
  597.     scrunch(from, leftt);
  598.     scrunch(&from[8], middl);
  599.     scrunch(&from[16], right);
  600.     desfunc(leftt, KnL);
  601.     desfunc(middl, KnL);
  602.     desfunc(right, KnL);
  603.     swap = leftt[1];
  604.     leftt[1] = middl[0];
  605.     middl[0] = swap;
  606.     swap = middl[1];
  607.     middl[1] = right[0];
  608.     right[0] = swap;
  609.     desfunc(leftt, KnR);
  610.     desfunc(middl, KnR);
  611.     desfunc(right, KnR);
  612.     swap = leftt[1];
  613.     leftt[1] = middl[0];
  614.     middl[0] = swap;
  615.     swap = middl[1];
  616.     middl[1] = right[0];
  617.     right[0] = swap;
  618.     desfunc(leftt, Kn3);
  619.     desfunc(middl, Kn3);
  620.     desfunc(right, Kn3);
  621.     unscrun(leftt, into);
  622.     unscrun(middl, &into[8]);
  623.     unscrun(right, &into[16]);
  624.     return;
  625.     }
  626.  
  627. void make3key(aptr, kptr)
  628. register char *aptr;                /* NULL-terminated   */
  629. register unsigned char *kptr;        /* unsigned char[24] */
  630. {
  631.     register unsigned char *store;
  632.     register int first, i;
  633.     unsigned long savek[96];
  634.  
  635.     cp3key(savek);
  636.     des3key(Df_Key, EN0);
  637.     for( i = 0; i < 24; i++ ) kptr[i] = Df_Key[i];
  638.     first = 1;
  639.     while( (*aptr != '\0') || first ) {
  640.         store = kptr;
  641.         for( i = 0; i < 24 && (*aptr != '\0'); i++ ) {
  642.             *store++ ^= *aptr & 0x7f;
  643.             *aptr++ = '\0';
  644.             }
  645.         D3des(kptr, kptr);
  646.         first = 0;
  647.         }
  648.     use3key(savek);
  649.     return;
  650.     }
  651.  
  652. #endif    /* D3_DES */
  653. #endif    /* D2_DES */
  654.  
  655. /* Validation sets:
  656.  *
  657.  * Single-length key, single-length plaintext -
  658.  * Key      : 0123 4567 89ab cdef
  659.  * Plain  : 0123 4567 89ab cde7
  660.  * Cipher : c957 4425 6a5e d31d
  661.  *
  662.  * Double-length key, single-length plaintext -
  663.  * Key      : 0123 4567 89ab cdef fedc ba98 7654 3210
  664.  * Plain  : 0123 4567 89ab cde7
  665.  * Cipher : 7f1d 0a77 826b 8aff
  666.  *
  667.  * Double-length key, double-length plaintext -
  668.  * Key      : 0123 4567 89ab cdef fedc ba98 7654 3210
  669.  * Plain  : 0123 4567 89ab cdef 0123 4567 89ab cdff
  670.  * Cipher : 27a0 8440 406a df60 278f 47cf 42d6 15d7
  671.  *
  672.  * Triple-length key, single-length plaintext -
  673.  * Key      : 0123 4567 89ab cdef fedc ba98 7654 3210 89ab cdef 0123 4567
  674.  * Plain  : 0123 4567 89ab cde7
  675.  * Cipher : de0b 7c06 ae5e 0ed5
  676.  *
  677.  * Triple-length key, double-length plaintext -
  678.  * Key      : 0123 4567 89ab cdef fedc ba98 7654 3210 89ab cdef 0123 4567
  679.  * Plain  : 0123 4567 89ab cdef 0123 4567 89ab cdff
  680.  * Cipher : ad0d 1b30 ac17 cf07 0ed1 1c63 81e4 4de5
  681.  *
  682.  * d3des V5.0a rwo 9208.07 18:44 Graven Imagery
  683.  **********************************************************************/
  684.