module Crypto.Threefish.Skein.StreamCipher (
Key256, Nonce256, Block256,
encrypt, decrypt, toBlock, fromBlock
) where
import Crypto.Threefish.Skein (Nonce256)
import Crypto.Threefish.UBI
import Crypto.Threefish.Threefish256
import Crypto.Threefish
import Crypto.Threefish.Skein.Internal
import Data.ByteString.Unsafe
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BSL
import Foreign.ForeignPtr
import Foreign.Ptr
import Foreign.Marshal.Alloc
import System.IO.Unsafe
import Data.Bits (xor)
init256 :: Key256 -> Nonce256 -> Skein256Ctx
init256 :: Key256 -> Key256 -> Skein256Ctx
init256 (Block256 ByteString
k) (Block256 ByteString
n) =
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
ByteString -> (CString -> IO ()) -> IO ()
forall a. ByteString -> (CString -> IO a) -> IO a
unsafeUseAsCString ByteString
k ((CString -> IO ()) -> IO ()) -> (CString -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CString
key -> do
ByteString -> (CString -> IO ()) -> IO ()
forall a. ByteString -> (CString -> IO a) -> IO a
unsafeUseAsCString ByteString
n ((CString -> IO ()) -> IO ()) -> (CString -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CString
nonce -> do
Ptr Word64 -> Ptr Word64 -> Word64 -> IO ()
skein256_init Ptr Word64
ctx (CString -> Ptr Word64
forall a b. Ptr a -> Ptr b
castPtr CString
key) Word64
0xffffffffffffffff
Ptr Word64 -> Int -> Int -> Word64 -> Ptr Word64 -> IO ()
skein256_update Ptr Word64
ctx Int
3 (BlockType -> Int
type2int BlockType
Nonce) Word64
len (CString -> Ptr Word64
forall a b. Ptr a -> Ptr b
castPtr CString
nonce)
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
len :: Word64
len = 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
n
stream256 :: Skein256Ctx -> [BS.ByteString]
stream256 :: Skein256Ctx -> [ByteString]
stream256 (Skein256Ctx ForeignPtr Word64
c) =
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 -> Ptr Word64 -> IO [ByteString]
go Int
0
where
go :: Int -> Ptr Word64 -> IO [ByteString]
go Int
n Ptr Word64
ctx = IO [ByteString] -> IO [ByteString]
forall a. IO a -> IO a
unsafeInterleaveIO (IO [ByteString] -> IO [ByteString])
-> IO [ByteString] -> IO [ByteString]
forall a b. (a -> b) -> a -> b
$ do
ByteString
bs <- Int -> (Ptr Word64 -> IO ByteString) -> IO ByteString
forall a b. Int -> (Ptr a -> IO b) -> IO b
allocaBytes Int
1024 ((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
n (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
32) Ptr Word64
ptr
CStringLen -> IO ByteString
BS.packCStringLen (Ptr Word64 -> CString
forall a b. Ptr a -> Ptr b
castPtr Ptr Word64
ptr, Int
1024)
[ByteString]
bss <- Int -> Ptr Word64 -> IO [ByteString]
go (Int
nInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
32) Ptr Word64
ctx
[ByteString] -> IO [ByteString]
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ([ByteString] -> IO [ByteString])
-> [ByteString] -> IO [ByteString]
forall a b. (a -> b) -> a -> b
$ ByteString
bs ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: [ByteString]
bss
keystream256 :: Key256 -> Nonce256 -> [BS.ByteString]
keystream256 :: Key256 -> Key256 -> [ByteString]
keystream256 Key256
k Key256
n = Skein256Ctx -> [ByteString]
stream256 (Key256 -> Key256 -> Skein256Ctx
init256 Key256
k Key256
n)
encrypt :: Key256 -> Nonce256 -> BSL.ByteString -> BSL.ByteString
encrypt :: Key256 -> Key256 -> ByteString -> ByteString
encrypt Key256
k Key256
n ByteString
plaintext =
[ByteString] -> ByteString
BSL.fromChunks ([ByteString] -> ByteString) -> [ByteString] -> ByteString
forall a b. (a -> b) -> a -> b
$ [ByteString] -> ByteString -> [ByteString]
go (Key256 -> Key256 -> [ByteString]
keystream256 Key256
k Key256
n) ByteString
plaintext
where
go :: [ByteString] -> ByteString -> [ByteString]
go (ByteString
ks:[ByteString]
kss) ByteString
msg = IO [ByteString] -> [ByteString]
forall a. IO a -> a
unsafePerformIO (IO [ByteString] -> [ByteString])
-> (IO [ByteString] -> IO [ByteString])
-> IO [ByteString]
-> [ByteString]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO [ByteString] -> IO [ByteString]
forall a. IO a -> IO a
unsafeInterleaveIO (IO [ByteString] -> [ByteString])
-> IO [ByteString] -> [ByteString]
forall a b. (a -> b) -> a -> b
$ do
case Int64 -> ByteString -> (ByteString, ByteString)
BSL.splitAt Int64
1024 ByteString
msg of
(ByteString
chunk, ByteString
rest)
| ByteString -> Bool
BSL.null ByteString
chunk ->
[ByteString] -> IO [ByteString]
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return []
| Bool
otherwise ->
let chunk' :: ByteString
chunk' = ByteString -> ByteString
BSL.toStrict ByteString
chunk
in [ByteString] -> IO [ByteString]
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ([ByteString] -> IO [ByteString])
-> [ByteString] -> IO [ByteString]
forall a b. (a -> b) -> a -> b
$ ([Word8] -> ByteString
BS.pack ([Word8] -> ByteString) -> [Word8] -> ByteString
forall a b. (a -> b) -> a -> b
$ (Word8 -> Word8 -> Word8) -> ByteString -> ByteString -> [Word8]
forall a. (Word8 -> Word8 -> a) -> ByteString -> ByteString -> [a]
BS.zipWith Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
xor ByteString
ks ByteString
chunk') ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: [ByteString] -> ByteString -> [ByteString]
go [ByteString]
kss ByteString
rest
go [ByteString]
_ ByteString
_ =
[Char] -> [ByteString]
forall a. HasCallStack => [Char] -> a
error [Char]
"The key stream is infinite, so this will never happen."
decrypt :: Key256 -> Nonce256 -> BSL.ByteString -> BSL.ByteString
decrypt :: Key256 -> Key256 -> ByteString -> ByteString
decrypt = Key256 -> Key256 -> ByteString -> ByteString
encrypt