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.

While RC2, RC4, and RC5 support absurdly high key lengths, it really is overkill to<br />

use more than a 256-bit symmetric key. There is not likely to be any greater security,<br />

only less efficiency. Therefore, OpenSSL puts a hard limit of 256 bits on key sizes.<br />

When calling the OpenSSLcipher initialization functions, you can set to NULL any<br />

value you do not want to provide immediately. If the cipher requires data you have<br />

not yet provided, clearly encryption will not work properly.<br />

Therefore, we can choose a cipher using EVP_EncryptInit_ex( ) without specifying a<br />

key, then set the key size using EVP_CIPHER_CTX_set_key_length( ), which takes two<br />

arguments: the first is the context initialized by the call to EVP_EncryptInit_ex( ), and<br />

the second is the new key length in bytes.<br />

Finally, we can set the key by calling EVP_EncryptInit_ex( ) again, passing in the context<br />

and any new data, along with NULL for any parameters we’ve already set. For<br />

example, the following code would set up a 256-bit version of Blowfish in CBC<br />

mode:<br />

#include <br />

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

EVP_CIPHER_CTX *ctx;<br />

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

EVP_CIPHER_CTX_init(ctx);<br />

/* Uses 128-bit keys by default. We pass in NULLs for the parameters that we'll<br />

* fill in after properly setting the key length.<br />

*/<br />

EVP_EncryptInit_ex(ctx, EVP_bf_cbc( ), 0, 0, 0);<br />

EVP_CIPHER_CTX_set_key_length(ctx, 32);<br />

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

return ctx;<br />

}<br />

5.19 Disabling Cipher Padding in OpenSSL in CBC<br />

Mode<br />

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

You’re encrypting in CBC or ECB mode, and the length of your data to encrypt is<br />

always a multiple of the block size. You would like to avoid padding because it adds<br />

an extra, unnecessary block of output.<br />

Solution<br />

OpenSSL has a function that can turn padding on and off for a context object:<br />

int EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *ctx, int pad);<br />

Disabling Cipher Padding in OpenSSL in CBC Mode | 227<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!