git.iko.soy/typeable/entropy
on main help sign in
Add some docs on the new getHardwareEntropy
commit6c7df016c5c199cbc95fbcf3be832966792315c8 authorThomas M. DuBuisson <tommd@galois.com> dateSun 31 Dec 2017, 10:55 (9 years ago) parent16309d3
4 files changed+54−22
System/Entropy.hs+43−11 System/EntropyNix.hs+4−4 System/EntropyWindows.hs+5−5 System/EntropyXen.hs+2−2
System/Entropy.hs+43−11
@@ -16,18 +16,18 @@
1616
1717module System.Entropy
1818 ( getEntropy,
19−#if defined(isWindows)
20− module System.EntropyWindows
19+ getHardwareEntropy,
20+ CryptHandle,
21+ openHandle,
22+ hGetEntropy,
23+ closeHandle
2124 ) where
25+#if defined(isWindows)
2226import System.EntropyWindows
2327#else
2428#ifdef XEN
25− module System.EntropyXen
26− ) where
2729import System.EntropyXen
2830#else
29− module System.EntropyNix
30− ) where
3131import System.EntropyNix
3232#endif
3333#endif
@@ -36,10 +36,42 @@ import qualified Data.ByteString as B
3636import Control.Exception (bracket)
3737
3838-- |Get a specific number of bytes of cryptographically
39−-- secure random data using the system-specific facilities.
39+-- secure random data using the *system-specific* sources.
40+-- (As of 0.4. Verions <0.4 mixed system and hardware sources)
4041--
41−-- Use RDRAND if available and XOR with '/dev/urandom' on *nix and CryptAPI when on
42−-- Windows. In short, this entropy is considered cryptographically secure
43−-- but not true entropy.
44−getEntropy :: Int -> IO B.ByteString
42+-- The returned random value is considered cryptographically secure but not true entropy.
43+--
44+-- On some platforms this requies a file handle which can lead to resource
45+-- exhaustion in some situations.
46+getEntropy :: Int -- ^ Number of bytes
47+ -> IO B.ByteString
4548getEntropy = bracket openHandle closeHandle . flip hGetEntropy
49+
50+-- |Get a specific number of bytes of cryptographically
51+-- secure random data using a supported *hardware* random bit generator.
52+--
53+-- If there is no hardware random number generator then @Nothing@ is returned.
54+-- If any call returns non-Nothing then it should never be @Nothing@ unless
55+-- there has been a hardware failure.
56+--
57+-- If trust of the CPU allows it and no context switching is important,
58+-- a bias to the hardware rng with system rng as fall back is trivial:
59+--
60+-- @
61+-- let fastRandom nr = maybe (getEntropy nr) pure =<< getHardwareEntropy nr
62+-- @
63+--
64+-- The old, @<0.4@, behavior is possible using @xor@ from 'Data.Bits':
65+--
66+-- @
67+-- let oldRandom nr =
68+-- do hwRnd <- maybe (replicate nr 0) BS.unpack <$> getHardwareEntropy nr
69+-- sysRnd <- BS.unpack <$> getEntropy nr
70+-- pure $ BS.pack $ zipWith xor sysRnd hwRnd
71+-- @
72+--
73+-- A less maliable mixing can be accomplished by replacing `xor` with a
74+-- composition of concat and cryptographic hash.
75+getHardwareEntropy :: Int -- ^ Number of bytes
76+ -> IO (Maybe B.ByteString)
77+getHardwareEntropy = hardwareRandom
System/EntropyNix.hs+4−4
@@ -13,7 +13,7 @@ module System.EntropyNix
1313 , openHandle
1414 , hGetEntropy
1515 , closeHandle
16− , hardwareRNG
16+ , hardwareRandom
1717 ) where
1818
1919import Control.Monad (liftM, when)
@@ -46,9 +46,9 @@ data CryptHandle
4646-- Supported hardware:
4747-- * RDRAND
4848-- * Patches welcome
49−hardwareRNG :: Int -> IO (Maybe B.ByteString)
49+hardwareRandom :: Int -> IO (Maybe B.ByteString)
5050#ifdef HAVE_RDRAND
51−hardwareRNG n =
51+hardwareRandom n =
5252 do b <- cpuHasRdRand
5353 if b
5454 then Just <$> B.create n (\ptr ->
@@ -56,7 +56,7 @@ hardwareRNG n =
5656 when (r /= 0) (fail "RDRand failed to gather entropy"))
5757 else pure Nothing
5858#else
59−hardwareRNG _ = pure Nothing
59+hardwareRandom _ = pure Nothing
6060#endif
6161
6262-- |Open a `CryptHandle`
System/EntropyWindows.hs+5−5
@@ -12,7 +12,7 @@ module System.EntropyWindows
1212 , openHandle
1313 , hGetEntropy
1414 , closeHandle
15− , hardwareRNG
15+ , hardwareRandom
1616 ) where
1717
1818import Control.Monad (liftM, when)
@@ -73,9 +73,9 @@ data CryptHandle
7373-- Supported hardware:
7474-- * RDRAND
7575-- * Patches welcome
76−hardwareRNG :: Int -> IO (Maybe B.ByteString)
76+hardwareRandom :: Int -> IO (Maybe B.ByteString)
7777#ifdef HAVE_RDRAND
78−hardwareRNG n =
78+hardwareRandom n =
7979 do b <- cpuHasRdRand
8080 if b
8181 then Just <$> BI.create n (\ptr ->
@@ -83,7 +83,7 @@ hardwareRNG n =
8383 when (r /= 0) (fail "RDRand failed to gather entropy"))
8484 else pure Nothing
8585#else
86−hardwareRNG _ = pure Nothing
86+hardwareRandom _ = pure Nothing
8787#endif
8888
8989-- Define the constants we need from WinCrypt.h
@@ -135,5 +135,5 @@ closeHandle :: CryptHandle -> IO ()
135135closeHandle (CH h) = cryptReleaseCtx h
136136
137137-- |Read from `CryptHandle`
138−hGetEntropy :: CryptHandle -> Int -> IO B.ByteString
138+hGetEntropy :: CryptHandle -> Int -> IO B.ByteString
139139hGetEntropy (CH h) n = cryptGenRandom h n
System/EntropyXen.hs+2−2
@@ -55,8 +55,8 @@ closeHandle UseRdRand = return ()
5555-- Supported hardware:
5656-- * RDRAND
5757-- * Patches welcome
58−hardwareRNG :: Int -> IO (Maybe B.ByteString)
59−hardwareRNG sz = Just <$> hGetEntropy UseRdRand sz
58+hardwareRandom :: Int -> IO (Maybe B.ByteString)
59+hardwareRandom sz = Just <$> hGetEntropy UseRdRand sz
6060
6161-- |Read random data from a `CryptHandle`, which uses RDRAND (when on Xen)
6262hGetEntropy :: CryptHandle -> Int -> IO B.ByteString