Sat, 03 Jan 2015 20:18:00 +0100
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 2013 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 3174. |
michael@0 | 8 | * http://www.ietf.org/rfc/rfc3174.txt |
michael@0 | 9 | */ |
michael@0 | 10 | |
michael@0 | 11 | #include "SkTypes.h" |
michael@0 | 12 | #include "SkSHA1.h" |
michael@0 | 13 | #include <string.h> |
michael@0 | 14 | |
michael@0 | 15 | /** SHA1 basic transformation. Transforms state based on block. */ |
michael@0 | 16 | static void transform(uint32_t state[5], const uint8_t block[64]); |
michael@0 | 17 | |
michael@0 | 18 | /** Encodes input into output (5 big endian 32 bit values). */ |
michael@0 | 19 | static void encode(uint8_t output[20], const uint32_t input[5]); |
michael@0 | 20 | |
michael@0 | 21 | /** Encodes input into output (big endian 64 bit value). */ |
michael@0 | 22 | static void encode(uint8_t output[8], const uint64_t input); |
michael@0 | 23 | |
michael@0 | 24 | SkSHA1::SkSHA1() : byteCount(0) { |
michael@0 | 25 | // These are magic numbers from the specification. The first four are the same as MD5. |
michael@0 | 26 | this->state[0] = 0x67452301; |
michael@0 | 27 | this->state[1] = 0xefcdab89; |
michael@0 | 28 | this->state[2] = 0x98badcfe; |
michael@0 | 29 | this->state[3] = 0x10325476; |
michael@0 | 30 | this->state[4] = 0xc3d2e1f0; |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | void SkSHA1::update(const uint8_t* input, size_t inputLength) { |
michael@0 | 34 | unsigned int bufferIndex = (unsigned int)(this->byteCount & 0x3F); |
michael@0 | 35 | unsigned int bufferAvailable = 64 - bufferIndex; |
michael@0 | 36 | |
michael@0 | 37 | unsigned int inputIndex; |
michael@0 | 38 | if (inputLength >= bufferAvailable) { |
michael@0 | 39 | if (bufferIndex) { |
michael@0 | 40 | memcpy(&this->buffer[bufferIndex], input, bufferAvailable); |
michael@0 | 41 | transform(this->state, this->buffer); |
michael@0 | 42 | inputIndex = bufferAvailable; |
michael@0 | 43 | } else { |
michael@0 | 44 | inputIndex = 0; |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | for (; inputIndex + 63 < inputLength; inputIndex += 64) { |
michael@0 | 48 | transform(this->state, &input[inputIndex]); |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | bufferIndex = 0; |
michael@0 | 52 | } else { |
michael@0 | 53 | inputIndex = 0; |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | memcpy(&this->buffer[bufferIndex], &input[inputIndex], inputLength - inputIndex); |
michael@0 | 57 | |
michael@0 | 58 | this->byteCount += inputLength; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | void SkSHA1::finish(Digest& digest) { |
michael@0 | 62 | // Get the number of bits before padding. |
michael@0 | 63 | uint8_t bits[8]; |
michael@0 | 64 | encode(bits, this->byteCount << 3); |
michael@0 | 65 | |
michael@0 | 66 | // Pad out to 56 mod 64. |
michael@0 | 67 | unsigned int bufferIndex = (unsigned int)(this->byteCount & 0x3F); |
michael@0 | 68 | unsigned int paddingLength = (bufferIndex < 56) ? (56 - bufferIndex) : (120 - bufferIndex); |
michael@0 | 69 | static uint8_t PADDING[64] = { |
michael@0 | 70 | 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
michael@0 | 71 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
michael@0 | 72 | 0, 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 | }; |
michael@0 | 75 | this->update(PADDING, paddingLength); |
michael@0 | 76 | |
michael@0 | 77 | // Append length (length before padding, will cause final update). |
michael@0 | 78 | this->update(bits, 8); |
michael@0 | 79 | |
michael@0 | 80 | // Write out digest. |
michael@0 | 81 | encode(digest.data, this->state); |
michael@0 | 82 | |
michael@0 | 83 | #if defined(SK_SHA1_CLEAR_DATA) |
michael@0 | 84 | // Clear state. |
michael@0 | 85 | memset(this, 0, sizeof(*this)); |
michael@0 | 86 | #endif |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | struct F1 { uint32_t operator()(uint32_t B, uint32_t C, uint32_t D) { |
michael@0 | 90 | return (B & C) | ((~B) & D); |
michael@0 | 91 | //return D ^ (B & (C ^ D)); |
michael@0 | 92 | //return (B & C) ^ ((~B) & D); |
michael@0 | 93 | //return (B & C) + ((~B) & D); |
michael@0 | 94 | //return _mm_or_ps(_mm_andnot_ps(B, D), _mm_and_ps(B, C)); //SSE2 |
michael@0 | 95 | //return vec_sel(D, C, B); //PPC |
michael@0 | 96 | }}; |
michael@0 | 97 | |
michael@0 | 98 | struct F2 { uint32_t operator()(uint32_t B, uint32_t C, uint32_t D) { |
michael@0 | 99 | return B ^ C ^ D; |
michael@0 | 100 | }}; |
michael@0 | 101 | |
michael@0 | 102 | struct F3 { uint32_t operator()(uint32_t B, uint32_t C, uint32_t D) { |
michael@0 | 103 | return (B & C) | (B & D) | (C & D); |
michael@0 | 104 | //return (B & C) | (D & (B | C)); |
michael@0 | 105 | //return (B & C) | (D & (B ^ C)); |
michael@0 | 106 | //return (B & C) + (D & (B ^ C)); |
michael@0 | 107 | //return (B & C) ^ (B & D) ^ (C & D); |
michael@0 | 108 | }}; |
michael@0 | 109 | |
michael@0 | 110 | /** Rotates x left n bits. */ |
michael@0 | 111 | static inline uint32_t rotate_left(uint32_t x, uint8_t n) { |
michael@0 | 112 | return (x << n) | (x >> (32 - n)); |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | template <typename T> |
michael@0 | 116 | static inline void operation(T operation, |
michael@0 | 117 | uint32_t A, uint32_t& B, uint32_t C, uint32_t D, uint32_t& E, |
michael@0 | 118 | uint32_t w, uint32_t k) { |
michael@0 | 119 | E += rotate_left(A, 5) + operation(B, C, D) + w + k; |
michael@0 | 120 | B = rotate_left(B, 30); |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | static void transform(uint32_t state[5], const uint8_t block[64]) { |
michael@0 | 124 | uint32_t A = state[0], B = state[1], C = state[2], D = state[3], E = state[4]; |
michael@0 | 125 | |
michael@0 | 126 | // Round constants defined in SHA-1. |
michael@0 | 127 | static const uint32_t K[] = { |
michael@0 | 128 | 0x5A827999, //sqrt(2) * 2^30 |
michael@0 | 129 | 0x6ED9EBA1, //sqrt(3) * 2^30 |
michael@0 | 130 | 0x8F1BBCDC, //sqrt(5) * 2^30 |
michael@0 | 131 | 0xCA62C1D6, //sqrt(10) * 2^30 |
michael@0 | 132 | }; |
michael@0 | 133 | |
michael@0 | 134 | uint32_t W[80]; |
michael@0 | 135 | |
michael@0 | 136 | // Initialize the array W. |
michael@0 | 137 | size_t i = 0; |
michael@0 | 138 | for (size_t j = 0; i < 16; ++i, j += 4) { |
michael@0 | 139 | W[i] = (((uint32_t)block[j ]) << 24) | |
michael@0 | 140 | (((uint32_t)block[j+1]) << 16) | |
michael@0 | 141 | (((uint32_t)block[j+2]) << 8) | |
michael@0 | 142 | (((uint32_t)block[j+3]) ); |
michael@0 | 143 | } |
michael@0 | 144 | for (; i < 80; ++i) { |
michael@0 | 145 | W[i] = rotate_left(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1); |
michael@0 | 146 | //The following is equivelent and speeds up SSE implementations, but slows non-SSE. |
michael@0 | 147 | //W[i] = rotate_left(W[i-6] ^ W[i-16] ^ W[i-28] ^ W[i-32], 2); |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | // Round 1 |
michael@0 | 151 | operation(F1(), A, B, C, D, E, W[ 0], K[0]); |
michael@0 | 152 | operation(F1(), E, A, B, C, D, W[ 1], K[0]); |
michael@0 | 153 | operation(F1(), D, E, A, B, C, W[ 2], K[0]); |
michael@0 | 154 | operation(F1(), C, D, E, A, B, W[ 3], K[0]); |
michael@0 | 155 | operation(F1(), B, C, D, E, A, W[ 4], K[0]); |
michael@0 | 156 | operation(F1(), A, B, C, D, E, W[ 5], K[0]); |
michael@0 | 157 | operation(F1(), E, A, B, C, D, W[ 6], K[0]); |
michael@0 | 158 | operation(F1(), D, E, A, B, C, W[ 7], K[0]); |
michael@0 | 159 | operation(F1(), C, D, E, A, B, W[ 8], K[0]); |
michael@0 | 160 | operation(F1(), B, C, D, E, A, W[ 9], K[0]); |
michael@0 | 161 | operation(F1(), A, B, C, D, E, W[10], K[0]); |
michael@0 | 162 | operation(F1(), E, A, B, C, D, W[11], K[0]); |
michael@0 | 163 | operation(F1(), D, E, A, B, C, W[12], K[0]); |
michael@0 | 164 | operation(F1(), C, D, E, A, B, W[13], K[0]); |
michael@0 | 165 | operation(F1(), B, C, D, E, A, W[14], K[0]); |
michael@0 | 166 | operation(F1(), A, B, C, D, E, W[15], K[0]); |
michael@0 | 167 | operation(F1(), E, A, B, C, D, W[16], K[0]); |
michael@0 | 168 | operation(F1(), D, E, A, B, C, W[17], K[0]); |
michael@0 | 169 | operation(F1(), C, D, E, A, B, W[18], K[0]); |
michael@0 | 170 | operation(F1(), B, C, D, E, A, W[19], K[0]); |
michael@0 | 171 | |
michael@0 | 172 | // Round 2 |
michael@0 | 173 | operation(F2(), A, B, C, D, E, W[20], K[1]); |
michael@0 | 174 | operation(F2(), E, A, B, C, D, W[21], K[1]); |
michael@0 | 175 | operation(F2(), D, E, A, B, C, W[22], K[1]); |
michael@0 | 176 | operation(F2(), C, D, E, A, B, W[23], K[1]); |
michael@0 | 177 | operation(F2(), B, C, D, E, A, W[24], K[1]); |
michael@0 | 178 | operation(F2(), A, B, C, D, E, W[25], K[1]); |
michael@0 | 179 | operation(F2(), E, A, B, C, D, W[26], K[1]); |
michael@0 | 180 | operation(F2(), D, E, A, B, C, W[27], K[1]); |
michael@0 | 181 | operation(F2(), C, D, E, A, B, W[28], K[1]); |
michael@0 | 182 | operation(F2(), B, C, D, E, A, W[29], K[1]); |
michael@0 | 183 | operation(F2(), A, B, C, D, E, W[30], K[1]); |
michael@0 | 184 | operation(F2(), E, A, B, C, D, W[31], K[1]); |
michael@0 | 185 | operation(F2(), D, E, A, B, C, W[32], K[1]); |
michael@0 | 186 | operation(F2(), C, D, E, A, B, W[33], K[1]); |
michael@0 | 187 | operation(F2(), B, C, D, E, A, W[34], K[1]); |
michael@0 | 188 | operation(F2(), A, B, C, D, E, W[35], K[1]); |
michael@0 | 189 | operation(F2(), E, A, B, C, D, W[36], K[1]); |
michael@0 | 190 | operation(F2(), D, E, A, B, C, W[37], K[1]); |
michael@0 | 191 | operation(F2(), C, D, E, A, B, W[38], K[1]); |
michael@0 | 192 | operation(F2(), B, C, D, E, A, W[39], K[1]); |
michael@0 | 193 | |
michael@0 | 194 | // Round 3 |
michael@0 | 195 | operation(F3(), A, B, C, D, E, W[40], K[2]); |
michael@0 | 196 | operation(F3(), E, A, B, C, D, W[41], K[2]); |
michael@0 | 197 | operation(F3(), D, E, A, B, C, W[42], K[2]); |
michael@0 | 198 | operation(F3(), C, D, E, A, B, W[43], K[2]); |
michael@0 | 199 | operation(F3(), B, C, D, E, A, W[44], K[2]); |
michael@0 | 200 | operation(F3(), A, B, C, D, E, W[45], K[2]); |
michael@0 | 201 | operation(F3(), E, A, B, C, D, W[46], K[2]); |
michael@0 | 202 | operation(F3(), D, E, A, B, C, W[47], K[2]); |
michael@0 | 203 | operation(F3(), C, D, E, A, B, W[48], K[2]); |
michael@0 | 204 | operation(F3(), B, C, D, E, A, W[49], K[2]); |
michael@0 | 205 | operation(F3(), A, B, C, D, E, W[50], K[2]); |
michael@0 | 206 | operation(F3(), E, A, B, C, D, W[51], K[2]); |
michael@0 | 207 | operation(F3(), D, E, A, B, C, W[52], K[2]); |
michael@0 | 208 | operation(F3(), C, D, E, A, B, W[53], K[2]); |
michael@0 | 209 | operation(F3(), B, C, D, E, A, W[54], K[2]); |
michael@0 | 210 | operation(F3(), A, B, C, D, E, W[55], K[2]); |
michael@0 | 211 | operation(F3(), E, A, B, C, D, W[56], K[2]); |
michael@0 | 212 | operation(F3(), D, E, A, B, C, W[57], K[2]); |
michael@0 | 213 | operation(F3(), C, D, E, A, B, W[58], K[2]); |
michael@0 | 214 | operation(F3(), B, C, D, E, A, W[59], K[2]); |
michael@0 | 215 | |
michael@0 | 216 | // Round 4 |
michael@0 | 217 | operation(F2(), A, B, C, D, E, W[60], K[3]); |
michael@0 | 218 | operation(F2(), E, A, B, C, D, W[61], K[3]); |
michael@0 | 219 | operation(F2(), D, E, A, B, C, W[62], K[3]); |
michael@0 | 220 | operation(F2(), C, D, E, A, B, W[63], K[3]); |
michael@0 | 221 | operation(F2(), B, C, D, E, A, W[64], K[3]); |
michael@0 | 222 | operation(F2(), A, B, C, D, E, W[65], K[3]); |
michael@0 | 223 | operation(F2(), E, A, B, C, D, W[66], K[3]); |
michael@0 | 224 | operation(F2(), D, E, A, B, C, W[67], K[3]); |
michael@0 | 225 | operation(F2(), C, D, E, A, B, W[68], K[3]); |
michael@0 | 226 | operation(F2(), B, C, D, E, A, W[69], K[3]); |
michael@0 | 227 | operation(F2(), A, B, C, D, E, W[70], K[3]); |
michael@0 | 228 | operation(F2(), E, A, B, C, D, W[71], K[3]); |
michael@0 | 229 | operation(F2(), D, E, A, B, C, W[72], K[3]); |
michael@0 | 230 | operation(F2(), C, D, E, A, B, W[73], K[3]); |
michael@0 | 231 | operation(F2(), B, C, D, E, A, W[74], K[3]); |
michael@0 | 232 | operation(F2(), A, B, C, D, E, W[75], K[3]); |
michael@0 | 233 | operation(F2(), E, A, B, C, D, W[76], K[3]); |
michael@0 | 234 | operation(F2(), D, E, A, B, C, W[77], K[3]); |
michael@0 | 235 | operation(F2(), C, D, E, A, B, W[78], K[3]); |
michael@0 | 236 | operation(F2(), B, C, D, E, A, W[79], K[3]); |
michael@0 | 237 | |
michael@0 | 238 | state[0] += A; |
michael@0 | 239 | state[1] += B; |
michael@0 | 240 | state[2] += C; |
michael@0 | 241 | state[3] += D; |
michael@0 | 242 | state[4] += E; |
michael@0 | 243 | |
michael@0 | 244 | #if defined(SK_SHA1_CLEAR_DATA) |
michael@0 | 245 | // Clear sensitive information. |
michael@0 | 246 | memset(W, 0, sizeof(W)); |
michael@0 | 247 | #endif |
michael@0 | 248 | } |
michael@0 | 249 | |
michael@0 | 250 | static void encode(uint8_t output[20], const uint32_t input[5]) { |
michael@0 | 251 | for (size_t i = 0, j = 0; i < 5; i++, j += 4) { |
michael@0 | 252 | output[j ] = (uint8_t)((input[i] >> 24) & 0xff); |
michael@0 | 253 | output[j+1] = (uint8_t)((input[i] >> 16) & 0xff); |
michael@0 | 254 | output[j+2] = (uint8_t)((input[i] >> 8) & 0xff); |
michael@0 | 255 | output[j+3] = (uint8_t)((input[i] ) & 0xff); |
michael@0 | 256 | } |
michael@0 | 257 | } |
michael@0 | 258 | |
michael@0 | 259 | static void encode(uint8_t output[8], const uint64_t input) { |
michael@0 | 260 | output[0] = (uint8_t)((input >> 56) & 0xff); |
michael@0 | 261 | output[1] = (uint8_t)((input >> 48) & 0xff); |
michael@0 | 262 | output[2] = (uint8_t)((input >> 40) & 0xff); |
michael@0 | 263 | output[3] = (uint8_t)((input >> 32) & 0xff); |
michael@0 | 264 | output[4] = (uint8_t)((input >> 24) & 0xff); |
michael@0 | 265 | output[5] = (uint8_t)((input >> 16) & 0xff); |
michael@0 | 266 | output[6] = (uint8_t)((input >> 8) & 0xff); |
michael@0 | 267 | output[7] = (uint8_t)((input ) & 0xff); |
michael@0 | 268 | } |