diff options
author | joe <joe@jerkface.net> | 2017-11-19 18:09:57 -0500 |
---|---|---|
committer | James Crayne <jim.crayne@gmail.com> | 2017-11-19 23:40:18 +0000 |
commit | 43957dc798365d5066bcf25ae5ae9400701ee6dd (patch) | |
tree | e35b293571db637ba79a5d3b7bcec17f4e85f6d6 /src/Network/Tox | |
parent | 8d9abc1df036a8184bc2fd88ddf6f1d621e7e4c1 (diff) |
Note and TODO regarding crypto packet padding.
Diffstat (limited to 'src/Network/Tox')
-rw-r--r-- | src/Network/Tox/Crypto/Transport.hs | 30 |
1 files changed, 29 insertions, 1 deletions
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 | |||
154 | -- | [ uint32_t packet number if lossless | 154 | -- | [ uint32_t packet number if lossless |
155 | -- , sendbuffer buffer_end if lossy , (big endian)] | 155 | -- , sendbuffer buffer_end if lossy , (big endian)] |
156 | , bufferEnd :: Word32 | 156 | , bufferEnd :: Word32 |
157 | -- | [data] | 157 | -- | [data] (TODO See Note [Padding]) |
158 | , bufferData :: CryptoMessage | 158 | , bufferData :: CryptoMessage |
159 | } | 159 | } |
160 | 160 | ||
161 | {- | ||
162 | Note [Padding] | ||
163 | |||
164 | TODO: The 'bufferData' field of 'CryptoData' should probably be something like | ||
165 | /Padded CryptoMessage/ because c-toxcore strips leading zeros on incoming and | ||
166 | pads leading zeros on outgoing packets. | ||
167 | |||
168 | After studying c-toxcore (at commit c49a6e7f5bc245a51a3c85cc2c8b7f881c412998), | ||
169 | I've determined the following behavior. | ||
170 | |||
171 | Incoming: All leading zero bytes are stripped until possibly the whole packet | ||
172 | is consumed (in which case it is discarded). This happens at | ||
173 | toxcore/net_crypto.c:1366:handle_data_packet_core(). | ||
174 | |||
175 | Outgoing: The number of zeros added is: | ||
176 | |||
177 | padding_length len = (1373 - len) `mod` 8 where | ||
178 | |||
179 | where /len/ is the size of the non-padded CryptoMessage. This happens at | ||
180 | toxcore/net_crypto.c:936:send_data_packet_helper() | ||
181 | |||
182 | The number 1373 is written in C as MAX_CRYPTO_DATA_SIZE which is defined in | ||
183 | terms of the max /NetCrypto/ packet size (1400) minus the minimum possible size | ||
184 | of an id-byte (1) and a /CryptoPacket Encrypted/ ( 2 + 4 + 4 + 16 ). | ||
185 | |||
186 | One effect of this is that short messages will be padded to at least 5 bytes. | ||
187 | -} | ||
188 | |||
161 | instance Serialize CryptoData where | 189 | instance Serialize CryptoData where |
162 | get = CryptoData <$> get <*> get <*> get | 190 | get = CryptoData <$> get <*> get <*> get |
163 | put (CryptoData start end dta) = put start >> put end >> put dta | 191 | put (CryptoData start end dta) = put start >> put end >> put dta |