-- |
-- Module      : Crypto.Error.Types
-- License     : BSD-style
-- Maintainer  : Vincent Hanquez <vincent@snarc.org>
-- Stability   : stable
-- Portability : Good
--
-- Cryptographic Error enumeration and handling
--
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE TypeFamilies       #-}
module Crypto.Error.Types
    ( CryptoError(..)
    , CryptoFailable(..)
    , throwCryptoErrorIO
    , throwCryptoError
    , onCryptoFailure
    , eitherCryptoError
    , maybeCryptoError
    ) where

import qualified Control.Exception as E
import           Data.Data

import           Basement.Monad (MonadFailure(..))

-- | Enumeration of all possible errors that can be found in this library
data 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 authentification error
    | CryptoError_MacKeyInvalid
    | CryptoError_AuthenticationTagSizeInvalid
    -- Prime generation error
    | CryptoError_PrimeSizeInvalid
    -- Parameter errors
    | CryptoError_SaltTooSmall
    | CryptoError_OutputLengthTooSmall
    | CryptoError_OutputLengthTooBig
    deriving (Int -> CryptoError -> ShowS
[CryptoError] -> ShowS
CryptoError -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CryptoError] -> ShowS
$cshowList :: [CryptoError] -> ShowS
show :: CryptoError -> String
$cshow :: CryptoError -> String
showsPrec :: Int -> CryptoError -> ShowS
$cshowsPrec :: Int -> CryptoError -> ShowS
Show,CryptoError -> CryptoError -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CryptoError -> CryptoError -> Bool
$c/= :: CryptoError -> CryptoError -> Bool
== :: CryptoError -> CryptoError -> Bool
$c== :: CryptoError -> CryptoError -> Bool
Eq,Int -> CryptoError
CryptoError -> Int
CryptoError -> [CryptoError]
CryptoError -> CryptoError
CryptoError -> CryptoError -> [CryptoError]
CryptoError -> CryptoError -> CryptoError -> [CryptoError]
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: CryptoError -> CryptoError -> CryptoError -> [CryptoError]
$cenumFromThenTo :: CryptoError -> CryptoError -> CryptoError -> [CryptoError]
enumFromTo :: CryptoError -> CryptoError -> [CryptoError]
$cenumFromTo :: CryptoError -> CryptoError -> [CryptoError]
enumFromThen :: CryptoError -> CryptoError -> [CryptoError]
$cenumFromThen :: CryptoError -> CryptoError -> [CryptoError]
enumFrom :: CryptoError -> [CryptoError]
$cenumFrom :: CryptoError -> [CryptoError]
fromEnum :: CryptoError -> Int
$cfromEnum :: CryptoError -> Int
toEnum :: Int -> CryptoError
$ctoEnum :: Int -> CryptoError
pred :: CryptoError -> CryptoError
$cpred :: CryptoError -> CryptoError
succ :: CryptoError -> CryptoError
$csucc :: CryptoError -> CryptoError
Enum,Typeable CryptoError
CryptoError -> DataType
CryptoError -> Constr
(forall b. Data b => b -> b) -> CryptoError -> CryptoError
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> CryptoError -> u
forall u. (forall d. Data d => d -> u) -> CryptoError -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> CryptoError -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> CryptoError -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> CryptoError -> m CryptoError
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> CryptoError -> m CryptoError
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c CryptoError
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> CryptoError -> c CryptoError
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c CryptoError)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c CryptoError)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> CryptoError -> m CryptoError
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> CryptoError -> m CryptoError
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> CryptoError -> m CryptoError
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> CryptoError -> m CryptoError
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> CryptoError -> m CryptoError
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> CryptoError -> m CryptoError
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> CryptoError -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> CryptoError -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> CryptoError -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> CryptoError -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> CryptoError -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> CryptoError -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> CryptoError -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> CryptoError -> r
gmapT :: (forall b. Data b => b -> b) -> CryptoError -> CryptoError
$cgmapT :: (forall b. Data b => b -> b) -> CryptoError -> CryptoError
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c CryptoError)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c CryptoError)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c CryptoError)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c CryptoError)
dataTypeOf :: CryptoError -> DataType
$cdataTypeOf :: CryptoError -> DataType
toConstr :: CryptoError -> Constr
$ctoConstr :: CryptoError -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c CryptoError
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c CryptoError
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> CryptoError -> c CryptoError
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> CryptoError -> c CryptoError
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 associated
--
data CryptoFailable a =
      CryptoPassed a
    | CryptoFailed CryptoError
    deriving (Int -> CryptoFailable a -> ShowS
forall a. Show a => Int -> CryptoFailable a -> ShowS
forall a. Show a => [CryptoFailable a] -> ShowS
forall a. Show a => CryptoFailable a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CryptoFailable a] -> ShowS
$cshowList :: forall a. Show a => [CryptoFailable a] -> ShowS
show :: CryptoFailable a -> String
$cshow :: forall a. Show a => CryptoFailable a -> String
showsPrec :: Int -> CryptoFailable a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> CryptoFailable a -> ShowS
Show)

instance Eq a => Eq (CryptoFailable a) where
    == :: CryptoFailable a -> CryptoFailable a -> Bool
(==) (CryptoPassed a
a)  (CryptoPassed a
b)  = a
a forall a. Eq a => a -> a -> Bool
== a
b
    (==) (CryptoFailed CryptoError
e1) (CryptoFailed CryptoError
e2) = CryptoError
e1 forall a. Eq a => a -> a -> Bool
== CryptoError
e2
    (==) CryptoFailable a
_                 CryptoFailable a
_                 = Bool
False

instance Functor CryptoFailable where
    fmap :: forall a b. (a -> b) -> CryptoFailable a -> CryptoFailable b
fmap a -> b
f (CryptoPassed a
a) = forall a. a -> CryptoFailable a
CryptoPassed (a -> b
f a
a)
    fmap a -> b
_ (CryptoFailed CryptoError
r) = forall a. CryptoError -> CryptoFailable a
CryptoFailed CryptoError
r

instance Applicative CryptoFailable where
    pure :: forall a. a -> CryptoFailable a
pure a
a     = forall a. a -> CryptoFailable a
CryptoPassed a
a
    <*> :: forall a b.
CryptoFailable (a -> b) -> CryptoFailable a -> CryptoFailable b
(<*>) CryptoFailable (a -> b)
fm CryptoFailable a
m = CryptoFailable (a -> b)
fm forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a -> b
p -> CryptoFailable a
m forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a
r2 -> forall (m :: * -> *) a. Monad m => a -> m a
return (a -> b
p a
r2)
instance Monad CryptoFailable where
    return :: forall a. a -> CryptoFailable a
return = forall (f :: * -> *) a. Applicative f => a -> f a
pure
    >>= :: forall a b.
CryptoFailable a -> (a -> CryptoFailable b) -> CryptoFailable b
(>>=) CryptoFailable a
m1 a -> CryptoFailable b
m2 = do
        case CryptoFailable a
m1 of
            CryptoPassed a
a -> a -> CryptoFailable b
m2 a
a
            CryptoFailed CryptoError
e -> forall a. CryptoError -> CryptoFailable a
CryptoFailed CryptoError
e

instance MonadFailure CryptoFailable where
    type Failure CryptoFailable = CryptoError
    mFail :: Failure CryptoFailable -> CryptoFailable ()
mFail = forall a. CryptoError -> CryptoFailable a
CryptoFailed

-- | Throw an CryptoError as exception on CryptoFailed result,
-- otherwise return the computed value
throwCryptoErrorIO :: CryptoFailable a -> IO a
throwCryptoErrorIO :: forall a. CryptoFailable a -> IO a
throwCryptoErrorIO (CryptoFailed CryptoError
e) = forall e a. Exception e => e -> IO a
E.throwIO CryptoError
e
throwCryptoErrorIO (CryptoPassed a
r) = forall (m :: * -> *) a. Monad m => a -> m a
return a
r

-- | Same as 'throwCryptoErrorIO' but throw the error asynchronously.
throwCryptoError :: CryptoFailable a -> a
throwCryptoError :: forall a. CryptoFailable a -> a
throwCryptoError (CryptoFailed CryptoError
e) = forall a e. Exception e => e -> a
E.throw CryptoError
e
throwCryptoError (CryptoPassed a
r) = a
r

-- | Simple 'either' like combinator for CryptoFailable type
onCryptoFailure :: (CryptoError -> r) -> (a -> r) -> CryptoFailable a -> r
onCryptoFailure :: forall r a. (CryptoError -> r) -> (a -> r) -> CryptoFailable a -> r
onCryptoFailure CryptoError -> r
onError a -> r
_         (CryptoFailed CryptoError
e) = CryptoError -> r
onError CryptoError
e
onCryptoFailure CryptoError -> r
_       a -> r
onSuccess (CryptoPassed a
r) = a -> r
onSuccess a
r

-- | Transform a CryptoFailable to an Either
eitherCryptoError :: CryptoFailable a -> Either CryptoError a
eitherCryptoError :: forall a. CryptoFailable a -> Either CryptoError a
eitherCryptoError (CryptoFailed CryptoError
e) = forall a b. a -> Either a b
Left CryptoError
e
eitherCryptoError (CryptoPassed a
a) = forall a b. b -> Either a b
Right a
a

-- | Transform a CryptoFailable to a Maybe
maybeCryptoError :: CryptoFailable a -> Maybe a
maybeCryptoError :: forall a. CryptoFailable a -> Maybe a
maybeCryptoError (CryptoFailed CryptoError
_) = forall a. Maybe a
Nothing
maybeCryptoError (CryptoPassed a
r) = forall a. a -> Maybe a
Just a
r