summaryrefslogtreecommitdiffstats
path: root/openpgp/src/crypto
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2019-06-27 17:12:26 +0200
committerJustus Winter <justus@sequoia-pgp.org>2019-06-27 17:12:26 +0200
commit0e026f8ac794a70012d36e662a0b54ad0888d890 (patch)
tree0c905dc768cea33dd3cd416398c00f489af953e9 /openpgp/src/crypto
parent9ce35cc2c757fae1338a356354608cf1d6fa563f (diff)
openpgp: Rework secret key handling.
- Introduce two new types, `Encrypted` and `Unencrypted`, to make the fields of enum `SecretKey` private. Add accessors, implement From<..> to make the new types ergonomic to use, update callsites.
Diffstat (limited to 'openpgp/src/crypto')
-rw-r--r--openpgp/src/crypto/asymmetric.rs16
1 files changed, 7 insertions, 9 deletions
diff --git a/openpgp/src/crypto/asymmetric.rs b/openpgp/src/crypto/asymmetric.rs
index 5cb0221d..839f0eaa 100644
--- a/openpgp/src/crypto/asymmetric.rs
+++ b/openpgp/src/crypto/asymmetric.rs
@@ -51,12 +51,12 @@ pub trait Decryptor {
#[derive(Clone)]
pub struct KeyPair {
public: Key,
- secret: mpis::SecretKey,
+ secret: packet::key::Unencrypted,
}
impl KeyPair {
/// Creates a new key pair.
- pub fn new(public: Key, secret: mpis::SecretKey) -> Result<Self> {
+ pub fn new(public: Key, secret: packet::key::Unencrypted) -> Result<Self> {
Ok(Self {
public: public,
secret: secret,
@@ -69,7 +69,7 @@ impl KeyPair {
}
/// Returns a reference to the secret key.
- pub fn secret(&self) -> &mpis::SecretKey {
+ pub fn secret(&self) -> &packet::key::Unencrypted {
&self.secret
}
}
@@ -89,7 +89,7 @@ impl Signer for KeyPair {
let mut rng = Yarrow::default();
#[allow(deprecated)]
- match (self.public.pk_algo(), self.public.mpis(), &self.secret)
+ match (self.public.pk_algo(), self.public.mpis(), &self.secret.mpis())
{
(RSASign,
&PublicKey::RSA { ref e, ref n },
@@ -215,7 +215,7 @@ impl Decryptor for KeyPair {
use crypto::mpis::PublicKey;
use nettle::rsa;
- Ok(match (self.public.mpis(), &self.secret, ciphertext)
+ Ok(match (self.public.mpis(), &self.secret.mpis(), ciphertext)
{
(PublicKey::RSA{ ref e, ref n },
mpis::SecretKey::RSA{ ref p, ref q, ref d, .. },
@@ -237,7 +237,7 @@ impl Decryptor for KeyPair {
(PublicKey::ECDH{ .. },
mpis::SecretKey::ECDH { .. },
mpis::Ciphertext::ECDH { .. }) =>
- ::crypto::ecdh::decrypt(&self.public, &self.secret,
+ ::crypto::ecdh::decrypt(&self.public, &self.secret.mpis(),
ciphertext)?,
(public, secret, ciphertext) =>
@@ -252,9 +252,7 @@ impl Decryptor for KeyPair {
impl From<KeyPair> for packet::Key {
fn from(p: KeyPair) -> Self {
let (mut key, secret) = (p.public, p.secret);
- key.set_secret(Some(packet::key::SecretKey::Unencrypted {
- mpis: secret,
- }));
+ key.set_secret(Some(secret.into()));
key
}