-- |
-- 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
(Int -> CryptoError -> ShowS)
-> (CryptoError -> String)
-> ([CryptoError] -> ShowS)
-> Show CryptoError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CryptoError -> ShowS
showsPrec :: Int -> CryptoError -> ShowS
$cshow :: CryptoError -> String
show :: CryptoError -> String
$cshowList :: [CryptoError] -> ShowS
showList :: [CryptoError] -> ShowS
Show,CryptoError -> CryptoError -> Bool
(CryptoError -> CryptoError -> Bool)
-> (CryptoError -> CryptoError -> Bool) -> Eq CryptoError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CryptoError -> CryptoError -> Bool
== :: CryptoError -> CryptoError -> Bool
$c/= :: CryptoError -> CryptoError -> Bool
/= :: CryptoError -> CryptoError -> Bool
Eq,Int -> CryptoError
CryptoError -> Int
CryptoError -> [CryptoError]
CryptoError -> CryptoError
CryptoError -> CryptoError -> [CryptoError]
CryptoError -> CryptoError -> CryptoError -> [CryptoError]
(CryptoError -> CryptoError)
-> (CryptoError -> CryptoError)
-> (Int -> CryptoError)
-> (CryptoError -> Int)
-> (CryptoError -> [CryptoError])
-> (CryptoError -> CryptoError -> [CryptoError])
-> (CryptoError -> CryptoError -> [CryptoError])
-> (CryptoError -> CryptoError -> CryptoError -> [CryptoError])
-> Enum 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
$csucc :: CryptoError -> CryptoError
succ :: CryptoError -> CryptoError
$cpred :: CryptoError -> CryptoError
pred :: CryptoError -> CryptoError
$ctoEnum :: Int -> CryptoError
toEnum :: Int -> CryptoError
$cfromEnum :: CryptoError -> Int
fromEnum :: CryptoError -> Int
$cenumFrom :: CryptoError -> [CryptoError]
enumFrom :: CryptoError -> [CryptoError]
$cenumFromThen :: CryptoError -> CryptoError -> [CryptoError]
enumFromThen :: CryptoError -> CryptoError -> [CryptoError]
$cenumFromTo :: CryptoError -> CryptoError -> [CryptoError]
enumFromTo :: CryptoError -> CryptoError -> [CryptoError]
$cenumFromThenTo :: CryptoError -> CryptoError -> CryptoError -> [CryptoError]
enumFromThenTo :: CryptoError -> CryptoError -> CryptoError -> [CryptoError]
Enum,Typeable CryptoError
Typeable CryptoError =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> CryptoError -> c CryptoError)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c CryptoError)
-> (CryptoError -> Constr)
-> (CryptoError -> DataType)
-> (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))
-> ((forall b. Data b => b -> b) -> CryptoError -> CryptoError)
-> (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 u. (forall d. Data d => d -> u) -> CryptoError -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> CryptoError -> u)
-> (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 (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> CryptoError -> m CryptoError)
-> Data CryptoError
CryptoError -> Constr
CryptoError -> DataType
(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)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> CryptoError -> c CryptoError
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> CryptoError -> c CryptoError
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c CryptoError
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c CryptoError
$ctoConstr :: CryptoError -> Constr
toConstr :: CryptoError -> Constr
$cdataTypeOf :: CryptoError -> DataType
dataTypeOf :: CryptoError -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c CryptoError)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c CryptoError)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c CryptoError)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c CryptoError)
$cgmapT :: (forall b. Data b => b -> b) -> CryptoError -> CryptoError
gmapT :: (forall b. Data b => b -> b) -> CryptoError -> CryptoError
$cgmapQl :: 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
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> CryptoError -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> CryptoError -> r
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> CryptoError -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> CryptoError -> [u]
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> CryptoError -> u
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> CryptoError -> u
$cgmapM :: forall (m :: * -> *).
Monad 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
$cgmapMp :: 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
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> CryptoError -> m CryptoError
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> CryptoError -> m 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
[CryptoFailable a] -> ShowS
CryptoFailable a -> String
(Int -> CryptoFailable a -> ShowS)
-> (CryptoFailable a -> String)
-> ([CryptoFailable a] -> ShowS)
-> Show (CryptoFailable a)
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
$cshowsPrec :: forall a. Show a => Int -> CryptoFailable a -> ShowS
showsPrec :: Int -> CryptoFailable a -> ShowS
$cshow :: forall a. Show a => CryptoFailable a -> String
show :: CryptoFailable a -> String
$cshowList :: forall a. Show a => [CryptoFailable a] -> ShowS
showList :: [CryptoFailable a] -> ShowS
Show)

instance Eq a => Eq (CryptoFailable a) where
    == :: CryptoFailable a -> CryptoFailable a -> Bool
(==) (CryptoPassed a
a)  (CryptoPassed a
b)  = a
a a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
b
    (==) (CryptoFailed CryptoError
e1) (CryptoFailed CryptoError
e2) = CryptoError
e1 CryptoError -> CryptoError -> Bool
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) = b -> CryptoFailable b
forall a. a -> CryptoFailable a
CryptoPassed (a -> b
f a
a)
    fmap a -> b
_ (CryptoFailed CryptoError
r) = CryptoError -> CryptoFailable b
forall a. CryptoError -> CryptoFailable a
CryptoFailed CryptoError
r

instance Applicative CryptoFailable where
    pure :: forall a. a -> CryptoFailable a
pure a
a     = a -> CryptoFailable 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 CryptoFailable (a -> b)
-> ((a -> b) -> CryptoFailable b) -> CryptoFailable b
forall a b.
CryptoFailable a -> (a -> CryptoFailable b) -> CryptoFailable b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a -> b
p -> CryptoFailable a
m CryptoFailable a -> (a -> CryptoFailable b) -> CryptoFailable b
forall a b.
CryptoFailable a -> (a -> CryptoFailable b) -> CryptoFailable b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a
r2 -> b -> CryptoFailable b
forall a. a -> CryptoFailable a
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 = a -> CryptoFailable a
forall a. a -> CryptoFailable a
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 -> CryptoError -> CryptoFailable b
forall a. CryptoError -> CryptoFailable a
CryptoFailed CryptoError
e

instance MonadFailure CryptoFailable where
    type Failure CryptoFailable = CryptoError
    mFail :: Failure CryptoFailable -> CryptoFailable ()
mFail = Failure CryptoFailable -> CryptoFailable ()
CryptoError -> CryptoFailable ()
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) = CryptoError -> IO a
forall e a. Exception e => e -> IO a
E.throwIO CryptoError
e
throwCryptoErrorIO (CryptoPassed a
r) = a -> IO a
forall a. a -> IO a
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) = CryptoError -> a
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) = CryptoError -> Either CryptoError a
forall a b. a -> Either a b
Left CryptoError
e
eitherCryptoError (CryptoPassed a
a) = a -> Either CryptoError 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
_) = Maybe a
forall a. Maybe a
Nothing
maybeCryptoError (CryptoPassed a
r) = a -> Maybe a
forall a. a -> Maybe a
Just a
r