161821Smarkm/* crypto/idea/idea_lcl.h */ 261821Smarkm/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 361821Smarkm * All rights reserved. 461821Smarkm * 561821Smarkm * This package is an SSL implementation written 661821Smarkm * by Eric Young (eay@cryptsoft.com). 761821Smarkm * The implementation was written so as to conform with Netscapes SSL. 8280304Sjkim * 961821Smarkm * This library is free for commercial and non-commercial use as long as 1061821Smarkm * the following conditions are aheared to. The following conditions 1161821Smarkm * apply to all code found in this distribution, be it the RC4, RSA, 1261821Smarkm * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1361821Smarkm * included with this distribution is covered by the same copyright terms 1461821Smarkm * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15280304Sjkim * 1661821Smarkm * Copyright remains Eric Young's, and as such any Copyright notices in 1761821Smarkm * the code are not to be removed. 1861821Smarkm * If this package is used in a product, Eric Young should be given attribution 1961821Smarkm * as the author of the parts of the library used. 2061821Smarkm * This can be in the form of a textual message at program startup or 2161821Smarkm * in documentation (online or textual) provided with the package. 22280304Sjkim * 2361821Smarkm * Redistribution and use in source and binary forms, with or without 2461821Smarkm * modification, are permitted provided that the following conditions 2561821Smarkm * are met: 2661821Smarkm * 1. Redistributions of source code must retain the copyright 2761821Smarkm * notice, this list of conditions and the following disclaimer. 2861821Smarkm * 2. Redistributions in binary form must reproduce the above copyright 2961821Smarkm * notice, this list of conditions and the following disclaimer in the 3061821Smarkm * documentation and/or other materials provided with the distribution. 3161821Smarkm * 3. All advertising materials mentioning features or use of this software 3261821Smarkm * must display the following acknowledgement: 3361821Smarkm * "This product includes cryptographic software written by 3461821Smarkm * Eric Young (eay@cryptsoft.com)" 3561821Smarkm * The word 'cryptographic' can be left out if the rouines from the library 3661821Smarkm * being used are not cryptographic related :-). 37280304Sjkim * 4. If you include any Windows specific code (or a derivative thereof) from 3861821Smarkm * the apps directory (application code) you must include an acknowledgement: 3961821Smarkm * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40280304Sjkim * 4161821Smarkm * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4261821Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4361821Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4461821Smarkm * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4561821Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4661821Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4761821Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4861821Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4961821Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5061821Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5161821Smarkm * SUCH DAMAGE. 52280304Sjkim * 5361821Smarkm * The licence and distribution terms for any publically available version or 5461821Smarkm * derivative of this code cannot be changed. i.e. this code cannot simply be 5561821Smarkm * copied and put under another distribution licence 5661821Smarkm * [including the GNU Public Licence.] 5761821Smarkm */ 5861821Smarkm 59280304Sjkim/* 60280304Sjkim * The new form of this macro (check if the a*b == 0) was suggested by Colin 61280304Sjkim * Plumb <colin@nyx10.cs.du.edu> 62280304Sjkim */ 6361821Smarkm/* Removal of the inner if from from Wei Dai 24/4/96 */ 6461821Smarkm#define idea_mul(r,a,b,ul) \ 6561821Smarkmul=(unsigned long)a*b; \ 6661821Smarkmif (ul != 0) \ 67280304Sjkim { \ 68280304Sjkim r=(ul&0xffff)-(ul>>16); \ 69280304Sjkim r-=((r)>>16); \ 70280304Sjkim } \ 7161821Smarkmelse \ 72280304Sjkim r=(-(int)a-b+1); /* assuming a or b is 0 and in range */ 7361821Smarkm 7461821Smarkm#ifdef undef 75280304Sjkim# define idea_mul(r,a,b,ul,sl) \ 7661821Smarkmif (a == 0) r=(0x10001-b)&0xffff; \ 7761821Smarkmelse if (b == 0) r=(0x10001-a)&0xffff; \ 78280304Sjkimelse { \ 79280304Sjkim ul=(unsigned long)a*b; \ 80280304Sjkim sl=(ul&0xffff)-(ul>>16); \ 81280304Sjkim if (sl <= 0) sl+=0x10001; \ 82280304Sjkim r=sl; \ 83280304Sjkim } 8461821Smarkm#endif 8561821Smarkm 86280304Sjkim/* 87280304Sjkim * 7/12/95 - Many thanks to Rhys Weatherley <rweather@us.oracle.com> for 88280304Sjkim * pointing out that I was assuming little endian byte order for all 89280304Sjkim * quantities what idea actually used bigendian. No where in the spec does 90280304Sjkim * it mention this, it is all in terms of 16 bit numbers and even the example 91280304Sjkim * does not use byte streams for the input example :-(. If you byte swap each 92280304Sjkim * pair of input, keys and iv, the functions would produce the output as the 93280304Sjkim * old version :-(. 9461821Smarkm */ 9561821Smarkm 9661821Smarkm/* NOTE - c is not incremented as per n2l */ 97280304Sjkim#define n2ln(c,l1,l2,n) { \ 98280304Sjkim c+=n; \ 99280304Sjkim l1=l2=0; \ 100280304Sjkim switch (n) { \ 101280304Sjkim case 8: l2 =((unsigned long)(*(--(c)))) ; \ 102280304Sjkim case 7: l2|=((unsigned long)(*(--(c))))<< 8; \ 103280304Sjkim case 6: l2|=((unsigned long)(*(--(c))))<<16; \ 104280304Sjkim case 5: l2|=((unsigned long)(*(--(c))))<<24; \ 105280304Sjkim case 4: l1 =((unsigned long)(*(--(c)))) ; \ 106280304Sjkim case 3: l1|=((unsigned long)(*(--(c))))<< 8; \ 107280304Sjkim case 2: l1|=((unsigned long)(*(--(c))))<<16; \ 108280304Sjkim case 1: l1|=((unsigned long)(*(--(c))))<<24; \ 109280304Sjkim } \ 110280304Sjkim } 11161821Smarkm 11261821Smarkm/* NOTE - c is not incremented as per l2n */ 113280304Sjkim#define l2nn(l1,l2,c,n) { \ 114280304Sjkim c+=n; \ 115280304Sjkim switch (n) { \ 116280304Sjkim case 8: *(--(c))=(unsigned char)(((l2) )&0xff); \ 117280304Sjkim case 7: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \ 118280304Sjkim case 6: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \ 119280304Sjkim case 5: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \ 120280304Sjkim case 4: *(--(c))=(unsigned char)(((l1) )&0xff); \ 121280304Sjkim case 3: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \ 122280304Sjkim case 2: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \ 123280304Sjkim case 1: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \ 124280304Sjkim } \ 125280304Sjkim } 12661821Smarkm 12761821Smarkm#undef n2l 12861821Smarkm#define n2l(c,l) (l =((unsigned long)(*((c)++)))<<24L, \ 12961821Smarkm l|=((unsigned long)(*((c)++)))<<16L, \ 13061821Smarkm l|=((unsigned long)(*((c)++)))<< 8L, \ 13161821Smarkm l|=((unsigned long)(*((c)++)))) 13261821Smarkm 13361821Smarkm#undef l2n 13461821Smarkm#define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \ 13561821Smarkm *((c)++)=(unsigned char)(((l)>>16L)&0xff), \ 13661821Smarkm *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \ 13761821Smarkm *((c)++)=(unsigned char)(((l) )&0xff)) 13861821Smarkm 13961821Smarkm#undef s2n 140280304Sjkim#define s2n(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \ 141280304Sjkim *((c)++)=(unsigned char)(((l)>> 8L)&0xff)) 14261821Smarkm 14361821Smarkm#undef n2s 144280304Sjkim#define n2s(c,l) (l =((IDEA_INT)(*((c)++)))<< 8L, \ 145280304Sjkim l|=((IDEA_INT)(*((c)++))) ) 14661821Smarkm 14761821Smarkm#ifdef undef 14861821Smarkm/* NOTE - c is not incremented as per c2l */ 149280304Sjkim# define c2ln(c,l1,l2,n) { \ 150280304Sjkim c+=n; \ 151280304Sjkim l1=l2=0; \ 152280304Sjkim switch (n) { \ 153280304Sjkim case 8: l2 =((unsigned long)(*(--(c))))<<24; \ 154280304Sjkim case 7: l2|=((unsigned long)(*(--(c))))<<16; \ 155280304Sjkim case 6: l2|=((unsigned long)(*(--(c))))<< 8; \ 156280304Sjkim case 5: l2|=((unsigned long)(*(--(c)))); \ 157280304Sjkim case 4: l1 =((unsigned long)(*(--(c))))<<24; \ 158280304Sjkim case 3: l1|=((unsigned long)(*(--(c))))<<16; \ 159280304Sjkim case 2: l1|=((unsigned long)(*(--(c))))<< 8; \ 160280304Sjkim case 1: l1|=((unsigned long)(*(--(c)))); \ 161280304Sjkim } \ 162280304Sjkim } 16361821Smarkm 16461821Smarkm/* NOTE - c is not incremented as per l2c */ 165280304Sjkim# define l2cn(l1,l2,c,n) { \ 166280304Sjkim c+=n; \ 167280304Sjkim switch (n) { \ 168280304Sjkim case 8: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \ 169280304Sjkim case 7: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \ 170280304Sjkim case 6: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \ 171280304Sjkim case 5: *(--(c))=(unsigned char)(((l2) )&0xff); \ 172280304Sjkim case 4: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \ 173280304Sjkim case 3: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \ 174280304Sjkim case 2: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \ 175280304Sjkim case 1: *(--(c))=(unsigned char)(((l1) )&0xff); \ 176280304Sjkim } \ 177280304Sjkim } 17861821Smarkm 179280304Sjkim# undef c2s 180280304Sjkim# define c2s(c,l) (l =((unsigned long)(*((c)++))) , \ 181280304Sjkim l|=((unsigned long)(*((c)++)))<< 8L) 18261821Smarkm 183280304Sjkim# undef s2c 184280304Sjkim# define s2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \ 185280304Sjkim *((c)++)=(unsigned char)(((l)>> 8L)&0xff)) 18661821Smarkm 187280304Sjkim# undef c2l 188280304Sjkim# define c2l(c,l) (l =((unsigned long)(*((c)++))) , \ 189280304Sjkim l|=((unsigned long)(*((c)++)))<< 8L, \ 190280304Sjkim l|=((unsigned long)(*((c)++)))<<16L, \ 191280304Sjkim l|=((unsigned long)(*((c)++)))<<24L) 19261821Smarkm 193280304Sjkim# undef l2c 194280304Sjkim# define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \ 195280304Sjkim *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \ 196280304Sjkim *((c)++)=(unsigned char)(((l)>>16L)&0xff), \ 197280304Sjkim *((c)++)=(unsigned char)(((l)>>24L)&0xff)) 19861821Smarkm#endif 19961821Smarkm 20061821Smarkm#define E_IDEA(num) \ 201280304Sjkim x1&=0xffff; \ 202280304Sjkim idea_mul(x1,x1,*p,ul); p++; \ 203280304Sjkim x2+= *(p++); \ 204280304Sjkim x3+= *(p++); \ 205280304Sjkim x4&=0xffff; \ 206280304Sjkim idea_mul(x4,x4,*p,ul); p++; \ 207280304Sjkim t0=(x1^x3)&0xffff; \ 208280304Sjkim idea_mul(t0,t0,*p,ul); p++; \ 209280304Sjkim t1=(t0+(x2^x4))&0xffff; \ 210280304Sjkim idea_mul(t1,t1,*p,ul); p++; \ 211280304Sjkim t0+=t1; \ 212280304Sjkim x1^=t1; \ 213280304Sjkim x4^=t0; \ 214280304Sjkim ul=x2^t0; /* do the swap to x3 */ \ 215280304Sjkim x2=x3^t1; \ 216280304Sjkim x3=ul; 217