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
|
#include "opensslmanager.h"
#include <openssl/crypto.h>
#include <openssl/md5.h>
#include <openssl/md4.h>
#include <openssl/md2.h>
#include <openssl/sha.h>
#include <openssl/ripemd.h>
// NEED to convert this to using EVP one of these days...
// https://www.openssl.org/docs/manmaster/man3/EVP_DigestInit.html
// -sappho
#define MD5_FILE_BUFFER_SIZE 1024*16
// #define MD4_FILE_BUFFER_SIZE 1024*16
// #define MD2_FILE_BUFFER_SIZE 1024*16
// #define SHA_FILE_BUFFER_SIZE 1024*16
#define SHA1_FILE_BUFFER_SIZE 1024*16
#define SHA224_FILE_BUFFER_SIZE 1024*16
#define SHA256_FILE_BUFFER_SIZE 1024*16
#define SHA384_FILE_BUFFER_SIZE 1024*16
#define SHA512_FILE_BUFFER_SIZE 1024*16
// #define RIPEMD160_FILE_BUFFER_SIZE 1024*16
#include <openssl/evp.h>
OpensslManager g_OpensslManager;
static IMutex **ssl_lockarray;
static void MD5_File(FILE *file, unsigned char **output, int *outlength)
{
*output = new unsigned char[MD5_DIGEST_LENGTH];
*outlength = MD5_DIGEST_LENGTH;
MD5_CTX c;
int i;
unsigned char buf[MD5_FILE_BUFFER_SIZE];
MD5_Init(&c);
for (;;)
{
i = fread(buf,1,MD5_FILE_BUFFER_SIZE,file);
if(i <= 0)
break;
MD5_Update(&c,buf,(unsigned long)i);
}
MD5_Final(*output, &c);
}
static void SHA1_File(FILE *file, unsigned char **output, int *outlength)
{
*output = new unsigned char[SHA_DIGEST_LENGTH];
*outlength = SHA_DIGEST_LENGTH;
SHA_CTX c;
int i;
unsigned char buf[SHA1_FILE_BUFFER_SIZE];
SHA1_Init(&c);
for (;;)
{
i = fread(buf,1,SHA1_FILE_BUFFER_SIZE,file);
if(i <= 0)
break;
SHA1_Update(&c,buf,(unsigned long)i);
}
SHA1_Final(*output, &c);
}
static void SHA224_File(FILE *file, unsigned char **output, int *outlength)
{
*output = new unsigned char[SHA224_DIGEST_LENGTH];
*outlength = SHA224_DIGEST_LENGTH;
SHA256_CTX c;
int i;
unsigned char buf[SHA224_FILE_BUFFER_SIZE];
SHA224_Init(&c);
for (;;)
{
i = fread(buf,1,SHA224_FILE_BUFFER_SIZE,file);
if(i <= 0)
break;
SHA224_Update(&c,buf,(unsigned long)i);
}
SHA224_Final(*output, &c);
}
static void SHA256_File(FILE *file, unsigned char **output, int *outlength)
{
*output = new unsigned char[SHA256_DIGEST_LENGTH];
*outlength = SHA256_DIGEST_LENGTH;
SHA256_CTX c;
int i;
unsigned char buf[SHA256_FILE_BUFFER_SIZE];
SHA256_Init(&c);
for (;;)
{
i = fread(buf,1,SHA256_FILE_BUFFER_SIZE,file);
if(i <= 0)
break;
SHA256_Update(&c,buf,(unsigned long)i);
}
SHA256_Final(*output, &c);
}
static void SHA384_File(FILE *file, unsigned char **output, int *outlength)
{
*output = new unsigned char[SHA384_DIGEST_LENGTH];
*outlength = SHA384_DIGEST_LENGTH;
SHA512_CTX c;
int i;
unsigned char buf[SHA384_FILE_BUFFER_SIZE];
SHA384_Init(&c);
for (;;)
{
i = fread(buf,1,SHA384_FILE_BUFFER_SIZE,file);
if(i <= 0)
break;
SHA384_Update(&c,buf,(unsigned long)i);
}
SHA384_Final(*output, &c);
}
static void SHA512_File(FILE *file, unsigned char **output, int *outlength)
{
*output = new unsigned char[SHA512_DIGEST_LENGTH];
*outlength = SHA512_DIGEST_LENGTH;
SHA512_CTX c;
int i;
unsigned char buf[SHA512_FILE_BUFFER_SIZE];
SHA512_Init(&c);
for (;;)
{
i = fread(buf,1,SHA512_FILE_BUFFER_SIZE,file);
if(i <= 0)
break;
SHA512_Update(&c,buf,(unsigned long)i);
}
SHA512_Final(*output, &c);
}
static void ssl_locking_callback(int mode, int type, const char *file, int line)
{
if(mode & CRYPTO_LOCK)
{
ssl_lockarray[type]->Lock();
} else {
ssl_lockarray[type]->Unlock();
}
}
#ifdef PLATFORM_LINUX
static unsigned long ssl_id_function(void)
{
return ((unsigned long)getpid());
}
#endif
void OpensslManager::SDK_OnLoad()
{
ssl_lockarray = (IMutex **)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(IMutex *));
for(int i = 0; i < CRYPTO_num_locks(); i++)
{
ssl_lockarray[i] = threader->MakeMutex();
}
#ifdef PLATFORM_LINUX
CRYPTO_set_id_callback(ssl_id_function);
#endif
CRYPTO_set_locking_callback(ssl_locking_callback);
}
void OpensslManager::SDK_OnUnload()
{
#ifdef PLATFORM_LINUX
CRYPTO_set_id_callback(NULL);
#endif
CRYPTO_set_locking_callback(NULL);
for (int i=0; i<CRYPTO_num_locks(); i++)
{
ssl_lockarray[i]->DestroyThis();
}
OPENSSL_free(ssl_lockarray);
}
bool OpensslManager::HashFile(Openssl_Hash algorithm, FILE *pFile, unsigned char **output, int *outlength)
{
if(pFile == NULL)
return false;
switch(algorithm)
{
case Openssl_Hash_MD5:
MD5_File(pFile, output, outlength);
return true;
case Openssl_Hash_SHA1:
SHA1_File(pFile, output, outlength);
return true;
case Openssl_Hash_SHA224:
SHA224_File(pFile, output, outlength);
return true;
case Openssl_Hash_SHA256:
SHA256_File(pFile, output, outlength);
return true;
case Openssl_Hash_SHA384:
SHA384_File(pFile, output, outlength);
return true;
case Openssl_Hash_SHA512:
SHA512_File(pFile, output, outlength);
return true;
}
return false;
}
bool OpensslManager::HashString(Openssl_Hash algorithm, unsigned char *input, int size, unsigned char *output, int *outlength)
{
switch(algorithm)
{
case Openssl_Hash_MD5:
MD5(input, size, output);
*outlength = MD5_DIGEST_LENGTH;
return true;
case Openssl_Hash_SHA1:
SHA1(input, size, output);
*outlength = SHA_DIGEST_LENGTH;
return true;
case Openssl_Hash_SHA224:
SHA224(input, size, output);
*outlength = SHA224_DIGEST_LENGTH;
return true;
case Openssl_Hash_SHA256:
SHA256(input, size, output);
*outlength = SHA256_DIGEST_LENGTH;
return true;
case Openssl_Hash_SHA384:
SHA384(input, size, output);
*outlength = SHA384_DIGEST_LENGTH;
return true;
case Openssl_Hash_SHA512:
SHA512(input, size, output);
*outlength = SHA512_DIGEST_LENGTH;
return true;
}
return false;
}
|