AES 0.2.2 → 0.2.3
raw patch · 2 files changed
+16/−27 lines, 2 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- AES.cabal +1/−1
- Codec/Crypto/AES/IO.hsc +15/−26
AES.cabal view
@@ -1,7 +1,7 @@ Name: AES Synopsis: Fast AES encryption/decryption for bytestrings Description: A zero-copy binding to Brian Gladman's AES implementation, including a copy of that implementation-Version: 0.2.2+Version: 0.2.3 License: BSD3 License-file: COPYING Copyright: Copyright (c) 2009 University of Tromsø
Codec/Crypto/AES/IO.hsc view
@@ -41,23 +41,20 @@ -- of 16; when using lazy bytestrings, its /component/ strict -- bytestrings must all satisfy this. ----- Other modes can handle bytestrings of any length, by storing--- overflow for later. However, the total length of bytestrings passed--- in must still be a multiple of 16, or the overflow will be lost.+-- Other modes can handle bytestrings of any length. However,+-- encrypting a bytestring of length 5 and then one of length 4 is not+-- the same operation as encrypting a single bytestring of length 9;+-- they are internally padded to a multiple of 16 bytes. -- -- For OFB and CTR, Encrypt and Decrypt are the same operation. For -- CTR, the IV is the initial value of the counter. data Mode = ECB | CBC | CFB | OFB | CTR -data Context = ECBCtx DirectionalCtx- | CBCCtx IV DirectionalCtx- | CFBCtx IV Direction EncryptCtxP- | OFBCtx IV EncryptCtxP- | CTRCtx IV EncryptCtxP----data AESCtx = AESCtx Context (IORef Int)+data AESCtx = ECBCtx DirectionalCtx+ | CBCCtx IV DirectionalCtx+ | CFBCtx IV Direction EncryptCtxP+ | OFBCtx IV EncryptCtxP+ | CTRCtx IV EncryptCtxP data DirectionalCtx = EncryptCtx EncryptCtxP | DecryptCtx DecryptCtxP@@ -72,9 +69,9 @@ -> B.ByteString -- ^ A 16-byte IV -> Direction -> IO AESCtx-newCtx mode (toKey -> key) (toIV -> iv) dir = wrapCtr =<< newCtx' key iv mode dir+newCtx mode (toKey -> key) (toIV -> iv) dir = newCtx' key iv mode dir -newCtx' :: AESKey -> IV -> Mode -> Direction -> IO Context+newCtx' :: AESKey -> IV -> Mode -> Direction -> IO AESCtx newCtx' key _ ECB dir = newECBCtx' key dir newCtx' key iv CBC Encrypt = CBCCtx iv . EncryptCtx <$> encryptCtx key newCtx' key iv CBC Decrypt = CBCCtx iv . DecryptCtx <$> decryptCtx key@@ -82,15 +79,12 @@ newCtx' key iv OFB _ = OFBCtx iv <$> encryptCtx key newCtx' key iv CTR _ = CTRCtx iv <$> encryptCtx key -wrapCtr :: Context -> IO AESCtx-wrapCtr ctx = AESCtx ctx <$> newIORef 0- -- | Create a context for ECB, which doesn't need an IV newECBCtx :: B.ByteString -- ^ A 16, 24 or 32-byte AES key -> Direction -> IO AESCtx-newECBCtx (toKey -> key) dir = wrapCtr =<< newECBCtx' key dir+newECBCtx (toKey -> key) dir = newECBCtx' key dir -newECBCtx' :: AESKey -> Direction -> IO Context+newECBCtx' :: AESKey -> Direction -> IO AESCtx newECBCtx' key Encrypt = ECBCtx . EncryptCtx <$> encryptCtx key newECBCtx' key Decrypt = ECBCtx . DecryptCtx <$> decryptCtx key @@ -99,14 +93,9 @@ -- crypt is definitely not thread-safe. Don't even think about -- it. crypt :: AESCtx -> B.ByteString -> IO B.ByteString-crypt (AESCtx ctx count) bs = do- before <- readIORef count- let blocks = ((before + B.length bs) `div` 16) - (before `div` 16)- bytes = blocks * 16- writeIORef count (before + bytes)- crypt' ctx bs bytes+crypt ctx bs = crypt' ctx bs (B.length bs) -crypt' :: Context -> B.ByteString -> Int -> IO B.ByteString+crypt' :: AESCtx -> B.ByteString -> Int -> IO B.ByteString crypt' (ECBCtx (EncryptCtx ctx)) = call _aes_ecb_encrypt ctx crypt' (ECBCtx (DecryptCtx ctx)) = call _aes_ecb_decrypt ctx crypt' (CBCCtx iv (EncryptCtx ctx)) = calliv _aes_cbc_encrypt iv ctx