{-# LANGUAGE BangPatterns, ForeignFunctionInterface #-}
module Crypto.Threefish.Threefish256 (
Block256 (..), Key256, Threefish256 (..),
encrypt256, decrypt256, readBlock256, Tweak (..)
) where
import Data.Word
import Crypto.Threefish.Common
import Data.Serialize
import Control.Applicative
import Crypto.Classes
import Data.Tagged
import qualified Data.ByteString as BS
import Data.ByteString.Unsafe
import Foreign.Storable
import Foreign.Ptr
import Foreign.ForeignPtr
import System.IO.Unsafe
foreign import ccall unsafe "encrypt256" c_encrypt256 :: Ptr Word64
-> Word64
-> Word64
-> Ptr Word64
-> Ptr Word64
-> IO ()
foreign import ccall unsafe "decrypt256" c_decrypt256 :: Ptr Word64
-> Word64
-> Word64
-> Ptr Word64
-> Ptr Word64
-> IO ()
newtype Block256 = Block256 BS.ByteString deriving Block256 -> Block256 -> Bool
(Block256 -> Block256 -> Bool)
-> (Block256 -> Block256 -> Bool) -> Eq Block256
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Block256 -> Block256 -> Bool
== :: Block256 -> Block256 -> Bool
$c/= :: Block256 -> Block256 -> Bool
/= :: Block256 -> Block256 -> Bool
Eq
type Key256 = Block256
instance Show Block256 where
show :: Block256 -> String
show (Block256 ByteString
bs) =
case ByteString -> Int -> (Word64, Word64, Word64, Word64)
readBlock256 ByteString
bs Int
0 of
(Word64
a, Word64
b, Word64
c, Word64
d) -> Word64 -> String
showBytes Word64
a String -> ShowS
forall a. [a] -> [a] -> [a]
++ Word64 -> String
showBytes Word64
b String -> ShowS
forall a. [a] -> [a] -> [a]
++ Word64 -> String
showBytes Word64
c String -> ShowS
forall a. [a] -> [a] -> [a]
++ Word64 -> String
showBytes Word64
d
instance Serialize Block256 where
put :: Putter Block256
put (Block256 ByteString
bs) = Putter ByteString
putByteString ByteString
bs
get :: Get Block256
get = ByteString -> Block256
Block256 (ByteString -> Block256) -> Get ByteString -> Get Block256
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Int -> Get ByteString
getBytes Int
32
instance Serialize Threefish256 where
put :: Putter Threefish256
put (Threefish256 Tweak
tweak Block256
key) = Putter Tweak
forall t. Serialize t => Putter t
put Tweak
tweak PutM () -> PutM () -> PutM ()
forall a b. PutM a -> PutM b -> PutM b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Putter Block256
forall t. Serialize t => Putter t
put Block256
key
get :: Get Threefish256
get = Tweak -> Block256 -> Threefish256
Threefish256 (Tweak -> Block256 -> Threefish256)
-> Get Tweak -> Get (Block256 -> Threefish256)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Tweak
forall t. Serialize t => Get t
get Get (Block256 -> Threefish256) -> Get Block256 -> Get Threefish256
forall a b. Get (a -> b) -> Get a -> Get b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Get Block256
forall t. Serialize t => Get t
get
data Threefish256 = Threefish256 !Tweak !Key256
{-# INLINE readBlock256 #-}
readBlock256 :: BS.ByteString -> Int -> (Word64, Word64, Word64, Word64)
readBlock256 :: ByteString -> Int -> (Word64, Word64, Word64, Word64)
readBlock256 ByteString
bs Int
off = IO (Word64, Word64, Word64, Word64)
-> (Word64, Word64, Word64, Word64)
forall a. IO a -> a
unsafePerformIO (IO (Word64, Word64, Word64, Word64)
-> (Word64, Word64, Word64, Word64))
-> ((CString -> IO (Word64, Word64, Word64, Word64))
-> IO (Word64, Word64, Word64, Word64))
-> (CString -> IO (Word64, Word64, Word64, Word64))
-> (Word64, Word64, Word64, Word64)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString
-> (CString -> IO (Word64, Word64, Word64, Word64))
-> IO (Word64, Word64, Word64, Word64)
forall a. ByteString -> (CString -> IO a) -> IO a
unsafeUseAsCString ByteString
bs ((CString -> IO (Word64, Word64, Word64, Word64))
-> (Word64, Word64, Word64, Word64))
-> (CString -> IO (Word64, Word64, Word64, Word64))
-> (Word64, Word64, Word64, Word64)
forall a b. (a -> b) -> a -> b
$ \CString
ptr -> do
Word64
a <- Ptr Word64 -> Int -> IO Word64
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff (CString -> Ptr Word64
forall a b. Ptr a -> Ptr b
castPtr CString
ptr) Int
off
Word64
b <- Ptr Word64 -> Int -> IO Word64
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff (CString -> Ptr Word64
forall a b. Ptr a -> Ptr b
castPtr CString
ptr) (Int
offInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
Word64
c <- Ptr Word64 -> Int -> IO Word64
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff (CString -> Ptr Word64
forall a b. Ptr a -> Ptr b
castPtr CString
ptr) (Int
offInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
2)
Word64
d <- Ptr Word64 -> Int -> IO Word64
forall a. Storable a => Ptr a -> Int -> IO a
peekElemOff (CString -> Ptr Word64
forall a b. Ptr a -> Ptr b
castPtr CString
ptr) (Int
offInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
3)
(Word64, Word64, Word64, Word64)
-> IO (Word64, Word64, Word64, Word64)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ((Word64, Word64, Word64, Word64)
-> IO (Word64, Word64, Word64, Word64))
-> (Word64, Word64, Word64, Word64)
-> IO (Word64, Word64, Word64, Word64)
forall a b. (a -> b) -> a -> b
$! (Word64
a, Word64
b, Word64
c, Word64
d)
instance BlockCipher Threefish256 where
blockSize :: Tagged Threefish256 Int
blockSize = Int -> Tagged Threefish256 Int
forall {k} (s :: k) b. b -> Tagged s b
Tagged Int
256
keyLength :: Tagged Threefish256 Int
keyLength = Int -> Tagged Threefish256 Int
forall {k} (s :: k) b. b -> Tagged s b
Tagged Int
256
encryptBlock :: Threefish256 -> ByteString -> ByteString
encryptBlock (Threefish256 Tweak
tweak Block256
key) ByteString
block =
case Block256 -> Tweak -> Block256 -> Block256
encrypt256 Block256
key Tweak
tweak (ByteString -> Block256
Block256 ByteString
block) of
Block256 ByteString
out -> ByteString
out
decryptBlock :: Threefish256 -> ByteString -> ByteString
decryptBlock (Threefish256 Tweak
tweak Block256
key) ByteString
block =
case Block256 -> Tweak -> Block256 -> Block256
decrypt256 Block256
key Tweak
tweak (ByteString -> Block256
Block256 ByteString
block) of
Block256 ByteString
out -> ByteString
out
buildKey :: ByteString -> Maybe Threefish256
buildKey ByteString
bs | ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
32 = Maybe Threefish256
forall a. Maybe a
Nothing
| Bool
otherwise = Threefish256 -> Maybe Threefish256
forall a. a -> Maybe a
Just (Tweak -> Block256 -> Threefish256
Threefish256 Tweak
defaultTweak
(ByteString -> Block256
Block256 ByteString
bs))
decrypt256 :: Key256 -> Tweak -> Block256 -> Block256
decrypt256 :: Block256 -> Tweak -> Block256 -> Block256
decrypt256 (Block256 ByteString
key) (Tweak Word64
t0 Word64
t1) (Block256 ByteString
block) =
IO Block256 -> Block256
forall a. IO a -> a
unsafePerformIO (IO Block256 -> Block256) -> IO Block256 -> Block256
forall a b. (a -> b) -> a -> b
$ ByteString -> (CString -> IO Block256) -> IO Block256
forall a. ByteString -> (CString -> IO a) -> IO a
unsafeUseAsCString ByteString
key ((CString -> IO Block256) -> IO Block256)
-> (CString -> IO Block256) -> IO Block256
forall a b. (a -> b) -> a -> b
$ \CString
k ->
ByteString -> (CString -> IO Block256) -> IO Block256
forall a. ByteString -> (CString -> IO a) -> IO a
unsafeUseAsCString ByteString
block ((CString -> IO Block256) -> IO Block256)
-> (CString -> IO Block256) -> IO Block256
forall a b. (a -> b) -> a -> b
$ \CString
b -> do
ForeignPtr Word64
out <- Int -> IO (ForeignPtr Word64)
forall a. Storable a => Int -> IO (ForeignPtr a)
mallocForeignPtrArray Int
4
ForeignPtr Word64 -> (Ptr Word64 -> IO Block256) -> IO Block256
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word64
out ((Ptr Word64 -> IO Block256) -> IO Block256)
-> (Ptr Word64 -> IO Block256) -> IO Block256
forall a b. (a -> b) -> a -> b
$ \Ptr Word64
out' -> do
Ptr Word64 -> Word64 -> Word64 -> Ptr Word64 -> Ptr Word64 -> IO ()
c_decrypt256 (CString -> Ptr Word64
forall a b. Ptr a -> Ptr b
castPtr CString
k) Word64
t0 Word64
t1 (CString -> Ptr Word64
forall a b. Ptr a -> Ptr b
castPtr CString
b) Ptr Word64
out'
ByteString -> Block256
Block256 (ByteString -> Block256) -> IO ByteString -> IO Block256
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CStringLen -> IO ByteString
BS.packCStringLen (Ptr Word64 -> CString
forall a b. Ptr a -> Ptr b
castPtr Ptr Word64
out', Int
32)
encrypt256 :: Key256 -> Tweak -> Block256 -> Block256
encrypt256 :: Block256 -> Tweak -> Block256 -> Block256
encrypt256 (Block256 ByteString
key) (Tweak Word64
t0 Word64
t1) (Block256 ByteString
block) =
IO Block256 -> Block256
forall a. IO a -> a
unsafePerformIO (IO Block256 -> Block256) -> IO Block256 -> Block256
forall a b. (a -> b) -> a -> b
$ ByteString -> (CString -> IO Block256) -> IO Block256
forall a. ByteString -> (CString -> IO a) -> IO a
unsafeUseAsCString ByteString
key ((CString -> IO Block256) -> IO Block256)
-> (CString -> IO Block256) -> IO Block256
forall a b. (a -> b) -> a -> b
$ \CString
k ->
ByteString -> (CString -> IO Block256) -> IO Block256
forall a. ByteString -> (CString -> IO a) -> IO a
unsafeUseAsCString ByteString
block ((CString -> IO Block256) -> IO Block256)
-> (CString -> IO Block256) -> IO Block256
forall a b. (a -> b) -> a -> b
$ \CString
b -> do
ForeignPtr Word64
out <- Int -> IO (ForeignPtr Word64)
forall a. Storable a => Int -> IO (ForeignPtr a)
mallocForeignPtrArray Int
4
ForeignPtr Word64 -> (Ptr Word64 -> IO Block256) -> IO Block256
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word64
out ((Ptr Word64 -> IO Block256) -> IO Block256)
-> (Ptr Word64 -> IO Block256) -> IO Block256
forall a b. (a -> b) -> a -> b
$ \Ptr Word64
out' -> do
Ptr Word64 -> Word64 -> Word64 -> Ptr Word64 -> Ptr Word64 -> IO ()
c_encrypt256 (CString -> Ptr Word64
forall a b. Ptr a -> Ptr b
castPtr CString
k) Word64
t0 Word64
t1 (CString -> Ptr Word64
forall a b. Ptr a -> Ptr b
castPtr CString
b) Ptr Word64
out'
ByteString -> Block256
Block256 (ByteString -> Block256) -> IO ByteString -> IO Block256
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> CStringLen -> IO ByteString
BS.packCStringLen (Ptr Word64 -> CString
forall a b. Ptr a -> Ptr b
castPtr Ptr Word64
out', Int
32)