[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
|
-- 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