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
use super::{
chacha::{self, *},
chacha20_poly1305::derive_poly1305_key,
cpu, poly1305, Nonce, Tag,
};
use crate::{constant_time, endian::*, error};
use core::convert::TryInto;
pub struct SealingKey {
key: Key,
}
impl SealingKey {
pub fn new(key_material: &[u8; KEY_LEN]) -> SealingKey {
SealingKey {
key: Key::new(key_material, cpu::features()),
}
}
pub fn seal_in_place(
&self,
sequence_number: u32,
plaintext_in_ciphertext_out: &mut [u8],
tag_out: &mut [u8; TAG_LEN],
) {
let mut counter = make_counter(sequence_number);
let poly_key =
derive_poly1305_key(&self.key.k_2, counter.increment(), self.key.cpu_features);
{
let (len_in_out, data_and_padding_in_out) =
plaintext_in_ciphertext_out.split_at_mut(PACKET_LENGTH_LEN);
self.key
.k_1
.encrypt_in_place(make_counter(sequence_number), len_in_out);
self.key
.k_2
.encrypt_in_place(counter, data_and_padding_in_out);
}
let Tag(tag) = poly1305::sign(poly_key, plaintext_in_ciphertext_out);
tag_out.copy_from_slice(tag.as_ref());
}
}
pub struct OpeningKey {
key: Key,
}
impl OpeningKey {
pub fn new(key_material: &[u8; KEY_LEN]) -> OpeningKey {
OpeningKey {
key: Key::new(key_material, cpu::features()),
}
}
pub fn decrypt_packet_length(
&self,
sequence_number: u32,
encrypted_packet_length: [u8; PACKET_LENGTH_LEN],
) -> [u8; PACKET_LENGTH_LEN] {
let mut packet_length = encrypted_packet_length;
let counter = make_counter(sequence_number);
self.key.k_1.encrypt_in_place(counter, &mut packet_length);
packet_length
}
pub fn open_in_place<'a>(
&self,
sequence_number: u32,
ciphertext_in_plaintext_out: &'a mut [u8],
tag: &[u8; TAG_LEN],
) -> Result<&'a [u8], error::Unspecified> {
let mut counter = make_counter(sequence_number);
let poly_key =
derive_poly1305_key(&self.key.k_2, counter.increment(), self.key.cpu_features);
verify(poly_key, ciphertext_in_plaintext_out, tag)?;
let plaintext_in_ciphertext_out = &mut ciphertext_in_plaintext_out[PACKET_LENGTH_LEN..];
self.key
.k_2
.encrypt_in_place(counter, plaintext_in_ciphertext_out);
Ok(plaintext_in_ciphertext_out)
}
}
struct Key {
k_1: chacha::Key,
k_2: chacha::Key,
cpu_features: cpu::Features,
}
impl Key {
fn new(key_material: &[u8; KEY_LEN], cpu_features: cpu::Features) -> Key {
let (k_2, k_1) = key_material.split_at(chacha::KEY_LEN);
let k_1: [u8; chacha::KEY_LEN] = k_1.try_into().unwrap();
let k_2: [u8; chacha::KEY_LEN] = k_2.try_into().unwrap();
Key {
k_1: chacha::Key::from(k_1),
k_2: chacha::Key::from(k_2),
cpu_features,
}
}
}
fn make_counter(sequence_number: u32) -> Counter {
let nonce = [
BigEndian::ZERO,
BigEndian::ZERO,
BigEndian::from(sequence_number),
];
Counter::zero(Nonce::assume_unique_for_key(*(nonce.as_byte_array())))
}
pub const KEY_LEN: usize = chacha::KEY_LEN * 2;
pub const PACKET_LENGTH_LEN: usize = 4;
pub const TAG_LEN: usize = super::BLOCK_LEN;
fn verify(key: poly1305::Key, msg: &[u8], tag: &[u8; TAG_LEN]) -> Result<(), error::Unspecified> {
let Tag(calculated_tag) = poly1305::sign(key, msg);
constant_time::verify_slices_are_equal(calculated_tag.as_ref(), tag)
}