Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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 | #ifdef FREEBL_NO_DEPEND |
michael@0 | 6 | #include "stubs.h" |
michael@0 | 7 | #endif |
michael@0 | 8 | |
michael@0 | 9 | #include <memory.h> |
michael@0 | 10 | #include "blapi.h" |
michael@0 | 11 | #include "sha_fast.h" |
michael@0 | 12 | #include "prerror.h" |
michael@0 | 13 | |
michael@0 | 14 | #ifdef TRACING_SSL |
michael@0 | 15 | #include "ssl.h" |
michael@0 | 16 | #include "ssltrace.h" |
michael@0 | 17 | #endif |
michael@0 | 18 | |
michael@0 | 19 | static void shaCompress(volatile SHA_HW_t *X, const PRUint32 * datain); |
michael@0 | 20 | |
michael@0 | 21 | #define W u.w |
michael@0 | 22 | #define B u.b |
michael@0 | 23 | |
michael@0 | 24 | |
michael@0 | 25 | #define SHA_F1(X,Y,Z) ((((Y)^(Z))&(X))^(Z)) |
michael@0 | 26 | #define SHA_F2(X,Y,Z) ((X)^(Y)^(Z)) |
michael@0 | 27 | #define SHA_F3(X,Y,Z) (((X)&(Y))|((Z)&((X)|(Y)))) |
michael@0 | 28 | #define SHA_F4(X,Y,Z) ((X)^(Y)^(Z)) |
michael@0 | 29 | |
michael@0 | 30 | #define SHA_MIX(n,a,b,c) XW(n) = SHA_ROTL(XW(a)^XW(b)^XW(c)^XW(n), 1) |
michael@0 | 31 | |
michael@0 | 32 | /* |
michael@0 | 33 | * SHA: initialize context |
michael@0 | 34 | */ |
michael@0 | 35 | void |
michael@0 | 36 | SHA1_Begin(SHA1Context *ctx) |
michael@0 | 37 | { |
michael@0 | 38 | ctx->size = 0; |
michael@0 | 39 | /* |
michael@0 | 40 | * Initialize H with constants from FIPS180-1. |
michael@0 | 41 | */ |
michael@0 | 42 | ctx->H[0] = 0x67452301L; |
michael@0 | 43 | ctx->H[1] = 0xefcdab89L; |
michael@0 | 44 | ctx->H[2] = 0x98badcfeL; |
michael@0 | 45 | ctx->H[3] = 0x10325476L; |
michael@0 | 46 | ctx->H[4] = 0xc3d2e1f0L; |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | /* Explanation of H array and index values: |
michael@0 | 50 | * The context's H array is actually the concatenation of two arrays |
michael@0 | 51 | * defined by SHA1, the H array of state variables (5 elements), |
michael@0 | 52 | * and the W array of intermediate values, of which there are 16 elements. |
michael@0 | 53 | * The W array starts at H[5], that is W[0] is H[5]. |
michael@0 | 54 | * Although these values are defined as 32-bit values, we use 64-bit |
michael@0 | 55 | * variables to hold them because the AMD64 stores 64 bit values in |
michael@0 | 56 | * memory MUCH faster than it stores any smaller values. |
michael@0 | 57 | * |
michael@0 | 58 | * Rather than passing the context structure to shaCompress, we pass |
michael@0 | 59 | * this combined array of H and W values. We do not pass the address |
michael@0 | 60 | * of the first element of this array, but rather pass the address of an |
michael@0 | 61 | * element in the middle of the array, element X. Presently X[0] is H[11]. |
michael@0 | 62 | * So we pass the address of H[11] as the address of array X to shaCompress. |
michael@0 | 63 | * Then shaCompress accesses the members of the array using positive AND |
michael@0 | 64 | * negative indexes. |
michael@0 | 65 | * |
michael@0 | 66 | * Pictorially: (each element is 8 bytes) |
michael@0 | 67 | * H | H0 H1 H2 H3 H4 W0 W1 W2 W3 W4 W5 W6 W7 W8 W9 Wa Wb Wc Wd We Wf | |
michael@0 | 68 | * X |-11-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 | |
michael@0 | 69 | * |
michael@0 | 70 | * The byte offset from X[0] to any member of H and W is always |
michael@0 | 71 | * representable in a signed 8-bit value, which will be encoded |
michael@0 | 72 | * as a single byte offset in the X86-64 instruction set. |
michael@0 | 73 | * If we didn't pass the address of H[11], and instead passed the |
michael@0 | 74 | * address of H[0], the offsets to elements H[16] and above would be |
michael@0 | 75 | * greater than 127, not representable in a signed 8-bit value, and the |
michael@0 | 76 | * x86-64 instruction set would encode every such offset as a 32-bit |
michael@0 | 77 | * signed number in each instruction that accessed element H[16] or |
michael@0 | 78 | * higher. This results in much bigger and slower code. |
michael@0 | 79 | */ |
michael@0 | 80 | #if !defined(SHA_PUT_W_IN_STACK) |
michael@0 | 81 | #define H2X 11 /* X[0] is H[11], and H[0] is X[-11] */ |
michael@0 | 82 | #define W2X 6 /* X[0] is W[6], and W[0] is X[-6] */ |
michael@0 | 83 | #else |
michael@0 | 84 | #define H2X 0 |
michael@0 | 85 | #endif |
michael@0 | 86 | |
michael@0 | 87 | /* |
michael@0 | 88 | * SHA: Add data to context. |
michael@0 | 89 | */ |
michael@0 | 90 | void |
michael@0 | 91 | SHA1_Update(SHA1Context *ctx, const unsigned char *dataIn, unsigned int len) |
michael@0 | 92 | { |
michael@0 | 93 | register unsigned int lenB; |
michael@0 | 94 | register unsigned int togo; |
michael@0 | 95 | |
michael@0 | 96 | if (!len) |
michael@0 | 97 | return; |
michael@0 | 98 | |
michael@0 | 99 | /* accumulate the byte count. */ |
michael@0 | 100 | lenB = (unsigned int)(ctx->size) & 63U; |
michael@0 | 101 | |
michael@0 | 102 | ctx->size += len; |
michael@0 | 103 | |
michael@0 | 104 | /* |
michael@0 | 105 | * Read the data into W and process blocks as they get full |
michael@0 | 106 | */ |
michael@0 | 107 | if (lenB > 0) { |
michael@0 | 108 | togo = 64U - lenB; |
michael@0 | 109 | if (len < togo) |
michael@0 | 110 | togo = len; |
michael@0 | 111 | memcpy(ctx->B + lenB, dataIn, togo); |
michael@0 | 112 | len -= togo; |
michael@0 | 113 | dataIn += togo; |
michael@0 | 114 | lenB = (lenB + togo) & 63U; |
michael@0 | 115 | if (!lenB) { |
michael@0 | 116 | shaCompress(&ctx->H[H2X], ctx->W); |
michael@0 | 117 | } |
michael@0 | 118 | } |
michael@0 | 119 | #if !defined(SHA_ALLOW_UNALIGNED_ACCESS) |
michael@0 | 120 | if ((ptrdiff_t)dataIn % sizeof(PRUint32)) { |
michael@0 | 121 | while (len >= 64U) { |
michael@0 | 122 | memcpy(ctx->B, dataIn, 64); |
michael@0 | 123 | len -= 64U; |
michael@0 | 124 | shaCompress(&ctx->H[H2X], ctx->W); |
michael@0 | 125 | dataIn += 64U; |
michael@0 | 126 | } |
michael@0 | 127 | } else |
michael@0 | 128 | #endif |
michael@0 | 129 | { |
michael@0 | 130 | while (len >= 64U) { |
michael@0 | 131 | len -= 64U; |
michael@0 | 132 | shaCompress(&ctx->H[H2X], (PRUint32 *)dataIn); |
michael@0 | 133 | dataIn += 64U; |
michael@0 | 134 | } |
michael@0 | 135 | } |
michael@0 | 136 | if (len) { |
michael@0 | 137 | memcpy(ctx->B, dataIn, len); |
michael@0 | 138 | } |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | |
michael@0 | 142 | /* |
michael@0 | 143 | * SHA: Generate hash value from context |
michael@0 | 144 | */ |
michael@0 | 145 | void |
michael@0 | 146 | SHA1_End(SHA1Context *ctx, unsigned char *hashout, |
michael@0 | 147 | unsigned int *pDigestLen, unsigned int maxDigestLen) |
michael@0 | 148 | { |
michael@0 | 149 | register PRUint64 size; |
michael@0 | 150 | register PRUint32 lenB; |
michael@0 | 151 | PRUint32 tmpbuf[5]; |
michael@0 | 152 | |
michael@0 | 153 | static const unsigned char bulk_pad[64] = { 0x80,0,0,0,0,0,0,0,0,0, |
michael@0 | 154 | 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 | 155 | 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 | 156 | #define tmp lenB |
michael@0 | 157 | |
michael@0 | 158 | PORT_Assert (maxDigestLen >= SHA1_LENGTH); |
michael@0 | 159 | |
michael@0 | 160 | /* |
michael@0 | 161 | * Pad with a binary 1 (e.g. 0x80), then zeroes, then length in bits |
michael@0 | 162 | */ |
michael@0 | 163 | size = ctx->size; |
michael@0 | 164 | |
michael@0 | 165 | lenB = (PRUint32)size & 63; |
michael@0 | 166 | SHA1_Update(ctx, bulk_pad, (((55+64) - lenB) & 63) + 1); |
michael@0 | 167 | PORT_Assert(((PRUint32)ctx->size & 63) == 56); |
michael@0 | 168 | /* Convert size from bytes to bits. */ |
michael@0 | 169 | size <<= 3; |
michael@0 | 170 | ctx->W[14] = SHA_HTONL((PRUint32)(size >> 32)); |
michael@0 | 171 | ctx->W[15] = SHA_HTONL((PRUint32)size); |
michael@0 | 172 | shaCompress(&ctx->H[H2X], ctx->W); |
michael@0 | 173 | |
michael@0 | 174 | /* |
michael@0 | 175 | * Output hash |
michael@0 | 176 | */ |
michael@0 | 177 | SHA_STORE_RESULT; |
michael@0 | 178 | if (pDigestLen) { |
michael@0 | 179 | *pDigestLen = SHA1_LENGTH; |
michael@0 | 180 | } |
michael@0 | 181 | #undef tmp |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | void |
michael@0 | 185 | SHA1_EndRaw(SHA1Context *ctx, unsigned char *hashout, |
michael@0 | 186 | unsigned int *pDigestLen, unsigned int maxDigestLen) |
michael@0 | 187 | { |
michael@0 | 188 | #if defined(SHA_NEED_TMP_VARIABLE) |
michael@0 | 189 | register PRUint32 tmp; |
michael@0 | 190 | #endif |
michael@0 | 191 | PRUint32 tmpbuf[5]; |
michael@0 | 192 | PORT_Assert (maxDigestLen >= SHA1_LENGTH); |
michael@0 | 193 | |
michael@0 | 194 | SHA_STORE_RESULT; |
michael@0 | 195 | if (pDigestLen) |
michael@0 | 196 | *pDigestLen = SHA1_LENGTH; |
michael@0 | 197 | } |
michael@0 | 198 | |
michael@0 | 199 | #undef B |
michael@0 | 200 | /* |
michael@0 | 201 | * SHA: Compression function, unrolled. |
michael@0 | 202 | * |
michael@0 | 203 | * Some operations in shaCompress are done as 5 groups of 16 operations. |
michael@0 | 204 | * Others are done as 4 groups of 20 operations. |
michael@0 | 205 | * The code below shows that structure. |
michael@0 | 206 | * |
michael@0 | 207 | * The functions that compute the new values of the 5 state variables |
michael@0 | 208 | * A-E are done in 4 groups of 20 operations (or you may also think |
michael@0 | 209 | * of them as being done in 16 groups of 5 operations). They are |
michael@0 | 210 | * done by the SHA_RNDx macros below, in the right column. |
michael@0 | 211 | * |
michael@0 | 212 | * The functions that set the 16 values of the W array are done in |
michael@0 | 213 | * 5 groups of 16 operations. The first group is done by the |
michael@0 | 214 | * LOAD macros below, the latter 4 groups are done by SHA_MIX below, |
michael@0 | 215 | * in the left column. |
michael@0 | 216 | * |
michael@0 | 217 | * gcc's optimizer observes that each member of the W array is assigned |
michael@0 | 218 | * a value 5 times in this code. It reduces the number of store |
michael@0 | 219 | * operations done to the W array in the context (that is, in the X array) |
michael@0 | 220 | * by creating a W array on the stack, and storing the W values there for |
michael@0 | 221 | * the first 4 groups of operations on W, and storing the values in the |
michael@0 | 222 | * context's W array only in the fifth group. This is undesirable. |
michael@0 | 223 | * It is MUCH bigger code than simply using the context's W array, because |
michael@0 | 224 | * all the offsets to the W array in the stack are 32-bit signed offsets, |
michael@0 | 225 | * and it is no faster than storing the values in the context's W array. |
michael@0 | 226 | * |
michael@0 | 227 | * The original code for sha_fast.c prevented this creation of a separate |
michael@0 | 228 | * W array in the stack by creating a W array of 80 members, each of |
michael@0 | 229 | * whose elements is assigned only once. It also separated the computations |
michael@0 | 230 | * of the W array values and the computations of the values for the 5 |
michael@0 | 231 | * state variables into two separate passes, W's, then A-E's so that the |
michael@0 | 232 | * second pass could be done all in registers (except for accessing the W |
michael@0 | 233 | * array) on machines with fewer registers. The method is suboptimal |
michael@0 | 234 | * for machines with enough registers to do it all in one pass, and it |
michael@0 | 235 | * necessitates using many instructions with 32-bit offsets. |
michael@0 | 236 | * |
michael@0 | 237 | * This code eliminates the separate W array on the stack by a completely |
michael@0 | 238 | * different means: by declaring the X array volatile. This prevents |
michael@0 | 239 | * the optimizer from trying to reduce the use of the X array by the |
michael@0 | 240 | * creation of a MORE expensive W array on the stack. The result is |
michael@0 | 241 | * that all instructions use signed 8-bit offsets and not 32-bit offsets. |
michael@0 | 242 | * |
michael@0 | 243 | * The combination of this code and the -O3 optimizer flag on GCC 3.4.3 |
michael@0 | 244 | * results in code that is 3 times faster than the previous NSS sha_fast |
michael@0 | 245 | * code on AMD64. |
michael@0 | 246 | */ |
michael@0 | 247 | static void |
michael@0 | 248 | shaCompress(volatile SHA_HW_t *X, const PRUint32 *inbuf) |
michael@0 | 249 | { |
michael@0 | 250 | register SHA_HW_t A, B, C, D, E; |
michael@0 | 251 | |
michael@0 | 252 | #if defined(SHA_NEED_TMP_VARIABLE) |
michael@0 | 253 | register PRUint32 tmp; |
michael@0 | 254 | #endif |
michael@0 | 255 | |
michael@0 | 256 | #if !defined(SHA_PUT_W_IN_STACK) |
michael@0 | 257 | #define XH(n) X[n-H2X] |
michael@0 | 258 | #define XW(n) X[n-W2X] |
michael@0 | 259 | #else |
michael@0 | 260 | SHA_HW_t w_0, w_1, w_2, w_3, w_4, w_5, w_6, w_7, |
michael@0 | 261 | w_8, w_9, w_10, w_11, w_12, w_13, w_14, w_15; |
michael@0 | 262 | #define XW(n) w_ ## n |
michael@0 | 263 | #define XH(n) X[n] |
michael@0 | 264 | #endif |
michael@0 | 265 | |
michael@0 | 266 | #define K0 0x5a827999L |
michael@0 | 267 | #define K1 0x6ed9eba1L |
michael@0 | 268 | #define K2 0x8f1bbcdcL |
michael@0 | 269 | #define K3 0xca62c1d6L |
michael@0 | 270 | |
michael@0 | 271 | #define SHA_RND1(a,b,c,d,e,n) \ |
michael@0 | 272 | a = SHA_ROTL(b,5)+SHA_F1(c,d,e)+a+XW(n)+K0; c=SHA_ROTL(c,30) |
michael@0 | 273 | #define SHA_RND2(a,b,c,d,e,n) \ |
michael@0 | 274 | a = SHA_ROTL(b,5)+SHA_F2(c,d,e)+a+XW(n)+K1; c=SHA_ROTL(c,30) |
michael@0 | 275 | #define SHA_RND3(a,b,c,d,e,n) \ |
michael@0 | 276 | a = SHA_ROTL(b,5)+SHA_F3(c,d,e)+a+XW(n)+K2; c=SHA_ROTL(c,30) |
michael@0 | 277 | #define SHA_RND4(a,b,c,d,e,n) \ |
michael@0 | 278 | a = SHA_ROTL(b,5)+SHA_F4(c,d,e)+a+XW(n)+K3; c=SHA_ROTL(c,30) |
michael@0 | 279 | |
michael@0 | 280 | #define LOAD(n) XW(n) = SHA_HTONL(inbuf[n]) |
michael@0 | 281 | |
michael@0 | 282 | A = XH(0); |
michael@0 | 283 | B = XH(1); |
michael@0 | 284 | C = XH(2); |
michael@0 | 285 | D = XH(3); |
michael@0 | 286 | E = XH(4); |
michael@0 | 287 | |
michael@0 | 288 | LOAD(0); SHA_RND1(E,A,B,C,D, 0); |
michael@0 | 289 | LOAD(1); SHA_RND1(D,E,A,B,C, 1); |
michael@0 | 290 | LOAD(2); SHA_RND1(C,D,E,A,B, 2); |
michael@0 | 291 | LOAD(3); SHA_RND1(B,C,D,E,A, 3); |
michael@0 | 292 | LOAD(4); SHA_RND1(A,B,C,D,E, 4); |
michael@0 | 293 | LOAD(5); SHA_RND1(E,A,B,C,D, 5); |
michael@0 | 294 | LOAD(6); SHA_RND1(D,E,A,B,C, 6); |
michael@0 | 295 | LOAD(7); SHA_RND1(C,D,E,A,B, 7); |
michael@0 | 296 | LOAD(8); SHA_RND1(B,C,D,E,A, 8); |
michael@0 | 297 | LOAD(9); SHA_RND1(A,B,C,D,E, 9); |
michael@0 | 298 | LOAD(10); SHA_RND1(E,A,B,C,D,10); |
michael@0 | 299 | LOAD(11); SHA_RND1(D,E,A,B,C,11); |
michael@0 | 300 | LOAD(12); SHA_RND1(C,D,E,A,B,12); |
michael@0 | 301 | LOAD(13); SHA_RND1(B,C,D,E,A,13); |
michael@0 | 302 | LOAD(14); SHA_RND1(A,B,C,D,E,14); |
michael@0 | 303 | LOAD(15); SHA_RND1(E,A,B,C,D,15); |
michael@0 | 304 | |
michael@0 | 305 | SHA_MIX( 0, 13, 8, 2); SHA_RND1(D,E,A,B,C, 0); |
michael@0 | 306 | SHA_MIX( 1, 14, 9, 3); SHA_RND1(C,D,E,A,B, 1); |
michael@0 | 307 | SHA_MIX( 2, 15, 10, 4); SHA_RND1(B,C,D,E,A, 2); |
michael@0 | 308 | SHA_MIX( 3, 0, 11, 5); SHA_RND1(A,B,C,D,E, 3); |
michael@0 | 309 | |
michael@0 | 310 | SHA_MIX( 4, 1, 12, 6); SHA_RND2(E,A,B,C,D, 4); |
michael@0 | 311 | SHA_MIX( 5, 2, 13, 7); SHA_RND2(D,E,A,B,C, 5); |
michael@0 | 312 | SHA_MIX( 6, 3, 14, 8); SHA_RND2(C,D,E,A,B, 6); |
michael@0 | 313 | SHA_MIX( 7, 4, 15, 9); SHA_RND2(B,C,D,E,A, 7); |
michael@0 | 314 | SHA_MIX( 8, 5, 0, 10); SHA_RND2(A,B,C,D,E, 8); |
michael@0 | 315 | SHA_MIX( 9, 6, 1, 11); SHA_RND2(E,A,B,C,D, 9); |
michael@0 | 316 | SHA_MIX(10, 7, 2, 12); SHA_RND2(D,E,A,B,C,10); |
michael@0 | 317 | SHA_MIX(11, 8, 3, 13); SHA_RND2(C,D,E,A,B,11); |
michael@0 | 318 | SHA_MIX(12, 9, 4, 14); SHA_RND2(B,C,D,E,A,12); |
michael@0 | 319 | SHA_MIX(13, 10, 5, 15); SHA_RND2(A,B,C,D,E,13); |
michael@0 | 320 | SHA_MIX(14, 11, 6, 0); SHA_RND2(E,A,B,C,D,14); |
michael@0 | 321 | SHA_MIX(15, 12, 7, 1); SHA_RND2(D,E,A,B,C,15); |
michael@0 | 322 | |
michael@0 | 323 | SHA_MIX( 0, 13, 8, 2); SHA_RND2(C,D,E,A,B, 0); |
michael@0 | 324 | SHA_MIX( 1, 14, 9, 3); SHA_RND2(B,C,D,E,A, 1); |
michael@0 | 325 | SHA_MIX( 2, 15, 10, 4); SHA_RND2(A,B,C,D,E, 2); |
michael@0 | 326 | SHA_MIX( 3, 0, 11, 5); SHA_RND2(E,A,B,C,D, 3); |
michael@0 | 327 | SHA_MIX( 4, 1, 12, 6); SHA_RND2(D,E,A,B,C, 4); |
michael@0 | 328 | SHA_MIX( 5, 2, 13, 7); SHA_RND2(C,D,E,A,B, 5); |
michael@0 | 329 | SHA_MIX( 6, 3, 14, 8); SHA_RND2(B,C,D,E,A, 6); |
michael@0 | 330 | SHA_MIX( 7, 4, 15, 9); SHA_RND2(A,B,C,D,E, 7); |
michael@0 | 331 | |
michael@0 | 332 | SHA_MIX( 8, 5, 0, 10); SHA_RND3(E,A,B,C,D, 8); |
michael@0 | 333 | SHA_MIX( 9, 6, 1, 11); SHA_RND3(D,E,A,B,C, 9); |
michael@0 | 334 | SHA_MIX(10, 7, 2, 12); SHA_RND3(C,D,E,A,B,10); |
michael@0 | 335 | SHA_MIX(11, 8, 3, 13); SHA_RND3(B,C,D,E,A,11); |
michael@0 | 336 | SHA_MIX(12, 9, 4, 14); SHA_RND3(A,B,C,D,E,12); |
michael@0 | 337 | SHA_MIX(13, 10, 5, 15); SHA_RND3(E,A,B,C,D,13); |
michael@0 | 338 | SHA_MIX(14, 11, 6, 0); SHA_RND3(D,E,A,B,C,14); |
michael@0 | 339 | SHA_MIX(15, 12, 7, 1); SHA_RND3(C,D,E,A,B,15); |
michael@0 | 340 | |
michael@0 | 341 | SHA_MIX( 0, 13, 8, 2); SHA_RND3(B,C,D,E,A, 0); |
michael@0 | 342 | SHA_MIX( 1, 14, 9, 3); SHA_RND3(A,B,C,D,E, 1); |
michael@0 | 343 | SHA_MIX( 2, 15, 10, 4); SHA_RND3(E,A,B,C,D, 2); |
michael@0 | 344 | SHA_MIX( 3, 0, 11, 5); SHA_RND3(D,E,A,B,C, 3); |
michael@0 | 345 | SHA_MIX( 4, 1, 12, 6); SHA_RND3(C,D,E,A,B, 4); |
michael@0 | 346 | SHA_MIX( 5, 2, 13, 7); SHA_RND3(B,C,D,E,A, 5); |
michael@0 | 347 | SHA_MIX( 6, 3, 14, 8); SHA_RND3(A,B,C,D,E, 6); |
michael@0 | 348 | SHA_MIX( 7, 4, 15, 9); SHA_RND3(E,A,B,C,D, 7); |
michael@0 | 349 | SHA_MIX( 8, 5, 0, 10); SHA_RND3(D,E,A,B,C, 8); |
michael@0 | 350 | SHA_MIX( 9, 6, 1, 11); SHA_RND3(C,D,E,A,B, 9); |
michael@0 | 351 | SHA_MIX(10, 7, 2, 12); SHA_RND3(B,C,D,E,A,10); |
michael@0 | 352 | SHA_MIX(11, 8, 3, 13); SHA_RND3(A,B,C,D,E,11); |
michael@0 | 353 | |
michael@0 | 354 | SHA_MIX(12, 9, 4, 14); SHA_RND4(E,A,B,C,D,12); |
michael@0 | 355 | SHA_MIX(13, 10, 5, 15); SHA_RND4(D,E,A,B,C,13); |
michael@0 | 356 | SHA_MIX(14, 11, 6, 0); SHA_RND4(C,D,E,A,B,14); |
michael@0 | 357 | SHA_MIX(15, 12, 7, 1); SHA_RND4(B,C,D,E,A,15); |
michael@0 | 358 | |
michael@0 | 359 | SHA_MIX( 0, 13, 8, 2); SHA_RND4(A,B,C,D,E, 0); |
michael@0 | 360 | SHA_MIX( 1, 14, 9, 3); SHA_RND4(E,A,B,C,D, 1); |
michael@0 | 361 | SHA_MIX( 2, 15, 10, 4); SHA_RND4(D,E,A,B,C, 2); |
michael@0 | 362 | SHA_MIX( 3, 0, 11, 5); SHA_RND4(C,D,E,A,B, 3); |
michael@0 | 363 | SHA_MIX( 4, 1, 12, 6); SHA_RND4(B,C,D,E,A, 4); |
michael@0 | 364 | SHA_MIX( 5, 2, 13, 7); SHA_RND4(A,B,C,D,E, 5); |
michael@0 | 365 | SHA_MIX( 6, 3, 14, 8); SHA_RND4(E,A,B,C,D, 6); |
michael@0 | 366 | SHA_MIX( 7, 4, 15, 9); SHA_RND4(D,E,A,B,C, 7); |
michael@0 | 367 | SHA_MIX( 8, 5, 0, 10); SHA_RND4(C,D,E,A,B, 8); |
michael@0 | 368 | SHA_MIX( 9, 6, 1, 11); SHA_RND4(B,C,D,E,A, 9); |
michael@0 | 369 | SHA_MIX(10, 7, 2, 12); SHA_RND4(A,B,C,D,E,10); |
michael@0 | 370 | SHA_MIX(11, 8, 3, 13); SHA_RND4(E,A,B,C,D,11); |
michael@0 | 371 | SHA_MIX(12, 9, 4, 14); SHA_RND4(D,E,A,B,C,12); |
michael@0 | 372 | SHA_MIX(13, 10, 5, 15); SHA_RND4(C,D,E,A,B,13); |
michael@0 | 373 | SHA_MIX(14, 11, 6, 0); SHA_RND4(B,C,D,E,A,14); |
michael@0 | 374 | SHA_MIX(15, 12, 7, 1); SHA_RND4(A,B,C,D,E,15); |
michael@0 | 375 | |
michael@0 | 376 | XH(0) += A; |
michael@0 | 377 | XH(1) += B; |
michael@0 | 378 | XH(2) += C; |
michael@0 | 379 | XH(3) += D; |
michael@0 | 380 | XH(4) += E; |
michael@0 | 381 | } |
michael@0 | 382 | |
michael@0 | 383 | /************************************************************************* |
michael@0 | 384 | ** Code below this line added to make SHA code support BLAPI interface |
michael@0 | 385 | */ |
michael@0 | 386 | |
michael@0 | 387 | SHA1Context * |
michael@0 | 388 | SHA1_NewContext(void) |
michael@0 | 389 | { |
michael@0 | 390 | SHA1Context *cx; |
michael@0 | 391 | |
michael@0 | 392 | /* no need to ZNew, SHA1_Begin will init the context */ |
michael@0 | 393 | cx = PORT_New(SHA1Context); |
michael@0 | 394 | return cx; |
michael@0 | 395 | } |
michael@0 | 396 | |
michael@0 | 397 | /* Zero and free the context */ |
michael@0 | 398 | void |
michael@0 | 399 | SHA1_DestroyContext(SHA1Context *cx, PRBool freeit) |
michael@0 | 400 | { |
michael@0 | 401 | memset(cx, 0, sizeof *cx); |
michael@0 | 402 | if (freeit) { |
michael@0 | 403 | PORT_Free(cx); |
michael@0 | 404 | } |
michael@0 | 405 | } |
michael@0 | 406 | |
michael@0 | 407 | SECStatus |
michael@0 | 408 | SHA1_HashBuf(unsigned char *dest, const unsigned char *src, PRUint32 src_length) |
michael@0 | 409 | { |
michael@0 | 410 | SHA1Context ctx; |
michael@0 | 411 | unsigned int outLen; |
michael@0 | 412 | |
michael@0 | 413 | SHA1_Begin(&ctx); |
michael@0 | 414 | SHA1_Update(&ctx, src, src_length); |
michael@0 | 415 | SHA1_End(&ctx, dest, &outLen, SHA1_LENGTH); |
michael@0 | 416 | memset(&ctx, 0, sizeof ctx); |
michael@0 | 417 | return SECSuccess; |
michael@0 | 418 | } |
michael@0 | 419 | |
michael@0 | 420 | /* Hash a null-terminated character string. */ |
michael@0 | 421 | SECStatus |
michael@0 | 422 | SHA1_Hash(unsigned char *dest, const char *src) |
michael@0 | 423 | { |
michael@0 | 424 | return SHA1_HashBuf(dest, (const unsigned char *)src, PORT_Strlen (src)); |
michael@0 | 425 | } |
michael@0 | 426 | |
michael@0 | 427 | /* |
michael@0 | 428 | * need to support save/restore state in pkcs11. Stores all the info necessary |
michael@0 | 429 | * for a structure into just a stream of bytes. |
michael@0 | 430 | */ |
michael@0 | 431 | unsigned int |
michael@0 | 432 | SHA1_FlattenSize(SHA1Context *cx) |
michael@0 | 433 | { |
michael@0 | 434 | return sizeof(SHA1Context); |
michael@0 | 435 | } |
michael@0 | 436 | |
michael@0 | 437 | SECStatus |
michael@0 | 438 | SHA1_Flatten(SHA1Context *cx,unsigned char *space) |
michael@0 | 439 | { |
michael@0 | 440 | PORT_Memcpy(space,cx, sizeof(SHA1Context)); |
michael@0 | 441 | return SECSuccess; |
michael@0 | 442 | } |
michael@0 | 443 | |
michael@0 | 444 | SHA1Context * |
michael@0 | 445 | SHA1_Resurrect(unsigned char *space,void *arg) |
michael@0 | 446 | { |
michael@0 | 447 | SHA1Context *cx = SHA1_NewContext(); |
michael@0 | 448 | if (cx == NULL) return NULL; |
michael@0 | 449 | |
michael@0 | 450 | PORT_Memcpy(cx,space, sizeof(SHA1Context)); |
michael@0 | 451 | return cx; |
michael@0 | 452 | } |
michael@0 | 453 | |
michael@0 | 454 | void SHA1_Clone(SHA1Context *dest, SHA1Context *src) |
michael@0 | 455 | { |
michael@0 | 456 | memcpy(dest, src, sizeof *dest); |
michael@0 | 457 | } |
michael@0 | 458 | |
michael@0 | 459 | void |
michael@0 | 460 | SHA1_TraceState(SHA1Context *ctx) |
michael@0 | 461 | { |
michael@0 | 462 | PORT_SetError(PR_NOT_IMPLEMENTED_ERROR); |
michael@0 | 463 | } |