commit 8142ee29b9dde2a8c5ca481d15d4de5c769e45f3
parent 757702ac7cfd85759a08f1934cc4d29216f9d643
Author: Jared Tobin <jared@jtobin.io>
Date: Sat, 14 Nov 2026 21:34:13 +0400
lib: drop the C stack zeroing
Scans after 'wipe' found no DRBG state left on the C stack by C code:
the remaining fragments were words spilled by GHC's native code
generator, in the C stack space the RTS reserves for it (above the
stack pointer, out of a burn's reach) and on the Haskell stack, and
builds using the LLVM backend left none. The zeroing thus changed
nothing.
The README recommends the LLVM backend accordingly, and notes how
seed material and 'wipe' are handled; the version is bumped to 0.3.2.
Diffstat:
7 files changed, 18 insertions(+), 41 deletions(-)
diff --git a/CHANGELOG b/CHANGELOG
@@ -1,5 +1,10 @@
# Changelog
+- 0.3.2 (UNRELEASED)
+ * 'new' and 'reseed' combine their seed material in a pinned buffer
+ that is zeroed after use, rather than concatenating it into
+ ordinary heap bytestrings that could outlive 'wipe'.
+
- 0.3.1 (2026-02-04)
* Adds SPECIALIZE pragmas for IO and ST to improve the generated Core.
diff --git a/README.md b/README.md
@@ -114,7 +114,16 @@ DRBG internal state, which must be kept secret, is kept in a single,
pinned, heap-allocated mutable buffer. It is never copied, is guaranteed
never to be moved around by the garbage collector, and its components
are never allocated anywhere else on the heap. You should zero out the
-DRBG state via the 'wipe' function when you've finished using it.
+DRBG state via the 'wipe' function when you've finished using it, and
+not use the DRBG afterwards. Seed material passed to 'new' or 'reseed'
+is combined only in a pinned buffer that is zeroed after use; the
+caller's own copies are the caller's to manage.
+
+We recommend building with the LLVM backend (the 'llvm' flag). GHC's
+native code generator can spill individual words of the DRBG state to
+the Haskell stack, or to the C stack space it reserves for spills,
+where fragments of past states may persist; builds using the LLVM
+backend have shown no such fragments.
(The security properties of this library have been
examined and defended in more detail in a
diff --git a/cbits/hmac_drbg.c b/cbits/hmac_drbg.c
@@ -1,17 +0,0 @@
-#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
@@ -14,7 +14,6 @@ module Crypto.DRBG.HMAC.Internal (
, _RESEED_COUNTER
, _MAX_BYTES
, _with_concat
- , _burn_stack
) where
import Control.Monad.Primitive (PrimMonad)
@@ -68,12 +67,3 @@ _with_concat bss act = 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
@@ -30,7 +30,7 @@ module Crypto.DRBG.HMAC.SHA256 (
) where
import Crypto.DRBG.HMAC.Internal
- (Error(..), _RESEED_COUNTER, _MAX_BYTES, _with_concat, _burn_stack)
+ (Error(..), _RESEED_COUNTER, _MAX_BYTES, _with_concat)
import qualified Crypto.Hash.SHA256 as SHA256
import Crypto.Hash.SHA256.Internal (Registers(..))
import qualified Crypto.Hash.SHA256.Internal as SHA256 (cat)
@@ -89,7 +89,6 @@ new entropy nonce ps = do
PA.setPrimArray drbg 10 08 (0x01010101 :: Word32) -- init v
PA.setPrimArray drbg 18 16 (0x00000000 :: Word32) -- scratch
_with_concat [entropy, nonce, ps] (update drbg)
- _burn_stack
pure $! DRBG drbg
{-# INLINABLE new #-}
{-# SPECIALIZE new
@@ -122,7 +121,6 @@ reseed
reseed (DRBG drbg) entr addl = do
_with_concat [entr, addl] (update drbg)
init_counter drbg
- _burn_stack
{-# INLINE reseed #-}
-- | Generate bytes from a DRBG, optionally injecting additional bytes
@@ -175,7 +173,6 @@ 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
@@ -205,7 +202,6 @@ 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
@@ -30,7 +30,7 @@ module Crypto.DRBG.HMAC.SHA512 (
) where
import Crypto.DRBG.HMAC.Internal
- (Error(..), _RESEED_COUNTER, _MAX_BYTES, _with_concat, _burn_stack)
+ (Error(..), _RESEED_COUNTER, _MAX_BYTES, _with_concat)
import qualified Crypto.Hash.SHA512 as SHA512
import Crypto.Hash.SHA512.Internal (Registers(..))
import qualified Crypto.Hash.SHA512.Internal as SHA512 (cat)
@@ -89,7 +89,6 @@ new entropy nonce ps = do
PA.setPrimArray drbg 09 08 (0x0101010101010101 :: Word64) -- init v
PA.setPrimArray drbg 17 16 (0x0000000000000000 :: Word64) -- scratch
_with_concat [entropy, nonce, ps] (update drbg)
- _burn_stack
pure $! DRBG drbg
{-# INLINABLE new #-}
{-# SPECIALIZE new
@@ -122,7 +121,6 @@ reseed
reseed (DRBG drbg) entr addl = do
_with_concat [entr, addl] (update drbg)
init_counter drbg
- _burn_stack
{-# INLINE reseed #-}
-- | Generate bytes from a DRBG, optionally injecting additional bytes
@@ -175,7 +173,6 @@ 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
@@ -205,7 +202,6 @@ 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
@@ -1,6 +1,6 @@
cabal-version: 3.0
name: ppad-hmac-drbg
-version: 0.3.1
+version: 0.3.2
synopsis: HMAC-based deterministic random bit generator
license: MIT
license-file: LICENSE
@@ -34,8 +34,6 @@ 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