Skip to content

Commit 7a82df0

Browse files
committed
test zeroization on failure
1 parent cabc81c commit 7a82df0

2 files changed

Lines changed: 49 additions & 14 deletions

File tree

aead/src/dev.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ pub fn pass_test<C: Aead + KeyInit>(
5050
return Err("decrypted data is different from target plaintext");
5151
}
5252

53+
let mut buf = ciphertext.to_vec();
54+
55+
// Flip one bit
56+
buf[0] ^= 1;
57+
58+
let res = cipher.decrypt_within_vec(nonce, aad, &mut buf);
59+
60+
if res.is_ok() {
61+
return Err("did not detect corrupted ciphertext");
62+
}
63+
if buf.iter().any(|&b| b != 0) {
64+
return Err("the buffer was not zeroized after failure");
65+
}
66+
5367
Ok(())
5468
}
5569

@@ -71,10 +85,20 @@ pub fn fail_test<C: Aead + KeyInit>(
7185

7286
let res = cipher.decrypt_into_vec(nonce, aad, ciphertext);
7387
if res.is_ok() {
74-
Err("decryption must return error")
75-
} else {
76-
Ok(())
88+
return Err("decryption must return error");
7789
}
90+
91+
let mut buf = ciphertext.to_vec();
92+
let res = cipher.decrypt_within_vec(nonce, aad, &mut buf);
93+
94+
if res.is_ok() {
95+
return Err("decryption must return error");
96+
}
97+
if buf.iter().any(|&b| b != 0) {
98+
return Err("the buffer was not zeroized after failure");
99+
}
100+
101+
Ok(())
78102
}
79103

80104
/// Define AEAD test for passing test vectors

aead/src/variable.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)