-- | Skein 256 as a PRNG.
module Crypto.Threefish.Random (
    SkeinGen, Block256, Random (..), RandomGen (..),
    newSkeinGen, mkSkeinGen, mkSkeinGenEx, randomBytes, reseedSkeinGen,
    toBlock, fromBlock
  ) where
import Crypto.Threefish.Skein
import Crypto.Threefish.Threefish256
import System.Random
import System.Entropy
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BSL
import Data.ByteString.Unsafe
import System.IO.Unsafe
import Foreign.Storable (sizeOf, peek)
import Foreign.Ptr (castPtr)
import Data.Serialize
import Crypto.Random
import Data.Tagged

emptyKey :: Key256
emptyKey :: Block256
emptyKey = ByteString -> Block256
Block256 ByteString
BS.empty

-- | Default amount of random bytes to buffer.
defaultSkeinGenPoolSize :: Int
defaultSkeinGenPoolSize :: Int
defaultSkeinGenPoolSize = Int
256

-- | Skein-based PRNG as defined in the Skein 1.3 paper.
data SkeinGen = SkeinGen {
    SkeinGen -> Block256
sgState    :: Block256,
    SkeinGen -> ByteString
sgPool     :: BS.ByteString,
    SkeinGen -> Int
sgPoolSize :: Int
  }

instance RandomGen SkeinGen where
  next :: SkeinGen -> (Int, SkeinGen)
next SkeinGen
g =
    case Int -> SkeinGen -> (ByteString, SkeinGen)
randomBytes (Int -> Int
forall a. Storable a => a -> Int
sizeOf (Int
0::Int)) SkeinGen
g of
      (ByteString
bs, SkeinGen
g') -> (IO Int -> Int
forall a. IO a -> a
unsafePerformIO (IO Int -> Int) -> IO Int -> Int
forall a b. (a -> b) -> a -> b
$ ByteString -> (CString -> IO Int) -> IO Int
forall a. ByteString -> (CString -> IO a) -> IO a
unsafeUseAsCString ByteString
bs ((CString -> IO Int) -> IO Int) -> (CString -> IO Int) -> IO Int
forall a b. (a -> b) -> a -> b
$ Ptr Int -> IO Int
forall a. Storable a => Ptr a -> IO a
peek (Ptr Int -> IO Int) -> (CString -> Ptr Int) -> CString -> IO Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CString -> Ptr Int
forall a b. Ptr a -> Ptr b
castPtr, SkeinGen
g')
  split :: SkeinGen -> (SkeinGen, SkeinGen)
split SkeinGen
g =
    case Int -> ByteString -> (ByteString, ByteString)
BS.splitAt Int
32 ((ByteString, SkeinGen) -> ByteString
forall a b. (a, b) -> a
fst ((ByteString, SkeinGen) -> ByteString)
-> (ByteString, SkeinGen) -> ByteString
forall a b. (a -> b) -> a -> b
$ Int -> SkeinGen -> (ByteString, SkeinGen)
randomBytes Int
64 SkeinGen
g) of
      (ByteString
a, ByteString
b) -> (Int -> Block256 -> SkeinGen
mkSkeinGenEx (SkeinGen -> Int
sgPoolSize SkeinGen
g) (ByteString -> Block256
Block256 ByteString
a),
                 Int -> Block256 -> SkeinGen
mkSkeinGenEx (SkeinGen -> Int
sgPoolSize SkeinGen
g) (ByteString -> Block256
Block256 ByteString
b))

-- | Create a new Skein PRNG from the system's entropy pool.
newSkeinGen :: IO SkeinGen
newSkeinGen :: IO SkeinGen
newSkeinGen =
  (Int -> Block256 -> SkeinGen
mkSkeinGenEx Int
defaultSkeinGenPoolSize (Block256 -> SkeinGen)
-> (ByteString -> Block256) -> ByteString -> SkeinGen
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Block256
Block256) (ByteString -> SkeinGen) -> IO ByteString -> IO SkeinGen
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` Int -> IO ByteString
getEntropy Int
32

-- | Create a Skein PRNG from a seed.
mkSkeinGen :: Serialize a => a -> SkeinGen
mkSkeinGen :: forall a. Serialize a => a -> SkeinGen
mkSkeinGen = Int -> Block256 -> SkeinGen
mkSkeinGenEx Int
defaultSkeinGenPoolSize (Block256 -> SkeinGen) -> (a -> Block256) -> a -> SkeinGen
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Block256
Block256 (ByteString -> Block256) -> (a -> ByteString) -> a -> Block256
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> ByteString
forall a. Serialize a => a -> ByteString
encode

-- | Create a Skein PRNG with a custom pool size. Larger pool sizes give faster
--   random data, but obviously take up more memory. Pool size is preserved
--   across splits.
mkSkeinGenEx :: Int -> Block256 -> SkeinGen
mkSkeinGenEx :: Int -> Block256 -> SkeinGen
mkSkeinGenEx Int
poolsize (Block256 ByteString
seed) = SkeinGen {
    sgState :: Block256
sgState    = ByteString -> Block256
forall a. Skein a => ByteString -> a
skein (ByteString -> Block256) -> ByteString -> Block256
forall a b. (a -> b) -> a -> b
$ ByteString -> ByteString
BSL.fromStrict (Int -> Word8 -> ByteString
BS.replicate Int
32 Word8
0 ByteString -> ByteString -> ByteString
`BS.append` ByteString
seed),
    sgPool :: ByteString
sgPool     = ByteString
BS.empty,
    sgPoolSize :: Int
sgPoolSize = Int
poolsize
  }

-- | Reseed a Skein PRNG.
reseedSkeinGen :: Block256 -> SkeinGen -> SkeinGen
reseedSkeinGen :: Block256 -> SkeinGen -> SkeinGen
reseedSkeinGen (Block256 ByteString
seed) (SkeinGen (Block256 ByteString
state) ByteString
_ Int
poolsize) =
  SkeinGen {
    sgState :: Block256
sgState    = ByteString -> Block256
forall a. Skein a => ByteString -> a
skein (ByteString -> Block256) -> ByteString -> Block256
forall a b. (a -> b) -> a -> b
$ ByteString -> ByteString
BSL.fromStrict (ByteString
state ByteString -> ByteString -> ByteString
`BS.append` ByteString
seed),
    sgPool :: ByteString
sgPool     = ByteString
BS.empty,
    sgPoolSize :: Int
sgPoolSize = Int
poolsize
  }

-- | Generate n random bytes using the given generator.
randomBytes :: Int -> SkeinGen -> (BS.ByteString, SkeinGen)
randomBytes :: Int -> SkeinGen -> (ByteString, SkeinGen)
randomBytes Int
nbytes (SkeinGen (Block256 ByteString
state) ByteString
pool Int
poolsize)
  | ByteString -> Int
BS.length ByteString
pool Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
nbytes =
    case Int -> ByteString -> (ByteString, ByteString)
BS.splitAt Int
nbytes ByteString
pool of
      (ByteString
output, ByteString
rest) -> (ByteString
output, Block256 -> ByteString -> Int -> SkeinGen
SkeinGen (ByteString -> Block256
Block256 ByteString
state) ByteString
rest Int
poolsize)
  | Bool
otherwise =
      (ByteString -> ByteString -> ByteString
BS.append ByteString
pool ByteString
out, Block256 -> ByteString -> Int -> SkeinGen
SkeinGen (ByteString -> Block256
Block256 ByteString
state') ByteString
pool' Int
poolsize)
  where
    -- Use all of the output to avoid making unnecessary calls
    nbytes' :: Word64
nbytes' = Int -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word64) -> Int -> Word64
forall a b. (a -> b) -> a -> b
$ Int
32 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int -> Int -> Int
forall a. Ord a => a -> a -> a
max (Int
nbytes Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (Int
32Int -> Int -> Int
forall a. Num a => a -> a -> a
-(Int
nbytesInt -> Int -> Int
forall a. Integral a => a -> a -> a
`rem`Int
32))) Int
poolsize
    bytes :: ByteString
bytes = Word64 -> Block256 -> ByteString -> ByteString
hash256 Word64
nbytes' Block256
emptyKey (ByteString -> ByteString
BSL.fromStrict ByteString
state)
    (ByteString
state', ByteString
buffer) = Int -> ByteString -> (ByteString, ByteString)
BS.splitAt Int
32 ByteString
bytes
    (ByteString
out, ByteString
pool') = Int -> ByteString -> (ByteString, ByteString)
BS.splitAt (Int
nbytes Int -> Int -> Int
forall a. Num a => a -> a -> a
- ByteString -> Int
BS.length ByteString
pool) ByteString
buffer

instance CryptoRandomGen SkeinGen where
  newGen :: ByteString -> Either GenError SkeinGen
newGen ByteString
seed =
    case ByteString -> Int
BS.length ByteString
seed of
      Int
n | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
32 ->
          SkeinGen -> Either GenError SkeinGen
forall a b. b -> Either a b
Right (SkeinGen -> Either GenError SkeinGen)
-> SkeinGen -> Either GenError SkeinGen
forall a b. (a -> b) -> a -> b
$ Int -> Block256 -> SkeinGen
mkSkeinGenEx Int
ps (ByteString -> Block256
Block256 (ByteString -> Block256) -> ByteString -> Block256
forall a b. (a -> b) -> a -> b
$ Int -> ByteString -> ByteString
BS.take Int
32 ByteString
seed)
        | Bool
otherwise ->
          GenError -> Either GenError SkeinGen
forall a b. a -> Either a b
Left GenError
NotEnoughEntropy
    where ps :: Int
ps = Int
defaultSkeinGenPoolSize
  genSeedLength :: Tagged SkeinGen Int
genSeedLength = Int -> Tagged SkeinGen Int
forall {k} (s :: k) b. b -> Tagged s b
Tagged Int
32
  genBytes :: Int -> SkeinGen -> Either GenError (ByteString, SkeinGen)
genBytes Int
n SkeinGen
g = (ByteString, SkeinGen) -> Either GenError (ByteString, SkeinGen)
forall a b. b -> Either a b
Right ((ByteString, SkeinGen) -> Either GenError (ByteString, SkeinGen))
-> (ByteString, SkeinGen) -> Either GenError (ByteString, SkeinGen)
forall a b. (a -> b) -> a -> b
$ Int -> SkeinGen -> (ByteString, SkeinGen)
randomBytes Int
n SkeinGen
g
  reseedInfo :: SkeinGen -> ReseedInfo
reseedInfo = ReseedInfo -> SkeinGen -> ReseedInfo
forall a b. a -> b -> a
const ReseedInfo
Never
  reseedPeriod :: SkeinGen -> ReseedInfo
reseedPeriod = ReseedInfo -> SkeinGen -> ReseedInfo
forall a b. a -> b -> a
const ReseedInfo
Never
  reseed :: ByteString -> SkeinGen -> Either GenError SkeinGen
reseed ByteString
seed SkeinGen
g =
    case ByteString -> Int
BS.length ByteString
seed of
      Int
n | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
32 ->
          SkeinGen -> Either GenError SkeinGen
forall a b. b -> Either a b
Right (SkeinGen -> Either GenError SkeinGen)
-> SkeinGen -> Either GenError SkeinGen
forall a b. (a -> b) -> a -> b
$ Block256 -> SkeinGen -> SkeinGen
reseedSkeinGen (ByteString -> Block256
Block256 (ByteString -> Block256) -> ByteString -> Block256
forall a b. (a -> b) -> a -> b
$ Int -> ByteString -> ByteString
BS.take Int
32 ByteString
seed) SkeinGen
g
        | Bool
otherwise ->
          GenError -> Either GenError SkeinGen
forall a b. a -> Either a b
Left GenError
NotEnoughEntropy