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.

one block at a time, by encrypting the previous keystream block. * The first block is<br />

generated by encrypting the nonce.<br />

P 0<br />

IV<br />

E K<br />

KS 0<br />

C 0<br />

Figure 5-3. OFB mode<br />

P 1<br />

E K<br />

KS 1<br />

C 1<br />

P 2<br />

This mode shares many properties with counter mode (CTR), but CTR mode has<br />

additional benefits. OFB mode is therefore seeing less and less use these days. Of<br />

course, we recommend a higher-level mode than both of these modes, one that provides<br />

stronger security guarantees—for example, CWC or CCM mode.<br />

In Recipe 5.4, we discuss the advantages and drawbacks of OFB and compare it to<br />

other popular modes.<br />

Many libraries already come with an implementation of OFB mode for any ciphers<br />

they support. However, some don’t. For example, you may only get an implementation<br />

of the raw block cipher when you obtain reference code for a new cipher.<br />

In the following sections we present a reasonably optimized implementation of OFB<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.<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_ofb_encrypt(unsigned char *key, size_t kl, unsigned char *nonce,<br />

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

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

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

* As with CFB mode, the “feedback size” could conceivably be smaller than the block size, but such schemes<br />

aren’t secure.<br />

Using a Generic OFB Mode Implementation | 193<br />

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

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.<br />

E K<br />

KS 2<br />

C 2<br />

. . .<br />

= XOR

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

Saved successfully!

Ooh no, something went wrong!