{-# LANGUAGE TypeFamilies #-}
-------------------------------------------------
-- |
-- Module      : Crypto.Noise.Hash.BLAKE2b
-- Maintainer  : John Galt <jgalt@centromere.net>
-- Stability   : experimental
-- Portability : POSIX
module Crypto.Noise.Hash.BLAKE2b
  ( -- * Types
    BLAKE2b
  ) where

import qualified Crypto.Hash      as H
import qualified Crypto.MAC.HMAC  as M
import Data.ByteArray (ScrubbedBytes, convert, empty, snoc)
import Data.List      (unfoldr)
import Data.Word      (Word8)

import Crypto.Noise.Hash

-- | Represents the BLAKE2b hash.
data BLAKE2b

instance Hash BLAKE2b where
  newtype ChainingKey BLAKE2b = HCKB2b ScrubbedBytes
  newtype Digest      BLAKE2b = HDB2b  (H.Digest H.Blake2b_512)

  hashName :: forall (proxy :: * -> *). proxy BLAKE2b -> ScrubbedBytes
hashName   proxy BLAKE2b
_  = ScrubbedBytes
"BLAKE2b"
  hashLength :: forall (proxy :: * -> *). proxy BLAKE2b -> Int
hashLength proxy BLAKE2b
_  = Int
64
  hash :: ScrubbedBytes -> Digest BLAKE2b
hash          = ScrubbedBytes -> Digest BLAKE2b
hash'
  hashHKDF :: ChainingKey BLAKE2b -> ScrubbedBytes -> Word8 -> [ScrubbedBytes]
hashHKDF      = ChainingKey BLAKE2b -> ScrubbedBytes -> Word8 -> [ScrubbedBytes]
hkdf
  hashBytesToCK :: ScrubbedBytes -> ChainingKey BLAKE2b
hashBytesToCK = ScrubbedBytes -> ChainingKey BLAKE2b
bytesToCK
  hashCKToBytes :: ChainingKey BLAKE2b -> ScrubbedBytes
hashCKToBytes = ChainingKey BLAKE2b -> ScrubbedBytes
ckToBytes
  hashToBytes :: Digest BLAKE2b -> ScrubbedBytes
hashToBytes   = Digest BLAKE2b -> ScrubbedBytes
toBytes

hash' :: ScrubbedBytes
      -> Digest BLAKE2b
hash' :: ScrubbedBytes -> Digest BLAKE2b
hash' ScrubbedBytes
bs = Digest Blake2b_512 -> Digest BLAKE2b
HDB2b (Digest Blake2b_512 -> Digest BLAKE2b)
-> Digest Blake2b_512 -> Digest BLAKE2b
forall a b. (a -> b) -> a -> b
$ ScrubbedBytes -> Digest Blake2b_512
forall ba a.
(ByteArrayAccess ba, HashAlgorithm a) =>
ba -> Digest a
H.hash ScrubbedBytes
bs

hkdf :: ChainingKey BLAKE2b
     -> ScrubbedBytes
     -> Word8
     -> [ScrubbedBytes]
hkdf :: ChainingKey BLAKE2b -> ScrubbedBytes -> Word8 -> [ScrubbedBytes]
hkdf (HCKB2b ScrubbedBytes
ck) ScrubbedBytes
keyMat Word8
numOutputs = (ScrubbedBytes, Word8) -> [ScrubbedBytes]
loop (ScrubbedBytes
forall a. ByteArray a => a
empty, Word8
1)
  where
    hmac :: key -> message -> ScrubbedBytes
hmac key
key message
info = HMAC Blake2b_512 -> ScrubbedBytes
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
convert (key -> message -> HMAC Blake2b_512
forall key message a.
(ByteArrayAccess key, ByteArrayAccess message, HashAlgorithm a) =>
key -> message -> HMAC a
M.hmac key
key message
info :: M.HMAC H.Blake2b_512) :: ScrubbedBytes
    tempKey :: ScrubbedBytes
tempKey = ScrubbedBytes -> ScrubbedBytes -> ScrubbedBytes
forall {key} {message}.
(ByteArrayAccess key, ByteArrayAccess message) =>
key -> message -> ScrubbedBytes
hmac ScrubbedBytes
ck ScrubbedBytes
keyMat
    loop :: (ScrubbedBytes, Word8) -> [ScrubbedBytes]
loop = ((ScrubbedBytes, Word8)
 -> Maybe (ScrubbedBytes, (ScrubbedBytes, Word8)))
-> (ScrubbedBytes, Word8) -> [ScrubbedBytes]
forall b a. (b -> Maybe (a, b)) -> b -> [a]
unfoldr (((ScrubbedBytes, Word8)
  -> Maybe (ScrubbedBytes, (ScrubbedBytes, Word8)))
 -> (ScrubbedBytes, Word8) -> [ScrubbedBytes])
-> ((ScrubbedBytes, Word8)
    -> Maybe (ScrubbedBytes, (ScrubbedBytes, Word8)))
-> (ScrubbedBytes, Word8)
-> [ScrubbedBytes]
forall a b. (a -> b) -> a -> b
$ \(ScrubbedBytes
c, Word8
i) -> let r :: ScrubbedBytes
r = ScrubbedBytes -> ScrubbedBytes -> ScrubbedBytes
forall {key} {message}.
(ByteArrayAccess key, ByteArrayAccess message) =>
key -> message -> ScrubbedBytes
hmac ScrubbedBytes
tempKey (ScrubbedBytes
c ScrubbedBytes -> Word8 -> ScrubbedBytes
forall a. ByteArray a => a -> Word8 -> a
`snoc` Word8
i) in
      if Word8
i Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0
        then Maybe (ScrubbedBytes, (ScrubbedBytes, Word8))
forall a. Maybe a
Nothing
        else if Word8
i Word8 -> Word8 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word8
numOutputs
          then (ScrubbedBytes, (ScrubbedBytes, Word8))
-> Maybe (ScrubbedBytes, (ScrubbedBytes, Word8))
forall a. a -> Maybe a
Just (ScrubbedBytes
r, (ScrubbedBytes
r, Word8
i Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
+ Word8
1))
          else Maybe (ScrubbedBytes, (ScrubbedBytes, Word8))
forall a. Maybe a
Nothing

bytesToCK :: ScrubbedBytes
          -> ChainingKey BLAKE2b
bytesToCK :: ScrubbedBytes -> ChainingKey BLAKE2b
bytesToCK = ScrubbedBytes -> ChainingKey BLAKE2b
HCKB2b

ckToBytes :: ChainingKey BLAKE2b
          -> ScrubbedBytes
ckToBytes :: ChainingKey BLAKE2b -> ScrubbedBytes
ckToBytes (HCKB2b ScrubbedBytes
ck) = ScrubbedBytes
ck

toBytes :: Digest BLAKE2b
        -> ScrubbedBytes
toBytes :: Digest BLAKE2b -> ScrubbedBytes
toBytes (HDB2b Digest Blake2b_512
d) = Digest Blake2b_512 -> ScrubbedBytes
forall bin bout.
(ByteArrayAccess bin, ByteArray bout) =>
bin -> bout
convert Digest Blake2b_512
d