{-# OPTIONS_HADDOCK prune #-}

-- |
-- Module: Crypto.Hash.SHA256
-- Copyright: (c) 2024 Jared Tobin
-- License: MIT
-- Maintainer: Jared Tobin <jared@ppad.tech>
--
-- SHA-256 and HMAC-SHA256 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 SHA2 or x86 SHA extensions via FFI if they're available, and
-- will otherwise use a pure Haskell implementation.

module Crypto.Hash.SHA256 (
  -- * SHA-256 message digest functions
    hash
  , hash_prefixed
  , Lazy.hash_lazy

  -- * SHA256-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, Word32)
import Foreign.Ptr (Ptr)
import qualified Crypto.Hash.SHA256.Native as Native
import Crypto.Hash.SHA256.Internal (MAC(..), Registers)
import qualified Crypto.Hash.SHA256.Lazy as Lazy
import qualified Crypto.Hash.SHA256.Pure as Pure

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

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

-- | Compute a condensed representation of the concatenation of two
--   strict bytestrings via SHA-256, i.e. @hash (p <> m)@.
--
--   When the length of the prefix @p@ is a multiple of 64 bytes (the
--   SHA-256 block size), @m@ is hashed in place, rather than being
--   copied into a new buffer. Otherwise both are copied, as for
--   @hash (p <> m)@.
--
--   The 256-bit output digest is returned as a strict bytestring.
--
--   >>> hash_prefixed "64-byte prefix" "strict bytestring input"
--   "<strict 256-bit message digest>"
hash_prefixed :: BS.ByteString -> BS.ByteString -> BS.ByteString
hash_prefixed :: ByteString -> ByteString -> ByteString
hash_prefixed ByteString
p ByteString
m
  | Bool
Native.sha256_native_available = ByteString -> ByteString -> ByteString
Native.hash_prefixed ByteString
p ByteString
m
  | Bool
otherwise = ByteString -> ByteString -> ByteString
Pure.hash_prefixed ByteString
p ByteString
m
{-# INLINABLE hash_prefixed #-}

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

-- | Produce a message authentication code for a strict bytestring,
--   based on the provided (strict, bytestring) key, via SHA-256.
--
--   The 256-bit MAC is returned as a strict bytestring.
--
--   Per RFC 2104, the key /should/ be a minimum of 32 bytes long. Keys
--   exceeding 64 bytes in length will first be hashed (via SHA-256).
--
--   >>> hmac "strict bytestring key" "strict bytestring input"
--   "<strict 256-bit MAC>"
hmac :: BS.ByteString -> BS.ByteString -> MAC
hmac :: ByteString -> ByteString -> MAC
hmac ByteString
k ByteString
m
  | Bool
Native.sha256_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 32-byte result is written to the destination pointer.
_hmac_rr
  :: Ptr Word32    -- ^ destination (8 Word32s)
  -> Ptr Word32    -- ^ scratch block buffer (16 Word32s)
  -> Registers     -- ^ key
  -> Registers     -- ^ message
  -> IO ()
_hmac_rr :: Ptr Word32 -> Ptr Word32 -> Registers -> Registers -> IO ()
_hmac_rr Ptr Word32
rp Ptr Word32
bp Registers
k Registers
m
  | Bool
Native.sha256_native_available = Ptr Word32 -> Ptr Word32 -> Registers -> Registers -> IO ()
Native._hmac_rr Ptr Word32
rp Ptr Word32
bp Registers
k Registers
m
  | Bool
otherwise = Ptr Word32 -> Ptr Word32 -> Registers -> Registers -> IO ()
Pure._hmac_rr Ptr Word32
rp Ptr Word32
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 32-byte result is written to the destination pointer.
_hmac_rsb
  :: Ptr Word32    -- ^ destination pointer (8 x Word32)
  -> Ptr Word32    -- ^ scratch block pointer (16 x Word32)
  -> Registers     -- ^ k
  -> Registers     -- ^ v
  -> Word8         -- ^ separator byte
  -> BS.ByteString -- ^ data
  -> IO ()
_hmac_rsb :: Ptr Word32
-> Ptr Word32
-> Registers
-> Registers
-> Word8
-> ByteString
-> IO ()
_hmac_rsb Ptr Word32
rp Ptr Word32
bp Registers
k Registers
v Word8
sep ByteString
dat
  | Bool
Native.sha256_native_available = Ptr Word32
-> Ptr Word32
-> Registers
-> Registers
-> Word8
-> ByteString
-> IO ()
Native._hmac_rsb Ptr Word32
rp Ptr Word32
bp Registers
k Registers
v Word8
sep ByteString
dat
  | Bool
otherwise = Ptr Word32
-> Ptr Word32
-> Registers
-> Registers
-> Word8
-> ByteString
-> IO ()
Pure._hmac_rsb Ptr Word32
rp Ptr Word32
bp Registers
k Registers
v Word8
sep ByteString
dat
{-# INLINABLE _hmac_rsb #-}