-
Notifications
You must be signed in to change notification settings - Fork 2
/
Hash.c
250 lines (191 loc) · 5.79 KB
/
Hash.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
#include "Hash.h"
#include "String.h"
#include "Encodings.h"
#include "List.h"
#include "HashCRC32.h"
#include "HashMD5.h"
#include "HashSHA.h"
#include "HashJH.h"
#include "HashWhirlpool.h"
#include "HashOpenSSL.h"
#include "HMAC.h"
static ListNode *HashTypes=NULL;
int HashEncodingFromStr(const char *Str)
{
return(EncodingParse(Str));
}
void HashRegister(const char *Name, int Len, HASH_INIT_FUNC Init)
{
if (! HashTypes) HashTypes=ListCreate();
if (! ListFindNamedItem(HashTypes, Name)) ListAddTypedItem(HashTypes, Len, Name, Init);
}
void HashRegisterAll()
{
HashRegisterCRC32();
HashRegisterMD5();
HashRegisterSHA();
HashRegisterJH();
HashRegisterWhirlpool();
HashRegisterOpenSSL();
}
void HashDestroy(HASH *Hash)
{
//Hash->Ctx is destroyed in 'HashFinish'
DestroyString(Hash->Key1);
DestroyString(Hash->Key2);
DestroyString(Hash->Type);
if (Hash->Ctx) free(Hash->Ctx);
free(Hash);
}
char *HashAvailableTypes(char *RetStr)
{
ListNode *Curr;
if (! HashTypes) HashRegisterAll();
RetStr=CopyStr(RetStr,"");
Curr=ListGetNext(HashTypes);
while (Curr)
{
RetStr=MCatStr(RetStr, Curr->Tag, ",", NULL);
Curr=ListGetNext(Curr);
}
return(RetStr);
}
//Setup a hash. This can accept a "hash chain" in the form "whirl,sha1,md5" where the output
//of one has is fed into the next. This function sets up the "InitialType" (first hash in the list)
//the user then feeds data into this, and when 'HashFinish' is called the resulting hash output
//is hashed with the other hashes in the chain
HASH *HashInit(const char *Type)
{
HASH *Hash=NULL;
ListNode *Node;
HASH_INIT_FUNC InitFunc;
char *InitialType=NULL;
if (! HashTypes) HashRegisterAll();
GetToken(Type, ",", &InitialType, 0);
if (strncasecmp(InitialType, "hmac-", 5) == 0) Hash=HMACInit(InitialType+5);
else
{
Node=ListFindNamedItem(HashTypes, InitialType);
if (Node)
{
InitFunc=(HASH_INIT_FUNC) Node->Item;
Hash=(HASH *) calloc(1,sizeof(HASH));
Hash->Type=CopyStr(Hash->Type,Type);
if (! InitFunc(Hash, Node->Tag, Node->ItemType))
{
HashDestroy(Hash);
Hash=NULL;
RaiseError(0, "HashInit", "Failed to setup Hash Type: '%s'", InitialType);
}
}
else RaiseError(0, "HashInit", "Unsupported Hash Type: '%s'", InitialType);
}
Destroy(InitialType);
return(Hash);
}
int HashFinish(HASH *Hash, int Encoding, char **Return)
{
char *Token=NULL, *Bytes=NULL, *Hashed=NULL;
const char *ptr;
int len;
len=Hash->Finish(Hash, &Bytes);
//The first hashtype is the 'InitialType' of HashInit, and will
//already have been processed, so throw it awway here
ptr=GetToken(Hash->Type, ",", &Token, 0);
while (StrValid(ptr))
{
//process each hash type in the hash chain
ptr=GetToken(ptr, ",", &Token, 0);
len=HashBytes(&Hashed, Token, Bytes, len, 0);
Bytes=SetStrLen(Bytes, len);
memcpy(Bytes,Hashed,len);
}
if (Encoding > 0)
{
*Return=EncodeBytes(*Return, Bytes, len, Encoding);
len=StrLen(*Return);
}
else
{
*Return=SetStrLen(*Return, len);
memcpy(*Return, Bytes, len);
}
DestroyString(Hashed);
DestroyString(Token);
DestroyString(Bytes);
return(len);
}
int HashBytes(char **Return, const char *Type, const char *text, int len, int Encoding)
{
HASH *Hash;
int result;
Hash=HashInit(Type);
if (! Hash) return(0);
Hash->Update(Hash, text, len);
result=HashFinish(Hash, Encoding, Return);
return(result);
}
int HashBytes2(const char *Type, int Encoding, const char *text, int len, char **RetStr)
{
return(HashBytes(RetStr, Type, text, len, Encoding));
}
int PBK2DF2(char **Return, const char *Type, const char *Bytes, int Len, const char *Salt, int SaltLen, uint32_t Rounds, int Encoding)
{
char *Tempstr=NULL, *Hash=NULL;
uint32_t RoundsBE;
int i, len, hlen=0, dlen=0;
//Network byte order is big endian
RoundsBE=htonl(Rounds);
Tempstr=SetStrLen(Tempstr, Len + SaltLen + 20);
dlen=Len;
//for hmac- pbk2df the 'Bytes' are the key, and thus the data hashed is just the salt plus the rounds counter
if (strncasecmp(Type, "hmac-", 5) !=0) dlen=0;
memcpy(Tempstr, Bytes, dlen);
memcpy(Tempstr + dlen, Salt, SaltLen);
memcpy(Tempstr + dlen + SaltLen, &RoundsBE, sizeof(uint32_t));
len=dlen + SaltLen + sizeof(uint32_t);
for (i=0; i <Rounds; i++)
{
if (strncasecmp(Type, "hmac-", 5)==0) hlen=HMACBytes(&Hash, Type, Bytes, Len, Tempstr, len, 0);
else hlen=HashBytes(&Hash, Type, Tempstr, len, 0);
Tempstr=SetStrLen(Tempstr, Len + hlen + 20);
memcpy(Tempstr, Bytes, Len);
memcpy(Tempstr + Len, Hash, hlen);
len=Len + hlen;
}
*Return=EncodeBytes(*Return, Hash, hlen, Encoding);
DestroyString(Tempstr);
DestroyString(Hash);
return(StrLen(*Return));
}
int HashSTREAM(char **Return, const char *Type, STREAM *S, int Encoding)
{
HASH *Hash;
char *Tempstr=NULL;
int result;
if (! S) return(FALSE);
Hash=HashInit(Type);
if (! Hash) return(FALSE);
Tempstr=SetStrLen(Tempstr,4096);
result=STREAMReadBytes(S,Tempstr,4096);
while (result !=EOF)
{
Hash->Update(Hash, Tempstr, result);
result=STREAMReadBytes(S,Tempstr,4096);
}
DestroyString(Tempstr);
result=HashFinish(Hash, Encoding, Return);
return(result);
}
int HashFile(char **Return, const char *Type, const char *Path, int Encoding)
{
int result=FALSE;
STREAM *S;
S=STREAMOpen(Path,"r");
if (S)
{
result=HashSTREAM(Return, Type, S, Encoding);
STREAMClose(S);
}
return(result);
}