Debian Codegraph @

third_party/rust/ohttp/src/hpke.rs

raw
1 macro_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
32 convert_enum! {
33 pub enum Kem {
34 X25519Sha256 = 32,
35
36 #[cfg(feature = "pq")]
37 X25519Kyber768Draft00 = 48,
38 }
39 }
40
41 impl Kem {
42 #[must_use]
43 pub fn n_enc(self) -> usize {
44 match self {
45 Kem::X25519Sha256 => 32,
46
47 #[cfg(feature = "pq")]
48 Kem::X25519Kyber768Draft00 => 1120,
49 }
50 }
51
52 #[must_use]
53 pub fn n_pk(self) -> usize {
54 match self {
55 Kem::X25519Sha256 => 32,
56
57 #[cfg(feature = "pq")]
58 Kem::X25519Kyber768Draft00 => 1216,
59 }
60 }
61 }
62
63 convert_enum! {
64 pub enum Kdf {
65 HkdfSha256 = 1,
66 HkdfSha384 = 2,
67 HkdfSha512 = 3,
68 }
69 }
70
71 convert_enum! {
72 pub enum Aead {
73 Aes128Gcm = 1,
74 Aes256Gcm = 2,
75 ChaCha20Poly1305 = 3,
76 }
77 }
78
79 impl Aead {
80 /// The size of the key for this AEAD.
81 #[must_use]
82 pub fn n_k(self) -> usize {
83 match self {
84 Aead::Aes128Gcm => 16,
85 Aead::Aes256Gcm | Aead::ChaCha20Poly1305 => 32,
86 }
87 }
88
89 /// The size of the nonce for this AEAD.
90 #[must_use]
91 pub fn n_n(self) -> usize {
92 match self {
93 Aead::Aes128Gcm | Aead::Aes256Gcm | Aead::ChaCha20Poly1305 => 12,
94 }
95 }
96
97 /// The size of the tag for this AEAD.
98 #[must_use]
99 #[allow(clippy::unused_self)] // This is only presently constant.
100 pub fn n_t(self) -> usize {
101 16
102 }
103 }