Compare commits
No commits in common. "3e5442cb8ee2add61c11d47949f9de7a11f771ed" and "e2f38468c7561b577d97caa889e9e212e93cd28b" have entirely different histories.
3e5442cb8e
...
e2f38468c7
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "async-psec"
|
name = "async-psec"
|
||||||
version = "0.4.0"
|
version = "0.3.2"
|
||||||
authors = ["Hardcore Sushi <hardcore.sushi@disroot.org>"]
|
authors = ["Hardcore Sushi <hardcore.sushi@disroot.org>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
description = "Asynchronous PSEC implementation"
|
description = "Asynchronous PSEC implementation"
|
||||||
|
@ -30,7 +30,7 @@ async fn main() -> Result<(), PsecError> {
|
|||||||
To add this crate to your project, add the following to your project's Cargo.toml:
|
To add this crate to your project, add the following to your project's Cargo.toml:
|
||||||
```toml
|
```toml
|
||||||
[dependencies]
|
[dependencies]
|
||||||
async-psec = "0.4"
|
async-psec = "0.3"
|
||||||
```
|
```
|
||||||
|
|
||||||
# Documentation
|
# Documentation
|
||||||
|
19
src/lib.rs
19
src/lib.rs
@ -6,7 +6,7 @@
|
|||||||
Add this in your `Cargo.toml`:
|
Add this in your `Cargo.toml`:
|
||||||
```toml
|
```toml
|
||||||
[dependencies]
|
[dependencies]
|
||||||
async-psec = "0.4"
|
async-psec = "0.3"
|
||||||
```
|
```
|
||||||
And then:
|
And then:
|
||||||
```no_run
|
```no_run
|
||||||
@ -36,7 +36,7 @@ async fn main() -> Result<(), PsecError> {
|
|||||||
If you want to split the [`Session`] struct in two parts, you must enable the `split` feature:
|
If you want to split the [`Session`] struct in two parts, you must enable the `split` feature:
|
||||||
```toml
|
```toml
|
||||||
[dependencies]
|
[dependencies]
|
||||||
async-psec = { version = "0.4", feature = ["split"] }
|
async-psec = { version = "0.3", feature = ["split"] }
|
||||||
```
|
```
|
||||||
This can be useful if you want to send data from one thread/task and receive from another in parallel.
|
This can be useful if you want to send data from one thread/task and receive from another in parallel.
|
||||||
*/
|
*/
|
||||||
@ -169,7 +169,7 @@ fn unpad(input: Vec<u8>) -> Result<Vec<u8>, PsecError> {
|
|||||||
|
|
||||||
fn encrypt(local_cipher: &Aes128Gcm, local_iv: &[u8], local_counter: &mut usize, plain_text: &[u8], use_padding: bool) -> Vec<u8> {
|
fn encrypt(local_cipher: &Aes128Gcm, local_iv: &[u8], local_counter: &mut usize, plain_text: &[u8], use_padding: bool) -> Vec<u8> {
|
||||||
let padded_msg = pad(plain_text, use_padding);
|
let padded_msg = pad(plain_text, use_padding);
|
||||||
let cipher_len = ((padded_msg.len() + AES_TAG_LEN) as MessageLenType).to_be_bytes();
|
let cipher_len = (padded_msg.len() as MessageLenType).to_be_bytes();
|
||||||
let payload = Payload {
|
let payload = Payload {
|
||||||
msg: &padded_msg,
|
msg: &padded_msg,
|
||||||
aad: &cipher_len
|
aad: &cipher_len
|
||||||
@ -187,7 +187,7 @@ async fn encrypt_and_send<T: AsyncWriteExt + Unpin>(writer: &mut T, local_cipher
|
|||||||
async fn receive_and_decrypt<T: AsyncReadExt + Unpin>(reader: &mut T, peer_cipher: &Aes128Gcm, peer_iv: &[u8], peer_counter: &mut usize, max_recv_size: Option<usize>) -> Result<Vec<u8>, PsecError> {
|
async fn receive_and_decrypt<T: AsyncReadExt + Unpin>(reader: &mut T, peer_cipher: &Aes128Gcm, peer_iv: &[u8], peer_counter: &mut usize, max_recv_size: Option<usize>) -> Result<Vec<u8>, PsecError> {
|
||||||
let mut message_len = [0; MESSAGE_LEN_LEN];
|
let mut message_len = [0; MESSAGE_LEN_LEN];
|
||||||
receive(reader, &mut message_len).await?;
|
receive(reader, &mut message_len).await?;
|
||||||
let recv_len = MessageLenType::from_be_bytes(message_len) as usize;
|
let recv_len = MessageLenType::from_be_bytes(message_len) as usize + AES_TAG_LEN;
|
||||||
if let Some(max_recv_size) = max_recv_size {
|
if let Some(max_recv_size) = max_recv_size {
|
||||||
if recv_len > max_recv_size {
|
if recv_len > max_recv_size {
|
||||||
return Err(PsecError::BufferTooLarge);
|
return Err(PsecError::BufferTooLarge);
|
||||||
@ -664,7 +664,7 @@ fn hex_encode(buff: &[u8]) -> String {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::{pad, unpad, compute_max_recv_size, MESSAGE_LEN_LEN};
|
use super::{pad, unpad, MESSAGE_LEN_LEN};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn padding() {
|
fn padding() {
|
||||||
@ -680,13 +680,4 @@ mod tests {
|
|||||||
let large_msg = "a".repeat(5000);
|
let large_msg = "a".repeat(5000);
|
||||||
assert_eq!(pad(large_msg.as_bytes(), true).len(), 8000);
|
assert_eq!(pad(large_msg.as_bytes(), true).len(), 8000);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn compute_max_size() {
|
|
||||||
assert_eq!(compute_max_recv_size(5, false), 1016);
|
|
||||||
assert_eq!(compute_max_recv_size(996, false), 1016);
|
|
||||||
assert_eq!(compute_max_recv_size(997, false), 2016);
|
|
||||||
assert_eq!(compute_max_recv_size(16383996, false), 16384016);
|
|
||||||
assert_eq!(compute_max_recv_size(16383997, false), 32768016)
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user