crypto-api 0.13 → 0.13.1
raw patch · 5 files changed
+34/−22 lines, 5 filesdep ~bytestring
Dependency ranges changed: bytestring
Files
- Crypto/Classes.hs +29/−11
- Crypto/Classes/Exceptions.hs +1/−1
- Crypto/Modes.hs +1/−7
- Crypto/Random.hs +2/−2
- crypto-api.cabal +1/−1
Crypto/Classes.hs view
@@ -320,7 +320,7 @@ | otherwise -> Left (GenErrorOther "Generator failed to provide requested number of bytes") {-# INLINEABLE getIV #-} --- | Obtain an 'IV' using the system entropy (see 'System.Crypto.Random')+-- | Obtain an 'IV' using the system entropy (see 'System.Entropy') getIVIO :: (BlockCipher k) => IO (IV k) getIVIO = do let p = Proxy@@ -363,7 +363,6 @@ . ctrLazy k2 (IV . sivMask . B.concat . L.toChunks $ iv) $ m where- bSize = fromIntegral $ blockSizeBytes `for` k1 bSizeb = fromIntegral $ blockSize `for` k1 iv = cMacStar k1 $ xs ++ [m] @@ -374,13 +373,13 @@ -- when authentication fails. modeUnSiv :: BlockCipher k => k -> k -> [L.ByteString] -> L.ByteString -> Maybe L.ByteString modeUnSiv k1 k2 xs c | length xs > bSizeb - 1 = Nothing- | L.length c < fromIntegral bSize = Nothing+ | L.length c < bSize = Nothing | iv /= (cMacStar k1 $ xs ++ [dm]) = Nothing | otherwise = Just dm where bSize = fromIntegral $ blockSizeBytes `for` k1 bSizeb = fromIntegral $ blockSize `for` k1- (iv,m) = L.splitAt (fromIntegral bSize) c+ (iv,m) = L.splitAt bSize c dm = fst $ modeUnCtr incIV k2 (IV $ sivMask $ B.concat $ L.toChunks iv) m -- |SIV (Synthetic IV) mode for strict bytestrings. First argument is@@ -391,7 +390,6 @@ modeSiv' k1 k2 xs m | length xs > bSizeb - 1 = Nothing | otherwise = Just $ B.append iv $ fst $ Crypto.Classes.ctr k2 (IV $ sivMask iv) m where- bSize = fromIntegral $ blockSizeBytes `for` k1 bSizeb = fromIntegral $ blockSize `for` k1 iv = cMacStar' k1 $ xs ++ [m] @@ -413,7 +411,7 @@ modeCbc :: BlockCipher k => k -> IV k -> L.ByteString -> (L.ByteString, IV k) modeCbc k (IV v) plaintext =- let blks = chunkFor k plaintext+ let blks = nontruncatedChunkFor k plaintext (cts, iv) = go blks v in (L.fromChunks cts, IV iv) where@@ -426,7 +424,7 @@ modeUnCbc :: BlockCipher k => k -> IV k -> L.ByteString -> (L.ByteString, IV k) modeUnCbc k (IV v) ciphertext =- let blks = chunkFor k ciphertext+ let blks = nontruncatedChunkFor k ciphertext (pts, iv) = go blks v in (L.fromChunks pts, IV iv) where@@ -459,7 +457,7 @@ keyLengthBytes :: (BlockCipher k) => Tagged k ByteLength keyLengthBytes = fmap (`div` 8) keyLength --- |Build a symmetric key using the system entropy (see 'System.Crypto.Random')+-- |Build a symmetric key using the system entropy (see 'System.Entropy') buildKeyIO :: (BlockCipher k) => IO k buildKeyIO = buildKeyM getEntropy fail @@ -570,7 +568,7 @@ -- |Cipher block chaining encryption mode on strict bytestrings modeCbc' :: BlockCipher k => k -> IV k -> B.ByteString -> (B.ByteString, IV k) modeCbc' k (IV v) plaintext =- let blks = chunkFor' k plaintext+ let blks = nontruncatedChunkFor' k plaintext (cts, iv) = go blks v in (B.concat cts, IV iv) where@@ -584,7 +582,7 @@ -- |Cipher block chaining decryption for strict bytestrings modeUnCbc' :: BlockCipher k => k -> IV k -> B.ByteString -> (B.ByteString, IV k) modeUnCbc' k (IV v) ciphertext =- let blks = chunkFor' k ciphertext+ let blks = nontruncatedChunkFor' k ciphertext (pts, iv) = go blks v in (B.concat pts, IV iv) where@@ -688,10 +686,20 @@ blkSz = (blockSize `for` k) `div` 8 blkSzI = fromIntegral blkSz go bs | L.length bs < blkSzI = []- | otherwise = let (blk,rest) = L.splitAt blkSzI bs in B.concat (L.toChunks blk) : go rest+ | otherwise = let (blk,rest) = L.splitAt blkSzI bs in L.toStrict blk : go rest {-# INLINE chunkFor #-} -- Break a bytestring into block size chunks.+nontruncatedChunkFor :: (BlockCipher k) => k -> L.ByteString -> [B.ByteString]+nontruncatedChunkFor k = go+ where+ blkSz = (blockSize `for` k) `div` 8+ blkSzI = fromIntegral blkSz+ go bs | L.length bs < blkSzI = [L.toStrict bs]+ | otherwise = let (blk,rest) = L.splitAt blkSzI bs in L.toStrict blk : go rest+{-# INLINE nontruncatedChunkFor #-}++-- Break a bytestring into block size chunks. chunkFor' :: (BlockCipher k) => k -> B.ByteString -> [B.ByteString] chunkFor' k = go where@@ -699,6 +707,16 @@ go bs | B.length bs < blkSz = [] | otherwise = let (blk,rest) = B.splitAt blkSz bs in blk : go rest {-# INLINE chunkFor' #-}++-- Break a bytestring into block size chunks plus a final element of+-- possibly less length. Inefficient!+nontruncatedChunkFor' :: (BlockCipher k) => k -> B.ByteString -> [B.ByteString]+nontruncatedChunkFor' k = go+ where+ blkSz = (blockSize `for` k) `div` 8+ go bs | B.length bs < blkSz = [bs]+ | otherwise = let (blk,rest) = B.splitAt blkSz bs in blk : go rest+{-# INLINE nontruncatedChunkFor' #-} -- |Create the mask for SIV based ciphers sivMask :: B.ByteString -> B.ByteString
Crypto/Classes/Exceptions.hs view
@@ -1,4 +1,4 @@--- |The module mirrors 'Crypto.Classes' except that errors are thrown as+-- |The module mirrors "Crypto.Classes" except that errors are thrown as -- exceptions instead of having returning types of @Either error result@ -- or @Maybe result@. --
Crypto/Modes.hs view
@@ -2,13 +2,7 @@ {-| Maintainer: Thomas.DuBuisson@gmail.com Stability: beta- Portability: portable - Authors: Thomas DuBuisson--- Generic mode implementations useable by any correct BlockCipher- instance Be aware there are no tests for CFB mode yet. See- 'Test.Crypto'.+ Portability: portable -} module Crypto.Modes ( -- * Initialization Vector Type, Modifiers (for all ciphers, all modes that use IVs)
Crypto/Random.hs view
@@ -9,7 +9,7 @@ This module is for instantiating cryptographicly strong determinitic random bit generators (DRBGs, aka PRNGs) For the simple use case of using the system random number generator-('System.Crypto.Random') to seed the DRBG:+('System.Entropy') to seed the DRBG: @ g <- newGenIO @@@ -160,7 +160,7 @@ -- (`NotEnoughEntropy`). reseed :: B.ByteString -> g -> Either GenError g - -- |By default this uses "System.Crypto.Random" to obtain+ -- |By default this uses "System.Entropy" to obtain -- entropy for `newGen`. newGenIO :: IO g newGenIO = go 0
crypto-api.cabal view
@@ -1,5 +1,5 @@ name: crypto-api-version: 0.13+version: 0.13.1 license: BSD3 license-file: LICENSE copyright: Thomas DuBuisson <thomas.dubuisson@gmail.com>