Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim-Leon committed Nov 19, 2024
1 parent 34be389 commit 1c9238b
Show file tree
Hide file tree
Showing 21 changed files with 273 additions and 140 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

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

11 changes: 11 additions & 0 deletions .idea/bucket-common-types.iml

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

8 changes: 8 additions & 0 deletions .idea/modules.xml

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

21 changes: 21 additions & 0 deletions .idea/remote-targets.xml

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

6 changes: 6 additions & 0 deletions .idea/vcs.xml

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

37 changes: 15 additions & 22 deletions src/bucket/bucket_guid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ impl fmt::Display for BucketGuid {
}
}
impl BucketGuid {
/// Returns a 32-byte array representation of the BucketGuid.
pub fn to_bytes(&self) -> [u8; 32] {
let mut slice = [0u8; 32];
slice[0..16].copy_from_slice(self.user_id.as_bytes());
slice[16..32].copy_from_slice(self.bucket_id.as_bytes());
slice
}

/// Format the BucketGuid using the specified format.
pub fn fmt_with(&self, f: &mut fmt::Formatter<'_>, format: BucketGuidFormat) -> fmt::Result {
match format {
Expand All @@ -63,15 +71,7 @@ impl BucketGuid {
}
}

//match format {
// BucketGuidFormat::Hyphenated(uuid_format) => write!(f, "{}-{}", self.user_id, self.bucket_id),
// BucketGuidFormat::Simple(uuid_format) => write!(
// f,
// "{}{}",
// self.user_id,
// self.bucket_id
// ),
//}

impl BucketGuid {
pub fn new(user_id: uuid::Uuid, bucket_id: uuid::Uuid) -> Self {
Self { user_id, bucket_id }
Expand All @@ -87,22 +87,10 @@ impl BucketGuid {
// Define the size of a ``BucketGuid`` in bytes.
pub const fn size() -> usize {
// Since each UUID is 16 bytes, the total length is 32 bytes
let size:usize = 32;
debug_assert_eq!(size, mem::size_of::<BucketGuid>());
size
32
}
}

impl SlicePattern for BucketGuid {
type Item = u8;
/// 8-bit array collection of 32 items.
fn as_slice(&self) -> &[Self::Item] {
let mut slice = [0u8; 32];
slice[0..16].copy_from_slice(self.user_id.as_bytes());
slice[16..32].copy_from_slice(self.bucket_id.as_bytes());
&slice
}
}

impl FromStr for BucketGuid {
type Err = BucketGuidParseError;
Expand Down Expand Up @@ -131,6 +119,11 @@ pub enum BucketGuidParseError {
#[cfg(test)]
mod tests {
use super::*;
#[test]
// Very important, checks the size to ensure there is no opsy
fn test_bucket_guid_size() {
debug_assert_eq!(BucketGuid::size(), std::mem::size_of::<BucketGuid>());
}

// Test the `new` method to create a new BucketGuid
#[test]
Expand Down
1 change: 1 addition & 0 deletions src/bucket/bucket_limits.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

pub struct BucketLimits {
pub bucket_size_limit : usize,
pub bucket_file_count_limit: usize,
Expand Down
48 changes: 48 additions & 0 deletions src/bucket/conditional_requests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use core::range::Range;
use time::OffsetDateTime;

/// CAS
/// Compare and swap os usually the conditional part of a request that must be met inorder for the request to be able to be completed.
pub enum BucketHash {
Sha256([u8; 32]),
Sha512([u8; 64]),
None,
// add more..
}


pub struct DataForRange {
/// Byte range to compare against.
range: Range<u32>,
/// You are only able to do 1 Kbyte of compare and swap for data.
data: Vec<u8>,
}




pub enum Condition {
BucketMetadataCondition(BucketMetadataCondition),
FileCondition(FileCondition),
}

pub enum BucketMetadataCondition {
Hash(BucketHash), /// Will compare the hash to see if it matches.
Tag(Vec<String>), /// every tag is an entity in a collection, you are able to check the tags for it.
ModifyDate(OffsetDateTime), /// When check if it's the last date.
Name(String),
Size(u64),
}

pub enum FileCondition {
Range(DataForRange),
/// Compares the entire file hash. Maybe TODO: Remove???
Data(BucketHash),

Size(u64),
}

pub struct ConditionalRequest {

}
5 changes: 3 additions & 2 deletions src/bucket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ pub mod bucket_feature_flags;
pub mod bucket_permission;
mod bucket_retention_policy;
pub mod bucket_compression;
mod storage_operation_behavior_flags;
mod bucket_limits;
pub mod storage_operation_behavior_flags;
pub mod bucket_limits;
pub mod conditional_requests;
2 changes: 1 addition & 1 deletion src/bucket/storage_operation_behavior_flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ bitflags::bitflags! {
/// Allow operations to overwrite existing data.
const SHOULD_OVERWRITE = 0b0000_0010;

/// Indicates that the operation can be destructive to storage capacity of the bucket.
/// Indicates that the operation can be destructive to the storage capacity of the bucket.
const IS_CAPACITY_DESTRUCTIVE = 0b0000_0100;

}
Expand Down
18 changes: 14 additions & 4 deletions src/encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::fmt;
use std::fmt::Display;
use std::num::ParseIntError;
use std::str::FromStr;
use pkcs8::ObjectIdentifier;
use serde::{Deserialize, Serialize};
use strum::{Display, EnumString};

Expand All @@ -25,6 +26,19 @@ pub enum EncryptionAlgorithm {
Custom(String),
}


impl EncryptionAlgorithm {
fn oid() -> Option<ObjectIdentifier> {
match Self {
EncryptionAlgorithm::None => { None }
EncryptionAlgorithm::AES256 => { Some(ObjectIdentifier::new("2.16.840.1.101.3.4.1.46").unwrap()) }
EncryptionAlgorithm::ChaCha20Poly1305 => { None }
EncryptionAlgorithm::XChaCha20Poly1305 => { None }
EncryptionAlgorithm::Custom(_) => { None }
}
}
}

#[derive(EnumString, PartialEq, Debug, Serialize, strum::Display, Clone, Eq, Deserialize)]
#[repr(u8)]
pub enum Role {
Expand All @@ -47,9 +61,6 @@ pub struct BucketEncryptionScheme {
/// The encryption algorithm used to secure the data in the bucket.
/// This is represented by the `EncryptionAlgorithm` enum.
pub encryption: EncryptionAlgorithm,
/// Derive function used for the bucket.
/// Argon2iD is the most secure with PBKDF2 being less so but, docent require much memory.
pub kdf: KeyDeriveFunction,
}


Expand Down Expand Up @@ -130,7 +141,6 @@ impl FromStr for BucketEncryptionScheme
responsible: role,
encryption: EncryptionAlgorithm::from_str(encryption.as_str()).unwrap(),
version,
kdf: Default::default(),
})
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/key/derived_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use secrecy::ExposeSecret;
use sha3::digest;
use sha3::digest::Update;
use sha3::Sha3_256;
use crate::encryption::EncryptionAlgorithm;
use crate::key::{CryptoHashDerivedKeyType, CryptoMasterKey, SecureGenericArray};

/// 256-bit key
Expand Down Expand Up @@ -37,6 +38,8 @@ impl<TKeyLength> CryptoHashDerivedKeyType<TKeyLength> for Sha3_256CryptoHashDeri
{
type Error = Infallible;
type CryptoHasher = Sha3_256;


/// Generates a `HashDerivedKey` from a master key and a nonce.
///
/// # Parameters
Expand All @@ -50,11 +53,11 @@ impl<TKeyLength> CryptoHashDerivedKeyType<TKeyLength> for Sha3_256CryptoHashDeri
hasher.update(master_key.as_slice());
hasher.update(nonce);
// Create a SecureGenericArray from the finalized hash
Self {
Ok( Self {
secret: SecureGenericArray {
0: GenericArray::from_slice(&hasher.finalize()),
},
}
})
}
}

Expand Down
31 changes: 13 additions & 18 deletions src/key/master_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use secrecy::ExposeSecret;
use sha3::{Digest, Sha3_256};
use std::convert::Infallible;
use digest::typenum;
use pkcs8::PrivateKeyInfo;
use pkcs8::{ObjectIdentifier, PrivateKeyInfo};
use pkcs8::spki::AlgorithmIdentifier;
use crate::key::{CryptoMasterKey, SecureGenericArray};

Expand Down Expand Up @@ -60,6 +60,11 @@ impl CryptoMasterKey for MasterKey256 {
}


#[derive(thiserror::Error, Debug)]
pub enum MasterKey256ParseError {

}


impl TryFrom<&PasswordHash<'_>> for MasterKey256 {
type Error = Infallible;
Expand All @@ -74,7 +79,7 @@ impl TryFrom<&PasswordHash<'_>> for MasterKey256 {
})
}
}
impl<T, TArrayLength> From<SecureGenericArray<T, TArrayLength>> for MasterKey256 {
impl<T, TArrayLength: generic_array::ArrayLength> From<SecureGenericArray<T, TArrayLength>> for MasterKey256 {
fn from(value: SecureGenericArray<T, TArrayLength>) -> Self {
Self {
secrete: value,
Expand All @@ -86,7 +91,7 @@ impl<T, TArrayLength> From<SecureGenericArray<T, TArrayLength>> for MasterKey256

impl TryInto<pkcs8::PrivateKeyInfo<'_>> for MasterKey256 {
type Error = Infallible;
fn try_into(self) -> Result<pkcs8::PrivateKeyInfo<'_>, Self::Error> {
fn try_into(self) -> Result<pkcs8::PrivateKeyInfo<'static>, Self::Error> {
Ok(PrivateKeyInfo {
algorithm: AlgorithmIdentifier {
oid: (),
Expand All @@ -98,26 +103,16 @@ impl TryInto<pkcs8::PrivateKeyInfo<'_>> for MasterKey256 {
}
}

impl TryFrom<pkcs8::PrivateKeyInfo> for MasterKey256 {
type Error = Infallible;
fn try_from(value: pkcs8::PrivateKeyInfo) -> Result<Self, Self::Error> {
value.algorithm.oid
Ok(
Self {
secrete: SecureGenericArray::from(value.private_key),
}
impl MasterKey256 {
pub fn oid() -> Option<ObjectIdentifier> {
// Example OID for the master key (custom or private)
Some(
ObjectIdentifier::new("1.3.6.1.4.1.99999.1.1").unwrap()
)
}
}


impl TryFrom<pkc1::key> for MasterKey256 {
type Error = Infallible;
fn try_from(value: pkc1::key) -> Result<Self, Self::Error> {

}
}

#[cfg(test)]
mod tests {
use crate::module::encryption::key::master_key::MasterKey256;
Expand Down
1 change: 1 addition & 0 deletions src/key/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use generic_array::{ArrayLength, GenericArray};
use secrecy::ExposeSecret;
use std::fmt::Debug;
use zeroize::Zeroize;
use crate::encryption::EncryptionAlgorithm;

pub mod derived_key;
pub mod master_key;
Expand Down
Loading

0 comments on commit 1c9238b

Please sign in to comment.