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.

This function never fails (save for a catastrophic error in encryption), and it returns<br />

the address of the output buffer.<br />

Here is an implementation of this generator API, which makes use of the block<br />

cipher interface we developed in Recipe 5.5:<br />

/* NOTE: This code should be augmented to reseed after each request<br />

/* for pseudo-random data, as discussed in Recipe 11.6<br />

/*<br />

#ifndef WIN32<br />

#include <br />

#include <br />

#else<br />

#include <br />

#endif<br />

/* if encryption operations fail, you passed in a bad key size or are using a<br />

* hardware API that failed. In that case, be sure to perform error checking.<br />

*/<br />

typedef struct {<br />

SPC_KEY_SCHED ks;<br />

unsigned char ctr[SPC_BLOCK_SZ];<br />

unsigned char lo[SPC_BLOCK_SZ]; /* Leftover block of output */<br />

int ix; /* index into lo */<br />

int kl; /* The length of key used to key the cipher */<br />

} SPC_BCPRNG_CTX;<br />

#ifndef WIN32<br />

static pthread_mutex_t spc_bcprng_mutex = PTHREAD_MUTEX_INITIALIZER;<br />

#define SPC_BCPRNG_LOCK( ) pthread_mutex_lock(&spc_bcprng_mutex);<br />

#define SPC_BCPRNG_UNLOCK( ) pthread_mutex_unlock(&spc_bcprng_mutex);<br />

#else<br />

static HANDLE hSpcBCPRNGMutex;<br />

#define SPC_BCPRNG_LOCK( ) WaitForSingleObject(hSpcBCPRNGMutex, INFINITE)<br />

#define SPC_BCPRNG_UNLOCK( ) ReleaseMutex(hSpcBCPRNGMutex)<br />

#endif<br />

static void spc_increment_counter(SPC_BCPRNG_CTX *prng) {<br />

int i = SPC_BLOCK_SZ;<br />

}<br />

while (i--)<br />

if (++prng->ctr[i]) return;<br />

void spc_bcprng_init(SPC_BCPRNG_CTX *prng, unsigned char *key, int kl,<br />

unsigned char *x, int xl) {<br />

int i = 0;<br />

SPC_BCPRNG_LOCK( );<br />

SPC_ENCRYPT_INIT(&(prng->ks), key, kl);<br />

memset(prng->ctr, 0, SPC_BLOCK_SZ);<br />

Using an Application-Level Generator | 585<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!