Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/mechanisms/aead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl<
const KEY_LEN: usize = T::KeySize::USIZE;
const NONCE_LEN: usize = T::NonceSize::USIZE;
const TOTAL_LEN: usize = KeyNonceSize::USIZE;
const TAG_LEN: usize = T::TagSize::USIZE;

const KIND: key::Kind = key::Kind::Symmetric(Self::KEY_LEN);
const KIND_NONCE: key::Kind = key::Kind::Symmetric32Nonce(Self::NONCE_LEN);
Expand Down Expand Up @@ -76,7 +77,13 @@ impl<
let mut aead = T::new(&GenericArray::clone_from_slice(symmetric_key));

let mut plaintext = request.message.clone();
if request.nonce.len() != Self::NONCE_LEN {
return Err(Error::MechanismParamInvalid);
}
let nonce = GenericArray::from_slice(&request.nonce);
if request.tag.len() != Self::TAG_LEN {
return Err(Error::MechanismParamInvalid);
}
let tag = GenericArray::from_slice(&request.tag);

let outcome =
Expand Down
44 changes: 44 additions & 0 deletions tests/aead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,47 @@ fn test_invalid_key_size() {
}
});
}

#[test]
fn test_encrypt_bad_nonce_length() {
client::get(|client| {
for mechanism in MECHANISMS {
let key = syscall!(client.generate_secret_key(32, Location::Volatile)).key;
let result =
try_syscall!(client.encrypt(*mechanism, key, &[], &[], Some(b"nonce".into())));
assert_eq!(result, Err(Error::MechanismParamInvalid));
}
})
}

#[test]
fn test_decrypt_bad_lengths() {
client::get(|client| {
for mechanism in MECHANISMS {
let key = syscall!(client.generate_secret_key(32, Location::Volatile)).key;
let encrypted = syscall!(client.encrypt(*mechanism, key, &[], &[], None));

// bad nonce length
let result = try_syscall!(client.decrypt(
*mechanism,
key,
&encrypted.ciphertext,
&[],
b"nonce",
&encrypted.tag
));
assert_eq!(result, Err(Error::MechanismParamInvalid));

// bad tag length
let result = try_syscall!(client.decrypt(
*mechanism,
key,
&encrypted.ciphertext,
&[],
&encrypted.nonce,
b"tag"
));
assert_eq!(result, Err(Error::MechanismParamInvalid));
}
})
}
Loading