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.

SPC_MPRNG_UNLOCK( );<br />

}<br />

unsigned char *spc_mprng_rand(SPC_MPRNG_CTX *prng, unsigned char *buf, size_t l) {<br />

unsigned char *p;<br />

SPC_MPRNG_LOCK( );<br />

for (p = buf; prng->ix && l; l--) {<br />

*p++ = prng->lo[prng->ix++];<br />

prng->ix %= MAC_OUT_SZ;<br />

}<br />

while (l >= MAC_OUT_SZ) {<br />

SPC_HMAC_Reset(&(prng->ctx));<br />

SPC_HMAC_Update(&(prng->ctx), prng->ctr, sizeof(prng->ctr));<br />

SPC_HMAC_Final(p, &(prng->ctx));<br />

spc_increment_mcounter(prng);<br />

p += MAC_OUT_SZ;<br />

l -= MAC_OUT_SZ;<br />

}<br />

if (l) {<br />

SPC_HMAC_Reset(&(prng->ctx));<br />

SPC_HMAC_Update(&(prng->ctx), prng->ctr, sizeof(prng->ctr));<br />

SPC_HMAC_Final(prng->lo, &(prng->ctx));<br />

spc_increment_mcounter(prng);<br />

prng->ix = l;<br />

while (l--) p[l] = prng->lo[l];<br />

}<br />

SPC_MPRNG_UNLOCK( );<br />

return buf;<br />

}<br />

This implementation has two publicly exported functions. The first initializes the<br />

generator:<br />

void spc_mprng_init(SPC_MPRNG_CTX *prng, unsigned char *seed, int l);<br />

This function has the following arguments:<br />

prng<br />

Context object used to hold the state for a MAC-based PRNG.<br />

seed<br />

Buffer containing data that should be filled with entropy (the seed). This data is<br />

used to key the MAC.<br />

l<br />

Length of the seed buffer in bytes.<br />

The second function actually produces random data:<br />

unsigned char *spc_mprng_rand(SPC_MPRNG_CTX *prng, unsigned char *buf, size_t l);<br />

This function has the following arguments:<br />

prng<br />

Context object used to hold the state for a MAC-based PRNG.<br />

590 | Chapter 11: Random Numbers<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!