home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / comm / tcp / Crak.lha / crypt_util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-24  |  21.8 KB  |  829 lines

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