-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
483 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
use base64::{engine::general_purpose, Engine}; | ||
|
||
pub trait Base64Utils | ||
where | ||
Self: AsRef<[u8]>, | ||
{ | ||
fn encode_base64(&self) -> String { | ||
general_purpose::STANDARD.encode(self.as_ref()) | ||
} | ||
|
||
fn decode_base64(&self) -> Result<Vec<u8>, base64::DecodeError> { | ||
general_purpose::STANDARD.decode(self.as_ref()) | ||
} | ||
} | ||
|
||
impl<T: AsRef<[u8]>> Base64Utils for T {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use crate::*; | ||
|
||
pub struct AES256GCM; | ||
|
||
impl Cipher for AES256GCM { | ||
fn authorization_tag_size(&self) -> usize { | ||
16 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub trait Cipher { | ||
fn authorization_tag_size(&self) -> usize; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
mod core; | ||
pub use core::Cipher; | ||
|
||
mod variant; | ||
pub use variant::CipherVariant; | ||
|
||
#[cfg(feature = "aes-gcm")] | ||
mod aes256_gcm; | ||
#[cfg(feature = "aes-gcm")] | ||
pub use aes256_gcm::AES256GCM; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use strum::{AsRefStr, EnumString}; | ||
|
||
use crate::*; | ||
|
||
#[derive(Debug, PartialEq, AsRefStr, EnumString)] | ||
pub enum CipherVariant { | ||
#[cfg(feature = "aes-gcm")] | ||
#[strum(serialize = "AES256_GCM")] | ||
AES256GCM, | ||
} | ||
|
||
impl CipherVariant { | ||
pub fn cipher(&self) -> &dyn Cipher { | ||
match self { | ||
#[cfg(feature = "aes-gcm")] | ||
CipherVariant::AES256GCM => &AES256GCM, | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[cfg(feature = "aes-gcm")] | ||
mod aes_gcm { | ||
use crate::*; | ||
|
||
#[test] | ||
fn displays_aes256_gcm_cipher() { | ||
assert_eq!("AES256_GCM", CipherVariant::AES256GCM.as_ref()) | ||
} | ||
|
||
#[test] | ||
fn parses_aes256_gcm_cipher() { | ||
assert_eq!(CipherVariant::AES256GCM, "AES256_GCM".parse::<CipherVariant>().unwrap()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use derive_more::AsRef; | ||
|
||
use crate::*; | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub struct EncryptedValueData(Vec<u8>); | ||
|
||
#[derive(AsRef)] | ||
#[as_ref(forward)] | ||
pub struct EncryptedValueDataAuthorizationTag<'a>(&'a [u8]); | ||
|
||
#[derive(AsRef)] | ||
#[as_ref(forward)] | ||
pub struct EncryptedValueDataExceptTag<'a>(&'a [u8]); | ||
|
||
impl EncryptedValueData { | ||
pub fn tag(&self, cipher: &dyn Cipher) -> EncryptedValueDataAuthorizationTag { | ||
EncryptedValueDataAuthorizationTag(&self.0[self.cipher_authorization_tag_start_index(cipher)..]) | ||
} | ||
|
||
pub fn except_tag(&self, cipher: &dyn Cipher) -> EncryptedValueDataExceptTag { | ||
EncryptedValueDataExceptTag(&self.0[..self.cipher_authorization_tag_start_index(cipher)]) | ||
} | ||
|
||
fn cipher_authorization_tag_start_index(&self, cipher: &dyn Cipher) -> usize { | ||
self.0 | ||
.len() | ||
.checked_sub(cipher.authorization_tag_size()) | ||
.expect("minimum encrypted value length less than cipher authorization tag size") | ||
} | ||
} | ||
|
||
#[cfg(feature = "test-utils")] | ||
mod mock { | ||
use super::*; | ||
|
||
impl MockStringTestUtil for EncryptedValueDataExceptTag<'_> { | ||
fn mock_string() -> String { | ||
"3S1E9am/".to_string() | ||
} | ||
} | ||
|
||
impl MockStringTestUtil for EncryptedValueDataAuthorizationTag<'_> { | ||
fn mock_string() -> String { | ||
"nQUDkuh0OR1cjR5hGC5jOw==".to_string() | ||
} | ||
} | ||
|
||
impl MockTestUtil for EncryptedValueData { | ||
fn mock() -> Self { | ||
Self(vec![ | ||
221, 45, 68, 245, 169, 191, 157, 5, 3, 146, 232, 116, 57, 29, 92, 141, 30, 97, 24, 46, 99, 59, | ||
]) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
use crate::*; | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub struct EncryptedValueMetaData { | ||
pub cipher_variant: CipherVariant, | ||
pub initial_value: InitialValue, | ||
pub value_type: ValueType, | ||
} | ||
|
||
#[cfg(feature = "test-utils")] | ||
mod mock { | ||
use super::*; | ||
|
||
impl MockTestUtil for EncryptedValueMetaData { | ||
fn mock() -> Self { | ||
Self { | ||
cipher_variant: CipherVariant::AES256GCM, | ||
initial_value: MockTestUtil::mock(), | ||
value_type: ValueType::String, | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
mod data; | ||
pub use data::{EncryptedValueData, EncryptedValueDataAuthorizationTag, EncryptedValueDataExceptTag}; | ||
|
||
mod metadata; | ||
pub use metadata::EncryptedValueMetaData; | ||
|
||
mod value; | ||
pub use value::EncryptedValue; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// GOAL: serialize age into | ||
// ENC[AES256_GCM,data:EjRPNlhx,iv:XmS4b2ZqB39Qjpl/IQRm36KLclV8wXuBjuZsw4yekcU=,tag: | ||
// SWK3XZBBUA49muEyeqld4g==,type:str] | ||
|
||
use std::fmt::{Display, Formatter}; | ||
|
||
use crate::*; | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub struct EncryptedValue { | ||
data: EncryptedValueData, | ||
metadata: EncryptedValueMetaData, | ||
} | ||
|
||
impl Display for EncryptedValue { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
write!( | ||
f, | ||
"ENC[{},data:{},iv:{},tag:{},type:{}]", | ||
self.metadata.cipher_variant.as_ref(), | ||
self.data.except_tag(self.metadata.cipher_variant.cipher()).encode_base64(), | ||
self.metadata.initial_value.encode_base64(), | ||
self.data.tag(self.metadata.cipher_variant.cipher()).encode_base64(), | ||
self.metadata.value_type.as_ref(), | ||
) | ||
} | ||
} | ||
|
||
mod base64 { | ||
use base64::{engine::general_purpose, Engine}; | ||
|
||
pub trait Base64 | ||
where | ||
Self: AsRef<[u8]>, | ||
{ | ||
fn as_base64(&self) -> String { | ||
general_purpose::STANDARD.encode(self.as_ref()) | ||
} | ||
} | ||
|
||
impl<T: AsRef<[u8]>> Base64 for T {} | ||
} | ||
|
||
#[cfg(feature = "test-utils")] | ||
mod mock { | ||
use super::*; | ||
|
||
impl MockTestUtil for EncryptedValue { | ||
fn mock() -> Self { | ||
Self { | ||
data: MockTestUtil::mock(), | ||
metadata: MockTestUtil::mock(), | ||
} | ||
} | ||
} | ||
|
||
impl MockStringTestUtil for EncryptedValue { | ||
fn mock_string() -> String { | ||
format!( | ||
"ENC[AES256_GCM,data:{},iv:kwtVOk4u/wLHMovHYG2ngLv+uM8U9UJrIxjS6zCKmVY=,tag:{},type:str]", | ||
EncryptedValueDataExceptTag::mock_string(), | ||
EncryptedValueDataAuthorizationTag::mock_string() | ||
) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn displays_value_encryption_content() { | ||
DisplayTestUtils::assert_display::<EncryptedValue>() | ||
} | ||
|
||
#[test] | ||
fn parses_value_encryption_content() {} | ||
} |
Oops, something went wrong.