commit 757702ac7cfd85759a08f1934cc4d29216f9d643
parent dcdcd23c0d8b32a17c4cce0960f2a987c700e1aa
Author: Jared Tobin <jared@jtobin.io>
Date: Sat, 14 Nov 2026 21:24:43 +0400
lib: zero seed copies and the C stack
'new' and 'reseed' concatenated their seed material into ordinary heap
bytestrings, which outlived 'wipe' and from which every DRBG state
could be recomputed; they now assemble it in a pinned buffer that is
zeroed after use. 'new', 'reseed', 'gen', and 'wipe' also zero the C
stack region below them, where C code run on the DRBG's behalf can
leave copies of its state. 'wipe' notes that a wiped DRBG must not be
used.
Diffstat:
5 files changed, 88 insertions(+), 9 deletions(-)
diff --git a/cbits/hmac_drbg.c b/cbits/hmac_drbg.c
@@ -0,0 +1,17 @@
+#include <stdint.h>
+#include <string.h>
+
+/*
+ * Zero the C stack region below the caller. C code that runs on
+ * behalf of a DRBG, e.g. the HMAC kernels, or C code that saves the
+ * registers in which GHC holds DRBG state, may leave copies of that
+ * state there. The region comfortably exceeds those kernels' frames.
+ */
+#define BURN_BYTES 4096
+
+void hmac_drbg_burn_stack(void) {
+ uint8_t buf[BURN_BYTES];
+ memset(buf, 0, sizeof buf);
+ /* keep the otherwise dead stores */
+ __asm__ __volatile__("" : : "r"(buf) : "memory");
+}
diff --git a/lib/Crypto/DRBG/HMAC/Internal.hs b/lib/Crypto/DRBG/HMAC/Internal.hs
@@ -1,4 +1,5 @@
{-# OPTIONS_HADDOCK hide #-}
+{-# LANGUAGE BangPatterns #-}
-- |
-- Module: Crypto.DRBG.HMAC.Internal
@@ -12,9 +13,19 @@ module Crypto.DRBG.HMAC.Internal (
Error(..)
, _RESEED_COUNTER
, _MAX_BYTES
+ , _with_concat
+ , _burn_stack
) where
-import Data.Word (Word64)
+import Control.Monad.Primitive (PrimMonad)
+import qualified Control.Monad.Primitive as Prim (unsafeIOToPrim)
+import qualified Data.ByteString as BS
+import qualified Data.ByteString.Internal as BI
+import qualified Data.ByteString.Unsafe as BU
+import Data.Word (Word8, Word64)
+import Foreign.ForeignPtr (withForeignPtr)
+import Foreign.Marshal.Utils (copyBytes, fillBytes)
+import Foreign.Ptr (Ptr, castPtr, plusPtr)
-- | A DRBG error.
data Error =
@@ -31,3 +42,38 @@ _RESEED_COUNTER = (2 :: Word64) ^ (48 :: Word64)
_MAX_BYTES :: Word64
_MAX_BYTES = 0x10000
{-# INLINE _MAX_BYTES #-}
+
+-- | Run an action on the concatenation of the given strings, held in a
+-- pinned buffer that is zeroed once the action completes, so that
+-- (secret) seed material leaves no copy of the library's making
+-- behind. The action must not retain its argument.
+_with_concat
+ :: PrimMonad m
+ => [BS.ByteString]
+ -> (BS.ByteString -> m a)
+ -> m a
+_with_concat bss act = do
+ let !n = sum (fmap BS.length bss)
+ fp <- Prim.unsafeIOToPrim $ do
+ fp <- BI.mallocByteString n
+ withForeignPtr fp $ \dst -> copy_all dst bss
+ pure fp
+ !r <- act (BI.BS fp n)
+ Prim.unsafeIOToPrim $ withForeignPtr fp $ \p -> fillBytes p 0 n
+ pure r
+ where
+ copy_all :: Ptr Word8 -> [BS.ByteString] -> IO ()
+ copy_all _ [] = pure ()
+ copy_all dst (b@(BI.PS _ _ l) : bs) = do
+ BU.unsafeUseAsCString b $ \src -> copyBytes dst (castPtr src) l
+ copy_all (dst `plusPtr` l) bs
+{-# INLINE _with_concat #-}
+
+foreign import ccall unsafe "hmac_drbg_burn_stack"
+ c_hmac_drbg_burn_stack :: IO ()
+
+-- | Zero the C stack region below the caller, where C code run on a
+-- DRBG's behalf may have left copies of its state.
+_burn_stack :: PrimMonad m => m ()
+_burn_stack = Prim.unsafeIOToPrim c_hmac_drbg_burn_stack
+{-# INLINE _burn_stack #-}
diff --git a/lib/Crypto/DRBG/HMAC/SHA256.hs b/lib/Crypto/DRBG/HMAC/SHA256.hs
@@ -29,7 +29,8 @@ module Crypto.DRBG.HMAC.SHA256 (
, _read_k
) where
-import Crypto.DRBG.HMAC.Internal (Error(..), _RESEED_COUNTER, _MAX_BYTES)
+import Crypto.DRBG.HMAC.Internal
+ (Error(..), _RESEED_COUNTER, _MAX_BYTES, _with_concat, _burn_stack)
import qualified Crypto.Hash.SHA256 as SHA256
import Crypto.Hash.SHA256.Internal (Registers(..))
import qualified Crypto.Hash.SHA256.Internal as SHA256 (cat)
@@ -87,7 +88,8 @@ new entropy nonce ps = do
PA.setPrimArray drbg 02 08 (0x00000000 :: Word32) -- init k
PA.setPrimArray drbg 10 08 (0x01010101 :: Word32) -- init v
PA.setPrimArray drbg 18 16 (0x00000000 :: Word32) -- scratch
- update drbg (entropy <> nonce <> ps)
+ _with_concat [entropy, nonce, ps] (update drbg)
+ _burn_stack
pure $! DRBG drbg
{-# INLINABLE new #-}
{-# SPECIALIZE new
@@ -118,8 +120,9 @@ reseed
-> BS.ByteString
-> m ()
reseed (DRBG drbg) entr addl = do
- update drbg (entr <> addl)
+ _with_concat [entr, addl] (update drbg)
init_counter drbg
+ _burn_stack
{-# INLINE reseed #-}
-- | Generate bytes from a DRBG, optionally injecting additional bytes
@@ -172,6 +175,7 @@ gen (DRBG drbg) addl@(BI.PS _ _ l) bytes
!res <- gen_loop drbg k0 v0 bytes
update drbg addl
write_counter drbg (ctr + 1)
+ _burn_stack
pure $! Right res
{-# INLINABLE gen #-}
{-# SPECIALIZE gen
@@ -184,7 +188,9 @@ gen (DRBG drbg) addl@(BI.PS _ _ l) bytes
-- | Wipe the state of a DRBG.
--
-- You should call this when you're finished with a DRBG to ensure that its
--- state is wiped from memory.
+-- state is wiped from memory. A wiped DRBG must not be used afterwards:
+-- its state is reset to the publicly known initial one, so it would
+-- produce predictable output.
--
-- >>> drbg <- new mempty mempty mempty
-- >>> Right bytes <- gen drbg addl_bytes 16
@@ -199,6 +205,7 @@ wipe (DRBG drbg) = do
PA.setPrimArray drbg 02 08 (0x00000000 :: Word32) -- init k
PA.setPrimArray drbg 10 08 (0x01010101 :: Word32) -- init v
PA.setPrimArray drbg 18 16 (0x00000000 :: Word32) -- init scratch
+ _burn_stack
{-# INLINE wipe #-}
-- utilities ------------------------------------------------------------------
diff --git a/lib/Crypto/DRBG/HMAC/SHA512.hs b/lib/Crypto/DRBG/HMAC/SHA512.hs
@@ -29,7 +29,8 @@ module Crypto.DRBG.HMAC.SHA512 (
, _read_k
) where
-import Crypto.DRBG.HMAC.Internal (Error(..), _RESEED_COUNTER, _MAX_BYTES)
+import Crypto.DRBG.HMAC.Internal
+ (Error(..), _RESEED_COUNTER, _MAX_BYTES, _with_concat, _burn_stack)
import qualified Crypto.Hash.SHA512 as SHA512
import Crypto.Hash.SHA512.Internal (Registers(..))
import qualified Crypto.Hash.SHA512.Internal as SHA512 (cat)
@@ -87,7 +88,8 @@ new entropy nonce ps = do
PA.setPrimArray drbg 01 08 (0x0000000000000000 :: Word64) -- init k
PA.setPrimArray drbg 09 08 (0x0101010101010101 :: Word64) -- init v
PA.setPrimArray drbg 17 16 (0x0000000000000000 :: Word64) -- scratch
- update drbg (entropy <> nonce <> ps)
+ _with_concat [entropy, nonce, ps] (update drbg)
+ _burn_stack
pure $! DRBG drbg
{-# INLINABLE new #-}
{-# SPECIALIZE new
@@ -118,8 +120,9 @@ reseed
-> BS.ByteString
-> m ()
reseed (DRBG drbg) entr addl = do
- update drbg (entr <> addl)
+ _with_concat [entr, addl] (update drbg)
init_counter drbg
+ _burn_stack
{-# INLINE reseed #-}
-- | Generate bytes from a DRBG, optionally injecting additional bytes
@@ -172,6 +175,7 @@ gen (DRBG drbg) addl@(BI.PS _ _ l) bytes
!res <- gen_loop drbg k0 v0 bytes
update drbg addl
write_counter drbg (ctr + 1)
+ _burn_stack
pure $! Right res
{-# INLINABLE gen #-}
{-# SPECIALIZE gen
@@ -184,7 +188,9 @@ gen (DRBG drbg) addl@(BI.PS _ _ l) bytes
-- | Wipe the state of a DRBG.
--
-- You should call this when you're finished with a DRBG to ensure that its
--- state is wiped from memory.
+-- state is wiped from memory. A wiped DRBG must not be used afterwards:
+-- its state is reset to the publicly known initial one, so it would
+-- produce predictable output.
--
-- >>> drbg <- new mempty mempty mempty
-- >>> Right bytes <- gen drbg addl_bytes 16
@@ -199,6 +205,7 @@ wipe (DRBG drbg) = do
PA.setPrimArray drbg 01 08 (0x0000000000000000 :: Word64) -- init k
PA.setPrimArray drbg 09 08 (0x0101010101010101 :: Word64) -- init v
PA.setPrimArray drbg 17 16 (0x0000000000000000 :: Word64) -- init scratch
+ _burn_stack
{-# INLINE wipe #-}
-- drbg utilities -------------------------------------------------------------
diff --git a/ppad-hmac-drbg.cabal b/ppad-hmac-drbg.cabal
@@ -34,6 +34,8 @@ library
Crypto.DRBG.HMAC.Internal
, Crypto.DRBG.HMAC.SHA256
, Crypto.DRBG.HMAC.SHA512
+ c-sources:
+ cbits/hmac_drbg.c
build-depends:
base >= 4.9 && < 5
, bytestring >= 0.9 && < 0.13