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_HMAC_Reset(&ctx);<br />

SPC_HMAC_Update(&ctx, ctr, sizeof(ctr));<br />

SPC_HMAC_Final(out, &ctx);<br />

for (i = 0; i < len % MAC_OUT_SZ; i++) *out++ = *in++ ^ keystream[i];<br />

}<br />

return out;<br />

}<br />

Note that this code is not optimized; it works on individual characters to avoid<br />

potential endian-ness problems.<br />

See Also<br />

Recipes 6.4, 6.10<br />

6.21 Securely Authenticating a MAC (Thwarting<br />

Capture Replay Attacks)<br />

<strong>Problem</strong><br />

You are using a MAC, and you need to make sure that when you get a message, you<br />

properly validate the MAC.<br />

Solution<br />

If you’re using an ever-increasing nonce (which we strongly recommend), check to<br />

make sure that the nonce associated with the message is indeed larger than the last<br />

one. Then, of course, recalculate the MAC and check against the transmitted MAC.<br />

Discussion<br />

The following is an example of validating a MAC using the OMAC1 implementation<br />

in Recipe 6.11, along with AES-128. We nonce the MAC by using a 16-byte nonce as<br />

the first block of input, as discussed in Recipe 6.12. Note that we expect you to be<br />

MAC’ing the ciphertext, as discussed in Recipe 6.18.<br />

#include <br />

#include <br />

/* last_nonce must be a pointer to a NULL on first invocation. */<br />

int spc_omac1_validate(unsigned char *ct, size_t ctlen, unsigned char sent_nonce[16],<br />

unsigned char *sent_tag, unsigned char *k,<br />

unsigned char **last_nonce) {<br />

int i;<br />

SPC_OMAC_CTX c;<br />

unsigned char calc_tag[16]; /* Maximum tag size for OMAC. */<br />

Securely Authenticating a MAC (Thwarting Capture Replay Attacks) | 303<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!