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
use core::num::NonZeroUsize;
use generic_array::{typenum::Unsigned, ArrayLength, GenericArray};
pub mod consts {
pub use generic_array::typenum::*;
}
pub type Key<T> = GenericArray<u8, <T as Aead>::KeyLength>;
pub type Nonce<T> = GenericArray<u8, <T as Aead>::NonceLength>;
pub type Tag<T> = GenericArray<u8, <T as Aead>::TagLength>;
pub trait Aead {
type KeyLength: ArrayLength<u8>;
type NonceLength: ArrayLength<u8>;
type TagLength: ArrayLength<u8>;
const NAME: &'static str;
const KEY_LENGTH: usize = <Self::KeyLength as Unsigned>::USIZE;
const NONCE_LENGTH: usize = <Self::NonceLength as Unsigned>::USIZE;
const TAG_LENGTH: usize = <Self::TagLength as Unsigned>::USIZE;
fn encrypt(
key: &Key<Self>,
nonce: &Nonce<Self>,
associated_data: &[u8],
plaintext: &[u8],
ciphertext: &mut [u8],
tag: &mut Tag<Self>,
) -> crate::Result<()>;
fn decrypt(
key: &Key<Self>,
nonce: &Nonce<Self>,
associated_data: &[u8],
plaintext: &mut [u8],
ciphertext: &[u8],
tag: &Tag<Self>,
) -> crate::Result<usize>;
fn try_encrypt(
key: &[u8],
nonce: &[u8],
associated_data: &[u8],
plaintext: &[u8],
ciphertext: &mut [u8],
tag: &mut [u8],
) -> crate::Result<()> {
let key: &Key<Self> = try_generic_array(key, "key")?;
let nonce: &Nonce<Self> = try_generic_array(nonce, "nonce")?;
let tag: &mut Tag<Self> = try_generic_array_mut(tag, "tag")?;
Self::encrypt(key, nonce, associated_data, plaintext, ciphertext, tag)
}
fn try_decrypt(
key: &[u8],
nonce: &[u8],
associated_data: &[u8],
plaintext: &mut [u8],
ciphertext: &[u8],
tag: &[u8],
) -> crate::Result<usize> {
let key: &Key<Self> = try_generic_array(key, "key")?;
let nonce: &Nonce<Self> = try_generic_array(nonce, "nonce")?;
let tag: &Tag<Self> = try_generic_array(tag, "tag")?;
Self::decrypt(key, nonce, associated_data, plaintext, ciphertext, tag)
}
#[cfg(feature = "random")]
fn random_nonce() -> crate::Result<Nonce<Self>> {
let mut nonce: Nonce<Self> = Default::default();
crate::utils::rand::fill(&mut nonce)?;
Ok(nonce)
}
fn padsize(_: &[u8]) -> Option<NonZeroUsize> {
None
}
}
#[inline(always)]
fn try_generic_array<'a, T>(slice: &'a [u8], name: &'static str) -> crate::Result<&'a GenericArray<u8, T>>
where
T: ArrayLength<u8>,
{
if slice.len() == T::USIZE {
Ok(slice.into())
} else {
Err(crate::Error::BufferSize {
name,
needs: T::USIZE,
has: slice.len(),
})
}
}
#[inline(always)]
fn try_generic_array_mut<'a, T>(slice: &'a mut [u8], name: &'static str) -> crate::Result<&'a mut GenericArray<u8, T>>
where
T: ArrayLength<u8>,
{
if slice.len() == T::USIZE {
Ok(slice.into())
} else {
Err(crate::Error::BufferSize {
name,
needs: T::USIZE,
has: slice.len(),
})
}
}