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
200
201
202
203
204
use aead::{self, Aes128Siv, Aes128PmacSiv, Aes256Siv, Aes256PmacSiv};
use byteorder::{BigEndian, ByteOrder};
use error::Error;
pub const NONCE_SIZE: usize = 8;
const LAST_BLOCK_FLAG: u8 = 1;
pub struct Encryptor<A: aead::Algorithm> {
alg: A,
nonce: NonceEncoder32,
}
pub type Aes128SivEncryptor = Encryptor<Aes128Siv>;
pub type Aes256SivEncryptor = Encryptor<Aes256Siv>;
pub type Aes128PmacSivEncryptor = Encryptor<Aes128PmacSiv>;
pub type Aes256PmacSivEncryptor = Encryptor<Aes256PmacSiv>;
impl<A: aead::Algorithm> Encryptor<A> {
pub fn new(key: &[u8], nonce: &[u8]) -> Self {
Self {
alg: A::new(key),
nonce: NonceEncoder32::new(nonce),
}
}
pub fn seal_next_in_place(&mut self, ad: &[u8], buffer: &mut [u8]) {
self.alg.seal_in_place(self.nonce.as_slice(), ad, buffer);
self.nonce.increment();
}
pub fn seal_last_in_place(mut self, ad: &[u8], buffer: &mut [u8]) {
self.alg.seal_in_place(&self.nonce.finish(), ad, buffer);
}
#[cfg(feature = "std")]
pub fn seal_next(&mut self, ad: &[u8], plaintext: &[u8]) -> Vec<u8> {
let ciphertext = self.alg.seal(self.nonce.as_slice(), ad, plaintext);
self.nonce.increment();
ciphertext
}
#[cfg(feature = "std")]
pub fn seal_last(mut self, ad: &[u8], plaintext: &[u8]) -> Vec<u8> {
self.alg.seal(&self.nonce.finish(), ad, plaintext)
}
}
pub struct Decryptor<A: aead::Algorithm> {
alg: A,
nonce: NonceEncoder32,
}
pub type Aes128SivDecryptor = Decryptor<Aes128Siv>;
pub type Aes256SivDecryptor = Decryptor<Aes256Siv>;
pub type Aes128PmacSivDecryptor = Decryptor<Aes128PmacSiv>;
pub type Aes256PmacSivDecryptor = Decryptor<Aes256PmacSiv>;
impl<A: aead::Algorithm> Decryptor<A> {
pub fn new(key: &[u8], nonce: &[u8]) -> Self {
Self {
alg: A::new(key),
nonce: NonceEncoder32::new(nonce),
}
}
pub fn open_next_in_place<'a>(
&mut self,
ad: &[u8],
buffer: &'a mut [u8],
) -> Result<&'a [u8], Error> {
let result = self.alg.open_in_place(self.nonce.as_slice(), ad, buffer)?;
self.nonce.increment();
Ok(result)
}
pub fn open_last_in_place<'a>(
mut self,
ad: &[u8],
buffer: &'a mut [u8],
) -> Result<&'a [u8], Error> {
self.alg.open_in_place(&self.nonce.finish(), ad, buffer)
}
#[cfg(feature = "std")]
pub fn open_next(&mut self, ad: &[u8], ciphertext: &[u8]) -> Result<Vec<u8>, Error> {
let plaintext = self.alg.open(self.nonce.as_slice(), ad, ciphertext)?;
self.nonce.increment();
Ok(plaintext)
}
#[cfg(feature = "std")]
pub fn open_last(mut self, ad: &[u8], ciphertext: &[u8]) -> Result<Vec<u8>, Error> {
self.alg.open(&self.nonce.finish(), ad, ciphertext)
}
}
type StreamNonce = [u8; NONCE_SIZE + 4 + 1];
struct NonceEncoder32 {
value: StreamNonce,
counter: u32,
}
impl NonceEncoder32 {
fn new(prefix: &[u8]) -> Self {
if prefix.len() != NONCE_SIZE {
panic!(
"incorrect nonce size (expected {}, got {})",
NONCE_SIZE,
prefix.len()
);
}
let mut result = Self {
value: Default::default(),
counter: 0,
};
result.value[..NONCE_SIZE].copy_from_slice(prefix);
result
}
pub fn increment(&mut self) {
self.counter = self.counter.checked_add(1).expect(
"STREAM nonce counter overflowed",
);
BigEndian::write_u32(&mut self.value[NONCE_SIZE..(NONCE_SIZE + 4)], self.counter);
}
pub fn as_slice(&self) -> &[u8] {
&self.value
}
pub fn finish(mut self) -> StreamNonce {
*self.value.iter_mut().last().unwrap() = LAST_BLOCK_FLAG;
self.value
}
}