use super::hmac::HMACTag;
use super::{Command, Response};
use session::SessionErrorKind::ResponseError;
use {Adapter, CommandType, ObjectId, Session, SessionError};
pub fn verify_hmac<A, D, T>(
session: &mut Session<A>,
key_id: ObjectId,
data: D,
tag: T,
) -> Result<(), SessionError>
where
A: Adapter,
D: Into<Vec<u8>>,
T: Into<HMACTag>,
{
let result = session.send_command(VerifyHMACCommand {
key_id,
tag: tag.into(),
data: data.into(),
})?;
if result.0 == 1 {
Ok(())
} else {
Err(err!(ResponseError, "HMAC verification failure"))
}
}
#[derive(Serialize, Deserialize, Debug)]
pub(crate) struct VerifyHMACCommand {
pub key_id: ObjectId,
pub tag: HMACTag,
pub data: Vec<u8>,
}
impl Command for VerifyHMACCommand {
type ResponseType = VerifyHMACResponse;
}
#[derive(Serialize, Deserialize, Debug)]
pub(crate) struct VerifyHMACResponse(pub(crate) u8);
impl Response for VerifyHMACResponse {
const COMMAND_TYPE: CommandType = CommandType::VerifyHMAC;
}