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.

senting that mode. Note that OpenSSLprovides only a CTR mode implementation<br />

for AES. See Recipe 5.9 for more on CTR mode.<br />

3. Instantiate a cipher context (type EVP_CIPHER_CTX).<br />

4. Pass a pointer to the cipher context to EVP_CIPHER_CTX_init( ) to initialize memory<br />

properly.<br />

5. Choose an IV or nonce, if appropriate to the mode (all except ECB).<br />

6. Initialize the mode by calling EVP_EncryptInit_ex( ) or EVP_DecryptInit_ex( ), as<br />

appropriate:<br />

int EVP_EncryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, ENGINE<br />

*engine, unsigned char *key, unsigned char *ivornonce);<br />

int EVP_DecryptInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type, ENGINE<br />

*engine, unsigned char *key, unsigned char *ivornonce);<br />

7. If desired, perform any additional configuration the cipher may allow (see Recipe<br />

5.20).<br />

Discussion<br />

Use the raw OpenSSLAPI only when absolutely necessary because<br />

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

accident. For general-purpose use, we recommend a high-level<br />

abstraction, such as that discussed in Recipe 5.16.<br />

The OpenSSLEVP API is a reasonably high-level interface to a multitude of cryptographic<br />

primitives. It attempts to abstract out most algorithm dependencies, so that<br />

algorithms are easy to swap. *<br />

The EVP_EncryptInit_ex( ) and EVP_DecryptInit_ex( ) functions set up a cipher context<br />

object to be used for further operations. It takes four arguments that provide all<br />

the information necessary before encryption or decryption can begin. Both take the<br />

same arguments:<br />

ctx<br />

Pointer to an EVP_CIPHER_CTX object, which stores cipher state across calls.<br />

type<br />

Pointer to an EVP_CIPHER object, which represents the cipher configuration to use<br />

(see the later discussion).<br />

engine<br />

Pointer to an ENGINE object representing the actual implementation to use. For<br />

example, if you want to use hardware acceleration, you can pass in an ENGINE<br />

object that represents your cryptographic accelerator.<br />

* EVP stands for “envelope.”<br />

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