1
$\begingroup$

I am working on improving my grasp on applied cryptography. Following question is just for learning/understanding purposes..

Lets say I want to generate a 16 byte key that I want to use for some symmetric encryption (leaving aside the specific algorithm, IV etc.) which of the following two is better and for what reasons ?

openssl rand -hex 16

openssl enc -pbkdf2 -aes-128-cbc -k {pass phrase} -P

$\endgroup$

1 Answer 1

3
$\begingroup$

If you have access to a source of random data, just extract the random data directly.

If you're generating the key from a cryptographically-secure source, there is no reason to use a slow KDF like PBKDF2. The only reason to use a slow KDF is if the input material does not have sufficient entropy for your use-cases. If the key itself already has 128 bits, then there's never a reason to pass it through PBKDF2 (or bcrypt, or scrypt, or Argon2, or S2K...).

If you are deriving your key from a low-entropy passphrase, then a slow KDF is vital to slow down brute force attacks. However in that case, you'd be better off using a memory-hard KDF like Argon2 and not PBKDF2, as memory-hardness makes it more difficult to parallelize attacks.


To answer your exact question, you shouldn't be using the openssl command line utility at all. You should be extracting random data using whatever API your programming language provides, such as crypto.getRandomValues() in JavaScript, getrandom() in C, the /dev/urandom device, etc.

$\endgroup$
2
  • $\begingroup$ I think the main reason to use a key derivation function instead of using random number is when someone else needs to be able to derive the same key (and you can't easily give that random number to them). $\endgroup$ Commented Sep 9, 2023 at 16:23
  • $\begingroup$ In addition, sometimes, we want more than one key from Input Key Material, so we use KDFs to derive more key from IKMs. $\endgroup$
    – kelalaka
    Commented Sep 9, 2023 at 16:49

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.