From be75de64e19db417ffadfb9215cfb3280bfbdf75 Mon Sep 17 00:00:00 2001 From: Vincent Hanquez Date: Tue, 2 Jun 2015 14:22:26 +0100 Subject: [PATCH] [aes] fast track length == 0 with ECB encryption/decryption --- Crypto/Cipher/AES/Primitive.hs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Crypto/Cipher/AES/Primitive.hs b/Crypto/Cipher/AES/Primitive.hs index d24acf9..5038fbe 100644 --- a/Crypto/Cipher/AES/Primitive.hs +++ b/Crypto/Cipher/AES/Primitive.hs @@ -351,11 +351,13 @@ doECB :: ByteArray ba => (Ptr b -> Ptr AES -> CString -> CUInt -> IO ()) -> AES -> ba -> ba doECB f ctx input - | r /= 0 = error $ "Encryption error: input length must be a multiple of block size (16). Its length is: " ++ (show len) - | otherwise = B.allocAndFreeze len $ \o -> - keyToPtr ctx $ \k -> - withByteArray input $ \i -> - f (castPtr o) k i (fromIntegral nbBlocks) + | len == 0 = B.empty + | r /= 0 = error $ "Encryption error: input length must be a multiple of block size (16). Its length is: " ++ (show len) + | otherwise = + B.allocAndFreeze len $ \o -> + keyToPtr ctx $ \k -> + withByteArray input $ \i -> + f (castPtr o) k i (fromIntegral nbBlocks) where (nbBlocks, r) = len `quotRem` 16 len = B.length input