git.iko.soy/typeable/entropy
on main help sign in
Fixes for Windows: proper usage of pointer types in imported foreign function signatures and improved error messages.
commitcedb3d7a85540ee4bb7ab00d8d698782ec429649 authormwu <mwu-tow@gazeta.pl> dateFri 9 Nov 2018, 20:46 (8 years ago) parent65f8199
2 files changed+17−14
System/EntropyWindows.hs+16−14 entropy.cabal+1
System/EntropyWindows.hs+16−14
@@ -17,6 +17,7 @@ module System.EntropyWindows
1717
1818import Control.Monad (liftM, when)
1919import System.IO.Error (mkIOError, eofErrorType, ioeSetErrorString)
20+import System.Win32.Types (ULONG_PTR, errorWin)
2021import Foreign (allocaBytes)
2122import Data.ByteString as B
2223import Data.ByteString.Internal as BI
@@ -63,8 +64,9 @@ cpuHasRdRand :: IO Bool
6364cpuHasRdRand = (/= 0) `fmap` c_cpu_has_rdrand
6465#endif
6566
67+type HCRYPTPROV = ULONG_PTR
6668data CryptHandle
67− = CH Word32
69+ = CH HCRYPTPROV
6870
6971
7072-- | Get random values from the hardward RNG or return Nothing if no
@@ -86,7 +88,7 @@ hardwareRandom n =
8688hardwareRandom _ = pure Nothing
8789#endif
8890
89−-- Define the constants we need from WinCrypt.h
91+-- Define the constants we need from WinCrypt.h
9092msDefProv :: String
9193msDefProv = "Microsoft Base Cryptographic Provider v1.0"
9294provRSAFull :: Word32
@@ -94,37 +96,37 @@ provRSAFull = 1
9496cryptVerifyContext :: Word32
9597cryptVerifyContext = fromIntegral 0xF0000000
9698
97−-- Declare the required CryptoAPI imports
99+-- Declare the required CryptoAPI imports
98100foreign import stdcall unsafe "CryptAcquireContextA"
99− c_cryptAcquireCtx :: Ptr Word32 -> CString -> CString -> Word32 -> Word32 -> IO Int32
101+ c_cryptAcquireCtx :: Ptr HCRYPTPROV -> CString -> CString -> Word32 -> Word32 -> IO Int32
100102foreign import stdcall unsafe "CryptGenRandom"
101− c_cryptGenRandom :: Word32 -> Word32 -> Ptr Word8 -> IO Int32
103+ c_cryptGenRandom :: HCRYPTPROV -> Word32 -> Ptr Word8 -> IO Int32
102104foreign import stdcall unsafe "CryptReleaseContext"
103− c_cryptReleaseCtx :: Word32 -> Word32 -> IO Int32
105+ c_cryptReleaseCtx :: HCRYPTPROV -> Word32 -> IO Int32
104106
105−cryptAcquireCtx :: IO Word32
107+cryptAcquireCtx :: IO HCRYPTPROV
106108cryptAcquireCtx =
107− alloca $ \handlePtr ->
109+ alloca $ \handlePtr ->
108110 withCString msDefProv $ \provName -> do
109111 stat <- c_cryptAcquireCtx handlePtr nullPtr provName provRSAFull cryptVerifyContext
110112 if (toBool stat)
111113 then peek handlePtr
112− else fail "c_cryptAcquireCtx"
114+ else errorWin "c_cryptAcquireCtx"
113115
114−cryptGenRandom :: Word32 -> Int -> IO B.ByteString
115−cryptGenRandom h i =
116+cryptGenRandom :: HCRYPTPROV -> Int -> IO B.ByteString
117+cryptGenRandom h i =
116118 BI.create i $ \c_buffer -> do
117119 stat <- c_cryptGenRandom h (fromIntegral i) c_buffer
118120 if (toBool stat)
119121 then return ()
120− else fail "c_cryptGenRandom"
122+ else errorWin "c_cryptGenRandom"
121123
122−cryptReleaseCtx :: Word32 -> IO ()
124+cryptReleaseCtx :: HCRYPTPROV -> IO ()
123125cryptReleaseCtx h = do
124126 stat <- c_cryptReleaseCtx h 0
125127 if (toBool stat)
126128 then return ()
127− else fail "c_cryptReleaseCtx"
129+ else errorWin "c_cryptReleaseCtx"
128130
129131-- |Open a handle from which random data can be read
130132openHandle :: IO CryptHandle
entropy.cabal+1
@@ -66,6 +66,7 @@ library
6666 cpp-options: -Darch_i386
6767 cc-options: -Darch_i386 -O2
6868 if os(windows)
69+ build-depends: Win32
6970 cpp-options: -DisWindows
7071 cc-options: -DisWindows
7172 extra-libraries: advapi32