hmac-drbg

Pure Haskell HMAC-DRBG (docs.ppad.tech/hmac-drbg).
git clone git://git.ppad.tech/hmac-drbg.git
Log | Files | Refs | README | LICENSE

commit 8baa4a16540069f9586205306919bdbb009f60d9
parent dcdcd23c0d8b32a17c4cce0960f2a987c700e1aa
Author: Jared Tobin <jared@jtobin.io>
Date:   Sat, 14 Nov 2026 21:34:22 +0400

Merge branch 'impl/scrub'

Stops the DRBG leaving copies of its seed material behind.

- 'new' and 'reseed' concatenated their seed material into ordinary
  heap bytestrings, which outlived 'wipe' and from which every DRBG
  state since instantiation could be recomputed. They now combine it
  in a pinned buffer that is zeroed after use; whole-process memory
  scans after 'wipe' found the library's copies gone.
- The same scans found fragments of past states (single spilled
  words) only in builds using GHC's native code generator, in the C
  stack space the RTS reserves for its spills and on the Haskell
  stack, and none with the LLVM backend. A C stack zeroing tried
  alongside reached none of them and was dropped; the README now
  recommends the LLVM backend.
- 'wipe' notes that a wiped DRBG must not be used, its state being
  the publicly known initial one.
- Version 0.3.2, unreleased.

Diffstat:
MCHANGELOG | 5+++++
MREADME.md | 11++++++++++-
Mlib/Crypto/DRBG/HMAC/Internal.hs | 38+++++++++++++++++++++++++++++++++++++-
Mlib/Crypto/DRBG/HMAC/SHA256.hs | 11+++++++----
Mlib/Crypto/DRBG/HMAC/SHA512.hs | 11+++++++----
Mppad-hmac-drbg.cabal | 2+-
6 files changed, 67 insertions(+), 11 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/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,18 @@ module Crypto.DRBG.HMAC.Internal ( Error(..) , _RESEED_COUNTER , _MAX_BYTES + , _with_concat ) 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 +41,29 @@ _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 #-} 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) 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,7 @@ 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) pure $! DRBG drbg {-# INLINABLE new #-} {-# SPECIALIZE new @@ -118,7 +119,7 @@ reseed -> BS.ByteString -> m () reseed (DRBG drbg) entr addl = do - update drbg (entr <> addl) + _with_concat [entr, addl] (update drbg) init_counter drbg {-# INLINE reseed #-} @@ -184,7 +185,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 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) 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,7 @@ 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) pure $! DRBG drbg {-# INLINABLE new #-} {-# SPECIALIZE new @@ -118,7 +119,7 @@ reseed -> BS.ByteString -> m () reseed (DRBG drbg) entr addl = do - update drbg (entr <> addl) + _with_concat [entr, addl] (update drbg) init_counter drbg {-# INLINE reseed #-} @@ -184,7 +185,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 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