home *** CD-ROM | disk | FTP | other *** search
/ Freesoft 1997 May / Freesoft_1997-05_cd.bin / nerecenz / programers / CRYPT / XCRYPT / crypt_util.c next >
C/C++ Source or Header  |  1997-04-02  |  26KB  |  1,007 lines

  1. /*
  2.  * UFC-crypt: ultra fast crypt(3) implementation
  3.  *
  4.  * Copyright (C) 1991, 1992, Michael Glad, email: glad@daimi.aau.dk
  5.  *
  6.  * This library is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Library General Public
  8.  * License as published by the Free Software Foundation; either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * This library is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * Library General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU Library General Public
  17.  * License along with this library; if not, write to the Free
  18.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  * @(#)crypt_util.c    2.29 01/23/92
  21.  *
  22.  * Support routines
  23.  *
  24.  */
  25.  
  26. #ifdef DEBUG
  27. #include <stdio.h>
  28. #endif
  29.  
  30. #ifdef linux
  31. #include <string.h>
  32. #endif
  33.  
  34. #ifdef _WINDOWS            //added by Markus Schulte markus@dom.de
  35. //#include <string.h>    
  36. #include <windows.h>
  37. #include <ole2.h>
  38. #include <time.h>
  39. #define CCONV _stdcall
  40. #define NOMANGLE
  41. #endif
  42.  
  43. #ifndef STATIC
  44. #define STATIC static
  45. #endif
  46.  
  47. #include "patchlevel.h"
  48. #include "ufc-crypt.h"
  49.  
  50. extern ufc_long *ufc_doit  (ufc_long, ufc_long, ufc_long,
  51.                             ufc_long, ufc_long);
  52.  
  53. /*
  54.  *  Prototypes
  55.  */
  56. #ifdef __STDC__
  57. #ifdef DEBUG
  58. void           pr_bits              (ufc_long *, int);
  59. static void    set_bits             (ufc_long, ufc_long *);
  60. #endif    /* DEBUG */
  61. STATIC void    clearmem             (char *, int);
  62. void init_des                       (void);
  63. #ifdef _UFC_32_
  64. STATIC void    shuffle_sb           (long32 *, ufc_long);
  65. #endif    /* _UFC_32_ */
  66. #ifdef _UFC_64_
  67. STATIC void    shuffle_sb           (long64 *, ufc_long);
  68. #endif    /* _UFC_64_ */
  69. STATIC void    setup_salt           (char *);
  70. STATIC void    ufc_mk_keytab        (char *);
  71. ufc_long      *ufc_dofinalperm      (ufc_long, ufc_long, ufc_long,
  72.                                      ufc_long);
  73. STATIC char   *output_conversion    (ufc_long, ufc_long, char *);
  74. char          *crypt                (char *, char *);
  75. char          *fcrypt               (char *, char *);
  76. void           encrypt              (char *, int);
  77. void           setkey               (char *);
  78. #else        /* ! __STDC__ */
  79. #ifdef DEBUG
  80. void           pr_bits              ();
  81. static void    set_bits             ();
  82. #endif    /* DEBUG */
  83. STATIC void    clearmem             ();
  84. void init_des                       ();
  85. #ifdef _UFC_32_
  86. STATIC  void    shuffle_sb           ();
  87. #endif    /* _UFC_32_ */
  88. #ifdef _UFC_64_
  89. STATIC void    shuffle_sb           ();
  90. #endif    /* _UFC_64_ */
  91. STATIC void    setup_salt           ();
  92. STATIC void    ufc_mk_keytab        ();
  93. ufc_long      *ufc_dofinalperm      ();
  94. STATIC char   *output_conversion    ();
  95. char          *crypt                ();
  96. char          *fcrypt               ();
  97. void           encrypt              ();
  98. void           setkey               ();
  99. #endif    /* __STDC__ */
  100.  
  101. #if 0
  102. /* We don't need the warning for this: */
  103. static char patchlevel_str[] = PATCHLEVEL;
  104. #endif    /* #if 0 */
  105.  
  106. /* 
  107.  * Permutation done once on the 56 bit 
  108.  *  key derived from the original 8 byte ASCII key.
  109.  */
  110. static int pc1[56] = { 
  111.   57, 49, 41, 33, 25, 17,  9,  1, 58, 50, 42, 34, 26, 18,
  112.   10,  2, 59, 51, 43, 35, 27, 19, 11,  3, 60, 52, 44, 36,
  113.   63, 55, 47, 39, 31, 23, 15,  7, 62, 54, 46, 38, 30, 22,
  114.   14,  6, 61, 53, 45, 37, 29, 21, 13,  5, 28, 20, 12,  4
  115. };
  116.  
  117. /*
  118.  * How much to rotate each 28 bit half of the pc1 permutated
  119.  *  56 bit key before using pc2 to give the i' key
  120.  */
  121. static int rots[16] = { 
  122.   1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 
  123. };
  124.  
  125. /* 
  126.  * Permutation giving the key 
  127.  * of the i' DES round 
  128.  */
  129. static int pc2[48] = { 
  130.   14, 17, 11, 24,  1,  5,  3, 28, 15,  6, 21, 10,
  131.   23, 19, 12,  4, 26,  8, 16,  7, 27, 20, 13,  2,
  132.   41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48,
  133.   44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32
  134. };
  135.  
  136. /*
  137.  * The E expansion table which selects
  138.  * bits from the 32 bit intermediate result.
  139.  */
  140. static int esel[48] = { 
  141.   32,  1,  2,  3,  4,  5,  4,  5,  6,  7,  8,  9,
  142.    8,  9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17,
  143.   16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25,
  144.   24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32,  1
  145. };
  146. static int e_inverse[64];
  147.  
  148. /* 
  149.  * Permutation done on the 
  150.  * result of sbox lookups 
  151.  */
  152. static int perm32[32] = {
  153.   16,  7, 20, 21, 29, 12, 28, 17,  1, 15, 23, 26,  5, 18, 31, 10,
  154.   2,   8, 24, 14, 32, 27,  3,  9, 19, 13, 30,  6, 22, 11,  4, 25
  155. };
  156.  
  157. /* 
  158.  * The sboxes
  159.  */
  160. static int sbox[8][4][16]= {
  161.         { { 14,  4, 13,  1,  2, 15, 11,  8,  3, 10,  6, 12,  5,  9,  0,  7 },
  162.           {  0, 15,  7,  4, 14,  2, 13,  1, 10,  6, 12, 11,  9,  5,  3,  8 },
  163.           {  4,  1, 14,  8, 13,  6,  2, 11, 15, 12,  9,  7,  3, 10,  5,  0 },
  164.           { 15, 12,  8,  2,  4,  9,  1,  7,  5, 11,  3, 14, 10,  0,  6, 13 }
  165.         },
  166.  
  167.         { { 15,  1,  8, 14,  6, 11,  3,  4,  9,  7,  2, 13, 12,  0,  5, 10 },
  168.           {  3, 13,  4,  7, 15,  2,  8, 14, 12,  0,  1, 10,  6,  9, 11,  5 },
  169.           {  0, 14,  7, 11, 10,  4, 13,  1,  5,  8, 12,  6,  9,  3,  2, 15 },
  170.           { 13,  8, 10,  1,  3, 15,  4,  2, 11,  6,  7, 12,  0,  5, 14,  9 }
  171.         },
  172.  
  173.         { { 10,  0,  9, 14,  6,  3, 15,  5,  1, 13, 12,  7, 11,  4,  2,  8 },
  174.           { 13,  7,  0,  9,  3,  4,  6, 10,  2,  8,  5, 14, 12, 11, 15,  1 },
  175.           { 13,  6,  4,  9,  8, 15,  3,  0, 11,  1,  2, 12,  5, 10, 14,  7 },
  176.           {  1, 10, 13,  0,  6,  9,  8,  7,  4, 15, 14,  3, 11,  5,  2, 12 }
  177.         },
  178.  
  179.         { {  7, 13, 14,  3,  0,  6,  9, 10,  1,  2,  8,  5, 11, 12,  4, 15 },
  180.           { 13,  8, 11,  5,  6, 15,  0,  3,  4,  7,  2, 12,  1, 10, 14,  9 },
  181.           { 10,  6,  9,  0, 12, 11,  7, 13, 15,  1,  3, 14,  5,  2,  8,  4 },
  182.           {  3, 15,  0,  6, 10,  1, 13,  8,  9,  4,  5, 11, 12,  7,  2, 14 }
  183.         },
  184.  
  185.         { {  2, 12,  4,  1,  7, 10, 11,  6,  8,  5,  3, 15, 13,  0, 14,  9 },
  186.           { 14, 11,  2, 12,  4,  7, 13,  1,  5,  0, 15, 10,  3,  9,  8,  6 },
  187.           {  4,  2,  1, 11, 10, 13,  7,  8, 15,  9, 12,  5,  6,  3,  0, 14 },
  188.           { 11,  8, 12,  7,  1, 14,  2, 13,  6, 15,  0,  9, 10,  4,  5,  3 }
  189.         },
  190.  
  191.         { { 12,  1, 10, 15,  9,  2,  6,  8,  0, 13,  3,  4, 14,  7,  5, 11 },
  192.           { 10, 15,  4,  2,  7, 12,  9,  5,  6,  1, 13, 14,  0, 11,  3,  8 },
  193.           {  9, 14, 15,  5,  2,  8, 12,  3,  7,  0,  4, 10,  1, 13, 11,  6 },
  194.           {  4,  3,  2, 12,  9,  5, 15, 10, 11, 14,  1,  7,  6,  0,  8, 13 }
  195.         },
  196.  
  197.         { {  4, 11,  2, 14, 15,  0,  8, 13,  3, 12,  9,  7,  5, 10,  6,  1 },
  198.           { 13,  0, 11,  7,  4,  9,  1, 10, 14,  3,  5, 12,  2, 15,  8,  6 },
  199.           {  1,  4, 11, 13, 12,  3,  7, 14, 10, 15,  6,  8,  0,  5,  9,  2 },
  200.           {  6, 11, 13,  8,  1,  4, 10,  7,  9,  5,  0, 15, 14,  2,  3, 12 }
  201.         },
  202.  
  203.         { { 13,  2,  8,  4,  6, 15, 11,  1, 10,  9,  3, 14,  5,  0, 12,  7 },
  204.           {  1, 15, 13,  8, 10,  3,  7,  4, 12,  5,  6, 11,  0, 14,  9,  2 },
  205.           {  7, 11,  4,  1,  9, 12, 14,  2,  0,  6, 10, 13, 15,  3,  5,  8 },
  206.           {  2,  1, 14,  7,  4, 10,  8, 13, 15, 12,  9,  0,  3,  5,  6, 11 }
  207.         }
  208. };
  209.  
  210. /* 
  211.  * This is the initial 
  212.  * permutation matrix
  213.  */
  214. static int initial_perm[64] = { 
  215.   58, 50, 42, 34, 26, 18, 10,  2, 60, 52, 44, 36, 28, 20, 12, 4,
  216.   62, 54, 46, 38, 30, 22, 14,  6, 64, 56, 48, 40, 32, 24, 16, 8,
  217.   57, 49, 41, 33, 25, 17,  9,  1, 59, 51, 43, 35, 27, 19, 11, 3,
  218.   61, 53, 45, 37, 29, 21, 13,  5, 63, 55, 47, 39, 31, 23, 15, 7
  219. };
  220.  
  221. /* 
  222.  * This is the final 
  223.  * permutation matrix
  224.  */
  225. static int final_perm[64] = {
  226.   40,  8, 48, 16, 56, 24, 64, 32, 39,  7, 47, 15, 55, 23, 63, 31,
  227.   38,  6, 46, 14, 54, 22, 62, 30, 37,  5, 45, 13, 53, 21, 61, 29,
  228.   36,  4, 44, 12, 52, 20, 60, 28, 35,  3, 43, 11, 51, 19, 59, 27,
  229.   34,  2, 42, 10, 50, 18, 58, 26, 33,  1, 41,  9, 49, 17, 57, 25
  230. };
  231.  
  232. /* 
  233.  * The 16 DES keys in BITMASK format 
  234.  */
  235. #ifdef _UFC_32_
  236. long32 ufc_keytab[16][2];
  237. #endif
  238. #ifdef _UFC_64_
  239. long64 ufc_keytab[16];
  240. #endif
  241.  
  242. #define ascii_to_bin(c) ((c)>='a'?(c-59):(c)>='A'?((c)-53):(c)-'.')
  243. #define bin_to_ascii(c) ((c)>=38?((c)-38+'a'):(c)>=12?((c)-12+'A'):(c)+'.')
  244.  
  245. /* Macro to set a bit (0..23) */
  246. #define BITMASK(i) ( (1<<(11-(i)%12+3)) << ((i)<12?16:0) )
  247.  
  248. /*
  249.  * sb arrays:
  250.  *
  251.  * Workhorses of the inner loop of the DES implementation.
  252.  * They do sbox lookup, shifting of this  value, 32 bit
  253.  * permutation and E permutation for the next round.
  254.  *
  255.  * Kept in 'BITMASK' format.
  256.  */
  257.  
  258. #ifdef _UFC_32_
  259. long32 ufc_sb0[8192], ufc_sb1[8192], ufc_sb2[8192], ufc_sb3[8192];
  260. static long32 *sb[4] = {ufc_sb0, ufc_sb1, ufc_sb2, ufc_sb3}; 
  261. #endif
  262.  
  263. #ifdef _UFC_64_
  264. long64 ufc_sb0[4096], ufc_sb1[4096], ufc_sb2[4096], ufc_sb3[4096];
  265. static long64 *sb[4] = {ufc_sb0, ufc_sb1, ufc_sb2, ufc_sb3}; 
  266. #endif
  267.  
  268. /* 
  269.  * eperm32tab: do 32 bit permutation and E selection
  270.  *
  271.  * The first index is the byte number in the 32 bit value to be permuted
  272.  *  -  second  -   is the value of this byte
  273.  *  -  third   -   selects the two 32 bit values
  274.  *
  275.  * The table is used and generated internally in init_des to speed it up
  276.  */
  277. static ufc_long eperm32tab[4][256][2];
  278.  
  279. /* 
  280.  * do_pc1: permform pc1 permutation in the key schedule generation.
  281.  *
  282.  * The first   index is the byte number in the 8 byte ASCII key
  283.  *  -  second    -      -    the two 28 bits halfs of the result
  284.  *  -  third     -   selects the 7 bits actually used of each byte
  285.  *
  286.  * The result is kept with 28 bit per 32 bit with the 4 most significant
  287.  * bits zero.
  288.  */
  289. static ufc_long do_pc1[8][2][128];
  290.  
  291. /*
  292.  * do_pc2: permform pc2 permutation in the key schedule generation.
  293.  *
  294.  * The first   index is the septet number in the two 28 bit intermediate values
  295.  *  -  second    -    -  -  septet values
  296.  *
  297.  * Knowledge of the structure of the pc2 permutation is used.
  298.  *
  299.  * The result is kept with 28 bit per 32 bit with the 4 most significant
  300.  * bits zero.
  301.  */
  302. static ufc_long do_pc2[8][128];
  303.  
  304. /*
  305.  * efp: undo an extra e selection and do final
  306.  *      permutation giving the DES result.
  307.  * 
  308.  *      Invoked 6 bit a time on two 48 bit values
  309.  *      giving two 32 bit longs.
  310.  */
  311. static ufc_long efp[16][64][2];
  312.  
  313. static unsigned char bytemask[8]  = {
  314.   0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01
  315. };
  316.  
  317. static ufc_long longmask[32] = {
  318.   0x80000000, 0x40000000, 0x20000000, 0x10000000,
  319.   0x08000000, 0x04000000, 0x02000000, 0x01000000,
  320.   0x00800000, 0x00400000, 0x00200000, 0x00100000,
  321.   0x00080000, 0x00040000, 0x00020000, 0x00010000,
  322.   0x00008000, 0x00004000, 0x00002000, 0x00001000,
  323.   0x00000800, 0x00000400, 0x00000200, 0x00000100,
  324.   0x00000080, 0x00000040, 0x00000020, 0x00000010,
  325.   0x00000008, 0x00000004, 0x00000002, 0x00000001
  326. };
  327.  
  328. #ifdef DEBUG
  329.  
  330. #ifdef __STDC__
  331. void
  332. pr_bits( ufc_long *a, int n)
  333. #else
  334. void
  335. pr_bits(a, n)
  336.   ufc_long *a;
  337.   int n;
  338. #endif
  339. {
  340.     ufc_long i, j, t, tmp;
  341.     n /= 8;
  342.     for(i = 0; i < n; i++) {
  343.       tmp=0;
  344.       for(j = 0; j < 8; j++) {
  345.     t=8*i+j;
  346.     tmp|=(a[t/24] & BITMASK(t % 24))?bytemask[j]:0;
  347.       }
  348.       (void)printf("%02x ",tmp);
  349.     }
  350.     printf(" ");
  351. }
  352.  
  353. #ifdef __STDC__
  354. void
  355. static set_bits(ufc_long v, ufc_long *b)
  356. #else
  357. void
  358. static set_bits(v, b)
  359.   ufc_long v;
  360.   ufc_long *b;
  361. #endif
  362. {
  363.     ufc_long i;
  364.     *b = 0;
  365.     for(i = 0; i < 24; i++) {
  366.       if(v & longmask[8 + i])
  367.     *b |= BITMASK(i);
  368.     }
  369. }
  370.  
  371. #endif    /* DEBUG */
  372.  
  373. /*
  374.  * Silly rewrite of 'bzero'. I do so
  375.  * because some machines don't have
  376.  * bzero and some don't have memset.
  377.  */
  378.  
  379. #ifdef __STDC__
  380. STATIC void
  381. clearmem( char *start, int cnt)
  382. #else
  383. //void
  384. STATIC void clearmem(start, cnt)
  385.   char *start;
  386.   int cnt;
  387. #endif
  388. {
  389.    while(cnt--)
  390.       *start++ = '\0';
  391. }
  392.  
  393. static int initialized = 0;
  394.  
  395. /* lookup a 6 bit value in sbox */
  396.  
  397. #define s_lookup(i,s) sbox[(i)][(((s)>>4) & 0x2)|((s) & 0x1)][((s)>>1) & 0xf];
  398.  
  399. /*
  400.  * Initialize unit - may be invoked directly
  401.  * by fcrypt users.
  402.  */
  403.  
  404. #ifdef __STDC__
  405. void
  406. init_des(void)
  407. #else
  408. void init_des()
  409. #endif
  410. {
  411.     int comes_from_bit;
  412.     int bit, sg;
  413.     ufc_long j;
  414.     ufc_long mask1, mask2;
  415.  
  416.     /*
  417.      * Create the do_pc1 table used
  418.      * to affect pc1 permutation
  419.      * when generating keys
  420.      */
  421.     for(bit = 0; bit < 56; bit++) {
  422.       comes_from_bit  = pc1[bit] - 1;
  423.       mask1 = bytemask[comes_from_bit % 8 + 1];
  424.       mask2 = longmask[bit % 28 + 4];
  425.       for(j = 0; j < 128; j++) {
  426.     if(j & mask1) 
  427.       do_pc1[comes_from_bit / 8][bit / 28][j] |= mask2;
  428.       }
  429.     }
  430.  
  431.     /*
  432.      * Create the do_pc2 table used
  433.      * to affect pc2 permutation when
  434.      * generating keys
  435.      */
  436.     for(bit = 0; bit < 48; bit++) {
  437.       comes_from_bit  = pc2[bit] - 1;
  438.       mask1 = bytemask[comes_from_bit % 7 + 1];
  439.       mask2 = BITMASK(bit % 24);
  440.       for(j = 0; j < 128; j++) {
  441.     if(j & mask1)
  442.       do_pc2[comes_from_bit / 7][j] |= mask2;
  443.       }
  444.     }
  445.  
  446.     /* 
  447.      * Now generate the table used to do combined
  448.      * 32 bit permutation and e expansion
  449.      *
  450.      * We use it because we have to permute 16384 32 bit
  451.      * longs into 48 bit in order to initialize sb.
  452.      *
  453.      * Looping 48 rounds per permutation becomes 
  454.      * just too slow...
  455.      *
  456.      */
  457.  
  458.     clearmem((char*)eperm32tab, sizeof(eperm32tab));
  459.  
  460.     for(bit = 0; bit < 48; bit++) {
  461.       ufc_long mask1,comes_from;
  462.     
  463.       comes_from = perm32[esel[bit]-1]-1;
  464.       mask1      = bytemask[comes_from % 8];
  465.     
  466.       for(j = 256; j--;) {
  467.     if(j & mask1)
  468.       eperm32tab[comes_from / 8][j][bit / 24] |= BITMASK(bit % 24);
  469.       }
  470.     }
  471.     
  472.     /* 
  473.      * Create the sb tables:
  474.      *
  475.      * For each 12 bit segment of an 48 bit intermediate
  476.      * result, the sb table precomputes the two 4 bit
  477.      * values of the sbox lookups done with the two 6
  478.      * bit halves, shifts them to their proper place,
  479.      * sends them through perm32 and finally E expands
  480.      * them so that they are ready for the next
  481.      * DES round.
  482.      *
  483.      */
  484.     for(sg = 0; sg < 4; sg++) {
  485.       int j1, j2;
  486.       int s1, s2;
  487.     
  488.       for(j1 = 0; j1 < 64; j1++) {
  489.     s1 = s_lookup(2 * sg, j1);
  490.     for(j2 = 0; j2 < 64; j2++) {
  491.       ufc_long to_permute, inx;
  492.     
  493.       s2         = s_lookup(2 * sg + 1, j2);
  494.       to_permute = ((s1 << 4)  | s2) << (24 - 8 * sg);
  495.  
  496. #ifdef _UFC_32_
  497.       inx = ((j1 << 6)  | j2) << 1;
  498.       sb[sg][inx  ]  = eperm32tab[0][(to_permute >> 24) & 0xff][0];
  499.       sb[sg][inx+1]  = eperm32tab[0][(to_permute >> 24) & 0xff][1];
  500.       sb[sg][inx  ] |= eperm32tab[1][(to_permute >> 16) & 0xff][0];
  501.       sb[sg][inx+1] |= eperm32tab[1][(to_permute >> 16) & 0xff][1];
  502.         sb[sg][inx  ] |= eperm32tab[2][(to_permute >>  8) & 0xff][0];
  503.       sb[sg][inx+1] |= eperm32tab[2][(to_permute >>  8) & 0xff][1];
  504.       sb[sg][inx  ] |= eperm32tab[3][(to_permute)       & 0xff][0];
  505.       sb[sg][inx+1] |= eperm32tab[3][(to_permute)       & 0xff][1];
  506. #endif
  507. #ifdef _UFC_64_
  508.       inx = ((j1 << 6)  | j2);
  509.       sb[sg][inx]  = 
  510.         ((long64)eperm32tab[0][(to_permute >> 24) & 0xff][0] << 32) |
  511.          (long64)eperm32tab[0][(to_permute >> 24) & 0xff][1];
  512.       sb[sg][inx] |=
  513.         ((long64)eperm32tab[1][(to_permute >> 16) & 0xff][0] << 32) |
  514.          (long64)eperm32tab[1][(to_permute >> 16) & 0xff][1];
  515.         sb[sg][inx] |= 
  516.         ((long64)eperm32tab[2][(to_permute >>  8) & 0xff][0] << 32) |
  517.          (long64)eperm32tab[2][(to_permute >>  8) & 0xff][1];
  518.       sb[sg][inx] |=
  519.         ((long64)eperm32tab[3][(to_permute)       & 0xff][0] << 32) |
  520.          (long64)eperm32tab[3][(to_permute)       & 0xff][1];
  521. #endif
  522.     }
  523.       }
  524.     }  
  525.  
  526.     /* 
  527.      * Create an inverse matrix for esel telling
  528.      * where to plug out bits if undoing it
  529.      */
  530.     for(bit=48; bit--;) {
  531.       e_inverse[esel[bit] - 1     ] = bit;
  532.       e_inverse[esel[bit] - 1 + 32] = bit + 48;
  533.     }
  534.  
  535.     /* 
  536.      * create efp: the matrix used to
  537.      * undo the E expansion and effect final permutation
  538.      */
  539.     clearmem((char*)efp, sizeof efp);
  540.     for(bit = 0; bit < 64; bit++) {
  541.       int o_bit, o_long;
  542.       ufc_long word_value, mask1, mask2;
  543.       int comes_from_f_bit, comes_from_e_bit;
  544.       int comes_from_word, bit_within_word;
  545.  
  546.       /* See where bit i belongs in the two 32 bit long's */
  547.       o_long = bit / 32; /* 0..1  */
  548.       o_bit  = bit % 32; /* 0..31 */
  549.  
  550.       /* 
  551.        * And find a bit in the e permutated value setting this bit.
  552.        *
  553.        * Note: the e selection may have selected the same bit several
  554.        * times. By the initialization of e_inverse, we only look
  555.        * for one specific instance.
  556.        */
  557.       comes_from_f_bit = final_perm[bit] - 1;         /* 0..63 */
  558.       comes_from_e_bit = e_inverse[comes_from_f_bit]; /* 0..95 */
  559.       comes_from_word  = comes_from_e_bit / 6;        /* 0..15 */
  560.       bit_within_word  = comes_from_e_bit % 6;        /* 0..5  */
  561.  
  562.       mask1 = longmask[bit_within_word + 26];
  563.       mask2 = longmask[o_bit];
  564.  
  565.       for(word_value = 64; word_value--;) {
  566.     if(word_value & mask1)
  567.       efp[comes_from_word][word_value][o_long] |= mask2;
  568.       }
  569.     }
  570.     initialized++;
  571. }
  572.  
  573. /* 
  574.  * Process the elements of the sb table permuting the
  575.  * bits swapped in the expansion by the current salt.
  576.  */
  577.  
  578. #ifdef _UFC_32_
  579. #ifdef __STDC__
  580. STATIC void
  581. shuffle_sb(long32 *k, ufc_long saltbits)
  582. #else
  583. STATIC void shuffle_sb(k, saltbits)
  584.   long32 *k;
  585.   ufc_long saltbits;
  586. #endif
  587. {
  588.     ufc_long j;
  589.     long32 x;
  590.     for(j=4096; j--;) {
  591.       x = (k[0] ^ k[1]) & (long32)saltbits;
  592.       *k++ ^= x;
  593.       *k++ ^= x;
  594.     }
  595. }
  596. #endif    /* _UFC_32_ */
  597.  
  598. #ifdef _UFC_64_
  599. #ifdef __STDC__
  600. STATIC void
  601. shuffle_sb(long64 *k, ufc_long saltbits)
  602. #else
  603. STATIC void shuffle_sb(k, saltbits)
  604.   long64 *k;
  605.   ufc_long saltbits;
  606. #endif
  607.   { ufc_long j;
  608.     long64 x;
  609.     for(j=4096; j--;) {
  610.       x = ((*k >> 32) ^ *k) & (long64)saltbits;
  611.       *k++ ^= (x << 32) | x;
  612.     }
  613.   }
  614. #endif    /* _UFC_64_ */
  615.  
  616. /* 
  617.  * Setup the unit for a new salt
  618.  * Hopefully we'll not see a new salt in each crypt call.
  619.  */
  620.  
  621. static char current_salt[3] = "&&"; /* invalid value */
  622. static ufc_long current_saltbits = 0;
  623. static int direction = 0;
  624.  
  625. #ifdef __STDC__
  626. STATIC void
  627. setup_salt(char *s)
  628. #else
  629. STATIC void setup_salt(s)
  630.   char *s;
  631. #endif
  632. {
  633.     ufc_long i, j, saltbits;
  634.  
  635.     if(!initialized)
  636.       init_des();
  637.  
  638.     if(s[0] == current_salt[0] && s[1] == current_salt[1])
  639.       return;
  640.     current_salt[0] = s[0]; current_salt[1] = s[1];
  641.  
  642.     /* 
  643.      * This is the only crypt change to DES:
  644.      * entries are swapped in the expansion table
  645.      * according to the bits set in the salt.
  646.      */
  647.     saltbits = 0;
  648.     for(i = 0; i < 2; i++) {
  649.       long c=ascii_to_bin(s[i]);
  650.       if(c < 0 || c > 63)
  651.     c = 0;
  652.       for(j = 0; j < 6; j++) {
  653.     if((c >> j) & 0x1)
  654.       saltbits |= BITMASK(6 * i + j);
  655.       }
  656.     }
  657.  
  658.     /*
  659.      * Permute the sb table values
  660.      * to reflect the changed e
  661.      * selection table
  662.      */
  663.     shuffle_sb(ufc_sb0, current_saltbits ^ saltbits); 
  664.     shuffle_sb(ufc_sb1, current_saltbits ^ saltbits);
  665.     shuffle_sb(ufc_sb2, current_saltbits ^ saltbits);
  666.     shuffle_sb(ufc_sb3, current_saltbits ^ saltbits);
  667.  
  668.     current_saltbits = saltbits;
  669. }
  670.  
  671. #ifdef __STDC__
  672. STATIC void
  673. ufc_mk_keytab(char *key)
  674. #else
  675. STATIC void ufc_mk_keytab(key)
  676.   char *key;
  677. #endif
  678. {
  679.     ufc_long v1, v2, *k1;
  680.     int i;
  681. #ifdef _UFC_32_
  682.     long32 v, *k2 = &ufc_keytab[0][0];
  683. #endif
  684. #ifdef _UFC_64_
  685.     long64 v, *k2 = &ufc_keytab[0];
  686. #endif
  687.  
  688.     v1 = v2 = 0; k1 = &do_pc1[0][0][0];
  689.     for(i = 8; i--;) {
  690.       v1 |= k1[*key   & 0x7f]; k1 += 128;
  691.       v2 |= k1[*key++ & 0x7f]; k1 += 128;
  692.     }
  693.  
  694.     for(i = 0; i < 16; i++) {
  695.       k1 = &do_pc2[0][0];
  696.  
  697.       v1 = (v1 << rots[i]) | (v1 >> (28 - rots[i]));
  698.       v  = k1[(v1 >> 21) & 0x7f]; k1 += 128;
  699.       v |= k1[(v1 >> 14) & 0x7f]; k1 += 128;
  700.       v |= k1[(v1 >>  7) & 0x7f]; k1 += 128;
  701.       v |= k1[(v1      ) & 0x7f]; k1 += 128;
  702.  
  703. #ifdef _UFC_32_
  704.       *k2++ = v;
  705.       v = 0;
  706. #endif
  707. #ifdef _UFC_64_
  708.       v <<= 32;
  709. #endif
  710.  
  711.       v2 = (v2 << rots[i]) | (v2 >> (28 - rots[i]));
  712.       v |= k1[(v2 >> 21) & 0x7f]; k1 += 128;
  713.       v |= k1[(v2 >> 14) & 0x7f]; k1 += 128;
  714.       v |= k1[(v2 >>  7) & 0x7f]; k1 += 128;
  715.       v |= k1[(v2      ) & 0x7f];
  716.  
  717.       *k2++ = v;
  718.     }
  719.  
  720.     direction = 0;
  721. }
  722.  
  723. /* 
  724.  * Undo an extra E selection and do final permutations
  725.  */
  726.  
  727. #ifdef __STDC__
  728. ufc_long *
  729. ufc_dofinalperm( ufc_long l1, ufc_long l2, ufc_long r1, ufc_long r2)
  730. #else
  731. ufc_long *
  732. ufc_dofinalperm(l1, l2, r1, r2)
  733.   ufc_long l1,l2,r1,r2;
  734. #endif
  735. {
  736.     ufc_long v1, v2, x;
  737.     static ufc_long ary[2];
  738.  
  739.     x = (l1 ^ l2) & current_saltbits; l1 ^= x; l2 ^= x;
  740.     x = (r1 ^ r2) & current_saltbits; r1 ^= x; r2 ^= x;
  741.  
  742.     v1=v2=0; l1 >>= 3; l2 >>= 3; r1 >>= 3; r2 >>= 3;
  743.  
  744.     v1 |= efp[15][ r2         & 0x3f][0]; v2 |= efp[15][ r2 & 0x3f][1];
  745.     v1 |= efp[14][(r2 >>= 6)  & 0x3f][0]; v2 |= efp[14][ r2 & 0x3f][1];
  746.     v1 |= efp[13][(r2 >>= 10) & 0x3f][0]; v2 |= efp[13][ r2 & 0x3f][1];
  747.     v1 |= efp[12][(r2 >>= 6)  & 0x3f][0]; v2 |= efp[12][ r2 & 0x3f][1];
  748.  
  749.     v1 |= efp[11][ r1         & 0x3f][0]; v2 |= efp[11][ r1 & 0x3f][1];
  750.     v1 |= efp[10][(r1 >>= 6)  & 0x3f][0]; v2 |= efp[10][ r1 & 0x3f][1];
  751.     v1 |= efp[ 9][(r1 >>= 10) & 0x3f][0]; v2 |= efp[ 9][ r1 & 0x3f][1];
  752.     v1 |= efp[ 8][(r1 >>= 6)  & 0x3f][0]; v2 |= efp[ 8][ r1 & 0x3f][1];
  753.  
  754.     v1 |= efp[ 7][ l2         & 0x3f][0]; v2 |= efp[ 7][ l2 & 0x3f][1];
  755.     v1 |= efp[ 6][(l2 >>= 6)  & 0x3f][0]; v2 |= efp[ 6][ l2 & 0x3f][1];
  756.     v1 |= efp[ 5][(l2 >>= 10) & 0x3f][0]; v2 |= efp[ 5][ l2 & 0x3f][1];
  757.     v1 |= efp[ 4][(l2 >>= 6)  & 0x3f][0]; v2 |= efp[ 4][ l2 & 0x3f][1];
  758.  
  759.     v1 |= efp[ 3][ l1         & 0x3f][0]; v2 |= efp[ 3][ l1 & 0x3f][1];
  760.     v1 |= efp[ 2][(l1 >>= 6)  & 0x3f][0]; v2 |= efp[ 2][ l1 & 0x3f][1];
  761.     v1 |= efp[ 1][(l1 >>= 10) & 0x3f][0]; v2 |= efp[ 1][ l1 & 0x3f][1];
  762.     v1 |= efp[ 0][(l1 >>= 6)  & 0x3f][0]; v2 |= efp[ 0][ l1 & 0x3f][1];
  763.  
  764.     ary[0] = v1; ary[1] = v2;
  765.     return ary;
  766. }
  767.  
  768. /* 
  769.  * crypt only: convert from 64 bit to 11 bit ASCII 
  770.  * prefixing with the salt
  771.  */
  772.  
  773. #ifdef __STDC__
  774. STATIC char *
  775. output_conversion( ufc_long v1, ufc_long v2, char *salt)
  776. #else
  777. STATIC char *output_conversion(v1, v2, salt)
  778.    ufc_long v1, v2;
  779.    char *salt;
  780. #endif
  781. {
  782.     static char outbuf[14];
  783.     int i, s;
  784.  
  785.     outbuf[0] = salt[0];
  786.     outbuf[1] = salt[1] ? salt[1] : salt[0];
  787.  
  788.     for(i = 0; i < 5; i++)
  789.       outbuf[i + 2] = bin_to_ascii((v1 >> (26 - 6 * i)) & 0x3f);
  790.  
  791.     s  = (v2 & 0xf) << 2;
  792.     v2 = (v2 >> 2) | ((v1 & 0x3) << 30);
  793.  
  794.     for(i = 5; i < 10; i++)
  795.       outbuf[i + 2] = bin_to_ascii((v2 >> (56 - 6 * i)) & 0x3f);
  796.  
  797.     outbuf[12] = bin_to_ascii(s);
  798.     outbuf[13] = 0;
  799.  
  800.     return outbuf;
  801. }
  802.  
  803. /* 
  804.  * UNIX crypt function
  805.  */
  806.    
  807. #ifdef __STDC__
  808. char *
  809. crypt(char *key, char *salt)
  810. #else
  811. char *crypt(key, salt)
  812.   char *key, *salt;
  813. #endif
  814. {
  815.     ufc_long *s;
  816.     char ktab[9];
  817.  
  818.     /*
  819.      * Hack DES tables according to salt
  820.      */
  821.     setup_salt(salt);
  822.  
  823.     /*
  824.      * Setup key schedule
  825.      */
  826.     clearmem(ktab, sizeof ktab);
  827.     (void)strncpy(ktab, key, 8);
  828.     ufc_mk_keytab(ktab);
  829.  
  830.     /*
  831.      * Go for the 25 DES encryptions
  832.      */
  833.     s = ufc_doit((ufc_long)0, (ufc_long)0, 
  834.          (ufc_long)0, (ufc_long)0, (ufc_long)25);
  835.  
  836.     /*
  837.      * And convert back to 6 bit ASCII
  838.      */
  839.     return output_conversion(s[0], s[1], salt);
  840. }
  841.  
  842. /* 
  843.  * To make fcrypt users happy.
  844.  * They don't need to call init_des.
  845.  */
  846.  
  847. #ifdef __STDC__
  848. char *
  849. fcrypt(char *key, char *salt)
  850. #else
  851. char *fcrypt(key, salt)
  852.   char *key;
  853.   char *salt;
  854. #endif
  855. {
  856.    return crypt(key, salt);
  857. }
  858.  
  859. /* 
  860.  * UNIX encrypt function. Takes a bitvector
  861.  * represented by one byte per bit and
  862.  * encrypt/decrypt according to edflag
  863.  */
  864.  
  865. #ifdef __STDC__
  866. void
  867. encrypt(char *block, int edflag)
  868. #else
  869. void encrypt(block, edflag)
  870.   char *block;
  871.   int edflag;
  872. #endif
  873. {
  874.     ufc_long l1, l2, r1, r2, *s;
  875.     int i;
  876.  
  877.     /*
  878.      * Undo any salt changes to E expansion
  879.      */
  880.     setup_salt("..");
  881.  
  882.     /*
  883.      * Reverse key table if
  884.      * changing operation (encrypt/decrypt)
  885.      */
  886.     if((edflag == 0) != (direction == 0)) {
  887.       for(i = 0; i < 8; i++) {
  888. #ifdef _UFC_32_
  889.     long32 x;
  890.     x = ufc_keytab[15-i][0]; 
  891.         ufc_keytab[15-i][0] = ufc_keytab[i][0]; 
  892.         ufc_keytab[i][0] = x;
  893.  
  894.     x = ufc_keytab[15-i][1]; 
  895.         ufc_keytab[15-i][1] = ufc_keytab[i][1]; 
  896.         ufc_keytab[i][1] = x;
  897. #endif
  898. #ifdef _UFC_64_
  899.     long64 x;
  900.     x = ufc_keytab[15-i];
  901.     ufc_keytab[15-i] = ufc_keytab[i];
  902.     ufc_keytab[i] = x;
  903. #endif
  904.       }
  905.       direction = edflag;
  906.     }
  907.  
  908.     /*
  909.      * Do initial permutation + E expansion
  910.      */
  911.     i = 0;
  912.     for(l1 = 0; i < 24; i++) {
  913.       if(block[initial_perm[esel[i]-1]-1])
  914.     l1 |= BITMASK(i);
  915.     }
  916.     for(l2 = 0; i < 48; i++) {
  917.       if(block[initial_perm[esel[i]-1]-1])
  918.     l2 |= BITMASK(i-24);
  919.     }
  920.  
  921.     i = 0;
  922.     for(r1 = 0; i < 24; i++) {
  923.       if(block[initial_perm[esel[i]-1+32]-1])
  924.     r1 |= BITMASK(i);
  925.     }
  926.     for(r2 = 0; i < 48; i++) {
  927.       if(block[initial_perm[esel[i]-1+32]-1])
  928.     r2 |= BITMASK(i-24);
  929.     }
  930.  
  931.     /*
  932.      * Do DES inner loops + final conversion
  933.      */
  934.     s = ufc_doit(l1, l2, r1, r2, (ufc_long)1);
  935.  
  936.     /*
  937.      * And convert to bit array
  938.      */
  939.     l1 = s[0]; r1 = s[1];
  940.     for(i = 0; i < 32; i++) {
  941.       *block++ = (l1 & longmask[i]) != 0;
  942.     }
  943.     for(i = 0; i < 32; i++) {
  944.       *block++ = (r1 & longmask[i]) != 0;
  945.     }
  946.     
  947. }
  948.  
  949. /* 
  950.  * UNIX setkey function. Take a 64 bit DES
  951.  * key and setup the machinery.
  952.  */
  953.  
  954. #ifdef __STDC__
  955. void
  956. setkey(char *key)
  957. #else
  958. void setkey(key)
  959.   char *key;
  960. #endif
  961. {
  962.     int i,j;
  963.     unsigned char c;
  964.     unsigned char ktab[8];
  965.  
  966.     setup_salt(".."); /* be sure we're initialized */
  967.  
  968.     for(i = 0; i < 8; i++) {
  969.       for(j = 0, c = 0; j < 8; j++)
  970.     c = c << 1 | *key++;
  971.       ktab[i] = c >> 1;
  972.     }
  973.     
  974.     ufc_mk_keytab(ktab);
  975. }
  976.  
  977. #ifdef _WINDOWS            //added by Markus Schulte markus@dom.de
  978. #undef RAND_MAX
  979. #define RAND_MAX 0x3f
  980. NOMANGLE BSTR CCONV xcrypt (BSTR *bstrClearPwd)
  981. {
  982.     BSTR bstrEncPwd;
  983.     char *pEncPwd,*pTmp;
  984.     int i;
  985.     unsigned zahl1;
  986.     char salt[2];
  987.     const char* allsalt = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
  988.     srand( (unsigned)time( NULL ) );
  989.     do {
  990.         zahl1 = rand();
  991.     } while (zahl1>63);
  992.     salt[0] = *(allsalt+zahl1);
  993.     do {
  994.         zahl1 = rand();
  995.     } while (zahl1>63);
  996.     salt[1] = *(allsalt+zahl1);
  997.     pTmp = crypt((char*)*bstrClearPwd,salt);
  998.     bstrEncPwd = SysAllocStringLen(NULL, 13);
  999.     pEncPwd = (char*) bstrEncPwd;
  1000.     for(i=0;i<14;i++)
  1001.         *pEncPwd++ = *pTmp++;
  1002.     SysReAllocString(bstrClearPwd,(BSTR)"Good Bye");
  1003.     return bstrEncPwd;
  1004. }
  1005.  
  1006. #endif
  1007.