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

0xaace1e7c 0xd9f385b9 0xee39d7ab 0xeae397b2 0x3a6efa74

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: idea_enc(&ampc,lbp,5); for(i=0;i<10;i+=2) printf(“Block %01d = %08lx %08lx.\n”, i/2,long_block[i],long_block[i+1]); idea_dec(&ampc,lbp,3); idea_dec(&ampc,lbp+24,2); for(i=0;i<10;i+=2) printf(“Block %01d = %08lx %08lx.\n”, i/2,long_block[i],long_block[i+1]); return 0; } /* main */ /* normal exit */ GOST typedef unsigned long u4; typedef unsigned char byte; typedef struct { u4 k[8]; /* Constant s–boxes –– set up in gost_init(). */ char k87[256],k65[256],k43[256],k21[256]; } gost_ctx; /* Note: void void void void void encrypt and decrypt expect full blocks––padding blocks is caller’s responsibility. All bulk encryption is done in ECB mode by these calls. Other modes may be added easily enough. gost_enc(gost_ctx *, u4 *, int); gost_dec(gost_ctx *, u4 *, int); gost_key(gost_ctx *, u4 *); gost_init(gost_ctx *); gost_destroy(gost_ctx *); */ #ifdef __alpha /* Any other 64–bit machines? */ typedef unsigned int word32; #else typedef unsigned long word32; #endif kboxinit(gost_ctx *c) { int i; byte k8[16] = {14, 12, byte k7[16] = {15, 13, byte k6[16] = {10, 7, byte k5[16] = { 7, 5, byte k4[16] = { 2, 15, byte k3[16] = {12, 4, byte k2[16] = { 4, 7, byte k1[16] = {13, 14, 4, 13, 5, 9, 1, 12, 0, 11, 13, 11, 12, 13, 1, 14, 11, 5, 2, 5, 8, 0, 9, 4, 14, 12, 4, 0, 10, 7, 2, 10, 8, 0, 1, 0, 14, 5, 14, 2, 3, 4, 1, 14, 15, 5, 14, 6, 4, 12, 2, 15, 11, 7 }; 8, 3, 10, 6, 6, 11, 3, 4, 9, 7, 2, 10 }; 6, 3, 15, 5, 1, 13, 12, 8 }; 0, 6, 9, 10, 1, 2, 8, 15 }; 7, 10, 11, 6, 8, 5, 3, 9 }; 9, 2, 6, 8, 0, 13, 3, 11 }; 15, 0, 8, 13, 3, 12, 9, 1 }; 6, 15, 11, 1, 10, 9, 3, 7 }; for (i = 0; i < 256; i++) { c–>k87[i] = k8[i >> 4] << 4 | k7[i & 15]; c–>k65[i] = k6[i >> 4] << 4 | k5[i & 15]; c–>k43[i] = k4[i >> 4] << 4 | k3[i & 15]; c–>k21[i] = k2[i >> 4] << 4 | k1[i & 15]; } } static word32 f(gost_ctx *c,word32 x) { x = c–>k87[x>>24 & 255] << 24 | c–>k65[x>>16 & 255] << 16 | c–>k43[x>> 8 & 255] << 8 | c–>k21[x & 255]; /* Rotate left 11 bits */ return x<<11 | x>>(32–11); } void gostcrypt(gost_ctx *c, word32 *d){ register word32 n1, n2; /* As named in the GOST */ n1 = d[0]; n2 = d[1]; /* Instead of swapping halves, swap names each round */ n2 ^= f(c,n1+c–>k[0]); n1 ^= f(c,n2+c–>k[1]); n2 ^= f(c,n1+c–>k[2]); n1 ^= f(c,n2+c–>k[3]); n2 ^= f(c,n1+c–>k[4])...
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