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