mux/src/crypt/crypt_util.cpp

Go to the documentation of this file.
00001 /*
00002  * UFC-crypt: ultra fast crypt(3) implementation
00003  *
00004  * Copyright (C) 1991, 1992, 1993, 1996, 1997 Free Software Foundation, Inc.
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Library General Public
00017  * License along with this library; see the file COPYING.LIB.  If not,
00018  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019  * Boston, MA 02111-1307, USA.
00020  *
00021  * @(#)crypt_util.c 2.56 12/20/96
00022  *
00023  * Support routines
00024  *
00025  */
00026 
00027 #ifdef DEBUG
00028 #include <stdio.h>
00029 #endif
00030 #include <string.h>
00031 
00032 #ifndef STATIC
00033 #define STATIC static
00034 #endif
00035 
00036 #ifndef DOS
00037 #include "patchlevel.h"
00038 #include "ufc-crypt.h"
00039 #else
00040 /*
00041  * Thanks to greg%wind@plains.NoDak.edu (Greg W. Wettstein)
00042  * for DOS patches
00043  */
00044 #include "pl.h"
00045 #include "ufc.h"
00046 #endif
00047 #include "crypt.h"
00048 #include "crypt-private.h"
00049 
00050 /* Prototypes for local functions.  */
00051 #ifdef _UFC_32_
00052 STATIC void shuffle_sb (long32 *k, ufc_long saltbits);
00053 #else
00054 STATIC void shuffle_sb (long64 *k, ufc_long saltbits);
00055 #endif
00056 
00057 
00058 static const char patchlevel_str[] = PATCHLEVEL;
00059 
00060 /*
00061  * Permutation done once on the 56 bit
00062  *  key derived from the original 8 byte ASCII key.
00063  */
00064 static const int pc1[56] = {
00065   57, 49, 41, 33, 25, 17,  9,  1, 58, 50, 42, 34, 26, 18,
00066   10,  2, 59, 51, 43, 35, 27, 19, 11,  3, 60, 52, 44, 36,
00067   63, 55, 47, 39, 31, 23, 15,  7, 62, 54, 46, 38, 30, 22,
00068   14,  6, 61, 53, 45, 37, 29, 21, 13,  5, 28, 20, 12,  4
00069 };
00070 
00071 /*
00072  * How much to rotate each 28 bit half of the pc1 permutated
00073  *  56 bit key before using pc2 to give the i' key
00074  */
00075 static const int rots[16] = {
00076   1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
00077 };
00078 
00079 /*
00080  * Permutation giving the key
00081  * of the i' DES round
00082  */
00083 static const int pc2[48] = {
00084   14, 17, 11, 24,  1,  5,  3, 28, 15,  6, 21, 10,
00085   23, 19, 12,  4, 26,  8, 16,  7, 27, 20, 13,  2,
00086   41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48,
00087   44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32
00088 };
00089 
00090 /*
00091  * The E expansion table which selects
00092  * bits from the 32 bit intermediate result.
00093  */
00094 static const int esel[48] = {
00095   32,  1,  2,  3,  4,  5,  4,  5,  6,  7,  8,  9,
00096    8,  9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17,
00097   16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25,
00098   24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32,  1
00099 };
00100 
00101 /*
00102  * Permutation done on the
00103  * result of sbox lookups
00104  */
00105 static const int perm32[32] = {
00106   16,  7, 20, 21, 29, 12, 28, 17,  1, 15, 23, 26,  5, 18, 31, 10,
00107   2,   8, 24, 14, 32, 27,  3,  9, 19, 13, 30,  6, 22, 11,  4, 25
00108 };
00109 
00110 /*
00111  * The sboxes
00112  */
00113 static const int sbox[8][4][16]= {
00114         { { 14,  4, 13,  1,  2, 15, 11,  8,  3, 10,  6, 12,  5,  9,  0,  7 },
00115           {  0, 15,  7,  4, 14,  2, 13,  1, 10,  6, 12, 11,  9,  5,  3,  8 },
00116           {  4,  1, 14,  8, 13,  6,  2, 11, 15, 12,  9,  7,  3, 10,  5,  0 },
00117           { 15, 12,  8,  2,  4,  9,  1,  7,  5, 11,  3, 14, 10,  0,  6, 13 }
00118         },
00119 
00120         { { 15,  1,  8, 14,  6, 11,  3,  4,  9,  7,  2, 13, 12,  0,  5, 10 },
00121           {  3, 13,  4,  7, 15,  2,  8, 14, 12,  0,  1, 10,  6,  9, 11,  5 },
00122           {  0, 14,  7, 11, 10,  4, 13,  1,  5,  8, 12,  6,  9,  3,  2, 15 },
00123           { 13,  8, 10,  1,  3, 15,  4,  2, 11,  6,  7, 12,  0,  5, 14,  9 }
00124         },
00125 
00126         { { 10,  0,  9, 14,  6,  3, 15,  5,  1, 13, 12,  7, 11,  4,  2,  8 },
00127           { 13,  7,  0,  9,  3,  4,  6, 10,  2,  8,  5, 14, 12, 11, 15,  1 },
00128           { 13,  6,  4,  9,  8, 15,  3,  0, 11,  1,  2, 12,  5, 10, 14,  7 },
00129           {  1, 10, 13,  0,  6,  9,  8,  7,  4, 15, 14,  3, 11,  5,  2, 12 }
00130         },
00131 
00132         { {  7, 13, 14,  3,  0,  6,  9, 10,  1,  2,  8,  5, 11, 12,  4, 15 },
00133           { 13,  8, 11,  5,  6, 15,  0,  3,  4,  7,  2, 12,  1, 10, 14,  9 },
00134           { 10,  6,  9,  0, 12, 11,  7, 13, 15,  1,  3, 14,  5,  2,  8,  4 },
00135           {  3, 15,  0,  6, 10,  1, 13,  8,  9,  4,  5, 11, 12,  7,  2, 14 }
00136         },
00137 
00138         { {  2, 12,  4,  1,  7, 10, 11,  6,  8,  5,  3, 15, 13,  0, 14,  9 },
00139           { 14, 11,  2, 12,  4,  7, 13,  1,  5,  0, 15, 10,  3,  9,  8,  6 },
00140           {  4,  2,  1, 11, 10, 13,  7,  8, 15,  9, 12,  5,  6,  3,  0, 14 },
00141           { 11,  8, 12,  7,  1, 14,  2, 13,  6, 15,  0,  9, 10,  4,  5,  3 }
00142         },
00143 
00144         { { 12,  1, 10, 15,  9,  2,  6,  8,  0, 13,  3,  4, 14,  7,  5, 11 },
00145           { 10, 15,  4,  2,  7, 12,  9,  5,  6,  1, 13, 14,  0, 11,  3,  8 },
00146           {  9, 14, 15,  5,  2,  8, 12,  3,  7,  0,  4, 10,  1, 13, 11,  6 },
00147           {  4,  3,  2, 12,  9,  5, 15, 10, 11, 14,  1,  7,  6,  0,  8, 13 }
00148         },
00149 
00150         { {  4, 11,  2, 14, 15,  0,  8, 13,  3, 12,  9,  7,  5, 10,  6,  1 },
00151           { 13,  0, 11,  7,  4,  9,  1, 10, 14,  3,  5, 12,  2, 15,  8,  6 },
00152           {  1,  4, 11, 13, 12,  3,  7, 14, 10, 15,  6,  8,  0,  5,  9,  2 },
00153           {  6, 11, 13,  8,  1,  4, 10,  7,  9,  5,  0, 15, 14,  2,  3, 12 }
00154         },
00155 
00156         { { 13,  2,  8,  4,  6, 15, 11,  1, 10,  9,  3, 14,  5,  0, 12,  7 },
00157           {  1, 15, 13,  8, 10,  3,  7,  4, 12,  5,  6, 11,  0, 14,  9,  2 },
00158           {  7, 11,  4,  1,  9, 12, 14,  2,  0,  6, 10, 13, 15,  3,  5,  8 },
00159           {  2,  1, 14,  7,  4, 10,  8, 13, 15, 12,  9,  0,  3,  5,  6, 11 }
00160         }
00161 };
00162 
00163 /*
00164  * This is the initial
00165  * permutation matrix
00166  */
00167 static const int initial_perm[64] = {
00168   58, 50, 42, 34, 26, 18, 10,  2, 60, 52, 44, 36, 28, 20, 12, 4,
00169   62, 54, 46, 38, 30, 22, 14,  6, 64, 56, 48, 40, 32, 24, 16, 8,
00170   57, 49, 41, 33, 25, 17,  9,  1, 59, 51, 43, 35, 27, 19, 11, 3,
00171   61, 53, 45, 37, 29, 21, 13,  5, 63, 55, 47, 39, 31, 23, 15, 7
00172 };
00173 
00174 /*
00175  * This is the final
00176  * permutation matrix
00177  */
00178 static const int final_perm[64] = {
00179   40,  8, 48, 16, 56, 24, 64, 32, 39,  7, 47, 15, 55, 23, 63, 31,
00180   38,  6, 46, 14, 54, 22, 62, 30, 37,  5, 45, 13, 53, 21, 61, 29,
00181   36,  4, 44, 12, 52, 20, 60, 28, 35,  3, 43, 11, 51, 19, 59, 27,
00182   34,  2, 42, 10, 50, 18, 58, 26, 33,  1, 41,  9, 49, 17, 57, 25
00183 };
00184 
00185 #define ascii_to_bin(c) ((c)>='a'?(c-59):(c)>='A'?((c)-53):(c)-'.')
00186 #define bin_to_ascii(c) (char)((c)>=38?((c)-38+'a'):(c)>=12?((c)-12+'A'):(c)+'.')
00187 
00188 static const ufc_long BITMASK[24] = {
00189   0x40000000, 0x20000000, 0x10000000, 0x08000000, 0x04000000, 0x02000000,
00190   0x01000000, 0x00800000, 0x00400000, 0x00200000, 0x00100000, 0x00080000,
00191   0x00004000, 0x00002000, 0x00001000, 0x00000800, 0x00000400, 0x00000200,
00192   0x00000100, 0x00000080, 0x00000040, 0x00000020, 0x00000010, 0x00000008
00193 };
00194 
00195 static const unsigned char bytemask[8]  = {
00196   0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01
00197 };
00198 
00199 static const ufc_long longmask[32] = {
00200   0x80000000, 0x40000000, 0x20000000, 0x10000000,
00201   0x08000000, 0x04000000, 0x02000000, 0x01000000,
00202   0x00800000, 0x00400000, 0x00200000, 0x00100000,
00203   0x00080000, 0x00040000, 0x00020000, 0x00010000,
00204   0x00008000, 0x00004000, 0x00002000, 0x00001000,
00205   0x00000800, 0x00000400, 0x00000200, 0x00000100,
00206   0x00000080, 0x00000040, 0x00000020, 0x00000010,
00207   0x00000008, 0x00000004, 0x00000002, 0x00000001
00208 };
00209 
00210 /*
00211  * do_pc1: permform pc1 permutation in the key schedule generation.
00212  *
00213  * The first   index is the byte number in the 8 byte ASCII key
00214  *  -  second    -      -    the two 28 bits halfs of the result
00215  *  -  third     -   selects the 7 bits actually used of each byte
00216  *
00217  * The result is kept with 28 bit per 32 bit with the 4 most significant
00218  * bits zero.
00219  */
00220 static ufc_long do_pc1[8][2][128];
00221 
00222 /*
00223  * do_pc2: permform pc2 permutation in the key schedule generation.
00224  *
00225  * The first   index is the septet number in the two 28 bit intermediate values
00226  *  -  second    -    -  -  septet values
00227  *
00228  * Knowledge of the structure of the pc2 permutation is used.
00229  *
00230  * The result is kept with 28 bit per 32 bit with the 4 most significant
00231  * bits zero.
00232  */
00233 static ufc_long do_pc2[8][128];
00234 
00235 /*
00236  * eperm32tab: do 32 bit permutation and E selection
00237  *
00238  * The first index is the byte number in the 32 bit value to be permuted
00239  *  -  second  -   is the value of this byte
00240  *  -  third   -   selects the two 32 bit values
00241  *
00242  * The table is used and generated internally in init_des to speed it up
00243  */
00244 static ufc_long eperm32tab[4][256][2];
00245 
00246 /*
00247  * efp: undo an extra e selection and do final
00248  *      permutation giving the DES result.
00249  *
00250  *      Invoked 6 bit a time on two 48 bit values
00251  *      giving two 32 bit longs.
00252  */
00253 static ufc_long efp[16][64][2];
00254 
00255 /*
00256  * For use by the old, non-reentrant routines
00257  * (crypt/encrypt/setkey)
00258  */
00259 struct crypt_data _ufc_foobar;
00260 
00261 #ifdef __GNU_LIBRARY__
00262 #include <libc-lock.h>
00263 
00264 __libc_lock_define_initialized (static, _ufc_tables_lock)
00265 #endif
00266 
00267 #ifdef DEBUG
00268 
00269 void
00270 _ufc_prbits(a, n)
00271      ufc_long *a;
00272      int n;
00273 {
00274   ufc_long i, j, t, tmp;
00275   n /= 8;
00276   for(i = 0; i < n; i++) {
00277     tmp=0;
00278     for(j = 0; j < 8; j++) {
00279       t=8*i+j;
00280       tmp|=(a[t/24] & BITMASK[t % 24])?bytemask[j]:0;
00281     }
00282     (void)printf("%02x ",tmp);
00283   }
00284   printf(" ");
00285 }
00286 
00287 static void
00288 _ufc_set_bits(v, b)
00289      ufc_long v;
00290      ufc_long *b;
00291 {
00292   ufc_long i;
00293   *b = 0;
00294   for(i = 0; i < 24; i++) {
00295     if(v & longmask[8 + i])
00296       *b |= BITMASK[i];
00297   }
00298 }
00299 
00300 #endif
00301 
00302 /* lookup a 6 bit value in sbox */
00303 
00304 #define s_lookup(i,s) sbox[(i)][(((s)>>4) & 0x2)|((s) & 0x1)][((s)>>1) & 0xf];
00305 
00306 /*
00307  * Initialize unit - may be invoked directly
00308  * by fcrypt users.
00309  */
00310 
00311 void
00312 __init_des_r(struct crypt_data *__data)
00313 {
00314   int comes_from_bit;
00315   int bit, sg;
00316   ufc_long j;
00317   ufc_long mask1, mask2;
00318   int e_inverse[64];
00319   static volatile int small_tables_initialized = 0;
00320 
00321 #ifdef _UFC_32_
00322   long32 *sb[4];
00323   sb[0] = (long32*)__data->sb0; sb[1] = (long32*)__data->sb1;
00324   sb[2] = (long32*)__data->sb2; sb[3] = (long32*)__data->sb3;
00325 #endif
00326 #ifdef _UFC_64_
00327   long64 *sb[4];
00328   sb[0] = (long64*)__data->sb0; sb[1] = (long64*)__data->sb1;
00329   sb[2] = (long64*)__data->sb2; sb[3] = (long64*)__data->sb3;
00330 #endif
00331 
00332   if(small_tables_initialized == 0) {
00333 #ifdef __GNU_LIBRARY__
00334     __libc_lock_lock (_ufc_tables_lock);
00335     if(small_tables_initialized)
00336       goto small_tables_done;
00337 #endif
00338 
00339     /*
00340      * Create the do_pc1 table used
00341      * to affect pc1 permutation
00342      * when generating keys
00343      */
00344     memset(do_pc1, 0, sizeof(do_pc1));
00345     for(bit = 0; bit < 56; bit++) {
00346       comes_from_bit  = pc1[bit] - 1;
00347       mask1 = bytemask[comes_from_bit % 8 + 1];
00348       mask2 = longmask[bit % 28 + 4];
00349       for(j = 0; j < 128; j++) {
00350     if(j & mask1)
00351       do_pc1[comes_from_bit / 8][bit / 28][j] |= mask2;
00352       }
00353     }
00354 
00355     /*
00356      * Create the do_pc2 table used
00357      * to affect pc2 permutation when
00358      * generating keys
00359      */
00360     memset(do_pc2, 0, sizeof(do_pc2));
00361     for(bit = 0; bit < 48; bit++) {
00362       comes_from_bit  = pc2[bit] - 1;
00363       mask1 = bytemask[comes_from_bit % 7 + 1];
00364       mask2 = BITMASK[bit % 24];
00365       for(j = 0; j < 128; j++) {
00366     if(j & mask1)
00367       do_pc2[comes_from_bit / 7][j] |= mask2;
00368       }
00369     }
00370 
00371     /*
00372      * Now generate the table used to do combined
00373      * 32 bit permutation and e expansion
00374      *
00375      * We use it because we have to permute 16384 32 bit
00376      * longs into 48 bit in order to initialize sb.
00377      *
00378      * Looping 48 rounds per permutation becomes
00379      * just too slow...
00380      *
00381      */
00382 
00383     memset(eperm32tab, 0, sizeof(eperm32tab));
00384     for(bit = 0; bit < 48; bit++) {
00385       ufc_long mask1,comes_from;
00386       comes_from = perm32[esel[bit]-1]-1;
00387       mask1      = bytemask[comes_from % 8];
00388       for(j = 256; j--;) {
00389     if(j & mask1)
00390       eperm32tab[comes_from / 8][j][bit / 24] |= BITMASK[bit % 24];
00391       }
00392     }
00393 
00394     /*
00395      * Create an inverse matrix for esel telling
00396      * where to plug out bits if undoing it
00397      */
00398     for(bit=48; bit--;) {
00399       e_inverse[esel[bit] - 1     ] = bit;
00400       e_inverse[esel[bit] - 1 + 32] = bit + 48;
00401     }
00402 
00403     /*
00404      * create efp: the matrix used to
00405      * undo the E expansion and effect final permutation
00406      */
00407     memset(efp, 0, sizeof efp);
00408     for(bit = 0; bit < 64; bit++) {
00409       int o_bit, o_long;
00410       ufc_long word_value, mask1, mask2;
00411       int comes_from_f_bit, comes_from_e_bit;
00412       int comes_from_word, bit_within_word;
00413 
00414       /* See where bit i belongs in the two 32 bit long's */
00415       o_long = bit / 32; /* 0..1  */
00416       o_bit  = bit % 32; /* 0..31 */
00417 
00418       /*
00419        * And find a bit in the e permutated value setting this bit.
00420        *
00421        * Note: the e selection may have selected the same bit several
00422        * times. By the initialization of e_inverse, we only look
00423        * for one specific instance.
00424        */
00425       comes_from_f_bit = final_perm[bit] - 1;         /* 0..63 */
00426       comes_from_e_bit = e_inverse[comes_from_f_bit]; /* 0..95 */
00427       comes_from_word  = comes_from_e_bit / 6;        /* 0..15 */
00428       bit_within_word  = comes_from_e_bit % 6;        /* 0..5  */
00429 
00430       mask1 = longmask[bit_within_word + 26];
00431       mask2 = longmask[o_bit];
00432 
00433       for(word_value = 64; word_value--;) {
00434     if(word_value & mask1)
00435       efp[comes_from_word][word_value][o_long] |= mask2;
00436       }
00437     }
00438     small_tables_initialized = 1;
00439 #ifdef __GNU_LIBRARY__
00440 small_tables_done:
00441     __libc_lock_unlock(_ufc_tables_lock);
00442 #endif
00443   }
00444 
00445   /*
00446    * Create the sb tables:
00447    *
00448    * For each 12 bit segment of an 48 bit intermediate
00449    * result, the sb table precomputes the two 4 bit
00450    * values of the sbox lookups done with the two 6
00451    * bit halves, shifts them to their proper place,
00452    * sends them through perm32 and finally E expands
00453    * them so that they are ready for the next
00454    * DES round.
00455    *
00456    */
00457 
00458   memset(__data->sb0, 0, sizeof(__data->sb0));
00459   memset(__data->sb1, 0, sizeof(__data->sb1));
00460   memset(__data->sb2, 0, sizeof(__data->sb2));
00461   memset(__data->sb3, 0, sizeof(__data->sb3));
00462 
00463   for(sg = 0; sg < 4; sg++) {
00464     int j1, j2;
00465     int s1, s2;
00466 
00467     for(j1 = 0; j1 < 64; j1++) {
00468       s1 = s_lookup(2 * sg, j1);
00469       for(j2 = 0; j2 < 64; j2++) {
00470     ufc_long to_permute, inx;
00471 
00472     s2         = s_lookup(2 * sg + 1, j2);
00473     to_permute = (((ufc_long)s1 << 4)  |
00474               (ufc_long)s2) << (24 - 8 * (ufc_long)sg);
00475 
00476 #ifdef _UFC_32_
00477     inx = ((j1 << 6)  | j2) << 1;
00478     sb[sg][inx  ]  = eperm32tab[0][(to_permute >> 24) & 0xff][0];
00479     sb[sg][inx+1]  = eperm32tab[0][(to_permute >> 24) & 0xff][1];
00480     sb[sg][inx  ] |= eperm32tab[1][(to_permute >> 16) & 0xff][0];
00481     sb[sg][inx+1] |= eperm32tab[1][(to_permute >> 16) & 0xff][1];
00482     sb[sg][inx  ] |= eperm32tab[2][(to_permute >>  8) & 0xff][0];
00483     sb[sg][inx+1] |= eperm32tab[2][(to_permute >>  8) & 0xff][1];
00484     sb[sg][inx  ] |= eperm32tab[3][(to_permute)       & 0xff][0];
00485     sb[sg][inx+1] |= eperm32tab[3][(to_permute)       & 0xff][1];
00486 #endif
00487 #ifdef _UFC_64_
00488     inx = ((j1 << 6)  | j2);
00489     sb[sg][inx]  =
00490       ((long64)eperm32tab[0][(to_permute >> 24) & 0xff][0] << 32) |
00491        (long64)eperm32tab[0][(to_permute >> 24) & 0xff][1];
00492     sb[sg][inx] |=
00493       ((long64)eperm32tab[1][(to_permute >> 16) & 0xff][0] << 32) |
00494        (long64)eperm32tab[1][(to_permute >> 16) & 0xff][1];
00495     sb[sg][inx] |=
00496       ((long64)eperm32tab[2][(to_permute >>  8) & 0xff][0] << 32) |
00497        (long64)eperm32tab[2][(to_permute >>  8) & 0xff][1];
00498     sb[sg][inx] |=
00499       ((long64)eperm32tab[3][(to_permute)       & 0xff][0] << 32) |
00500        (long64)eperm32tab[3][(to_permute)       & 0xff][1];
00501 #endif
00502       }
00503     }
00504   }
00505 
00506   __data->initialized++;
00507 }
00508 
00509 void
00510 __init_des()
00511 {
00512   __init_des_r(&_ufc_foobar);
00513 }
00514 
00515 /*
00516  * Process the elements of the sb table permuting the
00517  * bits swapped in the expansion by the current salt.
00518  */
00519 
00520 #ifdef _UFC_32_
00521 STATIC void
00522 shuffle_sb(long32 *k, ufc_long saltbits)
00523 {
00524   ufc_long j;
00525   long32 x;
00526   for(j=4096; j--;) {
00527     x = (k[0] ^ k[1]) & (long32)saltbits;
00528     *k++ ^= x;
00529     *k++ ^= x;
00530   }
00531 }
00532 #endif
00533 
00534 #ifdef _UFC_64_
00535 STATIC void
00536 shuffle_sb(k, saltbits)
00537      long64 *k;
00538      ufc_long saltbits;
00539 {
00540   ufc_long j;
00541   long64 x;
00542   for(j=4096; j--;) {
00543     x = ((*k >> 32) ^ *k) & (long64)saltbits;
00544     *k++ ^= (x << 32) | x;
00545   }
00546 }
00547 #endif
00548 
00549 /*
00550  * Setup the unit for a new salt
00551  * Hopefully we'll not see a new salt in each crypt call.
00552  */
00553 
00554 void
00555 _ufc_setup_salt_r(const char *s, struct crypt_data *__data)
00556 {
00557   ufc_long i, j, saltbits;
00558 
00559   if(__data->initialized == 0)
00560     __init_des_r(__data);
00561 
00562   if(s[0] == __data->current_salt[0] && s[1] == __data->current_salt[1])
00563     return;
00564   __data->current_salt[0] = s[0]; __data->current_salt[1] = s[1];
00565 
00566   /*
00567    * This is the only crypt change to DES:
00568    * entries are swapped in the expansion table
00569    * according to the bits set in the salt.
00570    */
00571   saltbits = 0;
00572   for(i = 0; i < 2; i++) {
00573     long c=ascii_to_bin(s[i]);
00574     for(j = 0; j < 6; j++) {
00575       if((c >> j) & 0x1)
00576     saltbits |= BITMASK[6 * i + j];
00577     }
00578   }
00579 
00580   /*
00581    * Permute the sb table values
00582    * to reflect the changed e
00583    * selection table
00584    */
00585 #ifdef _UFC_32_
00586 #define LONGG long32*
00587 #endif
00588 #ifdef _UFC_64_
00589 #define LONGG long64*
00590 #endif
00591 
00592   shuffle_sb((LONGG)__data->sb0, __data->current_saltbits ^ saltbits);
00593   shuffle_sb((LONGG)__data->sb1, __data->current_saltbits ^ saltbits);
00594   shuffle_sb((LONGG)__data->sb2, __data->current_saltbits ^ saltbits);
00595   shuffle_sb((LONGG)__data->sb3, __data->current_saltbits ^ saltbits);
00596 
00597   __data->current_saltbits = saltbits;
00598 }
00599 
00600 void
00601 _ufc_mk_keytab_r(char *key, struct crypt_data *__data)
00602 {
00603   ufc_long v1, v2, *k1;
00604   int i;
00605 #ifdef _UFC_32_
00606   long32 v, *k2;
00607   k2 = (long32*)__data->keysched;
00608 #endif
00609 #ifdef _UFC_64_
00610   long64 v, *k2;
00611   k2 = (long64*)__data->keysched;
00612 #endif
00613 
00614   v1 = v2 = 0; k1 = &do_pc1[0][0][0];
00615   for(i = 8; i--;) {
00616     v1 |= k1[*key   & 0x7f]; k1 += 128;
00617     v2 |= k1[*key++ & 0x7f]; k1 += 128;
00618   }
00619 
00620   for(i = 0; i < 16; i++) {
00621     k1 = &do_pc2[0][0];
00622 
00623     v1 = (v1 << rots[i]) | (v1 >> (28 - rots[i]));
00624     v  = k1[(v1 >> 21) & 0x7f]; k1 += 128;
00625     v |= k1[(v1 >> 14) & 0x7f]; k1 += 128;
00626     v |= k1[(v1 >>  7) & 0x7f]; k1 += 128;
00627     v |= k1[(v1      ) & 0x7f]; k1 += 128;
00628 
00629 #ifdef _UFC_32_
00630     *k2++ = (v | 0x00008000);
00631     v = 0;
00632 #endif
00633 #ifdef _UFC_64_
00634     v = (v << 32);
00635 #endif
00636 
00637     v2 = (v2 << rots[i]) | (v2 >> (28 - rots[i]));
00638     v |= k1[(v2 >> 21) & 0x7f]; k1 += 128;
00639     v |= k1[(v2 >> 14) & 0x7f]; k1 += 128;
00640     v |= k1[(v2 >>  7) & 0x7f]; k1 += 128;
00641     v |= k1[(v2      ) & 0x7f];
00642 
00643 #ifdef _UFC_32_
00644     *k2++ = (v | 0x00008000);
00645 #endif
00646 #ifdef _UFC_64_
00647     *k2++ = v | 0x0000800000008000l;
00648 #endif
00649   }
00650 
00651   __data->direction = 0;
00652 }
00653 
00654 /*
00655  * Undo an extra E selection and do final permutations
00656  */
00657 
00658 void
00659 _ufc_dofinalperm_r(ufc_long *res, struct crypt_data *__data)
00660 {
00661   ufc_long v1, v2, x;
00662   ufc_long l1,l2,r1,r2;
00663 
00664   l1 = res[0]; l2 = res[1];
00665   r1 = res[2]; r2 = res[3];
00666 
00667   x = (l1 ^ l2) & __data->current_saltbits; l1 ^= x; l2 ^= x;
00668   x = (r1 ^ r2) & __data->current_saltbits; r1 ^= x; r2 ^= x;
00669 
00670   v1=v2=0; l1 >>= 3; l2 >>= 3; r1 >>= 3; r2 >>= 3;
00671 
00672   v1 |= efp[15][ r2         & 0x3f][0]; v2 |= efp[15][ r2 & 0x3f][1];
00673   v1 |= efp[14][(r2 >>= 6)  & 0x3f][0]; v2 |= efp[14][ r2 & 0x3f][1];
00674   v1 |= efp[13][(r2 >>= 10) & 0x3f][0]; v2 |= efp[13][ r2 & 0x3f][1];
00675   v1 |= efp[12][(r2 >>= 6)  & 0x3f][0]; v2 |= efp[12][ r2 & 0x3f][1];
00676 
00677   v1 |= efp[11][ r1         & 0x3f][0]; v2 |= efp[11][ r1 & 0x3f][1];
00678   v1 |= efp[10][(r1 >>= 6)  & 0x3f][0]; v2 |= efp[10][ r1 & 0x3f][1];
00679   v1 |= efp[ 9][(r1 >>= 10) & 0x3f][0]; v2 |= efp[ 9][ r1 & 0x3f][1];
00680   v1 |= efp[ 8][(r1 >>= 6)  & 0x3f][0]; v2 |= efp[ 8][ r1 & 0x3f][1];
00681 
00682   v1 |= efp[ 7][ l2         & 0x3f][0]; v2 |= efp[ 7][ l2 & 0x3f][1];
00683   v1 |= efp[ 6][(l2 >>= 6)  & 0x3f][0]; v2 |= efp[ 6][ l2 & 0x3f][1];
00684   v1 |= efp[ 5][(l2 >>= 10) & 0x3f][0]; v2 |= efp[ 5][ l2 & 0x3f][1];
00685   v1 |= efp[ 4][(l2 >>= 6)  & 0x3f][0]; v2 |= efp[ 4][ l2 & 0x3f][1];
00686 
00687   v1 |= efp[ 3][ l1         & 0x3f][0]; v2 |= efp[ 3][ l1 & 0x3f][1];
00688   v1 |= efp[ 2][(l1 >>= 6)  & 0x3f][0]; v2 |= efp[ 2][ l1 & 0x3f][1];
00689   v1 |= efp[ 1][(l1 >>= 10) & 0x3f][0]; v2 |= efp[ 1][ l1 & 0x3f][1];
00690   v1 |= efp[ 0][(l1 >>= 6)  & 0x3f][0]; v2 |= efp[ 0][ l1 & 0x3f][1];
00691 
00692   res[0] = v1; res[1] = v2;
00693 }
00694 
00695 /*
00696  * crypt only: convert from 64 bit to 11 bit ASCII
00697  * prefixing with the salt
00698  */
00699 
00700 void
00701 _ufc_output_conversion_r(ufc_long v1, ufc_long v2, const char *salt, struct crypt_data *__data)
00702 {
00703   int i, s, shf;
00704 
00705   __data->crypt_3_buf[0] = salt[0];
00706   __data->crypt_3_buf[1] = salt[1] ? salt[1] : salt[0];
00707 
00708   for(i = 0; i < 5; i++) {
00709     shf = (26 - 6 * i); /* to cope with MSC compiler bug */
00710     __data->crypt_3_buf[i + 2] = bin_to_ascii((v1 >> shf) & 0x3f);
00711   }
00712 
00713   s  = (v2 & 0xf) << 2;
00714   v2 = (v2 >> 2) | ((v1 & 0x3) << 30);
00715 
00716   for(i = 5; i < 10; i++) {
00717     shf = (56 - 6 * i);
00718     __data->crypt_3_buf[i + 2] = bin_to_ascii((v2 >> shf) & 0x3f);
00719   }
00720 
00721   __data->crypt_3_buf[12] = bin_to_ascii(s);
00722   __data->crypt_3_buf[13] = 0;
00723 }
00724 
00725 
00726 /*
00727  * UNIX encrypt function. Takes a bitvector
00728  * represented by one byte per bit and
00729  * encrypt/decrypt according to edflag
00730  */
00731 
00732 void
00733 __encrypt_r(char *__block, int __edflag, struct crypt_data *__data)
00734 {
00735   ufc_long l1, l2, r1, r2, res[4];
00736   int i;
00737 #ifdef _UFC_32_
00738   long32 *kt;
00739   kt = (long32*)__data->keysched;
00740 #endif
00741 #ifdef _UFC_64_
00742   long64 *kt;
00743   kt = (long64*)__data->keysched;
00744 #endif
00745 
00746   /*
00747    * Undo any salt changes to E expansion
00748    */
00749   _ufc_setup_salt_r("..", __data);
00750 
00751   /*
00752    * Reverse key table if
00753    * changing operation (encrypt/decrypt)
00754    */
00755   if((__edflag == 0) != (__data->direction == 0)) {
00756     for(i = 0; i < 8; i++) {
00757 #ifdef _UFC_32_
00758       long32 x;
00759       x = kt[2 * (15-i)];
00760       kt[2 * (15-i)] = kt[2 * i];
00761       kt[2 * i] = x;
00762 
00763       x = kt[2 * (15-i) + 1];
00764       kt[2 * (15-i) + 1] = kt[2 * i + 1];
00765       kt[2 * i + 1] = x;
00766 #endif
00767 #ifdef _UFC_64_
00768       long64 x;
00769       x = kt[15-i];
00770       kt[15-i] = kt[i];
00771       kt[i] = x;
00772 #endif
00773       }
00774     __data->direction = __edflag;
00775   }
00776 
00777   /*
00778    * Do initial permutation + E expansion
00779    */
00780   i = 0;
00781   for(l1 = 0; i < 24; i++) {
00782     if(__block[initial_perm[esel[i]-1]-1])
00783       l1 |= BITMASK[i];
00784   }
00785   for(l2 = 0; i < 48; i++) {
00786     if(__block[initial_perm[esel[i]-1]-1])
00787       l2 |= BITMASK[i-24];
00788   }
00789 
00790   i = 0;
00791   for(r1 = 0; i < 24; i++) {
00792     if(__block[initial_perm[esel[i]-1+32]-1])
00793       r1 |= BITMASK[i];
00794   }
00795   for(r2 = 0; i < 48; i++) {
00796     if(__block[initial_perm[esel[i]-1+32]-1])
00797       r2 |= BITMASK[i-24];
00798   }
00799 
00800   /*
00801    * Do DES inner loops + final conversion
00802    */
00803   res[0] = l1; res[1] = l2;
00804   res[2] = r1; res[3] = r2;
00805   _ufc_doit_r((ufc_long)1, __data, &res[0]);
00806 
00807   /*
00808    * Do final permutations
00809    */
00810   _ufc_dofinalperm_r(res, __data);
00811 
00812   /*
00813    * And convert to bit array
00814    */
00815   l1 = res[0]; r1 = res[1];
00816   for(i = 0; i < 32; i++) {
00817     *__block++ = (l1 & longmask[i]) != 0;
00818   }
00819   for(i = 0; i < 32; i++) {
00820     *__block++ = (r1 & longmask[i]) != 0;
00821   }
00822 }
00823 //weak_alias (__encrypt_r, encrypt_r)
00824 
00825 void encrypt(char *__block, int __edflag)
00826 {
00827   __encrypt_r(__block, __edflag, &_ufc_foobar);
00828 }
00829 
00830 
00831 /*
00832  * UNIX setkey function. Take a 64 bit DES
00833  * key and setup the machinery.
00834  */
00835 
00836 void
00837 __setkey_r(const char *__key, struct crypt_data *__data)
00838 {
00839   int i,j;
00840   unsigned char c;
00841   unsigned char ktab[8];
00842 
00843   _ufc_setup_salt_r("..", __data); /* be sure we're initialized */
00844 
00845   for(i = 0; i < 8; i++) {
00846     for(j = 0, c = 0; j < 8; j++)
00847       c = c << 1 | *__key++;
00848     ktab[i] = c >> 1;
00849   }
00850   _ufc_mk_keytab_r((char *)ktab, __data);
00851 }
00852 
00853 void setkey(const char *__key)
00854 {
00855   __setkey_r(__key, &_ufc_foobar);
00856 }

Generated on Mon May 28 04:40:08 2007 for MUX by  doxygen 1.4.7