Import from 1.9a8 tarball
[mozilla-nss.git] / security / nss / lib / freebl / sha_fast.h
blob6243471b025cfaf4a2750a205d99e51415a1bc0e
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is SHA 180-1 Reference Implementation (Optimized).
16 * The Initial Developer of the Original Code is
17 * Paul Kocher of Cryptography Research.
18 * Portions created by the Initial Developer are Copyright (C) 1995-9
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
37 #ifndef _SHA_FAST_H_
38 #define _SHA_FAST_H_
40 #include "prlong.h"
42 #define SHA1_INPUT_LEN 64
44 #if defined(IS_64) && !defined(__sparc)
45 typedef PRUint64 SHA_HW_t;
46 #define SHA1_USING_64_BIT 1
47 #else
48 typedef PRUint32 SHA_HW_t;
49 #endif
51 struct SHA1ContextStr {
52 union {
53 PRUint32 w[16]; /* input buffer */
54 PRUint8 b[64];
55 } u;
56 PRUint64 size; /* count of hashed bytes. */
57 SHA_HW_t H[22]; /* 5 state variables, 16 tmp values, 1 extra */
60 #if defined(_MSC_VER) && defined(_X86_)
61 #if defined(IS_LITTLE_ENDIAN)
62 #ifndef FORCEINLINE
63 #if (_MSC_VER >= 1200)
64 #define FORCEINLINE __forceinline
65 #else
66 #define FORCEINLINE __inline
67 #endif /* _MSC_VER */
68 #endif /* !defined FORCEINLINE */
69 #define FASTCALL __fastcall
71 static FORCEINLINE PRUint32 FASTCALL
72 swap4b(PRUint32 dwd)
74 __asm {
75 mov eax,dwd
76 bswap eax
80 #define SHA_HTONL(x) swap4b(x)
81 #endif /* IS_LITTLE_ENDIAN */
83 #pragma intrinsic (_lrotr, _lrotl)
84 #define SHA_ROTL(x,n) _lrotl(x,n)
85 #define SHA_ROTL_IS_DEFINED 1
86 #endif /* _MSC_VER && _X86_ */
88 #if defined(__GNUC__)
89 /* __x86_64__ and __x86_64 are defined by GCC on x86_64 CPUs */
91 #if defined( SHA1_USING_64_BIT )
92 static __inline__ PRUint64 SHA_ROTL(PRUint64 x, PRUint32 n)
94 PRUint32 t = (PRUint32)x;
95 return ((t << n) | (t >> (32 - n)));
97 #else
98 static __inline__ PRUint32 SHA_ROTL(PRUint32 t, PRUint32 n)
100 return ((t << n) | (t >> (32 - n)));
102 #endif
103 #define SHA_ROTL_IS_DEFINED 1
105 #if defined(_X86_) || defined(__x86_64__) || defined(__x86_64)
106 static __inline__ PRUint32 swap4b(PRUint32 value)
108 __asm__("bswap %0" : "+r" (value));
109 return (value);
111 #define SHA_HTONL(x) swap4b(x)
112 #endif /* x86 family */
114 #endif /* __GNUC__ */
116 #if !defined(SHA_ROTL_IS_DEFINED)
117 #define SHA_NEED_TMP_VARIABLE 1
118 #define SHA_ROTL(X,n) (tmp = (X), ((tmp) << (n)) | ((tmp) >> (32-(n))))
119 #endif
121 #if defined(_X86_) || defined(__x86_64__) || defined(__x86_64)
122 #define SHA_ALLOW_UNALIGNED_ACCESS 1
123 #endif
125 #if !defined(SHA_HTONL)
126 #define SHA_MASK 0x00FF00FF
127 #if defined(IS_LITTLE_ENDIAN)
128 #undef SHA_NEED_TMP_VARIABLE
129 #define SHA_NEED_TMP_VARIABLE 1
130 #define SHA_HTONL(x) (tmp = (x), tmp = (tmp << 16) | (tmp >> 16), \
131 ((tmp & SHA_MASK) << 8) | ((tmp >> 8) & SHA_MASK))
132 #else
133 #define SHA_HTONL(x) (x)
134 #endif
135 #endif
137 #define SHA_BYTESWAP(x) x = SHA_HTONL(x)
139 #define SHA_STORE(n) ((PRUint32*)hashout)[n] = SHA_HTONL(ctx->H[n])
140 #if defined(SHA_ALLOW_UNALIGNED_ACCESS)
141 #define SHA_STORE_RESULT \
142 SHA_STORE(0); \
143 SHA_STORE(1); \
144 SHA_STORE(2); \
145 SHA_STORE(3); \
146 SHA_STORE(4);
148 #elif defined(IS_LITTLE_ENDIAN) || defined( SHA1_USING_64_BIT )
149 #define SHA_STORE_RESULT \
150 if (!((ptrdiff_t)hashout % sizeof(PRUint32))) { \
151 SHA_STORE(0); \
152 SHA_STORE(1); \
153 SHA_STORE(2); \
154 SHA_STORE(3); \
155 SHA_STORE(4); \
156 } else { \
157 ctx->u.w[0] = SHA_HTONL(ctx->H[0]); \
158 ctx->u.w[1] = SHA_HTONL(ctx->H[1]); \
159 ctx->u.w[2] = SHA_HTONL(ctx->H[2]); \
160 ctx->u.w[3] = SHA_HTONL(ctx->H[3]); \
161 ctx->u.w[4] = SHA_HTONL(ctx->H[4]); \
162 memcpy(hashout, ctx->u.w, SHA1_LENGTH); \
165 #else
166 #define SHA_STORE_RESULT \
167 if (!((ptrdiff_t)hashout % sizeof(PRUint32))) { \
168 SHA_STORE(0); \
169 SHA_STORE(1); \
170 SHA_STORE(2); \
171 SHA_STORE(3); \
172 SHA_STORE(4); \
173 } else { \
174 memcpy(hashout, ctx->H, SHA1_LENGTH); \
176 #endif
178 #endif /* _SHA_FAST_H_ */