| 6 | #define BASE_HASH_MD5_CONSTEXPR_INTERNAL_H_ |
| 7 |
| 8 | #include <array> |
| 9 | #include <cstddef> |
| 10 | #include <cstdint> |
| 11 |
| 12 | #include "base/hash/md5.h" |
| 13 | #include "base/logging.h" |
| 14 |
| 15 | namespace base { |
| 16 | namespace internal { |
| 17 |
| 18 | // The implementation here is based on the pseudocode provided by Wikipedia: |
| 19 | // https://en.wikipedia.org/wiki/MD5#Pseudocode |
| 20 | struct MD5CE { |
| 21 | ////////////////////////////////////////////////////////////////////////////// |
| 22 | // DATA STRUCTURES |
| 23 |
| 24 | // The data representation at each round is a 4-tuple of uint32_t. |
| 25 | struct IntermediateData { |
| 26 | uint32_t a; |
| 27 | uint32_t b; |
| 28 | uint32_t c; |
| 29 | uint32_t d; |
| 30 | }; |
| 31 |
| 32 | // The input data for a single round consists of 16 uint32_t (64 bytes). |
| 33 | using RoundData = std::array<uint32_t, 16>; |
| 34 |
| 35 | ////////////////////////////////////////////////////////////////////////////// |
| 36 | // CONSTANTS |
| 37 |
| 38 | static constexpr std::array<uint32_t, 64> kConstants = { |
| 39 | {0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, |
| 40 | 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, |
| 41 | 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340, |
| 42 | 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, |
| 43 | 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, |
| 44 | 0x676f02d9, 0x8d2a4c8a, 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, |
| 45 | 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 0x289b7ec6, 0xeaa127fa, |
| 46 | 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, |
| 47 | 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, |
| 48 | 0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, |
| 49 | 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391}}; |
| 50 |
| 51 | static constexpr std::array<uint32_t, 16> kShifts = { |
| 52 | {7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21}}; |
| 53 |
| 54 | // The initial intermediate data. |
| 55 | static constexpr IntermediateData kInitialIntermediateData{ |
| 56 | 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476}; |
| 57 |
| 58 | ////////////////////////////////////////////////////////////////////////////// |
| 59 | // PADDED MESSAGE GENERATION / EXTRACTION |
| 60 |
| 61 | // Given the message length, calculates the padded message length. There has |
| 62 | // to be room for the 1-byte end-of-message marker, plus 8 bytes for the |
| 63 | // uint64_t encoded message length, all rounded up to a multiple of 64 bytes. |
| 64 | static constexpr uint32_t GetPaddedMessageLength(const uint32_t n) { |
| 65 | return (((n + 1 + 8) + 63) / 64) * 64; |
| 66 | } |
| 67 |
| 68 | // Extracts the |i|th byte of a uint64_t, where |i == 0| extracts the least |
| 69 | // significant byte. It is expected that 0 <= i < 8. |
| 70 | static constexpr uint8_t ExtractByte(const uint64_t value, const uint32_t i) { |
| 71 | DCHECK(i < 8); |
| 72 | return static_cast<uint8_t>((value >> (i * 8)) & 0xff); |
| 73 | } |
| 74 |
| 75 | // Extracts the |i|th byte of a message of length |n|. |
| 76 | static constexpr uint8_t GetPaddedMessageByte(const char* data, |
| 77 | const uint32_t n, |
| 78 | const uint32_t m, |
| 79 | const uint32_t i) { |
| 80 | DCHECK(i < m); |
| 81 | DCHECK(n < m); |
| 82 | DCHECK(m % 64 == 0); |
| 83 | if (i < n) { |
| 84 | // Emit the message itself... |
| 85 | return data[i]; |
| 86 | } else if (i == n) { |
| 87 | // ...followed by the end of message marker. |
| 88 | return 0x80; |
| 89 | } else if (i >= m - 8) { |
| 90 | // The last 8 bytes encode the original message length times 8. |
| 91 | return ExtractByte(n * 8, i - (m - 8)); |
| 92 | } else { |
| 93 | // And everything else is just empyt padding. |
| 94 | return 0; |
| 95 | } |
| 96 | } |
| 97 |
| 98 | // Extracts the uint32_t starting at position |i| from the padded message |
| 99 | // generate by the provided input |data| of length |n|. The bytes are treated |
| 100 | // in little endian order. |
| 101 | static constexpr uint32_t GetPaddedMessageWord(const char* data, |
| 102 | const uint32_t n, |
| 103 | const uint32_t m, |
| 104 | const uint32_t i) { |
| 105 | DCHECK(i % 4 == 0); |
| 106 | DCHECK(i < m); |
| 107 | DCHECK(n < m); |
| 108 | DCHECK(m % 64 == 0); |
| 109 | return static_cast<uint32_t>(GetPaddedMessageByte(data, n, m, i)) | |
| 110 | static_cast<uint32_t>((GetPaddedMessageByte(data, n, m, i + 1)) |
| 111 | << 8) | |
| 112 | static_cast<uint32_t>((GetPaddedMessageByte(data, n, m, i + 2)) |
| 113 | << 16) | |
| 114 | static_cast<uint32_t>((GetPaddedMessageByte(data, n, m, i + 3)) |
| 115 | << 24); |
| 116 | } |
| 117 |
| 118 | // Given an input buffer of length |n| bytes, extracts one round worth of data |
| 119 | // starting at offset |i|. |
| 120 | static constexpr RoundData GetRoundData(const char* data, |
| 121 | const uint32_t n, |
| 122 | const uint32_t m, |
| 123 | const uint32_t i) { |
| 124 | DCHECK(i % 64 == 0); |
| 125 | DCHECK(i < m); |
| 126 | DCHECK(n < m); |
| 127 | DCHECK(m % 64 == 0); |
| 128 | return RoundData{{GetPaddedMessageWord(data, n, m, i), |
| 129 | GetPaddedMessageWord(data, n, m, i + 4), |
| 130 | GetPaddedMessageWord(data, n, m, i + 8), |
| 131 | GetPaddedMessageWord(data, n, m, i + 12), |
| 132 | GetPaddedMessageWord(data, n, m, i + 16), |
| 133 | GetPaddedMessageWord(data, n, m, i + 20), |
| 134 | GetPaddedMessageWord(data, n, m, i + 24), |
| 135 | GetPaddedMessageWord(data, n, m, i + 28), |
| 136 | GetPaddedMessageWord(data, n, m, i + 32), |
| 137 | GetPaddedMessageWord(data, n, m, i + 36), |
| 138 | GetPaddedMessageWord(data, n, m, i + 40), |
| 139 | GetPaddedMessageWord(data, n, m, i + 44), |
| 140 | GetPaddedMessageWord(data, n, m, i + 48), |
| 141 | GetPaddedMessageWord(data, n, m, i + 52), |
| 142 | GetPaddedMessageWord(data, n, m, i + 56), |
| 143 | GetPaddedMessageWord(data, n, m, i + 60)}}; |
| 144 | } |
| 145 |
| 146 | ////////////////////////////////////////////////////////////////////////////// |
| 147 | // HASH IMPLEMENTATION |
| 148 |
| 149 | // Mixes elements |b|, |c| and |d| at round |i| of the calculation. |
| 150 | static constexpr uint32_t CalcF(const uint32_t i, |
| 151 | const uint32_t b, |
| 152 | const uint32_t c, |
| 153 | const uint32_t d) { |
| 154 | DCHECK(i < 64); |
| 155 | if (i < 16) { |
| 156 | return d ^ (b & (c ^ d)); |
| 157 | } else if (i < 32) { |
| 158 | return c ^ (d & (b ^ c)); |
| 159 | } else if (i < 48) { |
| 160 | return b ^ c ^ d; |
| 161 | } else { |
| 162 | return c ^ (b | (~d)); |
| 163 | } |
| 164 | } |
| 165 | static constexpr uint32_t CalcF(const uint32_t i, |
| 166 | const IntermediateData& intermediate) { |
| 167 | return CalcF(i, intermediate.b, intermediate.c, intermediate.d); |
| 168 | } |
| 169 |
| 170 | // Calculates the indexing function at round |i|. |
| 171 | static constexpr uint32_t CalcG(const uint32_t i) { |
| 172 | DCHECK(i < 64); |
| 173 | if (i < 16) { |
| 174 | return i; |
| 175 | } else if (i < 32) { |
| 176 | return (5 * i + 1) % 16; |
| 177 | } else if (i < 48) { |
| 178 | return (3 * i + 5) % 16; |
| 179 | } else { |
| 180 | return (7 * i) % 16; |
| 181 | } |
| 182 | } |
| 183 |
| 184 | // Calculates the rotation to be applied at round |i|. |
| 185 | static constexpr uint32_t GetShift(const uint32_t i) { |
| 186 | DCHECK(i < 64); |
| 187 | return kShifts[(i / 16) * 4 + (i % 4)]; |
| 188 | } |
| 189 |
| 190 | // Rotates to the left the given |value| by the given |bits|. |
| 191 | static constexpr uint32_t LeftRotate(const uint32_t value, |
| 192 | const uint32_t bits) { |
| 193 | DCHECK(bits < 32); |
| 194 | return (value << bits) | (value >> (32 - bits)); |
| 195 | } |
| 196 |
| 197 | // Applies the ith step of mixing. |
| 198 | static constexpr IntermediateData ApplyStep( |
| 199 | const uint32_t i, |
| 200 | const RoundData& data, |
| 201 | const IntermediateData& intermediate) { |
| 202 | DCHECK(i < 64); |
| 203 | const uint32_t g = CalcG(i); |
| 204 | DCHECK(g < 16); |
| 205 | const uint32_t f = |
| 206 | CalcF(i, intermediate) + intermediate.a + kConstants[i] + data[g]; |
| 207 | const uint32_t s = GetShift(i); |
| 208 | return IntermediateData{/* a */ intermediate.d, |
| 209 | /* b */ intermediate.b + LeftRotate(f, s), |
| 210 | /* c */ intermediate.b, |
| 211 | /* d */ intermediate.c}; |
| 212 | } |
| 213 |
| 214 | // Adds two IntermediateData together. |
| 215 | static constexpr IntermediateData Add(const IntermediateData& intermediate1, |
| 216 | const IntermediateData& intermediate2) { |
| 217 | return IntermediateData{ |
| 218 | intermediate1.a + intermediate2.a, intermediate1.b + intermediate2.b, |
| 219 | intermediate1.c + intermediate2.c, intermediate1.d + intermediate2.d}; |
| 220 | } |
| 221 |
| 222 | // Processes an entire message. |
| 223 | static constexpr IntermediateData ProcessMessage(const char* message, |
| 224 | const uint32_t n) { |
| 225 | const uint32_t m = GetPaddedMessageLength(n); |
| 226 | IntermediateData intermediate0 = kInitialIntermediateData; |
| 227 | for (uint32_t offset = 0; offset < m; offset += 64) { |
| 228 | RoundData data = GetRoundData(message, n, m, offset); |
| 229 | IntermediateData intermediate1 = intermediate0; |
| 230 | for (uint32_t i = 0; i < 64; ++i) |
| 231 | intermediate1 = ApplyStep(i, data, intermediate1); |
| 232 | intermediate0 = Add(intermediate0, intermediate1); |
| 233 | } |
| 234 | return intermediate0; |
| 235 | } |
| 236 |
| 237 | ////////////////////////////////////////////////////////////////////////////// |
| 238 | // HELPER FUNCTIONS |
| 239 |
| 240 | // Converts an IntermediateData to a final digest. |
| 241 | static constexpr MD5Digest IntermediateDataToMD5Digest( |
| 242 | const IntermediateData& intermediate) { |
| 243 | return MD5Digest{{static_cast<uint8_t>((intermediate.a >> 0) & 0xff), |
| 244 | static_cast<uint8_t>((intermediate.a >> 8) & 0xff), |
| 245 | static_cast<uint8_t>((intermediate.a >> 16) & 0xff), |
| 246 | static_cast<uint8_t>((intermediate.a >> 24) & 0xff), |
| 247 | static_cast<uint8_t>((intermediate.b >> 0) & 0xff), |
| 248 | static_cast<uint8_t>((intermediate.b >> 8) & 0xff), |
| 249 | static_cast<uint8_t>((intermediate.b >> 16) & 0xff), |
| 250 | static_cast<uint8_t>((intermediate.b >> 24) & 0xff), |
| 251 | static_cast<uint8_t>((intermediate.c >> 0) & 0xff), |
| 252 | static_cast<uint8_t>((intermediate.c >> 8) & 0xff), |
| 253 | static_cast<uint8_t>((intermediate.c >> 16) & 0xff), |
| 254 | static_cast<uint8_t>((intermediate.c >> 24) & 0xff), |
| 255 | static_cast<uint8_t>((intermediate.d >> 0) & 0xff), |
| 256 | static_cast<uint8_t>((intermediate.d >> 8) & 0xff), |
| 257 | static_cast<uint8_t>((intermediate.d >> 16) & 0xff), |
| 258 | static_cast<uint8_t>((intermediate.d >> 24) & 0xff)}}; |
| 259 | } |
| 260 |
| 261 | static constexpr uint32_t StringLength(const char* string) { |
| 262 | const char* end = string; |
| 263 | while (*end != 0) |
| 264 | ++end; |
| 265 | // Double check that the precision losing conversion is safe. |
| 266 | DCHECK(end >= string); |
| 267 | DCHECK(static_cast<std::ptrdiff_t>(static_cast<uint32_t>(end - string)) == |
| 268 | (end - string)); |
| 269 | return static_cast<uint32_t>(end - string); |
| 270 | } |
| 271 |
| 272 | static constexpr uint32_t SwapEndian(uint32_t a) { |
| 273 | return ((a & 0xff) << 24) | (((a >> 8) & 0xff) << 16) | |
| 274 | (((a >> 16) & 0xff) << 8) | ((a >> 24) & 0xff); |
| 275 | } |
| 276 |
| 277 | ////////////////////////////////////////////////////////////////////////////// |
| 278 | // WRAPPER FUNCTIONS |
| 279 |
| 280 | static constexpr MD5Digest Sum(const char* data, uint32_t n) { |
| 281 | return IntermediateDataToMD5Digest(ProcessMessage(data, n)); |
| 282 | } |
| 283 |
| 284 | static constexpr uint64_t Hash64(const char* data, uint32_t n) { |
| 285 | IntermediateData intermediate = ProcessMessage(data, n); |
| 286 | return (static_cast<uint64_t>(SwapEndian(intermediate.a)) << 32) | |
| 287 | static_cast<uint64_t>(SwapEndian(intermediate.b)); |
| 288 | } |
| 289 |
| 290 | static constexpr uint32_t Hash32(const char* data, uint32_t n) { |
| 291 | IntermediateData intermediate = ProcessMessage(data, n); |
| 292 | return SwapEndian(intermediate.a); |
| 293 | } |
| 294 | }; |
| 295 |
| 296 | } // namespace internal |
| 297 |
| 298 | // Implementations of the functions exposed in the public header. |
| 299 |
| 300 | constexpr MD5Digest MD5SumConstexpr(const char* string) { |
| 301 | return internal::MD5CE::Sum(string, internal::MD5CE::StringLength(string)); |
| 302 | } |
| 303 |
| 304 | constexpr MD5Digest MD5SumConstexpr(const char* string, uint32_t length) { |
| 305 | return internal::MD5CE::Sum(string, length); |
| 306 | } |
| 307 |
| 308 | constexpr uint64_t MD5Hash64Constexpr(const char* string) { |
| 309 | return internal::MD5CE::Hash64(string, internal::MD5CE::StringLength(string)); |
| 310 | } |
| 311 |
| 312 | constexpr uint64_t MD5Hash64Constexpr(const char* string, uint32_t length) { |
| 313 | return internal::MD5CE::Hash64(string, length); |
| 314 | } |
| 315 |
| 316 | constexpr uint32_t MD5Hash32Constexpr(const char* string) { |
| 317 | return internal::MD5CE::Hash32(string, internal::MD5CE::StringLength(string)); |
| 318 | } |
| 319 |
| 320 | constexpr uint32_t MD5Hash32Constexpr(const char* string, uint32_t length) { |
| 321 | return internal::MD5CE::Hash32(string, length); |
| 322 | } |
| 323 |
| 324 | } // namespace base |
| 325 |
| 326 | #endif // BASE_HASH_MD5_CONSTEXPR_INTERNAL_H_ |
| 327 |