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.

Our API allows for incremental processing of messages, which means that there is a<br />

context object. The type for our context object is named SPC_MDC2_CTX. As with other<br />

hash functions presented in this chapter, the incremental API has three operations:<br />

initialization, updating (where data is processed), and finalization (where the resulting<br />

hash is output).<br />

The initialization function has the following signature:<br />

void spc_mdc2_init(SPC_MDC2_CTX *c);<br />

All this function does is set internal state to the correct starting values.<br />

Processing data is actually done by the following updating function:<br />

void spc_mdc2_update(SPC_MDC2_CTX *c, unsigned char *t, size_t l);<br />

This function hashes l bytes located at memory address t into the context c.<br />

The result is obtained with the following finalization function:<br />

void spc_mdc2_final(SPC_MDC2_CTX *c, unsigned char *output);<br />

The output argument is always a pointer to a buffer that is twice the block size of the<br />

cipher being used. In the case of AES, the output buffer should be 32 bytes.<br />

Following is our implementation of MDC-2, which is intended for use with AES-128.<br />

Remember: if you want to use this for other AES key sizes or for ciphers where the<br />

key size is different from the block size, you will need to perform some sort of key<br />

expansion before calling SPC_ENCRYPT_INIT( ). Of course, you’ll also have to change<br />

that call to SPC_ENCRYPT_INIT( ) to pass in the desired key length.<br />

#include <br />

#include <br />

#ifndef WIN32<br />

#include <br />

#include <br />

#include <br />

#else<br />

#include <br />

#include <br />

#endif<br />

/* This implementation only works when the block size is equal to the key size */<br />

typedef struct {<br />

unsigned char h1[SPC_BLOCK_SZ];<br />

unsigned char h2[SPC_BLOCK_SZ];<br />

unsigned char bf[SPC_BLOCK_SZ];<br />

size_t ix;<br />

size_t tl;<br />

} SPC_MDC2_CTX;<br />

void spc_mdc2_init(SPC_MDC2_CTX *c) {<br />

memset(c->h1, 0x52, SPC_BLOCK_SZ);<br />

memset(c->h2, 0x25, SPC_BLOCK_SZ);<br />

296 | Chapter 6: Hashes and Message Authentication<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!