packages feed

crypton-2.3.1 / Crypto / Random / Entropy / Windows.hs

-- |-- Module      : Crypto.Random.Entropy.Windows-- License     : BSD-style-- Maintainer  : Vincent Hanquez <vincent@snarc.org>-- Stability   : experimental-- Portability : Good---- Code originally from the entropy package and thus is:--   Copyright (c) Thomas DuBuisson.--{-# LANGUAGE ForeignFunctionInterface #-}{-# LANGUAGE CPP #-}module Crypto.Random.Entropy.Windows    ( WinCryptoAPI    ) whereimport Data.Int (Int32)import Data.Wordimport Foreign.C.String (CString, withCString)import Foreign.Ptr (Ptr, nullPtr, plusPtr)import Foreign.Marshal.Alloc (alloca)import Foreign.Marshal.Utils (toBool)import Foreign.Storable (peek)import System.Win32.Types (getLastError)import Control.Exception (throwIO)import Crypto.Random.Entropy.Source-- | Handle to Windows crypto API for random generationdata WinCryptoAPI = WinCryptoAPIinstance EntropySource WinCryptoAPI where    entropyOpen = do        mctx <- cryptAcquireCtx        maybe (return Nothing) (\ctx -> cryptReleaseCtx ctx >> return (Just WinCryptoAPI)) mctx    entropyGather WinCryptoAPI ptr n = do        mctx <- cryptAcquireCtx        case mctx of            Nothing  -> do                lastError <- getLastError                throwIO $ EntropySourceLost                    ("the Windows crypto API: error " ++ show lastError)            Just ctx -> do                r <- cryptGenRandom ctx ptr n                cryptReleaseCtx ctx                return r    entropyClose WinCryptoAPI = return ()type DWORD = Word32type BOOL  = Int32type BYTE  = Word8#if defined(ARCH_X86)# define WINDOWS_CCONV stdcalltype CryptCtx = Word32#elif defined(ARCH_X86_64)# define WINDOWS_CCONV ccalltype CryptCtx = Word64#else# error Unknown mingw32 arch#endif-- Declare the required CryptoAPI importsforeign import WINDOWS_CCONV unsafe "CryptAcquireContextA"   c_cryptAcquireCtx :: Ptr CryptCtx -> CString -> CString -> DWORD -> DWORD -> IO BOOLforeign import WINDOWS_CCONV unsafe "CryptGenRandom"   c_cryptGenRandom :: CryptCtx -> DWORD -> Ptr BYTE -> IO BOOLforeign import WINDOWS_CCONV unsafe "CryptReleaseContext"   c_cryptReleaseCtx :: CryptCtx -> DWORD -> IO BOOL-- Define the constants we need from WinCrypt.hmsDefProv :: StringmsDefProv = "Microsoft Base Cryptographic Provider v1.0"provRSAFull :: DWORDprovRSAFull = 1cryptVerifyContext :: DWORDcryptVerifyContext = 0xF0000000cryptAcquireCtx :: IO (Maybe CryptCtx)cryptAcquireCtx =    alloca $ \handlePtr ->    withCString msDefProv $ \provName -> do        r <- toBool `fmap` c_cryptAcquireCtx handlePtr nullPtr provName provRSAFull cryptVerifyContext        if r            then Just `fmap` peek handlePtr            else return Nothing-- The length is a DWORD, so a request of 4 GiB or more goes in pieces;-- returns how many bytes were filled.cryptGenRandom :: CryptCtx -> Ptr Word8 -> Int -> IO IntcryptGenRandom h buf n = go buf (max 0 n)  where    go p left        | left == 0 = return n        | otherwise = do            let m = min left 0x40000000            success <- toBool `fmap` c_cryptGenRandom h (fromIntegral m) p            if success then go (p `plusPtr` m) (left - m) else return (n - left)cryptReleaseCtx :: CryptCtx -> IO ()cryptReleaseCtx h = do    success <- toBool `fmap` c_cryptReleaseCtx h 0    if success        then return ()        else do            lastError <- getLastError            throwIO $ EntropySourceLost                ("cryptReleaseCtx: error " ++ show lastError)