{-# LANGUAGE ForeignFunctionInterface #-}
module Crypto.Random.Entropy.RDRand
( RDRand
) where
import Foreign.Ptr
import Foreign.C.Types
import Data.Word (Word8)
import Crypto.Random.Entropy.Source
foreign import ccall unsafe "cryptonite_cpu_has_rdrand"
c_cpu_has_rdrand :: IO CInt
foreign import ccall unsafe "cryptonite_get_rand_bytes"
c_get_rand_bytes :: Ptr Word8 -> CInt -> IO CInt
data RDRand = RDRand
instance EntropySource RDRand where
entropyOpen :: IO (Maybe RDRand)
entropyOpen = IO (Maybe RDRand)
rdrandGrab
entropyGather :: RDRand -> Ptr Word8 -> Int -> IO Int
entropyGather RDRand
_ = Ptr Word8 -> Int -> IO Int
rdrandGetBytes
entropyClose :: RDRand -> IO ()
entropyClose RDRand
_ = forall (m :: * -> *) a. Monad m => a -> m a
return ()
rdrandGrab :: IO (Maybe RDRand)
rdrandGrab :: IO (Maybe RDRand)
rdrandGrab = forall {a}. (Eq a, Num a) => a -> Maybe RDRand
supported forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` IO CInt
c_cpu_has_rdrand
where supported :: a -> Maybe RDRand
supported a
0 = forall a. Maybe a
Nothing
supported a
_ = forall a. a -> Maybe a
Just RDRand
RDRand
rdrandGetBytes :: Ptr Word8 -> Int -> IO Int
rdrandGetBytes :: Ptr Word8 -> Int -> IO Int
rdrandGetBytes Ptr Word8
ptr Int
sz = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Ptr Word8 -> CInt -> IO CInt
c_get_rand_bytes Ptr Word8
ptr (forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
sz)