Add a type alias for the scheme serialize type

This commit is contained in:
Rodolphe Bréard 2024-02-25 12:03:06 +01:00
parent 929d089e56
commit 58df7fb221
3 changed files with 9 additions and 6 deletions

View file

@ -1,4 +1,5 @@
use crate::ikm::IkmId;
use crate::scheme::SchemeSerializeType;
pub(crate) type Result<T, E = Error> = core::result::Result<T, E>;
@ -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}")]

View file

@ -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<Self> {
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])?,

View file

@ -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<u32> for Scheme {
impl TryFrom<SchemeSerializeType> for Scheme {
type Error = Error;
fn try_from(value: u32) -> Result<Self, Self::Error> {
fn try_from(value: SchemeSerializeType) -> Result<Self, Self::Error> {
match value {
1 => Ok(Scheme::XChaCha20Poly1305WithBlake3),
_ => Err(Error::ParsingSchemeUnknownScheme(value)),