git.iko.soy/typeable/entropy
on main help sign in
HaLVM support, sort of.
commit3bda5dff83f7d8a4270e413894f5a9cfe9420edc authorThomas M. DuBuisson <tommd@galois.com> dateWed 28 May 2014, 17:12 (12 years ago) parent1a8bf9e
3 files changed+59−19
README.md+2 System/Entropy.hs+42−14 entropy.cabal+15−5
README.md+2
@@ -3,3 +3,5 @@
33This package allows Haskell users to easily acquire entropy for use in
44critical security applications by calling out to either windows crypto api,
55unix/linux's `/dev/urandom`, or the RDRAND instruction.
6+
7+This package supports Windows, {li,u}nix, QNX, and has preliminary support for HaLVM.
System/Entropy.hs+42−14
@@ -4,22 +4,32 @@
44 Stability: beta
55 Portability: portable
66
7− Obtain entropy from system sources.
8− Currently, windows and *nix systems with a @/dev/urandom@ are supported.
7+ Obtain entropy from system sources or x86 RDRAND when available.
8+
9+ Currently supporting:
10+
11+ - Windows via CryptoAPO
12+ - *nix systems via @\/dev\/urandom@
13+ - QNX
14+ - Xen only when RDRAND is available.
915-}
1016
1117module System.Entropy
12− ( getEntropy
13− , CryptHandle
14− , openHandle
15− , hGetEntropy
16− , closeHandle
17− ) where
18−
19−import Control.Monad (liftM)
20−import Data.ByteString as B
18+ ( getEntropy
19+ , CryptHandle
20+ , openHandle
21+ , hGetEntropy
22+ , closeHandle
23+ ) where
24+
25+#ifndef XEN
26+import Control.Monad (liftM, when)
2127import System.IO.Error (mkIOError, eofErrorType, ioeSetErrorString)
2228import Foreign (allocaBytes)
29+#else
30+import Control.Monad (when)
31+#endif
32+import Data.ByteString as B
2333
2434#if defined(isWindows)
2535{- C example for windows rng - taken from a blog, can't recall which one but thank you!
@@ -149,8 +159,13 @@ hGetEntropy UseRdRand =
149159
150160#else
151161{- Not windows, assuming nix with a /dev/urandom -}
152−import System.Posix (openFd, closeFd, fdReadBuf, OpenMode(..), defaultFileFlags, Fd)
153162import Foreign.Ptr
163+import Foreign.C.Types
164+import Data.ByteString.Internal as B
165+#ifdef XEN
166+data CryptHandle = UseRdRand -- or die trying
167+#else
168+import System.Posix (openFd, closeFd, fdReadBuf, OpenMode(..), defaultFileFlags, Fd)
154169
155170source :: FilePath
156171source = "/dev/urandom"
@@ -161,6 +176,7 @@ data CryptHandle
161176#ifdef HAVE_RDRAND
162177 | UseRdRand
163178#endif
179+#endif
164180
165181-- |Open a `CryptHandle`
166182openHandle :: IO CryptHandle
@@ -170,15 +186,22 @@ openHandle = do
170186 if b then return UseRdRand
171187 else do
172188#endif
173− liftM CH (openFd source ReadOnly Nothing defaultFileFlags)
189+#ifdef XEN
190+ error "entropy: On halvm there is no entropy other than RDRAND."
191+#else
192+ liftM CH (openFd source ReadOnly Nothing defaultFileFlags)
193+#endif
174194
175195-- |Close the `CryptHandle`
176196closeHandle :: CryptHandle -> IO ()
197+#ifndef XEN
177198closeHandle (CH h) = closeFd h
199+#endif
178200#ifdef HAVE_RDRAND
179201closeHandle UseRdRand = return ()
180202#endif
181203
204+#ifndef XEN
182205fdReadBS :: Fd -> Int -> IO B.ByteString
183206fdReadBS fd n = do
184207 allocaBytes n $ \buf -> do
@@ -186,10 +209,13 @@ fdReadBS fd n = do
186209 case rc of
187210 0 -> ioError (ioeSetErrorString (mkIOError eofErrorType "fdRead" Nothing Nothing) "EOF")
188211 n' -> B.packCStringLen (castPtr buf, fromIntegral n')
212+#endif
189213
190214-- |Read random data from a `CryptHandle`
191215hGetEntropy :: CryptHandle -> Int -> IO B.ByteString
216+#ifndef XEN
192217hGetEntropy (CH h) = fdReadBS h
218+#else
193219#ifdef HAVE_RDRAND
194220hGetEntropy UseRdRand = \n -> do
195221 B.create n $ \ptr -> do
@@ -197,6 +223,8 @@ hGetEntropy UseRdRand = \n -> do
197223 when (r /= 0)
198224 (fail "RDRand failed to gather entropy")
199225#endif
226+#endif
227+{- XEN -}
200228
201229#ifdef HAVE_RDRAND
202230foreign import ccall unsafe "cpu_has_rdrand"
@@ -213,7 +241,7 @@ cpuHasRdRand = (/= 0) `fmap` c_cpu_has_rdrand
213241-- secure random data using the system-specific facilities.
214242--
215243-- Use '/dev/urandom' on *nix and CryptAPI when on Windows. In short,
216−-- this entropy is considered "cryptographically secure" but not true
244+-- this entropy is considered cryptographically secure but not true
217245-- entropy.
218246getEntropy :: Int -> IO B.ByteString
219247getEntropy n = do
entropy.cabal+15−5
@@ -1,5 +1,5 @@
11name: entropy
2−version: 0.2.2.4
2+version: 0.3
33description: A platform independent method to obtain cryptographically strong entropy
44 (urandom on Linux, CryptAPI on Windows, patches welcome).
55 Users looking for cryptographically strong (number-theoretically
@@ -14,20 +14,29 @@ category: Data, Cryptography
1414homepage: https://github.com/TomMD/entropy
1515bug-reports: https://github.com/TomMD/entropy/issues
1616stability: stable
17+-- build-type: Simple
18+-- ^^ Used for HaLVM
1719build-type: Custom
18−cabal-version: >=1.10
20+-- ^^ Test for RDRAND support using 'ghc'
21+cabal-version: >=1.10
1922tested-with: GHC == 7.6.3
2023-- data-files:
2124extra-source-files: ./cbits/rdrand.c, ./cbits/rdrand.h, README.md
2225
26+-- Notice to compile with HaLVM the above 'build-type' must be changed
27+-- to 'Simple' instead of 'Custom'. The current build system naively
28+-- runs GHC to determine if the compiler supports RDRAND before proceeding.
29+flag halvm
30+ description: Build for the HaLVM
31+
2332library
2433 ghc-options: -Wall -O2
2534 exposed-modules: System.Entropy
2635 other-extensions: CPP, ForeignFunctionInterface, BangPatterns, ScopedTypeVariables
2736 build-depends: base == 4.*, bytestring
28− -- hs-source-dirs:
29− -- other-modules:
3037 default-language: Haskell2010
38+ if(flag(halvm))
39+ cpp-options: -DXEN -DHAVE_RDRAND
3140 if arch(x86_64)
3241 cpp-options: -Darch_x86_64
3342 c-sources: cbits/rdrand.c
@@ -36,7 +45,8 @@ library
3645 cpp-options: -DisWindows
3746 extra-libraries: advapi32
3847 else
39− Build-Depends: unix
48+ if !flag(halvm)
49+ Build-Depends: unix
4050
4151source-repository head
4252 type: git