22
22
//! [`crypto.cipher.AEAD`]: https://golang.org/pkg/crypto/cipher/#AEAD
23
23
24
24
use self :: block:: { Block , BLOCK_LEN } ;
25
- use crate :: { constant_time, cpu, error, hkdf, polyfill, rand } ;
25
+ use crate :: { constant_time, cpu, error, hkdf, polyfill} ;
26
26
use core:: ops:: RangeFrom ;
27
27
28
28
pub use self :: {
@@ -445,58 +445,18 @@ impl hkdf::KeyType for &'static Algorithm {
445
445
}
446
446
}
447
447
448
- mod sealed {
449
- pub trait NonceGeneration {
450
- const VALUE : Self ;
451
- }
452
- }
453
-
454
- /// A nonce generation strategy for `LesssSafeKey`; either `ExplicitNonces` or
455
- /// `RandomNonces`.
456
- pub trait NonceGeneration : sealed:: NonceGeneration { }
457
- impl < T > NonceGeneration for T where T : sealed:: NonceGeneration { }
458
-
459
- /// A nonce generation strategy where random nonces are used.
460
- ///
461
- /// These algorithms are *NOT* nonce-misuse-resistant. They use nonces small
462
- /// enough where birthday collisions need to be considered, so even ensuring
463
- /// perfectly-random generation of nonces isn't sufficient to prevent nonce
464
- /// reuse.
465
- pub struct RandomNonces ( ( ) ) ;
466
- impl sealed:: NonceGeneration for RandomNonces {
467
- const VALUE : Self = Self ( ( ) ) ;
468
- }
469
-
470
- /// A nonce generation strategy where explicitly-constructed nonces are used.
471
- pub struct ExplicitNonces ( ( ) ) ;
472
- impl sealed:: NonceGeneration for ExplicitNonces {
473
- const VALUE : Self = Self ( ( ) ) ;
474
- }
475
-
476
448
/// Immutable keys for use in situations where `OpeningKey`/`SealingKey` and
477
449
/// `NonceSequence` cannot reasonably be used.
478
450
///
479
- /// These algorithms are *NOT* nonce-misuse-resistant. They use nonces small
480
- /// enough where the likelihood of collisions must be carefully considered.
481
- pub struct LessSafeKey < N = ExplicitNonces >
482
- where
483
- N : NonceGeneration ,
484
- {
451
+ /// Prefer to use `OpeningKey`/`SealingKey` and `NonceSequence` when practical.
452
+ pub struct LessSafeKey {
485
453
key : UnboundKey ,
486
- _nonce_generation : N ,
487
454
}
488
455
489
- impl < N > LessSafeKey < N >
490
- where
491
- N : NonceGeneration ,
492
- {
456
+ impl LessSafeKey {
493
457
/// Constructs a `LessSafeKey` from an `UnboundKey`.
494
- #[ inline]
495
458
pub fn new ( key : UnboundKey ) -> Self {
496
- Self {
497
- key,
498
- _nonce_generation : N :: VALUE ,
499
- }
459
+ Self { key }
500
460
}
501
461
502
462
/// Like [`OpeningKey::open_in_place()`], except it accepts an arbitrary nonce.
@@ -532,14 +492,6 @@ where
532
492
open_within_ ( & self . key , nonce, aad, in_out, ciphertext_and_tag)
533
493
}
534
494
535
- /// The key's AEAD algorithm.
536
- #[ inline]
537
- pub fn algorithm ( & self ) -> & ' static Algorithm {
538
- & self . key . algorithm
539
- }
540
- }
541
-
542
- impl LessSafeKey < ExplicitNonces > {
543
495
/// Deprecated. Renamed to [`seal_in_place_append_tag()`].
544
496
#[ deprecated( note = "Renamed to `seal_in_place_append_tag`." ) ]
545
497
#[ inline]
@@ -571,9 +523,6 @@ impl LessSafeKey<ExplicitNonces> {
571
523
A : AsRef < [ u8 ] > ,
572
524
InOut : AsMut < [ u8 ] > + for < ' in_out > Extend < & ' in_out u8 > ,
573
525
{
574
- // Overwrite the plaintext with the ciphertext before extending `in_out`
575
- // so that if the `extend()` causes a reallocation, the ciphertext (not
576
- // the plaintext) will be in the old deallocated buffer.
577
526
self . seal_in_place_separate_tag ( nonce, aad, in_out. as_mut ( ) )
578
527
. map ( |tag| in_out. extend ( tag. as_ref ( ) ) )
579
528
}
@@ -594,63 +543,15 @@ impl LessSafeKey<ExplicitNonces> {
594
543
{
595
544
seal_in_place_separate_tag_ ( & self . key , nonce, Aad :: from ( aad. as_ref ( ) ) , in_out)
596
545
}
597
- }
598
546
599
- impl LessSafeKey < RandomNonces > {
600
- /// Like [`SealingKey::seal_in_place_append_tag()`], except the nonce is
601
- /// randomly generated.
602
- ///
603
- /// The randomly-generated nonce is returned on success.
604
- #[ inline]
605
- pub fn seal_with_random_nonce_in_place_append_tag < A , InOut > (
606
- & self ,
607
- aad : Aad < A > ,
608
- in_out : & mut InOut ,
609
- rng : & dyn rand:: SecureRandom ,
610
- ) -> Result < Nonce , error:: Unspecified >
611
- where
612
- A : AsRef < [ u8 ] > ,
613
- InOut : AsMut < [ u8 ] > + for < ' in_out > Extend < & ' in_out u8 > ,
614
- {
615
- // Overwrite the plaintext with the ciphertext before extending `in_out`
616
- // so that if the `extend()` causes a reallocation, the ciphertext (not
617
- // the plaintext) will be in the old deallocated buffer.
618
- self . seal_with_random_nonce_in_place_separate_tag ( aad, in_out. as_mut ( ) , rng)
619
- . map ( |( nonce, tag) | {
620
- in_out. extend ( tag. as_ref ( ) ) ;
621
- nonce
622
- } )
623
- }
624
-
625
- /// Like [`SealingKey::seal_in_place_separate_tag()`], except the nonce is
626
- /// randomly generated.
627
- ///
628
- /// The randomly-generated nonce and the tag are returned on success.
547
+ /// The key's AEAD algorithm.
629
548
#[ inline]
630
- pub fn seal_with_random_nonce_in_place_separate_tag < A > (
631
- & self ,
632
- aad : Aad < A > ,
633
- in_out : & mut [ u8 ] ,
634
- rng : & dyn rand:: SecureRandom ,
635
- ) -> Result < ( Nonce , Tag ) , error:: Unspecified >
636
- where
637
- A : AsRef < [ u8 ] > ,
638
- {
639
- let nonce = Nonce :: assume_unique_for_key ( rand:: generate ( rng) ?. expose ( ) ) ;
640
- seal_in_place_separate_tag_ (
641
- & self . key ,
642
- Nonce :: assume_unique_for_key ( * nonce. as_ref ( ) ) ,
643
- Aad :: from ( aad. as_ref ( ) ) ,
644
- in_out,
645
- )
646
- . map ( |tag| ( nonce, tag) )
549
+ pub fn algorithm ( & self ) -> & ' static Algorithm {
550
+ & self . key . algorithm
647
551
}
648
552
}
649
553
650
- impl < N > core:: fmt:: Debug for LessSafeKey < N >
651
- where
652
- N : NonceGeneration ,
653
- {
554
+ impl core:: fmt:: Debug for LessSafeKey {
654
555
fn fmt ( & self , f : & mut core:: fmt:: Formatter ) -> Result < ( ) , core:: fmt:: Error > {
655
556
f. debug_struct ( "LessSafeKey" )
656
557
. field ( "algorithm" , self . algorithm ( ) )
0 commit comments