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.

Table 5-6. Cipher instantiation reference (continued)<br />

Key strength /<br />

actual size (if<br />

Cipher lookup<br />

Cipher<br />

different) Cipher mode Call for EVP_CIPHER object<br />

string<br />

IDEA 128 bits CFB EVP_idea_cfb( ) idea-cfb<br />

IDEA 128 bits OFB EVP_idea_ofb( ) idea-ofb<br />

RC2 128 bits ECB EVP_rc2_ecb( ) rc2-ecb<br />

RC2 128 bits CBC EVP_rc2_cbc( ) rc2-cbc<br />

RC2 128 bits CFB EVP_rc2_cfb( ) rc2-cfb<br />

RC2 128 bits OFB EVP_rc2_ofb( ) rc2-ofb<br />

RC4 40 bits n/a EVP_rc4_40( ) rc4-40<br />

RC4 128 bits n/a EVP_rc4( ) rc4<br />

RC5 128 bits ECB EVP_rc5_32_16_12_ecb( ) rc5-ecb<br />

RC5 128 bits CBC EVP_rc5_32_16_12_cbc( ) rc5-cbc<br />

RC5 128 bits CFB EVP_rc5_32_16_12_cfb( ) rc5-cfb<br />

RC5 128 bits OFB EVP_rc5_32_16_12_ofb( ) rc5-ofb<br />

a There are known plaintext attacks against DESX that reduce the effective strength to 60 bits, but these are generally considered infeasible.<br />

For stream-based modes (CFB and OFB), encryption and decryption are identical<br />

operations. Therefore, EVP_EncryptInit_ex( ) and EVP_DecryptInit_ex( ) are interchangeable<br />

in these cases.<br />

While RC4 can be set up using these instructions, you must be very<br />

careful to set it up securely. We discuss how to do so in Recipe 5.23.<br />

Here is an example of setting up an encryption context using 128-bit AES in CBC<br />

mode:<br />

#include <br />

#include <br />

/* key must be of size EVP_MAX_KEY_LENGTH.<br />

* iv must be of size EVP_MAX_IV_LENGTH.<br />

*/<br />

EVP_CIPHER_CTX *sample_setup(unsigned char *key, unsigned char *iv) {<br />

EVP_CIPHER_CTX *ctx;<br />

/* This uses the OpenSSL PRNG . See Recipe 11.9 */<br />

RAND_bytes(key, EVP_MAX_KEY_LENGTH);<br />

RAND_bytes(iv, EVP_MAX_IV_LENGTH);<br />

if (!(ctx = (EVP_CIPHER_CTX *)malloc(sizeof(EVP_CIPHER_CTX)))) return 0;<br />

EVP_CIPHER_CTX_init(ctx);<br />

EVP_EncryptInit_ex(ctx, EVP_aes_128_cbc( ), 0, key, iv);<br />

return ctx;<br />

}<br />

Performing Block Cipher Setup (for CBC, CFB, OFB, and ECB Modes) in OpenSSL | 225<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!