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.

Because we assume the message is available up front, all of the information we need<br />

to operate on a message is passed into the function spc_pctr_setup( ), which requires<br />

a context object (here, the type is SPC_CTR2_CTX), the key, the key length in bytes, a<br />

nonce SPC_BLOCK_SZ - SPC_CTR_BYTES in length, the input buffer, the length of the<br />

message, and the output buffer. This function does not do any of the encryption and<br />

decryption, nor does it copy the input buffer anywhere.<br />

To process the first block, as well as every second block after that, call spc_pctr_do_<br />

odd( ), passing in a pointer to the context object. Nothing else is required because the<br />

input and output buffers used are the ones passed to the spc_pctr_setup( ) function.<br />

If you test, you’ll notice that the results are exactly the same as with the CTR mode<br />

implementation from Recipe 5.9.<br />

This code requires the preliminaries from Recipe 5.5, as well as the spc_memset( )<br />

function from Recipe 13.2.<br />

#include <br />

#include <br />

typedef struct {<br />

SPC_KEY_SCHED ks;<br />

size_t len;<br />

unsigned char ctr_odd[SPC_BLOCK_SZ];<br />

unsigned char ctr_even[SPC_BLOCK_SZ];<br />

unsigned char *inptr_odd;<br />

unsigned char *inptr_even;<br />

unsigned char *outptr_odd;<br />

unsigned char *outptr_even;<br />

} SPC_CTR2_CTX;<br />

static void pctr_increment(unsigned char *ctr) {<br />

unsigned char *x = ctr + SPC_CTR_BYTES;<br />

while (x-- != ctr) if (++(*x)) return;<br />

}<br />

void spc_pctr_setup(SPC_CTR2_CTX *ctx, unsigned char *key, size_t kl,<br />

unsigned char *nonce, unsigned char *in, size_t len,<br />

unsigned char *out) {<br />

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

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

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

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

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

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

pctr_increment(ctx->ctr_even);<br />

ctx->inptr_odd = in;<br />

ctx->inptr_even = in + SPC_BLOCK_SZ;<br />

ctx->outptr_odd = out;<br />

ctx->outptr_even = out + SPC_BLOCK_SZ;<br />

ctx->len = len;<br />

}<br />

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