Skip to content

Commit

Permalink
Cryptographic RNG for MLockedSeed
Browse files Browse the repository at this point in the history
  • Loading branch information
tdammers committed Aug 24, 2023
1 parent d6db7d9 commit 71974b9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
5 changes: 5 additions & 0 deletions cardano-crypto-class/src/Cardano/Crypto/Libsodium/C.hs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ module Cardano.Crypto.Libsodium.C (
c_crypto_sign_ed25519_detached,
c_crypto_sign_ed25519_verify_detached,
c_crypto_sign_ed25519_sk_to_pk,
-- * RNG
c_sodium_randombytes_buf,
-- * Helpers
c_sodium_compare,
-- * Constants
Expand Down Expand Up @@ -182,3 +184,6 @@ foreign import capi unsafe "sodium.h crypto_sign_ed25519_sk_to_pk" c_crypto_sign
--
-- <https://libsodium.gitbook.io/doc/helpers#comparing-large-numbers>
foreign import capi unsafe "sodium.h sodium_compare" c_sodium_compare :: Ptr a -> Ptr a -> CSize -> IO Int

-- | @void randombytes_buf(void * const buf, const size_t size);@
foreign import capi unsafe "sodium/randombytes.h randombytes_buf" c_sodium_randombytes_buf :: Ptr a -> CSize -> IO ()
21 changes: 19 additions & 2 deletions cardano-crypto-class/src/Cardano/Crypto/Libsodium/MLockedSeed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

module Cardano.Crypto.Libsodium.MLockedSeed
where
Expand All @@ -20,12 +21,16 @@ import Cardano.Crypto.Libsodium.Memory (
MLockedAllocator,
mlockedMalloc,
)
import Cardano.Crypto.Libsodium.C (
c_sodium_randombytes_buf,
)
import Cardano.Foreign (SizedPtr)
import Control.DeepSeq (NFData)
import Control.Monad.Class.MonadST (MonadST)
import Data.Proxy (Proxy (..))
import Data.Word (Word8)
import Foreign.Ptr (Ptr)
import GHC.TypeNats (KnownNat)
import GHC.TypeNats (KnownNat, natVal)
import NoThunks.Class (NoThunks)

-- | A seed of size @n@, stored in mlocked memory. This is required to prevent
Expand Down Expand Up @@ -66,6 +71,18 @@ mlockedSeedNewZeroWith :: (KnownNat n, MonadST m) => MLockedAllocator m -> m (ML
mlockedSeedNewZeroWith allocator =
MLockedSeed <$> mlsbNewZeroWith allocator

mlockedSeedNewRandom :: forall n. (KnownNat n) => IO (MLockedSeed n)
mlockedSeedNewRandom = mlockedSeedNewRandomWith mlockedMalloc

mlockedSeedNewRandomWith :: forall n. (KnownNat n) => MLockedAllocator IO -> IO (MLockedSeed n)
mlockedSeedNewRandomWith allocator = do
mls <- MLockedSeed <$> mlsbNewZeroWith allocator
mlockedSeedUseAsCPtr mls $ \dst -> do
c_sodium_randombytes_buf dst size
return mls
where
size = fromIntegral $ natVal (Proxy @n)

mlockedSeedFinalize :: (MonadST m) => MLockedSeed n -> m ()
mlockedSeedFinalize = mlsbFinalize . mlockedSeedMLSB

Expand Down

0 comments on commit 71974b9

Please sign in to comment.