{-# LANGUAGE CPP #-}
{-# LANGUAGE BangPatterns #-}
module Crypto.Random.AESCtr.Internal where
import qualified Crypto.Cipher.AES as AES
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
data RNG = RNG !AES.AESIV !Int !AES.AES
getNbChunksGenerated :: RNG -> Int
getNbChunksGenerated :: RNG -> Int
getNbChunksGenerated (RNG AESIV
_ Int
c AES
_) = Int
c
makeParams :: ByteString -> (AES.AES, AES.AESIV)
makeParams :: ByteString -> (AES, AESIV)
makeParams ByteString
b = AES
key AES -> (AES, AESIV) -> (AES, AESIV)
forall a b. a -> b -> b
`seq` AESIV
iv AESIV -> (AES, AESIV) -> (AES, AESIV)
forall a b. a -> b -> b
`seq` (AES
key, AESIV
iv)
where (ByteString
keyBS, ByteString
r1) = Int -> ByteString -> (ByteString, ByteString)
B.splitAt Int
32 ByteString
b
(ByteString
cnt, ByteString
_) = Int -> ByteString -> (ByteString, ByteString)
B.splitAt Int
16 ByteString
r1
!key :: AES
key = ByteString -> AES
forall b. Byteable b => b -> AES
AES.initAES ByteString
keyBS
!iv :: AESIV
iv = ByteString -> AESIV
AES.aesIV_ (ByteString -> AESIV) -> ByteString -> AESIV
forall a b. (a -> b) -> a -> b
$ ByteString -> ByteString
B.copy ByteString
cnt
makeRNG :: ByteString -> RNG
makeRNG :: ByteString -> RNG
makeRNG ByteString
b = AESIV -> Int -> AES -> RNG
RNG AESIV
iv Int
0 AES
key
where (AES
key,AESIV
iv) = ByteString -> (AES, AESIV)
makeParams ByteString
b
chunkSize :: Int
chunkSize :: Int
chunkSize = Int
1024
genNextChunk :: RNG -> (ByteString, RNG)
genNextChunk :: RNG -> (ByteString, RNG)
genNextChunk (RNG AESIV
counter Int
nbChunks AES
key) =
ByteString
chunk ByteString -> (ByteString, RNG) -> (ByteString, RNG)
forall a b. a -> b -> b
`seq` RNG
newrng RNG -> (ByteString, RNG) -> (ByteString, RNG)
forall a b. a -> b -> b
`seq` (ByteString
chunk, RNG
newrng)
where
newrng :: RNG
newrng = AESIV -> Int -> AES -> RNG
RNG AESIV
newCounter (Int
nbChunksInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1) AES
key
(ByteString
chunk,AESIV
newCounter) = AES -> AESIV -> Int -> (ByteString, AESIV)
AES.genCounter AES
key AESIV
counter Int
chunkSize