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    /// Returns whether the algorithm has a post-quantum component.
202    pub fn has_post_quantum_component(&self) -> bool {
203        use PublicKeyAlgorithm::*;
204
205        match self {
206            MLDSA65_Ed25519 => true,
207            MLDSA87_Ed448 => true,
208            SLHDSA128s => true,
209            SLHDSA128f => true,
210            SLHDSA256s => true,
211            MLKEM768_X25519 => true,
212            MLKEM1024_X448 => true,
213            _ => false,
214        }
215    }
216
217    /// Returns whether the algorithm has a traditional (non-post
218    /// quantum) component.
219    pub fn has_traditional_component(&self) -> bool {
220        use PublicKeyAlgorithm::*;
221
222        match self {
223            SLHDSA128s => false,
224            SLHDSA128f => false,
225            SLHDSA256s => false,
226            _ => true,
227        }
228    }
229
230    /// Returns whether the algorithm is a composite algorithm, i.e.,
231    /// it has traditional and post quantum components.
232    pub fn is_composite(&self) -> bool {
233        self.has_post_quantum_component()
234            && self.has_traditional_component()
235    }
236}
237
238impl From<PublicKeyAlgorithm> for openpgp::crypto::PublicKeyAlgorithm {
239    /// Note: this is a lossy transformation.
240    ///
241    /// `EdDSA` and `Cv25519` map onto to `EdDSA` and `ECDH`,
242    /// respectively, which are actually group of algorithms.
243    fn from(pk: PublicKeyAlgorithm) -> openpgp::crypto::PublicKeyAlgorithm {
244	use PublicKeyAlgorithm::*;
245	match pk {
246	    RSA => openpgp::crypto::PublicKeyAlgorithm::RSAEncryptSign,
247	    Cv25519 => openpgp::crypto::PublicKeyAlgorithm::ECDH,
248	    X25519 => openpgp::crypto::PublicKeyAlgorithm::X25519,
249	    X448 => openpgp::crypto::PublicKeyAlgorithm::X448,
250	    EdDSA => openpgp::crypto::PublicKeyAlgorithm::EdDSA,
251	    Ed25519 => openpgp::crypto::PublicKeyAlgorithm::Ed25519,
252	    Ed448 => openpgp::crypto::PublicKeyAlgorithm::Ed448,
253	    MLDSA65_Ed25519 => openpgp::crypto::PublicKeyAlgorithm::MLDSA65_Ed25519,
254	    MLDSA87_Ed448 => openpgp::crypto::PublicKeyAlgorithm::MLDSA87_Ed448,
255	    SLHDSA128s => openpgp::crypto::PublicKeyAlgorithm::SLHDSA128s,
256	    SLHDSA128f => openpgp::crypto::PublicKeyAlgorithm::SLHDSA128f,
257	    SLHDSA256s => openpgp::crypto::PublicKeyAlgorithm::SLHDSA256s,
258	    MLKEM768_X25519 => openpgp::crypto::PublicKeyAlgorithm::MLKEM768_X25519,
259	    MLKEM1024_X448 => openpgp::crypto::PublicKeyAlgorithm::MLKEM1024_X448,
260	}
261    }
262}
263
264impl<P, R> TryFrom<&packet::Key<P, R>> for PublicKeyAlgorithm
265where
266    P: packet::key::KeyParts,
267    R: packet::key::KeyRole,
268{
269    type Error = anyhow::Error;
270
271    fn try_from(key: &packet::Key<P, R>) -> std::result::Result<Self, Self::Error> {
272	#[allow(deprecated)]
273        match key.mpis() {
274	    PublicKey::RSA { .. } => {
275                Ok(PublicKeyAlgorithm::RSA)
276            }
277
278	    PublicKey::ECDH { curve, .. } => {
279		match curve {
280		    openpgp::crypto::Curve::Cv25519 => {
281			Ok(PublicKeyAlgorithm::Cv25519)
282		    }
283		    curve => {
284			Err(anyhow::anyhow!("Unsupported algorithm: {} with {}",
285					    key.pk_algo(), curve))
286		    }
287		}
288            }
289
290	    PublicKey::EdDSA { curve, .. } => {
291		match curve {
292		    openpgp::crypto::Curve::Ed25519 => {
293			Ok(PublicKeyAlgorithm::Ed25519)
294		    }
295		    curve => {
296			Err(anyhow::anyhow!("Unsupported algorithm: {} with {}",
297					    key.pk_algo(), curve))
298		    }
299		}
300            }
301
302	    PublicKey::X25519 { .. } => {
303                Ok(PublicKeyAlgorithm::X25519)
304            }
305	    PublicKey::X448 { .. } => {
306                Ok(PublicKeyAlgorithm::X448)
307            }
308	    PublicKey::Ed25519 { .. } => {
309                Ok(PublicKeyAlgorithm::Ed25519)
310            }
311	    PublicKey::Ed448 { .. } => {
312                Ok(PublicKeyAlgorithm::Ed448)
313            }
314            PublicKey::MLDSA65_Ed25519 { .. } => {
315                Ok(PublicKeyAlgorithm::MLDSA65_Ed25519)
316            }
317            PublicKey::MLDSA87_Ed448 { .. } => {
318                Ok(PublicKeyAlgorithm::MLDSA87_Ed448)
319            }
320            PublicKey::SLHDSA128s { .. } => {
321                Ok(PublicKeyAlgorithm::SLHDSA128s)
322            }
323            PublicKey::SLHDSA128f { .. } => {
324                Ok(PublicKeyAlgorithm::SLHDSA128f)
325            }
326            PublicKey::SLHDSA256s { .. } => {
327                Ok(PublicKeyAlgorithm::SLHDSA256s)
328            }
329            PublicKey::MLKEM768_X25519 { .. } => {
330                Ok(PublicKeyAlgorithm::MLKEM768_X25519)
331            }
332            PublicKey::MLKEM1024_X448 { .. } => {
333                Ok(PublicKeyAlgorithm::MLKEM1024_X448)
334            }
335            _ => {
336                Err(anyhow::anyhow!("Unsupported algorithm: {}", key.pk_algo()))
337            }
338        }
339    }
340}
341
342impl std::fmt::Display for PublicKeyAlgorithm {
343    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
344	let algo = match self {
345	    PublicKeyAlgorithm::RSA => "rsa",
346	    PublicKeyAlgorithm::Cv25519 => "cv25519",
347	    PublicKeyAlgorithm::X25519 => "x25519",
348	    PublicKeyAlgorithm::X448 => "x448",
349	    PublicKeyAlgorithm::EdDSA => "eddsa",
350	    PublicKeyAlgorithm::Ed25519 => "ed25519",
351	    PublicKeyAlgorithm::Ed448 => "ed448",
352	    PublicKeyAlgorithm::MLDSA65_Ed25519 => "mldsa65+ed25519",
353	    PublicKeyAlgorithm::MLDSA87_Ed448 => "mldsa87+ed448",
354	    PublicKeyAlgorithm::SLHDSA128s => "slhdsa128s",
355	    PublicKeyAlgorithm::SLHDSA128f => "slhdsa128f",
356	    PublicKeyAlgorithm::SLHDSA256s => "slhdsa256s",
357	    PublicKeyAlgorithm::MLKEM768_X25519 => "mlkem768+x25519",
358	    PublicKeyAlgorithm::MLKEM1024_X448 => "mlkem1024+x448",
359	};
360
361	write!(f, "{}", algo)
362    }
363}
364
365impl FromStr for PublicKeyAlgorithm {
366    type Err = anyhow::Error;
367
368    fn from_str(algo: &str) -> Result<Self, Self::Err> {
369	match algo {
370	    "rsa" => Ok(PublicKeyAlgorithm::RSA),
371	    "cv25519" => Ok(PublicKeyAlgorithm::Cv25519),
372	    "x25519" => Ok(PublicKeyAlgorithm::X25519),
373	    "x448" => Ok(PublicKeyAlgorithm::X448),
374	    "eddsa" => Ok(PublicKeyAlgorithm::EdDSA),
375	    "ed25519" => Ok(PublicKeyAlgorithm::Ed25519),
376	    "ed448" => Ok(PublicKeyAlgorithm::Ed448),
377	    "mldsa65+ed25519" => Ok(PublicKeyAlgorithm::MLDSA65_Ed25519),
378	    "mldsa87+ed448" => Ok(PublicKeyAlgorithm::MLDSA87_Ed448),
379	    "slhdsa128s" => Ok(PublicKeyAlgorithm::SLHDSA128s),
380	    "slhdsa128f" => Ok(PublicKeyAlgorithm::SLHDSA128f),
381	    "slhdsa256s" => Ok(PublicKeyAlgorithm::SLHDSA256s),
382	    "mlkem768+x25519" => Ok(PublicKeyAlgorithm::MLKEM768_X25519),
383	    "mlkem1024+x448" => Ok(PublicKeyAlgorithm::MLKEM1024_X448),
384	    algo => Err(anyhow::anyhow!("Unknown algorithm {}", algo)),
385	}
386    }
387}
388
389#[cfg(test)]
390mod test {
391    use super::*;
392
393    #[test]
394    fn roundtrip() {
395	for v in PublicKeyAlgorithm::variants() {
396	    let s = v.to_string();
397	    assert_eq!(v, s.parse().expect(&format!("can parse {}", s)));
398	}
399    }
400
401    #[test]
402    fn for_signing() {
403	for v in PublicKeyAlgorithm::variants() {
404	    assert_eq!(v.for_signing(),
405		       openpgp::crypto::PublicKeyAlgorithm::from(v).for_signing());
406	}
407    }
408
409    #[test]
410    fn for_encryption() {
411	for v in PublicKeyAlgorithm::variants() {
412	    assert_eq!(v.for_encryption(),
413		       openpgp::crypto::PublicKeyAlgorithm::from(v).for_encryption());
414	}
415    }
416}