applied cryptography - protocols, algorithms, and source code in c

0x83f44239 0xf6e96c9a 0xab5133a3 0x39af0176 0x3b8b5ebe

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: * if min if min macro not already defined */ ( (a)<(b) ? (a) : (b) ) ( (a)>(b) ? (a) : (b) ) macro not already defined */ #define IDEAKEYSIZE 16 #define IDEABLOCKSIZE 8 #define IDEAROUNDS 8 #define IDEAKEYLEN (6*IDEAROUNDS+4) typedef struct{ word16 ek[IDEAKEYLEN],dk[IDEAKEYLEN]; }idea_ctx; /* End includes for IDEA.C */ #ifdef IDEA32 /* Use >16–bit temporaries */ #define low16(x) ((x) & 0xFFFF) typedef unsigned int uint16;/* at LEAST 16 bits, maybe more */ #else #define low16(x) (x) /* this is only ever applied to uint16’s */ typedef word16 uint16; #endif #ifdef SMALL_CACHE static uint16 mul(register uint16 a, register uint16 b) { register word32 p; p = (word32)a * b; if (p) { b = low16(p); a = p>>16; return (b – a) + (b < a); } else if (a) { return 1–b; } else { return 1–a; } } /* mul */ #endif /* SMALL_CACHE */ static uint16 mulInv(uint16 x) { uint16 t0, t1; uint16 q, y; if (x <= 1) return x; /* 0 and 1 are self–inverse */ t1 = 0x10001L / x; /* Since x >= 2, this fits into 16 bits */ y = 0x10001L % x; if (y == 1) return low16(1–t1); t0 = 1; do { q = x / y; x = x % y; t0 += q * t1; if (x == 1) return t0; q = y / x; y = y % x; t1 += q * t0; } while (y != 1); return low16(1–t1); } /* mukInv */ static void ideaExpandKey(byte const *userkey, word16 *EK) { int i,j; for (j=0; j<8; j++) { EK[j] = (userkey[0]<<8) + userkey[1]; userkey += 2; } for (i=0; j < IDEAKEYLEN; j++) { i++; EK[i+7] = EK[i & 7] << 9 | EK[i+1 & 7] >> 7; EK += i & 8; i &= 7; } } /* ideaExpandKey */ static void ideaInvertKey(word16 const *EK, word16 DK[IDEAKEYLEN]) { int i; uint16 t1, t2, t3; word16 temp[IDEAKEYLEN]; word16 *p = temp + IDEAKEYLEN; t1 = mulInv(*EK++); t2 = –*EK++; t3 = –*EK++; *––p = mulInv(*EK++); *––p = t3; *–p = t2; *––p = t1; for (i = 0; i < IDEAROUNDS–1; i++) { t1 = *EK++; *––p = *EK++; *––p = t1; t1 = t2 = t3 = *––p *––p *––p *––p mulInv(*EK++); –*EK++; –*EK++; = mulInv(*EK++); = t2; = t3; = t1; } t1 = *EK++; *––p = *EK++; *––p = t1; t1 = t2 = t3 = *––p mulInv(*EK++); –*EK++; –*EK++; = mulInv(*EK++); *––p = t3; *––p = t2; *––p = t1; /* Copy and destroy temp copy */ memcpy(DK, temp, sizeof(temp)); for(i=0;i<IDEAKEYLEN;i++)temp[i]=0; } /* ideaInvertKey */ #ifdef SMALL_CACHE #define MUL(x,y) (x = mul(low16(x),y)) #else /* !SMALL_CACHE */ #ifdef AVOID_JUMPS #define MUL(x,y) (x = low16(x–1), t16 = low16((y)...
View Full Document

Ask a homework question - tutors are online