summaryrefslogtreecommitdiff
path: root/src/Network
diff options
context:
space:
mode:
authorDebian Live user <user@localhost.localdomain>2017-10-29 21:19:24 +0000
committerDebian Live user <user@localhost.localdomain>2017-10-29 21:19:24 +0000
commit41dd1e80778bb2f65d28569d859dd85255712876 (patch)
tree95d043bb16f70a21b0ebc1f01303eec3494e8440 /src/Network
parent85b6fc2a851d1321ca8d21d50ab88bd2c95cec6a (diff)
parseNetCrypto,encodeNetCrypto
Diffstat (limited to 'src/Network')
-rw-r--r--src/Network/Tox/Crypto/Transport.hs29
1 files changed, 6 insertions, 23 deletions
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 @@
2{-# LANGUAGE ViewPatterns #-} 2{-# LANGUAGE ViewPatterns #-}
3{-# LANGUAGE FlexibleInstances #-} 3{-# LANGUAGE FlexibleInstances #-}
4{-# LANGUAGE LambdaCase #-} 4{-# LANGUAGE LambdaCase #-}
5{-# LANGUAGE TupleSections #-}
5module Network.Tox.Crypto.Transport 6module Network.Tox.Crypto.Transport
6 ( parseNetCrypto 7 ( parseNetCrypto
7 , encodeNetCrypto 8 , encodeNetCrypto
@@ -50,6 +51,7 @@ import Control.Lens
50import Data.Text as T 51import Data.Text as T
51import Data.Text.Encoding as T 52import Data.Text.Encoding as T
52import Data.Serialize as S 53import Data.Serialize as S
54import Control.Arrow
53 55
54 56
55data NetCrypto 57data NetCrypto
@@ -57,10 +59,12 @@ data NetCrypto
57 | NetCrypto (CryptoPacket Encrypted) 59 | NetCrypto (CryptoPacket Encrypted)
58 60
59parseNetCrypto :: ByteString -> SockAddr -> Either String (NetCrypto, SockAddr) 61parseNetCrypto :: ByteString -> SockAddr -> Either String (NetCrypto, SockAddr)
60parseNetCrypto pkt saddr = Left "TODO: parseNetCrypto" 62parseNetCrypto pkt@(B.uncons -> Just (0x1a,_)) saddr = left ("parseNetCrypto: "++) $ (,saddr) . NetHandshake <$> runGet get pkt
63parseNetCrypto pkt@(B.uncons -> Just (0x1b,_)) saddr = left ("parseNetCrypto: "++) $ (,saddr) . NetCrypto <$> runGet get pkt
61 64
62encodeNetCrypto :: NetCrypto -> SockAddr -> (ByteString, SockAddr) 65encodeNetCrypto :: NetCrypto -> SockAddr -> (ByteString, SockAddr)
63encodeNetCrypto _ _ = _todo 66encodeNetCrypto (NetHandshake x) saddr = (B.cons 0x1a (runPut $ put x),saddr)
67encodeNetCrypto (NetCrypto x) saddr = (B.cons 0x1b (runPut $ put x),saddr)
64 68
65data Handshake (f :: * -> *) = Handshake 69data Handshake (f :: * -> *) = Handshake
66 { handshakeCookie :: Cookie 70 { handshakeCookie :: Cookie
@@ -940,16 +944,6 @@ data MessageName = Ping -- 0x00
940-- [uint64_t echo id (must be sent back untouched in cookie response)] 944-- [uint64_t echo id (must be sent back untouched in cookie response)]
941-- ] 945-- ]
942 946
943-- Handshake packet:
944-- [uint8_t 26] (0x1a)
945-- [Cookie]
946-- [nonce (24 bytes)]
947-- [Encrypted message containing:
948-- [24 bytes base nonce]
949-- [session public key of the peer (32 bytes)]
950-- [sha512 hash of the entire Cookie sitting outside the encrypted part]
951-- [Other Cookie (used by the other to respond to the handshake packet)]
952-- ]
953 947
954-- cookie response packet (161 bytes): 948-- cookie response packet (161 bytes):
955-- 949--
@@ -973,15 +967,4 @@ data MessageName = Ping -- 0x00
973-- should be (NodeId -> Secret) and the cookie-request map should be 967-- should be (NodeId -> Secret) and the cookie-request map should be
974-- (SockAddr -> NodeId) 968-- (SockAddr -> NodeId)
975 969
976-- Encrypted packets:
977--
978-- Length Contents
979-- :---------:--------------------------------------------------------------
980-- `1` `uint8_t` (0x1b)
981-- `2` `uint16_t` The last 2 bytes of the nonce used to encrypt this
982-- variable  Payload
983--
984-- The payload is encrypted with the session key and 'base nonce' set by the
985-- receiver in their handshake + packet number (starting at 0, big endian math).
986
987 970