1use std::str::FromStr;
2
3use sequoia_openpgp as openpgp;
4use openpgp::crypto::mpi::PublicKey;
5use openpgp::packet;
6
7#[non_exhaustive]
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub enum PublicKeyAlgorithm {
11 RSA,
18
19 Cv25519,
30
31 X25519,
40
41 X448,
47
48 EdDSA,
59
60 Ed25519,
69
70 Ed448,
76
77 MLDSA65_Ed25519,
84
85 MLDSA87_Ed448,
92
93 SLHDSA128s,
100
101 SLHDSA128f,
108
109 SLHDSA256s,
116
117 MLKEM768_X25519,
124
125 MLKEM1024_X448,
132
133 }
136
137impl PublicKeyAlgorithm {
138 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 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 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 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}