1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/nss/lib/freebl/sha_fast.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,171 @@ 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 +#ifndef _SHA_FAST_H_ 1.9 +#define _SHA_FAST_H_ 1.10 + 1.11 +#include "prlong.h" 1.12 + 1.13 +#define SHA1_INPUT_LEN 64 1.14 + 1.15 +#if defined(IS_64) && !defined(__sparc) 1.16 +typedef PRUint64 SHA_HW_t; 1.17 +#define SHA1_USING_64_BIT 1 1.18 +#else 1.19 +typedef PRUint32 SHA_HW_t; 1.20 +#endif 1.21 + 1.22 +struct SHA1ContextStr { 1.23 + union { 1.24 + PRUint32 w[16]; /* input buffer */ 1.25 + PRUint8 b[64]; 1.26 + } u; 1.27 + PRUint64 size; /* count of hashed bytes. */ 1.28 + SHA_HW_t H[22]; /* 5 state variables, 16 tmp values, 1 extra */ 1.29 +}; 1.30 + 1.31 +#if defined(_MSC_VER) 1.32 +#include <stdlib.h> 1.33 +#if defined(IS_LITTLE_ENDIAN) 1.34 +#if (_MSC_VER >= 1300) 1.35 +#pragma intrinsic(_byteswap_ulong) 1.36 +#define SHA_HTONL(x) _byteswap_ulong(x) 1.37 +#elif defined(NSS_X86_OR_X64) 1.38 +#ifndef FORCEINLINE 1.39 +#if (_MSC_VER >= 1200) 1.40 +#define FORCEINLINE __forceinline 1.41 +#else 1.42 +#define FORCEINLINE __inline 1.43 +#endif /* _MSC_VER */ 1.44 +#endif /* !defined FORCEINLINE */ 1.45 +#define FASTCALL __fastcall 1.46 + 1.47 +static FORCEINLINE PRUint32 FASTCALL 1.48 +swap4b(PRUint32 dwd) 1.49 +{ 1.50 + __asm { 1.51 + mov eax,dwd 1.52 + bswap eax 1.53 + } 1.54 +} 1.55 + 1.56 +#define SHA_HTONL(x) swap4b(x) 1.57 +#endif /* NSS_X86_OR_X64 */ 1.58 +#endif /* IS_LITTLE_ENDIAN */ 1.59 + 1.60 +#pragma intrinsic (_lrotr, _lrotl) 1.61 +#define SHA_ROTL(x,n) _lrotl(x,n) 1.62 +#define SHA_ROTL_IS_DEFINED 1 1.63 +#endif /* _MSC_VER */ 1.64 + 1.65 +#if defined(__GNUC__) 1.66 +/* __x86_64__ and __x86_64 are defined by GCC on x86_64 CPUs */ 1.67 +#if defined( SHA1_USING_64_BIT ) 1.68 +static __inline__ PRUint64 SHA_ROTL(PRUint64 x, PRUint32 n) 1.69 +{ 1.70 + PRUint32 t = (PRUint32)x; 1.71 + return ((t << n) | (t >> (32 - n))); 1.72 +} 1.73 +#else 1.74 +static __inline__ PRUint32 SHA_ROTL(PRUint32 t, PRUint32 n) 1.75 +{ 1.76 + return ((t << n) | (t >> (32 - n))); 1.77 +} 1.78 +#endif 1.79 +#define SHA_ROTL_IS_DEFINED 1 1.80 + 1.81 +#if defined(NSS_X86_OR_X64) 1.82 +static __inline__ PRUint32 swap4b(PRUint32 value) 1.83 +{ 1.84 + __asm__("bswap %0" : "+r" (value)); 1.85 + return (value); 1.86 +} 1.87 +#define SHA_HTONL(x) swap4b(x) 1.88 + 1.89 +#elif defined(__thumb2__) || \ 1.90 + (!defined(__thumb__) && \ 1.91 + (defined(__ARM_ARCH_6__) || \ 1.92 + defined(__ARM_ARCH_6J__) || \ 1.93 + defined(__ARM_ARCH_6K__) || \ 1.94 + defined(__ARM_ARCH_6Z__) || \ 1.95 + defined(__ARM_ARCH_6ZK__) || \ 1.96 + defined(__ARM_ARCH_6T2__) || \ 1.97 + defined(__ARM_ARCH_7__) || \ 1.98 + defined(__ARM_ARCH_7A__) || \ 1.99 + defined(__ARM_ARCH_7R__))) 1.100 +static __inline__ PRUint32 swap4b(PRUint32 value) 1.101 +{ 1.102 + PRUint32 ret; 1.103 + __asm__("rev %0, %1" : "=r" (ret) : "r"(value)); 1.104 + return ret; 1.105 +} 1.106 +#define SHA_HTONL(x) swap4b(x) 1.107 + 1.108 +#endif /* x86 family */ 1.109 + 1.110 +#endif /* __GNUC__ */ 1.111 + 1.112 +#if !defined(SHA_ROTL_IS_DEFINED) 1.113 +#define SHA_NEED_TMP_VARIABLE 1 1.114 +#define SHA_ROTL(X,n) (tmp = (X), ((tmp) << (n)) | ((tmp) >> (32-(n)))) 1.115 +#endif 1.116 + 1.117 +#if defined(NSS_X86_OR_X64) 1.118 +#define SHA_ALLOW_UNALIGNED_ACCESS 1 1.119 +#endif 1.120 + 1.121 +#if !defined(SHA_HTONL) 1.122 +#define SHA_MASK 0x00FF00FF 1.123 +#if defined(IS_LITTLE_ENDIAN) 1.124 +#undef SHA_NEED_TMP_VARIABLE 1.125 +#define SHA_NEED_TMP_VARIABLE 1 1.126 +#define SHA_HTONL(x) (tmp = (x), tmp = (tmp << 16) | (tmp >> 16), \ 1.127 + ((tmp & SHA_MASK) << 8) | ((tmp >> 8) & SHA_MASK)) 1.128 +#else 1.129 +#define SHA_HTONL(x) (x) 1.130 +#endif 1.131 +#endif 1.132 + 1.133 +#define SHA_BYTESWAP(x) x = SHA_HTONL(x) 1.134 + 1.135 +#define SHA_STORE(n) ((PRUint32*)hashout)[n] = SHA_HTONL(ctx->H[n]) 1.136 +#if defined(SHA_ALLOW_UNALIGNED_ACCESS) 1.137 +#define SHA_STORE_RESULT \ 1.138 + SHA_STORE(0); \ 1.139 + SHA_STORE(1); \ 1.140 + SHA_STORE(2); \ 1.141 + SHA_STORE(3); \ 1.142 + SHA_STORE(4); 1.143 + 1.144 +#elif defined(IS_LITTLE_ENDIAN) || defined( SHA1_USING_64_BIT ) 1.145 +#define SHA_STORE_RESULT \ 1.146 + if (!((ptrdiff_t)hashout % sizeof(PRUint32))) { \ 1.147 + SHA_STORE(0); \ 1.148 + SHA_STORE(1); \ 1.149 + SHA_STORE(2); \ 1.150 + SHA_STORE(3); \ 1.151 + SHA_STORE(4); \ 1.152 + } else { \ 1.153 + tmpbuf[0] = SHA_HTONL(ctx->H[0]); \ 1.154 + tmpbuf[1] = SHA_HTONL(ctx->H[1]); \ 1.155 + tmpbuf[2] = SHA_HTONL(ctx->H[2]); \ 1.156 + tmpbuf[3] = SHA_HTONL(ctx->H[3]); \ 1.157 + tmpbuf[4] = SHA_HTONL(ctx->H[4]); \ 1.158 + memcpy(hashout, tmpbuf, SHA1_LENGTH); \ 1.159 + } 1.160 + 1.161 +#else 1.162 +#define SHA_STORE_RESULT \ 1.163 + if (!((ptrdiff_t)hashout % sizeof(PRUint32))) { \ 1.164 + SHA_STORE(0); \ 1.165 + SHA_STORE(1); \ 1.166 + SHA_STORE(2); \ 1.167 + SHA_STORE(3); \ 1.168 + SHA_STORE(4); \ 1.169 + } else { \ 1.170 + memcpy(hashout, ctx->H, SHA1_LENGTH); \ 1.171 + } 1.172 +#endif 1.173 + 1.174 +#endif /* _SHA_FAST_H_ */