dwyl / dwyl/phoenix-ecto-encryption-example

How to work with key rotation?

Open
#26 1 comment 0 reactions 0 assignees View on GitHub
discuss question
Dominant language
Elixir
Stars
291
Forks
21
Avg merge
1d 4h
Merged PRs (30d)
3

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
![image](https://user-images.githubusercontent.com/6057298/69864886-d0c2a400-1297-11ea-9857-17fa81dbbc07.png)

https://github.com/dwyl/phoenix-ecto-encryption-example#1-create-the-encryption-app
![image](https://user-images.githubusercontent.com/6057298/69864979-06678d00-1298-11ea-9244-fada6d6d8e1d.png)

and on the `get_key` function description:
https://github.com/dwyl/phoenix-ecto-encryption-example#3-define-the-6-functions
![image](https://user-images.githubusercontent.com/6057298/69865031-226b2e80-1298-11ea-9e79-f78ea5582ea7.png)

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 https://github.com/dwyl/phoenix-ecto-encryption-example/issues/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?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.