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.

communicate asynchronously (that is, without taking turns), there is the possibility<br />

for a race condition in which the SPC_CIPHERQ states on each side of the communication<br />

get out of sync, which will needlessly cause decryption operations to fail.<br />

If you need to perform asynchronous communication with an infrastructure like this,<br />

you could use two SPC_CIPHERQ instances, one where the client encrypts messages for<br />

the server to decrypt, and another where the server encrypts messages for the client<br />

to decrypt.<br />

The choice you need to make is whether each SPC_CIPHERQ object should be keyed<br />

separately or should share the same key. Sharing the same key is possible, as long as<br />

you ensure that the same {key, nonce} pair is never reused. The way to do this is to<br />

manage two sets of nonces that can never collide. Generally, you do this by setting<br />

the high bit of the nonce buffer to 1 in one context and 0 in another context.<br />

Here’s a function that takes an existing context that has been set up, but not otherwise<br />

used, and turns it into two contexts with the same key:<br />

void spc_cipherq_async_setup(SPC_CIPHERQ *q1, SPC_CIPHERQ *q2) {<br />

memcpy(q2, q1, sizeof(SPC_CIPHERQ));<br />

q1->nonce[0] &= 0x7f; /* The upper bit of q1's nonce is always 0. */<br />

q2->nonce[0] |= 0x80; /* The upper bit of q2's nonce is always 1. */<br />

}<br />

We show a similar trick in which we use only one abstraction in Recipe 9.12.<br />

See Also<br />

Recipes 4.11, 5.5, 5.10, 9.12, 11.2<br />

5.17 Performing Block Cipher Setup (for CBC,<br />

CFB, OFB, and ECB Modes) in OpenSSL<br />

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

You need to set up a cipher so that you can perform encryption and/or decryption<br />

operations in CBC, CFB, OFB, or ECB mode.<br />

Solution<br />

Here are the steps you need to perform for cipher setup in OpenSSL, using their<br />

high-level API:<br />

1. Make sure your code includes openssl/evp.h and links to libcrypto (-lcrypto).<br />

2. Decide which algorithm and mode you want to use, looking up the mode in<br />

Table 5-6 to determine which function instantiates an OpenSSLobject repre-<br />

Performing Block Cipher Setup (for CBC, CFB, OFB, and ECB Modes) in OpenSSL | 221<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!