summaryrefslogtreecommitdiffstats
path: root/openpgp
diff options
context:
space:
mode:
authorNeal H. Walfield <neal@pep.foundation>2019-08-14 22:13:33 +0200
committerNeal H. Walfield <neal@pep.foundation>2019-08-23 19:15:13 +0200
commit56eacae5037826b7d6bf15962065287960d8b894 (patch)
tree8f2249537cbca8904dbe2ecf45542e12404d7be3 /openpgp
parentc678874604b1a48a9aa46276e001ed540a823c06 (diff)
openpgp: Rename SecretKey to SecretKeyMaterial.
- When the `SecretKey` type only refers to the secret key material and not a TPK-like thing, call it `SecretKeyMaterial`.
Diffstat (limited to 'openpgp')
-rw-r--r--openpgp/src/crypto/asymmetric.rs16
-rw-r--r--openpgp/src/crypto/ecdh.rs6
-rw-r--r--openpgp/src/crypto/mpis.rs136
-rw-r--r--openpgp/src/packet/key.rs89
-rw-r--r--openpgp/src/packet/pkesk.rs2
-rw-r--r--openpgp/src/packet/prelude.rs2
-rw-r--r--openpgp/src/packet/signature/mod.rs2
-rw-r--r--openpgp/src/parse/mpis.rs16
-rw-r--r--openpgp/src/parse/parse.rs2
-rw-r--r--openpgp/src/serialize/mod.rs18
-rw-r--r--openpgp/src/serialize/stream.rs4
-rw-r--r--openpgp/src/tpk/keyiter.rs4
12 files changed, 149 insertions, 148 deletions
diff --git a/openpgp/src/crypto/asymmetric.rs b/openpgp/src/crypto/asymmetric.rs
index b970843d..af8ea7f0 100644
--- a/openpgp/src/crypto/asymmetric.rs
+++ b/openpgp/src/crypto/asymmetric.rs
@@ -93,10 +93,10 @@ impl Signer for KeyPair {
{
(RSASign,
&PublicKey::RSA { ref e, ref n },
- &mpis::SecretKey::RSA { ref p, ref q, ref d, .. }) |
+ &mpis::SecretKeyMaterial::RSA { ref p, ref q, ref d, .. }) |
(RSAEncryptSign,
&PublicKey::RSA { ref e, ref n },
- &mpis::SecretKey::RSA { ref p, ref q, ref d, .. }) => {
+ &mpis::SecretKeyMaterial::RSA { ref p, ref q, ref d, .. }) => {
let public = rsa::PublicKey::new(n.value(), e.value())?;
let secret = rsa::PrivateKey::new(d.value(), p.value(),
q.value(), Option::None)?;
@@ -121,7 +121,7 @@ impl Signer for KeyPair {
(DSA,
&PublicKey::DSA { ref p, ref q, ref g, .. },
- &mpis::SecretKey::DSA { ref x }) => {
+ &mpis::SecretKeyMaterial::DSA { ref x }) => {
let params = dsa::Params::new(p.value(), q.value(), g.value());
let secret = dsa::PrivateKey::new(x.value());
@@ -135,7 +135,7 @@ impl Signer for KeyPair {
(EdDSA,
&PublicKey::EdDSA { ref curve, ref q },
- &mpis::SecretKey::EdDSA { ref scalar }) => match curve {
+ &mpis::SecretKeyMaterial::EdDSA { ref scalar }) => match curve {
Curve::Ed25519 => {
let public = q.decode_point(&Curve::Ed25519)?.0;
@@ -169,7 +169,7 @@ impl Signer for KeyPair {
(ECDSA,
&PublicKey::ECDSA { ref curve, .. },
- &mpis::SecretKey::ECDSA { ref scalar }) => {
+ &mpis::SecretKeyMaterial::ECDSA { ref scalar }) => {
let secret = match curve {
Curve::NistP256 =>
ecc::Scalar::new::<ecc::Secp256r1>(
@@ -218,7 +218,7 @@ impl Decryptor for KeyPair {
|secret| Ok(match (self.public.mpis(), secret, ciphertext)
{
(PublicKey::RSA{ ref e, ref n },
- mpis::SecretKey::RSA{ ref p, ref q, ref d, .. },
+ mpis::SecretKeyMaterial::RSA{ ref p, ref q, ref d, .. },
mpis::Ciphertext::RSA{ ref c }) => {
let public = rsa::PublicKey::new(n.value(), e.value())?;
let secret = rsa::PrivateKey::new(d.value(), p.value(),
@@ -229,13 +229,13 @@ impl Decryptor for KeyPair {
}
(PublicKey::Elgamal{ .. },
- mpis::SecretKey::Elgamal{ .. },
+ mpis::SecretKeyMaterial::Elgamal{ .. },
mpis::Ciphertext::Elgamal{ .. }) =>
return Err(
Error::UnsupportedPublicKeyAlgorithm(ElgamalEncrypt).into()),
(PublicKey::ECDH{ .. },
- mpis::SecretKey::ECDH { .. },
+ mpis::SecretKeyMaterial::ECDH { .. },
mpis::Ciphertext::ECDH { .. }) =>
crate::crypto::ecdh::decrypt(&self.public, secret, ciphertext)?,
diff --git a/openpgp/src/crypto/ecdh.rs b/openpgp/src/crypto/ecdh.rs
index 3dbd9f36..a51362b6 100644
--- a/openpgp/src/crypto/ecdh.rs
+++ b/openpgp/src/crypto/ecdh.rs
@@ -15,7 +15,7 @@ use crate::conversions::{
};
use crate::crypto::SessionKey;
use crate::crypto::mem::Protected;
-use crate::crypto::mpis::{MPI, PublicKey, SecretKey, Ciphertext};
+use crate::crypto::mpis::{MPI, PublicKey, SecretKeyMaterial, Ciphertext};
use nettle::{cipher, curve25519, mode, Mode, ecc, ecdh, Yarrow};
/// Wraps a session key using Elliptic Curve Diffie-Hellman.
@@ -169,12 +169,12 @@ pub fn encrypt_shared(recipient: &Key, session_key: &SessionKey, VB: MPI,
/// Unwraps a session key using Elliptic Curve Diffie-Hellman.
#[allow(non_snake_case)]
-pub fn decrypt(recipient: &Key, recipient_sec: &SecretKey,
+pub fn decrypt(recipient: &Key, recipient_sec: &SecretKeyMaterial,
ciphertext: &Ciphertext)
-> Result<SessionKey> {
match (recipient.mpis(), recipient_sec, ciphertext) {
(PublicKey::ECDH { ref curve, ..},
- SecretKey::ECDH { ref scalar, },
+ SecretKeyMaterial::ECDH { ref scalar, },
Ciphertext::ECDH { ref e, .. }) =>
{
let S: Protected = match curve {
diff --git a/openpgp/src/crypto/mpis.rs b/openpgp/src/crypto/mpis.rs
index 347fd3c8..e3a7f483 100644
--- a/openpgp/src/crypto/mpis.rs
+++ b/openpgp/src/crypto/mpis.rs
@@ -482,7 +482,7 @@ impl Arbitrary for PublicKey {
/// Provides a typed and structured way of storing multiple MPIs in
/// packets.
#[derive(Clone, Hash)]
-pub enum SecretKey {
+pub enum SecretKeyMaterial {
/// RSA secret key.
RSA {
/// Secret exponent, inverse of e in Phi(N).
@@ -534,40 +534,40 @@ pub enum SecretKey {
},
}
-impl fmt::Debug for SecretKey {
+impl fmt::Debug for SecretKeyMaterial {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if cfg!(debug_assertions) {
match self {
- &SecretKey::RSA{ ref d, ref p, ref q, ref u } =>
+ &SecretKeyMaterial::RSA{ ref d, ref p, ref q, ref u } =>
write!(f, "RSA {{ d: {:?}, p: {:?}, q: {:?}, u: {:?} }}", d, p, q, u),
- &SecretKey::DSA{ ref x } =>
+ &SecretKeyMaterial::DSA{ ref x } =>
write!(f, "DSA {{ x: {:?} }}", x),
- &SecretKey::Elgamal{ ref x } =>
+ &SecretKeyMaterial::Elgamal{ ref x } =>
write!(f, "Elgamal {{ x: {:?} }}", x),
- &SecretKey::EdDSA{ ref scalar } =>
+ &SecretKeyMaterial::EdDSA{ ref scalar } =>
write!(f, "EdDSA {{ scalar: {:?} }}", scalar),
- &SecretKey::ECDSA{ ref scalar } =>
+ &SecretKeyMaterial::ECDSA{ ref scalar } =>
write!(f, "ECDSA {{ scalar: {:?} }}", scalar),
- &SecretKey::ECDH{ ref scalar } =>
+ &SecretKeyMaterial::ECDH{ ref scalar } =>
write!(f, "ECDH {{ scalar: {:?} }}", scalar),
- &SecretKey::Unknown{ ref mpis, ref rest } =>
+ &SecretKeyMaterial::Unknown{ ref mpis, ref rest } =>
write!(f, "Unknown {{ mips: {:?}, rest: {:?} }}", mpis, rest),
}
} else {
match self {
- &SecretKey::RSA{ .. } =>
+ &SecretKeyMaterial::RSA{ .. } =>
f.write_str("RSA { <Redacted> }"),
- &SecretKey::DSA{ .. } =>
+ &SecretKeyMaterial::DSA{ .. } =>
f.write_str("DSA { <Redacted> }"),
- &SecretKey::Elgamal{ .. } =>
+ &SecretKeyMaterial::Elgamal{ .. } =>
f.write_str("Elgamal { <Redacted> }"),
- &SecretKey::EdDSA{ .. } =>
+ &SecretKeyMaterial::EdDSA{ .. } =>
f.write_str("EdDSA { <Redacted> }"),
- &SecretKey::ECDSA{ .. } =>
+ &SecretKeyMaterial::ECDSA{ .. } =>
f.write_str("ECDSA { <Redacted> }"),
- &SecretKey::ECDH{ .. } =>
+ &SecretKeyMaterial::ECDH{ .. } =>
f.write_str("ECDH { <Redacted> }"),
- &SecretKey::Unknown{ .. } =>
+ &SecretKeyMaterial::Unknown{ .. } =>
f.write_str("Unknown { <Redacted> }"),
}
}
@@ -581,25 +581,25 @@ fn secure_mpi_cmp(a: &ProtectedMPI, b: &ProtectedMPI) -> Ordering {
if ord1 == Ordering::Equal { ord2 } else { ord1 }
}
-impl PartialOrd for SecretKey {
- fn partial_cmp(&self, other: &SecretKey) -> Option<Ordering> {
+impl PartialOrd for SecretKeyMaterial {
+ fn partial_cmp(&self, other: &SecretKeyMaterial) -> Option<Ordering> {
use std::iter;
- fn discriminant(sk: &SecretKey) -> usize {
+ fn discriminant(sk: &SecretKeyMaterial) -> usize {
match sk {
- &SecretKey::RSA{ .. } => 0,
- &SecretKey::DSA{ .. } => 1,
- &SecretKey::Elgamal{ .. } => 2,
- &SecretKey::EdDSA{ .. } => 3,
- &SecretKey::ECDSA{ .. } => 4,
- &SecretKey::ECDH{ .. } => 5,
- &SecretKey::Unknown{ .. } => 6,
+ &SecretKeyMaterial::RSA{ .. } => 0,
+ &SecretKeyMaterial::DSA{ .. } => 1,
+ &SecretKeyMaterial::Elgamal{ .. } => 2,
+ &SecretKeyMaterial::EdDSA{ .. } => 3,
+ &SecretKeyMaterial::ECDSA{ .. } => 4,
+ &SecretKeyMaterial::ECDH{ .. } => 5,
+ &SecretKeyMaterial::Unknown{ .. } => 6,
}
}
let ret = match (self, other) {
- (&SecretKey::RSA{ d: ref d1, p: ref p1, q: ref q1, u: ref u1 }
- ,&SecretKey::RSA{ d: ref d2, p: ref p2, q: ref q2, u: ref u2 }) => {
+ (&SecretKeyMaterial::RSA{ d: ref d1, p: ref p1, q: ref q1, u: ref u1 }
+ ,&SecretKeyMaterial::RSA{ d: ref d2, p: ref p2, q: ref q2, u: ref u2 }) => {
let o1 = secure_mpi_cmp(d1, d2);
let o2 = secure_mpi_cmp(p1, p2);
let o3 = secure_mpi_cmp(q1, q2);
@@ -610,28 +610,28 @@ impl PartialOrd for SecretKey {
if o3 != Ordering::Equal { return Some(o3); }
o4
}
- (&SecretKey::DSA{ x: ref x1 }
- ,&SecretKey::DSA{ x: ref x2 }) => {
+ (&SecretKeyMaterial::DSA{ x: ref x1 }
+ ,&SecretKeyMaterial::DSA{ x: ref x2 }) => {
secure_mpi_cmp(x1, x2)
}
- (&SecretKey::Elgamal{ x: ref x1 }
- ,&SecretKey::Elgamal{ x: ref x2 }) => {
+ (&SecretKeyMaterial::Elgamal{ x: ref x1 }
+ ,&SecretKeyMaterial::Elgamal{ x: ref x2 }) => {
secure_mpi_cmp(x1, x2)
}
- (&SecretKey::EdDSA{ scalar: ref scalar1 }
- ,&SecretKey::EdDSA{ scalar: ref scalar2 }) => {
+ (&SecretKeyMaterial::EdDSA{ scalar: ref scalar1 }
+ ,&SecretKeyMaterial::EdDSA{ scalar: ref scalar2 }) => {
secure_mpi_cmp(scalar1, scalar2)
}
- (&SecretKey::ECDSA{ scalar: ref scalar1 }
- ,&SecretKey::ECDSA{ scalar: ref scalar2 }) => {
+ (&SecretKeyMaterial::ECDSA{ scalar: ref scalar1 }
+ ,&SecretKeyMaterial::ECDSA{ scalar: ref scalar2 }) => {
secure_mpi_cmp(scalar1, scalar2)
}
- (&SecretKey::ECDH{ scalar: ref scalar1 }
- ,&SecretKey::ECDH{ scalar: ref scalar2 }) => {
+ (&SecretKeyMaterial::ECDH{ scalar: ref scalar1 }
+ ,&SecretKeyMaterial::ECDH{ scalar: ref scalar2 }) => {
secure_mpi_cmp(scalar1, scalar2)
}
- (&SecretKey::Unknown{ mpis: ref mpis1, rest: ref rest1 }
- ,&SecretKey::Unknown{ mpis: ref mpis2, rest: ref rest2 }) => {
+ (&SecretKeyMaterial::Unknown{ mpis: ref mpis1, rest: ref rest1 }
+ ,&SecretKeyMaterial::Unknown{ mpis: ref mpis2, rest: ref rest2 }) => {
let o1 = secure_cmp(rest1, rest2);
let on = mpis1.iter().zip(mpis2.iter()).map(|(a,b)| {
secure_mpi_cmp(a, b)
@@ -656,22 +656,22 @@ impl PartialOrd for SecretKey {
}
}
-impl Ord for SecretKey {
+impl Ord for SecretKeyMaterial {
fn cmp(&self, other: &Self) -> Ordering {
self.partial_cmp(other).unwrap()
}
}
-impl PartialEq for SecretKey {
+impl PartialEq for SecretKeyMaterial {
fn eq(&self, other: &Self) -> bool { self.cmp(other) == Ordering::Equal }
}
-impl Eq for SecretKey {}
+impl Eq for SecretKeyMaterial {}
-impl SecretKey {
+impl SecretKeyMaterial {
/// Number of octets all MPIs of this instance occupy when serialized.
pub fn serialized_len(&self) -> usize {
- use self::SecretKey::*;
+ use self::SecretKeyMaterial::*;
// Fields are mostly MPIs that consist of two octets length
// plus the big endian value itself. All other field types are
@@ -700,7 +700,7 @@ impl SecretKey {
/// Returns, if known, the public-key algorithm for this secret
/// key.
pub fn algo(&self) -> Option<PublicKeyAlgorithm> {
- use self::SecretKey::*;
+ use self::SecretKeyMaterial::*;
match self {
RSA { .. } => Some(PublicKeyAlgorithm::RSAEncryptSign),
DSA { .. } => Some(PublicKeyAlgorithm::DSA),
@@ -713,40 +713,40 @@ impl SecretKey {
}
}
-impl Hash for SecretKey {
+impl Hash for SecretKeyMaterial {
/// Update the Hash with a hash of the MPIs.
fn hash(&self, hash: &mut hash::Context) {
self.serialize(hash).expect("hashing does not fail")
}
}
-impl Arbitrary for SecretKey {
+impl Arbitrary for SecretKeyMaterial {
fn arbitrary<G: Gen>(g: &mut G) -> Self {
match g.gen_range(0, 6) {
- 0 => SecretKey::RSA {
+ 0 => SecretKeyMaterial::RSA {
d: MPI::arbitrary(g).into(),
p: MPI::arbitrary(g).into(),
q: MPI::arbitrary(g).into(),
u: MPI::arbitrary(g).into(),
},
- 1 => SecretKey::DSA {
+ 1 => SecretKeyMaterial::DSA {
x: MPI::arbitrary(g).into(),
},
- 2 => SecretKey::Elgamal {
+ 2 => SecretKeyMaterial::Elgamal {
x: MPI::arbitrary(g).into(),
},
- 3 => SecretKey::EdDSA {
+ 3 => SecretKeyMaterial::EdDSA {
scalar: MPI::arbitrary(g).into(),
},
- 4 => SecretKey::ECDSA {
+ 4 => SecretKeyMaterial::ECDSA {
scalar: MPI::arbitrary(g).into(),
},
- 5 => SecretKey::ECDH {
+ 5 => SecretKeyMaterial::ECDH {
scalar: MPI::arbitrary(g).into(),
},
@@ -1054,7 +1054,7 @@ mod tests {
}
quickcheck! {
- fn sk_roundtrip(sk: SecretKey) -> bool {
+ fn sk_roundtrip(sk: SecretKeyMaterial) -> bool {
use std::io::Cursor;
use crate::PublicKeyAlgorithm::*;
use crate::serialize::Serialize;
@@ -1066,26 +1066,26 @@ mod tests {
#[allow(deprecated)]
let sk_ = match &sk {
- SecretKey::RSA { .. } =>
- SecretKey::parse(
+ SecretKeyMaterial::RSA { .. } =>
+ SecretKeyMaterial::parse(
RSAEncryptSign, cur.into_inner()).unwrap(),
- SecretKey::DSA { .. } =>
- SecretKey::parse(
+ SecretKeyMaterial::DSA { .. } =>
+ SecretKeyMaterial::parse(
DSA, cur.into_inner()).unwrap(),
- SecretKey::EdDSA { .. } =>
- SecretKey::parse(
+ SecretKeyMaterial::EdDSA { .. } =>
+ SecretKeyMaterial::parse(
EdDSA, cur.into_inner()).unwrap(),
- SecretKey::ECDSA { .. } =>
- SecretKey::parse(
+ SecretKeyMaterial::ECDSA { .. } =>
+ SecretKeyMaterial::parse(
ECDSA, cur.into_inner()).unwrap(),
- SecretKey::ECDH { .. } =>
- SecretKey::parse(
+ SecretKeyMaterial::ECDH { .. } =>
+ SecretKeyMaterial::parse(
ECDH, cur.into_inner()).unwrap(),
- SecretKey::Elgamal { .. } =>
- SecretKey::parse(
+ SecretKeyMaterial::Elgamal { .. } =>
+ SecretKeyMaterial::parse(
ElgamalEncrypt, cur.into_inner()).unwrap(),
- SecretKey::Unknown { .. } => unreachable!(),
+ SecretKeyMaterial::Unknown { .. } => unreachable!(),
};
sk == sk_
diff --git a/openpgp/src/packet/key.rs b/openpgp/src/packet/key.rs
index 1d2faf93..4962d4c1 100644
--- a/openpgp/src/packet/key.rs
+++ b/openpgp/src/packet/key.rs
@@ -36,7 +36,7 @@ pub struct Key4 {
/// Public key MPIs.
mpis: mpis::PublicKey,
/// Optional secret part of the key.
- secret: Option<SecretKey>,
+ secret: Option<SecretKeyMaterial>,
}
@@ -83,7 +83,7 @@ impl Key4 {
impl Key4 {
/// Creates a new OpenPGP key packet.
pub fn new(creation_time: time::Tm, pk_algo: PublicKeyAlgorithm,
- mpis: mpis::PublicKey, secret: Option<SecretKey>)
+ mpis: mpis::PublicKey, secret: Option<SecretKeyMaterial>)
-> Result<Self>
{
Ok(Key4 {
@@ -154,7 +154,7 @@ impl Key4 {
sym: sym.into().unwrap_or(SymmetricAlgorithm::AES256),
q: mpis::MPI::new(&public_key),
},
- secret: Some(mpis::SecretKey::ECDH {
+ secret: Some(mpis::SecretKeyMaterial::ECDH {
scalar: private_key.into(),
}.into()),
})
@@ -206,7 +206,7 @@ impl Key4 {
curve: Curve::Ed25519,
q: mpis::MPI::new(&public_key),
},
- secret: Some(mpis::SecretKey::EdDSA {
+ secret: Some(mpis::SecretKeyMaterial::EdDSA {
scalar: mpis::MPI::new(private_key).into(),
}.into()),
})
@@ -254,7 +254,7 @@ impl Key4 {
e: mpis::MPI::new(&key.e()[..]),
n: mpis::MPI::new(&key.n()[..]),
},
- secret: Some(mpis::SecretKey::RSA {
+ secret: Some(mpis::SecretKeyMaterial::RSA {
d: mpis::MPI::new(d).into(),
p: mpis::MPI::new(&a[..]).into(),
q: mpis::MPI::new(&b[..]).into(),
@@ -275,7 +275,7 @@ impl Key4 {
e: MPI::new(&*public.e()).into(),
n: MPI::new(&*public.n()).into(),
};
- let private_mpis = mpis::SecretKey::RSA {
+ let private_mpis = mpis::SecretKeyMaterial::RSA {
d: MPI::new(&*private.d()).into(),
p: MPI::new(&*p).into(),
q: MPI::new(&*q).into(),
@@ -324,7 +324,7 @@ impl Key4 {
curve: Curve::Ed25519,
q: MPI::new(&public),
};
- let private_mpis = mpis::SecretKey::EdDSA {
+ let private_mpis = mpis::SecretKeyMaterial::EdDSA {
scalar: private.into(),
};
let sec = Some(private_mpis.into());
@@ -351,7 +351,7 @@ impl Key4 {
hash: HashAlgorithm::SHA256,
sym: SymmetricAlgorithm::AES256,
};
- let private_mpis = mpis::SecretKey::ECDH {
+ let private_mpis = mpis::SecretKeyMaterial::ECDH {
scalar: private.into(),
};
let sec = Some(private_mpis.into());
@@ -384,7 +384,7 @@ impl Key4 {
curve: curve,
q: MPI::new_weierstrass(&pub_x, &pub_y, field_sz),
};
- let private_mpis = mpis::SecretKey::ECDSA{
+ let private_mpis = mpis::SecretKeyMaterial::ECDSA{
scalar: MPI::new(&private.as_bytes()).into(),
};
let sec = Some(private_mpis.into());
@@ -423,7 +423,7 @@ impl Key4 {
hash: hash,
sym: SymmetricAlgorithm::AES256,
};
- let private_mpis = mpis::SecretKey::ECDH{
+ let private_mpis = mpis::SecretKeyMaterial::ECDH{
scalar: MPI::new(&private.as_bytes()).into(),
};
let sec = Some(private_mpis.into());
@@ -480,21 +480,21 @@ impl Key4 {
::std::mem::replace(&mut self.mpis, mpis)
}
- /// Gets the key packet's SecretKey.
- pub fn secret(&self) -> Option<&SecretKey> {
+ /// Gets the key packet's `SecretKeyMaterial`.
+ pub fn secret(&self) -> Option<&SecretKeyMaterial> {
self.secret.as_ref()
}
- /// Gets a mutable reference to the key packet's SecretKey.
- pub fn secret_mut(&mut self) -> Option<&mut SecretKey> {
+ /// Gets a mutable reference to the key packet's `SecretKeyMaterial`.
+ pub fn secret_mut(&mut self) -> Option<&mut SecretKeyMaterial> {
self.secret.as_mut()
}
- /// Sets the key packet's SecretKey.
+ /// Sets the key packet's `SecretKeyMaterial`.
///
/// Returns the old value.
- pub fn set_secret(&mut self, secret: Option<SecretKey>)
- -> Option<SecretKey>
+ pub fn set_secret(&mut self, secret: Option<SecretKeyMaterial>)
+ -> Option<SecretKeyMaterial>
{
std::mem::replace(&mut self.secret, secret)
}
@@ -538,9 +538,10 @@ impl Key4 {
///
/// Fails if the secret key is missing, or encrypted.
pub fn into_keypair(mut self) -> Result<KeyPair> {
+ use crate::packet::key::SecretKeyMaterial;
let secret = match self.set_secret(None) {
- Some(SecretKey::Unencrypted(secret)) => secret,
- Some(SecretKey::Encrypted(_)) =>
+ Some(SecretKeyMaterial::Unencrypted(secret)) => secret,
+ Some(SecretKeyMaterial::Encrypted(_)) =>
return Err(Error::InvalidArgument(
"secret key is encrypted".into()).into()),
None =>
@@ -562,43 +563,43 @@ impl From<Key4> for super::Key {
///
/// This type allows postponing the decryption of the secret key until we need to use it.
#[derive(PartialEq, Eq, Hash, Clone, Debug)]
-pub enum SecretKey {
+pub enum SecretKeyMaterial {
/// Unencrypted secret key. Can be used as-is.
Unencrypted(Unencrypted),
/// The secret key is encrypted with a password.
Encrypted(Encrypted),
}
-impl From<mpis::SecretKey> for SecretKey {
- fn from(mpis: mpis::SecretKey) -> Self {
- SecretKey::Unencrypted(mpis.into())
+impl From<mpis::SecretKeyMaterial> for SecretKeyMaterial {
+ fn from(mpis: mpis::SecretKeyMaterial) -> Self {
+ SecretKeyMaterial::Unencrypted(mpis.into())
}
}
-impl From<Unencrypted> for SecretKey {
+impl From<Unencrypted> for SecretKeyMaterial {
fn from(key: Unencrypted) -> Self {
- SecretKey::Unencrypted(key)
+ SecretKeyMaterial::Unencrypted(key)
}
}
-impl From<Encrypted> for SecretKey {
+impl From<Encrypted> for SecretKeyMaterial {
fn from(key: Encrypted) -> Self {
- SecretKey::Encrypted(key)
+ SecretKeyMaterial::Encrypted(key)
}
}
-impl SecretKey {
+impl SecretKeyMaterial {
/// Decrypts this secret key using `password`.
///
- /// The SecretKey type does not know what kind of key it is, so
+ /// The `SecretKeyMaterial` type does not know what kind of key it is, so
/// `pk_algo` is needed to parse the correct number of MPIs.
pub fn decrypt_in_place(&mut self, pk_algo: PublicKeyAlgorithm,
password: &Password)
-> Result<()> {
let new = match self {
- SecretKey::Encrypted(ref e) =>
+ SecretKeyMaterial::Encrypted(ref e) =>
Some(e.decrypt(pk_algo, password)?.into()),
- SecretKey::Unencrypted(_) => None,
+ SecretKeyMaterial::Unencrypted(_) => None,
};
if let Some(v) = new {
@@ -611,9 +612,9 @@ impl SecretKey {
/// Encrypts this secret key using `password`.
pub fn encrypt_in_place(&mut self, password: &Password) -> Result<()> {
let new = match self {
- SecretKey::Unencrypted(ref u) =>
+ SecretKeyMaterial::Unencrypted(ref u) =>
Some(u.encrypt(password)?.into()),
- SecretKey::Encrypted(_) => None,
+ SecretKeyMaterial::Encrypted(_) => None,
};
if let Some(v) = new {
@@ -626,8 +627,8 @@ impl SecretKey {
/// Returns true if this secret key is encrypted.
pub fn is_encrypted(&self) -> bool {
match self {
- SecretKey::Encrypted(_) => true,
- SecretKey::Unencrypted(_) => false,
+ SecretKeyMaterial::Encrypted(_) => true,
+ SecretKeyMaterial::Unencrypted(_) => false,
}
}
}
@@ -645,8 +646,8 @@ impl PartialEq for Unencrypted {
}
}
-impl From<mpis::SecretKey> for Unencrypted {
- fn from(mpis: mpis::SecretKey) -> Self {
+impl From<mpis::SecretKeyMaterial> for Unencrypted {
+ fn from(mpis: mpis::SecretKeyMaterial) -> Self {
use crate::serialize::Serialize;
let mut plaintext = Vec::new();
// We need to store the type.
@@ -661,11 +662,11 @@ impl From<mpis::SecretKey> for Unencrypted {
impl Unencrypted {
/// Maps the given function over the secret.
pub fn map<F, T>(&self, mut fun: F) -> T
- where F: FnMut(&mpis::SecretKey) -> T
+ where F: FnMut(&mpis::SecretKeyMaterial) -> T
{
self.mpis.map(|plaintext| {
let algo: PublicKeyAlgorithm = plaintext[0].into();
- let mpis = mpis::SecretKey::parse(algo, &plaintext[1..])
+ let mpis = mpis::SecretKeyMaterial::parse(algo, &plaintext[1..])
.expect("Decrypted secret key is malformed");
fun(&mpis)
})
@@ -747,7 +748,7 @@ impl Encrypted {
let mut trash = vec![0u8; self.algo.block_size()?];
dec.read_exact