cprng-aes-0.6.1/0000755000000000000000000000000012431112703011555 5ustar0000000000000000cprng-aes-0.6.1/cprng-aes.cabal0000644000000000000000000000426612431112703014430 0ustar0000000000000000Name: cprng-aes Version: 0.6.1 Description: Simple crypto pseudo-random-number-generator with really good randomness property. . Using ent, a randomness property maker on one 1Mb sample: Entropy = 7.999837 bits per byte. Optimum compression would reduce the size of this 1048576 byte file by 0 percent. Chi square distribution for 1048576 samples is 237.02 Arithmetic mean value of data bytes is 127.3422 (127.5 = random) Monte Carlo value for Pi is 3.143589568 (error 0.06 percent) . Compared to urandom with the same sampling: Entropy = 7.999831 bits per byte. Optimum compression would reduce the size of this 1048576 byte file by 0 percent. Chi square distribution for 1048576 samples is 246.63 Arithmetic mean value of data bytes is 127.6347 (127.5 = random). Monte Carlo value for Pi is 3.132465868 (error 0.29 percent). License: BSD3 License-file: LICENSE Copyright: Vincent Hanquez Author: Vincent Hanquez Maintainer: Vincent Hanquez Synopsis: Crypto Pseudo Random Number Generator using AES in counter mode. Build-Type: Simple Category: Cryptography stability: experimental Cabal-Version: >=1.8 Homepage: http://github.com/vincenthz/hs-cprng-aes data-files: README.md Library Build-Depends: base >= 3 && < 5 , bytestring , byteable , crypto-random >= 0.0.7 && < 0.1 , cipher-aes >= 0.2.9 && < 0.3 Exposed-modules: Crypto.Random.AESCtr Other-modules: Crypto.Random.AESCtr.Internal ghc-options: -Wall Benchmark bench-cprng-aes hs-source-dirs: Benchmarks Main-Is: Benchmarks.hs type: exitcode-stdio-1.0 Build-depends: base >= 4 && < 5 , bytestring , crypto-random , cprng-aes , criterion , mtl source-repository head type: git location: git://github.com/vincenthz/hs-cprng-aes cprng-aes-0.6.1/LICENSE0000644000000000000000000000273112431112703012565 0ustar0000000000000000Copyright (c) 2010-2011 Vincent Hanquez All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the author nor the names of his contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. cprng-aes-0.6.1/README.md0000644000000000000000000000207312431112703013036 0ustar0000000000000000CPRNG-AES ========= This module provides a crypto pseudo random number generator using AES in counter mode. to import: import Crypto.Random.AESCtr to use: rng <- makeSystem let (ran, rng') = getRandomBytes rng 1024 it's also an instance of CryptoRandomGen from the crypto-api package. Statistics ---------- Using ent, a randomness property maker on one 1Mb sample. cprng-AES: Entropy = 7.999837 bits per byte. Optimum compression would reduce the size of this 1048576 byte file by 0 percent. Chi square distribution for 1048576 samples is 237.02. Arithmetic mean value of data bytes is 127.3422 (127.5 = random). Monte Carlo value for Pi is 3.143589568 (error 0.06 percent). Compared to urandom with the same sampling: Entropy = 7.999831 bits per byte. Optimum compression would reduce the size of this 1048576 byte file by 0 percent. Chi square distribution for 1048576 samples is 246.63. Arithmetic mean value of data bytes is 127.6347 (127.5 = random). Monte Carlo value for Pi is 3.132465868 (error 0.29 percent). cprng-aes-0.6.1/Setup.hs0000644000000000000000000000005612431112703013212 0ustar0000000000000000import Distribution.Simple main = defaultMain cprng-aes-0.6.1/Benchmarks/0000755000000000000000000000000012431112703013632 5ustar0000000000000000cprng-aes-0.6.1/Benchmarks/Benchmarks.hs0000644000000000000000000000220612431112703016243 0ustar0000000000000000module Main where import Criterion.Main import qualified Data.ByteString as B import Crypto.Random.AESCtr import Crypto.Random import System.IO.Unsafe (unsafePerformIO) import Data.IORef gen rng n = fst (cprgGenerate n rng) gen2 rngref n = unsafePerformIO $ do rng <- readIORef rngref let (b, rng2) = cprgGenerate n rng writeIORef rngref rng2 return b main = do rng <- makeSystem rngref <- newIORef rng defaultMain [ bgroup "generate random bytes (init)" [ bench "1" $ nf (gen rng) 1 , bench "8" $ nf (gen rng) 8 , bench "16" $ nf (gen rng) 16 , bench "256" $ nf (gen rng) 256 , bench "1024" $ nf (gen rng) 1024 , bench "4096" $ nf (gen rng) 4096 ] , bgroup "generate random bytes (continous)" [ bench "1" $ nf (gen2 rngref) 1 , bench "8" $ nf (gen2 rngref) 8 , bench "16" $ nf (gen2 rngref) 16 , bench "256" $ nf (gen2 rngref) 256 , bench "1024" $ nf (gen2 rngref) 1024 , bench "4096" $ nf (gen2 rngref) 4096 ] ] cprng-aes-0.6.1/Crypto/0000755000000000000000000000000012431112703013035 5ustar0000000000000000cprng-aes-0.6.1/Crypto/Random/0000755000000000000000000000000012431112703014255 5ustar0000000000000000cprng-aes-0.6.1/Crypto/Random/AESCtr.hs0000644000000000000000000001073612431112703015701 0ustar0000000000000000-- | -- Module : Crypto.Random.AESCtr -- License : BSD-style -- Maintainer : Vincent Hanquez -- Stability : stable -- Portability : unknown -- -- this CPRNG is an AES based counter system. -- -- the internal size of fields are: 16 bytes IV, 16 bytes counter, 32 bytes key -- -- each block are generated the following way: -- aes (IV `xor` counter) -> 16 bytes output -- {-# LANGUAGE CPP #-} {-# LANGUAGE BangPatterns #-} module Crypto.Random.AESCtr ( AESRNG , make , makeSystem ) where import Crypto.Random import Crypto.Random.AESCtr.Internal import Control.Arrow (second) import Data.ByteString (ByteString) import qualified Data.ByteString as B import Data.Byteable import Data.Bits (xor, (.&.)) -- | AES Counter mode Pseudo random generator. -- -- Provide a very good Cryptographic pseudo random generator -- that create pseudo random output based an AES cipher -- used in counter mode, initialized from random key, random IV -- and random nonce. -- -- This CPRG uses 64 bytes of pure entropy to create its random state. -- -- By default, this generator will automatically reseed after generating -- 1 megabyte of data. data AESRNG = AESRNG { aesrngState :: !RNG , aesrngEntropy :: EntropyPool , aesrngThreshold :: !Int -- ^ in number of generated block , aesrngCache :: !ByteString } instance Show AESRNG where show _ = "aesrng[..]" makeFrom :: EntropyPool -> B.ByteString -> AESRNG makeFrom entPool b = AESRNG { aesrngState = rng , aesrngEntropy = entPool , aesrngThreshold = 1024 -- in blocks generated, so 1mb , aesrngCache = B.empty } where rng = makeRNG b -- | make an AES RNG from an EntropyPool. -- -- use `makeSystem` to not have to deal with the entropy pool. make :: EntropyPool -> AESRNG make entPool = makeFrom entPool b where !b = toBytes $ grabEntropy 64 entPool -- | Initialize a new AES RNG using the system entropy. -- {-# DEPRECATED makeSystem "use cprgCreate with an entropy pool" #-} makeSystem :: IO AESRNG makeSystem = make `fmap` createEntropyPool -- | get a Random number of bytes from the RNG. -- it generate randomness by block of chunkSize bytes and will returns -- a block bigger or equal to the size requested. genRandomBytesState :: RNG -> Int -> (ByteString, RNG) genRandomBytesState rng n | n <= chunkSize = genNextChunk rng | otherwise = let (bs, rng') = acc 0 [] rng in (B.concat bs, rng') where acc l bs g | l * chunkSize >= n = (bs, g) | otherwise = let (b, g') = genNextChunk g in acc (l+1) (b:bs) g' genRanBytesNoCheck :: AESRNG -> Int -> (ByteString, AESRNG) genRanBytesNoCheck rng n | B.length (aesrngCache rng) >= n = let (b1,b2) = B.splitAt n (aesrngCache rng) in (b1, rng { aesrngCache = b2 }) | otherwise = let (b, rng') = genRandomBytesState (aesrngState rng) n (b1, b2) = B.splitAt n b in (b1, rng { aesrngState = rng', aesrngCache = b2 }) -- | generate a random set of bytes genRanBytes :: AESRNG -> Int -> (ByteString, AESRNG) genRanBytes rng n = second reseedThreshold $ genRanBytesNoCheck rng n reseedThreshold :: AESRNG -> AESRNG reseedThreshold rng | getNbChunksGenerated (aesrngState rng) >= lvl = let newRngState = makeRNG $ toBytes $ grabEntropy 64 (aesrngEntropy rng) in rng { aesrngState = newRngState } | otherwise = rng where lvl = aesrngThreshold rng instance CPRG AESRNG where cprgCreate = make cprgSetReseedThreshold lvl rng = reseedThreshold (rng { aesrngThreshold = if nbChunks > 0 then nbChunks else 1 }) where nbChunks = lvl `div` chunkSize cprgGenerate len rng = genRanBytes rng len cprgGenerateWithEntropy len rng = let ent = toBytes $ grabEntropy len (aesrngEntropy rng) (bs, rng') = genRanBytes rng len in (B.pack $ B.zipWith xor ent bs, rng') cprgFork rng = let (b,rng') = genRanBytes rng 64 in (rng', makeFrom (aesrngEntropy rng) b) {- instance RandomGen AESRNG where next rng = let (bs, rng') = genRanBytes rng 16 in let (Word128 a _) = get128 bs in let n = fromIntegral (a .&. 0x7fffffff) in (n, rng') split rng = let rng' = make (aesrngEntropy rng) in (rng, rng') genRange _ = (0, 0x7fffffff) -} cprng-aes-0.6.1/Crypto/Random/AESCtr/0000755000000000000000000000000012431112703015336 5ustar0000000000000000cprng-aes-0.6.1/Crypto/Random/AESCtr/Internal.hs0000644000000000000000000000224312431112703017447 0ustar0000000000000000-- | -- Module : Crypto.Random.AESCtr.Internal -- License : BSD-style -- Maintainer : Vincent Hanquez -- Stability : stable -- Portability : unknown -- {-# 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 {-| An opaque object containing an AES CPRNG -} data RNG = RNG !AES.AESIV !Int !AES.AES getNbChunksGenerated :: RNG -> Int getNbChunksGenerated (RNG _ c _) = c makeParams :: ByteString -> (AES.AES, AES.AESIV) makeParams b = key `seq` iv `seq` (key, iv) where (keyBS, r1) = B.splitAt 32 b (cnt, _) = B.splitAt 16 r1 !key = AES.initAES keyBS !iv = AES.aesIV_ $ B.copy cnt makeRNG :: ByteString -> RNG makeRNG b = RNG iv 0 key where (key,iv) = makeParams b chunkSize :: Int chunkSize = 1024 genNextChunk :: RNG -> (ByteString, RNG) genNextChunk (RNG counter nbChunks key) = chunk `seq` newrng `seq` (chunk, newrng) where newrng = RNG newCounter (nbChunks+1) key (chunk,newCounter) = AES.genCounter key counter chunkSize