hpke_dispatch/
kdf.rs

1use crate::IdLookupError;
2use num_enum::TryFromPrimitive;
3use std::str::FromStr;
4
5#[cfg(target_arch = "wasm32")]
6use wasm_bindgen::prelude::*;
7
8/**
9Kdf represents an key derivation function, as per
10[RFC9180ยง7.2](https://www.rfc-editor.org/rfc/rfc9180.html#section-7.2)
11*/
12#[non_exhaustive]
13#[repr(u16)]
14#[derive(Copy, Clone, Debug, PartialEq, Eq, TryFromPrimitive)]
15#[cfg_attr(
16    feature = "serde",
17    derive(serde_crate::Serialize, serde_crate::Deserialize)
18)]
19#[cfg_attr(feature = "serde", serde(crate = "serde_crate"))]
20#[cfg_attr(feature = "cfg_eval", cfg_eval)]
21#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
22pub enum Kdf {
23    #[cfg(feature = "kdf-sha256")]
24    /// Sha256 [RFC5869](https://www.rfc-editor.org/info/rfc5869)
25    Sha256 = 1,
26    #[cfg(feature = "kdf-sha384")]
27    /// Sha384 [RFC5869](https://www.rfc-editor.org/info/rfc5869)
28    Sha384 = 2,
29    #[cfg(feature = "kdf-sha512")]
30    /// Sha512 [RFC5869](https://www.rfc-editor.org/info/rfc5869)
31    Sha512 = 3,
32}
33
34impl FromStr for Kdf {
35    type Err = IdLookupError;
36
37    fn from_str(s: &str) -> Result<Self, Self::Err> {
38        match &*s.to_lowercase().replace('-', "") {
39            #[cfg(feature = "kdf-sha256")]
40            "hkdfsha256" | "sha256" => Ok(Self::Sha256),
41            #[cfg(feature = "kdf-sha384")]
42            "hkdfsha384" | "sha384" => Ok(Self::Sha384),
43            #[cfg(feature = "kdf-sha512")]
44            "hkdfsha512" | "sha512" => Ok(Self::Sha512),
45            _ => Err(IdLookupError("kdf not recognized")),
46        }
47    }
48}
49
50/// An iterable slice of [`Kdf`] variants
51pub const KDF_ALL: &[Kdf] = &[
52    #[cfg(feature = "kdf-sha256")]
53    Kdf::Sha256,
54    #[cfg(feature = "kdf-sha384")]
55    Kdf::Sha384,
56    #[cfg(feature = "kdf-sha512")]
57    Kdf::Sha512,
58];