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