144301Swollman/* crypto/ripemd/rmd_locl.h */
244301Swollman/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
344301Swollman * All rights reserved.
444301Swollman *
544301Swollman * This package is an SSL implementation written
644301Swollman * by Eric Young (eay@cryptsoft.com).
744301Swollman * The implementation was written so as to conform with Netscapes SSL.
844301Swollman *
944301Swollman * This library is free for commercial and non-commercial use as long as
1044301Swollman * the following conditions are aheared to.  The following conditions
1144301Swollman * apply to all code found in this distribution, be it the RC4, RSA,
1244301Swollman * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1344301Swollman * included with this distribution is covered by the same copyright terms
1444301Swollman * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1544301Swollman *
1644301Swollman * Copyright remains Eric Young's, and as such any Copyright notices in
1744301Swollman * the code are not to be removed.
1844301Swollman * If this package is used in a product, Eric Young should be given attribution
1944301Swollman * as the author of the parts of the library used.
2044301Swollman * This can be in the form of a textual message at program startup or
2144301Swollman * in documentation (online or textual) provided with the package.
2244301Swollman *
2344301Swollman * Redistribution and use in source and binary forms, with or without
2444301Swollman * modification, are permitted provided that the following conditions
2544301Swollman * are met:
2644301Swollman * 1. Redistributions of source code must retain the copyright
2744301Swollman *    notice, this list of conditions and the following disclaimer.
2844301Swollman * 2. Redistributions in binary form must reproduce the above copyright
2944301Swollman *    notice, this list of conditions and the following disclaimer in the
3044301Swollman *    documentation and/or other materials provided with the distribution.
3144301Swollman * 3. All advertising materials mentioning features or use of this software
3244301Swollman *    must display the following acknowledgement:
3344301Swollman *    "This product includes cryptographic software written by
3444301Swollman *     Eric Young (eay@cryptsoft.com)"
3544301Swollman *    The word 'cryptographic' can be left out if the rouines from the library
3644301Swollman *    being used are not cryptographic related :-).
3744301Swollman * 4. If you include any Windows specific code (or a derivative thereof) from
3844301Swollman *    the apps directory (application code) you must include an acknowledgement:
3944301Swollman *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4044301Swollman *
4144301Swollman * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4244301Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4344301Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4444301Swollman * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4544301Swollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4644301Swollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4744301Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4844301Swollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4944301Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5044301Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5144301Swollman * SUCH DAMAGE.
5244301Swollman *
5344301Swollman * The licence and distribution terms for any publically available version or
5444301Swollman * derivative of this code cannot be changed.  i.e. this code cannot simply be
5544301Swollman * copied and put under another distribution licence
5644301Swollman * [including the GNU Public Licence.]
5744301Swollman */
5844301Swollman
5944301Swollman#include "ripemd.h"
6044301Swollman
6144301Swollman#undef c2nl
6244301Swollman#define c2nl(c,l)	(l =(((u_int32_t)(*((c)++)))<<24), \
6344301Swollman			 l|=(((u_int32_t)(*((c)++)))<<16), \
6444301Swollman			 l|=(((u_int32_t)(*((c)++)))<< 8), \
6544301Swollman			 l|=(((u_int32_t)(*((c)++)))    ))
6644301Swollman
6744301Swollman#undef p_c2nl
6844301Swollman#define p_c2nl(c,l,n)	{ \
6944301Swollman			switch (n) { \
7044301Swollman			case 0: l =((u_int32_t)(*((c)++)))<<24; \
7144301Swollman			case 1: l|=((u_int32_t)(*((c)++)))<<16; \
7244301Swollman			case 2: l|=((u_int32_t)(*((c)++)))<< 8; \
7344301Swollman			case 3: l|=((u_int32_t)(*((c)++))); \
7444301Swollman				} \
7544301Swollman			}
7644301Swollman
7744301Swollman#undef c2nl_p
7844301Swollman/* NOTE the pointer is not incremented at the end of this */
7944301Swollman#define c2nl_p(c,l,n)	{ \
8044301Swollman			l=0; \
8144301Swollman			(c)+=n; \
8244301Swollman			switch (n) { \
8344301Swollman			case 3: l =((u_int32_t)(*(--(c))))<< 8; \
8444301Swollman			case 2: l|=((u_int32_t)(*(--(c))))<<16; \
8544301Swollman			case 1: l|=((u_int32_t)(*(--(c))))<<24; \
8644301Swollman				} \
8744301Swollman			}
8844301Swollman
8944301Swollman#undef p_c2nl_p
9044301Swollman#define p_c2nl_p(c,l,sc,len) { \
9144301Swollman			switch (sc) \
9244301Swollman				{ \
9344301Swollman			case 0: l =((u_int32_t)(*((c)++)))<<24; \
9444301Swollman				if (--len == 0) break; \
9544301Swollman			case 1: l|=((u_int32_t)(*((c)++)))<<16; \
9644301Swollman				if (--len == 0) break; \
9744301Swollman			case 2: l|=((u_int32_t)(*((c)++)))<< 8; \
9844301Swollman				} \
9944301Swollman			}
10044301Swollman
10144301Swollman#undef nl2c
10244301Swollman#define nl2c(l,c)	(*((c)++)=(unsigned char)(((l)>>24)&0xff), \
10344301Swollman			 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
10444301Swollman			 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
10544301Swollman			 *((c)++)=(unsigned char)(((l)    )&0xff))
10644301Swollman
10744301Swollman#undef c2l
10844301Swollman#define c2l(c,l)	(l =(((u_int32_t)(*((c)++)))    ), \
10944301Swollman			 l|=(((u_int32_t)(*((c)++)))<< 8), \
11044301Swollman			 l|=(((u_int32_t)(*((c)++)))<<16), \
11144301Swollman			 l|=(((u_int32_t)(*((c)++)))<<24))
11244301Swollman
11344301Swollman#undef p_c2l
11444301Swollman#define p_c2l(c,l,n)	{ \
11544301Swollman			switch (n) { \
11644301Swollman			case 0: l =((u_int32_t)(*((c)++))); \
11744301Swollman			case 1: l|=((u_int32_t)(*((c)++)))<< 8; \
11844301Swollman			case 2: l|=((u_int32_t)(*((c)++)))<<16; \
11944301Swollman			case 3: l|=((u_int32_t)(*((c)++)))<<24; \
12044301Swollman				} \
12144301Swollman			}
12244301Swollman
12344301Swollman#undef c2l_p
12444301Swollman/* NOTE the pointer is not incremented at the end of this */
12544301Swollman#define c2l_p(c,l,n)	{ \
12644301Swollman			l=0; \
12744301Swollman			(c)+=n; \
12844301Swollman			switch (n) { \
12944301Swollman			case 3: l =((u_int32_t)(*(--(c))))<<16; \
13044301Swollman			case 2: l|=((u_int32_t)(*(--(c))))<< 8; \
13144301Swollman			case 1: l|=((u_int32_t)(*(--(c)))); \
13244301Swollman				} \
13344301Swollman			}
13444301Swollman
13544301Swollman#undef p_c2l_p
13644301Swollman#define p_c2l_p(c,l,sc,len) { \
13744301Swollman			switch (sc) \
13844301Swollman				{ \
13944301Swollman			case 0: l =((u_int32_t)(*((c)++))); \
14044301Swollman				if (--len == 0) break; \
14144301Swollman			case 1: l|=((u_int32_t)(*((c)++)))<< 8; \
14244301Swollman				if (--len == 0) break; \
14344301Swollman			case 2: l|=((u_int32_t)(*((c)++)))<<16; \
14444301Swollman				} \
14544301Swollman			}
14644301Swollman
14744301Swollman#undef l2c
14844301Swollman#define l2c(l,c)	(*((c)++)=(unsigned char)(((l)    )&0xff), \
14944301Swollman			 *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
15044301Swollman			 *((c)++)=(unsigned char)(((l)>>16)&0xff), \
15144301Swollman			 *((c)++)=(unsigned char)(((l)>>24)&0xff))
15244301Swollman
15344301Swollman#undef ROTATE
15444301Swollman#if defined(WIN32)
15544301Swollman#define ROTATE(a,n)     _lrotl(a,n)
15644301Swollman#else
15744301Swollman#define ROTATE(a,n)     (((a)<<(n))|(((a)&0xffffffff)>>(32-(n))))
15844301Swollman#endif
15944301Swollman
16044301Swollman/* A nice byte order reversal from Wei Dai <weidai@eskimo.com> */
16144301Swollman#if defined(WIN32)
16244301Swollman/* 5 instructions with rotate instruction, else 9 */
16344301Swollman#define Endian_Reverse32(a) \
16444301Swollman	{ \
16544301Swollman	u_int32_t l=(a); \
16644301Swollman	(a)=((ROTATE(l,8)&0x00FF00FF)|(ROTATE(l,24)&0xFF00FF00)); \
16744301Swollman	}
16844301Swollman#else
16944301Swollman/* 6 instructions with rotate instruction, else 8 */
17044301Swollman#define Endian_Reverse32(a) \
17144301Swollman	{ \
17244301Swollman	u_int32_t l=(a); \
17344301Swollman	l=(((l&0xFF00FF00)>>8L)|((l&0x00FF00FF)<<8L)); \
17444301Swollman	(a)=ROTATE(l,16L); \
17544301Swollman	}
17644301Swollman#endif
17744301Swollman
17844301Swollman#define F1(x,y,z)	 ((x)^(y)^(z))
17944301Swollman#define F2(x,y,z)	(((x)&(y))|((~x)&z))
18044301Swollman#define F3(x,y,z)	(((x)|(~y))^(z))
18144301Swollman#define F4(x,y,z)	(((x)&(z))|((y)&(~(z))))
18244301Swollman#define F5(x,y,z)	 ((x)^((y)|(~(z))))
18344301Swollman
18444301Swollman#define RIPEMD160_A	0x67452301L
18544301Swollman#define RIPEMD160_B	0xEFCDAB89L
18644301Swollman#define RIPEMD160_C	0x98BADCFEL
18744301Swollman#define RIPEMD160_D	0x10325476L
18844301Swollman#define RIPEMD160_E	0xC3D2E1F0L
18944301Swollman
19044301Swollman#include "rmdconst.h"
19144301Swollman
19244301Swollman#define RIP1(a,b,c,d,e,w,s) { \
19344301Swollman	a+=F1(b,c,d)+X[w]; \
19444301Swollman        a=ROTATE(a,s)+e; \
19544301Swollman        c=ROTATE(c,10); }
19644301Swollman
19744301Swollman#define RIP2(a,b,c,d,e,w,s,K) { \
19844301Swollman	a+=F2(b,c,d)+X[w]+K; \
19944301Swollman        a=ROTATE(a,s)+e; \
20044301Swollman        c=ROTATE(c,10); }
20144301Swollman
20244301Swollman#define RIP3(a,b,c,d,e,w,s,K) { \
20344301Swollman	a+=F3(b,c,d)+X[w]+K; \
20444301Swollman        a=ROTATE(a,s)+e; \
20544301Swollman        c=ROTATE(c,10); }
20644301Swollman
20744301Swollman#define RIP4(a,b,c,d,e,w,s,K) { \
20844301Swollman	a+=F4(b,c,d)+X[w]+K; \
20944301Swollman        a=ROTATE(a,s)+e; \
21044301Swollman        c=ROTATE(c,10); }
21144301Swollman
21244301Swollman#define RIP5(a,b,c,d,e,w,s,K) { \
21344301Swollman	a+=F5(b,c,d)+X[w]+K; \
21444301Swollman        a=ROTATE(a,s)+e; \
21544301Swollman        c=ROTATE(c,10); }
21644301Swollman
217