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 */
60 #if defined(_MSC_VER) && defined(_X86_)
61 #if defined(IS_LITTLE_ENDIAN)
63 #if (_MSC_VER >= 1200)
64 #define FORCEINLINE __forceinline
66 #define FORCEINLINE __inline
68 #endif /* !defined FORCEINLINE */
69 #define FASTCALL __fastcall
71 static FORCEINLINE PRUint32 FASTCALL
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_ */
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
)));
98 static __inline__ PRUint32
SHA_ROTL(PRUint32 t
, PRUint32 n
)
100 return ((t
<< n
) | (t
>> (32 - n
)));
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
));
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))))
121 #if defined(_X86_) || defined(__x86_64__) || defined(__x86_64)
122 #define SHA_ALLOW_UNALIGNED_ACCESS 1
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))
133 #define SHA_HTONL(x) (x)
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 \
148 #elif defined(IS_LITTLE_ENDIAN) || defined( SHA1_USING_64_BIT )
149 #define SHA_STORE_RESULT \
150 if (!((ptrdiff_t)hashout % sizeof(PRUint32))) { \
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); \
166 #define SHA_STORE_RESULT \
167 if (!((ptrdiff_t)hashout % sizeof(PRUint32))) { \
174 memcpy(hashout, ctx->H, SHA1_LENGTH); \
178 #endif /* _SHA_FAST_H_ */