Skip to content
This repository has been archived by the owner on Aug 30, 2022. It is now read-only.

Commit

Permalink
refactor masking
Browse files Browse the repository at this point in the history
  • Loading branch information
little-dude committed May 19, 2020
1 parent e7ea51d commit 5239ef0
Show file tree
Hide file tree
Showing 26 changed files with 2,420 additions and 2,518 deletions.
10 changes: 10 additions & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@ homepage = "https://xain.io/"
[dependencies]
futures = "0.3.4"
tokio = { version = "0.2.19", features = ["rt-core", "rt-threaded", "tcp", "time", "macros", "signal", "sync", "stream"] }
derive_more = { version = "0.99.3", default-features = false, features = [ "display", "from" , "as_ref", "as_mut"] }
derive_more = { version = "0.99.3", default-features = false, features = [ "display", "from" , "as_ref", "as_mut", "into", "index", "index_mut"] }
rand = "0.7.3"
rand_chacha = "0.2.2"
serde = { version = "1.0.104", features = [ "derive" ] }
bytes = "0.5.4"
tracing = "0.1.13"
sodiumoxide = "0.2.5"
num = "0.2.1"
num = { version = "0.2.1" }
bincode = "1.2.1"
thiserror = "1.0.16"
anyhow = "1.0.28"
bitflags = "1.2.1"
paste = "0.1.12"
counter = "0.4.3"

[[bin]]
name = "coordinator"
Expand Down
65 changes: 28 additions & 37 deletions rust/src/certificate.rs
Original file line number Diff line number Diff line change
@@ -1,54 +1,45 @@
use derive_more::{AsMut, AsRef};
use crate::PetError;

use crate::{crypto::ByteObject, PetError};

#[derive(AsRef, AsMut, Clone, Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq)]
/// A dummy certificate.
pub struct Certificate(Vec<u8>);

impl ByteObject for Certificate {
/// Create a certificate a slice of bytes. Fails if the length of the input is invalid.
fn from_slice(bytes: &[u8]) -> Option<Self> {
Some(Self(bytes.to_vec()))
#[allow(clippy::len_without_is_empty)]
impl Certificate {
#[allow(clippy::new_without_default)]
/// Create a certificate
pub fn new() -> Self {
Self(vec![0_u8; 32])
}

/// Create a certificate initialized to zero.
fn zeroed() -> Self {
Self(vec![0_u8; Self::BYTES])
/// Get the length of the certificate.
pub fn len(&self) -> usize {
self.as_ref().len()
}

/// Get the certificate as a slice.
fn as_slice(&self) -> &[u8] {
self.0.as_slice()
/// Validate a certificate
pub fn validate(&self) -> Result<(), PetError> {
Ok(())
}
}

#[allow(clippy::len_without_is_empty)]
impl Certificate {
/// Get the number of bytes of a certificate.
pub const BYTES: usize = 32;

/// Get the length of the serialized certificate.
pub fn len(&self) -> usize {
self.as_slice().len()
}

/// Serialize the certificate into bytes.
pub fn serialize(&self) -> Vec<u8> {
self.as_slice().to_vec()
impl AsRef<[u8]> for Certificate {
/// Get a reference to the certificate.
fn as_ref(&self) -> &[u8] {
self.0.as_slice()
}
}

/// Deserialize the certificate from bytes. Fails if the length of the input is invalid.
pub fn deserialize(bytes: &[u8]) -> Result<Self, PetError> {
Self::from_slice(bytes).ok_or(PetError::InvalidMessage)
impl From<Vec<u8>> for Certificate {
/// Create a certificate from bytes.
fn from(bytes: Vec<u8>) -> Self {
Self(bytes)
}
}

/// Validate the certificate.
pub fn validate(&self) -> Result<(), PetError> {
if self.as_slice() == [0_u8; 32] {
Ok(())
} else {
Err(PetError::InvalidMessage)
}
impl From<&[u8]> for Certificate {
/// Create a certificate from a slice of bytes.
fn from(slice: &[u8]) -> Self {
Self(slice.to_vec())
}
}
Loading

0 comments on commit 5239ef0

Please sign in to comment.