From 41dd1e80778bb2f65d28569d859dd85255712876 Mon Sep 17 00:00:00 2001 From: Debian Live user Date: Sun, 29 Oct 2017 21:19:24 +0000 Subject: parseNetCrypto,encodeNetCrypto --- src/Network/Tox/Crypto/Transport.hs | 29 ++++++----------------------- 1 file changed, 6 insertions(+), 23 deletions(-) (limited to 'src/Network/Tox/Crypto') diff --git a/src/Network/Tox/Crypto/Transport.hs b/src/Network/Tox/Crypto/Transport.hs index 4cf6c7a5..09c492ef 100644 --- a/src/Network/Tox/Crypto/Transport.hs +++ b/src/Network/Tox/Crypto/Transport.hs @@ -2,6 +2,7 @@ {-# LANGUAGE ViewPatterns #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE LambdaCase #-} +{-# LANGUAGE TupleSections #-} module Network.Tox.Crypto.Transport ( parseNetCrypto , encodeNetCrypto @@ -50,6 +51,7 @@ import Control.Lens import Data.Text as T import Data.Text.Encoding as T import Data.Serialize as S +import Control.Arrow data NetCrypto @@ -57,10 +59,12 @@ data NetCrypto | NetCrypto (CryptoPacket Encrypted) parseNetCrypto :: ByteString -> SockAddr -> Either String (NetCrypto, SockAddr) -parseNetCrypto pkt saddr = Left "TODO: parseNetCrypto" +parseNetCrypto pkt@(B.uncons -> Just (0x1a,_)) saddr = left ("parseNetCrypto: "++) $ (,saddr) . NetHandshake <$> runGet get pkt +parseNetCrypto pkt@(B.uncons -> Just (0x1b,_)) saddr = left ("parseNetCrypto: "++) $ (,saddr) . NetCrypto <$> runGet get pkt encodeNetCrypto :: NetCrypto -> SockAddr -> (ByteString, SockAddr) -encodeNetCrypto _ _ = _todo +encodeNetCrypto (NetHandshake x) saddr = (B.cons 0x1a (runPut $ put x),saddr) +encodeNetCrypto (NetCrypto x) saddr = (B.cons 0x1b (runPut $ put x),saddr) data Handshake (f :: * -> *) = Handshake { handshakeCookie :: Cookie @@ -940,16 +944,6 @@ data MessageName = Ping -- 0x00 -- [uint64_t echo id (must be sent back untouched in cookie response)] -- ] --- Handshake packet: --- [uint8_t 26] (0x1a) --- [Cookie] --- [nonce (24 bytes)] --- [Encrypted message containing: --- [24 bytes base nonce] --- [session public key of the peer (32 bytes)] --- [sha512 hash of the entire Cookie sitting outside the encrypted part] --- [Other Cookie (used by the other to respond to the handshake packet)] --- ] -- cookie response packet (161 bytes): -- @@ -973,15 +967,4 @@ data MessageName = Ping -- 0x00 -- should be (NodeId -> Secret) and the cookie-request map should be -- (SockAddr -> NodeId) --- Encrypted packets: --- --- Length Contents --- :---------:-------------------------------------------------------------- --- `1` `uint8_t` (0x1b) --- `2` `uint16_t` The last 2 bytes of the nonce used to encrypt this --- variable  Payload --- --- The payload is encrypted with the session key and 'base nonce' set by the --- receiver in their handshake + packet number (starting at 0, big endian math). - -- cgit v1.2.3