Add more documentation
This commit is contained in:
parent
a6a7e31459
commit
51d543a064
2 changed files with 83 additions and 0 deletions
82
src/ikm.rs
82
src/ikm.rs
|
@ -27,26 +27,31 @@ pub struct InputKeyMaterial {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl InputKeyMaterial {
|
impl InputKeyMaterial {
|
||||||
|
/// Returns the IKM's identifier.
|
||||||
#[cfg(feature = "ikm-management")]
|
#[cfg(feature = "ikm-management")]
|
||||||
pub fn get_id(&self) -> IkmId {
|
pub fn get_id(&self) -> IkmId {
|
||||||
self.id
|
self.id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the IKM's scheme.
|
||||||
#[cfg(feature = "ikm-management")]
|
#[cfg(feature = "ikm-management")]
|
||||||
pub fn get_scheme(&self) -> Scheme {
|
pub fn get_scheme(&self) -> Scheme {
|
||||||
self.scheme
|
self.scheme
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the date before which the IKM must not be used to encrypt data.
|
||||||
#[cfg(feature = "ikm-management")]
|
#[cfg(feature = "ikm-management")]
|
||||||
pub fn get_not_before(&self) -> SystemTime {
|
pub fn get_not_before(&self) -> SystemTime {
|
||||||
self.not_before
|
self.not_before
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the date after which the IKM must not be used to encrypt data.
|
||||||
#[cfg(feature = "ikm-management")]
|
#[cfg(feature = "ikm-management")]
|
||||||
pub fn get_not_after(&self) -> SystemTime {
|
pub fn get_not_after(&self) -> SystemTime {
|
||||||
self.not_after
|
self.not_after
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Check whether or not the IKM has been revoked.
|
||||||
#[cfg(feature = "ikm-management")]
|
#[cfg(feature = "ikm-management")]
|
||||||
pub fn is_revoked(&self) -> bool {
|
pub fn is_revoked(&self) -> bool {
|
||||||
self.is_revoked
|
self.is_revoked
|
||||||
|
@ -178,11 +183,29 @@ pub struct InputKeyMaterialList {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl InputKeyMaterialList {
|
impl InputKeyMaterialList {
|
||||||
|
/// Create a new empty IKM list.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// let ikml = coffio::InputKeyMaterialList::new();
|
||||||
|
/// ```
|
||||||
#[cfg(feature = "ikm-management")]
|
#[cfg(feature = "ikm-management")]
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self::default()
|
Self::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Add a new IKM to the list. The `not_before` field will be set to the current timestamp and
|
||||||
|
/// the `not_after` will be set to the current timestamp plus the value of
|
||||||
|
/// [DEFAULT_IKM_DURATION][crate::DEFAULT_IKM_DURATION].
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// let mut ikml = coffio::InputKeyMaterialList::new();
|
||||||
|
/// let _ = ikml.add_ikm()?;
|
||||||
|
/// # Ok::<(), coffio::Error>(())
|
||||||
|
/// ```
|
||||||
#[cfg(feature = "ikm-management")]
|
#[cfg(feature = "ikm-management")]
|
||||||
pub fn add_ikm(&mut self) -> Result<()> {
|
pub fn add_ikm(&mut self) -> Result<()> {
|
||||||
let not_before = SystemTime::now();
|
let not_before = SystemTime::now();
|
||||||
|
@ -190,6 +213,24 @@ impl InputKeyMaterialList {
|
||||||
self.add_custom_ikm(crate::DEFAULT_SCHEME, not_before, not_after)
|
self.add_custom_ikm(crate::DEFAULT_SCHEME, not_before, not_after)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Add a new IKM with a specified scheme, `not_before` and `not_after` fields.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use coffio::{InputKeyMaterialList, Scheme};
|
||||||
|
/// use std::time::{Duration, SystemTime};
|
||||||
|
///
|
||||||
|
/// let mut ikml = InputKeyMaterialList::new();
|
||||||
|
/// let not_before = SystemTime::now();
|
||||||
|
/// let not_after = not_before + Duration::from_secs(315_569_252);
|
||||||
|
/// let _ = ikml.add_custom_ikm(
|
||||||
|
/// Scheme::XChaCha20Poly1305WithBlake3,
|
||||||
|
/// not_before,
|
||||||
|
/// not_after,
|
||||||
|
/// );
|
||||||
|
/// # Ok::<(), coffio::Error>(())
|
||||||
|
/// ```
|
||||||
#[cfg(feature = "ikm-management")]
|
#[cfg(feature = "ikm-management")]
|
||||||
pub fn add_custom_ikm(
|
pub fn add_custom_ikm(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
@ -212,11 +253,33 @@ impl InputKeyMaterialList {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Delete the specified IKM from the list.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// let mut ikml = coffio::InputKeyMaterialList::new();
|
||||||
|
/// let _ = ikml.add_ikm()?;
|
||||||
|
/// let ikm_id = ikml[0].get_id();
|
||||||
|
/// ikml.delete_ikm(ikm_id);
|
||||||
|
/// # Ok::<(), coffio::Error>(())
|
||||||
|
/// ```
|
||||||
#[cfg(feature = "ikm-management")]
|
#[cfg(feature = "ikm-management")]
|
||||||
pub fn delete_ikm(&mut self, id: IkmId) {
|
pub fn delete_ikm(&mut self, id: IkmId) {
|
||||||
self.ikm_lst.retain(|ikm| ikm.id != id);
|
self.ikm_lst.retain(|ikm| ikm.id != id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Revoke the specified IKM from the list.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// let mut ikml = coffio::InputKeyMaterialList::new();
|
||||||
|
/// let _ = ikml.add_ikm()?;
|
||||||
|
/// let ikm_id = ikml[0].get_id();
|
||||||
|
/// ikml.revoke_ikm(ikm_id);
|
||||||
|
/// # Ok::<(), coffio::Error>(())
|
||||||
|
/// ```
|
||||||
#[cfg(feature = "ikm-management")]
|
#[cfg(feature = "ikm-management")]
|
||||||
pub fn revoke_ikm(&mut self, id: IkmId) -> Result<()> {
|
pub fn revoke_ikm(&mut self, id: IkmId) -> Result<()> {
|
||||||
let ikm = self
|
let ikm = self
|
||||||
|
@ -228,11 +291,30 @@ impl InputKeyMaterialList {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Export the IKM list to a displayable string.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// let mut ikml = coffio::InputKeyMaterialList::new();
|
||||||
|
/// let _ = ikml.add_ikm()?;
|
||||||
|
/// let exported_ikml = ikml.export()?;
|
||||||
|
/// # Ok::<(), coffio::Error>(())
|
||||||
|
/// ```
|
||||||
#[cfg(feature = "ikm-management")]
|
#[cfg(feature = "ikm-management")]
|
||||||
pub fn export(&self) -> Result<String> {
|
pub fn export(&self) -> Result<String> {
|
||||||
crate::storage::encode_ikm_list(self)
|
crate::storage::encode_ikm_list(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Import an IKM list.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// let stored_ikml = "AQAAAA:AQAAAAEAAAC_vYEw1ujVG5i-CtoPYSzik_6xaAq59odjPm5ij01-e6zz4mUAAAAALJGBiwAAAAAA";
|
||||||
|
/// let mut ikml = coffio::InputKeyMaterialList::import(stored_ikml)?;
|
||||||
|
/// # Ok::<(), coffio::Error>(())
|
||||||
|
/// ```
|
||||||
pub fn import(s: &str) -> Result<Self> {
|
pub fn import(s: &str) -> Result<Self> {
|
||||||
crate::storage::decode_ikm_list(s)
|
crate::storage::decode_ikm_list(s)
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,5 +45,6 @@ pub const DEFAULT_IKM_DURATION: u64 = 315_569_252;
|
||||||
/// [tropical_year]: https://en.wikipedia.org/wiki/Tropical_year
|
/// [tropical_year]: https://en.wikipedia.org/wiki/Tropical_year
|
||||||
#[cfg(feature = "encryption")]
|
#[cfg(feature = "encryption")]
|
||||||
pub const DEFAULT_KEY_CTX_PERIODICITY: u64 = 31_556_925;
|
pub const DEFAULT_KEY_CTX_PERIODICITY: u64 = 31_556_925;
|
||||||
|
/// Default scheme used when adding a new IKM.
|
||||||
#[cfg(feature = "ikm-management")]
|
#[cfg(feature = "ikm-management")]
|
||||||
const DEFAULT_SCHEME: Scheme = Scheme::XChaCha20Poly1305WithBlake3;
|
const DEFAULT_SCHEME: Scheme = Scheme::XChaCha20Poly1305WithBlake3;
|
||||||
|
|
Loading…
Reference in a new issue