Merge pull request #180 from haskell-crypto/chacha-drg-expose
Expose properly things to create ChaChaDRG
This commit is contained in:
commit
23ba060c73
@ -54,11 +54,13 @@ initialize nbRounds key nonce
|
|||||||
nonceLen = B.length nonce
|
nonceLen = B.length nonce
|
||||||
|
|
||||||
-- | Initialize simple ChaCha State
|
-- | Initialize simple ChaCha State
|
||||||
initializeSimple :: ByteArray seed
|
--
|
||||||
|
-- The seed need to be at least 40 bytes long
|
||||||
|
initializeSimple :: ByteArrayAccess seed
|
||||||
=> seed -- ^ a 40 bytes long seed
|
=> seed -- ^ a 40 bytes long seed
|
||||||
-> StateSimple
|
-> StateSimple
|
||||||
initializeSimple seed
|
initializeSimple seed
|
||||||
| sLen /= 40 = error "ChaCha Random: seed length should be 40 bytes"
|
| sLen < 40 = error "ChaCha Random: seed length should be 40 bytes"
|
||||||
| otherwise = unsafeDoIO $ do
|
| otherwise = unsafeDoIO $ do
|
||||||
stPtr <- B.alloc 64 $ \stPtr ->
|
stPtr <- B.alloc 64 $ \stPtr ->
|
||||||
B.withByteArray seed $ \seedPtr ->
|
B.withByteArray seed $ \seedPtr ->
|
||||||
|
|||||||
@ -28,6 +28,7 @@ data CryptoError =
|
|||||||
-- symmetric cipher errors
|
-- symmetric cipher errors
|
||||||
CryptoError_KeySizeInvalid
|
CryptoError_KeySizeInvalid
|
||||||
| CryptoError_IvSizeInvalid
|
| CryptoError_IvSizeInvalid
|
||||||
|
| CryptoError_SeedSizeInvalid
|
||||||
| CryptoError_AEADModeNotSupported
|
| CryptoError_AEADModeNotSupported
|
||||||
-- public key cryptography error
|
-- public key cryptography error
|
||||||
| CryptoError_SecretKeySizeInvalid
|
| CryptoError_SecretKeySizeInvalid
|
||||||
|
|||||||
@ -16,6 +16,7 @@ module Crypto.Random
|
|||||||
, seedNew
|
, seedNew
|
||||||
, seedFromInteger
|
, seedFromInteger
|
||||||
, seedToInteger
|
, seedToInteger
|
||||||
|
, seedFromBinary
|
||||||
-- * Deterministic Random class
|
-- * Deterministic Random class
|
||||||
, getSystemDRG
|
, getSystemDRG
|
||||||
, drgNew
|
, drgNew
|
||||||
@ -29,10 +30,12 @@ module Crypto.Random
|
|||||||
, MonadPseudoRandom
|
, MonadPseudoRandom
|
||||||
) where
|
) where
|
||||||
|
|
||||||
|
import Crypto.Error
|
||||||
import Crypto.Random.Types
|
import Crypto.Random.Types
|
||||||
import Crypto.Random.ChaChaDRG
|
import Crypto.Random.ChaChaDRG
|
||||||
import Crypto.Random.SystemDRG
|
import Crypto.Random.SystemDRG
|
||||||
import Data.ByteArray (ByteArray, ByteArrayAccess, ScrubbedBytes)
|
import Data.ByteArray (ByteArray, ByteArrayAccess, ScrubbedBytes)
|
||||||
|
import qualified Data.ByteArray as B
|
||||||
import Crypto.Internal.Imports
|
import Crypto.Internal.Imports
|
||||||
|
|
||||||
import qualified Crypto.Number.Serialize as Serialize
|
import qualified Crypto.Number.Serialize as Serialize
|
||||||
@ -56,6 +59,12 @@ seedToInteger (Seed b) = Serialize.os2ip b
|
|||||||
seedFromInteger :: Integer -> Seed
|
seedFromInteger :: Integer -> Seed
|
||||||
seedFromInteger i = Seed $ Serialize.i2ospOf_ seedLength (i `mod` 2^(seedLength * 8))
|
seedFromInteger i = Seed $ Serialize.i2ospOf_ seedLength (i `mod` 2^(seedLength * 8))
|
||||||
|
|
||||||
|
-- | Convert a binary to a seed
|
||||||
|
seedFromBinary :: ByteArrayAccess b => b -> CryptoFailable Seed
|
||||||
|
seedFromBinary b
|
||||||
|
| B.length b /= 40 = CryptoFailed (CryptoError_SeedSizeInvalid)
|
||||||
|
| otherwise = CryptoPassed $ Seed $ B.convert b
|
||||||
|
|
||||||
-- | Create a new DRG from system entropy
|
-- | Create a new DRG from system entropy
|
||||||
drgNew :: MonadRandom randomly => randomly ChaChaDRG
|
drgNew :: MonadRandom randomly => randomly ChaChaDRG
|
||||||
drgNew = drgNewSeed `fmap` seedNew
|
drgNew = drgNewSeed `fmap` seedNew
|
||||||
|
|||||||
@ -14,7 +14,7 @@ module Crypto.Random.ChaChaDRG
|
|||||||
|
|
||||||
import Crypto.Random.Types
|
import Crypto.Random.Types
|
||||||
import Crypto.Internal.Imports
|
import Crypto.Internal.Imports
|
||||||
import Crypto.Internal.ByteArray (ByteArray, ScrubbedBytes)
|
import Crypto.Internal.ByteArray (ByteArray, ByteArrayAccess, ScrubbedBytes)
|
||||||
import qualified Crypto.Internal.ByteArray as B
|
import qualified Crypto.Internal.ByteArray as B
|
||||||
import Foreign.Storable (pokeElemOff)
|
import Foreign.Storable (pokeElemOff)
|
||||||
|
|
||||||
@ -29,7 +29,7 @@ newtype ChaChaDRG = ChaChaDRG C.StateSimple
|
|||||||
|
|
||||||
-- | Initialize a new ChaCha context with the number of rounds,
|
-- | Initialize a new ChaCha context with the number of rounds,
|
||||||
-- the key and the nonce associated.
|
-- the key and the nonce associated.
|
||||||
initialize :: ByteArray seed
|
initialize :: B.ByteArrayAccess seed
|
||||||
=> seed -- ^ 40 bytes of seed
|
=> seed -- ^ 40 bytes of seed
|
||||||
-> ChaChaDRG -- ^ the initial ChaCha state
|
-> ChaChaDRG -- ^ the initial ChaCha state
|
||||||
initialize seed = ChaChaDRG $ C.initializeSimple seed
|
initialize seed = ChaChaDRG $ C.initializeSimple seed
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user