1use std::str::FromStr;
2
3use num_enum::TryFromPrimitive;
4
5use crate::IdLookupError;
6
7#[non_exhaustive]
14#[repr(u16)]
15#[derive(Copy, Clone, Debug, PartialEq, Eq, TryFromPrimitive)]
16#[cfg_attr(
17 feature = "serde",
18 derive(serde_crate::Serialize, serde_crate::Deserialize)
19)]
20#[cfg_attr(feature = "serde", serde(crate = "serde_crate"))]
21pub enum Kdf {
22 #[cfg(feature = "hkdfsha2")]
24 Sha256 = 0x0001,
25 #[cfg(feature = "hkdfsha2")]
27 Sha384 = 0x0002,
28 #[cfg(feature = "hkdfsha2")]
30 Sha512 = 0x0003,
31 #[cfg(feature = "shake")]
33 Shake128 = 0x0010,
34 #[cfg(feature = "shake")]
36 Shake256 = 0x0011,
37 #[cfg(feature = "shake")]
39 TurboShake128 = 0x0012,
40 #[cfg(feature = "shake")]
42 TurboShake256 = 0x0013,
43}
44
45impl FromStr for Kdf {
46 type Err = IdLookupError;
47
48 fn from_str(s: &str) -> Result<Self, Self::Err> {
49 match &*s.to_lowercase().replace('-', "") {
50 #[cfg(feature = "hkdfsha2")]
51 "hkdfsha256" | "sha256" => Ok(Self::Sha256),
52 #[cfg(feature = "hkdfsha2")]
53 "hkdfsha384" | "sha384" => Ok(Self::Sha384),
54 #[cfg(feature = "hkdfsha2")]
55 "hkdfsha512" | "sha512" => Ok(Self::Sha512),
56 #[cfg(feature = "shake")]
57 "shake128" => Ok(Self::Shake128),
58 #[cfg(feature = "shake")]
59 "shake256" => Ok(Self::Shake256),
60 #[cfg(feature = "shake")]
61 "turboshake128" => Ok(Self::TurboShake128),
62 #[cfg(feature = "shake")]
63 "turboshake256" => Ok(Self::TurboShake256),
64 _ => Err(IdLookupError("kdf not recognized")),
65 }
66 }
67}
68
69pub const KDF_ALL: &[Kdf] = &[
71 #[cfg(feature = "hkdfsha2")]
72 Kdf::Sha256,
73 #[cfg(feature = "hkdfsha2")]
74 Kdf::Sha384,
75 #[cfg(feature = "hkdfsha2")]
76 Kdf::Sha512,
77 #[cfg(feature = "shake")]
78 Kdf::Shake128,
79 #[cfg(feature = "shake")]
80 Kdf::Shake256,
81 #[cfg(feature = "shake")]
82 Kdf::TurboShake128,
83 #[cfg(feature = "shake")]
84 Kdf::TurboShake256,
85];