0
$\begingroup$

For example, if I use Rijndael for encryption, I have to specify a key of a certain length. For user convenience, I want the user to be able to use a key of any length. Then, to get the key to the required length of the algorithm, I build the hash value, which I then use for encryption and decryption. Is the security of the ciphertext still guaranteed if I use SHA-256 for instance? Should I rather pad the user key and shorten long keys? What is the best way to get a key length of 256 bit?

$\endgroup$
5
  • $\begingroup$ What cryptographic library are you using? It should provide a Key Derivation Function (KDF) such as HKDF-SHA256, or if you're using a user password a Password Based Key Derivation Function such as Argon2id. SHA256 is not safe for this use on its own, but can be used in a KDF such as HKDF-SHA256. Also why Rijndael instead of AES? What mode of operation are you using, why not AES-GCM-SIV, AES-GCM, or ChaCha20-Poly1305? Encrypting with anything other than one of those 3 is suspicious and should be justified. $\endgroup$ Commented Dec 1, 2021 at 15:42
  • $\begingroup$ I use the Java cipher library with the algorithm AES in CBC mode (Rijndael). The security of the algorithm is sufficient for me for the time being, I was interested in the hash as a key. $\endgroup$
    – Luqus
    Commented Dec 1, 2021 at 16:17
  • 1
    $\begingroup$ There are numerous Java cipher libraries. That unauthenticated CBC mode seems easily available is a bad sign for the usability and security of whichever you picked. $\endgroup$ Commented Dec 1, 2021 at 17:15
  • $\begingroup$ I am using the standard javax.crypto.Cipher library which is most likely not malicious. $\endgroup$
    – Luqus
    Commented Dec 1, 2021 at 18:37
  • $\begingroup$ I'm not saying it's malicious, I'm saying it's badly designed. It makes insecure choices easily available (eg ECB & CBC modes are required, but secure modes like GCM and SIV are optional) and even includes thoroughly broken ciphers like RC2 and RC4. It's not that it's malicious, it's that it's designed to be easy to screw up catastrophically. Compare to something like libsodium that's designed to make it hard to screw up, while both could end up with the same security if you make good choices it's much harder to make bad choices with libsodium. $\endgroup$ Commented Dec 1, 2021 at 19:08

2 Answers 2

2
$\begingroup$

SHA-256 is not a Key Derivation Function (KDF), and should not be used as one.

SHA-256 can be part of a KDF, such as HKDF-SHA256 which uses HMAC-SHA256 which in turn uses SHA256. HKDF is subject to misuse which can break its security, be sure you're using it correctly (if you use it).

HKDF is ONLY SUITABLE FOR HIGH-ENTROPY INPUTS, like other keys or the result of an Elliptic Curve Diffie Hellman exchange. If you're deriving keys from passwords, you need a password-based key derivation function like Argon2id.

$\endgroup$
1
  • $\begingroup$ great blog link! (y) $\endgroup$
    – baro77
    Commented Dec 1, 2021 at 16:21
0
$\begingroup$

If you need the security given by 256 bit entropy password, hashing alone will not help you if starting key < 256 bit of entropy because hashes are fast functions, so testing original key from its reduced-size space or the hashed key is almost the same. A Key Derivation Function (aka a CPU and/or memory/storage hard functions) could mitigate the problem, but only from a computational point of view, not theoretical one (so it matters how long you hope to be helped by the KDF). Of course if original password > 256 bit and 256 bit are enough for you, SHA256 is ok, but I guess that's not a realistic user case ;)

$\endgroup$
2
  • $\begingroup$ You've confused KDFs and PBKDFs. KDFs are still fast, PBKDFs have tunable difficulty. $\endgroup$ Commented Dec 1, 2021 at 17:20
  • $\begingroup$ You are right , sorry to the OP and thank you for clarification! $\endgroup$
    – baro77
    Commented Dec 1, 2021 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.