home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ool_main.zip / ool / source / crypt_util.c next >
C/C++ Source or Header  |  1998-02-22  |  24KB  |  891 lines

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