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.

if (klen inner[i] ^= key[i];<br />

ctx->outer[i] ^= key[i];<br />

}<br />

} else {<br />

DGST_Update(&(ctx->mdctx), key, klen);<br />

DGST_Final(dk, &(ctx->mdctx));<br />

DGST_Reset(&(ctx->mdctx));<br />

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

ctx->inner[i] ^= dk[i];<br />

ctx->outer[i] ^= dk[i];<br />

}<br />

}<br />

DGST_Update(&(ctx->mdctx), ctx->inner, DGST_BLK_SZ);<br />

}<br />

void SPC_HMAC_Reset(SPC_HMAC_CTX *ctx) {<br />

DGST_Reset(&(ctx->mdctx));<br />

DGST_Update(&(ctx->mdctx), ctx->inner, DGST_BLK_SZ);<br />

}<br />

void SPC_HMAC_Update(SPC_HMAC_CTX *ctx, unsigned char *m, size_t l) {<br />

DGST_Update(&(ctx->mdctx), m, l);<br />

}<br />

void SPC_HMAC_Final(unsigned char *tag, SPC_HMAC_CTX *ctx) {<br />

unsigned char is[DGST_OUT_SZ];<br />

DGST_Final(is, &(ctx->mdctx));<br />

DGST_Reset(&(ctx->mdctx));<br />

DGST_Update(&(ctx->mdctx), ctx->outer, DGST_BLK_SZ);<br />

DGST_Update(&(ctx->mdctx), is, DGST_OUT_SZ);<br />

DGST_Final(tag, &(ctx->mdctx));<br />

}<br />

void SPC_HMAC_Cleanup(SPC_HMAC_CTX *ctx) {<br />

volatile char *p = ctx->inner;<br />

volatile char *q = ctx->outer;<br />

int i;<br />

for (i = 0; i < DGST_BLK_SZ; i++) *p++ = *q++ = 0;<br />

}<br />

The previous code does require a particular interface to a hash function interface.<br />

First, it requires two constants: DGST_BLK_SZ, which is the internal block size of the<br />

underlying hash function (see Recipe 6.3), and DGST_OUT_SZ, which is the size of the<br />

resulting message digest. Second, it requires a context type for the message digest,<br />

which you should typedef to DGST_CTX. Finally, it requires an incremental interface to<br />

the hash function:<br />

void DGST_Init(DGST_CTX *ctx);<br />

void DGST_Reset(DGST_CTX *ctx);<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.<br />

Using HMAC | 279

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!