xref: /onnv-gate/usr/src/common/openssl/crypto/sha/sha512.c (revision 2139:6243c3338933)
1*2139Sjp161948 /* crypto/sha/sha512.c */
2*2139Sjp161948 /* ====================================================================
3*2139Sjp161948  * Copyright (c) 2004 The OpenSSL Project.  All rights reserved
4*2139Sjp161948  * according to the OpenSSL license [found in ../../LICENSE].
5*2139Sjp161948  * ====================================================================
6*2139Sjp161948  */
7*2139Sjp161948 #include <openssl/opensslconf.h>
8*2139Sjp161948 #if !defined(OPENSSL_NO_SHA) && !defined(OPENSSL_NO_SHA512)
9*2139Sjp161948 /*
10*2139Sjp161948  * IMPLEMENTATION NOTES.
11*2139Sjp161948  *
12*2139Sjp161948  * As you might have noticed 32-bit hash algorithms:
13*2139Sjp161948  *
14*2139Sjp161948  * - permit SHA_LONG to be wider than 32-bit (case on CRAY);
15*2139Sjp161948  * - optimized versions implement two transform functions: one operating
16*2139Sjp161948  *   on [aligned] data in host byte order and one - on data in input
17*2139Sjp161948  *   stream byte order;
18*2139Sjp161948  * - share common byte-order neutral collector and padding function
19*2139Sjp161948  *   implementations, ../md32_common.h;
20*2139Sjp161948  *
21*2139Sjp161948  * Neither of the above applies to this SHA-512 implementations. Reasons
22*2139Sjp161948  * [in reverse order] are:
23*2139Sjp161948  *
24*2139Sjp161948  * - it's the only 64-bit hash algorithm for the moment of this writing,
25*2139Sjp161948  *   there is no need for common collector/padding implementation [yet];
26*2139Sjp161948  * - by supporting only one transform function [which operates on
27*2139Sjp161948  *   *aligned* data in input stream byte order, big-endian in this case]
28*2139Sjp161948  *   we minimize burden of maintenance in two ways: a) collector/padding
29*2139Sjp161948  *   function is simpler; b) only one transform function to stare at;
30*2139Sjp161948  * - SHA_LONG64 is required to be exactly 64-bit in order to be able to
31*2139Sjp161948  *   apply a number of optimizations to mitigate potential performance
32*2139Sjp161948  *   penalties caused by previous design decision;
33*2139Sjp161948  *
34*2139Sjp161948  * Caveat lector.
35*2139Sjp161948  *
36*2139Sjp161948  * Implementation relies on the fact that "long long" is 64-bit on
37*2139Sjp161948  * both 32- and 64-bit platforms. If some compiler vendor comes up
38*2139Sjp161948  * with 128-bit long long, adjustment to sha.h would be required.
39*2139Sjp161948  * As this implementation relies on 64-bit integer type, it's totally
40*2139Sjp161948  * inappropriate for platforms which don't support it, most notably
41*2139Sjp161948  * 16-bit platforms.
42*2139Sjp161948  *					<appro@fy.chalmers.se>
43*2139Sjp161948  */
44*2139Sjp161948 #include <stdlib.h>
45*2139Sjp161948 #include <string.h>
46*2139Sjp161948 
47*2139Sjp161948 #include <openssl/crypto.h>
48*2139Sjp161948 #include <openssl/sha.h>
49*2139Sjp161948 #include <openssl/opensslv.h>
50*2139Sjp161948 
51*2139Sjp161948 #include "cryptlib.h"
52*2139Sjp161948 
53*2139Sjp161948 const char *SHA512_version="SHA-512" OPENSSL_VERSION_PTEXT;
54*2139Sjp161948 
55*2139Sjp161948 #if defined(_M_IX86) || defined(_M_AMD64) || defined(__i386) || defined(__x86_64)
56*2139Sjp161948 #define SHA512_BLOCK_CAN_MANAGE_UNALIGNED_DATA
57*2139Sjp161948 #endif
58*2139Sjp161948 
SHA384_Init(SHA512_CTX * c)59*2139Sjp161948 int SHA384_Init (SHA512_CTX *c)
60*2139Sjp161948 	{
61*2139Sjp161948 	c->h[0]=U64(0xcbbb9d5dc1059ed8);
62*2139Sjp161948 	c->h[1]=U64(0x629a292a367cd507);
63*2139Sjp161948 	c->h[2]=U64(0x9159015a3070dd17);
64*2139Sjp161948 	c->h[3]=U64(0x152fecd8f70e5939);
65*2139Sjp161948 	c->h[4]=U64(0x67332667ffc00b31);
66*2139Sjp161948 	c->h[5]=U64(0x8eb44a8768581511);
67*2139Sjp161948 	c->h[6]=U64(0xdb0c2e0d64f98fa7);
68*2139Sjp161948 	c->h[7]=U64(0x47b5481dbefa4fa4);
69*2139Sjp161948         c->Nl=0;        c->Nh=0;
70*2139Sjp161948         c->num=0;       c->md_len=SHA384_DIGEST_LENGTH;
71*2139Sjp161948         return 1;
72*2139Sjp161948 	}
73*2139Sjp161948 
SHA512_Init(SHA512_CTX * c)74*2139Sjp161948 int SHA512_Init (SHA512_CTX *c)
75*2139Sjp161948 	{
76*2139Sjp161948 	c->h[0]=U64(0x6a09e667f3bcc908);
77*2139Sjp161948 	c->h[1]=U64(0xbb67ae8584caa73b);
78*2139Sjp161948 	c->h[2]=U64(0x3c6ef372fe94f82b);
79*2139Sjp161948 	c->h[3]=U64(0xa54ff53a5f1d36f1);
80*2139Sjp161948 	c->h[4]=U64(0x510e527fade682d1);
81*2139Sjp161948 	c->h[5]=U64(0x9b05688c2b3e6c1f);
82*2139Sjp161948 	c->h[6]=U64(0x1f83d9abfb41bd6b);
83*2139Sjp161948 	c->h[7]=U64(0x5be0cd19137e2179);
84*2139Sjp161948         c->Nl=0;        c->Nh=0;
85*2139Sjp161948         c->num=0;       c->md_len=SHA512_DIGEST_LENGTH;
86*2139Sjp161948         return 1;
87*2139Sjp161948 	}
88*2139Sjp161948 
89*2139Sjp161948 #ifndef SHA512_ASM
90*2139Sjp161948 static
91*2139Sjp161948 #endif
92*2139Sjp161948 void sha512_block (SHA512_CTX *ctx, const void *in, size_t num);
93*2139Sjp161948 
SHA512_Final(unsigned char * md,SHA512_CTX * c)94*2139Sjp161948 int SHA512_Final (unsigned char *md, SHA512_CTX *c)
95*2139Sjp161948 	{
96*2139Sjp161948 	unsigned char *p=(unsigned char *)c->u.p;
97*2139Sjp161948 	size_t n=c->num;
98*2139Sjp161948 
99*2139Sjp161948 	p[n]=0x80;	/* There always is a room for one */
100*2139Sjp161948 	n++;
101*2139Sjp161948 	if (n > (sizeof(c->u)-16))
102*2139Sjp161948 		memset (p+n,0,sizeof(c->u)-n), n=0,
103*2139Sjp161948 		sha512_block (c,p,1);
104*2139Sjp161948 
105*2139Sjp161948 	memset (p+n,0,sizeof(c->u)-16-n);
106*2139Sjp161948 #ifdef	B_ENDIAN
107*2139Sjp161948 	c->u.d[SHA_LBLOCK-2] = c->Nh;
108*2139Sjp161948 	c->u.d[SHA_LBLOCK-1] = c->Nl;
109*2139Sjp161948 #else
110*2139Sjp161948 	p[sizeof(c->u)-1]  = (unsigned char)(c->Nl);
111*2139Sjp161948 	p[sizeof(c->u)-2]  = (unsigned char)(c->Nl>>8);
112*2139Sjp161948 	p[sizeof(c->u)-3]  = (unsigned char)(c->Nl>>16);
113*2139Sjp161948 	p[sizeof(c->u)-4]  = (unsigned char)(c->Nl>>24);
114*2139Sjp161948 	p[sizeof(c->u)-5]  = (unsigned char)(c->Nl>>32);
115*2139Sjp161948 	p[sizeof(c->u)-6]  = (unsigned char)(c->Nl>>40);
116*2139Sjp161948 	p[sizeof(c->u)-7]  = (unsigned char)(c->Nl>>48);
117*2139Sjp161948 	p[sizeof(c->u)-8]  = (unsigned char)(c->Nl>>56);
118*2139Sjp161948 	p[sizeof(c->u)-9]  = (unsigned char)(c->Nh);
119*2139Sjp161948 	p[sizeof(c->u)-10] = (unsigned char)(c->Nh>>8);
120*2139Sjp161948 	p[sizeof(c->u)-11] = (unsigned char)(c->Nh>>16);
121*2139Sjp161948 	p[sizeof(c->u)-12] = (unsigned char)(c->Nh>>24);
122*2139Sjp161948 	p[sizeof(c->u)-13] = (unsigned char)(c->Nh>>32);
123*2139Sjp161948 	p[sizeof(c->u)-14] = (unsigned char)(c->Nh>>40);
124*2139Sjp161948 	p[sizeof(c->u)-15] = (unsigned char)(c->Nh>>48);
125*2139Sjp161948 	p[sizeof(c->u)-16] = (unsigned char)(c->Nh>>56);
126*2139Sjp161948 #endif
127*2139Sjp161948 
128*2139Sjp161948 	sha512_block (c,p,1);
129*2139Sjp161948 
130*2139Sjp161948 	if (md==0) return 0;
131*2139Sjp161948 
132*2139Sjp161948 	switch (c->md_len)
133*2139Sjp161948 		{
134*2139Sjp161948 		/* Let compiler decide if it's appropriate to unroll... */
135*2139Sjp161948 		case SHA384_DIGEST_LENGTH:
136*2139Sjp161948 			for (n=0;n<SHA384_DIGEST_LENGTH/8;n++)
137*2139Sjp161948 				{
138*2139Sjp161948 				SHA_LONG64 t = c->h[n];
139*2139Sjp161948 
140*2139Sjp161948 				*(md++)	= (unsigned char)(t>>56);
141*2139Sjp161948 				*(md++)	= (unsigned char)(t>>48);
142*2139Sjp161948 				*(md++)	= (unsigned char)(t>>40);
143*2139Sjp161948 				*(md++)	= (unsigned char)(t>>32);
144*2139Sjp161948 				*(md++)	= (unsigned char)(t>>24);
145*2139Sjp161948 				*(md++)	= (unsigned char)(t>>16);
146*2139Sjp161948 				*(md++)	= (unsigned char)(t>>8);
147*2139Sjp161948 				*(md++)	= (unsigned char)(t);
148*2139Sjp161948 				}
149*2139Sjp161948 			break;
150*2139Sjp161948 		case SHA512_DIGEST_LENGTH:
151*2139Sjp161948 			for (n=0;n<SHA512_DIGEST_LENGTH/8;n++)
152*2139Sjp161948 				{
153*2139Sjp161948 				SHA_LONG64 t = c->h[n];
154*2139Sjp161948 
155*2139Sjp161948 				*(md++)	= (unsigned char)(t>>56);
156*2139Sjp161948 				*(md++)	= (unsigned char)(t>>48);
157*2139Sjp161948 				*(md++)	= (unsigned char)(t>>40);
158*2139Sjp161948 				*(md++)	= (unsigned char)(t>>32);
159*2139Sjp161948 				*(md++)	= (unsigned char)(t>>24);
160*2139Sjp161948 				*(md++)	= (unsigned char)(t>>16);
161*2139Sjp161948 				*(md++)	= (unsigned char)(t>>8);
162*2139Sjp161948 				*(md++)	= (unsigned char)(t);
163*2139Sjp161948 				}
164*2139Sjp161948 			break;
165*2139Sjp161948 		/* ... as well as make sure md_len is not abused. */
166*2139Sjp161948 		default:	return 0;
167*2139Sjp161948 		}
168*2139Sjp161948 
169*2139Sjp161948 	return 1;
170*2139Sjp161948 	}
171*2139Sjp161948 
SHA384_Final(unsigned char * md,SHA512_CTX * c)172*2139Sjp161948 int SHA384_Final (unsigned char *md,SHA512_CTX *c)
173*2139Sjp161948 {   return SHA512_Final (md,c);   }
174*2139Sjp161948 
SHA512_Update(SHA512_CTX * c,const void * _data,size_t len)175*2139Sjp161948 int SHA512_Update (SHA512_CTX *c, const void *_data, size_t len)
176*2139Sjp161948 	{
177*2139Sjp161948 	SHA_LONG64	l;
178*2139Sjp161948 	unsigned char  *p=c->u.p;
179*2139Sjp161948 	const unsigned char *data=(const unsigned char *)_data;
180*2139Sjp161948 
181*2139Sjp161948 	if (len==0) return  1;
182*2139Sjp161948 
183*2139Sjp161948 	l = (c->Nl+(((SHA_LONG64)len)<<3))&U64(0xffffffffffffffff);
184*2139Sjp161948 	if (l < c->Nl)		c->Nh++;
185*2139Sjp161948 	if (sizeof(len)>=8)	c->Nh+=(((SHA_LONG64)len)>>61);
186*2139Sjp161948 	c->Nl=l;
187*2139Sjp161948 
188*2139Sjp161948 	if (c->num != 0)
189*2139Sjp161948 		{
190*2139Sjp161948 		size_t n = sizeof(c->u) - c->num;
191*2139Sjp161948 
192*2139Sjp161948 		if (len < n)
193*2139Sjp161948 			{
194*2139Sjp161948 			memcpy (p+c->num,data,len), c->num += len;
195*2139Sjp161948 			return 1;
196*2139Sjp161948 			}
197*2139Sjp161948 		else	{
198*2139Sjp161948 			memcpy (p+c->num,data,n), c->num = 0;
199*2139Sjp161948 			len-=n, data+=n;
200*2139Sjp161948 			sha512_block (c,p,1);
201*2139Sjp161948 			}
202*2139Sjp161948 		}
203*2139Sjp161948 
204*2139Sjp161948 	if (len >= sizeof(c->u))
205*2139Sjp161948 		{
206*2139Sjp161948 #ifndef SHA512_BLOCK_CAN_MANAGE_UNALIGNED_DATA
207*2139Sjp161948 		if ((size_t)data%sizeof(c->u.d[0]) != 0)
208*2139Sjp161948 			while (len >= sizeof(c->u))
209*2139Sjp161948 				memcpy (p,data,sizeof(c->u)),
210*2139Sjp161948 				sha512_block (c,p,1),
211*2139Sjp161948 				len  -= sizeof(c->u),
212*2139Sjp161948 				data += sizeof(c->u);
213*2139Sjp161948 		else
214*2139Sjp161948 #endif
215*2139Sjp161948 			sha512_block (c,data,len/sizeof(c->u)),
216*2139Sjp161948 			data += len,
217*2139Sjp161948 			len  %= sizeof(c->u),
218*2139Sjp161948 			data -= len;
219*2139Sjp161948 		}
220*2139Sjp161948 
221*2139Sjp161948 	if (len != 0)	memcpy (p,data,len), c->num = (int)len;
222*2139Sjp161948 
223*2139Sjp161948 	return 1;
224*2139Sjp161948 	}
225*2139Sjp161948 
SHA384_Update(SHA512_CTX * c,const void * data,size_t len)226*2139Sjp161948 int SHA384_Update (SHA512_CTX *c, const void *data, size_t len)
227*2139Sjp161948 {   return SHA512_Update (c,data,len);   }
228*2139Sjp161948 
SHA512_Transform(SHA512_CTX * c,const unsigned char * data)229*2139Sjp161948 void SHA512_Transform (SHA512_CTX *c, const unsigned char *data)
230*2139Sjp161948 {   sha512_block (c,data,1);  }
231*2139Sjp161948 
SHA384(const unsigned char * d,size_t n,unsigned char * md)232*2139Sjp161948 unsigned char *SHA384(const unsigned char *d, size_t n, unsigned char *md)
233*2139Sjp161948 	{
234*2139Sjp161948 	SHA512_CTX c;
235*2139Sjp161948 	static unsigned char m[SHA384_DIGEST_LENGTH];
236*2139Sjp161948 
237*2139Sjp161948 	if (md == NULL) md=m;
238*2139Sjp161948 	SHA384_Init(&c);
239*2139Sjp161948 	SHA512_Update(&c,d,n);
240*2139Sjp161948 	SHA512_Final(md,&c);
241*2139Sjp161948 	OPENSSL_cleanse(&c,sizeof(c));
242*2139Sjp161948 	return(md);
243*2139Sjp161948 	}
244*2139Sjp161948 
SHA512(const unsigned char * d,size_t n,unsigned char * md)245*2139Sjp161948 unsigned char *SHA512(const unsigned char *d, size_t n, unsigned char *md)
246*2139Sjp161948 	{
247*2139Sjp161948 	SHA512_CTX c;
248*2139Sjp161948 	static unsigned char m[SHA512_DIGEST_LENGTH];
249*2139Sjp161948 
250*2139Sjp161948 	if (md == NULL) md=m;
251*2139Sjp161948 	SHA512_Init(&c);
252*2139Sjp161948 	SHA512_Update(&c,d,n);
253*2139Sjp161948 	SHA512_Final(md,&c);
254*2139Sjp161948 	OPENSSL_cleanse(&c,sizeof(c));
255*2139Sjp161948 	return(md);
256*2139Sjp161948 	}
257*2139Sjp161948 
258*2139Sjp161948 #ifndef SHA512_ASM
259*2139Sjp161948 static const SHA_LONG64 K512[80] = {
260*2139Sjp161948         U64(0x428a2f98d728ae22),U64(0x7137449123ef65cd),
261*2139Sjp161948         U64(0xb5c0fbcfec4d3b2f),U64(0xe9b5dba58189dbbc),
262*2139Sjp161948         U64(0x3956c25bf348b538),U64(0x59f111f1b605d019),
263*2139Sjp161948         U64(0x923f82a4af194f9b),U64(0xab1c5ed5da6d8118),
264*2139Sjp161948         U64(0xd807aa98a3030242),U64(0x12835b0145706fbe),
265*2139Sjp161948         U64(0x243185be4ee4b28c),U64(0x550c7dc3d5ffb4e2),
266*2139Sjp161948         U64(0x72be5d74f27b896f),U64(0x80deb1fe3b1696b1),
267*2139Sjp161948         U64(0x9bdc06a725c71235),U64(0xc19bf174cf692694),
268*2139Sjp161948         U64(0xe49b69c19ef14ad2),U64(0xefbe4786384f25e3),
269*2139Sjp161948         U64(0x0fc19dc68b8cd5b5),U64(0x240ca1cc77ac9c65),
270*2139Sjp161948         U64(0x2de92c6f592b0275),U64(0x4a7484aa6ea6e483),
271*2139Sjp161948         U64(0x5cb0a9dcbd41fbd4),U64(0x76f988da831153b5),
272*2139Sjp161948         U64(0x983e5152ee66dfab),U64(0xa831c66d2db43210),
273*2139Sjp161948         U64(0xb00327c898fb213f),U64(0xbf597fc7beef0ee4),
274*2139Sjp161948         U64(0xc6e00bf33da88fc2),U64(0xd5a79147930aa725),
275*2139Sjp161948         U64(0x06ca6351e003826f),U64(0x142929670a0e6e70),
276*2139Sjp161948         U64(0x27b70a8546d22ffc),U64(0x2e1b21385c26c926),
277*2139Sjp161948         U64(0x4d2c6dfc5ac42aed),U64(0x53380d139d95b3df),
278*2139Sjp161948         U64(0x650a73548baf63de),U64(0x766a0abb3c77b2a8),
279*2139Sjp161948         U64(0x81c2c92e47edaee6),U64(0x92722c851482353b),
280*2139Sjp161948         U64(0xa2bfe8a14cf10364),U64(0xa81a664bbc423001),
281*2139Sjp161948         U64(0xc24b8b70d0f89791),U64(0xc76c51a30654be30),
282*2139Sjp161948         U64(0xd192e819d6ef5218),U64(0xd69906245565a910),
283*2139Sjp161948         U64(0xf40e35855771202a),U64(0x106aa07032bbd1b8),
284*2139Sjp161948         U64(0x19a4c116b8d2d0c8),U64(0x1e376c085141ab53),
285*2139Sjp161948         U64(0x2748774cdf8eeb99),U64(0x34b0bcb5e19b48a8),
286*2139Sjp161948         U64(0x391c0cb3c5c95a63),U64(0x4ed8aa4ae3418acb),
287*2139Sjp161948         U64(0x5b9cca4f7763e373),U64(0x682e6ff3d6b2b8a3),
288*2139Sjp161948         U64(0x748f82ee5defb2fc),U64(0x78a5636f43172f60),
289*2139Sjp161948         U64(0x84c87814a1f0ab72),U64(0x8cc702081a6439ec),
290*2139Sjp161948         U64(0x90befffa23631e28),U64(0xa4506cebde82bde9),
291*2139Sjp161948         U64(0xbef9a3f7b2c67915),U64(0xc67178f2e372532b),
292*2139Sjp161948         U64(0xca273eceea26619c),U64(0xd186b8c721c0c207),
293*2139Sjp161948         U64(0xeada7dd6cde0eb1e),U64(0xf57d4f7fee6ed178),
294*2139Sjp161948         U64(0x06f067aa72176fba),U64(0x0a637dc5a2c898a6),
295*2139Sjp161948         U64(0x113f9804bef90dae),U64(0x1b710b35131c471b),
296*2139Sjp161948         U64(0x28db77f523047d84),U64(0x32caab7b40c72493),
297*2139Sjp161948         U64(0x3c9ebe0a15c9bebc),U64(0x431d67c49c100d4c),
298*2139Sjp161948         U64(0x4cc5d4becb3e42b6),U64(0x597f299cfc657e2a),
299*2139Sjp161948         U64(0x5fcb6fab3ad6faec),U64(0x6c44198c4a475817) };
300*2139Sjp161948 
301*2139Sjp161948 #ifndef PEDANTIC
302*2139Sjp161948 # if defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
303*2139Sjp161948 #  if defined(__x86_64) || defined(__x86_64__)
304*2139Sjp161948 #   define PULL64(x) ({ SHA_LONG64 ret=*((const SHA_LONG64 *)(&(x)));	\
305*2139Sjp161948 				asm ("bswapq	%0"		\
306*2139Sjp161948 				: "=r"(ret)			\
307*2139Sjp161948 				: "0"(ret)); ret;		})
308*2139Sjp161948 #  endif
309*2139Sjp161948 # endif
310*2139Sjp161948 #endif
311*2139Sjp161948 
312*2139Sjp161948 #ifndef PULL64
313*2139Sjp161948 #define B(x,j)    (((SHA_LONG64)(*(((const unsigned char *)(&x))+j)))<<((7-j)*8))
314*2139Sjp161948 #define PULL64(x) (B(x,0)|B(x,1)|B(x,2)|B(x,3)|B(x,4)|B(x,5)|B(x,6)|B(x,7))
315*2139Sjp161948 #endif
316*2139Sjp161948 
317*2139Sjp161948 #ifndef PEDANTIC
318*2139Sjp161948 # if defined(_MSC_VER)
319*2139Sjp161948 #  if defined(_WIN64)	/* applies to both IA-64 and AMD64 */
320*2139Sjp161948 #   define ROTR(a,n)	_rotr64((a),n)
321*2139Sjp161948 #  endif
322*2139Sjp161948 # elif defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
323*2139Sjp161948 #  if defined(__x86_64) || defined(__x86_64__)
324*2139Sjp161948 #   define ROTR(a,n)	({ unsigned long ret;		\
325*2139Sjp161948 				asm ("rorq %1,%0"	\
326*2139Sjp161948 				: "=r"(ret)		\
327*2139Sjp161948 				: "J"(n),"0"(a)		\
328*2139Sjp161948 				: "cc"); ret;		})
329*2139Sjp161948 #  elif defined(_ARCH_PPC) && defined(__64BIT__)
330*2139Sjp161948 #   define ROTR(a,n)	({ unsigned long ret;		\
331*2139Sjp161948 				asm ("rotrdi %0,%1,%2"	\
332*2139Sjp161948 				: "=r"(ret)		\
333*2139Sjp161948 				: "r"(a),"K"(n)); ret;	})
334*2139Sjp161948 #  endif
335*2139Sjp161948 # endif
336*2139Sjp161948 #endif
337*2139Sjp161948 
338*2139Sjp161948 #ifndef ROTR
339*2139Sjp161948 #define ROTR(x,s)	(((x)>>s) | (x)<<(64-s))
340*2139Sjp161948 #endif
341*2139Sjp161948 
342*2139Sjp161948 #define Sigma0(x)	(ROTR((x),28) ^ ROTR((x),34) ^ ROTR((x),39))
343*2139Sjp161948 #define Sigma1(x)	(ROTR((x),14) ^ ROTR((x),18) ^ ROTR((x),41))
344*2139Sjp161948 #define sigma0(x)	(ROTR((x),1)  ^ ROTR((x),8)  ^ ((x)>>7))
345*2139Sjp161948 #define sigma1(x)	(ROTR((x),19) ^ ROTR((x),61) ^ ((x)>>6))
346*2139Sjp161948 
347*2139Sjp161948 #define Ch(x,y,z)	(((x) & (y)) ^ ((~(x)) & (z)))
348*2139Sjp161948 #define Maj(x,y,z)	(((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
349*2139Sjp161948 
350*2139Sjp161948 #if defined(OPENSSL_IA32_SSE2) && !defined(OPENSSL_NO_ASM) && !defined(I386_ONLY)
351*2139Sjp161948 #define	GO_FOR_SSE2(ctx,in,num)		do {		\
352*2139Sjp161948 	void	sha512_block_sse2(void *,const void *,size_t);	\
353*2139Sjp161948 	if (!(OPENSSL_ia32cap_P & (1<<26))) break;	\
354*2139Sjp161948 	sha512_block_sse2(ctx->h,in,num); return;	\
355*2139Sjp161948 					} while (0)
356*2139Sjp161948 #endif
357*2139Sjp161948 
358*2139Sjp161948 #ifdef OPENSSL_SMALL_FOOTPRINT
359*2139Sjp161948 
sha512_block(SHA512_CTX * ctx,const void * in,size_t num)360*2139Sjp161948 static void sha512_block (SHA512_CTX *ctx, const void *in, size_t num)
361*2139Sjp161948 	{
362*2139Sjp161948 	const SHA_LONG64 *W=in;
363*2139Sjp161948 	SHA_LONG64	a,b,c,d,e,f,g,h,s0,s1,T1,T2;
364*2139Sjp161948 	SHA_LONG64	X[16];
365*2139Sjp161948 	int i;
366*2139Sjp161948 
367*2139Sjp161948 #ifdef GO_FOR_SSE2
368*2139Sjp161948 	GO_FOR_SSE2(ctx,in,num);
369*2139Sjp161948 #endif
370*2139Sjp161948 
371*2139Sjp161948 			while (num--) {
372*2139Sjp161948 
373*2139Sjp161948 	a = ctx->h[0];	b = ctx->h[1];	c = ctx->h[2];	d = ctx->h[3];
374*2139Sjp161948 	e = ctx->h[4];	f = ctx->h[5];	g = ctx->h[6];	h = ctx->h[7];
375*2139Sjp161948 
376*2139Sjp161948 	for (i=0;i<16;i++)
377*2139Sjp161948 		{
378*2139Sjp161948 #ifdef B_ENDIAN
379*2139Sjp161948 		T1 = X[i] = W[i];
380*2139Sjp161948 #else
381*2139Sjp161948 		T1 = X[i] = PULL64(W[i]);
382*2139Sjp161948 #endif
383*2139Sjp161948 		T1 += h + Sigma1(e) + Ch(e,f,g) + K512[i];
384*2139Sjp161948 		T2 = Sigma0(a) + Maj(a,b,c);
385*2139Sjp161948 		h = g;	g = f;	f = e;	e = d + T1;
386*2139Sjp161948 		d = c;	c = b;	b = a;	a = T1 + T2;
387*2139Sjp161948 		}
388*2139Sjp161948 
389*2139Sjp161948 	for (;i<80;i++)
390*2139Sjp161948 		{
391*2139Sjp161948 		s0 = X[(i+1)&0x0f];	s0 = sigma0(s0);
392*2139Sjp161948 		s1 = X[(i+14)&0x0f];	s1 = sigma1(s1);
393*2139Sjp161948 
394*2139Sjp161948 		T1 = X[i&0xf] += s0 + s1 + X[(i+9)&0xf];
395*2139Sjp161948 		T1 += h + Sigma1(e) + Ch(e,f,g) + K512[i];
396*2139Sjp161948 		T2 = Sigma0(a) + Maj(a,b,c);
397*2139Sjp161948 		h = g;	g = f;	f = e;	e = d + T1;
398*2139Sjp161948 		d = c;	c = b;	b = a;	a = T1 + T2;
399*2139Sjp161948 		}
400*2139Sjp161948 
401*2139Sjp161948 	ctx->h[0] += a;	ctx->h[1] += b;	ctx->h[2] += c;	ctx->h[3] += d;
402*2139Sjp161948 	ctx->h[4] += e;	ctx->h[5] += f;	ctx->h[6] += g;	ctx->h[7] += h;
403*2139Sjp161948 
404*2139Sjp161948 			W+=SHA_LBLOCK;
405*2139Sjp161948 			}
406*2139Sjp161948 	}
407*2139Sjp161948 
408*2139Sjp161948 #else
409*2139Sjp161948 
410*2139Sjp161948 #define	ROUND_00_15(i,a,b,c,d,e,f,g,h)		do {	\
411*2139Sjp161948 	T1 += h + Sigma1(e) + Ch(e,f,g) + K512[i];	\
412*2139Sjp161948 	h = Sigma0(a) + Maj(a,b,c);			\
413*2139Sjp161948 	d += T1;	h += T1;		} while (0)
414*2139Sjp161948 
415*2139Sjp161948 #define	ROUND_16_80(i,a,b,c,d,e,f,g,h,X)	do {	\
416*2139Sjp161948 	s0 = X[(i+1)&0x0f];	s0 = sigma0(s0);	\
417*2139Sjp161948 	s1 = X[(i+14)&0x0f];	s1 = sigma1(s1);	\
418*2139Sjp161948 	T1 = X[(i)&0x0f] += s0 + s1 + X[(i+9)&0x0f];	\
419*2139Sjp161948 	ROUND_00_15(i,a,b,c,d,e,f,g,h);		} while (0)
420*2139Sjp161948 
sha512_block(SHA512_CTX * ctx,const void * in,size_t num)421*2139Sjp161948 static void sha512_block (SHA512_CTX *ctx, const void *in, size_t num)
422*2139Sjp161948 	{
423*2139Sjp161948 	const SHA_LONG64 *W=in;
424*2139Sjp161948 	SHA_LONG64	a,b,c,d,e,f,g,h,s0,s1,T1;
425*2139Sjp161948 	SHA_LONG64	X[16];
426*2139Sjp161948 	int i;
427*2139Sjp161948 
428*2139Sjp161948 #ifdef GO_FOR_SSE2
429*2139Sjp161948 	GO_FOR_SSE2(ctx,in,num);
430*2139Sjp161948 #endif
431*2139Sjp161948 
432*2139Sjp161948 			while (num--) {
433*2139Sjp161948 
434*2139Sjp161948 	a = ctx->h[0];	b = ctx->h[1];	c = ctx->h[2];	d = ctx->h[3];
435*2139Sjp161948 	e = ctx->h[4];	f = ctx->h[5];	g = ctx->h[6];	h = ctx->h[7];
436*2139Sjp161948 
437*2139Sjp161948 #ifdef B_ENDIAN
438*2139Sjp161948 	T1 = X[0] = W[0];	ROUND_00_15(0,a,b,c,d,e,f,g,h);
439*2139Sjp161948 	T1 = X[1] = W[1];	ROUND_00_15(1,h,a,b,c,d,e,f,g);
440*2139Sjp161948 	T1 = X[2] = W[2];	ROUND_00_15(2,g,h,a,b,c,d,e,f);
441*2139Sjp161948 	T1 = X[3] = W[3];	ROUND_00_15(3,f,g,h,a,b,c,d,e);
442*2139Sjp161948 	T1 = X[4] = W[4];	ROUND_00_15(4,e,f,g,h,a,b,c,d);
443*2139Sjp161948 	T1 = X[5] = W[5];	ROUND_00_15(5,d,e,f,g,h,a,b,c);
444*2139Sjp161948 	T1 = X[6] = W[6];	ROUND_00_15(6,c,d,e,f,g,h,a,b);
445*2139Sjp161948 	T1 = X[7] = W[7];	ROUND_00_15(7,b,c,d,e,f,g,h,a);
446*2139Sjp161948 	T1 = X[8] = W[8];	ROUND_00_15(8,a,b,c,d,e,f,g,h);
447*2139Sjp161948 	T1 = X[9] = W[9];	ROUND_00_15(9,h,a,b,c,d,e,f,g);
448*2139Sjp161948 	T1 = X[10] = W[10];	ROUND_00_15(10,g,h,a,b,c,d,e,f);
449*2139Sjp161948 	T1 = X[11] = W[11];	ROUND_00_15(11,f,g,h,a,b,c,d,e);
450*2139Sjp161948 	T1 = X[12] = W[12];	ROUND_00_15(12,e,f,g,h,a,b,c,d);
451*2139Sjp161948 	T1 = X[13] = W[13];	ROUND_00_15(13,d,e,f,g,h,a,b,c);
452*2139Sjp161948 	T1 = X[14] = W[14];	ROUND_00_15(14,c,d,e,f,g,h,a,b);
453*2139Sjp161948 	T1 = X[15] = W[15];	ROUND_00_15(15,b,c,d,e,f,g,h,a);
454*2139Sjp161948 #else
455*2139Sjp161948 	T1 = X[0]  = PULL64(W[0]);	ROUND_00_15(0,a,b,c,d,e,f,g,h);
456*2139Sjp161948 	T1 = X[1]  = PULL64(W[1]);	ROUND_00_15(1,h,a,b,c,d,e,f,g);
457*2139Sjp161948 	T1 = X[2]  = PULL64(W[2]);	ROUND_00_15(2,g,h,a,b,c,d,e,f);
458*2139Sjp161948 	T1 = X[3]  = PULL64(W[3]);	ROUND_00_15(3,f,g,h,a,b,c,d,e);
459*2139Sjp161948 	T1 = X[4]  = PULL64(W[4]);	ROUND_00_15(4,e,f,g,h,a,b,c,d);
460*2139Sjp161948 	T1 = X[5]  = PULL64(W[5]);	ROUND_00_15(5,d,e,f,g,h,a,b,c);
461*2139Sjp161948 	T1 = X[6]  = PULL64(W[6]);	ROUND_00_15(6,c,d,e,f,g,h,a,b);
462*2139Sjp161948 	T1 = X[7]  = PULL64(W[7]);	ROUND_00_15(7,b,c,d,e,f,g,h,a);
463*2139Sjp161948 	T1 = X[8]  = PULL64(W[8]);	ROUND_00_15(8,a,b,c,d,e,f,g,h);
464*2139Sjp161948 	T1 = X[9]  = PULL64(W[9]);	ROUND_00_15(9,h,a,b,c,d,e,f,g);
465*2139Sjp161948 	T1 = X[10] = PULL64(W[10]);	ROUND_00_15(10,g,h,a,b,c,d,e,f);
466*2139Sjp161948 	T1 = X[11] = PULL64(W[11]);	ROUND_00_15(11,f,g,h,a,b,c,d,e);
467*2139Sjp161948 	T1 = X[12] = PULL64(W[12]);	ROUND_00_15(12,e,f,g,h,a,b,c,d);
468*2139Sjp161948 	T1 = X[13] = PULL64(W[13]);	ROUND_00_15(13,d,e,f,g,h,a,b,c);
469*2139Sjp161948 	T1 = X[14] = PULL64(W[14]);	ROUND_00_15(14,c,d,e,f,g,h,a,b);
470*2139Sjp161948 	T1 = X[15] = PULL64(W[15]);	ROUND_00_15(15,b,c,d,e,f,g,h,a);
471*2139Sjp161948 #endif
472*2139Sjp161948 
473*2139Sjp161948 	for (i=16;i<80;i+=8)
474*2139Sjp161948 		{
475*2139Sjp161948 		ROUND_16_80(i+0,a,b,c,d,e,f,g,h,X);
476*2139Sjp161948 		ROUND_16_80(i+1,h,a,b,c,d,e,f,g,X);
477*2139Sjp161948 		ROUND_16_80(i+2,g,h,a,b,c,d,e,f,X);
478*2139Sjp161948 		ROUND_16_80(i+3,f,g,h,a,b,c,d,e,X);
479*2139Sjp161948 		ROUND_16_80(i+4,e,f,g,h,a,b,c,d,X);
480*2139Sjp161948 		ROUND_16_80(i+5,d,e,f,g,h,a,b,c,X);
481*2139Sjp161948 		ROUND_16_80(i+6,c,d,e,f,g,h,a,b,X);
482*2139Sjp161948 		ROUND_16_80(i+7,b,c,d,e,f,g,h,a,X);
483*2139Sjp161948 		}
484*2139Sjp161948 
485*2139Sjp161948 	ctx->h[0] += a;	ctx->h[1] += b;	ctx->h[2] += c;	ctx->h[3] += d;
486*2139Sjp161948 	ctx->h[4] += e;	ctx->h[5] += f;	ctx->h[6] += g;	ctx->h[7] += h;
487*2139Sjp161948 
488*2139Sjp161948 			W+=SHA_LBLOCK;
489*2139Sjp161948 			}
490*2139Sjp161948 	}
491*2139Sjp161948 
492*2139Sjp161948 #endif
493*2139Sjp161948 
494*2139Sjp161948 #endif /* SHA512_ASM */
495*2139Sjp161948 
496*2139Sjp161948 #endif /* OPENSSL_NO_SHA512 */
497