# Encrypt a file with AES using a secret key derived with Argon2

I would like to encrypt some sensitive files on my computer and smartphone so that they remain unreadable even if the computer is stolen.

So far as I'm concerned, AES is the best encryption algorithm, so I would go for it. For the AES, I also need a secret key (or cipher key). Therefore, I need a key derivation function, preferably a password-based one. Obviously, I do not want to store the derived secret key on the computer (or smartphone), otherwise, the encrypted text could be easily decrypted using the secret key.

I have read that Argon2 is considered the best choice for password-based key derivation these days.

Now, in the "argon2 cffi Documentation Release 19.1.0" (January 17, 2019) I have read that "Argon2i uses data-independent memory access, which is preferred for password hashing and password-based key derivation" (page 3). Then, on page 9 I read the very same words about Argon2id: "That makes it [Argon2id] the preferred type for password hashing and password-based key derivation".

Now my first question is: Which of the two types of Argon2 is the best for password-based key derivation?

Further, as per the same document, the argon2.PasswordHasher algorithm "always uses a random salt for hashing". This implies that every time I enter my password to decrypt the file, the hashing algorithm will produce different secret keys, so in no way can I reproduce the secret key to decrypt my file.

Of course, I can store the salt on the computer (or smartphone) but then two other questions arise:

How can I use Argon2 with a predefined salt?

Is there a point in using a salt that is freely accessible (if my data are stolen)?

So, can somebody shed light on how Argon2 can be used as a password-based key derivation function? And what would be the solution to my problem?

Which of the two types of Argon2 is the best for password-based key derivation?

As noted by Forest if you are unsure always choose Argon2id.

How can I use Argon2 with a predefined salt?

From a library for Argon2

    Usage:  ./argon2 [-h] salt [-i|-d|-id] [-t iterations] [-m memory] [-p parallelism] [-l hash length] [-e|-r] [-v (10|13)]
Parameters:
salt            The salt to use, at least 8 characters
-i              Use Argon2i (this is the default)
-d              Use Argon2d instead of Argon2i
-id             Use Argon2id instead of Argon2i
-t N            Sets the number of iterations to N (default = 3)
-m N            Sets the memory usage of 2^N KiB (default 12)
-p N            Sets parallelism to N threads (default 1)
-l N            Sets hash output length to N bytes (default 32)
-e              Output only encoded hash
-r              Output only the raw bytes of the hash
-v (10|13)      Argon2 version (defaults to the most recent version, currently 13)
-h              Print argon2 usage


The libraries handle this for you. You just provide a random salt and store it, if you want to get the same result again, use the same salt.

Is there a point in using a salt that is freely accessible (if my data are stolen)?

Normally, you can store these values - salt, iteration, memory usage, parallelization, output size - with your data. If you really want to keep them separately from the storage you can do it, but there is no need. It is designed to be secure even if they are known by an attacker. When servers are attacked, all of these parameters are also acquired by the attackers. Salt is used against rainbows tables. Remember Kerckhoffs's principles, only the key is secret.

can somebody shed light on how Argon2 can be used as a password-based key derivation function?

There is a Key Encryption Key (KEK) methodology that is common with disk encryption utilities. Let say a file encryption key (FEK) is randomly generated by a good random source. Then your strong password is used to derive the KEK by a KDF, here in your case Argon2id. $$\text{KEK} = Argon2id(salt, password)$$ Use your KEK to encrypt FEK and store it on your target ( your computer or phone). $$FEK^* = AES\hbox{-} Enc_{KEK}(FEK)$$

Now, you want to encrypt a file, use your strong password to generate the KEK

$$\text{KEK} = Argon2id(salt, password)$$

and use the KEK to decrypt your FEK from the storage.

$$FEK = AES\hbox{-} Dec_{KEK}(FEK^*)$$

Encrypt the file with an authenticated encryption mode like AES-GCM or ChaCha20-Poly1305 and store it on your computer or cell-phone.

$$\text{EnryptedFile, ATag} = AES\hbox{-} GCM\hbox{-}Enc_{FEK}(\text{file})$$ where ATag is the authentication tag.

You need to keep FEK only in system memory and purge it when the operations are finished.

The benefits of this approach;

• Thank you very much for your quick and comprehensive answer. Actually, the argon2 cffi Documentation Release 19.1.0 also contains low level functions, such as argon2.low_level.hash_secret, which have a salt parameter, too. However, there is a warning at the beginning of the chapter: "This is a “Hazardous Materials” module. You should ONLY use it if you’re 100% absolutely sure that you know what you’re doing because this module is full of land mines, dragons, and dinosaurs with laser guns." Since I am afraid of dragons, I thought that this module is not for me. :) – K. Gabor Oct 14 at 19:34