1 // Crypto/Rar20Crypto.cpp
2
3 #include "StdAfx.h"
4
5 #include "../../../C/7zCrc.h"
6 #include "../../../C/CpuArch.h"
7 #include "../../../C/RotateDefs.h"
8
9 #include "Rar20Crypto.h"
10
11 namespace NCrypto {
12 namespace NRar2 {
13
14 static const unsigned kNumRounds = 32;
15
16 static const Byte g_InitSubstTable[256] = {
17 215, 19,149, 35, 73,197,192,205,249, 28, 16,119, 48,221, 2, 42,
18 232, 1,177,233, 14, 88,219, 25,223,195,244, 90, 87,239,153,137,
19 255,199,147, 70, 92, 66,246, 13,216, 40, 62, 29,217,230, 86, 6,
20 71, 24,171,196,101,113,218,123, 93, 91,163,178,202, 67, 44,235,
21 107,250, 75,234, 49,167,125,211, 83,114,157,144, 32,193,143, 36,
22 158,124,247,187, 89,214,141, 47,121,228, 61,130,213,194,174,251,
23 97,110, 54,229,115, 57,152, 94,105,243,212, 55,209,245, 63, 11,
24 164,200, 31,156, 81,176,227, 21, 76, 99,139,188,127, 17,248, 51,
25 207,120,189,210, 8,226, 41, 72,183,203,135,165,166, 60, 98, 7,
26 122, 38,155,170, 69,172,252,238, 39,134, 59,128,236, 27,240, 80,
27 131, 3, 85,206,145, 79,154,142,159,220,201,133, 74, 64, 20,129,
28 224,185,138,103,173,182, 43, 34,254, 82,198,151,231,180, 58, 10,
29 118, 26,102, 12, 50,132, 22,191,136,111,162,179, 45, 4,148,108,
30 161, 56, 78,126,242,222, 15,175,146, 23, 33,241,181,190, 77,225,
31 0, 46,169,186, 68, 95,237, 65, 53,208,253,168, 9, 18,100, 52,
32 116,184,160, 96,109, 37, 30,106,140,104,150, 5,204,117,112, 84
33 };
34
UpdateKeys(const Byte * data)35 void CData::UpdateKeys(const Byte *data)
36 {
37 for (unsigned i = 0; i < 16; i += 4)
38 for (unsigned j = 0; j < 4; j++)
39 Keys[j] ^= g_CrcTable[data[i + j]];
40 }
41
Swap(Byte & b1,Byte & b2)42 static inline void Swap(Byte &b1, Byte &b2)
43 {
44 Byte b = b1;
45 b1 = b2;
46 b2 = b;
47 }
48
SetPassword(const Byte * data,unsigned size)49 void CData::SetPassword(const Byte *data, unsigned size)
50 {
51 Keys[0] = 0xD3A3B879L;
52 Keys[1] = 0x3F6D12F7L;
53 Keys[2] = 0x7515A235L;
54 Keys[3] = 0xA4E7F123L;
55
56 Byte psw[128];
57 Z7_memset_0_ARRAY(psw);
58 if (size != 0)
59 {
60 if (size >= sizeof(psw))
61 size = sizeof(psw) - 1;
62 memcpy(psw, data, size);
63 }
64
65 memcpy(SubstTable, g_InitSubstTable, sizeof(SubstTable));
66
67 for (unsigned j = 0; j < 256; j++)
68 for (unsigned i = 0; i < size; i += 2)
69 {
70 unsigned n1 = (Byte)g_CrcTable[(psw[i] - j) & 0xFF];
71 unsigned n2 = (Byte)g_CrcTable[(psw[(size_t)i + 1] + j) & 0xFF];
72 for (unsigned k = 1; (n1 & 0xFF) != n2; n1++, k++)
73 Swap(SubstTable[n1 & 0xFF], SubstTable[(n1 + i + k) & 0xFF]);
74 }
75
76 for (unsigned i = 0; i < size; i += 16)
77 EncryptBlock(psw + i);
78 }
79
CryptBlock(Byte * buf,bool encrypt)80 void CData::CryptBlock(Byte *buf, bool encrypt)
81 {
82 Byte inBuf[16];
83 UInt32 A, B, C, D;
84
85 A = GetUi32(buf + 0) ^ Keys[0];
86 B = GetUi32(buf + 4) ^ Keys[1];
87 C = GetUi32(buf + 8) ^ Keys[2];
88 D = GetUi32(buf + 12) ^ Keys[3];
89
90 if (!encrypt)
91 memcpy(inBuf, buf, sizeof(inBuf));
92
93 for (unsigned i = 0; i < kNumRounds; i++)
94 {
95 UInt32 key = Keys[(encrypt ? i : (kNumRounds - 1 - i)) & 3];
96 UInt32 TA = A ^ SubstLong((C + rotlFixed(D, 11)) ^ key);
97 UInt32 TB = B ^ SubstLong((D ^ rotlFixed(C, 17)) + key);
98 A = C; C = TA;
99 B = D; D = TB;
100 }
101
102 SetUi32(buf + 0, C ^ Keys[0])
103 SetUi32(buf + 4, D ^ Keys[1])
104 SetUi32(buf + 8, A ^ Keys[2])
105 SetUi32(buf + 12, B ^ Keys[3])
106
107 UpdateKeys(encrypt ? buf : inBuf);
108 }
109
Z7_COM7F_IMF(CDecoder::Init ())110 Z7_COM7F_IMF(CDecoder::Init())
111 {
112 return S_OK;
113 }
114
115 static const UInt32 kBlockSize = 16;
116
Z7_COM7F_IMF2(UInt32,CDecoder::Filter (Byte * data,UInt32 size))117 Z7_COM7F_IMF2(UInt32, CDecoder::Filter(Byte *data, UInt32 size))
118 {
119 if (size == 0)
120 return 0;
121 if (size < kBlockSize)
122 return kBlockSize;
123 size -= kBlockSize;
124 UInt32 i;
125 for (i = 0; i <= size; i += kBlockSize)
126 DecryptBlock(data + i);
127 return i;
128 }
129
130 }}
131