159 points · by nikic · 14y ago · 84 comments
[1] bcrypt, the passphrase-handling part being eksblowfish, takes the user passphrase and turns it into the array of 18 32-bit (4 byte) subkey values blowfish needs for encryption. 18 * 4=72 so that's where 72 comes from. Increasing that limit requires pre-hashing the passphrase down to 72 bytes or less, or chaining multiple bcrypts using each 72-byte chunk of the passphrase. Either of which makes it no longer bcrypt. The problem with pre-hashing is that there is no standard; every implementation uses some arbitrary hashing algorithm. It also adds complexity to implementations.
I would prefer that every hash function, including one that's designated as a password hash, utilize every bit of input to generate the output. If password length is limited, shouldn't it be done earlier, and not silently?
I'm always happy to see PHP moving forwards like this. Baby steps towards better practices!
http://contest-2012.korelogic.com/stats.html
This is probably the premier password cracking contest in the world. I placed 7th overall and was first to crack most of the TrueCrypt volumes and I despise attempting to crack bcrypt hashes (most sane people do). You really have to try and crack them for yourself to fully appreciate how difficult and slow they are to attack.
Bcrypt isn't a defense against "rainbow tables"; the hash function bcrypt supplanted (PHK's MD5 crypt) was also not "rainbow-table-able".
// pre-5.5
$salt = substr(strtr(base64_encode(openssl_random_pseudo_bytes(16)), ['+' => '.']), 0, 22);
$hash = crypt($password, '$2a$10$'.$salt);
$match = crypt($password, $hash) == $hash;
// post-5.5
$hash = password_hash($password, PASSWORD_BCRYPT, ['cost' => 10]);
$match = password_verify($password, $hash);
Brilliant effort and should really help to ensure password security on the web.People often get the salt generation wrong, e.g. by just using a substring of `md5(mt_rand())`, which is obviously wrong (in several respects) :/
If the salt is allowed to be "less than secret" (by which I mean it can be stored in plain-text, not that it should be published on your website), then what does it matter if it's "pretty random" versus "cryptographically random"?
What's wrong with something like this:
$salt_length = 22;
$cost_factor = 10;
$lower = range('a', 'z');
$upper = range('A', 'Z');
$numeric = range('0', '9');
$special = array('/', '.');
$salt_chars = array_merge(
$lower,
$upper,
$numeric,
$special
);
$char_count = count($salt_chars);
$salt = '';
for ($i = 0; $i < $salt_length; $i++) {
$salt += $salt_chars[mt_rand(0, $char_count -1)];
}
$hash = crypt(
$password,
'$2a$' . $cost_factor . '$' . $salt
);But for this case, the salt generation is much better (assuming that `mt_rand` is a good enough source of entropy, which may or may not be the case).
The rest definitely applies though.
In short, you're using the wrong algorithm ($2y$ is the better one, the one you're using has a known bug). You're not checking for errors from `crypt()` prior to storing the hash. So you can wind up significantly messing up your database and potentially leaving it in a worse state than if you just used `md5($password)`... And the minor note about timing attacks...
Not to mention that you currently have an issue in your code (it needs to be .= for a string, not +=)...
Yep. Can't wait for GoogleGuy's php-doc changes to get in, then I could downvote suggestions like this: http://www.php.net/manual/en/function.hash.php#101987
What are the ways this is wrong? I know that mt_rand is not a cryptographically secure RNG, but what are the other ones?
(NB: I do not write my own auth/password code.)
It's great to see the PHP team thinking ahead, and I completely understand and agree with why they chose procedural over an object oriented approach to implementing the new functionality: ease of use.
Next step I hope is some kind of native support for web sockets, I'm tired of using third party libraries that don't implement a web socket server correctly. That would be an amazing feature.
The one thing that would help the PHP community immensely is nuking w3schools from orbit. It's like a museum of bad ideas that somehow is the first place people end up when looking to learn PHP.
There are two ways of constructing a software design: One way is to make it
so simple that there are obviously no deficiencies, and the other way is to
make it so complicated that there are no obvious deficiencies.
- C. A. R. Hoare $password = mysql_query("SELECT password FROM users
WHERE email = ".$_POST['email']." LIMIT 1");
To clarify this to the downvoters who chose not to comment:The finest password hashing algorithm ever committed to source control won't protect your other sensitive information (address, phone numbers, etc.) when SQL injection can make your entire database vulnerable to attack.
Your password could be ultra-secure, but it doesn't matter if you use that code without sanitizing inputs :)
I guess the reason he is being downvoted is; badly hashed passwords are not merely a newbie problem...
# One rot13
$ echo test | wm --rot13 --words stdin
grfg
# Double rot13
$ echo test | wm --rot13 --words stdin | wm --rot13 --words stdin
test
No, do not md5 as you will lose hash type info.
edit: To be clear I am assuming it IS a joke. I hope.. oh god I hope.
Additionally, the general drift hasn't been towards OOP within PHP. Some OOP libraries are being added, but the core remains largely procedural (and doesn't appear to be shifting significantly yet). It's taking the (very logical IMHO) route of "Does this make sense to be OOP". If the answer is yes, then it goes in as a class structure. If not, it goes in procedurally.
I did not feel it made sense to make this a class, and as such I did not...
i.e. Python's standard library provides objects where appropriate, but often they will just put functions directly into modules for simplicity.
Also, in case you didn't know, you can use bcrypt with PHP5.3 and the crypt() function. It's not as user friendly as this, but you can do it.
Here's the last paragraph (in case it's TLDR, or you don't want to click through):
> So in short (or not), I just felt that there's room for this API and things like PasswordLib to live side by side. And I will continue to maintain that project in the long run. But for the generic use-case, I felt that an OOP API was too much risk for not enough gain for a core implementation. With that said, if you can come up with a clean API, I'd be all ears and willing to consider implementing it. But for now, this is the better alternative IMHO...
http://www.openwall.com/phpass/
It does all the work for you, and will even fall back to other algorithms if you don't have bcrypt available.
* In the crypto community, "better" usually refers to how long an algorithm has been around, how well reviewed & used it is, and how bug-free it's been. Looking at all of these, PBKDF2 is clearly superior to bcrypt, which has had significant bugs, isn't as widely deployed, etc.
https://wiki.php.net/rfc/hash_pbkdf2 http://en.wikipedia.org/wiki/PBKDF2
There's presently no crypt(3) format specified for PBKDF2. So that means that I would need to invent one. That's not something I'm willing to do for a core language feature.
Additionally, pbkdf2 is actually slightly weaker than bcrypt (partially due to the higher memory requirements of the later, 32kb vs < 1kb).
So without a strong reason for including it, it wasn't included.
However, the API is designed to be extendable. When scrypt gets bindings to crypt(3), it'll be made available. If PBKDF2 gets bindings, it'll be made available. If a new and stronger algorithm is made, it'll be made available (but not default for quite some time).
But I personally am not willing to go out on a limb and create a new cryptographic specification for this project. And that's what it would have taken to put pbkdf2 into it. Hence, why it's not there...
Having said all that, who cares? Pick one or the other. I'd have exactly the same positive comment regarding the new PHP interface if they had used PBKDF2 with a sane cost. The only harmful decision you can make regarding password hashes is to wait to implement them in order to pick the "best" one.
bcrypt also weakens on long passphrases, since it only uses 55 chars of input.
I'm certainly not suggesting that people not use bcrypt, I'm just surprised that it's the only option.
That's not to knock PBKDF2 - I'm just pointing out that both have flaws, and that people should use the right tool for the job. The "KDF" in "PBKDF2" stands for Key Derivation Function; i.e., it's for deriving a(n encryption) key. bcrypt exists basically for the sole purpose of password storage. In practical terms, it's more obvious how to use bcrypt's work factor as computers speed up than PBKDF2's iteration count, although either is quite suitable for the job. It's also easier to rehash bcrypt-stored passwords when upping your work factor - the old version will still verify, just check for $WF$ not matching your current work factor. PBKDF2 requires a bit more logic since you have to rehash with a different iteration count and do a second comparison, which conceivably also opens you up to a timing attack unless you're very careful about how you do it.
To me, it's a matter of what a tool was designed for. Given that I have the right tool for the job, why use something that's designed for something else even though it works quite well?
PBKDF2 isn't necessarily better than bcrypt. But yes, it's a good choice.
This is a nice improvement.