orientdb icon indicating copy to clipboard operation
orientdb copied to clipboard

Introduction of the "salt" to improve password security

Open giastfader opened this issue 13 years ago • 29 comments

Hi, I think that the introduction of a salt field to improve the password security is almost a must. Many times we hear about stolen passwords (even the hashed ones) from big companies and because of these issues users' accounts are be compromised. OrientDB uses the SHA256 algorithm to hash and store passwords but this is not very secure because there are many dictionaries and lookup tables to crack this system. I think that a good way to improve password security is the introduction of a salt field in the OUser class.

Here the explanation of the "salt passwords" concepts: http://en.wikipedia.org/wiki/Salt_(cryptography)

Here some ideas and do/do not: http://crackstation.net/hashing-security.htm

giastfader avatar Dec 25 '12 11:12 giastfader

This is a very good point ! SHA256 is good for ciphering - but extrem big rainbow tables can crack within seconds cause most users doesn't use good passwords at all... There should be the possibility to create a salt per User / or per Database Instance... and the ability to "re-salt" with and "Re-Salt" ´field if the "salt" was compromized should be available also ....

bayoda avatar Dec 25 '12 11:12 bayoda

@giastfader your links are super clear. So the solution could be to store the salt along with the hashed password in the OUser class?

lvca avatar Dec 26 '12 11:12 lvca

Salt as well as iteration count can be stored safly with the secret. Salt is only to stop precomputing, iterationcount is for making computation per key slower (for an attacker worse than for an single legitimate calculation.)

Recommendations from 2000 for PBKDF2 were to use at least 64bit of securly random generated salt and over 1000 iterations of nested hash functions (prefferably a hmac alogithm in the core of it for adding the salt, to prevent lenght extension attacks).

Saltlength needs to be adapted with increase of diskspace available to attackers (since they would need to build rainbowtables per possible salt), iterationcount with increase in computationpower of cpu's and even more important with increase in gpu's performances. The performance of calculation we already reach gets often overlooked.

Overall, it doesn't make much sense for everone to implement password derivation for themselves. Instead you can make use of a common KDF (http://en.wikipedia.org/wiki/Key_derivation_function) like PBKDF2 or scrypt.

Luckily the use of those is pretty easy with the Java Crypto Extension. You can simply select one KDF with the included SecretKeyFactory and set algorithm, generated or read salt and iterationcount as parameters to a PBEKeySpec.

SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec keySpecification = new PBEKeySpec(mySecretPasswordAsCharArray, mySalt, iterationCount, keyLength);
SecretKey secureSecretKey = secretKeyFactory.generateSecret(keySpecification);
//secretKey.getEncoded() returns a byte[] with the derived key

make sure to use a SecureRandom for the salt-bytearray

Kind Regards, Tim

ttf avatar Dec 26 '12 13:12 ttf

Hi Tim, good this is already available in JDK 6+. What about the salt? Since it should be unique per generated password we could store along with the password in OUser class, right? What about IterationCount? Is it better to customize it per user or use a fixed number at code level?

lvca avatar Dec 26 '12 15:12 lvca

Hi, yes I think that the best place to store the salt is a field in OUser class. For the iterationcount I'm afraid that it will slow down some applications. My case: I have a RIA, each time a user ask for data it (the client) provides user/pass, my app connects to OrientDB using the provided credentials, executes the query and returns json data. Each time performs a login so OrientDB can validate the user and apply the record security level feature. If the login process will become slow due to iterationcount, the application will seem slower. Maybe the behaviour of the server as well as the "password security level" could be set using some new conf parameter.

giastfader avatar Dec 26 '12 16:12 giastfader

@lvca You can safly store the salt along the password in OUser. Iterationcount is something people might want to change per installation depending on the sensitivity of the credentials, and the hardware that they use. Storing it in OUser would allow for migration of those passwords.

@giastfader Slowing down the key derivation -- or at least be able to do so -- is the full intention behind it. The Process is called key-stretching. For User-Authentication of larger user bases current recommendations even go towards 100.000 iterations, which slows down a single calculation in a single javathread on my common pc to a couple of hundred milliseconds, instead of letting it calculate nearly a hundred thousand sha-256 per second. An Attacker gaining access to those credentials would even be able to test several hundred million combinations per second against the hash with a common graphics card gpu. http://security.stackexchange.com/questions/4687/are-salted-sha-256-512-hashes-still-safe-if-the-hashes-and-their-salts-are-expos Thus you want to make it as expensive for him, as your hardware (and application) can manage.

If the database only contains the database-connection users and no enduser credentials, the whole thing becomes suddenly less important, since salts and iterations protect the credentials, not the access. It's both to protect against this: http://money.cnn.com/2012/06/06/technology/linkedin-password-hack/index.htm

ttf avatar Dec 26 '12 20:12 ttf

Hi Tim, thanks for those points. I was thinking about providing an additional prefix to store in orientdb-server-config.xml file to add in the password hashing, so:

sha(server-unique-prefix) + sha(user's salt) + sha(password)

This is to avoid someone accesses to the OUser class and know the salt. WDYT?

Lvc@

lvca avatar Dec 27 '12 18:12 lvca

Hi Luca,

Adding an optional serverspecific additional salt won't hurt, although it won't be necessary either, since it doesn't really add so much to security as it adds to complexity (imho), especially when it comes to distributions or migrations.

sha(server-unique-prefix) + sha(user's salt) + sha(password)

But if you do, you should prefix it to the salt before supplying it to a well proven key derivation function, instead of deriving the key yourself (like sha-ing shas of those together, if i interpreted that correctly)

that is: iterated_sha(hmac_sha(server-unique-prefix + salt, password)), iterations)

hmac being specified as in https://en.wikipedia.org/wiki/HMAC (JCE will do that for you though)

Kind Regards, Tim

ttf avatar Dec 30 '12 13:12 ttf

As talked with Luca, I proposed that this could be implemented with the Dependency Injection of a IPasswordHandler:

https://github.com/nuvolabase/orientdb/issues/1471

FHeNuS avatar May 11 '13 08:05 FHeNuS

Hi Luca, The best practise is to create a unique salt for each password. http://stackoverflow.com/questions/184112/what-is-the-optimal-length-for-user-password-salt

thecyman avatar Jul 21 '14 20:07 thecyman

this is the same as #2242

phpnode avatar Jul 24 '14 10:07 phpnode

@giastfader Are you addressing also this issue?

lvca avatar Apr 26 '15 08:04 lvca

Hurray! We have salt in 2.2.0-SNAPSHOT. Soon I'll update the documentation. For now we support PBKDF2WithHmacSHA1 algorithm, where the password has this format:

{PBKDF2WithHmacSHA1}<hash>:<salt>:<iterations>

Where iterations is stored in security.userPasswordSaltIterations global configuration and it's 1000 by default.

Example:

{PBKDF2WithHmacSHA1}48E6F9BD7807A7AA18C67DD3810994B87B030B4E180D0D09:00397B36B4FFAD7467E3C34412D4E822AFB3D0B2B5CE8A99:1000

lvca avatar Aug 20 '15 15:08 lvca

I forgot to mention that database with previous SHA-256 can always be read, but if the password is changed, the new salt is used.

lvca avatar Aug 20 '15 15:08 lvca

I would suggest providing at least the bcrypt or (better) scrypt key derivation function if that's possible. PBKDF2 is quite old, and in the case of this algorithm this has lead to it becoming less and less effective for its original purposes, as reported by many sources. The main problem being its limited memory usage, which makes it worse and worse as time passes to resist parallel brute force attacks; this has been "fixed" and improved upon by bcrypt and scrypt.

Given that this will be a completely new feature I would say it's much better to go with a better, proved, more developed approach for password key derivation from the start if it isn't too much effort.

P.S. Anyways, regarding PBKDF2WithHmacSHA1, you don't mention the salt size use, which should be at least 128bits (or even 160bits, max for SHA-1) given the current year. Also, more importantly, the 1000 iterations value is way too low (even if it's just a default configurable value). 15 years ago (!) when the standard was published, that was the recommended number, nowadays, even for the (arguably better) version of PBKDF2 using SHA-256 instead of SHA-1, thus already slower, we are now in the range of the hundreds of thousands.

Please see: http://stackoverflow.com/questions/6054082/recommended-of-iterations-when-using-pbkdf2-sha256 http://security.stackexchange.com/questions/3959/recommended-of-iterations-when-using-pkbdf2-sha256 https://www.owasp.org/index.php/Password_Storage_Cheat_Sheet

In any case, I just wanted to thank you anyways for the progress, it's definitely something great in the right direction!

Shyiy avatar Aug 20 '15 21:08 Shyiy

@Shyiy Current salt size id 24 bytes = 192 bits. Last link you posted says:

Select:
- PBKDF2 [*4] when FIPS certification or enterprise support on many platforms is required;
- scrypt [*5] where resisting any/all hardware accelerated attacks is necessary but support isn’t.
- bcrypt where PBKDF2 or scrypt support is not available.

So seems PBKDF2 is the first choice.

lvca avatar Aug 20 '15 23:08 lvca

@Shyiy About iterations, what's in your opinion the number should I set as default?

lvca avatar Aug 20 '15 23:08 lvca

Perfect for the salt.

Regarding the iteration count, of course there isn't a fixed number and there's always a tradeoff, judging by some recent estimates and recomendations, in order not to impact too much performance by default I would say about 64'000.

Generally the community mentioned/agreed on this measure for 2012 and suggested doubling it every 2 years (although of course this can vary); the other alternative key derivation functions are also suggested instead of a continuous increase of iteration count for the same function, but I see your point about support and thus the PBKDF2.

Shyiy avatar Aug 20 '15 23:08 Shyiy

Ok, changing it to 64k ;-)

lvca avatar Aug 21 '15 07:08 lvca

Hey, 64k makes every login too slow... In order to authorize the login, OrientDB must execute 64 iterations per login to check if the hash is good. Anybody know any safe technique to avoid it? Like caching the password in RAM only?

lvca avatar Aug 21 '15 12:08 lvca