xref: /onnv-gate/usr/src/common/openssl/crypto/md32_common.h (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /* crypto/md32_common.h */
2*0Sstevel@tonic-gate /* ====================================================================
3*0Sstevel@tonic-gate  * Copyright (c) 1999-2002 The OpenSSL Project.  All rights reserved.
4*0Sstevel@tonic-gate  *
5*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
6*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
7*0Sstevel@tonic-gate  * are met:
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
10*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
11*0Sstevel@tonic-gate  *
12*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
13*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
14*0Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
15*0Sstevel@tonic-gate  *    distribution.
16*0Sstevel@tonic-gate  *
17*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
18*0Sstevel@tonic-gate  *    software must display the following acknowledgment:
19*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
20*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
21*0Sstevel@tonic-gate  *
22*0Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23*0Sstevel@tonic-gate  *    endorse or promote products derived from this software without
24*0Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
25*0Sstevel@tonic-gate  *    licensing@OpenSSL.org.
26*0Sstevel@tonic-gate  *
27*0Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
28*0Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
29*0Sstevel@tonic-gate  *    permission of the OpenSSL Project.
30*0Sstevel@tonic-gate  *
31*0Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
32*0Sstevel@tonic-gate  *    acknowledgment:
33*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
34*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
35*0Sstevel@tonic-gate  *
36*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37*0Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39*0Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40*0Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41*0Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42*0Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43*0Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45*0Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46*0Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47*0Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
48*0Sstevel@tonic-gate  * ====================================================================
49*0Sstevel@tonic-gate  *
50*0Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
51*0Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
52*0Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
53*0Sstevel@tonic-gate  *
54*0Sstevel@tonic-gate  */
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate /*
57*0Sstevel@tonic-gate  * This is a generic 32 bit "collector" for message digest algorithms.
58*0Sstevel@tonic-gate  * Whenever needed it collects input character stream into chunks of
59*0Sstevel@tonic-gate  * 32 bit values and invokes a block function that performs actual hash
60*0Sstevel@tonic-gate  * calculations.
61*0Sstevel@tonic-gate  *
62*0Sstevel@tonic-gate  * Porting guide.
63*0Sstevel@tonic-gate  *
64*0Sstevel@tonic-gate  * Obligatory macros:
65*0Sstevel@tonic-gate  *
66*0Sstevel@tonic-gate  * DATA_ORDER_IS_BIG_ENDIAN or DATA_ORDER_IS_LITTLE_ENDIAN
67*0Sstevel@tonic-gate  *	this macro defines byte order of input stream.
68*0Sstevel@tonic-gate  * HASH_CBLOCK
69*0Sstevel@tonic-gate  *	size of a unit chunk HASH_BLOCK operates on.
70*0Sstevel@tonic-gate  * HASH_LONG
71*0Sstevel@tonic-gate  *	has to be at lest 32 bit wide, if it's wider, then
72*0Sstevel@tonic-gate  *	HASH_LONG_LOG2 *has to* be defined along
73*0Sstevel@tonic-gate  * HASH_CTX
74*0Sstevel@tonic-gate  *	context structure that at least contains following
75*0Sstevel@tonic-gate  *	members:
76*0Sstevel@tonic-gate  *		typedef struct {
77*0Sstevel@tonic-gate  *			...
78*0Sstevel@tonic-gate  *			HASH_LONG	Nl,Nh;
79*0Sstevel@tonic-gate  *			HASH_LONG	data[HASH_LBLOCK];
80*0Sstevel@tonic-gate  *			int		num;
81*0Sstevel@tonic-gate  *			...
82*0Sstevel@tonic-gate  *			} HASH_CTX;
83*0Sstevel@tonic-gate  * HASH_UPDATE
84*0Sstevel@tonic-gate  *	name of "Update" function, implemented here.
85*0Sstevel@tonic-gate  * HASH_TRANSFORM
86*0Sstevel@tonic-gate  *	name of "Transform" function, implemented here.
87*0Sstevel@tonic-gate  * HASH_FINAL
88*0Sstevel@tonic-gate  *	name of "Final" function, implemented here.
89*0Sstevel@tonic-gate  * HASH_BLOCK_HOST_ORDER
90*0Sstevel@tonic-gate  *	name of "block" function treating *aligned* input message
91*0Sstevel@tonic-gate  *	in host byte order, implemented externally.
92*0Sstevel@tonic-gate  * HASH_BLOCK_DATA_ORDER
93*0Sstevel@tonic-gate  *	name of "block" function treating *unaligned* input message
94*0Sstevel@tonic-gate  *	in original (data) byte order, implemented externally (it
95*0Sstevel@tonic-gate  *	actually is optional if data and host are of the same
96*0Sstevel@tonic-gate  *	"endianess").
97*0Sstevel@tonic-gate  * HASH_MAKE_STRING
98*0Sstevel@tonic-gate  *	macro convering context variables to an ASCII hash string.
99*0Sstevel@tonic-gate  *
100*0Sstevel@tonic-gate  * Optional macros:
101*0Sstevel@tonic-gate  *
102*0Sstevel@tonic-gate  * B_ENDIAN or L_ENDIAN
103*0Sstevel@tonic-gate  *	defines host byte-order.
104*0Sstevel@tonic-gate  * HASH_LONG_LOG2
105*0Sstevel@tonic-gate  *	defaults to 2 if not states otherwise.
106*0Sstevel@tonic-gate  * HASH_LBLOCK
107*0Sstevel@tonic-gate  *	assumed to be HASH_CBLOCK/4 if not stated otherwise.
108*0Sstevel@tonic-gate  * HASH_BLOCK_DATA_ORDER_ALIGNED
109*0Sstevel@tonic-gate  *	alternative "block" function capable of treating
110*0Sstevel@tonic-gate  *	aligned input message in original (data) order,
111*0Sstevel@tonic-gate  *	implemented externally.
112*0Sstevel@tonic-gate  *
113*0Sstevel@tonic-gate  * MD5 example:
114*0Sstevel@tonic-gate  *
115*0Sstevel@tonic-gate  *	#define DATA_ORDER_IS_LITTLE_ENDIAN
116*0Sstevel@tonic-gate  *
117*0Sstevel@tonic-gate  *	#define HASH_LONG		MD5_LONG
118*0Sstevel@tonic-gate  *	#define HASH_LONG_LOG2		MD5_LONG_LOG2
119*0Sstevel@tonic-gate  *	#define HASH_CTX		MD5_CTX
120*0Sstevel@tonic-gate  *	#define HASH_CBLOCK		MD5_CBLOCK
121*0Sstevel@tonic-gate  *	#define HASH_LBLOCK		MD5_LBLOCK
122*0Sstevel@tonic-gate  *	#define HASH_UPDATE		MD5_Update
123*0Sstevel@tonic-gate  *	#define HASH_TRANSFORM		MD5_Transform
124*0Sstevel@tonic-gate  *	#define HASH_FINAL		MD5_Final
125*0Sstevel@tonic-gate  *	#define HASH_BLOCK_HOST_ORDER	md5_block_host_order
126*0Sstevel@tonic-gate  *	#define HASH_BLOCK_DATA_ORDER	md5_block_data_order
127*0Sstevel@tonic-gate  *
128*0Sstevel@tonic-gate  *					<appro@fy.chalmers.se>
129*0Sstevel@tonic-gate  */
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate #if !defined(DATA_ORDER_IS_BIG_ENDIAN) && !defined(DATA_ORDER_IS_LITTLE_ENDIAN)
132*0Sstevel@tonic-gate #error "DATA_ORDER must be defined!"
133*0Sstevel@tonic-gate #endif
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate #ifndef HASH_CBLOCK
136*0Sstevel@tonic-gate #error "HASH_CBLOCK must be defined!"
137*0Sstevel@tonic-gate #endif
138*0Sstevel@tonic-gate #ifndef HASH_LONG
139*0Sstevel@tonic-gate #error "HASH_LONG must be defined!"
140*0Sstevel@tonic-gate #endif
141*0Sstevel@tonic-gate #ifndef HASH_CTX
142*0Sstevel@tonic-gate #error "HASH_CTX must be defined!"
143*0Sstevel@tonic-gate #endif
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate #ifndef HASH_UPDATE
146*0Sstevel@tonic-gate #error "HASH_UPDATE must be defined!"
147*0Sstevel@tonic-gate #endif
148*0Sstevel@tonic-gate #ifndef HASH_TRANSFORM
149*0Sstevel@tonic-gate #error "HASH_TRANSFORM must be defined!"
150*0Sstevel@tonic-gate #endif
151*0Sstevel@tonic-gate #ifndef HASH_FINAL
152*0Sstevel@tonic-gate #error "HASH_FINAL must be defined!"
153*0Sstevel@tonic-gate #endif
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate #ifndef HASH_BLOCK_HOST_ORDER
156*0Sstevel@tonic-gate #error "HASH_BLOCK_HOST_ORDER must be defined!"
157*0Sstevel@tonic-gate #endif
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate #if 0
160*0Sstevel@tonic-gate /*
161*0Sstevel@tonic-gate  * Moved below as it's required only if HASH_BLOCK_DATA_ORDER_ALIGNED
162*0Sstevel@tonic-gate  * isn't defined.
163*0Sstevel@tonic-gate  */
164*0Sstevel@tonic-gate #ifndef HASH_BLOCK_DATA_ORDER
165*0Sstevel@tonic-gate #error "HASH_BLOCK_DATA_ORDER must be defined!"
166*0Sstevel@tonic-gate #endif
167*0Sstevel@tonic-gate #endif
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate #ifndef HASH_LBLOCK
170*0Sstevel@tonic-gate #define HASH_LBLOCK	(HASH_CBLOCK/4)
171*0Sstevel@tonic-gate #endif
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate #ifndef HASH_LONG_LOG2
174*0Sstevel@tonic-gate #define HASH_LONG_LOG2	2
175*0Sstevel@tonic-gate #endif
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate /*
178*0Sstevel@tonic-gate  * Engage compiler specific rotate intrinsic function if available.
179*0Sstevel@tonic-gate  */
180*0Sstevel@tonic-gate #undef ROTATE
181*0Sstevel@tonic-gate #ifndef PEDANTIC
182*0Sstevel@tonic-gate # if 0 /* defined(_MSC_VER) */
183*0Sstevel@tonic-gate #  define ROTATE(a,n)	_lrotl(a,n)
184*0Sstevel@tonic-gate # elif defined(__MWERKS__)
185*0Sstevel@tonic-gate #  if defined(__POWERPC__)
186*0Sstevel@tonic-gate #   define ROTATE(a,n)	__rlwinm(a,n,0,31)
187*0Sstevel@tonic-gate #  elif defined(__MC68K__)
188*0Sstevel@tonic-gate     /* Motorola specific tweak. <appro@fy.chalmers.se> */
189*0Sstevel@tonic-gate #   define ROTATE(a,n)	( n<24 ? __rol(a,n) : __ror(a,32-n) )
190*0Sstevel@tonic-gate #  else
191*0Sstevel@tonic-gate #   define ROTATE(a,n)	__rol(a,n)
192*0Sstevel@tonic-gate #  endif
193*0Sstevel@tonic-gate # elif defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
194*0Sstevel@tonic-gate   /*
195*0Sstevel@tonic-gate    * Some GNU C inline assembler templates. Note that these are
196*0Sstevel@tonic-gate    * rotates by *constant* number of bits! But that's exactly
197*0Sstevel@tonic-gate    * what we need here...
198*0Sstevel@tonic-gate    *
199*0Sstevel@tonic-gate    * 					<appro@fy.chalmers.se>
200*0Sstevel@tonic-gate    */
201*0Sstevel@tonic-gate #  if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
202*0Sstevel@tonic-gate #   define ROTATE(a,n)	({ register unsigned int ret;	\
203*0Sstevel@tonic-gate 				asm (			\
204*0Sstevel@tonic-gate 				"roll %1,%0"		\
205*0Sstevel@tonic-gate 				: "=r"(ret)		\
206*0Sstevel@tonic-gate 				: "I"(n), "0"(a)	\
207*0Sstevel@tonic-gate 				: "cc");		\
208*0Sstevel@tonic-gate 			   ret;				\
209*0Sstevel@tonic-gate 			})
210*0Sstevel@tonic-gate #  elif defined(__powerpc) || defined(__ppc)
211*0Sstevel@tonic-gate #   define ROTATE(a,n)	({ register unsigned int ret;	\
212*0Sstevel@tonic-gate 				asm (			\
213*0Sstevel@tonic-gate 				"rlwinm %0,%1,%2,0,31"	\
214*0Sstevel@tonic-gate 				: "=r"(ret)		\
215*0Sstevel@tonic-gate 				: "r"(a), "I"(n));	\
216*0Sstevel@tonic-gate 			   ret;				\
217*0Sstevel@tonic-gate 			})
218*0Sstevel@tonic-gate #  endif
219*0Sstevel@tonic-gate # endif
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate /*
222*0Sstevel@tonic-gate  * Engage compiler specific "fetch in reverse byte order"
223*0Sstevel@tonic-gate  * intrinsic function if available.
224*0Sstevel@tonic-gate  */
225*0Sstevel@tonic-gate # if defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
226*0Sstevel@tonic-gate   /* some GNU C inline assembler templates by <appro@fy.chalmers.se> */
227*0Sstevel@tonic-gate #  if (defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)) && !defined(I386_ONLY)
228*0Sstevel@tonic-gate #   define BE_FETCH32(a)	({ register unsigned int l=(a);\
229*0Sstevel@tonic-gate 				asm (			\
230*0Sstevel@tonic-gate 				"bswapl %0"		\
231*0Sstevel@tonic-gate 				: "=r"(l) : "0"(l));	\
232*0Sstevel@tonic-gate 			  l;				\
233*0Sstevel@tonic-gate 			})
234*0Sstevel@tonic-gate #  elif defined(__powerpc)
235*0Sstevel@tonic-gate #   define LE_FETCH32(a)	({ register unsigned int l;	\
236*0Sstevel@tonic-gate 				asm (			\
237*0Sstevel@tonic-gate 				"lwbrx %0,0,%1"		\
238*0Sstevel@tonic-gate 				: "=r"(l)		\
239*0Sstevel@tonic-gate 				: "r"(a));		\
240*0Sstevel@tonic-gate 			   l;				\
241*0Sstevel@tonic-gate 			})
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate #  elif defined(__sparc) && defined(OPENSSL_SYS_ULTRASPARC)
244*0Sstevel@tonic-gate #  define LE_FETCH32(a)	({ register unsigned int l;		\
245*0Sstevel@tonic-gate 				asm (				\
246*0Sstevel@tonic-gate 				"lda [%1]#ASI_PRIMARY_LITTLE,%0"\
247*0Sstevel@tonic-gate 				: "=r"(l)			\
248*0Sstevel@tonic-gate 				: "r"(a));			\
249*0Sstevel@tonic-gate 			   l;					\
250*0Sstevel@tonic-gate 			})
251*0Sstevel@tonic-gate #  endif
252*0Sstevel@tonic-gate # endif
253*0Sstevel@tonic-gate #endif /* PEDANTIC */
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate #if HASH_LONG_LOG2==2	/* Engage only if sizeof(HASH_LONG)== 4 */
256*0Sstevel@tonic-gate /* A nice byte order reversal from Wei Dai <weidai@eskimo.com> */
257*0Sstevel@tonic-gate #ifdef ROTATE
258*0Sstevel@tonic-gate /* 5 instructions with rotate instruction, else 9 */
259*0Sstevel@tonic-gate #define REVERSE_FETCH32(a,l)	(					\
260*0Sstevel@tonic-gate 		l=*(const HASH_LONG *)(a),				\
261*0Sstevel@tonic-gate 		((ROTATE(l,8)&0x00FF00FF)|(ROTATE((l&0x00FF00FF),24)))	\
262*0Sstevel@tonic-gate 				)
263*0Sstevel@tonic-gate #else
264*0Sstevel@tonic-gate /* 6 instructions with rotate instruction, else 8 */
265*0Sstevel@tonic-gate #define REVERSE_FETCH32(a,l)	(				\
266*0Sstevel@tonic-gate 		l=*(const HASH_LONG *)(a),			\
267*0Sstevel@tonic-gate 		l=(((l>>8)&0x00FF00FF)|((l&0x00FF00FF)<<8)),	\
268*0Sstevel@tonic-gate 		ROTATE(l,16)					\
269*0Sstevel@tonic-gate 				)
270*0Sstevel@tonic-gate /*
271*0Sstevel@tonic-gate  * Originally the middle line started with l=(((l&0xFF00FF00)>>8)|...
272*0Sstevel@tonic-gate  * It's rewritten as above for two reasons:
273*0Sstevel@tonic-gate  *	- RISCs aren't good at long constants and have to explicitely
274*0Sstevel@tonic-gate  *	  compose 'em with several (well, usually 2) instructions in a
275*0Sstevel@tonic-gate  *	  register before performing the actual operation and (as you
276*0Sstevel@tonic-gate  *	  already realized:-) having same constant should inspire the
277*0Sstevel@tonic-gate  *	  compiler to permanently allocate the only register for it;
278*0Sstevel@tonic-gate  *	- most modern CPUs have two ALUs, but usually only one has
279*0Sstevel@tonic-gate  *	  circuitry for shifts:-( this minor tweak inspires compiler
280*0Sstevel@tonic-gate  *	  to schedule shift instructions in a better way...
281*0Sstevel@tonic-gate  *
282*0Sstevel@tonic-gate  *				<appro@fy.chalmers.se>
283*0Sstevel@tonic-gate  */
284*0Sstevel@tonic-gate #endif
285*0Sstevel@tonic-gate #endif
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate #ifndef ROTATE
288*0Sstevel@tonic-gate #define ROTATE(a,n)     (((a)<<(n))|(((a)&0xffffffff)>>(32-(n))))
289*0Sstevel@tonic-gate #endif
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate /*
292*0Sstevel@tonic-gate  * Make some obvious choices. E.g., HASH_BLOCK_DATA_ORDER_ALIGNED
293*0Sstevel@tonic-gate  * and HASH_BLOCK_HOST_ORDER ought to be the same if input data
294*0Sstevel@tonic-gate  * and host are of the same "endianess". It's possible to mask
295*0Sstevel@tonic-gate  * this with blank #define HASH_BLOCK_DATA_ORDER though...
296*0Sstevel@tonic-gate  *
297*0Sstevel@tonic-gate  *				<appro@fy.chalmers.se>
298*0Sstevel@tonic-gate  */
299*0Sstevel@tonic-gate #if defined(B_ENDIAN)
300*0Sstevel@tonic-gate #  if defined(DATA_ORDER_IS_BIG_ENDIAN)
301*0Sstevel@tonic-gate #    if !defined(HASH_BLOCK_DATA_ORDER_ALIGNED) && HASH_LONG_LOG2==2
302*0Sstevel@tonic-gate #      define HASH_BLOCK_DATA_ORDER_ALIGNED	HASH_BLOCK_HOST_ORDER
303*0Sstevel@tonic-gate #    endif
304*0Sstevel@tonic-gate #  elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
305*0Sstevel@tonic-gate #    ifndef HOST_FETCH32
306*0Sstevel@tonic-gate #      ifdef LE_FETCH32
307*0Sstevel@tonic-gate #        define HOST_FETCH32(p,l)	LE_FETCH32(p)
308*0Sstevel@tonic-gate #      elif defined(REVERSE_FETCH32)
309*0Sstevel@tonic-gate #        define HOST_FETCH32(p,l)	REVERSE_FETCH32(p,l)
310*0Sstevel@tonic-gate #      endif
311*0Sstevel@tonic-gate #    endif
312*0Sstevel@tonic-gate #  endif
313*0Sstevel@tonic-gate #elif defined(L_ENDIAN)
314*0Sstevel@tonic-gate #  if defined(DATA_ORDER_IS_LITTLE_ENDIAN)
315*0Sstevel@tonic-gate #    if !defined(HASH_BLOCK_DATA_ORDER_ALIGNED) && HASH_LONG_LOG2==2
316*0Sstevel@tonic-gate #      define HASH_BLOCK_DATA_ORDER_ALIGNED	HASH_BLOCK_HOST_ORDER
317*0Sstevel@tonic-gate #    endif
318*0Sstevel@tonic-gate #  elif defined(DATA_ORDER_IS_BIG_ENDIAN)
319*0Sstevel@tonic-gate #    ifndef HOST_FETCH32
320*0Sstevel@tonic-gate #      ifdef BE_FETCH32
321*0Sstevel@tonic-gate #        define HOST_FETCH32(p,l)	BE_FETCH32(p)
322*0Sstevel@tonic-gate #      elif defined(REVERSE_FETCH32)
323*0Sstevel@tonic-gate #        define HOST_FETCH32(p,l)	REVERSE_FETCH32(p,l)
324*0Sstevel@tonic-gate #      endif
325*0Sstevel@tonic-gate #    endif
326*0Sstevel@tonic-gate #  endif
327*0Sstevel@tonic-gate #endif
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate #if !defined(HASH_BLOCK_DATA_ORDER_ALIGNED)
330*0Sstevel@tonic-gate #ifndef HASH_BLOCK_DATA_ORDER
331*0Sstevel@tonic-gate #error "HASH_BLOCK_DATA_ORDER must be defined!"
332*0Sstevel@tonic-gate #endif
333*0Sstevel@tonic-gate #endif
334*0Sstevel@tonic-gate 
335*0Sstevel@tonic-gate #if defined(DATA_ORDER_IS_BIG_ENDIAN)
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate #define HOST_c2l(c,l)	(l =(((unsigned long)(*((c)++)))<<24),		\
338*0Sstevel@tonic-gate 			 l|=(((unsigned long)(*((c)++)))<<16),		\
339*0Sstevel@tonic-gate 			 l|=(((unsigned long)(*((c)++)))<< 8),		\
340*0Sstevel@tonic-gate 			 l|=(((unsigned long)(*((c)++)))    ),		\
341*0Sstevel@tonic-gate 			 l)
342*0Sstevel@tonic-gate #define HOST_p_c2l(c,l,n)	{					\
343*0Sstevel@tonic-gate 			switch (n) {					\
344*0Sstevel@tonic-gate 			case 0: l =((unsigned long)(*((c)++)))<<24;	\
345*0Sstevel@tonic-gate 			case 1: l|=((unsigned long)(*((c)++)))<<16;	\
346*0Sstevel@tonic-gate 			case 2: l|=((unsigned long)(*((c)++)))<< 8;	\
347*0Sstevel@tonic-gate 			case 3: l|=((unsigned long)(*((c)++)));		\
348*0Sstevel@tonic-gate 				} }
349*0Sstevel@tonic-gate #define HOST_p_c2l_p(c,l,sc,len) {					\
350*0Sstevel@tonic-gate 			switch (sc) {					\
351*0Sstevel@tonic-gate 			case 0: l =((unsigned long)(*((c)++)))<<24;	\
352*0Sstevel@tonic-gate 				if (--len == 0) break;			\
353*0Sstevel@tonic-gate 			case 1: l|=((unsigned long)(*((c)++)))<<16;	\
354*0Sstevel@tonic-gate 				if (--len == 0) break;			\
355*0Sstevel@tonic-gate 			case 2: l|=((unsigned long)(*((c)++)))<< 8;	\
356*0Sstevel@tonic-gate 				} }
357*0Sstevel@tonic-gate /* NOTE the pointer is not incremented at the end of this */
358*0Sstevel@tonic-gate #define HOST_c2l_p(c,l,n)	{					\
359*0Sstevel@tonic-gate 			l=0; (c)+=n;					\
360*0Sstevel@tonic-gate 			switch (n) {					\
361*0Sstevel@tonic-gate 			case 3: l =((unsigned long)(*(--(c))))<< 8;	\
362*0Sstevel@tonic-gate 			case 2: l|=((unsigned long)(*(--(c))))<<16;	\
363*0Sstevel@tonic-gate 			case 1: l|=((unsigned long)(*(--(c))))<<24;	\
364*0Sstevel@tonic-gate 				} }
365*0Sstevel@tonic-gate #define HOST_l2c(l,c)	(*((c)++)=(unsigned char)(((l)>>24)&0xff),	\
366*0Sstevel@tonic-gate 			 *((c)++)=(unsigned char)(((l)>>16)&0xff),	\
367*0Sstevel@tonic-gate 			 *((c)++)=(unsigned char)(((l)>> 8)&0xff),	\
368*0Sstevel@tonic-gate 			 *((c)++)=(unsigned char)(((l)    )&0xff),	\
369*0Sstevel@tonic-gate 			 l)
370*0Sstevel@tonic-gate 
371*0Sstevel@tonic-gate #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
372*0Sstevel@tonic-gate 
373*0Sstevel@tonic-gate #define HOST_c2l(c,l)	(l =(((unsigned long)(*((c)++)))    ),		\
374*0Sstevel@tonic-gate 			 l|=(((unsigned long)(*((c)++)))<< 8),		\
375*0Sstevel@tonic-gate 			 l|=(((unsigned long)(*((c)++)))<<16),		\
376*0Sstevel@tonic-gate 			 l|=(((unsigned long)(*((c)++)))<<24),		\
377*0Sstevel@tonic-gate 			 l)
378*0Sstevel@tonic-gate #define HOST_p_c2l(c,l,n)	{					\
379*0Sstevel@tonic-gate 			switch (n) {					\
380*0Sstevel@tonic-gate 			case 0: l =((unsigned long)(*((c)++)));		\
381*0Sstevel@tonic-gate 			case 1: l|=((unsigned long)(*((c)++)))<< 8;	\
382*0Sstevel@tonic-gate 			case 2: l|=((unsigned long)(*((c)++)))<<16;	\
383*0Sstevel@tonic-gate 			case 3: l|=((unsigned long)(*((c)++)))<<24;	\
384*0Sstevel@tonic-gate 				} }
385*0Sstevel@tonic-gate #define HOST_p_c2l_p(c,l,sc,len) {					\
386*0Sstevel@tonic-gate 			switch (sc) {					\
387*0Sstevel@tonic-gate 			case 0: l =((unsigned long)(*((c)++)));		\
388*0Sstevel@tonic-gate 				if (--len == 0) break;			\
389*0Sstevel@tonic-gate 			case 1: l|=((unsigned long)(*((c)++)))<< 8;	\
390*0Sstevel@tonic-gate 				if (--len == 0) break;			\
391*0Sstevel@tonic-gate 			case 2: l|=((unsigned long)(*((c)++)))<<16;	\
392*0Sstevel@tonic-gate 				} }
393*0Sstevel@tonic-gate /* NOTE the pointer is not incremented at the end of this */
394*0Sstevel@tonic-gate #define HOST_c2l_p(c,l,n)	{					\
395*0Sstevel@tonic-gate 			l=0; (c)+=n;					\
396*0Sstevel@tonic-gate 			switch (n) {					\
397*0Sstevel@tonic-gate 			case 3: l =((unsigned long)(*(--(c))))<<16;	\
398*0Sstevel@tonic-gate 			case 2: l|=((unsigned long)(*(--(c))))<< 8;	\
399*0Sstevel@tonic-gate 			case 1: l|=((unsigned long)(*(--(c))));		\
400*0Sstevel@tonic-gate 				} }
401*0Sstevel@tonic-gate #define HOST_l2c(l,c)	(*((c)++)=(unsigned char)(((l)    )&0xff),	\
402*0Sstevel@tonic-gate 			 *((c)++)=(unsigned char)(((l)>> 8)&0xff),	\
403*0Sstevel@tonic-gate 			 *((c)++)=(unsigned char)(((l)>>16)&0xff),	\
404*0Sstevel@tonic-gate 			 *((c)++)=(unsigned char)(((l)>>24)&0xff),	\
405*0Sstevel@tonic-gate 			 l)
406*0Sstevel@tonic-gate 
407*0Sstevel@tonic-gate #endif
408*0Sstevel@tonic-gate 
409*0Sstevel@tonic-gate /*
410*0Sstevel@tonic-gate  * Time for some action:-)
411*0Sstevel@tonic-gate  */
412*0Sstevel@tonic-gate 
413*0Sstevel@tonic-gate int HASH_UPDATE (HASH_CTX *c, const void *data_, unsigned long len)
414*0Sstevel@tonic-gate 	{
415*0Sstevel@tonic-gate 	const unsigned char *data=data_;
416*0Sstevel@tonic-gate 	register HASH_LONG * p;
417*0Sstevel@tonic-gate 	register unsigned long l;
418*0Sstevel@tonic-gate 	int sw,sc,ew,ec;
419*0Sstevel@tonic-gate 
420*0Sstevel@tonic-gate 	if (len==0) return 1;
421*0Sstevel@tonic-gate 
422*0Sstevel@tonic-gate 	l=(c->Nl+(len<<3))&0xffffffffL;
423*0Sstevel@tonic-gate 	/* 95-05-24 eay Fixed a bug with the overflow handling, thanks to
424*0Sstevel@tonic-gate 	 * Wei Dai <weidai@eskimo.com> for pointing it out. */
425*0Sstevel@tonic-gate 	if (l < c->Nl) /* overflow */
426*0Sstevel@tonic-gate 		c->Nh++;
427*0Sstevel@tonic-gate 	c->Nh+=(len>>29);
428*0Sstevel@tonic-gate 	c->Nl=l;
429*0Sstevel@tonic-gate 
430*0Sstevel@tonic-gate 	if (c->num != 0)
431*0Sstevel@tonic-gate 		{
432*0Sstevel@tonic-gate 		p=c->data;
433*0Sstevel@tonic-gate 		sw=c->num>>2;
434*0Sstevel@tonic-gate 		sc=c->num&0x03;
435*0Sstevel@tonic-gate 
436*0Sstevel@tonic-gate 		if ((c->num+len) >= HASH_CBLOCK)
437*0Sstevel@tonic-gate 			{
438*0Sstevel@tonic-gate 			l=p[sw]; HOST_p_c2l(data,l,sc); p[sw++]=l;
439*0Sstevel@tonic-gate 			for (; sw<HASH_LBLOCK; sw++)
440*0Sstevel@tonic-gate 				{
441*0Sstevel@tonic-gate 				HOST_c2l(data,l); p[sw]=l;
442*0Sstevel@tonic-gate 				}
443*0Sstevel@tonic-gate 			HASH_BLOCK_HOST_ORDER (c,p,1);
444*0Sstevel@tonic-gate 			len-=(HASH_CBLOCK-c->num);
445*0Sstevel@tonic-gate 			c->num=0;
446*0Sstevel@tonic-gate 			/* drop through and do the rest */
447*0Sstevel@tonic-gate 			}
448*0Sstevel@tonic-gate 		else
449*0Sstevel@tonic-gate 			{
450*0Sstevel@tonic-gate 			c->num+=len;
451*0Sstevel@tonic-gate 			if ((sc+len) < 4) /* ugly, add char's to a word */
452*0Sstevel@tonic-gate 				{
453*0Sstevel@tonic-gate 				l=p[sw]; HOST_p_c2l_p(data,l,sc,len); p[sw]=l;
454*0Sstevel@tonic-gate 				}
455*0Sstevel@tonic-gate 			else
456*0Sstevel@tonic-gate 				{
457*0Sstevel@tonic-gate 				ew=(c->num>>2);
458*0Sstevel@tonic-gate 				ec=(c->num&0x03);
459*0Sstevel@tonic-gate 				if (sc)
460*0Sstevel@tonic-gate 					l=p[sw];
461*0Sstevel@tonic-gate 				HOST_p_c2l(data,l,sc);
462*0Sstevel@tonic-gate 				p[sw++]=l;
463*0Sstevel@tonic-gate 				for (; sw < ew; sw++)
464*0Sstevel@tonic-gate 					{
465*0Sstevel@tonic-gate 					HOST_c2l(data,l); p[sw]=l;
466*0Sstevel@tonic-gate 					}
467*0Sstevel@tonic-gate 				if (ec)
468*0Sstevel@tonic-gate 					{
469*0Sstevel@tonic-gate 					HOST_c2l_p(data,l,ec); p[sw]=l;
470*0Sstevel@tonic-gate 					}
471*0Sstevel@tonic-gate 				}
472*0Sstevel@tonic-gate 			return 1;
473*0Sstevel@tonic-gate 			}
474*0Sstevel@tonic-gate 		}
475*0Sstevel@tonic-gate 
476*0Sstevel@tonic-gate 	sw=len/HASH_CBLOCK;
477*0Sstevel@tonic-gate 	if (sw > 0)
478*0Sstevel@tonic-gate 		{
479*0Sstevel@tonic-gate #if defined(HASH_BLOCK_DATA_ORDER_ALIGNED)
480*0Sstevel@tonic-gate 		/*
481*0Sstevel@tonic-gate 		 * Note that HASH_BLOCK_DATA_ORDER_ALIGNED gets defined
482*0Sstevel@tonic-gate 		 * only if sizeof(HASH_LONG)==4.
483*0Sstevel@tonic-gate 		 */
484*0Sstevel@tonic-gate 		if ((((unsigned long)data)%4) == 0)
485*0Sstevel@tonic-gate 			{
486*0Sstevel@tonic-gate 			/* data is properly aligned so that we can cast it: */
487*0Sstevel@tonic-gate 			HASH_BLOCK_DATA_ORDER_ALIGNED (c,(HASH_LONG *)data,sw);
488*0Sstevel@tonic-gate 			sw*=HASH_CBLOCK;
489*0Sstevel@tonic-gate 			data+=sw;
490*0Sstevel@tonic-gate 			len-=sw;
491*0Sstevel@tonic-gate 			}
492*0Sstevel@tonic-gate 		else
493*0Sstevel@tonic-gate #if !defined(HASH_BLOCK_DATA_ORDER)
494*0Sstevel@tonic-gate 			while (sw--)
495*0Sstevel@tonic-gate 				{
496*0Sstevel@tonic-gate 				memcpy (p=c->data,data,HASH_CBLOCK);
497*0Sstevel@tonic-gate 				HASH_BLOCK_DATA_ORDER_ALIGNED(c,p,1);
498*0Sstevel@tonic-gate 				data+=HASH_CBLOCK;
499*0Sstevel@tonic-gate 				len-=HASH_CBLOCK;
500*0Sstevel@tonic-gate 				}
501*0Sstevel@tonic-gate #endif
502*0Sstevel@tonic-gate #endif
503*0Sstevel@tonic-gate #if defined(HASH_BLOCK_DATA_ORDER)
504*0Sstevel@tonic-gate 			{
505*0Sstevel@tonic-gate 			HASH_BLOCK_DATA_ORDER(c,data,sw);
506*0Sstevel@tonic-gate 			sw*=HASH_CBLOCK;
507*0Sstevel@tonic-gate 			data+=sw;
508*0Sstevel@tonic-gate 			len-=sw;
509*0Sstevel@tonic-gate 			}
510*0Sstevel@tonic-gate #endif
511*0Sstevel@tonic-gate 		}
512*0Sstevel@tonic-gate 
513*0Sstevel@tonic-gate 	if (len!=0)
514*0Sstevel@tonic-gate 		{
515*0Sstevel@tonic-gate 		p = c->data;
516*0Sstevel@tonic-gate 		c->num = len;
517*0Sstevel@tonic-gate 		ew=len>>2;	/* words to copy */
518*0Sstevel@tonic-gate 		ec=len&0x03;
519*0Sstevel@tonic-gate 		for (; ew; ew--,p++)
520*0Sstevel@tonic-gate 			{
521*0Sstevel@tonic-gate 			HOST_c2l(data,l); *p=l;
522*0Sstevel@tonic-gate 			}
523*0Sstevel@tonic-gate 		HOST_c2l_p(data,l,ec);
524*0Sstevel@tonic-gate 		*p=l;
525*0Sstevel@tonic-gate 		}
526*0Sstevel@tonic-gate 	return 1;
527*0Sstevel@tonic-gate 	}
528*0Sstevel@tonic-gate 
529*0Sstevel@tonic-gate 
530*0Sstevel@tonic-gate void HASH_TRANSFORM (HASH_CTX *c, const unsigned char *data)
531*0Sstevel@tonic-gate 	{
532*0Sstevel@tonic-gate #if defined(HASH_BLOCK_DATA_ORDER_ALIGNED)
533*0Sstevel@tonic-gate 	if ((((unsigned long)data)%4) == 0)
534*0Sstevel@tonic-gate 		/* data is properly aligned so that we can cast it: */
535*0Sstevel@tonic-gate 		HASH_BLOCK_DATA_ORDER_ALIGNED (c,(HASH_LONG *)data,1);
536*0Sstevel@tonic-gate 	else
537*0Sstevel@tonic-gate #if !defined(HASH_BLOCK_DATA_ORDER)
538*0Sstevel@tonic-gate 		{
539*0Sstevel@tonic-gate 		memcpy (c->data,data,HASH_CBLOCK);
540*0Sstevel@tonic-gate 		HASH_BLOCK_DATA_ORDER_ALIGNED (c,c->data,1);
541*0Sstevel@tonic-gate 		}
542*0Sstevel@tonic-gate #endif
543*0Sstevel@tonic-gate #endif
544*0Sstevel@tonic-gate #if defined(HASH_BLOCK_DATA_ORDER)
545*0Sstevel@tonic-gate 	HASH_BLOCK_DATA_ORDER (c,data,1);
546*0Sstevel@tonic-gate #endif
547*0Sstevel@tonic-gate 	}
548*0Sstevel@tonic-gate 
549*0Sstevel@tonic-gate 
550*0Sstevel@tonic-gate int HASH_FINAL (unsigned char *md, HASH_CTX *c)
551*0Sstevel@tonic-gate 	{
552*0Sstevel@tonic-gate 	register HASH_LONG *p;
553*0Sstevel@tonic-gate 	register unsigned long l;
554*0Sstevel@tonic-gate 	register int i,j;
555*0Sstevel@tonic-gate 	static const unsigned char end[4]={0x80,0x00,0x00,0x00};
556*0Sstevel@tonic-gate 	const unsigned char *cp=end;
557*0Sstevel@tonic-gate 
558*0Sstevel@tonic-gate 	/* c->num should definitly have room for at least one more byte. */
559*0Sstevel@tonic-gate 	p=c->data;
560*0Sstevel@tonic-gate 	i=c->num>>2;
561*0Sstevel@tonic-gate 	j=c->num&0x03;
562*0Sstevel@tonic-gate 
563*0Sstevel@tonic-gate #if 0
564*0Sstevel@tonic-gate 	/* purify often complains about the following line as an
565*0Sstevel@tonic-gate 	 * Uninitialized Memory Read.  While this can be true, the
566*0Sstevel@tonic-gate 	 * following p_c2l macro will reset l when that case is true.
567*0Sstevel@tonic-gate 	 * This is because j&0x03 contains the number of 'valid' bytes
568*0Sstevel@tonic-gate 	 * already in p[i].  If and only if j&0x03 == 0, the UMR will
569*0Sstevel@tonic-gate 	 * occur but this is also the only time p_c2l will do
570*0Sstevel@tonic-gate 	 * l= *(cp++) instead of l|= *(cp++)
571*0Sstevel@tonic-gate 	 * Many thanks to Alex Tang <altitude@cic.net> for pickup this
572*0Sstevel@tonic-gate 	 * 'potential bug' */
573*0Sstevel@tonic-gate #ifdef PURIFY
574*0Sstevel@tonic-gate 	if (j==0) p[i]=0; /* Yeah, but that's not the way to fix it:-) */
575*0Sstevel@tonic-gate #endif
576*0Sstevel@tonic-gate 	l=p[i];
577*0Sstevel@tonic-gate #else
578*0Sstevel@tonic-gate 	l = (j==0) ? 0 : p[i];
579*0Sstevel@tonic-gate #endif
580*0Sstevel@tonic-gate 	HOST_p_c2l(cp,l,j); p[i++]=l; /* i is the next 'undefined word' */
581*0Sstevel@tonic-gate 
582*0Sstevel@tonic-gate 	if (i>(HASH_LBLOCK-2)) /* save room for Nl and Nh */
583*0Sstevel@tonic-gate 		{
584*0Sstevel@tonic-gate 		if (i<HASH_LBLOCK) p[i]=0;
585*0Sstevel@tonic-gate 		HASH_BLOCK_HOST_ORDER (c,p,1);
586*0Sstevel@tonic-gate 		i=0;
587*0Sstevel@tonic-gate 		}
588*0Sstevel@tonic-gate 	for (; i<(HASH_LBLOCK-2); i++)
589*0Sstevel@tonic-gate 		p[i]=0;
590*0Sstevel@tonic-gate 
591*0Sstevel@tonic-gate #if   defined(DATA_ORDER_IS_BIG_ENDIAN)
592*0Sstevel@tonic-gate 	p[HASH_LBLOCK-2]=c->Nh;
593*0Sstevel@tonic-gate 	p[HASH_LBLOCK-1]=c->Nl;
594*0Sstevel@tonic-gate #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
595*0Sstevel@tonic-gate 	p[HASH_LBLOCK-2]=c->Nl;
596*0Sstevel@tonic-gate 	p[HASH_LBLOCK-1]=c->Nh;
597*0Sstevel@tonic-gate #endif
598*0Sstevel@tonic-gate 	HASH_BLOCK_HOST_ORDER (c,p,1);
599*0Sstevel@tonic-gate 
600*0Sstevel@tonic-gate #ifndef HASH_MAKE_STRING
601*0Sstevel@tonic-gate #error "HASH_MAKE_STRING must be defined!"
602*0Sstevel@tonic-gate #else
603*0Sstevel@tonic-gate 	HASH_MAKE_STRING(c,md);
604*0Sstevel@tonic-gate #endif
605*0Sstevel@tonic-gate 
606*0Sstevel@tonic-gate 	c->num=0;
607*0Sstevel@tonic-gate 	/* clear stuff, HASH_BLOCK may be leaving some stuff on the stack
608*0Sstevel@tonic-gate 	 * but I'm not worried :-)
609*0Sstevel@tonic-gate 	OPENSSL_cleanse((void *)c,sizeof(HASH_CTX));
610*0Sstevel@tonic-gate 	 */
611*0Sstevel@tonic-gate 	return 1;
612*0Sstevel@tonic-gate 	}
613*0Sstevel@tonic-gate 
614*0Sstevel@tonic-gate #ifndef MD32_REG_T
615*0Sstevel@tonic-gate #define MD32_REG_T long
616*0Sstevel@tonic-gate /*
617*0Sstevel@tonic-gate  * This comment was originaly written for MD5, which is why it
618*0Sstevel@tonic-gate  * discusses A-D. But it basically applies to all 32-bit digests,
619*0Sstevel@tonic-gate  * which is why it was moved to common header file.
620*0Sstevel@tonic-gate  *
621*0Sstevel@tonic-gate  * In case you wonder why A-D are declared as long and not
622*0Sstevel@tonic-gate  * as MD5_LONG. Doing so results in slight performance
623*0Sstevel@tonic-gate  * boost on LP64 architectures. The catch is we don't
624*0Sstevel@tonic-gate  * really care if 32 MSBs of a 64-bit register get polluted
625*0Sstevel@tonic-gate  * with eventual overflows as we *save* only 32 LSBs in
626*0Sstevel@tonic-gate  * *either* case. Now declaring 'em long excuses the compiler
627*0Sstevel@tonic-gate  * from keeping 32 MSBs zeroed resulting in 13% performance
628*0Sstevel@tonic-gate  * improvement under SPARC Solaris7/64 and 5% under AlphaLinux.
629*0Sstevel@tonic-gate  * Well, to be honest it should say that this *prevents*
630*0Sstevel@tonic-gate  * performance degradation.
631*0Sstevel@tonic-gate  *				<appro@fy.chalmers.se>
632*0Sstevel@tonic-gate  * Apparently there're LP64 compilers that generate better
633*0Sstevel@tonic-gate  * code if A-D are declared int. Most notably GCC-x86_64
634*0Sstevel@tonic-gate  * generates better code.
635*0Sstevel@tonic-gate  *				<appro@fy.chalmers.se>
636*0Sstevel@tonic-gate  */
637*0Sstevel@tonic-gate #endif
638