Directory: | cvmfs/ |
---|---|
File: | cvmfs/crypto/hash.cc |
Date: | 2025-05-11 02:35:43 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 267 | 298 | 89.6% |
Branches: | 169 | 268 | 63.1% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /** | ||
2 | * This file is part of the CernVM File System. | ||
3 | */ | ||
4 | |||
5 | |||
6 | #include "crypto/hash.h" | ||
7 | |||
8 | #include <alloca.h> | ||
9 | #include <errno.h> | ||
10 | #include <fcntl.h> | ||
11 | #include <openssl/hmac.h> | ||
12 | #include <openssl/md5.h> | ||
13 | #include <openssl/ripemd.h> | ||
14 | #include <openssl/sha.h> | ||
15 | #include <unistd.h> | ||
16 | |||
17 | #include <cstdio> | ||
18 | #include <cstring> | ||
19 | |||
20 | #include "crypto/openssl_version.h" | ||
21 | #include "util/exception.h" | ||
22 | #include "KeccakHash.h" | ||
23 | |||
24 | |||
25 | using namespace std; // NOLINT | ||
26 | |||
27 | #ifdef CVMFS_NAMESPACE_GUARD | ||
28 | namespace CVMFS_NAMESPACE_GUARD { | ||
29 | #endif | ||
30 | |||
31 | namespace shash { | ||
32 | |||
33 | const char *kAlgorithmIds[] = | ||
34 | {"", "", "-rmd160", "-shake128", ""}; | ||
35 | |||
36 | |||
37 | 66 | bool HexPtr::IsValid() const { | |
38 | 66 | const unsigned l = str->length(); | |
39 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 63 times.
|
66 | if (l == 0) |
40 | 3 | return false; | |
41 | 63 | const char *c = str->data(); // Walks through the string | |
42 | 63 | unsigned i = 0; // String position of *c | |
43 | |||
44 |
2/2✓ Branch 0 taken 2460 times.
✓ Branch 1 taken 15 times.
|
2475 | for ( ; i < l; ++i, ++c) { |
45 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 2418 times.
|
2460 | if (*c == '-') |
46 | 42 | break; | |
47 |
6/8✓ Branch 0 taken 2418 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2418 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 981 times.
✓ Branch 5 taken 1437 times.
✓ Branch 6 taken 6 times.
✓ Branch 7 taken 975 times.
|
2418 | if ((*c < '0') || (*c > 'f') || ((*c > '9') && (*c < 'a'))) |
48 | 6 | return false; | |
49 | } | ||
50 | |||
51 | // Walk through all algorithms | ||
52 |
2/2✓ Branch 0 taken 210 times.
✓ Branch 1 taken 45 times.
|
255 | for (unsigned j = 0; j < kAny; ++j) { |
53 | 210 | const unsigned hex_length = 2*kDigestSizes[j]; | |
54 | 210 | const unsigned algo_id_length = kAlgorithmIdSizes[j]; | |
55 |
2/2✓ Branch 0 taken 66 times.
✓ Branch 1 taken 144 times.
|
210 | if (i == hex_length) { |
56 | // Right suffix? | ||
57 |
4/4✓ Branch 0 taken 183 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 159 times.
✓ Branch 3 taken 24 times.
|
195 | for ( ; (i < l) && (i-hex_length < algo_id_length); ++i, ++c) { |
58 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 129 times.
|
159 | if (*c != kAlgorithmIds[j][i-hex_length]) |
59 | 30 | break; | |
60 | } | ||
61 |
3/4✓ Branch 0 taken 12 times.
✓ Branch 1 taken 54 times.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
|
66 | if ((i == l) && (l == hex_length + algo_id_length)) |
62 | 12 | return true; | |
63 | 54 | i = hex_length; | |
64 | 54 | c = str->data() + i; | |
65 | } | ||
66 | } | ||
67 | |||
68 | 45 | return false; | |
69 | } | ||
70 | |||
71 | |||
72 | ✗ | Algorithms ParseHashAlgorithm(const string &algorithm_option) { | |
73 | ✗ | if (algorithm_option == "sha1") | |
74 | ✗ | return kSha1; | |
75 | ✗ | if (algorithm_option == "rmd160") | |
76 | ✗ | return kRmd160; | |
77 | ✗ | if (algorithm_option == "shake128") | |
78 | ✗ | return kShake128; | |
79 | ✗ | return kAny; | |
80 | } | ||
81 | |||
82 | |||
83 | 58067 | Any MkFromHexPtr(const HexPtr hex, const char suffix) { | |
84 | 58067 | Any result; | |
85 | |||
86 | 58067 | const unsigned length = hex.str->length(); | |
87 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 58064 times.
|
58067 | if (length == 2*kDigestSizes[kMd5]) |
88 | 3 | result = Any(kMd5, hex); | |
89 |
2/2✓ Branch 0 taken 57670 times.
✓ Branch 1 taken 397 times.
|
58067 | if (length == 2*kDigestSizes[kSha1]) |
90 | 57670 | result = Any(kSha1, hex); | |
91 | // TODO(jblomer) compare -rmd160, -shake128 | ||
92 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 58046 times.
|
58067 | if ((length == 2*kDigestSizes[kRmd160] + kAlgorithmIdSizes[kRmd160])) |
93 | 21 | result = Any(kRmd160, hex); | |
94 |
2/2✓ Branch 0 taken 27 times.
✓ Branch 1 taken 58040 times.
|
58067 | if ((length == 2*kDigestSizes[kShake128] + kAlgorithmIdSizes[kShake128])) |
95 | 27 | result = Any(kShake128, hex); | |
96 | |||
97 | 58067 | result.suffix = suffix; | |
98 | 58067 | return result; | |
99 | } | ||
100 | |||
101 | |||
102 | /** | ||
103 | * Similar to MkFromHexPtr but the suffix is deducted from the HexPtr string. | ||
104 | */ | ||
105 | 41195 | Any MkFromSuffixedHexPtr(const HexPtr hex) { | |
106 | 41195 | Any result; | |
107 | |||
108 | 41195 | const unsigned length = hex.str->length(); | |
109 |
4/4✓ Branch 0 taken 1352 times.
✓ Branch 1 taken 39843 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1349 times.
|
41195 | if ((length == 2*kDigestSizes[kMd5]) || (length == 2*kDigestSizes[kMd5] + 1)) |
110 | { | ||
111 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 39843 times.
|
39849 | Suffix suffix = (length == 2*kDigestSizes[kMd5] + 1) ? |
112 | 3 | *(hex.str->rbegin()) : kSuffixNone; | |
113 | 39846 | result = Any(kMd5, hex, suffix); | |
114 | } | ||
115 |
2/2✓ Branch 0 taken 40464 times.
✓ Branch 1 taken 731 times.
|
41195 | if ((length == 2*kDigestSizes[kSha1]) || |
116 |
2/2✓ Branch 0 taken 591 times.
✓ Branch 1 taken 39873 times.
|
40464 | (length == 2*kDigestSizes[kSha1] + 1)) |
117 | { | ||
118 |
2/2✓ Branch 0 taken 591 times.
✓ Branch 1 taken 731 times.
|
1913 | Suffix suffix = (length == 2*kDigestSizes[kSha1] + 1) ? |
119 | 591 | *(hex.str->rbegin()) : kSuffixNone; | |
120 | 1322 | result = Any(kSha1, hex, suffix); | |
121 | } | ||
122 |
2/2✓ Branch 0 taken 41192 times.
✓ Branch 1 taken 3 times.
|
41195 | if ((length == 2*kDigestSizes[kRmd160] + kAlgorithmIdSizes[kRmd160]) || |
123 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 41177 times.
|
41192 | (length == 2*kDigestSizes[kRmd160] + kAlgorithmIdSizes[kRmd160] + 1)) |
124 | { | ||
125 | Suffix suffix = | ||
126 | 18 | (length == 2*kDigestSizes[kRmd160] + kAlgorithmIdSizes[kRmd160] + 1) | |
127 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 3 times.
|
18 | ? *(hex.str->rbegin()) |
128 | 18 | : kSuffixNone; | |
129 | 18 | result = Any(kRmd160, hex, suffix); | |
130 | } | ||
131 |
2/2✓ Branch 0 taken 41192 times.
✓ Branch 1 taken 3 times.
|
41195 | if ((length == 2*kDigestSizes[kShake128] + kAlgorithmIdSizes[kShake128]) || |
132 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 41189 times.
|
41192 | (length == 2*kDigestSizes[kShake128] + kAlgorithmIdSizes[kShake128] + 1)) |
133 | { | ||
134 | Suffix suffix = | ||
135 | 6 | (length == 2*kDigestSizes[kShake128] + kAlgorithmIdSizes[kShake128] + 1) | |
136 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | ? *(hex.str->rbegin()) |
137 | 6 | : kSuffixNone; | |
138 | 6 | result = Any(kShake128, hex, suffix); | |
139 | } | ||
140 | |||
141 | 41195 | return result; | |
142 | } | ||
143 | |||
144 | |||
145 | /** | ||
146 | * Allows the caller to create the context on the stack. | ||
147 | */ | ||
148 | 2548610 | unsigned GetContextSize(const Algorithms algorithm) { | |
149 |
4/5✓ Branch 0 taken 1013968 times.
✓ Branch 1 taken 1533670 times.
✓ Branch 2 taken 38 times.
✓ Branch 3 taken 976 times.
✗ Branch 4 not taken.
|
2548610 | switch (algorithm) { |
150 | 1013968 | case kMd5: | |
151 | 1013968 | return sizeof(MD5_CTX); | |
152 | 1533670 | case kSha1: | |
153 | 1533670 | return sizeof(SHA_CTX); | |
154 | 38 | case kRmd160: | |
155 | 38 | return sizeof(RIPEMD160_CTX); | |
156 | 976 | case kShake128: | |
157 | 976 | return sizeof(Keccak_HashInstance); | |
158 | ✗ | default: | |
159 | ✗ | PANIC(kLogDebug | kLogSyslogErr, | |
160 | "tried to generate hash context for unspecified hash. Aborting..."); | ||
161 | } | ||
162 | } | ||
163 | |||
164 | 2548528 | void Init(ContextPtr context) { | |
165 | HashReturn keccak_result; | ||
166 |
4/5✓ Branch 0 taken 1013986 times.
✓ Branch 1 taken 1533546 times.
✓ Branch 2 taken 38 times.
✓ Branch 3 taken 976 times.
✗ Branch 4 not taken.
|
2548528 | switch (context.algorithm) { |
167 | 1013986 | case kMd5: | |
168 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1013986 times.
|
1013986 | assert(context.size == sizeof(MD5_CTX)); |
169 | 1013986 | MD5_Init(reinterpret_cast<MD5_CTX *>(context.buffer)); | |
170 | 1013986 | break; | |
171 | 1533546 | case kSha1: | |
172 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1533546 times.
|
1533546 | assert(context.size == sizeof(SHA_CTX)); |
173 | 1533546 | SHA1_Init(reinterpret_cast<SHA_CTX *>(context.buffer)); | |
174 | 1533510 | break; | |
175 | 38 | case kRmd160: | |
176 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
|
38 | assert(context.size == sizeof(RIPEMD160_CTX)); |
177 | 38 | RIPEMD160_Init(reinterpret_cast<RIPEMD160_CTX *>(context.buffer)); | |
178 | 38 | break; | |
179 | 976 | case kShake128: | |
180 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 976 times.
|
976 | assert(context.size == sizeof(Keccak_HashInstance)); |
181 | 976 | keccak_result = Keccak_HashInitialize_SHAKE128( | |
182 | reinterpret_cast<Keccak_HashInstance *>(context.buffer)); | ||
183 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 976 times.
|
976 | assert(keccak_result == SUCCESS); |
184 | 976 | break; | |
185 | ✗ | default: | |
186 | ✗ | PANIC(NULL); // Undefined hash | |
187 | } | ||
188 | 2548510 | } | |
189 | |||
190 | 284651366 | void Update(const unsigned char *buffer, const unsigned buffer_length, | |
191 | ContextPtr context) | ||
192 | { | ||
193 | HashReturn keccak_result; | ||
194 |
4/5✓ Branch 0 taken 74359040 times.
✓ Branch 1 taken 71879293 times.
✓ Branch 2 taken 69206060 times.
✓ Branch 3 taken 69206985 times.
✗ Branch 4 not taken.
|
284651366 | switch (context.algorithm) { |
195 | 74359040 | case kMd5: | |
196 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 74359040 times.
|
74359040 | assert(context.size == sizeof(MD5_CTX)); |
197 | 74359040 | MD5_Update(reinterpret_cast<MD5_CTX *>(context.buffer), | |
198 | buffer, buffer_length); | ||
199 | 74359040 | break; | |
200 | 71879293 | case kSha1: | |
201 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 71879293 times.
|
71879293 | assert(context.size == sizeof(SHA_CTX)); |
202 | 71879293 | SHA1_Update(reinterpret_cast<SHA_CTX *>(context.buffer), | |
203 | buffer, buffer_length); | ||
204 | 71885791 | break; | |
205 | 69206060 | case kRmd160: | |
206 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 69206060 times.
|
69206060 | assert(context.size == sizeof(RIPEMD160_CTX)); |
207 | 69206060 | RIPEMD160_Update(reinterpret_cast<RIPEMD160_CTX *>(context.buffer), | |
208 | buffer, buffer_length); | ||
209 | 69206060 | break; | |
210 | 69206985 | case kShake128: | |
211 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 69206985 times.
|
69206985 | assert(context.size == sizeof(Keccak_HashInstance)); |
212 | 138413970 | keccak_result = Keccak_HashUpdate(reinterpret_cast<Keccak_HashInstance *>( | |
213 | 69206985 | context.buffer), buffer, buffer_length * 8); | |
214 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 69206985 times.
|
69206985 | assert(keccak_result == SUCCESS); |
215 | 69206985 | break; | |
216 | ✗ | default: | |
217 | ✗ | PANIC(NULL); // Undefined hash | |
218 | } | ||
219 | 284657876 | } | |
220 | |||
221 | 2536823 | void Final(ContextPtr context, Any *any_digest) { | |
222 | HashReturn keccak_result; | ||
223 |
4/5✓ Branch 0 taken 1013962 times.
✓ Branch 1 taken 1522273 times.
✓ Branch 2 taken 38 times.
✓ Branch 3 taken 976 times.
✗ Branch 4 not taken.
|
2536823 | switch (context.algorithm) { |
224 | 1013962 | case kMd5: | |
225 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1013962 times.
|
1013962 | assert(context.size == sizeof(MD5_CTX)); |
226 | 1013962 | MD5_Final(any_digest->digest, | |
227 | 1013962 | reinterpret_cast<MD5_CTX *>(context.buffer)); | |
228 | 1013962 | break; | |
229 | 1522273 | case kSha1: | |
230 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1522273 times.
|
1522273 | assert(context.size == sizeof(SHA_CTX)); |
231 | 1522273 | SHA1_Final(any_digest->digest, | |
232 | 1522273 | reinterpret_cast<SHA_CTX *>(context.buffer)); | |
233 | 1531285 | break; | |
234 | 38 | case kRmd160: | |
235 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
|
38 | assert(context.size == sizeof(RIPEMD160_CTX)); |
236 | 38 | RIPEMD160_Final(any_digest->digest, | |
237 | 38 | reinterpret_cast<RIPEMD160_CTX *>(context.buffer)); | |
238 | 38 | break; | |
239 | 976 | case kShake128: | |
240 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 976 times.
|
976 | assert(context.size == sizeof(Keccak_HashInstance)); |
241 | 1952 | keccak_result = Keccak_HashFinal(reinterpret_cast<Keccak_HashInstance *>( | |
242 | 976 | context.buffer), NULL); | |
243 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 976 times.
|
976 | assert(keccak_result == SUCCESS); |
244 | keccak_result = | ||
245 | 1952 | Keccak_HashSqueeze(reinterpret_cast<Keccak_HashInstance *>( | |
246 | 976 | context.buffer), any_digest->digest, kDigestSizes[kShake128] * 8); | |
247 | 976 | break; | |
248 | ✗ | default: | |
249 | ✗ | PANIC(NULL); // Undefined hash | |
250 | } | ||
251 | 2546261 | any_digest->algorithm = context.algorithm; | |
252 | 2546261 | } | |
253 | |||
254 | |||
255 | 19252 | void HashMem(const unsigned char *buffer, const unsigned buffer_size, | |
256 | Any *any_digest) | ||
257 | { | ||
258 | 19252 | Algorithms algorithm = any_digest->algorithm; | |
259 |
1/2✓ Branch 1 taken 19252 times.
✗ Branch 2 not taken.
|
19252 | ContextPtr context(algorithm); |
260 | 19252 | context.buffer = alloca(context.size); | |
261 | |||
262 |
1/2✓ Branch 1 taken 19252 times.
✗ Branch 2 not taken.
|
19252 | Init(context); |
263 |
1/2✓ Branch 1 taken 19252 times.
✗ Branch 2 not taken.
|
19252 | Update(buffer, buffer_size, context); |
264 |
1/2✓ Branch 1 taken 19252 times.
✗ Branch 2 not taken.
|
19252 | Final(context, any_digest); |
265 | 19252 | } | |
266 | |||
267 | |||
268 | 1212 | void HashString(const std::string &content, Any *any_digest) { | |
269 | 1212 | HashMem(reinterpret_cast<const unsigned char *>(content.data()), | |
270 | 1212 | content.length(), any_digest); | |
271 | 1212 | } | |
272 | |||
273 | |||
274 | 507409 | void Hmac( | |
275 | const string &key, | ||
276 | const unsigned char *buffer, | ||
277 | const unsigned buffer_size, | ||
278 | Any *any_digest | ||
279 | ) { | ||
280 | 507409 | Algorithms algorithm = any_digest->algorithm; | |
281 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 507409 times.
|
507409 | assert(algorithm != kAny); |
282 | |||
283 | 507409 | const unsigned block_size = kBlockSizes[algorithm]; | |
284 | 507409 | unsigned char key_block[block_size]; | |
285 | 507409 | memset(key_block, 0, block_size); | |
286 |
2/2✓ Branch 1 taken 7332 times.
✓ Branch 2 taken 500077 times.
|
507409 | if (key.length() > block_size) { |
287 |
1/2✓ Branch 1 taken 7332 times.
✗ Branch 2 not taken.
|
7332 | Any hash_key(algorithm); |
288 |
1/2✓ Branch 2 taken 7332 times.
✗ Branch 3 not taken.
|
7332 | HashMem(reinterpret_cast<const unsigned char *>(key.data()), |
289 | 7332 | key.length(), &hash_key); | |
290 | 7332 | memcpy(key_block, hash_key.digest, kDigestSizes[algorithm]); | |
291 | } else { | ||
292 |
2/2✓ Branch 1 taken 500068 times.
✓ Branch 2 taken 9 times.
|
500077 | if (key.length() > 0) |
293 | 500068 | memcpy(key_block, key.data(), key.length()); | |
294 | } | ||
295 | |||
296 | 507409 | unsigned char pad_block[block_size]; | |
297 | // Inner hash | ||
298 |
1/2✓ Branch 1 taken 507409 times.
✗ Branch 2 not taken.
|
507409 | Any hash_inner(algorithm); |
299 |
1/2✓ Branch 1 taken 507409 times.
✗ Branch 2 not taken.
|
507409 | ContextPtr context_inner(algorithm); |
300 | 507409 | context_inner.buffer = alloca(context_inner.size); | |
301 |
1/2✓ Branch 1 taken 507409 times.
✗ Branch 2 not taken.
|
507409 | Init(context_inner); |
302 |
2/2✓ Branch 0 taken 32474176 times.
✓ Branch 1 taken 507409 times.
|
32981585 | for (unsigned i = 0; i < block_size; ++i) |
303 | 32474176 | pad_block[i] = key_block[i] ^ 0x36; | |
304 |
1/2✓ Branch 1 taken 507409 times.
✗ Branch 2 not taken.
|
507409 | Update(pad_block, block_size, context_inner); |
305 |
1/2✓ Branch 1 taken 507409 times.
✗ Branch 2 not taken.
|
507409 | Update(buffer, buffer_size, context_inner); |
306 |
1/2✓ Branch 1 taken 507409 times.
✗ Branch 2 not taken.
|
507409 | Final(context_inner, &hash_inner); |
307 | |||
308 | // Outer hash | ||
309 |
1/2✓ Branch 1 taken 507409 times.
✗ Branch 2 not taken.
|
507409 | ContextPtr context_outer(algorithm); |
310 | 507409 | context_outer.buffer = alloca(context_outer.size); | |
311 |
1/2✓ Branch 1 taken 507409 times.
✗ Branch 2 not taken.
|
507409 | Init(context_outer); |
312 |
2/2✓ Branch 0 taken 32474176 times.
✓ Branch 1 taken 507409 times.
|
32981585 | for (unsigned i = 0; i < block_size; ++i) |
313 | 32474176 | pad_block[i] = key_block[i] ^ 0x5c; | |
314 |
1/2✓ Branch 1 taken 507409 times.
✗ Branch 2 not taken.
|
507409 | Update(pad_block, block_size, context_outer); |
315 |
1/2✓ Branch 1 taken 507409 times.
✗ Branch 2 not taken.
|
507409 | Update(hash_inner.digest, kDigestSizes[algorithm], context_outer); |
316 | |||
317 |
1/2✓ Branch 1 taken 507409 times.
✗ Branch 2 not taken.
|
507409 | Final(context_outer, any_digest); |
318 | 507409 | } | |
319 | |||
320 | |||
321 | 7995 | bool HashFd(int fd, Any *any_digest) { | |
322 | 7995 | Algorithms algorithm = any_digest->algorithm; | |
323 |
1/2✓ Branch 1 taken 7995 times.
✗ Branch 2 not taken.
|
7995 | ContextPtr context(algorithm); |
324 | 7995 | context.buffer = alloca(context.size); | |
325 | |||
326 |
1/2✓ Branch 1 taken 7995 times.
✗ Branch 2 not taken.
|
7995 | Init(context); |
327 | unsigned char io_buffer[4096]; | ||
328 | int actual_bytes; | ||
329 |
3/4✓ Branch 1 taken 3470870 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3462875 times.
✓ Branch 4 taken 7995 times.
|
3470870 | while ((actual_bytes = read(fd, io_buffer, 4096)) != 0) { |
330 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3462875 times.
|
3462875 | if (actual_bytes == -1) { |
331 | ✗ | if (errno == EINTR) | |
332 | ✗ | continue; | |
333 | ✗ | return false; | |
334 | } | ||
335 |
1/2✓ Branch 1 taken 3462875 times.
✗ Branch 2 not taken.
|
3462875 | Update(io_buffer, actual_bytes, context); |
336 | } | ||
337 |
1/2✓ Branch 1 taken 7995 times.
✗ Branch 2 not taken.
|
7995 | Final(context, any_digest); |
338 | 7995 | return true; | |
339 | } | ||
340 | |||
341 | |||
342 | 7995 | bool HashFile(const std::string &filename, Any *any_digest) { | |
343 | 7995 | int fd = open(filename.c_str(), O_RDONLY); | |
344 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7995 times.
|
7995 | if (fd == -1) |
345 | ✗ | return false; | |
346 | |||
347 | 7995 | bool result = HashFd(fd, any_digest); | |
348 | 7995 | close(fd); | |
349 | 7995 | return result; | |
350 | } | ||
351 | |||
352 | |||
353 | /** | ||
354 | * Fast constructor for hashing path names. | ||
355 | */ | ||
356 | 9628 | Md5::Md5(const AsciiPtr ascii) { | |
357 | 9628 | algorithm = kMd5; | |
358 | 9628 | const string *str = ascii.str; | |
359 | |||
360 | MD5_CTX md5_state; | ||
361 |
1/2✓ Branch 1 taken 9628 times.
✗ Branch 2 not taken.
|
9628 | MD5_Init(&md5_state); |
362 |
1/2✓ Branch 3 taken 9628 times.
✗ Branch 4 not taken.
|
9628 | MD5_Update(&md5_state, reinterpret_cast<const unsigned char *>(&(*str)[0]), |
363 | str->length()); | ||
364 |
1/2✓ Branch 1 taken 9628 times.
✗ Branch 2 not taken.
|
9628 | MD5_Final(digest, &md5_state); |
365 | 9628 | } | |
366 | |||
367 | |||
368 | 7412464 | Md5::Md5(const char *chars, const unsigned length) { | |
369 | 7412464 | algorithm = kMd5; | |
370 | |||
371 | MD5_CTX md5_state; | ||
372 |
1/2✓ Branch 1 taken 7412464 times.
✗ Branch 2 not taken.
|
7412464 | MD5_Init(&md5_state); |
373 |
1/2✓ Branch 1 taken 7412464 times.
✗ Branch 2 not taken.
|
7412464 | MD5_Update(&md5_state, reinterpret_cast<const unsigned char *>(chars), |
374 | length); | ||
375 |
1/2✓ Branch 1 taken 7412464 times.
✗ Branch 2 not taken.
|
7412464 | MD5_Final(digest, &md5_state); |
376 | 7412464 | } | |
377 | |||
378 | |||
379 | ✗ | Md5::Md5(const uint64_t lo, const uint64_t hi) { | |
380 | ✗ | algorithm = kMd5; | |
381 | ✗ | memcpy(digest, &lo, 8); | |
382 | ✗ | memcpy(digest+8, &hi, 8); | |
383 | } | ||
384 | |||
385 | 12458 | void Md5::ToIntPair(uint64_t *lo, uint64_t *hi) const { | |
386 | 12458 | memcpy(lo, digest, 8); | |
387 | 12458 | memcpy(hi, digest+8, 8); | |
388 | 12458 | } | |
389 | |||
390 | |||
391 | 500104 | Md5 Any::CastToMd5() { | |
392 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 500104 times.
|
500104 | assert(algorithm == kMd5); |
393 | 500104 | Md5 result; | |
394 | 500104 | memcpy(result.digest, digest, kDigestSizes[kMd5]); | |
395 | 500104 | return result; | |
396 | } | ||
397 | |||
398 | #ifndef OPENSSL_API_INTERFACE_V09 | ||
399 | 18 | static string HexFromSha256(unsigned char digest[SHA256_DIGEST_LENGTH]) { | |
400 | 18 | string result; | |
401 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
18 | result.reserve(2 * SHA256_DIGEST_LENGTH); |
402 |
2/2✓ Branch 0 taken 576 times.
✓ Branch 1 taken 18 times.
|
594 | for (unsigned i = 0; i < SHA256_DIGEST_LENGTH; ++i) { |
403 | 576 | const char d1 = digest[i] / 16; | |
404 | 576 | const char d2 = digest[i] % 16; | |
405 |
3/4✓ Branch 0 taken 333 times.
✓ Branch 1 taken 243 times.
✓ Branch 3 taken 576 times.
✗ Branch 4 not taken.
|
576 | result.push_back(d1 + ((d1 <= 9) ? '0' : 'a' - 10)); |
406 |
3/4✓ Branch 0 taken 399 times.
✓ Branch 1 taken 177 times.
✓ Branch 3 taken 576 times.
✗ Branch 4 not taken.
|
576 | result.push_back(d2 + ((d2 <= 9) ? '0' : 'a' - 10)); |
407 | } | ||
408 | 18 | return result; | |
409 | } | ||
410 | #endif | ||
411 | |||
412 | 3 | string Sha256File(const string &filename) { | |
413 | #ifdef OPENSSL_API_INTERFACE_V09 | ||
414 | PANIC(NULL); | ||
415 | #else | ||
416 |
1/2✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | int fd = open(filename.c_str(), O_RDONLY); |
417 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (fd < 0) |
418 | ✗ | return ""; | |
419 | |||
420 | SHA256_CTX ctx; | ||
421 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | SHA256_Init(&ctx); |
422 | |||
423 | unsigned char io_buffer[4096]; | ||
424 | int actual_bytes; | ||
425 |
2/4✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
|
3 | while ((actual_bytes = read(fd, io_buffer, 4096)) != 0) { |
426 | ✗ | if (actual_bytes == -1) { | |
427 | ✗ | if (errno == EINTR) | |
428 | ✗ | continue; | |
429 | ✗ | close(fd); | |
430 | ✗ | return ""; | |
431 | } | ||
432 | ✗ | SHA256_Update(&ctx, io_buffer, actual_bytes); | |
433 | } | ||
434 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | close(fd); |
435 | |||
436 | unsigned char digest[SHA256_DIGEST_LENGTH]; | ||
437 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | SHA256_Final(digest, &ctx); |
438 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | return HexFromSha256(digest); |
439 | #endif | ||
440 | } | ||
441 | |||
442 | 6 | string Sha256Mem(const unsigned char *buffer, const unsigned buffer_size) { | |
443 | #ifdef OPENSSL_API_INTERFACE_V09 | ||
444 | PANIC(NULL); | ||
445 | #else | ||
446 | unsigned char digest[SHA256_DIGEST_LENGTH]; | ||
447 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | SHA256(buffer, buffer_size, digest); |
448 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
12 | return HexFromSha256(digest); |
449 | #endif | ||
450 | } | ||
451 | |||
452 | 3 | string Sha256String(const string &content) { | |
453 | 3 | return Sha256Mem(reinterpret_cast<const unsigned char *>(content.data()), | |
454 | 3 | content.length()); | |
455 | } | ||
456 | |||
457 | |||
458 | 21 | std::string Hmac256( | |
459 | const std::string &key, | ||
460 | const std::string &content, | ||
461 | bool raw_output) | ||
462 | { | ||
463 | #ifdef OPENSSL_API_INTERFACE_V09 | ||
464 | PANIC(NULL); | ||
465 | #else | ||
466 | unsigned char digest[SHA256_DIGEST_LENGTH]; | ||
467 | 21 | const unsigned block_size = 64; | |
468 | 21 | const unsigned key_length = key.length(); | |
469 | unsigned char key_block[block_size]; | ||
470 | 21 | memset(key_block, 0, block_size); | |
471 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 18 times.
|
21 | if (key_length > block_size) { |
472 |
1/2✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
3 | SHA256(reinterpret_cast<const unsigned char *>(key.data()), key_length, |
473 | key_block); | ||
474 | } else { | ||
475 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
18 | if (key.length() > 0) |
476 | 18 | memcpy(key_block, key.data(), key_length); | |
477 | } | ||
478 | |||
479 | unsigned char pad_block[block_size]; | ||
480 | // Inner hash | ||
481 | SHA256_CTX ctx_inner; | ||
482 | unsigned char digest_inner[SHA256_DIGEST_LENGTH]; | ||
483 |
1/2✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
|
21 | SHA256_Init(&ctx_inner); |
484 |
2/2✓ Branch 0 taken 1344 times.
✓ Branch 1 taken 21 times.
|
1365 | for (unsigned i = 0; i < block_size; ++i) |
485 | 1344 | pad_block[i] = key_block[i] ^ 0x36; | |
486 |
1/2✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
|
21 | SHA256_Update(&ctx_inner, pad_block, block_size); |
487 |
1/2✓ Branch 3 taken 21 times.
✗ Branch 4 not taken.
|
21 | SHA256_Update(&ctx_inner, content.data(), content.length()); |
488 |
1/2✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
|
21 | SHA256_Final(digest_inner, &ctx_inner); |
489 | |||
490 | // Outer hash | ||
491 | SHA256_CTX ctx_outer; | ||
492 |
1/2✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
|
21 | SHA256_Init(&ctx_outer); |
493 |
2/2✓ Branch 0 taken 1344 times.
✓ Branch 1 taken 21 times.
|
1365 | for (unsigned i = 0; i < block_size; ++i) |
494 | 1344 | pad_block[i] = key_block[i] ^ 0x5c; | |
495 |
1/2✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
|
21 | SHA256_Update(&ctx_outer, pad_block, block_size); |
496 |
1/2✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
|
21 | SHA256_Update(&ctx_outer, digest_inner, SHA256_DIGEST_LENGTH); |
497 | |||
498 |
1/2✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
|
21 | SHA256_Final(digest, &ctx_outer); |
499 |
2/2✓ Branch 0 taken 12 times.
✓ Branch 1 taken 9 times.
|
21 | if (raw_output) |
500 |
1/2✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
|
12 | return string(reinterpret_cast<const char *>(digest), SHA256_DIGEST_LENGTH); |
501 |
1/2✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
|
9 | return HexFromSha256(digest); |
502 | #endif | ||
503 | } | ||
504 | |||
505 | } // namespace shash | ||
506 | |||
507 | #ifdef CVMFS_NAMESPACE_GUARD | ||
508 | } // namespace CVMFS_NAMESPACE_GUARD | ||
509 | #endif | ||
510 |