gfx/skia/trunk/src/utils/SkMD5.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial