sequoia_openpgp/crypto/
key.rs

1//! Common secret key related operations.
2
3use std::time::SystemTime;
4
5use crate::{
6    Result,
7    packet::key::{self, Key4, Key6, SecretParts},
8    types::Curve,
9};
10
11impl<R> Key6<SecretParts, R>
12    where R: key::KeyRole,
13{
14    /// Generates a new X25519 key.
15    pub fn generate_x25519() -> Result<Self> {
16        Key4::generate_x25519().map(Key6::from_common)
17    }
18
19    /// Generates a new X448 key.
20    pub fn generate_x448() -> Result<Self> {
21        Key4::generate_x448().map(Key6::from_common)
22    }
23
24    /// Generates a new Ed25519 key.
25    pub fn generate_ed25519() -> Result<Self> {
26        Key4::generate_ed25519().map(Key6::from_common)
27    }
28
29    /// Generates a new Ed448 key.
30    pub fn generate_ed448() -> Result<Self> {
31        Key4::generate_ed448().map(Key6::from_common)
32    }
33
34    /// Generates a new RSA key with a public modulos of size `bits`.
35    pub fn generate_rsa(bits: usize) -> Result<Self> {
36        Key4::generate_rsa(bits)
37            .map(Key6::from_common)
38    }
39
40    /// Creates a new OpenPGP public key packet for an existing RSA key.
41    ///
42    /// The RSA key will use public exponent `e` and modulo `n`. The key will
43    /// have its creation date set to `ctime` or the current time if `None`
44    /// is given.
45    #[allow(clippy::many_single_char_names)]
46    pub fn import_secret_rsa<T>(d: &[u8], p: &[u8], q: &[u8], ctime: T)
47        -> Result<Self> where T: Into<Option<SystemTime>>
48    {
49        Key4::import_secret_rsa(d, p, q, ctime)
50            .map(Key6::from_common)
51    }
52
53    /// Generates a new ECC key over `curve`.
54    ///
55    /// If `for_signing` is false a ECDH key, if it's true either a
56    /// EdDSA or ECDSA key is generated.  Giving `for_signing == true` and
57    /// `curve == Cv25519` will produce an error. Likewise
58    /// `for_signing == false` and `curve == Ed25519` will produce an error.
59    pub fn generate_ecc(for_signing: bool, curve: Curve) -> Result<Self> {
60        match (for_signing, curve) {
61            (true, Curve::Ed25519) => Self::generate_ed25519(),
62            (false, Curve::Cv25519) => Self::generate_x25519(),
63            (s, c) => Key4::generate_ecc(s, c).map(Key6::from_common),
64        }
65    }
66}