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_CIPHERQ objects are initialized by calling spc_cipherq_setup( ), which requires<br />

the code from Recipe 5.5, as well as an implementation of the randomness API discussed<br />

in Recipe 11.2:<br />

#include <br />

#include <br />

#include <br />

#define MAX_KEY_LEN (32) /* 256 bits */<br />

size_t spc_cipherq_setup(SPC_CIPHERQ *q, unsigned char *basekey, size_t keylen,<br />

size_t keyuses) {<br />

unsigned char dk[MAX_KEY_LEN];<br />

unsigned char salt[5];<br />

spc_rand(salt, 5);<br />

spc_make_derived_key(basekey, keylen, salt, 5, 1, dk, keylen);<br />

if (!cwc_init(&(q->ctx), dk, keylen * 8)) return 0;<br />

memcpy(q->nonce, salt, 5);<br />

spc_memset(basekey, 0, keylen);<br />

return keyuses + 1;<br />

}<br />

The function has the following arguments:<br />

q<br />

SPC_CIPHERQ context object.<br />

basekey<br />

Shared key used by both ends of communication (the “base key” that will be<br />

used to derive session keys).<br />

keylen<br />

Length of the shared key in bytes, which must be 16, 24, or 32.<br />

keyuses<br />

Indicates how many times the current key has been used to initialize a SPC_<br />

CIPHERQ object. If you are going to reuse keys, it is important that this argument<br />

be used properly.<br />

On error, spc_cipherq_setup() returns 0. Otherwise, it returns the<br />

next value it would expect to receive for the keyuses argument. Be sure<br />

to save this value if you ever plan to reuse keys.<br />

Note also that basekey is erased upon successful initialization.<br />

Every time you initialize an SPC_CIPHERQ object, a key specifically for use with that<br />

queue instance is generated, using the basekey and the keyuses arguments. To derive<br />

the key, we use the key derivation function discussed in Recipe 4.11. Note that this is<br />

useful when two parties share a long-term key that they wish to keep reusing. However,<br />

if you exchange a session key at connection establishment (i.e., using one of the<br />

218 | 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!