From 58df7fb221d1661e502c61ed2982915f9a7167bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolphe=20Br=C3=A9ard?= Date: Sun, 25 Feb 2024 12:03:06 +0100 Subject: [PATCH] Add a type alias for the scheme serialize type --- src/error.rs | 3 ++- src/ikm.rs | 6 +++--- src/scheme.rs | 6 ++++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/error.rs b/src/error.rs index 1180295..8a4a206 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,4 +1,5 @@ use crate::ikm::IkmId; +use crate::scheme::SchemeSerializeType; pub(crate) type Result = core::result::Result; @@ -19,7 +20,7 @@ pub enum Error { #[error("parsing error: ikm: invalid data length: {0} bytes")] ParsingIkmInvalidLength(usize), #[error("parsing error: scheme: {0}: unknown scheme")] - ParsingSchemeUnknownScheme(u32), + ParsingSchemeUnknownScheme(SchemeSerializeType), #[error("unable to generate random values: {0}")] RandomSourceError(getrandom::Error), #[error("system time error: {0}")] diff --git a/src/ikm.rs b/src/ikm.rs index 24a63bb..87ee50e 100644 --- a/src/ikm.rs +++ b/src/ikm.rs @@ -1,5 +1,5 @@ use crate::error::{Error, Result}; -use crate::Scheme; +use crate::scheme::{Scheme, SchemeSerializeType}; use base64ct::{Base64UrlUnpadded, Encoding}; use std::time::{Duration, SystemTime}; @@ -24,7 +24,7 @@ impl InputKeyMaterial { fn as_bytes(&self) -> Result<[u8; IKM_STRUCT_SIZE]> { let mut res = Vec::with_capacity(IKM_STRUCT_SIZE); res.extend_from_slice(&self.id.to_le_bytes()); - res.extend_from_slice(&(self.scheme as u32).to_le_bytes()); + res.extend_from_slice(&(self.scheme as SchemeSerializeType).to_le_bytes()); res.extend_from_slice(&self.content); res.extend_from_slice( &self @@ -47,7 +47,7 @@ impl InputKeyMaterial { pub(crate) fn from_bytes(b: [u8; IKM_STRUCT_SIZE]) -> Result { Ok(Self { id: IkmId::from_le_bytes(b[0..4].try_into().unwrap()), - scheme: u32::from_le_bytes(b[4..8].try_into().unwrap()).try_into()?, + scheme: SchemeSerializeType::from_le_bytes(b[4..8].try_into().unwrap()).try_into()?, content: b[8..40].try_into().unwrap(), created_at: InputKeyMaterial::bytes_to_system_time(&b[40..48])?, expire_at: InputKeyMaterial::bytes_to_system_time(&b[48..56])?, diff --git a/src/scheme.rs b/src/scheme.rs index 4afea4e..da67e0c 100644 --- a/src/scheme.rs +++ b/src/scheme.rs @@ -2,6 +2,8 @@ use crate::encryption::EncryptionFunction; use crate::kdf::KdfFunction; use crate::Error; +pub(crate) type SchemeSerializeType = u32; + #[derive(Copy, Clone, Debug, PartialEq)] pub enum Scheme { XChaCha20Poly1305WithBlake3 = 1, @@ -23,10 +25,10 @@ impl Scheme { } } -impl TryFrom for Scheme { +impl TryFrom for Scheme { type Error = Error; - fn try_from(value: u32) -> Result { + fn try_from(value: SchemeSerializeType) -> Result { match value { 1 => Ok(Scheme::XChaCha20Poly1305WithBlake3), _ => Err(Error::ParsingSchemeUnknownScheme(value)),