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