{-# OPTIONS_HADDOCK prune #-}

-- |
-- Module: Crypto.Hash.SHA512
-- Copyright: (c) 2024 Jared Tobin
-- License: MIT
-- Maintainer: Jared Tobin <jared@ppad.tech>
--
-- SHA-512 and HMAC-SHA512 implementations for
-- strict and lazy ByteStrings, as specified by RFC's
-- [6234](https://datatracker.ietf.org/doc/html/rfc6234) and
-- [2104](https://datatracker.ietf.org/doc/html/rfc2104).
--
-- The 'hash' and 'hmac' functions will use primitive instructions from
-- the ARM or x86 SHA512 extensions via FFI if they're available, and
-- will otherwise use a pure Haskell implementation.

module Crypto.Hash.SHA512 (
  -- * SHA-512 message digest functions
    hash
  , Lazy.hash_lazy

  -- * SHA512-based MAC functions
  , MAC(..)
  , hmac
  , Lazy.hmac_lazy

  -- low-level specialized HMAC primitives
  , _hmac_rr
  , _hmac_rsb
  ) where

import qualified Data.ByteString as BS
import Data.Word (Word8, Word64)
import Foreign.Ptr (Ptr)
import qualified Crypto.Hash.SHA512.Native as Native
import Crypto.Hash.SHA512.Internal (MAC(..), Registers)
import qualified Crypto.Hash.SHA512.Lazy as Lazy
import qualified Crypto.Hash.SHA512.Pure as Pure

-- hash -----------------------------------------------------------------------

-- | Compute a condensed representation of a strict bytestring via
--   SHA-512.
--
--   The 512-bit output digest is returned as a strict bytestring.
--
--   >>> hash "strict bytestring input"
--   "<strict 512-bit message digest>"
hash :: BS.ByteString -> BS.ByteString
hash :: ByteString -> ByteString
hash ByteString
m
  | Bool
Native.sha512_native_available = ByteString -> ByteString
Native.hash ByteString
m
  | Bool
otherwise = ByteString -> ByteString
Pure.hash ByteString
m
{-# INLINABLE hash #-}

-- hmac ----------------------------------------------------------------------

-- | Produce a message authentication code for a strict bytestring,
--   based on the provided (strict, bytestring) key, via SHA-512.
--
--   The 512-bit MAC is returned as a strict bytestring.
--
--   Per RFC 2104, the key /should/ be a minimum of 64 bytes long. Keys
--   exceeding 128 bytes in length will first be hashed (via SHA-512).
--
--   >>> hmac "strict bytestring key" "strict bytestring input"
--   "<strict 512-bit MAC>"
hmac :: BS.ByteString -> BS.ByteString -> MAC
hmac :: ByteString -> ByteString -> MAC
hmac ByteString
k ByteString
m
  | Bool
Native.sha512_native_available = ByteString -> MAC
MAC (ByteString -> ByteString -> ByteString
Native.hmac ByteString
k ByteString
m)
  | Bool
otherwise = ByteString -> MAC
MAC (ByteString -> ByteString -> ByteString
Pure.hmac ByteString
k ByteString
m)
{-# INLINABLE hmac #-}

-- the following functions are useful when we want to avoid allocating certain
-- components of the HMAC key and message on the heap.

-- Computes hmac(k, v) when k and v are Registers.
--
-- The 64-byte result is written to the destination pointer.
_hmac_rr
  :: Ptr Word64    -- ^ destination (8 Word64s)
  -> Ptr Word64    -- ^ scratch block buffer (16 Word64s)
  -> Registers     -- ^ key
  -> Registers     -- ^ message
  -> IO ()
_hmac_rr :: Ptr Word64 -> Ptr Word64 -> Registers -> Registers -> IO ()
_hmac_rr Ptr Word64
rp Ptr Word64
bp Registers
k Registers
m
  | Bool
Native.sha512_native_available = Ptr Word64 -> Ptr Word64 -> Registers -> Registers -> IO ()
Native._hmac_rr Ptr Word64
rp Ptr Word64
bp Registers
k Registers
m
  | Bool
otherwise = Ptr Word64 -> Ptr Word64 -> Registers -> Registers -> IO ()
Pure._hmac_rr Ptr Word64
rp Ptr Word64
bp Registers
k Registers
m
{-# INLINABLE _hmac_rr #-}

-- Calculate hmac(k, m) where m is the concatenation of v (registers), a
-- separator byte, and a ByteString. This avoids allocating 'v' on the
-- heap.
--
-- The 64-byte result is written to the destination pointer.
_hmac_rsb
  :: Ptr Word64    -- ^ destination pointer (8 x Word64)
  -> Ptr Word64    -- ^ scratch block pointer (16 x Word64)
  -> Registers     -- ^ k
  -> Registers     -- ^ v
  -> Word8         -- ^ separator byte
  -> BS.ByteString -- ^ data
  -> IO ()
_hmac_rsb :: Ptr Word64
-> Ptr Word64
-> Registers
-> Registers
-> Word8
-> ByteString
-> IO ()
_hmac_rsb Ptr Word64
rp Ptr Word64
bp Registers
k Registers
v Word8
sep ByteString
dat
  | Bool
Native.sha512_native_available = Ptr Word64
-> Ptr Word64
-> Registers
-> Registers
-> Word8
-> ByteString
-> IO ()
Native._hmac_rsb Ptr Word64
rp Ptr Word64
bp Registers
k Registers
v Word8
sep ByteString
dat
  | Bool
otherwise = Ptr Word64
-> Ptr Word64
-> Registers
-> Registers
-> Word8
-> ByteString
-> IO ()
Pure._hmac_rsb Ptr Word64
rp Ptr Word64
bp Registers
k Registers
v Word8
sep ByteString
dat
{-# INLINABLE _hmac_rsb #-}