DRBG 0.1.4 → 0.2
raw patch · 2 files changed
+68/−10 lines, 2 filesdep ~basedep ~cerealdep ~crypto-apiPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: base, cereal, crypto-api, mtl, tagged
API changes (from Hackage documentation)
+ Crypto.Random.DRBG: data GenCounter a
+ Crypto.Random.DRBG: instance BlockCipher x => CryptoRandomGen (GenCounter x)
Files
- Crypto/Random/DRBG.hs +62/−4
- DRBG.cabal +6/−6
Crypto/Random/DRBG.hs view
@@ -33,17 +33,27 @@ -} module Crypto.Random.DRBG- ( HmacDRBG, HashDRBG+ (+ -- * Basic Hash-based Generators+ HmacDRBG, HashDRBG , HmacDRBGWith, HashDRBGWith+ -- * Basic Cipher-based Generator+ , GenCounter+ -- * CryptoRandomGen Transformers , GenXor- , GenAutoReseed, newGenAutoReseed, newGenAutoReseedIO , GenBuffered+ , GenAutoReseed+ -- * AutoReseed generator construction with custom reseed interval+ , newGenAutoReseed, newGenAutoReseedIO+ -- * Helper Re-exports , module Crypto.Random ) where import qualified Crypto.Random.DRBG.HMAC as M import qualified Crypto.Random.DRBG.Hash as H+import Crypto.Random.DRBG.Util import Crypto.Classes+import Crypto.Modes import Crypto.Random import Crypto.Hash.SHA512 (SHA512) import Crypto.Hash.SHA384 (SHA384)@@ -52,14 +62,14 @@ import Crypto.Hash.SHA1 (SHA1) import System.Crypto.Random import qualified Data.ByteString as B-import qualified Data.ByteString.Lazy as L+import qualified Data.ByteString.Internal as BI import Data.Tagged import Data.Proxy import Data.Bits (xor) import Control.Parallel import Control.Monad (liftM) import Control.Monad.Error () -- Either instance-import System.IO.Unsafe (unsafeInterleaveIO)+import Data.Serialize (encode) instance H.SeedLength SHA512 where seedlen = Tagged 888@@ -216,6 +226,11 @@ {-# SPECIALIZE instance CryptoRandomGen (GenAutoReseed HashDRBG HmacDRBG) #-} {-# SPECIALIZE instance CryptoRandomGen (GenAutoReseed HmacDRBG HashDRBG) #-} newGen bs = newGenAutoReseed bs (2^15)+ newGenIO = do+ b <- newGenAutoReseedIO (2^15)+ case b of+ Left err -> error $ show err+ Right g -> return g genSeedLength = let a = helper1 res b = helper2 res@@ -285,6 +300,10 @@ a <- g1 b <- g2 return (GenXor a b)+ newGenIO = do+ a <- newGenIO+ b <- newGenIO+ return (GenXor a b) genSeedLength = let a = helperXor1 res b = helperXor2 res@@ -379,6 +398,45 @@ eval :: Either x (B.ByteString, g) -> Either x (B.ByteString, g) eval (Left x) = Left x eval (Right (g,bs)) = bs `seq` (g `seq` (Right (g, bs)))++-- |@GenCounter k@ is a cryptographic BlockCipher with key @k@+-- being used in 'ctr' mode to generate random bytes.+data GenCounter a = GenCounter a (IV a)++instance BlockCipher x => CryptoRandomGen (GenCounter x) where+ newGen bytes = case buildKey bytes of Nothing -> Left NotEnoughEntropy+ Just x -> Right (GenCounter x zeroIV)+ newGenIO = do+ let b = keyLength+ kd <- getEntropy ((untag b + 7) `div` 8)+ case buildKey kd of+ Nothing -> error "Failed to generate key for GenCounter"+ Just k -> return $ GenCounter (k `asTaggedTypeOf` b) zeroIV+ genSeedLength =+ let rt :: Tagged x Int -> Tagged (GenCounter x) Int+ rt = Tagged . (`div` 8) . unTagged+ in rt keyLength++ -- If this is called for less than blockSize data + genBytes req (GenCounter k counter) =+ let bs = B.replicate req' 0+ blkSz = blockSizeBytes `for` k+ (rnd,iv) = ctr' incIV k counter bs+ req' = (req + blkSz - 1) `div` blkSz+ in Right (B.take req rnd, GenCounter k iv)++ reseed bs (GenCounter k _) = newGen (xorExtendBS (encode k) bs)++xorExtendBS a b = res+ where+ x = B.pack $ B.zipWith Data.Bits.xor a b+ res | al /= bl = x+ | otherwise = B.append x rem+ al = B.length a+ bl = B.length b+ rem | bl > al = B.drop al b+ | otherwise = B.drop bl a+ -- |zipWith xor + Pack -- As a result of rewrite rules, this should automatically be optimized (at compile time)
DRBG.cabal view
@@ -1,11 +1,11 @@ name: DRBG-version: 0.1.4+version: 0.2 license: BSD3 license-file: LICENSE author: Thomas DuBuisson <thomas.dubuisson@gmail.com> maintainer: Thomas DuBuisson-description: A deterministic random bit generator implementing Hash and HMAC based DRBGs from NIST SP 800-90.-synopsis: A deterministic random bit generator (aka RNG, PRNG) implementing DRBGs from NIST SP 800-90+description: Cryptographically secure RNGs+synopsis: Deterministic random bit generator (aka RNG, PRNG) based HMACs, Hashes, and Ciphers. category: Cryptography stability: stable build-type: Simple@@ -20,9 +20,9 @@ default: False Library- Build-Depends: base >= 4.0 && < 4.4, cereal >= 0.2 && <= 0.4,- bytestring, prettyclass, tagged >= 0.2 && < 0.3,- crypto-api >= 0.2, cryptohash >= 0.6.1, parallel, mtl == 2.*+ Build-Depends: base >= 4.0 && < 5, cereal >= 0.2,+ bytestring, prettyclass, tagged >= 0.2,+ crypto-api >= 0.6, cryptohash >= 0.6.1, parallel, mtl >= 2.0 ghc-options: -O2 hs-source-dirs: exposed-modules: Crypto.Random.DRBG.Hash, Crypto.Random.DRBG.HMAC, Crypto.Random.DRBG, Crypto.Random.DRBG.Types