How to store encryption key?

1.7k views Asked by At

I look out many password managers like keeper, 1password, secret-in and I am following secret-in password manager to create my own project and trying to add same features, but got stuck at storing the data of users like his/her secrets, payment secrets in encrypted form. I read encryption model of keeper here but still didn't understand. Where to store a server side encryption key? I have some data that is symmetrically encrypted with a single key in my database. Rather than hard coding it into my code, I am looking for a safer way to store the encryption key. Where can I safely store it?

1

There are 1 answers

4
Woodstock On BEST ANSWER

The approach here is quite simple.

You only send encrypted data to the server for storage/backup. The encrypted data received doesn't come with a key.

You need to ensure all encryption and decryption occurs locally on the users device. Thus the user needs to supply the key.

Users aren't good at providing high quality key material, so instead, require the user to provide a password, take that password and pass it through a hash-based key derivation function with parameters that make the function slow (high ops, high mem requirements). An algorithm like pbkdf2 with a strong PRF like HMAC-SHA-2 should be sufficient.

Update: To answer your specific questions, you need to perform the following steps, you will need to use a cryptographic library that supports key derivation from password and symmetric encryption, like libsodium.

  1. request password from user on first use
  2. run this password through key derivation to derive a key from it: https://libsodium.gitbook.io/doc/key_derivation
  3. execute encryption of user data with key: https://libsodium.gitbook.io/doc/secret-key_cryptography
  4. destroy the key and send data to server for backup