-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WASI: implement in terms of
getentropy
This isn't really WASI-specific; it should work for any platform that has `getentropy`.
- Loading branch information
Showing
3 changed files
with
46 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
{-# LANGUAGE CApiFFI #-} | ||
|
||
module System.EntropyWasi where | ||
|
||
import Control.Monad (when) | ||
import Data.ByteString (ByteString) | ||
import Data.ByteString.Internal as B | ||
import Data.Word (Word8) | ||
import Foreign.C.Types | ||
import Foreign.Ptr | ||
|
||
data CryptHandle = BogusCryptHandle | ||
|
||
openHandle :: IO CryptHandle | ||
openHandle = pure BogusCryptHandle | ||
|
||
hGetEntropy :: CryptHandle -> Int -> IO ByteString | ||
hGetEntropy BogusCryptHandle = \n -> B.create n $ go (fromIntegral n) | ||
where | ||
go :: CSize -> Ptr Word8 -> IO () | ||
go n ptr | ||
| n <= 256 = getentropy' ptr n | ||
| otherwise = do | ||
getentropy' ptr 256 | ||
go (n - 256) (ptr `plusPtr` 256) | ||
|
||
getentropy' ptr n = do | ||
res <- getentropy ptr n | ||
when (res /= 0) $ | ||
fail "getentropy failed" | ||
|
||
foreign import capi safe "unistd.h getentropy" | ||
getentropy :: Ptr Word8 -> CSize -> IO CInt | ||
|
||
closeHandle :: CryptHandle -> IO () | ||
closeHandle BogusCryptHandle = pure () | ||
|
||
hardwareRandom :: Int -> IO (Maybe ByteString) | ||
hardwareRandom _ = pure Nothing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
cabal-version: >=1.10 | ||
cabal-version: 3.8 | ||
name: entropy | ||
version: 0.4.1.10 | ||
x-revision: 2 | ||
|
@@ -7,7 +7,7 @@ description: A mostly platform independent method to obtain cryptographically | |
Users looking for cryptographically strong (number-theoretically | ||
sound) PRNGs should see the 'DRBG' package too. | ||
synopsis: A platform independent entropy source | ||
license: BSD3 | ||
license: BSD-3-Clause | ||
license-file: LICENSE | ||
copyright: Thomas DuBuisson <[email protected]> | ||
author: Thomas DuBuisson <[email protected]> | ||
|
@@ -61,6 +61,8 @@ library | |
else { | ||
if os(windows) | ||
other-modules: System.EntropyWindows | ||
elif os(wasi) | ||
other-modules: System.EntropyWasi | ||
else { | ||
other-modules: System.EntropyNix | ||
} | ||
|
@@ -90,6 +92,7 @@ library | |
cpp-options: -DisWindows | ||
cc-options: -DisWindows | ||
extra-libraries: advapi32 | ||
elif os(wasi) | ||
else | ||
Build-Depends: unix | ||
c-sources: cbits/getrandom.c cbits/random_initialized.c | ||
|