module Crypto.Noise.Internal.CipherState
(
CipherState(CipherState),
csk,
csn,
encryptAndIncrement,
decryptAndIncrement
) where
import Control.Exception (throw)
import Control.Lens
import Data.Maybe (fromMaybe)
import Crypto.Noise.Cipher
import Crypto.Noise.Types (NoiseException(DecryptionFailure))
data CipherState c =
CipherState { _csk :: SymmetricKey c
, _csn :: Nonce c
}
$(makeLenses ''CipherState)
encryptAndIncrement :: Cipher c => AssocData -> Plaintext -> CipherState c -> (Ciphertext c, CipherState c)
encryptAndIncrement ad plaintext cs = (ct, newState)
where
ct = cipherEncrypt (cs ^. csk) (cs ^. csn) ad plaintext
newState = cs & csn %~ cipherIncNonce
decryptAndIncrement :: Cipher c => AssocData -> Ciphertext c -> CipherState c -> (Plaintext, CipherState c)
decryptAndIncrement ad ct cs = (pt, newState)
where
pt = fromMaybe (throw (DecryptionFailure "decryptAndIncrement"))
(cipherDecrypt (cs ^. csk) (cs ^. csn) ad ct)
newState = cs & csn %~ cipherIncNonce