[ChaCha] only required byteArrayAccess and add a way to convert from binary
This commit is contained in:
parent
f559c7bd9d
commit
664a37c16d
@ -28,6 +28,7 @@ data CryptoError =
|
||||
-- symmetric cipher errors
|
||||
CryptoError_KeySizeInvalid
|
||||
| CryptoError_IvSizeInvalid
|
||||
| CryptoError_SeedSizeInvalid
|
||||
| CryptoError_AEADModeNotSupported
|
||||
-- public key cryptography error
|
||||
| CryptoError_SecretKeySizeInvalid
|
||||
|
||||
@ -16,6 +16,7 @@ module Crypto.Random
|
||||
, seedNew
|
||||
, seedFromInteger
|
||||
, seedToInteger
|
||||
, seedFromBinary
|
||||
-- * Deterministic Random class
|
||||
, getSystemDRG
|
||||
, drgNew
|
||||
@ -29,10 +30,12 @@ module Crypto.Random
|
||||
, MonadPseudoRandom
|
||||
) where
|
||||
|
||||
import Crypto.Error
|
||||
import Crypto.Random.Types
|
||||
import Crypto.Random.ChaChaDRG
|
||||
import Crypto.Random.SystemDRG
|
||||
import Data.ByteArray (ByteArray, ByteArrayAccess, ScrubbedBytes)
|
||||
import qualified Data.ByteArray as B
|
||||
import Crypto.Internal.Imports
|
||||
|
||||
import qualified Crypto.Number.Serialize as Serialize
|
||||
@ -56,6 +59,12 @@ seedToInteger (Seed b) = Serialize.os2ip b
|
||||
seedFromInteger :: Integer -> Seed
|
||||
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
|
||||
drgNew :: MonadRandom randomly => randomly ChaChaDRG
|
||||
drgNew = drgNewSeed `fmap` seedNew
|
||||
|
||||
@ -14,7 +14,7 @@ module Crypto.Random.ChaChaDRG
|
||||
|
||||
import Crypto.Random.Types
|
||||
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 Foreign.Storable (pokeElemOff)
|
||||
|
||||
@ -29,7 +29,7 @@ newtype ChaChaDRG = ChaChaDRG C.StateSimple
|
||||
|
||||
-- | Initialize a new ChaCha context with the number of rounds,
|
||||
-- the key and the nonce associated.
|
||||
initialize :: ByteArray seed
|
||||
initialize :: B.ByteArrayAccess seed
|
||||
=> seed -- ^ 40 bytes of seed
|
||||
-> ChaChaDRG -- ^ the initial ChaCha state
|
||||
initialize seed = ChaChaDRG $ C.initializeSimple seed
|
||||
|
||||
Loading…
Reference in New Issue
Block a user