ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

crypt_des.c
Go to the documentation of this file.
00001 /*
00002  *  Copyright 2004 Hans Leidekker
00003  *  Copyright 2006 Mike McCormack
00004  *
00005  *  Based on DES.c from libcifs
00006  *
00007  *  Copyright (C) 2003, 2004 by Christopher R. Hertel
00008  *
00009  *  This library is free software; you can redistribute it and/or
00010  *  modify it under the terms of the GNU Lesser General Public
00011  *  License as published by the Free Software Foundation; either
00012  *  version 2.1 of the License, or (at your option) any later version.
00013  *
00014  *  This library is distributed in the hope that it will be useful,
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  *  Lesser General Public License for more details.
00018  *
00019  *  You should have received a copy of the GNU Lesser General Public
00020  *  License along with this library; if not, write to the Free Software
00021  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
00022  */
00023 
00024 #include <advapi32.h>
00025 
00026 static const unsigned char InitialPermuteMap[64] =
00027 {
00028     57, 49, 41, 33, 25, 17,  9, 1,
00029     59, 51, 43, 35, 27, 19, 11, 3,
00030     61, 53, 45, 37, 29, 21, 13, 5,
00031     63, 55, 47, 39, 31, 23, 15, 7,
00032     56, 48, 40, 32, 24, 16,  8, 0,
00033     58, 50, 42, 34, 26, 18, 10, 2,
00034     60, 52, 44, 36, 28, 20, 12, 4,
00035     62, 54, 46, 38, 30, 22, 14, 6
00036  };
00037 
00038 static const unsigned char KeyPermuteMap[56] =
00039 {
00040     49, 42, 35, 28, 21, 14,  7,  0,
00041     50, 43, 36, 29, 22, 15,  8,  1,
00042     51, 44, 37, 30, 23, 16,  9,  2,
00043     52, 45, 38, 31, 55, 48, 41, 34,
00044     27, 20, 13,  6, 54, 47, 40, 33,
00045     26, 19, 12,  5, 53, 46, 39, 32,
00046     25, 18, 11,  4, 24, 17, 10,  3,
00047 };
00048 
00049 static const unsigned char KeyRotation[16] =
00050     { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };
00051 
00052 static const unsigned char KeyCompression[48] =
00053 {
00054     13, 16, 10, 23,  0,  4,  2, 27,
00055     14,  5, 20,  9, 22, 18, 11,  3,
00056     25,  7, 15,  6, 26, 19, 12,  1,
00057     40, 51, 30, 36, 46, 54, 29, 39,
00058     50, 44, 32, 47, 43, 48, 38, 55,
00059     33, 52, 45, 41, 49, 35, 28, 31
00060 };
00061 
00062 static const unsigned char DataExpansion[48] =
00063 {
00064     31,  0,  1,  2,  3,  4,  3,  4,
00065      5,  6,  7,  8,  7,  8,  9, 10,
00066     11, 12, 11, 12, 13, 14, 15, 16,
00067     15, 16, 17, 18, 19, 20, 19, 20,
00068     21, 22, 23, 24, 23, 24, 25, 26,
00069     27, 28, 27, 28, 29, 30, 31,  0
00070 };
00071 
00072 static const unsigned char SBox[8][64] =
00073 {
00074     {  /* S0 */
00075         14,  0,  4, 15, 13,  7,  1,  4,  2, 14, 15,  2, 11, 13,  8,  1,
00076          3, 10, 10,  6,  6, 12, 12, 11,  5,  9,  9,  5,  0,  3,  7,  8,
00077          4, 15,  1, 12, 14,  8,  8,  2, 13,  4,  6,  9,  2,  1, 11,  7,
00078         15,  5, 12, 11,  9,  3,  7, 14,  3, 10, 10,  0,  5,  6,  0, 13
00079     },
00080     {  /* S1 */
00081         15,  3,  1, 13,  8,  4, 14,  7,  6, 15, 11,  2,  3,  8,  4, 14,
00082          9, 12,  7,  0,  2,  1, 13, 10, 12,  6,  0,  9,  5, 11, 10,  5,
00083          0, 13, 14,  8,  7, 10, 11,  1, 10,  3,  4, 15, 13,  4,  1,  2,
00084          5, 11,  8,  6, 12,  7,  6, 12,  9,  0,  3,  5,  2, 14, 15,  9
00085     },
00086     {  /* S2 */
00087         10, 13,  0,  7,  9,  0, 14,  9,  6,  3,  3,  4, 15,  6,  5, 10,
00088          1,  2, 13,  8, 12,  5,  7, 14, 11, 12,  4, 11,  2, 15,  8,  1,
00089         13,  1,  6, 10,  4, 13,  9,  0,  8,  6, 15,  9,  3,  8,  0,  7,
00090         11,  4,  1, 15,  2, 14, 12,  3,  5, 11, 10,  5, 14,  2,  7, 12
00091     },
00092     {  /* S3 */
00093          7, 13, 13,  8, 14, 11,  3,  5,  0,  6,  6, 15,  9,  0, 10,  3,
00094          1,  4,  2,  7,  8,  2,  5, 12, 11,  1, 12, 10,  4, 14, 15,  9,
00095         10,  3,  6, 15,  9,  0,  0,  6, 12, 10, 11,  1,  7, 13, 13,  8,
00096         15,  9,  1,  4,  3,  5, 14, 11,  5, 12,  2,  7,  8,  2,  4, 14
00097     },
00098     {  /* S4 */
00099          2, 14, 12, 11,  4,  2,  1, 12,  7,  4, 10,  7, 11, 13,  6,  1,
00100          8,  5,  5,  0,  3, 15, 15, 10, 13,  3,  0,  9, 14,  8,  9,  6,
00101          4, 11,  2,  8,  1, 12, 11,  7, 10,  1, 13, 14,  7,  2,  8, 13,
00102         15,  6,  9, 15, 12,  0,  5,  9,  6, 10,  3,  4,  0,  5, 14,  3
00103     },
00104     {  /* S5 */
00105         12, 10,  1, 15, 10,  4, 15,  2,  9,  7,  2, 12,  6,  9,  8,  5,
00106          0,  6, 13,  1,  3, 13,  4, 14, 14,  0,  7, 11,  5,  3, 11,  8,
00107          9,  4, 14,  3, 15,  2,  5, 12,  2,  9,  8,  5, 12, 15,  3, 10,
00108          7, 11,  0, 14,  4,  1, 10,  7,  1,  6, 13,  0, 11,  8,  6, 13
00109     },
00110     {  /* S6 */
00111          4, 13, 11,  0,  2, 11, 14,  7, 15,  4,  0,  9,  8,  1, 13, 10,
00112          3, 14, 12,  3,  9,  5,  7, 12,  5,  2, 10, 15,  6,  8,  1,  6,
00113          1,  6,  4, 11, 11, 13, 13,  8, 12,  1,  3,  4,  7, 10, 14,  7,
00114         10,  9, 15,  5,  6,  0,  8, 15,  0, 14,  5,  2,  9,  3,  2, 12
00115     },
00116     {  /* S7 */
00117         13,  1,  2, 15,  8, 13,  4,  8,  6, 10, 15,  3, 11,  7,  1,  4,
00118         10, 12,  9,  5,  3,  6, 14, 11,  5,  0,  0, 14, 12,  9,  7,  2,
00119          7,  2, 11,  1,  4, 14,  1,  7,  9,  4, 12, 10, 14,  8,  2, 13,
00120          0, 15,  6, 12, 10,  9, 13,  0, 15,  3,  3,  5,  5,  6,  8, 11
00121     }
00122 };
00123 
00124 static const unsigned char PBox[32] =
00125 {
00126     15,  6, 19, 20, 28, 11, 27, 16,
00127      0, 14, 22, 25,  4, 17, 30,  9,
00128      1,  7, 23, 13, 31, 26,  2,  8,
00129     18, 12, 29,  5, 21, 10,  3, 24
00130 };
00131 
00132 static const unsigned char FinalPermuteMap[64] =
00133 {
00134      7, 39, 15, 47, 23, 55, 31, 63,
00135      6, 38, 14, 46, 22, 54, 30, 62,
00136      5, 37, 13, 45, 21, 53, 29, 61,
00137      4, 36, 12, 44, 20, 52, 28, 60,
00138      3, 35, 11, 43, 19, 51, 27, 59,
00139      2, 34, 10, 42, 18, 50, 26, 58,
00140      1, 33,  9, 41, 17, 49, 25, 57,
00141      0, 32,  8, 40, 16, 48, 24, 56
00142 };
00143 
00144 #define CLRBIT( STR, IDX ) ( (STR)[(IDX)/8] &= ~(0x01 << (7 - ((IDX)%8))) )
00145 #define SETBIT( STR, IDX ) ( (STR)[(IDX)/8] |= (0x01 << (7 - ((IDX)%8))) )
00146 #define GETBIT( STR, IDX ) (( ((STR)[(IDX)/8]) >> (7 - ((IDX)%8)) ) & 0x01)
00147 
00148 static void Permute( unsigned char *dst, const unsigned char *src, const unsigned char *map, const int mapsize )
00149 {
00150     int bitcount, i;
00151 
00152     for (i = 0; i < mapsize; i++)
00153         dst[i] = 0;
00154 
00155     bitcount = mapsize * 8;
00156 
00157     for (i = 0; i < bitcount; i++)
00158     {
00159         if (GETBIT( src, map[i] ))
00160             SETBIT( dst, i );
00161     }
00162 }
00163 
00164 static void KeyShiftLeft( unsigned char *key, const int numbits )
00165 {
00166     int i;
00167     unsigned char keep = key[0];
00168 
00169     for (i = 0; i < numbits; i++)
00170     {
00171         int j;
00172 
00173         for (j = 0; j < 7; j++)
00174         {
00175             if (j && (key[j] & 0x80))
00176                 key[j-1] |=  0x01;
00177             key[j] <<= 1;
00178         }
00179 
00180         if (GETBIT( key, 27 ))
00181         {
00182             CLRBIT( key, 27 );
00183             SETBIT( key, 55 );
00184         }
00185 
00186         if (keep & 0x80)
00187             SETBIT( key, 27 );
00188 
00189         keep <<= 1;
00190     }
00191 }
00192 
00193 static void KeyShiftRight( unsigned char *key, const int numbits )
00194 {
00195     int i;
00196     unsigned char keep = key[6];
00197 
00198     for (i = 0; i < numbits; i++)
00199     {
00200         int j;
00201 
00202         for (j = 6; j >= 0; j--)
00203         {
00204             if (j!=6 && (key[j] & 0x01))
00205                 key[j+1] |=  0x80;
00206             key[j] >>= 1;
00207         }
00208 
00209         if (GETBIT( key, 28 ))
00210         {
00211             CLRBIT( key, 28 );
00212             SETBIT( key, 0 );
00213         }
00214 
00215         if (keep & 0x01)
00216             SETBIT( key, 28 );
00217 
00218         keep >>= 1;
00219     }
00220 }
00221 
00222 static void sbox( unsigned char *dst, const unsigned char *src )
00223 {
00224     int i;
00225 
00226     for (i = 0; i < 4; i++)
00227         dst[i] = 0;
00228 
00229     for (i = 0; i < 8; i++)
00230     {
00231         int j, Snum, bitnum;
00232 
00233         for (Snum = j = 0, bitnum = (i * 6); j < 6; j++, bitnum++)
00234         {
00235             Snum <<= 1;
00236             Snum |= GETBIT( src, bitnum );
00237         }
00238 
00239         if (0 == (i%2))
00240             dst[i/2] |= ((SBox[i][Snum]) << 4);
00241         else
00242             dst[i/2] |= SBox[i][Snum];
00243     }
00244 }
00245 
00246 static void xor( unsigned char *dst, const unsigned char *a, const unsigned char *b, const int count )
00247 {
00248     int i;
00249 
00250     for (i = 0; i < count; i++)
00251         dst[i] = a[i] ^ b[i];
00252 }
00253 
00254 unsigned char *CRYPT_DEShash( unsigned char *dst, const unsigned char *key, const unsigned char *src )
00255 {
00256     int i;
00257     unsigned char K[7];
00258     unsigned char D[8];
00259 
00260     Permute( K, key, KeyPermuteMap, 7 );
00261     Permute( D, src, InitialPermuteMap, 8 );
00262 
00263     for (i = 0; i < 16; i++)
00264     {
00265         int j;
00266         unsigned char *L = D;
00267         unsigned char *R = &(D[4]);
00268         unsigned char  Rexp[6];
00269         unsigned char  Rn[4];
00270         unsigned char  SubK[6];
00271 
00272         KeyShiftLeft( K, KeyRotation[i] );
00273         Permute( SubK, K, KeyCompression, 6 );
00274 
00275         Permute( Rexp, R, DataExpansion, 6 );
00276         xor( Rexp, Rexp, SubK, 6 );
00277 
00278         sbox( Rn, Rexp );
00279         Permute( Rexp, Rn, PBox, 4 );
00280         xor( Rn, L, Rexp, 4 );
00281 
00282         for (j = 0; j < 4; j++)
00283         {
00284             L[j] = R[j];
00285             R[j] = Rn[j];
00286         }
00287     }
00288 
00289     Permute( dst, D, FinalPermuteMap, 8 );
00290 
00291     return dst;
00292 }
00293 
00294 unsigned char *CRYPT_DESunhash( unsigned char *dst, const unsigned char *key, const unsigned char *src )
00295 {
00296     int i;
00297     unsigned char K[7];
00298     unsigned char D[8];
00299 
00300     Permute( K, key, KeyPermuteMap, 7 );
00301     Permute( D, src, InitialPermuteMap, 8 );
00302 
00303     for (i = 0; i < 16; i++)
00304     {
00305         int j;
00306         unsigned char *L = D;
00307         unsigned char *R = &(D[4]);
00308         unsigned char  Rexp[6];
00309         unsigned char  Rn[4];
00310         unsigned char  SubK[6];
00311 
00312         Permute( SubK, K, KeyCompression, 6 );
00313 
00314         Permute( Rexp, R, DataExpansion, 6 );
00315         xor( Rexp, Rexp, SubK, 6 );
00316 
00317         sbox( Rn, Rexp );
00318         Permute( Rexp, Rn, PBox, 4 );
00319         xor( Rn, L, Rexp, 4 );
00320 
00321         for (j = 0; j < 4; j++)
00322         {
00323             L[j] = R[j];
00324             R[j] = Rn[j];
00325         }
00326 
00327         KeyShiftRight( K, KeyRotation[15 - i] );
00328     }
00329 
00330     Permute( dst, D, FinalPermuteMap, 8 );
00331 
00332     return dst;
00333 }

Generated on Sun May 27 2012 04:22:38 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.