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.

techniques from Chapter 8), the key derivation step is unnecessary, because reusing<br />

{key, nonce} pairs is already incredibly unlikely in such a situation.<br />

Both communicating parties must initialize their queue with identical parameters.<br />

When you’re done with a queue, you should deallocate internally allocated memory<br />

by calling spc_cipherq_cleanup( ):<br />

void spc_cipherq_cleanup(SPC_CIPHERQ *q) {<br />

spc_memset(q, 0, sizeof(SPC_CIPHERQ));<br />

}<br />

Here are implementations of the encryption and decryption operations (including a<br />

helper function), both of which return a newly allocated buffer containing the results<br />

of the appropriate operation:<br />

static void increment_counter(SPC_CIPHERQ *q) {<br />

if (!++q->nonce[10]) if (!++q->nonce[9]) if (!++q->nonce[8]) if (!++q->nonce[7])<br />

if (!++q->nonce[6]) ++q->nonce[5];<br />

}<br />

unsigned char *spc_cipherq_encrypt(SPC_CIPHERQ *q, unsigned char *m, size_t mlen,<br />

size_t *ol) {<br />

unsigned char *ret;<br />

if (!(ret = (unsigned char *)malloc(mlen + 16))) {<br />

if (ol) *ol = 0;<br />

return 0;<br />

}<br />

cwc_encrypt(&(q->ctx), 0, 0, m, mlen, q->nonce, ret);<br />

increment_counter(q);<br />

if (ol) *ol = mlen + 16;<br />

return ret;<br />

}<br />

unsigned char *spc_cipherq_decrypt(SPC_CIPHERQ *q, unsigned char *m, size_t mlen,<br />

size_t *ol) {<br />

unsigned char *ret;<br />

if (!(ret = (unsigned char *)malloc(mlen - 16))) {<br />

if (ol) *ol = 0;<br />

return 0;<br />

}<br />

if (!cwc_decrypt(&(q->ctx), 0, 0, m, mlen, q->nonce, ret)) {<br />

free(ret);<br />

if (ol) *ol = 0;<br />

return 0;<br />

}<br />

increment_counter(q);<br />

if (ol) *ol = mlen - 16;<br />

return ret;<br />

}<br />

Using a High-Level, Error-Resistant Encryption and Decryption API | 219<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!