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.

EVP_DigestFinal(&ctx, result, &ol);<br />

printf("SHA1(\"%s%s\") = ", s1, s2);<br />

for (i = 0; i < ol; i++) printf("%02x", result[i]);<br />

printf("\n");<br />

free(result);<br />

return 0;<br />

}<br />

The OpenSSLlibrary supports only two cryptographic hash functions that we recommend,<br />

SHA1 and RIPEMD-160. It also supports MD2, MD4, MD5, and MDC-2-<br />

DES. MDC-2-DES is reasonable, but it is slow and provides only 64 bits of resistance<br />

to birthday attacks, whereas we recommend a minimum baseline of 80 bits of<br />

security. As an alternative, you could initialize the hash function with a nonce, as discussed<br />

in Recipe 6.8.<br />

Nonetheless, Table 6-3 contains a summary of the necessary information on each<br />

hash function to use both the EVP and hash-specific APIs with OpenSSL.<br />

Table 6-3. OpenSSL-supported hash functions<br />

Message<br />

digest function<br />

EVP function to<br />

specify MD<br />

Context type for<br />

MD-specific API<br />

Of course, you may want to use an off-the-shelf hash function that isn’t supported by<br />

either OpenSSLor CryptoAPI—for example, SHA-256, SHA-384, or SHA-512. Aaron<br />

Gifford has produced a good, free library with implementations of these functions and<br />

released it under a BSD-style license. It is available from http://www.aarongifford.com/<br />

computers/sha.html.<br />

That library exports an API that should look very familiar:<br />

SHA256_Init(SHA256_CTX *ctx);<br />

SHA256_Update(SHA256_CTX *ctx, unsigned char *data, size_t inlen);<br />

SHA256_Final(unsigned char out[SHA256_DIGEST_LENGTH], SHA256_CTX *ctx);<br />

SHA384_Init(SHA384_CTX *ctx);<br />

SHA384_Update(SHA384_CTX *ctx, unsigned char *data, size_t inlen);<br />

SHA384_Final(unsigned char out[SHA384_DIGEST_LENGTH], SHA384_CTX *ctx);<br />

SHA512_Init(SHA512_CTX *ctx);<br />

SHA512_Update(SHA512_CTX *ctx, unsigned char *data, size_t inlen);<br />

SHA512_Final(unsigned char out[SHA512_DIGEST_LENGTH], SHA512_CTX *ctx);<br />

All of the previous functions are prototyped in the sha2.h header file.<br />

266 | Chapter 6: Hashes and Message Authentication<br />

Prefix for MD-specific API<br />

calls (i.e., XXX_Init, …)<br />

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

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

Include file for MDspecific<br />

API<br />

MD2 EVP_md2() MD2_CTX MD2 openssl/md2.h<br />

MD4 EVP_md4() MD4_CTX MD4 openssl/md4.h<br />

MD5 EVP_md5() MD5_CTX MD5 openssl/md5.h<br />

MDC-2-DES EVP_mdc2() MDC2_CTX MDC2 openssl/mdc2.h<br />

RIPEMD-160 EVP_ripemd160() RIPEMD160_CTX RIPEMD160 openssl/ripemd.h<br />

SHA1 EVP_sha1() SHA_CTX SHA1 openssl/sha.h

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

Saved successfully!

Ooh no, something went wrong!