tommd / drbg Goto Github PK
View Code? Open in Web Editor NEWDeterministic Random Bit Generators (English -> Haskell translation from SP 800-90)
License: Other
Deterministic Random Bit Generators (English -> Haskell translation from SP 800-90)
License: Other
The following program attempts to split a GenAutoReseed CtrDRBG SystemRandom
, which fails with NotEnoughEntropy
. For control, it first successfully splits a GenAutoReseed CtrDRBG CtrDRBG
.
{-# LANGUAGE PackageImports #-}
module Main (main) where
import qualified Crypto.Random.DRBG as DRBG
import "crypto-api" Crypto.Random
import qualified Data.ByteString.Base64 as B64
main :: IO ()
main = do
testSplit (undefined :: DRBG.CtrDRBG)
testSplit (undefined :: DRBG.SystemRandom)
testSplit :: CryptoRandomGen g => g -> IO ()
testSplit g = do
g1 <- genWithType g
putStrLn $ case splitGen g1 of
Left err -> "Error: " ++ show err
Right (g2, _) -> "OK: " ++ show (B64.encode $ fst $ throwLeft $ genBytes 20 g2)
genWithType :: CryptoRandomGen g => g -> IO (DRBG.GenAutoReseed DRBG.CtrDRBG g)
genWithType _ = DRBG.newGenAutoReseedIO (2^(48::Int))
The output is something along the lines of:
$ ghc -Wall Main.hs --make
[1 of 1] Compiling Main ( Main.hs, Main.o )
Linking Main ...
$ ./Main
OK: "hza/a+tHlWsGCJLyaC7WM2nHHog="
Error: NotEnoughEntropy
I've chased the issue as far as the value for genSeedLength :: Tagged SystemRandom Int
which is 2^63
:
$ ghci -XPackageImports
GHCi, version 7.8.3: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Prelude> import "crypto-api" Crypto.Random
Prelude Crypto.Random> genSeedLength :: Data.Tagged.Tagged Crypto.Random.DRBG.SystemRandom Int
Loading package array-0.5.0.0 ... linking ... done.
Loading package deepseq-1.3.0.2 ... linking ... done.
Loading package bytestring-0.10.4.0 ... linking ... done.
Loading package old-locale-1.0.0.6 ... linking ... done.
Loading package time-1.4.2 ... linking ... done.
Loading package unix-2.7.0.1 ... linking ... done.
Loading package containers-0.5.5.1 ... linking ... done.
Loading package cereal-0.4.1.0 ... linking ... done.
Loading package entropy-0.3.4.1 ... linking ... done.
Loading package tagged-0.7.3 ... linking ... done.
Loading package transformers-0.3.0.0 ... linking ... done.
Loading package crypto-api-0.13.2 ... linking ... done.
Tagged 9223372036854775807
Prelude Crypto.Random>
Leaving GHCi.
Not sure what to suggest at this point.
Crypto/Random/DRBG/HMAC.hs:13:8:
Could not find module ‘Data.Serialize.Builder’
Perhaps you meant
Data.Serialize.Get (from cereal-0.5.0.0@cerea_BBfptUfmSBYLBoW9Jgic7f)
Data.Serialize.Put (from cereal-0.5.0.0@cerea_BBfptUfmSBYLBoW9Jgic7f)
Use -v to see a list of the files searched for.
Hackage revision: http://hackage.haskell.org/package/DRBG-0.5.4/revisions/
Build matrix: http://matrix.hackage.haskell.org/package/DRBG (as of writing build has been queued)
Available at:
EOM
GenAutoReseed uses an Int for the period but should use Word64 to cope with sensible periods (larger than 2^29).
Use Word64, remove Integer, from State types. Unbox the word.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.