crypto-conduit 0.4.3 → 0.5.5
raw patch · 3 files changed
Files
- crypto-conduit.cabal +20/−12
- src/Crypto/Conduit.hs +24/−21
- tests/runtests.hs +16/−25
crypto-conduit.cabal view
@@ -1,8 +1,8 @@ Cabal-version: >= 1.8 Name: crypto-conduit-Version: 0.4.3+Version: 0.5.5 Synopsis: Conduit interface for cryptographic operations (from crypto-api).-Homepage: https://github.com/meteficha/crypto-conduit+Homepage: https://github.com/prowdsponsor/crypto-conduit License: BSD3 License-file: LICENSE Author: Felipe Lessa <felipe.lessa@gmail.com>@@ -21,12 +21,15 @@ Source-repository head Type: git- Location: git://github.com/meteficha/crypto-conduit.git+ Location: git://github.com/prowdsponsor/crypto-conduit.git Flag old-crypto-api Description: Use crypto-api < 0.9. Default: False +Flag conduit11+ Description: Use conduit >= 1.1.+ Library Hs-Source-Dirs: src Exposed-modules:@@ -34,14 +37,17 @@ Build-depends: base >= 3 && < 5, bytestring >= 0.9,- cereal >= 0.3 && < 0.4,- conduit >= 0.5 && < 0.6,- transformers >= 0.2 && < 0.4+ cereal >= 0.3 && < 0.5,+ conduit >= 1.0 && < 1.3,+ transformers >= 0.2 && < 0.5,+ resourcet+ if flag(conduit11)+ Build-depends: conduit-extra >= 1.1 if flag(old-crypto-api) Build-depends: crypto-api >= 0.8 && < 0.9 CPP-options: -DOLD_CRYPTO_API else- Build-depends: crypto-api >= 0.9 && < 0.11+ Build-depends: crypto-api >= 0.9 && < 0.14 GHC-options: -Wall Test-suite runtests@@ -50,17 +56,19 @@ base, bytestring, cereal,- crypto-api >= 0.9,+ crypto-api >= 0.13, conduit, transformers, - cryptocipher >= 0.3,- cryptohash >= 0.7,- skein >= 0.1,- hspec >= 1.3,+ cryptocipher >= 0.4,+ cryptohash-cryptoapi >= 0.1,+ skein >= 0.1,+ hspec >= 1.3, -- finally, our own package crypto-conduit+ if flag(conduit11)+ Build-depends: conduit-extra >= 1.1 GHC-options: -Wall Hs-source-dirs: tests Main-is: runtests.hs
src/Crypto/Conduit.hs view
@@ -59,14 +59,17 @@ import qualified Crypto.Types as C -- from conduit-import Data.Conduit hiding (Source, Sink, Conduit, Pipe)+import Data.Conduit import Data.Conduit.Binary (sourceFile) -- from transformers import Control.Monad.IO.Class (MonadIO, liftIO) import Control.Monad.Trans.Class (lift) +-- from resourcet+import Control.Monad.Trans.Resource (runResourceT) + -- | Helper to get our return type. getType :: Monad m => sink input m output -> output getType = undefined@@ -77,7 +80,7 @@ -- | A 'Sink' that hashes a stream of 'B.ByteString'@s@ and -- creates a digest @d@.-sinkHash :: (Monad m, C.Hash ctx d) => GLSink B.ByteString m d+sinkHash :: (Monad m, C.Hash ctx d) => Consumer B.ByteString m d sinkHash = self where@@ -92,7 +95,7 @@ blockSize = (C.blockLength .::. getType self) `div` 8 -getBlock :: Monad m => BlockMode -> C.ByteLength -> GLSink B.ByteString m Block+getBlock :: Monad m => BlockMode -> C.ByteLength -> Consumer B.ByteString m Block getBlock blockMode blockSize = go id where@@ -136,7 +139,7 @@ #else C.MacKey ctx d #endif- -> GLSink B.ByteString m d+ -> Consumer B.ByteString m d sinkHmac (C.MacKey key) = sink where@@ -180,7 +183,7 @@ -- avoid it if you don't know what you're doing.) conduitEncryptEcb :: (Monad m, C.BlockCipher k) => k -- ^ Cipher key.- -> GLInfConduit B.ByteString m B.ByteString+ -> Conduit B.ByteString m B.ByteString conduitEncryptEcb k = blockCipherConduit k AnyMultiple@@ -194,7 +197,7 @@ -- the block size of the cipher and fails otherwise. conduitDecryptEcb :: (Monad m, C.BlockCipher k) => k -- ^ Cipher key.- -> GLInfConduit B.ByteString m B.ByteString+ -> Conduit B.ByteString m B.ByteString conduitDecryptEcb k = blockCipherConduit k AnyMultiple@@ -212,7 +215,7 @@ conduitEncryptCbc :: (Monad m, C.BlockCipher k) => k -- ^ Cipher key. -> C.IV k -- ^ Initialization vector.- -> GLInfConduit B.ByteString m B.ByteString+ -> Conduit B.ByteString m B.ByteString conduitEncryptCbc k iv = blockCipherConduit k StrictBlockSize@@ -228,7 +231,7 @@ conduitDecryptCbc :: (Monad m, C.BlockCipher k) => k -- ^ Cipher key. -> C.IV k -- ^ Initialization vector.- -> GLInfConduit B.ByteString m B.ByteString+ -> Conduit B.ByteString m B.ByteString conduitDecryptCbc k iv = blockCipherConduit k StrictBlockSize@@ -247,7 +250,7 @@ conduitEncryptCfb :: (Monad m, C.BlockCipher k) => k -- ^ Cipher key. -> C.IV k -- ^ Initialization vector.- -> GLInfConduit B.ByteString m B.ByteString+ -> Conduit B.ByteString m B.ByteString conduitEncryptCfb k iv = blockCipherConduit k StrictBlockSize@@ -263,7 +266,7 @@ conduitDecryptCfb :: (Monad m, C.BlockCipher k) => k -- ^ Cipher key. -> C.IV k -- ^ Initialization vector.- -> GLInfConduit B.ByteString m B.ByteString+ -> Conduit B.ByteString m B.ByteString conduitDecryptCfb k iv = blockCipherConduit k StrictBlockSize@@ -282,7 +285,7 @@ conduitEncryptOfb :: (Monad m, C.BlockCipher k) => k -- ^ Cipher key. -> C.IV k -- ^ Initialization vector.- -> GLInfConduit B.ByteString m B.ByteString+ -> Conduit B.ByteString m B.ByteString conduitEncryptOfb k iv = blockCipherConduit k StrictBlockSize@@ -297,7 +300,7 @@ conduitDecryptOfb :: (Monad m, C.BlockCipher k) => k -- ^ Cipher key. -> C.IV k -- ^ Initialization vector.- -> GLInfConduit B.ByteString m B.ByteString+ -> Conduit B.ByteString m B.ByteString conduitDecryptOfb = conduitEncryptOfb @@ -311,7 +314,7 @@ k -- ^ Cipher key. -> C.IV k -- ^ Initialization vector. -> (C.IV k -> C.IV k) -- ^ Increment counter ('C.incIV' is recommended)- -> GLInfConduit B.ByteString m B.ByteString+ -> Conduit B.ByteString m B.ByteString conduitEncryptCtr k iv incIV = blockCipherConduit k StrictBlockSize@@ -329,7 +332,7 @@ k -- ^ Cipher key. -> C.IV k -- ^ Initialization vector. -> (C.IV k -> C.IV k) -- ^ Increment counter ('C.incIV' is recommended)- -> GLInfConduit B.ByteString m B.ByteString+ -> Conduit B.ByteString m B.ByteString conduitDecryptCtr = conduitEncryptCtr @@ -338,7 +341,7 @@ sourceCtr :: (Monad m, C.BlockCipher k) => k -- ^ Cipher key. -> C.IV k -- ^ Initialization vector.- -> GSource m B.ByteString+ -> Producer m B.ByteString sourceCtr k = loop where@@ -360,7 +363,7 @@ -- for variable-length messages.) sinkCbcMac :: (Monad m, C.BlockCipher k) => k -- ^ Cipher key.- -> GLSink B.ByteString m B.ByteString+ -> Consumer B.ByteString m B.ByteString sinkCbcMac k = go $ B.replicate blockSize 0 where@@ -390,10 +393,10 @@ blocked :: Monad m => BlockMode -> C.ByteLength -- ^ Block size- -> GInfConduit B.ByteString m Block+ -> Conduit B.ByteString m Block blocked mode blockSize = go B.empty where- go x = awaitE >>= either (close x) (push x)+ go x = await >>= maybe (close x) (push x) block = case mode of StrictBlockSize -> blockStrict id@@ -418,7 +421,7 @@ (blks, rest) = block bs bs = append acc x - close acc r = yield (LastOne acc) >> return r+ close acc = yield (LastOne acc) -- | How 'Block's should be returned, either with strictly the@@ -439,7 +442,7 @@ -> s -- ^ Initial state. -> (s -> B.ByteString -> (s, B.ByteString)) -- ^ Encrypt block. -> (s -> B.ByteString -> m B.ByteString) -- ^ Final encryption.- -> GLInfConduit B.ByteString m B.ByteString+ -> Conduit B.ByteString m B.ByteString blockCipherConduit key mode initialState apply final = go initialState where@@ -456,7 +459,7 @@ | B.null input -> return () >> finish | otherwise -> lift (final state input) >>= yield >> finish - finish = awaitE >>= either return (const finish)+ finish = await >>= maybe (return ()) (const finish) -- | zipWith xor + pack --
tests/runtests.hs view
@@ -13,7 +13,7 @@ import Crypto.Classes ((.::.)) import qualified Crypto.Classes as C import qualified Crypto.HMAC as C-import qualified Crypto.Modes as C+import qualified Crypto.Modes as CM --import qualified Crypto.Padding as C --import qualified Crypto.Random as C import qualified Crypto.Types as C@@ -23,19 +23,9 @@ import Data.Conduit.Binary (isolate) import Data.Conduit.List (sourceList, consume) --- from cryptohash-import Crypto.Hash.MD2 (MD2)-import Crypto.Hash.MD4 (MD4)-import Crypto.Hash.MD5 (MD5)-import Crypto.Hash.RIPEMD160 (RIPEMD160)-import Crypto.Hash.SHA1 (SHA1)-import Crypto.Hash.SHA224 (SHA224)-import Crypto.Hash.SHA256 (SHA256)-import Crypto.Hash.SHA384 (SHA384)-import Crypto.Hash.SHA512 (SHA512)-import Crypto.Hash.Skein256 (Skein256)-import Crypto.Hash.Skein512 (Skein512)-import Crypto.Hash.Tiger (Tiger)+-- from cryptohash-cryptoapi+import Crypto.Hash.CryptoAPI ( MD2, MD4, MD5, RIPEMD160, SHA1, SHA224+ , SHA256, SHA384, SHA512, Tiger ) -- from skein import qualified Crypto.Skein as Skein@@ -60,8 +50,6 @@ describe "cryptohash's SHA256" $ testHash (undefined :: SHA256) describe "cryptohash's SHA384" $ testHash (undefined :: SHA384) describe "cryptohash's SHA512" $ testHash (undefined :: SHA512)- describe "cryptohash's Skein256" $ testHash (undefined :: Skein256)- describe "cryptohash's Skein512" $ testHash (undefined :: Skein512) describe "cryptohash's Tiger" $ testHash (undefined :: Tiger) describe "skein's Skein_512_512" $ testHash (undefined :: Skein.Skein_512_512) describe "skein's Skein_1024_1024" $ testHash (undefined :: Skein.Skein_1024_1024)@@ -161,37 +149,37 @@ testBlockCipherConduit Nothing (conduitEncryptCtr k C.zeroIV C.incIV)- (fst . C.ctr C.incIV k C.zeroIV)+ (fst . C.ctr k C.zeroIV) prop "works with conduitDecryptCtr" $ testBlockCipherConduit Nothing (conduitDecryptCtr k C.zeroIV C.incIV)- (fst . C.unCtr C.incIV k C.zeroIV)+ (fst . C.unCtr k C.zeroIV) it "works with sourceCtr" $ let len :: Num a => a len = 1024 * 1024 -- 1 MiB- r1 = runPureResource $ sourceCtr k C.zeroIV $$ isolate len =$ consumeAsLazy- r2 = fst $ C.ctr C.incIV k C.zeroIV (L.replicate len 0)+ r1 = runPureResource $ sourceCtr k C.zeroIV $$ isolate len =$ consumeAsStrict+ r2 = fst $ C.ctr k C.zeroIV (B.replicate len 0) in r1 == r2 prop "works with sinkCbcMac" $ \input -> let inputL = fixBlockedSize blockSize (L.pack input) r1 = runPureResource $ sourceList (L.toChunks inputL) $$ sinkCbcMac k- r2 = B.concat $ L.toChunks $ C.cbcMac k inputL+ r2 = C.encode $ snd $ C.cbc k C.zeroIV $ B.pack input in r1 == r2 testBlockCipherConduit :: Maybe C.ByteLength -- ^ Fix input length to be a multiple of the block size? -> (forall m. Monad m => Conduit B.ByteString m B.ByteString)- -> (L.ByteString -> L.ByteString)+ -> (B.ByteString -> B.ByteString) -> [Word8] -> Bool-testBlockCipherConduit mblockSize conduit lazyfun input =+testBlockCipherConduit mblockSize conduit strictfun input = let inputL = maybe id fixBlockedSize mblockSize (L.pack input)- r1 = runPureResource $ sourceList (L.toChunks inputL) $$ conduit =$ consumeAsLazy- r2 = lazyfun inputL+ r1 = runPureResource $ sourceList (L.toChunks inputL) $$ conduit =$ consumeAsStrict+ r2 = strictfun $ B.pack input in r1 == r2 @@ -203,6 +191,9 @@ consumeAsLazy :: Monad m => Sink B.ByteString m L.ByteString consumeAsLazy = L.fromChunks <$> consume++consumeAsStrict :: Monad m => Sink B.ByteString m B.ByteString+consumeAsStrict = B.concat <$> consume fixBlockedSize :: C.ByteLength -> L.ByteString -> L.ByteString fixBlockedSize blockSize lbs =