/src/trafficserver/src/tscore/CryptoHash.cc
Line | Count | Source |
1 | | /** @file |
2 | | |
3 | | Generic wrapper for cryptographic hashes. |
4 | | |
5 | | @section license License |
6 | | |
7 | | Licensed to the Apache Software Foundation (ASF) under one |
8 | | or more contributor license agreements. See the NOTICE file |
9 | | distributed with this work for additional information |
10 | | regarding copyright ownership. The ASF licenses this file |
11 | | to you under the Apache License, Version 2.0 (the |
12 | | "License"); you may not use this file except in compliance |
13 | | with the License. You may obtain a copy of the License at |
14 | | |
15 | | http://www.apache.org/licenses/LICENSE-2.0 |
16 | | |
17 | | Unless required by applicable law or agreed to in writing, software |
18 | | distributed under the License is distributed on an "AS IS" BASIS, |
19 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
20 | | See the License for the specific language governing permissions and |
21 | | limitations under the License. |
22 | | */ |
23 | | |
24 | | #include <cstdlib> |
25 | | #include <cstring> |
26 | | #include <new> |
27 | | #include "tscore/ink_assert.h" |
28 | | #include "tscore/ink_platform.h" |
29 | | #include "tscore/CryptoHash.h" |
30 | | #include "tscore/SHA256.h" |
31 | | |
32 | | #if TS_ENABLE_FIPS == 1 |
33 | | CryptoContext::HashType CryptoContext::Setting = CryptoContext::SHA256; |
34 | | #else |
35 | | #include "tscore/MD5.h" |
36 | | #include "tscore/MMH.h" |
37 | | CryptoContext::HashType CryptoContext::Setting = CryptoContext::MD5; |
38 | | #endif |
39 | | |
40 | | CryptoContext::CryptoContext() |
41 | 0 | { |
42 | 0 | switch (Setting) { |
43 | 0 | case UNSPECIFIED: |
44 | 0 | #if TS_ENABLE_FIPS == 0 |
45 | 0 | case MD5: |
46 | 0 | static_assert(OBJ_SIZE >= sizeof(MD5Context)); |
47 | 0 | new (_base) MD5Context; |
48 | 0 | break; |
49 | | #else |
50 | | case SHA256: |
51 | | static_assert(OBJ_SIZE >= sizeof(SHA256Context)); |
52 | | new (_base) SHA256Context; |
53 | | break; |
54 | | #endif |
55 | 0 | default: |
56 | 0 | ink_release_assert(!"Invalid global URL hash context"); |
57 | 0 | }; |
58 | 0 | } |
59 | | |
60 | | /** |
61 | | @brief Converts a hash to a null-terminated string |
62 | | |
63 | | Externalizes an hash as a null-terminated string into the first argument. |
64 | | Does so without internal procedure calls. |
65 | | Side Effects: none. |
66 | | Reentrancy: n/a. |
67 | | Thread Safety: safe. |
68 | | Mem Management: stomps the passed dest char*. |
69 | | |
70 | | @return returns the passed destination string ptr. |
71 | | */ |
72 | | /* reentrant version */ |
73 | | static char * |
74 | | ink_code_to_hex_str(char *dest, uint8_t const *hash) |
75 | 0 | { |
76 | 0 | int i; |
77 | 0 | char *d; |
78 | |
|
79 | 0 | static char hex_digits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; |
80 | |
|
81 | 0 | d = dest; |
82 | 0 | for (i = 0; i < CRYPTO_HASH_SIZE; i += 4) { |
83 | 0 | *(d + 0) = hex_digits[hash[i + 0] >> 4]; |
84 | 0 | *(d + 1) = hex_digits[hash[i + 0] & 15]; |
85 | 0 | *(d + 2) = hex_digits[hash[i + 1] >> 4]; |
86 | 0 | *(d + 3) = hex_digits[hash[i + 1] & 15]; |
87 | 0 | *(d + 4) = hex_digits[hash[i + 2] >> 4]; |
88 | 0 | *(d + 5) = hex_digits[hash[i + 2] & 15]; |
89 | 0 | *(d + 6) = hex_digits[hash[i + 3] >> 4]; |
90 | 0 | *(d + 7) = hex_digits[hash[i + 3] & 15]; |
91 | 0 | d += 8; |
92 | 0 | } |
93 | 0 | *d = '\0'; |
94 | 0 | return (dest); |
95 | 0 | } |
96 | | |
97 | | char * |
98 | | CryptoHash::toHexStr(char buffer[(CRYPTO_HASH_SIZE * 2) + 1]) const |
99 | 0 | { |
100 | 0 | return ink_code_to_hex_str(buffer, u8); |
101 | 0 | } |
102 | | |
103 | | namespace ts |
104 | | { |
105 | | swoc::BufferWriter & |
106 | | bwformat(swoc::BufferWriter &w, swoc::bwf::Spec const &spec, ts::CryptoHash const &hash) |
107 | 0 | { |
108 | 0 | swoc::bwf::Spec local_spec{spec}; |
109 | 0 | if ('X' != local_spec._type) { |
110 | 0 | local_spec._type = 'x'; |
111 | 0 | } |
112 | 0 | return bwformat(w, local_spec, std::string_view(reinterpret_cast<const char *>(hash.u8), CRYPTO_HASH_SIZE)); |
113 | 0 | } |
114 | | } // namespace ts |