-
Notifications
You must be signed in to change notification settings - Fork 28
Added support for AEAD encryption, which is default in Rails 5.2 #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -52,6 +52,30 @@ defmodule PlugRailsCookieSessionStore.MessageEncryptor do | |||||
| end | ||||||
| end | ||||||
|
|
||||||
| @doc """ | ||||||
| Encrypts and signs a message. | ||||||
| """ | ||||||
| def encrypt_and_authenticate(message, secret, cipher \\ :aes_gcm) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Rails seems to use AES 256 GCM: https://github.com/rails/rails/pull/28132/files#diff-744c15344fa1f284281b429673de936cR231 which seems to be a cypher type in :crypto: https://github.com/erlang/otp/blob/master/lib/crypto/src/crypto.erl#L495 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cconstantin can you take a look at this? :)
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gstokkink @4xposed what this is missing is a passing test for rails 5.2. I got stuck on that and never had time to get back and complete. Any chance you can contribute that? |
||||||
| when is_binary(message) and is_binary(secret) do | ||||||
| iv = :crypto.strong_rand_bytes(16) | ||||||
|
|
||||||
| {message, auth_tag} = encrypt({"", pad_message(message), 16}, cipher, secret, iv) | ||||||
| message | ||||||
| |> Base.encode64() | ||||||
| |> Kernel.<>("--#{Base.encode64(iv)}") | ||||||
| |> Kernel.<>("--#{Base.encode64(auth_tag)}") | ||||||
| end | ||||||
|
|
||||||
| @doc """ | ||||||
| Decrypts and authenticates a message. | ||||||
| """ | ||||||
| def authenticate_and_decrypt(encrypted, secret, cipher \\ :aes_gcm) | ||||||
| when is_binary(encrypted) and is_binary(secret) do | ||||||
| [encrypted, iv, auth_tag] = String.split(encrypted, "--") |> Enum.map(&Base.decode64!/1) | ||||||
| result = {"", encrypted, auth_tag} |> decrypt(cipher, secret, iv) |> unpad_message | ||||||
| result | ||||||
| end | ||||||
|
|
||||||
| defp encrypt(message, cipher, secret, iv) do | ||||||
| :crypto.block_encrypt(cipher, trim_secret(secret), iv, message) | ||||||
| end | ||||||
|
|
@@ -66,6 +90,10 @@ defmodule PlugRailsCookieSessionStore.MessageEncryptor do | |||||
| msg <> :binary.copy(<<padding_size>>, padding_size) | ||||||
| end | ||||||
|
|
||||||
| defp unpad_message(:error) do | ||||||
| :error | ||||||
| end | ||||||
|
|
||||||
| defp unpad_message(msg) do | ||||||
| padding_size = :binary.last(msg) | ||||||
| if padding_size <= 16 do | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tried use this branch with real Rails cookie. I've discovered that secret is generated differently. It was because
Plug.Crypto.KeyGeneratoruses key:digestinstead of:key_digestand becausesha1algorithm has to be represented as just:sha(without the "1").Not sure if it changed recently.
I am on Erlang 23.2.5
If a different key was intentional, it is not reflected in the
derivefunction.