packages feed

crypton-2.3.1 / Crypto / MAC / Poly1305.hs

{-# LANGUAGE ForeignFunctionInterface #-}{-# LANGUAGE GeneralizedNewtypeDeriving #-}-- |-- Module      : Crypto.MAC.Poly1305-- License     : BSD-style-- Maintainer  : Vincent Hanquez <vincent@snarc.org>-- Stability   : experimental-- Portability : unknown---- Poly1305 implementationmodule Crypto.MAC.Poly1305 (    Ctx,    State,    Key,    key,    Auth (..),    authTag,    -- * Incremental MAC Functions    initialize, -- :: State    update, -- :: State -> ByteString -> State    updates, -- :: State -> [ByteString] -> State    finalize, -- :: State -> Auth    -- * One-pass MAC function    auth,) whereimport Crypto.Errorimport Crypto.Internal.ByteArray (    ByteArrayAccess,    Bytes,    ScrubbedBytes, )import qualified Crypto.Internal.ByteArray as Bimport Crypto.Internal.DeepSeqimport Crypto.Internal.Poly1305 (Key (..), key)import Data.Wordimport Foreign.C.Typesimport Foreign.Ptr-- | Poly1305 State---- This type is an instance of 'ByteArrayAccess' for debugging purpose. Internal-- layout is architecture dependent, may contain uninitialized data fragments,-- and change in future versions.  The bytearray should not be used as input to-- cryptographic algorithms.newtype State = State ScrubbedBytes    deriving (ByteArrayAccess)-- | Poly1305 State. use State instead of Ctxtype Ctx = State{-# DEPRECATED Ctx "use Poly1305 State instead" #-}-- | Poly1305 Authnewtype Auth = Auth Bytes    deriving (ByteArrayAccess, NFData)-- | Take sixteen bytes as a tag, to compare against a computed t'Auth'.-- Another length is 'CryptoError_AuthenticationTagSizeInvalid'.authTag :: ByteArrayAccess b => b -> CryptoFailable AuthauthTag b    | B.length b /= 16 = CryptoFailed $ CryptoError_AuthenticationTagSizeInvalid    | otherwise = CryptoPassed $ Auth $ B.convert binstance Eq Auth where    (Auth a1) == (Auth a2) = B.constEq a1 a2-- | @sizeof(poly1305_ctx)@: the accumulator and the key, either as the-- limbs the C implementation works in or as the state the assembly keeps,-- and the buffer for a partial block.  See @cbits/crypton_poly1305.h@.sizeCtx :: IntsizeCtx = 232foreign import ccall unsafe "crypton_poly1305.h crypton_poly1305_init"    c_poly1305_init :: Ptr State -> Ptr Word8 -> IO ()foreign import ccall "crypton_poly1305.h crypton_poly1305_update"    c_poly1305_update :: Ptr State -> Ptr Word8 -> CUInt -> IO ()foreign import ccall unsafe "crypton_poly1305.h crypton_poly1305_finalize"    c_poly1305_finalize :: Ptr Word8 -> Ptr State -> IO ()-- | initialize a Poly1305 contextinitialize :: Key -> Stateinitialize k = State $ B.allocAndFreeze sizeCtx $ \ctxPtr ->    B.withByteArray k $ \keyPtr ->        c_poly1305_init (castPtr ctxPtr) keyPtr{-# NOINLINE initialize #-}-- | update a context with a bytestringupdate :: ByteArrayAccess ba => State -> ba -> Stateupdate (State prevCtx) d = State $ B.copyAndFreeze prevCtx $ \ctxPtr ->    B.withByteArray d $ \dataPtr ->        -- in pieces the C's uint32_t length can hold; the context carries        -- across, so it can simply be called again        B.inCLengths (B.length d) $ \off n ->            c_poly1305_update (castPtr ctxPtr) (dataPtr `plusPtr` off) (fromIntegral n){-# NOINLINE update #-}-- | updates a context with multiples bytestringupdates :: ByteArrayAccess ba => State -> [ba] -> Stateupdates (State prevCtx) d = State $ B.copyAndFreeze prevCtx (loop d)  where    loop [] _ = return ()    loop (x : xs) ctxPtr = do        B.withByteArray x $ \dataPtr ->            B.inCLengths (B.length x) $ \off n ->                c_poly1305_update ctxPtr (dataPtr `plusPtr` off) (fromIntegral n)        loop xs ctxPtr{-# NOINLINE updates #-}-- | finalize the context into a digest bytestringfinalize :: State -> Authfinalize (State prevCtx) = Auth $ B.allocAndFreeze 16 $ \dst -> do    _ <-        B.copy prevCtx (\ctxPtr -> c_poly1305_finalize dst (castPtr ctxPtr))            :: IO ScrubbedBytes    return (){-# NOINLINE finalize #-}-- | One-pass authorization creationauth :: ByteArrayAccess ba => Key -> ba -> Authauth k d = Auth $ B.allocAndFreeze 16 $ \dst -> do    _ <- B.alloc sizeCtx (onCtx dst) :: IO ScrubbedBytes    return ()  where    onCtx dst ctxPtr =        B.withByteArray k $ \keyPtr -> do            c_poly1305_init (castPtr ctxPtr) keyPtr            B.withByteArray d $ \dataPtr ->                B.inCLengths (B.length d) $ \off n ->                    c_poly1305_update                        (castPtr ctxPtr)                        (dataPtr `plusPtr` off)                        (fromIntegral n)            c_poly1305_finalize dst (castPtr ctxPtr)