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
use crate::{
generic_array::{typenum::U16, ArrayLength, GenericArray},
Error,
};
use aes::{Aes128, Aes256};
use aes_siv::siv::{Siv, IV_SIZE};
use cmac::Cmac;
use core::ops::Add;
use crypto_mac::Mac;
use ctr::Ctr128;
use stream_cipher::{NewStreamCipher, SyncStreamCipher};
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
#[cfg(feature = "pmac")]
use pmac_crate::Pmac;
pub type Tag = GenericArray<u8, U16>;
pub trait Aead {
type KeySize: ArrayLength<u8>;
type TagSize: ArrayLength<u8>;
fn new(key: &[u8]) -> Self;
fn encrypt_in_place(&mut self, nonce: &[u8], associated_data: &[u8], buffer: &mut [u8]);
fn decrypt_in_place<'a>(
&mut self,
nonce: &[u8],
associated_data: &[u8],
buffer: &'a mut [u8],
) -> Result<&'a [u8], Error>;
#[cfg(feature = "alloc")]
fn encrypt(&mut self, nonce: &[u8], associated_data: &[u8], plaintext: &[u8]) -> Vec<u8> {
let mut buffer = vec![0; IV_SIZE + plaintext.len()];
buffer[IV_SIZE..].copy_from_slice(plaintext);
self.encrypt_in_place(nonce, associated_data, &mut buffer);
buffer
}
#[cfg(feature = "alloc")]
fn decrypt(
&mut self,
nonce: &[u8],
associated_data: &[u8],
ciphertext: &[u8],
) -> Result<Vec<u8>, Error> {
let mut buffer = Vec::from(ciphertext);
self.decrypt_in_place(nonce, associated_data, &mut buffer)?;
buffer.drain(..IV_SIZE);
Ok(buffer)
}
}
pub struct SivAead<C, M>
where
C: NewStreamCipher<NonceSize = U16> + SyncStreamCipher,
M: Mac<OutputSize = U16>,
{
siv: Siv<C, M>,
}
pub type CmacSivAead<BlockCipher> = SivAead<Ctr128<BlockCipher>, Cmac<BlockCipher>>;
pub type Aes128SivAead = CmacSivAead<Aes128>;
pub type Aes256SivAead = CmacSivAead<Aes256>;
#[cfg(feature = "pmac")]
pub type PmacSivAead<BlockCipher> = SivAead<Ctr128<BlockCipher>, Pmac<BlockCipher>>;
#[cfg(feature = "pmac")]
pub type Aes128PmacSivAead = PmacSivAead<Aes128>;
#[cfg(feature = "pmac")]
pub type Aes256PmacSivAead = PmacSivAead<Aes256>;
impl<C, M> Aead for SivAead<C, M>
where
C: NewStreamCipher<NonceSize = U16> + SyncStreamCipher,
M: Mac<OutputSize = U16>,
C::KeySize: Add,
<C::KeySize as Add>::Output: ArrayLength<u8>,
{
type KeySize = <C as NewStreamCipher>::KeySize;
type TagSize = U16;
fn new(key: &[u8]) -> Self {
Self {
siv: Siv::new(GenericArray::clone_from_slice(key)),
}
}
fn encrypt_in_place(&mut self, nonce: &[u8], associated_data: &[u8], buffer: &mut [u8]) {
assert!(buffer.len() >= IV_SIZE, "no space for IV in buffer");
let tag = self
.siv
.encrypt_in_place_detached(&[associated_data, nonce], &mut buffer[IV_SIZE..])
.expect("encryption failure!");
buffer[..IV_SIZE].copy_from_slice(&tag);
}
fn decrypt_in_place<'a>(
&mut self,
nonce: &[u8],
associated_data: &[u8],
buffer: &'a mut [u8],
) -> Result<&'a [u8], Error> {
if buffer.len() < IV_SIZE {
return Err(Error);
}
let tag = Tag::clone_from_slice(&buffer[..IV_SIZE]);
self.siv.decrypt_in_place_detached(
&[associated_data, nonce],
&mut buffer[IV_SIZE..],
&tag,
)?;
Ok(&buffer[IV_SIZE..])
}
}