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
use aesni::{Aes128, Aes256};
use cmac::Cmac;
use crypto_mac::Mac;
use ctr::{Aes128Ctr, Aes256Ctr, Ctr, IV_SIZE};
use error::Error;
use generic_array::GenericArray;
use generic_array::typenum::{U16, Unsigned};
use pmac::Pmac;
use s2v::s2v;
use subtle;
pub struct Siv<C: Ctr, M: Mac> {
mac: M,
ctr: C,
}
pub type Aes128Siv = Siv<Aes128Ctr, Cmac<Aes128>>;
pub type Aes256Siv = Siv<Aes256Ctr, Cmac<Aes256>>;
pub type Aes128PmacSiv = Siv<Aes128Ctr, Pmac<Aes128>>;
pub type Aes256PmacSiv = Siv<Aes256Ctr, Pmac<Aes256>>;
impl<C: Ctr, M: Mac<OutputSize = U16>> Siv<C, M> {
pub fn new(key: &[u8]) -> Self {
let key_size = M::KeySize::to_usize() * 2;
assert_eq!(
key.len(),
key_size,
"expected {}-byte key, got {}",
key_size,
key.len()
);
Self {
mac: M::new(GenericArray::from_slice(&key[..(key_size / 2)])),
ctr: C::new(&key[(key_size / 2)..]),
}
}
pub fn seal_in_place<I, T>(&mut self, associated_data: I, plaintext: &mut [u8])
where
I: IntoIterator<Item = T>,
T: AsRef<[u8]>,
{
if plaintext.len() < IV_SIZE {
panic!("plaintext buffer too small to hold MAC tag!");
}
let iv = s2v(&mut self.mac, associated_data, &plaintext[IV_SIZE..]);
plaintext[..IV_SIZE].copy_from_slice(iv.as_slice());
self.ctr.xor_in_place(
&zero_iv_bits(&iv),
&mut plaintext[IV_SIZE..],
);
}
pub fn open_in_place<'a, I, T>(
&mut self,
associated_data: I,
ciphertext: &'a mut [u8],
) -> Result<&'a [u8], Error>
where
I: IntoIterator<Item = T>,
T: AsRef<[u8]>,
{
if ciphertext.len() < IV_SIZE {
return Err(Error);
}
let iv = zero_iv_bits(&ciphertext[..IV_SIZE]);
self.ctr.xor_in_place(&iv, &mut ciphertext[IV_SIZE..]);
let actual_tag = s2v(&mut self.mac, associated_data, &ciphertext[IV_SIZE..]);
if subtle::slices_equal(actual_tag.as_slice(), &ciphertext[..IV_SIZE]) != 1 {
self.ctr.xor_in_place(&iv, &mut ciphertext[IV_SIZE..]);
return Err(Error);
}
Ok(&ciphertext[IV_SIZE..])
}
#[cfg(feature = "std")]
pub fn seal<I, T>(&mut self, associated_data: I, plaintext: &[u8]) -> Vec<u8>
where
I: IntoIterator<Item = T>,
T: AsRef<[u8]>,
{
let mut buffer = vec![0; IV_SIZE + plaintext.len()];
buffer[IV_SIZE..].copy_from_slice(plaintext);
self.seal_in_place(associated_data, &mut buffer);
buffer
}
#[cfg(feature = "std")]
pub fn open<I, T>(&mut self, associated_data: I, ciphertext: &[u8]) -> Result<Vec<u8>, Error>
where
I: IntoIterator<Item = T>,
T: AsRef<[u8]>,
{
let mut buffer = Vec::from(ciphertext);
self.open_in_place(associated_data, &mut buffer)?;
buffer.drain(..IV_SIZE);
Ok(buffer)
}
}
#[inline]
fn zero_iv_bits(iv: &[u8]) -> [u8; IV_SIZE] {
debug_assert_eq!(iv.len(), IV_SIZE, "wrong IV size: {}", iv.len());
let mut result = [0u8; IV_SIZE];
result.copy_from_slice(iv);
result[8] &= 0x7f;
result[12] &= 0x7f;
result
}