Directory: | cvmfs/ |
---|---|
File: | cvmfs/crypto/hash.h |
Date: | 2025-06-22 02:36:02 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 165 | 168 | 98.2% |
Branches: | 94 | 122 | 77.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /** | ||
2 | * This file is part of the CernVM File System. | ||
3 | * | ||
4 | * Provides a bit syntactic sugar around the hash algorithms. | ||
5 | * In particular, hashes can easily be created by constructors. | ||
6 | * Also, we have a little to-string-from-string conversion. | ||
7 | * | ||
8 | * The complexity is due to the need to avoid dynamically allocated memory | ||
9 | * for the hashes. Almost everything happens on the stack. | ||
10 | */ | ||
11 | |||
12 | #ifndef CVMFS_CRYPTO_HASH_H_ | ||
13 | #define CVMFS_CRYPTO_HASH_H_ | ||
14 | |||
15 | #include <arpa/inet.h> | ||
16 | #include <stdint.h> | ||
17 | |||
18 | #include <cassert> | ||
19 | #include <cctype> | ||
20 | #include <cstdlib> | ||
21 | #include <cstring> | ||
22 | #include <string> | ||
23 | |||
24 | #include "util/export.h" | ||
25 | #include "util/logging.h" | ||
26 | #include "util/prng.h" | ||
27 | #include "util/smalloc.h" | ||
28 | |||
29 | #ifdef CVMFS_NAMESPACE_GUARD | ||
30 | namespace CVMFS_NAMESPACE_GUARD { | ||
31 | #endif | ||
32 | |||
33 | namespace shash { | ||
34 | |||
35 | /** | ||
36 | * Don't change order! The integer value of the enum constants is used | ||
37 | * as file catalog flags and as flags in communication with the cache manager. | ||
38 | * If algorithms are added, the protocol definition for external cache managers | ||
39 | * needs to be updated, too. | ||
40 | */ | ||
41 | enum Algorithms { | ||
42 | kMd5 = 0, | ||
43 | kSha1, | ||
44 | kRmd160, | ||
45 | kShake128, // with 160 output bits | ||
46 | kAny, | ||
47 | }; | ||
48 | |||
49 | /** | ||
50 | * NOTE: when adding a suffix here, one must edit `cvmfs_swissknife scrub` | ||
51 | * accordingly, that checks for invalid hash suffixes | ||
52 | */ | ||
53 | const char kSuffixNone = 0; | ||
54 | const char kSuffixCatalog = 'C'; | ||
55 | const char kSuffixHistory = 'H'; | ||
56 | const char kSuffixMicroCatalog = 'L'; // currently unused | ||
57 | const char kSuffixPartial = 'P'; | ||
58 | const char kSuffixTemporary = 'T'; | ||
59 | const char kSuffixCertificate = 'X'; | ||
60 | const char kSuffixMetainfo = 'M'; | ||
61 | |||
62 | |||
63 | /** | ||
64 | * Corresponds to Algorithms. "Any" is the maximum of all the other | ||
65 | * digest sizes. | ||
66 | * When the maximum digest size changes, the memory layout of DirectoryEntry and | ||
67 | * PosixQuotaManager::LruCommand changes, too! | ||
68 | */ | ||
69 | const unsigned kDigestSizes[] = {16, 20, 20, 20, 20}; | ||
70 | // Md5 Sha1 Rmd160 Shake128 Any | ||
71 | const unsigned kMaxDigestSize = 20; | ||
72 | |||
73 | /** | ||
74 | * The maximum of GetContextSize() | ||
75 | */ | ||
76 | const unsigned kMaxContextSize = 256; | ||
77 | |||
78 | /** | ||
79 | * Hex representations of hashes with the same length need a suffix | ||
80 | * to be distinguished from each other. They should all have one but | ||
81 | * for backwards compatibility MD5 and SHA-1 have none. Initialized in hash.cc | ||
82 | * like const char *kAlgorithmIds[] = {"", "", "-rmd160", ... | ||
83 | */ | ||
84 | CVMFS_EXPORT extern const char *kAlgorithmIds[]; | ||
85 | const unsigned kAlgorithmIdSizes[] = {0, 0, 7, 9, 0}; | ||
86 | // Md5 Sha1 -rmd160 -shake128 Any | ||
87 | const unsigned kMaxAlgorithmIdentifierSize = 9; | ||
88 | |||
89 | /** | ||
90 | * Corresponds to Algorithms. There is no block size for Any. | ||
91 | * Is an HMAC for SHAKE well-defined? | ||
92 | */ | ||
93 | const unsigned kBlockSizes[] = {64, 64, 64, 168}; | ||
94 | // Md5 Sha1 Rmd160 Shake128 | ||
95 | |||
96 | /** | ||
97 | * Distinguishes between interpreting a string as hex hash and hashing over | ||
98 | * the contents of a string. | ||
99 | */ | ||
100 | struct CVMFS_EXPORT HexPtr { | ||
101 | const std::string *str; | ||
102 | 1171883 | explicit HexPtr(const std::string &s) { str = &s; } | |
103 | bool IsValid() const; | ||
104 | }; | ||
105 | |||
106 | struct CVMFS_EXPORT AsciiPtr { | ||
107 | const std::string *str; | ||
108 | 53807 | explicit AsciiPtr(const std::string &s) { str = &s; } | |
109 | }; | ||
110 | |||
111 | typedef char Suffix; | ||
112 | |||
113 | /** | ||
114 | * Holds a hash digest and provides from string / to string conversion and | ||
115 | * comparison. The kAny algorithm may not be used in functions! The algorithm | ||
116 | * has to be changed beforehand. | ||
117 | * This class is not used directly, but used as base clase of Md5, Sha1, ... | ||
118 | */ | ||
119 | template<unsigned digest_size_, Algorithms algorithm_> | ||
120 | struct CVMFS_EXPORT Digest { | ||
121 | unsigned char digest[digest_size_]; | ||
122 | Algorithms algorithm; | ||
123 | Suffix suffix; | ||
124 | |||
125 | class Hex { | ||
126 | public: | ||
127 | 25086763 | explicit Hex(const Digest<digest_size_, algorithm_> *digest) | |
128 | 25086763 | : digest_(*digest) | |
129 | 25086763 | , hash_length_(2 * kDigestSizes[digest_.algorithm]) | |
130 | 25086763 | , algo_id_length_(kAlgorithmIdSizes[digest_.algorithm]) { } | |
131 | |||
132 | 2008201673 | unsigned int length() const { return hash_length_ + algo_id_length_; } | |
133 | |||
134 | 982339611 | char operator[](const unsigned int position) const { | |
135 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 985879175 times.
|
982339611 | assert(position < length()); |
136 |
2/2✓ Branch 0 taken 985863345 times.
✓ Branch 1 taken 15830 times.
|
986018535 | return (position < hash_length_) ? GetHashChar(position) |
137 | 982901466 | : GetAlgorithmIdentifierChar(position); | |
138 | } | ||
139 | |||
140 | protected: | ||
141 | 997153218 | char GetHashChar(const unsigned int position) const { | |
142 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 997013858 times.
|
997153218 | assert(position < hash_length_); |
143 |
2/2✓ Branch 0 taken 499697938 times.
✓ Branch 1 taken 497315920 times.
|
997153218 | const char digit = (position % 2 == 0) |
144 | 499767618 | ? digest_.digest[position / 2] / 16 | |
145 | 497385600 | : digest_.digest[position / 2] % 16; | |
146 | 997153218 | return ToHex(digit); | |
147 | } | ||
148 | |||
149 | 15830 | char GetAlgorithmIdentifierChar(const unsigned int position) const { | |
150 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 15830 times.
|
15830 | assert(position >= hash_length_); |
151 | 15830 | return kAlgorithmIds[digest_.algorithm][position - hash_length_]; | |
152 | } | ||
153 | |||
154 | 983317697 | char ToHex(const char c) const { | |
155 |
2/2✓ Branch 0 taken 706998597 times.
✓ Branch 1 taken 276179740 times.
|
983317697 | return static_cast<char>(c + ((c <= 9) ? '0' : 'a' - 10)); |
156 | } | ||
157 | |||
158 | private: | ||
159 | const Digest<digest_size_, algorithm_> &digest_; | ||
160 | const unsigned int hash_length_; | ||
161 | const unsigned int algo_id_length_; | ||
162 | }; | ||
163 | |||
164 | 80868318 | unsigned GetDigestSize() const { return kDigestSizes[algorithm]; } | |
165 | 2413 | unsigned GetHexSize() const { | |
166 | 2413 | return 2 * kDigestSizes[algorithm] + kAlgorithmIdSizes[algorithm]; | |
167 | } | ||
168 | |||
169 | 1474235088 | Digest() : algorithm(algorithm_), suffix(kSuffixNone) { SetNull(); } | |
170 | |||
171 | 207720 | explicit Digest(const Algorithms a, const HexPtr hex, const char s = 0) | |
172 | 207720 | : algorithm(a), suffix(s) { | |
173 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1324 times.
|
1324 | assert((algorithm_ == kAny) || (a == algorithm_)); |
174 | 207720 | const unsigned char_size = 2 * kDigestSizes[a]; | |
175 | |||
176 | 207720 | const std::string *str = hex.str; | |
177 | 207720 | const unsigned length = str->length(); | |
178 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 207720 times.
|
207720 | assert(length >= char_size); // A suffix won't hurt |
179 | |||
180 |
2/2✓ Branch 0 taken 4088444 times.
✓ Branch 1 taken 207720 times.
|
4296164 | for (unsigned i = 0; i < char_size; i += 2) { |
181 | 7452450 | this->digest[i / 2] = ((*str)[i] <= '9' ? (*str)[i] - '0' | |
182 | 724438 | : (*str)[i] - 'a' + 10) | |
183 | * 16 | ||
184 |
4/4✓ Branch 0 taken 3364006 times.
✓ Branch 1 taken 724438 times.
✓ Branch 3 taken 3272380 times.
✓ Branch 4 taken 816064 times.
|
8992952 | + ((*str)[i + 1] <= '9' ? (*str)[i + 1] - '0' |
185 | 816064 | : (*str)[i + 1] - 'a' + 10); | |
186 | } | ||
187 | 207720 | } | |
188 | |||
189 | 8275 | Digest(const Algorithms a, | |
190 | const unsigned char *digest_buffer, | ||
191 | const Suffix s = kSuffixNone) | ||
192 | 8275 | : algorithm(a), suffix(s) { | |
193 | 8275 | memcpy(digest, digest_buffer, kDigestSizes[a]); | |
194 | 8275 | } | |
195 | |||
196 | /** | ||
197 | * Generates a purely random hash | ||
198 | * Only used for testing purposes | ||
199 | */ | ||
200 | 2882 | void Randomize() { | |
201 | 2882 | Prng prng; | |
202 | 2882 | prng.InitLocaltime(); | |
203 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
2882 | Randomize(&prng); |
204 | 2882 | } | |
205 | |||
206 | /** | ||
207 | * Generates a purely random hash | ||
208 | * Only used for testing purposes | ||
209 | * | ||
210 | * @param seed random number generator seed (for reproducibility) | ||
211 | */ | ||
212 | 16013284 | void Randomize(const uint64_t seed) { | |
213 | 16013284 | Prng prng; | |
214 | 16013284 | prng.InitSeed(seed); | |
215 |
1/2✓ Branch 1 taken 16006642 times.
✗ Branch 2 not taken.
|
16013284 | Randomize(&prng); |
216 | 16013284 | } | |
217 | |||
218 | /** | ||
219 | * Generates a purely random hash | ||
220 | * Only used for testing purposes | ||
221 | * | ||
222 | * @param prng random number generator object (for external reproducibility) | ||
223 | */ | ||
224 | 76330127 | void Randomize(Prng *prng) { | |
225 | 76330127 | const unsigned bytes = GetDigestSize(); | |
226 |
2/2✓ Branch 0 taken 1406488420 times.
✓ Branch 1 taken 76330107 times.
|
1482818947 | for (unsigned i = 0; i < bytes; ++i) { |
227 | 1406488820 | digest[i] = prng->Next(256); | |
228 | } | ||
229 | 76330127 | } | |
230 | |||
231 | 144249 | bool HasSuffix() const { return suffix != kSuffixNone; } | |
232 | 18063458 | void set_suffix(const Suffix s) { suffix = s; } | |
233 | |||
234 | /** | ||
235 | * Generates a hexified representation of the digest including the identifier | ||
236 | * string for newly added hashes. | ||
237 | * | ||
238 | * @param with_suffix append the hash suffix (C,H,X, ...) to the result | ||
239 | * @return a string representation of the digest | ||
240 | */ | ||
241 | 24294757 | std::string ToString(const bool with_suffix = false) const { | |
242 | 24294757 | Hex hex(this); | |
243 |
4/4✓ Branch 0 taken 136553 times.
✓ Branch 1 taken 24154465 times.
✓ Branch 3 taken 2518 times.
✓ Branch 4 taken 133481 times.
|
24295373 | const bool use_suffix = with_suffix && HasSuffix(); |
244 | 24294819 | const unsigned string_length = hex.length() + use_suffix; | |
245 |
1/2✓ Branch 2 taken 24288654 times.
✗ Branch 3 not taken.
|
24291795 | std::string result(string_length, 0); |
246 | |||
247 |
2/2✓ Branch 1 taken 950541877 times.
✓ Branch 2 taken 23179760 times.
|
975162959 | for (unsigned int i = 0; i < hex.length(); ++i) { |
248 |
2/4✓ Branch 1 taken 951042805 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 950730588 times.
✗ Branch 5 not taken.
|
950681237 | result[i] = hex[i]; |
249 | } | ||
250 | |||
251 |
2/2✓ Branch 0 taken 2518 times.
✓ Branch 1 taken 23177242 times.
|
23184115 | if (use_suffix) { |
252 |
1/2✓ Branch 1 taken 2518 times.
✗ Branch 2 not taken.
|
2518 | result[string_length - 1] = suffix; |
253 | } | ||
254 | |||
255 |
2/2✓ Branch 1 taken 2067 times.
✓ Branch 2 taken 24275213 times.
|
23184115 | assert(result.length() == string_length); |
256 | 48559136 | return result; | |
257 | } | ||
258 | |||
259 | /** | ||
260 | * Generates a hexified representation of the digest including the identifier | ||
261 | * string for newly added hashes. Output is in the form of | ||
262 | * 'openssl x509 fingerprint', e.g. 00:AA:BB:...-SHAKE128 | ||
263 | * | ||
264 | * @param with_suffix append the hash suffix (C,H,X, ...) to the result | ||
265 | * @return a string representation of the digest | ||
266 | */ | ||
267 | 160 | std::string ToFingerprint(const bool with_suffix = false) const { | |
268 | 160 | Hex hex(this); | |
269 |
3/4✓ Branch 0 taken 80 times.
✓ Branch 1 taken 80 times.
✓ Branch 3 taken 80 times.
✗ Branch 4 not taken.
|
160 | const bool use_suffix = with_suffix && HasSuffix(); |
270 | 160 | const unsigned string_length = hex.length() + kDigestSizes[algorithm] - 1 | |
271 | 160 | + use_suffix; | |
272 |
1/2✓ Branch 2 taken 160 times.
✗ Branch 3 not taken.
|
160 | std::string result(string_length, 0); |
273 | |||
274 | 160 | unsigned l = hex.length(); | |
275 |
2/2✓ Branch 0 taken 6720 times.
✓ Branch 1 taken 160 times.
|
6880 | for (unsigned int hex_i = 0, result_i = 0; hex_i < l; ++hex_i, ++result_i) { |
276 |
2/4✓ Branch 1 taken 6720 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6720 times.
✗ Branch 5 not taken.
|
6720 | result[result_i] = toupper(hex[hex_i]); |
277 |
4/4✓ Branch 0 taken 5920 times.
✓ Branch 1 taken 800 times.
✓ Branch 2 taken 2880 times.
✓ Branch 3 taken 3040 times.
|
6720 | if ((hex_i < 2 * kDigestSizes[algorithm] - 1) && (hex_i % 2 == 1)) { |
278 |
1/2✓ Branch 1 taken 2880 times.
✗ Branch 2 not taken.
|
2880 | result[++result_i] = ':'; |
279 | } | ||
280 | } | ||
281 | |||
282 |
2/2✓ Branch 0 taken 80 times.
✓ Branch 1 taken 80 times.
|
160 | if (use_suffix) { |
283 |
1/2✓ Branch 1 taken 80 times.
✗ Branch 2 not taken.
|
80 | result[string_length - 1] = suffix; |
284 | } | ||
285 | |||
286 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 160 times.
|
160 | assert(result.length() == string_length); |
287 | 320 | return result; | |
288 | } | ||
289 | |||
290 | /** | ||
291 | * Convenience method to generate a string representation of the digest. | ||
292 | * See Digest<>::ToString() for details | ||
293 | * | ||
294 | * @return a string representation including the hash suffix of the digest | ||
295 | */ | ||
296 | 4884 | std::string ToStringWithSuffix() const { return ToString(true); } | |
297 | |||
298 | /** | ||
299 | * Generate the standard relative path from the hexified digest to be used in | ||
300 | * CAS areas or cache directories. Throughout the entire system we use one | ||
301 | * directory level (first to hex digest characters) for namespace splitting. | ||
302 | * Note: This method appends the internal hash suffix to the path. | ||
303 | * | ||
304 | * @return a relative path representation of the digest including the suffix | ||
305 | */ | ||
306 | 34443 | std::string MakePath() const { return MakePathExplicit(1, 2, suffix); } | |
307 | |||
308 | /** | ||
309 | * The alternative path is used to symlink the root catalog from the webserver | ||
310 | * root to the data directory. This way, the data directory can be protected | ||
311 | * while the root catalog remains accessible. | ||
312 | */ | ||
313 | 256 | std::string MakeAlternativePath() const { | |
314 |
1/2✓ Branch 2 taken 256 times.
✗ Branch 3 not taken.
|
256 | return ".cvmfsalt-" + ToStringWithSuffix(); |
315 | } | ||
316 | |||
317 | /** | ||
318 | * Produces a relative path representation of the digest without appending the | ||
319 | * hash suffix. See Digest<>::MakePath() for more details. | ||
320 | * | ||
321 | * @return a relative path representation of the digest without the suffix | ||
322 | */ | ||
323 | 756249 | std::string MakePathWithoutSuffix() const { | |
324 | 756249 | return MakePathExplicit(1, 2, kSuffixNone); | |
325 | } | ||
326 | |||
327 | /** | ||
328 | * Generates an arbitrary path representation of the digest. Both number of | ||
329 | * directory levels and the hash-digits per level can be customized. Further- | ||
330 | * more an arbitrary hash suffix can be provided. | ||
331 | * Note: This method is mainly meant for internal usage but stays public for | ||
332 | * historical reasons. | ||
333 | * | ||
334 | * @param dir_levels the number of namespace splitting directory levels | ||
335 | * @param digits_per_level each directory level's number of hex-digits | ||
336 | * @param hash_suffix the hash suffix character to be appended | ||
337 | * @return a relative path representation of the digest | ||
338 | */ | ||
339 | 791012 | std::string MakePathExplicit(const unsigned dir_levels, | |
340 | const unsigned digits_per_level, | ||
341 | const Suffix hash_suffix = kSuffixNone) const { | ||
342 | 791012 | Hex hex(this); | |
343 | |||
344 | // figure out how big the output string needs to be | ||
345 | 791012 | const bool use_suffix = (hash_suffix != kSuffixNone); | |
346 | 791012 | const unsigned string_length = hex.length() + dir_levels + use_suffix; | |
347 | 791012 | std::string result; | |
348 |
1/2✓ Branch 1 taken 791012 times.
✗ Branch 2 not taken.
|
791012 | result.resize(string_length); |
349 | |||
350 | // build hexified hash and path delimiters | ||
351 | 791012 | unsigned i = 0; | |
352 | 791012 | unsigned pos = 0; | |
353 |
2/2✓ Branch 1 taken 31546353 times.
✓ Branch 2 taken 791012 times.
|
32337365 | for (; i < hex.length(); ++i) { |
354 |
4/4✓ Branch 0 taken 30755341 times.
✓ Branch 1 taken 791012 times.
✓ Branch 2 taken 14983145 times.
✓ Branch 3 taken 15772196 times.
|
31546353 | if (i > 0 && (i % digits_per_level == 0) |
355 |
2/2✓ Branch 0 taken 791172 times.
✓ Branch 1 taken 14191973 times.
|
14983145 | && (i / digits_per_level <= dir_levels)) { |
356 |
1/2✓ Branch 1 taken 791172 times.
✗ Branch 2 not taken.
|
791172 | result[pos++] = '/'; |
357 | } | ||
358 |
2/4✓ Branch 1 taken 31546353 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 31546353 times.
✗ Branch 5 not taken.
|
31546353 | result[pos++] = hex[i]; |
359 | } | ||
360 | |||
361 | // (optionally) add hash hint suffix | ||
362 |
2/2✓ Branch 0 taken 17273 times.
✓ Branch 1 taken 773739 times.
|
791012 | if (use_suffix) { |
363 |
1/2✓ Branch 1 taken 17273 times.
✗ Branch 2 not taken.
|
17273 | result[pos++] = hash_suffix; |
364 | } | ||
365 | |||
366 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 791012 times.
|
791012 | assert(i == hex.length()); |
367 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 791012 times.
|
791012 | assert(pos == string_length); |
368 | 1582024 | return result; | |
369 | } | ||
370 | |||
371 | 72606430 | bool IsNull() const { | |
372 |
2/2✓ Branch 0 taken 416996328 times.
✓ Branch 1 taken 18108339 times.
|
435104667 | for (unsigned i = 0; i < kDigestSizes[algorithm]; ++i) { |
373 |
2/2✓ Branch 0 taken 54498091 times.
✓ Branch 1 taken 362498237 times.
|
416996328 | if (digest[i] != 0) |
374 | 54498091 | return false; | |
375 | } | ||
376 | 18108339 | return true; | |
377 | } | ||
378 | |||
379 | /** | ||
380 | * Get a partial digest for use when only 32 bits are required | ||
381 | */ | ||
382 | 1849 | uint32_t Partial32() const { | |
383 | 1849 | const uint32_t *partial = (const uint32_t *)digest; | |
384 | 1849 | return ntohl(*partial); | |
385 | } | ||
386 | |||
387 | |||
388 | 1474243272 | void SetNull() { memset(digest, 0, digest_size_); } | |
389 | |||
390 | |||
391 | 1236633880 | bool operator==(const Digest<digest_size_, algorithm_> &other) const { | |
392 |
2/2✓ Branch 0 taken 196124615 times.
✓ Branch 1 taken 422193327 times.
|
1236633880 | if (this->algorithm != other.algorithm) |
393 | 392247672 | return false; | |
394 |
2/2✓ Branch 0 taken 4211126873 times.
✓ Branch 1 taken 217502736 times.
|
8857249852 | for (unsigned i = 0; i < kDigestSizes[algorithm]; ++i) { |
395 |
2/2✓ Branch 0 taken 204690591 times.
✓ Branch 1 taken 4006436282 times.
|
8422244826 | if (this->digest[i] != other.digest[i]) |
396 | 409381182 | return false; | |
397 | } | ||
398 | 435005026 | return true; | |
399 | } | ||
400 | |||
401 | 81772075 | bool operator!=(const Digest<digest_size_, algorithm_> &other) const { | |
402 | 81772075 | return !(*this == other); | |
403 | } | ||
404 | |||
405 | 2054891209 | bool operator<(const Digest<digest_size_, algorithm_> &other) const { | |
406 |
2/2✓ Branch 0 taken 47862859 times.
✓ Branch 1 taken 2007028350 times.
|
2054891209 | if (this->algorithm != other.algorithm) |
407 | 47862859 | return (this->algorithm < other.algorithm); | |
408 |
1/2✓ Branch 0 taken 5537561842 times.
✗ Branch 1 not taken.
|
5332982109 | for (unsigned i = 0; i < kDigestSizes[algorithm]; ++i) { |
409 |
2/2✓ Branch 0 taken 1042510237 times.
✓ Branch 1 taken 4495051605 times.
|
5537561842 | if (this->digest[i] > other.digest[i]) |
410 | 1042510237 | return false; | |
411 |
2/2✓ Branch 0 taken 1169097846 times.
✓ Branch 1 taken 3325953759 times.
|
4495051605 | if (this->digest[i] < other.digest[i]) |
412 | 1169097846 | return true; | |
413 | } | ||
414 | 90 | return false; | |
415 | } | ||
416 | |||
417 | 359805 | bool operator>(const Digest<digest_size_, algorithm_> &other) const { | |
418 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 359805 times.
|
359805 | if (this->algorithm != other.algorithm) |
419 | ✗ | return (this->algorithm > other.algorithm); | |
420 |
2/2✓ Branch 0 taken 423405 times.
✓ Branch 1 taken 540 times.
|
423945 | for (unsigned i = 0; i < kDigestSizes[algorithm]; ++i) { |
421 |
2/2✓ Branch 0 taken 136003 times.
✓ Branch 1 taken 287402 times.
|
423405 | if (this->digest[i] < other.digest[i]) |
422 | 136003 | return false; | |
423 |
2/2✓ Branch 0 taken 223262 times.
✓ Branch 1 taken 64140 times.
|
287402 | if (this->digest[i] > other.digest[i]) |
424 | 223262 | return true; | |
425 | } | ||
426 | 540 | return false; | |
427 | } | ||
428 | }; | ||
429 | |||
430 | |||
431 | struct CVMFS_EXPORT Md5 : public Digest<16, kMd5> { | ||
432 | 172134220 | Md5() : Digest<16, kMd5>() { } | |
433 | explicit Md5(const AsciiPtr ascii); | ||
434 | 1324 | explicit Md5(const HexPtr hex) : Digest<16, kMd5>(kMd5, hex) { } | |
435 | Md5(const char *chars, const unsigned length); | ||
436 | |||
437 | /** | ||
438 | * An MD5 hash can be seen as two 64bit integers. | ||
439 | */ | ||
440 | Md5(const uint64_t lo, const uint64_t hi); | ||
441 | void ToIntPair(uint64_t *lo, uint64_t *hi) const; | ||
442 | }; | ||
443 | |||
444 | struct CVMFS_EXPORT Sha1 : public Digest<20, kSha1> { }; | ||
445 | struct CVMFS_EXPORT Rmd160 : public Digest<20, kRmd160> { }; | ||
446 | struct CVMFS_EXPORT Shake128 : public Digest<20, kShake128> { }; | ||
447 | |||
448 | /** | ||
449 | * Any as such must not be used except for digest storage. | ||
450 | * To do real work, the class has to be "blessed" to be a real hash by | ||
451 | * setting the algorithm field accordingly. | ||
452 | */ | ||
453 | struct CVMFS_EXPORT Any : public Digest<kMaxDigestSize, kAny> { | ||
454 | 476176489 | Any() : Digest<kMaxDigestSize, kAny>() { } | |
455 | |||
456 | 79944704 | explicit Any(const Algorithms a, const char s = kSuffixNone) | |
457 | 79944704 | : Digest<kMaxDigestSize, kAny>() { | |
458 | 79944704 | algorithm = a; | |
459 | 79944704 | suffix = s; | |
460 | 79944704 | } | |
461 | |||
462 | 8275 | Any(const Algorithms a, | |
463 | const unsigned char *digest_buffer, | ||
464 | const Suffix suffix = kSuffixNone) | ||
465 | 8275 | : Digest<kMaxDigestSize, kAny>(a, digest_buffer, suffix) { } | |
466 | |||
467 | 206396 | explicit Any(const Algorithms a, | |
468 | const HexPtr hex, | ||
469 | const char suffix = kSuffixNone) | ||
470 | 206396 | : Digest<kMaxDigestSize, kAny>(a, hex, suffix) { } | |
471 | |||
472 | Md5 CastToMd5(); | ||
473 | }; | ||
474 | |||
475 | |||
476 | /** | ||
477 | * Actual operations on digests, like "hash a file", "hash a buffer", or | ||
478 | * iterative operations. | ||
479 | */ | ||
480 | CVMFS_EXPORT unsigned GetContextSize(const Algorithms algorithm); | ||
481 | |||
482 | /** | ||
483 | * Holds an OpenSSL context, only required for hash operations. Allows to | ||
484 | * deferr the storage allocation for the context to alloca. | ||
485 | */ | ||
486 | class CVMFS_EXPORT ContextPtr { | ||
487 | public: | ||
488 | Algorithms algorithm; | ||
489 | void *buffer; | ||
490 | unsigned size; | ||
491 | |||
492 | 330007 | ContextPtr() : algorithm(kAny), buffer(NULL), size(0) { } | |
493 | |||
494 | 429316 | explicit ContextPtr(const Algorithms a) | |
495 | 429316 | : algorithm(a), buffer(NULL), size(GetContextSize(a)) { } | |
496 | ✗ | ContextPtr(const Algorithms a, void *b) | |
497 | ✗ | : algorithm(a), buffer(b), size(GetContextSize(a)) { } | |
498 | }; | ||
499 | |||
500 | CVMFS_EXPORT void Init(ContextPtr context); | ||
501 | CVMFS_EXPORT void Update(const unsigned char *buffer, | ||
502 | const unsigned buffer_size, | ||
503 | ContextPtr context); | ||
504 | CVMFS_EXPORT void Final(ContextPtr context, Any *any_digest); | ||
505 | CVMFS_EXPORT bool HashFile(const std::string &filename, Any *any_digest); | ||
506 | CVMFS_EXPORT bool HashFd(int fd, Any *any_digest); | ||
507 | CVMFS_EXPORT void HashMem(const unsigned char *buffer, | ||
508 | const unsigned buffer_size, | ||
509 | Any *any_digest); | ||
510 | CVMFS_EXPORT void HashString(const std::string &content, Any *any_digest); | ||
511 | CVMFS_EXPORT void Hmac(const std::string &key, | ||
512 | const unsigned char *buffer, | ||
513 | const unsigned buffer_size, | ||
514 | Any *any_digest); | ||
515 | 20 | inline void HmacString(const std::string &key, const std::string &content, | |
516 | Any *any_digest) { | ||
517 | 20 | Hmac(key, | |
518 | 20 | reinterpret_cast<const unsigned char *>(content.data()), | |
519 | 20 | content.size(), | |
520 | any_digest); | ||
521 | 20 | } | |
522 | |||
523 | /** | ||
524 | * Only used for AWS4 signature. | ||
525 | * | ||
526 | * Adding SHA-256 to the standard hash infrastructure would generally bloat the | ||
527 | * digets size to 32 bytes and require client data structure transformation | ||
528 | * during hotpatch. | ||
529 | */ | ||
530 | CVMFS_EXPORT std::string Hmac256(const std::string &key, | ||
531 | const std::string &content, | ||
532 | bool raw_output = false); | ||
533 | CVMFS_EXPORT std::string Sha256File(const std::string &filename); | ||
534 | CVMFS_EXPORT std::string Sha256Mem(const unsigned char *buffer, | ||
535 | const unsigned buffer_size); | ||
536 | CVMFS_EXPORT std::string Sha256String(const std::string &content); | ||
537 | |||
538 | CVMFS_EXPORT | ||
539 | Algorithms ParseHashAlgorithm(const std::string &algorithm_option); | ||
540 | CVMFS_EXPORT | ||
541 | Any MkFromHexPtr(const HexPtr hex, const Suffix suffix = kSuffixNone); | ||
542 | CVMFS_EXPORT Any MkFromSuffixedHexPtr(const HexPtr hex); | ||
543 | |||
544 | } // namespace shash | ||
545 | |||
546 | #ifdef CVMFS_NAMESPACE_GUARD | ||
547 | } // namespace CVMFS_NAMESPACE_GUARD | ||
548 | #endif | ||
549 | |||
550 | #endif // CVMFS_CRYPTO_HASH_H_ | ||
551 |