module Crypto.Cipher.Types.AEAD where
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import Data.Byteable
import Crypto.Cipher.Types.Base
import Crypto.Cipher.Types.Block
aeadAppendHeader :: BlockCipher a => AEAD a -> ByteString -> AEAD a
(AEAD a
cipher (AEADState st
state)) ByteString
bs =
a -> AEADState a -> AEAD a
forall cipher. cipher -> AEADState cipher -> AEAD cipher
AEAD a
cipher (AEADState a -> AEAD a) -> AEADState a -> AEAD a
forall a b. (a -> b) -> a -> b
$ st -> AEADState a
forall cipher st. AEADModeImpl cipher st => st -> AEADState cipher
AEADState (a -> st -> ByteString -> st
forall cipher state.
AEADModeImpl cipher state =>
cipher -> state -> ByteString -> state
aeadStateAppendHeader a
cipher st
state ByteString
bs)
aeadEncrypt :: BlockCipher a => AEAD a -> ByteString -> (ByteString, AEAD a)
aeadEncrypt :: forall a.
BlockCipher a =>
AEAD a -> ByteString -> (ByteString, AEAD a)
aeadEncrypt (AEAD a
cipher (AEADState st
state)) ByteString
input = (ByteString
output, a -> AEADState a -> AEAD a
forall cipher. cipher -> AEADState cipher -> AEAD cipher
AEAD a
cipher (st -> AEADState a
forall cipher st. AEADModeImpl cipher st => st -> AEADState cipher
AEADState st
nst))
where (ByteString
output, st
nst) = a -> st -> ByteString -> (ByteString, st)
forall cipher state.
AEADModeImpl cipher state =>
cipher -> state -> ByteString -> (ByteString, state)
aeadStateEncrypt a
cipher st
state ByteString
input
aeadDecrypt :: BlockCipher a => AEAD a -> ByteString -> (ByteString, AEAD a)
aeadDecrypt :: forall a.
BlockCipher a =>
AEAD a -> ByteString -> (ByteString, AEAD a)
aeadDecrypt (AEAD a
cipher (AEADState st
state)) ByteString
input = (ByteString
output, a -> AEADState a -> AEAD a
forall cipher. cipher -> AEADState cipher -> AEAD cipher
AEAD a
cipher (st -> AEADState a
forall cipher st. AEADModeImpl cipher st => st -> AEADState cipher
AEADState st
nst))
where (ByteString
output, st
nst) = a -> st -> ByteString -> (ByteString, st)
forall cipher state.
AEADModeImpl cipher state =>
cipher -> state -> ByteString -> (ByteString, state)
aeadStateDecrypt a
cipher st
state ByteString
input
aeadFinalize :: BlockCipher a => AEAD a -> Int -> AuthTag
aeadFinalize :: forall a. BlockCipher a => AEAD a -> Int -> AuthTag
aeadFinalize (AEAD a
cipher (AEADState st
state)) Int
len =
a -> st -> Int -> AuthTag
forall cipher state.
AEADModeImpl cipher state =>
cipher -> state -> Int -> AuthTag
aeadStateFinalize a
cipher st
state Int
len
aeadSimpleEncrypt :: BlockCipher a
=> AEAD a
-> B.ByteString
-> B.ByteString
-> Int
-> (AuthTag, B.ByteString)
aeadSimpleEncrypt :: forall a.
BlockCipher a =>
AEAD a -> ByteString -> ByteString -> Int -> (AuthTag, ByteString)
aeadSimpleEncrypt AEAD a
aeadIni ByteString
header ByteString
input Int
taglen = (AuthTag
tag, ByteString
output)
where aead :: AEAD a
aead = AEAD a -> ByteString -> AEAD a
forall a. BlockCipher a => AEAD a -> ByteString -> AEAD a
aeadAppendHeader AEAD a
aeadIni ByteString
header
(ByteString
output, AEAD a
aeadFinal) = AEAD a -> ByteString -> (ByteString, AEAD a)
forall a.
BlockCipher a =>
AEAD a -> ByteString -> (ByteString, AEAD a)
aeadEncrypt AEAD a
aead ByteString
input
tag :: AuthTag
tag = AEAD a -> Int -> AuthTag
forall a. BlockCipher a => AEAD a -> Int -> AuthTag
aeadFinalize AEAD a
aeadFinal Int
taglen
aeadSimpleDecrypt :: BlockCipher a
=> AEAD a
-> B.ByteString
-> B.ByteString
-> AuthTag
-> Maybe B.ByteString
aeadSimpleDecrypt :: forall a.
BlockCipher a =>
AEAD a -> ByteString -> ByteString -> AuthTag -> Maybe ByteString
aeadSimpleDecrypt AEAD a
aeadIni ByteString
header ByteString
input AuthTag
authTag
| AuthTag
tag AuthTag -> AuthTag -> Bool
forall a. Eq a => a -> a -> Bool
== AuthTag
authTag = ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just ByteString
output
| Bool
otherwise = Maybe ByteString
forall a. Maybe a
Nothing
where aead :: AEAD a
aead = AEAD a -> ByteString -> AEAD a
forall a. BlockCipher a => AEAD a -> ByteString -> AEAD a
aeadAppendHeader AEAD a
aeadIni ByteString
header
(ByteString
output, AEAD a
aeadFinal) = AEAD a -> ByteString -> (ByteString, AEAD a)
forall a.
BlockCipher a =>
AEAD a -> ByteString -> (ByteString, AEAD a)
aeadDecrypt AEAD a
aead ByteString
input
tag :: AuthTag
tag = AEAD a -> Int -> AuthTag
forall a. BlockCipher a => AEAD a -> Int -> AuthTag
aeadFinalize AEAD a
aeadFinal (AuthTag -> Int
forall a. Byteable a => a -> Int
byteableLength AuthTag
authTag)