There is no description of the protocol or of its security goals, so I am making some guesses based on a cursory look at the source and what I imagine this might be for.<p>A single symmetric key is derived for both directions, and there is no checking of nonces, so as far as I can tell any message can be dropped, reordered, or replayed in both directions. (Including replaying message from A to B as if they were from B to A.) This is a bit like using ECB and likely to lead to fun application-specific attacks like [0].<p>This is very much rolling your own crypto, in a dangerous way. I am on the record as being "against" the "don't roll your own crypto" refrain [1], but mostly because it doesn't work: it should discourage people from publishing hand-rolled protocols such as this, but instead people think it means "don't roll your own primitives" and accept any use of "Ed25519/X25519" as probably secure.<p>Please read about the Noise framework [2] to get an idea of how much nuance there is to this, and consider using a Go implementation of it [3] instead.<p>P.S. This kind of issue is also why I maintain that NaCl is not a high-level scheme [4]: this could have used NaCl and have the exact same issues. libsodium has a couple slightly higher-level APIs that could have helped, secretstream [5] and kx [6], but again please use Noise.<p>[0] <a href="https://cryptopals.com/sets/2/challenges/13" rel="nofollow">https://cryptopals.com/sets/2/challenges/13</a><p>[1] <a href="https://securitycryptographywhatever.buzzsprout.com/1822302/8953842-the-great-roll-your-own-crypto-debate-with-filippo-valsorda" rel="nofollow">https://securitycryptographywhatever.buzzsprout.com/1822302/...</a><p>[2] <a href="https://noiseprotocol.org/noise.html" rel="nofollow">https://noiseprotocol.org/noise.html</a><p>[3] <a href="https://github.com/flynn/noise">https://github.com/flynn/noise</a><p>[4] <a href="https://words.filippo.io/dispatches/nacl-api/" rel="nofollow">https://words.filippo.io/dispatches/nacl-api/</a><p>[5] <a href="https://libsodium.gitbook.io/doc/secret-key_cryptography/secretstream" rel="nofollow">https://libsodium.gitbook.io/doc/secret-key_cryptography/sec...</a><p>[6] <a href="https://libsodium.gitbook.io/doc/key_exchange" rel="nofollow">https://libsodium.gitbook.io/doc/key_exchange</a>
There has been a lot of discussion about how the crypto doesn't work. I have some quibbles about the Go.<p>Don't put a mutex in your io.Reader. It is assumed that, unless mentioned in the documentation, it is not safe to use anything in Go from multiple goroutines. If someone wants to make the reader synchronous for use among multiple goroutines, they can do so themselves. But it's so rare that it's unlikely anyone would want this.<p>read/write mutexes typically perform worse than a plain mutex. You pay a performance cost to prevent readers and writers from starving each other; if you don't care (and this code doesn't), use a Mutex. <a href="https://zephyrtronium.github.io/articles/rwmutex.html" rel="nofollow">https://zephyrtronium.github.io/articles/rwmutex.html</a><p>Finally, it's fine to embed the mutex value directly in your struct if your methods take pointer receivers. &MyThing{Mutex: new(sync.Mutex)} is pretty weird to see. If the mutex were included as its value, then the zero value of MyThing is ready to use; &MyThing{} has a new mutex in it. (You can't copy the value of &MyThing either way; a correct copy requires holding the mutex. The reason to embed a *sync.Mutex instead of a sync.Mutex is so that you can copy the containing type, but that is actually unsafe. You grab a pointer to the wrong Mutex in your copy, and you also copy data protected by the mutex without holding it. So your methods MUST have a pointer receiver either way, and thus the indirection to *sync.Mutex is unnecessary.)
Interesting, though AFAIK a secure tunnel is only useful for a net.Conn, not an io.ReadWriter. What's the usecase for a "secure tunnel" over a bytes.Buffer?<p>btw, I noticed that the decrypt function reads a 32-bit message length and immediately allocates a slice of that size. That means an attacker can send 0xFFFFFFFF and cause you to allocate 4GiB.
I recently implemented a very similar service to service gRPC authentication mechanism for Go using EC25519 and the noise protocol framework.<p><a href="https://github.com/sillystack/api/blob/main/transport/transport.go">https://github.com/sillystack/api/blob/main/transport/transp...</a><p>The code is very fresh and hasn't yet gone through a audit so please don't use it for anything where security actually matters.