nss: import at 3.0.1 beta 1
[mozilla-nss.git] / security / nss / lib / freebl / sha_fast.h
blobd579ebad463b4981a387a969862ca3f7ce5c92a5
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)
61 #include <stdlib.h>
62 #if defined(IS_LITTLE_ENDIAN)
63 #if (_MSC_VER >= 1300)
64 #pragma intrinsic(_byteswap_ulong)
65 #define SHA_HTONL(x) _byteswap_ulong(x)
66 #elif defined(_X86_)
67 #ifndef FORCEINLINE
68 #if (_MSC_VER >= 1200)
69 #define FORCEINLINE __forceinline
70 #else
71 #define FORCEINLINE __inline
72 #endif /* _MSC_VER */
73 #endif /* !defined FORCEINLINE */
74 #define FASTCALL __fastcall
76 static FORCEINLINE PRUint32 FASTCALL
77 swap4b(PRUint32 dwd)
79 __asm {
80 mov eax,dwd
81 bswap eax
85 #define SHA_HTONL(x) swap4b(x)
86 #endif /* _X86_ */
87 #endif /* IS_LITTLE_ENDIAN */
89 #pragma intrinsic (_lrotr, _lrotl)
90 #define SHA_ROTL(x,n) _lrotl(x,n)
91 #define SHA_ROTL_IS_DEFINED 1
92 #endif /* _MSC_VER */
94 #if defined(__GNUC__)
95 /* __x86_64__ and __x86_64 are defined by GCC on x86_64 CPUs */
97 #if defined( SHA1_USING_64_BIT )
98 static __inline__ PRUint64 SHA_ROTL(PRUint64 x, PRUint32 n)
100 PRUint32 t = (PRUint32)x;
101 return ((t << n) | (t >> (32 - n)));
103 #else
104 static __inline__ PRUint32 SHA_ROTL(PRUint32 t, PRUint32 n)
106 return ((t << n) | (t >> (32 - n)));
108 #endif
109 #define SHA_ROTL_IS_DEFINED 1
111 #if defined(_X86_) || defined(__x86_64__) || defined(__x86_64)
112 static __inline__ PRUint32 swap4b(PRUint32 value)
114 __asm__("bswap %0" : "+r" (value));
115 return (value);
117 #define SHA_HTONL(x) swap4b(x)
118 #endif /* x86 family */
120 #endif /* __GNUC__ */
122 #if !defined(SHA_ROTL_IS_DEFINED)
123 #define SHA_NEED_TMP_VARIABLE 1
124 #define SHA_ROTL(X,n) (tmp = (X), ((tmp) << (n)) | ((tmp) >> (32-(n))))
125 #endif
127 #if defined(_X86_) || defined(__x86_64__) || defined(__x86_64)
128 #define SHA_ALLOW_UNALIGNED_ACCESS 1
129 #endif
131 #if !defined(SHA_HTONL)
132 #define SHA_MASK 0x00FF00FF
133 #if defined(IS_LITTLE_ENDIAN)
134 #undef SHA_NEED_TMP_VARIABLE
135 #define SHA_NEED_TMP_VARIABLE 1
136 #define SHA_HTONL(x) (tmp = (x), tmp = (tmp << 16) | (tmp >> 16), \
137 ((tmp & SHA_MASK) << 8) | ((tmp >> 8) & SHA_MASK))
138 #else
139 #define SHA_HTONL(x) (x)
140 #endif
141 #endif
143 #define SHA_BYTESWAP(x) x = SHA_HTONL(x)
145 #define SHA_STORE(n) ((PRUint32*)hashout)[n] = SHA_HTONL(ctx->H[n])
146 #if defined(SHA_ALLOW_UNALIGNED_ACCESS)
147 #define SHA_STORE_RESULT \
148 SHA_STORE(0); \
149 SHA_STORE(1); \
150 SHA_STORE(2); \
151 SHA_STORE(3); \
152 SHA_STORE(4);
154 #elif defined(IS_LITTLE_ENDIAN) || defined( SHA1_USING_64_BIT )
155 #define SHA_STORE_RESULT \
156 if (!((ptrdiff_t)hashout % sizeof(PRUint32))) { \
157 SHA_STORE(0); \
158 SHA_STORE(1); \
159 SHA_STORE(2); \
160 SHA_STORE(3); \
161 SHA_STORE(4); \
162 } else { \
163 ctx->u.w[0] = SHA_HTONL(ctx->H[0]); \
164 ctx->u.w[1] = SHA_HTONL(ctx->H[1]); \
165 ctx->u.w[2] = SHA_HTONL(ctx->H[2]); \
166 ctx->u.w[3] = SHA_HTONL(ctx->H[3]); \
167 ctx->u.w[4] = SHA_HTONL(ctx->H[4]); \
168 memcpy(hashout, ctx->u.w, SHA1_LENGTH); \
171 #else
172 #define SHA_STORE_RESULT \
173 if (!((ptrdiff_t)hashout % sizeof(PRUint32))) { \
174 SHA_STORE(0); \
175 SHA_STORE(1); \
176 SHA_STORE(2); \
177 SHA_STORE(3); \
178 SHA_STORE(4); \
179 } else { \
180 memcpy(hashout, ctx->H, SHA1_LENGTH); \
182 #endif
184 #endif /* _SHA_FAST_H_ */