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