Skip to content

Commit b66c28f

Browse files
committed
aead: extract AeadCore trait
Extracts a single trait for defining nonce, tag, and overhead sizes instead of repetitively defining those associated types on every other trait. This allows for fully generic `Nonce<A>` and `Tag<A>` type aliases.
1 parent 1b53965 commit b66c28f

File tree

2 files changed

+54
-90
lines changed

2 files changed

+54
-90
lines changed

aead/src/lib.rs

+44-80
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ impl std::error::Error for Error {}
6868
pub type Key<A> = GenericArray<u8, <A as NewAead>::KeySize>;
6969

7070
/// Nonce: single-use value for ensuring ciphertexts are unique
71-
pub type Nonce<NonceSize> = GenericArray<u8, NonceSize>;
71+
pub type Nonce<A> = GenericArray<u8, <A as AeadCore>::NonceSize>;
7272

7373
/// Tag: authentication code which ensures ciphertexts are authentic
74-
pub type Tag<TagSize> = GenericArray<u8, TagSize>;
74+
pub type Tag<A> = GenericArray<u8, <A as AeadCore>::TagSize>;
7575

7676
/// Instantiate either a stateless [`Aead`] or stateful [`AeadMut`] algorithm.
7777
pub trait NewAead {
@@ -96,13 +96,11 @@ pub trait NewAead {
9696
}
9797
}
9898

99-
/// Authenticated Encryption with Associated Data (AEAD) algorithm.
99+
/// Authenticated Encryption with Associated Data (AEAD) algorithm core trait.
100100
///
101-
/// This trait is intended for use with stateless AEAD algorithms. The
102-
/// [`AeadMut`] trait provides a stateful interface.
103-
#[cfg(feature = "alloc")]
104-
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
105-
pub trait Aead {
101+
/// Defines nonce, tag, and overhead sizes that are consumed by various other
102+
/// `Aead*` traits.
103+
pub trait AeadCore {
106104
/// The length of a nonce.
107105
type NonceSize: ArrayLength<u8>;
108106

@@ -112,7 +110,15 @@ pub trait Aead {
112110
/// The upper bound amount of additional space required to support a
113111
/// ciphertext vs. a plaintext.
114112
type CiphertextOverhead: ArrayLength<u8> + Unsigned;
113+
}
115114

115+
/// Authenticated Encryption with Associated Data (AEAD) algorithm.
116+
///
117+
/// This trait is intended for use with stateless AEAD algorithms. The
118+
/// [`AeadMut`] trait provides a stateful interface.
119+
#[cfg(feature = "alloc")]
120+
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
121+
pub trait Aead: AeadCore {
116122
/// Encrypt the given plaintext payload, and return the resulting
117123
/// ciphertext as a vector of bytes.
118124
///
@@ -138,7 +144,7 @@ pub trait Aead {
138144
/// ciphertext message.
139145
fn encrypt<'msg, 'aad>(
140146
&self,
141-
nonce: &Nonce<Self::NonceSize>,
147+
nonce: &Nonce<Self>,
142148
plaintext: impl Into<Payload<'msg, 'aad>>,
143149
) -> Result<Vec<u8>, Error>;
144150

@@ -161,33 +167,23 @@ pub trait Aead {
161167
/// ciphertext message.
162168
fn decrypt<'msg, 'aad>(
163169
&self,
164-
nonce: &Nonce<Self::NonceSize>,
170+
nonce: &Nonce<Self>,
165171
ciphertext: impl Into<Payload<'msg, 'aad>>,
166172
) -> Result<Vec<u8>, Error>;
167173
}
168174

169175
/// Stateful Authenticated Encryption with Associated Data algorithm.
170176
#[cfg(feature = "alloc")]
171177
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
172-
pub trait AeadMut {
173-
/// The length of a nonce.
174-
type NonceSize: ArrayLength<u8>;
175-
176-
/// The maximum length of the nonce.
177-
type TagSize: ArrayLength<u8>;
178-
179-
/// The upper bound amount of additional space required to support a
180-
/// ciphertext vs. a plaintext.
181-
type CiphertextOverhead: ArrayLength<u8> + Unsigned;
182-
178+
pub trait AeadMut: AeadCore {
183179
/// Encrypt the given plaintext slice, and return the resulting ciphertext
184180
/// as a vector of bytes.
185181
///
186182
/// See notes on [`Aead::encrypt()`] about allowable message payloads and
187183
/// Associated Additional Data (AAD).
188184
fn encrypt<'msg, 'aad>(
189185
&mut self,
190-
nonce: &Nonce<Self::NonceSize>,
186+
nonce: &Nonce<Self>,
191187
plaintext: impl Into<Payload<'msg, 'aad>>,
192188
) -> Result<Vec<u8>, Error>;
193189

@@ -198,7 +194,7 @@ pub trait AeadMut {
198194
/// message payloads and Associated Additional Data (AAD).
199195
fn decrypt<'msg, 'aad>(
200196
&mut self,
201-
nonce: &Nonce<Self::NonceSize>,
197+
nonce: &Nonce<Self>,
202198
ciphertext: impl Into<Payload<'msg, 'aad>>,
203199
) -> Result<Vec<u8>, Error>;
204200
}
@@ -216,7 +212,7 @@ macro_rules! impl_decrypt_in_place {
216212

217213
let tag_pos = $buffer.len() - Self::TagSize::to_usize();
218214
let (msg, tag) = $buffer.as_mut().split_at_mut(tag_pos);
219-
$aead.decrypt_in_place_detached($nonce, $aad, msg, Tag::from_slice(tag))?;
215+
$aead.decrypt_in_place_detached($nonce, $aad, msg, Tag::<Self>::from_slice(tag))?;
220216
$buffer.truncate(tag_pos);
221217
Ok(())
222218
}};
@@ -225,17 +221,7 @@ macro_rules! impl_decrypt_in_place {
225221
/// In-place stateless AEAD trait.
226222
///
227223
/// This trait is both object safe and has no dependencies on `alloc` or `std`.
228-
pub trait AeadInPlace {
229-
/// The length of a nonce.
230-
type NonceSize: ArrayLength<u8>;
231-
232-
/// The maximum length of the nonce.
233-
type TagSize: ArrayLength<u8>;
234-
235-
/// The upper bound amount of additional space required to support a
236-
/// ciphertext vs. a plaintext.
237-
type CiphertextOverhead: ArrayLength<u8> + Unsigned;
238-
224+
pub trait AeadInPlace: AeadCore {
239225
/// Encrypt the given buffer containing a plaintext message in-place.
240226
///
241227
/// The buffer must have sufficient capacity to store the ciphertext
@@ -247,7 +233,7 @@ pub trait AeadInPlace {
247233
/// resulting ciphertext message.
248234
fn encrypt_in_place(
249235
&self,
250-
nonce: &Nonce<Self::NonceSize>,
236+
nonce: &Nonce<Self>,
251237
associated_data: &[u8],
252238
buffer: &mut dyn Buffer,
253239
) -> Result<(), Error> {
@@ -259,10 +245,10 @@ pub trait AeadInPlace {
259245
/// Encrypt the data in-place, returning the authentication tag
260246
fn encrypt_in_place_detached(
261247
&self,
262-
nonce: &Nonce<Self::NonceSize>,
248+
nonce: &Nonce<Self>,
263249
associated_data: &[u8],
264250
buffer: &mut [u8],
265-
) -> Result<Tag<Self::TagSize>, Error>;
251+
) -> Result<Tag<Self>, Error>;
266252

267253
/// Decrypt the message in-place, returning an error in the event the
268254
/// provided authentication tag does not match the given ciphertext.
@@ -271,7 +257,7 @@ pub trait AeadInPlace {
271257
/// message upon success.
272258
fn decrypt_in_place(
273259
&self,
274-
nonce: &Nonce<Self::NonceSize>,
260+
nonce: &Nonce<Self>,
275261
associated_data: &[u8],
276262
buffer: &mut dyn Buffer,
277263
) -> Result<(), Error> {
@@ -283,27 +269,17 @@ pub trait AeadInPlace {
283269
/// is modified/unauthentic)
284270
fn decrypt_in_place_detached(
285271
&self,
286-
nonce: &Nonce<Self::NonceSize>,
272+
nonce: &Nonce<Self>,
287273
associated_data: &[u8],
288274
buffer: &mut [u8],
289-
tag: &Tag<Self::TagSize>,
275+
tag: &Tag<Self>,
290276
) -> Result<(), Error>;
291277
}
292278

293279
/// In-place stateful AEAD trait.
294280
///
295281
/// This trait is both object safe and has no dependencies on `alloc` or `std`.
296-
pub trait AeadMutInPlace {
297-
/// The length of a nonce.
298-
type NonceSize: ArrayLength<u8>;
299-
300-
/// The maximum length of the nonce.
301-
type TagSize: ArrayLength<u8>;
302-
303-
/// The upper bound amount of additional space required to support a
304-
/// ciphertext vs. a plaintext.
305-
type CiphertextOverhead: ArrayLength<u8> + Unsigned;
306-
282+
pub trait AeadMutInPlace: AeadCore {
307283
/// Encrypt the given buffer containing a plaintext message in-place.
308284
///
309285
/// The buffer must have sufficient capacity to store the ciphertext
@@ -315,7 +291,7 @@ pub trait AeadMutInPlace {
315291
/// resulting ciphertext message.
316292
fn encrypt_in_place(
317293
&mut self,
318-
nonce: &Nonce<Self::NonceSize>,
294+
nonce: &Nonce<Self>,
319295
associated_data: &[u8],
320296
buffer: &mut impl Buffer,
321297
) -> Result<(), Error> {
@@ -327,10 +303,10 @@ pub trait AeadMutInPlace {
327303
/// Encrypt the data in-place, returning the authentication tag
328304
fn encrypt_in_place_detached(
329305
&mut self,
330-
nonce: &Nonce<Self::NonceSize>,
306+
nonce: &Nonce<Self>,
331307
associated_data: &[u8],
332308
buffer: &mut [u8],
333-
) -> Result<Tag<Self::TagSize>, Error>;
309+
) -> Result<Tag<Self>, Error>;
334310

335311
/// Decrypt the message in-place, returning an error in the event the
336312
/// provided authentication tag does not match the given ciphertext.
@@ -339,7 +315,7 @@ pub trait AeadMutInPlace {
339315
/// message upon success.
340316
fn decrypt_in_place(
341317
&mut self,
342-
nonce: &Nonce<Self::NonceSize>,
318+
nonce: &Nonce<Self>,
343319
associated_data: &[u8],
344320
buffer: &mut impl Buffer,
345321
) -> Result<(), Error> {
@@ -351,22 +327,18 @@ pub trait AeadMutInPlace {
351327
/// is modified/unauthentic)
352328
fn decrypt_in_place_detached(
353329
&mut self,
354-
nonce: &Nonce<Self::NonceSize>,
330+
nonce: &Nonce<Self>,
355331
associated_data: &[u8],
356332
buffer: &mut [u8],
357-
tag: &Tag<Self::TagSize>,
333+
tag: &Tag<Self>,
358334
) -> Result<(), Error>;
359335
}
360336

361337
#[cfg(feature = "alloc")]
362338
impl<Alg: AeadInPlace> Aead for Alg {
363-
type NonceSize = Alg::NonceSize;
364-
type TagSize = Alg::TagSize;
365-
type CiphertextOverhead = Alg::CiphertextOverhead;
366-
367339
fn encrypt<'msg, 'aad>(
368340
&self,
369-
nonce: &Nonce<Self::NonceSize>,
341+
nonce: &Nonce<Self>,
370342
plaintext: impl Into<Payload<'msg, 'aad>>,
371343
) -> Result<Vec<u8>, Error> {
372344
let payload = plaintext.into();
@@ -378,7 +350,7 @@ impl<Alg: AeadInPlace> Aead for Alg {
378350

379351
fn decrypt<'msg, 'aad>(
380352
&self,
381-
nonce: &Nonce<Self::NonceSize>,
353+
nonce: &Nonce<Self>,
382354
ciphertext: impl Into<Payload<'msg, 'aad>>,
383355
) -> Result<Vec<u8>, Error> {
384356
let payload = ciphertext.into();
@@ -390,13 +362,9 @@ impl<Alg: AeadInPlace> Aead for Alg {
390362

391363
#[cfg(feature = "alloc")]
392364
impl<Alg: AeadMutInPlace> AeadMut for Alg {
393-
type NonceSize = Alg::NonceSize;
394-
type TagSize = Alg::TagSize;
395-
type CiphertextOverhead = Alg::CiphertextOverhead;
396-
397365
fn encrypt<'msg, 'aad>(
398366
&mut self,
399-
nonce: &Nonce<Self::NonceSize>,
367+
nonce: &Nonce<Self>,
400368
plaintext: impl Into<Payload<'msg, 'aad>>,
401369
) -> Result<Vec<u8>, Error> {
402370
let payload = plaintext.into();
@@ -408,7 +376,7 @@ impl<Alg: AeadMutInPlace> AeadMut for Alg {
408376

409377
fn decrypt<'msg, 'aad>(
410378
&mut self,
411-
nonce: &Nonce<Self::NonceSize>,
379+
nonce: &Nonce<Self>,
412380
ciphertext: impl Into<Payload<'msg, 'aad>>,
413381
) -> Result<Vec<u8>, Error> {
414382
let payload = ciphertext.into();
@@ -419,13 +387,9 @@ impl<Alg: AeadMutInPlace> AeadMut for Alg {
419387
}
420388

421389
impl<Alg: AeadInPlace> AeadMutInPlace for Alg {
422-
type NonceSize = Alg::NonceSize;
423-
type TagSize = Alg::TagSize;
424-
type CiphertextOverhead = Alg::CiphertextOverhead;
425-
426390
fn encrypt_in_place(
427391
&mut self,
428-
nonce: &Nonce<Self::NonceSize>,
392+
nonce: &Nonce<Self>,
429393
associated_data: &[u8],
430394
buffer: &mut impl Buffer,
431395
) -> Result<(), Error> {
@@ -434,16 +398,16 @@ impl<Alg: AeadInPlace> AeadMutInPlace for Alg {
434398

435399
fn encrypt_in_place_detached(
436400
&mut self,
437-
nonce: &Nonce<Self::NonceSize>,
401+
nonce: &Nonce<Self>,
438402
associated_data: &[u8],
439403
buffer: &mut [u8],
440-
) -> Result<Tag<Self::TagSize>, Error> {
404+
) -> Result<Tag<Self>, Error> {
441405
<Self as AeadInPlace>::encrypt_in_place_detached(self, nonce, associated_data, buffer)
442406
}
443407

444408
fn decrypt_in_place(
445409
&mut self,
446-
nonce: &Nonce<Self::NonceSize>,
410+
nonce: &Nonce<Self>,
447411
associated_data: &[u8],
448412
buffer: &mut impl Buffer,
449413
) -> Result<(), Error> {
@@ -452,10 +416,10 @@ impl<Alg: AeadInPlace> AeadMutInPlace for Alg {
452416

453417
fn decrypt_in_place_detached(
454418
&mut self,
455-
nonce: &Nonce<Self::NonceSize>,
419+
nonce: &Nonce<Self>,
456420
associated_data: &[u8],
457421
buffer: &mut [u8],
458-
tag: &Tag<Self::TagSize>,
422+
tag: &Tag<Self>,
459423
) -> Result<(), Error> {
460424
<Self as AeadInPlace>::decrypt_in_place_detached(self, nonce, associated_data, buffer, tag)
461425
}

0 commit comments

Comments
 (0)