@@ -68,10 +68,10 @@ impl std::error::Error for Error {}
68
68
pub type Key < A > = GenericArray < u8 , <A as NewAead >:: KeySize > ;
69
69
70
70
/// 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 > ;
72
72
73
73
/// 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 > ;
75
75
76
76
/// Instantiate either a stateless [`Aead`] or stateful [`AeadMut`] algorithm.
77
77
pub trait NewAead {
@@ -96,13 +96,11 @@ pub trait NewAead {
96
96
}
97
97
}
98
98
99
- /// Authenticated Encryption with Associated Data (AEAD) algorithm.
99
+ /// Authenticated Encryption with Associated Data (AEAD) algorithm core trait .
100
100
///
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 {
106
104
/// The length of a nonce.
107
105
type NonceSize : ArrayLength < u8 > ;
108
106
@@ -112,7 +110,15 @@ pub trait Aead {
112
110
/// The upper bound amount of additional space required to support a
113
111
/// ciphertext vs. a plaintext.
114
112
type CiphertextOverhead : ArrayLength < u8 > + Unsigned ;
113
+ }
115
114
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 {
116
122
/// Encrypt the given plaintext payload, and return the resulting
117
123
/// ciphertext as a vector of bytes.
118
124
///
@@ -138,7 +144,7 @@ pub trait Aead {
138
144
/// ciphertext message.
139
145
fn encrypt < ' msg , ' aad > (
140
146
& self ,
141
- nonce : & Nonce < Self :: NonceSize > ,
147
+ nonce : & Nonce < Self > ,
142
148
plaintext : impl Into < Payload < ' msg , ' aad > > ,
143
149
) -> Result < Vec < u8 > , Error > ;
144
150
@@ -161,33 +167,23 @@ pub trait Aead {
161
167
/// ciphertext message.
162
168
fn decrypt < ' msg , ' aad > (
163
169
& self ,
164
- nonce : & Nonce < Self :: NonceSize > ,
170
+ nonce : & Nonce < Self > ,
165
171
ciphertext : impl Into < Payload < ' msg , ' aad > > ,
166
172
) -> Result < Vec < u8 > , Error > ;
167
173
}
168
174
169
175
/// Stateful Authenticated Encryption with Associated Data algorithm.
170
176
#[ cfg( feature = "alloc" ) ]
171
177
#[ 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 {
183
179
/// Encrypt the given plaintext slice, and return the resulting ciphertext
184
180
/// as a vector of bytes.
185
181
///
186
182
/// See notes on [`Aead::encrypt()`] about allowable message payloads and
187
183
/// Associated Additional Data (AAD).
188
184
fn encrypt < ' msg , ' aad > (
189
185
& mut self ,
190
- nonce : & Nonce < Self :: NonceSize > ,
186
+ nonce : & Nonce < Self > ,
191
187
plaintext : impl Into < Payload < ' msg , ' aad > > ,
192
188
) -> Result < Vec < u8 > , Error > ;
193
189
@@ -198,7 +194,7 @@ pub trait AeadMut {
198
194
/// message payloads and Associated Additional Data (AAD).
199
195
fn decrypt < ' msg , ' aad > (
200
196
& mut self ,
201
- nonce : & Nonce < Self :: NonceSize > ,
197
+ nonce : & Nonce < Self > ,
202
198
ciphertext : impl Into < Payload < ' msg , ' aad > > ,
203
199
) -> Result < Vec < u8 > , Error > ;
204
200
}
@@ -216,7 +212,7 @@ macro_rules! impl_decrypt_in_place {
216
212
217
213
let tag_pos = $buffer. len( ) - Self :: TagSize :: to_usize( ) ;
218
214
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) ) ?;
220
216
$buffer. truncate( tag_pos) ;
221
217
Ok ( ( ) )
222
218
} } ;
@@ -225,17 +221,7 @@ macro_rules! impl_decrypt_in_place {
225
221
/// In-place stateless AEAD trait.
226
222
///
227
223
/// 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 {
239
225
/// Encrypt the given buffer containing a plaintext message in-place.
240
226
///
241
227
/// The buffer must have sufficient capacity to store the ciphertext
@@ -247,7 +233,7 @@ pub trait AeadInPlace {
247
233
/// resulting ciphertext message.
248
234
fn encrypt_in_place (
249
235
& self ,
250
- nonce : & Nonce < Self :: NonceSize > ,
236
+ nonce : & Nonce < Self > ,
251
237
associated_data : & [ u8 ] ,
252
238
buffer : & mut dyn Buffer ,
253
239
) -> Result < ( ) , Error > {
@@ -259,10 +245,10 @@ pub trait AeadInPlace {
259
245
/// Encrypt the data in-place, returning the authentication tag
260
246
fn encrypt_in_place_detached (
261
247
& self ,
262
- nonce : & Nonce < Self :: NonceSize > ,
248
+ nonce : & Nonce < Self > ,
263
249
associated_data : & [ u8 ] ,
264
250
buffer : & mut [ u8 ] ,
265
- ) -> Result < Tag < Self :: TagSize > , Error > ;
251
+ ) -> Result < Tag < Self > , Error > ;
266
252
267
253
/// Decrypt the message in-place, returning an error in the event the
268
254
/// provided authentication tag does not match the given ciphertext.
@@ -271,7 +257,7 @@ pub trait AeadInPlace {
271
257
/// message upon success.
272
258
fn decrypt_in_place (
273
259
& self ,
274
- nonce : & Nonce < Self :: NonceSize > ,
260
+ nonce : & Nonce < Self > ,
275
261
associated_data : & [ u8 ] ,
276
262
buffer : & mut dyn Buffer ,
277
263
) -> Result < ( ) , Error > {
@@ -283,27 +269,17 @@ pub trait AeadInPlace {
283
269
/// is modified/unauthentic)
284
270
fn decrypt_in_place_detached (
285
271
& self ,
286
- nonce : & Nonce < Self :: NonceSize > ,
272
+ nonce : & Nonce < Self > ,
287
273
associated_data : & [ u8 ] ,
288
274
buffer : & mut [ u8 ] ,
289
- tag : & Tag < Self :: TagSize > ,
275
+ tag : & Tag < Self > ,
290
276
) -> Result < ( ) , Error > ;
291
277
}
292
278
293
279
/// In-place stateful AEAD trait.
294
280
///
295
281
/// 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 {
307
283
/// Encrypt the given buffer containing a plaintext message in-place.
308
284
///
309
285
/// The buffer must have sufficient capacity to store the ciphertext
@@ -315,7 +291,7 @@ pub trait AeadMutInPlace {
315
291
/// resulting ciphertext message.
316
292
fn encrypt_in_place (
317
293
& mut self ,
318
- nonce : & Nonce < Self :: NonceSize > ,
294
+ nonce : & Nonce < Self > ,
319
295
associated_data : & [ u8 ] ,
320
296
buffer : & mut impl Buffer ,
321
297
) -> Result < ( ) , Error > {
@@ -327,10 +303,10 @@ pub trait AeadMutInPlace {
327
303
/// Encrypt the data in-place, returning the authentication tag
328
304
fn encrypt_in_place_detached (
329
305
& mut self ,
330
- nonce : & Nonce < Self :: NonceSize > ,
306
+ nonce : & Nonce < Self > ,
331
307
associated_data : & [ u8 ] ,
332
308
buffer : & mut [ u8 ] ,
333
- ) -> Result < Tag < Self :: TagSize > , Error > ;
309
+ ) -> Result < Tag < Self > , Error > ;
334
310
335
311
/// Decrypt the message in-place, returning an error in the event the
336
312
/// provided authentication tag does not match the given ciphertext.
@@ -339,7 +315,7 @@ pub trait AeadMutInPlace {
339
315
/// message upon success.
340
316
fn decrypt_in_place (
341
317
& mut self ,
342
- nonce : & Nonce < Self :: NonceSize > ,
318
+ nonce : & Nonce < Self > ,
343
319
associated_data : & [ u8 ] ,
344
320
buffer : & mut impl Buffer ,
345
321
) -> Result < ( ) , Error > {
@@ -351,22 +327,18 @@ pub trait AeadMutInPlace {
351
327
/// is modified/unauthentic)
352
328
fn decrypt_in_place_detached (
353
329
& mut self ,
354
- nonce : & Nonce < Self :: NonceSize > ,
330
+ nonce : & Nonce < Self > ,
355
331
associated_data : & [ u8 ] ,
356
332
buffer : & mut [ u8 ] ,
357
- tag : & Tag < Self :: TagSize > ,
333
+ tag : & Tag < Self > ,
358
334
) -> Result < ( ) , Error > ;
359
335
}
360
336
361
337
#[ cfg( feature = "alloc" ) ]
362
338
impl < Alg : AeadInPlace > Aead for Alg {
363
- type NonceSize = Alg :: NonceSize ;
364
- type TagSize = Alg :: TagSize ;
365
- type CiphertextOverhead = Alg :: CiphertextOverhead ;
366
-
367
339
fn encrypt < ' msg , ' aad > (
368
340
& self ,
369
- nonce : & Nonce < Self :: NonceSize > ,
341
+ nonce : & Nonce < Self > ,
370
342
plaintext : impl Into < Payload < ' msg , ' aad > > ,
371
343
) -> Result < Vec < u8 > , Error > {
372
344
let payload = plaintext. into ( ) ;
@@ -378,7 +350,7 @@ impl<Alg: AeadInPlace> Aead for Alg {
378
350
379
351
fn decrypt < ' msg , ' aad > (
380
352
& self ,
381
- nonce : & Nonce < Self :: NonceSize > ,
353
+ nonce : & Nonce < Self > ,
382
354
ciphertext : impl Into < Payload < ' msg , ' aad > > ,
383
355
) -> Result < Vec < u8 > , Error > {
384
356
let payload = ciphertext. into ( ) ;
@@ -390,13 +362,9 @@ impl<Alg: AeadInPlace> Aead for Alg {
390
362
391
363
#[ cfg( feature = "alloc" ) ]
392
364
impl < Alg : AeadMutInPlace > AeadMut for Alg {
393
- type NonceSize = Alg :: NonceSize ;
394
- type TagSize = Alg :: TagSize ;
395
- type CiphertextOverhead = Alg :: CiphertextOverhead ;
396
-
397
365
fn encrypt < ' msg , ' aad > (
398
366
& mut self ,
399
- nonce : & Nonce < Self :: NonceSize > ,
367
+ nonce : & Nonce < Self > ,
400
368
plaintext : impl Into < Payload < ' msg , ' aad > > ,
401
369
) -> Result < Vec < u8 > , Error > {
402
370
let payload = plaintext. into ( ) ;
@@ -408,7 +376,7 @@ impl<Alg: AeadMutInPlace> AeadMut for Alg {
408
376
409
377
fn decrypt < ' msg , ' aad > (
410
378
& mut self ,
411
- nonce : & Nonce < Self :: NonceSize > ,
379
+ nonce : & Nonce < Self > ,
412
380
ciphertext : impl Into < Payload < ' msg , ' aad > > ,
413
381
) -> Result < Vec < u8 > , Error > {
414
382
let payload = ciphertext. into ( ) ;
@@ -419,13 +387,9 @@ impl<Alg: AeadMutInPlace> AeadMut for Alg {
419
387
}
420
388
421
389
impl < Alg : AeadInPlace > AeadMutInPlace for Alg {
422
- type NonceSize = Alg :: NonceSize ;
423
- type TagSize = Alg :: TagSize ;
424
- type CiphertextOverhead = Alg :: CiphertextOverhead ;
425
-
426
390
fn encrypt_in_place (
427
391
& mut self ,
428
- nonce : & Nonce < Self :: NonceSize > ,
392
+ nonce : & Nonce < Self > ,
429
393
associated_data : & [ u8 ] ,
430
394
buffer : & mut impl Buffer ,
431
395
) -> Result < ( ) , Error > {
@@ -434,16 +398,16 @@ impl<Alg: AeadInPlace> AeadMutInPlace for Alg {
434
398
435
399
fn encrypt_in_place_detached (
436
400
& mut self ,
437
- nonce : & Nonce < Self :: NonceSize > ,
401
+ nonce : & Nonce < Self > ,
438
402
associated_data : & [ u8 ] ,
439
403
buffer : & mut [ u8 ] ,
440
- ) -> Result < Tag < Self :: TagSize > , Error > {
404
+ ) -> Result < Tag < Self > , Error > {
441
405
<Self as AeadInPlace >:: encrypt_in_place_detached ( self , nonce, associated_data, buffer)
442
406
}
443
407
444
408
fn decrypt_in_place (
445
409
& mut self ,
446
- nonce : & Nonce < Self :: NonceSize > ,
410
+ nonce : & Nonce < Self > ,
447
411
associated_data : & [ u8 ] ,
448
412
buffer : & mut impl Buffer ,
449
413
) -> Result < ( ) , Error > {
@@ -452,10 +416,10 @@ impl<Alg: AeadInPlace> AeadMutInPlace for Alg {
452
416
453
417
fn decrypt_in_place_detached (
454
418
& mut self ,
455
- nonce : & Nonce < Self :: NonceSize > ,
419
+ nonce : & Nonce < Self > ,
456
420
associated_data : & [ u8 ] ,
457
421
buffer : & mut [ u8 ] ,
458
- tag : & Tag < Self :: TagSize > ,
422
+ tag : & Tag < Self > ,
459
423
) -> Result < ( ) , Error > {
460
424
<Self as AeadInPlace >:: decrypt_in_place_detached ( self , nonce, associated_data, buffer, tag)
461
425
}
0 commit comments