module Crypto.Random.Entropy.Unix
( DevRandom
, DevURandom
) where
import Foreign.Ptr
import Data.Word (Word8)
import Crypto.Random.Entropy.Source
import Control.Exception
import System.Posix.Types (Fd)
import System.Posix.IO
type H = Fd
newtype DevRandom = DevRandom H
newtype DevURandom = DevURandom H
instance EntropySource DevRandom where
entropyOpen = fmap DevRandom `fmap` openDev "/dev/random"
entropyGather (DevRandom h) = gatherDevEntropy h
entropyClose (DevRandom h) = closeDev h
instance EntropySource DevURandom where
entropyOpen = fmap DevURandom `fmap` openDev "/dev/urandom"
entropyGather (DevURandom h) = gatherDevEntropy h
entropyClose (DevURandom h) = closeDev h
openDev :: String -> IO (Maybe H)
openDev filepath = (Just `fmap` openFd filepath ReadOnly Nothing fileFlags)
`catch` \(_ :: IOException) -> return Nothing
where fileFlags = defaultFileFlags { nonBlock = True }
closeDev :: H -> IO ()
closeDev h = closeFd h
gatherDevEntropy :: H -> Ptr Word8 -> Int -> IO Int
gatherDevEntropy fd ptr sz =
(fromIntegral `fmap` fdReadBuf fd ptr (fromIntegral sz))
`catch` \(_ :: IOException) -> return 0