From a70d5484f29397f63a88e8c570953fcf2484d6ee Mon Sep 17 00:00:00 2001 From: Tobias Schramm Date: Wed, 29 Nov 2017 19:05:00 +0100 Subject: [PATCH] Add option to specify iv, fixes #4 --- src/aeslua.lua | 22 ++++++++++++---------- src/aeslua/ciphermode.lua | 6 ++++-- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/aeslua.lua b/src/aeslua.lua index a18573e..8bd77f5 100644 --- a/src/aeslua.lua +++ b/src/aeslua.lua @@ -48,10 +48,11 @@ end -- data - string to encrypt (must not be too large) -- keyLength - length of aes key: 128(default), 192 or 256 Bit -- mode - mode of encryption: ecb, cbc(default), ofb, cfb +-- iv - initialization vector to use -- -- mode and keyLength must be the same for encryption and decryption. -- -function public.encrypt(password, data, keyLength, mode) +function public.encrypt(password, data, keyLength, mode, iv) assert(password ~= nil, "Empty password."); assert(password ~= nil, "Empty data."); @@ -63,13 +64,13 @@ function public.encrypt(password, data, keyLength, mode) local paddedData = util.padByteString(data); if (mode == public.ECBMODE) then - return ciphermode.encryptString(key, paddedData, ciphermode.encryptECB); + return ciphermode.encryptString(key, paddedData, ciphermode.encryptECB, iv); elseif (mode == public.CBCMODE) then - return ciphermode.encryptString(key, paddedData, ciphermode.encryptCBC); + return ciphermode.encryptString(key, paddedData, ciphermode.encryptCBC, iv); elseif (mode == public.OFBMODE) then - return ciphermode.encryptString(key, paddedData, ciphermode.encryptOFB); + return ciphermode.encryptString(key, paddedData, ciphermode.encryptOFB, iv); elseif (mode == public.CFBMODE) then - return ciphermode.encryptString(key, paddedData, ciphermode.encryptCFB); + return ciphermode.encryptString(key, paddedData, ciphermode.encryptCFB, iv); else return nil; end @@ -84,10 +85,11 @@ end -- data - string to encrypt -- keyLength - length of aes key: 128(default), 192 or 256 Bit -- mode - mode of decryption: ecb, cbc(default), ofb, cfb +-- iv - initialization vector to use -- -- mode and keyLength must be the same for encryption and decryption. -- -function public.decrypt(password, data, keyLength, mode) +function public.decrypt(password, data, keyLength, mode, iv) local mode = mode or public.CBCMODE; local keyLength = keyLength or public.AES128; @@ -95,13 +97,13 @@ function public.decrypt(password, data, keyLength, mode) local plain; if (mode == public.ECBMODE) then - plain = ciphermode.decryptString(key, data, ciphermode.decryptECB); + plain = ciphermode.decryptString(key, data, ciphermode.decryptECB, iv); elseif (mode == public.CBCMODE) then - plain = ciphermode.decryptString(key, data, ciphermode.decryptCBC); + plain = ciphermode.decryptString(key, data, ciphermode.decryptCBC, iv); elseif (mode == public.OFBMODE) then - plain = ciphermode.decryptString(key, data, ciphermode.decryptOFB); + plain = ciphermode.decryptString(key, data, ciphermode.decryptOFB, iv); elseif (mode == public.CFBMODE) then - plain = ciphermode.decryptString(key, data, ciphermode.decryptCFB); + plain = ciphermode.decryptString(key, data, ciphermode.decryptCFB, iv); end result = util.unpadByteString(plain); diff --git a/src/aeslua/ciphermode.lua b/src/aeslua/ciphermode.lua index ab1faf1..b247f36 100644 --- a/src/aeslua/ciphermode.lua +++ b/src/aeslua/ciphermode.lua @@ -11,8 +11,9 @@ aeslua.ciphermode = public; -- key - byte array with key -- string - string to encrypt -- modefunction - function for cipher mode to use +-- iv - initialization vector to use -- -function public.encryptString(key, data, modeFunction) +function public.encryptString(key, data, modeFunction, iv) local iv = iv or {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; local keySched = aes.expandEncryptionKey(key); local encryptedData = buffer.new(); @@ -71,8 +72,9 @@ end -- key - byte array with key -- string - string to decrypt -- modefunction - function for cipher mode to use +-- iv - initialization vector to use -- -function public.decryptString(key, data, modeFunction) +function public.decryptString(key, data, modeFunction, iv) local iv = iv or {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; local keySched;