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.

#include <br />

#include <br />

#include <br />

#include /* for htonl */<br />

#ifdef WIN32<br />

typedef unsigned __int64 spc_uint64_t;<br />

#else<br />

typedef unsigned long long spc_uint64_t;<br />

#endif<br />

/* This value needs to be the output size of your pseudo-random function (PRF)! */<br />

#define PRF_OUT_LEN 20<br />

/* This is an implementation of the PKCS#5 PBKDF2 PRF using HMAC-SHA1. It<br />

* always gives 20-byte outputs.<br />

*/<br />

/* The first three functions are internal helper functions. */<br />

static void pkcs5_initial_prf(unsigned char *p, size_t plen, unsigned char *salt,<br />

size_t saltlen, size_t i, unsigned char *out,<br />

size_t *outlen) {<br />

size_t swapped_i;<br />

HMAC_CTX ctx;<br />

HMAC_CTX_init(&ctx);<br />

HMAC_Init(&ctx, p, plen, EVP_sha1( ));<br />

HMAC_Update(&ctx, salt, saltlen);<br />

swapped_i = htonl(i);<br />

HMAC_Update(&ctx, (unsigned char *)&swapped_i, 4);<br />

HMAC_Final(&ctx, out, (unsigned int *)outlen);<br />

}<br />

/* The PRF doesn't *really* change in subsequent calls, but above we handled the<br />

* concatenation of the salt and i within the function, instead of external to it,<br />

* because the implementation is easier that way.<br />

*/<br />

static void pkcs5_subsequent_prf(unsigned char *p, size_t plen, unsigned char *v,<br />

size_t vlen, unsigned char *o, size_t *olen) {<br />

HMAC_CTX ctx;<br />

HMAC_CTX_init(&ctx);<br />

HMAC_Init(&ctx, p, plen, EVP_sha1( ));<br />

HMAC_Update(&ctx, v, vlen);<br />

HMAC_Final(&ctx, o, (unsigned int *)olen);<br />

}<br />

static void pkcs5_F(unsigned char *p, size_t plen, unsigned char *salt,<br />

size_t saltlen, size_t ic, size_t bix, unsigned char *out) {<br />

size_t i = 1, j, outlen;<br />

unsigned char ulast[PRF_OUT_LEN];<br />

memset(out,0, PRF_OUT_LEN);<br />

pkcs5_initial_prf(p, plen, salt, saltlen, bix, ulast, &outlen);<br />

while (i++

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

Saved successfully!

Ooh no, something went wrong!