diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
commit | 43a97878ce14b72f0981164f87f2e35e14151312 (patch) | |
tree | 620249daf56c0258faa40cbdcf9cfba06de2a846 /third_party/rust/ohttp/src/hpke.rs | |
parent | Initial commit. (diff) | |
download | firefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip |
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/ohttp/src/hpke.rs')
-rw-r--r-- | third_party/rust/ohttp/src/hpke.rs | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/third_party/rust/ohttp/src/hpke.rs b/third_party/rust/ohttp/src/hpke.rs new file mode 100644 index 0000000000..7bca571923 --- /dev/null +++ b/third_party/rust/ohttp/src/hpke.rs @@ -0,0 +1,92 @@ +macro_rules! convert_enum { + ($(#[$meta:meta])* $vis:vis enum $name:ident { + $($(#[$vmeta:meta])* $vname:ident $(= $val:expr)?,)* + }) => { + $(#[$meta])* + #[derive(Clone, Copy, Debug, PartialEq, Eq)] + $vis enum $name { + $($(#[$vmeta])* $vname $(= $val)?,)* + } + + impl std::convert::TryFrom<u16> for $name { + type Error = crate::Error; + + fn try_from(v: u16) -> Result<Self, Self::Error> { + match v { + $(x if x == $name::$vname as u16 => Ok($name::$vname),)* + _ => Err(crate::Error::Unsupported), + } + } + } + + impl std::convert::From<$name> for u16 { + fn from(v: $name) -> u16 { + v as u16 + } + } + } +} + +convert_enum! { +pub enum Kem { + X25519Sha256 = 32, +} +} + +impl Kem { + #[must_use] + pub fn n_enc(self) -> usize { + match self { + Kem::X25519Sha256 => 32, + } + } + + #[must_use] + pub fn n_pk(self) -> usize { + match self { + Kem::X25519Sha256 => 32, + } + } +} + +convert_enum! { + pub enum Kdf { + HkdfSha256 = 1, + HkdfSha384 = 2, + HkdfSha512 = 3, + } +} + +convert_enum! { + pub enum Aead { + Aes128Gcm = 1, + Aes256Gcm = 2, + ChaCha20Poly1305 = 3, + } +} + +impl Aead { + /// The size of the key for this AEAD. + #[must_use] + pub fn n_k(self) -> usize { + match self { + Aead::Aes128Gcm => 16, + Aead::Aes256Gcm | Aead::ChaCha20Poly1305 => 32, + } + } + + /// The size of the nonce for this AEAD. + #[must_use] + pub fn n_n(self) -> usize { + match self { + Aead::Aes128Gcm | Aead::Aes256Gcm | Aead::ChaCha20Poly1305 => 12, + } + } + + /// The size of the tag for this AEAD. + #[must_use] + #[allow(clippy::unused_self)] // This is only presently constant. + pub fn n_t(self) -> usize { + 16 + } +} |