cl_noise_protocol/
traits.rs1pub trait U8Array: Sized {
8 fn new() -> Self;
10 fn new_with(_: u8) -> Self;
12 fn from_slice(_: &[u8]) -> Self;
18 fn len() -> usize;
20 fn as_slice(&self) -> &[u8];
22 fn as_mut(&mut self) -> &mut [u8];
24 fn clone(&self) -> Self {
27 Self::from_slice(self.as_slice())
28 }
29}
30
31macro_rules! impl_array {
32 ($len:expr) => {
33 impl U8Array for [u8; $len] {
34 fn new() -> Self {
35 [0u8; $len]
36 }
37 fn new_with(x: u8) -> Self {
38 [x; $len]
39 }
40 fn from_slice(data: &[u8]) -> Self {
41 let mut a = [0u8; $len];
42 a.copy_from_slice(data);
43 a
44 }
45 fn len() -> usize {
46 $len
47 }
48 fn as_slice(&self) -> &[u8] {
49 self
50 }
51 fn as_mut(&mut self) -> &mut [u8] {
52 self
53 }
54 }
55 };
56}
57
58impl_array!(32);
59impl_array!(64);
60impl_array!(128);
61
62#[derive(PartialEq, Copy, Clone, Debug)]
64pub struct Unspecified;
65
66impl core::fmt::Display for Unspecified {
68 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
69 f.write_str("ring::error::Unspecified")
70 }
71}
72
73#[cfg(feature = "std")]
74impl std::error::Error for Unspecified {}
75
76pub trait DH {
78 type Key;
80 type Pubkey: U8Array;
82 type Output: U8Array;
84
85 fn name() -> &'static str;
87
88 fn genkey() -> Self::Key;
90
91 fn pubkey(_: &Self::Key) -> Self::Pubkey;
93
94 fn dh(_: &Self::Key, _: &Self::Pubkey) -> Result<Self::Output, Unspecified>;
96}
97
98pub trait Cipher {
100 fn name() -> &'static str;
102 type Key;
104
105 fn key_len() -> usize;
107
108 fn key_from_slice(b: &[u8]) -> Self::Key;
114
115 fn tag_len() -> usize {
119 16
120 }
121
122 fn encrypt(k: &Self::Key, nonce: u64, ad: &[u8], plaintext: &[u8], out: &mut [u8]);
128
129 fn encrypt_in_place(
136 k: &Self::Key,
137 nonce: u64,
138 ad: &[u8],
139 in_out: &mut [u8],
140 plaintext_len: usize,
141 ) -> usize;
142
143 fn decrypt(
149 k: &Self::Key,
150 nonce: u64,
151 ad: &[u8],
152 ciphertext: &[u8],
153 out: &mut [u8],
154 ) -> Result<(), Unspecified>;
155
156 fn decrypt_in_place(
163 k: &Self::Key,
164 nonce: u64,
165 ad: &[u8],
166 in_out: &mut [u8],
167 ciphertext_len: usize,
168 ) -> Result<usize, Unspecified>;
169
170 fn rekey(k: &Self::Key) -> Self::Key {
172 let mut k1 = [0u8; 48];
174 Self::encrypt(k, 0u64.wrapping_sub(1), &[], &[0; 32], &mut k1);
175 Self::key_from_slice(&k1[..32])
176 }
177}
178
179pub trait Hash: Default {
181 fn name() -> &'static str;
183
184 type Block: U8Array;
186 type Output: U8Array;
188
189 fn block_len() -> usize {
191 Self::Block::len()
192 }
193
194 fn hash_len() -> usize {
196 Self::Output::len()
197 }
198
199 fn input(&mut self, data: &[u8]);
201
202 fn result(self) -> Self::Output;
204
205 fn hash(data: &[u8]) -> Self::Output {
207 let mut h: Self = Default::default();
208 h.input(data);
209 h.result()
210 }
211
212 fn hmac_many(key: &[u8], data: &[&[u8]]) -> Self::Output {
214 assert!(key.len() <= Self::block_len());
215
216 let mut ipad = Self::Block::new_with(0x36u8);
217 let mut opad = Self::Block::new_with(0x5cu8);
218
219 let ipad = ipad.as_mut();
220 let opad = opad.as_mut();
221
222 for (i, b) in key.iter().enumerate() {
223 ipad[i] ^= b;
224 opad[i] ^= b;
225 }
226
227 let mut hasher: Self = Default::default();
228 hasher.input(ipad);
229 for d in data {
230 hasher.input(d);
231 }
232 let inner_output = hasher.result();
233
234 hasher = Default::default();
235 hasher.input(opad);
236 hasher.input(inner_output.as_slice());
237 hasher.result()
238 }
239
240 fn hmac(key: &[u8], data: &[u8]) -> Self::Output {
242 Self::hmac_many(key, &[data])
243 }
244
245 fn hkdf(chaining_key: &[u8], input_key_material: &[u8]) -> (Self::Output, Self::Output) {
247 let temp_key = Self::hmac(chaining_key, input_key_material);
248 let out1 = Self::hmac(temp_key.as_slice(), &[1u8]);
249 let out2 = Self::hmac_many(temp_key.as_slice(), &[out1.as_slice(), &[2u8]]);
250 (out1, out2)
251 }
252
253 fn hkdf3(
255 chaining_key: &[u8],
256 input_key_material: &[u8],
257 ) -> (Self::Output, Self::Output, Self::Output) {
258 let temp_key = Self::hmac(chaining_key, input_key_material);
259 let out1 = Self::hmac(temp_key.as_slice(), &[1u8]);
260 let out2 = Self::hmac_many(temp_key.as_slice(), &[out1.as_slice(), &[2u8]]);
261 let out3 = Self::hmac_many(temp_key.as_slice(), &[out2.as_slice(), &[3u8]]);
262 (out1, out2, out3)
263 }
264}