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