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.

if (!il--) return 1;<br />

ctx->nonce[ctx->ix] = *out++ = *in++ ^ ctx->nonce[ctx->ix++];<br />

ctx->ix %= SPC_BLOCK_SZ;<br />

}<br />

}<br />

if (!il) return 1;<br />

while (il >= SPC_BLOCK_SZ) {<br />

SPC_DO_ENCRYPT(&(ctx->ks), ctx->nonce, ctx->nonce);<br />

for (i = 0; i < SPC_BLOCK_SZ / sizeof(int); i++) {<br />

((int *)ctx->nonce)[i] = ((int *)out)[i] = ((int *)in)[i] ^<br />

((int *)ctx->nonce)[i];<br />

}<br />

il -= SPC_BLOCK_SZ;<br />

in += SPC_BLOCK_SZ;<br />

out += SPC_BLOCK_SZ;<br />

}<br />

SPC_DO_ENCRYPT(&(ctx->ks), ctx->nonce, ctx->nonce);<br />

for (i = 0; i nonce[ctx->ix] = *out++ = *in++ ^ ctx->nonce[ctx->ix++];<br />

return 1;<br />

}<br />

Decryption has a similar API, but a different implementation:<br />

int spc_cfb_decrypt_update(SPC_CFB_CTX *ctx, unsigned char *in, size_t il,<br />

unsigned char *out) {<br />

int i, x;<br />

char c;<br />

if (ctx->ix) {<br />

while (ctx->ix) {<br />

if (!il--) return 1;<br />

c = *in;<br />

*out++ = *in++ ^ ctx->nonce[ctx->ix];<br />

ctx->nonce[ctx->ix++] = c;<br />

ctx->ix %= SPC_BLOCK_SZ;<br />

}<br />

}<br />

if (!il) return 1;<br />

while (il >= SPC_BLOCK_SZ) {<br />

SPC_DO_ENCRYPT(&(ctx->ks), ctx->nonce, ctx->nonce);<br />

for (i = 0; i < SPC_BLOCK_SZ / sizeof(int); i++) {<br />

x = ((int *)in)[i];<br />

((int *)out)[i] = x ^ ((int *)ctx->nonce)[i];<br />

((int *)ctx->nonce)[i] = x;<br />

}<br />

il -= SPC_BLOCK_SZ;<br />

in += SPC_BLOCK_SZ;<br />

out += SPC_BLOCK_SZ;<br />

}<br />

SPC_DO_ENCRYPT(&(ctx->ks), ctx->nonce, ctx->nonce);<br />

for (i = 0; i < il; i++) {<br />

c = *in;<br />

Using a Generic CFB Mode Implementation | 191<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!