summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Crypto/Threefish/Authenticated.hs11
-rw-r--r--Crypto/Threefish/Skein.hs7
-rw-r--r--threefish.cabal18
3 files changed, 24 insertions, 12 deletions
diff --git a/Crypto/Threefish/Authenticated.hs b/Crypto/Threefish/Authenticated.hs
index d0e0e3a..97d9a31 100644
--- a/Crypto/Threefish/Authenticated.hs
+++ b/Crypto/Threefish/Authenticated.hs
@@ -22,10 +22,19 @@ import Control.Monad
prng :: IORef SkeinGen
prng = unsafePerformIO $ newSkeinGen >>= newIORef
+-- | Strict version of 'atomicModifyIORef'. This forces both the value stored
+-- in the 'IORef' as well as the value returned.
+atomicModIORef' :: IORef a -> (a -> (a,b)) -> IO b
+atomicModIORef' ref f = do
+ b <- atomicModifyIORef ref
+ (\x -> let (a, b) = f x
+ in (a, a `seq` b))
+ b `seq` return b
+
-- | Generate a 256 bit nonce using the Skein PRNG.
generateNonce :: IO Nonce256
generateNonce =
- Block256 `fmap` atomicModifyIORef' prng (pflip . randomBytes 32)
+ Block256 `fmap` atomicModIORef' prng (pflip . randomBytes 32)
where
pflip (a, b) = (b, a)
diff --git a/Crypto/Threefish/Skein.hs b/Crypto/Threefish/Skein.hs
index aaac98b..96e0315 100644
--- a/Crypto/Threefish/Skein.hs
+++ b/Crypto/Threefish/Skein.hs
@@ -58,7 +58,7 @@ update256 (Skein256Ctx c) outlen bytes =
BS.packCStringLen (castPtr ptr, outlen)
| otherwise -> do
let !chunk' =
- BSL.toStrict chunk
+ toStrict chunk
(!lst, !len) =
if BSL.null rest
then (2, fromIntegral $ BS.length chunk')
@@ -67,6 +67,9 @@ update256 (Skein256Ctx c) outlen bytes =
skein256_update ctx (first .|. lst) msgtype len (castPtr ptr)
go 0 rest ctx
+toStrict :: BSL.ByteString -> BS.ByteString
+toStrict = BS.concat . BSL.toChunks
+
hash256 :: Word64 -> Key256 -> BSL.ByteString -> BS.ByteString
hash256 outlen k bs =
case init256 k outlen of
@@ -75,7 +78,7 @@ hash256 outlen k bs =
{-# INLINE skein256 #-}
-- | Hash a message using 256 bit Skein.
skein256 :: BSL.ByteString -> Block256
-skein256 = Block256 . hash256 32 (Block256 "")
+skein256 = Block256 . hash256 32 (Block256 BS.empty)
{-# INLINE skeinMAC256 #-}
-- | Create a 256 bit Skein-MAC.
diff --git a/threefish.cabal b/threefish.cabal
index c34bc7b..37bc47e 100644
--- a/threefish.cabal
+++ b/threefish.cabal
@@ -1,5 +1,5 @@
name: threefish
-version: 0.2.5
+version: 0.2.6
synopsis: The Threefish block cipher and the Skein hash function for Haskell.
description: Implements 256 and 512 bit variants of Threefish and Skein. Skein is usable as a "normal" hash function as well as in Skein-MAC, as a cryptographically secure PRNG, as a stream cipher and as a key derivation function, all implemented according to the specifications of the Skein 1.3 paper.
homepage: http://github.com/valderman/threefish
@@ -37,15 +37,15 @@ library
MultiParamTypeClasses,
FunctionalDependencies
build-depends:
- base >=4.6 && <5,
- bytestring >=0.10,
- cereal >=0.3,
- array >=0.4,
- crypto-api >=0.12,
- tagged >=0.4,
- data-default >=0.5,
+ base >=4.5 && <5,
+ bytestring >=0.10.4.0,
+ cereal >=0.4.0.1,
+ array >=0.4.0.0,
+ crypto-api >=0.12.2.2,
+ tagged >=0.7,
+ data-default >=0.5.3,
random,
- entropy >= 0.2.2.2
+ entropy
default-language: Haskell2010
ghc-options: -Wall -O2
include-dirs: cbits