Score:0

Bringing a key to 256 bit by a hash algorithm

us flag

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?

SAI Peregrinus avatar
si flag
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.
Luqus avatar
us flag
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.
SAI Peregrinus avatar
si flag
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.
Luqus avatar
us flag
I am using the standard [javax.crypto.Cipher library](https://docs.oracle.com/javase/7/docs/api/javax/crypto/Cipher.html) which is most likely not malicious.
SAI Peregrinus avatar
si flag
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.
Score:2
si flag

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.

baro77 avatar
gd flag
great blog link! (y)
Score:0
gd flag

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 ;)

SAI Peregrinus avatar
si flag
You've confused KDFs and PBKDFs. KDFs are still fast, PBKDFs have tunable difficulty.
baro77 avatar
gd flag
You are right , sorry to the OP and thank you for clarification!
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.