forked from negrutiu/nsis-nscurl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.c
323 lines (279 loc) · 8.12 KB
/
crypto.c
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
//? Marius Negrutiu (mailto:[email protected]) :: 2019/11/20
//? Compute hashes using our embedded cryptographic engine
#include "main.h"
#include "crypto.h"
#if !defined(NO_HASH_EXPORTS)
#include <openssl/crypto/evp.h>
//++ Hash
ULONG Hash( _In_ IDATA *pData, _Out_opt_ PUCHAR md5, _Out_opt_ PUCHAR sha1, _Out_opt_ PUCHAR sha256 )
{
if (!pData)
return ERROR_INVALID_PARAMETER;
if (pData->Type == IDATA_TYPE_FILE) {
return HashFile( pData->File, md5, sha1, sha256 );
} else if (pData->Type == IDATA_TYPE_MEM) {
return HashMem( pData->Mem, (size_t)pData->Size, md5, sha1, sha256 );
} else if (pData->Type == IDATA_TYPE_STRING) {
assert( lstrlenA( pData->Str ) == pData->Size );
return HashMem( pData->Str, (size_t)pData->Size, md5, sha1, sha256 );
}
return ERROR_NOT_SUPPORTED;
}
//++ HashFile
ULONG HashFile( _In_ LPCTSTR pszFile, _Out_opt_ PUCHAR md5, _Out_opt_ PUCHAR sha1, _Out_opt_ PUCHAR sha256 )
{
ULONG e = ERROR_SUCCESS;
HANDLE h;
if (!pszFile || !*pszFile || (!md5 && !sha1 && !sha256))
return ERROR_INVALID_PARAMETER;
h = CreateFile( pszFile, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, 0, NULL );
if (h != INVALID_HANDLE_VALUE) {
ULONG bufsize = 1024 * 1024;
PUCHAR buf = (PUCHAR)HeapAlloc( GetProcessHeap(), 0, bufsize );
if (buf) {
ULONG l;
EVP_MD_CTX *md5ctx = EVP_MD_CTX_new();
EVP_MD_CTX *sha1ctx = EVP_MD_CTX_new();
EVP_MD_CTX *sha2ctx = EVP_MD_CTX_new();
if (md5ctx && sha1ctx && sha2ctx) {
if (md5) EVP_DigestInit( md5ctx, EVP_md5() );
if (sha1) EVP_DigestInit( sha1ctx, EVP_sha1() );
if (sha256) EVP_DigestInit( sha2ctx, EVP_sha256() );
while ((e = ReadFile( h, buf, bufsize, &l, NULL ) ? ERROR_SUCCESS : GetLastError()) == ERROR_SUCCESS && (l > 0)) {
if (md5) EVP_DigestUpdate( md5ctx, buf, l );
if (sha1) EVP_DigestUpdate( sha1ctx, buf, l );
if (sha256) EVP_DigestUpdate( sha2ctx, buf, l );
}
if (md5) EVP_DigestFinal( md5ctx, md5, NULL );
if (sha1) EVP_DigestFinal( sha1ctx, sha1, NULL );
if (sha256) EVP_DigestFinal( sha2ctx, sha256, NULL );
}
if (md5ctx) EVP_MD_CTX_free( md5ctx );
if (sha1ctx) EVP_MD_CTX_free( sha1ctx );
if (sha2ctx) EVP_MD_CTX_free( sha2ctx );
HeapFree( GetProcessHeap(), 0, buf );
} else {
e = ERROR_OUTOFMEMORY;
}
CloseHandle( h );
} else {
e = GetLastError();
}
return e;
}
//++ HashMem
ULONG HashMem( _In_ LPCVOID pPtr, _In_ size_t iSize, _Out_opt_ PUCHAR md5, _Out_opt_ PUCHAR sha1, _Out_opt_ PUCHAR sha256 )
{
if (!pPtr || !iSize || (!md5 && !sha1 && !sha256))
return ERROR_INVALID_PARAMETER;
if (md5) {
EVP_MD_CTX *ctx = EVP_MD_CTX_new();
if (ctx) {
EVP_DigestInit( ctx, EVP_md5() );
EVP_DigestUpdate( ctx, pPtr, iSize );
EVP_DigestFinal( ctx, md5, NULL );
EVP_MD_CTX_free( ctx );
}
}
if (sha1) {
EVP_MD_CTX *ctx = EVP_MD_CTX_new();
if (ctx) {
EVP_DigestInit( ctx, EVP_sha1() );
EVP_DigestUpdate( ctx, pPtr, iSize );
EVP_DigestFinal( ctx, sha1, NULL );
EVP_MD_CTX_free( ctx );
}
}
if (sha256) {
EVP_MD_CTX *ctx = EVP_MD_CTX_new();
if (ctx) {
EVP_DigestInit( ctx, EVP_sha256() );
EVP_DigestUpdate( ctx, pPtr, iSize );
EVP_DigestFinal( ctx, sha256, NULL );
EVP_MD_CTX_free( ctx );
}
}
return ERROR_SUCCESS;
}
//++ EncBase64
LPSTR EncBase64( _In_ LPCVOID pPtr, _In_ size_t iSize )
{
LPSTR pszBase64 = NULL;
EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new();
if (ctx) {
int lOut = 4 * ((iSize + 2) / 3) + 1;
if ((pszBase64 = (LPSTR)MyAlloc( lOut )) != NULL) {
LPSTR psz = pszBase64;
int len;
EVP_EncodeInit( ctx );
evp_encode_ctx_set_flags( ctx, EVP_ENCODE_CTX_NO_NEWLINES );
EVP_EncodeUpdate( ctx, psz, &len, pPtr, (int)iSize );
EVP_EncodeFinal( ctx, (psz += len), &len );
}
EVP_ENCODE_CTX_free( ctx );
}
return pszBase64;
}
//++ DecBase64
PVOID DecBase64( _In_ LPCSTR pszBase64, _Out_opt_ size_t *piSize )
{
LPSTR pPtr = NULL;
EVP_ENCODE_CTX *ctx = EVP_ENCODE_CTX_new();
if (piSize) *piSize = 0;
if (ctx) {
int lIn = lstrlenA( pszBase64 );
int lOut = (3 * lIn) / 4;
if ((pPtr = (LPSTR)MyAlloc( lOut )) != NULL) {
LPSTR psz = pPtr;
int len1 = 0, len2 = 0;
EVP_DecodeInit( ctx );
if (EVP_DecodeUpdate( ctx, psz, &len1, pszBase64, lIn ) != -1 &&
EVP_DecodeFinal( ctx, (psz += len1), &len2 ) != -1)
{
if (piSize) *piSize += len1 + len2;
} else {
MyFree( pPtr );
}
}
EVP_ENCODE_CTX_free( ctx );
}
return (PVOID)pPtr;
}
#else
static const char* alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const int alphabet_len = sizeof("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
static void _base64Encode(const unsigned char* input, unsigned int input_len, char* output)
{
unsigned int char_count;
unsigned int bits;
unsigned int input_idx = 0;
unsigned int output_idx = 0;
char_count = 0;
bits = 0;
for (input_idx = 0; input_idx < input_len; input_idx++)
{
bits |= input[input_idx];
char_count++;
if (char_count == 3)
{
output[output_idx++] = alphabet[(bits >> 18) & 0x3f];
output[output_idx++] = alphabet[(bits >> 12) & 0x3f];
output[output_idx++] = alphabet[(bits >> 6) & 0x3f];
output[output_idx++] = alphabet[bits & 0x3f];
bits = 0;
char_count = 0;
}
else
{
bits <<= 8;
}
}
if (char_count)
{
if (char_count == 1)
{
bits <<= 8;
}
output[output_idx++] = alphabet[(bits >> 18) & 0x3f];
output[output_idx++] = alphabet[(bits >> 12) & 0x3f];
if (char_count > 1)
{
output[output_idx++] = alphabet[(bits >> 6) & 0x3f];
}
else
{
output[output_idx++] = '=';
}
output[output_idx++] = '=';
}
output[output_idx++] = 0;
}
static int _base64Decode(const unsigned char* input, unsigned int input_len, unsigned char* output, unsigned int* output_len)
{
static char inalphabet[256], decoder[256];
int c = 0, char_count, errors = 0;
unsigned int input_idx = 0;
unsigned int output_idx = 0;
int bits = 0, i = 0;
for (; i < alphabet_len; ++i)
{
inalphabet[alphabet[i]] = 1;
decoder[alphabet[i]] = i;
}
char_count = 0;
for (input_idx = 0; input_idx < input_len; input_idx++)
{
c = input[input_idx];
if (c == '=')
break;
if (c > 255 || !inalphabet[c])
continue;
bits += decoder[c];
char_count++;
if (char_count == 4)
{
output[output_idx++] = (bits >> 16);
output[output_idx++] = ((bits >> 8) & 0xff);
output[output_idx++] = (bits & 0xff);
bits = 0;
char_count = 0;
}
else
{
bits <<= 6;
}
}
if (c == '=')
{
switch (char_count)
{
case 1:
errors++;
break;
case 2:
output[output_idx++] = (bits >> 10);
break;
case 3:
output[output_idx++] = (bits >> 16);
output[output_idx++] = ((bits >> 8) & 0xff);
break;
}
}
else if (input_idx < input_len)
{
if (char_count)
{
errors++;
}
}
*output_len = output_idx;
return errors;
}
LPSTR EncBase64(_In_ LPCVOID pPtr, _In_ size_t iSize)
{
unsigned int outLength = (iSize + 2) / 3 * 4;
// should be enough to store 8-bit buffers in 6-bit buffers
char* out = (char*)malloc(outLength + 1);
if (out)
_base64Encode(pPtr, iSize, out);
return out;
}
PVOID DecBase64(_In_ LPCSTR pszBase64, _Out_opt_ size_t* piSize)
{
unsigned int outLength = 0;
// should be enough to store 6-bit buffers in 8-bit buffers
char* out = (unsigned char*)malloc(*piSize / 4 * 3 + 1);
if (out)
{
int ret = _base64Decode(pszBase64, *piSize, out, &outLength);
if (ret > 0)
{
free(out);
out = NULL;
outLength = 0;
}
}
*piSize = outLength;
return out;
}
#endif