forked from negrutiu/nsis-nscurl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.c
165 lines (140 loc) · 4.37 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
//? Marius Negrutiu (mailto:[email protected]) :: 2019/11/20
//? Compute hashes using our embedded cryptographic engine
#include "main.h"
#include "crypto.h"
#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;
}