hpke_dispatch/
aead.rs

1use crate::IdLookupError;
2use num_enum::TryFromPrimitive;
3use std::str::FromStr;
4
5#[cfg(target_arch = "wasm32")]
6use wasm_bindgen::prelude::*;
7
8/**
9Aead represents an authenticated encryption with additional data
10encryption function, as per [RFC9180ยง7.3](https://www.rfc-editor.org/rfc/rfc9180.html#section-7.3)
11*/
12#[non_exhaustive]
13#[repr(u16)]
14#[derive(Debug, Copy, Clone, 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 Aead {
23    #[cfg(feature = "aead-aes-gcm-128")]
24    /// AES-128-GCM [GCM](https://doi.org/10.6028/nist.sp.800-38d)
25    AesGcm128 = 1,
26    #[cfg(feature = "aead-aes-gcm-256")]
27    /// AES-256-GCM [GCM](https://doi.org/10.6028/nist.sp.800-38d)
28    AesGcm256 = 2,
29    #[cfg(feature = "aead-chacha-20-poly-1305")]
30    /// ChaCha20Poly1305 [RFC8439](https://www.rfc-editor.org/info/rfc8439)
31    ChaCha20Poly1305 = 3,
32}
33
34impl FromStr for Aead {
35    type Err = IdLookupError;
36
37    fn from_str(s: &str) -> Result<Self, Self::Err> {
38        match &*s.to_lowercase().replace('-', "") {
39            #[cfg(feature = "aead-aes-gcm-128")]
40            "aesgcm128" | "aes128gcm" => Ok(Self::AesGcm128),
41            #[cfg(feature = "aead-aes-gcm-256")]
42            "aesgcm256" | "aes256gcm" => Ok(Self::AesGcm256),
43            #[cfg(feature = "aead-chacha-20-poly-1305")]
44            "chacha20poly1305" => Ok(Self::ChaCha20Poly1305),
45            _ => Err(IdLookupError("aead not recognized")),
46        }
47    }
48}
49
50/// An iterable slice of [`Aead`] variants
51pub const AEAD_ALL: &[Aead] = &[
52    #[cfg(feature = "aead-aes-gcm-128")]
53    Aead::AesGcm128,
54    #[cfg(feature = "aead-aes-gcm-256")]
55    Aead::AesGcm256,
56    #[cfg(feature = "aead-chacha-20-poly-1305")]
57    Aead::ChaCha20Poly1305,
58];