From 43957dc798365d5066bcf25ae5ae9400701ee6dd Mon Sep 17 00:00:00 2001 From: joe Date: Sun, 19 Nov 2017 18:09:57 -0500 Subject: Note and TODO regarding crypto packet padding. --- src/Network/Tox/Crypto/Transport.hs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) (limited to 'src/Network/Tox') diff --git a/src/Network/Tox/Crypto/Transport.hs b/src/Network/Tox/Crypto/Transport.hs index b89bde46..3133ee9b 100644 --- a/src/Network/Tox/Crypto/Transport.hs +++ b/src/Network/Tox/Crypto/Transport.hs @@ -154,10 +154,38 @@ data CryptoData = CryptoData -- | [ uint32_t packet number if lossless -- , sendbuffer buffer_end if lossy , (big endian)] , bufferEnd :: Word32 - -- | [data] + -- | [data] (TODO See Note [Padding]) , bufferData :: CryptoMessage } +{- +Note [Padding] + +TODO: The 'bufferData' field of 'CryptoData' should probably be something like +/Padded CryptoMessage/ because c-toxcore strips leading zeros on incoming and +pads leading zeros on outgoing packets. + +After studying c-toxcore (at commit c49a6e7f5bc245a51a3c85cc2c8b7f881c412998), +I've determined the following behavior. + +Incoming: All leading zero bytes are stripped until possibly the whole packet +is consumed (in which case it is discarded). This happens at +toxcore/net_crypto.c:1366:handle_data_packet_core(). + +Outgoing: The number of zeros added is: + + padding_length len = (1373 - len) `mod` 8 where + +where /len/ is the size of the non-padded CryptoMessage. This happens at +toxcore/net_crypto.c:936:send_data_packet_helper() + +The number 1373 is written in C as MAX_CRYPTO_DATA_SIZE which is defined in +terms of the max /NetCrypto/ packet size (1400) minus the minimum possible size +of an id-byte (1) and a /CryptoPacket Encrypted/ ( 2 + 4 + 4 + 16 ). + +One effect of this is that short messages will be padded to at least 5 bytes. +-} + instance Serialize CryptoData where get = CryptoData <$> get <*> get <*> get put (CryptoData start end dta) = put start >> put end >> put dta -- cgit v1.2.3