{-# LANGUAGE DataKinds                  #-}

-- | The portable C-implementation of Sha256.
module Sha256.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 (Sha256, Sha256Mem, process256Last)

import Raaz.Verse.Sha256.C.Portable

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

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

type Prim                    = Sha256
type Internals               = Sha256Mem
type BufferAlignment         = 32
type BufferPtr               = AlignedBlockPtr BufferAlignment Prim

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

-- | The compression algorithm.
compressBlocks :: BufferPtr
               -> BlockCount Sha256
               -> Internals
               -> IO ()
compressBlocks :: BufferPtr -> BlockCount Sha256 -> Internals -> IO ()
compressBlocks BufferPtr
buf BlockCount Sha256
blks Internals
mem = let hPtr :: Ptr b
hPtr = Ptr Sha256 -> Ptr b
forall a b. Ptr a -> Ptr b
castPtr (Ptr Sha256 -> Ptr b) -> Ptr Sha256 -> Ptr b
forall a b. (a -> b) -> a -> b
$ Internals -> Ptr Sha256
forall h. Storable h => HashMemory64 h -> Ptr h
hashCellPointer Internals
mem
                                  blkPtr :: Ptr b
blkPtr = Ptr (Tuple 16 (BE Word32)) -> Ptr b
forall a b. Ptr a -> Ptr b
castPtr (Ptr (Tuple 16 (BE Word32)) -> Ptr b)
-> Ptr (Tuple 16 (BE Word32)) -> Ptr b
forall a b. (a -> b) -> a -> b
$ AlignedPtr BufferAlignment (Tuple 16 (BE Word32))
-> Ptr (Tuple 16 (BE Word32))
forall (n :: Nat) a. AlignedPtr n a -> Ptr a
forgetAlignment AlignedPtr BufferAlignment (Tuple 16 (BE Word32))
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 Sha256 -> Int
forall a. Enum a => a -> Int
fromEnum BlockCount Sha256
blks
                              in Ptr (Tuple 16 (BE Word32))
-> Word64 -> Ptr (Tuple 8 Word32) -> IO ()
verse_sha256_c_portable Ptr (Tuple 16 (BE Word32))
forall {b}. Ptr b
blkPtr Word64
wBlks Ptr (Tuple 8 Word32)
forall {b}. Ptr b
hPtr


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

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