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.

decrypt the encrypted data. To make the function one-way, crypt( ) encrypts the key<br />

with itself. *<br />

The DES algorithm requires a salt, which crypt( ) limits to 12 bits. It also prepends<br />

the salt to the resulting ciphertext, which is base64-encoded. DES is a weak block<br />

cipher to start, and the crypt( ) function traditionally limits passwords to a single<br />

block, which serves to further weaken its capabilities because the block size is 64<br />

bits, or 8 bytes.<br />

Because DES is a weak cipher and crypt( ) limits the plaintext to a single DES block,<br />

we strongly recommend against using crypt( ) in new authentication systems. You<br />

should use it only if you have a need to maintain compatibility with an older system<br />

that uses it.<br />

Encrypting a password with crypt( ) is a simple operation, but programmers often<br />

get it wrong. The most common mistake is to use the plaintext password as the salt,<br />

but recall that crypt( ) stores the salt as the first two bytes of its result. Because passwords<br />

are limited to eight bytes, using the plaintext password as the salt reveals at<br />

least a quarter of the password and makes dictionary attacks easier.<br />

The crypt( ) function has the following signature:<br />

char *crypt(const char *key, const char *salt);<br />

This function has the following arguments:<br />

key<br />

Password to encrypt.<br />

salt<br />

Buffer containing the salt to use. Remember that crypt( ) will use only 12 bits for<br />

the salt, so it will use only the first two bytes of this buffer; passing in a larger<br />

salt will have no effect. For maximum compatibility, the salt should contain only<br />

alphanumeric characters, a period, or a forward slash.<br />

The following function, spc_crypt_encrypt( ), will generate a suitable random salt<br />

and return the result from calling crypt( ) with the password and generated salt. The<br />

crypt( ) function returns a pointer to a statically allocated buffer, so you should not<br />

call crypt( ) more than once without using the results from earlier calls because the<br />

data returned from earlier calls will be overwritten.<br />

#include <br />

#include <br />

char *spc_crypt_encrypt(const char *password) {<br />

char salt[3];<br />

static char *choices = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"<br />

"0123456789./";<br />

* Some older versions encrypt a string of zeros instead.<br />

Performing Password-Based Authentication with crypt( ) | 401<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!