Skip to content
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

feat: add AAD support in aes gcm #99

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions lib/resty/aes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ function _M.new(self, key, salt, _cipher, _hash, hash_rounds, iv_len, enable_pad
end


function _M.encrypt(self, s)
function _M.encrypt(self, s, aad)
local typ = type(self)
if typ ~= "table" then
error("bad argument #1 self: table expected, got " .. typ, 2)
Expand All @@ -241,6 +241,14 @@ function _M.encrypt(self, s)
return nil, "EVP_EncryptInit_ex failed"
end

if self._cipher == "gcm" then
if aad ~= nil then
if C.EVP_EncryptUpdate(ctx, nil, tmp_len, aad, #aad) == 0 then
return nil, "C.EVP_EncryptUpdate failed"
end
end
end

if C.EVP_EncryptUpdate(ctx, buf, out_len, s, s_len) == 0 then
return nil, "EVP_EncryptUpdate failed"
end
Expand All @@ -267,7 +275,7 @@ function _M.encrypt(self, s)
end


function _M.decrypt(self, s, tag)
function _M.decrypt(self, s, tag, aad)
local typ = type(self)
if typ ~= "table" then
error("bad argument #1 self: table expected, got " .. typ, 2)
Expand All @@ -284,6 +292,14 @@ function _M.decrypt(self, s, tag)
return nil, "EVP_DecryptInit_ex failed"
end

if self._cipher == "gcm" then
if aad ~= nil then
if C.EVP_DecryptUpdate(ctx, nil, tmp_len, aad, #aad) == 0 then
return nil, "C.EVP_DecryptUpdate failed"
end
end
end

if C.EVP_DecryptUpdate(ctx, buf, out_len, s, s_len) == 0 then
return nil, "EVP_DecryptUpdate failed"
end
Expand Down