Eneboo - Documentación para desarrolladores
|
00001 /* crypto/idea/idea_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 /* The new form of this macro (check if the a*b == 0) was suggested by 00060 * Colin Plumb <colin@nyx10.cs.du.edu> */ 00061 /* Removal of the inner if from from Wei Dai 24/4/96 */ 00062 #define idea_mul(r,a,b,ul) \ 00063 ul=(unsigned long)a*b; \ 00064 if (ul != 0) \ 00065 { \ 00066 r=(ul&0xffff)-(ul>>16); \ 00067 r-=((r)>>16); \ 00068 } \ 00069 else \ 00070 r=(-(int)a-b+1); /* assuming a or b is 0 and in range */ 00071 00072 #ifdef undef 00073 #define idea_mul(r,a,b,ul,sl) \ 00074 if (a == 0) r=(0x10001-b)&0xffff; \ 00075 else if (b == 0) r=(0x10001-a)&0xffff; \ 00076 else { \ 00077 ul=(unsigned long)a*b; \ 00078 sl=(ul&0xffff)-(ul>>16); \ 00079 if (sl <= 0) sl+=0x10001; \ 00080 r=sl; \ 00081 } 00082 #endif 00083 00084 /* 7/12/95 - Many thanks to Rhys Weatherley <rweather@us.oracle.com> 00085 * for pointing out that I was assuming little endian 00086 * byte order for all quantities what idea 00087 * actually used bigendian. No where in the spec does it mention 00088 * this, it is all in terms of 16 bit numbers and even the example 00089 * does not use byte streams for the input example :-(. 00090 * If you byte swap each pair of input, keys and iv, the functions 00091 * would produce the output as the old version :-(. 00092 */ 00093 00094 /* NOTE - c is not incremented as per n2l */ 00095 #define n2ln(c,l1,l2,n) { \ 00096 c+=n; \ 00097 l1=l2=0; \ 00098 switch (n) { \ 00099 case 8: l2 =((unsigned long)(*(--(c)))) ; \ 00100 case 7: l2|=((unsigned long)(*(--(c))))<< 8; \ 00101 case 6: l2|=((unsigned long)(*(--(c))))<<16; \ 00102 case 5: l2|=((unsigned long)(*(--(c))))<<24; \ 00103 case 4: l1 =((unsigned long)(*(--(c)))) ; \ 00104 case 3: l1|=((unsigned long)(*(--(c))))<< 8; \ 00105 case 2: l1|=((unsigned long)(*(--(c))))<<16; \ 00106 case 1: l1|=((unsigned long)(*(--(c))))<<24; \ 00107 } \ 00108 } 00109 00110 /* NOTE - c is not incremented as per l2n */ 00111 #define l2nn(l1,l2,c,n) { \ 00112 c+=n; \ 00113 switch (n) { \ 00114 case 8: *(--(c))=(unsigned char)(((l2) )&0xff); \ 00115 case 7: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \ 00116 case 6: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \ 00117 case 5: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \ 00118 case 4: *(--(c))=(unsigned char)(((l1) )&0xff); \ 00119 case 3: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \ 00120 case 2: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \ 00121 case 1: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \ 00122 } \ 00123 } 00124 00125 #undef n2l 00126 #define n2l(c,l) (l =((unsigned long)(*((c)++)))<<24L, \ 00127 l|=((unsigned long)(*((c)++)))<<16L, \ 00128 l|=((unsigned long)(*((c)++)))<< 8L, \ 00129 l|=((unsigned long)(*((c)++)))) 00130 00131 #undef l2n 00132 #define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \ 00133 *((c)++)=(unsigned char)(((l)>>16L)&0xff), \ 00134 *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \ 00135 *((c)++)=(unsigned char)(((l) )&0xff)) 00136 00137 #undef s2n 00138 #define s2n(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \ 00139 *((c)++)=(unsigned char)(((l)>> 8L)&0xff)) 00140 00141 #undef n2s 00142 #define n2s(c,l) (l =((IDEA_INT)(*((c)++)))<< 8L, \ 00143 l|=((IDEA_INT)(*((c)++))) ) 00144 00145 #ifdef undef 00146 /* NOTE - c is not incremented as per c2l */ 00147 #define c2ln(c,l1,l2,n) { \ 00148 c+=n; \ 00149 l1=l2=0; \ 00150 switch (n) { \ 00151 case 8: l2 =((unsigned long)(*(--(c))))<<24; \ 00152 case 7: l2|=((unsigned long)(*(--(c))))<<16; \ 00153 case 6: l2|=((unsigned long)(*(--(c))))<< 8; \ 00154 case 5: l2|=((unsigned long)(*(--(c)))); \ 00155 case 4: l1 =((unsigned long)(*(--(c))))<<24; \ 00156 case 3: l1|=((unsigned long)(*(--(c))))<<16; \ 00157 case 2: l1|=((unsigned long)(*(--(c))))<< 8; \ 00158 case 1: l1|=((unsigned long)(*(--(c)))); \ 00159 } \ 00160 } 00161 00162 /* NOTE - c is not incremented as per l2c */ 00163 #define l2cn(l1,l2,c,n) { \ 00164 c+=n; \ 00165 switch (n) { \ 00166 case 8: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \ 00167 case 7: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \ 00168 case 6: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \ 00169 case 5: *(--(c))=(unsigned char)(((l2) )&0xff); \ 00170 case 4: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \ 00171 case 3: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \ 00172 case 2: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \ 00173 case 1: *(--(c))=(unsigned char)(((l1) )&0xff); \ 00174 } \ 00175 } 00176 00177 #undef c2s 00178 #define c2s(c,l) (l =((unsigned long)(*((c)++))) , \ 00179 l|=((unsigned long)(*((c)++)))<< 8L) 00180 00181 #undef s2c 00182 #define s2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \ 00183 *((c)++)=(unsigned char)(((l)>> 8L)&0xff)) 00184 00185 #undef c2l 00186 #define c2l(c,l) (l =((unsigned long)(*((c)++))) , \ 00187 l|=((unsigned long)(*((c)++)))<< 8L, \ 00188 l|=((unsigned long)(*((c)++)))<<16L, \ 00189 l|=((unsigned long)(*((c)++)))<<24L) 00190 00191 #undef l2c 00192 #define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \ 00193 *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \ 00194 *((c)++)=(unsigned char)(((l)>>16L)&0xff), \ 00195 *((c)++)=(unsigned char)(((l)>>24L)&0xff)) 00196 #endif 00197 00198 #define E_IDEA(num) \ 00199 x1&=0xffff; \ 00200 idea_mul(x1,x1,*p,ul); p++; \ 00201 x2+= *(p++); \ 00202 x3+= *(p++); \ 00203 x4&=0xffff; \ 00204 idea_mul(x4,x4,*p,ul); p++; \ 00205 t0=(x1^x3)&0xffff; \ 00206 idea_mul(t0,t0,*p,ul); p++; \ 00207 t1=(t0+(x2^x4))&0xffff; \ 00208 idea_mul(t1,t1,*p,ul); p++; \ 00209 t0+=t1; \ 00210 x1^=t1; \ 00211 x4^=t0; \ 00212 ul=x2^t0; /* do the swap to x3 */ \ 00213 x2=x3^t1; \ 00214 x3=ul; 00215