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.

*out++ = ctx->ctbuf[i];<br />

if (ol) *ol = SPC_BLOCK_SZ - pad;<br />

spc_memset(ctx, 0, sizeof(SPC_CBC_CTX));<br />

return 1;<br />

}<br />

See Also<br />

• PKCS #11 web page: http://www.rsasecurity.com/rsalabs/pkcs/pkcs-11/<br />

• Recipes 4.9, 5.4, 5.5, 5.11, 5.16, 13.2<br />

5.7 Using a Generic CFB Mode Implementation<br />

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

You want a more high-level interface for CFB mode than your library provides. Alternatively,<br />

you want a portable CFB interface, or you have only a block cipher implementation<br />

and would like to use CFB mode.<br />

Solution<br />

CFB mode generates keystream by encrypting a “state” buffer, which starts out being<br />

the nonce and changes after each output, based on the actual outputted value.<br />

Many libraries provide a CFB implementation. If you need code that implements this<br />

mode, you will find it in the following “Discussion” section.<br />

Discussion<br />

You should probably use a higher-level abstraction, such as the one<br />

discussed in Recipe 5.16. Use a raw mode only when absolutely necessary,<br />

because there is a huge potential for introducing a security vulnerability<br />

by accident. If you still want to use CFB, be sure to use a<br />

message authentication code with it (see Chapter 6).<br />

CFB is a stream-based mode. Encryption occurs by XOR’ing the keystream bytes<br />

with the plaintext bytes, as shown in Figure 5-2. The keystream is generated one<br />

block at a time, and it is always dependent on the previous keystream block as well<br />

as the plaintext data XOR’d with the previous keystream block.<br />

CFB does this by keeping a “state” buffer, which is initially the nonce. As a block’s<br />

worth of data gets encrypted, the state buffer has some or all of its bits shifted out<br />

and ciphertext bits shifted in. The amount of data shifted in before each encryption<br />

operation is the “feedback size,” which is often the block size of the cipher, meaning<br />

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