Skip to content

Commit

Permalink
Add option to specify iv, fixes bighil#4
Browse files Browse the repository at this point in the history
  • Loading branch information
TobleMiner committed Nov 29, 2017
1 parent c8c6c56 commit a70d548
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
22 changes: 12 additions & 10 deletions src/aeslua.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.");

Expand All @@ -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
Expand All @@ -84,24 +85,25 @@ 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;

local key = private.pwToKey(password, keyLength);

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);
Expand Down
6 changes: 4 additions & 2 deletions src/aeslua/ciphermode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit a70d548

Please sign in to comment.