• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Crypto/RarAes.cpp
2 
3 #include "StdAfx.h"
4 
5 #include "../../../C/CpuArch.h"
6 #include "../../../C/RotateDefs.h"
7 
8 #include "RarAes.h"
9 #include "Sha1Cls.h"
10 
11 namespace NCrypto {
12 namespace NRar3 {
13 
CDecoder()14 CDecoder::CDecoder():
15     CAesCbcDecoder(kAesKeySize),
16     _thereIsSalt(false),
17     _needCalc(true)
18     // _rar350Mode(false)
19 {
20   for (unsigned i = 0; i < sizeof(_salt); i++)
21     _salt[i] = 0;
22 }
23 
SetDecoderProperties2(const Byte * data,UInt32 size)24 HRESULT CDecoder::SetDecoderProperties2(const Byte *data, UInt32 size)
25 {
26   bool prev = _thereIsSalt;
27   _thereIsSalt = false;
28   if (size == 0)
29   {
30     if (!_needCalc && prev)
31       _needCalc = true;
32     return S_OK;
33   }
34   if (size < 8)
35     return E_INVALIDARG;
36   _thereIsSalt = true;
37   bool same = false;
38   if (_thereIsSalt == prev)
39   {
40     same = true;
41     if (_thereIsSalt)
42     {
43       for (unsigned i = 0; i < sizeof(_salt); i++)
44         if (_salt[i] != data[i])
45         {
46           same = false;
47           break;
48         }
49     }
50   }
51   for (unsigned i = 0; i < sizeof(_salt); i++)
52     _salt[i] = data[i];
53   if (!_needCalc && !same)
54     _needCalc = true;
55   return S_OK;
56 }
57 
58 static const unsigned kPasswordLen_Bytes_MAX = 127 * 2;
59 
SetPassword(const Byte * data,unsigned size)60 void CDecoder::SetPassword(const Byte *data, unsigned size)
61 {
62   if (size > kPasswordLen_Bytes_MAX)
63     size = kPasswordLen_Bytes_MAX;
64   bool same = false;
65   if (size == _password.Size())
66   {
67     same = true;
68     for (UInt32 i = 0; i < size; i++)
69       if (data[i] != _password[i])
70       {
71         same = false;
72         break;
73       }
74   }
75   if (!_needCalc && !same)
76     _needCalc = true;
77   _password.Wipe();
78   _password.CopyFrom(data, (size_t)size);
79 }
80 
Z7_COM7F_IMF(CDecoder::Init ())81 Z7_COM7F_IMF(CDecoder::Init())
82 {
83   CalcKey();
84   RINOK(SetKey(_key, kAesKeySize))
85   RINOK(SetInitVector(_iv, AES_BLOCK_SIZE))
86   return CAesCoder::Init();
87 }
88 
89 
90 // if (password_size_in_bytes + SaltSize > 64),
91 // the original rar code updates password_with_salt buffer
92 // with some generated data from SHA1 code.
93 
94 // #define RAR_SHA1_REDUCE
95 
96 #ifdef RAR_SHA1_REDUCE
97   #define kNumW 16
98   #define WW(i) W[(i)&15]
99 #else
100   #define kNumW 80
101   #define WW(i) W[i]
102 #endif
103 
UpdatePswDataSha1(Byte * data)104 static void UpdatePswDataSha1(Byte *data)
105 {
106   UInt32 W[kNumW];
107   size_t i;
108 
109   for (i = 0; i < SHA1_NUM_BLOCK_WORDS; i++)
110     W[i] = GetBe32(data + i * 4);
111 
112   for (i = 16; i < 80; i++)
113   {
114     WW(i) = rotlFixed(WW((i)-3) ^ WW((i)-8) ^ WW((i)-14) ^ WW((i)-16), 1);
115   }
116 
117   for (i = 0; i < SHA1_NUM_BLOCK_WORDS; i++)
118   {
119     SetUi32(data + i * 4, W[kNumW - SHA1_NUM_BLOCK_WORDS + i])
120   }
121 }
122 
123 
CalcKey()124 void CDecoder::CalcKey()
125 {
126   if (!_needCalc)
127     return;
128 
129   const unsigned kSaltSize = 8;
130 
131   Byte buf[kPasswordLen_Bytes_MAX + kSaltSize];
132 
133   if (_password.Size() != 0)
134     memcpy(buf, _password, _password.Size());
135 
136   size_t rawSize = _password.Size();
137 
138   if (_thereIsSalt)
139   {
140     memcpy(buf + rawSize, _salt, kSaltSize);
141     rawSize += kSaltSize;
142   }
143 
144   MY_ALIGN (16)
145   NSha1::CContext sha;
146   sha.Init();
147 
148   MY_ALIGN (16)
149   Byte digest[NSha1::kDigestSize];
150   // rar reverts hash for sha.
151   const UInt32 kNumRounds = ((UInt32)1 << 18);
152   UInt32 pos = 0;
153   UInt32 i;
154   for (i = 0; i < kNumRounds; i++)
155   {
156     sha.Update(buf, rawSize);
157     // if (_rar350Mode)
158     {
159       const UInt32 kBlockSize = 64;
160       const UInt32 endPos = (pos + (UInt32)rawSize) & ~(kBlockSize - 1);
161       if (endPos > pos + kBlockSize)
162       {
163         UInt32 curPos = pos & ~(kBlockSize - 1);
164         curPos += kBlockSize;
165         do
166         {
167           UpdatePswDataSha1(buf + (curPos - pos));
168           curPos += kBlockSize;
169         }
170         while (curPos != endPos);
171       }
172     }
173     pos += (UInt32)rawSize;
174     Byte pswNum[3] = { (Byte)i, (Byte)(i >> 8), (Byte)(i >> 16) };
175     sha.Update(pswNum, 3);
176     pos += 3;
177     if (i % (kNumRounds / 16) == 0)
178     {
179       MY_ALIGN (16)
180       NSha1::CContext shaTemp = sha;
181       shaTemp.Final(digest);
182       _iv[i / (kNumRounds / 16)] = (Byte)digest[4 * 4 + 3];
183     }
184   }
185 
186   sha.Final(digest);
187   for (i = 0; i < 4; i++)
188     for (unsigned j = 0; j < 4; j++)
189       _key[i * 4 + j] = (digest[i * 4 + 3 - j]);
190 
191   _needCalc = false;
192 }
193 
194 }}
195