21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

To switch hash functions, replace the SHA1 calls as appropriate, and change the<br />

value of HASH_SZ to be the digest size of the hash function that you wish to use.<br />

The function spc_lion_encrypt( ) encrypts its first argument, putting the result into<br />

the memory pointed to by the second argument. The third argument specifies the<br />

size of the message, and the last argument is the key. Again, note that the input size<br />

must be larger than the hash function’s output size.<br />

The spc_lion_decrypt( ) function takes a similar argument set as spc_lion_encrypt( ),<br />

merely performing the inverse operation.<br />

#include <br />

#include <br />

#include <br />

#define HASH_SZ 20<br />

#define NUM_WORDS (HASH_SZ / sizeof(int))<br />

void spc_lion_encrypt(char *in, char *out, size_t blklen, char *key) {<br />

int i, tmp[NUM_WORDS];<br />

RC4_KEY k;<br />

/* Round 1: R = R ^ RC4(L ^ K1) */<br />

for (i = 0; i < NUM_WORDS; i++)<br />

tmp[i] = ((int *)in)[i] ^ ((int *)key)[i];<br />

RC4_set_key(&k, HASH_SZ, (char *)tmp);<br />

RC4(&k, blklen - HASH_SZ, in + HASH_SZ, out + HASH_SZ);<br />

/* Round 2: L = L ^ SHA1(R) */<br />

SHA1(out + HASH_SZ, blklen - HASH_SZ, out);<br />

for (i = 0; i < NUM_WORDS; i++)<br />

((int *)out)[i] ^= ((int *)in)[i];<br />

/* Round 3: R = R ^ RC4(L ^ K2) */<br />

for (i = 0; i < NUM_WORDS; i++)<br />

tmp[i] = ((int *)out)[i] ^ ((int *)key)[i + NUM_WORDS];<br />

RC4_set_key(&k, HASH_SZ, (char *)tmp);<br />

RC4(&k, blklen - HASH_SZ, out + HASH_SZ, out + HASH_SZ);<br />

}<br />

void spc_lion_decrypt(char *in, char *out, size_t blklen, char *key) {<br />

int i, tmp[NUM_WORDS];<br />

RC4_KEY k;<br />

for (i = 0; i < NUM_WORDS; i++)<br />

tmp[i] = ((int *)in)[i] ^ ((int *)key)[i + NUM_WORDS];<br />

RC4_set_key(&k, HASH_SZ, (char *)tmp);<br />

RC4(&k, blklen - HASH_SZ, in + HASH_SZ, out + HASH_SZ);<br />

SHA1(out + HASH_SZ, blklen - HASH_SZ, out);<br />

for (i = 0; i < NUM_WORDS; i++) {<br />

((int *)out)[i] ^= ((int *)in)[i];<br />

tmp[i] = ((int *)out)[i] ^ ((int *)key)[i];<br />

216 | Chapter 5: Symmetric Encryption<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!