{-# OPTIONS_HADDOCK hide #-}
{-# LANGUAGE BangPatterns #-}
module Crypto.Cipher.ChaCha20.Native (
chacha20_native_available
, block
, cipher
, keystream_into
, interleaved_into
) where
import qualified Data.ByteString as BS
import qualified Data.ByteString.Internal as BI
import Data.Word (Word8, Word32)
import Foreign.C.Types (CInt(..), CSize(..))
import Foreign.ForeignPtr (withForeignPtr)
import Foreign.Marshal.Utils (fillBytes)
import Foreign.Ptr (Ptr, plusPtr)
import System.IO.Unsafe (unsafeDupablePerformIO)
foreign import ccall unsafe "chacha20_block_native"
c_chacha20_block
:: Ptr Word8 -> Word32 -> Ptr Word8 -> Ptr Word8 -> CInt -> IO ()
foreign import ccall unsafe "chacha20_cipher_native"
c_chacha20_cipher
:: Ptr Word8 -> Word32 -> Ptr Word8
-> Ptr Word8 -> Ptr Word8 -> CSize -> CInt -> IO ()
foreign import ccall unsafe "chacha20_interleaved_native"
c_chacha20_interleaved
:: Ptr Word8 -> Word32 -> Ptr Word8 -> Ptr Word8 -> CSize -> CInt
-> CInt -> IO ()
foreign import ccall unsafe "chacha20_native_available"
c_chacha20_native_available :: IO CInt
fi :: (Integral a, Num b) => a -> b
fi :: forall a b. (Integral a, Num b) => a -> b
fi = a -> b
forall a b. (Integral a, Num b) => a -> b
fromIntegral
{-# INLINE fi #-}
chacha20_native_available :: Bool
chacha20_native_available :: Bool
chacha20_native_available =
IO CInt -> CInt
forall a. IO a -> a
unsafeDupablePerformIO IO CInt
c_chacha20_native_available CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
/= CInt
0
{-# NOINLINE chacha20_native_available #-}
block
:: Int -> BS.ByteString -> Word32 -> BS.ByteString -> BS.ByteString
block :: Int -> ByteString -> Word32 -> ByteString -> ByteString
block Int
drounds (BI.PS ForeignPtr Word8
kfp Int
koff Int
_) Word32
counter (BI.PS ForeignPtr Word8
nfp Int
noff Int
_) =
Int -> (Ptr Word8 -> IO ()) -> ByteString
BI.unsafeCreate Int
64 ((Ptr Word8 -> IO ()) -> ByteString)
-> (Ptr Word8 -> IO ()) -> ByteString
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
dst ->
ForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word8
kfp ((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
kp0 ->
ForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word8
nfp ((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
np0 ->
Ptr Word8 -> Word32 -> Ptr Word8 -> Ptr Word8 -> CInt -> IO ()
c_chacha20_block (Ptr Word8
kp0 Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
koff)
Word32
counter
(Ptr Word8
np0 Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
noff)
Ptr Word8
dst
(Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fi Int
drounds)
cipher
:: Int -> BS.ByteString -> Word32 -> BS.ByteString -> BS.ByteString
-> BS.ByteString
cipher :: Int
-> ByteString -> Word32 -> ByteString -> ByteString -> ByteString
cipher Int
drounds (BI.PS ForeignPtr Word8
kfp Int
koff Int
_) Word32
counter (BI.PS ForeignPtr Word8
nfp Int
noff Int
_)
(BI.PS ForeignPtr Word8
pfp Int
poff Int
plen) =
Int -> (Ptr Word8 -> IO ()) -> ByteString
BI.unsafeCreate Int
plen ((Ptr Word8 -> IO ()) -> ByteString)
-> (Ptr Word8 -> IO ()) -> ByteString
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
dst ->
ForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word8
kfp ((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
kp0 ->
ForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word8
nfp ((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
np0 ->
ForeignPtr Word8 -> (Ptr Word8 -> IO ()) -> IO ()
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word8
pfp ((Ptr Word8 -> IO ()) -> IO ()) -> (Ptr Word8 -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
pp0 ->
Ptr Word8
-> Word32
-> Ptr Word8
-> Ptr Word8
-> Ptr Word8
-> CSize
-> CInt
-> IO ()
c_chacha20_cipher (Ptr Word8
kp0 Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
koff)
Word32
counter
(Ptr Word8
np0 Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
noff)
(Ptr Word8
pp0 Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` Int
poff)
Ptr Word8
dst
(Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fi Int
plen)
(Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fi Int
drounds)
keystream_into
:: Int -> Ptr Word8 -> Word32 -> Ptr Word8 -> Ptr Word8 -> Int
-> IO ()
keystream_into :: Int
-> Ptr Word8 -> Word32 -> Ptr Word8 -> Ptr Word8 -> Int -> IO ()
keystream_into Int
drounds Ptr Word8
kp Word32
counter Ptr Word8
np Ptr Word8
dst Int
len = do
Ptr Word8 -> Word8 -> Int -> IO ()
forall a. Ptr a -> Word8 -> Int -> IO ()
fillBytes Ptr Word8
dst Word8
0 Int
len
Ptr Word8
-> Word32
-> Ptr Word8
-> Ptr Word8
-> Ptr Word8
-> CSize
-> CInt
-> IO ()
c_chacha20_cipher Ptr Word8
kp Word32
counter Ptr Word8
np Ptr Word8
dst Ptr Word8
dst (Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fi Int
len) (Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fi Int
drounds)
interleaved_into
:: Int -> Bool -> Ptr Word8 -> Word32 -> Ptr Word8 -> Ptr Word8 -> Int
-> IO ()
interleaved_into :: Int
-> Bool
-> Ptr Word8
-> Word32
-> Ptr Word8
-> Ptr Word8
-> Int
-> IO ()
interleaved_into Int
drounds Bool
key_only Ptr Word8
kp Word32
counter Ptr Word8
np Ptr Word8
dst Int
groups =
Ptr Word8
-> Word32
-> Ptr Word8
-> Ptr Word8
-> CSize
-> CInt
-> CInt
-> IO ()
c_chacha20_interleaved Ptr Word8
kp Word32
counter Ptr Word8
np Ptr Word8
dst (Int -> CSize
forall a b. (Integral a, Num b) => a -> b
fi Int
groups) (Int -> CInt
forall a b. (Integral a, Num b) => a -> b
fi Int
drounds)
(if Bool
key_only then CInt
1 else CInt
0)