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.

*out++ = *in++ ^ ctx->nonce[ctx->ix];<br />

ctx->nonce[ctx->ix++] = c;<br />

}<br />

return 1;<br />

}<br />

To finalize either encryption or decryption, use spc_cfb_final( ), which never needs<br />

to output anything, because CFB is a streaming mode:<br />

int spc_cfb_final(SPC_CFB_CTX *ctx) {<br />

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

return 1;<br />

}<br />

See Also<br />

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

5.8 Using a Generic OFB Mode Implementation<br />

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

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

Alternatively, you want a portable OFB interface, or you have only a block cipher<br />

implementation and you would like to use OFB mode.<br />

Solution<br />

OFB mode encrypts by generating keystream, then combining the keystream with<br />

the plaintext via XOR. OFB generates keystream one block at a time. Each block of<br />

keystream is produced by encrypting the previous block of keystream, except for the<br />

first block, which is generated by encrypting the nonce.<br />

Many libraries provide an OFB implementation. If you need code implementing 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 OFB, be sure to use a<br />

message authentication code with it.<br />

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

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

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