Skip to main content

hpke_dispatch/
kdf.rs

1use std::str::FromStr;
2
3use num_enum::TryFromPrimitive;
4
5use crate::IdLookupError;
6
7/// A key derivation function used in HPKE.
8///
9/// Codepoints are defined in [RFC9180ยง7.2][1] and [draft-ietf-hpke-pq][2].
10///
11/// [1]: https://www.rfc-editor.org/rfc/rfc9180.html#section-7.2
12/// [2]: https://www.ietf.org/archive/id/draft-ietf-hpke-pq-05.html#table-1
13#[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    /// Sha256 [RFC5869](https://www.rfc-editor.org/info/rfc5869)
23    #[cfg(feature = "hkdfsha2")]
24    Sha256 = 0x0001,
25    /// Sha384 [RFC5869](https://www.rfc-editor.org/info/rfc5869)
26    #[cfg(feature = "hkdfsha2")]
27    Sha384 = 0x0002,
28    /// Sha512 [RFC5869](https://www.rfc-editor.org/info/rfc5869)
29    #[cfg(feature = "hkdfsha2")]
30    Sha512 = 0x0003,
31    /// SHAKE128 [FIPS 202](https://csrc.nist.gov/pubs/fips/202/final)
32    #[cfg(feature = "shake")]
33    Shake128 = 0x0010,
34    /// SHAKE256 [FIPS 202](https://csrc.nist.gov/pubs/fips/202/final)
35    #[cfg(feature = "shake")]
36    Shake256 = 0x0011,
37    /// TurboSHAKE128 [RFC 9861](https://www.rfc-editor.org/info/rfc9861/)
38    #[cfg(feature = "shake")]
39    TurboShake128 = 0x0012,
40    /// TurboSHAKE256 [RFC 9861](https://www.rfc-editor.org/info/rfc9861/)
41    #[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
69/// An iterable slice of [`Kdf`] variants
70pub 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];