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

0x3c2ab4b3 0x319ee9d5 0x623d7da8 0xf837889a 0xc7e61fd6

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: –1), \ t32 = (word32)x*t16 + x + t16 + 1, x = low16(t32), \ t16 = t32>>16, x = (x–t16) + (x<t16) ) #else /* !AVOID_JUMPS (default) */ #define MUL(x,y) \ ((t16 = (y)) ? \ (x=low16(x)) ? \ t32 = (word32)x*t16, \ x = low16(t32), \ t16 = t32>>16, \ x = (x–t16)+(x<t16) \ :\ (x = 1–t16) \ :\ (x = 1–x)) #endif #endif static void ideaCipher(byte *inbuf, byte *outbuf, word16 *key) { register uint16 x1, x2, x3, x4, s2, s3; word16 *in, *out; #ifndef SMALL_CACHE register uint16 t16; /* Temporaries needed by MUL macro */ register word32 t32; #endif int r = IDEAROUNDS; in = (word16 *)inbuf; x1 = *in++; x2 = *in++; x3 = *in++; x4 = *in; #ifndef HIGHFIRST x1 = (x1 >>8) | (x1<<8); x2 = (x2 >>8) | (x2<<8); x3 = (x3 >>8) | (x3<<8); x4 = (x4 >>8) | (x4<<8); #endif do { MUL(x1,*key++); x2 += *key++; x3 += *key++; MUL(x4, *key++); s3 = x3; x3 ^= x1; MUL(x3, *key++); s2 = x2; x2 ^= x4; x2 += x3; MUL(x2, *key++); x3 += x2; x1 ^= x2; x2 ^= s3; } while (––r); MUL(x1, *key++); x3 += *key++; x2 += *key++; MUL(x4, *key); x4 ^= x3; x3 ^= s2; out = (word16 *)outbuf; #ifdef HIGHFIRST *out++ = x1; *out++ = x3; *out++ = x2; *out = x4; #else /* !HIGHFIRST */ *out++ = (x1 >>8) | (x1<<8); *out++ = (x3 >>8) | (x3<<8); *out++ = (x2 >>8) | (x2<<8); *out = (x4 >>8) | (x4<<8); #endif } /* ideaCipher */ void idea_key(idea_ctx *c, unsigned char *key){ ideaExpandKey(key,c–>ek); ideaInvertKey(c–>ek,c–>dk); } void idea_enc(idea_ctx *c, unsigned char *data, int blocks){ int i; unsigned char *d = data; for(i=0;i<blocks;i++){ ideaCipher(d,d,c–>ek); d+=8; } } void idea_dec(idea_ctx *c, unsigned char *data, int blocks){ int i; unsigned char *d = data; for(i=0;i<blocks;i++){ ideaCipher(d,d,c–>dk); d+=8; } } #include <stdio.h> #ifndef #ifndef #define #endif #define #endif BLOCKS KBYTES KBYTES 1024 BLOCKS (64*KBYTES) int main(void) { /* Test driver for IDEA cipher */ int i, j, k; idea_ctx c; byte userkey[16]; word16 EK[IDEAKEYLEN], DK[IDEAKEYLEN]; byte XX[8], YY[8], ZZ[8]; word32 long_block[10]; /* 5 blocks */ long l; char *lbp; /* Make a sample user key for testing... */ for(i=0; i<16; i++) userkey[i] = i+1; idea_key(&ampc,userkey); /* Make a sample plaintext pattern for testing... */ for (k=0; k<8; k++) XX[k] = k; idea_enc(&ampc,XX,1); /* encrypt */ lbp = (unsigned char *) long_block; for(i=0;i<10;i++) long_block[i] = i;...
View Full Document

This note was uploaded on 10/18/2010 for the course MATH CS 301 taught by Professor Aliulger during the Fall '10 term at Koç University.

Ask a homework question - tutors are online