Skip to content

Commit d1ae2e5

Browse files
committed
Allow iodata in AAD
1 parent 060ee3a commit d1ae2e5

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

lib/plug/crypto/message_encryptor.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ defmodule Plug.Crypto.MessageEncryptor do
3434
It defaults to "A128GCM" for backwards compatibility.
3535
"""
3636
def encrypt(message, aad \\ "A128GCM", secret, sign_secret)
37-
when is_binary(message) and is_binary(aad) and byte_size(secret) > 0 and
37+
when is_binary(message) and (is_binary(aad) or is_list(aad)) and byte_size(secret) > 0 and
3838
is_binary(sign_secret) do
3939
aes128_gcm_encrypt(message, aad, secret, sign_secret)
4040
rescue
@@ -45,7 +45,7 @@ defmodule Plug.Crypto.MessageEncryptor do
4545
Decrypts a message using authenticated encryption.
4646
"""
4747
def decrypt(encrypted, aad \\ "A128GCM", secret, sign_secret)
48-
when is_binary(encrypted) and is_binary(aad) and byte_size(secret) > 0 and
48+
when is_binary(encrypted) and (is_binary(aad) or is_list(aad)) and byte_size(secret) > 0 and
4949
is_binary(sign_secret) do
5050
aes128_gcm_decrypt(encrypted, aad, secret, sign_secret)
5151
rescue

test/plug/crypto/message_encryptor_test.exs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ defmodule Plug.Crypto.MessageEncryptorTest do
1111

1212
test "it encrypts/decrypts a message" do
1313
data = <<0, "hełłoworld", 0>>
14-
encrypted = ME.encrypt(<<0, "hełłoworld", 0>>, "right aad", @right, @right)
14+
encrypted = ME.encrypt(data, "right aad", @right, @right)
1515

1616
decrypted = ME.decrypt(encrypted, "right aad", @wrong, @wrong)
1717
assert decrypted == :error
@@ -29,6 +29,12 @@ defmodule Plug.Crypto.MessageEncryptorTest do
2929
assert decrypted == {:ok, data}
3030
end
3131

32+
test "it encrypts/decrypts with iodata aad" do
33+
data = <<0, "hełłoworld", 0>>
34+
encrypted = ME.encrypt(data, ["right", ?\s, "aad"], @right, @right)
35+
assert ME.decrypt(encrypted, ["right", ?\s, "aad"], @right, @right) == {:ok, data}
36+
end
37+
3238
test "it uses only the first 32 bytes to encrypt/decrypt" do
3339
data = <<0, "helloworld", 0>>
3440
encrypted = ME.encrypt(<<0, "helloworld", 0>>, @large, @large)

0 commit comments

Comments
 (0)