0

I'm using a KDF (PBKDF2HMAC) to generate a Fernet key from a given password, but to do so I also need to generate and store a salt.

    import base64
    import os
    from cryptography.fernet import Fernet
    from cryptography.hazmat.backends imort default_backend
    from cryptography.hazmat.primitives import hashes
    from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
    password = \"$PASSWORD\"
    kdf = PBKDF2HMAC(
        algorithm=hashes.SHA256(),length=32,salt=\"$FERNET_SALT\",
        iterations=100000,backend=default_backend()
    )
    key = kdf.derive(password)
    key_encoded = base64.urlsafe_b64encode(key)
    print(key_encoded)

Due to technical constraints, it would be hugely convenient if I could generate that salt using AWS Secrets Manager, but it only generates strings with alphanumeric+punctuation.

So the question is if I could use these random strings as salt, even if they're coming from a much smaller sample space than a typical urandom would give. And if not, if I could somehow compress the given random string to achieve the necessary randomness.

2
  • 1
    "Due to technical constraints, it would be hugely convenient if I could generate that salt using AWS Secrets Manager," - why the effort? A salt does not need to be cryptographically safe, it just needs to be sufficiently random within a large enough space so that using precomputed hash tables is infeasible. Thus, using a (properly seeded) cheap pseudorandom function is sufficient. Commented Mar 21, 2020 at 23:07
  • @SteffenUllrich It's a distributed system that has to share the Fernet key, but there's no leader to generate it. I can either use a networked lock and have one of the machines create the salt and run the KDF, or I can have the salt generated by AWS and each machine can run the KDF independently. The latter approach would be easier IMO. Even better would be generating the Fernet key with AWS, but that's not easily done AFAIK.
    – villasv
    Commented Mar 21, 2020 at 23:15

1 Answer 1

3

it [only] generates strings with alphanumeric+punctuation.

This means that each character in the string can be 1 of 72 symbols (26 uppercase letters + 26 lowercase letters + 10 digits + 10 punctuations).

So, if you randomly generated a string 42 characters in length, where each character is one of these 72 symbols, this randomly generated string would have more entropy than 256 randomly generated bits:

72 ^ 42 = 1.01 e78
2 ^ 256 = 1.15 e77

But, as @Steffen Ulrich points out in the comments, salts do not need to have this much entropy.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .