{-# LANGUAGE BangPatterns, OverloadedStrings, MultiParamTypeClasses #-}
module Crypto.Threefish.Skein (
Skein (..), Threefish (..), Block256, Block512, Key256, Key512, Nonce256,
hash256, hash512
) where
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BSL
import Crypto.Threefish.Threefish256
import Crypto.Threefish.Threefish512
import Crypto.Threefish.UBI
import Crypto.Threefish
import Crypto.Threefish.Skein.Internal
import Data.Bits
import Data.Serialize
import Data.Word
import Data.ByteString.Unsafe
import Foreign.Ptr
import Foreign.ForeignPtr
import Foreign.Marshal.Alloc
import System.IO.Unsafe
class Skein a where
skeinMAC :: a -> BSL.ByteString -> a
skein :: BSL.ByteString -> a
type Nonce256 = Block256
init256 :: Key256 -> Word64 -> Skein256Ctx
init256 :: Key256 -> Word64 -> Skein256Ctx
init256 (Block256 ByteString
k) Word64
outlen =
IO Skein256Ctx -> Skein256Ctx
forall a. IO a -> a
unsafePerformIO (IO Skein256Ctx -> Skein256Ctx) -> IO Skein256Ctx -> Skein256Ctx
forall a b. (a -> b) -> a -> b
$ do
ForeignPtr Word64
c <- Int -> IO (ForeignPtr Word64)
forall a. Int -> IO (ForeignPtr a)
mallocForeignPtrBytes Int
64
ForeignPtr Word64 -> (Ptr Word64 -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word64
c ((Ptr Word64 -> IO ()) -> IO ()) -> (Ptr Word64 -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr Word64
ctx -> do
(Ptr Any -> IO ()) -> IO ()
forall {b} {a}. (Ptr b -> IO a) -> IO a
withKey ((Ptr Any -> IO ()) -> IO ()) -> (Ptr Any -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr Any
key -> do
Ptr Word64 -> Ptr Word64 -> Word64 -> IO ()
skein256_init Ptr Word64
ctx (Ptr Any -> Ptr Word64
forall a b. Ptr a -> Ptr b
castPtr Ptr Any
key) (Word64
outlenWord64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
*Word64
8)
Skein256Ctx -> IO Skein256Ctx
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (ForeignPtr Word64 -> Skein256Ctx
Skein256Ctx ForeignPtr Word64
c)
where
withKey :: (Ptr b -> IO a) -> IO a
withKey Ptr b -> IO a
f | ByteString -> Int
BS.length ByteString
k Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
32 = ByteString -> (CString -> IO a) -> IO a
forall a. ByteString -> (CString -> IO a) -> IO a
unsafeUseAsCString ByteString
k (Ptr b -> IO a
f (Ptr b -> IO a) -> (CString -> Ptr b) -> CString -> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CString -> Ptr b
forall a b. Ptr a -> Ptr b
castPtr)
| Bool
otherwise = Ptr b -> IO a
f Ptr b
forall a. Ptr a
nullPtr
update256 :: Skein256Ctx -> Int -> BSL.ByteString -> BS.ByteString
update256 :: Skein256Ctx -> Int -> ByteString -> ByteString
update256 (Skein256Ctx ForeignPtr Word64
c) Int
outlen ByteString
bytes =
IO ByteString -> ByteString
forall a. IO a -> a
unsafePerformIO (IO ByteString -> ByteString) -> IO ByteString -> ByteString
forall a b. (a -> b) -> a -> b
$ ForeignPtr Word64 -> (Ptr Word64 -> IO ByteString) -> IO ByteString
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word64
c ((Ptr Word64 -> IO ByteString) -> IO ByteString)
-> (Ptr Word64 -> IO ByteString) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ Int -> ByteString -> Ptr Word64 -> IO ByteString
go Int
1 ByteString
bytes
where
outblocks :: Int
outblocks =
case Int
outlen Int -> Int -> (Int, Int)
forall a. Integral a => a -> a -> (a, a)
`quotRem` Int
32 of
(Int
blocks, Int
0) -> Int
blocks
(Int
blocks, Int
_) -> Int
blocksInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1
!msgtype :: Int
msgtype = BlockType -> Int
type2int BlockType
Message
go :: Int -> ByteString -> Ptr Word64 -> IO ByteString
go !Int
first !ByteString
msg !Ptr Word64
ctx = do
case Int64 -> ByteString -> (ByteString, ByteString)
BSL.splitAt Int64
16384 ByteString
msg of
(ByteString
chunk, ByteString
rest)
| ByteString -> Bool
BSL.null ByteString
chunk ->
Int -> (Ptr Word64 -> IO ByteString) -> IO ByteString
forall a b. Int -> (Ptr a -> IO b) -> IO b
allocaBytes (Int
outblocksInt -> Int -> Int
forall a. Num a => a -> a -> a
*Int
32) ((Ptr Word64 -> IO ByteString) -> IO ByteString)
-> (Ptr Word64 -> IO ByteString) -> IO ByteString
forall a b. (a -> b) -> a -> b
$ \Ptr Word64
ptr -> do
Ptr Word64 -> Int -> Int -> Ptr Word64 -> IO ()
skein256_output Ptr Word64
ctx Int
0 (Int
outblocksInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1) Ptr Word64
ptr
CStringLen -> IO ByteString
BS.packCStringLen (Ptr Word64 -> CString
forall a b. Ptr a -> Ptr b
castPtr Ptr Word64
ptr, Int
outlen)
| Bool
otherwise -> do
let !chunk' :: ByteString
chunk' =
ByteString -> ByteString
toStrict ByteString
chunk
(!Int
lst, !Word64
len) =
if ByteString -> Bool
BSL.null ByteString
rest
then (Int
2, Int -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word64) -> Int -> Word64
forall a b. (a -> b) -> a -> b
$ ByteString -> Int
BS.length ByteString
chunk')
else (Int
0, Word64
16384)
ByteString -> (CString -> IO ()) -> IO ()
forall a. ByteString -> (CString -> IO a) -> IO a
unsafeUseAsCString ByteString
chunk' ((CString -> IO ()) -> IO ()) -> (CString -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CString
ptr -> do
Ptr Word64 -> Int -> Int -> Word64 -> Ptr Word64 -> IO ()
skein256_update Ptr Word64
ctx (Int
first Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. Int
lst) Int
msgtype Word64
len (CString -> Ptr Word64
forall a b. Ptr a -> Ptr b
castPtr CString
ptr)
Int -> ByteString -> Ptr Word64 -> IO ByteString
go Int
0 ByteString
rest Ptr Word64
ctx
toStrict :: BSL.ByteString -> BS.ByteString
toStrict :: ByteString -> ByteString
toStrict = [ByteString] -> ByteString
BS.concat ([ByteString] -> ByteString)
-> (ByteString -> [ByteString]) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> [ByteString]
BSL.toChunks
hash256 :: Word64 -> Key256 -> BSL.ByteString -> BS.ByteString
hash256 :: Word64 -> Key256 -> ByteString -> ByteString
hash256 Word64
outlen Key256
k ByteString
bs =
case Key256 -> Word64 -> Skein256Ctx
init256 Key256
k Word64
outlen of
Skein256Ctx
ctx -> Skein256Ctx -> Int -> ByteString -> ByteString
update256 Skein256Ctx
ctx (Word64 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
outlen) ByteString
bs
{-# INLINE skein256 #-}
skein256 :: BSL.ByteString -> Block256
skein256 :: ByteString -> Key256
skein256 = ByteString -> Key256
Block256 (ByteString -> Key256)
-> (ByteString -> ByteString) -> ByteString -> Key256
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> Key256 -> ByteString -> ByteString
hash256 Word64
32 (ByteString -> Key256
Block256 ByteString
BS.empty)
{-# INLINE skeinMAC256 #-}
skeinMAC256 :: Key256 -> BSL.ByteString -> Block256
skeinMAC256 :: Key256 -> ByteString -> Key256
skeinMAC256 Key256
key = ByteString -> Key256
Block256 (ByteString -> Key256)
-> (ByteString -> ByteString) -> ByteString -> Key256
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> Key256 -> ByteString -> ByteString
hash256 Word64
32 Key256
key
instance Skein Block256 where
skeinMAC :: Key256 -> ByteString -> Key256
skeinMAC = Key256 -> ByteString -> Key256
skeinMAC256
skein :: ByteString -> Key256
skein = ByteString -> Key256
skein256
config512 :: Block512
config512 :: Block512
config512 = Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> Block512
Block512 Word64
0x0000000133414853 Word64
512 Word64
0 Word64
0 Word64
0 Word64
0 Word64
0 Word64
0
{-# INLINE xb512 #-}
xb512 :: Block512 -> Block512 -> Block512
xb512 :: Block512 -> Block512 -> Block512
xb512 (Block512 Word64
x1 Word64
x2 Word64
x3 Word64
x4 Word64
x5 Word64
x6 Word64
x7 Word64
x8)
(Block512 Word64
y1 Word64
y2 Word64
y3 Word64
y4 Word64
y5 Word64
y6 Word64
y7 Word64
y8) =
Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> Block512
Block512 (Word64
x1 Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
`xor` Word64
y1) (Word64
x2 Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
`xor` Word64
y2) (Word64
x3 Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
`xor` Word64
y3) (Word64
x4 Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
`xor` Word64
y4)
(Word64
x5 Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
`xor` Word64
y5) (Word64
x6 Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
`xor` Word64
y6) (Word64
x7 Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
`xor` Word64
y7) (Word64
x8 Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
`xor` Word64
y8)
init512 :: Key512 -> Block512
init512 :: Block512 -> Block512
init512 Block512
key = (Block512, Tweak) -> Block512
forall a b. (a, b) -> a
fst ((Block512, Tweak) -> Block512) -> (Block512, Tweak) -> Block512
forall a b. (a -> b) -> a -> b
$ Word64 -> Block512 -> Tweak -> Block512 -> (Block512, Tweak)
processBlock512 Word64
32 Block512
key Tweak
configTweak Block512
config512
zero512 :: Block512
zero512 :: Block512
zero512 = Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> Word64
-> Block512
Block512 Word64
0 Word64
0 Word64
0 Word64
0 Word64
0 Word64
0 Word64
0 Word64
0
{-# INLINE processBlock512 #-}
processBlock512 :: Word64 -> Key512 -> Tweak -> Block512 -> (Key512, Tweak)
processBlock512 :: Word64 -> Block512 -> Tweak -> Block512 -> (Block512, Tweak)
processBlock512 !Word64
len !Block512
key !Tweak
tweak !Block512
block =
(Block512 -> Tweak -> Block512 -> Block512
encrypt512 Block512
key Tweak
tweak' Block512
block Block512 -> Block512 -> Block512
`xb512` Block512
block, Bool -> Tweak -> Tweak
setFirst Bool
False Tweak
tweak')
where
!tweak' :: Tweak
tweak' = Word64 -> Tweak -> Tweak
addBytes Word64
len Tweak
tweak
hash512 :: Key512 -> BSL.ByteString -> Block512
hash512 :: Block512 -> ByteString -> Block512
hash512 !Block512
firstkey !ByteString
bs =
case (Get Block512 -> ByteString -> Either String Block512)
-> ByteString -> Get Block512 -> Either String Block512
forall a b c. (a -> b -> c) -> b -> a -> c
flip Get Block512 -> ByteString -> Either String Block512
forall a. Get a -> ByteString -> Either String a
runGetLazy ByteString
bs' (Get Block512 -> Either String Block512)
-> Get Block512 -> Either String Block512
forall a b. (a -> b) -> a -> b
$ Int64 -> Block512 -> Tweak -> Get Block512
forall {t}.
(Ord t, Num t) =>
t -> Block512 -> Tweak -> Get Block512
go Int64
len (Block512 -> Block512
init512 Block512
firstkey) (BlockType -> Tweak
newTweak BlockType
Message) of
Right Block512
x -> Block512
x
Left String
_ -> String -> Block512
forall a. HasCallStack => String -> a
error String
"hash512 failed to get output bytes - impossible!"
where
!len :: Int64
len = ByteString -> Int64
BSL.length ByteString
bs
!lastLen :: Int64
lastLen = case Int64
len Int64 -> Int64 -> Int64
forall a. Integral a => a -> a -> a
`rem` Int64
64 of Int64
0 -> Int64
64 ; Int64
n -> Int64
n
!lastLenW64 :: Word64
lastLenW64 = Int64 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
lastLen
!bs' :: ByteString
bs' = ByteString -> ByteString -> ByteString
BSL.append ByteString
bs (Int64 -> Word8 -> ByteString
BSL.replicate (Int64
64Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
-Int64 -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
lastLen) Word8
0)
go :: t -> Block512 -> Tweak -> Get Block512
go !t
n !Block512
key !Tweak
tweak
| t
n t -> t -> Bool
forall a. Ord a => a -> a -> Bool
> t
64 = do
Block512
block <- Get Block512
forall t. Serialize t => Get t
get
let (Block512
block', Tweak
tweak') = Word64 -> Block512 -> Tweak -> Block512 -> (Block512, Tweak)
processBlock512 Word64
64 Block512
key Tweak
tweak Block512
block
t -> Block512 -> Tweak -> Get Block512
go (t
nt -> t -> t
forall a. Num a => a -> a -> a
-t
64) Block512
block' Tweak
tweak'
| Bool
otherwise = do
Block512
block <- Get Block512
forall t. Serialize t => Get t
get
let tweak' :: Tweak
tweak' = Bool -> Tweak -> Tweak
setLast Bool
True Tweak
tweak
(Block512
block', Tweak
_) = Word64 -> Block512 -> Tweak -> Block512 -> (Block512, Tweak)
processBlock512 Word64
lastLenW64 Block512
key Tweak
tweak' Block512
block
finalTweak :: Tweak
finalTweak = Bool -> Tweak -> Tweak
setLast Bool
True (Tweak -> Tweak) -> Tweak -> Tweak
forall a b. (a -> b) -> a -> b
$ BlockType -> Tweak
newTweak BlockType
Output
(Block512
b,Tweak
_) = Word64 -> Block512 -> Tweak -> Block512 -> (Block512, Tweak)
processBlock512 Word64
8 Block512
block' Tweak
finalTweak Block512
zero512
Block512 -> Get Block512
forall a. a -> Get a
forall (m :: * -> *) a. Monad m => a -> m a
return Block512
b
{-# INLINE skein512 #-}
skein512 :: BSL.ByteString -> Block512
skein512 :: ByteString -> Block512
skein512 = Block512 -> ByteString -> Block512
hash512 Block512
zero512
{-# INLINE skeinMAC512 #-}
skeinMAC512 :: Key512 -> BSL.ByteString -> Block512
skeinMAC512 :: Block512 -> ByteString -> Block512
skeinMAC512 =
Block512 -> ByteString -> Block512
hash512 (Block512 -> ByteString -> Block512)
-> (Block512 -> Block512) -> Block512 -> ByteString -> Block512
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Block512, Tweak) -> Block512
forall a b. (a, b) -> a
fst ((Block512, Tweak) -> Block512)
-> (Block512 -> (Block512, Tweak)) -> Block512 -> Block512
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> Block512 -> Tweak -> Block512 -> (Block512, Tweak)
processBlock512 Word64
64 Block512
zero512 (Bool -> Tweak -> Tweak
setLast Bool
True (Tweak -> Tweak) -> Tweak -> Tweak
forall a b. (a -> b) -> a -> b
$ BlockType -> Tweak
newTweak BlockType
Key)
instance Skein Block512 where
skeinMAC :: Block512 -> ByteString -> Block512
skeinMAC = Block512 -> ByteString -> Block512
skeinMAC512
skein :: ByteString -> Block512
skein = ByteString -> Block512
skein512