[Hash] remove unnecessary modules and not used template

This commit is contained in:
Vincent Hanquez 2015-04-28 11:37:42 +01:00
parent 4e12aceee9
commit 1c8d966d71
19 changed files with 0 additions and 1551 deletions

View File

@ -1,90 +0,0 @@
-- |
-- Module : Crypto.Hash.Internal.Kekkak
-- License : BSD-style
-- Maintainer : Vincent Hanquez <vincent@snarc.org>
-- Stability : experimental
-- Portability : unknown
--
-- A module containing Kekkak bindings
--
{-# LANGUAGE ForeignFunctionInterface #-}
module Crypto.Hash.Internal.Kekkak
( Ctx(..)
-- * Internal values
, sizeCtx
-- * Internal IO hash functions
, internalInit
, internalInitAt
, internalUpdate
, internalUpdateUnsafe
, internalFinalize
-- * Context copy and creation
, withCtxCopy
, withCtxNewThrow
, withCtxThrow
) where
import Foreign.Ptr
import Foreign.Storable (peek)
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
{- return the number of bytes of output for the digest -}
peekHashlen :: Ptr Ctx -> IO Int
peekHashlen ptr = peek iptr >>= \v -> return $! fromIntegral v
where iptr :: Ptr Word32
iptr = castPtr ptr
{-# INLINE sizeCtx #-}
sizeCtx :: Int
sizeCtx = 360
withCtxCopy :: Ctx -> (Ptr Ctx -> IO ()) -> IO Ctx
withCtxCopy (Ctx b) f = Ctx `fmap` bytesCopyAndModify b f
withCtxThrow :: Ctx -> (Ptr Ctx -> IO a) -> IO a
withCtxThrow (Ctx b) f = bytesCopyTemporary b f
withCtxNewThrow :: (Ptr Ctx -> IO a) -> IO a
withCtxNewThrow f = bytesTemporary 360 f
foreign import ccall unsafe "cryptonite_kekkak.h cryptonite_kekkak_init"
c_kekkak_init :: Ptr Ctx -> Word32 -> IO ()
foreign import ccall "cryptonite_kekkak.h cryptonite_kekkak_update"
c_kekkak_update :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_kekkak.h cryptonite_kekkak_update"
c_kekkak_update_unsafe :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_kekkak.h cryptonite_kekkak_finalize"
c_kekkak_finalize :: Ptr Ctx -> Ptr Word8 -> IO ()
internalInitAt :: Int -> Ptr Ctx -> IO ()
internalInitAt hashlen ptr = c_kekkak_init ptr (fromIntegral hashlen)
-- | init a context
internalInit :: Int -> IO Ctx
internalInit hashlen = Ctx `fmap` bytesAlloc 360 (internalInitAt hashlen)
-- | Update a context in place
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d =
B.withByteArray d $ \cs -> c_kekkak_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d =
B.withByteArray d $ \cs -> c_kekkak_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr =
peekHashlen ptr >>= \digestSize -> B.alloc digestSize (c_kekkak_finalize ptr)

View File

@ -1,87 +0,0 @@
-- |
-- Module : Crypto.Hash.Internal.MD2
-- License : BSD-style
-- Maintainer : Vincent Hanquez <vincent@snarc.org>
-- Stability : experimental
-- Portability : unknown
--
-- A module containing MD2 bindings
--
{-# LANGUAGE ForeignFunctionInterface #-}
module Crypto.Hash.Internal.MD2
( Ctx(..)
-- * Internal values
, digestSize
, sizeCtx
-- * Internal IO hash functions
, internalInit
, internalInitAt
, internalUpdate
, internalUpdateUnsafe
, internalFinalize
-- * Context copy and creation
, withCtxCopy
, withCtxNewThrow
, withCtxThrow
) where
import Foreign.Ptr
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
{-# INLINE digestSize #-}
digestSize :: Int
digestSize = 16
{-# INLINE sizeCtx #-}
sizeCtx :: Int
sizeCtx = 96
withCtxCopy :: Ctx -> (Ptr Ctx -> IO ()) -> IO Ctx
withCtxCopy (Ctx b) f = Ctx `fmap` bytesCopyAndModify b f
withCtxThrow :: Ctx -> (Ptr Ctx -> IO a) -> IO a
withCtxThrow (Ctx b) f = bytesCopyTemporary b f
withCtxNewThrow :: (Ptr Ctx -> IO a) -> IO a
withCtxNewThrow f = bytesTemporary 96 f
foreign import ccall unsafe "cryptonite_md2.h cryptonite_md2_init"
c_md2_init :: Ptr Ctx -> IO ()
foreign import ccall "cryptonite_md2.h cryptonite_md2_update"
c_md2_update :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_md2.h cryptonite_md2_update"
c_md2_update_unsafe :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_md2.h cryptonite_md2_finalize"
c_md2_finalize :: Ptr Ctx -> Ptr Word8 -> IO ()
internalInitAt :: Ptr Ctx -> IO ()
internalInitAt = c_md2_init
-- | init a context
internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc 96 internalInitAt
-- | Update a context in place
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d =
B.withByteArray d $ \cs -> c_md2_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d =
B.withByteArray d $ \cs -> c_md2_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = B.alloc digestSize (c_md2_finalize ptr)

View File

@ -1,87 +0,0 @@
-- |
-- Module : Crypto.Hash.Internal.MD4
-- License : BSD-style
-- Maintainer : Vincent Hanquez <vincent@snarc.org>
-- Stability : experimental
-- Portability : unknown
--
-- A module containing MD4 bindings
--
{-# LANGUAGE ForeignFunctionInterface #-}
module Crypto.Hash.Internal.MD4
( Ctx(..)
-- * Internal values
, digestSize
, sizeCtx
-- * Internal IO hash functions
, internalInit
, internalInitAt
, internalUpdate
, internalUpdateUnsafe
, internalFinalize
-- * Context copy and creation
, withCtxCopy
, withCtxNewThrow
, withCtxThrow
) where
import Foreign.Ptr
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
{-# INLINE digestSize #-}
digestSize :: Int
digestSize = 16
{-# INLINE sizeCtx #-}
sizeCtx :: Int
sizeCtx = 96
withCtxCopy :: Ctx -> (Ptr Ctx -> IO ()) -> IO Ctx
withCtxCopy (Ctx b) f = Ctx `fmap` bytesCopyAndModify b f
withCtxThrow :: Ctx -> (Ptr Ctx -> IO a) -> IO a
withCtxThrow (Ctx b) f = bytesCopyTemporary b f
withCtxNewThrow :: (Ptr Ctx -> IO a) -> IO a
withCtxNewThrow f = bytesTemporary 96 f
foreign import ccall unsafe "cryptonite_md4.h cryptonite_md4_init"
c_md4_init :: Ptr Ctx -> IO ()
foreign import ccall "cryptonite_md4.h cryptonite_md4_update"
c_md4_update :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_md4.h cryptonite_md4_update"
c_md4_update_unsafe :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_md4.h cryptonite_md4_finalize"
c_md4_finalize :: Ptr Ctx -> Ptr Word8 -> IO ()
internalInitAt :: Ptr Ctx -> IO ()
internalInitAt = c_md4_init
-- | init a context
internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc 96 internalInitAt
-- | Update a context in place
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d =
B.withByteArray d $ \cs -> c_md4_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d =
B.withByteArray d $ \cs -> c_md4_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = B.alloc digestSize (c_md4_finalize ptr)

View File

@ -1,87 +0,0 @@
-- |
-- Module : Crypto.Hash.Internal.MD5
-- License : BSD-style
-- Maintainer : Vincent Hanquez <vincent@snarc.org>
-- Stability : experimental
-- Portability : unknown
--
-- A module containing MD5 bindings
--
{-# LANGUAGE ForeignFunctionInterface #-}
module Crypto.Hash.Internal.MD5
( Ctx(..)
-- * Internal values
, digestSize
, sizeCtx
-- * Internal IO hash functions
, internalInit
, internalInitAt
, internalUpdate
, internalUpdateUnsafe
, internalFinalize
-- * Context copy and creation
, withCtxCopy
, withCtxNewThrow
, withCtxThrow
) where
import Foreign.Ptr
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
{-# INLINE digestSize #-}
digestSize :: Int
digestSize = 16
{-# INLINE sizeCtx #-}
sizeCtx :: Int
sizeCtx = 96
withCtxCopy :: Ctx -> (Ptr Ctx -> IO ()) -> IO Ctx
withCtxCopy (Ctx b) f = Ctx `fmap` bytesCopyAndModify b f
withCtxThrow :: Ctx -> (Ptr Ctx -> IO a) -> IO a
withCtxThrow (Ctx b) f = bytesCopyTemporary b f
withCtxNewThrow :: (Ptr Ctx -> IO a) -> IO a
withCtxNewThrow f = bytesTemporary 96 f
foreign import ccall unsafe "cryptonite_md5.h cryptonite_md5_init"
c_md5_init :: Ptr Ctx -> IO ()
foreign import ccall "cryptonite_md5.h cryptonite_md5_update"
c_md5_update :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_md5.h cryptonite_md5_update"
c_md5_update_unsafe :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_md5.h cryptonite_md5_finalize"
c_md5_finalize :: Ptr Ctx -> Ptr Word8 -> IO ()
internalInitAt :: Ptr Ctx -> IO ()
internalInitAt = c_md5_init
-- | init a context
internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc 96 internalInitAt
-- | Update a context in place
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d =
B.withByteArray d $ \cs -> c_md5_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d =
B.withByteArray d $ \cs -> c_md5_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = B.alloc digestSize (c_md5_finalize ptr)

View File

@ -1,87 +0,0 @@
-- |
-- Module : Crypto.Hash.Internal.RIPEMD160
-- License : BSD-style
-- Maintainer : Vincent Hanquez <vincent@snarc.org>
-- Stability : experimental
-- Portability : unknown
--
-- A module containing RIPEMD160 bindings
--
{-# LANGUAGE ForeignFunctionInterface #-}
module Crypto.Hash.Internal.RIPEMD160
( Ctx(..)
-- * Internal values
, digestSize
, sizeCtx
-- * Internal IO hash functions
, internalInit
, internalInitAt
, internalUpdate
, internalUpdateUnsafe
, internalFinalize
-- * Context copy and creation
, withCtxCopy
, withCtxNewThrow
, withCtxThrow
) where
import Foreign.Ptr
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
{-# INLINE digestSize #-}
digestSize :: Int
digestSize = 20
{-# INLINE sizeCtx #-}
sizeCtx :: Int
sizeCtx = 128
withCtxCopy :: Ctx -> (Ptr Ctx -> IO ()) -> IO Ctx
withCtxCopy (Ctx b) f = Ctx `fmap` bytesCopyAndModify b f
withCtxThrow :: Ctx -> (Ptr Ctx -> IO a) -> IO a
withCtxThrow (Ctx b) f = bytesCopyTemporary b f
withCtxNewThrow :: (Ptr Ctx -> IO a) -> IO a
withCtxNewThrow f = bytesTemporary 128 f
foreign import ccall unsafe "cryptonite_ripemd.h cryptonite_ripemd160_init"
c_ripemd160_init :: Ptr Ctx -> IO ()
foreign import ccall "cryptonite_ripemd.h cryptonite_ripemd160_update"
c_ripemd160_update :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_ripemd.h cryptonite_ripemd160_update"
c_ripemd160_update_unsafe :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_ripemd.h cryptonite_ripemd160_finalize"
c_ripemd160_finalize :: Ptr Ctx -> Ptr Word8 -> IO ()
internalInitAt :: Ptr Ctx -> IO ()
internalInitAt = c_ripemd160_init
-- | init a context
internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc 128 internalInitAt
-- | Update a context in place
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d =
B.withByteArray d $ \cs -> c_ripemd160_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d =
B.withByteArray d $ \cs -> c_ripemd160_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = B.alloc digestSize (c_ripemd160_finalize ptr)

View File

@ -1,87 +0,0 @@
-- |
-- Module : Crypto.Hash.Internal.SHA1
-- License : BSD-style
-- Maintainer : Vincent Hanquez <vincent@snarc.org>
-- Stability : experimental
-- Portability : unknown
--
-- A module containing SHA1 bindings
--
{-# LANGUAGE ForeignFunctionInterface #-}
module Crypto.Hash.Internal.SHA1
( Ctx(..)
-- * Internal values
, digestSize
, sizeCtx
-- * Internal IO hash functions
, internalInit
, internalInitAt
, internalUpdate
, internalUpdateUnsafe
, internalFinalize
-- * Context copy and creation
, withCtxCopy
, withCtxNewThrow
, withCtxThrow
) where
import Foreign.Ptr
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
{-# INLINE digestSize #-}
digestSize :: Int
digestSize = 20
{-# INLINE sizeCtx #-}
sizeCtx :: Int
sizeCtx = 96
withCtxCopy :: Ctx -> (Ptr Ctx -> IO ()) -> IO Ctx
withCtxCopy (Ctx b) f = Ctx `fmap` bytesCopyAndModify b f
withCtxThrow :: Ctx -> (Ptr Ctx -> IO a) -> IO a
withCtxThrow (Ctx b) f = bytesCopyTemporary b f
withCtxNewThrow :: (Ptr Ctx -> IO a) -> IO a
withCtxNewThrow f = bytesTemporary 96 f
foreign import ccall unsafe "cryptonite_sha1.h cryptonite_sha1_init"
c_sha1_init :: Ptr Ctx -> IO ()
foreign import ccall "cryptonite_sha1.h cryptonite_sha1_update"
c_sha1_update :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_sha1.h cryptonite_sha1_update"
c_sha1_update_unsafe :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_sha1.h cryptonite_sha1_finalize"
c_sha1_finalize :: Ptr Ctx -> Ptr Word8 -> IO ()
internalInitAt :: Ptr Ctx -> IO ()
internalInitAt = c_sha1_init
-- | init a context
internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc 96 internalInitAt
-- | Update a context in place
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d =
B.withByteArray d $ \cs -> c_sha1_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d =
B.withByteArray d $ \cs -> c_sha1_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = B.alloc digestSize (c_sha1_finalize ptr)

View File

@ -1,87 +0,0 @@
-- |
-- Module : Crypto.Hash.Internal.SHA224
-- License : BSD-style
-- Maintainer : Vincent Hanquez <vincent@snarc.org>
-- Stability : experimental
-- Portability : unknown
--
-- A module containing SHA224 bindings
--
{-# LANGUAGE ForeignFunctionInterface #-}
module Crypto.Hash.Internal.SHA224
( Ctx(..)
-- * Internal values
, digestSize
, sizeCtx
-- * Internal IO hash functions
, internalInit
, internalInitAt
, internalUpdate
, internalUpdateUnsafe
, internalFinalize
-- * Context copy and creation
, withCtxCopy
, withCtxNewThrow
, withCtxThrow
) where
import Foreign.Ptr
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
{-# INLINE digestSize #-}
digestSize :: Int
digestSize = 28
{-# INLINE sizeCtx #-}
sizeCtx :: Int
sizeCtx = 192
withCtxCopy :: Ctx -> (Ptr Ctx -> IO ()) -> IO Ctx
withCtxCopy (Ctx b) f = Ctx `fmap` bytesCopyAndModify b f
withCtxThrow :: Ctx -> (Ptr Ctx -> IO a) -> IO a
withCtxThrow (Ctx b) f = bytesCopyTemporary b f
withCtxNewThrow :: (Ptr Ctx -> IO a) -> IO a
withCtxNewThrow f = bytesTemporary 192 f
foreign import ccall unsafe "cryptonite_sha256.h cryptonite_sha224_init"
c_sha224_init :: Ptr Ctx -> IO ()
foreign import ccall "cryptonite_sha256.h cryptonite_sha224_update"
c_sha224_update :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_sha256.h cryptonite_sha224_update"
c_sha224_update_unsafe :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_sha256.h cryptonite_sha224_finalize"
c_sha224_finalize :: Ptr Ctx -> Ptr Word8 -> IO ()
internalInitAt :: Ptr Ctx -> IO ()
internalInitAt = c_sha224_init
-- | init a context
internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc 192 internalInitAt
-- | Update a context in place
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d =
B.withByteArray d $ \cs -> c_sha224_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d =
B.withByteArray d $ \cs -> c_sha224_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = B.alloc digestSize (c_sha224_finalize ptr)

View File

@ -1,87 +0,0 @@
-- |
-- Module : Crypto.Hash.Internal.SHA256
-- License : BSD-style
-- Maintainer : Vincent Hanquez <vincent@snarc.org>
-- Stability : experimental
-- Portability : unknown
--
-- A module containing SHA256 bindings
--
{-# LANGUAGE ForeignFunctionInterface #-}
module Crypto.Hash.Internal.SHA256
( Ctx(..)
-- * Internal values
, digestSize
, sizeCtx
-- * Internal IO hash functions
, internalInit
, internalInitAt
, internalUpdate
, internalUpdateUnsafe
, internalFinalize
-- * Context copy and creation
, withCtxCopy
, withCtxNewThrow
, withCtxThrow
) where
import Foreign.Ptr
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
{-# INLINE digestSize #-}
digestSize :: Int
digestSize = 32
{-# INLINE sizeCtx #-}
sizeCtx :: Int
sizeCtx = 192
withCtxCopy :: Ctx -> (Ptr Ctx -> IO ()) -> IO Ctx
withCtxCopy (Ctx b) f = Ctx `fmap` bytesCopyAndModify b f
withCtxThrow :: Ctx -> (Ptr Ctx -> IO a) -> IO a
withCtxThrow (Ctx b) f = bytesCopyTemporary b f
withCtxNewThrow :: (Ptr Ctx -> IO a) -> IO a
withCtxNewThrow f = bytesTemporary 192 f
foreign import ccall unsafe "cryptonite_sha256.h cryptonite_sha256_init"
c_sha256_init :: Ptr Ctx -> IO ()
foreign import ccall "cryptonite_sha256.h cryptonite_sha256_update"
c_sha256_update :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_sha256.h cryptonite_sha256_update"
c_sha256_update_unsafe :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_sha256.h cryptonite_sha256_finalize"
c_sha256_finalize :: Ptr Ctx -> Ptr Word8 -> IO ()
internalInitAt :: Ptr Ctx -> IO ()
internalInitAt = c_sha256_init
-- | init a context
internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc 192 internalInitAt
-- | Update a context in place
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d =
B.withByteArray d $ \cs -> c_sha256_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d =
B.withByteArray d $ \cs -> c_sha256_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = B.alloc digestSize (c_sha256_finalize ptr)

View File

@ -1,90 +0,0 @@
-- |
-- Module : Crypto.Hash.Internal.SHA3
-- License : BSD-style
-- Maintainer : Vincent Hanquez <vincent@snarc.org>
-- Stability : experimental
-- Portability : unknown
--
-- A module containing SHA3 bindings
--
{-# LANGUAGE ForeignFunctionInterface #-}
module Crypto.Hash.Internal.SHA3
( Ctx(..)
-- * Internal values
, sizeCtx
-- * Internal IO hash functions
, internalInit
, internalInitAt
, internalUpdate
, internalUpdateUnsafe
, internalFinalize
-- * Context copy and creation
, withCtxCopy
, withCtxNewThrow
, withCtxThrow
) where
import Foreign.Ptr
import Foreign.Storable (peek)
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
{- return the number of bytes of output for the digest -}
peekHashlen :: Ptr Ctx -> IO Int
peekHashlen ptr = peek iptr >>= \v -> return $! fromIntegral v
where iptr :: Ptr Word32
iptr = castPtr ptr
{-# INLINE sizeCtx #-}
sizeCtx :: Int
sizeCtx = 360
withCtxCopy :: Ctx -> (Ptr Ctx -> IO ()) -> IO Ctx
withCtxCopy (Ctx b) f = Ctx `fmap` bytesCopyAndModify b f
withCtxThrow :: Ctx -> (Ptr Ctx -> IO a) -> IO a
withCtxThrow (Ctx b) f = bytesCopyTemporary b f
withCtxNewThrow :: (Ptr Ctx -> IO a) -> IO a
withCtxNewThrow f = bytesTemporary 360 f
foreign import ccall unsafe "cryptonite_sha3.h cryptonite_sha3_init"
c_sha3_init :: Ptr Ctx -> Word32 -> IO ()
foreign import ccall "cryptonite_sha3.h cryptonite_sha3_update"
c_sha3_update :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_sha3.h cryptonite_sha3_update"
c_sha3_update_unsafe :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_sha3.h cryptonite_sha3_finalize"
c_sha3_finalize :: Ptr Ctx -> Ptr Word8 -> IO ()
internalInitAt :: Int -> Ptr Ctx -> IO ()
internalInitAt hashlen ptr = c_sha3_init ptr (fromIntegral hashlen)
-- | init a context
internalInit :: Int -> IO Ctx
internalInit hashlen = Ctx `fmap` bytesAlloc 360 (internalInitAt hashlen)
-- | Update a context in place
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d =
B.withByteArray d $ \cs -> c_sha3_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d =
B.withByteArray d $ \cs -> c_sha3_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr =
peekHashlen ptr >>= \digestSize -> B.alloc digestSize (c_sha3_finalize ptr)

View File

@ -1,87 +0,0 @@
-- |
-- Module : Crypto.Hash.Internal.SHA384
-- License : BSD-style
-- Maintainer : Vincent Hanquez <vincent@snarc.org>
-- Stability : experimental
-- Portability : unknown
--
-- A module containing SHA384 bindings
--
{-# LANGUAGE ForeignFunctionInterface #-}
module Crypto.Hash.Internal.SHA384
( Ctx(..)
-- * Internal values
, digestSize
, sizeCtx
-- * Internal IO hash functions
, internalInit
, internalInitAt
, internalUpdate
, internalUpdateUnsafe
, internalFinalize
-- * Context copy and creation
, withCtxCopy
, withCtxNewThrow
, withCtxThrow
) where
import Foreign.Ptr
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
{-# INLINE digestSize #-}
digestSize :: Int
digestSize = 48
{-# INLINE sizeCtx #-}
sizeCtx :: Int
sizeCtx = 256
withCtxCopy :: Ctx -> (Ptr Ctx -> IO ()) -> IO Ctx
withCtxCopy (Ctx b) f = Ctx `fmap` bytesCopyAndModify b f
withCtxThrow :: Ctx -> (Ptr Ctx -> IO a) -> IO a
withCtxThrow (Ctx b) f = bytesCopyTemporary b f
withCtxNewThrow :: (Ptr Ctx -> IO a) -> IO a
withCtxNewThrow f = bytesTemporary 256 f
foreign import ccall unsafe "cryptonite_sha512.h cryptonite_sha384_init"
c_sha384_init :: Ptr Ctx -> IO ()
foreign import ccall "cryptonite_sha512.h cryptonite_sha384_update"
c_sha384_update :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_sha512.h cryptonite_sha384_update"
c_sha384_update_unsafe :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_sha512.h cryptonite_sha384_finalize"
c_sha384_finalize :: Ptr Ctx -> Ptr Word8 -> IO ()
internalInitAt :: Ptr Ctx -> IO ()
internalInitAt = c_sha384_init
-- | init a context
internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc 256 internalInitAt
-- | Update a context in place
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d =
B.withByteArray d $ \cs -> c_sha384_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d =
B.withByteArray d $ \cs -> c_sha384_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = B.alloc digestSize (c_sha384_finalize ptr)

View File

@ -1,91 +0,0 @@
-- |
-- Module : Crypto.Hash.Internal.SHA512
-- License : BSD-style
-- Maintainer : Vincent Hanquez <vincent@snarc.org>
-- Stability : experimental
-- Portability : unknown
--
-- A module containing SHA512 bindings
--
{-# LANGUAGE ForeignFunctionInterface #-}
module Crypto.Hash.Internal.SHA512
( Ctx(..)
-- * Internal values
, digestSize
, sizeCtx
-- * Internal IO hash functions
, internalInit
, internalInitAt
, internalUpdate
, internalUpdateUnsafe
, internalFinalize
-- * Context copy and creation
, withCtxNew
, withCtxCopy
, withCtxNewThrow
, withCtxThrow
) where
import Foreign.Ptr
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
{-# INLINE digestSize #-}
digestSize :: Int
digestSize = 64
{-# INLINE sizeCtx #-}
sizeCtx :: Int
sizeCtx = 256
withCtxCopy :: Ctx -> (Ptr Ctx -> IO ()) -> IO Ctx
withCtxCopy (Ctx b) f = Ctx `fmap` bytesCopyAndModify b f
withCtxThrow :: Ctx -> (Ptr Ctx -> IO a) -> IO a
withCtxThrow (Ctx b) f = bytesCopyTemporary b f
withCtxNewThrow :: (Ptr Ctx -> IO a) -> IO a
withCtxNewThrow f = bytesTemporary 256 f
withCtxNew :: (Ptr Ctx -> IO ()) -> IO Ctx
withCtxNew f = Ctx `fmap` bytesAlloc 256 f
foreign import ccall unsafe "cryptonite_sha512.h cryptonite_sha512_init"
c_sha512_init :: Ptr Ctx -> IO ()
foreign import ccall "cryptonite_sha512.h cryptonite_sha512_update"
c_sha512_update :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_sha512.h cryptonite_sha512_update"
c_sha512_update_unsafe :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_sha512.h cryptonite_sha512_finalize"
c_sha512_finalize :: Ptr Ctx -> Ptr Word8 -> IO ()
internalInitAt :: Ptr Ctx -> IO ()
internalInitAt = c_sha512_init
-- | init a context
internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc 256 internalInitAt
-- | Update a context in place
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d =
B.withByteArray d $ \cs -> c_sha512_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d =
B.withByteArray d $ \cs -> c_sha512_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = B.alloc digestSize (c_sha512_finalize ptr)

View File

@ -1,37 +0,0 @@
{-# LANGUAGE ForeignFunctionInterface #-}
-- |
-- Module : Crypto.Hash.SHA512
-- License : BSD-style
-- Maintainer : Vincent Hanquez <vincent@snarc.org>
-- Stability : experimental
-- Portability : unknown
--
-- module containing the internal functions to work with the
-- SHA512t cryptographic hash (FIPS-180-4 truncated SHA512).
--
-- it is recommended to import this module qualified.
--
module Crypto.Hash.Internal.SHA512t
(
-- * Internal IO hash functions
internalInit
, internalInitAt
) where
import Foreign.Ptr
import Data.Word
import Crypto.Hash.Internal.SHA512 (Ctx)
import qualified Crypto.Hash.Internal.SHA512 as SHA512
foreign import ccall unsafe "cryptonite_sha512.h cryptonite_sha512_init_t"
c_sha512_init_t :: Ptr Ctx -> Word32 -> IO ()
-- | init a context using FIPS 180-4 for truncated SHA512
internalInitAt :: Int -> Ptr Ctx -> IO ()
internalInitAt hashlen ptr = c_sha512_init_t ptr (fromIntegral hashlen)
-- | init a context using FIPS 180-4 for truncated SHA512
internalInit :: Int -> IO Ctx
internalInit hashlen = do
SHA512.withCtxNew (internalInitAt hashlen)

View File

@ -1,90 +0,0 @@
-- |
-- Module : Crypto.Hash.Internal.Skein256
-- License : BSD-style
-- Maintainer : Vincent Hanquez <vincent@snarc.org>
-- Stability : experimental
-- Portability : unknown
--
-- A module containing Skein256 bindings
--
{-# LANGUAGE ForeignFunctionInterface #-}
module Crypto.Hash.Internal.Skein256
( Ctx(..)
-- * Internal values
, sizeCtx
-- * Internal IO hash functions
, internalInit
, internalInitAt
, internalUpdate
, internalUpdateUnsafe
, internalFinalize
-- * Context copy and creation
, withCtxCopy
, withCtxNewThrow
, withCtxThrow
) where
import Foreign.Ptr
import Foreign.Storable (peek)
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
{- return the number of bytes of output for the digest -}
peekHashlen :: Ptr Ctx -> IO Int
peekHashlen ptr = peek iptr >>= \v -> return $! fromIntegral v
where iptr :: Ptr Word32
iptr = castPtr ptr
{-# INLINE sizeCtx #-}
sizeCtx :: Int
sizeCtx = 96
withCtxCopy :: Ctx -> (Ptr Ctx -> IO ()) -> IO Ctx
withCtxCopy (Ctx b) f = Ctx `fmap` bytesCopyAndModify b f
withCtxThrow :: Ctx -> (Ptr Ctx -> IO a) -> IO a
withCtxThrow (Ctx b) f = bytesCopyTemporary b f
withCtxNewThrow :: (Ptr Ctx -> IO a) -> IO a
withCtxNewThrow f = bytesTemporary 96 f
foreign import ccall unsafe "cryptonite_skein256.h cryptonite_skein256_init"
c_skein256_init :: Ptr Ctx -> Word32 -> IO ()
foreign import ccall "cryptonite_skein256.h cryptonite_skein256_update"
c_skein256_update :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_skein256.h cryptonite_skein256_update"
c_skein256_update_unsafe :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_skein256.h cryptonite_skein256_finalize"
c_skein256_finalize :: Ptr Ctx -> Ptr Word8 -> IO ()
internalInitAt :: Int -> Ptr Ctx -> IO ()
internalInitAt hashlen ptr = c_skein256_init ptr (fromIntegral hashlen)
-- | init a context
internalInit :: Int -> IO Ctx
internalInit hashlen = Ctx `fmap` bytesAlloc 96 (internalInitAt hashlen)
-- | Update a context in place
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d =
B.withByteArray d $ \cs -> c_skein256_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d =
B.withByteArray d $ \cs -> c_skein256_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr =
peekHashlen ptr >>= \digestSize -> B.alloc digestSize (c_skein256_finalize ptr)

View File

@ -1,90 +0,0 @@
-- |
-- Module : Crypto.Hash.Internal.Skein512
-- License : BSD-style
-- Maintainer : Vincent Hanquez <vincent@snarc.org>
-- Stability : experimental
-- Portability : unknown
--
-- A module containing Skein512 bindings
--
{-# LANGUAGE ForeignFunctionInterface #-}
module Crypto.Hash.Internal.Skein512
( Ctx(..)
-- * Internal values
, sizeCtx
-- * Internal IO hash functions
, internalInit
, internalInitAt
, internalUpdate
, internalUpdateUnsafe
, internalFinalize
-- * Context copy and creation
, withCtxCopy
, withCtxNewThrow
, withCtxThrow
) where
import Foreign.Ptr
import Foreign.Storable (peek)
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
{- return the number of bytes of output for the digest -}
peekHashlen :: Ptr Ctx -> IO Int
peekHashlen ptr = peek iptr >>= \v -> return $! fromIntegral v
where iptr :: Ptr Word32
iptr = castPtr ptr
{-# INLINE sizeCtx #-}
sizeCtx :: Int
sizeCtx = 160
withCtxCopy :: Ctx -> (Ptr Ctx -> IO ()) -> IO Ctx
withCtxCopy (Ctx b) f = Ctx `fmap` bytesCopyAndModify b f
withCtxThrow :: Ctx -> (Ptr Ctx -> IO a) -> IO a
withCtxThrow (Ctx b) f = bytesCopyTemporary b f
withCtxNewThrow :: (Ptr Ctx -> IO a) -> IO a
withCtxNewThrow f = bytesTemporary 160 f
foreign import ccall unsafe "cryptonite_skein512.h cryptonite_skein512_init"
c_skein512_init :: Ptr Ctx -> Word32 -> IO ()
foreign import ccall "cryptonite_skein512.h cryptonite_skein512_update"
c_skein512_update :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_skein512.h cryptonite_skein512_update"
c_skein512_update_unsafe :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_skein512.h cryptonite_skein512_finalize"
c_skein512_finalize :: Ptr Ctx -> Ptr Word8 -> IO ()
internalInitAt :: Int -> Ptr Ctx -> IO ()
internalInitAt hashlen ptr = c_skein512_init ptr (fromIntegral hashlen)
-- | init a context
internalInit :: Int -> IO Ctx
internalInit hashlen = Ctx `fmap` bytesAlloc 160 (internalInitAt hashlen)
-- | Update a context in place
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d =
B.withByteArray d $ \cs -> c_skein512_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d =
B.withByteArray d $ \cs -> c_skein512_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr =
peekHashlen ptr >>= \digestSize -> B.alloc digestSize (c_skein512_finalize ptr)

View File

@ -1,87 +0,0 @@
-- |
-- Module : Crypto.Hash.Internal.Tiger
-- License : BSD-style
-- Maintainer : Vincent Hanquez <vincent@snarc.org>
-- Stability : experimental
-- Portability : unknown
--
-- A module containing Tiger bindings
--
{-# LANGUAGE ForeignFunctionInterface #-}
module Crypto.Hash.Internal.Tiger
( Ctx(..)
-- * Internal values
, digestSize
, sizeCtx
-- * Internal IO hash functions
, internalInit
, internalInitAt
, internalUpdate
, internalUpdateUnsafe
, internalFinalize
-- * Context copy and creation
, withCtxCopy
, withCtxNewThrow
, withCtxThrow
) where
import Foreign.Ptr
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
{-# INLINE digestSize #-}
digestSize :: Int
digestSize = 24
{-# INLINE sizeCtx #-}
sizeCtx :: Int
sizeCtx = 96
withCtxCopy :: Ctx -> (Ptr Ctx -> IO ()) -> IO Ctx
withCtxCopy (Ctx b) f = Ctx `fmap` bytesCopyAndModify b f
withCtxThrow :: Ctx -> (Ptr Ctx -> IO a) -> IO a
withCtxThrow (Ctx b) f = bytesCopyTemporary b f
withCtxNewThrow :: (Ptr Ctx -> IO a) -> IO a
withCtxNewThrow f = bytesTemporary 96 f
foreign import ccall unsafe "cryptonite_tiger.h cryptonite_tiger_init"
c_tiger_init :: Ptr Ctx -> IO ()
foreign import ccall "cryptonite_tiger.h cryptonite_tiger_update"
c_tiger_update :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_tiger.h cryptonite_tiger_update"
c_tiger_update_unsafe :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_tiger.h cryptonite_tiger_finalize"
c_tiger_finalize :: Ptr Ctx -> Ptr Word8 -> IO ()
internalInitAt :: Ptr Ctx -> IO ()
internalInitAt = c_tiger_init
-- | init a context
internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc 96 internalInitAt
-- | Update a context in place
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d =
B.withByteArray d $ \cs -> c_tiger_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d =
B.withByteArray d $ \cs -> c_tiger_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = B.alloc digestSize (c_tiger_finalize ptr)

View File

@ -1,87 +0,0 @@
-- |
-- Module : Crypto.Hash.Internal.Whirlpool
-- License : BSD-style
-- Maintainer : Vincent Hanquez <vincent@snarc.org>
-- Stability : experimental
-- Portability : unknown
--
-- A module containing Whirlpool bindings
--
{-# LANGUAGE ForeignFunctionInterface #-}
module Crypto.Hash.Internal.Whirlpool
( Ctx(..)
-- * Internal values
, digestSize
, sizeCtx
-- * Internal IO hash functions
, internalInit
, internalInitAt
, internalUpdate
, internalUpdateUnsafe
, internalFinalize
-- * Context copy and creation
, withCtxCopy
, withCtxNewThrow
, withCtxThrow
) where
import Foreign.Ptr
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
{-# INLINE digestSize #-}
digestSize :: Int
digestSize = 64
{-# INLINE sizeCtx #-}
sizeCtx :: Int
sizeCtx = 168
withCtxCopy :: Ctx -> (Ptr Ctx -> IO ()) -> IO Ctx
withCtxCopy (Ctx b) f = Ctx `fmap` bytesCopyAndModify b f
withCtxThrow :: Ctx -> (Ptr Ctx -> IO a) -> IO a
withCtxThrow (Ctx b) f = bytesCopyTemporary b f
withCtxNewThrow :: (Ptr Ctx -> IO a) -> IO a
withCtxNewThrow f = bytesTemporary 168 f
foreign import ccall unsafe "cryptonite_whirlpool.h cryptonite_whirlpool_init"
c_whirlpool_init :: Ptr Ctx -> IO ()
foreign import ccall "cryptonite_whirlpool.h cryptonite_whirlpool_update"
c_whirlpool_update :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_whirlpool.h cryptonite_whirlpool_update"
c_whirlpool_update_unsafe :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_whirlpool.h cryptonite_whirlpool_finalize"
c_whirlpool_finalize :: Ptr Ctx -> Ptr Word8 -> IO ()
internalInitAt :: Ptr Ctx -> IO ()
internalInitAt = c_whirlpool_init
-- | init a context
internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc 168 internalInitAt
-- | Update a context in place
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d =
B.withByteArray d $ \cs -> c_whirlpool_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d =
B.withByteArray d $ \cs -> c_whirlpool_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = B.alloc digestSize (c_whirlpool_finalize ptr)

View File

@ -108,22 +108,6 @@ Library
Crypto.Hash.Utils
Crypto.Hash.Utils.Cpu
Crypto.Hash.Types
Crypto.Hash.Internal.SHA1
Crypto.Hash.Internal.SHA224
Crypto.Hash.Internal.SHA256
Crypto.Hash.Internal.SHA384
Crypto.Hash.Internal.SHA512
Crypto.Hash.Internal.SHA512t
Crypto.Hash.Internal.SHA3
Crypto.Hash.Internal.Kekkak
Crypto.Hash.Internal.MD2
Crypto.Hash.Internal.MD4
Crypto.Hash.Internal.MD5
Crypto.Hash.Internal.RIPEMD160
Crypto.Hash.Internal.Skein256
Crypto.Hash.Internal.Skein512
Crypto.Hash.Internal.Tiger
Crypto.Hash.Internal.Whirlpool
Crypto.Random.Entropy.Source
Crypto.Random.Entropy.Backend
Crypto.Random.ChaChaDRG

View File

@ -1,90 +0,0 @@
-- |
-- Module : Crypto.Hash.Internal.%%MODULENAME%%
-- License : BSD-style
-- Maintainer : Vincent Hanquez <vincent@snarc.org>
-- Stability : experimental
-- Portability : unknown
--
-- A module containing %%MODULENAME%% bindings
--
{-# LANGUAGE ForeignFunctionInterface #-}
module Crypto.Hash.Internal.%%MODULENAME%%
( Ctx(..)
-- * Internal values
, sizeCtx
-- * Internal IO hash functions
, internalInit
, internalInitAt
, internalUpdate
, internalUpdateUnsafe
, internalFinalize
-- * Context copy and creation
, withCtxCopy
, withCtxNewThrow
, withCtxThrow
) where
import Foreign.Ptr
import Foreign.Storable (peek)
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
{- return the number of bytes of output for the digest -}
peekHashlen :: Ptr Ctx -> IO Int
peekHashlen ptr = peek iptr >>= \v -> return $! fromIntegral v
where iptr :: Ptr Word32
iptr = castPtr ptr
{-# INLINE sizeCtx #-}
sizeCtx :: Int
sizeCtx = %%SIZECTX%%
withCtxCopy :: Ctx -> (Ptr Ctx -> IO ()) -> IO Ctx
withCtxCopy (Ctx b) f = Ctx `fmap` bytesCopyAndModify b f
withCtxThrow :: Ctx -> (Ptr Ctx -> IO a) -> IO a
withCtxThrow (Ctx b) f = bytesCopyTemporary b f
withCtxNewThrow :: (Ptr Ctx -> IO a) -> IO a
withCtxNewThrow f = bytesTemporary %%SIZECTX%% f
foreign import ccall unsafe "cryptonite_%%HEADER_FILE%% cryptonite_%%HASHNAME%%_init"
c_%%HASHNAME%%_init :: Ptr Ctx -> Word32 -> IO ()
foreign import ccall "cryptonite_%%HEADER_FILE%% cryptonite_%%HASHNAME%%_update"
c_%%HASHNAME%%_update :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_%%HEADER_FILE%% cryptonite_%%HASHNAME%%_update"
c_%%HASHNAME%%_update_unsafe :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_%%HEADER_FILE%% cryptonite_%%HASHNAME%%_finalize"
c_%%HASHNAME%%_finalize :: Ptr Ctx -> Ptr Word8 -> IO ()
internalInitAt :: Int -> Ptr Ctx -> IO ()
internalInitAt hashlen ptr = c_%%HASHNAME%%_init ptr (fromIntegral hashlen)
-- | init a context
internalInit :: Int -> IO Ctx
internalInit hashlen = Ctx `fmap` bytesAlloc %%SIZECTX%% (internalInitAt hashlen)
-- | Update a context in place
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d =
B.withByteArray d $ \cs -> c_%%HASHNAME%%_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d =
B.withByteArray d $ \cs -> c_%%HASHNAME%%_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr =
peekHashlen ptr >>= \digestSize -> B.alloc digestSize (c_%%HASHNAME%%_finalize ptr)

View File

@ -1,87 +0,0 @@
-- |
-- Module : Crypto.Hash.Internal.%%MODULENAME%%
-- License : BSD-style
-- Maintainer : Vincent Hanquez <vincent@snarc.org>
-- Stability : experimental
-- Portability : unknown
--
-- A module containing %%MODULENAME%% bindings
--
{-# LANGUAGE ForeignFunctionInterface #-}
module Crypto.Hash.Internal.%%MODULENAME%%
( Ctx(..)
-- * Internal values
, digestSize
, sizeCtx
-- * Internal IO hash functions
, internalInit
, internalInitAt
, internalUpdate
, internalUpdateUnsafe
, internalFinalize
-- * Context copy and creation
, withCtxCopy
, withCtxNewThrow
, withCtxThrow
) where
import Foreign.Ptr
import Crypto.Internal.ByteArray (ByteArrayAccess, ByteArray)
import qualified Crypto.Internal.ByteArray as B
import Data.Word
import Crypto.Internal.Memory
newtype Ctx = Ctx Bytes
{-# INLINE digestSize #-}
digestSize :: Int
digestSize = %%DIGESTSIZE%%
{-# INLINE sizeCtx #-}
sizeCtx :: Int
sizeCtx = %%SIZECTX%%
withCtxCopy :: Ctx -> (Ptr Ctx -> IO ()) -> IO Ctx
withCtxCopy (Ctx b) f = Ctx `fmap` bytesCopyAndModify b f
withCtxThrow :: Ctx -> (Ptr Ctx -> IO a) -> IO a
withCtxThrow (Ctx b) f = bytesCopyTemporary b f
withCtxNewThrow :: (Ptr Ctx -> IO a) -> IO a
withCtxNewThrow f = bytesTemporary %%SIZECTX%% f
foreign import ccall unsafe "cryptonite_%%HEADER_FILE%% cryptonite_%%HASHNAME%%_init"
c_%%HASHNAME%%_init :: Ptr Ctx -> IO ()
foreign import ccall "cryptonite_%%HEADER_FILE%% cryptonite_%%HASHNAME%%_update"
c_%%HASHNAME%%_update :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_%%HEADER_FILE%% cryptonite_%%HASHNAME%%_update"
c_%%HASHNAME%%_update_unsafe :: Ptr Ctx -> Ptr Word8 -> Word32 -> IO ()
foreign import ccall unsafe "cryptonite_%%HEADER_FILE%% cryptonite_%%HASHNAME%%_finalize"
c_%%HASHNAME%%_finalize :: Ptr Ctx -> Ptr Word8 -> IO ()
internalInitAt :: Ptr Ctx -> IO ()
internalInitAt = c_%%HASHNAME%%_init
-- | init a context
internalInit :: IO Ctx
internalInit = Ctx `fmap` bytesAlloc %%SIZECTX%% internalInitAt
-- | Update a context in place
internalUpdate :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdate ptr d =
B.withByteArray d $ \cs -> c_%%HASHNAME%%_update ptr cs (fromIntegral $ B.length d)
-- | Update a context in place using an unsafe foreign function call.
--
-- It is faster than `internalUpdate`, but will block the haskell runtime.
-- This shouldn't be used if the input data is large.
internalUpdateUnsafe :: ByteArrayAccess ba => Ptr Ctx -> ba -> IO ()
internalUpdateUnsafe ptr d =
B.withByteArray d $ \cs -> c_%%HASHNAME%%_update_unsafe ptr cs (fromIntegral $ B.length d)
-- | Finalize a context in place
internalFinalize :: ByteArray output => Ptr Ctx -> IO output
internalFinalize ptr = B.alloc digestSize (c_%%HASHNAME%%_finalize ptr)