Skip to main content

ssh_key/
algorithm.rs

1//! Algorithm support.
2
3#[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
22/// bcrypt-pbkdf
23const BCRYPT: &str = "bcrypt";
24
25/// OpenSSH certificate for DSA public key
26const CERT_DSA: &str = "ssh-dss-cert-v01@openssh.com";
27
28/// OpenSSH certificate for ECDSA (NIST P-256) public key
29const CERT_ECDSA_SHA2_P256: &str = "ecdsa-sha2-nistp256-cert-v01@openssh.com";
30
31/// OpenSSH certificate for ECDSA (NIST P-384) public key
32const CERT_ECDSA_SHA2_P384: &str = "ecdsa-sha2-nistp384-cert-v01@openssh.com";
33
34/// OpenSSH certificate for ECDSA (NIST P-521) public key
35const CERT_ECDSA_SHA2_P521: &str = "ecdsa-sha2-nistp521-cert-v01@openssh.com";
36
37/// OpenSSH certificate for Ed25519 public key
38const CERT_ED25519: &str = "ssh-ed25519-cert-v01@openssh.com";
39
40/// OpenSSH certificate with RSA public key
41const CERT_RSA: &str = "ssh-rsa-cert-v01@openssh.com";
42
43/// OpenSSH certificate with RSA + SHA-256 as described in RFC8332 § 3
44const CERT_RSA_SHA2_256: &str = "rsa-sha2-256-cert-v01@openssh.com";
45
46/// OpenSSH certificate with RSA + SHA-512 as described in RFC8332 § 3
47const CERT_RSA_SHA2_512: &str = "rsa-sha2-512-cert-v01@openssh.com";
48
49/// OpenSSH certificate for ECDSA (NIST P-256) U2F/FIDO security key
50const CERT_SK_ECDSA_SHA2_P256: &str = "sk-ecdsa-sha2-nistp256-cert-v01@openssh.com";
51
52/// OpenSSH certificate for Ed25519 U2F/FIDO security key
53const CERT_SK_SSH_ED25519: &str = "sk-ssh-ed25519-cert-v01@openssh.com";
54
55/// OpenSSH certificate for MLDSA44-Ed25519 composite public key
56#[cfg(all(feature = "mldsa", feature = "ed25519"))]
57const CERT_MLDSA44_ED25519: &str = "ssh-mldsa44-ed25519-cert";
58
59/// SSH certificate for MLDSA87-P384 composite public key
60#[cfg(all(feature = "mldsa", feature = "p384"))]
61const CERT_MLDSA87_P384: &str = "ssh-mldsa87-p384-cert";
62
63/// ECDSA with SHA-256 + NIST P-256
64const ECDSA_SHA2_P256: &str = "ecdsa-sha2-nistp256";
65
66/// ECDSA with SHA-256 + NIST P-384
67const ECDSA_SHA2_P384: &str = "ecdsa-sha2-nistp384";
68
69/// ECDSA with SHA-256 + NIST P-521
70const ECDSA_SHA2_P521: &str = "ecdsa-sha2-nistp521";
71
72/// None
73const NONE: &str = "none";
74
75/// RSA with SHA-256 as described in RFC8332 § 3
76const RSA_SHA2_256: &str = "rsa-sha2-256";
77
78/// RSA with SHA-512 as described in RFC8332 § 3
79const RSA_SHA2_512: &str = "rsa-sha2-512";
80
81/// SHA-256 hash function
82const SHA256: &str = "sha256";
83
84/// SHA-512 hash function
85const SHA512: &str = "sha512";
86
87/// Digital Signature Algorithm
88const SSH_DSA: &str = "ssh-dss";
89
90/// Ed25519
91const SSH_ED25519: &str = "ssh-ed25519";
92
93/// RSA
94const SSH_RSA: &str = "ssh-rsa";
95
96/// U2F/FIDO security key with ECDSA/NIST P-256
97const SK_ECDSA_SHA2_P256: &str = "sk-ecdsa-sha2-nistp256@openssh.com";
98
99/// U2F/FIDO security key with Ed25519
100const SK_SSH_ED25519: &str = "sk-ssh-ed25519@openssh.com";
101
102/// MLDSA44-Ed25519-SHA512 composite signature algorithm
103#[cfg(all(feature = "mldsa", feature = "ed25519"))]
104const SSH_MLDSA44_ED25519: &str = "ssh-mldsa44-ed25519";
105
106/// MLDSA87-P384-SHA512 composite signature algorithm
107#[cfg(all(feature = "mldsa", feature = "p384"))]
108const SSH_MLDSA87_P384: &str = "ssh-mldsa87-p384";
109
110/// SSH key algorithms, i.e. digital signature algorithms used with SSH private/public keys.
111#[derive(Clone, Debug, Default, Eq, Hash, PartialEq, PartialOrd, Ord)]
112#[non_exhaustive]
113pub enum Algorithm {
114    /// Digital Signature Algorithm
115    Dsa,
116
117    /// Elliptic Curve Digital Signature Algorithm
118    Ecdsa {
119        /// Elliptic curve with which to instantiate ECDSA.
120        curve: EcdsaCurve,
121    },
122
123    /// Ed25519
124    #[default]
125    Ed25519,
126
127    /// RSA
128    Rsa {
129        /// Hash function to use with RSASSA-PKCS#1v15 signatures as specified
130        /// using [RFC8332] algorithm identifiers.
131        ///
132        /// If `hash` is set to `None`, then `ssh-rsa` is used as the algorithm
133        /// name.
134        ///
135        /// [RFC8332]: https://datatracker.ietf.org/doc/html/rfc8332
136        hash: Option<HashAlg>,
137    },
138
139    /// FIDO/U2F key with ECDSA/NIST-P256 + SHA-256
140    SkEcdsaSha2NistP256,
141
142    /// FIDO/U2F key with Ed25519
143    SkEd25519,
144
145    /// MLDSA44-Ed25519-SHA512 composite signature algorithm.
146    #[cfg(all(feature = "mldsa", feature = "ed25519"))]
147    MlDsa44Ed25519,
148
149    /// MLDSA87-P384-SHA512 composite signature algorithm.
150    #[cfg(all(feature = "mldsa", feature = "p384"))]
151    MlDsa87P384,
152
153    /// Other
154    #[cfg(feature = "alloc")]
155    Other(AlgorithmName),
156}
157
158impl Algorithm {
159    /// Decode algorithm from the given string identifier.
160    ///
161    /// # Supported algorithms
162    /// - `ecdsa-sha2-nistp256`
163    /// - `ecdsa-sha2-nistp384`
164    /// - `ecdsa-sha2-nistp521`
165    /// - `ssh-dss`
166    /// - `ssh-ed25519`
167    /// - `ssh-rsa`
168    /// - `sk-ecdsa-sha2-nistp256@openssh.com` (FIDO/U2F key)
169    /// - `sk-ssh-ed25519@openssh.com` (FIDO/U2F key)
170    /// - `ssh-mldsa44-ed25519`
171    /// - `ssh-mldsa87-p384`
172    ///
173    /// Any other algorithms are mapped to the [`Algorithm::Other`] variant.
174    ///
175    /// # Errors
176    /// Returns [`Error::Encoding`] in the event the algorithm name is not known.
177    pub fn new(id: &str) -> Result<Self> {
178        Ok(id.parse()?)
179    }
180
181    /// Decode algorithm from the given string identifier as used by the OpenSSH certificate format.
182    ///
183    /// OpenSSH certificate algorithms end in `*-cert-v01@openssh.com`.
184    /// See [draft-ietf-sshm-cert] for more information.
185    ///
186    /// # Supported algorithms
187    /// - `ssh-rsa-cert-v01@openssh.com`
188    /// - `ssh-dss-cert-v01@openssh.com`
189    /// - `ecdsa-sha2-nistp256-cert-v01@openssh.com`
190    /// - `ecdsa-sha2-nistp384-cert-v01@openssh.com`
191    /// - `ecdsa-sha2-nistp521-cert-v01@openssh.com`
192    /// - `ssh-ed25519-cert-v01@openssh.com`
193    /// - `sk-ecdsa-sha2-nistp256-cert-v01@openssh.com` (FIDO/U2F key)
194    /// - `sk-ssh-ed25519-cert-v01@openssh.com` (FIDO/U2F key)
195    /// - `ssh-mldsa44-ed25519-cert`
196    /// - `ssh-mldsa87-p384-cert`
197    ///
198    /// Any other algorithms are mapped to the [`Algorithm::Other`] variant.
199    ///
200    /// [draft-ietf-sshm-cert]: https://www.ietf.org/archive/id/draft-ietf-sshm-cert-01.html
201    ///
202    /// # Errors
203    /// Returns [`Error::AlgorithmUnknown`] in the event the algorithm is not known.
204    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    /// Get the string identifier which corresponds to this algorithm.
238    #[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    /// Get the string identifier which corresponds to the OpenSSH certificate
265    /// format.
266    ///
267    /// OpenSSH certificate algorithms end in `*-cert-v01@openssh.com`.
268    /// See [draft-ietf-sshm-cert] for more information.
269    ///
270    /// [draft-ietf-sshm-cert]: https://www.ietf.org/archive/id/draft-ietf-sshm-cert-01.html
271    #[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    /// Is the algorithm DSA?
301    #[must_use]
302    pub fn is_dsa(self) -> bool {
303        self == Algorithm::Dsa
304    }
305
306    /// Is the algorithm ECDSA?
307    #[must_use]
308    pub fn is_ecdsa(self) -> bool {
309        matches!(self, Algorithm::Ecdsa { .. })
310    }
311
312    /// Is the algorithm Ed25519?
313    #[must_use]
314    pub fn is_ed25519(self) -> bool {
315        self == Algorithm::Ed25519
316    }
317
318    /// Is the algorithm RSA?
319    #[must_use]
320    pub fn is_rsa(self) -> bool {
321        matches!(self, Algorithm::Rsa { .. })
322    }
323
324    /// Is the algorithm the MLDSA44-Ed25519 composite scheme?
325    #[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    /// Is the algorithm the MLDSA87-P384 composite scheme?
332    #[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    /// Return an error indicating this algorithm is unsupported.
339    #[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/// Elliptic curves supported for use with ECDSA.
397#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
398pub enum EcdsaCurve {
399    /// NIST P-256 (a.k.a. prime256v1, secp256r1)
400    NistP256,
401
402    /// NIST P-384 (a.k.a. secp384r1)
403    NistP384,
404
405    /// NIST P-521 (a.k.a. secp521r1)
406    NistP521,
407}
408
409impl EcdsaCurve {
410    /// Decode elliptic curve from the given string identifier.
411    ///
412    /// # Supported curves
413    ///
414    /// - `nistp256`
415    /// - `nistp384`
416    /// - `nistp521`
417    ///
418    /// # Errors
419    /// Returns [`Error::Encoding`] in the event the algorithm name is not known.
420    pub fn new(id: &str) -> Result<Self> {
421        Ok(id.parse()?)
422    }
423
424    /// Get the string identifier which corresponds to this ECDSA elliptic curve.
425    #[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    /// Get the number of bytes needed to encode a field element for this curve.
435    #[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/// Hashing algorithms a.k.a. digest functions.
479#[derive(Copy, Clone, Debug, Default, Eq, Hash, PartialEq, PartialOrd, Ord)]
480#[non_exhaustive]
481pub enum HashAlg {
482    /// SHA-256
483    #[default]
484    Sha256,
485
486    /// SHA-512
487    Sha512,
488}
489
490impl HashAlg {
491    /// Decode hash algorithm from the given string identifier.
492    ///
493    /// # Supported hash algorithms
494    ///
495    /// - `sha256`
496    /// - `sha512`
497    ///
498    /// # Errors
499    /// Returns [`Error::Encoding`] in the event the algorithm name is not known.
500    pub fn new(id: &str) -> Result<Self> {
501        Ok(id.parse()?)
502    }
503
504    /// Get the string identifier for this hash algorithm.
505    #[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    /// Get the size of a digest produced by this hash function.
514    #[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    /// Compute a digest of the given message using this hash function.
523    #[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
559/// Associate an SSH [`HashAlg`] with the given type.
560pub trait AssociatedHashAlg: Digest {
561    /// Algorithm identifier for this hash.
562    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/// Key Derivation Function (KDF) algorithms.
576#[derive(Copy, Clone, Debug, Default, Eq, Hash, PartialEq, PartialOrd, Ord)]
577#[non_exhaustive]
578pub enum KdfAlg {
579    /// None.
580    None,
581
582    /// bcrypt-pbkdf.
583    #[default]
584    Bcrypt,
585}
586
587impl KdfAlg {
588    /// Decode KDF algorithm from the given `kdfname`.
589    ///
590    /// # Supported KDF names
591    /// - `none`
592    /// - `bcrypt`
593    ///
594    /// # Errors
595    /// Returns [`Error::Encoding`] in the event the algorithm name is not known.
596    pub fn new(kdfname: &str) -> Result<Self> {
597        Ok(kdfname.parse()?)
598    }
599
600    /// Get the string identifier which corresponds to this algorithm.
601    #[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    /// Is the KDF algorithm "none"?
610    #[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}