1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
/*
dbx_tree: tree database driver for Miranda IM
Copyright 2007-2010 Michael "Protogenes" Kunz,
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "Interface.h"
#include "EncryptionManager.h"
#include <tchar.h>
uint32_t CEncryptionManager::CipherListRefCount = 0;
CEncryptionManager::TCipherList* CEncryptionManager::CipherList = NULL;
static const uint32_t cFileBlockMask = 0xfffff000;
void CEncryptionManager::LoadCipherList()
{
if (CipherList)
return;
CipherList = new TCipherList;
CipherListRefCount++;
WIN32_FIND_DATA search;
TCHAR path[MAX_PATH * 8];
GetModuleFileName(NULL, path, sizeof(path));
TCHAR * file = _tcsrchr(path, '\\');
if (!file)
file = path;
_tcscpy_s(file, sizeof(path) / sizeof(path[0]) - (file - path), _T("\\plugins\\encryption\\*.dll"));
file += 20;
HANDLE hfinder = FindFirstFile(path, &search);
if (hfinder != INVALID_HANDLE_VALUE)
{
TCipherItem item;
TCipherInfo* (__cdecl *CipherInfoProc)(void *);
do {
_tcscpy_s(file, sizeof(path) / sizeof(path[0]) - (file - path), search.cFileName);
HMODULE hmod = LoadLibrary(path);
if (hmod)
{
CipherInfoProc = (TCipherInfo*(__cdecl*)(void*)) GetProcAddress(hmod, "CipherInfo");
if (CipherInfoProc)
{
TCipherInfo* info = CipherInfoProc(NULL);
if (info && (info->cbSize == sizeof(TCipherInfo)) && (CipherList->find(info->ID) == CipherList->end()))
{
item.ID = info->ID;
item.Name = _wcsdup(info->Name);
item.Description = _wcsdup(info->Description);
item.FilePath = _tcsdup(path);
item.FileName = item.FilePath + (file - path);
CipherList->insert(std::make_pair(item.ID, item));
}
}
FreeLibrary(hmod);
}
} while (FindNextFile(hfinder, &search));
FindClose(hfinder);
}
}
CEncryptionManager::CEncryptionManager()
{
m_Ciphers[CURRENT].Cipher = NULL;
m_Ciphers[OLD].Cipher = NULL;
m_Changing = false;
m_ChangingProcess = 0;
LoadCipherList();
}
CEncryptionManager::~CEncryptionManager()
{
delete m_Ciphers[CURRENT].Cipher;
m_Ciphers[CURRENT].Cipher = NULL;
delete m_Ciphers[OLD].Cipher;
m_Ciphers[OLD].Cipher = NULL;
CipherListRefCount--;
if (!CipherListRefCount)
{
TCipherList::iterator i = CipherList->begin();
while (i != CipherList->end())
{
free(i->second.Description);
free(i->second.Name);
free(i->second.FilePath);
// do not free Filename... it's a substring of FilePath
++i;
}
delete CipherList;
CipherList = NULL;
}
}
bool CEncryptionManager::InitEncryption(TFileEncryption & Enc)
{
if (Enc.ConversionProcess)
{
m_Changing = true;
m_ChangingProcess = Enc.ConversionProcess;
}
for (int c = (int)CURRENT; c < (int)COUNT; c++)
{
TCipherList::iterator i = CipherList->find(Enc.CipherID);
if (i != CipherList->end())
{
m_Ciphers[c].CipherDLL = LoadLibrary(i->second.FilePath);
if (m_Ciphers[c].CipherDLL)
{
TCipherInfo* (__cdecl *cipherinfoproc)(void *);
cipherinfoproc = (TCipherInfo*(__cdecl*)(void*)) GetProcAddress(m_Ciphers[c].CipherDLL, "CipherInfo");
if (cipherinfoproc)
{
TCipherInfo* info = cipherinfoproc(NULL);
if (info && (info->cbSize == sizeof(TCipherInfo)))
m_Ciphers[c].Cipher = new CCipher(info->Create());
}
if (!m_Ciphers[c].Cipher)
{
FreeLibrary(m_Ciphers[c].CipherDLL);
m_Ciphers[c].CipherDLL = NULL;
}
}
}
}
return true;
}
bool CEncryptionManager::AlignData(uint32_t ID, uint32_t & Start, uint32_t & End)
{
if (m_Ciphers[CURRENT].Cipher && (!m_Changing || (ID < m_ChangingProcess)))
{
if (m_Ciphers[CURRENT].Cipher->IsStreamCipher())
{
Start = 0;
End = End - End % m_Ciphers[CURRENT].Cipher->BlockSizeBytes() + m_Ciphers[CURRENT].Cipher->BlockSizeBytes();
} else {
Start = Start - Start % m_Ciphers[CURRENT].Cipher->BlockSizeBytes();
if (End % m_Ciphers[CURRENT].Cipher->BlockSizeBytes())
End = End - End % m_Ciphers[CURRENT].Cipher->BlockSizeBytes() + m_Ciphers[CURRENT].Cipher->BlockSizeBytes();
}
return true;
} else if (m_Ciphers[OLD].Cipher && m_Changing && (ID >= m_ChangingProcess))
{
if (m_Ciphers[OLD].Cipher->IsStreamCipher())
{
Start = 0;
End = End - End % m_Ciphers[OLD].Cipher->BlockSizeBytes() + m_Ciphers[OLD].Cipher->BlockSizeBytes();
} else {
Start = Start - Start % m_Ciphers[OLD].Cipher->BlockSizeBytes();
if (End % m_Ciphers[OLD].Cipher->BlockSizeBytes())
End = End - End % m_Ciphers[OLD].Cipher->BlockSizeBytes() + m_Ciphers[OLD].Cipher->BlockSizeBytes();
}
return true;
}
return false;
}
uint32_t CEncryptionManager::AlignSize(uint32_t ID, uint32_t Size)
{
if (m_Ciphers[CURRENT].Cipher && (!m_Changing || (ID < m_ChangingProcess)))
{
if (Size % m_Ciphers[CURRENT].Cipher->BlockSizeBytes())
return Size - Size % m_Ciphers[CURRENT].Cipher->BlockSizeBytes() + m_Ciphers[CURRENT].Cipher->BlockSizeBytes();
} else if (m_Ciphers[OLD].Cipher && m_Changing && (ID >= m_ChangingProcess))
{
if (Size % m_Ciphers[OLD].Cipher->BlockSizeBytes())
return Size - Size % m_Ciphers[OLD].Cipher->BlockSizeBytes() + m_Ciphers[OLD].Cipher->BlockSizeBytes();
}
return Size;
}
bool CEncryptionManager::IsEncrypted(uint32_t ID)
{
return (m_Ciphers[CURRENT].Cipher && (!m_Changing || (ID < m_ChangingProcess))) ||
(m_Ciphers[OLD].Cipher && m_Changing && (ID >= m_ChangingProcess));
}
void CEncryptionManager::Encrypt(void* Data, uint32_t DataLength, uint32_t ID, uint32_t StartByte)
{
if (m_Ciphers[CURRENT].Cipher && (!m_Changing || (ID < m_ChangingProcess)))
{
m_Ciphers[CURRENT].Cipher->Encrypt(Data, DataLength, ID, StartByte);
} else if (m_Ciphers[OLD].Cipher && m_Changing && (ID >= m_ChangingProcess))
{
m_Ciphers[OLD].Cipher->Encrypt(Data, DataLength, ID, StartByte);
}
}
void CEncryptionManager::Decrypt(void* Data, uint32_t DataLength, uint32_t ID, uint32_t StartByte)
{
if (m_Ciphers[CURRENT].Cipher && (!m_Changing || (ID < m_ChangingProcess)))
{
m_Ciphers[CURRENT].Cipher->Decrypt(Data, DataLength, ID, StartByte);
} else if (m_Ciphers[OLD].Cipher && m_Changing && (ID >= m_ChangingProcess))
{
m_Ciphers[OLD].Cipher->Decrypt(Data, DataLength, ID, StartByte);
}
}
bool CEncryptionManager::CanChangeCipher()
{
return false;
}
bool CEncryptionManager::ChangeCipher(TEncryption & Encryption)
{
return false;
}
|