module Botan.Low.RNG
(
RNG(..)
, RNGType(..)
, withRNG
, rngInit
, rngDestroy
, rngGet
, systemRNGGet
, rngReseed
, rngReseedFromRNG
, rngAddEntropy
, pattern SystemRNG
, pattern UserRNG
, pattern UserThreadsafeRNG
, pattern RDRandRNG
) where
import qualified Data.ByteString as ByteString
import Botan.Bindings.RNG
import Botan.Low.Error ( throwBotanIfNegative_ )
import Botan.Low.Make ( mkWithTemp1 )
import Botan.Low.Remake
import Botan.Low.Prelude
newtype RNG = MkRNG { RNG -> ForeignPtr BotanRNGStruct
getRNGForeignPtr :: ForeignPtr BotanRNGStruct }
newRNG :: BotanRNG -> IO RNG
withRNG :: RNG -> (BotanRNG -> IO a) -> IO a
rngDestroy :: RNG -> IO ()
createRNG :: (Ptr BotanRNG -> IO CInt) -> IO RNG
(BotanRNG -> IO RNG
newRNG, RNG -> (BotanRNG -> IO a) -> IO a
withRNG, RNG -> IO ()
rngDestroy, (Ptr BotanRNG -> IO CInt) -> IO RNG
createRNG, (Ptr BotanRNG -> Ptr CSize -> IO CInt) -> IO [RNG]
_)
= (Ptr BotanRNGStruct -> BotanRNG)
-> (BotanRNG -> Ptr BotanRNGStruct)
-> (ForeignPtr BotanRNGStruct -> RNG)
-> (RNG -> ForeignPtr BotanRNGStruct)
-> FinalizerPtr BotanRNGStruct
-> (BotanRNG -> IO RNG, RNG -> (BotanRNG -> IO a) -> IO a,
RNG -> IO (), (Ptr BotanRNG -> IO CInt) -> IO RNG,
(Ptr BotanRNG -> Ptr CSize -> IO CInt) -> IO [RNG])
forall botan struct object a.
Storable botan =>
(Ptr struct -> botan)
-> (botan -> Ptr struct)
-> (ForeignPtr struct -> object)
-> (object -> ForeignPtr struct)
-> FinalizerPtr struct
-> (botan -> IO object, object -> (botan -> IO a) -> IO a,
object -> IO (), (Ptr botan -> IO CInt) -> IO object,
(Ptr botan -> Ptr CSize -> IO CInt) -> IO [object])
mkBindings Ptr BotanRNGStruct -> BotanRNG
MkBotanRNG BotanRNG -> Ptr BotanRNGStruct
runBotanRNG ForeignPtr BotanRNGStruct -> RNG
MkRNG RNG -> ForeignPtr BotanRNGStruct
getRNGForeignPtr FinalizerPtr BotanRNGStruct
botan_rng_destroy
type RNGType = ByteString
pattern SystemRNG
, UserRNG
, UserThreadsafeRNG
, RDRandRNG
:: RNGType
pattern $mSystemRNG :: forall {r}. RNGType -> ((# #) -> r) -> ((# #) -> r) -> r
$bSystemRNG :: RNGType
SystemRNG = BOTAN_RNG_TYPE_SYSTEM
pattern $mUserRNG :: forall {r}. RNGType -> ((# #) -> r) -> ((# #) -> r) -> r
$bUserRNG :: RNGType
UserRNG = BOTAN_RNG_TYPE_USER
pattern $mUserThreadsafeRNG :: forall {r}. RNGType -> ((# #) -> r) -> ((# #) -> r) -> r
$bUserThreadsafeRNG :: RNGType
UserThreadsafeRNG = BOTAN_RNG_TYPE_USER_THREADSAFE
pattern $mRDRandRNG :: forall {r}. RNGType -> ((# #) -> r) -> ((# #) -> r) -> r
$bRDRandRNG :: RNGType
RDRandRNG = BOTAN_RNG_TYPE_RDRAND
rngInit
:: RNGType
-> IO RNG
rngInit :: RNGType -> IO RNG
rngInit = ((Ptr BotanRNG -> IO CInt) -> IO RNG)
-> (Ptr BotanRNG -> ConstPtr CChar -> IO CInt) -> RNGType -> IO RNG
forall botan object.
((Ptr botan -> IO CInt) -> IO object)
-> (Ptr botan -> ConstPtr CChar -> IO CInt) -> RNGType -> IO object
mkCreateObjectCString (Ptr BotanRNG -> IO CInt) -> IO RNG
createRNG Ptr BotanRNG -> ConstPtr CChar -> IO CInt
botan_rng_init
withRNGInit :: RNGType -> (RNG -> IO a) -> IO a
withRNGInit :: forall a. RNGType -> (RNG -> IO a) -> IO a
withRNGInit = (RNGType -> IO RNG)
-> (RNG -> IO ()) -> RNGType -> (RNG -> IO a) -> IO a
forall x t a.
(x -> IO t) -> (t -> IO ()) -> x -> (t -> IO a) -> IO a
mkWithTemp1 RNGType -> IO RNG
rngInit RNG -> IO ()
rngDestroy
rngGet
:: RNG
-> Int
-> IO ByteString
rngGet :: RNG -> Int -> IO RNGType
rngGet RNG
rng Int
len = RNG -> (BotanRNG -> IO RNGType) -> IO RNGType
forall a. RNG -> (BotanRNG -> IO a) -> IO a
withRNG RNG
rng ((BotanRNG -> IO RNGType) -> IO RNGType)
-> (BotanRNG -> IO RNGType) -> IO RNGType
forall a b. (a -> b) -> a -> b
$ \ BotanRNG
botanRNG -> do
Int -> (Ptr Word8 -> IO ()) -> IO RNGType
forall byte. Int -> (Ptr byte -> IO ()) -> IO RNGType
allocBytes Int
len ((Ptr Word8 -> IO ()) -> IO RNGType)
-> (Ptr Word8 -> IO ()) -> IO RNGType
forall a b. (a -> b) -> a -> b
$ \ Ptr Word8
bytesPtr -> do
HasCallStack => IO CInt -> IO ()
IO CInt -> IO ()
throwBotanIfNegative_ (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ BotanRNG -> Ptr Word8 -> CSize -> IO CInt
botan_rng_get BotanRNG
botanRNG Ptr Word8
bytesPtr (Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len)
systemRNGGet
:: Int
-> IO ByteString
systemRNGGet :: Int -> IO RNGType
systemRNGGet Int
len = Int -> (Ptr Word8 -> IO ()) -> IO RNGType
forall byte. Int -> (Ptr byte -> IO ()) -> IO RNGType
allocBytes Int
len ((Ptr Word8 -> IO ()) -> IO RNGType)
-> (Ptr Word8 -> IO ()) -> IO RNGType
forall a b. (a -> b) -> a -> b
$ \ Ptr Word8
bytesPtr -> do
HasCallStack => IO CInt -> IO ()
IO CInt -> IO ()
throwBotanIfNegative_ (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ Ptr Word8 -> CSize -> IO CInt
botan_system_rng_get Ptr Word8
bytesPtr (Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len)
rngReseed
:: RNG
-> Int
-> IO ()
rngReseed :: RNG -> Int -> IO ()
rngReseed RNG
rng Int
bits = RNG -> (BotanRNG -> IO ()) -> IO ()
forall a. RNG -> (BotanRNG -> IO a) -> IO a
withRNG RNG
rng ((BotanRNG -> IO ()) -> IO ()) -> (BotanRNG -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ BotanRNG
botanRNG -> do
HasCallStack => IO CInt -> IO ()
IO CInt -> IO ()
throwBotanIfNegative_ (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ BotanRNG -> CSize -> IO CInt
botan_rng_reseed BotanRNG
botanRNG (Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
bits)
rngReseedFromRNG
:: RNG
-> RNG
-> Int
-> IO ()
rngReseedFromRNG :: RNG -> RNG -> Int -> IO ()
rngReseedFromRNG RNG
rng RNG
source Int
bits = RNG -> (BotanRNG -> IO ()) -> IO ()
forall a. RNG -> (BotanRNG -> IO a) -> IO a
withRNG RNG
rng ((BotanRNG -> IO ()) -> IO ()) -> (BotanRNG -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ BotanRNG
botanRNG -> do
RNG -> (BotanRNG -> IO ()) -> IO ()
forall a. RNG -> (BotanRNG -> IO a) -> IO a
withRNG RNG
source ((BotanRNG -> IO ()) -> IO ()) -> (BotanRNG -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ BotanRNG
sourcePtr -> do
HasCallStack => IO CInt -> IO ()
IO CInt -> IO ()
throwBotanIfNegative_ (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ BotanRNG -> BotanRNG -> CSize -> IO CInt
botan_rng_reseed_from_rng BotanRNG
botanRNG BotanRNG
sourcePtr (Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
bits)
rngAddEntropy
:: RNG
-> ByteString
-> IO ()
rngAddEntropy :: RNG -> RNGType -> IO ()
rngAddEntropy RNG
rng RNGType
bytes = RNG -> (BotanRNG -> IO ()) -> IO ()
forall a. RNG -> (BotanRNG -> IO a) -> IO a
withRNG RNG
rng ((BotanRNG -> IO ()) -> IO ()) -> (BotanRNG -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ BotanRNG
botanRNG -> do
RNGType -> (Ptr Word8 -> CSize -> IO ()) -> IO ()
forall byte a. RNGType -> (Ptr byte -> CSize -> IO a) -> IO a
asBytesLen RNGType
bytes ((Ptr Word8 -> CSize -> IO ()) -> IO ())
-> (Ptr Word8 -> CSize -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \ Ptr Word8
bytesPtr CSize
bytesLen -> do
HasCallStack => IO CInt -> IO ()
IO CInt -> IO ()
throwBotanIfNegative_ (IO CInt -> IO ()) -> IO CInt -> IO ()
forall a b. (a -> b) -> a -> b
$ BotanRNG -> ConstPtr Word8 -> CSize -> IO CInt
botan_rng_add_entropy BotanRNG
botanRNG (Ptr Word8 -> ConstPtr Word8
forall a. Ptr a -> ConstPtr a
ConstPtr Ptr Word8
bytesPtr) CSize
bytesLen