1d00ed299SGarrett Wollman /* crypto/ripemd/rmd_locl.h */ 2d00ed299SGarrett Wollman /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3d00ed299SGarrett Wollman * All rights reserved. 4d00ed299SGarrett Wollman * 5d00ed299SGarrett Wollman * This package is an SSL implementation written 6d00ed299SGarrett Wollman * by Eric Young (eay@cryptsoft.com). 7d00ed299SGarrett Wollman * The implementation was written so as to conform with Netscapes SSL. 8d00ed299SGarrett Wollman * 9d00ed299SGarrett Wollman * This library is free for commercial and non-commercial use as long as 10d00ed299SGarrett Wollman * the following conditions are aheared to. The following conditions 11d00ed299SGarrett Wollman * apply to all code found in this distribution, be it the RC4, RSA, 12d00ed299SGarrett Wollman * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13d00ed299SGarrett Wollman * included with this distribution is covered by the same copyright terms 14d00ed299SGarrett Wollman * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15d00ed299SGarrett Wollman * 16d00ed299SGarrett Wollman * Copyright remains Eric Young's, and as such any Copyright notices in 17d00ed299SGarrett Wollman * the code are not to be removed. 18d00ed299SGarrett Wollman * If this package is used in a product, Eric Young should be given attribution 19d00ed299SGarrett Wollman * as the author of the parts of the library used. 20d00ed299SGarrett Wollman * This can be in the form of a textual message at program startup or 21d00ed299SGarrett Wollman * in documentation (online or textual) provided with the package. 22d00ed299SGarrett Wollman * 23d00ed299SGarrett Wollman * Redistribution and use in source and binary forms, with or without 24d00ed299SGarrett Wollman * modification, are permitted provided that the following conditions 25d00ed299SGarrett Wollman * are met: 26d00ed299SGarrett Wollman * 1. Redistributions of source code must retain the copyright 27d00ed299SGarrett Wollman * notice, this list of conditions and the following disclaimer. 28d00ed299SGarrett Wollman * 2. Redistributions in binary form must reproduce the above copyright 29d00ed299SGarrett Wollman * notice, this list of conditions and the following disclaimer in the 30d00ed299SGarrett Wollman * documentation and/or other materials provided with the distribution. 31d00ed299SGarrett Wollman * 3. All advertising materials mentioning features or use of this software 32d00ed299SGarrett Wollman * must display the following acknowledgement: 33d00ed299SGarrett Wollman * "This product includes cryptographic software written by 34d00ed299SGarrett Wollman * Eric Young (eay@cryptsoft.com)" 35*3e5a11d5SGordon Bergling * The word 'cryptographic' can be left out if the routines from the library 36d00ed299SGarrett Wollman * being used are not cryptographic related :-). 37d00ed299SGarrett Wollman * 4. If you include any Windows specific code (or a derivative thereof) from 38d00ed299SGarrett Wollman * the apps directory (application code) you must include an acknowledgement: 39d00ed299SGarrett Wollman * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40d00ed299SGarrett Wollman * 41d00ed299SGarrett Wollman * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42d00ed299SGarrett Wollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43d00ed299SGarrett Wollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44d00ed299SGarrett Wollman * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45d00ed299SGarrett Wollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46d00ed299SGarrett Wollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47d00ed299SGarrett Wollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48d00ed299SGarrett Wollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49d00ed299SGarrett Wollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50d00ed299SGarrett Wollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51d00ed299SGarrett Wollman * SUCH DAMAGE. 52d00ed299SGarrett Wollman * 53d00ed299SGarrett Wollman * The licence and distribution terms for any publically available version or 54d00ed299SGarrett Wollman * derivative of this code cannot be changed. i.e. this code cannot simply be 55d00ed299SGarrett Wollman * copied and put under another distribution licence 56d00ed299SGarrett Wollman * [including the GNU Public Licence.] 57d00ed299SGarrett Wollman */ 58d00ed299SGarrett Wollman 59d00ed299SGarrett Wollman #include "ripemd.h" 60d00ed299SGarrett Wollman 61d00ed299SGarrett Wollman #undef c2nl 62d00ed299SGarrett Wollman #define c2nl(c,l) (l =(((u_int32_t)(*((c)++)))<<24), \ 63d00ed299SGarrett Wollman l|=(((u_int32_t)(*((c)++)))<<16), \ 64d00ed299SGarrett Wollman l|=(((u_int32_t)(*((c)++)))<< 8), \ 65d00ed299SGarrett Wollman l|=(((u_int32_t)(*((c)++))) )) 66d00ed299SGarrett Wollman 67d00ed299SGarrett Wollman #undef p_c2nl 68d00ed299SGarrett Wollman #define p_c2nl(c,l,n) { \ 69d00ed299SGarrett Wollman switch (n) { \ 70d00ed299SGarrett Wollman case 0: l =((u_int32_t)(*((c)++)))<<24; \ 71d00ed299SGarrett Wollman case 1: l|=((u_int32_t)(*((c)++)))<<16; \ 72d00ed299SGarrett Wollman case 2: l|=((u_int32_t)(*((c)++)))<< 8; \ 73d00ed299SGarrett Wollman case 3: l|=((u_int32_t)(*((c)++))); \ 74d00ed299SGarrett Wollman } \ 75d00ed299SGarrett Wollman } 76d00ed299SGarrett Wollman 77d00ed299SGarrett Wollman #undef c2nl_p 78d00ed299SGarrett Wollman /* NOTE the pointer is not incremented at the end of this */ 79d00ed299SGarrett Wollman #define c2nl_p(c,l,n) { \ 80d00ed299SGarrett Wollman l=0; \ 81d00ed299SGarrett Wollman (c)+=n; \ 82d00ed299SGarrett Wollman switch (n) { \ 83d00ed299SGarrett Wollman case 3: l =((u_int32_t)(*(--(c))))<< 8; \ 84d00ed299SGarrett Wollman case 2: l|=((u_int32_t)(*(--(c))))<<16; \ 85d00ed299SGarrett Wollman case 1: l|=((u_int32_t)(*(--(c))))<<24; \ 86d00ed299SGarrett Wollman } \ 87d00ed299SGarrett Wollman } 88d00ed299SGarrett Wollman 89d00ed299SGarrett Wollman #undef p_c2nl_p 90d00ed299SGarrett Wollman #define p_c2nl_p(c,l,sc,len) { \ 91d00ed299SGarrett Wollman switch (sc) \ 92d00ed299SGarrett Wollman { \ 93d00ed299SGarrett Wollman case 0: l =((u_int32_t)(*((c)++)))<<24; \ 94d00ed299SGarrett Wollman if (--len == 0) break; \ 95d00ed299SGarrett Wollman case 1: l|=((u_int32_t)(*((c)++)))<<16; \ 96d00ed299SGarrett Wollman if (--len == 0) break; \ 97d00ed299SGarrett Wollman case 2: l|=((u_int32_t)(*((c)++)))<< 8; \ 98d00ed299SGarrett Wollman } \ 99d00ed299SGarrett Wollman } 100d00ed299SGarrett Wollman 101d00ed299SGarrett Wollman #undef nl2c 102d00ed299SGarrett Wollman #define nl2c(l,c) (*((c)++)=(unsigned char)(((l)>>24)&0xff), \ 103d00ed299SGarrett Wollman *((c)++)=(unsigned char)(((l)>>16)&0xff), \ 104d00ed299SGarrett Wollman *((c)++)=(unsigned char)(((l)>> 8)&0xff), \ 105d00ed299SGarrett Wollman *((c)++)=(unsigned char)(((l) )&0xff)) 106d00ed299SGarrett Wollman 107d00ed299SGarrett Wollman #undef c2l 108d00ed299SGarrett Wollman #define c2l(c,l) (l =(((u_int32_t)(*((c)++))) ), \ 109d00ed299SGarrett Wollman l|=(((u_int32_t)(*((c)++)))<< 8), \ 110d00ed299SGarrett Wollman l|=(((u_int32_t)(*((c)++)))<<16), \ 111d00ed299SGarrett Wollman l|=(((u_int32_t)(*((c)++)))<<24)) 112d00ed299SGarrett Wollman 113d00ed299SGarrett Wollman #undef p_c2l 114d00ed299SGarrett Wollman #define p_c2l(c,l,n) { \ 115d00ed299SGarrett Wollman switch (n) { \ 116d00ed299SGarrett Wollman case 0: l =((u_int32_t)(*((c)++))); \ 117d00ed299SGarrett Wollman case 1: l|=((u_int32_t)(*((c)++)))<< 8; \ 118d00ed299SGarrett Wollman case 2: l|=((u_int32_t)(*((c)++)))<<16; \ 119d00ed299SGarrett Wollman case 3: l|=((u_int32_t)(*((c)++)))<<24; \ 120d00ed299SGarrett Wollman } \ 121d00ed299SGarrett Wollman } 122d00ed299SGarrett Wollman 123d00ed299SGarrett Wollman #undef c2l_p 124d00ed299SGarrett Wollman /* NOTE the pointer is not incremented at the end of this */ 125d00ed299SGarrett Wollman #define c2l_p(c,l,n) { \ 126d00ed299SGarrett Wollman l=0; \ 127d00ed299SGarrett Wollman (c)+=n; \ 128d00ed299SGarrett Wollman switch (n) { \ 129d00ed299SGarrett Wollman case 3: l =((u_int32_t)(*(--(c))))<<16; \ 130d00ed299SGarrett Wollman case 2: l|=((u_int32_t)(*(--(c))))<< 8; \ 131d00ed299SGarrett Wollman case 1: l|=((u_int32_t)(*(--(c)))); \ 132d00ed299SGarrett Wollman } \ 133d00ed299SGarrett Wollman } 134d00ed299SGarrett Wollman 135d00ed299SGarrett Wollman #undef p_c2l_p 136d00ed299SGarrett Wollman #define p_c2l_p(c,l,sc,len) { \ 137d00ed299SGarrett Wollman switch (sc) \ 138d00ed299SGarrett Wollman { \ 139d00ed299SGarrett Wollman case 0: l =((u_int32_t)(*((c)++))); \ 140d00ed299SGarrett Wollman if (--len == 0) break; \ 141d00ed299SGarrett Wollman case 1: l|=((u_int32_t)(*((c)++)))<< 8; \ 142d00ed299SGarrett Wollman if (--len == 0) break; \ 143d00ed299SGarrett Wollman case 2: l|=((u_int32_t)(*((c)++)))<<16; \ 144d00ed299SGarrett Wollman } \ 145d00ed299SGarrett Wollman } 146d00ed299SGarrett Wollman 147d00ed299SGarrett Wollman #undef l2c 148d00ed299SGarrett Wollman #define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \ 149d00ed299SGarrett Wollman *((c)++)=(unsigned char)(((l)>> 8)&0xff), \ 150d00ed299SGarrett Wollman *((c)++)=(unsigned char)(((l)>>16)&0xff), \ 151d00ed299SGarrett Wollman *((c)++)=(unsigned char)(((l)>>24)&0xff)) 152d00ed299SGarrett Wollman 153d00ed299SGarrett Wollman #undef ROTATE 154d00ed299SGarrett Wollman #if defined(WIN32) 155d00ed299SGarrett Wollman #define ROTATE(a,n) _lrotl(a,n) 156d00ed299SGarrett Wollman #else 157d00ed299SGarrett Wollman #define ROTATE(a,n) (((a)<<(n))|(((a)&0xffffffff)>>(32-(n)))) 158d00ed299SGarrett Wollman #endif 159d00ed299SGarrett Wollman 160d00ed299SGarrett Wollman /* A nice byte order reversal from Wei Dai <weidai@eskimo.com> */ 161d00ed299SGarrett Wollman #if defined(WIN32) 162d00ed299SGarrett Wollman /* 5 instructions with rotate instruction, else 9 */ 163d00ed299SGarrett Wollman #define Endian_Reverse32(a) \ 164d00ed299SGarrett Wollman { \ 165d00ed299SGarrett Wollman u_int32_t l=(a); \ 166d00ed299SGarrett Wollman (a)=((ROTATE(l,8)&0x00FF00FF)|(ROTATE(l,24)&0xFF00FF00)); \ 167d00ed299SGarrett Wollman } 168d00ed299SGarrett Wollman #else 169d00ed299SGarrett Wollman /* 6 instructions with rotate instruction, else 8 */ 170d00ed299SGarrett Wollman #define Endian_Reverse32(a) \ 171d00ed299SGarrett Wollman { \ 172d00ed299SGarrett Wollman u_int32_t l=(a); \ 173d00ed299SGarrett Wollman l=(((l&0xFF00FF00)>>8L)|((l&0x00FF00FF)<<8L)); \ 174d00ed299SGarrett Wollman (a)=ROTATE(l,16L); \ 175d00ed299SGarrett Wollman } 176d00ed299SGarrett Wollman #endif 177d00ed299SGarrett Wollman 178d00ed299SGarrett Wollman #define F1(x,y,z) ((x)^(y)^(z)) 179d00ed299SGarrett Wollman #define F2(x,y,z) (((x)&(y))|((~x)&z)) 180d00ed299SGarrett Wollman #define F3(x,y,z) (((x)|(~y))^(z)) 181d00ed299SGarrett Wollman #define F4(x,y,z) (((x)&(z))|((y)&(~(z)))) 182d00ed299SGarrett Wollman #define F5(x,y,z) ((x)^((y)|(~(z)))) 183d00ed299SGarrett Wollman 184d00ed299SGarrett Wollman #define RIPEMD160_A 0x67452301L 185d00ed299SGarrett Wollman #define RIPEMD160_B 0xEFCDAB89L 186d00ed299SGarrett Wollman #define RIPEMD160_C 0x98BADCFEL 187d00ed299SGarrett Wollman #define RIPEMD160_D 0x10325476L 188d00ed299SGarrett Wollman #define RIPEMD160_E 0xC3D2E1F0L 189d00ed299SGarrett Wollman 190d00ed299SGarrett Wollman #include "rmdconst.h" 191d00ed299SGarrett Wollman 192d00ed299SGarrett Wollman #define RIP1(a,b,c,d,e,w,s) { \ 193d00ed299SGarrett Wollman a+=F1(b,c,d)+X[w]; \ 194d00ed299SGarrett Wollman a=ROTATE(a,s)+e; \ 195d00ed299SGarrett Wollman c=ROTATE(c,10); } 196d00ed299SGarrett Wollman 197d00ed299SGarrett Wollman #define RIP2(a,b,c,d,e,w,s,K) { \ 198d00ed299SGarrett Wollman a+=F2(b,c,d)+X[w]+K; \ 199d00ed299SGarrett Wollman a=ROTATE(a,s)+e; \ 200d00ed299SGarrett Wollman c=ROTATE(c,10); } 201d00ed299SGarrett Wollman 202d00ed299SGarrett Wollman #define RIP3(a,b,c,d,e,w,s,K) { \ 203d00ed299SGarrett Wollman a+=F3(b,c,d)+X[w]+K; \ 204d00ed299SGarrett Wollman a=ROTATE(a,s)+e; \ 205d00ed299SGarrett Wollman c=ROTATE(c,10); } 206d00ed299SGarrett Wollman 207d00ed299SGarrett Wollman #define RIP4(a,b,c,d,e,w,s,K) { \ 208d00ed299SGarrett Wollman a+=F4(b,c,d)+X[w]+K; \ 209d00ed299SGarrett Wollman a=ROTATE(a,s)+e; \ 210d00ed299SGarrett Wollman c=ROTATE(c,10); } 211d00ed299SGarrett Wollman 212d00ed299SGarrett Wollman #define RIP5(a,b,c,d,e,w,s,K) { \ 213d00ed299SGarrett Wollman a+=F5(b,c,d)+X[w]+K; \ 214d00ed299SGarrett Wollman a=ROTATE(a,s)+e; \ 215d00ed299SGarrett Wollman c=ROTATE(c,10); } 216d00ed299SGarrett Wollman 217