@@ -268,6 +268,44 @@ impl<N: NonceSequence> core::fmt::Debug for SealingKey<N> {
268
268
}
269
269
270
270
impl < N : NonceSequence > SealingKey < N > {
271
+ /// Deprecated. Renamed to [`seal_in_place_append_tag()`].
272
+ #[ deprecated( note = "Renamed to `seal_in_place_append_tag`." ) ]
273
+ #[ inline]
274
+ pub fn seal_in_place < A , InOut > (
275
+ & mut self ,
276
+ aad : Aad < A > ,
277
+ in_out : & mut InOut ,
278
+ ) -> Result < ( ) , error:: Unspecified >
279
+ where
280
+ A : AsRef < [ u8 ] > ,
281
+ InOut : AsMut < [ u8 ] > + for < ' in_out > Extend < & ' in_out u8 > ,
282
+ {
283
+ self . seal_in_place_append_tag ( aad, in_out)
284
+ }
285
+
286
+ /// Encrypts and signs (“seals”) data in place, appending the tag to the
287
+ /// resulting ciphertext.
288
+ ///
289
+ /// `key.seal_in_place_append_tag(aad, in_out)` is equivalent to:
290
+ ///
291
+ /// ```skip
292
+ /// key.seal_in_place_separate_tag(aad, in_out.as_mut())
293
+ /// .map(|tag| in_out.extend(tag.as_ref()))
294
+ /// ```
295
+ #[ inline]
296
+ pub fn seal_in_place_append_tag < A , InOut > (
297
+ & mut self ,
298
+ aad : Aad < A > ,
299
+ in_out : & mut InOut ,
300
+ ) -> Result < ( ) , error:: Unspecified >
301
+ where
302
+ A : AsRef < [ u8 ] > ,
303
+ InOut : AsMut < [ u8 ] > + for < ' in_out > Extend < & ' in_out u8 > ,
304
+ {
305
+ self . seal_in_place_separate_tag ( aad, in_out. as_mut ( ) )
306
+ . map ( |tag| in_out. extend ( tag. as_ref ( ) ) )
307
+ }
308
+
271
309
/// Encrypts and signs (“seals”) data in place.
272
310
///
273
311
/// `nonce` must be unique for every use of the key to seal data.
@@ -278,49 +316,42 @@ impl<N: NonceSequence> SealingKey<N> {
278
316
/// If there is no AAD then use `Aad::empty()`.
279
317
///
280
318
/// The plaintext is given as the input value of `in_out`. `seal_in_place()`
281
- /// will overwrite the plaintext with the ciphertext and then append the tag
282
- /// using `in_out.extend()`; the tag will be `self.algorithm.tag_len()` bytes
283
- /// long. Common types for `InOut` are `Vec<u8>` or `VecDeque<u8>` from the
284
- /// standard library, or `BytesMut` from the `bytes` crate.
319
+ /// will overwrite the plaintext with the ciphertext and return the tag.
320
+ /// For most protocols, the caller must append the tag to the ciphertext.
321
+ /// The tag will be `self.algorithm.tag_len()` bytes long.
285
322
#[ inline]
286
- pub fn seal_in_place < A , InOut > (
323
+ pub fn seal_in_place_separate_tag < A > (
287
324
& mut self ,
288
325
aad : Aad < A > ,
289
- in_out : & mut InOut ,
290
- ) -> Result < ( ) , error:: Unspecified >
326
+ in_out : & mut [ u8 ] ,
327
+ ) -> Result < Tag , error:: Unspecified >
291
328
where
292
329
A : AsRef < [ u8 ] > ,
293
- InOut : AsMut < [ u8 ] > + for < ' in_out > Extend < & ' in_out u8 > ,
294
330
{
295
- seal_in_place_ ( & self . key , self . nonce_sequence . advance ( ) ?, aad, in_out)
331
+ seal_in_place_separate_tag_ (
332
+ & self . key ,
333
+ self . nonce_sequence . advance ( ) ?,
334
+ Aad :: from ( aad. 0 . as_ref ( ) ) ,
335
+ in_out,
336
+ )
296
337
}
297
338
}
298
339
299
340
#[ inline]
300
- fn seal_in_place_ < A : AsRef < [ u8 ] > , InOut : AsMut < [ u8 ] > + for < ' in_out > Extend < & ' in_out u8 > > (
341
+ fn seal_in_place_separate_tag_ (
301
342
key : & UnboundKey ,
302
343
nonce : Nonce ,
303
- Aad ( aad) : Aad < A > ,
304
- in_out : & mut InOut ,
305
- ) -> Result < ( ) , error:: Unspecified > {
306
- fn seal_in_place (
307
- key : & UnboundKey ,
308
- nonce : Nonce ,
309
- aad : Aad < & [ u8 ] > ,
310
- in_out : & mut [ u8 ] ,
311
- ) -> Result < Tag , error:: Unspecified > {
312
- check_per_nonce_max_bytes ( key. algorithm , in_out. len ( ) ) ?;
313
- Ok ( ( key. algorithm . seal ) (
314
- & key. inner ,
315
- nonce,
316
- aad,
317
- in_out,
318
- key. cpu_features ,
319
- ) )
320
- }
321
- let Tag ( tag) = seal_in_place ( key, nonce, Aad :: from ( aad. as_ref ( ) ) , in_out. as_mut ( ) ) ?;
322
- in_out. extend ( tag. as_ref ( ) ) ;
323
- Ok ( ( ) )
344
+ aad : Aad < & [ u8 ] > ,
345
+ in_out : & mut [ u8 ] ,
346
+ ) -> Result < Tag , error:: Unspecified > {
347
+ check_per_nonce_max_bytes ( key. algorithm , in_out. len ( ) ) ?;
348
+ Ok ( ( key. algorithm . seal ) (
349
+ & key. inner ,
350
+ nonce,
351
+ aad,
352
+ in_out,
353
+ key. cpu_features ,
354
+ ) )
324
355
}
325
356
326
357
/// The additionally authenticated data (AAD) for an opening or sealing
@@ -450,7 +481,8 @@ impl LessSafeKey {
450
481
open_within_ ( & self . key , nonce, aad, in_out, ciphertext_and_tag)
451
482
}
452
483
453
- /// Like [`SealingKey::seal_in_place()`], except it accepts an arbitrary nonce.
484
+ /// Deprecated. Renamed to [`seal_in_place_append_tag()`].
485
+ #[ deprecated( note = "Renamed to `seal_in_place_append_tag`." ) ]
454
486
#[ inline]
455
487
pub fn seal_in_place < A , InOut > (
456
488
& self ,
@@ -462,7 +494,39 @@ impl LessSafeKey {
462
494
A : AsRef < [ u8 ] > ,
463
495
InOut : AsMut < [ u8 ] > + for < ' in_out > Extend < & ' in_out u8 > ,
464
496
{
465
- seal_in_place_ ( & self . key , nonce, aad, in_out)
497
+ self . seal_in_place_append_tag ( nonce, aad, in_out)
498
+ }
499
+
500
+ /// Like [`SealingKey::seal_in_place_append_tag()`], except it accepts an
501
+ /// arbitrary nonce.
502
+ #[ inline]
503
+ pub fn seal_in_place_append_tag < A , InOut > (
504
+ & self ,
505
+ nonce : Nonce ,
506
+ aad : Aad < A > ,
507
+ in_out : & mut InOut ,
508
+ ) -> Result < ( ) , error:: Unspecified >
509
+ where
510
+ A : AsRef < [ u8 ] > ,
511
+ InOut : AsMut < [ u8 ] > + for < ' in_out > Extend < & ' in_out u8 > ,
512
+ {
513
+ self . seal_in_place_separate_tag ( nonce, aad, in_out. as_mut ( ) )
514
+ . map ( |tag| in_out. extend ( tag. as_ref ( ) ) )
515
+ }
516
+
517
+ /// Like `SealingKey::seal_in_place_separate_tag()`, except it accepts an
518
+ /// arbitrary nonce.
519
+ #[ inline]
520
+ pub fn seal_in_place_separate_tag < A > (
521
+ & self ,
522
+ nonce : Nonce ,
523
+ aad : Aad < A > ,
524
+ in_out : & mut [ u8 ] ,
525
+ ) -> Result < Tag , error:: Unspecified >
526
+ where
527
+ A : AsRef < [ u8 ] > ,
528
+ {
529
+ seal_in_place_separate_tag_ ( & self . key , nonce, Aad :: from ( aad. 0 . as_ref ( ) ) , in_out)
466
530
}
467
531
468
532
/// The key's AEAD algorithm.
@@ -557,7 +621,13 @@ impl Eq for Algorithm {}
557
621
/// An authentication tag.
558
622
#[ must_use]
559
623
#[ repr( C ) ]
560
- struct Tag ( Block ) ;
624
+ pub struct Tag ( Block ) ;
625
+
626
+ impl AsRef < [ u8 ] > for Tag {
627
+ fn as_ref ( & self ) -> & [ u8 ] {
628
+ self . 0 . as_ref ( )
629
+ }
630
+ }
561
631
562
632
const MAX_KEY_LEN : usize = 32 ;
563
633
0 commit comments