{-# LANGUAGE DataKinds                  #-}

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

import Foreign.Ptr                ( castPtr      )

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

import Raaz.Verse.Sha512.C.Portable


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

description :: String
description :: String
description = String
"Sha512 Implementation in C exposed by libverse"

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

compressBlocks :: BufferPtr
               -> BlockCount Sha512
               -> Internals
               -> IO ()
compressBlocks :: BufferPtr -> BlockCount Sha512 -> Internals -> IO ()
compressBlocks BufferPtr
buf BlockCount Sha512
blks Internals
mem = let hPtr :: Ptr b
hPtr = Ptr Sha512 -> Ptr b
forall a b. Ptr a -> Ptr b
castPtr (Ptr Sha512 -> Ptr b) -> Ptr Sha512 -> Ptr b
forall a b. (a -> b) -> a -> b
$ Internals -> Ptr Sha512
forall h. Storable h => HashMemory128 h -> Ptr h
hashCell128Pointer Internals
mem
                                  blkPtr :: Ptr b
blkPtr = Ptr (Tuple 16 (BE Word64)) -> Ptr b
forall a b. Ptr a -> Ptr b
castPtr (Ptr (Tuple 16 (BE Word64)) -> Ptr b)
-> Ptr (Tuple 16 (BE Word64)) -> Ptr b
forall a b. (a -> b) -> a -> b
$ AlignedPtr BufferAlignment (Tuple 16 (BE Word64))
-> Ptr (Tuple 16 (BE Word64))
forall (n :: Nat) a. AlignedPtr n a -> Ptr a
forgetAlignment AlignedPtr BufferAlignment (Tuple 16 (BE Word64))
BufferPtr
buf
                                  wBlks :: Word64
wBlks  = Int -> Word64
forall a. Enum a => Int -> a
toEnum (Int -> Word64) -> Int -> Word64
forall a b. (a -> b) -> a -> b
$ BlockCount Sha512 -> Int
forall a. Enum a => a -> Int
fromEnum BlockCount Sha512
blks
                               in Ptr (Tuple 16 (BE Word64))
-> Word64 -> Ptr (Tuple 8 Word64) -> IO ()
verse_sha512_c_portable Ptr (Tuple 16 (BE Word64))
forall b. Ptr b
blkPtr Word64
wBlks Ptr (Tuple 8 Word64)
forall b. Ptr b
hPtr

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