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.

In CBC and ECB modes, the cipher cannot always encrypt all the plaintext you give<br />

it as that plaintext arrives, because it requires block-aligned data to operate. In the<br />

finalization phase, those algorithms add padding if appropriate, then yield the<br />

remaining output. Note that, because of the internal buffering that can happen in<br />

these modes, the output to any single call of EVP_EncryptUpdate( ) or EVP_<br />

EncryptFinal_ex( ) can be about a full block larger or smaller than the actual input. If<br />

you’re encrypting data into a single buffer, you can always avoid overflow if you<br />

make the output buffer an entire block bigger than the input buffer. Remember,<br />

however, that if padding is turned off (as described in Recipe 5.19), the library will<br />

be expecting block-aligned data, and the output will always be the same size as the<br />

input.<br />

In OFB and CFB modes, the call to EVP_EncryptUpdate( ) will always return the<br />

amount of data you passed in, and EVP_EncryptFinal_ex( ) will never return any data.<br />

This is because these modes are stream-based modes that don’t require aligned data<br />

to operate. Therefore, it is sufficient to call only EVP_EncryptUpdate( ), skipping finalization<br />

entirely. Nonetheless, you should always call the finalization function so that<br />

the library has the chance to do any internal cleanup that may be necessary. For<br />

example, if you’re using a cryptographic accelerator, the finalization call essentially<br />

gives the hardware license to free up resources for other operations.<br />

These functions all return 1 on success, and 0 on failure. EVP_EncryptFinal_ex( ) will<br />

fail if padding is turned off and the data is not block-aligned. EVP_DecryptFinal_ex( )<br />

will fail if the decrypted padding is not in the proper format. Additionally, any of<br />

these functions may fail if they are using hardware acceleration and the underlying<br />

hardware throws an error. Beyond those problems, they should not fail. Note again<br />

that when decrypting, this API has no way of determining whether the data<br />

decrypted properly. That is, the data may have been modified in transit; other means<br />

are necessary to ensure integrity (i.e., use a MAC, as we discuss throughout<br />

Chapter 6).<br />

Here’s an example function that, when given an already instantiated cipher context,<br />

encrypts an entire plaintext message 100 bytes at a time into a single heap-allocated<br />

buffer, which is returned at the end of the function. This example demonstrates how<br />

you can perform multiple encryption operations over time and keep encrypting into<br />

a single buffer. This code will work properly with any of the OpenSSL-supported<br />

cipher modes.<br />

#include <br />

#include <br />

/* The integer pointed to by rb receives the number of bytes in the output.<br />

* Note that the malloced buffer can be realloced right before the return.<br />

*/<br />

char *encrypt_example(EVP_CIPHER_CTX *ctx, char *data, int inl, int *rb) {<br />

int i, ol, tmp;<br />

char *ret;<br />

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