144290Swollman/* crypto/sha/sha_locl.h */ 244290Swollman/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 344290Swollman * All rights reserved. 444290Swollman * 544290Swollman * This package is an SSL implementation written 644290Swollman * by Eric Young (eay@cryptsoft.com). 744290Swollman * The implementation was written so as to conform with Netscapes SSL. 844290Swollman * 944290Swollman * This library is free for commercial and non-commercial use as long as 1044290Swollman * the following conditions are aheared to. The following conditions 1144290Swollman * apply to all code found in this distribution, be it the RC4, RSA, 1244290Swollman * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1344290Swollman * included with this distribution is covered by the same copyright terms 1444290Swollman * except that the holder is Tim Hudson (tjh@cryptsoft.com). 1544290Swollman * 1644290Swollman * Copyright remains Eric Young's, and as such any Copyright notices in 1744290Swollman * the code are not to be removed. 1844290Swollman * If this package is used in a product, Eric Young should be given attribution 1944290Swollman * as the author of the parts of the library used. 2044290Swollman * This can be in the form of a textual message at program startup or 2144290Swollman * in documentation (online or textual) provided with the package. 2244290Swollman * 2344290Swollman * Redistribution and use in source and binary forms, with or without 2444290Swollman * modification, are permitted provided that the following conditions 2544290Swollman * are met: 2644290Swollman * 1. Redistributions of source code must retain the copyright 2744290Swollman * notice, this list of conditions and the following disclaimer. 2844290Swollman * 2. Redistributions in binary form must reproduce the above copyright 2944290Swollman * notice, this list of conditions and the following disclaimer in the 3044290Swollman * documentation and/or other materials provided with the distribution. 3144290Swollman * 3. All advertising materials mentioning features or use of this software 3244290Swollman * must display the following acknowledgement: 3344290Swollman * "This product includes cryptographic software written by 3444290Swollman * Eric Young (eay@cryptsoft.com)" 3544290Swollman * The word 'cryptographic' can be left out if the rouines from the library 3644290Swollman * being used are not cryptographic related :-). 3744290Swollman * 4. If you include any Windows specific code (or a derivative thereof) from 3844290Swollman * the apps directory (application code) you must include an acknowledgement: 3944290Swollman * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 4044290Swollman * 4144290Swollman * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4244290Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4344290Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4444290Swollman * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4544290Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4644290Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4744290Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4844290Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4944290Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5044290Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5144290Swollman * SUCH DAMAGE. 5244290Swollman * 5344290Swollman * The licence and distribution terms for any publically available version or 5444290Swollman * derivative of this code cannot be changed. i.e. this code cannot simply be 5544290Swollman * copied and put under another distribution licence 5644290Swollman * [including the GNU Public Licence.] 5744290Swollman */ 5844290Swollman 5944290Swollman#ifdef undef 6044290Swollman/* one or the other needs to be defined */ 6144290Swollman#ifndef SHA_1 /* FIPE 180-1 */ 6244290Swollman#define SHA_0 /* FIPS 180 */ 6344290Swollman#endif 6444290Swollman#endif 6544290Swollman 6644290Swollman#define ULONG unsigned long 6744290Swollman#define UCHAR unsigned char 6844290Swollman#define UINT unsigned int 6944290Swollman 7044290Swollman#ifdef NOCONST 7144290Swollman#define const 7244290Swollman#endif 7344290Swollman 7444290Swollman#undef c2nl 7544290Swollman#define c2nl(c,l) (l =(((unsigned long)(*((c)++)))<<24), \ 7644290Swollman l|=(((unsigned long)(*((c)++)))<<16), \ 7744290Swollman l|=(((unsigned long)(*((c)++)))<< 8), \ 7844290Swollman l|=(((unsigned long)(*((c)++))) )) 7944290Swollman 8044290Swollman#undef p_c2nl 8144290Swollman#define p_c2nl(c,l,n) { \ 8244290Swollman switch (n) { \ 8344290Swollman case 0: l =((unsigned long)(*((c)++)))<<24; \ 8444290Swollman case 1: l|=((unsigned long)(*((c)++)))<<16; \ 8544290Swollman case 2: l|=((unsigned long)(*((c)++)))<< 8; \ 8644290Swollman case 3: l|=((unsigned long)(*((c)++))); \ 8744290Swollman } \ 8844290Swollman } 8944290Swollman 9044290Swollman#undef c2nl_p 9144290Swollman/* NOTE the pointer is not incremented at the end of this */ 9244290Swollman#define c2nl_p(c,l,n) { \ 9344290Swollman l=0; \ 9444290Swollman (c)+=n; \ 9544290Swollman switch (n) { \ 9644290Swollman case 3: l =((unsigned long)(*(--(c))))<< 8; \ 9744290Swollman case 2: l|=((unsigned long)(*(--(c))))<<16; \ 9844290Swollman case 1: l|=((unsigned long)(*(--(c))))<<24; \ 9944290Swollman } \ 10044290Swollman } 10144290Swollman 10244290Swollman#undef p_c2nl_p 10344290Swollman#define p_c2nl_p(c,l,sc,len) { \ 10444290Swollman switch (sc) \ 10544290Swollman { \ 10644290Swollman case 0: l =((unsigned long)(*((c)++)))<<24; \ 10744290Swollman if (--len == 0) break; \ 10844290Swollman case 1: l|=((unsigned long)(*((c)++)))<<16; \ 10944290Swollman if (--len == 0) break; \ 11044290Swollman case 2: l|=((unsigned long)(*((c)++)))<< 8; \ 11144290Swollman } \ 11244290Swollman } 11344290Swollman 11444290Swollman#undef nl2c 11544290Swollman#define nl2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \ 11644290Swollman *((c)++)=(unsigned char)(((l)>>16)&0xff), \ 11744290Swollman *((c)++)=(unsigned char)(((l)>> 8)&0xff), \ 11844290Swollman *((c)++)=(unsigned char)(((l) )&0xff)) 11944290Swollman 12044290Swollman#undef c2l 12144290Swollman#define c2l(c,l) (l =(((unsigned long)(*((c)++))) ), \ 12244290Swollman l|=(((unsigned long)(*((c)++)))<< 8), \ 12344290Swollman l|=(((unsigned long)(*((c)++)))<<16), \ 12444290Swollman l|=(((unsigned long)(*((c)++)))<<24)) 12544290Swollman 12644290Swollman#undef p_c2l 12744290Swollman#define p_c2l(c,l,n) { \ 12844290Swollman switch (n) { \ 12944290Swollman case 0: l =((unsigned long)(*((c)++))); \ 13044290Swollman case 1: l|=((unsigned long)(*((c)++)))<< 8; \ 13144290Swollman case 2: l|=((unsigned long)(*((c)++)))<<16; \ 13244290Swollman case 3: l|=((unsigned long)(*((c)++)))<<24; \ 13344290Swollman } \ 13444290Swollman } 13544290Swollman 13644290Swollman#undef c2l_p 13744290Swollman/* NOTE the pointer is not incremented at the end of this */ 13844290Swollman#define c2l_p(c,l,n) { \ 13944290Swollman l=0; \ 14044290Swollman (c)+=n; \ 14144290Swollman switch (n) { \ 14244290Swollman case 3: l =((unsigned long)(*(--(c))))<<16; \ 14344290Swollman case 2: l|=((unsigned long)(*(--(c))))<< 8; \ 14444290Swollman case 1: l|=((unsigned long)(*(--(c)))); \ 14544290Swollman } \ 14644290Swollman } 14744290Swollman 14844290Swollman#undef p_c2l_p 14944290Swollman#define p_c2l_p(c,l,sc,len) { \ 15044290Swollman switch (sc) \ 15144290Swollman { \ 15244290Swollman case 0: l =((unsigned long)(*((c)++))); \ 15344290Swollman if (--len == 0) break; \ 15444290Swollman case 1: l|=((unsigned long)(*((c)++)))<< 8; \ 15544290Swollman if (--len == 0) break; \ 15644290Swollman case 2: l|=((unsigned long)(*((c)++)))<<16; \ 15744290Swollman } \ 15844290Swollman } 15944290Swollman 16044290Swollman#undef l2c 16144290Swollman#define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \ 16244290Swollman *((c)++)=(unsigned char)(((l)>> 8)&0xff), \ 16344290Swollman *((c)++)=(unsigned char)(((l)>>16)&0xff), \ 16444290Swollman *((c)++)=(unsigned char)(((l)>>24)&0xff)) 16544290Swollman 16644290Swollman#undef ROTATE 16744290Swollman#if defined(WIN32) 16844290Swollman#define ROTATE(a,n) _lrotl(a,n) 16944290Swollman#else 17044290Swollman#define ROTATE(a,n) (((a)<<(n))|(((a)&0xffffffff)>>(32-(n)))) 17144290Swollman#endif 17244290Swollman 17344290Swollman/* A nice byte order reversal from Wei Dai <weidai@eskimo.com> */ 17444290Swollman#if defined(WIN32) 17544290Swollman/* 5 instructions with rotate instruction, else 9 */ 17644290Swollman#define Endian_Reverse32(a) \ 17744290Swollman { \ 17844290Swollman unsigned long l=(a); \ 17944290Swollman (a)=((ROTATE(l,8)&0x00FF00FF)|(ROTATE(l,24)&0xFF00FF00)); \ 18044290Swollman } 18144290Swollman#else 18244290Swollman/* 6 instructions with rotate instruction, else 8 */ 18344290Swollman#define Endian_Reverse32(a) \ 18444290Swollman { \ 18544290Swollman unsigned long l=(a); \ 18644290Swollman l=(((l&0xFF00FF00)>>8L)|((l&0x00FF00FF)<<8L)); \ 18744290Swollman (a)=ROTATE(l,16L); \ 18844290Swollman } 18944290Swollman#endif 19044290Swollman 19144290Swollman/* As pointed out by Wei Dai <weidai@eskimo.com>, F() below can be 19244290Swollman * simplified to the code in F_00_19. Wei attributes these optimisations 19344290Swollman * to Peter Gutmann's SHS code, and he attributes it to Rich Schroeppel. 19444290Swollman * #define F(x,y,z) (((x) & (y)) | ((~(x)) & (z))) 19544290Swollman * I've just become aware of another tweak to be made, again from Wei Dai, 19644290Swollman * in F_40_59, (x&a)|(y&a) -> (x|y)&a 19744290Swollman */ 19844290Swollman#define F_00_19(b,c,d) ((((c) ^ (d)) & (b)) ^ (d)) 19944290Swollman#define F_20_39(b,c,d) ((b) ^ (c) ^ (d)) 20044290Swollman#define F_40_59(b,c,d) (((b) & (c)) | (((b)|(c)) & (d))) 20144290Swollman#define F_60_79(b,c,d) F_20_39(b,c,d) 20244290Swollman 20344290Swollman#ifdef SHA_0 20444290Swollman#undef Xupdate 20544290Swollman#define Xupdate(a,i,ia,ib,ic,id) X[(i)&0x0f]=(a)=\ 20644290Swollman (ia[(i)&0x0f]^ib[((i)+2)&0x0f]^ic[((i)+8)&0x0f]^id[((i)+13)&0x0f]); 20744290Swollman#endif 20844290Swollman#ifdef SHA_1 20944290Swollman#undef Xupdate 21044290Swollman#define Xupdate(a,i,ia,ib,ic,id) (a)=\ 21144290Swollman (ia[(i)&0x0f]^ib[((i)+2)&0x0f]^ic[((i)+8)&0x0f]^id[((i)+13)&0x0f]);\ 21244290Swollman X[(i)&0x0f]=(a)=ROTATE((a),1); 21344290Swollman#endif 21444290Swollman 21544290Swollman#define BODY_00_15(i,a,b,c,d,e,f,xa) \ 21644290Swollman (f)=xa[i]+(e)+K_00_19+ROTATE((a),5)+F_00_19((b),(c),(d)); \ 21744290Swollman (b)=ROTATE((b),30); 21844290Swollman 21944290Swollman#define BODY_16_19(i,a,b,c,d,e,f,xa,xb,xc,xd) \ 22044290Swollman Xupdate(f,i,xa,xb,xc,xd); \ 22144290Swollman (f)+=(e)+K_00_19+ROTATE((a),5)+F_00_19((b),(c),(d)); \ 22244290Swollman (b)=ROTATE((b),30); 22344290Swollman 22444290Swollman#define BODY_20_31(i,a,b,c,d,e,f,xa,xb,xc,xd) \ 22544290Swollman Xupdate(f,i,xa,xb,xc,xd); \ 22644290Swollman (f)+=(e)+K_20_39+ROTATE((a),5)+F_20_39((b),(c),(d)); \ 22744290Swollman (b)=ROTATE((b),30); 22844290Swollman 22944290Swollman#define BODY_32_39(i,a,b,c,d,e,f,xa) \ 23044290Swollman Xupdate(f,i,xa,xa,xa,xa); \ 23144290Swollman (f)+=(e)+K_20_39+ROTATE((a),5)+F_20_39((b),(c),(d)); \ 23244290Swollman (b)=ROTATE((b),30); 23344290Swollman 23444290Swollman#define BODY_40_59(i,a,b,c,d,e,f,xa) \ 23544290Swollman Xupdate(f,i,xa,xa,xa,xa); \ 23644290Swollman (f)+=(e)+K_40_59+ROTATE((a),5)+F_40_59((b),(c),(d)); \ 23744290Swollman (b)=ROTATE((b),30); 23844290Swollman 23944290Swollman#define BODY_60_79(i,a,b,c,d,e,f,xa) \ 24044290Swollman Xupdate(f,i,xa,xa,xa,xa); \ 24144290Swollman (f)=X[(i)&0x0f]+(e)+K_60_79+ROTATE((a),5)+F_60_79((b),(c),(d)); \ 24244290Swollman (b)=ROTATE((b),30); 24344290Swollman 244