@@ -12,27 +12,27 @@ use aead::{
1212use ring:: aead:: {
1313 Aad , LessSafeKey as Key , Nonce , UnboundKey , AES_128_GCM , AES_256_GCM , CHACHA20_POLY1305 ,
1414} ;
15- use zeroize:: Zeroize ;
1615
1716/// Authentication tags
1817pub type Tag = GenericArray < u8 , U16 > ;
1918
2019/// AES-GCM with a 128-bit key
21- pub struct Aes128Gcm ( GenericArray < u8 , U16 > ) ;
20+ pub struct Aes128Gcm ( Cipher ) ;
2221
2322/// AES-GCM with a 256-bit key
24- pub struct Aes256Gcm ( GenericArray < u8 , U32 > ) ;
23+ pub struct Aes256Gcm ( Cipher ) ;
2524
2625/// ChaCha20Poly1305
27- pub struct ChaCha20Poly1305 ( GenericArray < u8 , U32 > ) ;
26+ pub struct ChaCha20Poly1305 ( Cipher ) ;
2827
2928macro_rules! impl_aead {
3029 ( $cipher: ty, $algorithm: expr, $key_size: ty) => {
3130 impl NewAead for $cipher {
3231 type KeySize = $key_size;
3332
3433 fn new( key: & GenericArray <u8 , Self :: KeySize >) -> Self {
35- Self ( * key)
34+ let key = UnboundKey :: new( & $algorithm, key. as_slice( ) ) . unwrap( ) ;
35+ Self ( Cipher :: new( key) )
3636 }
3737 }
3838
@@ -49,12 +49,8 @@ macro_rules! impl_aead {
4949 associated_data: & [ u8 ] ,
5050 buffer: & mut [ u8 ] ,
5151 ) -> Result <Tag , Error > {
52- let key = UnboundKey :: new( & $algorithm, self . 0 . as_slice( ) ) . unwrap( ) ;
53- Cipher :: new( key) . encrypt_in_place_detached(
54- nonce. as_slice( ) ,
55- associated_data,
56- buffer,
57- )
52+ self . 0
53+ . encrypt_in_place_detached( nonce. as_slice( ) , associated_data, buffer)
5854 }
5955
6056 fn decrypt_in_place(
@@ -63,8 +59,8 @@ macro_rules! impl_aead {
6359 associated_data: & [ u8 ] ,
6460 buffer: & mut dyn Buffer ,
6561 ) -> Result <( ) , Error > {
66- let key = UnboundKey :: new ( & $algorithm , self . 0 . as_slice ( ) ) . unwrap ( ) ;
67- Cipher :: new ( key ) . decrypt_in_place( nonce. as_slice( ) , associated_data, buffer)
62+ self . 0
63+ . decrypt_in_place( nonce. as_slice( ) , associated_data, buffer)
6864 }
6965
7066 fn decrypt_in_place_detached(
@@ -77,12 +73,6 @@ macro_rules! impl_aead {
7773 unimplemented!( ) ; // ring does not allow us to implement this API
7874 }
7975 }
80-
81- impl Drop for $cipher {
82- fn drop( & mut self ) {
83- self . 0 . zeroize( ) ;
84- }
85- }
8676 } ;
8777}
8878
0 commit comments