bitcoin_ohttp/
hpke.rs

1macro_rules! convert_enum {
2    ($(#[$meta:meta])* $vis:vis enum $name:ident {
3        $($(#[$vmeta:meta])* $vname:ident $(= $val:expr)?,)*
4    }) => {
5        $(#[$meta])*
6        #[derive(Clone, Copy, Debug, PartialEq, Eq)]
7        $vis enum $name {
8            $($(#[$vmeta])* $vname $(= $val)?,)*
9        }
10
11        impl std::convert::TryFrom<u16> for $name {
12            type Error = crate::Error;
13
14            fn try_from(v: u16) -> Result<Self, Self::Error> {
15                match v {
16                    $($(#[$vmeta])*
17                      x if x == $name::$vname as u16
18                      => Ok($name::$vname),)*
19                    _ => Err(crate::Error::Unsupported),
20                }
21            }
22        }
23
24        impl std::convert::From<$name> for u16 {
25            fn from(v: $name) -> u16 {
26                v as u16
27            }
28        }
29    }
30}
31
32convert_enum! {
33pub enum Kem {
34    K256Sha256 = 22,
35}
36}
37
38impl Kem {
39    #[must_use]
40    pub fn n_enc(self) -> usize {
41        match self {
42            Kem::K256Sha256 => 65,
43        }
44    }
45
46    #[must_use]
47    pub fn n_pk(self) -> usize {
48        match self {
49            Kem::K256Sha256 => 65,
50        }
51    }
52}
53
54convert_enum! {
55    pub enum Kdf {
56        HkdfSha256 = 1,
57        HkdfSha384 = 2,
58        HkdfSha512 = 3,
59    }
60}
61
62convert_enum! {
63    pub enum Aead {
64        Aes128Gcm = 1,
65        Aes256Gcm = 2,
66        ChaCha20Poly1305 = 3,
67    }
68}
69
70impl Aead {
71    /// The size of the key for this AEAD.
72    #[must_use]
73    pub fn n_k(self) -> usize {
74        match self {
75            Aead::Aes128Gcm => 16,
76            Aead::Aes256Gcm | Aead::ChaCha20Poly1305 => 32,
77        }
78    }
79
80    /// The size of the nonce for this AEAD.
81    #[must_use]
82    pub fn n_n(self) -> usize {
83        match self {
84            Aead::Aes128Gcm | Aead::Aes256Gcm | Aead::ChaCha20Poly1305 => 12,
85        }
86    }
87
88    /// The size of the tag for this AEAD.
89    #[must_use]
90    #[allow(clippy::unused_self)] // This is only presently constant.
91    pub fn n_t(self) -> usize {
92        16
93    }
94}