summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2023-11-14 17:55:51 +0100
committerJustus Winter <justus@sequoia-pgp.org>2023-11-14 17:55:51 +0100
commit54318d2d26d4a63cd62d980594ef2f122ac33104 (patch)
treeba3eac3a9dc82a035eae3b94d35651453864ba61
parente3858cead04dad2985a64decf2b0bc530a278e99 (diff)
openpgp: Add a type that combines S2K, cipher, and session key.
- I'm happy to wordsmith the name, EncryptionParameters isn't quite ideal...
-rw-r--r--openpgp/src/crypto/mod.rs2
-rw-r--r--openpgp/src/crypto/s2k.rs45
2 files changed, 46 insertions, 1 deletions
diff --git a/openpgp/src/crypto/mod.rs b/openpgp/src/crypto/mod.rs
index 1c136102..633bb2b2 100644
--- a/openpgp/src/crypto/mod.rs
+++ b/openpgp/src/crypto/mod.rs
@@ -37,7 +37,7 @@ pub mod hash;
pub mod mem;
pub mod mpi;
mod s2k;
-pub use s2k::S2K;
+pub use s2k::{EncryptionParameters, S2K};
pub(crate) mod symmetric;
#[cfg(test)]
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::*;