1 #include "rar.hpp"
2
3 #ifndef SFX_MODULE
4 extern uint CRCTab[256];
5 #endif
6
7 #define NROUNDS 32
8
9 #define rol(x,n,xsize) (((x)<<(n)) | ((x)>>(xsize-(n))))
10 #define ror(x,n,xsize) (((x)>>(n)) | ((x)<<(xsize-(n))))
11
12 #define substLong(t) ( (uint)SubstTable[(uint)t&255] | \
13 ((uint)SubstTable[(int)(t>> 8)&255]<< 8) | \
14 ((uint)SubstTable[(int)(t>>16)&255]<<16) | \
15 ((uint)SubstTable[(int)(t>>24)&255]<<24) )
16
17 CryptKeyCacheItem CryptData::Cache[4];
18 int CryptData::CachePos=0;
19
20
21 #ifndef SFX_MODULE
22 static byte InitSubstTable[256]={
23 215, 19,149, 35, 73,197,192,205,249, 28, 16,119, 48,221, 2, 42,
24 232, 1,177,233, 14, 88,219, 25,223,195,244, 90, 87,239,153,137,
25 255,199,147, 70, 92, 66,246, 13,216, 40, 62, 29,217,230, 86, 6,
26 71, 24,171,196,101,113,218,123, 93, 91,163,178,202, 67, 44,235,
27 107,250, 75,234, 49,167,125,211, 83,114,157,144, 32,193,143, 36,
28 158,124,247,187, 89,214,141, 47,121,228, 61,130,213,194,174,251,
29 97,110, 54,229,115, 57,152, 94,105,243,212, 55,209,245, 63, 11,
30 164,200, 31,156, 81,176,227, 21, 76, 99,139,188,127, 17,248, 51,
31 207,120,189,210, 8,226, 41, 72,183,203,135,165,166, 60, 98, 7,
32 122, 38,155,170, 69,172,252,238, 39,134, 59,128,236, 27,240, 80,
33 131, 3, 85,206,145, 79,154,142,159,220,201,133, 74, 64, 20,129,
34 224,185,138,103,173,182, 43, 34,254, 82,198,151,231,180, 58, 10,
35 118, 26,102, 12, 50,132, 22,191,136,111,162,179, 45, 4,148,108,
36 161, 56, 78,126,242,222, 15,175,146, 23, 33,241,181,190, 77,225,
37 0, 46,169,186, 68, 95,237, 65, 53,208,253,168, 9, 18,100, 52,
38 116,184,160, 96,109, 37, 30,106,140,104,150, 5,204,117,112, 84
39 };
40 #endif
41
42
43
DecryptBlock(byte * Buf,int Size)44 void CryptData::DecryptBlock(byte *Buf,int Size)
45 {
46 rin.blockDecrypt(Buf,Size,Buf);
47 }
48
49
50 #ifndef SFX_MODULE
EncryptBlock20(byte * Buf)51 void CryptData::EncryptBlock20(byte *Buf)
52 {
53 uint A,B,C,D,T,TA,TB;
54 #if defined(BIG_ENDIAN) || !defined(PRESENT_INT32) || !defined(ALLOW_NOT_ALIGNED_INT)
55 A=((uint)Buf[0]|((uint)Buf[1]<<8)|((uint)Buf[2]<<16)|((uint)Buf[3]<<24))^Key[0];
56 B=((uint)Buf[4]|((uint)Buf[5]<<8)|((uint)Buf[6]<<16)|((uint)Buf[7]<<24))^Key[1];
57 C=((uint)Buf[8]|((uint)Buf[9]<<8)|((uint)Buf[10]<<16)|((uint)Buf[11]<<24))^Key[2];
58 D=((uint)Buf[12]|((uint)Buf[13]<<8)|((uint)Buf[14]<<16)|((uint)Buf[15]<<24))^Key[3];
59 #else
60 uint32 *BufPtr=(uint32 *)Buf;
61 A=BufPtr[0]^Key[0];
62 B=BufPtr[1]^Key[1];
63 C=BufPtr[2]^Key[2];
64 D=BufPtr[3]^Key[3];
65 #endif
66 for(int I=0;I<NROUNDS;I++)
67 {
68 T=((C+rol(D,11,32))^Key[I&3]);
69 TA=A^substLong(T);
70 T=((D^rol(C,17,32))+Key[I&3]);
71 TB=B^substLong(T);
72 A=C;
73 B=D;
74 C=TA;
75 D=TB;
76 }
77 #if defined(BIG_ENDIAN) || !defined(PRESENT_INT32) || !defined(ALLOW_NOT_ALIGNED_INT)
78 C^=Key[0];
79 Buf[0]=(byte)C;
80 Buf[1]=(byte)(C>>8);
81 Buf[2]=(byte)(C>>16);
82 Buf[3]=(byte)(C>>24);
83 D^=Key[1];
84 Buf[4]=(byte)D;
85 Buf[5]=(byte)(D>>8);
86 Buf[6]=(byte)(D>>16);
87 Buf[7]=(byte)(D>>24);
88 A^=Key[2];
89 Buf[8]=(byte)A;
90 Buf[9]=(byte)(A>>8);
91 Buf[10]=(byte)(A>>16);
92 Buf[11]=(byte)(A>>24);
93 B^=Key[3];
94 Buf[12]=(byte)B;
95 Buf[13]=(byte)(B>>8);
96 Buf[14]=(byte)(B>>16);
97 Buf[15]=(byte)(B>>24);
98 #else
99 BufPtr[0]=C^Key[0];
100 BufPtr[1]=D^Key[1];
101 BufPtr[2]=A^Key[2];
102 BufPtr[3]=B^Key[3];
103 #endif
104 UpdKeys(Buf);
105 }
106
107
DecryptBlock20(byte * Buf)108 void CryptData::DecryptBlock20(byte *Buf)
109 {
110 byte InBuf[16];
111 uint A,B,C,D,T,TA,TB;
112 #if defined(BIG_ENDIAN) || !defined(PRESENT_INT32) || !defined(ALLOW_NOT_ALIGNED_INT)
113 A=((uint)Buf[0]|((uint)Buf[1]<<8)|((uint)Buf[2]<<16)|((uint)Buf[3]<<24))^Key[0];
114 B=((uint)Buf[4]|((uint)Buf[5]<<8)|((uint)Buf[6]<<16)|((uint)Buf[7]<<24))^Key[1];
115 C=((uint)Buf[8]|((uint)Buf[9]<<8)|((uint)Buf[10]<<16)|((uint)Buf[11]<<24))^Key[2];
116 D=((uint)Buf[12]|((uint)Buf[13]<<8)|((uint)Buf[14]<<16)|((uint)Buf[15]<<24))^Key[3];
117 #else
118 uint32 *BufPtr=(uint32 *)Buf;
119 A=BufPtr[0]^Key[0];
120 B=BufPtr[1]^Key[1];
121 C=BufPtr[2]^Key[2];
122 D=BufPtr[3]^Key[3];
123 #endif
124 memcpy(InBuf,Buf,sizeof(InBuf));
125 for(int I=NROUNDS-1;I>=0;I--)
126 {
127 T=((C+rol(D,11,32))^Key[I&3]);
128 TA=A^substLong(T);
129 T=((D^rol(C,17,32))+Key[I&3]);
130 TB=B^substLong(T);
131 A=C;
132 B=D;
133 C=TA;
134 D=TB;
135 }
136 #if defined(BIG_ENDIAN) || !defined(PRESENT_INT32) || !defined(ALLOW_NOT_ALIGNED_INT)
137 C^=Key[0];
138 Buf[0]=(byte)C;
139 Buf[1]=(byte)(C>>8);
140 Buf[2]=(byte)(C>>16);
141 Buf[3]=(byte)(C>>24);
142 D^=Key[1];
143 Buf[4]=(byte)D;
144 Buf[5]=(byte)(D>>8);
145 Buf[6]=(byte)(D>>16);
146 Buf[7]=(byte)(D>>24);
147 A^=Key[2];
148 Buf[8]=(byte)A;
149 Buf[9]=(byte)(A>>8);
150 Buf[10]=(byte)(A>>16);
151 Buf[11]=(byte)(A>>24);
152 B^=Key[3];
153 Buf[12]=(byte)B;
154 Buf[13]=(byte)(B>>8);
155 Buf[14]=(byte)(B>>16);
156 Buf[15]=(byte)(B>>24);
157 #else
158 BufPtr[0]=C^Key[0];
159 BufPtr[1]=D^Key[1];
160 BufPtr[2]=A^Key[2];
161 BufPtr[3]=B^Key[3];
162 #endif
163 UpdKeys(InBuf);
164 }
165
166
UpdKeys(byte * Buf)167 void CryptData::UpdKeys(byte *Buf)
168 {
169 for (int I=0;I<16;I+=4)
170 {
171 Key[0]^=CRCTab[Buf[I]];
172 Key[1]^=CRCTab[Buf[I+1]];
173 Key[2]^=CRCTab[Buf[I+2]];
174 Key[3]^=CRCTab[Buf[I+3]];
175 }
176 }
177
178
Swap(byte * Ch1,byte * Ch2)179 void CryptData::Swap(byte *Ch1,byte *Ch2)
180 {
181 byte Ch=*Ch1;
182 *Ch1=*Ch2;
183 *Ch2=Ch;
184 }
185 #endif
186
187
SetCryptKeys(char * Password,byte * Salt,bool Encrypt,bool OldOnly)188 void CryptData::SetCryptKeys(char *Password,byte *Salt,bool Encrypt,bool OldOnly)
189 {
190 if (*Password==0)
191 return;
192 if (OldOnly)
193 {
194 #ifndef SFX_MODULE
195 if (CRCTab[1]==0)
196 InitCRC();
197 byte Psw[MAXPASSWORD];
198 SetOldKeys(Password);
199 Key[0]=0xD3A3B879L;
200 Key[1]=0x3F6D12F7L;
201 Key[2]=0x7515A235L;
202 Key[3]=0xA4E7F123L;
203 memset(Psw,0,sizeof(Psw));
204 #if defined(_WIN_32) && !defined(GUI)
205 CharToOemBuff(Password,(char*)Psw,strlen(Password));
206 #else
207 strncpy((char *)Psw,Password,MAXPASSWORD-1);
208 #endif
209 int PswLength=strlen(Password);
210 memcpy(SubstTable,InitSubstTable,sizeof(SubstTable));
211 for (int J=0;J<256;J++)
212 for (int I=0;I<PswLength;I+=2)
213 {
214 uint N1=(byte)CRCTab[(Psw[I]-J)&0xff];
215 uint N2=(byte)CRCTab[(Psw[I+1]+J)&0xff];
216 for (int K=1;N1!=N2;N1=(N1+1)&0xff,K++)
217 Swap(&SubstTable[N1],&SubstTable[(N1+I+K)&0xff]);
218 }
219 for (int I=0;I<PswLength;I+=16)
220 EncryptBlock20(&Psw[I]);
221 #endif
222 return;
223 }
224
225 bool Cached=false;
226 for (int I=0;I<sizeof(Cache)/sizeof(Cache[0]);I++)
227 if (strcmp(Cache[I].Password,Password)==0 &&
228 (Salt==NULL && !Cache[I].SaltPresent || Salt!=NULL &&
229 Cache[I].SaltPresent && memcmp(Cache[I].Salt,Salt,SALT_SIZE)==0))
230 {
231 memcpy(AESKey,Cache[I].AESKey,sizeof(AESKey));
232 memcpy(AESInit,Cache[I].AESInit,sizeof(AESInit));
233 Cached=true;
234 break;
235 }
236
237 if (!Cached)
238 {
239 wchar PswW[MAXPASSWORD];
240 CharToWide(Password,PswW,MAXPASSWORD-1);
241 PswW[MAXPASSWORD-1]=0;
242 byte RawPsw[2*MAXPASSWORD+SALT_SIZE];
243 WideToRaw(PswW,RawPsw);
244 int RawLength=2*strlenw(PswW);
245 if (Salt!=NULL)
246 {
247 memcpy(RawPsw+RawLength,Salt,SALT_SIZE);
248 RawLength+=SALT_SIZE;
249 }
250 hash_context c;
251 hash_initial(&c);
252
253 const int HashRounds=0x40000;
254 for (int I=0;I<HashRounds;I++)
255 {
256 hash_process( &c, RawPsw, RawLength);
257 byte PswNum[3];
258 PswNum[0]=(byte)I;
259 PswNum[1]=(byte)(I>>8);
260 PswNum[2]=(byte)(I>>16);
261 hash_process( &c, PswNum, 3);
262 if (I%(HashRounds/16)==0)
263 {
264 hash_context tempc=c;
265 uint32 digest[5];
266 hash_final( &tempc, digest);
267 AESInit[I/(HashRounds/16)]=(byte)digest[4];
268 }
269 }
270 uint32 digest[5];
271 hash_final( &c, digest);
272 for (int I=0;I<4;I++)
273 for (int J=0;J<4;J++)
274 AESKey[I*4+J]=(byte)(digest[I]>>(J*8));
275
276 strcpy(Cache[CachePos].Password,Password);
277 if ((Cache[CachePos].SaltPresent=(Salt!=NULL))==true)
278 memcpy(Cache[CachePos].Salt,Salt,SALT_SIZE);
279 memcpy(Cache[CachePos].AESKey,AESKey,sizeof(AESKey));
280 memcpy(Cache[CachePos].AESInit,AESInit,sizeof(AESInit));
281 CachePos=(CachePos+1)%(sizeof(Cache)/sizeof(Cache[0]));
282 }
283 rin.init(Encrypt ? Rijndael::Encrypt : Rijndael::Decrypt,AESKey,AESInit);
284 }
285
286
287 #ifndef SFX_MODULE
SetOldKeys(char * Password)288 void CryptData::SetOldKeys(char *Password)
289 {
290 uint PswCRC=CRC(0xffffffff,Password,strlen(Password));
291 OldKey[0]=PswCRC&0xffff;
292 OldKey[1]=(PswCRC>>16)&0xffff;
293 OldKey[2]=OldKey[3]=0;
294 PN1=PN2=PN3=0;
295 byte Ch;
296 while ((Ch=*Password)!=0)
297 {
298 PN1+=Ch;
299 PN2^=Ch;
300 PN3+=Ch;
301 PN3=(byte)rol(PN3,1,8);
302 OldKey[2]^=Ch^CRCTab[Ch];
303 OldKey[3]+=Ch+(CRCTab[Ch]>>16);
304 Password++;
305 }
306 }
307
308
SetAV15Encryption()309 void CryptData::SetAV15Encryption()
310 {
311 OldKey[0]=0x4765;
312 OldKey[1]=0x9021;
313 OldKey[2]=0x7382;
314 OldKey[3]=0x5215;
315 }
316
317
SetCmt13Encryption()318 void CryptData::SetCmt13Encryption()
319 {
320 PN1=0;
321 PN2=7;
322 PN3=77;
323 }
324
325
Crypt(byte * Data,uint Count,int Method)326 void CryptData::Crypt(byte *Data,uint Count,int Method)
327 {
328 if (Method==OLD_DECODE)
329 Decode13(Data,Count);
330 else
331 if (Method==OLD_ENCODE)
332 Encode13(Data,Count);
333 else
334 Crypt15(Data,Count);
335 }
336
337
Encode13(byte * Data,uint Count)338 void CryptData::Encode13(byte *Data,uint Count)
339 {
340 while (Count--)
341 {
342 PN2+=PN3;
343 PN1+=PN2;
344 *Data+=PN1;
345 Data++;
346 }
347 }
348
349
Decode13(byte * Data,uint Count)350 void CryptData::Decode13(byte *Data,uint Count)
351 {
352 while (Count--)
353 {
354 PN2+=PN3;
355 PN1+=PN2;
356 *Data-=PN1;
357 Data++;
358 }
359 }
360
361
Crypt15(byte * Data,uint Count)362 void CryptData::Crypt15(byte *Data,uint Count)
363 {
364 while (Count--)
365 {
366 OldKey[0]+=0x1234;
367 OldKey[1]^=CRCTab[(OldKey[0] & 0x1fe)>>1];
368 OldKey[2]-=CRCTab[(OldKey[0] & 0x1fe)>>1]>>16;
369 OldKey[0]^=OldKey[2];
370 OldKey[3]=ror(OldKey[3]&0xffff,1,16)^OldKey[1];
371 OldKey[3]=ror(OldKey[3]&0xffff,1,16);
372 OldKey[0]^=OldKey[3];
373 *Data^=(byte)(OldKey[0]>>8);
374 Data++;
375 }
376 }
377 #endif
378
379
380