michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "mozilla/Assertions.h" michael@0: #include "mozilla/Endian.h" michael@0: #include "mozilla/SHA1.h" michael@0: michael@0: #include michael@0: michael@0: using mozilla::NativeEndian; michael@0: using mozilla::SHA1Sum; michael@0: michael@0: static inline uint32_t michael@0: SHA_ROTL(uint32_t t, uint32_t n) michael@0: { michael@0: MOZ_ASSERT(n < 32); michael@0: return (t << n) | (t >> (32 - n)); michael@0: } michael@0: michael@0: static void michael@0: shaCompress(volatile unsigned* X, const uint32_t* datain); michael@0: michael@0: #define SHA_F1(X, Y, Z) ((((Y) ^ (Z)) & (X)) ^ (Z)) michael@0: #define SHA_F2(X, Y, Z) ((X) ^ (Y) ^ (Z)) michael@0: #define SHA_F3(X, Y, Z) (((X) & (Y)) | ((Z) & ((X) | (Y)))) michael@0: #define SHA_F4(X, Y, Z) ((X) ^ (Y) ^ (Z)) michael@0: michael@0: #define SHA_MIX(n, a, b, c) XW(n) = SHA_ROTL(XW(a) ^ XW(b) ^ XW(c) ^XW(n), 1) michael@0: michael@0: SHA1Sum::SHA1Sum() michael@0: : size(0), mDone(false) michael@0: { michael@0: // Initialize H with constants from FIPS180-1. michael@0: H[0] = 0x67452301L; michael@0: H[1] = 0xefcdab89L; michael@0: H[2] = 0x98badcfeL; michael@0: H[3] = 0x10325476L; michael@0: H[4] = 0xc3d2e1f0L; michael@0: } michael@0: michael@0: /* michael@0: * Explanation of H array and index values: michael@0: * michael@0: * The context's H array is actually the concatenation of two arrays michael@0: * defined by SHA1, the H array of state variables (5 elements), michael@0: * and the W array of intermediate values, of which there are 16 elements. michael@0: * The W array starts at H[5], that is W[0] is H[5]. michael@0: * Although these values are defined as 32-bit values, we use 64-bit michael@0: * variables to hold them because the AMD64 stores 64 bit values in michael@0: * memory MUCH faster than it stores any smaller values. michael@0: * michael@0: * Rather than passing the context structure to shaCompress, we pass michael@0: * this combined array of H and W values. We do not pass the address michael@0: * of the first element of this array, but rather pass the address of an michael@0: * element in the middle of the array, element X. Presently X[0] is H[11]. michael@0: * So we pass the address of H[11] as the address of array X to shaCompress. michael@0: * Then shaCompress accesses the members of the array using positive AND michael@0: * negative indexes. michael@0: * michael@0: * Pictorially: (each element is 8 bytes) michael@0: * H | H0 H1 H2 H3 H4 W0 W1 W2 W3 W4 W5 W6 W7 W8 W9 Wa Wb Wc Wd We Wf | michael@0: * X |-11-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 | michael@0: * michael@0: * The byte offset from X[0] to any member of H and W is always michael@0: * representable in a signed 8-bit value, which will be encoded michael@0: * as a single byte offset in the X86-64 instruction set. michael@0: * If we didn't pass the address of H[11], and instead passed the michael@0: * address of H[0], the offsets to elements H[16] and above would be michael@0: * greater than 127, not representable in a signed 8-bit value, and the michael@0: * x86-64 instruction set would encode every such offset as a 32-bit michael@0: * signed number in each instruction that accessed element H[16] or michael@0: * higher. This results in much bigger and slower code. michael@0: */ michael@0: #define H2X 11 /* X[0] is H[11], and H[0] is X[-11] */ michael@0: #define W2X 6 /* X[0] is W[6], and W[0] is X[-6] */ michael@0: michael@0: /* michael@0: * SHA: Add data to context. michael@0: */ michael@0: void michael@0: SHA1Sum::update(const void* dataIn, uint32_t len) michael@0: { michael@0: MOZ_ASSERT(!mDone, "SHA1Sum can only be used to compute a single hash."); michael@0: michael@0: const uint8_t* data = static_cast(dataIn); michael@0: michael@0: if (len == 0) michael@0: return; michael@0: michael@0: /* Accumulate the byte count. */ michael@0: unsigned int lenB = static_cast(size) & 63U; michael@0: michael@0: size += len; michael@0: michael@0: /* Read the data into W and process blocks as they get full. */ michael@0: unsigned int togo; michael@0: if (lenB > 0) { michael@0: togo = 64U - lenB; michael@0: if (len < togo) michael@0: togo = len; michael@0: memcpy(u.b + lenB, data, togo); michael@0: len -= togo; michael@0: data += togo; michael@0: lenB = (lenB + togo) & 63U; michael@0: if (!lenB) michael@0: shaCompress(&H[H2X], u.w); michael@0: } michael@0: michael@0: while (len >= 64U) { michael@0: len -= 64U; michael@0: shaCompress(&H[H2X], reinterpret_cast(data)); michael@0: data += 64U; michael@0: } michael@0: michael@0: if (len > 0) michael@0: memcpy(u.b, data, len); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * SHA: Generate hash value michael@0: */ michael@0: void michael@0: SHA1Sum::finish(SHA1Sum::Hash& hashOut) michael@0: { michael@0: MOZ_ASSERT(!mDone, "SHA1Sum can only be used to compute a single hash."); michael@0: michael@0: uint64_t size2 = size; michael@0: uint32_t lenB = uint32_t(size2) & 63; michael@0: michael@0: static const uint8_t bulk_pad[64] = michael@0: { 0x80,0,0,0,0,0,0,0,0,0, michael@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,0, michael@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,0 }; michael@0: michael@0: /* Pad with a binary 1 (e.g. 0x80), then zeroes, then length in bits. */ michael@0: update(bulk_pad, (((55 + 64) - lenB) & 63) + 1); michael@0: MOZ_ASSERT((uint32_t(size) & 63) == 56); michael@0: michael@0: /* Convert size from bytes to bits. */ michael@0: size2 <<= 3; michael@0: u.w[14] = NativeEndian::swapToBigEndian(uint32_t(size2 >> 32)); michael@0: u.w[15] = NativeEndian::swapToBigEndian(uint32_t(size2)); michael@0: shaCompress(&H[H2X], u.w); michael@0: michael@0: /* Output hash. */ michael@0: u.w[0] = NativeEndian::swapToBigEndian(H[0]); michael@0: u.w[1] = NativeEndian::swapToBigEndian(H[1]); michael@0: u.w[2] = NativeEndian::swapToBigEndian(H[2]); michael@0: u.w[3] = NativeEndian::swapToBigEndian(H[3]); michael@0: u.w[4] = NativeEndian::swapToBigEndian(H[4]); michael@0: memcpy(hashOut, u.w, 20); michael@0: mDone = true; michael@0: } michael@0: michael@0: /* michael@0: * SHA: Compression function, unrolled. michael@0: * michael@0: * Some operations in shaCompress are done as 5 groups of 16 operations. michael@0: * Others are done as 4 groups of 20 operations. michael@0: * The code below shows that structure. michael@0: * michael@0: * The functions that compute the new values of the 5 state variables michael@0: * A-E are done in 4 groups of 20 operations (or you may also think michael@0: * of them as being done in 16 groups of 5 operations). They are michael@0: * done by the SHA_RNDx macros below, in the right column. michael@0: * michael@0: * The functions that set the 16 values of the W array are done in michael@0: * 5 groups of 16 operations. The first group is done by the michael@0: * LOAD macros below, the latter 4 groups are done by SHA_MIX below, michael@0: * in the left column. michael@0: * michael@0: * gcc's optimizer observes that each member of the W array is assigned michael@0: * a value 5 times in this code. It reduces the number of store michael@0: * operations done to the W array in the context (that is, in the X array) michael@0: * by creating a W array on the stack, and storing the W values there for michael@0: * the first 4 groups of operations on W, and storing the values in the michael@0: * context's W array only in the fifth group. This is undesirable. michael@0: * It is MUCH bigger code than simply using the context's W array, because michael@0: * all the offsets to the W array in the stack are 32-bit signed offsets, michael@0: * and it is no faster than storing the values in the context's W array. michael@0: * michael@0: * The original code for sha_fast.c prevented this creation of a separate michael@0: * W array in the stack by creating a W array of 80 members, each of michael@0: * whose elements is assigned only once. It also separated the computations michael@0: * of the W array values and the computations of the values for the 5 michael@0: * state variables into two separate passes, W's, then A-E's so that the michael@0: * second pass could be done all in registers (except for accessing the W michael@0: * array) on machines with fewer registers. The method is suboptimal michael@0: * for machines with enough registers to do it all in one pass, and it michael@0: * necessitates using many instructions with 32-bit offsets. michael@0: * michael@0: * This code eliminates the separate W array on the stack by a completely michael@0: * different means: by declaring the X array volatile. This prevents michael@0: * the optimizer from trying to reduce the use of the X array by the michael@0: * creation of a MORE expensive W array on the stack. The result is michael@0: * that all instructions use signed 8-bit offsets and not 32-bit offsets. michael@0: * michael@0: * The combination of this code and the -O3 optimizer flag on GCC 3.4.3 michael@0: * results in code that is 3 times faster than the previous NSS sha_fast michael@0: * code on AMD64. michael@0: */ michael@0: static void michael@0: shaCompress(volatile unsigned *X, const uint32_t *inbuf) michael@0: { michael@0: unsigned A, B, C, D, E; michael@0: michael@0: #define XH(n) X[n - H2X] michael@0: #define XW(n) X[n - W2X] michael@0: michael@0: #define K0 0x5a827999L michael@0: #define K1 0x6ed9eba1L michael@0: #define K2 0x8f1bbcdcL michael@0: #define K3 0xca62c1d6L michael@0: michael@0: #define SHA_RND1(a, b, c, d, e, n) \ michael@0: a = SHA_ROTL(b, 5) + SHA_F1(c, d, e) + a + XW(n) + K0; c = SHA_ROTL(c, 30) michael@0: #define SHA_RND2(a, b, c, d, e, n) \ michael@0: a = SHA_ROTL(b, 5) + SHA_F2(c, d, e) + a + XW(n) + K1; c = SHA_ROTL(c, 30) michael@0: #define SHA_RND3(a, b, c, d, e, n) \ michael@0: a = SHA_ROTL(b, 5) + SHA_F3(c, d, e) + a + XW(n) + K2; c = SHA_ROTL(c, 30) michael@0: #define SHA_RND4(a, b, c, d, e, n) \ michael@0: a = SHA_ROTL(b ,5) + SHA_F4(c, d, e) + a + XW(n) + K3; c = SHA_ROTL(c, 30) michael@0: michael@0: #define LOAD(n) XW(n) = NativeEndian::swapToBigEndian(inbuf[n]) michael@0: michael@0: A = XH(0); michael@0: B = XH(1); michael@0: C = XH(2); michael@0: D = XH(3); michael@0: E = XH(4); michael@0: michael@0: LOAD(0); SHA_RND1(E,A,B,C,D, 0); michael@0: LOAD(1); SHA_RND1(D,E,A,B,C, 1); michael@0: LOAD(2); SHA_RND1(C,D,E,A,B, 2); michael@0: LOAD(3); SHA_RND1(B,C,D,E,A, 3); michael@0: LOAD(4); SHA_RND1(A,B,C,D,E, 4); michael@0: LOAD(5); SHA_RND1(E,A,B,C,D, 5); michael@0: LOAD(6); SHA_RND1(D,E,A,B,C, 6); michael@0: LOAD(7); SHA_RND1(C,D,E,A,B, 7); michael@0: LOAD(8); SHA_RND1(B,C,D,E,A, 8); michael@0: LOAD(9); SHA_RND1(A,B,C,D,E, 9); michael@0: LOAD(10); SHA_RND1(E,A,B,C,D,10); michael@0: LOAD(11); SHA_RND1(D,E,A,B,C,11); michael@0: LOAD(12); SHA_RND1(C,D,E,A,B,12); michael@0: LOAD(13); SHA_RND1(B,C,D,E,A,13); michael@0: LOAD(14); SHA_RND1(A,B,C,D,E,14); michael@0: LOAD(15); SHA_RND1(E,A,B,C,D,15); michael@0: michael@0: SHA_MIX( 0, 13, 8, 2); SHA_RND1(D,E,A,B,C, 0); michael@0: SHA_MIX( 1, 14, 9, 3); SHA_RND1(C,D,E,A,B, 1); michael@0: SHA_MIX( 2, 15, 10, 4); SHA_RND1(B,C,D,E,A, 2); michael@0: SHA_MIX( 3, 0, 11, 5); SHA_RND1(A,B,C,D,E, 3); michael@0: michael@0: SHA_MIX( 4, 1, 12, 6); SHA_RND2(E,A,B,C,D, 4); michael@0: SHA_MIX( 5, 2, 13, 7); SHA_RND2(D,E,A,B,C, 5); michael@0: SHA_MIX( 6, 3, 14, 8); SHA_RND2(C,D,E,A,B, 6); michael@0: SHA_MIX( 7, 4, 15, 9); SHA_RND2(B,C,D,E,A, 7); michael@0: SHA_MIX( 8, 5, 0, 10); SHA_RND2(A,B,C,D,E, 8); michael@0: SHA_MIX( 9, 6, 1, 11); SHA_RND2(E,A,B,C,D, 9); michael@0: SHA_MIX(10, 7, 2, 12); SHA_RND2(D,E,A,B,C,10); michael@0: SHA_MIX(11, 8, 3, 13); SHA_RND2(C,D,E,A,B,11); michael@0: SHA_MIX(12, 9, 4, 14); SHA_RND2(B,C,D,E,A,12); michael@0: SHA_MIX(13, 10, 5, 15); SHA_RND2(A,B,C,D,E,13); michael@0: SHA_MIX(14, 11, 6, 0); SHA_RND2(E,A,B,C,D,14); michael@0: SHA_MIX(15, 12, 7, 1); SHA_RND2(D,E,A,B,C,15); michael@0: michael@0: SHA_MIX( 0, 13, 8, 2); SHA_RND2(C,D,E,A,B, 0); michael@0: SHA_MIX( 1, 14, 9, 3); SHA_RND2(B,C,D,E,A, 1); michael@0: SHA_MIX( 2, 15, 10, 4); SHA_RND2(A,B,C,D,E, 2); michael@0: SHA_MIX( 3, 0, 11, 5); SHA_RND2(E,A,B,C,D, 3); michael@0: SHA_MIX( 4, 1, 12, 6); SHA_RND2(D,E,A,B,C, 4); michael@0: SHA_MIX( 5, 2, 13, 7); SHA_RND2(C,D,E,A,B, 5); michael@0: SHA_MIX( 6, 3, 14, 8); SHA_RND2(B,C,D,E,A, 6); michael@0: SHA_MIX( 7, 4, 15, 9); SHA_RND2(A,B,C,D,E, 7); michael@0: michael@0: SHA_MIX( 8, 5, 0, 10); SHA_RND3(E,A,B,C,D, 8); michael@0: SHA_MIX( 9, 6, 1, 11); SHA_RND3(D,E,A,B,C, 9); michael@0: SHA_MIX(10, 7, 2, 12); SHA_RND3(C,D,E,A,B,10); michael@0: SHA_MIX(11, 8, 3, 13); SHA_RND3(B,C,D,E,A,11); michael@0: SHA_MIX(12, 9, 4, 14); SHA_RND3(A,B,C,D,E,12); michael@0: SHA_MIX(13, 10, 5, 15); SHA_RND3(E,A,B,C,D,13); michael@0: SHA_MIX(14, 11, 6, 0); SHA_RND3(D,E,A,B,C,14); michael@0: SHA_MIX(15, 12, 7, 1); SHA_RND3(C,D,E,A,B,15); michael@0: michael@0: SHA_MIX( 0, 13, 8, 2); SHA_RND3(B,C,D,E,A, 0); michael@0: SHA_MIX( 1, 14, 9, 3); SHA_RND3(A,B,C,D,E, 1); michael@0: SHA_MIX( 2, 15, 10, 4); SHA_RND3(E,A,B,C,D, 2); michael@0: SHA_MIX( 3, 0, 11, 5); SHA_RND3(D,E,A,B,C, 3); michael@0: SHA_MIX( 4, 1, 12, 6); SHA_RND3(C,D,E,A,B, 4); michael@0: SHA_MIX( 5, 2, 13, 7); SHA_RND3(B,C,D,E,A, 5); michael@0: SHA_MIX( 6, 3, 14, 8); SHA_RND3(A,B,C,D,E, 6); michael@0: SHA_MIX( 7, 4, 15, 9); SHA_RND3(E,A,B,C,D, 7); michael@0: SHA_MIX( 8, 5, 0, 10); SHA_RND3(D,E,A,B,C, 8); michael@0: SHA_MIX( 9, 6, 1, 11); SHA_RND3(C,D,E,A,B, 9); michael@0: SHA_MIX(10, 7, 2, 12); SHA_RND3(B,C,D,E,A,10); michael@0: SHA_MIX(11, 8, 3, 13); SHA_RND3(A,B,C,D,E,11); michael@0: michael@0: SHA_MIX(12, 9, 4, 14); SHA_RND4(E,A,B,C,D,12); michael@0: SHA_MIX(13, 10, 5, 15); SHA_RND4(D,E,A,B,C,13); michael@0: SHA_MIX(14, 11, 6, 0); SHA_RND4(C,D,E,A,B,14); michael@0: SHA_MIX(15, 12, 7, 1); SHA_RND4(B,C,D,E,A,15); michael@0: michael@0: SHA_MIX( 0, 13, 8, 2); SHA_RND4(A,B,C,D,E, 0); michael@0: SHA_MIX( 1, 14, 9, 3); SHA_RND4(E,A,B,C,D, 1); michael@0: SHA_MIX( 2, 15, 10, 4); SHA_RND4(D,E,A,B,C, 2); michael@0: SHA_MIX( 3, 0, 11, 5); SHA_RND4(C,D,E,A,B, 3); michael@0: SHA_MIX( 4, 1, 12, 6); SHA_RND4(B,C,D,E,A, 4); michael@0: SHA_MIX( 5, 2, 13, 7); SHA_RND4(A,B,C,D,E, 5); michael@0: SHA_MIX( 6, 3, 14, 8); SHA_RND4(E,A,B,C,D, 6); michael@0: SHA_MIX( 7, 4, 15, 9); SHA_RND4(D,E,A,B,C, 7); michael@0: SHA_MIX( 8, 5, 0, 10); SHA_RND4(C,D,E,A,B, 8); michael@0: SHA_MIX( 9, 6, 1, 11); SHA_RND4(B,C,D,E,A, 9); michael@0: SHA_MIX(10, 7, 2, 12); SHA_RND4(A,B,C,D,E,10); michael@0: SHA_MIX(11, 8, 3, 13); SHA_RND4(E,A,B,C,D,11); michael@0: SHA_MIX(12, 9, 4, 14); SHA_RND4(D,E,A,B,C,12); michael@0: SHA_MIX(13, 10, 5, 15); SHA_RND4(C,D,E,A,B,13); michael@0: SHA_MIX(14, 11, 6, 0); SHA_RND4(B,C,D,E,A,14); michael@0: SHA_MIX(15, 12, 7, 1); SHA_RND4(A,B,C,D,E,15); michael@0: michael@0: XH(0) += A; michael@0: XH(1) += B; michael@0: XH(2) += C; michael@0: XH(3) += D; michael@0: XH(4) += E; michael@0: }