Eneboo - Documentación para desarrolladores
|
00001 /* crypto/cast/cast_lcl.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 00060 #include "e_os.h" 00061 00062 #ifdef OPENSSL_SYS_WIN32 00063 #include <stdlib.h> 00064 #endif 00065 00066 00067 #undef c2l 00068 #define c2l(c,l) (l =((unsigned long)(*((c)++))) , \ 00069 l|=((unsigned long)(*((c)++)))<< 8L, \ 00070 l|=((unsigned long)(*((c)++)))<<16L, \ 00071 l|=((unsigned long)(*((c)++)))<<24L) 00072 00073 /* NOTE - c is not incremented as per c2l */ 00074 #undef c2ln 00075 #define c2ln(c,l1,l2,n) { \ 00076 c+=n; \ 00077 l1=l2=0; \ 00078 switch (n) { \ 00079 case 8: l2 =((unsigned long)(*(--(c))))<<24L; \ 00080 case 7: l2|=((unsigned long)(*(--(c))))<<16L; \ 00081 case 6: l2|=((unsigned long)(*(--(c))))<< 8L; \ 00082 case 5: l2|=((unsigned long)(*(--(c)))); \ 00083 case 4: l1 =((unsigned long)(*(--(c))))<<24L; \ 00084 case 3: l1|=((unsigned long)(*(--(c))))<<16L; \ 00085 case 2: l1|=((unsigned long)(*(--(c))))<< 8L; \ 00086 case 1: l1|=((unsigned long)(*(--(c)))); \ 00087 } \ 00088 } 00089 00090 #undef l2c 00091 #define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \ 00092 *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \ 00093 *((c)++)=(unsigned char)(((l)>>16L)&0xff), \ 00094 *((c)++)=(unsigned char)(((l)>>24L)&0xff)) 00095 00096 /* NOTE - c is not incremented as per l2c */ 00097 #undef l2cn 00098 #define l2cn(l1,l2,c,n) { \ 00099 c+=n; \ 00100 switch (n) { \ 00101 case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \ 00102 case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \ 00103 case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \ 00104 case 5: *(--(c))=(unsigned char)(((l2) )&0xff); \ 00105 case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \ 00106 case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \ 00107 case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \ 00108 case 1: *(--(c))=(unsigned char)(((l1) )&0xff); \ 00109 } \ 00110 } 00111 00112 /* NOTE - c is not incremented as per n2l */ 00113 #define n2ln(c,l1,l2,n) { \ 00114 c+=n; \ 00115 l1=l2=0; \ 00116 switch (n) { \ 00117 case 8: l2 =((unsigned long)(*(--(c)))) ; \ 00118 case 7: l2|=((unsigned long)(*(--(c))))<< 8; \ 00119 case 6: l2|=((unsigned long)(*(--(c))))<<16; \ 00120 case 5: l2|=((unsigned long)(*(--(c))))<<24; \ 00121 case 4: l1 =((unsigned long)(*(--(c)))) ; \ 00122 case 3: l1|=((unsigned long)(*(--(c))))<< 8; \ 00123 case 2: l1|=((unsigned long)(*(--(c))))<<16; \ 00124 case 1: l1|=((unsigned long)(*(--(c))))<<24; \ 00125 } \ 00126 } 00127 00128 /* NOTE - c is not incremented as per l2n */ 00129 #define l2nn(l1,l2,c,n) { \ 00130 c+=n; \ 00131 switch (n) { \ 00132 case 8: *(--(c))=(unsigned char)(((l2) )&0xff); \ 00133 case 7: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \ 00134 case 6: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \ 00135 case 5: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \ 00136 case 4: *(--(c))=(unsigned char)(((l1) )&0xff); \ 00137 case 3: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \ 00138 case 2: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \ 00139 case 1: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \ 00140 } \ 00141 } 00142 00143 #undef n2l 00144 #define n2l(c,l) (l =((unsigned long)(*((c)++)))<<24L, \ 00145 l|=((unsigned long)(*((c)++)))<<16L, \ 00146 l|=((unsigned long)(*((c)++)))<< 8L, \ 00147 l|=((unsigned long)(*((c)++)))) 00148 00149 #undef l2n 00150 #define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \ 00151 *((c)++)=(unsigned char)(((l)>>16L)&0xff), \ 00152 *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \ 00153 *((c)++)=(unsigned char)(((l) )&0xff)) 00154 00155 #if defined(OPENSSL_SYS_WIN32) && defined(_MSC_VER) 00156 #define ROTL(a,n) (_lrotl(a,n)) 00157 #else 00158 #define ROTL(a,n) ((((a)<<(n))&0xffffffffL)|((a)>>(32-(n)))) 00159 #endif 00160 00161 #define C_M 0x3fc 00162 #define C_0 22L 00163 #define C_1 14L 00164 #define C_2 6L 00165 #define C_3 2L /* left shift */ 00166 00167 /* The rotate has an extra 16 added to it to help the x86 asm */ 00168 #if defined(CAST_PTR) 00169 #define E_CAST(n,key,L,R,OP1,OP2,OP3) \ 00170 { \ 00171 int i; \ 00172 t=(key[n*2] OP1 R)&0xffffffffL; \ 00173 i=key[n*2+1]; \ 00174 t=ROTL(t,i); \ 00175 L^= (((((*(CAST_LONG *)((unsigned char *) \ 00176 CAST_S_table0+((t>>C_2)&C_M)) OP2 \ 00177 *(CAST_LONG *)((unsigned char *) \ 00178 CAST_S_table1+((t<<C_3)&C_M)))&0xffffffffL) OP3 \ 00179 *(CAST_LONG *)((unsigned char *) \ 00180 CAST_S_table2+((t>>C_0)&C_M)))&0xffffffffL) OP1 \ 00181 *(CAST_LONG *)((unsigned char *) \ 00182 CAST_S_table3+((t>>C_1)&C_M)))&0xffffffffL; \ 00183 } 00184 #elif defined(CAST_PTR2) 00185 #define E_CAST(n,key,L,R,OP1,OP2,OP3) \ 00186 { \ 00187 int i; \ 00188 CAST_LONG u,v,w; \ 00189 w=(key[n*2] OP1 R)&0xffffffffL; \ 00190 i=key[n*2+1]; \ 00191 w=ROTL(w,i); \ 00192 u=w>>C_2; \ 00193 v=w<<C_3; \ 00194 u&=C_M; \ 00195 v&=C_M; \ 00196 t= *(CAST_LONG *)((unsigned char *)CAST_S_table0+u); \ 00197 u=w>>C_0; \ 00198 t=(t OP2 *(CAST_LONG *)((unsigned char *)CAST_S_table1+v))&0xffffffffL;\ 00199 v=w>>C_1; \ 00200 u&=C_M; \ 00201 v&=C_M; \ 00202 t=(t OP3 *(CAST_LONG *)((unsigned char *)CAST_S_table2+u)&0xffffffffL);\ 00203 t=(t OP1 *(CAST_LONG *)((unsigned char *)CAST_S_table3+v)&0xffffffffL);\ 00204 L^=(t&0xffffffff); \ 00205 } 00206 #else 00207 #define E_CAST(n,key,L,R,OP1,OP2,OP3) \ 00208 { \ 00209 CAST_LONG a,b,c,d; \ 00210 t=(key[n*2] OP1 R)&0xffffffff; \ 00211 t=ROTL(t,(key[n*2+1])); \ 00212 a=CAST_S_table0[(t>> 8)&0xff]; \ 00213 b=CAST_S_table1[(t )&0xff]; \ 00214 c=CAST_S_table2[(t>>24)&0xff]; \ 00215 d=CAST_S_table3[(t>>16)&0xff]; \ 00216 L^=(((((a OP2 b)&0xffffffffL) OP3 c)&0xffffffffL) OP1 d)&0xffffffffL; \ 00217 } 00218 #endif 00219 00220 extern const CAST_LONG CAST_S_table0[256]; 00221 extern const CAST_LONG CAST_S_table1[256]; 00222 extern const CAST_LONG CAST_S_table2[256]; 00223 extern const CAST_LONG CAST_S_table3[256]; 00224 extern const CAST_LONG CAST_S_table4[256]; 00225 extern const CAST_LONG CAST_S_table5[256]; 00226 extern const CAST_LONG CAST_S_table6[256]; 00227 extern const CAST_LONG CAST_S_table7[256];