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

H stdioh stdlibh timeh eg intel ifdef littleendian

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: –>loki_subkeys[i+1]); } ((Long *)b)[0] = R; ((Long *)b)[1] = L; #ifdef LITTLE_ENDIAN bswap(b); #endif } /* Y = swap(LR) */ /* swap bytes round if little–endian */ void deloki(loki_ctx *c, char *b) { register i; register Long L, R; #ifdef LITTLE_ENDIAN bswap(b); #endif L = ((Long *)b)[0]; R = ((Long *)b)[1]; for (i=ROUNDS; i>0; i–=2) { L ^= f(R, c–>loki_subkeys[i–1]); R ^= f(L, c–>loki_subkeys[i–2]); } ((Long *)b)[0] = R; ((Long *)b)[1] = L; } #define MASK12 0×0fff /* left & right data halves */ /* swap bytes round if little–endian */ /* LR = X XOR K */ /* subkeys in reverse order */ /* Y = LR XOR K */ /* 12 bit mask for expansion E */ static Long f(r, k) register Long r; /* Data value R(i–1) */ Long k; /* Key K(i) */ { Long a, b, c; /* 32 bit S–box output, & P output */ a = r ^ k; /* A = R(i–1) XOR K(i) */ /* want to use slow speed/small size version */ b = ((Long)s((a & MASK12)) ) | /* B = S(E(R(i–1))^K(i)) */ ((Long)s(((a >> 8) & MASK12)) << 8) | ((Long)s(((a >> 16) & MASK12)) << 16) | ((Long)s((((a >> 24) | (a << 8)) & MASK12)) << 24); perm32(&ampc, &ampb, P); return(c); } /* C = P(S( E(R(i–1)) XOR K(i))) */ /* f returns the result C */ static short s(i) register Long i; /* return S–box value for input i */ { register short r, c, v, t; short exp8(); /* exponentiation routine for GF(2^8) */ r = ((i>>8) & 0xc) | (i & 0x3); c = (i>>2) & 0xff; t = (c + ((r * 17) ^ 0xff)) & 0xff; v = exp8(t, sfn[r].exp, sfn[r].gen); return(v); } #define MSB 0x80000000L /* MSB of 32–bit word */ /* /* /* /* row value–top 2 & bottom 2 */ column value–middle 8 bits */ base value for Sfn */ Sfn[r] = t ^ exp mod gen */ perm32(out, in , perm) Long *out; /* Output 32–bit block to be permuted */ Long *in; /* Input 32–bit block after permutation */ char perm[32]; /* Permutation array */ { Long mask = MSB; /* mask used to set bit in output register int i, o, b; /* input bit no, output bit no, value */ register char *p = perm; /* ptr to permutation array */ *out = 0; for (o=0; o<32; o++) { i =(int)*p++; b = (*in >> i) & 01; if (b) *out |= mask; mask >>= 1; } } #define SIZE 256 /* 256 elements in GF(2^8) */ */ /* clear output block */ /* For each output bit position o */ /* get input bit permuted to output o */ /* value of input bit i */ /* If the input bit i is set */ /* OR in mask to output i */ /* Shift m...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online