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