somewhat (not work) poly1305 MAC
This commit is contained in:
parent
4b3fcec525
commit
4bef2ecc9c
3 changed files with 37 additions and 3 deletions
31
src/lib.rs
31
src/lib.rs
|
|
@ -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))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue