{-# LANGUAGE CPP #-}
{-# LANGUAGE ExistentialQuantification #-}
module Crypto.Random.Entropy.Backend
( EntropyBackend
, supportedBackends
, gatherBackend
) where
import Foreign.Ptr
import Data.Proxy
import Data.Word (Word8)
import Crypto.Random.Entropy.Source
#ifdef SUPPORT_RDRAND
import Crypto.Random.Entropy.RDRand
#endif
#ifdef WINDOWS
import Crypto.Random.Entropy.Windows
#else
import Crypto.Random.Entropy.Unix
#endif
supportedBackends :: [IO (Maybe EntropyBackend)]
supportedBackends :: [IO (Maybe EntropyBackend)]
supportedBackends =
[
#ifdef SUPPORT_RDRAND
forall b. EntropySource b => Proxy b -> IO (Maybe EntropyBackend)
openBackend (forall {k} (t :: k). Proxy t
Proxy :: Proxy RDRand),
#endif
#ifdef WINDOWS
openBackend (Proxy :: Proxy WinCryptoAPI)
#else
forall b. EntropySource b => Proxy b -> IO (Maybe EntropyBackend)
openBackend (forall {k} (t :: k). Proxy t
Proxy :: Proxy DevRandom), forall b. EntropySource b => Proxy b -> IO (Maybe EntropyBackend)
openBackend (forall {k} (t :: k). Proxy t
Proxy :: Proxy DevURandom)
#endif
]
data EntropyBackend = forall b . EntropySource b => EntropyBackend b
openBackend :: EntropySource b => Proxy b -> IO (Maybe EntropyBackend)
openBackend :: forall b. EntropySource b => Proxy b -> IO (Maybe EntropyBackend)
openBackend Proxy b
b = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall b. EntropySource b => b -> EntropyBackend
EntropyBackend forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` forall b. EntropySource b => Proxy b -> IO (Maybe b)
callOpen Proxy b
b
where callOpen :: EntropySource b => Proxy b -> IO (Maybe b)
callOpen :: forall b. EntropySource b => Proxy b -> IO (Maybe b)
callOpen Proxy b
_ = forall a. EntropySource a => IO (Maybe a)
entropyOpen
gatherBackend :: EntropyBackend
-> Ptr Word8
-> Int
-> IO Int
gatherBackend :: EntropyBackend -> Ptr Word8 -> Int -> IO Int
gatherBackend (EntropyBackend b
backend) Ptr Word8
ptr Int
n = forall a. EntropySource a => a -> Ptr Word8 -> Int -> IO Int
entropyGather b
backend Ptr Word8
ptr Int
n