entropy 0.2.2.2 → 0.2.2.3
raw patch · 5 files changed
+68/−101 lines, 5 filesdep +unixsetup-changed
Dependencies added: unix
Files
- LICENSE +22/−22
- README.md +5/−0
- Setup.hs +1/−47
- System/Entropy.hs +21/−16
- entropy.cabal +19/−16
LICENSE view
@@ -1,30 +1,30 @@-Copyright (c) Thomas DuBuisson+Copyright (c) 2013, Thomas M. DuBuisson All rights reserved. Redistribution and use in source and binary forms, with or without-modification, are permitted provided that the following conditions-are met:+modification, are permitted provided that the following conditions are met: -1. Redistributions of source code must retain the above copyright- notice, this list of conditions and the following disclaimer.+ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer. -2. Redistributions in binary form must reproduce the above copyright- notice, this list of conditions and the following disclaimer in the- documentation and/or other materials provided with the distribution.+ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution. -3. Neither the name of the author nor the names of his contributors- may be used to endorse or promote products derived from this software- without specific prior written permission.+ * Neither the name of Thomas M. DuBuisson nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission. -THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND ANY EXPRESS-OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED-WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE-DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR-ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL-DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS-OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)-HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,-STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN-ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE-POSSIBILITY OF SUCH DAMAGE.+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,5 @@+# Introduction++This package allows Haskell users to easily acquire entropy for use in+critical security applications by calling out to either windows crypto api,+unix/linux's `/dev/urandom`, or the RDRAND instruction.
Setup.hs view
@@ -1,48 +1,2 @@ import Distribution.Simple-import Distribution.Simple.LocalBuildInfo-import Distribution.Simple.Setup-import Distribution.PackageDescription-import Distribution.Simple.Utils-import Distribution.Simple.Program-import Distribution.Verbosity-import System.Process-import System.Directory-import System.FilePath-import System.Exit--main = defaultMainWithHooks hk- where- hk = simpleUserHooks { buildHook = \pd lbi uh bf -> do- let ccProg = Program "gcc" undefined undefined undefined- mConf = lookupProgram ccProg (withPrograms lbi)- err = error "Could not determine C compiler"- cc = locationPath . programLocation . maybe err id $ mConf- b <- canUseRDRAND cc- let newWithPrograms1 = userSpecifyArgs "gcc" cArgs (withPrograms lbi)- newWithPrograms = userSpecifyArgs "ghc" cArgsHC newWithPrograms1- lbiNew = if b then (lbi {withPrograms = newWithPrograms }) else lbi- buildHook simpleUserHooks pd lbiNew uh bf- }--cArgs :: [String]-cArgs = ["-DHAVE_RDRAND"]--cArgsHC :: [String]-cArgsHC = map ("-optc" ++) cArgs--canUseRDRAND :: FilePath -> IO Bool-canUseRDRAND cc = do- withTempDirectory normal "" "testRDRAND" $ \tmpDir -> do- writeFile (tmpDir ++ "/testRDRAND.c")- (unlines [ "#include <stdint.h>"- , "int main() {"- , " uint64_t therand;"- , " unsigned char err;"- , " asm volatile(\"rdrand %0 ; setc %1\""- , " : \"=r\" (therand), \"=qm\" (err));"- , " return (!err);"- , "}"- ])- ec <- rawSystemExitCode normal cc [tmpDir </> "testRDRAND.c", "-o" ++ tmpDir ++ "/a.out"]- notice normal $ "Result of RDRAND Test: " ++ show (ec == ExitSuccess)- return (ec == ExitSuccess)+main = defaultMain
System/Entropy.hs view
@@ -16,9 +16,11 @@ , closeHandle ) where -import Control.Monad (liftM, when)+import Control.Monad (liftM) import Data.ByteString as B-import System.IO (openFile, hClose, IOMode(..), Handle, withBinaryFile)+import System.IO.Error (mkIOError, eofErrorType, ioeSetErrorString)+import System.Posix (openFd, closeFd, fdReadBuf, OpenMode(..), defaultFileFlags, Fd)+import Foreign (allocaBytes) #if defined(isWindows) {- C example for windows rng - taken from a blog, can't recall which one but thank you!@@ -148,7 +150,6 @@ #else {- Not windows, assuming nix with a /dev/urandom -}-import Foreign.C.Types import Foreign.Ptr source :: FilePath@@ -156,7 +157,7 @@ -- |Handle for manual resource mangement data CryptHandle- = CH Handle+ = CH Fd #ifdef HAVE_RDRAND | UseRdRand #endif@@ -169,18 +170,26 @@ if b then return UseRdRand else do #endif- liftM CH (openFile source ReadMode)+ liftM CH (openFd source ReadOnly Nothing defaultFileFlags) -- |Close the `CryptHandle` closeHandle :: CryptHandle -> IO ()-closeHandle (CH h) = hClose h+closeHandle (CH h) = closeFd h #ifdef HAVE_RDRAND closeHandle UseRdRand = return () #endif +fdReadBS :: Fd -> Int -> IO B.ByteString+fdReadBS fd n = do+ allocaBytes n $ \buf -> do+ rc <- fdReadBuf fd buf (fromIntegral n)+ case rc of+ 0 -> ioError (ioeSetErrorString (mkIOError eofErrorType "fdRead" Nothing Nothing) "EOF")+ n' -> B.packCStringLen (castPtr buf, fromIntegral n')+ -- |Read random data from a `CryptHandle`-hGetEntropy :: CryptHandle -> Int -> IO B.ByteString -hGetEntropy (CH h) = B.hGet h+hGetEntropy :: CryptHandle -> Int -> IO B.ByteString+hGetEntropy (CH h) = fdReadBS h #ifdef HAVE_RDRAND hGetEntropy UseRdRand = \n -> do B.create n $ \ptr -> do@@ -208,13 +217,9 @@ -- entropy. getEntropy :: Int -> IO B.ByteString getEntropy n = do-#ifdef HAVE_RDRAND- b <- cpuHasRdRand- if b then hGetEntropy UseRdRand n- else do-#endif-{- arch_x86 -}- withBinaryFile source ReadMode (`B.hGet` n)+ h <- openHandle+ e <- hGetEntropy h n+ closeHandle h+ return e #endif {- OS Test -}-
entropy.cabal view
@@ -1,32 +1,33 @@ name: entropy-version: 0.2.2.2-license: BSD3-license-file: LICENSE-copyright: Thomas DuBuisson <thomas.dubuisson@gmail.com>-author: Thomas DuBuisson <thomas.dubuisson@gmail.com>-maintainer: Thomas DuBuisson <thomas.dubuisson@gmail.com>+version: 0.2.2.3 description: A platform independent method to obtain cryptographically strong entropy (urandom on Linux, CryptAPI on Windows, patches welcome). Users looking for cryptographically strong (number-theoretically sound) PRNGs should see the 'DRBG' package too! synopsis: A platform independent entropy source+license: BSD3+license-file: LICENSE+copyright: Thomas DuBuisson <thomas.dubuisson@gmail.com>+author: Thomas DuBuisson <thomas.dubuisson@gmail.com>+maintainer: Thomas DuBuisson <thomas.dubuisson@gmail.com> category: Data, Cryptography homepage: https://github.com/TomMD/entropy bug-reports: https://github.com/TomMD/entropy/issues stability: stable-build-type: Custom-cabal-version: >= 1.6-tested-with: GHC == 6.12.1-data-files:-extra-source-files: ./cbits/rdrand.c- , ./cbits/rdrand.h+build-type: Custom+cabal-version: >=1.10+tested-with: GHC == 7.6.3+-- data-files:+extra-source-files: ./cbits/rdrand.c, ./cbits/rdrand.h, README.md -Library- Build-Depends: base == 4.*, bytestring+library ghc-options: -Wall -O2- hs-source-dirs: exposed-modules: System.Entropy- other-modules:+ other-extensions: CPP, ForeignFunctionInterface, BangPatterns, ScopedTypeVariables+ build-depends: base == 4.*, bytestring+ -- hs-source-dirs:+ -- other-modules:+ default-language: Haskell2010 if arch(x86_64) cpp-options: -Darch_x86_64 c-sources: cbits/rdrand.c@@ -34,6 +35,8 @@ if os(windows) cpp-options: -DisWindows extra-libraries: advapi32+ else+ Build-Depends: unix source-repository head type: git