File: Blake2b.hs

package info (click to toggle)
haskell-crypton 1.0.4-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,548 kB
  • sloc: haskell: 26,764; ansic: 22,294; makefile: 6
file content (110 lines) | stat: -rw-r--r-- 3,598 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE TypeFamilies #-}

-- |
-- Module      : Crypto.Hash.Blake2b
-- License     : BSD-style
-- Maintainer  : Vincent Hanquez <vincent@snarc.org>
-- Stability   : experimental
-- Portability : unknown
--
-- Module containing the binding functions to work with the
-- Blake2b cryptographic hash.
module Crypto.Hash.Blake2b (
    Blake2b_160 (..),
    Blake2b_224 (..),
    Blake2b_256 (..),
    Blake2b_384 (..),
    Blake2b_512 (..),
) where

import Crypto.Hash.Types
import Data.Data
import Data.Word (Word32, Word8)
import Foreign.Ptr (Ptr)

-- | Blake2b (160 bits) cryptographic hash algorithm
data Blake2b_160 = Blake2b_160
    deriving (Show, Data)

instance HashAlgorithm Blake2b_160 where
    type HashBlockSize Blake2b_160 = 128
    type HashDigestSize Blake2b_160 = 20
    type HashInternalContextSize Blake2b_160 = 248
    hashBlockSize _ = 128
    hashDigestSize _ = 20
    hashInternalContextSize _ = 248
    hashInternalInit p = c_blake2b_init p 160
    hashInternalUpdate = c_blake2b_update
    hashInternalFinalize p = c_blake2b_finalize p 160

-- | Blake2b (224 bits) cryptographic hash algorithm
data Blake2b_224 = Blake2b_224
    deriving (Show, Data)

instance HashAlgorithm Blake2b_224 where
    type HashBlockSize Blake2b_224 = 128
    type HashDigestSize Blake2b_224 = 28
    type HashInternalContextSize Blake2b_224 = 248
    hashBlockSize _ = 128
    hashDigestSize _ = 28
    hashInternalContextSize _ = 248
    hashInternalInit p = c_blake2b_init p 224
    hashInternalUpdate = c_blake2b_update
    hashInternalFinalize p = c_blake2b_finalize p 224

-- | Blake2b (256 bits) cryptographic hash algorithm
data Blake2b_256 = Blake2b_256
    deriving (Show, Data)

instance HashAlgorithm Blake2b_256 where
    type HashBlockSize Blake2b_256 = 128
    type HashDigestSize Blake2b_256 = 32
    type HashInternalContextSize Blake2b_256 = 248
    hashBlockSize _ = 128
    hashDigestSize _ = 32
    hashInternalContextSize _ = 248
    hashInternalInit p = c_blake2b_init p 256
    hashInternalUpdate = c_blake2b_update
    hashInternalFinalize p = c_blake2b_finalize p 256

-- | Blake2b (384 bits) cryptographic hash algorithm
data Blake2b_384 = Blake2b_384
    deriving (Show, Data)

instance HashAlgorithm Blake2b_384 where
    type HashBlockSize Blake2b_384 = 128
    type HashDigestSize Blake2b_384 = 48
    type HashInternalContextSize Blake2b_384 = 248
    hashBlockSize _ = 128
    hashDigestSize _ = 48
    hashInternalContextSize _ = 248
    hashInternalInit p = c_blake2b_init p 384
    hashInternalUpdate = c_blake2b_update
    hashInternalFinalize p = c_blake2b_finalize p 384

-- | Blake2b (512 bits) cryptographic hash algorithm
data Blake2b_512 = Blake2b_512
    deriving (Show, Data)

instance HashAlgorithm Blake2b_512 where
    type HashBlockSize Blake2b_512 = 128
    type HashDigestSize Blake2b_512 = 64
    type HashInternalContextSize Blake2b_512 = 248
    hashBlockSize _ = 128
    hashDigestSize _ = 64
    hashInternalContextSize _ = 248
    hashInternalInit p = c_blake2b_init p 512
    hashInternalUpdate = c_blake2b_update
    hashInternalFinalize p = c_blake2b_finalize p 512

foreign import ccall unsafe "crypton_blake2b_init"
    c_blake2b_init :: Ptr (Context a) -> Word32 -> IO ()

foreign import ccall "crypton_blake2b_update"
    c_blake2b_update :: Ptr (Context a) -> Ptr Word8 -> Word32 -> IO ()

foreign import ccall unsafe "crypton_blake2b_finalize"
    c_blake2b_finalize :: Ptr (Context a) -> Word32 -> Ptr (Digest a) -> IO ()