security/nss/lib/freebl/sha_fast.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/freebl/sha_fast.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,463 @@
     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 +#ifdef FREEBL_NO_DEPEND
     1.9 +#include "stubs.h"
    1.10 +#endif
    1.11 +
    1.12 +#include <memory.h>
    1.13 +#include "blapi.h"
    1.14 +#include "sha_fast.h"
    1.15 +#include "prerror.h"
    1.16 +
    1.17 +#ifdef TRACING_SSL
    1.18 +#include "ssl.h"
    1.19 +#include "ssltrace.h"
    1.20 +#endif
    1.21 +
    1.22 +static void shaCompress(volatile SHA_HW_t *X, const PRUint32 * datain);
    1.23 +
    1.24 +#define W u.w
    1.25 +#define B u.b
    1.26 +
    1.27 +
    1.28 +#define SHA_F1(X,Y,Z) ((((Y)^(Z))&(X))^(Z))
    1.29 +#define SHA_F2(X,Y,Z) ((X)^(Y)^(Z))
    1.30 +#define SHA_F3(X,Y,Z) (((X)&(Y))|((Z)&((X)|(Y))))
    1.31 +#define SHA_F4(X,Y,Z) ((X)^(Y)^(Z))
    1.32 +
    1.33 +#define SHA_MIX(n,a,b,c)    XW(n) = SHA_ROTL(XW(a)^XW(b)^XW(c)^XW(n), 1)
    1.34 +
    1.35 +/*
    1.36 + *  SHA: initialize context
    1.37 + */
    1.38 +void 
    1.39 +SHA1_Begin(SHA1Context *ctx)
    1.40 +{
    1.41 +  ctx->size = 0;
    1.42 +  /*
    1.43 +   *  Initialize H with constants from FIPS180-1.
    1.44 +   */
    1.45 +  ctx->H[0] = 0x67452301L;
    1.46 +  ctx->H[1] = 0xefcdab89L;
    1.47 +  ctx->H[2] = 0x98badcfeL;
    1.48 +  ctx->H[3] = 0x10325476L;
    1.49 +  ctx->H[4] = 0xc3d2e1f0L;
    1.50 +}
    1.51 +
    1.52 +/* Explanation of H array and index values:
    1.53 + * The context's H array is actually the concatenation of two arrays 
    1.54 + * defined by SHA1, the H array of state variables (5 elements),
    1.55 + * and the W array of intermediate values, of which there are 16 elements.
    1.56 + * The W array starts at H[5], that is W[0] is H[5].
    1.57 + * Although these values are defined as 32-bit values, we use 64-bit
    1.58 + * variables to hold them because the AMD64 stores 64 bit values in
    1.59 + * memory MUCH faster than it stores any smaller values.
    1.60 + *
    1.61 + * Rather than passing the context structure to shaCompress, we pass
    1.62 + * this combined array of H and W values.  We do not pass the address
    1.63 + * of the first element of this array, but rather pass the address of an
    1.64 + * element in the middle of the array, element X.  Presently X[0] is H[11].
    1.65 + * So we pass the address of H[11] as the address of array X to shaCompress.
    1.66 + * Then shaCompress accesses the members of the array using positive AND 
    1.67 + * negative indexes.  
    1.68 + *
    1.69 + * Pictorially: (each element is 8 bytes)
    1.70 + * H | H0 H1 H2 H3 H4 W0 W1 W2 W3 W4 W5 W6 W7 W8 W9 Wa Wb Wc Wd We Wf |
    1.71 + * X |-11-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 |
    1.72 + * 
    1.73 + * The byte offset from X[0] to any member of H and W is always 
    1.74 + * representable in a signed 8-bit value, which will be encoded 
    1.75 + * as a single byte offset in the X86-64 instruction set.  
    1.76 + * If we didn't pass the address of H[11], and instead passed the 
    1.77 + * address of H[0], the offsets to elements H[16] and above would be
    1.78 + * greater than 127, not representable in a signed 8-bit value, and the 
    1.79 + * x86-64 instruction set would encode every such offset as a 32-bit 
    1.80 + * signed number in each instruction that accessed element H[16] or 
    1.81 + * higher.  This results in much bigger and slower code. 
    1.82 + */
    1.83 +#if !defined(SHA_PUT_W_IN_STACK)
    1.84 +#define H2X 11 /* X[0] is H[11], and H[0] is X[-11] */
    1.85 +#define W2X  6 /* X[0] is W[6],  and W[0] is X[-6]  */
    1.86 +#else
    1.87 +#define H2X 0
    1.88 +#endif
    1.89 +
    1.90 +/*
    1.91 + *  SHA: Add data to context.
    1.92 + */
    1.93 +void 
    1.94 +SHA1_Update(SHA1Context *ctx, const unsigned char *dataIn, unsigned int len) 
    1.95 +{
    1.96 +  register unsigned int lenB;
    1.97 +  register unsigned int togo;
    1.98 +
    1.99 +  if (!len)
   1.100 +    return;
   1.101 +
   1.102 +  /* accumulate the byte count. */
   1.103 +  lenB = (unsigned int)(ctx->size) & 63U;
   1.104 +
   1.105 +  ctx->size += len;
   1.106 +
   1.107 +  /*
   1.108 +   *  Read the data into W and process blocks as they get full
   1.109 +   */
   1.110 +  if (lenB > 0) {
   1.111 +    togo = 64U - lenB;
   1.112 +    if (len < togo)
   1.113 +      togo = len;
   1.114 +    memcpy(ctx->B + lenB, dataIn, togo);
   1.115 +    len    -= togo;
   1.116 +    dataIn += togo;
   1.117 +    lenB    = (lenB + togo) & 63U;
   1.118 +    if (!lenB) {
   1.119 +      shaCompress(&ctx->H[H2X], ctx->W);
   1.120 +    }
   1.121 +  }
   1.122 +#if !defined(SHA_ALLOW_UNALIGNED_ACCESS)
   1.123 +  if ((ptrdiff_t)dataIn % sizeof(PRUint32)) {
   1.124 +    while (len >= 64U) {
   1.125 +      memcpy(ctx->B, dataIn, 64);
   1.126 +      len    -= 64U;
   1.127 +      shaCompress(&ctx->H[H2X], ctx->W);
   1.128 +      dataIn += 64U;
   1.129 +    }
   1.130 +  } else 
   1.131 +#endif
   1.132 +  {
   1.133 +    while (len >= 64U) {
   1.134 +      len    -= 64U;
   1.135 +      shaCompress(&ctx->H[H2X], (PRUint32 *)dataIn);
   1.136 +      dataIn += 64U;
   1.137 +    }
   1.138 +  }
   1.139 +  if (len) {
   1.140 +    memcpy(ctx->B, dataIn, len);
   1.141 +  }
   1.142 +}
   1.143 +
   1.144 +
   1.145 +/*
   1.146 + *  SHA: Generate hash value from context
   1.147 + */
   1.148 +void 
   1.149 +SHA1_End(SHA1Context *ctx, unsigned char *hashout,
   1.150 +         unsigned int *pDigestLen, unsigned int maxDigestLen)
   1.151 +{
   1.152 +  register PRUint64 size;
   1.153 +  register PRUint32 lenB;
   1.154 +  PRUint32 tmpbuf[5];
   1.155 +
   1.156 +  static const unsigned char bulk_pad[64] = { 0x80,0,0,0,0,0,0,0,0,0,
   1.157 +          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.158 +          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.159 +#define tmp lenB
   1.160 +
   1.161 +  PORT_Assert (maxDigestLen >= SHA1_LENGTH);
   1.162 +
   1.163 +  /*
   1.164 +   *  Pad with a binary 1 (e.g. 0x80), then zeroes, then length in bits
   1.165 +   */
   1.166 +  size = ctx->size;
   1.167 +
   1.168 +  lenB = (PRUint32)size & 63;
   1.169 +  SHA1_Update(ctx, bulk_pad, (((55+64) - lenB) & 63) + 1);
   1.170 +  PORT_Assert(((PRUint32)ctx->size & 63) == 56);
   1.171 +  /* Convert size from bytes to bits. */
   1.172 +  size <<= 3;
   1.173 +  ctx->W[14] = SHA_HTONL((PRUint32)(size >> 32));
   1.174 +  ctx->W[15] = SHA_HTONL((PRUint32)size);
   1.175 +  shaCompress(&ctx->H[H2X], ctx->W);
   1.176 +
   1.177 +  /*
   1.178 +   *  Output hash
   1.179 +   */
   1.180 +  SHA_STORE_RESULT;
   1.181 +  if (pDigestLen) {
   1.182 +    *pDigestLen = SHA1_LENGTH;
   1.183 +  }
   1.184 +#undef tmp
   1.185 +}
   1.186 +
   1.187 +void
   1.188 +SHA1_EndRaw(SHA1Context *ctx, unsigned char *hashout,
   1.189 +            unsigned int *pDigestLen, unsigned int maxDigestLen)
   1.190 +{
   1.191 +#if defined(SHA_NEED_TMP_VARIABLE)
   1.192 +  register PRUint32 tmp;
   1.193 +#endif
   1.194 +  PRUint32 tmpbuf[5];
   1.195 +  PORT_Assert (maxDigestLen >= SHA1_LENGTH);
   1.196 +
   1.197 +  SHA_STORE_RESULT;
   1.198 +  if (pDigestLen)
   1.199 +    *pDigestLen = SHA1_LENGTH;
   1.200 +}
   1.201 +
   1.202 +#undef B
   1.203 +/*
   1.204 + *  SHA: Compression function, unrolled.
   1.205 + *
   1.206 + * Some operations in shaCompress are done as 5 groups of 16 operations.
   1.207 + * Others are done as 4 groups of 20 operations.
   1.208 + * The code below shows that structure.
   1.209 + *
   1.210 + * The functions that compute the new values of the 5 state variables
   1.211 + * A-E are done in 4 groups of 20 operations (or you may also think
   1.212 + * of them as being done in 16 groups of 5 operations).  They are
   1.213 + * done by the SHA_RNDx macros below, in the right column.
   1.214 + *
   1.215 + * The functions that set the 16 values of the W array are done in 
   1.216 + * 5 groups of 16 operations.  The first group is done by the 
   1.217 + * LOAD macros below, the latter 4 groups are done by SHA_MIX below,
   1.218 + * in the left column.
   1.219 + *
   1.220 + * gcc's optimizer observes that each member of the W array is assigned
   1.221 + * a value 5 times in this code.  It reduces the number of store 
   1.222 + * operations done to the W array in the context (that is, in the X array)
   1.223 + * by creating a W array on the stack, and storing the W values there for 
   1.224 + * the first 4 groups of operations on W, and storing the values in the 
   1.225 + * context's W array only in the fifth group.  This is undesirable.
   1.226 + * It is MUCH bigger code than simply using the context's W array, because 
   1.227 + * all the offsets to the W array in the stack are 32-bit signed offsets, 
   1.228 + * and it is no faster than storing the values in the context's W array. 
   1.229 + *
   1.230 + * The original code for sha_fast.c prevented this creation of a separate 
   1.231 + * W array in the stack by creating a W array of 80 members, each of
   1.232 + * whose elements is assigned only once. It also separated the computations
   1.233 + * of the W array values and the computations of the values for the 5
   1.234 + * state variables into two separate passes, W's, then A-E's so that the 
   1.235 + * second pass could be done all in registers (except for accessing the W
   1.236 + * array) on machines with fewer registers.  The method is suboptimal
   1.237 + * for machines with enough registers to do it all in one pass, and it
   1.238 + * necessitates using many instructions with 32-bit offsets.
   1.239 + *
   1.240 + * This code eliminates the separate W array on the stack by a completely
   1.241 + * different means: by declaring the X array volatile.  This prevents
   1.242 + * the optimizer from trying to reduce the use of the X array by the
   1.243 + * creation of a MORE expensive W array on the stack. The result is
   1.244 + * that all instructions use signed 8-bit offsets and not 32-bit offsets.
   1.245 + *
   1.246 + * The combination of this code and the -O3 optimizer flag on GCC 3.4.3
   1.247 + * results in code that is 3 times faster than the previous NSS sha_fast
   1.248 + * code on AMD64.
   1.249 + */
   1.250 +static void 
   1.251 +shaCompress(volatile SHA_HW_t *X, const PRUint32 *inbuf) 
   1.252 +{
   1.253 +  register SHA_HW_t A, B, C, D, E;
   1.254 +
   1.255 +#if defined(SHA_NEED_TMP_VARIABLE)
   1.256 +  register PRUint32 tmp;
   1.257 +#endif
   1.258 +
   1.259 +#if !defined(SHA_PUT_W_IN_STACK)
   1.260 +#define XH(n) X[n-H2X]
   1.261 +#define XW(n) X[n-W2X]
   1.262 +#else
   1.263 +  SHA_HW_t w_0, w_1, w_2, w_3, w_4, w_5, w_6, w_7,
   1.264 +           w_8, w_9, w_10, w_11, w_12, w_13, w_14, w_15;
   1.265 +#define XW(n) w_ ## n
   1.266 +#define XH(n) X[n]
   1.267 +#endif
   1.268 +
   1.269 +#define K0 0x5a827999L
   1.270 +#define K1 0x6ed9eba1L
   1.271 +#define K2 0x8f1bbcdcL
   1.272 +#define K3 0xca62c1d6L
   1.273 +
   1.274 +#define SHA_RND1(a,b,c,d,e,n) \
   1.275 +  a = SHA_ROTL(b,5)+SHA_F1(c,d,e)+a+XW(n)+K0; c=SHA_ROTL(c,30) 
   1.276 +#define SHA_RND2(a,b,c,d,e,n) \
   1.277 +  a = SHA_ROTL(b,5)+SHA_F2(c,d,e)+a+XW(n)+K1; c=SHA_ROTL(c,30) 
   1.278 +#define SHA_RND3(a,b,c,d,e,n) \
   1.279 +  a = SHA_ROTL(b,5)+SHA_F3(c,d,e)+a+XW(n)+K2; c=SHA_ROTL(c,30) 
   1.280 +#define SHA_RND4(a,b,c,d,e,n) \
   1.281 +  a = SHA_ROTL(b,5)+SHA_F4(c,d,e)+a+XW(n)+K3; c=SHA_ROTL(c,30) 
   1.282 +
   1.283 +#define LOAD(n) XW(n) = SHA_HTONL(inbuf[n])
   1.284 +
   1.285 +  A = XH(0);
   1.286 +  B = XH(1);
   1.287 +  C = XH(2);
   1.288 +  D = XH(3);
   1.289 +  E = XH(4);
   1.290 +
   1.291 +  LOAD(0);		   SHA_RND1(E,A,B,C,D, 0);
   1.292 +  LOAD(1);		   SHA_RND1(D,E,A,B,C, 1);
   1.293 +  LOAD(2);		   SHA_RND1(C,D,E,A,B, 2);
   1.294 +  LOAD(3);		   SHA_RND1(B,C,D,E,A, 3);
   1.295 +  LOAD(4);		   SHA_RND1(A,B,C,D,E, 4);
   1.296 +  LOAD(5);		   SHA_RND1(E,A,B,C,D, 5);
   1.297 +  LOAD(6);		   SHA_RND1(D,E,A,B,C, 6);
   1.298 +  LOAD(7);		   SHA_RND1(C,D,E,A,B, 7);
   1.299 +  LOAD(8);		   SHA_RND1(B,C,D,E,A, 8);
   1.300 +  LOAD(9);		   SHA_RND1(A,B,C,D,E, 9);
   1.301 +  LOAD(10);		   SHA_RND1(E,A,B,C,D,10);
   1.302 +  LOAD(11);		   SHA_RND1(D,E,A,B,C,11);
   1.303 +  LOAD(12);		   SHA_RND1(C,D,E,A,B,12);
   1.304 +  LOAD(13);		   SHA_RND1(B,C,D,E,A,13);
   1.305 +  LOAD(14);		   SHA_RND1(A,B,C,D,E,14);
   1.306 +  LOAD(15);		   SHA_RND1(E,A,B,C,D,15);
   1.307 +
   1.308 +  SHA_MIX( 0, 13,  8,  2); SHA_RND1(D,E,A,B,C, 0);
   1.309 +  SHA_MIX( 1, 14,  9,  3); SHA_RND1(C,D,E,A,B, 1);
   1.310 +  SHA_MIX( 2, 15, 10,  4); SHA_RND1(B,C,D,E,A, 2);
   1.311 +  SHA_MIX( 3,  0, 11,  5); SHA_RND1(A,B,C,D,E, 3);
   1.312 +
   1.313 +  SHA_MIX( 4,  1, 12,  6); SHA_RND2(E,A,B,C,D, 4);
   1.314 +  SHA_MIX( 5,  2, 13,  7); SHA_RND2(D,E,A,B,C, 5);
   1.315 +  SHA_MIX( 6,  3, 14,  8); SHA_RND2(C,D,E,A,B, 6);
   1.316 +  SHA_MIX( 7,  4, 15,  9); SHA_RND2(B,C,D,E,A, 7);
   1.317 +  SHA_MIX( 8,  5,  0, 10); SHA_RND2(A,B,C,D,E, 8);
   1.318 +  SHA_MIX( 9,  6,  1, 11); SHA_RND2(E,A,B,C,D, 9);
   1.319 +  SHA_MIX(10,  7,  2, 12); SHA_RND2(D,E,A,B,C,10);
   1.320 +  SHA_MIX(11,  8,  3, 13); SHA_RND2(C,D,E,A,B,11);
   1.321 +  SHA_MIX(12,  9,  4, 14); SHA_RND2(B,C,D,E,A,12);
   1.322 +  SHA_MIX(13, 10,  5, 15); SHA_RND2(A,B,C,D,E,13);
   1.323 +  SHA_MIX(14, 11,  6,  0); SHA_RND2(E,A,B,C,D,14);
   1.324 +  SHA_MIX(15, 12,  7,  1); SHA_RND2(D,E,A,B,C,15);
   1.325 +
   1.326 +  SHA_MIX( 0, 13,  8,  2); SHA_RND2(C,D,E,A,B, 0);
   1.327 +  SHA_MIX( 1, 14,  9,  3); SHA_RND2(B,C,D,E,A, 1);
   1.328 +  SHA_MIX( 2, 15, 10,  4); SHA_RND2(A,B,C,D,E, 2);
   1.329 +  SHA_MIX( 3,  0, 11,  5); SHA_RND2(E,A,B,C,D, 3);
   1.330 +  SHA_MIX( 4,  1, 12,  6); SHA_RND2(D,E,A,B,C, 4);
   1.331 +  SHA_MIX( 5,  2, 13,  7); SHA_RND2(C,D,E,A,B, 5);
   1.332 +  SHA_MIX( 6,  3, 14,  8); SHA_RND2(B,C,D,E,A, 6);
   1.333 +  SHA_MIX( 7,  4, 15,  9); SHA_RND2(A,B,C,D,E, 7);
   1.334 +
   1.335 +  SHA_MIX( 8,  5,  0, 10); SHA_RND3(E,A,B,C,D, 8);
   1.336 +  SHA_MIX( 9,  6,  1, 11); SHA_RND3(D,E,A,B,C, 9);
   1.337 +  SHA_MIX(10,  7,  2, 12); SHA_RND3(C,D,E,A,B,10);
   1.338 +  SHA_MIX(11,  8,  3, 13); SHA_RND3(B,C,D,E,A,11);
   1.339 +  SHA_MIX(12,  9,  4, 14); SHA_RND3(A,B,C,D,E,12);
   1.340 +  SHA_MIX(13, 10,  5, 15); SHA_RND3(E,A,B,C,D,13);
   1.341 +  SHA_MIX(14, 11,  6,  0); SHA_RND3(D,E,A,B,C,14);
   1.342 +  SHA_MIX(15, 12,  7,  1); SHA_RND3(C,D,E,A,B,15);
   1.343 +
   1.344 +  SHA_MIX( 0, 13,  8,  2); SHA_RND3(B,C,D,E,A, 0);
   1.345 +  SHA_MIX( 1, 14,  9,  3); SHA_RND3(A,B,C,D,E, 1);
   1.346 +  SHA_MIX( 2, 15, 10,  4); SHA_RND3(E,A,B,C,D, 2);
   1.347 +  SHA_MIX( 3,  0, 11,  5); SHA_RND3(D,E,A,B,C, 3);
   1.348 +  SHA_MIX( 4,  1, 12,  6); SHA_RND3(C,D,E,A,B, 4);
   1.349 +  SHA_MIX( 5,  2, 13,  7); SHA_RND3(B,C,D,E,A, 5);
   1.350 +  SHA_MIX( 6,  3, 14,  8); SHA_RND3(A,B,C,D,E, 6);
   1.351 +  SHA_MIX( 7,  4, 15,  9); SHA_RND3(E,A,B,C,D, 7);
   1.352 +  SHA_MIX( 8,  5,  0, 10); SHA_RND3(D,E,A,B,C, 8);
   1.353 +  SHA_MIX( 9,  6,  1, 11); SHA_RND3(C,D,E,A,B, 9);
   1.354 +  SHA_MIX(10,  7,  2, 12); SHA_RND3(B,C,D,E,A,10);
   1.355 +  SHA_MIX(11,  8,  3, 13); SHA_RND3(A,B,C,D,E,11);
   1.356 +
   1.357 +  SHA_MIX(12,  9,  4, 14); SHA_RND4(E,A,B,C,D,12);
   1.358 +  SHA_MIX(13, 10,  5, 15); SHA_RND4(D,E,A,B,C,13);
   1.359 +  SHA_MIX(14, 11,  6,  0); SHA_RND4(C,D,E,A,B,14);
   1.360 +  SHA_MIX(15, 12,  7,  1); SHA_RND4(B,C,D,E,A,15);
   1.361 +
   1.362 +  SHA_MIX( 0, 13,  8,  2); SHA_RND4(A,B,C,D,E, 0);
   1.363 +  SHA_MIX( 1, 14,  9,  3); SHA_RND4(E,A,B,C,D, 1);
   1.364 +  SHA_MIX( 2, 15, 10,  4); SHA_RND4(D,E,A,B,C, 2);
   1.365 +  SHA_MIX( 3,  0, 11,  5); SHA_RND4(C,D,E,A,B, 3);
   1.366 +  SHA_MIX( 4,  1, 12,  6); SHA_RND4(B,C,D,E,A, 4);
   1.367 +  SHA_MIX( 5,  2, 13,  7); SHA_RND4(A,B,C,D,E, 5);
   1.368 +  SHA_MIX( 6,  3, 14,  8); SHA_RND4(E,A,B,C,D, 6);
   1.369 +  SHA_MIX( 7,  4, 15,  9); SHA_RND4(D,E,A,B,C, 7);
   1.370 +  SHA_MIX( 8,  5,  0, 10); SHA_RND4(C,D,E,A,B, 8);
   1.371 +  SHA_MIX( 9,  6,  1, 11); SHA_RND4(B,C,D,E,A, 9);
   1.372 +  SHA_MIX(10,  7,  2, 12); SHA_RND4(A,B,C,D,E,10);
   1.373 +  SHA_MIX(11,  8,  3, 13); SHA_RND4(E,A,B,C,D,11);
   1.374 +  SHA_MIX(12,  9,  4, 14); SHA_RND4(D,E,A,B,C,12);
   1.375 +  SHA_MIX(13, 10,  5, 15); SHA_RND4(C,D,E,A,B,13);
   1.376 +  SHA_MIX(14, 11,  6,  0); SHA_RND4(B,C,D,E,A,14);
   1.377 +  SHA_MIX(15, 12,  7,  1); SHA_RND4(A,B,C,D,E,15);
   1.378 +
   1.379 +  XH(0) += A;
   1.380 +  XH(1) += B;
   1.381 +  XH(2) += C;
   1.382 +  XH(3) += D;
   1.383 +  XH(4) += E;
   1.384 +}
   1.385 +
   1.386 +/*************************************************************************
   1.387 +** Code below this line added to make SHA code support BLAPI interface
   1.388 +*/
   1.389 +
   1.390 +SHA1Context *
   1.391 +SHA1_NewContext(void)
   1.392 +{
   1.393 +    SHA1Context *cx;
   1.394 +
   1.395 +    /* no need to ZNew, SHA1_Begin will init the context */
   1.396 +    cx = PORT_New(SHA1Context);
   1.397 +    return cx;
   1.398 +}
   1.399 +
   1.400 +/* Zero and free the context */
   1.401 +void
   1.402 +SHA1_DestroyContext(SHA1Context *cx, PRBool freeit)
   1.403 +{
   1.404 +    memset(cx, 0, sizeof *cx);
   1.405 +    if (freeit) {
   1.406 +        PORT_Free(cx);
   1.407 +    }
   1.408 +}
   1.409 +
   1.410 +SECStatus
   1.411 +SHA1_HashBuf(unsigned char *dest, const unsigned char *src, PRUint32 src_length)
   1.412 +{
   1.413 +    SHA1Context ctx;
   1.414 +    unsigned int outLen;
   1.415 +
   1.416 +    SHA1_Begin(&ctx);
   1.417 +    SHA1_Update(&ctx, src, src_length);
   1.418 +    SHA1_End(&ctx, dest, &outLen, SHA1_LENGTH);
   1.419 +    memset(&ctx, 0, sizeof ctx);
   1.420 +    return SECSuccess;
   1.421 +}
   1.422 +
   1.423 +/* Hash a null-terminated character string. */
   1.424 +SECStatus
   1.425 +SHA1_Hash(unsigned char *dest, const char *src)
   1.426 +{
   1.427 +    return SHA1_HashBuf(dest, (const unsigned char *)src, PORT_Strlen (src));
   1.428 +}
   1.429 +
   1.430 +/*
   1.431 + * need to support save/restore state in pkcs11. Stores all the info necessary
   1.432 + * for a structure into just a stream of bytes.
   1.433 + */
   1.434 +unsigned int
   1.435 +SHA1_FlattenSize(SHA1Context *cx)
   1.436 +{
   1.437 +    return sizeof(SHA1Context);
   1.438 +}
   1.439 +
   1.440 +SECStatus
   1.441 +SHA1_Flatten(SHA1Context *cx,unsigned char *space)
   1.442 +{
   1.443 +    PORT_Memcpy(space,cx, sizeof(SHA1Context));
   1.444 +    return SECSuccess;
   1.445 +}
   1.446 +
   1.447 +SHA1Context *
   1.448 +SHA1_Resurrect(unsigned char *space,void *arg)
   1.449 +{
   1.450 +    SHA1Context *cx = SHA1_NewContext();
   1.451 +    if (cx == NULL) return NULL;
   1.452 +
   1.453 +    PORT_Memcpy(cx,space, sizeof(SHA1Context));
   1.454 +    return cx;
   1.455 +}
   1.456 +
   1.457 +void SHA1_Clone(SHA1Context *dest, SHA1Context *src) 
   1.458 +{
   1.459 +    memcpy(dest, src, sizeof *dest);
   1.460 +}
   1.461 +
   1.462 +void
   1.463 +SHA1_TraceState(SHA1Context *ctx)
   1.464 +{
   1.465 +    PORT_SetError(PR_NOT_IMPLEMENTED_ERROR);
   1.466 +}

mercurial