diff options
| author | Roman Smrž <roman.smrz@seznam.cz> | 2026-10-18 10:13:30 +0200 |
|---|---|---|
| committer | Roman Smrž <roman.smrz@seznam.cz> | 2026-10-23 23:26:21 +0200 |
| commit | e45b366ca8b9caa4fe0087e5c4fb1720472d9c23 (patch) | |
| tree | 284f7cca2d3d58482ea459bd3b80c585a0fa7c72 /src/Erebos/Network/Channel.hs | |
| parent | 552cb773ce9d8472530e123e926493b3dd1e658f (diff) | |
Diffstat (limited to 'src/Erebos/Network/Channel.hs')
| -rw-r--r-- | src/Erebos/Network/Channel.hs | 18 |
1 files changed, 9 insertions, 9 deletions
diff --git a/src/Erebos/Network/Channel.hs b/src/Erebos/Network/Channel.hs index d9679bd..e53f8b3 100644 --- a/src/Erebos/Network/Channel.hs +++ b/src/Erebos/Network/Channel.hs @@ -16,15 +16,13 @@ import Control.Monad import Control.Monad.Except import Control.Monad.IO.Class -import Crypto.Cipher.ChaChaPoly1305 -import Crypto.Error - import Data.Binary import Data.ByteArray (ByteArray, Bytes, ScrubbedBytes, convert) import Data.ByteArray qualified as BA import Data.ByteString.Lazy qualified as BL import Data.List +import Crypto import Erebos.Identity import Erebos.PubKey import Erebos.Storable @@ -142,12 +140,12 @@ channelEncrypt Channel {..} plain = do count <- liftIO $ modifyMVar chCounterNextOut $ \c -> return (c + 1, c) let cbytes = convert $ BL.toStrict $ encode count nonce = nonce8 chNonceFixedOur cbytes - state <- case initialize chKey =<< nonce of + state <- case aeadInitialize chKey =<< nonce of CryptoPassed state -> return state CryptoFailed err -> throwOtherError $ "failed to init chacha-poly1305 cipher: " <> show err let (ctext, state') = encrypt plain state - tag = finalize state' + tag = aeadFinalize state' return (BA.concat [ convert $ BA.drop 7 cbytes, ctext, convert tag ], count) channelDecrypt :: (ByteArray ba, MonadIO m, MonadError e m, FromErebosError e) => Channel -> ba -> m (ba, Word64) @@ -163,13 +161,15 @@ channelDecrypt Channel {..} body = do blen = BA.length body' - 16 ctext = BA.takeView body' blen tag = BA.dropView body' blen - state <- case initialize chKey =<< nonce of + state <- case aeadInitialize chKey =<< nonce of CryptoPassed state -> return state CryptoFailed err -> throwOtherError $ "failed to init chacha-poly1305 cipher: " <> show err - let (plain, state') = decrypt (convert ctext) state - when (not $ tag `BA.constEq` finalize state') $ do - throwOtherError $ "tag validation falied" + auth <- case authTag tag of + CryptoPassed auth -> return auth + CryptoFailed err -> throwOtherError $ "failed to decode auth tag: " <> show err + plain <- maybe (throwOtherError "tag validation failed") return $ + decrypt ( convert ctext, auth ) state liftIO $ modifyMVar_ chCounterNextIn $ return . max (guessedCount + 1) return (plain, guessedCount) |