From 2a3d9e0c3aa75eb129c552926c5afb3b436a0e82 Mon Sep 17 00:00:00 2001 From: Avril Date: Mon, 16 Aug 2021 22:50:36 +0100 Subject: [PATCH] For some reaosn, any difference between exact bytes written and read (even if they are identical, just spread over different `write` calls) causes the reading half to produce garbage? MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For ESock to work, a `write(n)` needs to be paired with a `read(n)` for identical sizes of `n`. Not using `write_all(n)`+`read_exact(n)` causes this disjoint too? (Test with `write_all(n)`+`while read(...)` produced garbage.) I have no idea what is going on here. I think there"s some disconnect with the cipher, but it"s a stream cipher, so positioning (i.e. where byte pattern `A` appears in slice `B` on the writing side, and slice `C` on the reading size) shouldn"t matter right? Clearly it seems to. This is extremely disappointing. Fortune for rsh's current commit: Future small blessing − 末小吉 --- src/sock/enc.rs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/sock/enc.rs b/src/sock/enc.rs index 8e056ab..07ccc4d 100644 --- a/src/sock/enc.rs +++ b/src/sock/enc.rs @@ -461,7 +461,8 @@ impl ESock } } -//XXXXXXX: This isn't working. The first write to the socket succeeds. Any subsequent writes/reads produce garbage. Why? +//XXX: For some reason, non-exact reads + writes cause garbage to be produced on the receiving end? +// Is this fixable? Why does it disjoint? I have no idea... This is supposed to be a stream cipher, right? Why does positioning matter? Have I misunderstood how it workd? Eh... impl AsyncWrite for ESock where W: AsyncWrite @@ -717,7 +718,7 @@ mod tests // The duplex buffer size here is smaller than an RSA ciphertext block. So, writing the session key must be buffered with a buffer size this small (should return Pending at least once.) // Using a low buffer size to make sure the test passes even when the entire buffer cannot be written at once. - let (mut tx, mut rx) = gen_duplex_esock(256).wrap_err(eyre!("Failed to weave socks"))?; + let (mut tx, mut rx) = gen_duplex_esock(16).wrap_err(eyre!("Failed to weave socks"))?; let writer = tokio::spawn(async move { use tokio::prelude::*; @@ -728,9 +729,8 @@ mod tests tx.set_encrypted_write(true).await?; assert_eq!((true, false), tx.is_encrypted()); - tx.write_all(&VALUE[0..2]).await?; - tx.write_all(&VALUE[2..6]).await?; - tx.write_all(&VALUE[6..]).await?; + tx.write_all(VALUE).await?; + tx.write_all(VALUE).await?; // Check resp tx.set_encrypted_read(true).await?; @@ -757,6 +757,11 @@ mod tests let mut val = vec![0u8; VALUE.len()]; rx.read_exact(&mut val[..]).await?; + + let mut val2 = vec![0u8; VALUE.len()]; + rx.read_exact(&mut val2[..]).await?; + + assert_eq!(val, val2); // Send resp rx.set_encrypted_write(true).await?;