poly1305

The Poly1305 message authentication code (docs.ppad.tech/poly1305).
git clone git://git.ppad.tech/poly1305.git
Log | Files | Refs | README | LICENSE

Arm.hs (1765B)


      1 {-# OPTIONS_HADDOCK hide #-}
      2 {-# LANGUAGE BangPatterns #-}
      3 
      4 -- |
      5 -- Module: Crypto.MAC.Poly1305.Arm
      6 -- Copyright: (c) 2025 Jared Tobin
      7 -- License: MIT
      8 -- Maintainer: Jared Tobin <jared@ppad.tech>
      9 --
     10 -- ARM acceleration for the Poly1305 MAC.
     11 
     12 module Crypto.MAC.Poly1305.Arm (
     13     poly1305_arm_available
     14   , mac
     15   ) where
     16 
     17 import qualified Data.ByteString as BS
     18 import qualified Data.ByteString.Internal as BI
     19 import Data.Word (Word8)
     20 import Foreign.C.Types (CInt(..), CSize(..))
     21 import Foreign.ForeignPtr (withForeignPtr)
     22 import Foreign.Ptr (Ptr, plusPtr)
     23 import System.IO.Unsafe (unsafeDupablePerformIO)
     24 
     25 -- ffi ------------------------------------------------------------------------
     26 
     27 foreign import ccall unsafe "poly1305_mac_arm"
     28   c_poly1305_mac
     29     :: Ptr Word8 -> Ptr Word8 -> CSize -> Ptr Word8 -> IO ()
     30 
     31 foreign import ccall unsafe "poly1305_arm_available"
     32   c_poly1305_arm_available :: IO CInt
     33 
     34 -- utilities ------------------------------------------------------------------
     35 
     36 fi :: (Integral a, Num b) => a -> b
     37 fi = fromIntegral
     38 {-# INLINE fi #-}
     39 
     40 -- api ------------------------------------------------------------------------
     41 
     42 -- | Are ARM extensions available?
     43 poly1305_arm_available :: Bool
     44 poly1305_arm_available =
     45   unsafeDupablePerformIO c_poly1305_arm_available /= 0
     46 {-# NOINLINE poly1305_arm_available #-}
     47 
     48 -- | Compute a Poly1305 MAC over the message using the given (already-
     49 --   validated 32-byte) key.
     50 mac :: BS.ByteString -> BS.ByteString -> BS.ByteString
     51 mac (BI.PS kfp koff _) (BI.PS mfp moff mlen) =
     52   BI.unsafeCreate 16 $ \dst ->
     53     withForeignPtr kfp $ \kp0 ->
     54     withForeignPtr mfp $ \mp0 ->
     55       c_poly1305_mac (kp0 `plusPtr` koff)
     56                      (mp0 `plusPtr` moff)
     57                      (fi mlen)
     58                      dst