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.

This implementation is only for the case where the feedback size is<br />

equal to the cipher block size. This is the most efficient mechanism<br />

and is no less secure than other feedback sizes, so we strongly recommend<br />

this approach.<br />

The high-level API<br />

This implementation has two APIs. The first is a high-level API, which takes a message<br />

as input and returns a dynamically allocated result.<br />

unsigned char *spc_cfb_encrypt(unsigned char *key, size_t kl, unsigned char *nonce,<br />

unsigned char *in, size_t il);<br />

unsigned char *spc_cfb_decrypt(unsigned char *key, size_t kl, unsigned char *nonce,<br />

unsigned char *in, size_t il)<br />

Both of the previous functions output the same number of bytes as were input,<br />

unless a memory allocation error occurs, in which case 0 is returned.<br />

These two functions erase the key from memory before exiting. You<br />

may want to have them erase the plaintext as well.<br />

Here’s the implementation of the interface:<br />

#include <br />

#include <br />

unsigned char *spc_cfb_encrypt(unsigned char *key, size_t kl, unsigned char *nonce,<br />

unsigned char *in, size_t il) {<br />

SPC_CFB_CTX ctx;<br />

unsigned char *out;<br />

if (!(out = (unsigned char *)malloc(il))) return 0;<br />

spc_cfb_init(&ctx, key, kl, nonce);<br />

spc_cfb_encrypt_update(&ctx, in, il, out);<br />

spc_cfb_final(&ctx);<br />

return out;<br />

}<br />

unsigned char *spc_cfb_decrypt(unsigned char *key, size_t kl, unsigned char *nonce,<br />

unsigned char *in, size_t il) {<br />

SPC_CFB_CTX ctx;<br />

unsigned char *out;<br />

if (!(out = (unsigned char *)malloc(il))) return 0;<br />

spc_cfb_init(&ctx, key, kl, nonce);<br />

spc_cfb_decrypt_update(&ctx, in, il, out);<br />

spc_cfb_final(&ctx);<br />

return out;<br />

}<br />

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