-- SPDX-FileCopyrightText: 2020 Serokell
--
-- SPDX-License-Identifier: MPL-2.0

-- | Generate cryptographically-secure random data.
module Crypto.Random
  ( generate
  ) where

import Data.ByteArray (ByteArray)
import Data.ByteArray.Sized (SizedByteArray, alloc)
import Data.Proxy (Proxy (Proxy))
import GHC.TypeLits (KnownNat, natVal)

import qualified Libsodium as Na


-- | Generate a sequence of cryptographically-secure random bytes.
--
-- The output of this function is suitable to generate secret keys.
--
-- Note: This function is not thread-safe until Sodium is initialised.
-- See "Crypto.Init" for details.
generate
  :: forall ba n. (ByteArray ba, KnownNat n)
  => IO (SizedByteArray n ba)
generate :: IO (SizedByteArray n ba)
generate = (Ptr Any -> IO ()) -> IO (SizedByteArray n ba)
forall (n :: Nat) ba p.
(ByteArrayN n ba, KnownNat n) =>
(Ptr p -> IO ()) -> IO ba
alloc ((Ptr Any -> IO ()) -> IO (SizedByteArray n ba))
-> (Ptr Any -> IO ()) -> IO (SizedByteArray n ba)
forall a b. (a -> b) -> a -> b
$ \Ptr Any
bytesPtr ->
    Ptr Any -> (Any ::: CSize) -> IO ()
forall k1 k2 (buf :: k1) x (size :: k2).
(buf ::: Ptr x) -> (Any ::: CSize) -> IO ()
Na.randombytes_buf Ptr Any
bytesPtr Any ::: CSize
len
  where
    len :: Any ::: CSize
len = Integer -> Any ::: CSize
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Integer -> Any ::: CSize) -> Integer -> Any ::: CSize
forall a b. (a -> b) -> a -> b
$ Proxy n -> Integer
forall (n :: Nat) (proxy :: Nat -> *).
KnownNat n =>
proxy n -> Integer
natVal (Proxy n
forall k (t :: k). Proxy t
Proxy :: Proxy n)