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.

Solution<br />

Use the PBKDF2 method of converting passwords to symmetric keys. See Recipe 4.10<br />

for a more detailed discussion of PBKDF2.<br />

Discussion<br />

What we are doing here isn’t really encrypting a password. Actually,<br />

we are creating a password validator. We use the term encryption<br />

because it is in common use and is a more concise way to explain the<br />

process.<br />

The PBKDF2 algorithm provides a way to convert an arbitrary-sized password or<br />

passphrase into an arbitrary-sized key. This method fits perfectly with the need to<br />

store passwords in a way that does not allow recovery of the actual password. The<br />

PBKDF2 algorithm requires two extra pieces of information besides the password: an<br />

iteration count and a salt. The iteration count specifies how many times to run the<br />

underlying operation; this is a way to slow down the algorithm to thwart brute-force<br />

attacks. The salt provides the same function as the salt in MD5 or DES-based crypt( )<br />

implementations.<br />

Storing a password using this method is simple; store the result of the PBKDF2 operation,<br />

along with the iteration count and the salt. When verification of a password is<br />

required, retrieve the stored values and run the PBKDF2 using the supplied password,<br />

saved iteration count, and salt. Compare the output of this operation with the<br />

stored result, and if the two are equal, the password is correct; otherwise, the passwords<br />

do not match.<br />

The function spc_pbkdf2_encrypt( ) implements a crypt( )-like function that uses the<br />

PBKDF2 method that we’ve described, and it assumes the implementation found in<br />

Recipe 4.10. If it is successful (the only error that should ever occur is an out-ofmemory<br />

error), it will return a dynamically allocated buffer that contains the<br />

encrypted password in MCF, which encodes the salt and encrypted password in<br />

base64 as well as includes the iteration count.<br />

MCF delimits the information it encodes with dollar signs. The first field is a digit<br />

that identifies the algorithm represented, which also dictates what the other fields<br />

contain. As of this writing, only two algorithms are defined for MCF: 1 indicates<br />

MD5 (see Recipe 8.9), and 2 indicates Blowfish. We have chosen to use 10 for<br />

PBKDF2 so that it is unlikely that it will conflict with anything else.<br />

#include <br />

#include <br />

#include <br />

#include <br />

#include <br />

Performing Password-Based Authentication with PBKDF2 | 409<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!