-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCryptography.cpp
More file actions
436 lines (376 loc) · 12.8 KB
/
Copy pathCryptography.cpp
File metadata and controls
436 lines (376 loc) · 12.8 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/*#*******************************************************************************
# COPYRIGHT NOTES
# ---------------
# This is a part of VinaText Project
# Copyright(C) - free open source - vinadevs
# This source code can be used, distributed or modified under MIT license
#*******************************************************************************/
#include "stdafx.h"
#include "Cryptography.h"
#include "AppUtil.h"
#include "RAIIUtils.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#define new DEBUG_NEW
#endif
CCryptographyIO::CCryptographyIO()
{
m_iTotalByteRead = 0;
m_iLenghtFileToOpen = 0;
m_hAesAlg = NULL;
m_hKey = NULL;
m_pbKeyObject = NULL;
m_pbIV = NULL;
m_hHash = NULL;
m_pbHashObject = NULL;
m_hHashAlg = NULL;
}
CCryptographyIO::~CCryptographyIO()
{
if (m_hAesAlg)
{
BCryptCloseAlgorithmProvider(m_hAesAlg, 0);
}
if (m_hKey)
{
BCryptDestroyKey(m_hKey);
}
if (m_pbKeyObject)
{
HeapFree(GetProcessHeap(), 0, m_pbKeyObject);
}
if (m_pbIV != NULL)
{
HeapFree(GetProcessHeap(), 0, m_pbIV);
}
if (m_hHash)
{
BCryptDestroyHash(m_hHash);
}
if (m_pbHashObject)
{
free(m_pbHashObject);
}
if (m_hHashAlg)
{
BCryptCloseAlgorithmProvider(m_hHashAlg, 0);
}
}
BOOL CCryptographyIO::CryptFile(BOOL bEncrypt, BOOL bSHAMethod, CString strFileToOpen, CString strFileToCrypt, CString strKey)
{
CFile oFileToOpen, oFileToCrypt;
NTSTATUS ntStatus = STATUS_UNSUCCESSFUL;
DWORD cbKeyObject = 0;
DWORD cbIV = 0;
BOOL bFileToOpen = FALSE;
BOOL bFileToCrypt = FALSE;
UINT iBytesRead = 0;
BYTE pbufFileToOpen[BLOCK_BUFFER_SIZE];
BYTE pbufFileToSave[BLOCK_BUFFER_SIZE * 2];
if (!InitMicrosoftProviderAES())
return FALSE;
if (!bSHAMethod)
{
if (!CreateSymmetricKeyInternal(cbKeyObject, cbIV))
return FALSE;
}
else
{
if (!CreateSymmetricUserKeySHA1(strKey, cbKeyObject))
return FALSE;
}
bFileToOpen = oFileToOpen.Open(strFileToOpen, CFile::modeRead | CFile::typeBinary);
if (bFileToOpen == FALSE)
{
LOG_OUTPUT_MESSAGE_FORMAT(_T("> [Error]: opening the file '%s'"), strFileToOpen);
return FALSE;
}
bFileToCrypt = oFileToCrypt.Open(strFileToCrypt, CFile::modeWrite | CFile::shareExclusive | CFile::modeCreate);
if (bFileToCrypt == FALSE)
{
LOG_OUTPUT_MESSAGE_FORMAT(_T("> [Error]: opening the file '%s'"), strFileToOpen);
return FALSE;
}
SetLenghtFileToOpen(oFileToOpen.GetLength());
DWORD iBufToSave = 0;
while (!LastCryptBuffRead())
{
iBufToSave = 0;
iBytesRead = oFileToOpen.Read(pbufFileToOpen, BLOCK_BUFFER_SIZE);
SetTotalByteRead(iBytesRead);
if (iBytesRead == 0)
{
CString sError;
LOG_OUTPUT_MESSAGE_FORMAT(_T("> [Error]: reading the file '%s'"), strFileToOpen);
return FALSE;
}
if (!LastCryptBuffRead())
{
if (!CryptData(bEncrypt, pbufFileToOpen, iBytesRead, cbIV, pbufFileToSave, iBufToSave))
return FALSE;
oFileToCrypt.Write(pbufFileToSave, iBytesRead);
}
else
{
if (!CryptLastByte(bEncrypt, pbufFileToOpen, iBytesRead, cbIV, pbufFileToSave, iBufToSave))
return FALSE;
oFileToCrypt.Write(pbufFileToSave, iBufToSave);
}
}
oFileToOpen.Close();
oFileToCrypt.Close();
return TRUE;
}
BOOL CCryptographyIO::EnumProviders(CStringList *lstRegisteredProviders)
{
NTSTATUS ntStatus = STATUS_UNSUCCESSFUL;
DWORD cbBuffer = 0;
PCRYPT_PROVIDERS pProviders = NULL;
ntStatus = BCryptEnumRegisteredProviders(&cbBuffer, &pProviders);
CString sProvider;
if (ntStatus != STATUS_SUCCESS)
{
LOG_OUTPUT_MESSAGE_FORMAT(_T("> BCryptEnumRegisteredProviders failed with error code 0x%08x"), ntStatus);
return FALSE;
}
if (pProviders == NULL)
{
LOG_OUTPUT_MESSAGE_FORMAT(_T("> BCryptEnumRegisteredProviders returned a NULL"));
return FALSE;
}
else
{
for (DWORD i = 0; i < pProviders->cProviders; i++)
{
sProvider.Format(_T("%s\n"), pProviders->rgpszProviders[i]);
lstRegisteredProviders->AddHead(sProvider);
}
}
if (pProviders != NULL)
{
BCryptFreeBuffer(pProviders);
}
return TRUE;
}
BOOL CCryptographyIO::LastCryptBuffRead()
{
return (m_iLenghtFileToOpen <= m_iTotalByteRead);
}
void CCryptographyIO::SetLenghtFileToOpen(ULONGLONG iLenghtFileToOpen)
{
m_iLenghtFileToOpen = iLenghtFileToOpen;
}
void CCryptographyIO::SetTotalByteRead(UINT iByteRead)
{
m_iTotalByteRead += iByteRead;
}
BOOL CCryptographyIO::InitMicrosoftProviderAES()
{
NTSTATUS ntStatus = STATUS_UNSUCCESSFUL;
ntStatus = BCryptOpenAlgorithmProvider(&m_hAesAlg, BCRYPT_AES_ALGORITHM, NULL, 0); // use AES Symmetric algorithm
switch (ntStatus)
{
case STATUS_SUCCESS:
return TRUE;
case STATUS_INVALID_PARAMETER:
LOG_OUTPUT_MESSAGE_FORMAT(_T("> [Error]: opening The algorithm handle. BCryptOpenAlgorithmProvider one or more parameters are not valid. "));
break;
case STATUS_NO_MEMORY:
LOG_OUTPUT_MESSAGE_FORMAT(_T("> [Error]: opening The algorithm handle. BCryptOpenAlgorithmProvider a memory allocation failure occurred. "));
break;
default:
LOG_OUTPUT_MESSAGE_FORMAT(_T("> [Error]: opening The algorithm handle. BCryptOpenAlgorithmProvider return with error 0x%08x"), ntStatus);
break;
}
return FALSE;
}
BOOL CCryptographyIO::CryptData(BOOL bEncrypt, PUCHAR pbufFileToOpen, ULONG iBytesRead, ULONG cbIV, PBYTE pbufFileToSave, DWORD& iBufToSave)
{
NTSTATUS ntStatus = STATUS_UNSUCCESSFUL;
DWORD cbCipherText = 0;
if (bEncrypt)
ntStatus = BCryptEncrypt(m_hKey, pbufFileToOpen, iBytesRead, NULL, m_pbIV, cbIV, pbufFileToSave, iBytesRead, &iBufToSave, 0);
else
ntStatus = BCryptDecrypt(m_hKey, pbufFileToOpen, iBytesRead, NULL, m_pbIV, cbIV, pbufFileToSave, iBytesRead, &iBufToSave, 0);
switch (ntStatus)
{
case STATUS_SUCCESS:
return TRUE;
case STATUS_BUFFER_TOO_SMALL:
LOG_OUTPUT_MESSAGE_FORMAT(_T("> [Error]: %s The size specified by the cbOutput parameter is not large enough to hold the ciphertext. Return with error 0x%08x"), bEncrypt ? _T("BCryptEncrypt") : _T("BCryptDecrypt"), ntStatus);
break;
case STATUS_INVALID_PARAMETER:
LOG_OUTPUT_MESSAGE_FORMAT(_T("> [Error]: %s One or more parameters are not valid. Return with error 0x%08x"), bEncrypt ? "BCryptEncrypt" : "BCryptDecrypt", ntStatus);
break;
case STATUS_NOT_SUPPORTED:
LOG_OUTPUT_MESSAGE_FORMAT(_T("> [Error]: %s The algorithm does not support encryption. Return with error 0x%08x"), bEncrypt ? "BCryptEncrypt" : "BCryptDecrypt", ntStatus);
break;
default:
LOG_OUTPUT_MESSAGE_FORMAT(_T("> [Error]: %s return with error 0x%08x"), bEncrypt ? "BCryptEncrypt" : "BCryptDecrypt", ntStatus);
}
return FALSE;
}
BOOL CCryptographyIO::CryptLastByte(BOOL bEncrypt, PUCHAR pbufFileToOpen, ULONG iBytesRead, ULONG cbIV, PBYTE pbufFileToSave, DWORD& iBufToSave)
{
NTSTATUS ntStatus = STATUS_UNSUCCESSFUL;
DWORD cbCipherText = 0;
if (bEncrypt)
{
ntStatus = BCryptEncrypt(m_hKey, pbufFileToOpen, iBytesRead, NULL, m_pbIV, cbIV, NULL, 0, &cbCipherText, BCRYPT_BLOCK_PADDING);
if (ntStatus != STATUS_SUCCESS)
{
LOG_OUTPUT_MESSAGE_FORMAT(_T("> [Error]: receiving the size required for the ciphertext. BCryptEncrypt return with error 0x%08x"), ntStatus);
return FALSE;
}
ntStatus = BCryptEncrypt(m_hKey, pbufFileToOpen, iBytesRead, NULL, m_pbIV, cbIV, pbufFileToSave, cbCipherText, &cbCipherText, BCRYPT_BLOCK_PADDING);
switch (ntStatus)
{
case STATUS_SUCCESS:
iBufToSave = cbCipherText;
return TRUE;
default:
LOG_OUTPUT_MESSAGE_FORMAT(_T("> [Error]: receiving wrong password. BCryptEncrypt return with error 0x%08x"), ntStatus);
return FALSE;
}
}
else
{
ntStatus = BCryptDecrypt(m_hKey, pbufFileToOpen, iBytesRead, NULL, m_pbIV, cbIV, NULL, 0, &cbCipherText, BCRYPT_BLOCK_PADDING);
if (ntStatus != STATUS_SUCCESS)
{
LOG_OUTPUT_MESSAGE_FORMAT(_T("> [Error]: receiving the size required for the ciphertext. BCryptDecrypt return with error 0x%08x"), ntStatus);
return FALSE;
}
ntStatus = BCryptDecrypt(m_hKey, pbufFileToOpen, iBytesRead, NULL, m_pbIV, cbIV, pbufFileToSave, cbCipherText, &cbCipherText, BCRYPT_BLOCK_PADDING);
switch (ntStatus)
{
case STATUS_SUCCESS:
iBufToSave = cbCipherText;
return TRUE;
default:
LOG_OUTPUT_MESSAGE_FORMAT(_T("> [Error]: receiving wrong password. BCryptDecrypt return with error 0x%08x"), ntStatus);
return FALSE;
}
}
return FALSE;
}
BOOL CCryptographyIO::CreateSymmetricUserKeySHA1(PCWSTR pwszText, DWORD cbKeyObject)
{
NTSTATUS ntStatus = STATUS_SUCCESS;
BCRYPT_KEY_HANDLE hKey = NULL;
DWORD cbHashObject, cbResult;
BYTE rgbHash[20];
DWORD cbData = 0;
ntStatus = BCryptOpenAlgorithmProvider(&m_hHashAlg, BCRYPT_SHA1_ALGORITHM, NULL, 0);
if (STATUS_SUCCESS != ntStatus)
{
LOG_OUTPUT_MESSAGE_FORMAT(_T("> [Error]: Open Algoritm for the key. BCryptOpenAlgorithmProvider failed with status: 0x%08x\n"), ntStatus);
return FALSE;
}
ntStatus = BCryptGetProperty(m_hAesAlg, BCRYPT_OBJECT_LENGTH, (PBYTE)&cbKeyObject, sizeof(DWORD), &cbData, 0);
if (ntStatus != STATUS_SUCCESS)
{
LOG_OUTPUT_MESSAGE_FORMAT(_T("> [Error]: calculating the size of KeyObject. BCryptGetProperty return with error 0x%08x"), ntStatus);
return FALSE;
}
m_pbKeyObject = (PBYTE)HeapAlloc(GetProcessHeap(), 0, cbKeyObject);
if (NULL == m_pbKeyObject)
{
LOG_OUTPUT_MESSAGE_FORMAT(_T("> [Error]: memory allocation key object on the heap failed"));
return FALSE;
}
// Determine the size of the Hash object
ntStatus = BCryptGetProperty(m_hHashAlg, BCRYPT_OBJECT_LENGTH, (PBYTE)&cbHashObject, sizeof(DWORD), &cbResult, 0);
if (STATUS_SUCCESS != ntStatus)
{
LOG_OUTPUT_MESSAGE_FORMAT(_T("> [Error]: determining the size of the Hash object. BCryptGetProperty failed with status: 0x%08x"), ntStatus);
return FALSE;
}
m_pbHashObject = (PBYTE)malloc(cbHashObject);
if (NULL == m_pbHashObject)
{
LOG_OUTPUT_MESSAGE_FORMAT(_T("> [Error]: allocating Hash object memory"));
return FALSE;
}
// Create the Hash object
ntStatus = BCryptCreateHash(m_hHashAlg, &m_hHash, m_pbHashObject, cbHashObject, NULL, 0, 0);
if (STATUS_SUCCESS != ntStatus)
{
LOG_OUTPUT_MESSAGE_FORMAT(_T("> [Error]: Creating the Hash object. BCryptCreateHash failed with status: 0x%08x"), ntStatus);
return FALSE;
}
// Hash the data
ntStatus = BCryptHashData(m_hHash, (PBYTE)pwszText, (ULONG)wcslen(pwszText), 0);
if (STATUS_SUCCESS != ntStatus)
{
LOG_OUTPUT_MESSAGE_FORMAT(_T("> [Error]: hashing the data. BCryptHashData failed with status: 0x%08x"), ntStatus);
return FALSE;
}
// Finish the hash
ntStatus = BCryptFinishHash(m_hHash, rgbHash, sizeof(rgbHash), 0);
if (STATUS_SUCCESS != ntStatus)
{
LOG_OUTPUT_MESSAGE_FORMAT(_T("> [Error]: finish the hash. BCryptFinishHash failed with status: 0x%08x"), ntStatus);
return FALSE;
}
ntStatus = BCryptGenerateSymmetricKey(m_hAesAlg, &hKey, m_pbKeyObject, cbKeyObject, rgbHash, SYMM_KEY_SIZE_SECRET, 0);
if (STATUS_SUCCESS != ntStatus)
{
LOG_OUTPUT_MESSAGE_FORMAT(_T("> [Error]: creating the key. BCryptGenerateSymmetricKey failed with status: 0x%08x"), ntStatus);
hKey = NULL;
}
m_hKey = hKey;
return TRUE;
}
BOOL CCryptographyIO::CreateSymmetricKeyInternal(DWORD &cbKeyObject, DWORD &cbIV)
{
NTSTATUS ntStatus = STATUS_UNSUCCESSFUL;
DWORD cbData = 0;
cbKeyObject = 0;
cbIV = 0;
ntStatus = BCryptGetProperty(m_hAesAlg, BCRYPT_OBJECT_LENGTH, (PBYTE)&cbKeyObject, sizeof(DWORD), &cbData, 0);
if (ntStatus != STATUS_SUCCESS)
{
LOG_OUTPUT_MESSAGE_FORMAT(_T("> [Error]: calculating the size of KeyObject. BCryptGetProperty return with error 0x%08x"), ntStatus);
return FALSE;
}
m_pbKeyObject = (PBYTE)HeapAlloc(GetProcessHeap(), 0, cbKeyObject);
if (NULL == m_pbKeyObject)
{
LOG_OUTPUT_MESSAGE_FORMAT(_T("> [Error]: memory allocation key object on the heap failed"));
return FALSE;
}
ntStatus = BCryptGetProperty(m_hAesAlg, BCRYPT_BLOCK_LENGTH, (PBYTE)&cbIV, sizeof(DWORD), &cbData, 0);
if (ntStatus != STATUS_SUCCESS)
{
LOG_OUTPUT_MESSAGE_FORMAT(_T("> [Error]: calculating the block length for the IV. BCryptGetProperty return with error 0x%08x"), ntStatus);
return FALSE;
}
if (cbIV > sizeof(vectorIV))
{
LOG_OUTPUT_MESSAGE_FORMAT(_T("> Block length is longer than the provided IV length\n"));
return FALSE;
}
m_pbIV = (PBYTE)HeapAlloc(GetProcessHeap(), 0, cbIV);
if (NULL == m_pbIV)
{
LOG_OUTPUT_MESSAGE_FORMAT(_T("> [Error]: memory allocation buffer for the IV on the heap failed"));
return FALSE;
}
memcpy(m_pbIV, vectorIV, cbIV);
ntStatus = BCryptSetProperty(m_hAesAlg, BCRYPT_CHAINING_MODE, (PBYTE)BCRYPT_CHAIN_MODE_CBC, sizeof(BCRYPT_CHAIN_MODE_CBC), 0);
if (ntStatus != STATUS_SUCCESS)
{
LOG_OUTPUT_MESSAGE_FORMAT(_T("> [Error]: setting the ChainingMode CBC. BCryptSetProperty return with error 0x%08x"), ntStatus);
return FALSE;
}
// generate the key from supplied input key bytes
ntStatus = BCryptGenerateSymmetricKey(m_hAesAlg, &m_hKey, m_pbKeyObject, cbKeyObject, (PBYTE)AES128bit_Key, sizeof(AES128bit_Key), 0);
if (ntStatus != STATUS_SUCCESS)
{
LOG_OUTPUT_MESSAGE_FORMAT(_T("> [Error]: generate the key. BCryptGenerateSymmetricKey return with error 0x%08x"), ntStatus);
return FALSE;
}
return TRUE;
}