GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/crypto/hash.cc
Date: 2026-11-08 02:41:18
Exec Total Coverage
Lines: 278 307 90.6%
Branches: 159 246 64.6%

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 <nettle/md5.h>
12 #include <nettle/ripemd160.h>
13 #include <nettle/sha1.h>
14 #include <nettle/sha2.h>
15 #include <nettle/sha3.h>
16 #include <nettle/version.h>
17 #include <unistd.h>
18
19 #include <cstdio>
20 #include <cstring>
21
22 #include "util/exception.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 namespace {
34
35 // nettle 4 dropped the digest_size parameter from the *_digest() functions.
36 #if defined(NETTLE_VERSION_MAJOR) && (NETTLE_VERSION_MAJOR >= 4)
37 void Md5Digest(md5_ctx *ctx, uint8_t *digest) {
38 nettle_md5_digest(ctx, digest);
39 }
40
41 void Sha1Digest(sha1_ctx *ctx, uint8_t *digest) {
42 nettle_sha1_digest(ctx, digest);
43 }
44
45 void Sha256Digest(sha256_ctx *ctx, uint8_t *digest) {
46 nettle_sha256_digest(ctx, digest);
47 }
48
49 void Ripemd160Digest(ripemd160_ctx *ctx, uint8_t *digest) {
50 nettle_ripemd160_digest(ctx, digest);
51 }
52
53 #else // nettle 3.x takes digest size as 2nd arg
54 1727776 void Md5Digest(md5_ctx *ctx, uint8_t *digest) {
55 1727776 nettle_md5_digest(ctx, MD5_DIGEST_SIZE, digest);
56 1727776 }
57
58 258257 void Sha1Digest(sha1_ctx *ctx, uint8_t *digest) {
59 258257 nettle_sha1_digest(ctx, SHA1_DIGEST_SIZE, digest);
60 258278 }
61
62 18 void Sha256Digest(sha256_ctx *ctx, uint8_t *digest) {
63 18 nettle_sha256_digest(ctx, SHA256_DIGEST_SIZE, digest);
64 18 }
65
66 14 void Ripemd160Digest(ripemd160_ctx *ctx, uint8_t *digest) {
67 14 nettle_ripemd160_digest(ctx, RIPEMD160_DIGEST_SIZE, digest);
68 14 }
69
70 #endif
71
72 785 void Shake128Digest(sha3_128_ctx *ctx, uint8_t *digest) {
73 785 nettle_sha3_128_shake(ctx, kDigestSizes[kShake128], digest);
74 785 }
75
76 static_assert(sizeof(sha3_128_ctx) <= kMaxContextSize,
77 "SHAKE128 context exceeds allocated buffers");
78
79 } // namespace
80
81 const char *kAlgorithmIds[] = {"", "", "-rmd160", "-shake128", ""};
82
83
84 22 bool HexPtr::IsValid() const {
85 22 const unsigned l = str->length();
86
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 21 times.
22 if (l == 0)
87 1 return false;
88 21 const char *c = str->data(); // Walks through the string
89 21 unsigned i = 0; // String position of *c
90
91
2/2
✓ Branch 0 taken 820 times.
✓ Branch 1 taken 5 times.
825 for (; i < l; ++i, ++c) {
92
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 806 times.
820 if (*c == '-')
93 14 break;
94
6/8
✓ Branch 0 taken 806 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 806 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 327 times.
✓ Branch 5 taken 479 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 325 times.
806 if ((*c < '0') || (*c > 'f') || ((*c > '9') && (*c < 'a')))
95 2 return false;
96 }
97
98 // Walk through all algorithms
99
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 15 times.
85 for (unsigned j = 0; j < kAny; ++j) {
100 70 const unsigned hex_length = 2 * kDigestSizes[j];
101 70 const unsigned algo_id_length = kAlgorithmIdSizes[j];
102
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 48 times.
70 if (i == hex_length) {
103 // Right suffix?
104
4/4
✓ Branch 0 taken 61 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 53 times.
✓ Branch 3 taken 8 times.
65 for (; (i < l) && (i - hex_length < algo_id_length); ++i, ++c) {
105
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 43 times.
53 if (*c != kAlgorithmIds[j][i - hex_length])
106 10 break;
107 }
108
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
22 if ((i == l) && (l == hex_length + algo_id_length))
109 4 return true;
110 18 i = hex_length;
111 18 c = str->data() + i;
112 }
113 }
114
115 15 return false;
116 }
117
118
119 ✗ Algorithms ParseHashAlgorithm(const string &algorithm_option) {
120 ✗ if (algorithm_option == "sha1")
121 ✗ return kSha1;
122 ✗ if (algorithm_option == "rmd160")
123 ✗ return kRmd160;
124 ✗ if (algorithm_option == "shake128")
125 ✗ return kShake128;
126 ✗ return kAny;
127 }
128
129
130 57417 Any MkFromHexPtr(const HexPtr hex, const char suffix) {
131 57417 Any result;
132
133 57417 const unsigned length = hex.str->length();
134
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 57416 times.
57417 if (length == 2 * kDigestSizes[kMd5])
135 1 result = Any(kMd5, hex);
136
2/2
✓ Branch 0 taken 57023 times.
✓ Branch 1 taken 394 times.
57417 if (length == 2 * kDigestSizes[kSha1])
137 57023 result = Any(kSha1, hex);
138 // TODO(jblomer) compare -rmd160, -shake128
139
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 57413 times.
57417 if ((length == 2 * kDigestSizes[kRmd160] + kAlgorithmIdSizes[kRmd160]))
140 4 result = Any(kRmd160, hex);
141
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 57392 times.
57417 if ((length == 2 * kDigestSizes[kShake128] + kAlgorithmIdSizes[kShake128]))
142 25 result = Any(kShake128, hex);
143
144 57417 result.suffix = suffix;
145 57417 return result;
146 }
147
148
149 /**
150 * Similar to MkFromHexPtr but the suffix is deducted from the HexPtr string.
151 */
152 33639 Any MkFromSuffixedHexPtr(const HexPtr hex) {
153 33639 Any result;
154
155 33639 const unsigned length = hex.str->length();
156
2/2
✓ Branch 0 taken 1568 times.
✓ Branch 1 taken 32071 times.
33639 if ((length == 2 * kDigestSizes[kMd5])
157
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1567 times.
1568 || (length == 2 * kDigestSizes[kMd5] + 1)) {
158 32072 const Suffix suffix = (length == 2 * kDigestSizes[kMd5] + 1)
159
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 32071 times.
32072 ? *(hex.str->rbegin())
160 32072 : kSuffixNone;
161 32072 result = Any(kMd5, hex, suffix);
162 }
163
2/2
✓ Branch 0 taken 32830 times.
✓ Branch 1 taken 809 times.
33639 if ((length == 2 * kDigestSizes[kSha1])
164
2/2
✓ Branch 0 taken 743 times.
✓ Branch 1 taken 32087 times.
32830 || (length == 2 * kDigestSizes[kSha1] + 1)) {
165 1552 const Suffix suffix = (length == 2 * kDigestSizes[kSha1] + 1)
166
2/2
✓ Branch 0 taken 743 times.
✓ Branch 1 taken 809 times.
1552 ? *(hex.str->rbegin())
167 1552 : kSuffixNone;
168 1552 result = Any(kSha1, hex, suffix);
169 }
170
2/2
✓ Branch 0 taken 33638 times.
✓ Branch 1 taken 1 times.
33639 if ((length == 2 * kDigestSizes[kRmd160] + kAlgorithmIdSizes[kRmd160])
171 33638 || (length
172
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 33627 times.
33638 == 2 * kDigestSizes[kRmd160] + kAlgorithmIdSizes[kRmd160] + 1)) {
173 const Suffix suffix = (length
174 12 == 2 * kDigestSizes[kRmd160]
175 12 + kAlgorithmIdSizes[kRmd160] + 1)
176
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1 times.
12 ? *(hex.str->rbegin())
177 12 : kSuffixNone;
178 12 result = Any(kRmd160, hex, suffix);
179 }
180
2/2
✓ Branch 0 taken 33638 times.
✓ Branch 1 taken 1 times.
33639 if ((length == 2 * kDigestSizes[kShake128] + kAlgorithmIdSizes[kShake128])
181 33638 || (length
182
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 33637 times.
33638 == 2 * kDigestSizes[kShake128] + kAlgorithmIdSizes[kShake128] + 1)) {
183 const Suffix suffix = (length
184 2 == 2 * kDigestSizes[kShake128]
185 2 + kAlgorithmIdSizes[kShake128] + 1)
186
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 ? *(hex.str->rbegin())
187 2 : kSuffixNone;
188 2 result = Any(kShake128, hex, suffix);
189 }
190
191 33639 return result;
192 }
193
194
195 /**
196 * Allows the caller to create the context on the stack.
197 */
198 473284 unsigned GetContextSize(const Algorithms algorithm) {
199
4/5
✓ Branch 0 taken 209380 times.
✓ Branch 1 taken 263116 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 785 times.
✗ Branch 4 not taken.
473284 switch (algorithm) {
200 209380 case kMd5:
201 209380 return sizeof(md5_ctx);
202 263116 case kSha1:
203 263116 return sizeof(sha1_ctx);
204 14 case kRmd160:
205 14 return sizeof(ripemd160_ctx);
206 785 case kShake128:
207 785 return sizeof(sha3_128_ctx);
208 ✗ default:
209 ✗ PANIC(kLogDebug | kLogSyslogErr,
210 "tried to generate hash context for unspecified hash. Aborting...");
211 }
212 }
213
214 473383 void Init(ContextPtr context) {
215
4/5
✓ Branch 0 taken 209383 times.
✓ Branch 1 taken 263208 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 785 times.
✗ Branch 4 not taken.
473383 switch (context.algorithm) {
216 209383 case kMd5:
217 209383 md5_init(reinterpret_cast<md5_ctx *>(context.buffer));
218 209383 break;
219 263208 case kSha1:
220 263208 sha1_init(reinterpret_cast<sha1_ctx *>(context.buffer));
221 263146 break;
222 14 case kRmd160:
223 14 ripemd160_init(reinterpret_cast<ripemd160_ctx *>(context.buffer));
224 14 break;
225 785 case kShake128:
226 785 sha3_128_init(reinterpret_cast<sha3_128_ctx *>(context.buffer));
227 785 break;
228 ✗ default:
229 ✗ PANIC(NULL); // Undefined hash
230 }
231 473328 }
232
233 95847363 void Update(const unsigned char *buffer, const unsigned buffer_length,
234 ContextPtr context) {
235
4/5
✓ Branch 0 taken 26185484 times.
✓ Branch 1 taken 23523738 times.
✓ Branch 2 taken 23068688 times.
✓ Branch 3 taken 23069454 times.
✗ Branch 4 not taken.
95847363 switch (context.algorithm) {
236 26185484 case kMd5:
237 26185484 md5_update(reinterpret_cast<md5_ctx *>(context.buffer),
238 buffer_length, buffer);
239 26185484 break;
240 23523738 case kSha1:
241 23523738 sha1_update(reinterpret_cast<sha1_ctx *>(context.buffer),
242 buffer_length, buffer);
243 23523834 break;
244 23068688 case kRmd160:
245 23068688 ripemd160_update(reinterpret_cast<ripemd160_ctx *>(context.buffer),
246 buffer_length, buffer);
247 23068688 break;
248 23069454 case kShake128:
249 23069454 sha3_128_update(reinterpret_cast<sha3_128_ctx *>(context.buffer),
250 buffer_length, buffer);
251 23069454 break;
252 ✗ default:
253 ✗ PANIC(NULL); // Undefined hash
254 }
255 95847460 }
256
257 468431 void Final(ContextPtr context, Any *any_digest) {
258
5/5
✓ Branch 0 taken 209379 times.
✓ Branch 1 taken 258252 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 785 times.
✓ Branch 4 taken 1 times.
468431 switch (context.algorithm) {
259 209379 case kMd5:
260 209379 Md5Digest(reinterpret_cast<md5_ctx *>(context.buffer), any_digest->digest);
261 209379 break;
262 258252 case kSha1:
263 258252 Sha1Digest(reinterpret_cast<sha1_ctx *>(context.buffer),
264 258252 any_digest->digest);
265 258268 break;
266 14 case kRmd160:
267 14 Ripemd160Digest(reinterpret_cast<ripemd160_ctx *>(context.buffer),
268 14 any_digest->digest);
269 14 break;
270 785 case kShake128:
271 785 Shake128Digest(reinterpret_cast<sha3_128_ctx *>(context.buffer),
272 785 any_digest->digest);
273 785 break;
274 1 default:
275 1 PANIC(NULL); // Undefined hash
276 }
277 468446 any_digest->algorithm = context.algorithm;
278 468446 }
279
280
281 7139 void HashMem(const unsigned char *buffer, const unsigned buffer_size,
282 Any *any_digest) {
283 7139 const Algorithms algorithm = any_digest->algorithm;
284
1/2
✓ Branch 1 taken 7139 times.
✗ Branch 2 not taken.
7139 ContextPtr context(algorithm);
285 7139 context.buffer = alloca(context.size);
286
287
1/2
✓ Branch 1 taken 7139 times.
✗ Branch 2 not taken.
7139 Init(context);
288
1/2
✓ Branch 1 taken 7139 times.
✗ Branch 2 not taken.
7139 Update(buffer, buffer_size, context);
289
1/2
✓ Branch 1 taken 7139 times.
✗ Branch 2 not taken.
7139 Final(context, any_digest);
290 7139 }
291
292
293 845 void HashString(const std::string &content, Any *any_digest) {
294 845 HashMem(reinterpret_cast<const unsigned char *>(content.data()),
295 845 content.length(), any_digest);
296 845 }
297
298
299 101268 void Hmac(const string &key,
300 const unsigned char *buffer,
301 const unsigned buffer_size,
302 Any *any_digest) {
303 101268 const Algorithms algorithm = any_digest->algorithm;
304
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 101268 times.
101268 assert(algorithm != kAny);
305
306 101268 const unsigned block_size = kBlockSizes[algorithm];
307 101268 unsigned char key_block[block_size];
308 101268 memset(key_block, 0, block_size);
309
2/2
✓ Branch 1 taken 1228 times.
✓ Branch 2 taken 100040 times.
101268 if (key.length() > block_size) {
310
1/2
✓ Branch 1 taken 1228 times.
✗ Branch 2 not taken.
1228 Any hash_key(algorithm);
311
1/2
✓ Branch 3 taken 1228 times.
✗ Branch 4 not taken.
1228 HashMem(reinterpret_cast<const unsigned char *>(key.data()), key.length(),
312 &hash_key);
313 1228 memcpy(key_block, hash_key.digest, kDigestSizes[algorithm]);
314 } else {
315
2/2
✓ Branch 1 taken 100037 times.
✓ Branch 2 taken 3 times.
100040 if (key.length() > 0)
316 100037 memcpy(key_block, key.data(), key.length());
317 }
318
319 101268 unsigned char pad_block[block_size];
320 // Inner hash
321
1/2
✓ Branch 1 taken 101268 times.
✗ Branch 2 not taken.
101268 Any hash_inner(algorithm);
322
1/2
✓ Branch 1 taken 101268 times.
✗ Branch 2 not taken.
101268 ContextPtr context_inner(algorithm);
323 101268 context_inner.buffer = alloca(context_inner.size);
324
1/2
✓ Branch 1 taken 101268 times.
✗ Branch 2 not taken.
101268 Init(context_inner);
325
2/2
✓ Branch 0 taken 6481152 times.
✓ Branch 1 taken 101268 times.
6582420 for (unsigned i = 0; i < block_size; ++i)
326 6481152 pad_block[i] = key_block[i] ^ 0x36;
327
1/2
✓ Branch 1 taken 101268 times.
✗ Branch 2 not taken.
101268 Update(pad_block, block_size, context_inner);
328
1/2
✓ Branch 1 taken 101268 times.
✗ Branch 2 not taken.
101268 Update(buffer, buffer_size, context_inner);
329
1/2
✓ Branch 1 taken 101268 times.
✗ Branch 2 not taken.
101268 Final(context_inner, &hash_inner);
330
331 // Outer hash
332
1/2
✓ Branch 1 taken 101268 times.
✗ Branch 2 not taken.
101268 ContextPtr context_outer(algorithm);
333 101268 context_outer.buffer = alloca(context_outer.size);
334
1/2
✓ Branch 1 taken 101268 times.
✗ Branch 2 not taken.
101268 Init(context_outer);
335
2/2
✓ Branch 0 taken 6481152 times.
✓ Branch 1 taken 101268 times.
6582420 for (unsigned i = 0; i < block_size; ++i)
336 6481152 pad_block[i] = key_block[i] ^ 0x5c;
337
1/2
✓ Branch 1 taken 101268 times.
✗ Branch 2 not taken.
101268 Update(pad_block, block_size, context_outer);
338
1/2
✓ Branch 1 taken 101268 times.
✗ Branch 2 not taken.
101268 Update(hash_inner.digest, kDigestSizes[algorithm], context_outer);
339
340
1/2
✓ Branch 1 taken 101268 times.
✗ Branch 2 not taken.
101268 Final(context_outer, any_digest);
341 101268 }
342
343
344 6861 bool HashFd(int fd, Any *any_digest) {
345 6861 const Algorithms algorithm = any_digest->algorithm;
346
1/2
✓ Branch 1 taken 6861 times.
✗ Branch 2 not taken.
6861 ContextPtr context(algorithm);
347 6861 context.buffer = alloca(context.size);
348
349
1/2
✓ Branch 1 taken 6861 times.
✗ Branch 2 not taken.
6861 Init(context);
350 unsigned char io_buffer[4096];
351 int actual_bytes;
352
3/4
✓ Branch 1 taken 2775519 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2768658 times.
✓ Branch 4 taken 6861 times.
2775519 while ((actual_bytes = read(fd, io_buffer, 4096)) != 0) {
353
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2768658 times.
2768658 if (actual_bytes == -1) {
354 ✗ if (errno == EINTR)
355 ✗ continue;
356 ✗ return false;
357 }
358
1/2
✓ Branch 1 taken 2768658 times.
✗ Branch 2 not taken.
2768658 Update(io_buffer, actual_bytes, context);
359 }
360
1/2
✓ Branch 1 taken 6861 times.
✗ Branch 2 not taken.
6861 Final(context, any_digest);
361 6861 return true;
362 }
363
364
365 6861 bool HashFile(const std::string &filename, Any *any_digest) {
366 6861 const int fd = open(filename.c_str(), O_RDONLY);
367
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6861 times.
6861 if (fd == -1)
368 ✗ return false;
369
370 6861 const bool result = HashFd(fd, any_digest);
371 6861 close(fd);
372 6861 return result;
373 }
374
375
376 /**
377 * Fast constructor for hashing path names.
378 */
379 5402 Md5::Md5(const AsciiPtr ascii) {
380 5402 algorithm = kMd5;
381 5402 const string *str = ascii.str;
382
383 md5_ctx md5_state;
384
1/2
✓ Branch 1 taken 5402 times.
✗ Branch 2 not taken.
5402 md5_init(&md5_state);
385
1/2
✓ Branch 2 taken 5402 times.
✗ Branch 3 not taken.
5402 md5_update(&md5_state, str->length(),
386 5402 reinterpret_cast<const uint8_t *>(&(*str)[0]));
387
1/2
✓ Branch 1 taken 5402 times.
✗ Branch 2 not taken.
5402 Md5Digest(&md5_state, digest);
388 5402 }
389
390
391 1512995 Md5::Md5(const char *chars, const unsigned length) {
392 1512995 algorithm = kMd5;
393
394 md5_ctx md5_state;
395
1/2
✓ Branch 1 taken 1512995 times.
✗ Branch 2 not taken.
1512995 md5_init(&md5_state);
396
1/2
✓ Branch 1 taken 1512995 times.
✗ Branch 2 not taken.
1512995 md5_update(&md5_state, length, reinterpret_cast<const uint8_t *>(chars));
397
1/2
✓ Branch 1 taken 1512995 times.
✗ Branch 2 not taken.
1512995 Md5Digest(&md5_state, digest);
398 1512995 }
399
400
401 ✗ Md5::Md5(const uint64_t lo, const uint64_t hi) {
402 ✗ algorithm = kMd5;
403 ✗ memcpy(digest, &lo, 8);
404 ✗ memcpy(digest + 8, &hi, 8);
405 }
406
407 5612 void Md5::ToIntPair(uint64_t *lo, uint64_t *hi) const {
408 5612 memcpy(lo, digest, 8);
409 5612 memcpy(hi, digest + 8, 8);
410 5612 }
411
412
413 100061 Md5 Any::CastToMd5() {
414
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 100061 times.
100061 assert(algorithm == kMd5);
415 100061 Md5 result;
416 100061 memcpy(result.digest, digest, kDigestSizes[kMd5]);
417 100061 return result;
418 }
419
420 6 static string HexFromSha256(unsigned char digest[SHA256_DIGEST_SIZE]) {
421 6 string result;
422
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 result.reserve(2 * SHA256_DIGEST_SIZE);
423
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 6 times.
198 for (unsigned i = 0; i < SHA256_DIGEST_SIZE; ++i) {
424 192 const char d1 = digest[i] / 16;
425 192 const char d2 = digest[i] % 16;
426
3/4
✓ Branch 0 taken 111 times.
✓ Branch 1 taken 81 times.
✓ Branch 3 taken 192 times.
✗ Branch 4 not taken.
192 result.push_back(d1 + ((d1 <= 9) ? '0' : 'a' - 10));
427
3/4
✓ Branch 0 taken 133 times.
✓ Branch 1 taken 59 times.
✓ Branch 3 taken 192 times.
✗ Branch 4 not taken.
192 result.push_back(d2 + ((d2 <= 9) ? '0' : 'a' - 10));
428 }
429 6 return result;
430 }
431
432 1 string Sha256File(const string &filename) {
433
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 const int fd = open(filename.c_str(), O_RDONLY);
434
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (fd < 0)
435 ✗ return "";
436
437 sha256_ctx ctx;
438
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 sha256_init(&ctx);
439
440 unsigned char io_buffer[4096];
441 int actual_bytes;
442
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 while ((actual_bytes = read(fd, io_buffer, 4096)) != 0) {
443 ✗ if (actual_bytes == -1) {
444 ✗ if (errno == EINTR)
445 ✗ continue;
446 ✗ close(fd);
447 ✗ return "";
448 }
449 ✗ sha256_update(&ctx, actual_bytes, io_buffer);
450 }
451
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 close(fd);
452
453 unsigned char digest[SHA256_DIGEST_SIZE];
454
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 Sha256Digest(&ctx, digest);
455
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 return HexFromSha256(digest);
456 }
457
458 2 string Sha256Mem(const unsigned char *buffer, const unsigned buffer_size) {
459 unsigned char digest[SHA256_DIGEST_SIZE];
460 sha256_ctx ctx;
461
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 sha256_init(&ctx);
462
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 sha256_update(&ctx, buffer_size, buffer);
463
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 Sha256Digest(&ctx, digest);
464
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 return HexFromSha256(digest);
465 }
466
467 1 string Sha256String(const string &content) {
468 1 return Sha256Mem(reinterpret_cast<const unsigned char *>(content.data()),
469 1 content.length());
470 }
471
472
473 7 std::string Hmac256(const std::string &key,
474 const std::string &content,
475 bool raw_output) {
476 sha256_ctx ctx;
477 unsigned char digest[SHA256_DIGEST_SIZE];
478 7 const unsigned block_size = 64;
479 7 const unsigned key_length = key.length();
480 unsigned char key_block[block_size];
481 7 memset(key_block, 0, block_size);
482
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6 times.
7 if (key_length > block_size) {
483
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 sha256_init(&ctx);
484
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 sha256_update(&ctx, key_length,
485 1 reinterpret_cast<const unsigned char *>(key.data()));
486
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 Sha256Digest(&ctx, key_block);
487 } else {
488
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 if (key.length() > 0)
489 6 memcpy(key_block, key.data(), key_length);
490 }
491
492 unsigned char pad_block[block_size];
493 // Inner hash
494 unsigned char digest_inner[SHA256_DIGEST_SIZE];
495
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 sha256_init(&ctx);
496
2/2
✓ Branch 0 taken 448 times.
✓ Branch 1 taken 7 times.
455 for (unsigned i = 0; i < block_size; ++i)
497 448 pad_block[i] = key_block[i] ^ 0x36;
498
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 sha256_update(&ctx, block_size, pad_block);
499
1/2
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
7 sha256_update(&ctx, content.length(),
500 7 reinterpret_cast<const unsigned char *>(content.data()));
501
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 Sha256Digest(&ctx, digest_inner);
502
503 // Outer hash
504
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 sha256_init(&ctx);
505
2/2
✓ Branch 0 taken 448 times.
✓ Branch 1 taken 7 times.
455 for (unsigned i = 0; i < block_size; ++i)
506 448 pad_block[i] = key_block[i] ^ 0x5c;
507
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 sha256_update(&ctx, block_size, pad_block);
508
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 sha256_update(&ctx, SHA256_DIGEST_SIZE, digest_inner);
509
510
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
7 Sha256Digest(&ctx, digest);
511
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3 times.
7 if (raw_output)
512
1/2
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 return string(reinterpret_cast<const char *>(digest), SHA256_DIGEST_SIZE);
513
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 return HexFromSha256(digest);
514 }
515
516 } // namespace shash
517
518 #ifdef CVMFS_NAMESPACE_GUARD
519 } // namespace CVMFS_NAMESPACE_GUARD
520 #endif
521