@@ -512,6 +512,12 @@ impl Aead {
512512 } )
513513 }
514514
515+ fn make_nonce ( nonce : & mut [ u8 ; NONCE_LEN ] , seq : SequenceNumber ) {
516+ for ( n, & s) in nonce[ NONCE_LEN - COUNTER_LEN ..] . iter_mut ( ) . zip ( & seq. to_be_bytes ( ) ) {
517+ * n ^= s;
518+ }
519+ }
520+
515521 pub fn import_key ( algorithm : AeadAlgorithms , key : & [ u8 ] ) -> Result < SymKey , Error > {
516522 let slot = p11:: Slot :: internal ( ) . map_err ( |_| Error :: Internal ) ?;
517523
@@ -590,10 +596,10 @@ impl Aead {
590596 Ok ( ct)
591597 }
592598
593- /// Encrypt with an explicit sequence number. Mirrors `open `'s nonce
599+ /// Encrypt with an explicit sequence number. Mirrors `decrypt `'s nonce
594600 /// construction: the final nonce is `nonce_base XOR encode_be(seq)` over
595601 /// the trailing 8 bytes. The NSS PKCS#11 context's internal counter is
596- /// not used (CKG_NO_GENERATE). The caller must never reuse
602+ /// not used (` CKG_NO_GENERATE` ). The caller must never reuse
597603 /// `(nonce_base, seq)` with the same key.
598604 pub fn encrypt_with_seq (
599605 & mut self ,
@@ -605,9 +611,7 @@ impl Aead {
605611
606612 assert_eq ! ( self . mode, Mode :: Encrypt ) ;
607613 let mut nonce = self . nonce_base ;
608- for ( i, n) in nonce. iter_mut ( ) . rev ( ) . take ( COUNTER_LEN ) . enumerate ( ) {
609- * n ^= u8:: try_from ( ( seq >> ( 8 * i) ) & 0xff ) . unwrap ( ) ;
610- }
614+ Aead :: make_nonce ( & mut nonce, seq) ;
611615 let mut ct = vec ! [ 0 ; pt. len( ) + TAG_LEN ] ;
612616 let mut ct_len: c_int = 0 ;
613617 let mut tag = vec ! [ 0 ; TAG_LEN ] ;
@@ -621,15 +625,15 @@ impl Aead {
621625 aad. as_ptr ( ) ,
622626 c_int_len ( aad. len ( ) ) ?,
623627 ct. as_mut_ptr ( ) ,
624- & mut ct_len,
628+ & raw mut ct_len,
625629 c_int_len ( ct. len ( ) ) ?,
626630 tag. as_mut_ptr ( ) ,
627631 c_int_len ( tag. len ( ) ) ?,
628632 pt. as_ptr ( ) ,
629633 c_int_len ( pt. len ( ) ) ?,
630634 )
631635 } ) ?;
632- ct. truncate ( usize:: try_from ( ct_len) . unwrap ( ) ) ;
636+ ct. truncate ( usize:: try_from ( ct_len) . map_err ( |_| Error :: IntegerOverflow ) ? ) ;
633637 debug_assert_eq ! ( ct. len( ) , pt. len( ) ) ;
634638 ct. append ( & mut tag) ;
635639 Ok ( ct)
@@ -645,9 +649,7 @@ impl Aead {
645649
646650 assert_eq ! ( self . mode, Mode :: Decrypt ) ;
647651 let mut nonce = self . nonce_base ;
648- for ( i, n) in nonce. iter_mut ( ) . rev ( ) . take ( COUNTER_LEN ) . enumerate ( ) {
649- * n ^= u8:: try_from ( ( seq >> ( 8 * i) ) & 0xff ) . map_err ( |_| Error :: IntegerOverflow ) ?;
650- }
652+ Aead :: make_nonce ( & mut nonce, seq) ;
651653 let mut pt = vec ! [ 0 ; ct. len( ) ] ; // NSS needs more space than it uses for plaintext.
652654 let mut pt_len: c_int = 0 ;
653655 let pt_expected = ct. len ( ) . checked_sub ( TAG_LEN ) . ok_or ( Error :: AeadTruncated ) ?;
@@ -813,7 +815,7 @@ mod test {
813815 decrypt ( ALG , KEY , NONCE_BASE , 654_360_564 , AAD , PT , CT ) ;
814816 }
815817
816- fn roundtrip_seal_with_seq ( algorithm : AeadAlgorithms , key : & [ u8 ] ) {
818+ fn roundtrip_encrypt_with_seq ( algorithm : AeadAlgorithms , key : & [ u8 ] ) {
817819 fixture_init ( ) ;
818820
819821 const NONCE_BASE : [ u8 ; NONCE_LEN ] = [ 0 ; NONCE_LEN ] ;
@@ -831,14 +833,20 @@ mod test {
831833 }
832834
833835 #[ test]
834- fn seal_with_seq_aes128gcm ( ) {
836+ fn encrypt_with_seq_aes128gcm ( ) {
835837 const KEY : & [ u8 ] = & [ 0x42 ; 16 ] ;
836- roundtrip_seal_with_seq ( AeadAlgorithms :: Aes128Gcm , KEY ) ;
838+ roundtrip_encrypt_with_seq ( AeadAlgorithms :: Aes128Gcm , KEY ) ;
839+ }
840+
841+ #[ test]
842+ fn encrypt_with_seq_aes256gcm ( ) {
843+ const KEY : & [ u8 ] = & [ 0x42 ; 32 ] ;
844+ roundtrip_encrypt_with_seq ( AeadAlgorithms :: Aes256Gcm , KEY ) ;
837845 }
838846
839847 #[ test]
840- fn seal_with_seq_aes256gcm ( ) {
848+ fn seal_with_seq_chacha20poly1305 ( ) {
841849 const KEY : & [ u8 ] = & [ 0x42 ; 32 ] ;
842- roundtrip_seal_with_seq ( AeadAlgorithms :: Aes256Gcm , KEY ) ;
850+ roundtrip_encrypt_with_seq ( AeadAlgorithms :: ChaCha20Poly1305 , KEY ) ;
843851 }
844852}
0 commit comments