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.

unsigned char *HMAC(const EVP_MD *evp_md, const void *key, int key_len,<br />

const unsigned char *msg, int msglen, unsigned char *tag,<br />

unsigned int *tag_len);<br />

There is some variation in all-in-one APIs. Some are single-pass, like the OpenSSL<br />

API described in this section. Others have a separate initialization step and a context<br />

object, so that you do not need to specify the underlying cryptographic primitive and<br />

rekey every single time you want to use the MAC. That is, such interfaces automatically<br />

call functions for resetting, updating, and finalization for you.<br />

See Also<br />

Recipes 4.11, 6.10, 6.12, 6.18, 6.21<br />

6.10 Using HMAC<br />

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

You want to provide message authentication using HMAC.<br />

Solution<br />

If you are using OpenSSL, you can use the HMAC API:<br />

/* The incremental interface */<br />

void HMAC_Init(HMAC_CTX *ctx, const void *key, int len, const EVP_MD *md);<br />

void HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, int len);<br />

void HMAC_Final(HMAC_CTX *ctx, unsigned char *tag, unsigned int *tag_len);<br />

/* HMAC_cleanup erases the key material from memory. */<br />

void HMAC_cleanup(HMAC_CTX *ctx);<br />

/* The all-in-one interface. */<br />

unsigned char *HMAC(const EVP_MD *evp_md, const void *key, int key_len,<br />

const unsigned char *msg, int msglen, unsigned char *tag,<br />

unsigned int *tag_len);<br />

If you are using CryptoAPI, you can use the CryptCreateHash( ), CryptHashData( ),<br />

CryptGetHashParam( ), CryptSetHashParam( ), and CryptDestroyHash( ) functions:<br />

BOOL WINAPI CryptCreateHash(HCRYPTPROV hProv, ALG_ID Algid, HCRYPTKEY hKey,<br />

DWORD dwFlags, HCRYPTHASH *phHash);<br />

BOOL WINAPI CryptHashData(HCRYPTHASH hHash, BYTE *pbData, DWORD cbData,<br />

DWORD dwFlags);<br />

BOOL WINAPI CryptGetHashParam(HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData,<br />

DWORD *pcbData, DWORD dwFlags);<br />

BOOL WINAPI CryptSetHashParam(HCRYPTHASH hHash, DWORD dwParam, BYTE *pbData,<br />

DWORD dwFlags);<br />

BOOL WINAPI CryptDestroyHash(HCRYPTHASH hHash);<br />

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