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
|
/*
Miranda NG: the free IM client for Microsoft* Windows*
Copyright (C) 2012-13 Miranda NG project,
all portions of this codebase are copyrighted to the people
listed in contributors.txt.
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 "commonheaders.h"
#include "..\..\..\plugins\zlib\src\zlib.h"
struct ExternalKey
{
BYTE m_key[KEY_LENGTH];
DWORD m_crc32;
BYTE slack[BLOCK_SIZE - sizeof(DWORD)];
};
CStdCrypt::CStdCrypt() :
m_password("Miranda")
{}
void CStdCrypt::destroy()
{
delete this;
}
size_t CStdCrypt::getKeyLength()
{
return sizeof(ExternalKey);
}
bool CStdCrypt::getKey(BYTE *pKey, size_t cbKeyLen)
{
if (!m_valid || cbKeyLen < sizeof(ExternalKey))
return false;
ExternalKey tmp = { 0 };
memcpy(&tmp.m_key, m_key, KEY_LENGTH);
tmp.m_crc32 = crc32(0xAbbaDead, (LPCBYTE)m_password.GetString(), m_password.GetLength());
getRandomBytes(tmp.slack, sizeof(tmp.slack));
BYTE tmpHash[32];
slow_hash(m_password, m_password.GetLength(), tmpHash);
CRijndael tmpAes;
tmpAes.MakeKey(tmpHash, tmpAes.sm_chain0, KEY_LENGTH, BLOCK_SIZE);
tmpAes.Encrypt(&tmp, pKey, sizeof(tmp));
return true;
}
bool CStdCrypt::setKey(const BYTE *pKey, size_t cbKeyLen)
{
// full external key. decode & check password
if (cbKeyLen != sizeof(ExternalKey))
return false;
BYTE tmpHash[32];
slow_hash(m_password, m_password.GetLength(), tmpHash);
CRijndael tmpAes;
tmpAes.MakeKey(tmpHash, tmpAes.sm_chain0, KEY_LENGTH, BLOCK_SIZE);
ExternalKey tmp = { 0 };
tmpAes.Decrypt(pKey, &tmp, sizeof(tmp));
if (tmp.m_crc32 != crc32(0xAbbaDead, (LPCBYTE)m_password.GetString(), m_password.GetLength()))
return false;
memcpy(m_key, &tmp.m_key, KEY_LENGTH);
m_aes.MakeKey(m_key, "Miranda", KEY_LENGTH, BLOCK_SIZE);
return m_valid = true;
}
bool CStdCrypt::generateKey(void)
{
BYTE tmp[KEY_LENGTH];
if (!getRandomBytes(tmp, sizeof(tmp)))
return false;
memcpy(m_key, tmp, KEY_LENGTH);
m_aes.MakeKey(m_key, "Miranda", KEY_LENGTH, BLOCK_SIZE);
return m_valid = true;
}
void CStdCrypt::purgeKey(void)
{
memset(m_key, 0, sizeof(m_key));
m_valid = false;
}
// checks the master password (in utf-8)
bool CStdCrypt::checkPassword(const char *pszPassword)
{
return m_password == pszPassword;
}
// sets the master password (in utf-8)
void CStdCrypt::setPassword(const char *pszPassword)
{
m_password = (pszPassword == NULL) ? "Miranda" : pszPassword;
}
// result must be freed using mir_free or assigned to mir_ptr<BYTE>
BYTE* CStdCrypt::encodeString(const char *src, size_t *cbResultLen)
{
if (!m_valid || src == NULL) {
if (cbResultLen)
*cbResultLen = 0;
return NULL;
}
return encodeBuffer(src, strlen(src)+1, cbResultLen);
}
BYTE* CStdCrypt::encodeBuffer(const void *src, size_t cbLen, size_t *cbResultLen)
{
if (cbResultLen)
*cbResultLen = 0;
if (!m_valid || src == NULL || cbLen >= 0xFFFE)
return NULL;
BYTE *tmpBuf = (BYTE*)_alloca(cbLen + 2);
*(PWORD)tmpBuf = (WORD)cbLen;
memcpy(tmpBuf + 2, src, cbLen);
cbLen += 2;
size_t rest = cbLen % BLOCK_SIZE;
if (rest)
cbLen += BLOCK_SIZE - rest;
BYTE *result = (BYTE*)mir_alloc(cbLen);
m_aes.ResetChain();
if (m_aes.Encrypt(tmpBuf, LPSTR(result), cbLen)) {
mir_free(result);
return NULL;
}
if (cbResultLen)
*cbResultLen = cbLen;
return result;
}
char* CStdCrypt::decodeString(const BYTE *pBuf, size_t bufLen, size_t *cbResultLen)
{
size_t resLen;
char *result = (char*)decodeBuffer(pBuf, bufLen, &resLen);
if (result) {
if (result[resLen-1] != 0) { // smth went wrong
mir_free(result);
return NULL;
}
}
if (cbResultLen)
*cbResultLen = resLen;
return result;
}
void* CStdCrypt::decodeBuffer(const BYTE *pBuf, size_t bufLen, size_t *cbResultLen)
{
if (cbResultLen)
*cbResultLen = 0;
if (!m_valid || pBuf == NULL || (bufLen % BLOCK_SIZE) != 0)
return NULL;
char *result = (char*)mir_alloc(bufLen + 1);
m_aes.ResetChain();
if (m_aes.Decrypt(LPCSTR(pBuf), result, bufLen)) {
mir_free(result);
return NULL;
}
result[bufLen] = 0;
WORD cbLen = *(PWORD)result;
if (cbLen > bufLen) {
mir_free(result);
return NULL;
}
memmove(result, result + 2, cbLen);
if (cbResultLen)
*cbResultLen = cbLen;
return result;
}
static MICryptoEngine* __cdecl builder()
{
return new CStdCrypt();
}
int LoadEncryptionModule(void)
{
CRYPTO_PROVIDER cp = { sizeof(cp) };
cp.pszName = "AES (Rjindale)";
cp.pszDescr = LPGEN("Standard crypto provider");
cp.pFactory = builder;
Crypto_RegisterEngine(&cp);
return 0;
}
|