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