249 lines
7.8 KiB
Rust
249 lines
7.8 KiB
Rust
|
|
//! PQ prelude at the wire level: the hybrid prologue layout, a tampered
|
||
|
|
//! ciphertext failing the handshake, and a classical opening to a friend that
|
||
|
|
//! has a KEM key being refused before any application frame.
|
||
|
|
|
||
|
|
use std::io;
|
||
|
|
use std::pin::Pin;
|
||
|
|
use std::task::{Context, Poll};
|
||
|
|
|
||
|
|
use futures::io::{AsyncRead, AsyncWrite};
|
||
|
|
use onionwire::frame;
|
||
|
|
use onionwire::kem;
|
||
|
|
use onionwire::session::{self, Keys, PRELUDE_REQUIRED, PeerAuth};
|
||
|
|
use tokio::io::{AsyncRead as TokioRead, AsyncWrite as TokioWrite};
|
||
|
|
|
||
|
|
/// `tokio::io::duplex` is the only in-memory two-way pipe available without a
|
||
|
|
/// new dependency; it speaks `tokio::io`, while the handshake is generic over
|
||
|
|
/// `futures::io`. One thin adapter per direction.
|
||
|
|
struct Duplex(tokio::io::DuplexStream);
|
||
|
|
|
||
|
|
impl AsyncRead for Duplex {
|
||
|
|
fn poll_read(
|
||
|
|
self: Pin<&mut Self>,
|
||
|
|
cx: &mut Context<'_>,
|
||
|
|
buf: &mut [u8],
|
||
|
|
) -> Poll<io::Result<usize>> {
|
||
|
|
let mut read_buf = tokio::io::ReadBuf::new(buf);
|
||
|
|
match Pin::new(&mut self.get_mut().0).poll_read(cx, &mut read_buf) {
|
||
|
|
Poll::Ready(Ok(())) => Poll::Ready(Ok(read_buf.filled().len())),
|
||
|
|
Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
|
||
|
|
Poll::Pending => Poll::Pending,
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl AsyncWrite for Duplex {
|
||
|
|
fn poll_write(
|
||
|
|
self: Pin<&mut Self>,
|
||
|
|
cx: &mut Context<'_>,
|
||
|
|
buf: &[u8],
|
||
|
|
) -> Poll<io::Result<usize>> {
|
||
|
|
Pin::new(&mut self.get_mut().0).poll_write(cx, buf)
|
||
|
|
}
|
||
|
|
|
||
|
|
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
||
|
|
Pin::new(&mut self.get_mut().0).poll_flush(cx)
|
||
|
|
}
|
||
|
|
|
||
|
|
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
||
|
|
Pin::new(&mut self.get_mut().0).poll_shutdown(cx)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/// Flips one ciphertext byte in the initiator's single prelude frame as it is
|
||
|
|
/// written, leaving the Noise msg 1 that follows untouched.
|
||
|
|
struct TamperPrelude {
|
||
|
|
inner: Duplex,
|
||
|
|
done: bool,
|
||
|
|
}
|
||
|
|
|
||
|
|
impl AsyncRead for TamperPrelude {
|
||
|
|
fn poll_read(
|
||
|
|
self: Pin<&mut Self>,
|
||
|
|
cx: &mut Context<'_>,
|
||
|
|
buf: &mut [u8],
|
||
|
|
) -> Poll<io::Result<usize>> {
|
||
|
|
let this = self.get_mut();
|
||
|
|
Pin::new(&mut this.inner).poll_read(cx, buf)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
impl AsyncWrite for TamperPrelude {
|
||
|
|
fn poll_write(
|
||
|
|
self: Pin<&mut Self>,
|
||
|
|
cx: &mut Context<'_>,
|
||
|
|
buf: &[u8],
|
||
|
|
) -> Poll<io::Result<usize>> {
|
||
|
|
let this = self.get_mut();
|
||
|
|
if !this.done {
|
||
|
|
this.done = true;
|
||
|
|
let mut mutated = buf.to_vec();
|
||
|
|
// version(1) | len(4) | ct_len(2) | ct...
|
||
|
|
if mutated.len() > 7 {
|
||
|
|
mutated[7] ^= 0x01;
|
||
|
|
}
|
||
|
|
let wanted = buf.len();
|
||
|
|
return Pin::new(&mut this.inner)
|
||
|
|
.poll_write(cx, &mutated)
|
||
|
|
.map(|r| r.map(|_| wanted));
|
||
|
|
}
|
||
|
|
Pin::new(&mut this.inner).poll_write(cx, buf)
|
||
|
|
}
|
||
|
|
|
||
|
|
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
||
|
|
Pin::new(&mut self.get_mut().inner).poll_flush(cx)
|
||
|
|
}
|
||
|
|
|
||
|
|
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
|
||
|
|
Pin::new(&mut self.get_mut().inner).poll_close(cx)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
fn responder_auth(peer: &Keys) -> impl FnOnce(&[u8]) -> Option<PeerAuth> + '_ {
|
||
|
|
move |spk: &[u8]| {
|
||
|
|
(spk == &peer.prekey_pk[..]).then(|| PeerAuth {
|
||
|
|
identity: peer.identity_pk.to_vec(),
|
||
|
|
kem_pk: Some(peer.kem_pk.to_vec()),
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
fn responder_auth_classical(peer: &Keys) -> impl FnOnce(&[u8]) -> Option<PeerAuth> + '_ {
|
||
|
|
move |spk: &[u8]| {
|
||
|
|
(spk == &peer.prekey_pk[..]).then(|| PeerAuth {
|
||
|
|
identity: peer.identity_pk.to_vec(),
|
||
|
|
kem_pk: None,
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn hybrid_prologue_has_the_documented_layout() {
|
||
|
|
let ct = vec![0xABu8; kem::CT_LEN];
|
||
|
|
let ss = [0xCDu8; kem::SS_LEN];
|
||
|
|
let p = session::hybrid_prologue(&ct, &ss);
|
||
|
|
assert_eq!(p.len(), 12 + 1 + kem::CT_LEN + 1 + kem::SS_LEN);
|
||
|
|
assert_eq!(&p[..12], b"onionwire-v2");
|
||
|
|
assert_eq!(p[12], 0x00);
|
||
|
|
assert_eq!(&p[13..13 + kem::CT_LEN], &ct[..]);
|
||
|
|
assert_eq!(p[13 + kem::CT_LEN], 0x00);
|
||
|
|
assert_eq!(&p[14 + kem::CT_LEN..], &ss[..]);
|
||
|
|
}
|
||
|
|
|
||
|
|
#[test]
|
||
|
|
fn prelude_frame_is_version_two_with_a_u16_length_and_the_ct() {
|
||
|
|
let rt = tokio::runtime::Runtime::new().expect("runtime");
|
||
|
|
rt.block_on(async {
|
||
|
|
let (tx, mut rx) = tokio::io::duplex(4096);
|
||
|
|
let ct = vec![7u8; kem::CT_LEN];
|
||
|
|
frame::write_prelude(&mut Duplex(tx), &ct)
|
||
|
|
.await
|
||
|
|
.expect("write");
|
||
|
|
let mut head = [0u8; 7];
|
||
|
|
use tokio::io::AsyncReadExt;
|
||
|
|
rx.read_exact(&mut head).await.expect("head");
|
||
|
|
assert_eq!(head[0], frame::VERSION_PRELUDE);
|
||
|
|
assert_eq!(u32::from_be_bytes(head[1..5].try_into().unwrap()), 1090);
|
||
|
|
assert_eq!(u16::from_be_bytes(head[5..7].try_into().unwrap()), 1088);
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
#[tokio::test]
|
||
|
|
async fn tampered_prelude_fails_the_handshake() {
|
||
|
|
let node = Keys::generate();
|
||
|
|
let peer = Keys::generate();
|
||
|
|
let (client, server) = tokio::io::duplex(256 * 1024);
|
||
|
|
|
||
|
|
let send = async {
|
||
|
|
let mut stream = TamperPrelude {
|
||
|
|
inner: Duplex(client),
|
||
|
|
done: false,
|
||
|
|
};
|
||
|
|
session::handshake_initiator(
|
||
|
|
&mut stream,
|
||
|
|
&peer,
|
||
|
|
&node.identity_pk,
|
||
|
|
&node.prekey_pk,
|
||
|
|
Some(&node.kem_pk),
|
||
|
|
)
|
||
|
|
.await
|
||
|
|
};
|
||
|
|
let receive = async {
|
||
|
|
let mut stream = Duplex(server);
|
||
|
|
session::handshake_responder(&mut stream, &node, responder_auth(&peer)).await
|
||
|
|
};
|
||
|
|
let (sent, received) = futures::join!(send, receive);
|
||
|
|
|
||
|
|
let err = match received {
|
||
|
|
Ok(_) => panic!("a tampered ciphertext must not complete the handshake"),
|
||
|
|
Err(e) => e,
|
||
|
|
};
|
||
|
|
assert!(
|
||
|
|
!err.to_string().contains(PRELUDE_REQUIRED),
|
||
|
|
"the prelude was present, so this must fail in the prologue/Noise step: {err}"
|
||
|
|
);
|
||
|
|
assert!(sent.is_err(), "no initiator session can be established");
|
||
|
|
}
|
||
|
|
|
||
|
|
#[tokio::test]
|
||
|
|
async fn a_prelude_to_a_classical_friend_row_is_refused() {
|
||
|
|
use onionwire::session::UNEXPECTED_PRELUDE;
|
||
|
|
let node = Keys::generate();
|
||
|
|
let peer = Keys::generate();
|
||
|
|
let (client, server) = tokio::io::duplex(256 * 1024);
|
||
|
|
|
||
|
|
let send = async {
|
||
|
|
let mut stream = Duplex(client);
|
||
|
|
session::handshake_initiator(
|
||
|
|
&mut stream,
|
||
|
|
&peer,
|
||
|
|
&node.identity_pk,
|
||
|
|
&node.prekey_pk,
|
||
|
|
Some(&node.kem_pk),
|
||
|
|
)
|
||
|
|
.await
|
||
|
|
};
|
||
|
|
let receive = async {
|
||
|
|
let mut stream = Duplex(server);
|
||
|
|
session::handshake_responder(&mut stream, &node, responder_auth_classical(&peer)).await
|
||
|
|
};
|
||
|
|
let (sent, received) = futures::join!(send, receive);
|
||
|
|
|
||
|
|
let err = match received {
|
||
|
|
Ok(_) => panic!("the wire must not upgrade a classical friend row"),
|
||
|
|
Err(e) => e,
|
||
|
|
};
|
||
|
|
assert!(
|
||
|
|
err.to_string().contains(UNEXPECTED_PRELUDE),
|
||
|
|
"expected {UNEXPECTED_PRELUDE}, got: {err}"
|
||
|
|
);
|
||
|
|
assert!(sent.is_err());
|
||
|
|
}
|
||
|
|
|
||
|
|
#[tokio::test]
|
||
|
|
async fn a_classical_opening_to_a_kem_friend_is_refused() {
|
||
|
|
let node = Keys::generate();
|
||
|
|
let peer = Keys::generate();
|
||
|
|
let (client, server) = tokio::io::duplex(256 * 1024);
|
||
|
|
|
||
|
|
let send = async {
|
||
|
|
let mut stream = Duplex(client);
|
||
|
|
session::handshake_initiator(&mut stream, &peer, &node.identity_pk, &node.prekey_pk, None)
|
||
|
|
.await
|
||
|
|
};
|
||
|
|
let receive = async {
|
||
|
|
let mut stream = Duplex(server);
|
||
|
|
session::handshake_responder(&mut stream, &node, responder_auth(&peer)).await
|
||
|
|
};
|
||
|
|
let (sent, received) = futures::join!(send, receive);
|
||
|
|
|
||
|
|
let err = match received {
|
||
|
|
Ok(_) => panic!("a prelude-less session to a KEM friend is a hard fail"),
|
||
|
|
Err(e) => e,
|
||
|
|
};
|
||
|
|
assert!(
|
||
|
|
err.to_string().contains(PRELUDE_REQUIRED),
|
||
|
|
"expected {PRELUDE_REQUIRED}, got: {err}"
|
||
|
|
);
|
||
|
|
assert!(sent.is_err());
|
||
|
|
}
|