{-# LANGUAGE TypeFamilies #-}
module Crypto.Noise.Cipher
(
Cipher(..)
, AssocData
, Plaintext
) where
import Data.ByteArray (ScrubbedBytes, replicate)
import Prelude hiding (replicate)
class Cipher c where
data Ciphertext c :: *
data SymmetricKey c :: *
data Nonce c :: *
cipherName :: proxy c -> ScrubbedBytes
cipherEncrypt :: SymmetricKey c
-> Nonce c
-> AssocData
-> Plaintext
-> Ciphertext c
cipherDecrypt :: SymmetricKey c
-> Nonce c
-> AssocData
-> Ciphertext c
-> Maybe Plaintext
cipherRekey :: SymmetricKey c
-> SymmetricKey c
cipherRekey SymmetricKey c
k = AssocData -> SymmetricKey c
forall c. Cipher c => AssocData -> SymmetricKey c
cipherBytesToSym (AssocData -> SymmetricKey c)
-> (Ciphertext c -> AssocData) -> Ciphertext c -> SymmetricKey c
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
Ciphertext c -> AssocData
forall c. Cipher c => Ciphertext c -> AssocData
cipherTextToBytes (Ciphertext c -> SymmetricKey c) -> Ciphertext c -> SymmetricKey c
forall a b. (a -> b) -> a -> b
$
SymmetricKey c -> Nonce c -> AssocData -> AssocData -> Ciphertext c
forall c.
Cipher c =>
SymmetricKey c -> Nonce c -> AssocData -> AssocData -> Ciphertext c
cipherEncrypt SymmetricKey c
k Nonce c
forall c. Cipher c => Nonce c
cipherMaxNonce AssocData
forall a. Monoid a => a
mempty (Int -> Word8 -> AssocData
forall ba. ByteArray ba => Int -> Word8 -> ba
replicate Int
32 Word8
0)
cipherZeroNonce :: Nonce c
cipherMaxNonce :: Nonce c
cipherIncNonce :: Nonce c -> Nonce c
cipherNonceEq :: Nonce c -> Nonce c -> Bool
cipherNonceCmp :: Nonce c -> Nonce c -> Ordering
cipherBytesToSym :: ScrubbedBytes -> SymmetricKey c
cipherSymToBytes :: SymmetricKey c -> ScrubbedBytes
cipherTextToBytes :: Ciphertext c -> ScrubbedBytes
cipherBytesToText :: ScrubbedBytes -> Ciphertext c
type AssocData = ScrubbedBytes
type Plaintext = ScrubbedBytes
instance Cipher c => Eq (Nonce c) where
== :: Nonce c -> Nonce c -> Bool
(==) = Nonce c -> Nonce c -> Bool
forall c. Cipher c => Nonce c -> Nonce c -> Bool
cipherNonceEq
instance Cipher c => Ord (Nonce c) where
compare :: Nonce c -> Nonce c -> Ordering
compare = Nonce c -> Nonce c -> Ordering
forall c. Cipher c => Nonce c -> Nonce c -> Ordering
cipherNonceCmp
instance Show (SymmetricKey a) where
show :: SymmetricKey a -> String
show SymmetricKey a
_ = String
"<symmetric key>"
instance Show (Nonce a) where
show :: Nonce a -> String
show Nonce a
_ = String
"<nonce>"