tss_esapi/structures/attestation/
command_audit_info.rs

1// Copyright 2021 Contributors to the Parsec project.
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::{
5    constants::AlgorithmIdentifier, interface_types::algorithm::HashingAlgorithm,
6    structures::Digest, tss2_esys::TPMS_COMMAND_AUDIT_INFO, Error, Result,
7};
8
9use std::convert::{TryFrom, TryInto};
10
11/// Structure holding the attested data for
12/// TPM2_GetCommandAuditDigest().
13///
14/// # Details
15/// This corresponds to the TPMS_COMMAND_AUDIT_INFO
16#[derive(Debug, Clone)]
17pub struct CommandAuditInfo {
18    audit_counter: u64,
19    hashing_algorithm: HashingAlgorithm,
20    audit_digest: Digest,
21    command_digest: Digest,
22}
23
24impl CommandAuditInfo {
25    /// Returns the audit counter
26    pub const fn audit_counter(&self) -> u64 {
27        self.audit_counter
28    }
29
30    /// Returns the hash algorithm used for the command audit
31    pub const fn hashing_algorithm(&self) -> HashingAlgorithm {
32        self.hashing_algorithm
33    }
34
35    /// Returns the audit digest
36    pub const fn audit_digest(&self) -> &Digest {
37        &self.audit_digest
38    }
39
40    /// Returns the command digest
41    pub const fn command_digest(&self) -> &Digest {
42        &self.command_digest
43    }
44}
45
46impl From<CommandAuditInfo> for TPMS_COMMAND_AUDIT_INFO {
47    fn from(command_audit_info: CommandAuditInfo) -> Self {
48        TPMS_COMMAND_AUDIT_INFO {
49            auditCounter: command_audit_info.audit_counter,
50            digestAlg: AlgorithmIdentifier::from(command_audit_info.hashing_algorithm).into(),
51            auditDigest: command_audit_info.audit_digest.into(),
52            commandDigest: command_audit_info.command_digest.into(),
53        }
54    }
55}
56
57impl TryFrom<TPMS_COMMAND_AUDIT_INFO> for CommandAuditInfo {
58    type Error = Error;
59
60    fn try_from(tpms_command_audit_info: TPMS_COMMAND_AUDIT_INFO) -> Result<Self> {
61        Ok(CommandAuditInfo {
62            audit_counter: tpms_command_audit_info.auditCounter,
63            hashing_algorithm: AlgorithmIdentifier::try_from(tpms_command_audit_info.digestAlg)?
64                .try_into()?,
65            audit_digest: tpms_command_audit_info.auditDigest.try_into()?,
66            command_digest: tpms_command_audit_info.commandDigest.try_into()?,
67        })
68    }
69}