I saw a code snippet that generates an AES key by the following steps:
Generate a 16-byte random value array.
SecureRandom random = new SecureRandom(); byte[] key = new byte[16]; random.nextBytes(key);
apply the HKDF to the key to generate a new encryption key.
encrypt_key = KeyDerivation.hkdfSha256(Key,
/* inputSalt =*/ null,
hkdfInfoString.getBytes("UTF-8"),
16);
I am confused why we need two steps. The SecureRandom seems to give enough entropy for the key, right? Two questions:
- Can we use the
key
in 1) directly for an AES encryption? - What's the impact of the null salt in 2)? I am considering maybe an extra step 2) is to protect the key being leaked (just a guess). If it is, does the null salt invalidate the purpose? Because we can precompute the linkage between the input key material of HKDF and its output.
The HKDF claims the salt is optional although using a random salt does strengthen it. I am confused that when is the scenario that an HKDF is required (especially without salt). If we already have a key with enough entropy, why we need it? If we have a weak key without enough entropy, how does HKDF (without salt) help this situation? I am imagining that the attacker can precompute the mapping between the weak key to the generated key, right?