| 1 |
/* |
| 2 |
* Copyright 2012 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
* |
| 7 |
* The following code is based on the description in RFC 1321. |
| 8 |
* http://www.ietf.org/rfc/rfc1321.txt |
| 9 |
*/ |
| 10 |
|
| 11 |
//The following macros can be defined to affect the MD5 code generated. |
| 12 |
//SK_MD5_CLEAR_DATA causes all intermediate state to be overwritten with 0's. |
| 13 |
//SK_CPU_LENDIAN allows 32 bit <=> 8 bit conversions without copies (if alligned). |
| 14 |
//SK_CPU_FAST_UNALIGNED_ACCESS allows 32 bit <=> 8 bit conversions without copies if SK_CPU_LENDIAN. |
| 15 |
|
| 16 |
#include "src/base/SkUtils.h" |
| 17 |
#include "src/core/SkMD5.h" |
| 18 |
|
| 19 |
#include "include/private/base/SkFeatures.h" |
| 20 |
#include "include/private/base/SkMalloc.h" |
| 21 |
|
| 22 |
/** MD5 basic transformation. Transforms state based on block. */ |
| 23 |
static void transform(uint32_t state[4], const uint8_t block[64]); |
| 24 |
|
| 25 |
/** Encodes input into output (4 little endian 32 bit values). */ |
| 26 |
static void encode(uint8_t output[16], const uint32_t input[4]); |
| 27 |
|
| 28 |
/** Encodes input into output (little endian 64 bit value). */ |
| 29 |
static void encode(uint8_t output[8], uint64_t input); |
| 30 |
|
| 31 |
/** Decodes input (4 little endian 32 bit values) into storage, if required. */ |
| 32 |
static const uint32_t* decode(uint32_t storage[16], const uint8_t input[64]); |
| 33 |
|
| 34 |
SkMD5::SkMD5() : byteCount(0) { |
| 35 |
// These are magic numbers from the specification. |
| 36 |
this->state[0] = 0x67452301; |
| 37 |
this->state[1] = 0xefcdab89; |
| 38 |
this->state[2] = 0x98badcfe; |
| 39 |
this->state[3] = 0x10325476; |
| 40 |
} |
| 41 |
|
| 42 |
bool SkMD5::write(const void* buf, size_t inputLength) { |
| 43 |
const uint8_t* input = reinterpret_cast<const uint8_t*>(buf); |
| 44 |
unsigned int bufferIndex = (unsigned int)(this->byteCount & 0x3F); |
| 45 |
unsigned int bufferAvailable = 64 - bufferIndex; |
| 46 |
|
| 47 |
unsigned int inputIndex; |
| 48 |
if (inputLength >= bufferAvailable) { |
| 49 |
if (bufferIndex) { |
| 50 |
sk_careful_memcpy(&this->buffer[bufferIndex], input, bufferAvailable); |
| 51 |
transform(this->state, this->buffer); |
| 52 |
inputIndex = bufferAvailable; |
| 53 |
} else { |
| 54 |
inputIndex = 0; |
| 55 |
} |
| 56 |
|
| 57 |
for (; inputIndex + 63 < inputLength; inputIndex += 64) { |
| 58 |
transform(this->state, &input[inputIndex]); |
| 59 |
} |
| 60 |
|
| 61 |
bufferIndex = 0; |
| 62 |
} else { |
| 63 |
inputIndex = 0; |
| 64 |
} |
| 65 |
|
| 66 |
sk_careful_memcpy(&this->buffer[bufferIndex], &input[inputIndex], inputLength - inputIndex); |
| 67 |
|
| 68 |
this->byteCount += inputLength; |
| 69 |
return true; |
| 70 |
} |
| 71 |
|
| 72 |
SkMD5::Digest SkMD5::finish() { |
| 73 |
SkMD5::Digest digest; |
| 74 |
// Get the number of bits before padding. |
| 75 |
uint8_t bits[8]; |
| 76 |
encode(bits, this->byteCount << 3); |
| 77 |
|
| 78 |
// Pad out to 56 mod 64. |
| 79 |
unsigned int bufferIndex = (unsigned int)(this->byteCount & 0x3F); |
| 80 |
unsigned int paddingLength = (bufferIndex < 56) ? (56 - bufferIndex) : (120 - bufferIndex); |
| 81 |
static const uint8_t PADDING[64] = { |
| 82 |
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 83 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 84 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 85 |
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 86 |
}; |
| 87 |
(void)this->write(PADDING, paddingLength); |
| 88 |
|
| 89 |
// Append length (length before padding, will cause final update). |
| 90 |
(void)this->write(bits, 8); |
| 91 |
|
| 92 |
// Write out digest. |
| 93 |
encode(digest.data, this->state); |
| 94 |
|
| 95 |
#if defined(SK_MD5_CLEAR_DATA) |
| 96 |
// Clear state. |
| 97 |
memset(this, 0, sizeof(*this)); |
| 98 |
#endif |
| 99 |
return digest; |
| 100 |
} |
| 101 |
|
| 102 |
static SkString to_hex_string(const uint8_t* data, const char* hexDigits) { |
| 103 |
SkString hexString(2 * sizeof(SkMD5::Digest::data)); |
| 104 |
for (size_t i = 0; i < sizeof(SkMD5::Digest::data); ++i) { |
| 105 |
uint8_t byte = data[i]; |
| 106 |
hexString[2 * i + 0] = hexDigits[byte >> 4]; |
| 107 |
hexString[2 * i + 1] = hexDigits[byte & 0xF]; |
| 108 |
} |
| 109 |
return hexString; |
| 110 |
} |
| 111 |
|
| 112 |
SkString SkMD5::Digest::toHexString() const { |
| 113 |
return to_hex_string(data, SkHexadecimalDigits::gUpper); |
| 114 |
} |
| 115 |
|
| 116 |
SkString SkMD5::Digest::toLowercaseHexString() const { |
| 117 |
return to_hex_string(data, SkHexadecimalDigits::gLower); |
| 118 |
} |
| 119 |
|
| 120 |
struct F { uint32_t operator()(uint32_t x, uint32_t y, uint32_t z) { |
| 121 |
//return (x & y) | ((~x) & z); |
| 122 |
return ((y ^ z) & x) ^ z; //equivelent but faster |
| 123 |
}}; |
| 124 |
|
| 125 |
struct G { uint32_t operator()(uint32_t x, uint32_t y, uint32_t z) { |
| 126 |
return (x & z) | (y & (~z)); |
| 127 |
//return ((x ^ y) & z) ^ y; //equivelent but slower |
| 128 |
}}; |
| 129 |
|
| 130 |
struct H { uint32_t operator()(uint32_t x, uint32_t y, uint32_t z) { |
| 131 |
return x ^ y ^ z; |
| 132 |
}}; |
| 133 |
|
| 134 |
struct I { uint32_t operator()(uint32_t x, uint32_t y, uint32_t z) { |
| 135 |
return y ^ (x | (~z)); |
| 136 |
}}; |
| 137 |
|
| 138 |
/** Rotates x left n bits. */ |
| 139 |
static inline uint32_t rotate_left(uint32_t x, uint8_t n) { |
| 140 |
return (x << n) | (x >> (32 - n)); |
| 141 |
} |
| 142 |
|
| 143 |
template <typename T> |
| 144 |
static inline void operation(T operation, uint32_t& a, uint32_t b, uint32_t c, uint32_t d, |
| 145 |
uint32_t x, uint8_t s, uint32_t t) { |
| 146 |
a = b + rotate_left(a + operation(b, c, d) + x + t, s); |
| 147 |
} |
| 148 |
|
| 149 |
static void transform(uint32_t state[4], const uint8_t block[64]) { |
| 150 |
uint32_t a = state[0], b = state[1], c = state[2], d = state[3]; |
| 151 |
|
| 152 |
uint32_t storage[16]; |
| 153 |
const uint32_t* X = decode(storage, block); |
| 154 |
|
| 155 |
// Round 1 |
| 156 |
operation(F(), a, b, c, d, X[ 0], 7, 0xd76aa478); // 1 |
| 157 |
operation(F(), d, a, b, c, X[ 1], 12, 0xe8c7b756); // 2 |
| 158 |
operation(F(), c, d, a, b, X[ 2], 17, 0x242070db); // 3 |
| 159 |
operation(F(), b, c, d, a, X[ 3], 22, 0xc1bdceee); // 4 |
| 160 |
operation(F(), a, b, c, d, X[ 4], 7, 0xf57c0faf); // 5 |
| 161 |
operation(F(), d, a, b, c, X[ 5], 12, 0x4787c62a); // 6 |
| 162 |
operation(F(), c, d, a, b, X[ 6], 17, 0xa8304613); // 7 |
| 163 |
operation(F(), b, c, d, a, X[ 7], 22, 0xfd469501); // 8 |
| 164 |
operation(F(), a, b, c, d, X[ 8], 7, 0x698098d8); // 9 |
| 165 |
operation(F(), d, a, b, c, X[ 9], 12, 0x8b44f7af); // 10 |
| 166 |
operation(F(), c, d, a, b, X[10], 17, 0xffff5bb1); // 11 |
| 167 |
operation(F(), b, c, d, a, X[11], 22, 0x895cd7be); // 12 |
| 168 |
operation(F(), a, b, c, d, X[12], 7, 0x6b901122); // 13 |
| 169 |
operation(F(), d, a, b, c, X[13], 12, 0xfd987193); // 14 |
| 170 |
operation(F(), c, d, a, b, X[14], 17, 0xa679438e); // 15 |
| 171 |
operation(F(), b, c, d, a, X[15], 22, 0x49b40821); // 16 |
| 172 |
|
| 173 |
// Round 2 |
| 174 |
operation(G(), a, b, c, d, X[ 1], 5, 0xf61e2562); // 17 |
| 175 |
operation(G(), d, a, b, c, X[ 6], 9, 0xc040b340); // 18 |
| 176 |
operation(G(), c, d, a, b, X[11], 14, 0x265e5a51); // 19 |
| 177 |
operation(G(), b, c, d, a, X[ 0], 20, 0xe9b6c7aa); // 20 |
| 178 |
operation(G(), a, b, c, d, X[ 5], 5, 0xd62f105d); // 21 |
| 179 |
operation(G(), d, a, b, c, X[10], 9, 0x2441453); // 22 |
| 180 |
operation(G(), c, d, a, b, X[15], 14, 0xd8a1e681); // 23 |
| 181 |
operation(G(), b, c, d, a, X[ 4], 20, 0xe7d3fbc8); // 24 |
| 182 |
operation(G(), a, b, c, d, X[ 9], 5, 0x21e1cde6); // 25 |
| 183 |
operation(G(), d, a, b, c, X[14], 9, 0xc33707d6); // 26 |
| 184 |
operation(G(), c, d, a, b, X[ 3], 14, 0xf4d50d87); // 27 |
| 185 |
operation(G(), b, c, d, a, X[ 8], 20, 0x455a14ed); // 28 |
| 186 |
operation(G(), a, b, c, d, X[13], 5, 0xa9e3e905); // 29 |
| 187 |
operation(G(), d, a, b, c, X[ 2], 9, 0xfcefa3f8); // 30 |
| 188 |
operation(G(), c, d, a, b, X[ 7], 14, 0x676f02d9); // 31 |
| 189 |
operation(G(), b, c, d, a, X[12], 20, 0x8d2a4c8a); // 32 |
| 190 |
|
| 191 |
// Round 3 |
| 192 |
operation(H(), a, b, c, d, X[ 5], 4, 0xfffa3942); // 33 |
| 193 |
operation(H(), d, a, b, c, X[ 8], 11, 0x8771f681); // 34 |
| 194 |
operation(H(), c, d, a, b, X[11], 16, 0x6d9d6122); // 35 |
| 195 |
operation(H(), b, c, d, a, X[14], 23, 0xfde5380c); // 36 |
| 196 |
operation(H(), a, b, c, d, X[ 1], 4, 0xa4beea44); // 37 |
| 197 |
operation(H(), d, a, b, c, X[ 4], 11, 0x4bdecfa9); // 38 |
| 198 |
operation(H(), c, d, a, b, X[ 7], 16, 0xf6bb4b60); // 39 |
| 199 |
operation(H(), b, c, d, a, X[10], 23, 0xbebfbc70); // 40 |
| 200 |
operation(H(), a, b, c, d, X[13], 4, 0x289b7ec6); // 41 |
| 201 |
operation(H(), d, a, b, c, X[ 0], 11, 0xeaa127fa); // 42 |
| 202 |
operation(H(), c, d, a, b, X[ 3], 16, 0xd4ef3085); // 43 |
| 203 |
operation(H(), b, c, d, a, X[ 6], 23, 0x4881d05); // 44 |
| 204 |
operation(H(), a, b, c, d, X[ 9], 4, 0xd9d4d039); // 45 |
| 205 |
operation(H(), d, a, b, c, X[12], 11, 0xe6db99e5); // 46 |
| 206 |
operation(H(), c, d, a, b, X[15], 16, 0x1fa27cf8); // 47 |
| 207 |
operation(H(), b, c, d, a, X[ 2], 23, 0xc4ac5665); // 48 |
| 208 |
|
| 209 |
// Round 4 |
| 210 |
operation(I(), a, b, c, d, X[ 0], 6, 0xf4292244); // 49 |
| 211 |
operation(I(), d, a, b, c, X[ 7], 10, 0x432aff97); // 50 |
| 212 |
operation(I(), c, d, a, b, X[14], 15, 0xab9423a7); // 51 |
| 213 |
operation(I(), b, c, d, a, X[ 5], 21, 0xfc93a039); // 52 |
| 214 |
operation(I(), a, b, c, d, X[12], 6, 0x655b59c3); // 53 |
| 215 |
operation(I(), d, a, b, c, X[ 3], 10, 0x8f0ccc92); // 54 |
| 216 |
operation(I(), c, d, a, b, X[10], 15, 0xffeff47d); // 55 |
| 217 |
operation(I(), b, c, d, a, X[ 1], 21, 0x85845dd1); // 56 |
| 218 |
operation(I(), a, b, c, d, X[ 8], 6, 0x6fa87e4f); // 57 |
| 219 |
operation(I(), d, a, b, c, X[15], 10, 0xfe2ce6e0); // 58 |
| 220 |
operation(I(), c, d, a, b, X[ 6], 15, 0xa3014314); // 59 |
| 221 |
operation(I(), b, c, d, a, X[13], 21, 0x4e0811a1); // 60 |
| 222 |
operation(I(), a, b, c, d, X[ 4], 6, 0xf7537e82); // 61 |
| 223 |
operation(I(), d, a, b, c, X[11], 10, 0xbd3af235); // 62 |
| 224 |
operation(I(), c, d, a, b, X[ 2], 15, 0x2ad7d2bb); // 63 |
| 225 |
operation(I(), b, c, d, a, X[ 9], 21, 0xeb86d391); // 64 |
| 226 |
|
| 227 |
state[0] += a; |
| 228 |
state[1] += b; |
| 229 |
state[2] += c; |
| 230 |
state[3] += d; |
| 231 |
|
| 232 |
#if defined(SK_MD5_CLEAR_DATA) |
| 233 |
// Clear sensitive information. |
| 234 |
if (X == &storage) { |
| 235 |
memset(storage, 0, sizeof(storage)); |
| 236 |
} |
| 237 |
#endif |
| 238 |
} |
| 239 |
|
| 240 |
static void encode(uint8_t output[16], const uint32_t input[4]) { |
| 241 |
for (size_t i = 0, j = 0; i < 4; i++, j += 4) { |
| 242 |
output[j ] = (uint8_t) (input[i] & 0xff); |
| 243 |
output[j+1] = (uint8_t)((input[i] >> 8) & 0xff); |
| 244 |
output[j+2] = (uint8_t)((input[i] >> 16) & 0xff); |
| 245 |
output[j+3] = (uint8_t)((input[i] >> 24) & 0xff); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
static void encode(uint8_t output[8], uint64_t input) { |
| 250 |
output[0] = (uint8_t) (input & 0xff); |
| 251 |
output[1] = (uint8_t)((input >> 8) & 0xff); |
| 252 |
output[2] = (uint8_t)((input >> 16) & 0xff); |
| 253 |
output[3] = (uint8_t)((input >> 24) & 0xff); |
| 254 |
output[4] = (uint8_t)((input >> 32) & 0xff); |
| 255 |
output[5] = (uint8_t)((input >> 40) & 0xff); |
| 256 |
output[6] = (uint8_t)((input >> 48) & 0xff); |
| 257 |
output[7] = (uint8_t)((input >> 56) & 0xff); |
| 258 |
} |
| 259 |
|
| 260 |
static inline bool is_aligned(const void *pointer, size_t byte_count) { |
| 261 |
return reinterpret_cast<uintptr_t>(pointer) % byte_count == 0; |
| 262 |
} |
| 263 |
|
| 264 |
static const uint32_t* decode(uint32_t storage[16], const uint8_t input[64]) { |
| 265 |
#if defined(SK_CPU_LENDIAN) && defined(SK_CPU_FAST_UNALIGNED_ACCESS) |
| 266 |
return reinterpret_cast<const uint32_t*>(input); |
| 267 |
#else |
| 268 |
#if defined(SK_CPU_LENDIAN) |
| 269 |
if (is_aligned(input, 4)) { |
| 270 |
return reinterpret_cast<const uint32_t*>(input); |
| 271 |
} |
| 272 |
#endif |
| 273 |
for (size_t i = 0, j = 0; j < 64; i++, j += 4) { |
| 274 |
storage[i] = ((uint32_t)input[j ]) | |
| 275 |
(((uint32_t)input[j+1]) << 8) | |
| 276 |
(((uint32_t)input[j+2]) << 16) | |
| 277 |
(((uint32_t)input[j+3]) << 24); |
| 278 |
} |
| 279 |
return storage; |
| 280 |
#endif |
| 281 |
} |