@@ -49,13 +49,22 @@ pub trait VariableAead: AeadCore {
4949 & self ,
5050 nonce : & [ u8 ] ,
5151 aad : & [ u8 ] ,
52- buf : InOutBuf < ' _ , ' _ , u8 > ,
52+ mut buf : InOutBuf < ' _ , ' _ , u8 > ,
5353 tag_dst : & mut [ u8 ] ,
5454 ) -> Result < ( ) > {
55- let nonce = nonce. try_into ( ) . map_err ( |_| Error ) ?;
56- let tag_dst: & mut Tag < Self > = tag_dst. try_into ( ) . map_err ( |_| Error ) ?;
57- * tag_dst = self . encrypt_inout_detached ( nonce, aad, buf) ?;
58- Ok ( ( ) )
55+ match ( nonce. try_into ( ) , tag_dst. try_into ( ) ) {
56+ ( Ok ( nonce) , Ok ( tag_dst) ) => {
57+ let tag_dst: & mut Tag < Self > = tag_dst;
58+ self . encrypt_inout_detached ( nonce, aad, buf)
59+ . map ( |tag| * tag_dst = tag)
60+ . inspect_err ( |_| tag_dst. fill ( 0 ) )
61+ }
62+ _ => {
63+ buf. get_out ( ) . fill ( 0 ) ;
64+ tag_dst. fill ( 0 ) ;
65+ Err ( Error )
66+ }
67+ }
5968 }
6069
6170 /// Decrypt the data in the provided [`InOutBuf`] with variable nonce and tag sizes,
@@ -71,12 +80,16 @@ pub trait VariableAead: AeadCore {
7180 & self ,
7281 nonce : & [ u8 ] ,
7382 aad : & [ u8 ] ,
74- buf : InOutBuf < ' _ , ' _ , u8 > ,
83+ mut buf : InOutBuf < ' _ , ' _ , u8 > ,
7584 tag : & [ u8 ] ,
7685 ) -> Result < ( ) > {
77- let nonce = nonce. try_into ( ) . map_err ( |_| Error ) ?;
78- let tag = tag. try_into ( ) . map_err ( |_| Error ) ?;
79- self . decrypt_inout_detached ( nonce, aad, buf, tag)
86+ match ( nonce. try_into ( ) , tag. try_into ( ) ) {
87+ ( Ok ( nonce) , Ok ( tag) ) => self . decrypt_inout_detached ( nonce, aad, buf, tag) ,
88+ _ => {
89+ buf. get_out ( ) . fill ( 0 ) ;
90+ Err ( Error )
91+ }
92+ }
8093 }
8194
8295 /// Encrypt the data in-place in the provided buffer with variable nonce and tag sizes,
@@ -225,8 +238,6 @@ pub trait VariableAead: AeadCore {
225238 } ;
226239
227240 self . variable_encrypt_inout_detached ( nonce, aad, pt. into ( ) , tag_dst)
228- // On failure the `pt` part should be zeroized by the encrypt function
229- . inspect_err ( |_| tag_dst. fill ( 0 ) )
230241 }
231242
232243 /// Decrypt data in `buf` with variable nonce and tag sizes
0 commit comments