1#[cfg(feature = "alloc")]
4mod name;
5
6use crate::{Error, Result};
7use core::{fmt, str};
8use encoding::{Label, LabelError, digest::Digest};
9
10#[cfg(feature = "alloc")]
11use alloc::{borrow::ToOwned, string::String};
12
13#[cfg(all(feature = "alloc", feature = "sha2"))]
14use alloc::vec::Vec;
15
16#[cfg(feature = "sha2")]
17use sha2::{Sha256, Sha512};
18
19#[cfg(feature = "alloc")]
20pub use name::AlgorithmName;
21
22const BCRYPT: &str = "bcrypt";
24
25const CERT_DSA: &str = "ssh-dss-cert-v01@openssh.com";
27
28const CERT_ECDSA_SHA2_P256: &str = "ecdsa-sha2-nistp256-cert-v01@openssh.com";
30
31const CERT_ECDSA_SHA2_P384: &str = "ecdsa-sha2-nistp384-cert-v01@openssh.com";
33
34const CERT_ECDSA_SHA2_P521: &str = "ecdsa-sha2-nistp521-cert-v01@openssh.com";
36
37const CERT_ED25519: &str = "ssh-ed25519-cert-v01@openssh.com";
39
40const CERT_RSA: &str = "ssh-rsa-cert-v01@openssh.com";
42
43const CERT_RSA_SHA2_256: &str = "rsa-sha2-256-cert-v01@openssh.com";
45
46const CERT_RSA_SHA2_512: &str = "rsa-sha2-512-cert-v01@openssh.com";
48
49const CERT_SK_ECDSA_SHA2_P256: &str = "sk-ecdsa-sha2-nistp256-cert-v01@openssh.com";
51
52const CERT_SK_SSH_ED25519: &str = "sk-ssh-ed25519-cert-v01@openssh.com";
54
55#[cfg(all(feature = "mldsa", feature = "ed25519"))]
57const CERT_MLDSA44_ED25519: &str = "ssh-mldsa44-ed25519-cert";
58
59#[cfg(all(feature = "mldsa", feature = "p384"))]
61const CERT_MLDSA87_P384: &str = "ssh-mldsa87-p384-cert";
62
63const ECDSA_SHA2_P256: &str = "ecdsa-sha2-nistp256";
65
66const ECDSA_SHA2_P384: &str = "ecdsa-sha2-nistp384";
68
69const ECDSA_SHA2_P521: &str = "ecdsa-sha2-nistp521";
71
72const NONE: &str = "none";
74
75const RSA_SHA2_256: &str = "rsa-sha2-256";
77
78const RSA_SHA2_512: &str = "rsa-sha2-512";
80
81const SHA256: &str = "sha256";
83
84const SHA512: &str = "sha512";
86
87const SSH_DSA: &str = "ssh-dss";
89
90const SSH_ED25519: &str = "ssh-ed25519";
92
93const SSH_RSA: &str = "ssh-rsa";
95
96const SK_ECDSA_SHA2_P256: &str = "sk-ecdsa-sha2-nistp256@openssh.com";
98
99const SK_SSH_ED25519: &str = "sk-ssh-ed25519@openssh.com";
101
102#[cfg(all(feature = "mldsa", feature = "ed25519"))]
104const SSH_MLDSA44_ED25519: &str = "ssh-mldsa44-ed25519";
105
106#[cfg(all(feature = "mldsa", feature = "p384"))]
108const SSH_MLDSA87_P384: &str = "ssh-mldsa87-p384";
109
110#[derive(Clone, Debug, Default, Eq, Hash, PartialEq, PartialOrd, Ord)]
112#[non_exhaustive]
113pub enum Algorithm {
114 Dsa,
116
117 Ecdsa {
119 curve: EcdsaCurve,
121 },
122
123 #[default]
125 Ed25519,
126
127 Rsa {
129 hash: Option<HashAlg>,
137 },
138
139 SkEcdsaSha2NistP256,
141
142 SkEd25519,
144
145 #[cfg(all(feature = "mldsa", feature = "ed25519"))]
147 MlDsa44Ed25519,
148
149 #[cfg(all(feature = "mldsa", feature = "p384"))]
151 MlDsa87P384,
152
153 #[cfg(feature = "alloc")]
155 Other(AlgorithmName),
156}
157
158impl Algorithm {
159 pub fn new(id: &str) -> Result<Self> {
178 Ok(id.parse()?)
179 }
180
181 pub fn new_certificate(id: &str) -> Result<Self> {
205 match id {
206 CERT_DSA => Ok(Algorithm::Dsa),
207 CERT_ECDSA_SHA2_P256 => Ok(Algorithm::Ecdsa {
208 curve: EcdsaCurve::NistP256,
209 }),
210 CERT_ECDSA_SHA2_P384 => Ok(Algorithm::Ecdsa {
211 curve: EcdsaCurve::NistP384,
212 }),
213 CERT_ECDSA_SHA2_P521 => Ok(Algorithm::Ecdsa {
214 curve: EcdsaCurve::NistP521,
215 }),
216 CERT_ED25519 => Ok(Algorithm::Ed25519),
217 CERT_RSA => Ok(Algorithm::Rsa { hash: None }),
218 CERT_RSA_SHA2_256 => Ok(Algorithm::Rsa {
219 hash: Some(HashAlg::Sha256),
220 }),
221 CERT_RSA_SHA2_512 => Ok(Algorithm::Rsa {
222 hash: Some(HashAlg::Sha512),
223 }),
224 CERT_SK_ECDSA_SHA2_P256 => Ok(Algorithm::SkEcdsaSha2NistP256),
225 CERT_SK_SSH_ED25519 => Ok(Algorithm::SkEd25519),
226 #[cfg(all(feature = "mldsa", feature = "ed25519"))]
227 CERT_MLDSA44_ED25519 => Ok(Algorithm::MlDsa44Ed25519),
228 #[cfg(all(feature = "mldsa", feature = "p384"))]
229 CERT_MLDSA87_P384 => Ok(Algorithm::MlDsa87P384),
230 #[cfg(feature = "alloc")]
231 _ => Ok(Algorithm::Other(AlgorithmName::from_certificate_type(id)?)),
232 #[cfg(not(feature = "alloc"))]
233 _ => Err(Error::AlgorithmUnknown),
234 }
235 }
236
237 #[must_use]
239 pub fn as_str(&self) -> &str {
240 match self {
241 Algorithm::Dsa => SSH_DSA,
242 Algorithm::Ecdsa { curve } => match curve {
243 EcdsaCurve::NistP256 => ECDSA_SHA2_P256,
244 EcdsaCurve::NistP384 => ECDSA_SHA2_P384,
245 EcdsaCurve::NistP521 => ECDSA_SHA2_P521,
246 },
247 Algorithm::Ed25519 => SSH_ED25519,
248 Algorithm::Rsa { hash } => match hash {
249 None => SSH_RSA,
250 Some(HashAlg::Sha256) => RSA_SHA2_256,
251 Some(HashAlg::Sha512) => RSA_SHA2_512,
252 },
253 Algorithm::SkEcdsaSha2NistP256 => SK_ECDSA_SHA2_P256,
254 Algorithm::SkEd25519 => SK_SSH_ED25519,
255 #[cfg(all(feature = "mldsa", feature = "ed25519"))]
256 Algorithm::MlDsa44Ed25519 => SSH_MLDSA44_ED25519,
257 #[cfg(all(feature = "mldsa", feature = "p384"))]
258 Algorithm::MlDsa87P384 => SSH_MLDSA87_P384,
259 #[cfg(feature = "alloc")]
260 Algorithm::Other(algorithm) => algorithm.as_str(),
261 }
262 }
263
264 #[cfg(feature = "alloc")]
272 #[must_use]
273 pub fn to_certificate_type(&self) -> String {
274 match self {
275 Algorithm::Dsa => CERT_DSA,
276 Algorithm::Ecdsa { curve } => match curve {
277 EcdsaCurve::NistP256 => CERT_ECDSA_SHA2_P256,
278 EcdsaCurve::NistP384 => CERT_ECDSA_SHA2_P384,
279 EcdsaCurve::NistP521 => CERT_ECDSA_SHA2_P521,
280 },
281 Algorithm::Ed25519 => CERT_ED25519,
282 Algorithm::Rsa { hash: None } => CERT_RSA,
283 Algorithm::Rsa {
284 hash: Some(HashAlg::Sha256),
285 } => CERT_RSA_SHA2_256,
286 Algorithm::Rsa {
287 hash: Some(HashAlg::Sha512),
288 } => CERT_RSA_SHA2_512,
289 Algorithm::SkEcdsaSha2NistP256 => CERT_SK_ECDSA_SHA2_P256,
290 Algorithm::SkEd25519 => CERT_SK_SSH_ED25519,
291 #[cfg(all(feature = "mldsa", feature = "ed25519"))]
292 Algorithm::MlDsa44Ed25519 => CERT_MLDSA44_ED25519,
293 #[cfg(all(feature = "mldsa", feature = "p384"))]
294 Algorithm::MlDsa87P384 => CERT_MLDSA87_P384,
295 Algorithm::Other(algorithm) => return algorithm.certificate_type(),
296 }
297 .to_owned()
298 }
299
300 #[must_use]
302 pub fn is_dsa(self) -> bool {
303 self == Algorithm::Dsa
304 }
305
306 #[must_use]
308 pub fn is_ecdsa(self) -> bool {
309 matches!(self, Algorithm::Ecdsa { .. })
310 }
311
312 #[must_use]
314 pub fn is_ed25519(self) -> bool {
315 self == Algorithm::Ed25519
316 }
317
318 #[must_use]
320 pub fn is_rsa(self) -> bool {
321 matches!(self, Algorithm::Rsa { .. })
322 }
323
324 #[cfg(all(feature = "mldsa", feature = "ed25519"))]
326 #[must_use]
327 pub fn is_mldsa44_ed25519(self) -> bool {
328 matches!(self, Algorithm::MlDsa44Ed25519)
329 }
330
331 #[cfg(all(feature = "mldsa", feature = "p384"))]
333 #[must_use]
334 pub fn is_mldsa87_p384(self) -> bool {
335 matches!(self, Algorithm::MlDsa87P384)
336 }
337
338 #[allow(dead_code)]
340 pub(crate) fn unsupported_error(self) -> Error {
341 Error::AlgorithmUnsupported { algorithm: self }
342 }
343}
344
345impl AsRef<str> for Algorithm {
346 fn as_ref(&self) -> &str {
347 self.as_str()
348 }
349}
350
351impl Label for Algorithm {}
352
353impl fmt::Display for Algorithm {
354 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
355 f.write_str(self.as_str())
356 }
357}
358
359impl str::FromStr for Algorithm {
360 type Err = LabelError;
361
362 fn from_str(id: &str) -> core::result::Result<Self, LabelError> {
363 match id {
364 SSH_DSA => Ok(Algorithm::Dsa),
365 ECDSA_SHA2_P256 => Ok(Algorithm::Ecdsa {
366 curve: EcdsaCurve::NistP256,
367 }),
368 ECDSA_SHA2_P384 => Ok(Algorithm::Ecdsa {
369 curve: EcdsaCurve::NistP384,
370 }),
371 ECDSA_SHA2_P521 => Ok(Algorithm::Ecdsa {
372 curve: EcdsaCurve::NistP521,
373 }),
374 RSA_SHA2_256 => Ok(Algorithm::Rsa {
375 hash: Some(HashAlg::Sha256),
376 }),
377 RSA_SHA2_512 => Ok(Algorithm::Rsa {
378 hash: Some(HashAlg::Sha512),
379 }),
380 SSH_ED25519 => Ok(Algorithm::Ed25519),
381 SSH_RSA => Ok(Algorithm::Rsa { hash: None }),
382 SK_ECDSA_SHA2_P256 => Ok(Algorithm::SkEcdsaSha2NistP256),
383 SK_SSH_ED25519 => Ok(Algorithm::SkEd25519),
384 #[cfg(all(feature = "mldsa", feature = "ed25519"))]
385 SSH_MLDSA44_ED25519 => Ok(Algorithm::MlDsa44Ed25519),
386 #[cfg(all(feature = "mldsa", feature = "p384"))]
387 SSH_MLDSA87_P384 => Ok(Algorithm::MlDsa87P384),
388 #[cfg(feature = "alloc")]
389 _ => Ok(Algorithm::Other(AlgorithmName::from_str(id)?)),
390 #[cfg(not(feature = "alloc"))]
391 _ => Err(LabelError::new(id)),
392 }
393 }
394}
395
396#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
398pub enum EcdsaCurve {
399 NistP256,
401
402 NistP384,
404
405 NistP521,
407}
408
409impl EcdsaCurve {
410 pub fn new(id: &str) -> Result<Self> {
421 Ok(id.parse()?)
422 }
423
424 #[must_use]
426 pub fn as_str(self) -> &'static str {
427 match self {
428 EcdsaCurve::NistP256 => "nistp256",
429 EcdsaCurve::NistP384 => "nistp384",
430 EcdsaCurve::NistP521 => "nistp521",
431 }
432 }
433
434 #[cfg(any(feature = "alloc", feature = "mldsa"))]
436 pub(crate) const fn field_size(self) -> usize {
437 match self {
438 EcdsaCurve::NistP256 => 32,
439 EcdsaCurve::NistP384 => 48,
440 EcdsaCurve::NistP521 => 66,
441 }
442 }
443}
444
445impl AsRef<str> for EcdsaCurve {
446 fn as_ref(&self) -> &str {
447 self.as_str()
448 }
449}
450
451impl From<EcdsaCurve> for Algorithm {
452 fn from(curve: EcdsaCurve) -> Algorithm {
453 Algorithm::Ecdsa { curve }
454 }
455}
456
457impl Label for EcdsaCurve {}
458
459impl fmt::Display for EcdsaCurve {
460 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
461 f.write_str(self.as_str())
462 }
463}
464
465impl str::FromStr for EcdsaCurve {
466 type Err = LabelError;
467
468 fn from_str(id: &str) -> core::result::Result<Self, LabelError> {
469 match id {
470 "nistp256" => Ok(EcdsaCurve::NistP256),
471 "nistp384" => Ok(EcdsaCurve::NistP384),
472 "nistp521" => Ok(EcdsaCurve::NistP521),
473 _ => Err(LabelError::new(id)),
474 }
475 }
476}
477
478#[derive(Copy, Clone, Debug, Default, Eq, Hash, PartialEq, PartialOrd, Ord)]
480#[non_exhaustive]
481pub enum HashAlg {
482 #[default]
484 Sha256,
485
486 Sha512,
488}
489
490impl HashAlg {
491 pub fn new(id: &str) -> Result<Self> {
501 Ok(id.parse()?)
502 }
503
504 #[must_use]
506 pub fn as_str(self) -> &'static str {
507 match self {
508 HashAlg::Sha256 => SHA256,
509 HashAlg::Sha512 => SHA512,
510 }
511 }
512
513 #[must_use]
515 pub const fn digest_size(self) -> usize {
516 match self {
517 HashAlg::Sha256 => 32,
518 HashAlg::Sha512 => 64,
519 }
520 }
521
522 #[cfg(all(feature = "alloc", feature = "sha2"))]
524 #[must_use]
525 pub fn digest(self, msg: &[u8]) -> Vec<u8> {
526 match self {
527 HashAlg::Sha256 => Sha256::digest(msg).to_vec(),
528 HashAlg::Sha512 => Sha512::digest(msg).to_vec(),
529 }
530 }
531}
532
533impl Label for HashAlg {}
534
535impl AsRef<str> for HashAlg {
536 fn as_ref(&self) -> &str {
537 self.as_str()
538 }
539}
540
541impl fmt::Display for HashAlg {
542 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
543 f.write_str(self.as_str())
544 }
545}
546
547impl str::FromStr for HashAlg {
548 type Err = LabelError;
549
550 fn from_str(id: &str) -> core::result::Result<Self, LabelError> {
551 match id {
552 SHA256 => Ok(HashAlg::Sha256),
553 SHA512 => Ok(HashAlg::Sha512),
554 _ => Err(LabelError::new(id)),
555 }
556 }
557}
558
559pub trait AssociatedHashAlg: Digest {
561 const HASH_ALG: HashAlg;
563}
564
565#[cfg(feature = "sha2")]
566impl AssociatedHashAlg for Sha256 {
567 const HASH_ALG: HashAlg = HashAlg::Sha256;
568}
569
570#[cfg(feature = "sha2")]
571impl AssociatedHashAlg for Sha512 {
572 const HASH_ALG: HashAlg = HashAlg::Sha512;
573}
574
575#[derive(Copy, Clone, Debug, Default, Eq, Hash, PartialEq, PartialOrd, Ord)]
577#[non_exhaustive]
578pub enum KdfAlg {
579 None,
581
582 #[default]
584 Bcrypt,
585}
586
587impl KdfAlg {
588 pub fn new(kdfname: &str) -> Result<Self> {
597 Ok(kdfname.parse()?)
598 }
599
600 #[must_use]
602 pub fn as_str(self) -> &'static str {
603 match self {
604 Self::None => NONE,
605 Self::Bcrypt => BCRYPT,
606 }
607 }
608
609 #[must_use]
611 pub fn is_none(self) -> bool {
612 self == Self::None
613 }
614}
615
616impl Label for KdfAlg {}
617
618impl AsRef<str> for KdfAlg {
619 fn as_ref(&self) -> &str {
620 self.as_str()
621 }
622}
623
624impl fmt::Display for KdfAlg {
625 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
626 f.write_str(self.as_str())
627 }
628}
629
630impl str::FromStr for KdfAlg {
631 type Err = LabelError;
632
633 fn from_str(kdfname: &str) -> core::result::Result<Self, LabelError> {
634 match kdfname {
635 NONE => Ok(Self::None),
636 BCRYPT => Ok(Self::Bcrypt),
637 _ => Err(LabelError::new(kdfname)),
638 }
639 }
640}