crypt.cpp 9.5 KB

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