Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add Typeable and Exception instances for GenError.

  • Loading branch information...
commit 779671de5c82b29a2528bdcde6fa26ec55f1fe2f 1 parent 0f9982c
@TomMD authored
View
3  CHANGELOG
@@ -1,3 +1,6 @@
+0.10
+* Add Typeable and Exception instances to GenError in response to users.
+
0.9
* Crypto.Classes now exports 'Data.Serialize.encode' mostly for folks who want their hashes as bytestrings
* AsymCipher now has proper fundeps
View
2  Crypto/HMAC.hs
@@ -16,6 +16,8 @@ import Crypto.Classes
import Data.Serialize (encode)
import Data.Bits (xor)
+-- | A key carrying phantom types @c@ and @d@, forcing the key data to only be used
+-- by particular hash algorithms.
newtype MacKey c d = MacKey B.ByteString deriving (Eq, Ord, Show)
-- |Message authentication code calculation for lazy bytestrings.
View
7 Crypto/Modes.hs
@@ -139,10 +139,12 @@ cbc' k (IV v) plaintext =
in (c:cs, ivFinal)
{-# INLINEABLE cbc' #-}
+-- |Cipher block chaining message authentication
cbcMac' :: BlockCipher k => k -> B.ByteString -> B.ByteString
cbcMac' k pt = encode $ snd $ cbc' k zeroIV pt
{-# INLINEABLE cbcMac' #-}
+-- |Cipher block chaining message authentication
cbcMac :: BlockCipher k => k -> L.ByteString -> L.ByteString
cbcMac k pt = L.fromChunks [encode $ snd $ cbc k zeroIV pt]
{-# INLINEABLE cbcMac #-}
@@ -444,25 +446,28 @@ zeroIV = iv
where bytes = ivBlockSizeBytes iv
iv = IV $ B.replicate bytes 0
-
+-- |Cook book mode - not really a mode at all. If you don't know what you're doing, don't use this mode^H^H^H^H library.
ecb :: BlockCipher k => k -> L.ByteString -> L.ByteString
ecb k msg =
let chunks = chunkFor k msg
in L.fromChunks $ map (encryptBlock k) chunks
{-# INLINEABLE ecb #-}
+-- |ECB decrypt, complementary to `ecb`.
unEcb :: BlockCipher k => k -> L.ByteString -> L.ByteString
unEcb k msg =
let chunks = chunkFor k msg
in L.fromChunks $ map (decryptBlock k) chunks
{-# INLINEABLE unEcb #-}
+-- | Like `ecb` but for strict bytestrings
ecb' :: BlockCipher k => k -> B.ByteString -> B.ByteString
ecb' k msg =
let chunks = chunkFor' k msg
in B.concat $ map (encryptBlock k) chunks
{-# INLINEABLE ecb' #-}
+-- |Decryption complement to `ecb'`
unEcb' :: BlockCipher k => k -> B.ByteString -> B.ByteString
unEcb' k ct =
let chunks = chunkFor' k ct
View
17 Crypto/Random.hs
@@ -1,11 +1,11 @@
-{-# LANGUAGE FlexibleInstances, TypeSynonymInstances, CPP #-}
+{-# LANGUAGE FlexibleInstances, TypeSynonymInstances, DeriveDataTypeable, CPP #-}
{-|
Maintainer: Thomas.DuBuisson@gmail.com
Stability: beta
Portability: portable
- This module is for instantiating cryptographically strong
+ 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:
@@ -32,10 +32,12 @@ module Crypto.Random
) where
import Control.Monad (liftM)
+import Control.Exception
import Crypto.Types
import Data.Bits (xor, setBit, shiftR, shiftL, (.&.))
import Data.List (foldl')
import Data.Tagged
+import Data.Typeable
import System.Entropy
import System.IO.Unsafe(unsafeInterleaveIO)
import qualified Data.ByteString as B
@@ -47,6 +49,10 @@ import Data.Proxy
#endif
-- |Generator failures should always return the appropriate GenError.
+-- Note 'GenError' in an instance of exception but wether or not an
+-- exception is thrown depends on if the selected generator (read:
+-- if you don't want execptions from code that uses 'throw' then
+-- pass in a generator that never has an error for the used functions)
data GenError =
GenErrorOther String -- ^ Misc
| RequestedTooManyBytes -- ^ Requested more bytes than a
@@ -64,7 +70,9 @@ data GenError =
| NeedsInfiniteSeed -- ^ This generator can not be
-- instantiated or reseeded with a
-- finite seed (ex: 'SystemRandom')
- deriving (Eq, Ord, Show)
+ deriving (Eq, Ord, Show, Typeable)
+
+instance Exception GenError
-- |A class of random bit generators that allows for the possibility
-- of failure, reseeding, providing entropy at the same time as
@@ -138,7 +146,8 @@ class CryptoRandomGen g where
newGenIO :: IO g
newGenIO = go 0
where
- go 1000 = error $ "The generator instance requested by" ++
+ go 1000 = throw $ GenErrorOther $
+ "The generator instance requested by" ++
"newGenIO never instantiates (1000 tries). " ++
"It must be broken."
go i = do
View
1  Crypto/Util.hs
@@ -1,3 +1,4 @@
+-- |A small selection of utilities that might be of use to others working with bytestring/number combinations.
module Crypto.Util where
import qualified Data.ByteString as B
import Data.ByteString.Unsafe (unsafeIndex)
View
2  crypto-api.cabal
@@ -1,5 +1,5 @@
name: crypto-api
-version: 0.9
+version: 0.10
license: BSD3
license-file: LICENSE
copyright: Thomas DuBuisson <thomas.dubuisson@gmail.com>, Francisco Blas Izquierdo Riera (klondike) (see AUTHORS)
Please sign in to comment.
Something went wrong with that request. Please try again.