lib/ufc.c

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

Sambaに対してSat Aug 29 21:23:00 2009に生成されました。  doxygen 1.4.7