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
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.
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 ***** */
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
48 typedef PRUint32 SHA_HW_t
;
51 struct SHA1ContextStr
{
53 PRUint32 w
[16]; /* input buffer */
56 PRUint64 size
; /* count of hashed bytes. */
57 SHA_HW_t H
[22]; /* 5 state variables, 16 tmp values, 1 extra */
62 #if defined(IS_LITTLE_ENDIAN)
63 #if (_MSC_VER >= 1300)
64 #pragma intrinsic(_byteswap_ulong)
65 #define SHA_HTONL(x) _byteswap_ulong(x)
68 #if (_MSC_VER >= 1200)
69 #define FORCEINLINE __forceinline
71 #define FORCEINLINE __inline
73 #endif /* !defined FORCEINLINE */
74 #define FASTCALL __fastcall
76 static FORCEINLINE PRUint32 FASTCALL
85 #define SHA_HTONL(x) swap4b(x)
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
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
)));
104 static __inline__ PRUint32
SHA_ROTL(PRUint32 t
, PRUint32 n
)
106 return ((t
<< n
) | (t
>> (32 - n
)));
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
));
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))))
127 #if defined(_X86_) || defined(__x86_64__) || defined(__x86_64)
128 #define SHA_ALLOW_UNALIGNED_ACCESS 1
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))
139 #define SHA_HTONL(x) (x)
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 \
154 #elif defined(IS_LITTLE_ENDIAN) || defined( SHA1_USING_64_BIT )
155 #define SHA_STORE_RESULT \
156 if (!((ptrdiff_t)hashout % sizeof(PRUint32))) { \
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); \
172 #define SHA_STORE_RESULT \
173 if (!((ptrdiff_t)hashout % sizeof(PRUint32))) { \
180 memcpy(hashout, ctx->H, SHA1_LENGTH); \
184 #endif /* _SHA_FAST_H_ */