@@ -47,7 +47,7 @@ import Foreign.Marshal.Alloc (alloca)
47import Foreign.Marshal.Utils (toBool)
47import Foreign.Marshal.Utils (toBool)
48import Foreign.Storable (peek)
48import Foreign.Storable (peek)
50−#ifdef arch_x86_64
50+#ifdef HAVE_RDRAND
51foreign import ccall unsafe "cpu_has_rdrand"
51foreign import ccall unsafe "cpu_has_rdrand"
52 c_cpu_has_rdrand :: IO CInt
52 c_cpu_has_rdrand :: IO CInt
@@ -60,7 +60,7 @@ cpuHasRdRand = (/= 0) `fmap` c_cpu_has_rdrand
61data CryptHandle
61data CryptHandle
62 = CH Word32
62 = CH Word32
63−#ifdef arch_x86_64
63+#ifdef HAVE_RDRAND
64 | UseRdRand
64 | UseRdRand
@@ -111,7 +111,7 @@ cryptReleaseCtx h = do
111-- on platforms without a secure RNG!
111-- on platforms without a secure RNG!
112getEntropy :: Int -> IO B.ByteString
112getEntropy :: Int -> IO B.ByteString
113getEntropy n = do
113getEntropy n = do
114−#ifdef arch_x86_64
114+#ifdef HAVE_RDRAND
115 b <- cpuHasRdRand
115 b <- cpuHasRdRand
116 if b then hGetEntropy UseRdRand n
116 if b then hGetEntropy UseRdRand n
@@ -125,7 +125,7 @@ getEntropy n = do
125-- |Open a handle from which random data can be read
125-- |Open a handle from which random data can be read
126openHandle :: IO CryptHandle
126openHandle :: IO CryptHandle
127openHandle = do
127openHandle = do
128−#ifdef arch_x86_64
128+#ifdef HAVE_RDRAND
129 b <- cpuHasRdRand
129 b <- cpuHasRdRand
130 if b then return UseRdRand
130 if b then return UseRdRand
@@ -138,7 +138,7 @@ closeHandle (CH h) = cryptReleaseCtx h
138-- |Read from `CryptHandle`
138-- |Read from `CryptHandle`
139hGetEntropy :: CryptHandle -> Int -> IO B.ByteString
139hGetEntropy :: CryptHandle -> Int -> IO B.ByteString
140hGetEntropy (CH h) = cryptGenRandom h
140hGetEntropy (CH h) = cryptGenRandom h
141−#ifdef arch_x86_64
141+#ifdef HAVE_RDRAND
142hGetEntropy UseRdRand =
142hGetEntropy UseRdRand =
143 B.create n $ \ptr -> do
143 B.create n $ \ptr -> do
144 r <- c_get_rand_bytes (castPtr ptr) (fromIntegral n)
144 r <- c_get_rand_bytes (castPtr ptr) (fromIntegral n)
@@ -150,7 +150,6 @@ hGetEntropy UseRdRand =
150{- Not windows, assuming nix with a /dev/urandom -}
150{- Not windows, assuming nix with a /dev/urandom -}
151import Foreign.C.Types
151import Foreign.C.Types
152import Foreign.Ptr
152import Foreign.Ptr
153−import Data.ByteString.Internal as B
155source :: FilePath
154source :: FilePath
156source = "/dev/urandom"
155source = "/dev/urandom"
@@ -158,14 +157,14 @@ source = "/dev/urandom"
158-- |Handle for manual resource mangement
157-- |Handle for manual resource mangement
159data CryptHandle
158data CryptHandle
160 = CH Handle
159 = CH Handle
161−#ifdef arch_x86_64
160+#ifdef HAVE_RDRAND
162 | UseRdRand
161 | UseRdRand
165-- |Open a `CryptHandle`
164-- |Open a `CryptHandle`
166openHandle :: IO CryptHandle
165openHandle :: IO CryptHandle
167openHandle = do
166openHandle = do
168−#ifdef arch_x86_64
167+#ifdef HAVE_RDRAND
169 b <- cpuHasRdRand
168 b <- cpuHasRdRand
170 if b then return UseRdRand
169 if b then return UseRdRand
@@ -175,14 +174,14 @@ openHandle = do
175-- |Close the `CryptHandle`
174-- |Close the `CryptHandle`
176closeHandle :: CryptHandle -> IO ()
175closeHandle :: CryptHandle -> IO ()
177closeHandle (CH h) = hClose h
176closeHandle (CH h) = hClose h
178−#ifdef arch_x86_64
177+#ifdef HAVE_RDRAND
179closeHandle UseRdRand = return ()
178closeHandle UseRdRand = return ()
182-- |Read random data from a `CryptHandle`
181-- |Read random data from a `CryptHandle`
183hGetEntropy :: CryptHandle -> Int -> IO B.ByteString
182hGetEntropy :: CryptHandle -> Int -> IO B.ByteString
184hGetEntropy (CH h) = B.hGet h
183hGetEntropy (CH h) = B.hGet h
185−#ifdef arch_x86_64
184+#ifdef HAVE_RDRAND
186hGetEntropy UseRdRand = \n -> do
185hGetEntropy UseRdRand = \n -> do
187 B.create n $ \ptr -> do
186 B.create n $ \ptr -> do
188 r <- c_get_rand_bytes (castPtr ptr) (fromIntegral n)
187 r <- c_get_rand_bytes (castPtr ptr) (fromIntegral n)
@@ -190,7 +189,7 @@ hGetEntropy UseRdRand = \n -> do
190 (fail "RDRand failed to gather entropy")
189 (fail "RDRand failed to gather entropy")
193−#ifdef arch_x86_64
192+#ifdef HAVE_RDRAND
194foreign import ccall unsafe "cpu_has_rdrand"
193foreign import ccall unsafe "cpu_has_rdrand"
195 c_cpu_has_rdrand :: IO CInt
194 c_cpu_has_rdrand :: IO CInt
@@ -209,7 +208,7 @@ cpuHasRdRand = (/= 0) `fmap` c_cpu_has_rdrand
209-- entropy.
208-- entropy.
210getEntropy :: Int -> IO B.ByteString
209getEntropy :: Int -> IO B.ByteString
211getEntropy n = do
210getEntropy n = do
212−#if arch_x86_64
211+#ifdef HAVE_RDRAND
213 b <- cpuHasRdRand
212 b <- cpuHasRdRand
214 if b then hGetEntropy UseRdRand n
213 if b then hGetEntropy UseRdRand n