1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mfbt/SHA1.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,327 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "mozilla/Assertions.h" 1.9 +#include "mozilla/Endian.h" 1.10 +#include "mozilla/SHA1.h" 1.11 + 1.12 +#include <string.h> 1.13 + 1.14 +using mozilla::NativeEndian; 1.15 +using mozilla::SHA1Sum; 1.16 + 1.17 +static inline uint32_t 1.18 +SHA_ROTL(uint32_t t, uint32_t n) 1.19 +{ 1.20 + MOZ_ASSERT(n < 32); 1.21 + return (t << n) | (t >> (32 - n)); 1.22 +} 1.23 + 1.24 +static void 1.25 +shaCompress(volatile unsigned* X, const uint32_t* datain); 1.26 + 1.27 +#define SHA_F1(X, Y, Z) ((((Y) ^ (Z)) & (X)) ^ (Z)) 1.28 +#define SHA_F2(X, Y, Z) ((X) ^ (Y) ^ (Z)) 1.29 +#define SHA_F3(X, Y, Z) (((X) & (Y)) | ((Z) & ((X) | (Y)))) 1.30 +#define SHA_F4(X, Y, Z) ((X) ^ (Y) ^ (Z)) 1.31 + 1.32 +#define SHA_MIX(n, a, b, c) XW(n) = SHA_ROTL(XW(a) ^ XW(b) ^ XW(c) ^XW(n), 1) 1.33 + 1.34 +SHA1Sum::SHA1Sum() 1.35 + : size(0), mDone(false) 1.36 +{ 1.37 + // Initialize H with constants from FIPS180-1. 1.38 + H[0] = 0x67452301L; 1.39 + H[1] = 0xefcdab89L; 1.40 + H[2] = 0x98badcfeL; 1.41 + H[3] = 0x10325476L; 1.42 + H[4] = 0xc3d2e1f0L; 1.43 +} 1.44 + 1.45 +/* 1.46 + * Explanation of H array and index values: 1.47 + * 1.48 + * The context's H array is actually the concatenation of two arrays 1.49 + * defined by SHA1, the H array of state variables (5 elements), 1.50 + * and the W array of intermediate values, of which there are 16 elements. 1.51 + * The W array starts at H[5], that is W[0] is H[5]. 1.52 + * Although these values are defined as 32-bit values, we use 64-bit 1.53 + * variables to hold them because the AMD64 stores 64 bit values in 1.54 + * memory MUCH faster than it stores any smaller values. 1.55 + * 1.56 + * Rather than passing the context structure to shaCompress, we pass 1.57 + * this combined array of H and W values. We do not pass the address 1.58 + * of the first element of this array, but rather pass the address of an 1.59 + * element in the middle of the array, element X. Presently X[0] is H[11]. 1.60 + * So we pass the address of H[11] as the address of array X to shaCompress. 1.61 + * Then shaCompress accesses the members of the array using positive AND 1.62 + * negative indexes. 1.63 + * 1.64 + * Pictorially: (each element is 8 bytes) 1.65 + * H | H0 H1 H2 H3 H4 W0 W1 W2 W3 W4 W5 W6 W7 W8 W9 Wa Wb Wc Wd We Wf | 1.66 + * X |-11-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 | 1.67 + * 1.68 + * The byte offset from X[0] to any member of H and W is always 1.69 + * representable in a signed 8-bit value, which will be encoded 1.70 + * as a single byte offset in the X86-64 instruction set. 1.71 + * If we didn't pass the address of H[11], and instead passed the 1.72 + * address of H[0], the offsets to elements H[16] and above would be 1.73 + * greater than 127, not representable in a signed 8-bit value, and the 1.74 + * x86-64 instruction set would encode every such offset as a 32-bit 1.75 + * signed number in each instruction that accessed element H[16] or 1.76 + * higher. This results in much bigger and slower code. 1.77 + */ 1.78 +#define H2X 11 /* X[0] is H[11], and H[0] is X[-11] */ 1.79 +#define W2X 6 /* X[0] is W[6], and W[0] is X[-6] */ 1.80 + 1.81 +/* 1.82 + * SHA: Add data to context. 1.83 + */ 1.84 +void 1.85 +SHA1Sum::update(const void* dataIn, uint32_t len) 1.86 +{ 1.87 + MOZ_ASSERT(!mDone, "SHA1Sum can only be used to compute a single hash."); 1.88 + 1.89 + const uint8_t* data = static_cast<const uint8_t*>(dataIn); 1.90 + 1.91 + if (len == 0) 1.92 + return; 1.93 + 1.94 + /* Accumulate the byte count. */ 1.95 + unsigned int lenB = static_cast<unsigned int>(size) & 63U; 1.96 + 1.97 + size += len; 1.98 + 1.99 + /* Read the data into W and process blocks as they get full. */ 1.100 + unsigned int togo; 1.101 + if (lenB > 0) { 1.102 + togo = 64U - lenB; 1.103 + if (len < togo) 1.104 + togo = len; 1.105 + memcpy(u.b + lenB, data, togo); 1.106 + len -= togo; 1.107 + data += togo; 1.108 + lenB = (lenB + togo) & 63U; 1.109 + if (!lenB) 1.110 + shaCompress(&H[H2X], u.w); 1.111 + } 1.112 + 1.113 + while (len >= 64U) { 1.114 + len -= 64U; 1.115 + shaCompress(&H[H2X], reinterpret_cast<const uint32_t*>(data)); 1.116 + data += 64U; 1.117 + } 1.118 + 1.119 + if (len > 0) 1.120 + memcpy(u.b, data, len); 1.121 +} 1.122 + 1.123 + 1.124 +/* 1.125 + * SHA: Generate hash value 1.126 + */ 1.127 +void 1.128 +SHA1Sum::finish(SHA1Sum::Hash& hashOut) 1.129 +{ 1.130 + MOZ_ASSERT(!mDone, "SHA1Sum can only be used to compute a single hash."); 1.131 + 1.132 + uint64_t size2 = size; 1.133 + uint32_t lenB = uint32_t(size2) & 63; 1.134 + 1.135 + static const uint8_t bulk_pad[64] = 1.136 + { 0x80,0,0,0,0,0,0,0,0,0, 1.137 + 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, 1.138 + 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 }; 1.139 + 1.140 + /* Pad with a binary 1 (e.g. 0x80), then zeroes, then length in bits. */ 1.141 + update(bulk_pad, (((55 + 64) - lenB) & 63) + 1); 1.142 + MOZ_ASSERT((uint32_t(size) & 63) == 56); 1.143 + 1.144 + /* Convert size from bytes to bits. */ 1.145 + size2 <<= 3; 1.146 + u.w[14] = NativeEndian::swapToBigEndian(uint32_t(size2 >> 32)); 1.147 + u.w[15] = NativeEndian::swapToBigEndian(uint32_t(size2)); 1.148 + shaCompress(&H[H2X], u.w); 1.149 + 1.150 + /* Output hash. */ 1.151 + u.w[0] = NativeEndian::swapToBigEndian(H[0]); 1.152 + u.w[1] = NativeEndian::swapToBigEndian(H[1]); 1.153 + u.w[2] = NativeEndian::swapToBigEndian(H[2]); 1.154 + u.w[3] = NativeEndian::swapToBigEndian(H[3]); 1.155 + u.w[4] = NativeEndian::swapToBigEndian(H[4]); 1.156 + memcpy(hashOut, u.w, 20); 1.157 + mDone = true; 1.158 +} 1.159 + 1.160 +/* 1.161 + * SHA: Compression function, unrolled. 1.162 + * 1.163 + * Some operations in shaCompress are done as 5 groups of 16 operations. 1.164 + * Others are done as 4 groups of 20 operations. 1.165 + * The code below shows that structure. 1.166 + * 1.167 + * The functions that compute the new values of the 5 state variables 1.168 + * A-E are done in 4 groups of 20 operations (or you may also think 1.169 + * of them as being done in 16 groups of 5 operations). They are 1.170 + * done by the SHA_RNDx macros below, in the right column. 1.171 + * 1.172 + * The functions that set the 16 values of the W array are done in 1.173 + * 5 groups of 16 operations. The first group is done by the 1.174 + * LOAD macros below, the latter 4 groups are done by SHA_MIX below, 1.175 + * in the left column. 1.176 + * 1.177 + * gcc's optimizer observes that each member of the W array is assigned 1.178 + * a value 5 times in this code. It reduces the number of store 1.179 + * operations done to the W array in the context (that is, in the X array) 1.180 + * by creating a W array on the stack, and storing the W values there for 1.181 + * the first 4 groups of operations on W, and storing the values in the 1.182 + * context's W array only in the fifth group. This is undesirable. 1.183 + * It is MUCH bigger code than simply using the context's W array, because 1.184 + * all the offsets to the W array in the stack are 32-bit signed offsets, 1.185 + * and it is no faster than storing the values in the context's W array. 1.186 + * 1.187 + * The original code for sha_fast.c prevented this creation of a separate 1.188 + * W array in the stack by creating a W array of 80 members, each of 1.189 + * whose elements is assigned only once. It also separated the computations 1.190 + * of the W array values and the computations of the values for the 5 1.191 + * state variables into two separate passes, W's, then A-E's so that the 1.192 + * second pass could be done all in registers (except for accessing the W 1.193 + * array) on machines with fewer registers. The method is suboptimal 1.194 + * for machines with enough registers to do it all in one pass, and it 1.195 + * necessitates using many instructions with 32-bit offsets. 1.196 + * 1.197 + * This code eliminates the separate W array on the stack by a completely 1.198 + * different means: by declaring the X array volatile. This prevents 1.199 + * the optimizer from trying to reduce the use of the X array by the 1.200 + * creation of a MORE expensive W array on the stack. The result is 1.201 + * that all instructions use signed 8-bit offsets and not 32-bit offsets. 1.202 + * 1.203 + * The combination of this code and the -O3 optimizer flag on GCC 3.4.3 1.204 + * results in code that is 3 times faster than the previous NSS sha_fast 1.205 + * code on AMD64. 1.206 + */ 1.207 +static void 1.208 +shaCompress(volatile unsigned *X, const uint32_t *inbuf) 1.209 +{ 1.210 + unsigned A, B, C, D, E; 1.211 + 1.212 +#define XH(n) X[n - H2X] 1.213 +#define XW(n) X[n - W2X] 1.214 + 1.215 +#define K0 0x5a827999L 1.216 +#define K1 0x6ed9eba1L 1.217 +#define K2 0x8f1bbcdcL 1.218 +#define K3 0xca62c1d6L 1.219 + 1.220 +#define SHA_RND1(a, b, c, d, e, n) \ 1.221 + a = SHA_ROTL(b, 5) + SHA_F1(c, d, e) + a + XW(n) + K0; c = SHA_ROTL(c, 30) 1.222 +#define SHA_RND2(a, b, c, d, e, n) \ 1.223 + a = SHA_ROTL(b, 5) + SHA_F2(c, d, e) + a + XW(n) + K1; c = SHA_ROTL(c, 30) 1.224 +#define SHA_RND3(a, b, c, d, e, n) \ 1.225 + a = SHA_ROTL(b, 5) + SHA_F3(c, d, e) + a + XW(n) + K2; c = SHA_ROTL(c, 30) 1.226 +#define SHA_RND4(a, b, c, d, e, n) \ 1.227 + a = SHA_ROTL(b ,5) + SHA_F4(c, d, e) + a + XW(n) + K3; c = SHA_ROTL(c, 30) 1.228 + 1.229 +#define LOAD(n) XW(n) = NativeEndian::swapToBigEndian(inbuf[n]) 1.230 + 1.231 + A = XH(0); 1.232 + B = XH(1); 1.233 + C = XH(2); 1.234 + D = XH(3); 1.235 + E = XH(4); 1.236 + 1.237 + LOAD(0); SHA_RND1(E,A,B,C,D, 0); 1.238 + LOAD(1); SHA_RND1(D,E,A,B,C, 1); 1.239 + LOAD(2); SHA_RND1(C,D,E,A,B, 2); 1.240 + LOAD(3); SHA_RND1(B,C,D,E,A, 3); 1.241 + LOAD(4); SHA_RND1(A,B,C,D,E, 4); 1.242 + LOAD(5); SHA_RND1(E,A,B,C,D, 5); 1.243 + LOAD(6); SHA_RND1(D,E,A,B,C, 6); 1.244 + LOAD(7); SHA_RND1(C,D,E,A,B, 7); 1.245 + LOAD(8); SHA_RND1(B,C,D,E,A, 8); 1.246 + LOAD(9); SHA_RND1(A,B,C,D,E, 9); 1.247 + LOAD(10); SHA_RND1(E,A,B,C,D,10); 1.248 + LOAD(11); SHA_RND1(D,E,A,B,C,11); 1.249 + LOAD(12); SHA_RND1(C,D,E,A,B,12); 1.250 + LOAD(13); SHA_RND1(B,C,D,E,A,13); 1.251 + LOAD(14); SHA_RND1(A,B,C,D,E,14); 1.252 + LOAD(15); SHA_RND1(E,A,B,C,D,15); 1.253 + 1.254 + SHA_MIX( 0, 13, 8, 2); SHA_RND1(D,E,A,B,C, 0); 1.255 + SHA_MIX( 1, 14, 9, 3); SHA_RND1(C,D,E,A,B, 1); 1.256 + SHA_MIX( 2, 15, 10, 4); SHA_RND1(B,C,D,E,A, 2); 1.257 + SHA_MIX( 3, 0, 11, 5); SHA_RND1(A,B,C,D,E, 3); 1.258 + 1.259 + SHA_MIX( 4, 1, 12, 6); SHA_RND2(E,A,B,C,D, 4); 1.260 + SHA_MIX( 5, 2, 13, 7); SHA_RND2(D,E,A,B,C, 5); 1.261 + SHA_MIX( 6, 3, 14, 8); SHA_RND2(C,D,E,A,B, 6); 1.262 + SHA_MIX( 7, 4, 15, 9); SHA_RND2(B,C,D,E,A, 7); 1.263 + SHA_MIX( 8, 5, 0, 10); SHA_RND2(A,B,C,D,E, 8); 1.264 + SHA_MIX( 9, 6, 1, 11); SHA_RND2(E,A,B,C,D, 9); 1.265 + SHA_MIX(10, 7, 2, 12); SHA_RND2(D,E,A,B,C,10); 1.266 + SHA_MIX(11, 8, 3, 13); SHA_RND2(C,D,E,A,B,11); 1.267 + SHA_MIX(12, 9, 4, 14); SHA_RND2(B,C,D,E,A,12); 1.268 + SHA_MIX(13, 10, 5, 15); SHA_RND2(A,B,C,D,E,13); 1.269 + SHA_MIX(14, 11, 6, 0); SHA_RND2(E,A,B,C,D,14); 1.270 + SHA_MIX(15, 12, 7, 1); SHA_RND2(D,E,A,B,C,15); 1.271 + 1.272 + SHA_MIX( 0, 13, 8, 2); SHA_RND2(C,D,E,A,B, 0); 1.273 + SHA_MIX( 1, 14, 9, 3); SHA_RND2(B,C,D,E,A, 1); 1.274 + SHA_MIX( 2, 15, 10, 4); SHA_RND2(A,B,C,D,E, 2); 1.275 + SHA_MIX( 3, 0, 11, 5); SHA_RND2(E,A,B,C,D, 3); 1.276 + SHA_MIX( 4, 1, 12, 6); SHA_RND2(D,E,A,B,C, 4); 1.277 + SHA_MIX( 5, 2, 13, 7); SHA_RND2(C,D,E,A,B, 5); 1.278 + SHA_MIX( 6, 3, 14, 8); SHA_RND2(B,C,D,E,A, 6); 1.279 + SHA_MIX( 7, 4, 15, 9); SHA_RND2(A,B,C,D,E, 7); 1.280 + 1.281 + SHA_MIX( 8, 5, 0, 10); SHA_RND3(E,A,B,C,D, 8); 1.282 + SHA_MIX( 9, 6, 1, 11); SHA_RND3(D,E,A,B,C, 9); 1.283 + SHA_MIX(10, 7, 2, 12); SHA_RND3(C,D,E,A,B,10); 1.284 + SHA_MIX(11, 8, 3, 13); SHA_RND3(B,C,D,E,A,11); 1.285 + SHA_MIX(12, 9, 4, 14); SHA_RND3(A,B,C,D,E,12); 1.286 + SHA_MIX(13, 10, 5, 15); SHA_RND3(E,A,B,C,D,13); 1.287 + SHA_MIX(14, 11, 6, 0); SHA_RND3(D,E,A,B,C,14); 1.288 + SHA_MIX(15, 12, 7, 1); SHA_RND3(C,D,E,A,B,15); 1.289 + 1.290 + SHA_MIX( 0, 13, 8, 2); SHA_RND3(B,C,D,E,A, 0); 1.291 + SHA_MIX( 1, 14, 9, 3); SHA_RND3(A,B,C,D,E, 1); 1.292 + SHA_MIX( 2, 15, 10, 4); SHA_RND3(E,A,B,C,D, 2); 1.293 + SHA_MIX( 3, 0, 11, 5); SHA_RND3(D,E,A,B,C, 3); 1.294 + SHA_MIX( 4, 1, 12, 6); SHA_RND3(C,D,E,A,B, 4); 1.295 + SHA_MIX( 5, 2, 13, 7); SHA_RND3(B,C,D,E,A, 5); 1.296 + SHA_MIX( 6, 3, 14, 8); SHA_RND3(A,B,C,D,E, 6); 1.297 + SHA_MIX( 7, 4, 15, 9); SHA_RND3(E,A,B,C,D, 7); 1.298 + SHA_MIX( 8, 5, 0, 10); SHA_RND3(D,E,A,B,C, 8); 1.299 + SHA_MIX( 9, 6, 1, 11); SHA_RND3(C,D,E,A,B, 9); 1.300 + SHA_MIX(10, 7, 2, 12); SHA_RND3(B,C,D,E,A,10); 1.301 + SHA_MIX(11, 8, 3, 13); SHA_RND3(A,B,C,D,E,11); 1.302 + 1.303 + SHA_MIX(12, 9, 4, 14); SHA_RND4(E,A,B,C,D,12); 1.304 + SHA_MIX(13, 10, 5, 15); SHA_RND4(D,E,A,B,C,13); 1.305 + SHA_MIX(14, 11, 6, 0); SHA_RND4(C,D,E,A,B,14); 1.306 + SHA_MIX(15, 12, 7, 1); SHA_RND4(B,C,D,E,A,15); 1.307 + 1.308 + SHA_MIX( 0, 13, 8, 2); SHA_RND4(A,B,C,D,E, 0); 1.309 + SHA_MIX( 1, 14, 9, 3); SHA_RND4(E,A,B,C,D, 1); 1.310 + SHA_MIX( 2, 15, 10, 4); SHA_RND4(D,E,A,B,C, 2); 1.311 + SHA_MIX( 3, 0, 11, 5); SHA_RND4(C,D,E,A,B, 3); 1.312 + SHA_MIX( 4, 1, 12, 6); SHA_RND4(B,C,D,E,A, 4); 1.313 + SHA_MIX( 5, 2, 13, 7); SHA_RND4(A,B,C,D,E, 5); 1.314 + SHA_MIX( 6, 3, 14, 8); SHA_RND4(E,A,B,C,D, 6); 1.315 + SHA_MIX( 7, 4, 15, 9); SHA_RND4(D,E,A,B,C, 7); 1.316 + SHA_MIX( 8, 5, 0, 10); SHA_RND4(C,D,E,A,B, 8); 1.317 + SHA_MIX( 9, 6, 1, 11); SHA_RND4(B,C,D,E,A, 9); 1.318 + SHA_MIX(10, 7, 2, 12); SHA_RND4(A,B,C,D,E,10); 1.319 + SHA_MIX(11, 8, 3, 13); SHA_RND4(E,A,B,C,D,11); 1.320 + SHA_MIX(12, 9, 4, 14); SHA_RND4(D,E,A,B,C,12); 1.321 + SHA_MIX(13, 10, 5, 15); SHA_RND4(C,D,E,A,B,13); 1.322 + SHA_MIX(14, 11, 6, 0); SHA_RND4(B,C,D,E,A,14); 1.323 + SHA_MIX(15, 12, 7, 1); SHA_RND4(A,B,C,D,E,15); 1.324 + 1.325 + XH(0) += A; 1.326 + XH(1) += B; 1.327 + XH(2) += C; 1.328 + XH(3) += D; 1.329 + XH(4) += E; 1.330 +}