1-- |
2-- Module      : Crypto.Hash.SHA3
3-- License     : BSD-style
4-- Maintainer  : Vincent Hanquez <vincent@snarc.org>
5-- Stability   : experimental
6-- Portability : unknown
7--
8-- Module containing the binding functions to work with the
9-- SHA3 cryptographic hash.
10--
11{-# LANGUAGE ForeignFunctionInterface #-}
12{-# LANGUAGE DeriveDataTypeable #-}
13{-# LANGUAGE DataKinds #-}
14{-# LANGUAGE TypeFamilies #-}
15module Crypto.Hash.SHA3
16    (  SHA3_224 (..), SHA3_256 (..), SHA3_384 (..), SHA3_512 (..)
17    ) where
18
19import           Crypto.Hash.Types
20import           Foreign.Ptr (Ptr)
21import           Data.Data
22import           Data.Word (Word8, Word32)
23
24
25-- | SHA3 (224 bits) cryptographic hash algorithm
26data SHA3_224 = SHA3_224
27    deriving (Show,Data)
28
29instance HashAlgorithm SHA3_224 where
30    type HashBlockSize           SHA3_224 = 144
31    type HashDigestSize          SHA3_224 = 28
32    type HashInternalContextSize SHA3_224 = 352
33    hashBlockSize  _          = 144
34    hashDigestSize _          = 28
35    hashInternalContextSize _ = 352
36    hashInternalInit p        = c_sha3_init p 224
37    hashInternalUpdate        = c_sha3_update
38    hashInternalFinalize p    = c_sha3_finalize p 224
39
40-- | SHA3 (256 bits) cryptographic hash algorithm
41data SHA3_256 = SHA3_256
42    deriving (Show,Data)
43
44instance HashAlgorithm SHA3_256 where
45    type HashBlockSize           SHA3_256 = 136
46    type HashDigestSize          SHA3_256 = 32
47    type HashInternalContextSize SHA3_256 = 344
48    hashBlockSize  _          = 136
49    hashDigestSize _          = 32
50    hashInternalContextSize _ = 344
51    hashInternalInit p        = c_sha3_init p 256
52    hashInternalUpdate        = c_sha3_update
53    hashInternalFinalize p    = c_sha3_finalize p 256
54
55-- | SHA3 (384 bits) cryptographic hash algorithm
56data SHA3_384 = SHA3_384
57    deriving (Show,Data)
58
59instance HashAlgorithm SHA3_384 where
60    type HashBlockSize           SHA3_384 = 104
61    type HashDigestSize          SHA3_384 = 48
62    type HashInternalContextSize SHA3_384 = 312
63    hashBlockSize  _          = 104
64    hashDigestSize _          = 48
65    hashInternalContextSize _ = 312
66    hashInternalInit p        = c_sha3_init p 384
67    hashInternalUpdate        = c_sha3_update
68    hashInternalFinalize p    = c_sha3_finalize p 384
69
70-- | SHA3 (512 bits) cryptographic hash algorithm
71data SHA3_512 = SHA3_512
72    deriving (Show,Data)
73
74instance HashAlgorithm SHA3_512 where
75    type HashBlockSize           SHA3_512 = 72
76    type HashDigestSize          SHA3_512 = 64
77    type HashInternalContextSize SHA3_512 = 280
78    hashBlockSize  _          = 72
79    hashDigestSize _          = 64
80    hashInternalContextSize _ = 280
81    hashInternalInit p        = c_sha3_init p 512
82    hashInternalUpdate        = c_sha3_update
83    hashInternalFinalize p    = c_sha3_finalize p 512
84
85
86foreign import ccall unsafe "cryptonite_sha3_init"
87    c_sha3_init :: Ptr (Context a) -> Word32 -> IO ()
88
89foreign import ccall "cryptonite_sha3_update"
90    c_sha3_update :: Ptr (Context a) -> Ptr Word8 -> Word32 -> IO ()
91
92foreign import ccall unsafe "cryptonite_sha3_finalize"
93    c_sha3_finalize :: Ptr (Context a) -> Word32 -> Ptr (Digest a) -> IO ()
94