forked from rfjakob/cshatag
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhash.c
267 lines (226 loc) · 6.49 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
/*
* Copyright (C) 2012 Jakob Unterwurzacher <[email protected]>
* Copyright (C) 2018 Tim Schlueter
*
* 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 of the License, 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, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the author of this program
* gives permission to link the code portions of this program with the
* OpenSSL library under certain conditions as described in each file,
* and distribute linked combinations including the two.
* You must obey the GNU General Public License in all respects for all
* of the code used other than OpenSSL. If you modify this file(s)
* with this exception, you may extend this exception to your version
* of the file(s), but you are not obligated to do so. If you do not
* wish to do so, delete this exception statement from your version.
* If you delete this exception statement from all source files in the
* program, then also delete it here.
*/
/** @file
* Hash helper functions for b2tag.
*/
#include "hash.h"
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include "utilities.h"
/** The size of the file read buffer. */
#define BUFSZ 65536
/** The function signature of the OpenSSL EVP algorithms. */
typedef const EVP_MD *(*evp_func)(void);
/** Structure containing info about a hash algorithm. */
struct alg_data {
/** The name of the algorithm (lowercase). */
const char *name;
/** The OpenSSL EVP function of the algorithm. */
evp_func md;
};
/** Data about all the hash algorithms b2tag supports. */
static struct alg_data hash_alg_data[] = {
[HASH_ALG_MD5] = {
.name = "md5",
.md = EVP_md5
},
[HASH_ALG_SHA1] = {
.name ="sha1",
.md = EVP_sha1
},
[HASH_ALG_SHA256] = {
.name ="sha256",
.md = EVP_sha256
},
[HASH_ALG_SHA512] = {
.name ="sha512",
.md = EVP_sha512
},
[HASH_ALG_BLAKE2B] = {
.name ="blake2b512",
.md = EVP_blake2b512
},
[HASH_ALG_BLAKE2S] = {
.name ="blake2s256",
.md = EVP_blake2s256
},
};
/**
* Converts a raw array into a hex string.
*
* @param out The buffer to store the hex string in.
* @param outlen The length of @p out.
* @param bin The input raw data array.
* @param len The length of @p bin.
*
* @note @p out must be at least (@p len * 2) + 1 in length.
*/
static int bin2hex(char *out, int outlen, unsigned char *bin, int len)
{
char hexval[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
int i;
assert(out != NULL);
assert(len >= 0);
assert(bin != NULL || len == 0);
if (outlen <= (len * 2)) {
pr_err("Hash buffer is too small: %d < %d\n", outlen, len * 2 + 1);
return -1;
}
for (i = 0; i < len; i++) {
out[2 * i] = hexval[(bin[i] >> 4) & 0x0F];
out[2 * i + 1] = hexval[bin[i] & 0x0F];
}
out[2 * len] = '\0';
return 0;
}
/**
* Hash the contents of file @p fd using the @p alg hash algorithm.
*
* Then store the ASCII hex representation of the resulting hash in @p hashbuf.
*
* @param fd The file to hash.
* @param hashbuf Where to store the resulting hash value.
* @param hashlen The length of @p hash.
* @param alg The hash algorithm to use.
*
* @retval 0 The contents of @p fd were successfully hashed.
* @retval !0 An error occurred while hashing the contents of @p fd.
*/
int fhash(int fd, char *hashbuf, int hashlen, hash_alg_t alg)
{
int err = -1;
EVP_MD_CTX *c;
char *buf;
unsigned char rawhash[EVP_MAX_MD_SIZE];
ssize_t len;
int alg_len;
assert(fd >= 0);
assert(hashbuf != NULL);
assert(hashlen > 0);
assert(alg < ARRAY_SIZE(hash_alg_data));
assert(hash_alg_data[alg].md != NULL);
assert(hash_alg_data[alg].md() != NULL);
buf = malloc(BUFSZ);
c = EVP_MD_CTX_new();
if (buf == NULL || c == NULL) {
pr_err("Insufficient memory for hashing file");
goto out;
}
if (EVP_DigestInit_ex(c, hash_alg_data[alg].md(), NULL) == 0) {
pr_err("Failed to initialize digest\n");
goto out;
}
/* The length of the algorithm's hash. */
alg_len = EVP_MD_CTX_size(c);
assert(alg_len > 0);
assert(alg_len <= MAX_HASH_SIZE);
if ((alg_len * 2) >= hashlen) {
pr_err("Hash exceeds buffer size: %d > %d\n", alg_len * 2 + 1, hashlen);
goto out;
}
while ((len = read(fd, buf, BUFSZ)) > 0) {
if (EVP_DigestUpdate(c, buf, (size_t)len) == 0) {
pr_err("Failed to update digest\n");
goto out;
}
}
if (len < 0) {
pr_err("Error reading file: %m\n");
goto out;
}
if (EVP_DigestFinal_ex(c, rawhash, (unsigned int *)&alg_len) == 0) {
pr_err("Failed to finalize digest\n");
goto out;
}
assert(alg_len > 0);
if (bin2hex(hashbuf, hashlen, rawhash, alg_len) != 0)
goto out;
err = 0;
out:
EVP_MD_CTX_free(c);
free(buf);
return err;
}
/**
* Returns the hash size of @p alg.
*
* @param alg The algorithm to use.
*
* @returns Returns the hash size of the @p alg hash algorithm.
* @returns Returns 0 if an error occurs.
*/
int get_alg_size(hash_alg_t alg)
{
int len;
assert(alg < ARRAY_SIZE(hash_alg_data));
assert(hash_alg_data[alg].md != NULL);
assert(hash_alg_data[alg].md() != NULL);
len = EVP_MD_size(hash_alg_data[alg].md());
if (len <= 0 || len > EVP_MAX_MD_SIZE) {
pr_err("Invalid algorithm size for alg %d: %d\n", (int)alg, len);
len = 0;
}
return len;
}
/**
* Returns the name of @p alg as a string.
*
* @param alg The algorithm to look up.
*
* @returns Returns the name of the @p alg hash algorithm.
*/
const char * get_alg_name(hash_alg_t alg)
{
assert(alg < ARRAY_SIZE(hash_alg_data));
assert(hash_alg_data[alg].name != NULL);
return hash_alg_data[alg].name;
}
/**
* Looks up a hash algorithm by name and sets @p alg if not NULL.
*
* @param name The algorithm to look up.
* @param alg Where to store the algorithm type (can be NULL).
*
* @returns Returns 0 on success and a negative number on failure.
*/
int get_alg_by_name(const char *name, hash_alg_t *alg)
{
size_t i;
assert(name != NULL);
for (i = 0; i < ARRAY_SIZE(hash_alg_data); i++) {
if (strcmp(hash_alg_data[i].name, name) == 0) {
if (alg != NULL)
*alg = (hash_alg_t)i;
return 0;
}
}
return -1;
}