Description
Key rotation is mentioned on the Readme on the following parts in the Readme:
https://github.com/dwyl/phoenix-ecto-encryption-example#owasp-cryptographic-rules
https://github.com/dwyl/phoenix-ecto-encryption-example#1-create-the-encryption-app
and on the get_key
function description:
https://github.com/dwyl/phoenix-ecto-encryption-example#3-define-the-6-functions
However it wasn't clear for me how to use key rotation (when to select a new key?, when to create a new key?, can we delete unused key?...). Maybe we can update the Readme to explain how rotation could be used in a project.
My first thought was to randomly get the index of the key from the list of keys and use this key each time we want to insert in the database an encrypted item. However randomising each encrypted item seems more to be the responsibility of the Initialization Vector
(see #8).
So I think it's ok to use the same key for consecutive inserted items on a "long period" of time. We need then to create a new key (every 6 months or a 1 year?) to be used to encrypt the new inserted items and the old keys are only used to decrypt the previous items.
So we want to
- Create a new key every 6 months or a year
- Add the new key at the end of the key list (saved in environment variable) eg, "key1,key2,newKey"
- The
get_key/0
function will always get the last key of the list (ie the latest key created) to encrypt the data - When data is encrypted we want to save the current index of the key used, to be able to decrypt the data later on. Here the
get_key/1
function can take the key index parameter to retrieve the correct key used on encryption.
@nelsonic is this logic correct or do you have other details or step in mind?