@@ -224,7 +224,7 @@ function _M.new(self, key, salt, _cipher, _hash, hash_rounds, iv_len, enable_pad
224224end
225225
226226
227- function _M .encrypt (self , s )
227+ function _M .encrypt (self , s , aad )
228228 local typ = type (self )
229229 if typ ~= " table" then
230230 error (" bad argument #1 self: table expected, got " .. typ , 2 )
@@ -241,6 +241,12 @@ function _M.encrypt(self, s)
241241 return nil , " EVP_EncryptInit_ex failed"
242242 end
243243
244+ if self ._cipher == " gcm" and aad ~= nil then
245+ if C .EVP_EncryptUpdate (ctx , nil , tmp_len , aad , # aad ) == 0 then
246+ return nil , " C.EVP_EncryptUpdate failed"
247+ end
248+ end
249+
244250 if C .EVP_EncryptUpdate (ctx , buf , out_len , s , s_len ) == 0 then
245251 return nil , " EVP_EncryptUpdate failed"
246252 end
@@ -267,7 +273,7 @@ function _M.encrypt(self, s)
267273end
268274
269275
270- function _M .decrypt (self , s , tag )
276+ function _M .decrypt (self , s , tag , aad )
271277 local typ = type (self )
272278 if typ ~= " table" then
273279 error (" bad argument #1 self: table expected, got " .. typ , 2 )
@@ -284,6 +290,12 @@ function _M.decrypt(self, s, tag)
284290 return nil , " EVP_DecryptInit_ex failed"
285291 end
286292
293+ if self ._cipher == " gcm" and aad ~= nil then
294+ if C .EVP_DecryptUpdate (ctx , nil , tmp_len , aad , # aad ) == 0 then
295+ return nil , " C.EVP_DecryptUpdate failed"
296+ end
297+ end
298+
287299 if C .EVP_DecryptUpdate (ctx , buf , out_len , s , s_len ) == 0 then
288300 return nil , " EVP_DecryptUpdate failed"
289301 end
0 commit comments