crypt_util.c
上传用户:zlh9724
上传日期:2007-01-04
资源大小:1991k
文件大小:25k
源码类别:

浏览器

开发平台:

Unix_Linux

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