-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unify line ending to unix-style (#41)
- Loading branch information
Showing
51 changed files
with
1,902 additions
and
1,902 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
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 | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
mod v1_local; | ||
mod v3_local; | ||
mod v1_local; | ||
mod v3_local; | ||
mod v4_local; |
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 |
---|---|---|
@@ -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, | ||
}) | ||
} | ||
} |
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 |
---|---|---|
@@ -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, | ||
}) | ||
} | ||
} |
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 |
---|---|---|
@@ -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, | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -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) | ||
} | ||
} |
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 |
---|---|---|
@@ -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 | ||
} | ||
} |
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 |
---|---|---|
@@ -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; |
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 |
---|---|---|
@@ -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, | ||
} | ||
} | ||
} |
Oops, something went wrong.