2024-02-17 20:47:07 +01:00
|
|
|
use crate::error::{Error, Result};
|
2024-02-25 12:03:06 +01:00
|
|
|
use crate::scheme::{Scheme, SchemeSerializeType};
|
2024-02-14 23:11:00 +01:00
|
|
|
use std::time::{Duration, SystemTime};
|
|
|
|
|
2024-03-02 11:00:59 +01:00
|
|
|
pub(crate) const IKM_BASE_STRUCT_SIZE: usize = 25;
|
2024-02-14 23:11:00 +01:00
|
|
|
|
2024-02-25 11:57:04 +01:00
|
|
|
pub(crate) type CounterId = u32;
|
2024-02-25 18:08:00 +01:00
|
|
|
pub type IkmId = u32;
|
2024-02-25 11:53:52 +01:00
|
|
|
|
2024-02-14 23:11:00 +01:00
|
|
|
#[derive(Debug)]
|
2024-02-25 18:08:00 +01:00
|
|
|
pub struct InputKeyMaterial {
|
|
|
|
pub id: IkmId,
|
|
|
|
pub scheme: Scheme,
|
2024-03-02 11:00:59 +01:00
|
|
|
pub(crate) content: Vec<u8>,
|
2024-02-25 18:08:00 +01:00
|
|
|
pub created_at: SystemTime,
|
|
|
|
pub expire_at: SystemTime,
|
|
|
|
pub is_revoked: bool,
|
2024-02-14 23:11:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl InputKeyMaterial {
|
2024-02-15 12:37:14 +01:00
|
|
|
#[cfg(feature = "ikm-management")]
|
2024-03-02 11:00:59 +01:00
|
|
|
pub(crate) fn as_bytes(&self) -> Result<Vec<u8>> {
|
|
|
|
let mut res = Vec::with_capacity(IKM_BASE_STRUCT_SIZE + self.scheme.get_ikm_size());
|
2024-02-14 23:11:00 +01:00
|
|
|
res.extend_from_slice(&self.id.to_le_bytes());
|
2024-02-25 12:03:06 +01:00
|
|
|
res.extend_from_slice(&(self.scheme as SchemeSerializeType).to_le_bytes());
|
2024-02-14 23:11:00 +01:00
|
|
|
res.extend_from_slice(&self.content);
|
|
|
|
res.extend_from_slice(
|
|
|
|
&self
|
|
|
|
.created_at
|
2024-02-15 10:56:21 +01:00
|
|
|
.duration_since(SystemTime::UNIX_EPOCH)?
|
2024-02-14 23:11:00 +01:00
|
|
|
.as_secs()
|
|
|
|
.to_le_bytes(),
|
|
|
|
);
|
|
|
|
res.extend_from_slice(
|
|
|
|
&self
|
|
|
|
.expire_at
|
2024-02-15 10:56:21 +01:00
|
|
|
.duration_since(SystemTime::UNIX_EPOCH)?
|
2024-02-14 23:11:00 +01:00
|
|
|
.as_secs()
|
|
|
|
.to_le_bytes(),
|
|
|
|
);
|
|
|
|
res.push(self.is_revoked as u8);
|
2024-03-02 11:00:59 +01:00
|
|
|
Ok(res)
|
2024-02-14 23:11:00 +01:00
|
|
|
}
|
|
|
|
|
2024-03-02 11:00:59 +01:00
|
|
|
pub(crate) fn from_bytes(b: &[u8]) -> Result<Self> {
|
|
|
|
if b.len() < IKM_BASE_STRUCT_SIZE {
|
|
|
|
return Err(Error::ParsingEncodedDataInvalidIkmLen(b.len()));
|
|
|
|
}
|
|
|
|
let scheme: Scheme =
|
|
|
|
SchemeSerializeType::from_le_bytes(b[4..8].try_into().unwrap()).try_into()?;
|
|
|
|
let is = scheme.get_ikm_size();
|
|
|
|
if b.len() != IKM_BASE_STRUCT_SIZE + is {
|
|
|
|
return Err(Error::ParsingEncodedDataInvalidIkmLen(b.len()));
|
|
|
|
}
|
2024-02-15 10:56:21 +01:00
|
|
|
Ok(Self {
|
2024-02-25 11:53:52 +01:00
|
|
|
id: IkmId::from_le_bytes(b[0..4].try_into().unwrap()),
|
2024-03-02 11:00:59 +01:00
|
|
|
scheme,
|
|
|
|
content: b[8..8 + is].into(),
|
|
|
|
created_at: InputKeyMaterial::bytes_to_system_time(&b[8 + is..8 + is + 8])?,
|
|
|
|
expire_at: InputKeyMaterial::bytes_to_system_time(&b[8 + is + 8..8 + is + 8 + 8])?,
|
|
|
|
is_revoked: b[8 + is + 8 + 8] != 0,
|
2024-02-15 10:56:21 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-02-17 20:47:07 +01:00
|
|
|
fn bytes_to_system_time(ts_slice: &[u8]) -> Result<SystemTime> {
|
2024-02-15 10:56:21 +01:00
|
|
|
let ts_array: [u8; 8] = ts_slice.try_into().unwrap();
|
|
|
|
let ts = u64::from_le_bytes(ts_array);
|
2024-02-15 11:48:13 +01:00
|
|
|
SystemTime::UNIX_EPOCH
|
2024-02-15 10:56:21 +01:00
|
|
|
.checked_add(Duration::from_secs(ts))
|
2024-02-15 11:48:13 +01:00
|
|
|
.ok_or(Error::SystemTimeReprError(ts))
|
2024-02-14 23:11:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Default)]
|
|
|
|
pub struct InputKeyMaterialList {
|
2024-03-02 11:00:59 +01:00
|
|
|
pub(crate) ikm_lst: Vec<InputKeyMaterial>,
|
|
|
|
pub(crate) id_counter: CounterId,
|
2024-02-14 23:11:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl InputKeyMaterialList {
|
2024-02-15 12:37:14 +01:00
|
|
|
#[cfg(feature = "ikm-management")]
|
2024-02-14 23:11:00 +01:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Self::default()
|
|
|
|
}
|
|
|
|
|
2024-02-15 12:37:14 +01:00
|
|
|
#[cfg(feature = "ikm-management")]
|
2024-02-17 20:47:07 +01:00
|
|
|
pub fn add_ikm(&mut self) -> Result<()> {
|
2024-03-02 11:00:59 +01:00
|
|
|
self.add_custom_ikm(
|
|
|
|
crate::DEFAULT_SCHEME,
|
|
|
|
Duration::from_secs(crate::DEFAULT_IKM_DURATION),
|
|
|
|
)
|
2024-02-14 23:11:00 +01:00
|
|
|
}
|
|
|
|
|
2024-02-15 12:37:14 +01:00
|
|
|
#[cfg(feature = "ikm-management")]
|
2024-03-02 11:00:59 +01:00
|
|
|
pub fn add_custom_ikm(&mut self, scheme: Scheme, duration: Duration) -> Result<()> {
|
|
|
|
let ikm_len = scheme.get_ikm_size();
|
|
|
|
let mut content: Vec<u8> = vec![0; ikm_len];
|
|
|
|
getrandom::getrandom(content.as_mut_slice())?;
|
2024-02-14 23:11:00 +01:00
|
|
|
let created_at = SystemTime::now();
|
|
|
|
self.id_counter += 1;
|
|
|
|
self.ikm_lst.push(InputKeyMaterial {
|
|
|
|
id: self.id_counter,
|
2024-03-02 11:00:59 +01:00
|
|
|
scheme,
|
2024-02-14 23:11:00 +01:00
|
|
|
created_at,
|
|
|
|
expire_at: created_at + duration,
|
|
|
|
is_revoked: false,
|
|
|
|
content,
|
|
|
|
});
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-02-25 15:28:08 +01:00
|
|
|
#[cfg(feature = "ikm-management")]
|
|
|
|
pub fn delete_ikm(&mut self, id: IkmId) {
|
|
|
|
self.ikm_lst.retain(|ikm| ikm.id != id);
|
|
|
|
}
|
|
|
|
|
2024-02-25 15:23:04 +01:00
|
|
|
#[cfg(feature = "ikm-management")]
|
|
|
|
pub fn revoke_ikm(&mut self, id: IkmId) -> Result<()> {
|
|
|
|
let ikm = self
|
|
|
|
.ikm_lst
|
|
|
|
.iter_mut()
|
|
|
|
.find(|ikm| ikm.id == id)
|
|
|
|
.ok_or(Error::IkmNotFound(id))?;
|
|
|
|
ikm.is_revoked = true;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-02-15 12:37:14 +01:00
|
|
|
#[cfg(feature = "ikm-management")]
|
2024-02-17 20:47:07 +01:00
|
|
|
pub fn export(&self) -> Result<String> {
|
2024-03-02 11:00:59 +01:00
|
|
|
crate::storage::encode_ikm_list(self)
|
2024-02-14 23:11:00 +01:00
|
|
|
}
|
|
|
|
|
2024-02-17 20:47:07 +01:00
|
|
|
pub fn import(s: &str) -> Result<Self> {
|
2024-03-02 11:00:59 +01:00
|
|
|
crate::storage::decode_ikm_list(s)
|
2024-02-14 23:11:00 +01:00
|
|
|
}
|
2024-02-15 18:37:59 +01:00
|
|
|
|
|
|
|
#[cfg(feature = "encryption")]
|
2024-02-17 20:47:07 +01:00
|
|
|
pub(crate) fn get_latest_ikm(&self) -> Result<&InputKeyMaterial> {
|
2024-02-15 18:37:59 +01:00
|
|
|
self.ikm_lst
|
|
|
|
.iter()
|
|
|
|
.rev()
|
|
|
|
.find(|&ikm| !ikm.is_revoked && ikm.created_at < SystemTime::now())
|
|
|
|
.ok_or(Error::IkmNoneAvailable)
|
|
|
|
}
|
2024-02-15 18:38:24 +01:00
|
|
|
|
|
|
|
#[cfg(feature = "encryption")]
|
2024-02-25 11:53:52 +01:00
|
|
|
pub(crate) fn get_ikm_by_id(&self, id: IkmId) -> Result<&InputKeyMaterial> {
|
2024-02-15 18:38:24 +01:00
|
|
|
self.ikm_lst
|
|
|
|
.iter()
|
|
|
|
.find(|&ikm| ikm.id == id)
|
|
|
|
.ok_or(Error::IkmNotFound(id))
|
|
|
|
}
|
2024-02-14 23:11:00 +01:00
|
|
|
}
|
|
|
|
|
2024-02-25 18:14:13 +01:00
|
|
|
#[cfg(feature = "ikm-management")]
|
|
|
|
impl std::ops::Deref for InputKeyMaterialList {
|
|
|
|
type Target = Vec<InputKeyMaterial>;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.ikm_lst
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-14 23:11:00 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2024-02-15 12:37:14 +01:00
|
|
|
#[cfg(feature = "ikm-management")]
|
2024-02-14 23:11:00 +01:00
|
|
|
fn round_time(t: SystemTime) -> SystemTime {
|
|
|
|
let secs = t.duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();
|
|
|
|
SystemTime::UNIX_EPOCH
|
|
|
|
.checked_add(Duration::from_secs(secs))
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-02-15 12:37:14 +01:00
|
|
|
#[cfg(feature = "ikm-management")]
|
2024-02-14 23:11:00 +01:00
|
|
|
fn gen_ikm_list() {
|
|
|
|
let mut lst = InputKeyMaterialList::new();
|
|
|
|
assert_eq!(lst.id_counter, 0);
|
|
|
|
assert_eq!(lst.ikm_lst.len(), 0);
|
|
|
|
|
|
|
|
let res = lst.add_ikm();
|
2024-03-02 11:05:56 +01:00
|
|
|
assert!(res.is_ok(), "res: {res:?}");
|
2024-02-14 23:11:00 +01:00
|
|
|
assert_eq!(lst.id_counter, 1);
|
|
|
|
assert_eq!(lst.ikm_lst.len(), 1);
|
|
|
|
assert!(lst.ikm_lst.first().is_some());
|
|
|
|
let el = lst.ikm_lst.first().unwrap();
|
|
|
|
assert_eq!(el.id, 1);
|
|
|
|
assert_eq!(el.is_revoked, false);
|
|
|
|
|
2024-03-02 11:00:59 +01:00
|
|
|
let res = lst.add_custom_ikm(
|
|
|
|
Scheme::XChaCha20Poly1305WithBlake3,
|
|
|
|
Duration::from_secs(crate::DEFAULT_IKM_DURATION),
|
|
|
|
);
|
2024-03-02 11:05:56 +01:00
|
|
|
assert!(res.is_ok(), "res: {res:?}");
|
2024-02-14 23:11:00 +01:00
|
|
|
assert_eq!(lst.id_counter, 2);
|
|
|
|
assert_eq!(lst.ikm_lst.len(), 2);
|
|
|
|
|
|
|
|
let res = lst.add_ikm();
|
2024-03-02 11:05:56 +01:00
|
|
|
assert!(res.is_ok(), "res: {res:?}");
|
2024-02-14 23:11:00 +01:00
|
|
|
assert_eq!(lst.id_counter, 3);
|
|
|
|
assert_eq!(lst.ikm_lst.len(), 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-02-15 12:37:14 +01:00
|
|
|
#[cfg(feature = "ikm-management")]
|
2024-02-14 23:11:00 +01:00
|
|
|
fn export_empty() {
|
|
|
|
let lst = InputKeyMaterialList::new();
|
|
|
|
assert_eq!(lst.id_counter, 0);
|
|
|
|
assert_eq!(lst.ikm_lst.len(), 0);
|
|
|
|
|
2024-02-15 10:56:21 +01:00
|
|
|
let res = lst.export();
|
2024-03-02 11:05:56 +01:00
|
|
|
assert!(res.is_ok(), "res: {res:?}");
|
2024-02-15 10:56:21 +01:00
|
|
|
let s = res.unwrap();
|
2024-02-14 23:11:00 +01:00
|
|
|
assert_eq!(&s, "AAAAAA");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-02-15 12:37:14 +01:00
|
|
|
#[cfg(feature = "ikm-management")]
|
2024-02-14 23:11:00 +01:00
|
|
|
fn export() {
|
|
|
|
let mut lst = InputKeyMaterialList::new();
|
|
|
|
let _ = lst.add_ikm();
|
|
|
|
|
2024-02-15 10:56:21 +01:00
|
|
|
let res = lst.export();
|
2024-03-02 11:05:56 +01:00
|
|
|
assert!(res.is_ok(), "res: {res:?}");
|
2024-02-15 10:56:21 +01:00
|
|
|
let s = res.unwrap();
|
2024-03-02 11:00:59 +01:00
|
|
|
assert_eq!(s.len(), 83);
|
2024-02-14 23:11:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn import() {
|
2024-02-15 10:00:06 +01:00
|
|
|
let s =
|
2024-03-02 11:00:59 +01:00
|
|
|
"AQAAAA:AQAAAAEAAAC_vYEw1ujVG5i-CtoPYSzik_6xaAq59odjPm5ij01-e6zz4mUAAAAALJGBiwAAAAAA";
|
2024-02-14 23:11:00 +01:00
|
|
|
let res = InputKeyMaterialList::import(s);
|
2024-03-02 11:05:56 +01:00
|
|
|
assert!(res.is_ok(), "res: {res:?}");
|
2024-02-14 23:11:00 +01:00
|
|
|
let lst = res.unwrap();
|
|
|
|
assert_eq!(lst.id_counter, 1);
|
|
|
|
assert_eq!(lst.ikm_lst.len(), 1);
|
|
|
|
let ikm = lst.ikm_lst.first().unwrap();
|
|
|
|
assert_eq!(ikm.id, 1);
|
2024-02-15 10:00:06 +01:00
|
|
|
assert_eq!(ikm.scheme, Scheme::XChaCha20Poly1305WithBlake3);
|
2024-02-14 23:11:00 +01:00
|
|
|
assert_eq!(
|
|
|
|
ikm.content,
|
|
|
|
[
|
2024-03-02 11:00:59 +01:00
|
|
|
191, 189, 129, 48, 214, 232, 213, 27, 152, 190, 10, 218, 15, 97, 44, 226, 147, 254,
|
|
|
|
177, 104, 10, 185, 246, 135, 99, 62, 110, 98, 143, 77, 126, 123
|
2024-02-14 23:11:00 +01:00
|
|
|
]
|
|
|
|
);
|
|
|
|
assert_eq!(ikm.is_revoked, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-02-15 12:37:14 +01:00
|
|
|
#[cfg(feature = "ikm-management")]
|
2024-02-14 23:11:00 +01:00
|
|
|
fn export_import_empty() {
|
|
|
|
let lst = InputKeyMaterialList::new();
|
|
|
|
|
2024-02-15 10:56:21 +01:00
|
|
|
let res = lst.export();
|
2024-03-02 11:05:56 +01:00
|
|
|
assert!(res.is_ok(), "res: {res:?}");
|
2024-02-15 10:56:21 +01:00
|
|
|
let s = res.unwrap();
|
|
|
|
|
2024-02-14 23:11:00 +01:00
|
|
|
let res = InputKeyMaterialList::import(&s);
|
2024-03-02 11:05:56 +01:00
|
|
|
assert!(res.is_ok(), "res: {res:?}");
|
2024-02-14 23:11:00 +01:00
|
|
|
let lst_bis = res.unwrap();
|
|
|
|
assert_eq!(lst_bis.id_counter, lst.id_counter);
|
|
|
|
assert_eq!(lst_bis.id_counter, 0);
|
|
|
|
assert_eq!(lst_bis.ikm_lst.len(), lst.ikm_lst.len());
|
|
|
|
assert_eq!(lst_bis.ikm_lst.len(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-02-15 12:37:14 +01:00
|
|
|
#[cfg(feature = "ikm-management")]
|
2024-02-14 23:11:00 +01:00
|
|
|
fn export_import() {
|
|
|
|
let mut lst = InputKeyMaterialList::new();
|
|
|
|
for _ in 0..10 {
|
|
|
|
let _ = lst.add_ikm();
|
|
|
|
}
|
|
|
|
|
2024-02-15 10:56:21 +01:00
|
|
|
let res = lst.export();
|
2024-03-02 11:05:56 +01:00
|
|
|
assert!(res.is_ok(), "res: {res:?}");
|
2024-02-15 10:56:21 +01:00
|
|
|
let s = res.unwrap();
|
|
|
|
|
2024-02-14 23:11:00 +01:00
|
|
|
let res = InputKeyMaterialList::import(&s);
|
2024-03-02 11:05:56 +01:00
|
|
|
assert!(res.is_ok(), "res: {res:?}");
|
2024-02-14 23:11:00 +01:00
|
|
|
let lst_bis = res.unwrap();
|
|
|
|
assert_eq!(lst_bis.id_counter, lst.id_counter);
|
|
|
|
assert_eq!(lst_bis.id_counter, 10);
|
|
|
|
assert_eq!(lst_bis.ikm_lst.len(), lst.ikm_lst.len());
|
|
|
|
assert_eq!(lst_bis.ikm_lst.len(), 10);
|
|
|
|
|
|
|
|
for i in 0..10 {
|
|
|
|
let el = &lst.ikm_lst[i];
|
|
|
|
let el_bis = &lst_bis.ikm_lst[i];
|
|
|
|
assert_eq!(el_bis.id, el.id);
|
|
|
|
assert_eq!(el_bis.content, el.content);
|
|
|
|
assert_eq!(el_bis.created_at, round_time(el.created_at));
|
|
|
|
assert_eq!(el_bis.expire_at, round_time(el.expire_at));
|
|
|
|
assert_eq!(el_bis.is_revoked, el.is_revoked);
|
|
|
|
}
|
|
|
|
}
|
2024-02-15 18:37:59 +01:00
|
|
|
|
2024-02-25 15:28:08 +01:00
|
|
|
#[test]
|
|
|
|
#[cfg(feature = "ikm-management")]
|
|
|
|
fn delete_ikm() {
|
|
|
|
let mut lst = InputKeyMaterialList::new();
|
|
|
|
let _ = lst.add_ikm();
|
|
|
|
let _ = lst.add_ikm();
|
|
|
|
|
|
|
|
let latest_ikm = lst.get_latest_ikm().unwrap();
|
|
|
|
assert_eq!(latest_ikm.id, 2);
|
|
|
|
|
|
|
|
lst.delete_ikm(2);
|
|
|
|
let latest_ikm = lst.get_latest_ikm().unwrap();
|
|
|
|
assert_eq!(latest_ikm.id, 1);
|
|
|
|
|
|
|
|
lst.delete_ikm(1);
|
|
|
|
let res = lst.get_latest_ikm();
|
|
|
|
assert!(res.is_err());
|
|
|
|
}
|
|
|
|
|
2024-02-25 15:23:04 +01:00
|
|
|
#[test]
|
|
|
|
#[cfg(feature = "ikm-management")]
|
|
|
|
fn revoke_ikm() {
|
|
|
|
let mut lst = InputKeyMaterialList::new();
|
|
|
|
let _ = lst.add_ikm();
|
|
|
|
let _ = lst.add_ikm();
|
|
|
|
|
|
|
|
let latest_ikm = lst.get_latest_ikm().unwrap();
|
|
|
|
assert_eq!(latest_ikm.id, 2);
|
|
|
|
|
|
|
|
let _ = lst.revoke_ikm(2);
|
|
|
|
let latest_ikm = lst.get_latest_ikm().unwrap();
|
|
|
|
assert_eq!(latest_ikm.id, 1);
|
|
|
|
|
|
|
|
let _ = lst.revoke_ikm(1);
|
|
|
|
let res = lst.get_latest_ikm();
|
|
|
|
assert!(res.is_err());
|
|
|
|
}
|
|
|
|
|
2024-02-25 18:14:13 +01:00
|
|
|
#[test]
|
|
|
|
#[cfg(feature = "ikm-management")]
|
|
|
|
fn iterate() {
|
|
|
|
let mut lst = InputKeyMaterialList::new();
|
|
|
|
for _ in 0..10 {
|
|
|
|
let _ = lst.add_ikm();
|
|
|
|
}
|
|
|
|
let mut id = 1;
|
|
|
|
for ikm in lst.iter() {
|
|
|
|
assert_eq!(id, ikm.id);
|
|
|
|
id += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-15 18:37:59 +01:00
|
|
|
#[test]
|
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
fn get_latest_ikm() {
|
|
|
|
let mut lst = InputKeyMaterialList::new();
|
|
|
|
let _ = lst.add_ikm();
|
|
|
|
let _ = lst.add_ikm();
|
2024-03-02 11:00:59 +01:00
|
|
|
let _ = lst.add_custom_ikm(
|
|
|
|
Scheme::XChaCha20Poly1305WithBlake3,
|
|
|
|
Duration::from_secs(crate::DEFAULT_IKM_DURATION),
|
|
|
|
);
|
2024-02-15 18:37:59 +01:00
|
|
|
let res = lst.get_latest_ikm();
|
2024-03-02 11:05:56 +01:00
|
|
|
assert!(res.is_ok(), "res: {res:?}");
|
2024-02-15 18:37:59 +01:00
|
|
|
let latest_ikm = res.unwrap();
|
|
|
|
assert_eq!(latest_ikm.id, 3);
|
2024-03-02 11:00:59 +01:00
|
|
|
assert_eq!(latest_ikm.scheme, Scheme::XChaCha20Poly1305WithBlake3);
|
|
|
|
assert_eq!(latest_ikm.content.len(), 32);
|
2024-02-15 18:37:59 +01:00
|
|
|
}
|
2024-02-15 18:38:24 +01:00
|
|
|
|
2024-02-15 19:02:39 +01:00
|
|
|
#[test]
|
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
fn get_latest_ikm_empty() {
|
2024-02-17 16:49:50 +01:00
|
|
|
let lst = InputKeyMaterialList::new();
|
2024-02-15 19:02:39 +01:00
|
|
|
let res = lst.get_latest_ikm();
|
|
|
|
assert!(res.is_err());
|
|
|
|
}
|
|
|
|
|
2024-02-15 18:38:24 +01:00
|
|
|
#[test]
|
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
fn get_ikm_by_id() {
|
|
|
|
let mut lst = InputKeyMaterialList::new();
|
|
|
|
let _ = lst.add_ikm();
|
|
|
|
let _ = lst.add_ikm();
|
|
|
|
let _ = lst.add_ikm();
|
|
|
|
for i in 1..=3 {
|
|
|
|
let res = lst.get_ikm_by_id(i);
|
2024-03-02 11:05:56 +01:00
|
|
|
assert!(res.is_ok(), "res: {res:?}");
|
2024-02-15 18:38:24 +01:00
|
|
|
let latest_ikm = res.unwrap();
|
|
|
|
assert_eq!(latest_ikm.id, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(feature = "encryption")]
|
|
|
|
fn get_ikm_by_id_noexists() {
|
|
|
|
let mut lst = InputKeyMaterialList::new();
|
|
|
|
let _ = lst.add_ikm();
|
|
|
|
let _ = lst.add_ikm();
|
|
|
|
let _ = lst.add_ikm();
|
|
|
|
let res = lst.get_ikm_by_id(42);
|
|
|
|
assert!(res.is_err());
|
|
|
|
}
|
2024-02-14 23:11:00 +01:00
|
|
|
}
|