diff options
Diffstat (limited to 'openpgp/src/crypto/s2k.rs')
-rw-r--r-- | openpgp/src/crypto/s2k.rs | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/openpgp/src/crypto/s2k.rs b/openpgp/src/crypto/s2k.rs index 9b7c24d6..d61f7969 100644 --- a/openpgp/src/crypto/s2k.rs +++ b/openpgp/src/crypto/s2k.rs @@ -9,6 +9,7 @@ use crate::Error; use crate::Result; use crate::HashAlgorithm; +use crate::types::SymmetricAlgorithm; use crate::crypto::Password; use crate::crypto::SessionKey; use crate::crypto::hash::Digest; @@ -162,6 +163,24 @@ impl S2K { } } + /// Makes encryption parameters. + /// + /// An `EncryptionParameters` object can be used to encrypt and + /// decrypt secret key material. + pub fn make_encryption_parameters<A>(&self, password: &Password, algo: A) + -> Result<EncryptionParameters> + where + A: Into<Option<SymmetricAlgorithm>>, + { + let algo = algo.into().unwrap_or_default(); + let key = self.derive_key(password, algo.key_size()?)?; + Ok(EncryptionParameters { + algo, + key, + s2k: self.clone(), + }) + } + /// Derives a key of the given size from a password. pub fn derive_key(&self, password: &Password, key_size: usize) -> Result<SessionKey> { @@ -415,6 +434,32 @@ impl Arbitrary for S2K { } } +/// XXX +pub struct EncryptionParameters { + algo: SymmetricAlgorithm, + s2k: S2K, + key: SessionKey, +} + +impl EncryptionParameters { + /// Returns the symmetric algorithm. + pub fn algo(&self) -> SymmetricAlgorithm { + self.algo + } + + /// Returns the S2K parameters. + pub fn s2k(&self) -> &S2K { + &self.s2k + } + + /// Returns the symmetric key derived from the S2K object and the + /// password. + pub fn key(&self) -> &SessionKey { + &self.key + } + +} + #[cfg(test)] mod tests { use super::*; |