andre@0: /* andre@0: * sha512.c - implementation of SHA224, SHA256, SHA384 and SHA512 andre@0: * 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 "prcpucfg.h" andre@0: #if defined(NSS_X86) || defined(SHA_NO_LONG_LONG) andre@0: #define NOUNROLL512 1 andre@0: #undef HAVE_LONG_LONG andre@0: #endif andre@0: #include "prtypes.h" /* for PRUintXX */ andre@0: #include "prlong.h" andre@0: #include "secport.h" /* for PORT_XXX */ andre@0: #include "blapi.h" andre@0: #include "sha256.h" /* for struct SHA256ContextStr */ andre@0: andre@0: /* ============= Common constants and defines ======================= */ andre@0: andre@0: #define W ctx->u.w andre@0: #define B ctx->u.b andre@0: #define H ctx->h andre@0: andre@0: #define SHR(x,n) (x >> n) andre@0: #define SHL(x,n) (x << n) andre@0: #define Ch(x,y,z) ((x & y) ^ (~x & z)) andre@0: #define Maj(x,y,z) ((x & y) ^ (x & z) ^ (y & z)) andre@0: #define SHA_MIN(a,b) (a < b ? a : b) andre@0: andre@0: /* Padding used with all flavors of SHA */ andre@0: static const PRUint8 pad[240] = { andre@0: 0x80,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,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,0,0,0,0,0 andre@0: /* compiler will fill the rest in with zeros */ andre@0: }; andre@0: andre@0: /* ============= SHA256 implementation ================================== */ andre@0: andre@0: /* SHA-256 constants, K256. */ andre@0: static const PRUint32 K256[64] = { andre@0: 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, andre@0: 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, andre@0: 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, andre@0: 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, andre@0: 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, andre@0: 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, andre@0: 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, andre@0: 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, andre@0: 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, andre@0: 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, andre@0: 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, andre@0: 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, andre@0: 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, andre@0: 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, andre@0: 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, andre@0: 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 andre@0: }; andre@0: andre@0: /* SHA-256 initial hash values */ andre@0: static const PRUint32 H256[8] = { andre@0: 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, andre@0: 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 andre@0: }; andre@0: andre@0: #if (_MSC_VER >= 1300) andre@0: #include andre@0: #pragma intrinsic(_byteswap_ulong) andre@0: #define SHA_HTONL(x) _byteswap_ulong(x) andre@0: #define BYTESWAP4(x) x = SHA_HTONL(x) andre@0: #elif defined(_MSC_VER) && defined(NSS_X86_OR_X64) andre@0: #ifndef FORCEINLINE andre@0: #if (_MSC_VER >= 1200) andre@0: #define FORCEINLINE __forceinline andre@0: #else andre@0: #define FORCEINLINE __inline andre@0: #endif andre@0: #endif andre@0: #define FASTCALL __fastcall andre@0: andre@0: static FORCEINLINE PRUint32 FASTCALL andre@0: swap4b(PRUint32 dwd) andre@0: { andre@0: __asm { andre@0: mov eax,dwd andre@0: bswap eax andre@0: } andre@0: } andre@0: andre@0: #define SHA_HTONL(x) swap4b(x) andre@0: #define BYTESWAP4(x) x = SHA_HTONL(x) andre@0: andre@0: #elif defined(__GNUC__) && defined(NSS_X86_OR_X64) andre@0: static __inline__ PRUint32 swap4b(PRUint32 value) andre@0: { andre@0: __asm__("bswap %0" : "+r" (value)); andre@0: return (value); andre@0: } andre@0: #define SHA_HTONL(x) swap4b(x) andre@0: #define BYTESWAP4(x) x = SHA_HTONL(x) andre@0: andre@0: #elif defined(__GNUC__) && (defined(__thumb2__) || \ andre@0: (!defined(__thumb__) && \ andre@0: (defined(__ARM_ARCH_6__) || \ andre@0: defined(__ARM_ARCH_6J__) || \ andre@0: defined(__ARM_ARCH_6K__) || \ andre@0: defined(__ARM_ARCH_6Z__) || \ andre@0: defined(__ARM_ARCH_6ZK__) || \ andre@0: defined(__ARM_ARCH_6T2__) || \ andre@0: defined(__ARM_ARCH_7__) || \ andre@0: defined(__ARM_ARCH_7A__) || \ andre@0: defined(__ARM_ARCH_7R__)))) andre@0: static __inline__ PRUint32 swap4b(PRUint32 value) andre@0: { andre@0: PRUint32 ret; andre@0: __asm__("rev %0, %1" : "=r" (ret) : "r"(value)); andre@0: return ret; andre@0: } andre@0: #define SHA_HTONL(x) swap4b(x) andre@0: #define BYTESWAP4(x) x = SHA_HTONL(x) andre@0: andre@0: #else andre@0: #define SWAP4MASK 0x00FF00FF andre@0: #define SHA_HTONL(x) (t1 = (x), t1 = (t1 << 16) | (t1 >> 16), \ andre@0: ((t1 & SWAP4MASK) << 8) | ((t1 >> 8) & SWAP4MASK)) andre@0: #define BYTESWAP4(x) x = SHA_HTONL(x) andre@0: #endif andre@0: andre@0: #if defined(_MSC_VER) andre@0: #pragma intrinsic (_lrotr, _lrotl) andre@0: #define ROTR32(x,n) _lrotr(x,n) andre@0: #define ROTL32(x,n) _lrotl(x,n) andre@0: #else andre@0: #define ROTR32(x,n) ((x >> n) | (x << ((8 * sizeof x) - n))) andre@0: #define ROTL32(x,n) ((x << n) | (x >> ((8 * sizeof x) - n))) andre@0: #endif andre@0: andre@0: /* Capitol Sigma and lower case sigma functions */ andre@0: #define S0(x) (ROTR32(x, 2) ^ ROTR32(x,13) ^ ROTR32(x,22)) andre@0: #define S1(x) (ROTR32(x, 6) ^ ROTR32(x,11) ^ ROTR32(x,25)) andre@0: #define s0(x) (t1 = x, ROTR32(t1, 7) ^ ROTR32(t1,18) ^ SHR(t1, 3)) andre@0: #define s1(x) (t2 = x, ROTR32(t2,17) ^ ROTR32(t2,19) ^ SHR(t2,10)) andre@0: andre@0: SHA256Context * andre@0: SHA256_NewContext(void) andre@0: { andre@0: SHA256Context *ctx = PORT_New(SHA256Context); andre@0: return ctx; andre@0: } andre@0: andre@0: void andre@0: SHA256_DestroyContext(SHA256Context *ctx, PRBool freeit) andre@0: { andre@0: memset(ctx, 0, sizeof *ctx); andre@0: if (freeit) { andre@0: PORT_Free(ctx); andre@0: } andre@0: } andre@0: andre@0: void andre@0: SHA256_Begin(SHA256Context *ctx) andre@0: { andre@0: memset(ctx, 0, sizeof *ctx); andre@0: memcpy(H, H256, sizeof H256); andre@0: } andre@0: andre@0: static void andre@0: SHA256_Compress(SHA256Context *ctx) andre@0: { andre@0: { andre@0: register PRUint32 t1, t2; andre@0: andre@0: #if defined(IS_LITTLE_ENDIAN) andre@0: BYTESWAP4(W[0]); andre@0: BYTESWAP4(W[1]); andre@0: BYTESWAP4(W[2]); andre@0: BYTESWAP4(W[3]); andre@0: BYTESWAP4(W[4]); andre@0: BYTESWAP4(W[5]); andre@0: BYTESWAP4(W[6]); andre@0: BYTESWAP4(W[7]); andre@0: BYTESWAP4(W[8]); andre@0: BYTESWAP4(W[9]); andre@0: BYTESWAP4(W[10]); andre@0: BYTESWAP4(W[11]); andre@0: BYTESWAP4(W[12]); andre@0: BYTESWAP4(W[13]); andre@0: BYTESWAP4(W[14]); andre@0: BYTESWAP4(W[15]); andre@0: #endif andre@0: andre@0: #define INITW(t) W[t] = (s1(W[t-2]) + W[t-7] + s0(W[t-15]) + W[t-16]) andre@0: andre@0: /* prepare the "message schedule" */ andre@0: #ifdef NOUNROLL256 andre@0: { andre@0: int t; andre@0: for (t = 16; t < 64; ++t) { andre@0: INITW(t); andre@0: } andre@0: } andre@0: #else andre@0: INITW(16); andre@0: INITW(17); andre@0: INITW(18); andre@0: INITW(19); andre@0: andre@0: INITW(20); andre@0: INITW(21); andre@0: INITW(22); andre@0: INITW(23); andre@0: INITW(24); andre@0: INITW(25); andre@0: INITW(26); andre@0: INITW(27); andre@0: INITW(28); andre@0: INITW(29); andre@0: andre@0: INITW(30); andre@0: INITW(31); andre@0: INITW(32); andre@0: INITW(33); andre@0: INITW(34); andre@0: INITW(35); andre@0: INITW(36); andre@0: INITW(37); andre@0: INITW(38); andre@0: INITW(39); andre@0: andre@0: INITW(40); andre@0: INITW(41); andre@0: INITW(42); andre@0: INITW(43); andre@0: INITW(44); andre@0: INITW(45); andre@0: INITW(46); andre@0: INITW(47); andre@0: INITW(48); andre@0: INITW(49); andre@0: andre@0: INITW(50); andre@0: INITW(51); andre@0: INITW(52); andre@0: INITW(53); andre@0: INITW(54); andre@0: INITW(55); andre@0: INITW(56); andre@0: INITW(57); andre@0: INITW(58); andre@0: INITW(59); andre@0: andre@0: INITW(60); andre@0: INITW(61); andre@0: INITW(62); andre@0: INITW(63); andre@0: andre@0: #endif andre@0: #undef INITW andre@0: } andre@0: { andre@0: PRUint32 a, b, c, d, e, f, g, h; andre@0: andre@0: a = H[0]; andre@0: b = H[1]; andre@0: c = H[2]; andre@0: d = H[3]; andre@0: e = H[4]; andre@0: f = H[5]; andre@0: g = H[6]; andre@0: h = H[7]; andre@0: andre@0: #define ROUND(n,a,b,c,d,e,f,g,h) \ andre@0: h += S1(e) + Ch(e,f,g) + K256[n] + W[n]; \ andre@0: d += h; \ andre@0: h += S0(a) + Maj(a,b,c); andre@0: andre@0: #ifdef NOUNROLL256 andre@0: { andre@0: int t; andre@0: for (t = 0; t < 64; t+= 8) { andre@0: ROUND(t+0,a,b,c,d,e,f,g,h) andre@0: ROUND(t+1,h,a,b,c,d,e,f,g) andre@0: ROUND(t+2,g,h,a,b,c,d,e,f) andre@0: ROUND(t+3,f,g,h,a,b,c,d,e) andre@0: ROUND(t+4,e,f,g,h,a,b,c,d) andre@0: ROUND(t+5,d,e,f,g,h,a,b,c) andre@0: ROUND(t+6,c,d,e,f,g,h,a,b) andre@0: ROUND(t+7,b,c,d,e,f,g,h,a) andre@0: } andre@0: } andre@0: #else andre@0: ROUND( 0,a,b,c,d,e,f,g,h) andre@0: ROUND( 1,h,a,b,c,d,e,f,g) andre@0: ROUND( 2,g,h,a,b,c,d,e,f) andre@0: ROUND( 3,f,g,h,a,b,c,d,e) andre@0: ROUND( 4,e,f,g,h,a,b,c,d) andre@0: ROUND( 5,d,e,f,g,h,a,b,c) andre@0: ROUND( 6,c,d,e,f,g,h,a,b) andre@0: ROUND( 7,b,c,d,e,f,g,h,a) andre@0: andre@0: ROUND( 8,a,b,c,d,e,f,g,h) andre@0: ROUND( 9,h,a,b,c,d,e,f,g) andre@0: ROUND(10,g,h,a,b,c,d,e,f) andre@0: ROUND(11,f,g,h,a,b,c,d,e) andre@0: ROUND(12,e,f,g,h,a,b,c,d) andre@0: ROUND(13,d,e,f,g,h,a,b,c) andre@0: ROUND(14,c,d,e,f,g,h,a,b) andre@0: ROUND(15,b,c,d,e,f,g,h,a) andre@0: andre@0: ROUND(16,a,b,c,d,e,f,g,h) andre@0: ROUND(17,h,a,b,c,d,e,f,g) andre@0: ROUND(18,g,h,a,b,c,d,e,f) andre@0: ROUND(19,f,g,h,a,b,c,d,e) andre@0: ROUND(20,e,f,g,h,a,b,c,d) andre@0: ROUND(21,d,e,f,g,h,a,b,c) andre@0: ROUND(22,c,d,e,f,g,h,a,b) andre@0: ROUND(23,b,c,d,e,f,g,h,a) andre@0: andre@0: ROUND(24,a,b,c,d,e,f,g,h) andre@0: ROUND(25,h,a,b,c,d,e,f,g) andre@0: ROUND(26,g,h,a,b,c,d,e,f) andre@0: ROUND(27,f,g,h,a,b,c,d,e) andre@0: ROUND(28,e,f,g,h,a,b,c,d) andre@0: ROUND(29,d,e,f,g,h,a,b,c) andre@0: ROUND(30,c,d,e,f,g,h,a,b) andre@0: ROUND(31,b,c,d,e,f,g,h,a) andre@0: andre@0: ROUND(32,a,b,c,d,e,f,g,h) andre@0: ROUND(33,h,a,b,c,d,e,f,g) andre@0: ROUND(34,g,h,a,b,c,d,e,f) andre@0: ROUND(35,f,g,h,a,b,c,d,e) andre@0: ROUND(36,e,f,g,h,a,b,c,d) andre@0: ROUND(37,d,e,f,g,h,a,b,c) andre@0: ROUND(38,c,d,e,f,g,h,a,b) andre@0: ROUND(39,b,c,d,e,f,g,h,a) andre@0: andre@0: ROUND(40,a,b,c,d,e,f,g,h) andre@0: ROUND(41,h,a,b,c,d,e,f,g) andre@0: ROUND(42,g,h,a,b,c,d,e,f) andre@0: ROUND(43,f,g,h,a,b,c,d,e) andre@0: ROUND(44,e,f,g,h,a,b,c,d) andre@0: ROUND(45,d,e,f,g,h,a,b,c) andre@0: ROUND(46,c,d,e,f,g,h,a,b) andre@0: ROUND(47,b,c,d,e,f,g,h,a) andre@0: andre@0: ROUND(48,a,b,c,d,e,f,g,h) andre@0: ROUND(49,h,a,b,c,d,e,f,g) andre@0: ROUND(50,g,h,a,b,c,d,e,f) andre@0: ROUND(51,f,g,h,a,b,c,d,e) andre@0: ROUND(52,e,f,g,h,a,b,c,d) andre@0: ROUND(53,d,e,f,g,h,a,b,c) andre@0: ROUND(54,c,d,e,f,g,h,a,b) andre@0: ROUND(55,b,c,d,e,f,g,h,a) andre@0: andre@0: ROUND(56,a,b,c,d,e,f,g,h) andre@0: ROUND(57,h,a,b,c,d,e,f,g) andre@0: ROUND(58,g,h,a,b,c,d,e,f) andre@0: ROUND(59,f,g,h,a,b,c,d,e) andre@0: ROUND(60,e,f,g,h,a,b,c,d) andre@0: ROUND(61,d,e,f,g,h,a,b,c) andre@0: ROUND(62,c,d,e,f,g,h,a,b) andre@0: ROUND(63,b,c,d,e,f,g,h,a) andre@0: #endif andre@0: andre@0: H[0] += a; andre@0: H[1] += b; andre@0: H[2] += c; andre@0: H[3] += d; andre@0: H[4] += e; andre@0: H[5] += f; andre@0: H[6] += g; andre@0: H[7] += h; andre@0: } andre@0: #undef ROUND andre@0: } andre@0: andre@0: #undef s0 andre@0: #undef s1 andre@0: #undef S0 andre@0: #undef S1 andre@0: andre@0: void andre@0: SHA256_Update(SHA256Context *ctx, const unsigned char *input, andre@0: unsigned int inputLen) andre@0: { andre@0: unsigned int inBuf = ctx->sizeLo & 0x3f; andre@0: if (!inputLen) andre@0: return; andre@0: andre@0: /* Add inputLen into the count of bytes processed, before processing */ andre@0: if ((ctx->sizeLo += inputLen) < inputLen) andre@0: ctx->sizeHi++; andre@0: andre@0: /* if data already in buffer, attemp to fill rest of buffer */ andre@0: if (inBuf) { andre@0: unsigned int todo = SHA256_BLOCK_LENGTH - inBuf; andre@0: if (inputLen < todo) andre@0: todo = inputLen; andre@0: memcpy(B + inBuf, input, todo); andre@0: input += todo; andre@0: inputLen -= todo; andre@0: if (inBuf + todo == SHA256_BLOCK_LENGTH) andre@0: SHA256_Compress(ctx); andre@0: } andre@0: andre@0: /* if enough data to fill one or more whole buffers, process them. */ andre@0: while (inputLen >= SHA256_BLOCK_LENGTH) { andre@0: memcpy(B, input, SHA256_BLOCK_LENGTH); andre@0: input += SHA256_BLOCK_LENGTH; andre@0: inputLen -= SHA256_BLOCK_LENGTH; andre@0: SHA256_Compress(ctx); andre@0: } andre@0: /* if data left over, fill it into buffer */ andre@0: if (inputLen) andre@0: memcpy(B, input, inputLen); andre@0: } andre@0: andre@0: void andre@0: SHA256_End(SHA256Context *ctx, unsigned char *digest, andre@0: unsigned int *digestLen, unsigned int maxDigestLen) andre@0: { andre@0: unsigned int inBuf = ctx->sizeLo & 0x3f; andre@0: unsigned int padLen = (inBuf < 56) ? (56 - inBuf) : (56 + 64 - inBuf); andre@0: PRUint32 hi, lo; andre@0: #ifdef SWAP4MASK andre@0: PRUint32 t1; andre@0: #endif andre@0: andre@0: hi = (ctx->sizeHi << 3) | (ctx->sizeLo >> 29); andre@0: lo = (ctx->sizeLo << 3); andre@0: andre@0: SHA256_Update(ctx, pad, padLen); andre@0: andre@0: #if defined(IS_LITTLE_ENDIAN) andre@0: W[14] = SHA_HTONL(hi); andre@0: W[15] = SHA_HTONL(lo); andre@0: #else andre@0: W[14] = hi; andre@0: W[15] = lo; andre@0: #endif andre@0: SHA256_Compress(ctx); andre@0: andre@0: /* now output the answer */ andre@0: #if defined(IS_LITTLE_ENDIAN) andre@0: BYTESWAP4(H[0]); andre@0: BYTESWAP4(H[1]); andre@0: BYTESWAP4(H[2]); andre@0: BYTESWAP4(H[3]); andre@0: BYTESWAP4(H[4]); andre@0: BYTESWAP4(H[5]); andre@0: BYTESWAP4(H[6]); andre@0: BYTESWAP4(H[7]); andre@0: #endif andre@0: padLen = PR_MIN(SHA256_LENGTH, maxDigestLen); andre@0: memcpy(digest, H, padLen); andre@0: if (digestLen) andre@0: *digestLen = padLen; andre@0: } andre@0: andre@0: void andre@0: SHA256_EndRaw(SHA256Context *ctx, unsigned char *digest, andre@0: unsigned int *digestLen, unsigned int maxDigestLen) andre@0: { andre@0: PRUint32 h[8]; andre@0: unsigned int len; andre@0: #ifdef SWAP4MASK andre@0: PRUint32 t1; andre@0: #endif andre@0: andre@0: memcpy(h, ctx->h, sizeof(h)); andre@0: andre@0: #if defined(IS_LITTLE_ENDIAN) andre@0: BYTESWAP4(h[0]); andre@0: BYTESWAP4(h[1]); andre@0: BYTESWAP4(h[2]); andre@0: BYTESWAP4(h[3]); andre@0: BYTESWAP4(h[4]); andre@0: BYTESWAP4(h[5]); andre@0: BYTESWAP4(h[6]); andre@0: BYTESWAP4(h[7]); andre@0: #endif andre@0: andre@0: len = PR_MIN(SHA256_LENGTH, maxDigestLen); andre@0: memcpy(digest, h, len); andre@0: if (digestLen) andre@0: *digestLen = len; andre@0: } andre@0: andre@0: SECStatus andre@0: SHA256_HashBuf(unsigned char *dest, const unsigned char *src, andre@0: PRUint32 src_length) andre@0: { andre@0: SHA256Context ctx; andre@0: unsigned int outLen; andre@0: andre@0: SHA256_Begin(&ctx); andre@0: SHA256_Update(&ctx, src, src_length); andre@0: SHA256_End(&ctx, dest, &outLen, SHA256_LENGTH); andre@0: memset(&ctx, 0, sizeof ctx); andre@0: andre@0: return SECSuccess; andre@0: } andre@0: andre@0: andre@0: SECStatus andre@0: SHA256_Hash(unsigned char *dest, const char *src) andre@0: { andre@0: return SHA256_HashBuf(dest, (const unsigned char *)src, PORT_Strlen(src)); andre@0: } andre@0: andre@0: andre@0: void SHA256_TraceState(SHA256Context *ctx) { } andre@0: andre@0: unsigned int andre@0: SHA256_FlattenSize(SHA256Context *ctx) andre@0: { andre@0: return sizeof *ctx; andre@0: } andre@0: andre@0: SECStatus andre@0: SHA256_Flatten(SHA256Context *ctx,unsigned char *space) andre@0: { andre@0: PORT_Memcpy(space, ctx, sizeof *ctx); andre@0: return SECSuccess; andre@0: } andre@0: andre@0: SHA256Context * andre@0: SHA256_Resurrect(unsigned char *space, void *arg) andre@0: { andre@0: SHA256Context *ctx = SHA256_NewContext(); andre@0: if (ctx) andre@0: PORT_Memcpy(ctx, space, sizeof *ctx); andre@0: return ctx; andre@0: } andre@0: andre@0: void SHA256_Clone(SHA256Context *dest, SHA256Context *src) andre@0: { andre@0: memcpy(dest, src, sizeof *dest); andre@0: } andre@0: andre@0: /* ============= SHA224 implementation ================================== */ andre@0: andre@0: /* SHA-224 initial hash values */ andre@0: static const PRUint32 H224[8] = { andre@0: 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, andre@0: 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4 andre@0: }; andre@0: andre@0: SHA224Context * andre@0: SHA224_NewContext(void) andre@0: { andre@0: return SHA256_NewContext(); andre@0: } andre@0: andre@0: void andre@0: SHA224_DestroyContext(SHA224Context *ctx, PRBool freeit) andre@0: { andre@0: SHA256_DestroyContext(ctx, freeit); andre@0: } andre@0: andre@0: void andre@0: SHA224_Begin(SHA224Context *ctx) andre@0: { andre@0: memset(ctx, 0, sizeof *ctx); andre@0: memcpy(H, H224, sizeof H224); andre@0: } andre@0: andre@0: void andre@0: SHA224_Update(SHA224Context *ctx, const unsigned char *input, andre@0: unsigned int inputLen) andre@0: { andre@0: SHA256_Update(ctx, input, inputLen); andre@0: } andre@0: andre@0: void andre@0: SHA224_End(SHA256Context *ctx, unsigned char *digest, andre@0: unsigned int *digestLen, unsigned int maxDigestLen) andre@0: { andre@0: unsigned int maxLen = SHA_MIN(maxDigestLen, SHA224_LENGTH); andre@0: SHA256_End(ctx, digest, digestLen, maxLen); andre@0: } andre@0: andre@0: void andre@0: SHA224_EndRaw(SHA256Context *ctx, unsigned char *digest, andre@0: unsigned int *digestLen, unsigned int maxDigestLen) andre@0: { andre@0: unsigned int maxLen = SHA_MIN(maxDigestLen, SHA224_LENGTH); andre@0: SHA256_EndRaw(ctx, digest, digestLen, maxLen); andre@0: } andre@0: andre@0: SECStatus andre@0: SHA224_HashBuf(unsigned char *dest, const unsigned char *src, andre@0: PRUint32 src_length) andre@0: { andre@0: SHA256Context ctx; andre@0: unsigned int outLen; andre@0: andre@0: SHA224_Begin(&ctx); andre@0: SHA256_Update(&ctx, src, src_length); andre@0: SHA256_End(&ctx, dest, &outLen, SHA224_LENGTH); andre@0: memset(&ctx, 0, sizeof ctx); andre@0: andre@0: return SECSuccess; andre@0: } andre@0: andre@0: SECStatus andre@0: SHA224_Hash(unsigned char *dest, const char *src) andre@0: { andre@0: return SHA224_HashBuf(dest, (const unsigned char *)src, PORT_Strlen(src)); andre@0: } andre@0: andre@0: void SHA224_TraceState(SHA224Context *ctx) { } andre@0: andre@0: unsigned int andre@0: SHA224_FlattenSize(SHA224Context *ctx) andre@0: { andre@0: return SHA256_FlattenSize(ctx); andre@0: } andre@0: andre@0: SECStatus andre@0: SHA224_Flatten(SHA224Context *ctx, unsigned char *space) andre@0: { andre@0: return SHA256_Flatten(ctx, space); andre@0: } andre@0: andre@0: SHA224Context * andre@0: SHA224_Resurrect(unsigned char *space, void *arg) andre@0: { andre@0: return SHA256_Resurrect(space, arg); andre@0: } andre@0: andre@0: void SHA224_Clone(SHA224Context *dest, SHA224Context *src) andre@0: { andre@0: SHA256_Clone(dest, src); andre@0: } andre@0: andre@0: andre@0: /* ======= SHA512 and SHA384 common constants and defines ================= */ andre@0: andre@0: /* common #defines for SHA512 and SHA384 */ andre@0: #if defined(HAVE_LONG_LONG) andre@0: #if defined(_MSC_VER) andre@0: #pragma intrinsic(_rotr64,_rotl64) andre@0: #define ROTR64(x,n) _rotr64(x,n) andre@0: #define ROTL64(x,n) _rotl64(x,n) andre@0: #else andre@0: #define ROTR64(x,n) ((x >> n) | (x << (64 - n))) andre@0: #define ROTL64(x,n) ((x << n) | (x >> (64 - n))) andre@0: #endif andre@0: andre@0: #define S0(x) (ROTR64(x,28) ^ ROTR64(x,34) ^ ROTR64(x,39)) andre@0: #define S1(x) (ROTR64(x,14) ^ ROTR64(x,18) ^ ROTR64(x,41)) andre@0: #define s0(x) (t1 = x, ROTR64(t1, 1) ^ ROTR64(t1, 8) ^ SHR(t1,7)) andre@0: #define s1(x) (t2 = x, ROTR64(t2,19) ^ ROTR64(t2,61) ^ SHR(t2,6)) andre@0: andre@0: #if PR_BYTES_PER_LONG == 8 andre@0: #define ULLC(hi,lo) 0x ## hi ## lo ## UL andre@0: #elif defined(_MSC_VER) andre@0: #define ULLC(hi,lo) 0x ## hi ## lo ## ui64 andre@0: #else andre@0: #define ULLC(hi,lo) 0x ## hi ## lo ## ULL andre@0: #endif andre@0: andre@0: #if defined(_MSC_VER) andre@0: #pragma intrinsic(_byteswap_uint64) andre@0: #define SHA_HTONLL(x) _byteswap_uint64(x) andre@0: andre@0: #elif defined(__GNUC__) && (defined(__x86_64__) || defined(__x86_64)) andre@0: static __inline__ PRUint64 swap8b(PRUint64 value) andre@0: { andre@0: __asm__("bswapq %0" : "+r" (value)); andre@0: return (value); andre@0: } andre@0: #define SHA_HTONLL(x) swap8b(x) andre@0: andre@0: #else andre@0: #define SHA_MASK16 ULLC(0000FFFF,0000FFFF) andre@0: #define SHA_MASK8 ULLC(00FF00FF,00FF00FF) andre@0: #define SHA_HTONLL(x) (t1 = x, \ andre@0: t1 = ((t1 & SHA_MASK8 ) << 8) | ((t1 >> 8) & SHA_MASK8 ), \ andre@0: t1 = ((t1 & SHA_MASK16) << 16) | ((t1 >> 16) & SHA_MASK16), \ andre@0: (t1 >> 32) | (t1 << 32)) andre@0: #endif andre@0: #define BYTESWAP8(x) x = SHA_HTONLL(x) andre@0: andre@0: #else /* no long long */ andre@0: andre@0: #if defined(IS_LITTLE_ENDIAN) andre@0: #define ULLC(hi,lo) { 0x ## lo ## U, 0x ## hi ## U } andre@0: #else andre@0: #define ULLC(hi,lo) { 0x ## hi ## U, 0x ## lo ## U } andre@0: #endif andre@0: andre@0: #define SHA_HTONLL(x) ( BYTESWAP4(x.lo), BYTESWAP4(x.hi), \ andre@0: x.hi ^= x.lo ^= x.hi ^= x.lo, x) andre@0: #define BYTESWAP8(x) do { PRUint32 tmp; BYTESWAP4(x.lo); BYTESWAP4(x.hi); \ andre@0: tmp = x.lo; x.lo = x.hi; x.hi = tmp; } while (0) andre@0: #endif andre@0: andre@0: /* SHA-384 and SHA-512 constants, K512. */ andre@0: static const PRUint64 K512[80] = { andre@0: #if PR_BYTES_PER_LONG == 8 andre@0: 0x428a2f98d728ae22UL , 0x7137449123ef65cdUL , andre@0: 0xb5c0fbcfec4d3b2fUL , 0xe9b5dba58189dbbcUL , andre@0: 0x3956c25bf348b538UL , 0x59f111f1b605d019UL , andre@0: 0x923f82a4af194f9bUL , 0xab1c5ed5da6d8118UL , andre@0: 0xd807aa98a3030242UL , 0x12835b0145706fbeUL , andre@0: 0x243185be4ee4b28cUL , 0x550c7dc3d5ffb4e2UL , andre@0: 0x72be5d74f27b896fUL , 0x80deb1fe3b1696b1UL , andre@0: 0x9bdc06a725c71235UL , 0xc19bf174cf692694UL , andre@0: 0xe49b69c19ef14ad2UL , 0xefbe4786384f25e3UL , andre@0: 0x0fc19dc68b8cd5b5UL , 0x240ca1cc77ac9c65UL , andre@0: 0x2de92c6f592b0275UL , 0x4a7484aa6ea6e483UL , andre@0: 0x5cb0a9dcbd41fbd4UL , 0x76f988da831153b5UL , andre@0: 0x983e5152ee66dfabUL , 0xa831c66d2db43210UL , andre@0: 0xb00327c898fb213fUL , 0xbf597fc7beef0ee4UL , andre@0: 0xc6e00bf33da88fc2UL , 0xd5a79147930aa725UL , andre@0: 0x06ca6351e003826fUL , 0x142929670a0e6e70UL , andre@0: 0x27b70a8546d22ffcUL , 0x2e1b21385c26c926UL , andre@0: 0x4d2c6dfc5ac42aedUL , 0x53380d139d95b3dfUL , andre@0: 0x650a73548baf63deUL , 0x766a0abb3c77b2a8UL , andre@0: 0x81c2c92e47edaee6UL , 0x92722c851482353bUL , andre@0: 0xa2bfe8a14cf10364UL , 0xa81a664bbc423001UL , andre@0: 0xc24b8b70d0f89791UL , 0xc76c51a30654be30UL , andre@0: 0xd192e819d6ef5218UL , 0xd69906245565a910UL , andre@0: 0xf40e35855771202aUL , 0x106aa07032bbd1b8UL , andre@0: 0x19a4c116b8d2d0c8UL , 0x1e376c085141ab53UL , andre@0: 0x2748774cdf8eeb99UL , 0x34b0bcb5e19b48a8UL , andre@0: 0x391c0cb3c5c95a63UL , 0x4ed8aa4ae3418acbUL , andre@0: 0x5b9cca4f7763e373UL , 0x682e6ff3d6b2b8a3UL , andre@0: 0x748f82ee5defb2fcUL , 0x78a5636f43172f60UL , andre@0: 0x84c87814a1f0ab72UL , 0x8cc702081a6439ecUL , andre@0: 0x90befffa23631e28UL , 0xa4506cebde82bde9UL , andre@0: 0xbef9a3f7b2c67915UL , 0xc67178f2e372532bUL , andre@0: 0xca273eceea26619cUL , 0xd186b8c721c0c207UL , andre@0: 0xeada7dd6cde0eb1eUL , 0xf57d4f7fee6ed178UL , andre@0: 0x06f067aa72176fbaUL , 0x0a637dc5a2c898a6UL , andre@0: 0x113f9804bef90daeUL , 0x1b710b35131c471bUL , andre@0: 0x28db77f523047d84UL , 0x32caab7b40c72493UL , andre@0: 0x3c9ebe0a15c9bebcUL , 0x431d67c49c100d4cUL , andre@0: 0x4cc5d4becb3e42b6UL , 0x597f299cfc657e2aUL , andre@0: 0x5fcb6fab3ad6faecUL , 0x6c44198c4a475817UL andre@0: #else andre@0: ULLC(428a2f98,d728ae22), ULLC(71374491,23ef65cd), andre@0: ULLC(b5c0fbcf,ec4d3b2f), ULLC(e9b5dba5,8189dbbc), andre@0: ULLC(3956c25b,f348b538), ULLC(59f111f1,b605d019), andre@0: ULLC(923f82a4,af194f9b), ULLC(ab1c5ed5,da6d8118), andre@0: ULLC(d807aa98,a3030242), ULLC(12835b01,45706fbe), andre@0: ULLC(243185be,4ee4b28c), ULLC(550c7dc3,d5ffb4e2), andre@0: ULLC(72be5d74,f27b896f), ULLC(80deb1fe,3b1696b1), andre@0: ULLC(9bdc06a7,25c71235), ULLC(c19bf174,cf692694), andre@0: ULLC(e49b69c1,9ef14ad2), ULLC(efbe4786,384f25e3), andre@0: ULLC(0fc19dc6,8b8cd5b5), ULLC(240ca1cc,77ac9c65), andre@0: ULLC(2de92c6f,592b0275), ULLC(4a7484aa,6ea6e483), andre@0: ULLC(5cb0a9dc,bd41fbd4), ULLC(76f988da,831153b5), andre@0: ULLC(983e5152,ee66dfab), ULLC(a831c66d,2db43210), andre@0: ULLC(b00327c8,98fb213f), ULLC(bf597fc7,beef0ee4), andre@0: ULLC(c6e00bf3,3da88fc2), ULLC(d5a79147,930aa725), andre@0: ULLC(06ca6351,e003826f), ULLC(14292967,0a0e6e70), andre@0: ULLC(27b70a85,46d22ffc), ULLC(2e1b2138,5c26c926), andre@0: ULLC(4d2c6dfc,5ac42aed), ULLC(53380d13,9d95b3df), andre@0: ULLC(650a7354,8baf63de), ULLC(766a0abb,3c77b2a8), andre@0: ULLC(81c2c92e,47edaee6), ULLC(92722c85,1482353b), andre@0: ULLC(a2bfe8a1,4cf10364), ULLC(a81a664b,bc423001), andre@0: ULLC(c24b8b70,d0f89791), ULLC(c76c51a3,0654be30), andre@0: ULLC(d192e819,d6ef5218), ULLC(d6990624,5565a910), andre@0: ULLC(f40e3585,5771202a), ULLC(106aa070,32bbd1b8), andre@0: ULLC(19a4c116,b8d2d0c8), ULLC(1e376c08,5141ab53), andre@0: ULLC(2748774c,df8eeb99), ULLC(34b0bcb5,e19b48a8), andre@0: ULLC(391c0cb3,c5c95a63), ULLC(4ed8aa4a,e3418acb), andre@0: ULLC(5b9cca4f,7763e373), ULLC(682e6ff3,d6b2b8a3), andre@0: ULLC(748f82ee,5defb2fc), ULLC(78a5636f,43172f60), andre@0: ULLC(84c87814,a1f0ab72), ULLC(8cc70208,1a6439ec), andre@0: ULLC(90befffa,23631e28), ULLC(a4506ceb,de82bde9), andre@0: ULLC(bef9a3f7,b2c67915), ULLC(c67178f2,e372532b), andre@0: ULLC(ca273ece,ea26619c), ULLC(d186b8c7,21c0c207), andre@0: ULLC(eada7dd6,cde0eb1e), ULLC(f57d4f7f,ee6ed178), andre@0: ULLC(06f067aa,72176fba), ULLC(0a637dc5,a2c898a6), andre@0: ULLC(113f9804,bef90dae), ULLC(1b710b35,131c471b), andre@0: ULLC(28db77f5,23047d84), ULLC(32caab7b,40c72493), andre@0: ULLC(3c9ebe0a,15c9bebc), ULLC(431d67c4,9c100d4c), andre@0: ULLC(4cc5d4be,cb3e42b6), ULLC(597f299c,fc657e2a), andre@0: ULLC(5fcb6fab,3ad6faec), ULLC(6c44198c,4a475817) andre@0: #endif andre@0: }; andre@0: andre@0: struct SHA512ContextStr { andre@0: union { andre@0: PRUint64 w[80]; /* message schedule, input buffer, plus 64 words */ andre@0: PRUint32 l[160]; andre@0: PRUint8 b[640]; andre@0: } u; andre@0: PRUint64 h[8]; /* 8 state variables */ andre@0: PRUint64 sizeLo; /* 64-bit count of hashed bytes. */ andre@0: }; andre@0: andre@0: /* =========== SHA512 implementation ===================================== */ andre@0: andre@0: /* SHA-512 initial hash values */ andre@0: static const PRUint64 H512[8] = { andre@0: #if PR_BYTES_PER_LONG == 8 andre@0: 0x6a09e667f3bcc908UL , 0xbb67ae8584caa73bUL , andre@0: 0x3c6ef372fe94f82bUL , 0xa54ff53a5f1d36f1UL , andre@0: 0x510e527fade682d1UL , 0x9b05688c2b3e6c1fUL , andre@0: 0x1f83d9abfb41bd6bUL , 0x5be0cd19137e2179UL andre@0: #else andre@0: ULLC(6a09e667,f3bcc908), ULLC(bb67ae85,84caa73b), andre@0: ULLC(3c6ef372,fe94f82b), ULLC(a54ff53a,5f1d36f1), andre@0: ULLC(510e527f,ade682d1), ULLC(9b05688c,2b3e6c1f), andre@0: ULLC(1f83d9ab,fb41bd6b), ULLC(5be0cd19,137e2179) andre@0: #endif andre@0: }; andre@0: andre@0: andre@0: SHA512Context * andre@0: SHA512_NewContext(void) andre@0: { andre@0: SHA512Context *ctx = PORT_New(SHA512Context); andre@0: return ctx; andre@0: } andre@0: andre@0: void andre@0: SHA512_DestroyContext(SHA512Context *ctx, PRBool freeit) andre@0: { andre@0: memset(ctx, 0, sizeof *ctx); andre@0: if (freeit) { andre@0: PORT_Free(ctx); andre@0: } andre@0: } andre@0: andre@0: void andre@0: SHA512_Begin(SHA512Context *ctx) andre@0: { andre@0: memset(ctx, 0, sizeof *ctx); andre@0: memcpy(H, H512, sizeof H512); andre@0: } andre@0: andre@0: #if defined(SHA512_TRACE) andre@0: #if defined(HAVE_LONG_LONG) andre@0: #define DUMP(n,a,d,e,h) printf(" t = %2d, %s = %016lx, %s = %016lx\n", \ andre@0: n, #e, d, #a, h); andre@0: #else andre@0: #define DUMP(n,a,d,e,h) printf(" t = %2d, %s = %08x%08x, %s = %08x%08x\n", \ andre@0: n, #e, d.hi, d.lo, #a, h.hi, h.lo); andre@0: #endif andre@0: #else andre@0: #define DUMP(n,a,d,e,h) andre@0: #endif andre@0: andre@0: #if defined(HAVE_LONG_LONG) andre@0: andre@0: #define ADDTO(x,y) y += x andre@0: andre@0: #define INITW(t) W[t] = (s1(W[t-2]) + W[t-7] + s0(W[t-15]) + W[t-16]) andre@0: andre@0: #define ROUND(n,a,b,c,d,e,f,g,h) \ andre@0: h += S1(e) + Ch(e,f,g) + K512[n] + W[n]; \ andre@0: d += h; \ andre@0: h += S0(a) + Maj(a,b,c); \ andre@0: DUMP(n,a,d,e,h) andre@0: andre@0: #else /* use only 32-bit variables, and don't unroll loops */ andre@0: andre@0: #undef NOUNROLL512 andre@0: #define NOUNROLL512 1 andre@0: andre@0: #define ADDTO(x,y) y.lo += x.lo; y.hi += x.hi + (x.lo > y.lo) andre@0: andre@0: #define ROTR64a(x,n,lo,hi) (x.lo >> n | x.hi << (32-n)) andre@0: #define ROTR64A(x,n,lo,hi) (x.lo << (64-n) | x.hi >> (n-32)) andre@0: #define SHR64a(x,n,lo,hi) (x.lo >> n | x.hi << (32-n)) andre@0: andre@0: /* Capitol Sigma and lower case sigma functions */ andre@0: #define s0lo(x) (ROTR64a(x,1,lo,hi) ^ ROTR64a(x,8,lo,hi) ^ SHR64a(x,7,lo,hi)) andre@0: #define s0hi(x) (ROTR64a(x,1,hi,lo) ^ ROTR64a(x,8,hi,lo) ^ (x.hi >> 7)) andre@0: andre@0: #define s1lo(x) (ROTR64a(x,19,lo,hi) ^ ROTR64A(x,61,lo,hi) ^ SHR64a(x,6,lo,hi)) andre@0: #define s1hi(x) (ROTR64a(x,19,hi,lo) ^ ROTR64A(x,61,hi,lo) ^ (x.hi >> 6)) andre@0: andre@0: #define S0lo(x)(ROTR64a(x,28,lo,hi) ^ ROTR64A(x,34,lo,hi) ^ ROTR64A(x,39,lo,hi)) andre@0: #define S0hi(x)(ROTR64a(x,28,hi,lo) ^ ROTR64A(x,34,hi,lo) ^ ROTR64A(x,39,hi,lo)) andre@0: andre@0: #define S1lo(x)(ROTR64a(x,14,lo,hi) ^ ROTR64a(x,18,lo,hi) ^ ROTR64A(x,41,lo,hi)) andre@0: #define S1hi(x)(ROTR64a(x,14,hi,lo) ^ ROTR64a(x,18,hi,lo) ^ ROTR64A(x,41,hi,lo)) andre@0: andre@0: /* 32-bit versions of Ch and Maj */ andre@0: #define Chxx(x,y,z,lo) ((x.lo & y.lo) ^ (~x.lo & z.lo)) andre@0: #define Majx(x,y,z,lo) ((x.lo & y.lo) ^ (x.lo & z.lo) ^ (y.lo & z.lo)) andre@0: andre@0: #define INITW(t) \ andre@0: do { \ andre@0: PRUint32 lo, tm; \ andre@0: PRUint32 cy = 0; \ andre@0: lo = s1lo(W[t-2]); \ andre@0: lo += (tm = W[t-7].lo); if (lo < tm) cy++; \ andre@0: lo += (tm = s0lo(W[t-15])); if (lo < tm) cy++; \ andre@0: lo += (tm = W[t-16].lo); if (lo < tm) cy++; \ andre@0: W[t].lo = lo; \ andre@0: W[t].hi = cy + s1hi(W[t-2]) + W[t-7].hi + s0hi(W[t-15]) + W[t-16].hi; \ andre@0: } while (0) andre@0: andre@0: #define ROUND(n,a,b,c,d,e,f,g,h) \ andre@0: { \ andre@0: PRUint32 lo, tm, cy; \ andre@0: lo = S1lo(e); \ andre@0: lo += (tm = Chxx(e,f,g,lo)); cy = (lo < tm); \ andre@0: lo += (tm = K512[n].lo); if (lo < tm) cy++; \ andre@0: lo += (tm = W[n].lo); if (lo < tm) cy++; \ andre@0: h.lo += lo; if (h.lo < lo) cy++; \ andre@0: h.hi += cy + S1hi(e) + Chxx(e,f,g,hi) + K512[n].hi + W[n].hi; \ andre@0: d.lo += h.lo; \ andre@0: d.hi += h.hi + (d.lo < h.lo); \ andre@0: lo = S0lo(a); \ andre@0: lo += (tm = Majx(a,b,c,lo)); cy = (lo < tm); \ andre@0: h.lo += lo; if (h.lo < lo) cy++; \ andre@0: h.hi += cy + S0hi(a) + Majx(a,b,c,hi); \ andre@0: DUMP(n,a,d,e,h) \ andre@0: } andre@0: #endif andre@0: andre@0: static void andre@0: SHA512_Compress(SHA512Context *ctx) andre@0: { andre@0: #if defined(IS_LITTLE_ENDIAN) andre@0: { andre@0: #if defined(HAVE_LONG_LONG) andre@0: PRUint64 t1; andre@0: #else andre@0: PRUint32 t1; andre@0: #endif andre@0: BYTESWAP8(W[0]); andre@0: BYTESWAP8(W[1]); andre@0: BYTESWAP8(W[2]); andre@0: BYTESWAP8(W[3]); andre@0: BYTESWAP8(W[4]); andre@0: BYTESWAP8(W[5]); andre@0: BYTESWAP8(W[6]); andre@0: BYTESWAP8(W[7]); andre@0: BYTESWAP8(W[8]); andre@0: BYTESWAP8(W[9]); andre@0: BYTESWAP8(W[10]); andre@0: BYTESWAP8(W[11]); andre@0: BYTESWAP8(W[12]); andre@0: BYTESWAP8(W[13]); andre@0: BYTESWAP8(W[14]); andre@0: BYTESWAP8(W[15]); andre@0: } andre@0: #endif andre@0: andre@0: { andre@0: PRUint64 t1, t2; andre@0: #ifdef NOUNROLL512 andre@0: { andre@0: /* prepare the "message schedule" */ andre@0: int t; andre@0: for (t = 16; t < 80; ++t) { andre@0: INITW(t); andre@0: } andre@0: } andre@0: #else andre@0: INITW(16); andre@0: INITW(17); andre@0: INITW(18); andre@0: INITW(19); andre@0: andre@0: INITW(20); andre@0: INITW(21); andre@0: INITW(22); andre@0: INITW(23); andre@0: INITW(24); andre@0: INITW(25); andre@0: INITW(26); andre@0: INITW(27); andre@0: INITW(28); andre@0: INITW(29); andre@0: andre@0: INITW(30); andre@0: INITW(31); andre@0: INITW(32); andre@0: INITW(33); andre@0: INITW(34); andre@0: INITW(35); andre@0: INITW(36); andre@0: INITW(37); andre@0: INITW(38); andre@0: INITW(39); andre@0: andre@0: INITW(40); andre@0: INITW(41); andre@0: INITW(42); andre@0: INITW(43); andre@0: INITW(44); andre@0: INITW(45); andre@0: INITW(46); andre@0: INITW(47); andre@0: INITW(48); andre@0: INITW(49); andre@0: andre@0: INITW(50); andre@0: INITW(51); andre@0: INITW(52); andre@0: INITW(53); andre@0: INITW(54); andre@0: INITW(55); andre@0: INITW(56); andre@0: INITW(57); andre@0: INITW(58); andre@0: INITW(59); andre@0: andre@0: INITW(60); andre@0: INITW(61); andre@0: INITW(62); andre@0: INITW(63); andre@0: INITW(64); andre@0: INITW(65); andre@0: INITW(66); andre@0: INITW(67); andre@0: INITW(68); andre@0: INITW(69); andre@0: andre@0: INITW(70); andre@0: INITW(71); andre@0: INITW(72); andre@0: INITW(73); andre@0: INITW(74); andre@0: INITW(75); andre@0: INITW(76); andre@0: INITW(77); andre@0: INITW(78); andre@0: INITW(79); andre@0: #endif andre@0: } andre@0: #ifdef SHA512_TRACE andre@0: { andre@0: int i; andre@0: for (i = 0; i < 80; ++i) { andre@0: #ifdef HAVE_LONG_LONG andre@0: printf("W[%2d] = %016lx\n", i, W[i]); andre@0: #else andre@0: printf("W[%2d] = %08x%08x\n", i, W[i].hi, W[i].lo); andre@0: #endif andre@0: } andre@0: } andre@0: #endif andre@0: { andre@0: PRUint64 a, b, c, d, e, f, g, h; andre@0: andre@0: a = H[0]; andre@0: b = H[1]; andre@0: c = H[2]; andre@0: d = H[3]; andre@0: e = H[4]; andre@0: f = H[5]; andre@0: g = H[6]; andre@0: h = H[7]; andre@0: andre@0: #ifdef NOUNROLL512 andre@0: { andre@0: int t; andre@0: for (t = 0; t < 80; t+= 8) { andre@0: ROUND(t+0,a,b,c,d,e,f,g,h) andre@0: ROUND(t+1,h,a,b,c,d,e,f,g) andre@0: ROUND(t+2,g,h,a,b,c,d,e,f) andre@0: ROUND(t+3,f,g,h,a,b,c,d,e) andre@0: ROUND(t+4,e,f,g,h,a,b,c,d) andre@0: ROUND(t+5,d,e,f,g,h,a,b,c) andre@0: ROUND(t+6,c,d,e,f,g,h,a,b) andre@0: ROUND(t+7,b,c,d,e,f,g,h,a) andre@0: } andre@0: } andre@0: #else andre@0: ROUND( 0,a,b,c,d,e,f,g,h) andre@0: ROUND( 1,h,a,b,c,d,e,f,g) andre@0: ROUND( 2,g,h,a,b,c,d,e,f) andre@0: ROUND( 3,f,g,h,a,b,c,d,e) andre@0: ROUND( 4,e,f,g,h,a,b,c,d) andre@0: ROUND( 5,d,e,f,g,h,a,b,c) andre@0: ROUND( 6,c,d,e,f,g,h,a,b) andre@0: ROUND( 7,b,c,d,e,f,g,h,a) andre@0: andre@0: ROUND( 8,a,b,c,d,e,f,g,h) andre@0: ROUND( 9,h,a,b,c,d,e,f,g) andre@0: ROUND(10,g,h,a,b,c,d,e,f) andre@0: ROUND(11,f,g,h,a,b,c,d,e) andre@0: ROUND(12,e,f,g,h,a,b,c,d) andre@0: ROUND(13,d,e,f,g,h,a,b,c) andre@0: ROUND(14,c,d,e,f,g,h,a,b) andre@0: ROUND(15,b,c,d,e,f,g,h,a) andre@0: andre@0: ROUND(16,a,b,c,d,e,f,g,h) andre@0: ROUND(17,h,a,b,c,d,e,f,g) andre@0: ROUND(18,g,h,a,b,c,d,e,f) andre@0: ROUND(19,f,g,h,a,b,c,d,e) andre@0: ROUND(20,e,f,g,h,a,b,c,d) andre@0: ROUND(21,d,e,f,g,h,a,b,c) andre@0: ROUND(22,c,d,e,f,g,h,a,b) andre@0: ROUND(23,b,c,d,e,f,g,h,a) andre@0: andre@0: ROUND(24,a,b,c,d,e,f,g,h) andre@0: ROUND(25,h,a,b,c,d,e,f,g) andre@0: ROUND(26,g,h,a,b,c,d,e,f) andre@0: ROUND(27,f,g,h,a,b,c,d,e) andre@0: ROUND(28,e,f,g,h,a,b,c,d) andre@0: ROUND(29,d,e,f,g,h,a,b,c) andre@0: ROUND(30,c,d,e,f,g,h,a,b) andre@0: ROUND(31,b,c,d,e,f,g,h,a) andre@0: andre@0: ROUND(32,a,b,c,d,e,f,g,h) andre@0: ROUND(33,h,a,b,c,d,e,f,g) andre@0: ROUND(34,g,h,a,b,c,d,e,f) andre@0: ROUND(35,f,g,h,a,b,c,d,e) andre@0: ROUND(36,e,f,g,h,a,b,c,d) andre@0: ROUND(37,d,e,f,g,h,a,b,c) andre@0: ROUND(38,c,d,e,f,g,h,a,b) andre@0: ROUND(39,b,c,d,e,f,g,h,a) andre@0: andre@0: ROUND(40,a,b,c,d,e,f,g,h) andre@0: ROUND(41,h,a,b,c,d,e,f,g) andre@0: ROUND(42,g,h,a,b,c,d,e,f) andre@0: ROUND(43,f,g,h,a,b,c,d,e) andre@0: ROUND(44,e,f,g,h,a,b,c,d) andre@0: ROUND(45,d,e,f,g,h,a,b,c) andre@0: ROUND(46,c,d,e,f,g,h,a,b) andre@0: ROUND(47,b,c,d,e,f,g,h,a) andre@0: andre@0: ROUND(48,a,b,c,d,e,f,g,h) andre@0: ROUND(49,h,a,b,c,d,e,f,g) andre@0: ROUND(50,g,h,a,b,c,d,e,f) andre@0: ROUND(51,f,g,h,a,b,c,d,e) andre@0: ROUND(52,e,f,g,h,a,b,c,d) andre@0: ROUND(53,d,e,f,g,h,a,b,c) andre@0: ROUND(54,c,d,e,f,g,h,a,b) andre@0: ROUND(55,b,c,d,e,f,g,h,a) andre@0: andre@0: ROUND(56,a,b,c,d,e,f,g,h) andre@0: ROUND(57,h,a,b,c,d,e,f,g) andre@0: ROUND(58,g,h,a,b,c,d,e,f) andre@0: ROUND(59,f,g,h,a,b,c,d,e) andre@0: ROUND(60,e,f,g,h,a,b,c,d) andre@0: ROUND(61,d,e,f,g,h,a,b,c) andre@0: ROUND(62,c,d,e,f,g,h,a,b) andre@0: ROUND(63,b,c,d,e,f,g,h,a) andre@0: andre@0: ROUND(64,a,b,c,d,e,f,g,h) andre@0: ROUND(65,h,a,b,c,d,e,f,g) andre@0: ROUND(66,g,h,a,b,c,d,e,f) andre@0: ROUND(67,f,g,h,a,b,c,d,e) andre@0: ROUND(68,e,f,g,h,a,b,c,d) andre@0: ROUND(69,d,e,f,g,h,a,b,c) andre@0: ROUND(70,c,d,e,f,g,h,a,b) andre@0: ROUND(71,b,c,d,e,f,g,h,a) andre@0: andre@0: ROUND(72,a,b,c,d,e,f,g,h) andre@0: ROUND(73,h,a,b,c,d,e,f,g) andre@0: ROUND(74,g,h,a,b,c,d,e,f) andre@0: ROUND(75,f,g,h,a,b,c,d,e) andre@0: ROUND(76,e,f,g,h,a,b,c,d) andre@0: ROUND(77,d,e,f,g,h,a,b,c) andre@0: ROUND(78,c,d,e,f,g,h,a,b) andre@0: ROUND(79,b,c,d,e,f,g,h,a) andre@0: #endif andre@0: andre@0: ADDTO(a,H[0]); andre@0: ADDTO(b,H[1]); andre@0: ADDTO(c,H[2]); andre@0: ADDTO(d,H[3]); andre@0: ADDTO(e,H[4]); andre@0: ADDTO(f,H[5]); andre@0: ADDTO(g,H[6]); andre@0: ADDTO(h,H[7]); andre@0: } andre@0: } andre@0: andre@0: void andre@0: SHA512_Update(SHA512Context *ctx, const unsigned char *input, andre@0: unsigned int inputLen) andre@0: { andre@0: unsigned int inBuf; andre@0: if (!inputLen) andre@0: return; andre@0: andre@0: #if defined(HAVE_LONG_LONG) andre@0: inBuf = (unsigned int)ctx->sizeLo & 0x7f; andre@0: /* Add inputLen into the count of bytes processed, before processing */ andre@0: ctx->sizeLo += inputLen; andre@0: #else andre@0: inBuf = (unsigned int)ctx->sizeLo.lo & 0x7f; andre@0: ctx->sizeLo.lo += inputLen; andre@0: if (ctx->sizeLo.lo < inputLen) ctx->sizeLo.hi++; andre@0: #endif andre@0: andre@0: /* if data already in buffer, attemp to fill rest of buffer */ andre@0: if (inBuf) { andre@0: unsigned int todo = SHA512_BLOCK_LENGTH - inBuf; andre@0: if (inputLen < todo) andre@0: todo = inputLen; andre@0: memcpy(B + inBuf, input, todo); andre@0: input += todo; andre@0: inputLen -= todo; andre@0: if (inBuf + todo == SHA512_BLOCK_LENGTH) andre@0: SHA512_Compress(ctx); andre@0: } andre@0: andre@0: /* if enough data to fill one or more whole buffers, process them. */ andre@0: while (inputLen >= SHA512_BLOCK_LENGTH) { andre@0: memcpy(B, input, SHA512_BLOCK_LENGTH); andre@0: input += SHA512_BLOCK_LENGTH; andre@0: inputLen -= SHA512_BLOCK_LENGTH; andre@0: SHA512_Compress(ctx); andre@0: } andre@0: /* if data left over, fill it into buffer */ andre@0: if (inputLen) andre@0: memcpy(B, input, inputLen); andre@0: } andre@0: andre@0: void andre@0: SHA512_End(SHA512Context *ctx, unsigned char *digest, andre@0: unsigned int *digestLen, unsigned int maxDigestLen) andre@0: { andre@0: #if defined(HAVE_LONG_LONG) andre@0: unsigned int inBuf = (unsigned int)ctx->sizeLo & 0x7f; andre@0: PRUint64 t1; andre@0: #else andre@0: unsigned int inBuf = (unsigned int)ctx->sizeLo.lo & 0x7f; andre@0: PRUint32 t1; andre@0: #endif andre@0: unsigned int padLen = (inBuf < 112) ? (112 - inBuf) : (112 + 128 - inBuf); andre@0: PRUint64 lo; andre@0: LL_SHL(lo, ctx->sizeLo, 3); andre@0: andre@0: SHA512_Update(ctx, pad, padLen); andre@0: andre@0: #if defined(HAVE_LONG_LONG) andre@0: W[14] = 0; andre@0: #else andre@0: W[14].lo = 0; andre@0: W[14].hi = 0; andre@0: #endif andre@0: andre@0: W[15] = lo; andre@0: #if defined(IS_LITTLE_ENDIAN) andre@0: BYTESWAP8(W[15]); andre@0: #endif andre@0: SHA512_Compress(ctx); andre@0: andre@0: /* now output the answer */ andre@0: #if defined(IS_LITTLE_ENDIAN) andre@0: BYTESWAP8(H[0]); andre@0: BYTESWAP8(H[1]); andre@0: BYTESWAP8(H[2]); andre@0: BYTESWAP8(H[3]); andre@0: BYTESWAP8(H[4]); andre@0: BYTESWAP8(H[5]); andre@0: BYTESWAP8(H[6]); andre@0: BYTESWAP8(H[7]); andre@0: #endif andre@0: padLen = PR_MIN(SHA512_LENGTH, maxDigestLen); andre@0: memcpy(digest, H, padLen); andre@0: if (digestLen) andre@0: *digestLen = padLen; andre@0: } andre@0: andre@0: void andre@0: SHA512_EndRaw(SHA512Context *ctx, unsigned char *digest, andre@0: unsigned int *digestLen, unsigned int maxDigestLen) andre@0: { andre@0: #if defined(HAVE_LONG_LONG) andre@0: PRUint64 t1; andre@0: #else andre@0: PRUint32 t1; andre@0: #endif andre@0: PRUint64 h[8]; andre@0: unsigned int len; andre@0: andre@0: memcpy(h, ctx->h, sizeof(h)); andre@0: andre@0: #if defined(IS_LITTLE_ENDIAN) andre@0: BYTESWAP8(h[0]); andre@0: BYTESWAP8(h[1]); andre@0: BYTESWAP8(h[2]); andre@0: BYTESWAP8(h[3]); andre@0: BYTESWAP8(h[4]); andre@0: BYTESWAP8(h[5]); andre@0: BYTESWAP8(h[6]); andre@0: BYTESWAP8(h[7]); andre@0: #endif andre@0: len = PR_MIN(SHA512_LENGTH, maxDigestLen); andre@0: memcpy(digest, h, len); andre@0: if (digestLen) andre@0: *digestLen = len; andre@0: } andre@0: andre@0: SECStatus andre@0: SHA512_HashBuf(unsigned char *dest, const unsigned char *src, andre@0: PRUint32 src_length) andre@0: { andre@0: SHA512Context ctx; andre@0: unsigned int outLen; andre@0: andre@0: SHA512_Begin(&ctx); andre@0: SHA512_Update(&ctx, src, src_length); andre@0: SHA512_End(&ctx, dest, &outLen, SHA512_LENGTH); andre@0: memset(&ctx, 0, sizeof ctx); andre@0: andre@0: return SECSuccess; andre@0: } andre@0: andre@0: andre@0: SECStatus andre@0: SHA512_Hash(unsigned char *dest, const char *src) andre@0: { andre@0: return SHA512_HashBuf(dest, (const unsigned char *)src, PORT_Strlen(src)); andre@0: } andre@0: andre@0: andre@0: void SHA512_TraceState(SHA512Context *ctx) { } andre@0: andre@0: unsigned int andre@0: SHA512_FlattenSize(SHA512Context *ctx) andre@0: { andre@0: return sizeof *ctx; andre@0: } andre@0: andre@0: SECStatus andre@0: SHA512_Flatten(SHA512Context *ctx,unsigned char *space) andre@0: { andre@0: PORT_Memcpy(space, ctx, sizeof *ctx); andre@0: return SECSuccess; andre@0: } andre@0: andre@0: SHA512Context * andre@0: SHA512_Resurrect(unsigned char *space, void *arg) andre@0: { andre@0: SHA512Context *ctx = SHA512_NewContext(); andre@0: if (ctx) andre@0: PORT_Memcpy(ctx, space, sizeof *ctx); andre@0: return ctx; andre@0: } andre@0: andre@0: void SHA512_Clone(SHA512Context *dest, SHA512Context *src) andre@0: { andre@0: memcpy(dest, src, sizeof *dest); andre@0: } andre@0: andre@0: /* ======================================================================= */ andre@0: /* SHA384 uses a SHA512Context as the real context. andre@0: ** The only differences between SHA384 an SHA512 are: andre@0: ** a) the intialization values for the context, and andre@0: ** b) the number of bytes of data produced as output. andre@0: */ andre@0: andre@0: /* SHA-384 initial hash values */ andre@0: static const PRUint64 H384[8] = { andre@0: #if PR_BYTES_PER_LONG == 8 andre@0: 0xcbbb9d5dc1059ed8UL , 0x629a292a367cd507UL , andre@0: 0x9159015a3070dd17UL , 0x152fecd8f70e5939UL , andre@0: 0x67332667ffc00b31UL , 0x8eb44a8768581511UL , andre@0: 0xdb0c2e0d64f98fa7UL , 0x47b5481dbefa4fa4UL andre@0: #else andre@0: ULLC(cbbb9d5d,c1059ed8), ULLC(629a292a,367cd507), andre@0: ULLC(9159015a,3070dd17), ULLC(152fecd8,f70e5939), andre@0: ULLC(67332667,ffc00b31), ULLC(8eb44a87,68581511), andre@0: ULLC(db0c2e0d,64f98fa7), ULLC(47b5481d,befa4fa4) andre@0: #endif andre@0: }; andre@0: andre@0: SHA384Context * andre@0: SHA384_NewContext(void) andre@0: { andre@0: return SHA512_NewContext(); andre@0: } andre@0: andre@0: void andre@0: SHA384_DestroyContext(SHA384Context *ctx, PRBool freeit) andre@0: { andre@0: SHA512_DestroyContext(ctx, freeit); andre@0: } andre@0: andre@0: void andre@0: SHA384_Begin(SHA384Context *ctx) andre@0: { andre@0: memset(ctx, 0, sizeof *ctx); andre@0: memcpy(H, H384, sizeof H384); andre@0: } andre@0: andre@0: void andre@0: SHA384_Update(SHA384Context *ctx, const unsigned char *input, andre@0: unsigned int inputLen) andre@0: { andre@0: SHA512_Update(ctx, input, inputLen); andre@0: } andre@0: andre@0: void andre@0: SHA384_End(SHA384Context *ctx, unsigned char *digest, andre@0: unsigned int *digestLen, unsigned int maxDigestLen) andre@0: { andre@0: unsigned int maxLen = SHA_MIN(maxDigestLen, SHA384_LENGTH); andre@0: SHA512_End(ctx, digest, digestLen, maxLen); andre@0: } andre@0: andre@0: void andre@0: SHA384_EndRaw(SHA384Context *ctx, unsigned char *digest, andre@0: unsigned int *digestLen, unsigned int maxDigestLen) andre@0: { andre@0: unsigned int maxLen = SHA_MIN(maxDigestLen, SHA384_LENGTH); andre@0: SHA512_EndRaw(ctx, digest, digestLen, maxLen); andre@0: } andre@0: andre@0: SECStatus andre@0: SHA384_HashBuf(unsigned char *dest, const unsigned char *src, andre@0: PRUint32 src_length) andre@0: { andre@0: SHA512Context ctx; andre@0: unsigned int outLen; andre@0: andre@0: SHA384_Begin(&ctx); andre@0: SHA512_Update(&ctx, src, src_length); andre@0: SHA512_End(&ctx, dest, &outLen, SHA384_LENGTH); andre@0: memset(&ctx, 0, sizeof ctx); andre@0: andre@0: return SECSuccess; andre@0: } andre@0: andre@0: SECStatus andre@0: SHA384_Hash(unsigned char *dest, const char *src) andre@0: { andre@0: return SHA384_HashBuf(dest, (const unsigned char *)src, PORT_Strlen(src)); andre@0: } andre@0: andre@0: void SHA384_TraceState(SHA384Context *ctx) { } andre@0: andre@0: unsigned int andre@0: SHA384_FlattenSize(SHA384Context *ctx) andre@0: { andre@0: return sizeof(SHA384Context); andre@0: } andre@0: andre@0: SECStatus andre@0: SHA384_Flatten(SHA384Context *ctx,unsigned char *space) andre@0: { andre@0: return SHA512_Flatten(ctx, space); andre@0: } andre@0: andre@0: SHA384Context * andre@0: SHA384_Resurrect(unsigned char *space, void *arg) andre@0: { andre@0: return SHA512_Resurrect(space, arg); andre@0: } andre@0: andre@0: void SHA384_Clone(SHA384Context *dest, SHA384Context *src) andre@0: { andre@0: memcpy(dest, src, sizeof *dest); andre@0: } andre@0: andre@0: /* ======================================================================= */ andre@0: #ifdef SELFTEST andre@0: #include andre@0: andre@0: static const char abc[] = { "abc" }; andre@0: static const char abcdbc[] = { andre@0: "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" andre@0: }; andre@0: static const char abcdef[] = { andre@0: "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn" andre@0: "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" andre@0: }; andre@0: andre@0: void andre@0: dumpHash32(const unsigned char *buf, unsigned int bufLen) andre@0: { andre@0: unsigned int i; andre@0: for (i = 0; i < bufLen; i += 4) { andre@0: printf(" %02x%02x%02x%02x", buf[i], buf[i+1], buf[i+2], buf[i+3]); andre@0: } andre@0: printf("\n"); andre@0: } andre@0: andre@0: void test256(void) andre@0: { andre@0: unsigned char outBuf[SHA256_LENGTH]; andre@0: andre@0: printf("SHA256, input = %s\n", abc); andre@0: SHA256_Hash(outBuf, abc); andre@0: dumpHash32(outBuf, sizeof outBuf); andre@0: andre@0: printf("SHA256, input = %s\n", abcdbc); andre@0: SHA256_Hash(outBuf, abcdbc); andre@0: dumpHash32(outBuf, sizeof outBuf); andre@0: } andre@0: andre@0: void test224(void) andre@0: { andre@0: SHA224Context ctx; andre@0: unsigned char a1000times[1000]; andre@0: unsigned int outLen; andre@0: unsigned char outBuf[SHA224_LENGTH]; andre@0: int i; andre@0: andre@0: /* Test Vector 1 */ andre@0: printf("SHA224, input = %s\n", abc); andre@0: SHA224_Hash(outBuf, abc); andre@0: dumpHash32(outBuf, sizeof outBuf); andre@0: andre@0: /* Test Vector 2 */ andre@0: printf("SHA224, input = %s\n", abcdbc); andre@0: SHA224_Hash(outBuf, abcdbc); andre@0: dumpHash32(outBuf, sizeof outBuf); andre@0: andre@0: /* Test Vector 3 */ andre@0: andre@0: /* to hash one million 'a's perform 1000 andre@0: * sha224 updates on a buffer with 1000 'a's andre@0: */ andre@0: memset(a1000times, 'a', 1000); andre@0: printf("SHA224, input = %s\n", "a one million times"); andre@0: SHA224_Begin(&ctx); andre@0: for (i = 0; i < 1000; i++) andre@0: SHA224_Update(&ctx, a1000times, 1000); andre@0: SHA224_End(&ctx, outBuf, &outLen, SHA224_LENGTH); andre@0: dumpHash32(outBuf, sizeof outBuf); andre@0: } andre@0: andre@0: void andre@0: dumpHash64(const unsigned char *buf, unsigned int bufLen) andre@0: { andre@0: unsigned int i; andre@0: for (i = 0; i < bufLen; i += 8) { andre@0: if (i % 32 == 0) andre@0: printf("\n"); andre@0: printf(" %02x%02x%02x%02x%02x%02x%02x%02x", andre@0: buf[i ], buf[i+1], buf[i+2], buf[i+3], andre@0: buf[i+4], buf[i+5], buf[i+6], buf[i+7]); andre@0: } andre@0: printf("\n"); andre@0: } andre@0: andre@0: void test512(void) andre@0: { andre@0: unsigned char outBuf[SHA512_LENGTH]; andre@0: andre@0: printf("SHA512, input = %s\n", abc); andre@0: SHA512_Hash(outBuf, abc); andre@0: dumpHash64(outBuf, sizeof outBuf); andre@0: andre@0: printf("SHA512, input = %s\n", abcdef); andre@0: SHA512_Hash(outBuf, abcdef); andre@0: dumpHash64(outBuf, sizeof outBuf); andre@0: } andre@0: andre@0: void time512(void) andre@0: { andre@0: unsigned char outBuf[SHA512_LENGTH]; andre@0: andre@0: SHA512_Hash(outBuf, abc); andre@0: SHA512_Hash(outBuf, abcdef); andre@0: } andre@0: andre@0: void test384(void) andre@0: { andre@0: unsigned char outBuf[SHA384_LENGTH]; andre@0: andre@0: printf("SHA384, input = %s\n", abc); andre@0: SHA384_Hash(outBuf, abc); andre@0: dumpHash64(outBuf, sizeof outBuf); andre@0: andre@0: printf("SHA384, input = %s\n", abcdef); andre@0: SHA384_Hash(outBuf, abcdef); andre@0: dumpHash64(outBuf, sizeof outBuf); andre@0: } andre@0: andre@0: int main (int argc, char *argv[], char *envp[]) andre@0: { andre@0: int i = 1; andre@0: if (argc > 1) { andre@0: i = atoi(argv[1]); andre@0: } andre@0: if (i < 2) { andre@0: test224(); andre@0: test256(); andre@0: test384(); andre@0: test512(); andre@0: } else { andre@0: while (i-- > 0) { andre@0: time512(); andre@0: } andre@0: printf("done\n"); andre@0: } andre@0: return 0; andre@0: } andre@0: andre@0: void *PORT_Alloc(size_t len) { return malloc(len); } andre@0: void PORT_Free(void *ptr) { free(ptr); } andre@0: void PORT_ZFree(void *ptr, size_t len) { memset(ptr, 0, len); free(ptr); } andre@0: #endif