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.

spc_memset(key,0, kl);<br />

memcpy(ctx->nonce, nonce, SPC_BLOCK_SZ);<br />

ctx->ix = 0;<br />

}<br />

Note again that we remove the key from memory during this operation.<br />

Never use the same nonce (often called an IV in this context) twice with a single key.<br />

Use a secure random value or a counter. See Recipe 4.9 for more information on<br />

nonces.<br />

Now we can add data as we get it using the spc_ofb_update( ) function. This function<br />

is particularly useful when a message arrives in pieces. You’ll get the same<br />

results as if it all arrived at once. When you want to finish encrypting or decrypting,<br />

call spc_ofb_final( ).<br />

You’re responsible for making sure the init, update, and final calls do<br />

not happen out of order.<br />

The function spc_ofb_update( ) has the following signature:<br />

int spc_ofb_update(OFB_CTX *ctx, unsigned char *in, size_t il, unsigned char *out);<br />

This function has the following arguments:<br />

ctx<br />

Pointer to the SPC_OFB_CTX object associated with the current message.<br />

in<br />

Pointer to a buffer containing the data to be encrypted or decrypted.<br />

il<br />

Number of bytes contained in the input buffer.<br />

out<br />

Pointer to the output buffer, which needs to be exactly as long as the input<br />

buffer.<br />

Our implementation of this function always returns 1, but a hardwarebased<br />

implementation might have an unexpected failure, so it’s important<br />

to check the return value!<br />

This API is in the spirit of PKCS #11, which provides a standard cryptographic interface<br />

to hardware. We do this so that the above functions can have the bulk of their<br />

implementations replaced with calls to PKCS #11–compliant hardware. PKCS #11<br />

APIs generally pass out data explicitly indicating the length of data outputted, while<br />

Using a Generic OFB Mode Implementation | 195<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!