Skip to content

Commit

Permalink
work in progress encryption and controller upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
chanderlud committed Dec 15, 2023
1 parent f9c5351 commit 5d6ee1f
Show file tree
Hide file tree
Showing 9 changed files with 765 additions and 268 deletions.
18 changes: 17 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ name = "cccp"
version = "0.6.0"
edition = "2021"
build = "build.rs"
repository = "https://github.com/chanderlud/cccp"
authors = ["Chander Luderman <me@chanchan.dev>"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand All @@ -20,7 +22,21 @@ indicatif = "0.17"
prost = "0.12"
bytesize = "1.3.0"
kanal = "0.1.0-pre8"
blake3 = "1.5.0"
blake3 = "1.5"
chacha20 = "0.9"
rand = "0.8"
hex = "0.4"
ctr = "0.9"
aes = "0.8"
itertools = "0.12"
libc = "0.2.151"

[target.'cfg(unix)'.dependencies]
libc = "0.2.151"

[target.'cfg(windows)'.dependencies]
windows-sys = { version = "0.52.0", features = ["Win32_Storage_FileSystem", "Win32_Foundation"] }
widestring = "1.0.2"

[build-dependencies]
prost-build = "0.12.3"
Expand Down
157 changes: 157 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
use std::array::TryFromSliceError;
use std::process::{ExitCode, Termination};

use kanal::{ReceiveError, SendError};
use tokio::io;
use tokio::sync::AcquireError;

#[derive(Debug)]
pub(crate) struct Error {
pub(crate) kind: ErrorKind,
}

#[derive(Debug)]
pub(crate) enum ErrorKind {
Io(io::Error),
Parse(std::net::AddrParseError),
Decode(prost::DecodeError),
Join(tokio::task::JoinError),
Send(SendError),
Receive(ReceiveError),
Acquire(AcquireError),
TryFromSlice(TryFromSliceError),
#[cfg(windows)]
ContainsNull(widestring::error::ContainsNul<u16>),
#[cfg(unix)]
Nul(std::ffi::NulError),
MissingQueue,
MaxRetries,
StatusError,
Failure(u32),
}

impl From<io::Error> for Error {
fn from(error: io::Error) -> Self {
Self {
kind: ErrorKind::Io(error),
}
}
}

impl From<std::net::AddrParseError> for Error {
fn from(error: std::net::AddrParseError) -> Self {
Self {
kind: ErrorKind::Parse(error),
}
}
}

impl From<prost::DecodeError> for Error {
fn from(error: prost::DecodeError) -> Self {
Self {
kind: ErrorKind::Decode(error),
}
}
}

impl From<tokio::task::JoinError> for Error {
fn from(error: tokio::task::JoinError) -> Self {
Self {
kind: ErrorKind::Join(error),
}
}
}

impl From<SendError> for Error {
fn from(error: SendError) -> Self {
Self {
kind: ErrorKind::Send(error),
}
}
}

impl From<ReceiveError> for Error {
fn from(error: ReceiveError) -> Self {
Self {
kind: ErrorKind::Receive(error),
}
}
}

impl From<AcquireError> for Error {
fn from(error: AcquireError) -> Self {
Self {
kind: ErrorKind::Acquire(error),
}
}
}

impl From<TryFromSliceError> for Error {
fn from(error: TryFromSliceError) -> Self {
Self {
kind: ErrorKind::TryFromSlice(error),
}
}
}

#[cfg(windows)]
impl From<widestring::error::ContainsNul<u16>> for Error {
fn from(error: widestring::error::ContainsNul<u16>) -> Self {
Self {
kind: ErrorKind::ContainsNull(error),
}
}
}

#[cfg(unix)]
impl From<std::ffi::NulError> for Error {
fn from(error: std::ffi::NulError) -> Self {
Self {
kind: ErrorKind::Nul(error),
}
}
}

impl Termination for Error {
fn report(self) -> ExitCode {
ExitCode::from(match self.kind {
ErrorKind::Io(error) => match error.kind() {
io::ErrorKind::NotFound => 1,
_ => 2,
},
ErrorKind::Parse(_) => 3,
ErrorKind::Decode(_) => 4,
ErrorKind::Join(_) => 5,
ErrorKind::Send(_) => 6,
ErrorKind::Receive(_) => 7,
ErrorKind::Acquire(_) => 8,
_ => 9,
})
}
}

impl Error {
pub(crate) fn missing_queue() -> Self {
Self {
kind: ErrorKind::MissingQueue,
}
}

pub(crate) fn max_retries() -> Self {
Self {
kind: ErrorKind::MaxRetries,
}
}

pub(crate) fn failure(reason: u32) -> Self {
Self {
kind: ErrorKind::Failure(reason),
}
}

pub(crate) fn status_error() -> Self {
Self {
kind: ErrorKind::StatusError,
}
}
}
19 changes: 19 additions & 0 deletions src/items.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ message Message {
End end = 5;
Failure failure = 6;
Done done = 7;
Completed completed = 8;
}
}

Expand All @@ -24,6 +25,24 @@ message FileDetail {
string path = 1; // file path relative to the destination directory
uint64 size = 2; // file size in bytes
optional bytes signature = 3; // blake3 hash of file
optional Crypto crypto = 4; // encryption details
}

message Crypto {
Cipher cipher = 1;
bytes key = 2;
bytes iv = 3;
}

enum Cipher {
AES = 0;
CHACHA8 = 1;
CHACHA20 = 2;
}

// the receiver already had these files
message Completed {
repeated uint32 ids = 1;
}

// map of transfers and their confirmed indexes
Expand Down
48 changes: 48 additions & 0 deletions src/items.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::collections::HashMap;
use std::fmt::Display;

include!(concat!(env!("OUT_DIR"), "/cccp.items.rs"));

impl Message {
Expand All @@ -7,6 +10,18 @@ impl Message {
}
}

pub(crate) fn confirmations(indexes: HashMap<u32, ConfirmationIndexes>) -> Self {
Self {
message: Some(message::Message::Confirmations(Confirmations { indexes })),
}
}

pub(crate) fn completed(ids: Vec<u32>) -> Self {
Self {
message: Some(message::Message::Completed(Completed { ids })),
}
}

pub(crate) fn end(id: u32) -> Self {
Self {
message: Some(message::Message::End(End { id })),
Expand All @@ -25,3 +40,36 @@ impl Message {
}
}
}

impl Cipher {
/// the length of the key in bytes
pub(crate) fn key_length(&self) -> usize {
32
}

/// the length of the iv in bytes
pub(crate) fn iv_length(&self) -> usize {
match self {
Self::Chacha20 | Self::Chacha8 => 12,
Self::Aes => 16,
}
}
}

impl Display for Cipher {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let cipher = match self {
Self::Aes => "aes",
Self::Chacha8 => "chacha8",
Self::Chacha20 => "chacha20",
};

write!(f, "{}", cipher)
}
}

impl StartIndex {
pub(crate) fn new(index: u64) -> Self {
Self { index }
}
}
Loading

0 comments on commit 5d6ee1f

Please sign in to comment.