andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: #ifdef FREEBL_NO_DEPEND andre@0: #include "stubs.h" andre@0: #endif andre@0: andre@0: #include andre@0: #include "blapi.h" andre@0: #include "sha_fast.h" andre@0: #include "prerror.h" andre@0: andre@0: #ifdef TRACING_SSL andre@0: #include "ssl.h" andre@0: #include "ssltrace.h" andre@0: #endif andre@0: andre@0: static void shaCompress(volatile SHA_HW_t *X, const PRUint32 * datain); andre@0: andre@0: #define W u.w andre@0: #define B u.b andre@0: andre@0: andre@0: #define SHA_F1(X,Y,Z) ((((Y)^(Z))&(X))^(Z)) andre@0: #define SHA_F2(X,Y,Z) ((X)^(Y)^(Z)) andre@0: #define SHA_F3(X,Y,Z) (((X)&(Y))|((Z)&((X)|(Y)))) andre@0: #define SHA_F4(X,Y,Z) ((X)^(Y)^(Z)) andre@0: andre@0: #define SHA_MIX(n,a,b,c) XW(n) = SHA_ROTL(XW(a)^XW(b)^XW(c)^XW(n), 1) andre@0: andre@0: /* andre@0: * SHA: initialize context andre@0: */ andre@0: void andre@0: SHA1_Begin(SHA1Context *ctx) andre@0: { andre@0: ctx->size = 0; andre@0: /* andre@0: * Initialize H with constants from FIPS180-1. andre@0: */ andre@0: ctx->H[0] = 0x67452301L; andre@0: ctx->H[1] = 0xefcdab89L; andre@0: ctx->H[2] = 0x98badcfeL; andre@0: ctx->H[3] = 0x10325476L; andre@0: ctx->H[4] = 0xc3d2e1f0L; andre@0: } andre@0: andre@0: /* Explanation of H array and index values: andre@0: * The context's H array is actually the concatenation of two arrays andre@0: * defined by SHA1, the H array of state variables (5 elements), andre@0: * and the W array of intermediate values, of which there are 16 elements. andre@0: * The W array starts at H[5], that is W[0] is H[5]. andre@0: * Although these values are defined as 32-bit values, we use 64-bit andre@0: * variables to hold them because the AMD64 stores 64 bit values in andre@0: * memory MUCH faster than it stores any smaller values. andre@0: * andre@0: * Rather than passing the context structure to shaCompress, we pass andre@0: * this combined array of H and W values. We do not pass the address andre@0: * of the first element of this array, but rather pass the address of an andre@0: * element in the middle of the array, element X. Presently X[0] is H[11]. andre@0: * So we pass the address of H[11] as the address of array X to shaCompress. andre@0: * Then shaCompress accesses the members of the array using positive AND andre@0: * negative indexes. andre@0: * andre@0: * Pictorially: (each element is 8 bytes) andre@0: * H | H0 H1 H2 H3 H4 W0 W1 W2 W3 W4 W5 W6 W7 W8 W9 Wa Wb Wc Wd We Wf | andre@0: * X |-11-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 | andre@0: * andre@0: * The byte offset from X[0] to any member of H and W is always andre@0: * representable in a signed 8-bit value, which will be encoded andre@0: * as a single byte offset in the X86-64 instruction set. andre@0: * If we didn't pass the address of H[11], and instead passed the andre@0: * address of H[0], the offsets to elements H[16] and above would be andre@0: * greater than 127, not representable in a signed 8-bit value, and the andre@0: * x86-64 instruction set would encode every such offset as a 32-bit andre@0: * signed number in each instruction that accessed element H[16] or andre@0: * higher. This results in much bigger and slower code. andre@0: */ andre@0: #if !defined(SHA_PUT_W_IN_STACK) andre@0: #define H2X 11 /* X[0] is H[11], and H[0] is X[-11] */ andre@0: #define W2X 6 /* X[0] is W[6], and W[0] is X[-6] */ andre@0: #else andre@0: #define H2X 0 andre@0: #endif andre@0: andre@0: /* andre@0: * SHA: Add data to context. andre@0: */ andre@0: void andre@0: SHA1_Update(SHA1Context *ctx, const unsigned char *dataIn, unsigned int len) andre@0: { andre@0: register unsigned int lenB; andre@0: register unsigned int togo; andre@0: andre@0: if (!len) andre@0: return; andre@0: andre@0: /* accumulate the byte count. */ andre@0: lenB = (unsigned int)(ctx->size) & 63U; andre@0: andre@0: ctx->size += len; andre@0: andre@0: /* andre@0: * Read the data into W and process blocks as they get full andre@0: */ andre@0: if (lenB > 0) { andre@0: togo = 64U - lenB; andre@0: if (len < togo) andre@0: togo = len; andre@0: memcpy(ctx->B + lenB, dataIn, togo); andre@0: len -= togo; andre@0: dataIn += togo; andre@0: lenB = (lenB + togo) & 63U; andre@0: if (!lenB) { andre@0: shaCompress(&ctx->H[H2X], ctx->W); andre@0: } andre@0: } andre@0: #if !defined(SHA_ALLOW_UNALIGNED_ACCESS) andre@0: if ((ptrdiff_t)dataIn % sizeof(PRUint32)) { andre@0: while (len >= 64U) { andre@0: memcpy(ctx->B, dataIn, 64); andre@0: len -= 64U; andre@0: shaCompress(&ctx->H[H2X], ctx->W); andre@0: dataIn += 64U; andre@0: } andre@0: } else andre@0: #endif andre@0: { andre@0: while (len >= 64U) { andre@0: len -= 64U; andre@0: shaCompress(&ctx->H[H2X], (PRUint32 *)dataIn); andre@0: dataIn += 64U; andre@0: } andre@0: } andre@0: if (len) { andre@0: memcpy(ctx->B, dataIn, len); andre@0: } andre@0: } andre@0: andre@0: andre@0: /* andre@0: * SHA: Generate hash value from context andre@0: */ andre@0: void andre@0: SHA1_End(SHA1Context *ctx, unsigned char *hashout, andre@0: unsigned int *pDigestLen, unsigned int maxDigestLen) andre@0: { andre@0: register PRUint64 size; andre@0: register PRUint32 lenB; andre@0: PRUint32 tmpbuf[5]; andre@0: andre@0: static const unsigned char bulk_pad[64] = { 0x80,0,0,0,0,0,0,0,0,0, andre@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, andre@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 }; andre@0: #define tmp lenB andre@0: andre@0: PORT_Assert (maxDigestLen >= SHA1_LENGTH); andre@0: andre@0: /* andre@0: * Pad with a binary 1 (e.g. 0x80), then zeroes, then length in bits andre@0: */ andre@0: size = ctx->size; andre@0: andre@0: lenB = (PRUint32)size & 63; andre@0: SHA1_Update(ctx, bulk_pad, (((55+64) - lenB) & 63) + 1); andre@0: PORT_Assert(((PRUint32)ctx->size & 63) == 56); andre@0: /* Convert size from bytes to bits. */ andre@0: size <<= 3; andre@0: ctx->W[14] = SHA_HTONL((PRUint32)(size >> 32)); andre@0: ctx->W[15] = SHA_HTONL((PRUint32)size); andre@0: shaCompress(&ctx->H[H2X], ctx->W); andre@0: andre@0: /* andre@0: * Output hash andre@0: */ andre@0: SHA_STORE_RESULT; andre@0: if (pDigestLen) { andre@0: *pDigestLen = SHA1_LENGTH; andre@0: } andre@0: #undef tmp andre@0: } andre@0: andre@0: void andre@0: SHA1_EndRaw(SHA1Context *ctx, unsigned char *hashout, andre@0: unsigned int *pDigestLen, unsigned int maxDigestLen) andre@0: { andre@0: #if defined(SHA_NEED_TMP_VARIABLE) andre@0: register PRUint32 tmp; andre@0: #endif andre@0: PRUint32 tmpbuf[5]; andre@0: PORT_Assert (maxDigestLen >= SHA1_LENGTH); andre@0: andre@0: SHA_STORE_RESULT; andre@0: if (pDigestLen) andre@0: *pDigestLen = SHA1_LENGTH; andre@0: } andre@0: andre@0: #undef B andre@0: /* andre@0: * SHA: Compression function, unrolled. andre@0: * andre@0: * Some operations in shaCompress are done as 5 groups of 16 operations. andre@0: * Others are done as 4 groups of 20 operations. andre@0: * The code below shows that structure. andre@0: * andre@0: * The functions that compute the new values of the 5 state variables andre@0: * A-E are done in 4 groups of 20 operations (or you may also think andre@0: * of them as being done in 16 groups of 5 operations). They are andre@0: * done by the SHA_RNDx macros below, in the right column. andre@0: * andre@0: * The functions that set the 16 values of the W array are done in andre@0: * 5 groups of 16 operations. The first group is done by the andre@0: * LOAD macros below, the latter 4 groups are done by SHA_MIX below, andre@0: * in the left column. andre@0: * andre@0: * gcc's optimizer observes that each member of the W array is assigned andre@0: * a value 5 times in this code. It reduces the number of store andre@0: * operations done to the W array in the context (that is, in the X array) andre@0: * by creating a W array on the stack, and storing the W values there for andre@0: * the first 4 groups of operations on W, and storing the values in the andre@0: * context's W array only in the fifth group. This is undesirable. andre@0: * It is MUCH bigger code than simply using the context's W array, because andre@0: * all the offsets to the W array in the stack are 32-bit signed offsets, andre@0: * and it is no faster than storing the values in the context's W array. andre@0: * andre@0: * The original code for sha_fast.c prevented this creation of a separate andre@0: * W array in the stack by creating a W array of 80 members, each of andre@0: * whose elements is assigned only once. It also separated the computations andre@0: * of the W array values and the computations of the values for the 5 andre@0: * state variables into two separate passes, W's, then A-E's so that the andre@0: * second pass could be done all in registers (except for accessing the W andre@0: * array) on machines with fewer registers. The method is suboptimal andre@0: * for machines with enough registers to do it all in one pass, and it andre@0: * necessitates using many instructions with 32-bit offsets. andre@0: * andre@0: * This code eliminates the separate W array on the stack by a completely andre@0: * different means: by declaring the X array volatile. This prevents andre@0: * the optimizer from trying to reduce the use of the X array by the andre@0: * creation of a MORE expensive W array on the stack. The result is andre@0: * that all instructions use signed 8-bit offsets and not 32-bit offsets. andre@0: * andre@0: * The combination of this code and the -O3 optimizer flag on GCC 3.4.3 andre@0: * results in code that is 3 times faster than the previous NSS sha_fast andre@0: * code on AMD64. andre@0: */ andre@0: static void andre@0: shaCompress(volatile SHA_HW_t *X, const PRUint32 *inbuf) andre@0: { andre@0: register SHA_HW_t A, B, C, D, E; andre@0: andre@0: #if defined(SHA_NEED_TMP_VARIABLE) andre@0: register PRUint32 tmp; andre@0: #endif andre@0: andre@0: #if !defined(SHA_PUT_W_IN_STACK) andre@0: #define XH(n) X[n-H2X] andre@0: #define XW(n) X[n-W2X] andre@0: #else andre@0: SHA_HW_t w_0, w_1, w_2, w_3, w_4, w_5, w_6, w_7, andre@0: w_8, w_9, w_10, w_11, w_12, w_13, w_14, w_15; andre@0: #define XW(n) w_ ## n andre@0: #define XH(n) X[n] andre@0: #endif andre@0: andre@0: #define K0 0x5a827999L andre@0: #define K1 0x6ed9eba1L andre@0: #define K2 0x8f1bbcdcL andre@0: #define K3 0xca62c1d6L andre@0: andre@0: #define SHA_RND1(a,b,c,d,e,n) \ andre@0: a = SHA_ROTL(b,5)+SHA_F1(c,d,e)+a+XW(n)+K0; c=SHA_ROTL(c,30) andre@0: #define SHA_RND2(a,b,c,d,e,n) \ andre@0: a = SHA_ROTL(b,5)+SHA_F2(c,d,e)+a+XW(n)+K1; c=SHA_ROTL(c,30) andre@0: #define SHA_RND3(a,b,c,d,e,n) \ andre@0: a = SHA_ROTL(b,5)+SHA_F3(c,d,e)+a+XW(n)+K2; c=SHA_ROTL(c,30) andre@0: #define SHA_RND4(a,b,c,d,e,n) \ andre@0: a = SHA_ROTL(b,5)+SHA_F4(c,d,e)+a+XW(n)+K3; c=SHA_ROTL(c,30) andre@0: andre@0: #define LOAD(n) XW(n) = SHA_HTONL(inbuf[n]) andre@0: andre@0: A = XH(0); andre@0: B = XH(1); andre@0: C = XH(2); andre@0: D = XH(3); andre@0: E = XH(4); andre@0: andre@0: LOAD(0); SHA_RND1(E,A,B,C,D, 0); andre@0: LOAD(1); SHA_RND1(D,E,A,B,C, 1); andre@0: LOAD(2); SHA_RND1(C,D,E,A,B, 2); andre@0: LOAD(3); SHA_RND1(B,C,D,E,A, 3); andre@0: LOAD(4); SHA_RND1(A,B,C,D,E, 4); andre@0: LOAD(5); SHA_RND1(E,A,B,C,D, 5); andre@0: LOAD(6); SHA_RND1(D,E,A,B,C, 6); andre@0: LOAD(7); SHA_RND1(C,D,E,A,B, 7); andre@0: LOAD(8); SHA_RND1(B,C,D,E,A, 8); andre@0: LOAD(9); SHA_RND1(A,B,C,D,E, 9); andre@0: LOAD(10); SHA_RND1(E,A,B,C,D,10); andre@0: LOAD(11); SHA_RND1(D,E,A,B,C,11); andre@0: LOAD(12); SHA_RND1(C,D,E,A,B,12); andre@0: LOAD(13); SHA_RND1(B,C,D,E,A,13); andre@0: LOAD(14); SHA_RND1(A,B,C,D,E,14); andre@0: LOAD(15); SHA_RND1(E,A,B,C,D,15); andre@0: andre@0: SHA_MIX( 0, 13, 8, 2); SHA_RND1(D,E,A,B,C, 0); andre@0: SHA_MIX( 1, 14, 9, 3); SHA_RND1(C,D,E,A,B, 1); andre@0: SHA_MIX( 2, 15, 10, 4); SHA_RND1(B,C,D,E,A, 2); andre@0: SHA_MIX( 3, 0, 11, 5); SHA_RND1(A,B,C,D,E, 3); andre@0: andre@0: SHA_MIX( 4, 1, 12, 6); SHA_RND2(E,A,B,C,D, 4); andre@0: SHA_MIX( 5, 2, 13, 7); SHA_RND2(D,E,A,B,C, 5); andre@0: SHA_MIX( 6, 3, 14, 8); SHA_RND2(C,D,E,A,B, 6); andre@0: SHA_MIX( 7, 4, 15, 9); SHA_RND2(B,C,D,E,A, 7); andre@0: SHA_MIX( 8, 5, 0, 10); SHA_RND2(A,B,C,D,E, 8); andre@0: SHA_MIX( 9, 6, 1, 11); SHA_RND2(E,A,B,C,D, 9); andre@0: SHA_MIX(10, 7, 2, 12); SHA_RND2(D,E,A,B,C,10); andre@0: SHA_MIX(11, 8, 3, 13); SHA_RND2(C,D,E,A,B,11); andre@0: SHA_MIX(12, 9, 4, 14); SHA_RND2(B,C,D,E,A,12); andre@0: SHA_MIX(13, 10, 5, 15); SHA_RND2(A,B,C,D,E,13); andre@0: SHA_MIX(14, 11, 6, 0); SHA_RND2(E,A,B,C,D,14); andre@0: SHA_MIX(15, 12, 7, 1); SHA_RND2(D,E,A,B,C,15); andre@0: andre@0: SHA_MIX( 0, 13, 8, 2); SHA_RND2(C,D,E,A,B, 0); andre@0: SHA_MIX( 1, 14, 9, 3); SHA_RND2(B,C,D,E,A, 1); andre@0: SHA_MIX( 2, 15, 10, 4); SHA_RND2(A,B,C,D,E, 2); andre@0: SHA_MIX( 3, 0, 11, 5); SHA_RND2(E,A,B,C,D, 3); andre@0: SHA_MIX( 4, 1, 12, 6); SHA_RND2(D,E,A,B,C, 4); andre@0: SHA_MIX( 5, 2, 13, 7); SHA_RND2(C,D,E,A,B, 5); andre@0: SHA_MIX( 6, 3, 14, 8); SHA_RND2(B,C,D,E,A, 6); andre@0: SHA_MIX( 7, 4, 15, 9); SHA_RND2(A,B,C,D,E, 7); andre@0: andre@0: SHA_MIX( 8, 5, 0, 10); SHA_RND3(E,A,B,C,D, 8); andre@0: SHA_MIX( 9, 6, 1, 11); SHA_RND3(D,E,A,B,C, 9); andre@0: SHA_MIX(10, 7, 2, 12); SHA_RND3(C,D,E,A,B,10); andre@0: SHA_MIX(11, 8, 3, 13); SHA_RND3(B,C,D,E,A,11); andre@0: SHA_MIX(12, 9, 4, 14); SHA_RND3(A,B,C,D,E,12); andre@0: SHA_MIX(13, 10, 5, 15); SHA_RND3(E,A,B,C,D,13); andre@0: SHA_MIX(14, 11, 6, 0); SHA_RND3(D,E,A,B,C,14); andre@0: SHA_MIX(15, 12, 7, 1); SHA_RND3(C,D,E,A,B,15); andre@0: andre@0: SHA_MIX( 0, 13, 8, 2); SHA_RND3(B,C,D,E,A, 0); andre@0: SHA_MIX( 1, 14, 9, 3); SHA_RND3(A,B,C,D,E, 1); andre@0: SHA_MIX( 2, 15, 10, 4); SHA_RND3(E,A,B,C,D, 2); andre@0: SHA_MIX( 3, 0, 11, 5); SHA_RND3(D,E,A,B,C, 3); andre@0: SHA_MIX( 4, 1, 12, 6); SHA_RND3(C,D,E,A,B, 4); andre@0: SHA_MIX( 5, 2, 13, 7); SHA_RND3(B,C,D,E,A, 5); andre@0: SHA_MIX( 6, 3, 14, 8); SHA_RND3(A,B,C,D,E, 6); andre@0: SHA_MIX( 7, 4, 15, 9); SHA_RND3(E,A,B,C,D, 7); andre@0: SHA_MIX( 8, 5, 0, 10); SHA_RND3(D,E,A,B,C, 8); andre@0: SHA_MIX( 9, 6, 1, 11); SHA_RND3(C,D,E,A,B, 9); andre@0: SHA_MIX(10, 7, 2, 12); SHA_RND3(B,C,D,E,A,10); andre@0: SHA_MIX(11, 8, 3, 13); SHA_RND3(A,B,C,D,E,11); andre@0: andre@0: SHA_MIX(12, 9, 4, 14); SHA_RND4(E,A,B,C,D,12); andre@0: SHA_MIX(13, 10, 5, 15); SHA_RND4(D,E,A,B,C,13); andre@0: SHA_MIX(14, 11, 6, 0); SHA_RND4(C,D,E,A,B,14); andre@0: SHA_MIX(15, 12, 7, 1); SHA_RND4(B,C,D,E,A,15); andre@0: andre@0: SHA_MIX( 0, 13, 8, 2); SHA_RND4(A,B,C,D,E, 0); andre@0: SHA_MIX( 1, 14, 9, 3); SHA_RND4(E,A,B,C,D, 1); andre@0: SHA_MIX( 2, 15, 10, 4); SHA_RND4(D,E,A,B,C, 2); andre@0: SHA_MIX( 3, 0, 11, 5); SHA_RND4(C,D,E,A,B, 3); andre@0: SHA_MIX( 4, 1, 12, 6); SHA_RND4(B,C,D,E,A, 4); andre@0: SHA_MIX( 5, 2, 13, 7); SHA_RND4(A,B,C,D,E, 5); andre@0: SHA_MIX( 6, 3, 14, 8); SHA_RND4(E,A,B,C,D, 6); andre@0: SHA_MIX( 7, 4, 15, 9); SHA_RND4(D,E,A,B,C, 7); andre@0: SHA_MIX( 8, 5, 0, 10); SHA_RND4(C,D,E,A,B, 8); andre@0: SHA_MIX( 9, 6, 1, 11); SHA_RND4(B,C,D,E,A, 9); andre@0: SHA_MIX(10, 7, 2, 12); SHA_RND4(A,B,C,D,E,10); andre@0: SHA_MIX(11, 8, 3, 13); SHA_RND4(E,A,B,C,D,11); andre@0: SHA_MIX(12, 9, 4, 14); SHA_RND4(D,E,A,B,C,12); andre@0: SHA_MIX(13, 10, 5, 15); SHA_RND4(C,D,E,A,B,13); andre@0: SHA_MIX(14, 11, 6, 0); SHA_RND4(B,C,D,E,A,14); andre@0: SHA_MIX(15, 12, 7, 1); SHA_RND4(A,B,C,D,E,15); andre@0: andre@0: XH(0) += A; andre@0: XH(1) += B; andre@0: XH(2) += C; andre@0: XH(3) += D; andre@0: XH(4) += E; andre@0: } andre@0: andre@0: /************************************************************************* andre@0: ** Code below this line added to make SHA code support BLAPI interface andre@0: */ andre@0: andre@0: SHA1Context * andre@0: SHA1_NewContext(void) andre@0: { andre@0: SHA1Context *cx; andre@0: andre@0: /* no need to ZNew, SHA1_Begin will init the context */ andre@0: cx = PORT_New(SHA1Context); andre@0: return cx; andre@0: } andre@0: andre@0: /* Zero and free the context */ andre@0: void andre@0: SHA1_DestroyContext(SHA1Context *cx, PRBool freeit) andre@0: { andre@0: memset(cx, 0, sizeof *cx); andre@0: if (freeit) { andre@0: PORT_Free(cx); andre@0: } andre@0: } andre@0: andre@0: SECStatus andre@0: SHA1_HashBuf(unsigned char *dest, const unsigned char *src, PRUint32 src_length) andre@0: { andre@0: SHA1Context ctx; andre@0: unsigned int outLen; andre@0: andre@0: SHA1_Begin(&ctx); andre@0: SHA1_Update(&ctx, src, src_length); andre@0: SHA1_End(&ctx, dest, &outLen, SHA1_LENGTH); andre@0: memset(&ctx, 0, sizeof ctx); andre@0: return SECSuccess; andre@0: } andre@0: andre@0: /* Hash a null-terminated character string. */ andre@0: SECStatus andre@0: SHA1_Hash(unsigned char *dest, const char *src) andre@0: { andre@0: return SHA1_HashBuf(dest, (const unsigned char *)src, PORT_Strlen (src)); andre@0: } andre@0: andre@0: /* andre@0: * need to support save/restore state in pkcs11. Stores all the info necessary andre@0: * for a structure into just a stream of bytes. andre@0: */ andre@0: unsigned int andre@0: SHA1_FlattenSize(SHA1Context *cx) andre@0: { andre@0: return sizeof(SHA1Context); andre@0: } andre@0: andre@0: SECStatus andre@0: SHA1_Flatten(SHA1Context *cx,unsigned char *space) andre@0: { andre@0: PORT_Memcpy(space,cx, sizeof(SHA1Context)); andre@0: return SECSuccess; andre@0: } andre@0: andre@0: SHA1Context * andre@0: SHA1_Resurrect(unsigned char *space,void *arg) andre@0: { andre@0: SHA1Context *cx = SHA1_NewContext(); andre@0: if (cx == NULL) return NULL; andre@0: andre@0: PORT_Memcpy(cx,space, sizeof(SHA1Context)); andre@0: return cx; andre@0: } andre@0: andre@0: void SHA1_Clone(SHA1Context *dest, SHA1Context *src) andre@0: { andre@0: memcpy(dest, src, sizeof *dest); andre@0: } andre@0: andre@0: void andre@0: SHA1_TraceState(SHA1Context *ctx) andre@0: { andre@0: PORT_SetError(PR_NOT_IMPLEMENTED_ERROR); andre@0: }