use super::psa_key_attributes::Attributes;
use crate::operations::psa_algorithm::Cipher;
use crate::requests::ResponseStatus;
use derivative::Derivative;
#[derive(Derivative)]
#[derivative(Debug)]
pub struct Operation {
pub key_name: String,
pub alg: Cipher,
#[derivative(Debug = "ignore")]
pub plaintext: zeroize::Zeroizing<Vec<u8>>,
}
impl Operation {
pub fn validate(&self, key_attributes: Attributes) -> crate::requests::Result<()> {
key_attributes.can_encrypt_message()?;
key_attributes.permits_alg(self.alg.into())?;
key_attributes.compatible_with_alg(self.alg.into())?;
if self.plaintext.is_empty() {
return Err(ResponseStatus::PsaErrorInvalidArgument);
}
Ok(())
}
}
#[derive(Derivative)]
#[derivative(Debug)]
pub struct Result {
#[derivative(Debug = "ignore")]
pub ciphertext: zeroize::Zeroizing<Vec<u8>>,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::operations::psa_algorithm::Cipher;
use crate::operations::psa_key_attributes::{Lifetime, Policy, Type, UsageFlags};
fn get_attrs() -> Attributes {
let mut usage_flags = UsageFlags::default();
let _ = usage_flags.set_encrypt();
Attributes {
lifetime: Lifetime::Persistent,
key_type: Type::Arc4,
bits: 256,
policy: Policy {
usage_flags,
permitted_algorithms: Cipher::StreamCipher.into(),
},
}
}
#[test]
fn validate_success() {
(Operation {
key_name: String::from("some key"),
alg: Cipher::StreamCipher,
plaintext: vec![0xff, 32].into(),
})
.validate(get_attrs())
.unwrap();
}
#[test]
fn cannot_encrypt() {
let mut attrs = get_attrs();
attrs.policy.usage_flags = UsageFlags::default();
assert_eq!(
(Operation {
key_name: String::from("some key"),
alg: Cipher::StreamCipher,
plaintext: vec![0xff, 32].into(),
})
.validate(attrs)
.unwrap_err(),
ResponseStatus::PsaErrorNotPermitted
);
}
#[test]
fn wrong_algorithm() {
assert_eq!(
(Operation {
key_name: String::from("some key"),
alg: Cipher::Cfb,
plaintext: vec![0xff, 32].into(),
})
.validate(get_attrs())
.unwrap_err(),
ResponseStatus::PsaErrorNotPermitted
);
}
#[test]
fn invalid_plaintext() {
assert_eq!(
(Operation {
key_name: String::from("some key"),
alg: Cipher::StreamCipher,
plaintext: vec![].into(),
})
.validate(get_attrs())
.unwrap_err(),
ResponseStatus::PsaErrorInvalidArgument
);
}
}