21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

encrypting a plaintext block that includes a counter value. Given a single key, the<br />

counter value must be unique for every encryption.<br />

This mode has many benefits over the “standard” modes (e.g., ECB, CBC, CFB, and<br />

OFB). However, we recommend a higher-level mode, one that provides stronger<br />

security guarantees (i.e., message integrity detection), such as CWC or CCM modes.<br />

Most high-level modes use CTR mode as a component.<br />

In Recipe 5.4, we discuss the advantages and drawbacks of CTR mode and compare<br />

it to other popular modes.<br />

Like most other modes, CTR mode requires a nonce (often called an IV in this context).<br />

Most modes use the nonce as an input to encryption, and thus require something<br />

the same size as the algorithm’s block length. With CTR mode, the input to<br />

encryption is generally the concatenation of the nonce and a counter. The counter is<br />

usually at least 32 bits, depending on the maximum amount of data you might want<br />

to encrypt with a single {key, nonce} pair. We recommend using a good random<br />

value for the nonce.<br />

In the following sections we present a reasonably optimized implementation of CTR<br />

mode that builds upon the raw block cipher interface presented in Recipe 5.5. It also<br />

requires the spc_memset( ) function from Recipe 13.2. By default, we use a 6-byte<br />

counter, which leaves room for a nonce of SPC_BLOCK_SZ - 6 bytes. With AES and<br />

other ciphers with 128-bit blocks, this is sufficient space.<br />

CTR mode with 64-bit blocks is highly susceptible to birthday attacks<br />

unless you use a large random portion to the nonce, which limits the<br />

message you can send with a given key. In short, don’t use CTR mode<br />

with 64-bit block ciphers.<br />

The high-level API<br />

This implementation has two APIs. The first is a high-level API, which takes a message<br />

as input and returns a dynamically allocated result.<br />

unsigned char *spc_ctr_encrypt(unsigned char *key, size_t kl, unsigned char *nonce,<br />

unsigned char *in, size_t il);<br />

unsigned char *spc_ctr_decrypt(unsigned char *key, size_t kl, unsigned char *nonce,<br />

unsigned char *in, size_t il)<br />

Both of the previous functions output the same number of bytes as were input,<br />

unless a memory allocation error occurs, in which case 0 is returned. The decryption<br />

routine is exactly the same as the encryption routine, and it is implemented by<br />

macro.<br />

198 | 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!