1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
use crate::packet::{self, key, Key};
use crate::crypto::SessionKey;
use crate::crypto::mpi;
use crate::types::HashAlgorithm;
use crate::Result;
pub trait Signer {
fn public(&self) -> &Key<key::PublicParts, key::UnspecifiedRole>;
fn sign(&mut self, hash_algo: HashAlgorithm, digest: &[u8])
-> Result<mpi::Signature>;
}
impl Signer for Box<dyn Signer> {
fn public(&self) -> &Key<key::PublicParts, key::UnspecifiedRole> {
self.as_ref().public()
}
fn sign(&mut self, hash_algo: HashAlgorithm, digest: &[u8])
-> Result<mpi::Signature> {
self.as_mut().sign(hash_algo, digest)
}
}
pub trait Decryptor {
fn public(&self) -> &Key<key::PublicParts, key::UnspecifiedRole>;
fn decrypt(&mut self, ciphertext: &mpi::Ciphertext,
plaintext_len: Option<usize>)
-> Result<SessionKey>;
}
#[derive(Clone)]
pub struct KeyPair {
public: Key<key::PublicParts, key::UnspecifiedRole>,
secret: packet::key::Unencrypted,
}
impl KeyPair {
pub fn new(public: Key<key::PublicParts, key::UnspecifiedRole>,
secret: packet::key::Unencrypted)
-> Result<Self>
{
Ok(Self {
public,
secret,
})
}
pub fn public(&self) -> &Key<key::PublicParts, key::UnspecifiedRole> {
&self.public
}
pub fn secret(&self) -> &packet::key::Unencrypted {
&self.secret
}
}
impl From<KeyPair> for Key<key::SecretParts, key::UnspecifiedRole> {
fn from(p: KeyPair) -> Self {
let (key, secret) = (p.public, p.secret);
key.add_secret(secret.into()).0
}
}