Pol  Revision:cb584c9
crypt.cpp
Go to the documentation of this file.
1 
2 #include "../../clib/passert.h"
3 #include "../uconst.h"
4 #include "blowfish.h"
5 #include "crypt.h"
6 #include "cryptbase.h"
7 #include "logincrypt.h"
8 #include "md5.h"
9 #include "twofish.h"
10 
11 // NOCRYPT
12 namespace Pol
13 {
14 namespace Crypt
15 {
17 
19 
20 int CCryptNoCrypt::Receive( void* buffer, int max_expected, SOCKET socket )
21 {
22  return recv( socket, (char*)buffer, max_expected, 0 );
23 }
24 
25 void CCryptNoCrypt::Init( void* pvSeed, int type )
26 {
27  // Nocrypt doesnt need to Initialize anything.
28  (void)pvSeed;
29  (void)type;
30 }
31 
32 // BLOWFISH
33 
35 
37 
38 CCryptBlowfish::CCryptBlowfish( unsigned int masterKey1, unsigned int masterKey2 )
39 {
40  SetMasterKeys( masterKey1, masterKey2 );
41 }
42 
43 int CCryptBlowfish::Receive( void* buffer, int max_expected, SOCKET socket )
44 {
45  passert_always( max_expected >= 0 );
46  passert_always( MAXBUFFER > max_expected );
47  int count = recv( socket, (char*)encrypted_data, max_expected, 0 );
48  if ( count > 0 )
49  {
50  passert( count <= max_expected );
51  Decrypt( encrypted_data, buffer, count );
52  }
53  return count;
54 }
55 
56 void CCryptBlowfish::SetMasterKeys( unsigned int masterKey1, unsigned int masterKey2 )
57 {
58  m_masterKey[0] = masterKey1 & 0xFFFFFFFF;
59  m_masterKey[1] = masterKey2 & 0xFFFFFFFF;
60 }
61 
62 void CCryptBlowfish::Init( void* pvSeed, int type )
63 {
64  unsigned char* pSeed = (unsigned char*)pvSeed;
65 
66  lcrypt.Init( pSeed, m_masterKey[0], m_masterKey[1] );
67  bfish.Init();
68 
69  m_type = type;
70 }
71 
72 void CCryptBlowfish::Decrypt( void* pvIn, void* pvOut, int len )
73 {
74  unsigned char* pIn = (unsigned char*)pvIn;
75  unsigned char* pOut = (unsigned char*)pvOut;
76 
78  {
79  if ( ( *pIn ^ (unsigned char)lcrypt.lkey[0] ) == CRYPT_AUTO_VALUE )
81  else
83  }
84 
86  {
87  lcrypt.Decrypt( pIn, pOut, len );
88  }
89  else
90  {
91  bfish.Decrypt( pIn, pOut, len );
92  }
93 }
94 
95 // BLOWFISH OLD
96 
98 
100 
101 CCryptBlowfishOld::CCryptBlowfishOld( unsigned int masterKey1, unsigned int masterKey2 )
102 {
103  SetMasterKeys( masterKey1, masterKey2 );
104 }
105 
106 void CCryptBlowfishOld::Decrypt( void* pvIn, void* pvOut, int len )
107 {
108  unsigned char* pIn = (unsigned char*)pvIn;
109  unsigned char* pOut = (unsigned char*)pvOut;
110 
111  if ( m_type == CCryptBase::typeAuto )
112  {
113  if ( ( *pIn ^ (unsigned char)lcrypt.lkey[0] ) == CRYPT_AUTO_VALUE )
115  else
117  }
118 
119  if ( m_type == CCryptBase::typeLogin )
120  {
121  lcrypt.Decrypt_Old( pIn, pOut, len );
122  }
123  else
124  {
125  bfish.Decrypt( pIn, pOut, len );
126  }
127 }
128 
129 // BLOWFISH 1.25.36
130 
132 
134 
135 CCrypt12536::CCrypt12536( unsigned int masterKey1, unsigned int masterKey2 )
136 {
137  SetMasterKeys( masterKey1, masterKey2 );
138 }
139 
140 void CCrypt12536::Decrypt( void* pvIn, void* pvOut, int len )
141 {
142  unsigned char* pIn = (unsigned char*)pvIn;
143  unsigned char* pOut = (unsigned char*)pvOut;
144 
145  if ( m_type == CCryptBase::typeAuto )
146  {
147  if ( ( *pIn ^ (unsigned char)lcrypt.lkey[0] ) == CRYPT_AUTO_VALUE )
149  else
151  }
152 
153  if ( m_type == CCryptBase::typeLogin )
154  {
155  lcrypt.Decrypt_1_25_36( pIn, pOut, len );
156  }
157  else
158  {
159  bfish.Decrypt( pIn, pOut, len );
160  }
161 }
162 
163 // BLOWFISH + TWOFISH
164 
166 
168 
169 CCryptBlowfishTwofish::CCryptBlowfishTwofish( unsigned int masterKey1, unsigned int masterKey2 )
170 {
171  SetMasterKeys( masterKey1, masterKey2 );
172 }
173 
174 int CCryptBlowfishTwofish::Receive( void* buffer, int max_expected, SOCKET socket )
175 {
176  passert_always( max_expected >= 0 );
177  passert_always( MAXBUFFER > max_expected );
178  int count = recv( socket, (char*)encrypted_data, max_expected, 0 );
179  if ( count > 0 )
180  {
181  passert( count <= max_expected );
182  Decrypt( encrypted_data, buffer, count );
183  }
184  return count;
185 }
186 
187 void CCryptBlowfishTwofish::SetMasterKeys( unsigned int masterKey1, unsigned int masterKey2 )
188 {
189  m_masterKey[0] = masterKey1 & 0xFFFFFFFF;
190  m_masterKey[1] = masterKey2 & 0xFFFFFFFF;
191 }
192 
193 void CCryptBlowfishTwofish::Init( void* pvSeed, int type )
194 {
195  unsigned char* pSeed = (unsigned char*)pvSeed;
196 
197  lcrypt.Init( pSeed, m_masterKey[0], m_masterKey[1] );
198  tfish.Init( pSeed );
199  bfish.Init();
200 
201  m_type = type;
202 }
203 
204 void CCryptBlowfishTwofish::Decrypt( void* pvIn, void* pvOut, int len )
205 {
206  unsigned char* pIn = (unsigned char*)pvIn;
207  unsigned char* pOut = (unsigned char*)pvOut;
208 
209  if ( m_type == CCryptBase::typeAuto )
210  {
211  if ( ( *pIn ^ (unsigned char)lcrypt.lkey[0] ) == CRYPT_AUTO_VALUE )
213  else
215  }
216 
217  if ( m_type == CCryptBase::typeLogin )
218  {
219  lcrypt.Decrypt( pIn, pOut, len );
220  }
221  else
222  {
223  tfish.Decrypt( pIn, pOut, len );
224  bfish.Decrypt( pOut, pOut, len );
225  }
226 }
227 
228 // TWOFISH
229 
231 
233 
234 CCryptTwofish::CCryptTwofish( unsigned int masterKey1, unsigned int masterKey2 )
235 {
236  SetMasterKeys( masterKey1, masterKey2 );
237 }
238 
239 int CCryptTwofish::Receive( void* buffer, int max_expected, SOCKET socket )
240 {
241  passert_always( max_expected >= 0 );
242  passert_always( MAXBUFFER > max_expected );
243  int count = recv( socket, (char*)encrypted_data, max_expected, 0 );
244  if ( count > 0 )
245  {
246  passert( count <= max_expected );
247  Decrypt( encrypted_data, buffer, count );
248  }
249  return count;
250 }
251 
252 void CCryptTwofish::SetMasterKeys( unsigned int masterKey1, unsigned int masterKey2 )
253 {
254  m_masterKey[0] = masterKey1 & 0xFFFFFFFF;
255  m_masterKey[1] = masterKey2 & 0xFFFFFFFF;
256 }
257 
258 void CCryptTwofish::Init( void* pvSeed, int type )
259 {
260  unsigned char* pSeed = (unsigned char*)pvSeed;
261 
262  lcrypt.Init( pSeed, m_masterKey[0], m_masterKey[1] );
263  tfish.Init( pSeed );
264  md5.Init( tfish.subData3, 256 );
265 
266  m_type = type;
267 }
268 
269 void CCryptTwofish::Encrypt( void* pvIn, void* pvOut, int len )
270 {
271  unsigned char* pIn = (unsigned char*)pvIn;
272  unsigned char* pOut = (unsigned char*)pvOut;
273 
274  md5.Encrypt( pIn, pOut, len );
275 }
276 
277 void CCryptTwofish::Decrypt( void* pvIn, void* pvOut, int len )
278 {
279  unsigned char* pIn = (unsigned char*)pvIn;
280  unsigned char* pOut = (unsigned char*)pvOut;
281 
282  if ( m_type == CCryptBase::typeAuto )
283  {
284  if ( ( *pIn ^ (unsigned char)lcrypt.lkey[0] ) == CRYPT_AUTO_VALUE )
286  else
288  }
289 
290  if ( m_type == CCryptBase::typeLogin )
291  {
292  lcrypt.Decrypt( pIn, pOut, len );
293  }
294  else
295  {
296  tfish.Decrypt( pIn, pOut, len );
297  }
298 }
299 }
300 }
virtual void SetMasterKeys(unsigned int masterKey1, unsigned int masterKey2) POL_OVERRIDE
Definition: crypt.cpp:56
virtual void Init(void *pvSeed, int type=CCryptBase::typeAuto) POL_OVERRIDE
Definition: crypt.cpp:25
#define CRYPT_AUTO_VALUE
Definition: cryptbase.h:44
virtual void Encrypt(void *pvIn, void *pvOut, int len) POL_OVERRIDE
Definition: crypt.cpp:269
void Decrypt_Old(unsigned char *in, unsigned char *out, int len)
Definition: logincrypt.cpp:51
virtual int Receive(void *buffer, int max_expected, SOCKET socket) POL_OVERRIDE
Definition: crypt.cpp:174
virtual void Decrypt(void *pvIn, void *pvOut, int len) POL_OVERRIDE
Definition: crypt.cpp:72
int SOCKET
Definition: wnsckt.h:10
unsigned int m_masterKey[2]
Definition: cryptbase.h:123
unsigned char subData3[256]
Definition: twofish.h:49
virtual int Receive(void *buffer, int max_expected, SOCKET socket) POL_OVERRIDE
Definition: crypt.cpp:20
virtual ~CCryptNoCrypt()
Definition: crypt.cpp:18
virtual void SetMasterKeys(unsigned int masterKey1, unsigned int masterKey2) POL_OVERRIDE
Definition: crypt.cpp:187
virtual void Decrypt(void *pvIn, void *pvOut, int len) POL_OVERRIDE
Definition: crypt.cpp:277
virtual int Receive(void *buffer, int max_expected, SOCKET socket) POL_OVERRIDE
Definition: crypt.cpp:239
void Encrypt(unsigned char *in, unsigned char *out, int len)
Definition: md5.cpp:97
virtual void SetMasterKeys(unsigned int masterKey1, unsigned int masterKey2) POL_OVERRIDE
Definition: crypt.cpp:252
unsigned char buffer[10000]
Definition: UoToolMain.cpp:109
virtual ~CCryptTwofish()
Definition: crypt.cpp:232
virtual void Init(void *pvSeed, int type=CCryptBase::typeAuto) POL_OVERRIDE
Definition: crypt.cpp:62
#define MAXBUFFER
Definition: uconst.h:88
virtual void Decrypt(void *pvIn, void *pvOut, int len) POL_OVERRIDE
Definition: crypt.cpp:106
unsigned char encrypted_data[MAXBUFFER]
Definition: cryptbase.h:124
virtual int Receive(void *buffer, int max_expected, SOCKET socket) POL_OVERRIDE
Definition: crypt.cpp:43
#define passert(exp)
Definition: passert.h:62
void Init(unsigned char *lseed, unsigned int k1, unsigned int k2)
Definition: logincrypt.cpp:22
virtual void Init(void *pvSeed, int type=CCryptBase::typeAuto) POL_OVERRIDE
Definition: crypt.cpp:258
void Init(unsigned char *Data, unsigned int Size)
Definition: md5.cpp:86
unsigned int lkey[2]
Definition: logincrypt.h:28
virtual void Decrypt(void *pvIn, void *pvOut, int len) POL_OVERRIDE
Definition: crypt.cpp:204
virtual void Init(void *pvSeed, int type=CCryptBase::typeAuto) POL_OVERRIDE
Definition: crypt.cpp:193
virtual void Decrypt(void *pvIn, void *pvOut, int len) POL_OVERRIDE
Definition: crypt.cpp:140
void Decrypt(unsigned char *in, unsigned char *out, int len)
Definition: twofish.cpp:94
virtual ~CCryptBlowfish()
Definition: crypt.cpp:36
virtual ~CCrypt12536()
Definition: crypt.cpp:133
#define passert_always(exp)
Definition: passert.h:80
void Decrypt(unsigned char *in, unsigned char *out, int len)
Definition: logincrypt.cpp:34
void Decrypt_1_25_36(unsigned char *in, unsigned char *out, int len)
Definition: logincrypt.cpp:66
Definition: berror.cpp:12
void Init(unsigned char *gseed)
Definition: twofish.cpp:63
void Decrypt(unsigned char *in, unsigned char *out, int len)
Definition: blowfish.cpp:290