155714Skris/* crypto/cast/cast_lcl.h */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 8280304Sjkim * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15280304Sjkim * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 22280304Sjkim * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 37280304Sjkim * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40280304Sjkim * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 52280304Sjkim * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 5855714Skris 59109998Smarkm#include "e_os.h" 60109998Smarkm 61109998Smarkm#ifdef OPENSSL_SYS_WIN32 62280304Sjkim# include <stdlib.h> 6355714Skris#endif 6455714Skris 6555714Skris#undef c2l 66280304Sjkim#define c2l(c,l) (l =((unsigned long)(*((c)++))) , \ 67280304Sjkim l|=((unsigned long)(*((c)++)))<< 8L, \ 68280304Sjkim l|=((unsigned long)(*((c)++)))<<16L, \ 69280304Sjkim l|=((unsigned long)(*((c)++)))<<24L) 7055714Skris 7155714Skris/* NOTE - c is not incremented as per c2l */ 7255714Skris#undef c2ln 73280304Sjkim#define c2ln(c,l1,l2,n) { \ 74280304Sjkim c+=n; \ 75280304Sjkim l1=l2=0; \ 76280304Sjkim switch (n) { \ 77280304Sjkim case 8: l2 =((unsigned long)(*(--(c))))<<24L; \ 78280304Sjkim case 7: l2|=((unsigned long)(*(--(c))))<<16L; \ 79280304Sjkim case 6: l2|=((unsigned long)(*(--(c))))<< 8L; \ 80280304Sjkim case 5: l2|=((unsigned long)(*(--(c)))); \ 81280304Sjkim case 4: l1 =((unsigned long)(*(--(c))))<<24L; \ 82280304Sjkim case 3: l1|=((unsigned long)(*(--(c))))<<16L; \ 83280304Sjkim case 2: l1|=((unsigned long)(*(--(c))))<< 8L; \ 84280304Sjkim case 1: l1|=((unsigned long)(*(--(c)))); \ 85280304Sjkim } \ 86280304Sjkim } 8755714Skris 8855714Skris#undef l2c 89280304Sjkim#define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \ 90280304Sjkim *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \ 91280304Sjkim *((c)++)=(unsigned char)(((l)>>16L)&0xff), \ 92280304Sjkim *((c)++)=(unsigned char)(((l)>>24L)&0xff)) 9355714Skris 9455714Skris/* NOTE - c is not incremented as per l2c */ 9555714Skris#undef l2cn 96280304Sjkim#define l2cn(l1,l2,c,n) { \ 97280304Sjkim c+=n; \ 98280304Sjkim switch (n) { \ 99280304Sjkim case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \ 100280304Sjkim case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \ 101280304Sjkim case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \ 102280304Sjkim case 5: *(--(c))=(unsigned char)(((l2) )&0xff); \ 103280304Sjkim case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \ 104280304Sjkim case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \ 105280304Sjkim case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \ 106280304Sjkim case 1: *(--(c))=(unsigned char)(((l1) )&0xff); \ 107280304Sjkim } \ 108280304Sjkim } 10955714Skris 11055714Skris/* NOTE - c is not incremented as per n2l */ 111280304Sjkim#define n2ln(c,l1,l2,n) { \ 112280304Sjkim c+=n; \ 113280304Sjkim l1=l2=0; \ 114280304Sjkim switch (n) { \ 115280304Sjkim case 8: l2 =((unsigned long)(*(--(c)))) ; \ 116280304Sjkim case 7: l2|=((unsigned long)(*(--(c))))<< 8; \ 117280304Sjkim case 6: l2|=((unsigned long)(*(--(c))))<<16; \ 118280304Sjkim case 5: l2|=((unsigned long)(*(--(c))))<<24; \ 119280304Sjkim case 4: l1 =((unsigned long)(*(--(c)))) ; \ 120280304Sjkim case 3: l1|=((unsigned long)(*(--(c))))<< 8; \ 121280304Sjkim case 2: l1|=((unsigned long)(*(--(c))))<<16; \ 122280304Sjkim case 1: l1|=((unsigned long)(*(--(c))))<<24; \ 123280304Sjkim } \ 124280304Sjkim } 12555714Skris 12655714Skris/* NOTE - c is not incremented as per l2n */ 127280304Sjkim#define l2nn(l1,l2,c,n) { \ 128280304Sjkim c+=n; \ 129280304Sjkim switch (n) { \ 130280304Sjkim case 8: *(--(c))=(unsigned char)(((l2) )&0xff); \ 131280304Sjkim case 7: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \ 132280304Sjkim case 6: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \ 133280304Sjkim case 5: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \ 134280304Sjkim case 4: *(--(c))=(unsigned char)(((l1) )&0xff); \ 135280304Sjkim case 3: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \ 136280304Sjkim case 2: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \ 137280304Sjkim case 1: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \ 138280304Sjkim } \ 139280304Sjkim } 14055714Skris 14155714Skris#undef n2l 14255714Skris#define n2l(c,l) (l =((unsigned long)(*((c)++)))<<24L, \ 14355714Skris l|=((unsigned long)(*((c)++)))<<16L, \ 14455714Skris l|=((unsigned long)(*((c)++)))<< 8L, \ 14555714Skris l|=((unsigned long)(*((c)++)))) 14655714Skris 14755714Skris#undef l2n 14855714Skris#define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \ 14955714Skris *((c)++)=(unsigned char)(((l)>>16L)&0xff), \ 15055714Skris *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \ 15155714Skris *((c)++)=(unsigned char)(((l) )&0xff)) 15255714Skris 153109998Smarkm#if defined(OPENSSL_SYS_WIN32) && defined(_MSC_VER) 154280304Sjkim# define ROTL(a,n) (_lrotl(a,n)) 15555714Skris#else 156280304Sjkim# define ROTL(a,n) ((((a)<<(n))&0xffffffffL)|((a)>>(32-(n)))) 15755714Skris#endif 15855714Skris 15955714Skris#define C_M 0x3fc 16055714Skris#define C_0 22L 16155714Skris#define C_1 14L 16255714Skris#define C_2 6L 163280304Sjkim#define C_3 2L /* left shift */ 16455714Skris 16555714Skris/* The rotate has an extra 16 added to it to help the x86 asm */ 16655714Skris#if defined(CAST_PTR) 167280304Sjkim# define E_CAST(n,key,L,R,OP1,OP2,OP3) \ 168280304Sjkim { \ 169280304Sjkim int i; \ 170280304Sjkim t=(key[n*2] OP1 R)&0xffffffffL; \ 171280304Sjkim i=key[n*2+1]; \ 172280304Sjkim t=ROTL(t,i); \ 173280304Sjkim L^= (((((*(CAST_LONG *)((unsigned char *) \ 174280304Sjkim CAST_S_table0+((t>>C_2)&C_M)) OP2 \ 175280304Sjkim *(CAST_LONG *)((unsigned char *) \ 176280304Sjkim CAST_S_table1+((t<<C_3)&C_M)))&0xffffffffL) OP3 \ 177280304Sjkim *(CAST_LONG *)((unsigned char *) \ 178280304Sjkim CAST_S_table2+((t>>C_0)&C_M)))&0xffffffffL) OP1 \ 179280304Sjkim *(CAST_LONG *)((unsigned char *) \ 180280304Sjkim CAST_S_table3+((t>>C_1)&C_M)))&0xffffffffL; \ 181280304Sjkim } 18255714Skris#elif defined(CAST_PTR2) 183280304Sjkim# define E_CAST(n,key,L,R,OP1,OP2,OP3) \ 184280304Sjkim { \ 185280304Sjkim int i; \ 186280304Sjkim CAST_LONG u,v,w; \ 187280304Sjkim w=(key[n*2] OP1 R)&0xffffffffL; \ 188280304Sjkim i=key[n*2+1]; \ 189280304Sjkim w=ROTL(w,i); \ 190280304Sjkim u=w>>C_2; \ 191280304Sjkim v=w<<C_3; \ 192280304Sjkim u&=C_M; \ 193280304Sjkim v&=C_M; \ 194280304Sjkim t= *(CAST_LONG *)((unsigned char *)CAST_S_table0+u); \ 195280304Sjkim u=w>>C_0; \ 196280304Sjkim t=(t OP2 *(CAST_LONG *)((unsigned char *)CAST_S_table1+v))&0xffffffffL;\ 197280304Sjkim v=w>>C_1; \ 198280304Sjkim u&=C_M; \ 199280304Sjkim v&=C_M; \ 200280304Sjkim t=(t OP3 *(CAST_LONG *)((unsigned char *)CAST_S_table2+u)&0xffffffffL);\ 201280304Sjkim t=(t OP1 *(CAST_LONG *)((unsigned char *)CAST_S_table3+v)&0xffffffffL);\ 202280304Sjkim L^=(t&0xffffffff); \ 203280304Sjkim } 20455714Skris#else 205280304Sjkim# define E_CAST(n,key,L,R,OP1,OP2,OP3) \ 206280304Sjkim { \ 207280304Sjkim CAST_LONG a,b,c,d; \ 208280304Sjkim t=(key[n*2] OP1 R)&0xffffffff; \ 209280304Sjkim t=ROTL(t,(key[n*2+1])); \ 210280304Sjkim a=CAST_S_table0[(t>> 8)&0xff]; \ 211280304Sjkim b=CAST_S_table1[(t )&0xff]; \ 212280304Sjkim c=CAST_S_table2[(t>>24)&0xff]; \ 213280304Sjkim d=CAST_S_table3[(t>>16)&0xff]; \ 214280304Sjkim L^=(((((a OP2 b)&0xffffffffL) OP3 c)&0xffffffffL) OP1 d)&0xffffffffL; \ 215280304Sjkim } 21655714Skris#endif 21755714Skris 218160814Ssimonextern const CAST_LONG CAST_S_table0[256]; 219160814Ssimonextern const CAST_LONG CAST_S_table1[256]; 220160814Ssimonextern const CAST_LONG CAST_S_table2[256]; 221160814Ssimonextern const CAST_LONG CAST_S_table3[256]; 222160814Ssimonextern const CAST_LONG CAST_S_table4[256]; 223160814Ssimonextern const CAST_LONG CAST_S_table5[256]; 224160814Ssimonextern const CAST_LONG CAST_S_table6[256]; 225160814Ssimonextern const CAST_LONG CAST_S_table7[256]; 226