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.

the lifetime of the key; the counter ensures that the nonce is always unique. In such a<br />

nonce, the random part is said to be a “salt.” Generally, it’s good to have four or<br />

more bytes of salt in a nonce.<br />

If you decide to use only a random nonce, remember that the nonce needs to be<br />

changed after each message, and you lose the ability to prevent against capturereplay<br />

attacks.<br />

The random portion of a nonce can be generated using the techniques discussed in<br />

Chapter 11. Generally, you will have a fixed-size buffer into which you place the<br />

nonce, and you will then set the remaining bytes to zero, incrementing them after<br />

each message is sent. For example, if you have a 16-byte nonce with an 8-byte<br />

counter in the least significant bytes, you might use the following code:<br />

/* This assumes a 16-byte nonce where the last 8 bytes represent the counter! */<br />

void increment_nonce(unsigned char *nonce) {<br />

if (!++nonce[15]) if (!++nonce[14]) if (!++nonce[13]) if (!++nonce[12])<br />

if (!++nonce[11]) if (!++nonce[10]) if (!++nonce[9]) if (!++nonce[8]) {<br />

/* If you get here, you're out of nonces. This really shouldn't happen<br />

* with an 8-byte nonce, so often you'll see: if (!++nonce[9]) ++nonce[8];<br />

*/<br />

}<br />

}<br />

Note that the this code can be more efficient if we do a 32-bit increment, but then<br />

there are endian-ness issues that make portability more difficult.<br />

If sequential nonces are implemented correctly, they can help thwart<br />

capture relay attacks (see Recipe 6.1).<br />

Initialization vectors (IVs)<br />

The term initialization vector (IV) is the most widely used and abused of the three<br />

terms we’ve been discussing. IV and nonce are often used interchangeably. However,<br />

a careful definition does differentiate between these two concepts. For our purposes,<br />

an IV is a nonce with an additional requirement: it must be selected in a<br />

nonpredictable way. That is, the IV can’t be sequential; it must be random. One popular<br />

example in which a real IV is required for maximizing security is when using the<br />

CBC encryption mode (see Recipe 5.6).<br />

The big downside to an IV, as compared to a nonce, is that an IV does not afford<br />

protection against capture-replay attacks—unless you’re willing to remember every<br />

IV that has ever been used, which is not a good solution. To ensure protection<br />

against such attacks when using an IV, the higher-level protocol must have its own<br />

notion of sequence numbers that get checked in order.<br />

Using Salts, Nonces, and Initialization Vectors | 135<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!