diff options
| author | Roman Smrž <roman.smrz@seznam.cz> | 2026-11-08 15:08:02 +0100 |
|---|---|---|
| committer | Roman Smrž <roman.smrz@seznam.cz> | 2026-11-08 20:41:24 +0100 |
| commit | aefa4b4dc76c3feaab223ff7afe1293c7434fae0 (patch) | |
| tree | 65106a4d4c3443e7a057082f8cb768c2400d3be4 /src/Erebos | |
| parent | 4a83167a5199ad078b14b0df7a2ac1ef0b4f58b6 (diff) | |
Use ByteString in nonce-related functions
Diffstat (limited to 'src/Erebos')
| -rw-r--r-- | src/Erebos/Network/Channel.hs | 18 | ||||
| -rw-r--r-- | src/Erebos/Network/Protocol.hs | 6 |
2 files changed, 12 insertions, 12 deletions
diff --git a/src/Erebos/Network/Channel.hs b/src/Erebos/Network/Channel.hs index e8347ad..5b8d65e 100644 --- a/src/Erebos/Network/Channel.hs +++ b/src/Erebos/Network/Channel.hs @@ -17,7 +17,7 @@ import Control.Monad.Except import Control.Monad.IO.Class import Data.Binary -import Data.ByteArray (Bytes, ScrubbedBytes, convert) +import Data.ByteArray (ScrubbedBytes, convert) import Data.ByteArray qualified as BA import Data.ByteString (ByteString) import Data.ByteString qualified as BS @@ -32,8 +32,8 @@ import Erebos.Storable data Channel = Channel { chPeers :: [Stored (Signed IdentityData)] , chKey :: ScrubbedBytes - , chNonceFixedOur :: Bytes - , chNonceFixedPeer :: Bytes + , chNonceFixedOur :: ByteString + , chNonceFixedPeer :: ByteString , chCounterNextOut :: MVar Word64 , chCounterNextIn :: MVar Word64 } @@ -103,8 +103,8 @@ acceptChannelRequest self peer req = do let chPeers = crPeers $ fromStored $ signedData $ fromStored req chKey = BA.take keySize $ dhSecret xsecret $ fromStored $ crKey $ fromStored $ signedData $ fromStored req - chNonceFixedOur = BA.pack [ 2, 0, 0, 0 ] - chNonceFixedPeer = BA.pack [ 1, 0, 0, 0 ] + chNonceFixedOur = BS.pack [ 2, 0, 0, 0 ] + chNonceFixedPeer = BS.pack [ 1, 0, 0, 0 ] chCounterNextOut <- newMVar 0 chCounterNextIn <- newMVar 0 @@ -129,8 +129,8 @@ acceptedChannel self peer acc = do let chPeers = crPeers $ fromStored $ signedData $ fromStored req chKey = BA.take keySize $ dhSecret xsecret $ fromStored $ caKey $ fromStored $ signedData $ fromStored acc - chNonceFixedOur = BA.pack [ 1, 0, 0, 0 ] - chNonceFixedPeer = BA.pack [ 2, 0, 0, 0 ] + chNonceFixedOur = BS.pack [ 1, 0, 0, 0 ] + chNonceFixedPeer = BS.pack [ 2, 0, 0, 0 ] chCounterNextOut <- liftIO $ newMVar 0 chCounterNextIn <- liftIO $ newMVar 0 @@ -141,7 +141,7 @@ channelEncrypt :: (MonadIO m, MonadError e m, FromErebosError e) => Channel -> B channelEncrypt Channel {..} plain = do count <- liftIO $ modifyMVar chCounterNextOut $ \c -> return ( c + 1, c ) let cbytes = BL.toStrict $ encode count - nonce = nonce8 chNonceFixedOur (convert cbytes) + nonce = nonce8 chNonceFixedOur cbytes state <- case aeadInitialize chKey =<< nonce of CryptoPassed state -> return state CryptoFailed err -> throwOtherError $ "failed to init chacha-poly1305 cipher: " <> show err @@ -159,7 +159,7 @@ channelDecrypt Channel {..} body = do let countByte = body `BS.index` 0 body' = BS.drop 1 body guessedCount = expectedCount - 128 + fromIntegral (countByte - fromIntegral expectedCount + 128 :: Word8) - nonce = nonce8 chNonceFixedPeer $ convert $ BL.toStrict $ encode guessedCount + nonce = nonce8 chNonceFixedPeer $ BL.toStrict $ encode guessedCount blen = BS.length body' - 16 ctext = BS.take blen body' tag = BS.drop blen body' diff --git a/src/Erebos/Network/Protocol.hs b/src/Erebos/Network/Protocol.hs index 87272a2..92ec93a 100644 --- a/src/Erebos/Network/Protocol.hs +++ b/src/Erebos/Network/Protocol.hs @@ -49,7 +49,7 @@ import Data.Binary import Data.Binary.Get import Data.Binary.Put import Data.Bits -import Data.ByteArray (Bytes, ScrubbedBytes) +import Data.ByteArray (ScrubbedBytes) import Data.ByteArray qualified as BA import Data.ByteString (ByteString) import Data.ByteString qualified as B @@ -145,7 +145,7 @@ instance Binary ParsedCookie where putByteString cookieContent get = do - Just cookieNonce <- maybeCryptoError . C.nonce12 . BA.convert <$> getByteString 12 + Just cookieNonce <- maybeCryptoError . C.nonce12 <$> getByteString 12 cookieValidity <- getWord32be Just cookieMac <- maybeCryptoError . C.authTag <$> getByteString 16 cookieContent <- BL.toStrict <$> getRemainingLazyByteString @@ -800,7 +800,7 @@ generateCookieHeaders Connection {..} ch = catMaybes <$> sequence [ echoHeader, createCookie :: GlobalState addr -> addr -> IO Cookie createCookie GlobalState {..} addr = do - (nonceBytes :: Bytes) <- getRandomBytes 12 + nonceBytes <- getRandomBytes 12 validUntil <- (fromNanoSecs (60 * 10^(9 :: Int)) +) <$> getTime Monotonic let validSecondsFromStart = fromIntegral $ toNanoSecs (validUntil - gStartTime) `div` (10^(9 :: Int)) cookieValidity = validSecondsFromStart - gCookieStartTime |