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.

a key, which may be of a different length than the block size. Sometimes an algorithm<br />

will allow variable-length keys, but the block size is generally fixed.<br />

Setting up a block cipher generally involves turning the raw key into a key schedule.<br />

Basically, the key schedule is just a set of keys derived from the original key in a<br />

cipher-dependent manner. You need to create the key schedule only once; it’s good<br />

for every use of the underlying key because raw encryption always gives the same<br />

result for any {key, input} pair (the same is true for decryption).<br />

Once you have a key schedule, you can generally pass it, along with an input block,<br />

into the cipher encryption function (or the decryption function) to get an output<br />

block.<br />

To keep the example code as simple as possible, we’ve written it assuming you are<br />

going to want to use one and only one cipher with it (though it’s not so difficult to<br />

make the code work with multiple ciphers).<br />

To get the code in this book working, you need to define several macros:<br />

SPC_BLOCK_SZ<br />

Denotes the block size of the cipher in bytes.<br />

SPC_KEY_SCHED<br />

This macro must be an alias for the key schedule type that goes along with your<br />

cipher. This value will be library-specific and can be implemented by typedef<br />

instead of through a macro. Note that the key schedule type should be an array<br />

of bytes of some fixed size, so that we can ask for the size of the key schedule<br />

using sizeof(SPC_KEY_SCHED).<br />

SPC_ENCRYPT_INIT(sched, key, keybytes) and<br />

SPC_DECRYPT_INIT(sched, key, keybytes)<br />

Both of these macros take a pointer to a key schedule to write into, the key used<br />

to derive that schedule, and the number of bytes in that key. If you are using an<br />

algorithm with fixed-size keys, you can ignore the third parameter. Note that<br />

once you’ve built a key schedule, you shouldn’t be able to tell the difference<br />

between different key lengths. In many implementations, initializing for encryption<br />

and initializing for decryption are the same operation.<br />

SPC_DO_ENCRYPT(sched, in, out) and SPC_DO_DECRYPT(sched, in, out)<br />

Both of these macros are expected to take a pointer to a key schedule and two<br />

pointers to memory corresponding to the input block and the output block.<br />

Both blocks are expected to be of size SPC_BLOCK_SZ.<br />

In the following sections, we’ll provide some bindings for these macros for Brian Gladman’s<br />

AES implementation and for the OpenSSLAPI. Unfortunately, we cannot use<br />

Microsoft’s CryptoAPI because it does not allow for exchanging symmetric encryption<br />

keys without encrypting them (see Recipes 5.26 and 5.27 to see how to work<br />

around this limitation)—and that would add significant complexity to what we’re trying<br />

to achieve with this recipe. In addition, AES is only available in the .NET frame-<br />

172 | Chapter 5: Symmetric Encryption<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!