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