diff --git a/src/error.rs b/src/error.rs index ee413dd..1180295 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,3 +1,5 @@ +use crate::ikm::IkmId; + pub(crate) type Result = core::result::Result; #[derive(thiserror::Error, Debug)] @@ -7,7 +9,7 @@ pub enum Error { #[error("ikm error: no input key material available")] IkmNoneAvailable, #[error("ikm error: {0}: input key material not found")] - IkmNotFound(u32), + IkmNotFound(IkmId), #[error("parsing error: invalid base64-urlsafe-nopadding data: {0}")] ParsingBase64Error(base64ct::Error), #[error("parsing error: encoded data: invalid IKM id: {0:?}")] diff --git a/src/ikm.rs b/src/ikm.rs index 2ecd9ff..080fb70 100644 --- a/src/ikm.rs +++ b/src/ikm.rs @@ -6,9 +6,11 @@ use std::time::{Duration, SystemTime}; const IKM_STRUCT_SIZE: usize = 57; const IKM_CONTENT_SIZE: usize = 32; +pub(crate) type IkmId = u32; + #[derive(Debug)] pub(crate) struct InputKeyMaterial { - pub(crate) id: u32, + pub(crate) id: IkmId, pub(crate) scheme: Scheme, pub(crate) content: [u8; IKM_CONTENT_SIZE], pub(crate) created_at: SystemTime, @@ -43,7 +45,7 @@ impl InputKeyMaterial { pub(crate) fn from_bytes(b: [u8; IKM_STRUCT_SIZE]) -> Result { Ok(Self { - id: u32::from_le_bytes(b[0..4].try_into().unwrap()), + id: IkmId::from_le_bytes(b[0..4].try_into().unwrap()), scheme: u32::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])?, @@ -131,7 +133,7 @@ impl InputKeyMaterialList { } #[cfg(feature = "encryption")] - pub(crate) fn get_ikm_by_id(&self, id: u32) -> Result<&InputKeyMaterial> { + pub(crate) fn get_ikm_by_id(&self, id: IkmId) -> Result<&InputKeyMaterial> { self.ikm_lst .iter() .find(|&ikm| ikm.id == id) diff --git a/src/storage.rs b/src/storage.rs index 3fb9bcb..9b2f69a 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -1,5 +1,6 @@ use crate::encryption::EncryptedData; use crate::error::{Error, Result}; +use crate::ikm::IkmId; use base64ct::{Base64UrlUnpadded, Encoding}; const STORAGE_SEPARATOR: &str = ":"; @@ -15,7 +16,7 @@ fn decode_data(s: &str) -> Result> { Ok(Base64UrlUnpadded::decode_vec(s)?) } -pub(crate) fn encode(ikm_id: u32, encrypted_data: &EncryptedData) -> String { +pub(crate) fn encode(ikm_id: IkmId, encrypted_data: &EncryptedData) -> String { let mut ret = String::new(); ret += &encode_data(&ikm_id.to_le_bytes()); ret += STORAGE_SEPARATOR; @@ -25,7 +26,7 @@ pub(crate) fn encode(ikm_id: u32, encrypted_data: &EncryptedData) -> String { ret } -pub(crate) fn decode(data: &str) -> Result<(u32, EncryptedData)> { +pub(crate) fn decode(data: &str) -> Result<(IkmId, EncryptedData)> { let v: Vec<&str> = data.split(STORAGE_SEPARATOR).collect(); if v.len() != NB_PARTS { return Err(Error::ParsingEncodedDataInvalidPartLen(NB_PARTS, v.len())); @@ -35,7 +36,7 @@ pub(crate) fn decode(data: &str) -> Result<(u32, EncryptedData)> { .clone() .try_into() .map_err(|_| Error::ParsingEncodedDataInvalidIkmId(id_raw))?; - let id = u32::from_le_bytes(id_raw); + let id = IkmId::from_le_bytes(id_raw); let encrypted_data = EncryptedData { nonce: decode_data(v[1])?, ciphertext: decode_data(v[2])?, @@ -45,10 +46,11 @@ pub(crate) fn decode(data: &str) -> Result<(u32, EncryptedData)> { #[cfg(test)] mod tests { + use crate::ikm::IkmId; use crate::storage::EncryptedData; const TEST_STR: &str = "KgAAAA:a5SpjAoqhvuI9n3GPhDKuotqoLbf7_Fb:TI24Wr_g-ZV7_X1oHqVKak9iRlQSneYVOMWB-3Lp-hFHKfxfnY-zR_bN"; - const TEST_IKM_ID: u32 = 42; + const TEST_IKM_ID: IkmId = 42; const TEST_NONCE: &'static [u8] = &[ 0x6b, 0x94, 0xa9, 0x8c, 0x0a, 0x2a, 0x86, 0xfb, 0x88, 0xf6, 0x7d, 0xc6, 0x3e, 0x10, 0xca, 0xba, 0x8b, 0x6a, 0xa0, 0xb6, 0xdf, 0xef, 0xf1, 0x5b,