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.

}<br />

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

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

}<br />

See Also<br />

Recipes 4.10, 5.23, 6.7<br />

5.16 Using a High-Level, Error-Resistant<br />

Encryption and Decryption API<br />

<strong>Problem</strong><br />

You want to do encryption or decryption without the hassle of worrying about<br />

choosing an encryption algorithm, performing an integrity check, managing a nonce,<br />

and so on.<br />

Solution<br />

Use the following “Encryption Queue” implementation, which relies on the reference<br />

CWC mode implementation (discussed in Recipe 5.10) and the key derivation<br />

function from Recipe 4.11.<br />

Discussion<br />

Be sure to take into account the fact that functions in this API can fail,<br />

particularly the decryption functions. If a decryption function fails,<br />

you need to fail gracefully. In Recipe 9.12, we discuss many issues that<br />

help ensure robust network communication that we don’t cover here.<br />

This recipe provides an easy-to-use interface to symmetric encryption. The two ends<br />

of communication must set up cipher queues in exactly the same configuration.<br />

Thereafter, they can exchange messages easily until the queues are destroyed.<br />

This code relies on the reference CWC implementation discussed in Recipe 5.10. We<br />

use CWC mode because it gives us both encryption and integrity checking using a<br />

single key with a minimum of fuss.<br />

We add a new data type, SPC_CIPHERQ, which is responsible for keeping track of<br />

queue state. Here’s the declaration of the SPC_CIPHERQ data type:<br />

typedef struct {<br />

cwc_t ctx;<br />

unsigned char nonce[SPC_BLOCK_SZ];<br />

} SPC_CIPHERQ;<br />

Using a High-Level, Error-Resistant Encryption and Decryption API | 217<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!