-- |
-- Module      : Crypto.Hash.SHA256
-- License     : BSD-style
-- Maintainer  : Vincent Hanquez <vincent@snarc.org>
-- Stability   : experimental
-- Portability : unknown
--
-- Module containing the binding functions to work with the
-- SHA256 cryptographic hash.
--
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeFamilies #-}
module Crypto.Hash.SHA256 ( SHA256 (..) ) where

import           Crypto.Hash.Types
import           Foreign.Ptr (Ptr)
import           Data.Data
import           Data.Word (Word8, Word32)

-- | SHA256 cryptographic hash algorithm
data SHA256 = SHA256
    deriving (Int -> SHA256 -> ShowS
[SHA256] -> ShowS
SHA256 -> String
(Int -> SHA256 -> ShowS)
-> (SHA256 -> String) -> ([SHA256] -> ShowS) -> Show SHA256
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SHA256] -> ShowS
$cshowList :: [SHA256] -> ShowS
show :: SHA256 -> String
$cshow :: SHA256 -> String
showsPrec :: Int -> SHA256 -> ShowS
$cshowsPrec :: Int -> SHA256 -> ShowS
Show,Typeable SHA256
DataType
Constr
Typeable SHA256
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> SHA256 -> c SHA256)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c SHA256)
-> (SHA256 -> Constr)
-> (SHA256 -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c SHA256))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c SHA256))
-> ((forall b. Data b => b -> b) -> SHA256 -> SHA256)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> SHA256 -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> SHA256 -> r)
-> (forall u. (forall d. Data d => d -> u) -> SHA256 -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> SHA256 -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> SHA256 -> m SHA256)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> SHA256 -> m SHA256)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> SHA256 -> m SHA256)
-> Data SHA256
SHA256 -> DataType
SHA256 -> Constr
(forall b. Data b => b -> b) -> SHA256 -> SHA256
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> SHA256 -> c SHA256
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c SHA256
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> SHA256 -> u
forall u. (forall d. Data d => d -> u) -> SHA256 -> [u]
forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> SHA256 -> r
forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> SHA256 -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> SHA256 -> m SHA256
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> SHA256 -> m SHA256
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c SHA256
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> SHA256 -> c SHA256
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c SHA256)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c SHA256)
$cSHA256 :: Constr
$tSHA256 :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> SHA256 -> m SHA256
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> SHA256 -> m SHA256
gmapMp :: (forall d. Data d => d -> m d) -> SHA256 -> m SHA256
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> SHA256 -> m SHA256
gmapM :: (forall d. Data d => d -> m d) -> SHA256 -> m SHA256
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> SHA256 -> m SHA256
gmapQi :: Int -> (forall d. Data d => d -> u) -> SHA256 -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> SHA256 -> u
gmapQ :: (forall d. Data d => d -> u) -> SHA256 -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> SHA256 -> [u]
gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> SHA256 -> r
$cgmapQr :: forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> SHA256 -> r
gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> SHA256 -> r
$cgmapQl :: forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> SHA256 -> r
gmapT :: (forall b. Data b => b -> b) -> SHA256 -> SHA256
$cgmapT :: (forall b. Data b => b -> b) -> SHA256 -> SHA256
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c SHA256)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c SHA256)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c SHA256)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c SHA256)
dataTypeOf :: SHA256 -> DataType
$cdataTypeOf :: SHA256 -> DataType
toConstr :: SHA256 -> Constr
$ctoConstr :: SHA256 -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c SHA256
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c SHA256
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> SHA256 -> c SHA256
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> SHA256 -> c SHA256
$cp1Data :: Typeable SHA256
Data)

instance HashAlgorithm SHA256 where
    type HashBlockSize           SHA256 = 64
    type HashDigestSize          SHA256 = 32
    type HashInternalContextSize SHA256 = 192
    hashBlockSize :: SHA256 -> Int
hashBlockSize  SHA256
_          = Int
64
    hashDigestSize :: SHA256 -> Int
hashDigestSize SHA256
_          = Int
32
    hashInternalContextSize :: SHA256 -> Int
hashInternalContextSize SHA256
_ = Int
192
    hashInternalInit :: Ptr (Context SHA256) -> IO ()
hashInternalInit          = Ptr (Context SHA256) -> IO ()
forall a. Ptr (Context a) -> IO ()
c_sha256_init
    hashInternalUpdate :: Ptr (Context SHA256) -> Ptr Word8 -> Word32 -> IO ()
hashInternalUpdate        = Ptr (Context SHA256) -> Ptr Word8 -> Word32 -> IO ()
forall a. Ptr (Context a) -> Ptr Word8 -> Word32 -> IO ()
c_sha256_update
    hashInternalFinalize :: Ptr (Context SHA256) -> Ptr (Digest SHA256) -> IO ()
hashInternalFinalize      = Ptr (Context SHA256) -> Ptr (Digest SHA256) -> IO ()
forall a. Ptr (Context a) -> Ptr (Digest a) -> IO ()
c_sha256_finalize

instance HashAlgorithmPrefix SHA256 where
    hashInternalFinalizePrefix :: Ptr (Context SHA256)
-> Ptr Word8 -> Word32 -> Word32 -> Ptr (Digest SHA256) -> IO ()
hashInternalFinalizePrefix = Ptr (Context SHA256)
-> Ptr Word8 -> Word32 -> Word32 -> Ptr (Digest SHA256) -> IO ()
forall a.
Ptr (Context a)
-> Ptr Word8 -> Word32 -> Word32 -> Ptr (Digest a) -> IO ()
c_sha256_finalize_prefix

foreign import ccall unsafe "cryptonite_sha256_init"
    c_sha256_init :: Ptr (Context a)-> IO ()

foreign import ccall "cryptonite_sha256_update"
    c_sha256_update :: Ptr (Context a) -> Ptr Word8 -> Word32 -> IO ()

foreign import ccall unsafe "cryptonite_sha256_finalize"
    c_sha256_finalize :: Ptr (Context a) -> Ptr (Digest a) -> IO ()

foreign import ccall "cryptonite_sha256_finalize_prefix"
    c_sha256_finalize_prefix :: Ptr (Context a) -> Ptr Word8 -> Word32 -> Word32 -> Ptr (Digest a) -> IO ()