Skip to content

Commit

Permalink
Unify line ending to unix-style (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
LecrisUT authored Jun 3, 2024
1 parent 6ec9c27 commit f680bcc
Show file tree
Hide file tree
Showing 51 changed files with 1,902 additions and 1,902 deletions.
44 changes: 22 additions & 22 deletions src/core/common/authentication_key.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
use std::marker::PhantomData;
use std::ops::Deref;

pub(crate) struct AuthenticationKey<Version, Purpose> {
pub(crate) version: PhantomData<Version>,
pub(crate) purpose: PhantomData<Purpose>,
pub(crate) key: Vec<u8>,
}

impl<Version, Purpose> AsRef<[u8]> for AuthenticationKey<Version, Purpose> {
fn as_ref(&self) -> &[u8] {
&self.key
}
}

impl<Version, Purpose> Deref for AuthenticationKey<Version, Purpose> {
type Target = [u8];

fn deref(&self) -> &Self::Target {
&self.key
}
}
use std::marker::PhantomData;
use std::ops::Deref;

pub(crate) struct AuthenticationKey<Version, Purpose> {
pub(crate) version: PhantomData<Version>,
pub(crate) purpose: PhantomData<Purpose>,
pub(crate) key: Vec<u8>,
}

impl<Version, Purpose> AsRef<[u8]> for AuthenticationKey<Version, Purpose> {
fn as_ref(&self) -> &[u8] {
&self.key
}
}

impl<Version, Purpose> Deref for AuthenticationKey<Version, Purpose> {
type Target = [u8];

fn deref(&self) -> &Self::Target {
&self.key
}
}
4 changes: 2 additions & 2 deletions src/core/common/authentication_key_impl/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
mod v1_local;
mod v3_local;
mod v1_local;
mod v3_local;
mod v4_local;
48 changes: 24 additions & 24 deletions src/core/common/authentication_key_impl/v1_local.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
#![cfg(feature = "v1_local")]
use std::marker::PhantomData;
use ring::hkdf;
use crate::core::{Local, PasetoError, PasetoNonce, PasetoSymmetricKey, V1};
use crate::core::common::authentication_key::AuthenticationKey;
use crate::core::common::hkdf_key::HkdfKey;

impl AuthenticationKey<V1, Local> {
pub(crate) fn try_from(
message: &[u8; 24],
key: &PasetoSymmetricKey<V1, Local>,
nonce: &PasetoNonce<V1, Local>,
) -> Result<Self, PasetoError> {
let info = message.as_ref();
let salt = hkdf::Salt::new(hkdf::HKDF_SHA384, &nonce[..16]);
let HkdfKey(out) = salt.extract(key.as_ref()).expand(&[info], HkdfKey(32))?.try_into()?;

Ok(Self {
version: PhantomData,
purpose: PhantomData,
key: out,
})
}
}
#![cfg(feature = "v1_local")]
use std::marker::PhantomData;
use ring::hkdf;
use crate::core::{Local, PasetoError, PasetoNonce, PasetoSymmetricKey, V1};
use crate::core::common::authentication_key::AuthenticationKey;
use crate::core::common::hkdf_key::HkdfKey;

impl AuthenticationKey<V1, Local> {
pub(crate) fn try_from(
message: &[u8; 24],
key: &PasetoSymmetricKey<V1, Local>,
nonce: &PasetoNonce<V1, Local>,
) -> Result<Self, PasetoError> {
let info = message.as_ref();
let salt = hkdf::Salt::new(hkdf::HKDF_SHA384, &nonce[..16]);
let HkdfKey(out) = salt.extract(key.as_ref()).expand(&[info], HkdfKey(32))?.try_into()?;

Ok(Self {
version: PhantomData,
purpose: PhantomData,
key: out,
})
}
}
36 changes: 18 additions & 18 deletions src/core/common/authentication_key_impl/v3_local.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#![cfg(feature = "v3_local")]
use std::marker::PhantomData;
use ring::hkdf;
use crate::core::{Key, Local, PasetoError, PasetoSymmetricKey, V3};
use crate::core::common::HkdfKey;

impl crate::core::common::authentication_key::AuthenticationKey<V3, Local> {
pub(crate) fn try_from(message: &Key<56>, key: &PasetoSymmetricKey<V3, Local>) -> Result<Self, PasetoError> {
let info = message.as_ref();
let salt = hkdf::Salt::new(hkdf::HKDF_SHA384, &[]);
let HkdfKey(out) = salt.extract(key.as_ref()).expand(&[info], HkdfKey(48))?.try_into()?;

Ok(Self {
version: PhantomData,
purpose: PhantomData,
key: out,
})
}
#![cfg(feature = "v3_local")]
use std::marker::PhantomData;
use ring::hkdf;
use crate::core::{Key, Local, PasetoError, PasetoSymmetricKey, V3};
use crate::core::common::HkdfKey;

impl crate::core::common::authentication_key::AuthenticationKey<V3, Local> {
pub(crate) fn try_from(message: &Key<56>, key: &PasetoSymmetricKey<V3, Local>) -> Result<Self, PasetoError> {
let info = message.as_ref();
let salt = hkdf::Salt::new(hkdf::HKDF_SHA384, &[]);
let HkdfKey(out) = salt.extract(key.as_ref()).expand(&[info], HkdfKey(48))?.try_into()?;

Ok(Self {
version: PhantomData,
purpose: PhantomData,
key: out,
})
}
}
42 changes: 21 additions & 21 deletions src/core/common/authentication_key_impl/v4_local.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
#![cfg(feature = "v4_local")]
use std::marker::PhantomData;
use std::ops::Deref;
use blake2::digest::consts::U32;
use blake2::{Blake2bMac, digest::Update};
use blake2::digest::FixedOutput;
use digest::KeyInit;
use crate::core::{Key, Local, PasetoSymmetricKey, V4};

impl crate::core::common::authentication_key::AuthenticationKey<V4, Local> {
pub(crate) fn from(message: &Key<56>, key: &PasetoSymmetricKey<V4, Local>) -> Self {
let mut context = Blake2bMac::<U32>::new_from_slice(key.as_ref()).unwrap();
context.update(message.as_ref());
let binding = context.finalize_fixed();
let key = binding.to_vec();
Self {
version: PhantomData,
purpose: PhantomData,
key,
}
}
#![cfg(feature = "v4_local")]
use std::marker::PhantomData;
use std::ops::Deref;
use blake2::digest::consts::U32;
use blake2::{Blake2bMac, digest::Update};
use blake2::digest::FixedOutput;
use digest::KeyInit;
use crate::core::{Key, Local, PasetoSymmetricKey, V4};

impl crate::core::common::authentication_key::AuthenticationKey<V4, Local> {
pub(crate) fn from(message: &Key<56>, key: &PasetoSymmetricKey<V4, Local>) -> Self {
let mut context = Blake2bMac::<U32>::new_from_slice(key.as_ref()).unwrap();
context.update(message.as_ref());
let binding = context.finalize_fixed();
let key = binding.to_vec();
Self {
version: PhantomData,
purpose: PhantomData,
key,
}
}
}
88 changes: 44 additions & 44 deletions src/core/common/authentication_key_separator.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
use std::fmt;
use std::fmt::Display;
use std::ops::{Add, Deref};
use crate::core::{Key, Local, PasetoNonce};

#[derive(Debug)]
pub (crate) struct AuthenticationKeySeparator(&'static str);

impl Display for AuthenticationKeySeparator {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", &self.0)
}
}

impl Default for AuthenticationKeySeparator {
fn default() -> Self {
Self("paseto-auth-key-for-aead")
}
}

impl Deref for AuthenticationKeySeparator {
type Target = [u8];

fn deref(&self) -> &Self::Target {
self.0.as_bytes()
}
}

impl AsRef<str> for AuthenticationKeySeparator {
fn as_ref(&self) -> &str {
self.0
}
}

impl<'a, Version> Add<&PasetoNonce<'a, Version, Local>> for AuthenticationKeySeparator {
type Output = Key<56>;

fn add(self, rhs: &PasetoNonce<Version, Local>) -> Self::Output {
let mut output = [0u8; 56];
output[..24].copy_from_slice(self.0.as_bytes());
output[24..].copy_from_slice(rhs.as_ref());
Key::<56>::from(output)
}
}
use std::fmt;
use std::fmt::Display;
use std::ops::{Add, Deref};
use crate::core::{Key, Local, PasetoNonce};

#[derive(Debug)]
pub (crate) struct AuthenticationKeySeparator(&'static str);

impl Display for AuthenticationKeySeparator {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", &self.0)
}
}

impl Default for AuthenticationKeySeparator {
fn default() -> Self {
Self("paseto-auth-key-for-aead")
}
}

impl Deref for AuthenticationKeySeparator {
type Target = [u8];

fn deref(&self) -> &Self::Target {
self.0.as_bytes()
}
}

impl AsRef<str> for AuthenticationKeySeparator {
fn as_ref(&self) -> &str {
self.0
}
}

impl<'a, Version> Add<&PasetoNonce<'a, Version, Local>> for AuthenticationKeySeparator {
type Output = Key<56>;

fn add(self, rhs: &PasetoNonce<Version, Local>) -> Self::Output {
let mut output = [0u8; 56];
output[..24].copy_from_slice(self.0.as_bytes());
output[24..].copy_from_slice(rhs.as_ref());
Key::<56>::from(output)
}
}
38 changes: 19 additions & 19 deletions src/core/common/cipher_text.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use std::marker::PhantomData;

pub(crate) struct CipherText<Version, Purpose> {
pub(crate) ciphertext: Vec<u8>,
pub(crate) version: PhantomData<Version>,
pub(crate) purpose: PhantomData<Purpose>,
}

impl<Version, Purpose> AsRef<Vec<u8>> for CipherText<Version, Purpose> {
fn as_ref(&self) -> &Vec<u8> {
&self.ciphertext
}
}

impl<Version, Purpose> std::ops::Deref for CipherText<Version, Purpose> {
type Target = Vec<u8>;
fn deref(&self) -> &Self::Target {
&self.ciphertext
}
use std::marker::PhantomData;

pub(crate) struct CipherText<Version, Purpose> {
pub(crate) ciphertext: Vec<u8>,
pub(crate) version: PhantomData<Version>,
pub(crate) purpose: PhantomData<Purpose>,
}

impl<Version, Purpose> AsRef<Vec<u8>> for CipherText<Version, Purpose> {
fn as_ref(&self) -> &Vec<u8> {
&self.ciphertext
}
}

impl<Version, Purpose> std::ops::Deref for CipherText<Version, Purpose> {
type Target = Vec<u8>;
fn deref(&self) -> &Self::Target {
&self.ciphertext
}
}
8 changes: 4 additions & 4 deletions src/core/common/cipher_text_impl/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod v1_public;
mod v1_local;
mod v2_local;
mod v3_local;
mod v1_public;
mod v1_local;
mod v2_local;
mod v3_local;
mod v4_local;
52 changes: 26 additions & 26 deletions src/core/common/cipher_text_impl/v1_local.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
#![cfg(feature = "v1_local")]
use std::marker::PhantomData;
use aes::Aes256Ctr;
use aes::cipher::generic_array::GenericArray;
use aes::cipher::{NewCipher, StreamCipher};
use crate::core::common::cipher_text::CipherText;
use crate::core::{Local, V1};
use crate::core::common::EncryptionKey;

impl CipherText<V1, Local> {
pub(crate) fn from(payload: &[u8], encryption_key: &EncryptionKey<V1, Local>) -> Self {
let key = GenericArray::from_slice(encryption_key.as_ref());
let nonce = GenericArray::from_slice(encryption_key.counter_nonce());
let mut cipher = Aes256Ctr::new(key, nonce);
let mut ciphertext = vec![0u8; payload.as_ref().len()];

ciphertext.copy_from_slice(payload);

cipher.apply_keystream(&mut ciphertext);

CipherText {
ciphertext,
version: PhantomData,
purpose: PhantomData,
}
}
#![cfg(feature = "v1_local")]
use std::marker::PhantomData;
use aes::Aes256Ctr;
use aes::cipher::generic_array::GenericArray;
use aes::cipher::{NewCipher, StreamCipher};
use crate::core::common::cipher_text::CipherText;
use crate::core::{Local, V1};
use crate::core::common::EncryptionKey;

impl CipherText<V1, Local> {
pub(crate) fn from(payload: &[u8], encryption_key: &EncryptionKey<V1, Local>) -> Self {
let key = GenericArray::from_slice(encryption_key.as_ref());
let nonce = GenericArray::from_slice(encryption_key.counter_nonce());
let mut cipher = Aes256Ctr::new(key, nonce);
let mut ciphertext = vec![0u8; payload.as_ref().len()];

ciphertext.copy_from_slice(payload);

cipher.apply_keystream(&mut ciphertext);

CipherText {
ciphertext,
version: PhantomData,
purpose: PhantomData,
}
}
}
Loading

0 comments on commit f680bcc

Please sign in to comment.