-
Notifications
You must be signed in to change notification settings - Fork 6
/
hash.c
358 lines (305 loc) · 9.7 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
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
/* $Id: hash.c,v 1.4 2005/05/14 23:20:30 harbourn Exp $
* dcfldd - The Enhanced Forensic DD
* By Nicholas Harbour
*/
/* Copyright (C) 85, 90, 91, 1995-2001, 2005 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/* GNU dd originally written by Paul Rubin, David MacKenzie, and Stuart Kemp. */
#include "dcfldd.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "md5.h"
#include "sha1.h"
#include "sha2.h"
#include "hash.h"
#include "log.h"
hashflag_t hashflags = 0;
/* Md5 global data */
MD5_CTX MD5_total_context;
MD5_CTX MD5_window_context;
MD5_CTX MD5_vtotal_context;
MD5_CTX MD5_vwindow_context;
char MD5_hashstr[MD5_DIGEST_STRING_LENGTH + 1] = {'\0'};
/* SHA1 global data */
SHA1Context SHA1_total_context;
SHA1Context SHA1_window_context;
SHA1Context SHA1_vtotal_context;
SHA1Context SHA1_vwindow_context;
char SHA1_hashstr[SHA1_DIGEST_STRING_LENGTH + 1] = {'\0'};
/* SHA256 global data */
SHA256_CTX SHA256_total_context;
SHA256_CTX SHA256_window_context;
SHA256_CTX SHA256_vtotal_context;
SHA256_CTX SHA256_vwindow_context;
char SHA256_hashstr[SHA256_DIGEST_STRING_LENGTH + 1] = {'\0'};
/* SHA384 global data */
SHA384_CTX SHA384_total_context;
SHA384_CTX SHA384_window_context;
SHA384_CTX SHA384_vtotal_context;
SHA384_CTX SHA384_vwindow_context;
char SHA384_hashstr[SHA384_DIGEST_STRING_LENGTH + 1] = {'\0'};
/* SHA512 global data */
SHA512_CTX SHA512_total_context;
SHA512_CTX SHA512_window_context;
SHA512_CTX SHA512_vtotal_context;
SHA512_CTX SHA512_vwindow_context;
char SHA512_hashstr[SHA512_DIGEST_STRING_LENGTH + 1] = {'\0'};
off_t hash_windowlen = 0;
off_t window_beginning = 0;
off_t bytes_in_window = 0;
off_t bytes_in_total = 0;
void (*hashinit)(void *);
void (*hashupdate)(void *, const void *, size_t);
void (*hashfinal)(void *, void *);
void *hashstr_buf;
size_t hashstr_buf_size;
FILE *hash_log;
/* Hash algorithms */
hashlist_t *ihashlist;
hashtype_t hashops[] =
{
{"md5",
1,
&MD5_window_context,
&MD5_total_context,
&MD5_vwindow_context,
&MD5_vtotal_context,
(void (*)(void *)) MD5Init,
(void (*)(void *, const void *, size_t)) MD5Update,
(void (*)(void *, void *)) MD5Final,
&MD5_hashstr[0],
sizeof (MD5_hashstr),
NULL},
{"sha1",
1<<1,
&SHA1_window_context,
&SHA1_total_context,
&SHA1_vwindow_context,
&SHA1_vtotal_context,
(void (*)(void *)) SHA1Init,
(void (*)(void *, const void *, size_t)) SHA1Update,
(void (*)(void *, void *)) SHA1End,
&SHA1_hashstr[0],
sizeof (SHA1_hashstr),
NULL},
{"sha256",
1<<2,
&SHA256_window_context,
&SHA256_total_context,
&SHA256_vwindow_context,
&SHA256_vtotal_context,
(void (*)(void *)) SHA256_Init,
(void (*)(void *, const void *, size_t)) SHA256_Update,
(void (*)(void *, void *)) SHA256_End,
SHA256_hashstr,
sizeof (SHA256_hashstr),
NULL},
{"sha384",
1<<3,
&SHA384_window_context,
&SHA384_total_context,
&SHA384_vwindow_context,
&SHA384_vtotal_context,
(void (*)(void *)) SHA384_Init,
(void (*)(void *, const void *, size_t)) SHA384_Update,
(void (*)(void *, void *)) SHA384_End,
&SHA384_hashstr[0],
sizeof (SHA384_hashstr),
NULL},
{"sha512",
1<<4,
&SHA512_window_context,
&SHA512_total_context,
&SHA512_vwindow_context,
&SHA512_vtotal_context,
(void (*)(void *)) SHA512_Init,
(void (*)(void *, const void *, size_t)) SHA512_Update,
(void (*)(void *, void *)) SHA512_End,
&SHA512_hashstr[0],
sizeof (SHA512_hashstr),
NULL},
{NULL,
0,
NULL,
NULL,
NULL,
NULL,
(void (*)(void *)) NULL,
(void (*)(void *, const void *, size_t)) NULL,
(void (*)(void *, void *)) NULL,
NULL,
0,
NULL}
};
static void add_hash(hashlist_t **hashlist, int hash)
{
hashlist_t *hlptr = *hashlist;
int i;
if (hlptr == NULL) {
hlptr = malloc(sizeof (hashlist_t));
*hashlist = hlptr;
} else {
for ( ; hlptr->next != NULL; hlptr = hlptr->next)
;
hlptr->next = malloc(sizeof (hashlist_t));
hlptr = hlptr->next;
}
hlptr->next = NULL;
hlptr->hash = &hashops[hash];
}
/* add all the appropriate hashops according to the flags */
void init_hashlist(hashlist_t **hashlist, hashflag_t flags)
{
int i;
for (i = 0; hashops[i].name != NULL; i++)
if (hashops[i].flag & flags)
add_hash(hashlist, i);
}
/* not to be confused with init_hashlist, this function calls
* the hashtype specific init function for each hash type in
* the list */
void hashl_init(hashlist_t *hashlist, int context)
{
hashlist_t *hptr;
for (hptr = hashlist; hptr != NULL; hptr = hptr->next) {
void *ctx;
switch (context) {
case WINDOW_CTX:
ctx = hptr->hash->window_context;
break;
case TOTAL_CTX:
ctx = hptr->hash->total_context;
break;
case VWINDOW_CTX:
ctx = hptr->hash->vwindow_context;
break;
case VTOTAL_CTX:
ctx = hptr->hash->vtotal_context;
break;
default:
internal_error("unreachable branch encountered in hashl_init()");
break;
}
(hptr->hash->init)(ctx);
}
}
void hashl_update(hashlist_t *hashlist, int context, const void *buf, size_t len)
{
hashlist_t *hptr;
for (hptr = hashlist; hptr != NULL; hptr = hptr->next) {
void *ctx;
switch (context) {
case WINDOW_CTX:
ctx = hptr->hash->window_context;
break;
case TOTAL_CTX:
ctx = hptr->hash->total_context;
break;
case VWINDOW_CTX:
ctx = hptr->hash->vwindow_context;
break;
case VTOTAL_CTX:
ctx = hptr->hash->vtotal_context;
break;
default:
internal_error("unreachable branch encountered in hashl_update()");
break;
}
(hptr->hash->update)(ctx, buf, len);
}
}
void hashl_final(hashlist_t *hashlist, int context)
{
hashlist_t *hptr;
for (hptr = hashlist; hptr != NULL; hptr = hptr->next) {
void *ctx;
switch (context) {
case WINDOW_CTX:
ctx = hptr->hash->window_context;
break;
case TOTAL_CTX:
ctx = hptr->hash->total_context;
break;
case VWINDOW_CTX:
ctx = hptr->hash->vwindow_context;
break;
case VTOTAL_CTX:
ctx = hptr->hash->vtotal_context;
break;
default:
internal_error("unreachable branch encountered in hashl_final()");
break;
}
/* note that this writes the hash string to the global buffer
* for the specific hashtype, when calling this multiple times
* (i.e. like in verify) copy that buffer out before finalizing
* another list */
(hptr->hash->final)(ctx, hptr->hash->hashstr_buf);
}
}
void hash_update_buf(hashlist_t *hashlist, int winctx, int ttlctx,
void *buf, size_t len)
{
if (hash_windowlen != 0) {
hashl_update(hashlist, winctx, buf, len);
if (winctx == WINDOW_CTX) /* don't do this for verify or you'll get double */
bytes_in_window += len;
}
hashl_update(hashlist, ttlctx, buf, len);
if(ttlctx == TOTAL_CTX)
bytes_in_total += len;
}
void hash_update(hashlist_t *hashlist, void *buf, size_t len)
{
size_t left_in_window = hash_windowlen - bytes_in_window;
if (bytes_in_total == 0)
hashl_init(hashlist, TOTAL_CTX);
if (hash_windowlen == 0)
hash_update_buf(hashlist, WINDOW_CTX, TOTAL_CTX, buf, len);
else {
if (bytes_in_window == 0)
hashl_init(hashlist, WINDOW_CTX);
if (len >= left_in_window) {
hash_update_buf(hashlist, WINDOW_CTX, TOTAL_CTX, buf, left_in_window);
hashl_final(hashlist, WINDOW_CTX);
display_windowhash(hashlist, hash_windowlen);
window_beginning += hash_windowlen;
bytes_in_window = 0;
hash_update(hashlist, buf + left_in_window, len - left_in_window);
} else
hash_update_buf(hashlist, WINDOW_CTX, TOTAL_CTX, buf, len);
}
}
void display_windowhash(hashlist_t *hashlist, off_t windowlen)
{
hashlist_t *hptr;
for (hptr = hashlist; hptr != NULL; hptr = hptr->next)
log_hashwindow(hptr->hash, window_beginning, (window_beginning + windowlen),
input_blocksize, hptr->hash->hashstr_buf);
}
void display_totalhash(hashlist_t *hashlist, int ttlctx)
{
hashlist_t *hptr;
hashl_final(hashlist, ttlctx);
for (hptr = hashlist; hptr != NULL; hptr = hptr->next)
log_hashtotal(hptr->hash, 0, 0,
input_blocksize, hptr->hash->hashstr_buf);
}
void hash_remainder(hashlist_t *hashlist, int winctx)
{
if (hash_windowlen > 0 && bytes_in_window > 0) {
hashl_final(hashlist, winctx);
display_windowhash(hashlist, bytes_in_window);
}
}