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 | #ifndef _SHA_FAST_H_ |
michael@0 | 6 | #define _SHA_FAST_H_ |
michael@0 | 7 | |
michael@0 | 8 | #include "prlong.h" |
michael@0 | 9 | |
michael@0 | 10 | #define SHA1_INPUT_LEN 64 |
michael@0 | 11 | |
michael@0 | 12 | #if defined(IS_64) && !defined(__sparc) |
michael@0 | 13 | typedef PRUint64 SHA_HW_t; |
michael@0 | 14 | #define SHA1_USING_64_BIT 1 |
michael@0 | 15 | #else |
michael@0 | 16 | typedef PRUint32 SHA_HW_t; |
michael@0 | 17 | #endif |
michael@0 | 18 | |
michael@0 | 19 | struct SHA1ContextStr { |
michael@0 | 20 | union { |
michael@0 | 21 | PRUint32 w[16]; /* input buffer */ |
michael@0 | 22 | PRUint8 b[64]; |
michael@0 | 23 | } u; |
michael@0 | 24 | PRUint64 size; /* count of hashed bytes. */ |
michael@0 | 25 | SHA_HW_t H[22]; /* 5 state variables, 16 tmp values, 1 extra */ |
michael@0 | 26 | }; |
michael@0 | 27 | |
michael@0 | 28 | #if defined(_MSC_VER) |
michael@0 | 29 | #include <stdlib.h> |
michael@0 | 30 | #if defined(IS_LITTLE_ENDIAN) |
michael@0 | 31 | #if (_MSC_VER >= 1300) |
michael@0 | 32 | #pragma intrinsic(_byteswap_ulong) |
michael@0 | 33 | #define SHA_HTONL(x) _byteswap_ulong(x) |
michael@0 | 34 | #elif defined(NSS_X86_OR_X64) |
michael@0 | 35 | #ifndef FORCEINLINE |
michael@0 | 36 | #if (_MSC_VER >= 1200) |
michael@0 | 37 | #define FORCEINLINE __forceinline |
michael@0 | 38 | #else |
michael@0 | 39 | #define FORCEINLINE __inline |
michael@0 | 40 | #endif /* _MSC_VER */ |
michael@0 | 41 | #endif /* !defined FORCEINLINE */ |
michael@0 | 42 | #define FASTCALL __fastcall |
michael@0 | 43 | |
michael@0 | 44 | static FORCEINLINE PRUint32 FASTCALL |
michael@0 | 45 | swap4b(PRUint32 dwd) |
michael@0 | 46 | { |
michael@0 | 47 | __asm { |
michael@0 | 48 | mov eax,dwd |
michael@0 | 49 | bswap eax |
michael@0 | 50 | } |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | #define SHA_HTONL(x) swap4b(x) |
michael@0 | 54 | #endif /* NSS_X86_OR_X64 */ |
michael@0 | 55 | #endif /* IS_LITTLE_ENDIAN */ |
michael@0 | 56 | |
michael@0 | 57 | #pragma intrinsic (_lrotr, _lrotl) |
michael@0 | 58 | #define SHA_ROTL(x,n) _lrotl(x,n) |
michael@0 | 59 | #define SHA_ROTL_IS_DEFINED 1 |
michael@0 | 60 | #endif /* _MSC_VER */ |
michael@0 | 61 | |
michael@0 | 62 | #if defined(__GNUC__) |
michael@0 | 63 | /* __x86_64__ and __x86_64 are defined by GCC on x86_64 CPUs */ |
michael@0 | 64 | #if defined( SHA1_USING_64_BIT ) |
michael@0 | 65 | static __inline__ PRUint64 SHA_ROTL(PRUint64 x, PRUint32 n) |
michael@0 | 66 | { |
michael@0 | 67 | PRUint32 t = (PRUint32)x; |
michael@0 | 68 | return ((t << n) | (t >> (32 - n))); |
michael@0 | 69 | } |
michael@0 | 70 | #else |
michael@0 | 71 | static __inline__ PRUint32 SHA_ROTL(PRUint32 t, PRUint32 n) |
michael@0 | 72 | { |
michael@0 | 73 | return ((t << n) | (t >> (32 - n))); |
michael@0 | 74 | } |
michael@0 | 75 | #endif |
michael@0 | 76 | #define SHA_ROTL_IS_DEFINED 1 |
michael@0 | 77 | |
michael@0 | 78 | #if defined(NSS_X86_OR_X64) |
michael@0 | 79 | static __inline__ PRUint32 swap4b(PRUint32 value) |
michael@0 | 80 | { |
michael@0 | 81 | __asm__("bswap %0" : "+r" (value)); |
michael@0 | 82 | return (value); |
michael@0 | 83 | } |
michael@0 | 84 | #define SHA_HTONL(x) swap4b(x) |
michael@0 | 85 | |
michael@0 | 86 | #elif defined(__thumb2__) || \ |
michael@0 | 87 | (!defined(__thumb__) && \ |
michael@0 | 88 | (defined(__ARM_ARCH_6__) || \ |
michael@0 | 89 | defined(__ARM_ARCH_6J__) || \ |
michael@0 | 90 | defined(__ARM_ARCH_6K__) || \ |
michael@0 | 91 | defined(__ARM_ARCH_6Z__) || \ |
michael@0 | 92 | defined(__ARM_ARCH_6ZK__) || \ |
michael@0 | 93 | defined(__ARM_ARCH_6T2__) || \ |
michael@0 | 94 | defined(__ARM_ARCH_7__) || \ |
michael@0 | 95 | defined(__ARM_ARCH_7A__) || \ |
michael@0 | 96 | defined(__ARM_ARCH_7R__))) |
michael@0 | 97 | static __inline__ PRUint32 swap4b(PRUint32 value) |
michael@0 | 98 | { |
michael@0 | 99 | PRUint32 ret; |
michael@0 | 100 | __asm__("rev %0, %1" : "=r" (ret) : "r"(value)); |
michael@0 | 101 | return ret; |
michael@0 | 102 | } |
michael@0 | 103 | #define SHA_HTONL(x) swap4b(x) |
michael@0 | 104 | |
michael@0 | 105 | #endif /* x86 family */ |
michael@0 | 106 | |
michael@0 | 107 | #endif /* __GNUC__ */ |
michael@0 | 108 | |
michael@0 | 109 | #if !defined(SHA_ROTL_IS_DEFINED) |
michael@0 | 110 | #define SHA_NEED_TMP_VARIABLE 1 |
michael@0 | 111 | #define SHA_ROTL(X,n) (tmp = (X), ((tmp) << (n)) | ((tmp) >> (32-(n)))) |
michael@0 | 112 | #endif |
michael@0 | 113 | |
michael@0 | 114 | #if defined(NSS_X86_OR_X64) |
michael@0 | 115 | #define SHA_ALLOW_UNALIGNED_ACCESS 1 |
michael@0 | 116 | #endif |
michael@0 | 117 | |
michael@0 | 118 | #if !defined(SHA_HTONL) |
michael@0 | 119 | #define SHA_MASK 0x00FF00FF |
michael@0 | 120 | #if defined(IS_LITTLE_ENDIAN) |
michael@0 | 121 | #undef SHA_NEED_TMP_VARIABLE |
michael@0 | 122 | #define SHA_NEED_TMP_VARIABLE 1 |
michael@0 | 123 | #define SHA_HTONL(x) (tmp = (x), tmp = (tmp << 16) | (tmp >> 16), \ |
michael@0 | 124 | ((tmp & SHA_MASK) << 8) | ((tmp >> 8) & SHA_MASK)) |
michael@0 | 125 | #else |
michael@0 | 126 | #define SHA_HTONL(x) (x) |
michael@0 | 127 | #endif |
michael@0 | 128 | #endif |
michael@0 | 129 | |
michael@0 | 130 | #define SHA_BYTESWAP(x) x = SHA_HTONL(x) |
michael@0 | 131 | |
michael@0 | 132 | #define SHA_STORE(n) ((PRUint32*)hashout)[n] = SHA_HTONL(ctx->H[n]) |
michael@0 | 133 | #if defined(SHA_ALLOW_UNALIGNED_ACCESS) |
michael@0 | 134 | #define SHA_STORE_RESULT \ |
michael@0 | 135 | SHA_STORE(0); \ |
michael@0 | 136 | SHA_STORE(1); \ |
michael@0 | 137 | SHA_STORE(2); \ |
michael@0 | 138 | SHA_STORE(3); \ |
michael@0 | 139 | SHA_STORE(4); |
michael@0 | 140 | |
michael@0 | 141 | #elif defined(IS_LITTLE_ENDIAN) || defined( SHA1_USING_64_BIT ) |
michael@0 | 142 | #define SHA_STORE_RESULT \ |
michael@0 | 143 | if (!((ptrdiff_t)hashout % sizeof(PRUint32))) { \ |
michael@0 | 144 | SHA_STORE(0); \ |
michael@0 | 145 | SHA_STORE(1); \ |
michael@0 | 146 | SHA_STORE(2); \ |
michael@0 | 147 | SHA_STORE(3); \ |
michael@0 | 148 | SHA_STORE(4); \ |
michael@0 | 149 | } else { \ |
michael@0 | 150 | tmpbuf[0] = SHA_HTONL(ctx->H[0]); \ |
michael@0 | 151 | tmpbuf[1] = SHA_HTONL(ctx->H[1]); \ |
michael@0 | 152 | tmpbuf[2] = SHA_HTONL(ctx->H[2]); \ |
michael@0 | 153 | tmpbuf[3] = SHA_HTONL(ctx->H[3]); \ |
michael@0 | 154 | tmpbuf[4] = SHA_HTONL(ctx->H[4]); \ |
michael@0 | 155 | memcpy(hashout, tmpbuf, SHA1_LENGTH); \ |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | #else |
michael@0 | 159 | #define SHA_STORE_RESULT \ |
michael@0 | 160 | if (!((ptrdiff_t)hashout % sizeof(PRUint32))) { \ |
michael@0 | 161 | SHA_STORE(0); \ |
michael@0 | 162 | SHA_STORE(1); \ |
michael@0 | 163 | SHA_STORE(2); \ |
michael@0 | 164 | SHA_STORE(3); \ |
michael@0 | 165 | SHA_STORE(4); \ |
michael@0 | 166 | } else { \ |
michael@0 | 167 | memcpy(hashout, ctx->H, SHA1_LENGTH); \ |
michael@0 | 168 | } |
michael@0 | 169 | #endif |
michael@0 | 170 | |
michael@0 | 171 | #endif /* _SHA_FAST_H_ */ |