sha_locl.h

Go to the documentation of this file.
00001 /* crypto/sha/sha_locl.h */
00002 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
00003  * All rights reserved.
00004  *
00005  * This package is an SSL implementation written
00006  * by Eric Young (eay@cryptsoft.com).
00007  * The implementation was written so as to conform with Netscapes SSL.
00008  * 
00009  * This library is free for commercial and non-commercial use as long as
00010  * the following conditions are aheared to.  The following conditions
00011  * apply to all code found in this distribution, be it the RC4, RSA,
00012  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
00013  * included with this distribution is covered by the same copyright terms
00014  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
00015  * 
00016  * Copyright remains Eric Young's, and as such any Copyright notices in
00017  * the code are not to be removed.
00018  * If this package is used in a product, Eric Young should be given attribution
00019  * as the author of the parts of the library used.
00020  * This can be in the form of a textual message at program startup or
00021  * in documentation (online or textual) provided with the package.
00022  * 
00023  * Redistribution and use in source and binary forms, with or without
00024  * modification, are permitted provided that the following conditions
00025  * are met:
00026  * 1. Redistributions of source code must retain the copyright
00027  *    notice, this list of conditions and the following disclaimer.
00028  * 2. Redistributions in binary form must reproduce the above copyright
00029  *    notice, this list of conditions and the following disclaimer in the
00030  *    documentation and/or other materials provided with the distribution.
00031  * 3. All advertising materials mentioning features or use of this software
00032  *    must display the following acknowledgement:
00033  *    "This product includes cryptographic software written by
00034  *     Eric Young (eay@cryptsoft.com)"
00035  *    The word 'cryptographic' can be left out if the rouines from the library
00036  *    being used are not cryptographic related :-).
00037  * 4. If you include any Windows specific code (or a derivative thereof) from 
00038  *    the apps directory (application code) you must include an acknowledgement:
00039  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
00040  * 
00041  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
00042  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00043  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00044  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00045  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00046  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00047  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00048  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00049  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00050  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00051  * SUCH DAMAGE.
00052  * 
00053  * The licence and distribution terms for any publically available version or
00054  * derivative of this code cannot be changed.  i.e. this code cannot simply be
00055  * copied and put under another distribution licence
00056  * [including the GNU Public Licence.]
00057  */
00058 
00059 #include <stdlib.h>
00060 #include <string.h>
00061 
00062 #ifdef undef
00063 /* one or the other needs to be defined */
00064 #ifndef SHA_1 /* FIPE 180-1 */
00065 #define SHA_0 /* FIPS 180   */
00066 #endif
00067 #endif
00068 
00069 #define ULONG   unsigned long
00070 #define UCHAR   unsigned char
00071 #define UINT    unsigned int
00072 
00073 #ifdef NOCONST
00074 #define const
00075 #endif
00076 
00077 #undef c2nl
00078 #define c2nl(c,l)   (l =(((unsigned long)(*((c)++)))<<24), \
00079              l|=(((unsigned long)(*((c)++)))<<16), \
00080              l|=(((unsigned long)(*((c)++)))<< 8), \
00081              l|=(((unsigned long)(*((c)++)))    ))
00082 
00083 #undef p_c2nl
00084 #define p_c2nl(c,l,n)   { \
00085             switch (n) { \
00086             case 0: l =((unsigned long)(*((c)++)))<<24; \
00087             case 1: l|=((unsigned long)(*((c)++)))<<16; \
00088             case 2: l|=((unsigned long)(*((c)++)))<< 8; \
00089             case 3: l|=((unsigned long)(*((c)++))); \
00090                 } \
00091             }
00092 
00093 #undef c2nl_p
00094 /* NOTE the pointer is not incremented at the end of this */
00095 #define c2nl_p(c,l,n)   { \
00096             l=0; \
00097             (c)+=n; \
00098             switch (n) { \
00099             case 3: l =((unsigned long)(*(--(c))))<< 8; \
00100             case 2: l|=((unsigned long)(*(--(c))))<<16; \
00101             case 1: l|=((unsigned long)(*(--(c))))<<24; \
00102                 } \
00103             }
00104 
00105 #undef p_c2nl_p
00106 #define p_c2nl_p(c,l,sc,len) { \
00107             switch (sc) \
00108                 { \
00109             case 0: l =((unsigned long)(*((c)++)))<<24; \
00110                 if (--len == 0) break; \
00111             case 1: l|=((unsigned long)(*((c)++)))<<16; \
00112                 if (--len == 0) break; \
00113             case 2: l|=((unsigned long)(*((c)++)))<< 8; \
00114                 } \
00115             }
00116 
00117 #undef nl2c
00118 #define nl2c(l,c)   (*((c)++)=(unsigned char)(((l)>>24)&0xff), \
00119              *((c)++)=(unsigned char)(((l)>>16)&0xff), \
00120              *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
00121              *((c)++)=(unsigned char)(((l)    )&0xff))
00122 
00123 #undef c2l
00124 #define c2l(c,l)    (l =(((unsigned long)(*((c)++)))    ), \
00125              l|=(((unsigned long)(*((c)++)))<< 8), \
00126              l|=(((unsigned long)(*((c)++)))<<16), \
00127              l|=(((unsigned long)(*((c)++)))<<24))
00128 
00129 #undef p_c2l
00130 #define p_c2l(c,l,n)    { \
00131             switch (n) { \
00132             case 0: l =((unsigned long)(*((c)++))); \
00133             case 1: l|=((unsigned long)(*((c)++)))<< 8; \
00134             case 2: l|=((unsigned long)(*((c)++)))<<16; \
00135             case 3: l|=((unsigned long)(*((c)++)))<<24; \
00136                 } \
00137             }
00138 
00139 #undef c2l_p
00140 /* NOTE the pointer is not incremented at the end of this */
00141 #define c2l_p(c,l,n)    { \
00142             l=0; \
00143             (c)+=n; \
00144             switch (n) { \
00145             case 3: l =((unsigned long)(*(--(c))))<<16; \
00146             case 2: l|=((unsigned long)(*(--(c))))<< 8; \
00147             case 1: l|=((unsigned long)(*(--(c)))); \
00148                 } \
00149             }
00150 
00151 #undef p_c2l_p
00152 #define p_c2l_p(c,l,sc,len) { \
00153             switch (sc) \
00154                 { \
00155             case 0: l =((unsigned long)(*((c)++))); \
00156                 if (--len == 0) break; \
00157             case 1: l|=((unsigned long)(*((c)++)))<< 8; \
00158                 if (--len == 0) break; \
00159             case 2: l|=((unsigned long)(*((c)++)))<<16; \
00160                 } \
00161             }
00162 
00163 #undef l2c
00164 #define l2c(l,c)    (*((c)++)=(unsigned char)(((l)    )&0xff), \
00165              *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
00166              *((c)++)=(unsigned char)(((l)>>16)&0xff), \
00167              *((c)++)=(unsigned char)(((l)>>24)&0xff))
00168 
00169 /* (AR 02/17/1999
00170 #undef ROTATE
00171 #if defined(WIN32)
00172 #define ROTATE(a,n)     _lrotl(a,n)
00173 #else
00174 #define ROTATE(a,n)     (((a)<<(n))|(((a)&0xffffffff)>>(32-(n))))
00175 #endif
00176 */
00177 #define ROTATE(a,n)     (((a)<<(n))|(((a)&0xffffffff)>>(32-(n))))
00178 
00179 /* A nice byte order reversal from Wei Dai <weidai@eskimo.com> */
00180 #if defined(WIN32)
00181 /* 5 instructions with rotate instruction, else 9 */
00182 #define Endian_Reverse32(a) \
00183     { \
00184     unsigned long l=(a); \
00185     (a)=((ROTATE(l,8)&0x00FF00FF)|(ROTATE(l,24)&0xFF00FF00)); \
00186     }
00187 #else
00188 /* 6 instructions with rotate instruction, else 8 */
00189 #define Endian_Reverse32(a) \
00190     { \
00191     unsigned long l=(a); \
00192     l=(((l&0xFF00FF00)>>8L)|((l&0x00FF00FF)<<8L)); \
00193     (a)=ROTATE(l,16L); \
00194     }
00195 #endif
00196 
00197 /* As  pointed out by Wei Dai <weidai@eskimo.com>, F() below can be
00198  * simplified to the code in F_00_19.  Wei attributes these optimisations
00199  * to Peter Gutmann's SHS code, and he attributes it to Rich Schroeppel.
00200  * #define F(x,y,z) (((x) & (y))  |  ((~(x)) & (z)))
00201  * I've just become aware of another tweak to be made, again from Wei Dai,
00202  * in F_40_59, (x&a)|(y&a) -> (x|y)&a
00203  */
00204 #define F_00_19(b,c,d)  ((((c) ^ (d)) & (b)) ^ (d)) 
00205 #define F_20_39(b,c,d)  ((b) ^ (c) ^ (d))
00206 #define F_40_59(b,c,d)  (((b) & (c)) | (((b)|(c)) & (d))) 
00207 #define F_60_79(b,c,d)  F_20_39(b,c,d)
00208 
00209 #ifdef SHA_0
00210 #undef Xupdate
00211 #define Xupdate(a,i,ia,ib,ic,id) X[(i)&0x0f]=(a)=\
00212     (ia[(i)&0x0f]^ib[((i)+2)&0x0f]^ic[((i)+8)&0x0f]^id[((i)+13)&0x0f]);
00213 #endif
00214 #ifdef SHA_1
00215 #undef Xupdate
00216 #define Xupdate(a,i,ia,ib,ic,id) (a)=\
00217     (ia[(i)&0x0f]^ib[((i)+2)&0x0f]^ic[((i)+8)&0x0f]^id[((i)+13)&0x0f]);\
00218     X[(i)&0x0f]=(a)=ROTATE((a),1);
00219 #endif
00220 
00221 #define BODY_00_15(i,a,b,c,d,e,f,xa) \
00222     (f)=xa[i]+(e)+K_00_19+ROTATE((a),5)+F_00_19((b),(c),(d)); \
00223     (b)=ROTATE((b),30);
00224 
00225 #define BODY_16_19(i,a,b,c,d,e,f,xa,xb,xc,xd) \
00226     Xupdate(f,i,xa,xb,xc,xd); \
00227     (f)+=(e)+K_00_19+ROTATE((a),5)+F_00_19((b),(c),(d)); \
00228     (b)=ROTATE((b),30);
00229 
00230 #define BODY_20_31(i,a,b,c,d,e,f,xa,xb,xc,xd) \
00231     Xupdate(f,i,xa,xb,xc,xd); \
00232     (f)+=(e)+K_20_39+ROTATE((a),5)+F_20_39((b),(c),(d)); \
00233     (b)=ROTATE((b),30);
00234 
00235 #define BODY_32_39(i,a,b,c,d,e,f,xa) \
00236     Xupdate(f,i,xa,xa,xa,xa); \
00237     (f)+=(e)+K_20_39+ROTATE((a),5)+F_20_39((b),(c),(d)); \
00238     (b)=ROTATE((b),30);
00239 
00240 #define BODY_40_59(i,a,b,c,d,e,f,xa) \
00241     Xupdate(f,i,xa,xa,xa,xa); \
00242     (f)+=(e)+K_40_59+ROTATE((a),5)+F_40_59((b),(c),(d)); \
00243     (b)=ROTATE((b),30);
00244 
00245 #define BODY_60_79(i,a,b,c,d,e,f,xa) \
00246     Xupdate(f,i,xa,xa,xa,xa); \
00247     (f)=X[(i)&0x0f]+(e)+K_60_79+ROTATE((a),5)+F_60_79((b),(c),(d)); \
00248     (b)=ROTATE((b),30);
00249 

Generated on Wed May 31 18:20:00 2006 for frontierkernel 10.1.10a by  doxygen 1.4.6