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: #ifndef _SHA_FAST_H_ andre@0: #define _SHA_FAST_H_ andre@0: andre@0: #include "prlong.h" andre@0: andre@0: #define SHA1_INPUT_LEN 64 andre@0: andre@0: #if defined(IS_64) && !defined(__sparc) andre@0: typedef PRUint64 SHA_HW_t; andre@0: #define SHA1_USING_64_BIT 1 andre@0: #else andre@0: typedef PRUint32 SHA_HW_t; andre@0: #endif andre@0: andre@0: struct SHA1ContextStr { andre@0: union { andre@0: PRUint32 w[16]; /* input buffer */ andre@0: PRUint8 b[64]; andre@0: } u; andre@0: PRUint64 size; /* count of hashed bytes. */ andre@0: SHA_HW_t H[22]; /* 5 state variables, 16 tmp values, 1 extra */ andre@0: }; andre@0: andre@0: #if defined(_MSC_VER) andre@0: #include andre@0: #if defined(IS_LITTLE_ENDIAN) andre@0: #if (_MSC_VER >= 1300) andre@0: #pragma intrinsic(_byteswap_ulong) andre@0: #define SHA_HTONL(x) _byteswap_ulong(x) andre@0: #elif 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 /* _MSC_VER */ andre@0: #endif /* !defined FORCEINLINE */ 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: #endif /* NSS_X86_OR_X64 */ andre@0: #endif /* IS_LITTLE_ENDIAN */ andre@0: andre@0: #pragma intrinsic (_lrotr, _lrotl) andre@0: #define SHA_ROTL(x,n) _lrotl(x,n) andre@0: #define SHA_ROTL_IS_DEFINED 1 andre@0: #endif /* _MSC_VER */ andre@0: andre@0: #if defined(__GNUC__) andre@0: /* __x86_64__ and __x86_64 are defined by GCC on x86_64 CPUs */ andre@0: #if defined( SHA1_USING_64_BIT ) andre@0: static __inline__ PRUint64 SHA_ROTL(PRUint64 x, PRUint32 n) andre@0: { andre@0: PRUint32 t = (PRUint32)x; andre@0: return ((t << n) | (t >> (32 - n))); andre@0: } andre@0: #else andre@0: static __inline__ PRUint32 SHA_ROTL(PRUint32 t, PRUint32 n) andre@0: { andre@0: return ((t << n) | (t >> (32 - n))); andre@0: } andre@0: #endif andre@0: #define SHA_ROTL_IS_DEFINED 1 andre@0: andre@0: #if 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: andre@0: #elif 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: andre@0: #endif /* x86 family */ andre@0: andre@0: #endif /* __GNUC__ */ andre@0: andre@0: #if !defined(SHA_ROTL_IS_DEFINED) andre@0: #define SHA_NEED_TMP_VARIABLE 1 andre@0: #define SHA_ROTL(X,n) (tmp = (X), ((tmp) << (n)) | ((tmp) >> (32-(n)))) andre@0: #endif andre@0: andre@0: #if defined(NSS_X86_OR_X64) andre@0: #define SHA_ALLOW_UNALIGNED_ACCESS 1 andre@0: #endif andre@0: andre@0: #if !defined(SHA_HTONL) andre@0: #define SHA_MASK 0x00FF00FF andre@0: #if defined(IS_LITTLE_ENDIAN) andre@0: #undef SHA_NEED_TMP_VARIABLE andre@0: #define SHA_NEED_TMP_VARIABLE 1 andre@0: #define SHA_HTONL(x) (tmp = (x), tmp = (tmp << 16) | (tmp >> 16), \ andre@0: ((tmp & SHA_MASK) << 8) | ((tmp >> 8) & SHA_MASK)) andre@0: #else andre@0: #define SHA_HTONL(x) (x) andre@0: #endif andre@0: #endif andre@0: andre@0: #define SHA_BYTESWAP(x) x = SHA_HTONL(x) andre@0: andre@0: #define SHA_STORE(n) ((PRUint32*)hashout)[n] = SHA_HTONL(ctx->H[n]) andre@0: #if defined(SHA_ALLOW_UNALIGNED_ACCESS) andre@0: #define SHA_STORE_RESULT \ andre@0: SHA_STORE(0); \ andre@0: SHA_STORE(1); \ andre@0: SHA_STORE(2); \ andre@0: SHA_STORE(3); \ andre@0: SHA_STORE(4); andre@0: andre@0: #elif defined(IS_LITTLE_ENDIAN) || defined( SHA1_USING_64_BIT ) andre@0: #define SHA_STORE_RESULT \ andre@0: if (!((ptrdiff_t)hashout % sizeof(PRUint32))) { \ andre@0: SHA_STORE(0); \ andre@0: SHA_STORE(1); \ andre@0: SHA_STORE(2); \ andre@0: SHA_STORE(3); \ andre@0: SHA_STORE(4); \ andre@0: } else { \ andre@0: tmpbuf[0] = SHA_HTONL(ctx->H[0]); \ andre@0: tmpbuf[1] = SHA_HTONL(ctx->H[1]); \ andre@0: tmpbuf[2] = SHA_HTONL(ctx->H[2]); \ andre@0: tmpbuf[3] = SHA_HTONL(ctx->H[3]); \ andre@0: tmpbuf[4] = SHA_HTONL(ctx->H[4]); \ andre@0: memcpy(hashout, tmpbuf, SHA1_LENGTH); \ andre@0: } andre@0: andre@0: #else andre@0: #define SHA_STORE_RESULT \ andre@0: if (!((ptrdiff_t)hashout % sizeof(PRUint32))) { \ andre@0: SHA_STORE(0); \ andre@0: SHA_STORE(1); \ andre@0: SHA_STORE(2); \ andre@0: SHA_STORE(3); \ andre@0: SHA_STORE(4); \ andre@0: } else { \ andre@0: memcpy(hashout, ctx->H, SHA1_LENGTH); \ andre@0: } andre@0: #endif andre@0: andre@0: #endif /* _SHA_FAST_H_ */