The basic idea is :
- Generate new/Read existing AES + signature keys
- Encrypt the data using the first key (AES 256-CBC)
- Sign the encrypted data using the second key with HMAC-SHA256
The generation of bytes for the keys is simply the result of
Random.get_random_bytes(KEY_SIZE + SIG_SIZE), so 64 bytes in my case.
Now, to improve the security, I would like to add a password protection to the key (if somebody find the keys file). My initial idea is to hash salt+password and XOR the result with the random bytes in the key-file.
If I use SHA256 (to be consistent), it means I output 32 bytes and should XOR twice (for each 32 bytes key) and I don't like that (not sure but sounds like a potential attack). Using a different salt for each key ?
Finally, is there a standard way to store the salt and keys ? I don't like the idea of inventing my own scheme (usually a bad idea in cryptography). Is there some good practices ?
Update: thanks to the answer of Polynomial, I got the following code :
from Crypto import Random from Crypto.Protocol.KDF import PBKDF2 def generate_new_keys(password, enc_key_size=32, sig_key_size=32): rand_bytes = Random.get_random_bytes(enc_key_size + sig_key_size) # save the salt with open('keys.salt','w') as f: f.write(rand_bytes.encode("base64").replace("\n","")) # derive the two keys using PBKDF2 enc_key = PBKDF2(password, rand_bytes[:enc_key_size], dkLen=enc_key_size) sig_key = PBKDF2(password, rand_bytes[enc_key_size:], dkLen=sig_key_size) return (ency_key, sig_key)