mfbt/SHA1.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include "mozilla/Assertions.h"
michael@0 6 #include "mozilla/Endian.h"
michael@0 7 #include "mozilla/SHA1.h"
michael@0 8
michael@0 9 #include <string.h>
michael@0 10
michael@0 11 using mozilla::NativeEndian;
michael@0 12 using mozilla::SHA1Sum;
michael@0 13
michael@0 14 static inline uint32_t
michael@0 15 SHA_ROTL(uint32_t t, uint32_t n)
michael@0 16 {
michael@0 17 MOZ_ASSERT(n < 32);
michael@0 18 return (t << n) | (t >> (32 - n));
michael@0 19 }
michael@0 20
michael@0 21 static void
michael@0 22 shaCompress(volatile unsigned* X, const uint32_t* datain);
michael@0 23
michael@0 24 #define SHA_F1(X, Y, Z) ((((Y) ^ (Z)) & (X)) ^ (Z))
michael@0 25 #define SHA_F2(X, Y, Z) ((X) ^ (Y) ^ (Z))
michael@0 26 #define SHA_F3(X, Y, Z) (((X) & (Y)) | ((Z) & ((X) | (Y))))
michael@0 27 #define SHA_F4(X, Y, Z) ((X) ^ (Y) ^ (Z))
michael@0 28
michael@0 29 #define SHA_MIX(n, a, b, c) XW(n) = SHA_ROTL(XW(a) ^ XW(b) ^ XW(c) ^XW(n), 1)
michael@0 30
michael@0 31 SHA1Sum::SHA1Sum()
michael@0 32 : size(0), mDone(false)
michael@0 33 {
michael@0 34 // Initialize H with constants from FIPS180-1.
michael@0 35 H[0] = 0x67452301L;
michael@0 36 H[1] = 0xefcdab89L;
michael@0 37 H[2] = 0x98badcfeL;
michael@0 38 H[3] = 0x10325476L;
michael@0 39 H[4] = 0xc3d2e1f0L;
michael@0 40 }
michael@0 41
michael@0 42 /*
michael@0 43 * Explanation of H array and index values:
michael@0 44 *
michael@0 45 * The context's H array is actually the concatenation of two arrays
michael@0 46 * defined by SHA1, the H array of state variables (5 elements),
michael@0 47 * and the W array of intermediate values, of which there are 16 elements.
michael@0 48 * The W array starts at H[5], that is W[0] is H[5].
michael@0 49 * Although these values are defined as 32-bit values, we use 64-bit
michael@0 50 * variables to hold them because the AMD64 stores 64 bit values in
michael@0 51 * memory MUCH faster than it stores any smaller values.
michael@0 52 *
michael@0 53 * Rather than passing the context structure to shaCompress, we pass
michael@0 54 * this combined array of H and W values. We do not pass the address
michael@0 55 * of the first element of this array, but rather pass the address of an
michael@0 56 * element in the middle of the array, element X. Presently X[0] is H[11].
michael@0 57 * So we pass the address of H[11] as the address of array X to shaCompress.
michael@0 58 * Then shaCompress accesses the members of the array using positive AND
michael@0 59 * negative indexes.
michael@0 60 *
michael@0 61 * Pictorially: (each element is 8 bytes)
michael@0 62 * H | H0 H1 H2 H3 H4 W0 W1 W2 W3 W4 W5 W6 W7 W8 W9 Wa Wb Wc Wd We Wf |
michael@0 63 * X |-11-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 |
michael@0 64 *
michael@0 65 * The byte offset from X[0] to any member of H and W is always
michael@0 66 * representable in a signed 8-bit value, which will be encoded
michael@0 67 * as a single byte offset in the X86-64 instruction set.
michael@0 68 * If we didn't pass the address of H[11], and instead passed the
michael@0 69 * address of H[0], the offsets to elements H[16] and above would be
michael@0 70 * greater than 127, not representable in a signed 8-bit value, and the
michael@0 71 * x86-64 instruction set would encode every such offset as a 32-bit
michael@0 72 * signed number in each instruction that accessed element H[16] or
michael@0 73 * higher. This results in much bigger and slower code.
michael@0 74 */
michael@0 75 #define H2X 11 /* X[0] is H[11], and H[0] is X[-11] */
michael@0 76 #define W2X 6 /* X[0] is W[6], and W[0] is X[-6] */
michael@0 77
michael@0 78 /*
michael@0 79 * SHA: Add data to context.
michael@0 80 */
michael@0 81 void
michael@0 82 SHA1Sum::update(const void* dataIn, uint32_t len)
michael@0 83 {
michael@0 84 MOZ_ASSERT(!mDone, "SHA1Sum can only be used to compute a single hash.");
michael@0 85
michael@0 86 const uint8_t* data = static_cast<const uint8_t*>(dataIn);
michael@0 87
michael@0 88 if (len == 0)
michael@0 89 return;
michael@0 90
michael@0 91 /* Accumulate the byte count. */
michael@0 92 unsigned int lenB = static_cast<unsigned int>(size) & 63U;
michael@0 93
michael@0 94 size += len;
michael@0 95
michael@0 96 /* Read the data into W and process blocks as they get full. */
michael@0 97 unsigned int togo;
michael@0 98 if (lenB > 0) {
michael@0 99 togo = 64U - lenB;
michael@0 100 if (len < togo)
michael@0 101 togo = len;
michael@0 102 memcpy(u.b + lenB, data, togo);
michael@0 103 len -= togo;
michael@0 104 data += togo;
michael@0 105 lenB = (lenB + togo) & 63U;
michael@0 106 if (!lenB)
michael@0 107 shaCompress(&H[H2X], u.w);
michael@0 108 }
michael@0 109
michael@0 110 while (len >= 64U) {
michael@0 111 len -= 64U;
michael@0 112 shaCompress(&H[H2X], reinterpret_cast<const uint32_t*>(data));
michael@0 113 data += 64U;
michael@0 114 }
michael@0 115
michael@0 116 if (len > 0)
michael@0 117 memcpy(u.b, data, len);
michael@0 118 }
michael@0 119
michael@0 120
michael@0 121 /*
michael@0 122 * SHA: Generate hash value
michael@0 123 */
michael@0 124 void
michael@0 125 SHA1Sum::finish(SHA1Sum::Hash& hashOut)
michael@0 126 {
michael@0 127 MOZ_ASSERT(!mDone, "SHA1Sum can only be used to compute a single hash.");
michael@0 128
michael@0 129 uint64_t size2 = size;
michael@0 130 uint32_t lenB = uint32_t(size2) & 63;
michael@0 131
michael@0 132 static const uint8_t bulk_pad[64] =
michael@0 133 { 0x80,0,0,0,0,0,0,0,0,0,
michael@0 134 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
michael@0 135 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
michael@0 136
michael@0 137 /* Pad with a binary 1 (e.g. 0x80), then zeroes, then length in bits. */
michael@0 138 update(bulk_pad, (((55 + 64) - lenB) & 63) + 1);
michael@0 139 MOZ_ASSERT((uint32_t(size) & 63) == 56);
michael@0 140
michael@0 141 /* Convert size from bytes to bits. */
michael@0 142 size2 <<= 3;
michael@0 143 u.w[14] = NativeEndian::swapToBigEndian(uint32_t(size2 >> 32));
michael@0 144 u.w[15] = NativeEndian::swapToBigEndian(uint32_t(size2));
michael@0 145 shaCompress(&H[H2X], u.w);
michael@0 146
michael@0 147 /* Output hash. */
michael@0 148 u.w[0] = NativeEndian::swapToBigEndian(H[0]);
michael@0 149 u.w[1] = NativeEndian::swapToBigEndian(H[1]);
michael@0 150 u.w[2] = NativeEndian::swapToBigEndian(H[2]);
michael@0 151 u.w[3] = NativeEndian::swapToBigEndian(H[3]);
michael@0 152 u.w[4] = NativeEndian::swapToBigEndian(H[4]);
michael@0 153 memcpy(hashOut, u.w, 20);
michael@0 154 mDone = true;
michael@0 155 }
michael@0 156
michael@0 157 /*
michael@0 158 * SHA: Compression function, unrolled.
michael@0 159 *
michael@0 160 * Some operations in shaCompress are done as 5 groups of 16 operations.
michael@0 161 * Others are done as 4 groups of 20 operations.
michael@0 162 * The code below shows that structure.
michael@0 163 *
michael@0 164 * The functions that compute the new values of the 5 state variables
michael@0 165 * A-E are done in 4 groups of 20 operations (or you may also think
michael@0 166 * of them as being done in 16 groups of 5 operations). They are
michael@0 167 * done by the SHA_RNDx macros below, in the right column.
michael@0 168 *
michael@0 169 * The functions that set the 16 values of the W array are done in
michael@0 170 * 5 groups of 16 operations. The first group is done by the
michael@0 171 * LOAD macros below, the latter 4 groups are done by SHA_MIX below,
michael@0 172 * in the left column.
michael@0 173 *
michael@0 174 * gcc's optimizer observes that each member of the W array is assigned
michael@0 175 * a value 5 times in this code. It reduces the number of store
michael@0 176 * operations done to the W array in the context (that is, in the X array)
michael@0 177 * by creating a W array on the stack, and storing the W values there for
michael@0 178 * the first 4 groups of operations on W, and storing the values in the
michael@0 179 * context's W array only in the fifth group. This is undesirable.
michael@0 180 * It is MUCH bigger code than simply using the context's W array, because
michael@0 181 * all the offsets to the W array in the stack are 32-bit signed offsets,
michael@0 182 * and it is no faster than storing the values in the context's W array.
michael@0 183 *
michael@0 184 * The original code for sha_fast.c prevented this creation of a separate
michael@0 185 * W array in the stack by creating a W array of 80 members, each of
michael@0 186 * whose elements is assigned only once. It also separated the computations
michael@0 187 * of the W array values and the computations of the values for the 5
michael@0 188 * state variables into two separate passes, W's, then A-E's so that the
michael@0 189 * second pass could be done all in registers (except for accessing the W
michael@0 190 * array) on machines with fewer registers. The method is suboptimal
michael@0 191 * for machines with enough registers to do it all in one pass, and it
michael@0 192 * necessitates using many instructions with 32-bit offsets.
michael@0 193 *
michael@0 194 * This code eliminates the separate W array on the stack by a completely
michael@0 195 * different means: by declaring the X array volatile. This prevents
michael@0 196 * the optimizer from trying to reduce the use of the X array by the
michael@0 197 * creation of a MORE expensive W array on the stack. The result is
michael@0 198 * that all instructions use signed 8-bit offsets and not 32-bit offsets.
michael@0 199 *
michael@0 200 * The combination of this code and the -O3 optimizer flag on GCC 3.4.3
michael@0 201 * results in code that is 3 times faster than the previous NSS sha_fast
michael@0 202 * code on AMD64.
michael@0 203 */
michael@0 204 static void
michael@0 205 shaCompress(volatile unsigned *X, const uint32_t *inbuf)
michael@0 206 {
michael@0 207 unsigned A, B, C, D, E;
michael@0 208
michael@0 209 #define XH(n) X[n - H2X]
michael@0 210 #define XW(n) X[n - W2X]
michael@0 211
michael@0 212 #define K0 0x5a827999L
michael@0 213 #define K1 0x6ed9eba1L
michael@0 214 #define K2 0x8f1bbcdcL
michael@0 215 #define K3 0xca62c1d6L
michael@0 216
michael@0 217 #define SHA_RND1(a, b, c, d, e, n) \
michael@0 218 a = SHA_ROTL(b, 5) + SHA_F1(c, d, e) + a + XW(n) + K0; c = SHA_ROTL(c, 30)
michael@0 219 #define SHA_RND2(a, b, c, d, e, n) \
michael@0 220 a = SHA_ROTL(b, 5) + SHA_F2(c, d, e) + a + XW(n) + K1; c = SHA_ROTL(c, 30)
michael@0 221 #define SHA_RND3(a, b, c, d, e, n) \
michael@0 222 a = SHA_ROTL(b, 5) + SHA_F3(c, d, e) + a + XW(n) + K2; c = SHA_ROTL(c, 30)
michael@0 223 #define SHA_RND4(a, b, c, d, e, n) \
michael@0 224 a = SHA_ROTL(b ,5) + SHA_F4(c, d, e) + a + XW(n) + K3; c = SHA_ROTL(c, 30)
michael@0 225
michael@0 226 #define LOAD(n) XW(n) = NativeEndian::swapToBigEndian(inbuf[n])
michael@0 227
michael@0 228 A = XH(0);
michael@0 229 B = XH(1);
michael@0 230 C = XH(2);
michael@0 231 D = XH(3);
michael@0 232 E = XH(4);
michael@0 233
michael@0 234 LOAD(0); SHA_RND1(E,A,B,C,D, 0);
michael@0 235 LOAD(1); SHA_RND1(D,E,A,B,C, 1);
michael@0 236 LOAD(2); SHA_RND1(C,D,E,A,B, 2);
michael@0 237 LOAD(3); SHA_RND1(B,C,D,E,A, 3);
michael@0 238 LOAD(4); SHA_RND1(A,B,C,D,E, 4);
michael@0 239 LOAD(5); SHA_RND1(E,A,B,C,D, 5);
michael@0 240 LOAD(6); SHA_RND1(D,E,A,B,C, 6);
michael@0 241 LOAD(7); SHA_RND1(C,D,E,A,B, 7);
michael@0 242 LOAD(8); SHA_RND1(B,C,D,E,A, 8);
michael@0 243 LOAD(9); SHA_RND1(A,B,C,D,E, 9);
michael@0 244 LOAD(10); SHA_RND1(E,A,B,C,D,10);
michael@0 245 LOAD(11); SHA_RND1(D,E,A,B,C,11);
michael@0 246 LOAD(12); SHA_RND1(C,D,E,A,B,12);
michael@0 247 LOAD(13); SHA_RND1(B,C,D,E,A,13);
michael@0 248 LOAD(14); SHA_RND1(A,B,C,D,E,14);
michael@0 249 LOAD(15); SHA_RND1(E,A,B,C,D,15);
michael@0 250
michael@0 251 SHA_MIX( 0, 13, 8, 2); SHA_RND1(D,E,A,B,C, 0);
michael@0 252 SHA_MIX( 1, 14, 9, 3); SHA_RND1(C,D,E,A,B, 1);
michael@0 253 SHA_MIX( 2, 15, 10, 4); SHA_RND1(B,C,D,E,A, 2);
michael@0 254 SHA_MIX( 3, 0, 11, 5); SHA_RND1(A,B,C,D,E, 3);
michael@0 255
michael@0 256 SHA_MIX( 4, 1, 12, 6); SHA_RND2(E,A,B,C,D, 4);
michael@0 257 SHA_MIX( 5, 2, 13, 7); SHA_RND2(D,E,A,B,C, 5);
michael@0 258 SHA_MIX( 6, 3, 14, 8); SHA_RND2(C,D,E,A,B, 6);
michael@0 259 SHA_MIX( 7, 4, 15, 9); SHA_RND2(B,C,D,E,A, 7);
michael@0 260 SHA_MIX( 8, 5, 0, 10); SHA_RND2(A,B,C,D,E, 8);
michael@0 261 SHA_MIX( 9, 6, 1, 11); SHA_RND2(E,A,B,C,D, 9);
michael@0 262 SHA_MIX(10, 7, 2, 12); SHA_RND2(D,E,A,B,C,10);
michael@0 263 SHA_MIX(11, 8, 3, 13); SHA_RND2(C,D,E,A,B,11);
michael@0 264 SHA_MIX(12, 9, 4, 14); SHA_RND2(B,C,D,E,A,12);
michael@0 265 SHA_MIX(13, 10, 5, 15); SHA_RND2(A,B,C,D,E,13);
michael@0 266 SHA_MIX(14, 11, 6, 0); SHA_RND2(E,A,B,C,D,14);
michael@0 267 SHA_MIX(15, 12, 7, 1); SHA_RND2(D,E,A,B,C,15);
michael@0 268
michael@0 269 SHA_MIX( 0, 13, 8, 2); SHA_RND2(C,D,E,A,B, 0);
michael@0 270 SHA_MIX( 1, 14, 9, 3); SHA_RND2(B,C,D,E,A, 1);
michael@0 271 SHA_MIX( 2, 15, 10, 4); SHA_RND2(A,B,C,D,E, 2);
michael@0 272 SHA_MIX( 3, 0, 11, 5); SHA_RND2(E,A,B,C,D, 3);
michael@0 273 SHA_MIX( 4, 1, 12, 6); SHA_RND2(D,E,A,B,C, 4);
michael@0 274 SHA_MIX( 5, 2, 13, 7); SHA_RND2(C,D,E,A,B, 5);
michael@0 275 SHA_MIX( 6, 3, 14, 8); SHA_RND2(B,C,D,E,A, 6);
michael@0 276 SHA_MIX( 7, 4, 15, 9); SHA_RND2(A,B,C,D,E, 7);
michael@0 277
michael@0 278 SHA_MIX( 8, 5, 0, 10); SHA_RND3(E,A,B,C,D, 8);
michael@0 279 SHA_MIX( 9, 6, 1, 11); SHA_RND3(D,E,A,B,C, 9);
michael@0 280 SHA_MIX(10, 7, 2, 12); SHA_RND3(C,D,E,A,B,10);
michael@0 281 SHA_MIX(11, 8, 3, 13); SHA_RND3(B,C,D,E,A,11);
michael@0 282 SHA_MIX(12, 9, 4, 14); SHA_RND3(A,B,C,D,E,12);
michael@0 283 SHA_MIX(13, 10, 5, 15); SHA_RND3(E,A,B,C,D,13);
michael@0 284 SHA_MIX(14, 11, 6, 0); SHA_RND3(D,E,A,B,C,14);
michael@0 285 SHA_MIX(15, 12, 7, 1); SHA_RND3(C,D,E,A,B,15);
michael@0 286
michael@0 287 SHA_MIX( 0, 13, 8, 2); SHA_RND3(B,C,D,E,A, 0);
michael@0 288 SHA_MIX( 1, 14, 9, 3); SHA_RND3(A,B,C,D,E, 1);
michael@0 289 SHA_MIX( 2, 15, 10, 4); SHA_RND3(E,A,B,C,D, 2);
michael@0 290 SHA_MIX( 3, 0, 11, 5); SHA_RND3(D,E,A,B,C, 3);
michael@0 291 SHA_MIX( 4, 1, 12, 6); SHA_RND3(C,D,E,A,B, 4);
michael@0 292 SHA_MIX( 5, 2, 13, 7); SHA_RND3(B,C,D,E,A, 5);
michael@0 293 SHA_MIX( 6, 3, 14, 8); SHA_RND3(A,B,C,D,E, 6);
michael@0 294 SHA_MIX( 7, 4, 15, 9); SHA_RND3(E,A,B,C,D, 7);
michael@0 295 SHA_MIX( 8, 5, 0, 10); SHA_RND3(D,E,A,B,C, 8);
michael@0 296 SHA_MIX( 9, 6, 1, 11); SHA_RND3(C,D,E,A,B, 9);
michael@0 297 SHA_MIX(10, 7, 2, 12); SHA_RND3(B,C,D,E,A,10);
michael@0 298 SHA_MIX(11, 8, 3, 13); SHA_RND3(A,B,C,D,E,11);
michael@0 299
michael@0 300 SHA_MIX(12, 9, 4, 14); SHA_RND4(E,A,B,C,D,12);
michael@0 301 SHA_MIX(13, 10, 5, 15); SHA_RND4(D,E,A,B,C,13);
michael@0 302 SHA_MIX(14, 11, 6, 0); SHA_RND4(C,D,E,A,B,14);
michael@0 303 SHA_MIX(15, 12, 7, 1); SHA_RND4(B,C,D,E,A,15);
michael@0 304
michael@0 305 SHA_MIX( 0, 13, 8, 2); SHA_RND4(A,B,C,D,E, 0);
michael@0 306 SHA_MIX( 1, 14, 9, 3); SHA_RND4(E,A,B,C,D, 1);
michael@0 307 SHA_MIX( 2, 15, 10, 4); SHA_RND4(D,E,A,B,C, 2);
michael@0 308 SHA_MIX( 3, 0, 11, 5); SHA_RND4(C,D,E,A,B, 3);
michael@0 309 SHA_MIX( 4, 1, 12, 6); SHA_RND4(B,C,D,E,A, 4);
michael@0 310 SHA_MIX( 5, 2, 13, 7); SHA_RND4(A,B,C,D,E, 5);
michael@0 311 SHA_MIX( 6, 3, 14, 8); SHA_RND4(E,A,B,C,D, 6);
michael@0 312 SHA_MIX( 7, 4, 15, 9); SHA_RND4(D,E,A,B,C, 7);
michael@0 313 SHA_MIX( 8, 5, 0, 10); SHA_RND4(C,D,E,A,B, 8);
michael@0 314 SHA_MIX( 9, 6, 1, 11); SHA_RND4(B,C,D,E,A, 9);
michael@0 315 SHA_MIX(10, 7, 2, 12); SHA_RND4(A,B,C,D,E,10);
michael@0 316 SHA_MIX(11, 8, 3, 13); SHA_RND4(E,A,B,C,D,11);
michael@0 317 SHA_MIX(12, 9, 4, 14); SHA_RND4(D,E,A,B,C,12);
michael@0 318 SHA_MIX(13, 10, 5, 15); SHA_RND4(C,D,E,A,B,13);
michael@0 319 SHA_MIX(14, 11, 6, 0); SHA_RND4(B,C,D,E,A,14);
michael@0 320 SHA_MIX(15, 12, 7, 1); SHA_RND4(A,B,C,D,E,15);
michael@0 321
michael@0 322 XH(0) += A;
michael@0 323 XH(1) += B;
michael@0 324 XH(2) += C;
michael@0 325 XH(3) += D;
michael@0 326 XH(4) += E;
michael@0 327 }

mercurial