{-# LANGUAGE ForeignFunctionInterface   #-}
{-# LANGUAGE DataKinds                  #-}

-- | The portable C-implementation of Sha512.
module Sha512.CHandWritten
       ( name, description
       , Prim, Internals, BufferAlignment
       , BufferPtr
       , additionalBlocks
       , processBlocks
       , processLast
       ) where

import Raaz.Core
import Raaz.Core.Types.Internal
import Raaz.Primitive.HashMemory
import Raaz.Primitive.Sha2.Internal (Sha512, Sha512Mem, process512Last)

name :: String
name :: String
name = String
"sha512-c-handwritten"

description :: String
description :: String
description = String
"Hand written Sha512 Implementation using portable C and Haskell FFI"

type Prim                    = Sha512
type Internals               = Sha512Mem
type BufferAlignment         = 32
type BufferPtr               = AlignedBlockPtr BufferAlignment Prim

additionalBlocks :: BlockCount Sha512
additionalBlocks :: BlockCount Sha512
additionalBlocks = Int -> Proxy Sha512 -> BlockCount Sha512
forall p. Int -> Proxy p -> BlockCount p
blocksOf Int
1 Proxy Sha512
forall k (t :: k). Proxy t
Proxy

------------------------ The foreign function calls  ---------------------

foreign import ccall unsafe
  "raaz/hash/sha512/portable.h raazHashSha512PortableCompress"
  c_sha512_compress  :: BufferPtr
                     -> BlockCount Sha512
                     -> Ptr Sha512
                     -> IO ()


compressBlocks :: BufferPtr
               -> BlockCount Sha512
               -> Internals
               -> IO ()
compressBlocks :: BufferPtr -> BlockCount Sha512 -> Internals -> IO ()
compressBlocks BufferPtr
buf BlockCount Sha512
blks = BufferPtr -> BlockCount Sha512 -> Ptr Sha512 -> IO ()
c_sha512_compress BufferPtr
buf BlockCount Sha512
blks (Ptr Sha512 -> IO ())
-> (Internals -> Ptr Sha512) -> Internals -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Internals -> Ptr Sha512
forall h. Storable h => HashMemory128 h -> Ptr h
hashCell128Pointer


processBlocks :: BufferPtr
              -> BlockCount Sha512
              -> Internals
              -> IO ()
processBlocks :: BufferPtr -> BlockCount Sha512 -> Internals -> IO ()
processBlocks BufferPtr
buf BlockCount Sha512
blks Internals
mem = BufferPtr -> BlockCount Sha512 -> Internals -> IO ()
compressBlocks BufferPtr
buf BlockCount Sha512
blks Internals
mem IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> BlockCount Sha512 -> Internals -> IO ()
forall len h. LengthUnit len => len -> HashMemory128 h -> IO ()
updateLength128 BlockCount Sha512
blks Internals
mem

-- | Process the last bytes.
processLast :: BufferPtr
            -> BYTES Int
            -> Internals
            -> IO ()
processLast :: BufferPtr -> BYTES Int -> Internals -> IO ()
processLast = (BufferPtr -> BlockCount Sha512 -> Internals -> IO ())
-> BufferPtr -> BYTES Int -> Internals -> IO ()
forall (n :: Nat).
KnownNat n =>
Compressor512 n
-> AlignedBlockPtr n Sha512 -> BYTES Int -> Internals -> IO ()
process512Last BufferPtr -> BlockCount Sha512 -> Internals -> IO ()
processBlocks