From 47557fe350b664982de4587b7265e6495867ffd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolphe=20Br=C3=A9ard?= Date: Sat, 16 Mar 2024 11:22:08 +0100 Subject: [PATCH] Fix the features --- src/ikm.rs | 5 +++-- src/lib.rs | 3 ++- src/scheme.rs | 4 +++- src/storage.rs | 23 ++++++++++++++++------- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/ikm.rs b/src/ikm.rs index bf6dcf4..79c6ca0 100644 --- a/src/ikm.rs +++ b/src/ikm.rs @@ -74,6 +74,7 @@ impl InputKeyMaterial { #[derive(Debug, Default)] pub struct InputKeyMaterialList { pub(crate) ikm_lst: Vec, + #[allow(dead_code)] pub(crate) id_counter: CounterId, } @@ -134,7 +135,7 @@ impl InputKeyMaterialList { crate::storage::decode_ikm_list(s) } - #[cfg(feature = "encryption")] + #[cfg(any(test, feature = "encryption"))] pub(crate) fn get_latest_ikm(&self) -> Result<&InputKeyMaterial> { let now = SystemTime::now(); self.ikm_lst @@ -390,7 +391,7 @@ mod ikm_management { } } -#[cfg(all(test, feature = "encryption"))] +#[cfg(all(test, feature = "encryption", feature = "ikm-management"))] mod encryption { use super::*; diff --git a/src/lib.rs b/src/lib.rs index 367cc05..eb2fff1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +#[cfg(feature = "encryption")] mod canonicalization; #[cfg(feature = "encryption")] mod encryption; @@ -9,7 +10,7 @@ mod ikm; mod kdf; #[cfg(any(feature = "encryption", feature = "ikm-management"))] mod scheme; -#[cfg(feature = "encryption")] +#[cfg(any(feature = "encryption", feature = "ikm-management"))] mod storage; #[cfg(feature = "encryption")] diff --git a/src/scheme.rs b/src/scheme.rs index d8c9f7e..2ea18d1 100644 --- a/src/scheme.rs +++ b/src/scheme.rs @@ -16,14 +16,16 @@ pub enum Scheme { XChaCha20Poly1305WithBlake3 = 1, } -#[cfg(feature = "encryption")] impl Scheme { pub(crate) fn get_ikm_size(&self) -> usize { match self { Scheme::XChaCha20Poly1305WithBlake3 => 32, } } +} +#[cfg(feature = "encryption")] +impl Scheme { pub(crate) fn get_kdf(&self) -> Box { match self { Scheme::XChaCha20Poly1305WithBlake3 => Box::new(blake3::blake3_derive), diff --git a/src/storage.rs b/src/storage.rs index 83356a4..e855304 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -1,9 +1,15 @@ +#[cfg(feature = "encryption")] use crate::encryption::EncryptedData; use crate::error::{Error, Result}; -use crate::ikm::{CounterId, IkmId, InputKeyMaterial, InputKeyMaterialList, IKM_BASE_STRUCT_SIZE}; +#[cfg(feature = "encryption")] +use crate::ikm::IkmId; +#[cfg(feature = "ikm-management")] +use crate::ikm::IKM_BASE_STRUCT_SIZE; +use crate::ikm::{CounterId, InputKeyMaterial, InputKeyMaterialList}; use base64ct::{Base64UrlUnpadded, Encoding}; const STORAGE_SEPARATOR: &str = ":"; +#[cfg(feature = "encryption")] const NB_PARTS: usize = 3; #[inline] @@ -16,6 +22,7 @@ fn decode_data(s: &str) -> Result> { Ok(Base64UrlUnpadded::decode_vec(s)?) } +#[cfg(feature = "ikm-management")] pub(crate) fn encode_ikm_list(ikml: &InputKeyMaterialList) -> Result { let data_size = (ikml.ikm_lst.iter().fold(0, |acc, ikm| { acc + IKM_BASE_STRUCT_SIZE + ikm.scheme.get_ikm_size() @@ -29,6 +36,7 @@ pub(crate) fn encode_ikm_list(ikml: &InputKeyMaterialList) -> Result { Ok(ret) } +#[cfg(feature = "encryption")] pub(crate) fn encode_cipher( ikm_id: IkmId, encrypted_data: &EncryptedData, @@ -69,6 +77,7 @@ pub(crate) fn decode_ikm_list(data: &str) -> Result { }) } +#[cfg(feature = "encryption")] pub(crate) fn decode_cipher(data: &str) -> Result<(IkmId, EncryptedData, Option)> { let mut v: Vec<&str> = data.split(STORAGE_SEPARATOR).collect(); let time_period = if v.len() == NB_PARTS + 1 { @@ -108,10 +117,8 @@ pub(crate) fn decode_cipher(data: &str) -> Result<(IkmId, EncryptedData, Option< Ok((id, encrypted_data, time_period)) } -#[cfg(test)] +#[cfg(all(test, feature = "ikm-management"))] mod ikm_lst { - use crate::InputKeyMaterialList; - const TEST_STR: &str = "BgAAAA:AQAAAAEAAACUAPcqngJ46_HMtJSdIw-WeUtImcCVxOA47n6UIN5K2TbmoVwAAAAANmuEXgAAAAAB:AgAAAAEAAADf7CR8vl_aWOUyfsO0ek0YQr_Yi7L_sJmF2nIt_XOaCzYNal4AAAAAtkBLYAAAAAAA:AwAAAAEAAAAMoNIW9gIGkzegUDEsU3N1Rf_Zz0OMuylUSiQjUzLXqzY0MmAAAAAANsk0iwEAAAAA:BAAAAAEAAABbwRrMz3x3DkfOEFg1BHfLLRHoNqg6d_xGWwdh48hH8rZm9mEAAAAANjy9YwAAAAAA:BQAAAAEAAAA2LwnTgDUF7qn7dy79VA24JSSgo6vllAtU5zmhrxNJu7YIz4sBAAAANoUMjgEAAAAB:BgAAAAEAAAAn0Vqe2f9YRXBt6xVYaeSLs0Gf0S0_5B-hk-a2b0rhlraCJbwAAAAAtlErjAEAAAAA"; const TEST_CTN_0: &[u8] = &[ 0x94, 0x00, 0xf7, 0x2a, 0x9e, 0x02, 0x78, 0xeb, 0xf1, 0xcc, 0xb4, 0x94, 0x9d, 0x23, 0x0f, @@ -154,6 +161,7 @@ mod ikm_lst { } #[test] + #[cfg(feature = "ikm-management")] fn encode() { use std::time::{Duration, SystemTime}; let bytes_to_system_time = |ts: u64| { @@ -161,7 +169,7 @@ mod ikm_lst { .checked_add(Duration::from_secs(ts)) .unwrap() }; - let mut lst = InputKeyMaterialList::new(); + let mut lst = crate::InputKeyMaterialList::new(); let _ = lst.add_ikm(); lst.ikm_lst[0].content = TEST_CTN_0.to_vec(); lst.ikm_lst[0].created_at = bytes_to_system_time(1554114102); @@ -232,8 +240,9 @@ mod ikm_lst { } #[test] + #[cfg(feature = "ikm-management")] fn encode_decode() { - let mut lst = InputKeyMaterialList::new(); + let mut lst = crate::InputKeyMaterialList::new(); let _ = lst.add_ikm(); let _ = lst.add_ikm(); let _ = lst.add_ikm(); @@ -293,7 +302,7 @@ mod ikm_lst { } } -#[cfg(test)] +#[cfg(all(test, feature = "encryption"))] mod ciphers { use crate::ikm::IkmId; use crate::storage::EncryptedData;