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.

long i;<br />

unsigned char c;<br />

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

do {<br />

c = (*keystream_byte)( );<br />

} while(c > s->maxvalid);<br />

*out++ = s->cset[(s->reverse[*in++] + c) % s->csetlen];<br />

}<br />

}<br />

The function setup_charset_map( ) must be called once to set up a table that maps<br />

ASCII values into an index of the valid subset of characters. The data type that stores<br />

the mapping data is ENCMAP. The other two arguments are charset, a list of all characters<br />

in the valid subset, and csetlen, which specifies the number of characters in that<br />

set.<br />

Once the character map is set up, you can call encrypt_within_charset( ) to encrypt<br />

or decrypt data, while staying within the specified character set. This function has<br />

the following arguments:<br />

s<br />

Pointer to the ENCMAP object.<br />

in<br />

Buffer containing the data to be encrypted or decrypted.<br />

inlen<br />

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

out<br />

Buffer into which the encrypted or decrypted data is placed.<br />

keystream_byte<br />

Pointer to a callback function that should return a single byte of cryptographically<br />

strong keystream.<br />

This code needs to know how to get more bytes of keystream on demand, because<br />

some bytes of keystream will be thrown away if they could potentially be leveraged<br />

in a statistical attack. Therefore, the amount of keystream necessary is theoretically<br />

unbounded (though in practice it should never be significantly more than twice the<br />

length of the input). As a result, we need to know how to invoke a function that gives<br />

us new keystream instead of just passing in a buffer of static keystream.<br />

It would be easy (and preferable) to extend this code example to use a cipher context<br />

object (keyed and in a streaming mode) as a parameter instead of the function<br />

pointer. Then you could get the next byte of keystream directly from the passed context<br />

object. If your crypto library does not allow you direct access to keystream,<br />

encrypting all zeros returns the original keystream.<br />

148 | Chapter 4: Symmetric Cryptography Fundamentals<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!