Fix the features

This commit is contained in:
Rodolphe Bréard 2024-03-16 11:22:08 +01:00
parent 94b1809ffa
commit 47557fe350
4 changed files with 24 additions and 11 deletions

View file

@ -74,6 +74,7 @@ impl InputKeyMaterial {
#[derive(Debug, Default)] #[derive(Debug, Default)]
pub struct InputKeyMaterialList { pub struct InputKeyMaterialList {
pub(crate) ikm_lst: Vec<InputKeyMaterial>, pub(crate) ikm_lst: Vec<InputKeyMaterial>,
#[allow(dead_code)]
pub(crate) id_counter: CounterId, pub(crate) id_counter: CounterId,
} }
@ -134,7 +135,7 @@ impl InputKeyMaterialList {
crate::storage::decode_ikm_list(s) crate::storage::decode_ikm_list(s)
} }
#[cfg(feature = "encryption")] #[cfg(any(test, feature = "encryption"))]
pub(crate) fn get_latest_ikm(&self) -> Result<&InputKeyMaterial> { pub(crate) fn get_latest_ikm(&self) -> Result<&InputKeyMaterial> {
let now = SystemTime::now(); let now = SystemTime::now();
self.ikm_lst 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 { mod encryption {
use super::*; use super::*;

View file

@ -1,3 +1,4 @@
#[cfg(feature = "encryption")]
mod canonicalization; mod canonicalization;
#[cfg(feature = "encryption")] #[cfg(feature = "encryption")]
mod encryption; mod encryption;
@ -9,7 +10,7 @@ mod ikm;
mod kdf; mod kdf;
#[cfg(any(feature = "encryption", feature = "ikm-management"))] #[cfg(any(feature = "encryption", feature = "ikm-management"))]
mod scheme; mod scheme;
#[cfg(feature = "encryption")] #[cfg(any(feature = "encryption", feature = "ikm-management"))]
mod storage; mod storage;
#[cfg(feature = "encryption")] #[cfg(feature = "encryption")]

View file

@ -16,14 +16,16 @@ pub enum Scheme {
XChaCha20Poly1305WithBlake3 = 1, XChaCha20Poly1305WithBlake3 = 1,
} }
#[cfg(feature = "encryption")]
impl Scheme { impl Scheme {
pub(crate) fn get_ikm_size(&self) -> usize { pub(crate) fn get_ikm_size(&self) -> usize {
match self { match self {
Scheme::XChaCha20Poly1305WithBlake3 => 32, Scheme::XChaCha20Poly1305WithBlake3 => 32,
} }
} }
}
#[cfg(feature = "encryption")]
impl Scheme {
pub(crate) fn get_kdf(&self) -> Box<KdfFunction> { pub(crate) fn get_kdf(&self) -> Box<KdfFunction> {
match self { match self {
Scheme::XChaCha20Poly1305WithBlake3 => Box::new(blake3::blake3_derive), Scheme::XChaCha20Poly1305WithBlake3 => Box::new(blake3::blake3_derive),

View file

@ -1,9 +1,15 @@
#[cfg(feature = "encryption")]
use crate::encryption::EncryptedData; use crate::encryption::EncryptedData;
use crate::error::{Error, Result}; 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}; use base64ct::{Base64UrlUnpadded, Encoding};
const STORAGE_SEPARATOR: &str = ":"; const STORAGE_SEPARATOR: &str = ":";
#[cfg(feature = "encryption")]
const NB_PARTS: usize = 3; const NB_PARTS: usize = 3;
#[inline] #[inline]
@ -16,6 +22,7 @@ fn decode_data(s: &str) -> Result<Vec<u8>> {
Ok(Base64UrlUnpadded::decode_vec(s)?) Ok(Base64UrlUnpadded::decode_vec(s)?)
} }
#[cfg(feature = "ikm-management")]
pub(crate) fn encode_ikm_list(ikml: &InputKeyMaterialList) -> Result<String> { pub(crate) fn encode_ikm_list(ikml: &InputKeyMaterialList) -> Result<String> {
let data_size = (ikml.ikm_lst.iter().fold(0, |acc, ikm| { let data_size = (ikml.ikm_lst.iter().fold(0, |acc, ikm| {
acc + IKM_BASE_STRUCT_SIZE + ikm.scheme.get_ikm_size() acc + IKM_BASE_STRUCT_SIZE + ikm.scheme.get_ikm_size()
@ -29,6 +36,7 @@ pub(crate) fn encode_ikm_list(ikml: &InputKeyMaterialList) -> Result<String> {
Ok(ret) Ok(ret)
} }
#[cfg(feature = "encryption")]
pub(crate) fn encode_cipher( pub(crate) fn encode_cipher(
ikm_id: IkmId, ikm_id: IkmId,
encrypted_data: &EncryptedData, encrypted_data: &EncryptedData,
@ -69,6 +77,7 @@ pub(crate) fn decode_ikm_list(data: &str) -> Result<InputKeyMaterialList> {
}) })
} }
#[cfg(feature = "encryption")]
pub(crate) fn decode_cipher(data: &str) -> Result<(IkmId, EncryptedData, Option<u64>)> { pub(crate) fn decode_cipher(data: &str) -> Result<(IkmId, EncryptedData, Option<u64>)> {
let mut v: Vec<&str> = data.split(STORAGE_SEPARATOR).collect(); let mut v: Vec<&str> = data.split(STORAGE_SEPARATOR).collect();
let time_period = if v.len() == NB_PARTS + 1 { 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)) Ok((id, encrypted_data, time_period))
} }
#[cfg(test)] #[cfg(all(test, feature = "ikm-management"))]
mod ikm_lst { 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_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] = &[ const TEST_CTN_0: &[u8] = &[
0x94, 0x00, 0xf7, 0x2a, 0x9e, 0x02, 0x78, 0xeb, 0xf1, 0xcc, 0xb4, 0x94, 0x9d, 0x23, 0x0f, 0x94, 0x00, 0xf7, 0x2a, 0x9e, 0x02, 0x78, 0xeb, 0xf1, 0xcc, 0xb4, 0x94, 0x9d, 0x23, 0x0f,
@ -154,6 +161,7 @@ mod ikm_lst {
} }
#[test] #[test]
#[cfg(feature = "ikm-management")]
fn encode() { fn encode() {
use std::time::{Duration, SystemTime}; use std::time::{Duration, SystemTime};
let bytes_to_system_time = |ts: u64| { let bytes_to_system_time = |ts: u64| {
@ -161,7 +169,7 @@ mod ikm_lst {
.checked_add(Duration::from_secs(ts)) .checked_add(Duration::from_secs(ts))
.unwrap() .unwrap()
}; };
let mut lst = InputKeyMaterialList::new(); let mut lst = crate::InputKeyMaterialList::new();
let _ = lst.add_ikm(); let _ = lst.add_ikm();
lst.ikm_lst[0].content = TEST_CTN_0.to_vec(); lst.ikm_lst[0].content = TEST_CTN_0.to_vec();
lst.ikm_lst[0].created_at = bytes_to_system_time(1554114102); lst.ikm_lst[0].created_at = bytes_to_system_time(1554114102);
@ -232,8 +240,9 @@ mod ikm_lst {
} }
#[test] #[test]
#[cfg(feature = "ikm-management")]
fn encode_decode() { 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(); 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 { mod ciphers {
use crate::ikm::IkmId; use crate::ikm::IkmId;
use crate::storage::EncryptedData; use crate::storage::EncryptedData;