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.

To get the length of the initialization vector:<br />

int EVP_CIPHER_CTX_iv_length(EVP_CIPHER_CTX *ctx);<br />

To get the cipher mode being used:<br />

int EVP_CIPHER_CTX_mode(EVP_CIPHER_CTX *ctx);<br />

To see if automatic padding is disabled:<br />

int pad = (ctx->flags & EVP_CIPH_NO_PADDING);<br />

To see if we are encrypting or decrypting:<br />

int encr = (ctx->encrypt);<br />

To retrieve the original initialization vector:<br />

char *iv = (ctx->oiv);<br />

Discussion<br />

The EVP_CIPHER_CTX_cipher( ) function is actually implemented as a macro that<br />

returns an object of type EVP_CIPHER. The cipher itself can be queried, but interesting<br />

queries can also be made on the context object through appropriate macros.<br />

All functions returning lengths return them in bytes.<br />

The EVP_CIPHER_CTX_mode( ) function returns one of the following predefined values:<br />

EVP_CIPH_ECB_MODE<br />

EVP_CIPH_CBC_MODE<br />

EVP_CIPH_CFB_MODE<br />

EVP_CIPH_OFB_MODE<br />

5.22 Performing Low-Level Encryption and<br />

Decryption with OpenSSL<br />

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

You have set up your cipher and want to perform encryption and decryption.<br />

Solution<br />

Use the following suite of functions:<br />

int EVP_EncryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,<br />

unsigned char *in, int inl);<br />

int EVP_EncryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl);<br />

int EVP_DecryptUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl,<br />

unsigned char *in, int inl);<br />

int EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl);<br />

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