somewhat (not work) poly1305 MAC

This commit is contained in:
Neemek 2025-11-07 21:22:09 +01:00
parent 4b3fcec525
commit 4bef2ecc9c
Signed by: neemek
GPG key ID: 84FFE4D7D40AB25E
3 changed files with 37 additions and 3 deletions

8
Cargo.lock generated
View file

@ -113,6 +113,7 @@ dependencies = [
"blake3",
"chacha",
"diffie-hellman",
"poly1305",
"rand",
]
@ -131,6 +132,13 @@ dependencies = [
"autocfg",
]
[[package]]
name = "poly1305"
version = "0.1.0"
dependencies = [
"crypto-bigint",
]
[[package]]
name = "ppv-lite86"
version = "0.2.21"

View file

@ -6,5 +6,6 @@ edition = "2024"
[dependencies]
diffie-hellman = { path = "../diffie-hellman", features = ["rand"] }
chacha = { path = "../chacha" }
poly1305 = { path = "../poly1305" }
rand = "0.9.2"
blake3 = "1.8.2"

View file

@ -17,7 +17,8 @@ pub struct EncryptedStream {
block: Block,
}
const KEY_DERIVATION_CONTEXT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
const KEY_DERIVATION_CONTEXT: &str =
concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
impl EncryptedStream {
/// connect and negotiate an encrypted stream as a client
@ -39,7 +40,8 @@ impl EncryptedStream {
let mut nonce_bytes = [0u8; 12];
socket.read_exact(&mut nonce_bytes)?;
let key = shared_secret.derive_key(|bytes| blake3::derive_key(KEY_DERIVATION_CONTEXT, &bytes));
let key =
shared_secret.derive_key(|bytes| blake3::derive_key(KEY_DERIVATION_CONTEXT, &bytes));
Ok(EncryptedStream::wrap(socket, key, nonce_bytes))
}
@ -64,6 +66,28 @@ impl EncryptedStream {
pub fn reset(&mut self, next_nonce: [u32; 3]) {
self.block.reset(next_nonce)
}
pub fn read_packet(&mut self) -> Result<Vec<u8>, Error> {
// the first bytes should be the nonce
let mut nonce_bytes = [0u8; 12];
self.socket.read(&mut nonce_bytes)?;
let mut nonce = [0u32; 3];
u8_to_u32(&mut nonce, &nonce_bytes);
self.block.reset(nonce);
let poly_key = self.block.get_bytes();
self.block.advance();
let mut len_bytes = [0u8; 4];
self.read(&mut len_bytes)?;
let len = u32::from_le_bytes(len_bytes);
let mut buf = vec![0u8; len as usize];
}
}
impl TryFrom<TcpStream> for EncryptedStream {
@ -88,7 +112,8 @@ impl TryFrom<TcpStream> for EncryptedStream {
socket.write(&nonce_bytes)?;
let key = shared_secret.derive_key(|bytes| blake3::derive_key(KEY_DERIVATION_CONTEXT, &bytes));
let key =
shared_secret.derive_key(|bytes| blake3::derive_key(KEY_DERIVATION_CONTEXT, &bytes));
Ok(EncryptedStream::wrap(socket, key, nonce_bytes))
}