{-# OPTIONS_HADDOCK hide #-}
{-# LANGUAGE BangPatterns #-}

-- |
-- Module: Crypto.Cipher.ChaCha20.Native
-- Copyright: (c) 2025 Jared Tobin
-- License: MIT
-- Maintainer: Jared Tobin <jared@ppad.tech>
--
-- Vectorised ChaCha20, via ARM NEON, or x86 SSE2 and AVX2.

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)

-- ffi ------------------------------------------------------------------------

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

-- utilities ------------------------------------------------------------------

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 #-}

-- api ------------------------------------------------------------------------

-- | Is a vectorised kernel available?
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 #-}

-- | One 64-byte keystream block, using the given number of double
--   rounds, for the given (already-validated) key, counter, and nonce.
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)

-- | XOR the plaintext with the keystream derived, using the given
--   number of double rounds, from the given (already-validated) key,
--   counter, and nonce.
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)

-- | Write 'len' bytes of keystream, using the given number of double
--   rounds, for the 32-byte key and 12-byte nonce at the given
--   pointers, to the destination, which must not overlap them. The
--   counter must leave room for 'len' bytes.
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)

-- | Write 'groups' groups of four keystream blocks, interleaved word
--   by word, using the given number of double rounds, for the 32-byte
--   key and 12-byte nonce at the given pointers, to the destination,
--   which must not overlap them. If the flag is set, the final
--   addition of the input state covers the key words only. The
--   counter must leave room for four blocks per group.
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)