xref: /trafficserver/src/tscore/CryptoHash.cc (revision 4cfd5a73)
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/ink_code.h"
30 #include "tscore/CryptoHash.h"
31 #include "tscore/SHA256.h"
32 
33 #if TS_ENABLE_FIPS == 1
34 CryptoContext::HashType CryptoContext::Setting = CryptoContext::SHA256;
35 #else
36 #include "tscore/INK_MD5.h"
37 #include "tscore/MMH.h"
38 CryptoContext::HashType CryptoContext::Setting = CryptoContext::MD5;
39 #endif
40 
CryptoContext()41 CryptoContext::CryptoContext()
42 {
43   switch (Setting) {
44   case UNSPECIFIED:
45 #if TS_ENABLE_FIPS == 0
46   case MD5:
47     new (_obj) MD5Context;
48     break;
49   case MMH:
50     new (_obj) MMHContext;
51     break;
52 #else
53   case SHA256:
54     new (_obj) SHA256Context;
55     break;
56 #endif
57   default:
58     ink_release_assert(!"Invalid global URL hash context");
59   };
60 #if TS_ENABLE_FIPS == 0
61   static_assert(CryptoContext::OBJ_SIZE >= sizeof(MD5Context), "bad OBJ_SIZE");
62   static_assert(CryptoContext::OBJ_SIZE >= sizeof(MMHContext), "bad OBJ_SIZE");
63 #else
64   static_assert(CryptoContext::OBJ_SIZE >= sizeof(SHA256Context), "bad OBJ_SIZE");
65 #endif
66 }
67 
68 /**
69   @brief Converts a hash to a null-terminated string
70 
71   Externalizes an hash as a null-terminated string into the first argument.
72   Does so without internal procedure calls.
73   Side Effects: none.
74   Reentrancy:     n/a.
75   Thread Safety:  safe.
76   Mem Management: stomps the passed dest char*.
77 
78   @return returns the passed destination string ptr.
79 */
80 /* reentrant version */
81 static char *
ink_code_to_hex_str(char * dest,uint8_t const * hash)82 ink_code_to_hex_str(char *dest, uint8_t const *hash)
83 {
84   int i;
85   char *d;
86 
87   static char hex_digits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
88 
89   d = dest;
90   for (i = 0; i < CRYPTO_HASH_SIZE; i += 4) {
91     *(d + 0) = hex_digits[hash[i + 0] >> 4];
92     *(d + 1) = hex_digits[hash[i + 0] & 15];
93     *(d + 2) = hex_digits[hash[i + 1] >> 4];
94     *(d + 3) = hex_digits[hash[i + 1] & 15];
95     *(d + 4) = hex_digits[hash[i + 2] >> 4];
96     *(d + 5) = hex_digits[hash[i + 2] & 15];
97     *(d + 6) = hex_digits[hash[i + 3] >> 4];
98     *(d + 7) = hex_digits[hash[i + 3] & 15];
99     d += 8;
100   }
101   *d = '\0';
102   return (dest);
103 }
104 
105 char *
toHexStr(char buffer[(CRYPTO_HASH_SIZE * 2)+1]) const106 CryptoHash::toHexStr(char buffer[(CRYPTO_HASH_SIZE * 2) + 1]) const
107 {
108   return ink_code_to_hex_str(buffer, u8);
109 }
110 
111 namespace ats
112 {
113 ts::BufferWriter &
bwformat(ts::BufferWriter & w,ts::BWFSpec const & spec,ats::CryptoHash const & hash)114 bwformat(ts::BufferWriter &w, ts::BWFSpec const &spec, ats::CryptoHash const &hash)
115 {
116   ts::BWFSpec local_spec{spec};
117   if ('X' != local_spec._type) {
118     local_spec._type = 'x';
119   }
120   return bwformat(w, local_spec, std::string_view(reinterpret_cast<const char *>(hash.u8), CRYPTO_HASH_SIZE));
121 }
122 } // namespace ats
123