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.

Plaintext<br />

block 1<br />

Plaintext<br />

block 2<br />

Plaintext<br />

block n<br />

Figure 5-1. CBC mode<br />

IV<br />

Cipher<br />

Cipher<br />

. . .<br />

Cipher<br />

Ciphertext<br />

block 1<br />

Ciphertext<br />

block 2<br />

Ciphertext<br />

block n-1<br />

Ciphertext<br />

block n<br />

port arbitrary-length messages without message expansion using a modified version<br />

of CBC mode known as ciphertext stealing or CTS mode. We do not discuss CTS<br />

mode in the book, but there is a recipe about it on this book’s web site.<br />

Here, we present a reasonably optimized implementation of CBC mode that builds<br />

upon the raw block cipher interface presented in Recipe 5.5. It also requires the spc_<br />

memset( ) function from Recipe 13.2.<br />

The high-level API<br />

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

message as input and returns a dynamically allocated result. This API only deals with<br />

padded messages. If you want to turn off cipher padding, you will need to use the<br />

incremental interface.<br />

unsigned char *spc_cbc_encrypt(unsigned char *key, size_t kl, unsigned char *iv,<br />

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

unsigned char *spc_cbc_decrypt(unsigned char *key, size_t kl, unsigned char *iv,<br />

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

Both functions pass out the number of bytes in the result by writing to the memory<br />

pointed to by the final argument. If decryption fails for some reason, spc_cbc_<br />

decrypt( ) will return 0. Such an error means that the input was not a multiple of the<br />

block size, or that the padding was wrong.<br />

Using a Generic CBC Mode Implementation | 177<br />

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

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

= XOR

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

Saved successfully!

Ooh no, something went wrong!