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.

void spc_ctr_init(SPC_CTR_CTX *ctx, unsigned char *key, size_t kl, unsigned char<br />

*nonce) {<br />

SPC_ENCRYPT_INIT(&(ctx->ks), key, kl);<br />

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

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

spc_memset(ctx->ctr + SPC_BLOCK_SZ - SPC_CTR_BYTES, 0, SPC_CTR_BYTES);<br />

ctx->ix = 0;<br />

}<br />

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

Now you can add data as you get it using the spc_ctr_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_ctr_final( ).<br />

You’re responsible for making sure the initialization, updating, and<br />

finalization calls do not happen out of order.<br />

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

int spc_ctr_update(CTR_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_CTR_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 by 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 />

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