Skip to main content

sequoia_cryptoki/
algo.rs

1use std::str::FromStr;
2
3use sequoia_openpgp as openpgp;
4use openpgp::crypto::mpi::PublicKey;
5use openpgp::packet;
6
7/// Public-key algorithms.
8#[non_exhaustive]
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub enum PublicKeyAlgorithm {
11    /// RSA
12    ///
13    /// RSA is defined by [RFC 4880] and [RFC 9580].
14    ///
15    ///   [RFC 4880]: https://www.rfc-editor.org/rfc/rfc4880.html#section-9.1
16    ///   [RFC 9580]: https://www.rfc-editor.org/rfc/rfc9580.html#name-public-key-algorithms
17    RSA,
18
19    /// Cv25519.
20    ///
21    /// Cv25519 is defined by RFC 6637.
22    ///
23    /// Note: [Cv25519 has been deprecated] by RFC 9580 and must only
24    /// be used with v4 certificates.  For v6 certificates, prefer
25    /// [`PublicKeyAlgorithm::X25519`] instead.
26    ///
27    ///   [RFC 6637]: https://www.rfc-editor.org/rfc/rfc6637
28    ///   [Cv25519 has been deprecated]: https://www.rfc-editor.org/rfc/rfc9580.html#name-ecc-curves-for-openpgp
29    Cv25519,
30
31    /// X25519.
32    ///
33    /// X25519 is defined by [RFC 9580].
34    ///
35    /// X25519 keys can be used with v4-based certificates, but for
36    /// wide compatibility it is recommended to use Cv25519.
37    ///
38    ///   [RFC 9580]: https://www.rfc-editor.org/rfc/rfc9580.html#name-public-key-algorithms
39    X25519,
40
41    /// X448.
42    ///
43    /// X448 is defined by [RFC 9580].
44    ///
45    ///   [RFC 9580]: https://www.rfc-editor.org/rfc/rfc9580.html#name-public-key-algorithms
46    X448,
47
48    /// EdDSA.
49    ///
50    /// EdDSA is defined by RFC 6637.
51    ///
52    /// Note: [EdDSA has been deprecated] by RFC 9580 and must only
53    /// be used with v4 certificates.  For v6 certificates, prefer
54    /// [`PublicKeyAlgorithm::Ed25519`] instead.
55    ///
56    ///   [RFC 6637]: https://www.rfc-editor.org/rfc/rfc6637
57    ///   [Ed25519 has been deprecated]: https://www.rfc-editor.org/rfc/rfc9580.html#name-ecc-curves-for-openpgp
58    EdDSA,
59
60    /// Ed25519.
61    ///
62    /// Ed25519 is defined by [RFC 9580].
63    ///
64    /// Ed25519 keys can be used with v4-based certificates, but for
65    /// wide compatibility it is recommended to use EdDSA keys.
66    ///
67    ///   [RFC 9580]: https://www.rfc-editor.org/rfc/rfc9580.html#name-public-key-algorithms
68    Ed25519,
69
70    /// Ed448.
71    ///
72    /// Ed448 is defined by [RFC 9580].
73    ///
74    ///   [RFC 9580]: https://www.rfc-editor.org/rfc/rfc9580.html#name-public-key-algorithms
75    Ed448,
76
77    /// ML-DSA-65+Ed25519.
78    ///
79    /// As defined by the [Post-Quantum Cryptography in OpenPGP draft
80    /// specification].
81    ///
82    /// [Post-Quantum Cryptography in OpenPGP draft specification]: https://datatracker.ietf.org/doc/draft-ietf-openpgp-pqc/
83    MLDSA65_Ed25519,
84
85    /// ML-DSA-87+Ed448.
86    ///
87    /// As defined by the [Post-Quantum Cryptography in OpenPGP draft
88    /// specification].
89    ///
90    /// [Post-Quantum Cryptography in OpenPGP draft specification]: https://datatracker.ietf.org/doc/draft-ietf-openpgp-pqc/
91    MLDSA87_Ed448,
92
93    /// SLH-DSA-128s.
94    ///
95    /// As defined by the [Post-Quantum Cryptography in OpenPGP draft
96    /// specification].
97    ///
98    /// [Post-Quantum Cryptography in OpenPGP draft specification]: https://datatracker.ietf.org/doc/draft-ietf-openpgp-pqc/
99    SLHDSA128s,
100
101    /// SLH-DSA-128f.
102    ///
103    /// As defined by the [Post-Quantum Cryptography in OpenPGP draft
104    /// specification].
105    ///
106    /// [Post-Quantum Cryptography in OpenPGP draft specification]: https://datatracker.ietf.org/doc/draft-ietf-openpgp-pqc/
107    SLHDSA128f,
108
109    /// SLH-DSA-256s.
110    ///
111    /// As defined by the [Post-Quantum Cryptography in OpenPGP draft
112    /// specification].
113    ///
114    /// [Post-Quantum Cryptography in OpenPGP draft specification]: https://datatracker.ietf.org/doc/draft-ietf-openpgp-pqc/
115    SLHDSA256s,
116
117    /// MLKEM768+X25519.
118    ///
119    /// As defined by the [Post-Quantum Cryptography in OpenPGP draft
120    /// specification].
121    ///
122    /// [Post-Quantum Cryptography in OpenPGP draft specification]: https://datatracker.ietf.org/doc/draft-ietf-openpgp-pqc/
123    MLKEM768_X25519,
124
125    /// MLKEM1024+X448.
126    ///
127    /// As defined by the [Post-Quantum Cryptography in OpenPGP draft
128    /// specification].
129    ///
130    /// [Post-Quantum Cryptography in OpenPGP draft specification]: https://datatracker.ietf.org/doc/draft-ietf-openpgp-pqc/
131    MLKEM1024_X448,
132
133    // WARNING: If you add a new variant, be sure to also add it to
134    // PublicKeyAlgorithm::variants!
135}
136
137impl PublicKeyAlgorithm {
138    /// Returns all variants.
139    pub fn variants() -> impl Iterator<Item=Self> {
140	use PublicKeyAlgorithm::*;
141	[
142	    RSA,
143	    Cv25519,
144	    X25519,
145	    X448,
146	    EdDSA,
147	    Ed25519,
148	    Ed448,
149	    MLDSA65_Ed25519,
150	    MLDSA87_Ed448,
151	    SLHDSA128s,
152	    SLHDSA128f,
153	    SLHDSA256s,
154	    MLKEM768_X25519,
155	    MLKEM1024_X448,
156	].into_iter()
157    }
158
159    /// Returns whether the algorithm can be used for signing.
160    pub fn for_signing(&self) -> bool {
161	use PublicKeyAlgorithm::*;
162	match self {
163	    RSA => true,
164	    Cv25519 => false,
165	    X25519 => false,
166	    X448 => false,
167	    EdDSA => true,
168	    Ed25519 => true,
169	    Ed448 => true,
170	    MLDSA65_Ed25519 => true,
171	    MLDSA87_Ed448 => true,
172	    SLHDSA128s => true,
173	    SLHDSA128f => true,
174	    SLHDSA256s => true,
175	    MLKEM768_X25519 => false,
176	    MLKEM1024_X448 => false,
177	}
178    }
179
180    /// Returns whether the algorithm can be used for encryption.
181    pub fn for_encryption(&self) -> bool {
182	use PublicKeyAlgorithm::*;
183	match self {
184	    RSA => true,
185	    Cv25519 => true,
186	    X25519 => true,
187	    X448 => true,
188	    EdDSA => false,
189	    Ed25519 => false,
190	    Ed448 => false,
191	    MLDSA65_Ed25519 => false,
192	    MLDSA87_Ed448 => false,
193	    SLHDSA128s => false,
194	    SLHDSA128f => false,
195	    SLHDSA256s => false,
196	    MLKEM768_X25519 => true,
197	    MLKEM1024_X448 => true,
198	}
199    }
200}
201
202impl From<PublicKeyAlgorithm> for openpgp::crypto::PublicKeyAlgorithm {
203    /// Note: this is a lossy transformation.
204    ///
205    /// `EdDSA` and `Cv25519` map onto to `EdDSA` and `ECDH`,
206    /// respectively, which are actually group of algorithms.
207    fn from(pk: PublicKeyAlgorithm) -> openpgp::crypto::PublicKeyAlgorithm {
208	use PublicKeyAlgorithm::*;
209	match pk {
210	    RSA => openpgp::crypto::PublicKeyAlgorithm::RSAEncryptSign,
211	    Cv25519 => openpgp::crypto::PublicKeyAlgorithm::ECDH,
212	    X25519 => openpgp::crypto::PublicKeyAlgorithm::X25519,
213	    X448 => openpgp::crypto::PublicKeyAlgorithm::X448,
214	    EdDSA => openpgp::crypto::PublicKeyAlgorithm::EdDSA,
215	    Ed25519 => openpgp::crypto::PublicKeyAlgorithm::Ed25519,
216	    Ed448 => openpgp::crypto::PublicKeyAlgorithm::Ed448,
217	    MLDSA65_Ed25519 => openpgp::crypto::PublicKeyAlgorithm::MLDSA65_Ed25519,
218	    MLDSA87_Ed448 => openpgp::crypto::PublicKeyAlgorithm::MLDSA87_Ed448,
219	    SLHDSA128s => openpgp::crypto::PublicKeyAlgorithm::SLHDSA128s,
220	    SLHDSA128f => openpgp::crypto::PublicKeyAlgorithm::SLHDSA128f,
221	    SLHDSA256s => openpgp::crypto::PublicKeyAlgorithm::SLHDSA256s,
222	    MLKEM768_X25519 => openpgp::crypto::PublicKeyAlgorithm::MLKEM768_X25519,
223	    MLKEM1024_X448 => openpgp::crypto::PublicKeyAlgorithm::MLKEM1024_X448,
224	}
225    }
226}
227
228impl<P, R> TryFrom<&packet::Key<P, R>> for PublicKeyAlgorithm
229where
230    P: packet::key::KeyParts,
231    R: packet::key::KeyRole,
232{
233    type Error = anyhow::Error;
234
235    fn try_from(key: &packet::Key<P, R>) -> std::result::Result<Self, Self::Error> {
236	#[allow(deprecated)]
237        match key.mpis() {
238	    PublicKey::RSA { .. } => {
239                Ok(PublicKeyAlgorithm::RSA)
240            }
241
242	    PublicKey::ECDH { curve, .. } => {
243		match curve {
244		    openpgp::crypto::Curve::Cv25519 => {
245			Ok(PublicKeyAlgorithm::Cv25519)
246		    }
247		    curve => {
248			Err(anyhow::anyhow!("Unsupported algorithm: {} with {}",
249					    key.pk_algo(), curve))
250		    }
251		}
252            }
253
254	    PublicKey::EdDSA { curve, .. } => {
255		match curve {
256		    openpgp::crypto::Curve::Ed25519 => {
257			Ok(PublicKeyAlgorithm::Ed25519)
258		    }
259		    curve => {
260			Err(anyhow::anyhow!("Unsupported algorithm: {} with {}",
261					    key.pk_algo(), curve))
262		    }
263		}
264            }
265
266	    PublicKey::X25519 { .. } => {
267                Ok(PublicKeyAlgorithm::X25519)
268            }
269	    PublicKey::X448 { .. } => {
270                Ok(PublicKeyAlgorithm::X448)
271            }
272	    PublicKey::Ed25519 { .. } => {
273                Ok(PublicKeyAlgorithm::Ed25519)
274            }
275	    PublicKey::Ed448 { .. } => {
276                Ok(PublicKeyAlgorithm::Ed448)
277            }
278            PublicKey::MLDSA65_Ed25519 { .. } => {
279                Ok(PublicKeyAlgorithm::MLDSA65_Ed25519)
280            }
281            PublicKey::MLDSA87_Ed448 { .. } => {
282                Ok(PublicKeyAlgorithm::MLDSA87_Ed448)
283            }
284            PublicKey::SLHDSA128s { .. } => {
285                Ok(PublicKeyAlgorithm::SLHDSA128s)
286            }
287            PublicKey::SLHDSA128f { .. } => {
288                Ok(PublicKeyAlgorithm::SLHDSA128f)
289            }
290            PublicKey::SLHDSA256s { .. } => {
291                Ok(PublicKeyAlgorithm::SLHDSA256s)
292            }
293            PublicKey::MLKEM768_X25519 { .. } => {
294                Ok(PublicKeyAlgorithm::MLKEM768_X25519)
295            }
296            PublicKey::MLKEM1024_X448 { .. } => {
297                Ok(PublicKeyAlgorithm::MLKEM1024_X448)
298            }
299            _ => {
300                Err(anyhow::anyhow!("Unsupported algorithm: {}", key.pk_algo()))
301            }
302        }
303    }
304}
305
306impl std::fmt::Display for PublicKeyAlgorithm {
307    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
308	let algo = match self {
309	    PublicKeyAlgorithm::RSA => "rsa",
310	    PublicKeyAlgorithm::Cv25519 => "cv25519",
311	    PublicKeyAlgorithm::X25519 => "x25519",
312	    PublicKeyAlgorithm::X448 => "x448",
313	    PublicKeyAlgorithm::EdDSA => "eddsa",
314	    PublicKeyAlgorithm::Ed25519 => "ed25519",
315	    PublicKeyAlgorithm::Ed448 => "ed448",
316	    PublicKeyAlgorithm::MLDSA65_Ed25519 => "mldsa65+ed25519",
317	    PublicKeyAlgorithm::MLDSA87_Ed448 => "mldsa87+ed448",
318	    PublicKeyAlgorithm::SLHDSA128s => "slhdsa128s",
319	    PublicKeyAlgorithm::SLHDSA128f => "slhdsa128f",
320	    PublicKeyAlgorithm::SLHDSA256s => "slhdsa256s",
321	    PublicKeyAlgorithm::MLKEM768_X25519 => "mlkem768+x25519",
322	    PublicKeyAlgorithm::MLKEM1024_X448 => "mlkem1024+x448",
323	};
324
325	write!(f, "{}", algo)
326    }
327}
328
329impl FromStr for PublicKeyAlgorithm {
330    type Err = anyhow::Error;
331
332    fn from_str(algo: &str) -> Result<Self, Self::Err> {
333	match algo {
334	    "rsa" => Ok(PublicKeyAlgorithm::RSA),
335	    "cv25519" => Ok(PublicKeyAlgorithm::Cv25519),
336	    "x25519" => Ok(PublicKeyAlgorithm::X25519),
337	    "x448" => Ok(PublicKeyAlgorithm::X448),
338	    "eddsa" => Ok(PublicKeyAlgorithm::EdDSA),
339	    "ed25519" => Ok(PublicKeyAlgorithm::Ed25519),
340	    "ed448" => Ok(PublicKeyAlgorithm::Ed448),
341	    "mldsa65+ed25519" => Ok(PublicKeyAlgorithm::MLDSA65_Ed25519),
342	    "mldsa87+ed448" => Ok(PublicKeyAlgorithm::MLDSA87_Ed448),
343	    "slhdsa128s" => Ok(PublicKeyAlgorithm::SLHDSA128s),
344	    "slhdsa128f" => Ok(PublicKeyAlgorithm::SLHDSA128f),
345	    "slhdsa256s" => Ok(PublicKeyAlgorithm::SLHDSA256s),
346	    "mlkem768+x25519" => Ok(PublicKeyAlgorithm::MLKEM768_X25519),
347	    "mlkem1024+x448" => Ok(PublicKeyAlgorithm::MLKEM1024_X448),
348	    algo => Err(anyhow::anyhow!("Unknown algorithm {}", algo)),
349	}
350    }
351}
352
353#[cfg(test)]
354mod test {
355    use super::*;
356
357    #[test]
358    fn roundtrip() {
359	for v in PublicKeyAlgorithm::variants() {
360	    let s = v.to_string();
361	    assert_eq!(v, s.parse().expect(&format!("can parse {}", s)));
362	}
363    }
364
365    #[test]
366    fn for_signing() {
367	for v in PublicKeyAlgorithm::variants() {
368	    assert_eq!(v.for_signing(),
369		       openpgp::crypto::PublicKeyAlgorithm::from(v).for_signing());
370	}
371    }
372
373    #[test]
374    fn for_encryption() {
375	for v in PublicKeyAlgorithm::variants() {
376	    assert_eq!(v.for_encryption(),
377		       openpgp::crypto::PublicKeyAlgorithm::from(v).for_encryption());
378	}
379    }
380}