1-- |
2-- Module      : Crypto.Hash.Blake2b
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-- Blake2b cryptographic hash.
10--
11{-# LANGUAGE ForeignFunctionInterface #-}
12{-# LANGUAGE DeriveDataTypeable #-}
13{-# LANGUAGE DataKinds #-}
14{-# LANGUAGE TypeFamilies #-}
15module Crypto.Hash.Blake2b
16    (  Blake2b_160 (..), Blake2b_224 (..), Blake2b_256 (..), Blake2b_384 (..), Blake2b_512 (..)
17    ) where
18
19import           Crypto.Hash.Types
20import           Foreign.Ptr (Ptr)
21import           Data.Data
22import           Data.Word (Word8, Word32)
23
24
25-- | Blake2b (160 bits) cryptographic hash algorithm
26data Blake2b_160 = Blake2b_160
27    deriving (Show,Data)
28
29instance HashAlgorithm Blake2b_160 where
30    type HashBlockSize           Blake2b_160 = 128
31    type HashDigestSize          Blake2b_160 = 20
32    type HashInternalContextSize Blake2b_160 = 248
33    hashBlockSize  _          = 128
34    hashDigestSize _          = 20
35    hashInternalContextSize _ = 248
36    hashInternalInit p        = c_blake2b_init p 160
37    hashInternalUpdate        = c_blake2b_update
38    hashInternalFinalize p    = c_blake2b_finalize p 160
39
40-- | Blake2b (224 bits) cryptographic hash algorithm
41data Blake2b_224 = Blake2b_224
42    deriving (Show,Data)
43
44instance HashAlgorithm Blake2b_224 where
45    type HashBlockSize           Blake2b_224 = 128
46    type HashDigestSize          Blake2b_224 = 28
47    type HashInternalContextSize Blake2b_224 = 248
48    hashBlockSize  _          = 128
49    hashDigestSize _          = 28
50    hashInternalContextSize _ = 248
51    hashInternalInit p        = c_blake2b_init p 224
52    hashInternalUpdate        = c_blake2b_update
53    hashInternalFinalize p    = c_blake2b_finalize p 224
54
55-- | Blake2b (256 bits) cryptographic hash algorithm
56data Blake2b_256 = Blake2b_256
57    deriving (Show,Data)
58
59instance HashAlgorithm Blake2b_256 where
60    type HashBlockSize           Blake2b_256 = 128
61    type HashDigestSize          Blake2b_256 = 32
62    type HashInternalContextSize Blake2b_256 = 248
63    hashBlockSize  _          = 128
64    hashDigestSize _          = 32
65    hashInternalContextSize _ = 248
66    hashInternalInit p        = c_blake2b_init p 256
67    hashInternalUpdate        = c_blake2b_update
68    hashInternalFinalize p    = c_blake2b_finalize p 256
69
70-- | Blake2b (384 bits) cryptographic hash algorithm
71data Blake2b_384 = Blake2b_384
72    deriving (Show,Data)
73
74instance HashAlgorithm Blake2b_384 where
75    type HashBlockSize           Blake2b_384 = 128
76    type HashDigestSize          Blake2b_384 = 48
77    type HashInternalContextSize Blake2b_384 = 248
78    hashBlockSize  _          = 128
79    hashDigestSize _          = 48
80    hashInternalContextSize _ = 248
81    hashInternalInit p        = c_blake2b_init p 384
82    hashInternalUpdate        = c_blake2b_update
83    hashInternalFinalize p    = c_blake2b_finalize p 384
84
85-- | Blake2b (512 bits) cryptographic hash algorithm
86data Blake2b_512 = Blake2b_512
87    deriving (Show,Data)
88
89instance HashAlgorithm Blake2b_512 where
90    type HashBlockSize           Blake2b_512 = 128
91    type HashDigestSize          Blake2b_512 = 64
92    type HashInternalContextSize Blake2b_512 = 248
93    hashBlockSize  _          = 128
94    hashDigestSize _          = 64
95    hashInternalContextSize _ = 248
96    hashInternalInit p        = c_blake2b_init p 512
97    hashInternalUpdate        = c_blake2b_update
98    hashInternalFinalize p    = c_blake2b_finalize p 512
99
100
101foreign import ccall unsafe "cryptonite_blake2b_init"
102    c_blake2b_init :: Ptr (Context a) -> Word32 -> IO ()
103
104foreign import ccall "cryptonite_blake2b_update"
105    c_blake2b_update :: Ptr (Context a) -> Ptr Word8 -> Word32 -> IO ()
106
107foreign import ccall unsafe "cryptonite_blake2b_finalize"
108    c_blake2b_finalize :: Ptr (Context a) -> Word32 -> Ptr (Digest a) -> IO ()
109