pub struct KeyMaterial<const KEY_LEN: usize> { /* private fields */ }Expand description
A wrapper for holding bytes-like key material (symmetric keys or seeds) which aims to apply a strict typing system to prevent many kinds of mis-use mistakes. The capacity of the internal buffer can be set at compile-time via the <KEY_LEN> param.
Implementations§
Source§impl<const KEY_LEN: usize> KeyMaterial<KEY_LEN>
impl<const KEY_LEN: usize> KeyMaterial<KEY_LEN>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty instance (key_len = 0, key_type = Zeroized).
If you want a properly populated instance, use KeyMaterial::from_rng.
Sourcepub fn from_rng(rng: &mut impl RNG) -> Result<Self, KeyMaterialError>
pub fn from_rng(rng: &mut impl RNG) -> Result<Self, KeyMaterialError>
Creates a new instance of KeyMaterial containing random bytes from the provided random number generator.
Sourcepub fn from_bytes(source: &[u8]) -> Result<Self, KeyMaterialError>
pub fn from_bytes(source: &[u8]) -> Result<Self, KeyMaterialError>
Constructor.
Loads the provided data into a new KeyMaterial of type KeyType::Unknown.
It will detect if you give it all-zero source data and set the key type to KeyType::Zeroized instead.
Sourcepub fn from_bytes_as_type(
source: &[u8],
key_type: KeyType,
) -> Result<Self, KeyMaterialError>
pub fn from_bytes_as_type( source: &[u8], key_type: KeyType, ) -> Result<Self, KeyMaterialError>
Constructor.
Loads the provided data into a new KeyMaterial of the specified type.
This is discouraged unless the caller knows the provenance of the data, such as loading it
from a cryptographic private key file.
It will detect if you give it all-zero source data and set the key type to KeyType::Zeroized instead.
Will set the SecurityStrength automatically according to the following rules:
- If
KeyTypeisKeyType::ZeroizedorKeyType::Unknownthen it will beSecurityStrength::None. - Otherwise it will set it based on the length of the provided source bytes.
Sourcepub fn from_key(other: &impl KeyMaterialTrait) -> Result<Self, KeyMaterialError>
pub fn from_key(other: &impl KeyMaterialTrait) -> Result<Self, KeyMaterialError>
Copy constructor
Trait Implementations§
Source§impl<const KEY_LEN: usize> Clone for KeyMaterial<KEY_LEN>
impl<const KEY_LEN: usize> Clone for KeyMaterial<KEY_LEN>
Source§fn clone(&self) -> KeyMaterial<KEY_LEN>
fn clone(&self) -> KeyMaterial<KEY_LEN>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<const KEY_LEN: usize> Debug for KeyMaterial<KEY_LEN>
Block accidental logging of the internal key material buffer.
impl<const KEY_LEN: usize> Debug for KeyMaterial<KEY_LEN>
Block accidental logging of the internal key material buffer.
Source§impl<const KEY_LEN: usize> Default for KeyMaterial<KEY_LEN>
impl<const KEY_LEN: usize> Default for KeyMaterial<KEY_LEN>
Source§impl<const KEY_LEN: usize> Display for KeyMaterial<KEY_LEN>
Block accidental logging of the internal key material buffer.
impl<const KEY_LEN: usize> Display for KeyMaterial<KEY_LEN>
Block accidental logging of the internal key material buffer.
Source§impl<const KEY_LEN: usize> KeyMaterialTrait for KeyMaterial<KEY_LEN>
impl<const KEY_LEN: usize> KeyMaterialTrait for KeyMaterial<KEY_LEN>
Source§fn set_bytes_as_type(
&mut self,
source: &[u8],
key_type: KeyType,
) -> Result<(), KeyMaterialError>
fn set_bytes_as_type( &mut self, source: &[u8], key_type: KeyType, ) -> Result<(), KeyMaterialError>
Source§fn ref_to_bytes(&self) -> &[u8] ⓘ
fn ref_to_bytes(&self) -> &[u8] ⓘ
Source§fn ref_to_bytes_mut(&mut self) -> Result<&mut [u8], KeyMaterialError>
fn ref_to_bytes_mut(&mut self) -> Result<&mut [u8], KeyMaterialError>
Source§fn capacity(&self) -> usize
fn capacity(&self) -> usize
Source§fn set_key_len(&mut self, key_len: usize) -> Result<(), KeyMaterialError>
fn set_key_len(&mut self, key_len: usize) -> Result<(), KeyMaterialError>
Source§fn set_key_type(&mut self, key_type: KeyType) -> Result<(), KeyMaterialError>
fn set_key_type(&mut self, key_type: KeyType) -> Result<(), KeyMaterialError>
Source§fn security_strength(&self) -> SecurityStrength
fn security_strength(&self) -> SecurityStrength
Source§fn set_security_strength(
&mut self,
strength: SecurityStrength,
) -> Result<(), KeyMaterialError>
fn set_security_strength( &mut self, strength: SecurityStrength, ) -> Result<(), KeyMaterialError>
SecurityStrength of the KeyMaterial. Read moreSource§fn is_full_entropy(&self) -> bool
fn is_full_entropy(&self) -> bool
Source§fn zeroize(&mut self)
fn zeroize(&mut self)
Source§fn equals(&self, other: &dyn KeyMaterialTrait) -> bool
fn equals(&self, other: &dyn KeyMaterialTrait) -> bool
KeyType, SecurityStrength, etc.Source§fn truncate(self, into: &mut dyn KeyMaterialTrait)
fn truncate(self, into: &mut dyn KeyMaterialTrait)
clone() if you intend to make a copy.Source§impl<const KEY_LEN: usize> PartialEq for KeyMaterial<KEY_LEN>
Checks for equality of the key data (using a constant-time comparison), but does not check that
the two keys have the same type.
Therefore, for example, two keys loaded from the same bytes, one with type KeyType::Unknown and
the other with KeyType::MACKey will be considered equal.
impl<const KEY_LEN: usize> PartialEq for KeyMaterial<KEY_LEN>
Checks for equality of the key data (using a constant-time comparison), but does not check that
the two keys have the same type.
Therefore, for example, two keys loaded from the same bytes, one with type KeyType::Unknown and
the other with KeyType::MACKey will be considered equal.