1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
}
}
|