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