{-# LANGUAGE ForeignFunctionInterface #-}
module Crypto.Random.Entropy.SysRandom (
SysRandom,
) where
import Crypto.Random.Entropy.Source
import Data.Word (Word8)
import Foreign.C.Types
import Foreign.Ptr
foreign import ccall safe "crypton_sysrandom_available"
c_sysrandom_available :: IO CInt
foreign import ccall safe "crypton_sysrandom_bytes"
c_sysrandom_bytes :: Ptr Word8 -> CInt -> IO CInt
data SysRandom = SysRandom
instance EntropySource SysRandom where
entropyOpen :: IO (Maybe SysRandom)
entropyOpen = CInt -> Maybe SysRandom
forall {a}. (Eq a, Num a) => a -> Maybe SysRandom
available (CInt -> Maybe SysRandom) -> IO CInt -> IO (Maybe SysRandom)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` IO CInt
c_sysrandom_available
where
available :: a -> Maybe SysRandom
available a
0 = Maybe SysRandom
forall a. Maybe a
Nothing
available a
_ = SysRandom -> Maybe SysRandom
forall a. a -> Maybe a
Just SysRandom
SysRandom
entropyGather :: SysRandom -> Ptr Word8 -> Int -> IO Int
entropyGather SysRandom
_ Ptr Word8
ptr Int
n =
CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CInt -> Int) -> IO CInt -> IO Int
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Ptr Word8 -> CInt -> IO CInt
c_sysrandom_bytes Ptr Word8
ptr (Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n)
entropyClose :: SysRandom -> IO ()
entropyClose SysRandom
_ = () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()