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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use crate::sealed::Sealed;
use zeroize::Zeroize;
use core::fmt;

pub trait GenericKey : Sealed {
    fn slice(&self) -> &[u8];
    fn size(&self) -> u32;
}

macro_rules! basic_key_api {
    ($ident:ident $($lt:lifetime)?) => {
        impl $(<$lt>)? $ident $(<$lt>)? {
            #[inline]
            pub const fn new_128(key: $(&$lt)? [u8; 16]) -> Self {
                Self::B128(key)
            }

            #[inline]
            pub const fn new_256(key: $(&$lt)? [u8; 32]) -> Self {
                Self::B256(key)
            }

            #[inline]
            pub const fn len(&self) -> u32 {
                match self {
                    Self::B256(_) => 32,
                    Self::B128(_) => 16
                }
            }

            #[doc = " Returns a friendly identifier for the key."]
            #[doc = ""]
            #[doc = " # Returns"]
            #[doc = ""]
            #[doc = concat!(" - `256` bit key: `\"", stringify!($ident), "::256\"`")]
            #[doc = concat!(" - `128` bit key: `\"", stringify!($ident), "::128\"`")]
            pub const fn ident(&self) -> &'static str {
                match self {
                    Self::B256(_) => concat!(stringify!($ident), "::256"),
                    Self::B128(_) => concat!(stringify!($ident), "::128"),
                }
            }

            #[inline]
            pub const fn as_slice(&self) -> &[u8] {
                match self {
                    Self::B256(array) => array.as_slice(),
                    Self::B128(array) => array.as_slice()
                }
            }
        }

        impl $(<$lt>)? fmt::Debug for $ident $(<$lt>)? {
            #[doc = "This writes the following to the `Formatter` (depending on the variant): "]
            #[doc = ""]
            #[doc = concat!(" - `256` bit key: `\"", stringify!($ident), "::256\"`")]
            #[doc = concat!(" - `128` bit key: `\"", stringify!($ident), "::128\"`")]
            #[inline]
            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                f.write_str(self.ident())
            }
        }

        impl $(<$lt>)? fmt::Display for $ident $(<$lt>)? {
            #[doc = "This writes the following to the `Formatter` (depending on the variant): "]
            #[doc = ""]
            #[doc = concat!(" - `256` bit key: `\"", stringify!($ident), "::256\"`")]
            #[doc = concat!(" - `128` bit key: `\"", stringify!($ident), "::128\"`")]
            #[inline]
            fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                f.write_str(self.ident())
            }
        }

        impl $(<$lt>)? Sealed for $ident $(<$lt>)? {}

        impl $(<$lt>)? GenericKey for $ident $(<$lt>)? {
            #[inline]
            fn slice(&self) -> &[u8] {
                self.as_slice()
            }
            #[inline]
            fn size(&self) -> u32 {
                self.len()
            }
        }
    };
}

#[must_use]
pub enum Key {
    B128([u8; 16]),
    B256([u8; 32])
}

basic_key_api! { Key }

impl Key {
    #[inline]
    pub fn as_mut_slice(&mut self) -> &mut [u8] {
        match self {
            Self::B256(array) => array.as_mut_slice(),
            Self::B128(array) => array.as_mut_slice()
        }
    }

    #[inline]
    pub const fn as_ref(&self) -> KeyRef {
        match self {
            Self::B128(raw) => KeyRef::B128(raw),
            Self::B256(raw) => KeyRef::B256(raw)
        }
    }
}

impl Zeroize for Key {
    #[inline]
    fn zeroize(&mut self) {
        self.as_mut_slice().zeroize()
    }
}

impl Drop for Key {
    #[inline]
    fn drop(&mut self) {
        self.zeroize();
    }
}

impl From<[u8; 16]> for Key {
    #[inline]
    fn from(value: [u8; 16]) -> Self {
        Self::B128(value)
    }
}

impl From<[u8; 32]> for Key {
    #[inline]
    fn from(value: [u8; 32]) -> Self {
        Self::B256(value)
    }
}

#[must_use]
pub enum KeyRef<'r> {
    B128(&'r [u8; 16]),
    B256(&'r [u8; 32])
}

impl<'r> KeyRef<'r> {
    #[inline]
    pub const fn copy(&self) -> Key {
        match *self {
            Self::B128(raw) => Key::B128(*raw),
            Self::B256(raw) => Key::B256(*raw)
        }
    }
}

basic_key_api! { KeyRef 'r }

impl<'r> From<&'r [u8; 16]> for KeyRef<'r> {
    #[inline]
    fn from(value: &'r [u8; 16]) -> Self {
        Self::B128(value)
    }
}

impl<'r> From<&'r [u8; 32]> for KeyRef<'r> {
    #[inline]
    fn from(value: &'r [u8; 32]) -> Self {
        Self::B256(value)
    }
}

impl<'kr> Sealed for &'kr [u8; 16] {}

impl<'kr> GenericKey for &'kr [u8; 16] {
    #[inline]
    fn slice(&self) -> &[u8] {
        self.as_slice()
    }
    #[inline]
    fn size(&self) -> u32 {
        16
    }
}

impl<'kr> Sealed for &'kr [u8; 32] {}
impl<'kr> GenericKey for &'kr [u8; 32] {
    #[inline]
    fn slice(&self) -> &[u8] {
        self.as_slice()
    }
    #[inline]
    fn size(&self) -> u32 {
        32
    }
}