Pol  Revision:cb584c9
md5.cpp
Go to the documentation of this file.
1 #include <string.h>
2 
3 #include "md5.h"
4 
5 namespace Pol
6 {
7 namespace Crypt
8 {
9 // MD5 Definitions
10 
11 #define T_MASK ( (unsigned int)~0 )
12 #define T1 ( T_MASK ^ 0x28955b87 )
13 #define T2 ( T_MASK ^ 0x173848a9 )
14 #define T3 0x242070db
15 #define T4 ( T_MASK ^ 0x3e423111 )
16 #define T5 ( T_MASK ^ 0x0a83f050 )
17 #define T6 0x4787c62a
18 #define T7 ( T_MASK ^ 0x57cfb9ec )
19 #define T8 ( T_MASK ^ 0x02b96afe )
20 #define T9 0x698098d8
21 #define T10 ( T_MASK ^ 0x74bb0850 )
22 #define T11 ( T_MASK ^ 0x0000a44e )
23 #define T12 ( T_MASK ^ 0x76a32841 )
24 #define T13 0x6b901122
25 #define T14 ( T_MASK ^ 0x02678e6c )
26 #define T15 ( T_MASK ^ 0x5986bc71 )
27 #define T16 0x49b40821
28 #define T17 ( T_MASK ^ 0x09e1da9d )
29 #define T18 ( T_MASK ^ 0x3fbf4cbf )
30 #define T19 0x265e5a51
31 #define T20 ( T_MASK ^ 0x16493855 )
32 #define T21 ( T_MASK ^ 0x29d0efa2 )
33 #define T22 0x02441453
34 #define T23 ( T_MASK ^ 0x275e197e )
35 #define T24 ( T_MASK ^ 0x182c0437 )
36 #define T25 0x21e1cde6
37 #define T26 ( T_MASK ^ 0x3cc8f829 )
38 #define T27 ( T_MASK ^ 0x0b2af278 )
39 #define T28 0x455a14ed
40 #define T29 ( T_MASK ^ 0x561c16fa )
41 #define T30 ( T_MASK ^ 0x03105c07 )
42 #define T31 0x676f02d9
43 #define T32 ( T_MASK ^ 0x72d5b375 )
44 #define T33 ( T_MASK ^ 0x0005c6bd )
45 #define T34 ( T_MASK ^ 0x788e097e )
46 #define T35 0x6d9d6122
47 #define T36 ( T_MASK ^ 0x021ac7f3 )
48 #define T37 ( T_MASK ^ 0x5b4115bb )
49 #define T38 0x4bdecfa9
50 #define T39 ( T_MASK ^ 0x0944b49f )
51 #define T40 ( T_MASK ^ 0x4140438f )
52 #define T41 0x289b7ec6
53 #define T42 ( T_MASK ^ 0x155ed805 )
54 #define T43 ( T_MASK ^ 0x2b10cf7a )
55 #define T44 0x04881d05
56 #define T45 ( T_MASK ^ 0x262b2fc6 )
57 #define T46 ( T_MASK ^ 0x1924661a )
58 #define T47 0x1fa27cf8
59 #define T48 ( T_MASK ^ 0x3b53a99a )
60 #define T49 ( T_MASK ^ 0x0bd6ddbb )
61 #define T50 0x432aff97
62 #define T51 ( T_MASK ^ 0x546bdc58 )
63 #define T52 ( T_MASK ^ 0x036c5fc6 )
64 #define T53 0x655b59c3
65 #define T54 ( T_MASK ^ 0x70f3336d )
66 #define T55 ( T_MASK ^ 0x00100b82 )
67 #define T56 ( T_MASK ^ 0x7a7ba22e )
68 #define T57 0x6fa87e4f
69 #define T58 ( T_MASK ^ 0x01d3191f )
70 #define T59 ( T_MASK ^ 0x5cfebceb )
71 #define T60 0x4e0811a1
72 #define T61 ( T_MASK ^ 0x08ac817d )
73 #define T62 ( T_MASK ^ 0x42c50dca )
74 #define T63 0x2ad7d2bb
75 #define T64 ( T_MASK ^ 0x14792c6e )
76 
77 // Constructor / Destructor
78 MD5Crypt::MD5Crypt() : TableIdx( 0 )
79 {
80  memset( &Digest, 0, sizeof( Digest ) );
81 }
83 
84 // Public Member Functions
85 
86 void MD5Crypt::Init( unsigned char* Data, unsigned int Size )
87 {
88  md5_state tmpMd5;
89 
90  TableIdx = 0;
91 
92  start( &tmpMd5 );
93  append( &tmpMd5, (const unsigned char*)Data, Size );
94  finish( &tmpMd5, Digest );
95 }
96 
97 void MD5Crypt::Encrypt( unsigned char* in, unsigned char* out, int len )
98 {
99  for ( int i = 0; i < len; i++ )
100  out[i] = in[i] ^ Digest[TableIdx++ % 16];
101 }
102 
103 // Protected Member Functions
104 
105 void MD5Crypt::process( md5_state* pms, const unsigned char* data )
106 {
107  unsigned int a = pms->abcd[0], b = pms->abcd[1], c = pms->abcd[2], d = pms->abcd[3];
108  unsigned int t = 0;
109  unsigned int X[16];
110  const unsigned char* xp = data;
111 
112  for ( int i = 0; i < 16; ++i, xp += 4 )
113  X[i] = xp[0] + ( xp[1] << 8 ) + ( xp[2] << 16 ) + ( xp[3] << 24 );
114 
115 #define ROTATE_LEFT( x, n ) ( ( ( x ) << ( n ) ) | ( ( x ) >> ( 32 - ( n ) ) ) )
116 
117 // Round 1
118 // Let [abcd k s i] denote the operation
119 // a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s)
120 #define F( x, y, z ) ( ( ( x ) & ( y ) ) | ( ~( x ) & ( z ) ) )
121 #define SET( a, b, c, d, k, s, Ti ) \
122  t = a + F( b, c, d ) + X[k] + Ti; \
123  a = ROTATE_LEFT( t, s ) + b
124  // Do the following 16 operations
125  SET( a, b, c, d, 0, 7, T1 );
126  SET( d, a, b, c, 1, 12, T2 );
127  SET( c, d, a, b, 2, 17, T3 );
128  SET( b, c, d, a, 3, 22, T4 );
129  SET( a, b, c, d, 4, 7, T5 );
130  SET( d, a, b, c, 5, 12, T6 );
131  SET( c, d, a, b, 6, 17, T7 );
132  SET( b, c, d, a, 7, 22, T8 );
133  SET( a, b, c, d, 8, 7, T9 );
134  SET( d, a, b, c, 9, 12, T10 );
135  SET( c, d, a, b, 10, 17, T11 );
136  SET( b, c, d, a, 11, 22, T12 );
137  SET( a, b, c, d, 12, 7, T13 );
138  SET( d, a, b, c, 13, 12, T14 );
139  SET( c, d, a, b, 14, 17, T15 );
140  SET( b, c, d, a, 15, 22, T16 );
141 #undef SET
142 
143 // Round 2
144 // Let [abcd k s i] denote the operation
145 // a = b + ((a + G(b,c,d) + X[k] + T[i]) <<< s)
146 #define G( x, y, z ) ( ( ( x ) & ( z ) ) | ( ( y ) & ~( z ) ) )
147 #define SET( a, b, c, d, k, s, Ti ) \
148  t = a + G( b, c, d ) + X[k] + Ti; \
149  a = ROTATE_LEFT( t, s ) + b
150  // Do the following 16 operations
151  SET( a, b, c, d, 1, 5, T17 );
152  SET( d, a, b, c, 6, 9, T18 );
153  SET( c, d, a, b, 11, 14, T19 );
154  SET( b, c, d, a, 0, 20, T20 );
155  SET( a, b, c, d, 5, 5, T21 );
156  SET( d, a, b, c, 10, 9, T22 );
157  SET( c, d, a, b, 15, 14, T23 );
158  SET( b, c, d, a, 4, 20, T24 );
159  SET( a, b, c, d, 9, 5, T25 );
160  SET( d, a, b, c, 14, 9, T26 );
161  SET( c, d, a, b, 3, 14, T27 );
162  SET( b, c, d, a, 8, 20, T28 );
163  SET( a, b, c, d, 13, 5, T29 );
164  SET( d, a, b, c, 2, 9, T30 );
165  SET( c, d, a, b, 7, 14, T31 );
166  SET( b, c, d, a, 12, 20, T32 );
167 #undef SET
168 
169 // Round 3
170 // Let [abcd k s t] denote the operation
171 // a = b + ((a + H(b,c,d) + X[k] + T[i]) <<< s)
172 #define H( x, y, z ) ( ( x ) ^ ( y ) ^ ( z ) )
173 #define SET( a, b, c, d, k, s, Ti ) \
174  t = a + H( b, c, d ) + X[k] + Ti; \
175  a = ROTATE_LEFT( t, s ) + b
176  // Do the following 16 operations
177  SET( a, b, c, d, 5, 4, T33 );
178  SET( d, a, b, c, 8, 11, T34 );
179  SET( c, d, a, b, 11, 16, T35 );
180  SET( b, c, d, a, 14, 23, T36 );
181  SET( a, b, c, d, 1, 4, T37 );
182  SET( d, a, b, c, 4, 11, T38 );
183  SET( c, d, a, b, 7, 16, T39 );
184  SET( b, c, d, a, 10, 23, T40 );
185  SET( a, b, c, d, 13, 4, T41 );
186  SET( d, a, b, c, 0, 11, T42 );
187  SET( c, d, a, b, 3, 16, T43 );
188  SET( b, c, d, a, 6, 23, T44 );
189  SET( a, b, c, d, 9, 4, T45 );
190  SET( d, a, b, c, 12, 11, T46 );
191  SET( c, d, a, b, 15, 16, T47 );
192  SET( b, c, d, a, 2, 23, T48 );
193 #undef SET
194 
195 // Round 4
196 // Let [abcd k s t] denote the operation
197 // a = b + ((a + I(b,c,d) + X[k] + T[i]) <<< s)
198 #define I( x, y, z ) ( ( y ) ^ ( ( x ) | ~( z ) ) )
199 #define SET( a, b, c, d, k, s, Ti ) \
200  t = a + I( b, c, d ) + X[k] + Ti; \
201  a = ROTATE_LEFT( t, s ) + b
202  // Do the following 16 operations
203  SET( a, b, c, d, 0, 6, T49 );
204  SET( d, a, b, c, 7, 10, T50 );
205  SET( c, d, a, b, 14, 15, T51 );
206  SET( b, c, d, a, 5, 21, T52 );
207  SET( a, b, c, d, 12, 6, T53 );
208  SET( d, a, b, c, 3, 10, T54 );
209  SET( c, d, a, b, 10, 15, T55 );
210  SET( b, c, d, a, 1, 21, T56 );
211  SET( a, b, c, d, 8, 6, T57 );
212  SET( d, a, b, c, 15, 10, T58 );
213  SET( c, d, a, b, 6, 15, T59 );
214  SET( b, c, d, a, 13, 21, T60 );
215  SET( a, b, c, d, 4, 6, T61 );
216  SET( d, a, b, c, 11, 10, T62 );
217  SET( c, d, a, b, 2, 15, T63 );
218  SET( b, c, d, a, 9, 21, T64 );
219 #undef SET
220 
221  // Then perform the following additions (That is increment each
222  // of the four registers by the value it had before this block
223  // was started)
224  pms->abcd[0] += a;
225  pms->abcd[1] += b;
226  pms->abcd[2] += c;
227  pms->abcd[3] += d;
228 }
229 
231 {
232  pms->count[0] = pms->count[1] = 0;
233  pms->abcd[0] = 0x67452301;
234  pms->abcd[1] = T_MASK ^ 0x10325476;
235  pms->abcd[2] = T_MASK ^ 0x67452301;
236  pms->abcd[3] = 0x10325476;
237 }
238 
239 void MD5Crypt::append( md5_state* pms, const unsigned char* data, int nbytes )
240 {
241  const unsigned char* p = data;
242  unsigned int nbits = (unsigned int)( nbytes << 3 );
243  int left = nbytes, offset = ( pms->count[0] >> 3 ) & 63;
244 
245  if ( nbytes <= 0 )
246  return;
247 
248  // Update the message length
249  pms->count[1] += nbytes >> 29;
250  pms->count[0] += nbits;
251  if ( pms->count[0] < nbits )
252  pms->count[1]++;
253 
254  // Process an initial partial block
255  if ( offset )
256  {
257  int copy = ( offset + nbytes > 64 ? 64 - offset : nbytes );
258  memcpy( pms->buf + offset, p, copy );
259 
260  if ( offset + copy < 64 )
261  return;
262 
263  p += copy;
264  left -= copy;
265  process( pms, pms->buf );
266  }
267 
268  // Process full blocks
269  for ( ; left >= 64; p += 64, left -= 64 )
270  process( pms, p );
271 
272  // Process a final partial block
273  if ( left )
274  {
275  if ( left > 63 )
276  left = 63;
277  memcpy( pms->buf, p, left );
278  }
279 }
280 
281 void MD5Crypt::finish( md5_state* pms, unsigned char digest[16] )
282 {
283  static const unsigned char pad[64] = {0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
284  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
285  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
286  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
287 
288  unsigned char data[8];
289 
290  // Save the length before padding
291  for ( int i = 0; i < 8; ++i )
292  data[i] = (unsigned char)( pms->count[i >> 2] >> ( ( i & 3 ) << 3 ) );
293 
294  // Pad to 56 bytes mod 64
295  append( pms, pad, ( ( 55 - ( pms->count[0] >> 3 ) ) & 63 ) + 1 );
296 
297  // Append the length
298  append( pms, data, 8 );
299  for ( int i = 0; i < 16; ++i )
300  digest[i] = (unsigned char)( pms->abcd[i >> 2] >> ( ( i & 3 ) << 3 ) );
301 }
302 }
303 }
#define T48
Definition: md5.cpp:59
unsigned int count[2]
Definition: md5.h:17
#define T25
Definition: md5.cpp:36
#define T19
Definition: md5.cpp:30
#define T43
Definition: md5.cpp:54
#define T7
Definition: md5.cpp:18
#define T13
Definition: md5.cpp:24
static void finish(md5_state *pms, unsigned char digest[16])
Definition: md5.cpp:281
#define T34
Definition: md5.cpp:45
#define T54
Definition: md5.cpp:65
#define T63
Definition: md5.cpp:74
#define T50
Definition: md5.cpp:61
static void start(md5_state *pms)
Definition: md5.cpp:230
#define T39
Definition: md5.cpp:50
#define T44
Definition: md5.cpp:55
#define T45
Definition: md5.cpp:56
#define T58
Definition: md5.cpp:69
#define T20
Definition: md5.cpp:31
#define T32
Definition: md5.cpp:43
#define T55
Definition: md5.cpp:66
#define SET(a, b, c, d, k, s, Ti)
#define T38
Definition: md5.cpp:49
#define T15
Definition: md5.cpp:26
#define T10
Definition: md5.cpp:21
#define T22
Definition: md5.cpp:33
void Encrypt(unsigned char *in, unsigned char *out, int len)
Definition: md5.cpp:97
#define T30
Definition: md5.cpp:41
#define T64
Definition: md5.cpp:75
#define T5
Definition: md5.cpp:16
#define T2
Definition: md5.cpp:13
#define T24
Definition: md5.cpp:35
#define T26
Definition: md5.cpp:37
#define T1
Definition: md5.cpp:12
#define T3
Definition: md5.cpp:14
#define T31
Definition: md5.cpp:42
#define T6
Definition: md5.cpp:17
#define T17
Definition: md5.cpp:28
#define T29
Definition: md5.cpp:40
#define T9
Definition: md5.cpp:20
#define T4
Definition: md5.cpp:15
#define T11
Definition: md5.cpp:22
#define T53
Definition: md5.cpp:64
#define T62
Definition: md5.cpp:73
#define T35
Definition: md5.cpp:46
static void process(md5_state *pms, const unsigned char *data)
Definition: md5.cpp:105
#define T14
Definition: md5.cpp:25
#define T46
Definition: md5.cpp:57
#define T28
Definition: md5.cpp:39
#define T41
Definition: md5.cpp:52
std::unordered_map< u64, ScriptDiffData > data
Definition: osmod.cpp:966
#define T42
Definition: md5.cpp:53
static void append(md5_state *pms, const unsigned char *data, int nbytes)
Definition: md5.cpp:239
#define T37
Definition: md5.cpp:48
unsigned int abcd[4]
Definition: md5.h:18
unsigned char Digest[16]
Definition: md5.h:42
#define T40
Definition: md5.cpp:51
#define T12
Definition: md5.cpp:23
#define T27
Definition: md5.cpp:38
#define T52
Definition: md5.cpp:63
unsigned char buf[64]
Definition: md5.h:19
void Init(unsigned char *Data, unsigned int Size)
Definition: md5.cpp:86
#define T21
Definition: md5.cpp:32
#define T_MASK
Definition: md5.cpp:11
#define T57
Definition: md5.cpp:68
#define T56
Definition: md5.cpp:67
#define T8
Definition: md5.cpp:19
#define T59
Definition: md5.cpp:70
#define T51
Definition: md5.cpp:62
#define T60
Definition: md5.cpp:71
#define T23
Definition: md5.cpp:34
#define T18
Definition: md5.cpp:29
#define T33
Definition: md5.cpp:44
unsigned int TableIdx
Definition: md5.h:41
#define T16
Definition: md5.cpp:27
#define T47
Definition: md5.cpp:58
#define T49
Definition: md5.cpp:60
#define T36
Definition: md5.cpp:47
Definition: berror.cpp:12
#define T61
Definition: md5.cpp:72