poly1305 #1
3 changed files with 37 additions and 3 deletions
8
Cargo.lock
generated
8
Cargo.lock
generated
|
|
@ -113,6 +113,7 @@ dependencies = [
|
||||||
"blake3",
|
"blake3",
|
||||||
"chacha",
|
"chacha",
|
||||||
"diffie-hellman",
|
"diffie-hellman",
|
||||||
|
"poly1305",
|
||||||
"rand",
|
"rand",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
@ -131,6 +132,13 @@ dependencies = [
|
||||||
"autocfg",
|
"autocfg",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "poly1305"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"crypto-bigint",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ppv-lite86"
|
name = "ppv-lite86"
|
||||||
version = "0.2.21"
|
version = "0.2.21"
|
||||||
|
|
|
||||||
|
|
@ -6,5 +6,6 @@ edition = "2024"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
diffie-hellman = { path = "../diffie-hellman", features = ["rand"] }
|
diffie-hellman = { path = "../diffie-hellman", features = ["rand"] }
|
||||||
chacha = { path = "../chacha" }
|
chacha = { path = "../chacha" }
|
||||||
|
poly1305 = { path = "../poly1305" }
|
||||||
rand = "0.9.2"
|
rand = "0.9.2"
|
||||||
blake3 = "1.8.2"
|
blake3 = "1.8.2"
|
||||||
|
|
|
||||||
31
src/lib.rs
31
src/lib.rs
|
|
@ -17,7 +17,8 @@ pub struct EncryptedStream {
|
||||||
block: Block,
|
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 {
|
impl EncryptedStream {
|
||||||
/// connect and negotiate an encrypted stream as a client
|
/// connect and negotiate an encrypted stream as a client
|
||||||
|
|
@ -39,7 +40,8 @@ impl EncryptedStream {
|
||||||
let mut nonce_bytes = [0u8; 12];
|
let mut nonce_bytes = [0u8; 12];
|
||||||
socket.read_exact(&mut nonce_bytes)?;
|
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))
|
Ok(EncryptedStream::wrap(socket, key, nonce_bytes))
|
||||||
}
|
}
|
||||||
|
|
@ -64,6 +66,28 @@ impl EncryptedStream {
|
||||||
pub fn reset(&mut self, next_nonce: [u32; 3]) {
|
pub fn reset(&mut self, next_nonce: [u32; 3]) {
|
||||||
self.block.reset(next_nonce)
|
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 {
|
impl TryFrom<TcpStream> for EncryptedStream {
|
||||||
|
|
@ -88,7 +112,8 @@ impl TryFrom<TcpStream> for EncryptedStream {
|
||||||
|
|
||||||
socket.write(&nonce_bytes)?;
|
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))
|
Ok(EncryptedStream::wrap(socket, key, nonce_bytes))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue