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.

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

MD5_Init(&ctx);<br />

if (i & 1) MD5_Update(&ctx, key, key_length);<br />

else MD5_Update(&ctx, hash, 16);<br />

if (i % 3) MD5_Update(&ctx, salt, salt_length);<br />

if (i % 7) MD5_Update(&ctx, key, key_length);<br />

if (i & 1) MD5_Update(&ctx, hash, 16);<br />

else MD5_Update(&ctx, key, key_length);<br />

MD5_Final(hash, &ctx);<br />

}<br />

}<br />

char *spc_md5_encrypt(const char *key, const char *salt) {<br />

char *base64_out, *base64_salt, *result, *salt_end, *tmp_string;<br />

size_t result_length, salt_length;<br />

unsigned char out[16], raw_salt[16];<br />

base64_out = base64_salt = result = 0;<br />

if (!salt) {<br />

salt_length = 8;<br />

spc_rand(raw_salt, sizeof(raw_salt));<br />

if (!(base64_salt = crypt64_encode(raw_salt))) goto done;<br />

if (!(tmp_string = (char *)realloc(base64_salt, salt_length + 1)))<br />

goto done;<br />

base64_salt = tmp_string;<br />

} else {<br />

if (strncmp(salt, "$1$", 3) != 0) goto done;<br />

if (!(salt_end = strchr(salt + 3, '$'))) goto done;<br />

salt_length = salt_end - (salt + 3);<br />

if (salt_length > 8) salt_length = 8; /* maximum salt is 8 bytes */<br />

if (!(base64_salt = (char *)malloc(salt_length + 1))) goto done;<br />

memcpy(base64_salt, salt + 3, salt_length);<br />

}<br />

base64_salt[salt_length] = 0;<br />

compute_hash(out, key, base64_salt, salt_length);<br />

if (!(base64_out = crypt64_encode(out))) goto done;<br />

result_length = strlen(base64_out) + strlen(base64_salt) + 5;<br />

if (!(result = (char *)malloc(result_length + 1))) goto done;<br />

sprintf(result, "$1$%s$%s", base64_salt, base64_out);<br />

done:<br />

/* cleanup */<br />

if (base64_salt) free(base64_salt);<br />

if (base64_out) free(base64_out);<br />

return result;<br />

}<br />

We have named the Windows version of spc_md5_encrypt( ) as SpcMD5Encrypt( ) to<br />

adhere to conventional Windows naming conventions. In addition, the implementation<br />

uses only Win32 API and CryptoAPI functions, rather than relying on the standard<br />

C runtime for string and memory handling.<br />

Performing Password-Based Authentication with MD5-MCF | 405<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!