packages feed

crypton-2.3.1 / Crypto / Error / Types.hs

{-# LANGUAGE DeriveDataTypeable #-}{-# LANGUAGE TypeFamilies #-}-- |-- Module      : Crypto.Error.Types-- License     : BSD-style-- Maintainer  : Vincent Hanquez <vincent@snarc.org>-- Stability   : stable-- Portability : Good---- Cryptographic Error enumeration and handlingmodule Crypto.Error.Types (    CryptoError (..),    CryptoFailable (..),    throwCryptoErrorIO,    throwCryptoError,    onCryptoFailure,    eitherCryptoError,    maybeCryptoError,) whereimport qualified Control.Exception as Eimport Data.Data-- | Enumeration of all possible errors that can be found in this librarydata CryptoError    = -- symmetric cipher errors      CryptoError_KeySizeInvalid    | CryptoError_IvSizeInvalid    | CryptoError_SeedSizeInvalid    | CryptoError_AEADModeNotSupported    | -- public key cryptography error      CryptoError_SecretKeySizeInvalid    | CryptoError_SecretKeyStructureInvalid    | CryptoError_PublicKeySizeInvalid    | CryptoError_SharedSecretSizeInvalid    | -- elliptic cryptography error      CryptoError_EcScalarOutOfBounds    | CryptoError_PointSizeInvalid    | CryptoError_PointFormatInvalid    | CryptoError_PointFormatUnsupported    | CryptoError_PointCoordinatesInvalid    | CryptoError_ScalarMultiplicationInvalid    | -- Message authentication error      CryptoError_MacKeyInvalid    | CryptoError_AuthenticationTagSizeInvalid    | -- Prime generation error      CryptoError_PrimeSizeInvalid    | -- Parameter errors      CryptoError_SaltTooSmall    | CryptoError_OutputLengthTooSmall    | CryptoError_OutputLengthTooBig    | -- | A parameter is outside the range the algorithm accepts.  Appended to      -- keep the 'Enum' values of the constructors above unchanged.      CryptoError_ParameterInvalid    | -- | A point satisfies the curve equation but lies outside the subgroup      -- the base point generates, so multiplying it would answer modulo a      -- small order.  Appended for the same reason as the constructor above.      CryptoError_PointSubgroupInvalid    | -- | A public key is the right length but is not a well-formed encoding      -- of one, so no honest party produced it.  Appended for the same      -- reason as the two constructors above.      CryptoError_PublicKeyStructureInvalid    deriving (Show, Eq, Enum, Data)instance E.Exception CryptoError-- | A simple Either like type to represent a computation that can fail---- 2 possibles values are:---- * 'CryptoPassed' : The computation succeeded, and contains the result of the computation---- * 'CryptoFailed' : The computation failed, and contains the cryptographic error associateddata CryptoFailable a    = CryptoPassed a    | CryptoFailed CryptoError    deriving (Show)instance Eq a => Eq (CryptoFailable a) where    (==) (CryptoPassed a) (CryptoPassed b) = a == b    (==) (CryptoFailed e1) (CryptoFailed e2) = e1 == e2    (==) _ _ = Falseinstance Functor CryptoFailable where    fmap f (CryptoPassed a) = CryptoPassed (f a)    fmap _ (CryptoFailed r) = CryptoFailed rinstance Applicative CryptoFailable where    pure a = CryptoPassed a    (<*>) fm m = fm >>= \p -> m >>= \r2 -> return (p r2)instance Monad CryptoFailable where    return = pure    (>>=) m1 m2 = do        case m1 of            CryptoPassed a -> m2 a            CryptoFailed e -> CryptoFailed e-- | Throw an CryptoError as exception on CryptoFailed result,-- otherwise return the computed valuethrowCryptoErrorIO :: CryptoFailable a -> IO athrowCryptoErrorIO (CryptoFailed e) = E.throwIO ethrowCryptoErrorIO (CryptoPassed r) = return r-- | Same as 'throwCryptoErrorIO' but throw the error asynchronously.throwCryptoError :: CryptoFailable a -> athrowCryptoError (CryptoFailed e) = E.throw ethrowCryptoError (CryptoPassed r) = r-- | Simple 'either' like combinator for CryptoFailable typeonCryptoFailure :: (CryptoError -> r) -> (a -> r) -> CryptoFailable a -> ronCryptoFailure onError _ (CryptoFailed e) = onError eonCryptoFailure _ onSuccess (CryptoPassed r) = onSuccess r-- | Transform a CryptoFailable to an EithereitherCryptoError :: CryptoFailable a -> Either CryptoError aeitherCryptoError (CryptoFailed e) = Left eeitherCryptoError (CryptoPassed a) = Right a-- | Transform a CryptoFailable to a MaybemaybeCryptoError :: CryptoFailable a -> Maybe amaybeCryptoError (CryptoFailed _) = NothingmaybeCryptoError (CryptoPassed r) = Just r